Release mode debugging with VC ++

Source: Internet
Author: User

The debug configuration of VC ++ projects is the configuration that most debugging and development is done on. this is entirely appropriate because the debug information, lack of optimizations, and the extra debug checks in the Microsoft libraries, make stepping through your code and finding bugs much easier than in release builds.

However, sometimes you have to try and debug your release builds. Possible reasons include:

  • You may have a bug that only occurs in your release builds (For information on why this happens see this article) that you need to debug.
  • You may be trying to optimize your code, in which case stepping through the code and seeing what the compiler has created is essential.
  • Your shipped product may be crashing on a customer's machine.

You may be able to avoid release only crashes, and you may not need to optimize your release builds, but you never know when your program is going to start crashing on a distant customer's machine-and if you wait until it happens, it's too late.

If you want to be prepared and professional when your program crashes after you 've released it then you need to create debug information for the version that you shipped, You need to archive the debug information, and you need to know how to use it.

At this point somebody inevitably says thatIt is obviousThat enabling debug information is a nonsensical thing to do on a release build. they then claim that enabling debug information will make your program too big or too slow, and you certainly don't want to ship your precious program with symbols.

To this I say, the debug information in VC ++ will not make your program bigger, it will not make it slower, and of course you don't ship your symbols. done properly, enabling debug information lets you ship an executable that is less than a hundred bytes larger. the cost is essential zero, and the benefits are huge. in fact, I claim that all executables and DLLs created with VC ++ shocould be built with symbols-No exceptions. I further claim that failing to do this is irresponsible.

Enabling debug information in VC ++ 6.0 is relatively easy-But terribly non-obvious. There are three separate settings that you have to change.

First go to project-> Settings, select the release build, then:

  • On the C/C ++ tab for all librarys, DLLs and executables, in the general category, set debug info to program database. This tells the compiler to Generate debug information.
  • On the link tab for all DLLs and executables, in the general category, put a check mark beside Generate debug info. this tells the linker to collate debug information. PDB files. the linker also puts the name of. PDB file in the executable, so the debugger can find it. that's why your executable gets a bit bigger. dumpbin/headers will show you that information.
  • On the link tab, in the project options field, type in "/OPT: ref ". this tells the linker to continue optimizing away unreferenced code. this is the default when debug information is turned off, but it defaults to off when you tell the linker to Generate debug information. failing to specify this will cause your executables and DLLs to get 10-20% larger. you shoshould also specify/ignore: 4089 to tell the linker to not generate warnings if it optimizes away all references to a DLL (this tip works with VC ++. net also ).

This msdn article-http://msdn.microsoft.com/library/default.asp? Url =/library/TechArt/gendepdebug.htm-talks about the options for generating debug symbols, although it fails to differentiate between symbols that you ship with your product and symbols that you archive on site.

Creating debug information is nice, but it only helps if the debug information is available when you need it. in order to get maximum use out of the debug information, your build script (you do have an automatic build script don't you ?) Shocould archive the. PDB file along with the executable (you do archive the executables you release don't you ?), And you shoshould put a label in your version control (you do use version control don't you ?) So that you can recover the source code. Without these steps the debug information can easily be useless.

If you are using VC ++. net or windbg and you are creating minidumps when your program crashes then the following steps are not necessary-Just load the minidump. see this article for information on creating and using minidumps. however if you are using VC ++ 6.0 or you aren't yet creating minidumps, follow these steps:

To analyze a crash address, load. EXE into devstudio (yes, you can do that ). then instead of select debug-> go, single step to load it into the debugger, and make sure the debug information loads-check the output window to see what it says. then open the registers window, put an insertion point on eip and type in the crash address.

When you hit enter, the debugger will take you to that address. because you are in the debugger, the debugger will load the source code associated with that instruction, exactly as if you had crashed in the debugger.

Well, not exactly. the other registers aren't loaded, the stack isn't loaded, and a lot of information has been lost, but if it's an important bug and you're willing to pore over the stack dump for a while, you can frequently figure out what happened.

If you want to improve your odds, you shocould collect more information than just the instruction pointer. check out John Robbins book "debugging Applications" or download one of these Exception Handling Articles-my favorite is here.

With all this information and. PDB or. map file you can write tools to create call stacks-either starting from the EIP and working up the stack, or starting from a return address in winmain and working down the stack. you can also programmatically load the stack and registers into the executable while it's in the debugger, to let the debugger give you more balance in diagnosing the crash.
From: http://blog.csdn.net/bluecrest/archive/2004/07/15/41769.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.