Usage of common gdb commands

Source: Internet
Author: User

Today, it took several minutes to learn how to use GDB for debugging.ProgramIt takes only a few minutes to use the gdb command to debug the program.Here we use examples to illustrate how to use the gdb command. First, write a simple program and then debug the program.

Example program for debugging:

Sample Code

 1 # Include <stdio. h> 2   3   Int Sum ( Int A, Int  B)  4  {  5       Int  Result;  6 Result = a + B;  7       Return  Result;  8   }  9   10   Int Main ( Int Argc, Char **Argv)  11   {  12       If (Argc! = 3  )  13   {  14 Printf ( "  Enter two numeric parameters separated by Spaces  "  );  15           Return - 1 ;  16   }  17       Int A = * argv [ 1 ]- '  0  '  ;  18       Int B = * argv [ 2 ]- '  0  '  ; 19       Int Re = Sum (A, B );  20 Printf ( "  % D + % d = % d  "  , A, B, RE );  21       Return   0  ;  22 }

 

This program is very simple. It only performs addition operations. Now we start to debug the program.

1. compile and generate.

In IDE, debug and release versions are used. In Linux, we usually compile this file:

Gcc-O testgdb. c

This is equivalent to compiling and generating the release version in IDE. How can we compile the debug version? In this way, you only need to add the-G command:

Gcc-O testgdb. C-G

2. Enter the debugging status, that is, enter DGB.

There are two ways to enter GDB: one is to directly input the command GDB. Add the first compiled file name after GDB. After entering the gdb debugging status, you can see

Gnu gdb (GDB) 7.0.1-Debian
Copyright (c) 2009 Free Software Foundation, Inc.
License gplv3 +: gnu gpl Version 3 or later
This is free software: You are free to change and redistribute it.
There is no warranty, to the extent permitted by law. Type "show copying"
And "show warranty" for details.
This GDB was configured as "i486-linux-gnu ".
For bug reporting instructions, please see:
<Http://www.gnu.org/software/gdb/bugs/>.

(GDB)

If the first method is used, you must continue to use the file command to open the file to be debugged: (GDB) file testgdb

The following message is displayed:

Reading symbols from/home/hanxi/CPP/testgdb... done.

3. debug the program.

Common commands:

Common gdb commands Format Description Shorthand
List List [start, end] List objectsCodeList L
Prit Print variable name Print variable content P
Break Break [row number or function name] Set breakpoints B
Continue Continue [start, end] Continue running C
Info Info variable name List information I
Next Next Next line N
Step Step Enter function (STEP) S
Display DISPLAY variable name Display parameters  
File File Name (which can be an absolute or relative path) Load files  
Run Run ARGs Run the program R

 

Next, we will test one command. (Note: (GDB) is followed by the input commands and parameters)

(1 ):(GDB) Run 2 3

Indicates that debugging is executed and two parameters (2, 3) are input.

Running result:

Starting program:/home/hanxi/CPP/testgdb 2 3
2 + 3 = 5
Program exited normally. 

In this way, the calculation result is displayed. Because no breakpoint is set, the program will be executed. The following command sets the breakpoint.

(2): (GDB) B 4

Indicates that a breakpoint is set in row 4th. GDB prompts that the breakpoint is set successfully.

Breakpoint 1 at 0x80483ca: file testgdb. C, line 4.

After the breakpoint is set, run the program and run the Run Command. The following message is displayed:

Starting program:/home/hanxi/CPP/testgdb 2 3

Breakpoint 1, sum (A = 2, B = 3) at testgdb. C: 6
6 result = A + B;

In this way, it is stuck in the 6th rows. How can we continue to execute it? Using the C command is equivalent to the shortcut key F5 in.

(3): (GDB) c

Continue to execute the program. GDB provides the following prompt:

Continuing.
2 + 3 = 5
Program exited normally.

The program is executed again, because only one breakpoint is set before. So how do we know how many breakpoints are set? You can use the info break command

(4): (GDB) info break

GDB provides the following prompt information: 

Num type disp ENB address what
2 breakpoint keep y 0x080483ca In sum at testgdb. C: 4
Breakpoint already hit 1 time

This information indicates that a breakpoint has been set in row 4th, and the next step is to remove the breakpoint,How can we delete the breakpoint just now? In vs6.0, click the mouse icon on the repeat toolbar. In vs2010, click the first line of each line. Here, we can also use a simple command to delete or disable breakpoints. The command to delete a breakpoint is delete B, and the command to disable a breakpoint is disable B. We recommend that you use the disable B command. The command to restore the disabled breakpoint is enable B. These commands can be followed by parameters to describe the first few breakpoints (the first few are not the first few ). For example:

(5): (GDB) Disable B

After the Run Command is executed, the breakpoint will not be deleted. Then, the Enable B command is used and then the run command is used. Sometimes we need to view the value of a variable when we break a breakpoint. What command should we use to view the value of the variable and set the value of the variable? The answer is to use display or P.

(6): (GDB) display result

This effect also corresponds to the following in IDE: Display is equivalent to adding a listener variable. The result value is displayed after each run. Print corresponds to the variable value displayed when the cursor stays in the IDE.

(7): (GDB) S

This step is followed by the command next (abbreviated as N ).

Okay, some of the basic commands mentioned above are enough to debug some small code. After Baidu's search, there is a good detailed tutorial in Baidu Library. If you want to dive deeper, you can go in.Linux GDB tutorialLook.If you forget the command when using it, you can also use the help command to view help information. It will prompt the usage of some commands. Unfortunately, it is in English and can be understood in general, if you cannot understand it, you can test the possible commands. The following information is displayed after the Help Command is entered:

List of classes of commands:

Aliases -- aliases of other commands
Breakpoints -- making program stop at certain points
Data -- examining data
Files -- specifying and examining files
Internals -- maintenance commands
Obscure -- obscure features
Running -- running the program
Stack -- examining the stack
Status -- Status inquiries
Support -- Support Facilities
Tracepoints -- tracing of program execution without stopping the program
User-Defined -- user-defined commands

type" help "followed by a class name for a list of commands in that class.
type" help all "for the list of all commands.
type" help "followed by command name for full documentation.
type" Apropos word "to search for commands related to" word ".
command name abbreviations are allowed if unambiguous.

the above prompt is: for example, you can use the help Breakpoints command to view the breakpoint information. I hope you can learn something from the Article ! Welcome!

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.