Usage and summary of gdb Debugging commands

Source: Internet
Author: User

1. Basic commands

1) Go to GDB # gdb test

Test is the program to be debugged. It is generated by gcc test. c-g-o test. Enter the prompt and change to (gdb ).

2) view the source code (gdb) l

The source code prompts the line number.

If you need to view the functions defined in other files, add the function name after l to locate the function definition and view other nearby source code. Or: Use a breakpoint or a single-step operation to enter a function using s.

3) set the breakpoint (gdb) B 6

This will stop running to source code 6th. You can view the value and stack of the variable. The row number is the row number of gdb.

4) view the breakpoint (gdb) info B

You can type "info B" to view the breakpoint and set multiple breakpoints;

5) run the code (gdb) r

6) display the variable value (gdb) p n

When the program is paused, type "p variable name" (print;

GDB adds the "$ N" mark before the corresponding value when displaying the variable value. It is the reference mark of the current variable value. If you want to reference this variable again in the future, you can directly write "$ N" without the need to write lengthy variable names;

7) observe the variable (gdb) watch n

In a loop, you often want to observe the changes of a variable. In this case, you can enter the command "watch" to observe the changes of the variable. GDB sets an observation point in "n;

8) single-step running (gdb) n

9) program continues running (gdb) c

Let the program continue to run until the breakpoint or program ends again;

10) Exit GDB (gdb) q

2. breakpoint debugging

Command Format example function

Break + set the line number of the breakpoint. break n sets the breakpoint at n rows.

Tbreak + row number or function name tbreak n/func sets a temporary breakpoint and is automatically deleted upon arrival

Break + filename + line number break main. c: 10 is used to set a breakpoint on the corresponding line of the specified file

Break + <0x...> break 0x3400a is used to suspend a location in the memory.

Break + row number + if + condition break 10 if I = 3 is used to set the condition breakpoint, which is very convenient to use in a loop.

Info breakpoints/watchpoints [n] info break n indicates the breakpoint number. view the breakpoint/observation points.

Clear + the breakpoint line number to be cleared clear 10 is used to clear the breakpoint of the corresponding line, to give the breakpoint line number, GDB will prompt when clearing

Delete + breakpoint number to be cleared delete 3 command used to clear breakpoint and automatically displayed expression, to give the breakpoint number, GDB will not give any prompt when clearing

Disable/enable + breakpoint number disable 3 temporarily invalidates/enables a set breakpoint. If you want to disable/enable a breakpoint at multiple numbers, separate them with spaces.

Awatch/watch + variable awatch/watch I sets an observation point. When the variable is read or written, the program is paused.

Rwatch + variable rwatch I sets an observation point. When the variable is read, the program is paused.

Catch sets the capture point to catch some events during the program running. For example, loading a shared library (Dynamic Link Library) or a C ++ exception

Tcatch is set only once. When the program stops, the point should be automatically deleted.

3. Data commands

Display + expression display a is used to display the expression value. The expression value is displayed whenever the program runs to a breakpoint.

Info display is used to display the current expression of all values to be displayed.

Delete + display no. delete 3 is used to delete an expression for displaying values. The deleted expression is not displayed.

Disable/enable + display no. disable/enable 3 temporarily invalidates the expression for displaying the value/enable

Undisplay + display number undisplay 3 is used to end the display of a certain expression value

Whatis + variable whatis I displays the data type of an expression

Print (p) + variable/Expression p n is used to print the value of a variable or expression

Set + variable = variable value set I = 3 change the value of a variable in the program

When using the print command, you can output variables in the specified format. The command format is print/variable name +.

Common variable formats include x: hexadecimal, d: decimal, u: unsigned, o: octal, c: character, and f: Floating Point.

4. debug running environment commands

Set args arg1 arg2 set running parameters

See running parameters for show args

Set width + number set width 70 set the row width of GDB

Cd + working directory cd ../switch working directory

Run r/run

Step (s) s (will enter the called sub-function) for one-step execution. The prerequisite for entering the function is that the function is compiled with debug information.

Next (n) n non-entry (will not enter the called sub-function) one-step execution

Finish until the function returns and prints the stack address, return value, parameter value, and other information when the function returns.

Until + number of lines u 3 run to a certain row of the Function

Continue (c) c is executed until the next breakpoint or program ends.

Return <return value> return 5 changes the program process, ends the current function directly, and returns the specified value

Call + function call func executes the function to be run at the current position

5. Stack-related commands

Backtrace/bt is used to print the stack frame pointer. You can also add the number of stack frame pointers to be printed after this command to check which function calls are performed by the program at this time, the program "call stack" is the list of all called functions (including the current function) before the current function ). Each function and its variables are assigned a "frame". The recently called function is in frame 0 ("bottom" frame)

Frame 1 is used to print the specified stack frame.

Info reg view register usage

Info stack view stack usage

Up/down jump to the upper/lower layers of functions

6. Redirect execution

Jump specifies the running point of the next statement. It can be the row number of the file, it can be in file: line format, it can be in the offset format of + num. Table-based where the next running statement starts. It is equivalent to changing the PC register content. The stack content is not changed, and cross-function jump is prone to errors.

7. signal command

Signal SIGXXX generates XXX signals, such as SIGINT. A Method for fast query of Linux query signals: # kill-l

