How do I set a breakpoint on a DLL in Visual Studio?

I am studying the source code of a DLL (written in C ++), which is a plugin for another program. There is no documentation there and I have no source code for the main program. I am trying to figure out where and when the main program calls the DLL. There are over a hundred functions labeled as DllExport

, so this alone doesn't help much.

To that end, it would be nice if I could trigger a breakpoint every time I execute code in my DLL. This or write it down somewhere. Is this possible and how?

+3


source to share


1 answer


Fu, this is a pretty necessary requirement. But honestly, it might be something really useful, even for other scenarios.

The easiest way I think you could achieve this is using WinDbg and set breakpoints there. With WinDbg, you can do something like this (assuming your image has a name myplugin.dll

):

  • Open WinDbg
  • File -> Symbol File Path (add path where the .pdb for the plugin is located)
  • File -> Source File Path (add the path where the sources for the plugin are)
  • File -> Image File Path (add the path where the plugin dll itself is)
  • File -> Open executable file (or connect to an existing process, select the main application)
  • On the command line: bm myplugin!*

  • Run the program (F5)

Step 6 will add a breakpoint for each character in myplugin.dll

. But be careful: this will indeed add a breakpoint for every function, even in functions that you may not have defined yourself, but are included via headers (like the STL). This is the most drastic way to set breakpoints in your module. If the plugin is programmed to have all exported functions in a namespace or prefix, you can trim the search pattern and get more precise breakpoints. Suppose all these functions you are interested in are prefixed PLG_

, then you should write:

bm myplugin!PLG_*

      

to make each breakpoint match - pretty straight forward.



Of course, the downside is the use of an external debugger. Visual Studio does support Breakpoints features that work similarly, but as far as I know, they either don't work well with wildcards in every case, or they changed it in VS2013 +, or one of them.

This was the simplest approach I know (not that it doesn't exist, but I just don't know). The second is the easiest one: add breakpoints manually once and save the .suo file in case you need breakpoints later This is a cumbersome task, but finding a faster solution can take longer than doing it manually (invoicing on a workday).

The other was that you write a program that adds a trampoline at the very beginning of the exported functions (read the PE header and get the RVA functions) with int 3

and the original instruction, but that's too much - you'd easily put breakpoints on 1000 functions manually before than you do it.

Another way is to modify the .suo Solution Options file that stores the breakpoints. But this again, as far as I know, is only available through the Visual Studio Extensibility API, which means you'll have to write a plugin or macro for Visual Studio itself.

I am also very interested in this. So I will be watching this question for a while, maybe there is a much simpler solution that someone knows about. If you are new to WinDbg just add a comment and I will try to explain how you get it and how it works :)

+2


source







All Articles