Debugging a library of an external program? (Visual Studio 2010)

最后都变了- 提交于 2019-12-10 18:45:46

问题


This is a question towards programmers who have experience with using Visual Studio 2010 for debugging external applications.

Currently, I'm developping an add-on for a program made by people I work for. In order to make this add-on work with the external program, I have to place the .dll file in a certain folder, specified by the external program. I have my build settings configurated so the .dll file is copied automatically. I also copy the .pdb file to enable debugging options.

Since this add-on is nothing but a class library, I can't debug it like I normally would. I also don't have the source code for the external application. How should I debug this class library properly?

This is what I tried:

1. Attach to Process
See Attach to a Running Process. This seems to work fine, except it takes a few seconds to set up. That's a problem, because most of the exceptions/breakpoints occur at the very first seconds of the start of the external program. It also becomes annoying to manually attach Visual Studio to a process, because I debug this class library a lot, thus taking a lot of time after a while.

2. Debugger.Launch()
Currently I use Debugger.Launch() to attach Visual Studio to the external program. This will display a Just In Time prompt, asking me which instance of Visual Studio I would like to use. I find this to be much slower than "Attach to Process", but this way I am sure that the debugger is attached before any breakpoints can occur. Placing this between #if DEBUG and #end if makes sure this popup doesn't show when I create a release build, which helps too.

3. Add as Existing Project
I've added the executable of the program as a project to my solution. By doing this I can set this executable as startup project, and pressing F5 will start it. The biggest advantage of this, is that I can now debug the executable as soon at is it is started. See: Debug an Executable Not Part of a Visual Studio Solution. With this, Visual Studio breaks at exceptions, but unfortunately it doesn't seem to load the breakpoints. The following information is shown when I try using breakpoints:

The modules window displays: "Binary was not built with debug information.". Can this be solved?

4. Start Action
SilentDoc noted that I can also set an external program as start action, using Properties -> Debug -> Start Action. See: Change the Start Action for Application Debugging It appears to load the breakpoints correctly this way. However, for some reason this makes the external program freeze for 5 minutes at the first breakpoint/exception encountered. After it is done freezing, things seem to work as expected. What could cause this freeze?

Edit: the freeze was caused by the external program. See accepted answer's comments.

5. Image File Execution Options
As hmemcpy suggested, I can also use "Image File Execution Options" for Launching the Debugger Automatically. This seems to be the same as Debugger.Launch(), since it displays the same Just In Time prompt. However, this Just In Time "debugger" doesn't debug at all: breakpoints and exceptions are ignored. Also, when working with a release build, I can't simply disable the Just In Time prompt.

Summary
Option 1 loads breakpoints, breaks at breakpoints/exceptions, but takes too much time.
Option 2 loads breakpoints, breaks at breakpoints/exceptions, but is too slow.
Option 3 doesn't load breakpoints, and doesn't break at breakpoints/exceptions.
Option 4 loads breakpoints, but freezes for 5 minutes at first breakpoint/exception.
Option 5 doesn't load breakpoints, and doesn't break at breakpoints/exceptions.

Additional information
The external program and class library are both written in VB.NET, I am using Visual Studio 2010 Ultimate, and the target framework is .NET Framework 4.0.

Option 1 and 2 work for me, but after a while, all those dialogs and prompts become annoying, and slow me down. Option 3 and 4 seem more suitable for me, so if anyone can tell me how to get them to work, that would be great. Option 5 doesn't seem that interesting either, I don't want to be using regedit.exe all the time. If there are any other options, please let me know.


回答1:


Can't you set that executable as the Starting external program when debugging ?

Dll Project - Properties - Debug - Start External Program ...

EDIT - Sorry, I had not seen you're already doing that :S




回答2:


This might be one of the rare cases where using Image File Execution Options might be helpful. One of my favorite tricks for attaching a debugger at program launch. It's a registry facility which allows you, among other things, attach a debugger to an application before its execution. Same trick allows, for example, replacing your Windows Task Manager with Process Explorer, or Notepad.exe with Notepad2.

You can read all about it here.

Here's how you set it up:

  • Run regedit.exe
  • Go to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options
  • Create a new key named as your exe (example: yourprogram.exe)
  • Create a new string value under your exe. The name of the string value is Debugger, and the value is vsjitdebugger.exe

When you run the executable, you will see the Just In Time prompt asking you to select a debugger:

Select the instance of Visual Studio you want to debug with (or start a new one). Alternatively, you could 'attach to process' at this point, then close the dialog with the No button, to let it attach.

Hope that helps.




回答3:


I think that your approach is fine - you just need to make sure that your dll file contains information about its PDB file (using, for instance, dumpbin /pdbpath:verbose <your dll>) and that source paths from the PDB file are valid on your debugging machine (using, for example, srctool -r <your pdb>). Placing PDB file next to the dll file should also normally work.

Some time ago I wrote a post describing those and other tools to work with PDB files so maybe it will be useful for you: http://lowleveldesign.wordpress.com/2011/12/09/pdb-file-out-of-debugger/.



来源:https://stackoverflow.com/questions/12636966/debugging-a-library-of-an-external-program-visual-studio-2010

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!