GDB basic commands (very detailed)

Source: Internet
Author: User
Tags gdb debugger

GDB Basic CommandsThis article describes common commands for using the GDB debugger.
Main content:
Introduction
For example
Other

Introduction
=============
GDB is a powerful UNIX program debugging tool released by the GNU Open source organization. If you are doing software under the UNIX platform, you will find that GDB has a more powerful debugging tool than VC, BCB's graphical debugger. GDB also has a graphical debug side such as DDD.
In general, GDB accomplishes the following four main functions:
(1) Start your program, you can follow your custom requirements to run the program at will.
(2) allows the program to be debugged to stop at the breakpoint where you specified the reset. (Breakpoint can be a conditional expression)
(3) When the program is stopped, you can check what happens in your program at this time.
(4) Dynamically change the execution environment of your program.

Interest is the best teacher, here first to summarize in the process of debugging frequently encountered problems. Learning and practicing with these questions can help deepen the impression. Then is my practice in the process of summarizing the common commands, if there are any problems or suggestions, you can contact me, thank you! ^_^
(1) How do I print the value of a variable? (Print Var)
(2) How do I print the address of a variable? (Print &var)
(3) How to print the data value of the address? (Print *address)
(4) How do I view the currently running files and lines? (BackTrace)
(5) How to view the code of the specified file? (List file:n)
(6) How do I execute the current function immediately, but not complete the application? (finish)
(7) If the program is multi-file, how can I locate the specified line or function of the specified file? (List file:n)
(8) How do I complete the current loop if the number of loops is large? (until)
(9) How to debug multithreading? (???)

Quietheart
Email: [Email protected]


For example
=============
* Start GDB
$gdb
This allows you to interact with GDB.

* Start GDB and display the source code in Split screen:
$gdb-tui
In this way, using the '-tui ' option, the start can be directly divided into two parts of the screen, the above display source code, more convenient than the list. At this time use the UP and DOWN ARROW keys to view the source code, you want to use the command line with [Ctrl]n and [Ctrl]p].

* Start GDB Debug specified program app:
$gdb app
In this way, after launching GDB directly loaded the app executable, it should be noted that the loading of the app must be compiled with GDB debugging options, such as ' Gcc-g app App.c ', note that if the program's source code is modified, but not compiled, Then in the GDB will be displayed in the modified source code, but the run is the pre-change program, which will lead to tracking confusion.

* After starting the program, then debug with GDB:
$gdb <program> <PID>
Here,<program> is the program's executable file name,<pid> is the PID to debug the program. If your program is a service program, you can specify the process ID of this service program when it runs. GDB will automatically attach up and debug him. The program should be searched in the PATH environment variable.

* After starting the program, start GDB debugging again:
$gdb <PID>
Here, the program is a service program, then you can specify that this service program runs when the process id,<pid> is to debug the program's PID. So gdb is attached to the program, but now it is not possible to view the source code, the file command indicates that the executable can display the source code.


* * Interactive command after starting GDB:
The interactive command supports [TAB] completion.

* Display Help information:
(GDB) Help

* Load the specified program:
(gdb) File app
This loads the executable program app that you want to debug in GDB. If you just started gdb instead of launching it with the GDB app, you can load the app and, of course, add the-G debug option when compiling the app.

* Re-run the Debug program:
(GDB) Run
To run a program ready for debugging, you can use the Run command, followed by any parameters that are sent to the program, including standard input and standard output specifiers (< and >) and Shell wildcard characters (*,?). , [,]).

* Modify the parameters sent to the program:
(GDB) Set args No
Here, if I use "R yes" to set the program startup parameter to Yes, then set args here sets the parameter argv[1] to No.

* Displays the default list of parameters:
(GDB) Show args

* List the code for the specified range (N1 to N2):
(GDB) List N1 n2
This way, the list can be abbreviated to L, and the code between the N1 line and the N2 line will be displayed, and if you start gdb with-tui, it will be displayed in the appropriate location. Without the N1 and N2 parameters, the current line and the following 10 rows are displayed by default, followed by 10 rows. In addition, the list can be followed by the function name.
In general, after the list can be followed by the following parameters:
<linenum> line number.
<+offset> the positive offset of the current line number.
<-offset> the negative offset of the current line number.
<filename:linenum> which line of the file.
<function> function name.
<filename:function> which function in the file.
<*address> the address of the statement in memory when the program runs.

* Perform the next step:
(GDB) Next
This way, a line of code is executed, and the function is skipped if it is a function. This command can be simplified to n.

* Perform N-Times next:
(GDB) Next N

* Execute the last command executed:
(GDB) [Enter]
Here, the direct input carriage will execute the last command.

* Single Step Into:
(GDB) Step
In this way, a line of code is executed, but if a function is encountered, it goes inside the function and executes on one line.

* The function returned to the calling it after executing the current function:
(GDB) Finish
Here, run the program until the current function has finished running back and then stop. For example, to enter a single step execution if you have entered a function and want to exit the function returned to its calling function, you can use the command finish.

* Specify the program until you exit the current loop body:
(GDB) until
or (GDB) u
Here, we find that we need to stop the cursor at the head of the loop and enter u so that it automatically executes all loops.

