gdb Basic CommandThis article describes the common commands used to debug programs with GDB.
Main content:
Introduction
Example
Other
Introduction
=============
GDB is a powerful UNIX program debugging tool released by the GNU Open source organization. If you're working on a UNIX platform, you'll find that GDB's debugging tool has more power than VC, BCB's graphical debugger. GDB also has a graphical debugging end such as DDD.
In general, GDB mainly completes the following four areas of functionality:
(1) Start your program, you can follow your custom requirements of the arbitrary operation of the program.
(2) Allow the program to be debugged to stop at the breakpoint where you specified the adjustment. (A breakpoint can be a conditional expression)
(3) When the program is stopped, you can check what happened in your program at this time.
(4) Dynamically changing the execution environment of your program.
The interest is the best teacher, here first summarizes in the debugging process frequently encounters the question. Learning and practicing with these questions can help deepen the impression. And then I practice in the process of summing up the common orders, if any questions or suggestions, you can contact me, thank you. ^_^
(1) How to print the value of a variable. (Print Var)
(2) How to print the address of the variable. (Print &var)
(3) How to print the data value of the address. (Print *address)
(4) How to view the currently running files and rows. (BackTrace)
(5) How to view the code for the specified file. (List file:n)
(6) How to perform the current function immediately, but not the entire application. (finish)
(7) If the program is multiple files, how to navigate to the specified file specified line or function. (List file:n)
(8) If the number of loops, how to complete the current loop. (until)
(9) How to debug multithreading. (???)
Author: Quietheart
Email:quiet_heart000@126.com
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 launch can be directly divided into two parts of the screen, the above display source code, more convenient than using the list. At this time use the UP and DOWN ARROW keys can view the source code, want to use the command line with the up and down keys with [Ctrl]n and [Ctrl]p.
* Start GDB debug the specified program app:
$gdb app
In this way, after starting GDB directly load the app executable program, 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 you modify the source code of the program, but did not compile, Then the display in GDB will be the changed source code, but the operation is the change before the program, which will lead to tracking confusion.
* After starting the program, then debug with GDB:
$gdb <program> <PID>
Here,<program> is the executable filename of the program,<pid> is the PID to debug the program. If your program is a service program, then you can specify the process ID of the service program at run time. GDB will automatically attach up and debug him. program should be searched for in the PATH environment variable.
* After starting the program, and then start GDB debugging:
$gdb <PID>
Here, the program is a service program, then you can specify the process that this service program runs Id,<pid> is the PID of the program to be debugged. So gdb is attached to the program, but now it is not possible to view the source code, using the file command to indicate the executable can display the source code.
* * The interactive command after GDB starts:
The interactive command supports [TAB] completion.
* Display Help information:
(GDB) Help
* Loading the specified program:
(gdb) File app
This loads the executable app that you want to debug in GDB. If you start running GDB instead of using the GDB app, you can load the app and, of course, add the-G debug option when you compile the app.
* Rerun the Debugging program:
(GDB) Run
To run a program that is ready for debugging, you can use the Run command to follow 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, suppose I use "R yes" to set the program startup parameter to Yes, then the set args here sets the parameter argv[1] to No.
* Display the default parameter list:
(GDB) Show args
* List code for the specified range (between N1 to N2):
(GDB) List N1 n2
In this way, the list can be abbreviated to L and will display code between the N1 line and the N2 line, and if you use-tui to start GDB, it will appear in the appropriate location. If there are no N1 and N2 parameters, the current row and subsequent 10 rows are displayed by default, followed by 10 rows. In addition, the list can also receive the function name.
Generally after the list can be followed by the following parameters:
<linenum> line number.
<+offset> the positive offset of the current line number.
<-offset> negative offset of current line number.
<filename:linenum> which row of which file.
<function> function name.
<filename:function> which function in which file.
The address of the statement in memory at the time the <*address> program runs.
* Follow the next steps:
(GDB) Next
In this way, a line of code is executed, and if the function skips the function. This command can be simplified to n.
* Execute n Next step:
(GDB) Next N
* Execute last executed command:
(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 the function is encountered, it goes inside the function, and then the line executes.
* Run the current function back to the function that called it:
(GDB) Finish
Here, run the program until the current function finishes running and returns and stops. For example, if stepping into a function, and want to exit the function returned to its calling function, you can use the command finish.
* Specify the program until exiting the current loop body:
(GDB) until
or (GDB) u
Here, find the need to stop the cursor in the loop of the head, and then enter u so that automatically executes the entire loop.
* Jump Execution program to line 5th:
(GDB) Jump 5
Here, the shorthand for "J 5" is to note that after the jump to line 5th executes, the execution continues without a breakpoint, rather than stopping there.
In addition, the jump does not change the current stack content, so jump to another function will have strange phenomenon, so it is best to jump in a function inside, jump parameters can also be the address of the program line of code, 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 that returns the value of the function.
* Force Call Function:
(GDB) Call <expr>
Here,<expr> can be a function that returns the return value of the function, and if the return type of the function is void then the return value of the function is not printed, but the practice finds that the print statement in the function is 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 out the return value of the function and holds it in the history.
* Set a breakpoint on a line in the current file (assuming 6):
(GDB) Break 6
* Set a conditional breakpoint:
(GDB) Break if testsize==100
Here, if the testsize==100 is on the 46 line at the breakpoint.
* Check expression changes are stopped:
(GDB) Watch I!= 10
Here, I!= 10, once the expression changes, it stops. Watch <expr> set an observation point for expression (variable) expr. Stop the program (also a breakpoint) as soon as the value of the expression changes.
* Set a breakpoint in the current file for a function (assumed to be func):
(GDB) Break func
* Set a breakpoint on a row (N) for the specified file (fileName):
(GDB) Break filename:n
Here, it is the same to set a breakpoint on a function in a file.
* Display current GDB breakpoint information:
(GDB) Info breakpoints
Here, the info break can be abbreviated. All current breakpoints, breakpoint numbers, breakpoint locations, and so on are displayed.
* Remove the N-Number breakpoint:
(GDB) Delete N
* Remove All Breakpoints:
(GDB) Delete
* Clear all breakpoints above row N:
(GDB) Clear N
* Continue running the program to run directly to the next breakpoint:
(GDB) Continue
Here, if there is no breakpoint, run it all the time.
* Displays the functions in the current call function stack:
(GDB) BackTrace
The command produces a list of all the valid procedures that begin with the most recent procedure and the parameters that invoke them. Of course, this will also show where the current run is (file, line).
* View the locale of the current debugger:
(GDB) Show language
Here, if GDB does not recognize the program you are debugging, then the default is C language.
* View the program language of the current function:
(GDB) Info frame
* Display the current debug source file:
(GDB) Info source
This displays the source code file information that is currently in place, 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 program language, you can set this up.
* View the program language you can set:
(GDB) Set language
Here, use the set language without parameters to view the program language that can be set in GDB.
* Terminate a program that is being debugged:
(GDB) Kill
Here, entering a kill terminates the program that is being debugged.
*print display variable (var) value:
(gdb) Print Var
Here, print can be abbreviated to P,print is a powerful command of GDB, which can be used to display any valid expression in the language being debugged. In addition to the variables in your program, an expression can include function calls, complex data structures, and history, among other things.
* Display (VAR) value in 16:
(GDB) print/x var
As you can see here, print can specify the format of the display, which uses '/x ' to represent the 16-in format.
The variable display formats that you can support are:
x displays variables in hexadecimal format.
D displays variables in decimal format.
U displays the unsigned integer in hexadecimal format.
o Display variables in octal format.
T displays variables in binary format.
A the variable is displayed in hexadecimal format.
C displays variables in character format.
F displays variables in floating-point format.
* If a is an array, 10 elements, if you want to display:
(gdb) Print *a@10
This displays 10 elements, regardless of whether a is double or int, and the 10 elements are displayed correctly.
* Modify the Run-time variable value:
(gdb) Print x=4
Here, x=4 is the syntax of C + +, meaning to change the variable x value to 4, if your current debugging language is Pascal, then you can use Pascal's syntax: X:=4.
* Displays the type of VAR for a variable:
(GDB) Whatis var
* Displays the type of variable var in a more detailed manner:
(GDB) ptype var
Here, the structure definition of VAR is printed out.
Other
=============
* Print msg variables for 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 defined directly in GDB, which is said to be written to $home/.gdbinit, so that it can be automatically loaded each time it is started.
* Debug also indicates that the core file was generated:
$gdb <program> Core
Using GDB to debug both a running program and a core file, the core is the file that was generated after the program was illegally executed after the core dump. A core file is generated when the program crashes illegally, and then the command is positioned directly to where the program crashes. Note: It is sometimes necessary to set the system command "Ulimit-c Unlimited" to produce a core file.
* * There's no practice
*print displays a storage block, such as 10 integers following h:
Print h@10
**
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.