Darren
Darren

Reputation: 13

How to programatically add a tracepoint for Visual Studio?

I am looking for a method to monitor a running program that I have the source code. Basically, when the user runs it, I need to know what functions and parameter is called sequentially.

I can write a trace output code to all functions to achieve this. However, I am not allowed to modify the source code. I found out that Tracepoint in Visual Studio 2005 allows me to do this - output log info without modifying the source. But I need to add them to all functions.

As I have thousands of files and functions, I need to find a way to programatically do this. I found out about DTE.Debugger.Breakpoints.Add that able to add a breakpoint. However, I couldnt find any way for tracepoint. Or where is the breakpoint info for a project stored? I couldnt find it in sln or vcproj. Or is there a way to convert breakpoint to tracepoint programatically? I see that I can change it manually by changing the "When Hit" property dialog.

Thanks!

Upvotes: 1

Views: 1752

Answers (5)

anvish
anvish

Reputation: 515

You should cast your breakpoints to EnvDTE80.Breakpoint2. Then you'll be able to use

breakpoint.BreakWhenHit = false; 
breakpoint.Macro = "YourMacro";

Upvotes: 4

Omer Raviv
Omer Raviv

Reputation: 11827

I think this is the solution... this macro adds a breakpoint the Main method of your program, and then turns all breakpoints into tracepoints.

Sub AddBreakpointToMain()
     Dim bp As EnvDTE80.Breakpoint2
     Dim bps As EnvDTE.Breakpoints


     bps = DTE.Debugger.Breakpoints.Add("Main")
     For Each bp In bps
         bp.Tag = "SetByMacro"
         bp.BreakWhenHit = False
         bp.Message = "Hi, this is your tracepoint calling."
     Next
End Sub

Upvotes: 1

leoger
leoger

Reputation: 1074

First part of the solution:

DTE.ExecuteCommand("EditorContextMenus.CodeWindow.Breakpoint.InsertTracepoint")

It opens the TP window for the line where the cursor was. You will still have to hit Return to select OK, though. Enough for my needs--at least you don't have to right-click, etc.

Upvotes: 0

MrTelly
MrTelly

Reputation: 14875

You could also look at Aspect Oriented coding. By my understanding this will change the compiled assembly controlled by attributes and is typically used to add tracing to all methods/properties.

How can I add a Trace() to every method call in C#?

Upvotes: 0

David Brown
David Brown

Reputation: 36239

A .NET profiler will allow you to see which methods are executed and how long each takes without modifying the source code. It basically injects special code into the compiled assembly.

Upvotes: 1

Related Questions