How to programatically add a tracepoint for Visual

2019-08-09 04:06发布

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!

5条回答
放我归山
2楼-- · 2019-08-09 04:37

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.

查看更多
Root(大扎)
3楼-- · 2019-08-09 04:38

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
查看更多
We Are One
4楼-- · 2019-08-09 04:39

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.

查看更多
Root(大扎)
5楼-- · 2019-08-09 04:42

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

breakpoint.BreakWhenHit = false; 
breakpoint.Macro = "YourMacro";
查看更多
爱情/是我丢掉的垃圾
6楼-- · 2019-08-09 04:55

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#?

查看更多
登录 后发表回答