C Combat: A powerful program debugging tool GDB

Source: Internet
Author: User

C Combat: Powerful program Debugging Tool GDB1. Basic debugging

Only the most commonly used GDB commands are listed here.

1.1 Start GDB

GDB Program: Prepare the debugger. You can also go directly to GDB and then load it through the file command.

1.2 Adding breakpoints

b function: Sets a breakpoint for the function. b is the abbreviation for break, except for the function name, which can also be the address, the +/-offset of the current execution place, and so on.

1.3 Running the program

Run args: Start running the program, you can add the required parameters to the program, just as you would when the command line is working.

1.4 Single-Step commissioning

S/n/si/c/kill:s, step in, executes the next line of code, N is step next, executes the next line of code but does not enter; Si is step instruction, executes the next assembly/CPU directive; C is continue, Continue execution until the next breakpoint; kill terminates debugging; quit the GDB.

1.5 Printing Debugging information

BT:BT is the abbreviation for backtrace, which prints the stack path of the currently located function.
Info frame ID: Prints the information for the selected stack frame.
Info args: Prints the parameters of the selected stack frame.
Print variable: Prints the value of the specified variable.
List: Lists the appropriate source code.
Info registers: View the values of all registers.

There is a more flexible and powerful is the direct printing%esp start of the first n elements, such as the printing stack first 10 elements is:x/10x $sp.

2.GDB Combat

Here is a practical example of using the above command:

[Email protected] bufbomb]# gdb bufbomb GNU gdb (gdb) Red Hat Enterprise Linux (7.2-.EL6) Copyright (C) .Free software Foundation, inc.license gplv3+: GNU GPL version3 orLater  isFree software:you is free toChange andRedistribute it. There isNO WARRANTY, toThe extent permitted by law.Type "Show copying" and "Show warranty"  forDetails. This GDB is configured as"X86_64-redhat-linux-gnu". forBug reporting instructions, please see:1At0x8048ad6(GDB) run-t cdaistarting program:/root/temp/bufbomb/bufbomb-t Cdaiteam:cdaicookie:0x5e5ee04eBreakpoint1,0x08048ad6 inchGetbuf () Missing separate Debuginfos, Use: Debuginfo-install glibc-2.12-1.149. el6_6. 4. i686 (GDB) bt#0  0x08048ad6 inchGetbuf () #1  0X08048DB2 inchTest () #2  0x08049085 inchLaunch () #3  0x08049257 inchMain () (GDB) Info frame0Stack Frame at0xffffb540: EIP =0x8048ad6 inchGetbuf; Saved EIP0X8048DB2Called by frame at0xffffb560Arglist at0xffffb538, Args:locals at0xffffb538, Previous Frame' sSp is 0xffffb540Saved REGISTERS:EBP at0xffffb538, EIP at0xffffb53c(GDB) Info registerseax0xc       AEcx0xffffb548-19128fd[0xc8c340 13157184Ebx0x0      0Esp0xffffb510       0xffffb510Ebp0xffffb538       0xffffb538Esi0x804b018        134524952Edi0xFFFFFFFF-1Eip0x8048ad6        0x8048ad6<getbuf+6>eflags0x282[SFIF]cs0x23      *Ss0x2b      +Ds0x2b      +Es0x2b      +Fs0x0      0Gs0x63      About(GDB) x/TenX $sp0xffffb510:0xf7ffc6b0      0x00000001      0x00000001      0xffffb5640xffffb520:0x08048448      0x0804a12c      0xffffb548      0x00c8aff40xffffb530:0x0804b018      0xFFFFFFFF(GDB) Si0x08048ad9 inchGetbuf () (GDB) Si0X08048ADC inchGetbuf () (GDB) Si0x080489c0 inchGets () (GDB) Nsingle steppinguntil ExitFromfunctionGets,which has no. line number information.Type string:1230X08048AE1 inchGetbuf () (GDB) Si0x08048ae2 inchGetbuf () (GDB) CContinuing.Dud:getbuf returned0x1Better LuckNext  TimeProgram exited normally. (GDB) quit
3. Reverse Commissioning

GDB 7.0 added the reversal debugging function. Specifically, for example, I set breakpoints on GETBUF () and main () and stop at the breakpoint of the main () function when the program is started. When the record is typed and continue to the next breakpoint Getbuf (), GDB logs the run-time information from main () to Getbuf (). Now with RN It is possible to debug from GETBUF () to main () in reverse. Just like the X-Men: Reversing the future, it's amazing!

This method is suitable for the reverse of the bug to find the code that caused the bug, the practicality varies depending on the situation. Of course, it also has limitations. GDB cannot be "reversed" if external conditions such as the I/O output are changed by the program.

[Email protected] bufbomb]# gdb bufbomb GNU gdb (gdb) Red Hat Enterprise Linux (7.2-.EL6) Copyright (C) .Free software Foundation, inc.license gplv3+: GNU GPL version3 orLater  isFree software:you is free toChange andRedistribute it. There isNO WARRANTY, toThe extent permitted by law.Type "Show copying" and "Show warranty"  forDetails. This GDB is configured as"X86_64-redhat-linux-gnu". forBug reporting instructions, please see:1At0x8048ad6(GDB) B mainbreakpoint2At0x80490c6(GDB) run-t cdaithe program being debugged have been started already. Start it from the beginning? (YorN) ystarting program:/root/temp/bufbomb/bufbomb-t cdaibreakpoint2,0x080490c6 inchMain () (GDB)Record(GDB) CContinuing.Team:cdaiCookie:0x5e5ee04eBreakpoint1,0x08048ad6 inchGetbuf () (GDB) Rnsingle steppinguntil ExitFromfunctionGetbuf,which has no. line number information.0x08048dad inchTest () (GDB) Rnsingle steppinguntil ExitFromfunctionTest,which has no. line number information.0x08049080 inchLaunch () (GDB) Rnsingle steppinguntil ExitFromfunctionLaunch,which has no. line number information.0x08049252 inchMain ()

C Combat: A powerful program debugging tool GDB

Related Article

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.