Handle defines a signal processing in GDB. A signal can start with or not with SIG and define a range of signals to be processed (for example, a SIGIO-SIGKILL that represents a signal from SIGIO signal to SIGKILL, including SIGIO, SIGIOT, SIGKILL), or you can use the keyword "all" to indicate all signals to be processed. Once the debugged program receives a signal, the running program will be immediately stopped by GDB for debugging. It can be one or more of the following keywords:
Nostop/stop
When the program to be debugged receives a signal, GDB will not stop the program running, but it will send a message indicating that it will stop your program when it receives the signal.
Print/noprint
When the program to be debugged receives a signal, GDB will display a message/GDB will not tell you the signal received
Pass
Noignore
When the program to be debugged receives a signal, GDB does not process the signal. This indicates that GDB will hand over the signal to the debugged program for processing.
Nopass
Ignore
When the program to be debugged receives a signal, GDB will not let the program to be debugged process the signal.
Info signals
Info handle
You can view which signals are processed by GDB and see the default processing method.

The single command is different from the shell kill command. When the system's kill command sends a signal to the program to be debugged, It is intercepted by GDB, the single command sends a signal directly to the program to be debugged.

8. Run Shell commands

For example, (gdb) shell ls to Run ls.

9. More program running options and debugging

1. program running parameters.
Set args can Specify runtime parameters. (For example, set args 10 20 30 40 50)
Run the show args command to view the set running parameters.
2. Running environment.
Path can be used to set the running path of a program.
Show paths to view the program running path.

Set environment varname [= value] To set environment variables. For example, set env USER = hchen

Show environment [varname] to view environment variables.

3. working directory.

Cd is equivalent to the cd command of shell.

Pwd displays the current directory.
4. input and output of the program.
Info terminal shows the terminal mode used by your program.
Use the redirection control program output. For example, run> outfile
The tty command can be a terminal device that writes input and output data. For example, tty/dev/ttyb.

5. debug the running program

Two methods:
(1) view the PID (process ID) of the running program in ps on UNIX, and then hook the running program in gdb PID format.
(2) First Use gdb to associate the source code with gdb, and use the attach command in gdb to mount the PID of the process. Use detach to cancel the attached process.

6. Pause/resume the program running. when the process is stopped by gdb, you can use info program to check whether the program is running, process number, and cause of suspension. In gdb, we can use the following pause Methods: BreakPoint, WatchPoint, CatchPoint, Signals, and Thread Stops ), to resume the program running, run the c or continue command.

7. Thread (Stops)

If the program is multi-threaded, you can define whether the breakpoint is on all threads or on a specific thread.
Break thread
Break thread if...
Linespec specifies the line number of the source program where the breakpoint is set. Threadno specifies the thread ID. Note that this ID is allocated by GDB. You can run the "info threads" command to view the thread information in the running program. If no thread is specified, the breakpoint is set on all threads. You can also specify breakpoint conditions for a thread. For example:
(Gdb) break frik. c: 13 thread 28 if bartab> lim
When your program is stopped by GDB, all running threads are stopped. This allows you to conveniently view the overall situation of the running program. When you resume the program running, all threads will be resumed.

10. debug the core File

Core Dump: Core indicates memory, and Dump indicates throwing and heap. When developing and using Unix programs, sometimes the program is inexplicably down, but there is no prompt (sometimes it prompts core dumped). At this time, you can check whether it is like a core. process number file generation. This file is generated by the operating system throwing out the memory content when the program is down. It can be used as a reference for debugging programs.

(1) generate a Core File

By default, the core file size is set to 0, so that the system will not dump the core file. The core file can be generated only after modification.

# Set the core size to unlimited
Ulimit-c unlimited
# Set the file size to unlimited
Ulimit unlimited

These require the root permission. In ubuntu, you need to re-enter the first command to set the core size to unlimited.

Core File generation path: Enter the same path as the executable file running command. If the core file generated by the system does not contain any extension name, it is all named core. The new core file will overwrite the original core file.

1)/proc/sys/kernel/core_uses_pid can control whether pid is added to the file name of the core file as an extension. If the file content is 1, the pid is added as the extension, and the generated core file format is core. xxxx. If it is 0, the generated core file is named core.
Run the following command to modify the file:
Echo "1">/proc/sys/kernel/core_uses_pid

2) proc/sys/kernel/core_pattern can control the core file storage location and file name format.
Run the following command to modify the file:
Echo "/corefile/core-% e-% p-% t"> core_pattern, which can generate core files in the/corefile directory, the generated file name is core-command name-pid-timestamp.
The following is a list of parameters:
% P-insert pid into filename add pid
% U-insert current uid into filename add current uid
% G-insert current gid into filename add current gid
% S-insert signal that caused the coredump into the filename added to generate core signal
% T-insert UNIX time that the coredump occurred into filename unix time when the core file is generated
% H-insert hostname where the coredump happened into filename Add the Host Name
% E-insert coredumping executable name into filename add command name

(2) Use gdb to view core files

After a core dump occurs, use gdb to view the content of the core file to locate the line that causes the core dump in the file.
Gdb [exec file] [core file]
For example:
Gdb./test core

Or gdb./a. out
Core-file core. xxxx
After gdb, use the bt command backtrace or where to check where the program runs and locate the core dump file-> line.

The executable file to be debugged must be added to-g during compilation so that the core file can display error information normally.

1) gdb-core = core. xxxx
File./a. out
Bt
2) gdb-c core. xxxx
File./a. out
Bt

(3) Use gdb to observe the crash information of a process in real time

Start Process
Gdb-p PID
C
Run the process to crash
Gdb displays the crash information.
Bt

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.