break whenever a file (or class) is entere

2019-01-31 03:55发布

In Visual Studio, is there any way to make the debugger break whenever a certain file (or class) is entered? Please don't answer "just set a breakpoint at the beginning of every method" :)

I am using C#.

23条回答
\"骚年 ilove
2楼-- · 2019-01-31 04:36

You can use Debugger.Launch() and Debugger.Break() in the assembly System.Diagnostics

查看更多
不美不萌又怎样
3楼-- · 2019-01-31 04:38

No. Or rather, yes, but it involves setting a breakpoint at the beginning of every method.

查看更多
疯言疯语
4楼-- · 2019-01-31 04:39

Well, as everyone is saying, it involves setting a breakpoint at the beginning of every method. But you're not seeing the bigger picture.

For this to work at all, a breakpoint has to be set at the beginning of every method. Whether you do it manually, or the debugger does it automatically, those breakpoints must be set for this to work.

So, the question really becomes, "If there enough of a need for this functionality, that it is worth building into the debugger an automatic means of setting all those breakpoints?". And the answer is, "Not Really".

查看更多
等我变得足够好
5楼-- · 2019-01-31 04:40

IF this is C++ you are talking about, then you could probably get away with, (a hell of a lot of work) setting a break point in the preamble code in the CRT, or writing code that modifies the preamble code to stick INT 3's in there only for functions generated from the class in question... This, BTW, CAN be done at runtime... You'd have to have the PE file that's generated modify itself, possibly before relocation, to stick all the break's in there...

My only other suggestion would be to write a Macro that uses the predefined macro __FUNCTION__, in which you look for any function that's part of the class in question, and if necessary, stick a

__asm { int 3 }

in your macro to make VS break... This will prevent you from having to set break points at the start of every function, but you'd still have to stick a macro call, which is a lot better, if you ask me. I think I read somewhere on how you can define, or redefine the preamble code that's called per function.. I'll see what I can find.

I would think I similar hack could be used to detect which FILE you enter, but you STILL have to place YOUR function macro's all over your code, or it will never get called, and, well, that's pretty much what you didn't want to do.

查看更多
Melony?
6楼-- · 2019-01-31 04:41

Files have no existence at runtime (consider that partial classes are no different -- in terms of code -- from putting everything in a single file). Therefore a macro approach (or code in every method) is required.

To do the same with a type (which does exist at runtime) may be able to be done, but likely to be highly intrusive, creating more potential for heisenbugs. The "easiest" route to this is likely to be making use of .NET remoting's proxy infrastructure (see MOQ's implementation for an example of using transparent proxy).

Summary: use a macro, or select all followed by set breakpoint (ctrl-A, F9).

查看更多
Animai°情兽
7楼-- · 2019-01-31 04:42

If you are willing to use a macro then the accepted answer from this question

Should be trivially convertible to you needs by making the search function searching for methods, properties and constructors (as desired), there is also quite possibly a way to get the same information from the the ide/symbols which will be more stable (though perhaps a little more complex).

查看更多
登录 后发表回答