Windows Debug Artifact: WinDBG

Source: Internet
Author: User

At the beginning of the Java, recently in the consolidation of the foundation and broaden the purpose of knowledge to turn out the long-lost C 艹 research. Habitually use the IDE, either codeblocks, or vs.

After a while, the most obvious feeling is that the IDE with a lot of detail is not clear, and these details are often the most critical knowledge, related to you can fundamentally understand the nature of the program or the completion of some stubborn bug fixes, and the IDE will often put these things to you (for convenience and automation considerations), If you don't know these masked details, you often get overwhelmed when you build your program with the IDE reporting a lot of error.

Anyway, I finally understand the reason why the IDE is not suitable for beginners.

Under Unix or Unix-like systems there is GNU make+gcc+gdb as a tool for command-line compilation and debugging, a personal favorite called the 3G Suite.

So windows can only rely on VS or CODEBLOCKS+MINGW, and Codeblocks+mingw is essentially a 3G suit. When you develop something under Windows, you still have to have MS's own tools to eat.

After the previous period of study, probably learned some Ms NMake and CL and link basic usage, see this essay

Today on the Internet search, found the artifact WinDbg, finally do not need to debug when the time to open the cumbersome vs! New Skill get! Ms Set get! (=,= "" Tease ... )

Click here to download the teaching ppt(i downloaded from the Baidu Library to modify some minor errors and upload to My network disk)

Click here to download the project folder

Code, Main.cpp:

1#include <stdlib.h>2#include <stdio.h>3 4 Char*Getcharbuffer ();5 voidCHANGETO4P (Char*buffer);6 7 intMain ()8 {9System"Pause");Ten     Char*str =Getcharbuffer (); One changeto4p (str); Aprintf"%s", str); -     return 0; - } the  - Char*Getcharbuffer () - { -     return "6969,3p3p"; + } -  + voidCHANGETO4P (Char*buffer) A { at      while(*buffer) { -         if(*buffer = ='3') { -*buffer ='4'; -         } -++buffer; -     } in}

Obviously, the above code tries to modify the memory area of Read only (25 rows) in changeto4p, so access violation error occurs

First of all, if you want to debug your code, then you have to compile the time to compiler and linker are added debug options, so the debug information (symbol,line number and so on) will be retained to debugger

See Makefile:

1 # compiler2CC =CL3 # linker4LINK =Link5# Libraries6LIB =
7# Headers8Header_path =/I include9 # OptionsTenEHSC =/EHSC OneCompilation_only =/ C AC_out =/fo: -L_out =/OUT: - # Compiler & linker debug option, to disable debug, replace '/zi ' & '/debug ' with empty strings theC_debug =/zi -L_debug =/debug - # targets - Bin\test.exe:bin obj obj\main.obj + $ (LINK) $ (l_debug) $ (obj_path) $ (l_out) Bin\test.exe obj\main.obj - + Obj\main.obj: A $ (CC) $ (c_debug) $ (EHSC) $ (header_path) $ (compilation_only) Src\main.cpp $ (c_out) Obj\main.obj at - # Folders - - obj: - mkdir obj - in Bin: - MkDir bin to + # Clean - # Bin, obj folders and pdb files the * . Phony:clean $ Clean :Panax Notoginseng -rmdir/s/Q bin - -rmdir/s/q obj the-del *.pdb

I am not very good at playing makefile, so the debug option is added or removed by hand to modify the makefile implementation. See line 14th. As for the meaning of the various options of CL and link, you can find it on MSDN and don't repeat it.

(To tell the truth I think the better way is a target called release, the other is called Debug, need to debug when the input NMAKE debug, all of the obj, EXE, PDB files are put to debug\obj,debug\bin,debug\ PDB three folders, the need to clean when the input NMAKE Clean,nmake directly generated release version of the obj and Bin folder, study how to write this makefile.

Open the command line and enter NMAKE to generate the demo\vc120.pdb, Demo\bin\test.exe, demo\bin\test.pdb

Here the vc120.pdb test.pdb is the debug information file (program debug Database file), WinDbg use these files to find the source code location and line number and the location of the symbol and other information

Open WinDbg

The first step is to select File>symbol file Path, where the PDB files are set to Demo\bin because test.pdb is in this position

The second step, select File>source file Path, set the source code file location is DEMO\SRC, because main.cpp in this position

The third step, choose File>open executable, open Test.exe

You will find that Main.cpp has been opened (WinDbg Test.pdb found the location of main.cpp)

Fourth step, break point, shortcut key is F9 (command for BP < code in line of binary address >), I choose to give changeto4p this function to hit a breakpoint

Fifth step, start debugging, shortcut keys for F5 (the command is G, that is, Go meaning, you can also choose to press the button on the panel, such as, the basic functions are: Go, Restart, Stop debugging, break, step into, step over, step out, Run to Cursor, Insert or remove breakpoint, Command, Watch, Locals, registers, Memory window, call stack, disassembly, etc The white is (continue to run, re-debug, stop Debugging, pause, single-step instruction (skip, skip, jump, run to cursor), insert Delete Breakpoint, command window, variable Watch window, local variable Watch window, Register Watch window, main memory observation window, call stack, assembly code window, etc.), You can also find these features from the view options.

The corresponding shortcut keys can open WinDbg to see, I do not have to hit the hand, do not move, simply say, F10 is a step step over,f11 step into, and the same as vs inside

Sixth step, when running to changeto4p, open the Watch window, enter *buffer, you will find as shown, the value here is 0n57 ' 9 ', what does that mean? 0n means decimal (similar to 0x for hexadecimal, 0 for octal), 0n57 ' 9 ' means that the ASCII code of the character ' 9 ' is decimal 57, which is plainly the value of *buffer is the character ' 9 '. It is worth mentioning that the name column of the Watch window can not only write the variable name, but also write a valid C + + expression, anyway pointer manipulation, structure gymnastics can be written as a legal expression to the column of name, I guess the simple arithmetic expression is also possible, you can try it.

Alternatively: You can choose View>locals to view the local variables, you can see all the values of the buffer string, you can try it.

For WinDbg preliminary study is here. Some further examples will be added below.

Windows Debug Artifact: WinDBG

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.