* Jump to the 5th line of execution Program:
(GDB) Jump 5
Here, it can be abbreviated as "J 5" note that the jump to the 5th line after execution, if there is no breakpoint after the execution continues, rather than stop there.
In addition, the jump does not change the current stack content, so jumping to other functions will have strange phenomenon, so it is better to jump in a function inside, the parameters of the jump can also be the address of the program code line, function name and so on like list.

* Force returns the current function:
(GDB) return
This will ignore the statement that the current function has not finished executing, forcing the return. Return can be followed by an expression, the return value of the expression is the return value of the function.

* Force Call Function:
(GDB) Call <expr>
Here,<expr> can be a function, which returns the return value of the function, if the return type of the function is void then the return value of the function will not be printed, but the practice finds that the print statements in the function run are still not printed.

* Force Call Function 2:
(gdb) Print <expr>
Here, the print and call functions are similar, except that if the return value of the function is void then call does not print the return value, but print prints the return value of the function and stores it in the history.

* Set breakpoints on a line in the current file (assuming 6):
(GDB) Break 6

* Set Conditional breakpoints:
(GDB) Break if testsize==100
Here, if the testsize==100 is at the breakpoint at line 46.

* The change of Detection expression is stopped:
(GDB) Watch I! = 10
Here, I! = 10 Once the expression has changed, it stops. Watch <expr> sets an observer point for the expression (variable) expr. Stop the program (also a breakpoint) as soon as the value of the expression changes.

* Set breakpoints in the current file for a function (assumed as Func):
(GDB) Break func

* Set breakpoints at a line (N) of the specified file (fileName):
(GDB) Break filename:n
Here, it is similar to setting breakpoints on a function in a file.

* Displays the current GDB breakpoint information:
(GDB) Info breakpoints
Here, you can shorthand for info break. Displays all current breakpoints, breakpoint numbers, breakpoint locations, and so on.

* Remove N Breakpoint:
(GDB) Delete N

* Delete all breakpoints:
(GDB) Delete

* Clear all breakpoints above line N:
(GDB) Clear N


* Continue running the program directly to the next breakpoint:
(GDB) Continue
Here, it runs without a breakpoint.

* Displays functions in the current call function stack:
(GDB) BackTrace
The command produces a list of all the valid processes starting with the most recent process and the parameters that call those procedures. Of course, it will also show where it is currently running (file, line).

* View the locale of the current debugger:
(GDB) Show language
Here, if GDB does not recognize the program you are debugging, it is the C language by default.

* View the program language of the current function:
(GDB) Info frame

* Displays the current debug source file:
(GDB) Info source
This displays the current source code file information, such as file name, program language, and so on.

* Manually set the current program language to C + +:
(GDB) Set Language C + +
Here, if GDB does not detect your programming language, you can set this up.

* View the program languages you can set:
(GDB) Set language
Here, you can view the programming languages that can be set in GDB using set language without parameters.

* Terminate a program that is being debugged:
(GDB) Kill
Here, the input kill terminates the program being debugged.

*print display variable (var) value:
(gdb) Print Var
Here, print can be abbreviated as P,print is a very powerful command of GDB, which can be used to display any valid expression in the language being debugged. Expressions can include function calls, complex data structures, history, and so on, in addition to variables in your program.

* with 16 binary display (VAR) value:
(GDB) print/x var
As you can see, print can specify the format of the display, where '/X ' is used to represent the 16 binary format.
The variable display formats that can be supported are:
x Displays the variable in hexadecimal format.
D Displays the variable in decimal format.
u displays unsigned integers in hexadecimal format.
o Displays the variable in octal format.
T displays the variable in binary format.
A displays the variable in hexadecimal format.
C Displays the variable in character format.
F Displays the variable in floating-point number format.


* If a is an array, 10 elements, if you want to display it:
(gdb) print *[email protected]
In this way, 10 elements are displayed, regardless of whether a is double or int, 10 elements are displayed correctly.

* Modify the variable values at runtime:
(gdb) Print x=4
Here, x=4 is the C/s + + syntax, meaning to change the variable x value to 4, if you are currently debugging the language is Pascal, then you can use Pascal's syntax: X:=4.

* shows the type of VAR for a variable:
(GDB) Whatis var

* Display the variable var type in more detail:
(GDB) ptype var
Here, the structure definition of VAR is printed out.

Other
=============
* The MSG variable that prints qstring msg in the qt4.x environment:
The steps are as follows:
1) define a macro printqstring
Define Printqstring
printf "(QString) 0x%x (length=%i): \" ",& $arg 0, $arg 0.d->size
Set $i =0
While $i < $arg 0.d->size
Set $c = $arg 0.d->data[$i + +]
If $c < 32 | | $c > 127
printf "\\u0x%04x", $c
Else
printf "%c", (char) $c
End
End
printf "\" \ n "
End
2) (GDB) printqstring msg
Here, the macro can be directly defined in GDB, it is also said to be written to $home/.gdbinit, so that each start automatically loaded.

* Debug also indicates the build core file:
$gdb <program> Core
Using GDB to debug a running program and core file at the same time, the core is the file generated after core dump is executed illegally. When the program crashes, it generates a core file and then uses this command to navigate directly to where the program crashed. Note: Sometimes you need to set the system command "Ulimit-c Unlimited" to produce a core file.


* * Not practiced
*print displays a storage block, such as 10 integers after the h is displayed:
print [email protected]
**

GDB basic commands (very detailed)

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.