简体   繁体   English

如何在Visual Studio中的DLL上设置断点?

[英]How to set a breakpoint on a DLL in Visual Studio?

I'm studying the source code of a DLL (written in C++) which is a plugin for another program. 我正在研究DLL(用C ++编写)的源代码,该DLL是另一个程序的插件。 There's no documentation, and I don't have the source code of the main program. 没有文档,而且我没有主程序的源代码。 I'm trying to figure out where and when the main program calls into the DLL. 我试图弄清楚主程序在何时何处调用DLL。 There's over a hundred functions marked as DllExport , so that alone isn't much help. 有100多个标记为DllExport函数,因此仅此一项并没有多大帮助。

To this end then it would be nice if I could trigger a breakpoint every time execution reaches code in my DLL. 为此,如果每次执行到达DLL中的代码时都可以触发一个断点,那就太好了。 That or log it somewhere. 那或记录在某个地方。 Is this possible and how? 这有可能吗?

Phew, that is quite a requirement you have. ew,这是您的一项相当要求。 But to be honest, that might be something really usefull, even for other scenarios. 但老实说,即使对于其他情况,这也可能确实有用。

The most easiest way I think you could achieve this is using WinDbg and setting breakpoints there. 我认为可以实现此目的的最简单方法是使用WinDbg并在那里设置断点。 With WinDbg you could do something like this (assuming your image is named myplugin.dll ): 使用WinDbg,您可以执行以下操作(假设您的映像名为myplugin.dll ):

  1. Open WinDbg 打开WinDbg
  2. File -> Symbol File Path (add path where the .pdb for the plugin is) File- > Symbol File Path (添加插件的.pdb所在的路径)
  3. File -> Source File Path (add path where the sources for the plugin are) 文件 -> 源文件路径 (添加插件源所在的路径)
  4. File -> Image File Path (add path where the the plugin DLL itself is) 文件 -> 图像文件路径 (添加插件DLL本身所在的路径)
  5. File -> Open Executable (or attach to an existing process, select the main application) 文件 -> 打开可执行文件 (或附加到现有进程,选择主应用程序)
  6. At the command line: bm myplugin!* 在命令行上: bm myplugin!*
  7. Run the program (F5) 运行程序(F5)

Step 6 will add a breakpoint at every symbol in the myplugin.dll . 步骤6将在myplugin.dll中的每个符号处添加一个断点。 But be cautious here: This will really add a breakpoint at every function, even at functions that you might have not defined yourself but are included through headers (like the STL). 但请注意:这实际上会在每个函数上添加一个断点,即使在您可能尚未定义自己但通过标头(如STL)包含的函数上也是如此。 This is the most radical way to set breakpoints everywhere in your module. 这是在模块中各处设置断点的最根本的方法。 If the plugin is programmed in a way where all your exported functions are located in a namespace, or with a prefix, you can trim down the search-pattern and get more accurate breakpoints. 如果以所有导出函数都位于名称空间中或带有前缀的方式对插件进行编程,则可以缩小搜索模式并获得更准确的断点。 Let's assume that all these functions that are interesting to you are prefixed with PLG_ , then you would write: 假设所有您感兴趣的函数都以PLG_为前缀,那么您将编写:

bm myplugin!PLG_*

to have every breakpoint set accordingly - pretty straight forward. 相应地设置每个断点-非常简单

The drawback of course is that you have to use an external debugger. 当然,缺点是您必须使用外部调试器。 Visual Studio does support Function Breakpoints that work similar, but as far as I know they either don't work well with wildcards in every cases, or they have changed it in VS2013+, either one of these. Visual Studio确实支持功能相似的功能断点 ,但据我所知,它们要么在每种情况下都不适用于通配符,要么它们已在VS2013 +中进行了更改(其中之一)。

This was the most simple approach that I am aware of (not that there aren't any, but I simply don't know). 这是我所知道的最简单的方法(不是没有,但我只是不知道)。 The second most easiest: Add the breakpoints once manually and save away your .suo file if you need the breakpoints again later. 第二个最简单的方法:手动添加一次断点,如果以后需要再次使用断点,则保存您的.suo文件。 It is a cumbersome task, but lookig for a faster solution might take longer than doing it by hand (count in a working-day). 这是一项繁琐的任务,但是寻找一个更快的解决方案可能比手工完成要花费更多的时间(算在一个工作日内)。

Others were that you write a program that adds a trampoline at the very beginning of your exported functions (read PE header and get the RVA of the functions) with a int 3 and the original instruction, but this is overkill - you'd easily have put breakpoints in 1000 functions manually before you have done this. 其他情况是您编写了一个程序,该程序在导出的函数的开始处添加了一个蹦床(读取PE标头并获取函数的RVA),并带有int 3和原始指令,但这太过头了-您很容易拥有在执行此操作之前,将断点手动设置为1000个函数。

Another way would be to alter the .suo Solution Options file where the breakpoints are stored. 另一种方法是更改​​存储断点的.suo解决方案选项文件。 But these are, again as to my knowledge, only available through the Extensibility API of Visual Studio, which means you'd have to write a plugin or macro for Visual Studio itself. 但是,据我所知,这些只能通过Visual Studio的扩展性API获得,这意味着您必须为Visual Studio本身编写一个插件或宏。

I am very interested in this, too. 我对此也很感兴趣。 So I will observe this question some while, maybe there is a lot more simpler solution that somebody knows about. 因此,我将在一段时间内观察这个问题,也许有人会知道更简单的解决方案。 If you are new to WinDbg, just add a comment and I'll try to explain how you get it and how it works :) 如果您是WinDbg的新手,只需添加一条评论,我将尽力解释您如何获得它以及它如何工作的:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM