Usage and summarization of gdb debug Command _c language

Source: Internet
Author: User
Tags thread stop

1. Basic command

1 Enter GDB #gdb test

Test is the program to debug, generated by GCC test.c-g-o test. Enter the back-prompt into (GDB).

2) View Source (GDB) L

The source code will be the line number hint.

If you need to see the functions defined in other files, add the function name after l to locate the definition of it and see the other source code nearby. Or: Use a breakpoint or single-step to use S to enter this function at a function.

3) Set Breakpoint (GDB) b 6

This will stop at the 6th line of the source code, you can view the value of the variable, stack situation, and so on; this line number is the number of GDB.

4 View the breakpoint position (GDB) Info b

You can type "info B" to see where the breakpoint is, and you can set multiple breakpoints;

5) Run code (GDB) r

6 Display variable value (GDB) p n

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

GDB displays the variable value before the corresponding value of the "$N" tag, which is the reference mark of the current variable value, if you want to refer to this variable again, you can write "$N" without the need to write a lengthy variable name;

7) Observation variable (GDB) watch n

At a certain cycle, often want to be able to observe the change of a variable, then you can type the command "watch" to observe the change of variables, GDB set the observation point in "N";

8 Single Step Running (GDB) n

9) program continues to run (GDB) c

Causes the program to continue to run down until it encounters a breakpoint or the end of the program;

10 Exit gdb (GDB) Q

2. Breakpoint Debugging

Command Format example function

Break + Set Breakpoint line number break n set breakpoint at N line

Tbreak + line number or function name Tbreak N/func Set temporary breakpoint, automatically deleted after arrival

Break + filename + line number breaks Main.c:10 used to set breakpoints on the specified file corresponding line

Break + <0x...> Break 0x3400a used to pause at a location in memory

Break + line number + if + conditional breaks Ten if i==3 is used to set conditional breakpoints that are very handy to use in loops

Info breakpoints/watchpoints [n] Info break n indicates breakpoint number, view breakpoint/observation point

Clear + The breakpoint line number to be cleared clears 10 is used to clear the breakpoint of the corresponding line, to give the line number of the breakpoint, and GDB gives the prompt when cleared

Delete + The breakpoint number to clear delete 3 command to clear breakpoints and automatically displayed expressions, to give the number of breakpoints, GDB will not give any hints when cleared

Disable/enable + Breakpoint Number Disable 3 Let the breakpoint be set temporarily invalid/enable, if you want to make the breakpoint at multiple numbers break/enable, you can separate the number by a space

Awatch/watch + variable Awatch/watch I sets an observation point that the program is paused when the variable is read out or written

Rwatch + variable Rwatch I sets an observation point, and the program is paused when the variable is read out

Catch sets the capture point to catch some events while the program is running. such as: Load a shared library (dynamic link library) or C + + exception

Tcatch set the capture point only once, when the program stops, the point should be automatically deleted

3. Data command

The display + expression display a displays the value of an expression, displaying the value of an expression whenever the program runs to a breakpoint

Info display is used to show all the current expressions to display a value

Delete + display number Delete 3 to delete an expression to display a value, the deleted expression will not be displayed

Disable/enable + display number disable/enable 3 make an expression to display a value temporarily invalid/enable

Undisplay + Display number Undisplay 3 for ending an expression value

Whatis + variable Whatis I shows the data type of an expression

Print (p) + variable/expression p n for printing a variable or an expression value

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

When you use the Print command, you can output a variable in a specified format with a command format of print/variable name + format

The commonly used variable format: x: hexadecimal; d: decimal; u: unsigned number; o: octal; c: Character format; f: floating-point numbers.

4. Debugging Operation Environment Related command

Set args set args arg1 arg2 setting run parameters

Show args show args see Run parameters

Set width + number of Set width 70 sets GdB ruled

CD + working directory CD. /Switch Working directory

Run R/run program starts execution

Step (s) s entry (which enters into the called child function) steps into the function if the function is compiled with debug information

next (n) n non-entry (does not go into the called child function) stepping

Finish finish runs until the function returns and prints the information such as the stack address and return value and parameter value when the function returns

Until + line U 3 run to a line of functions

Continue (c) C executes to the next breakpoint or the end of the program

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

Call + function Func to execute the function you want to run at the current location

5. Stack-related commands

BACKTRACE/BT BT is used to print the stack frame pointer, or after the command to add the number of stack frame to print, to see the program executed at this time, what function called the program, the program "Call stack" is the current function before the list of all called functions (including the current function). Each function and its variables are assigned a "frame", and the most recently called function is in frame No. 0 ("bottom" frame)

Frame frame 1 To print the specified stack frame

Info Reg Info Reg View Register usage

Info stack info Stack view stack usage

Up/down Up/down jumps to the previous layer/next function

6. Jump Execution

Jump specifies the run point of the next statement. Can be the line number of a file, can be file:line format, can be +num this offset format. The table begins where the next run statement starts. The equivalent of changing the contents of the PC register, the stack content has not changed, cross function jump prone to errors.

7. Signal command

Signal signal sigxxx generates XXX signals, such as SIGINT. a quick way to check the Linux query signal: # kill-l

Handle defines a signal processing in GDB. A signal may begin with a sig or not at a sig, and can be used to define a range (e.g. Sigio-sigkill) that handles signals from Sigio to SIGKILL, including Sigio,sigiot,sigkill three signals, to process a signal. You can also use the keyword all to indicate that you want to process all the signals. Once the program being debugged receives the signal, the running program is immediately stopped by GDB for debugging. It can be one or more of the following keywords:
Nostop/stop
When the program being debugged receives a signal, GDB does not stop the program, but it tells you that the signal is received and/GDB will stop your program.
Print/noprint
When the program being debugged receives a signal, GDB displays a message that/GDB will not tell you about the signal you received.
Pass
Noignore
When the program being debugged receives a signal, GDB does not process the signal. This means that GDB will give this signal to the debugger to process it.
NoPass
Ignore
When the program being debugged receives a signal, GDB does not let the debugger process the signal.
Info signals
Info handle
You can see which signals are handled by GDB, and you can view the default processing

Unlike the shell's Kill command, the system's kill command signals to the debugger that it is intercepted by GDB, and a single command sends a signal directly to the debugger.

8. Run shell command

such as (GDB) shell ls to run LS.

9. More program running options and debugging

1. Operating parameters of the program.
Set args can specify Run-time parameters. (such as: Set args 10 20 30 40 50)
Show args command to see the set of run parameters.
2, the operating environment.
Path to set the running path of the program.
Show paths view the running path of the program.

  Set environment VarName [=value] Sets the environment variable. such as: Set env User=hchen

Show environment [varname] View environment variables.

3, working directory.

  the CD is equivalent to the Shell's CD command.

PWD Displays the current directory.
4, the program input and output.
Info Terminal shows the mode of the terminal used by your program.
Use the redirect Control program output. such as: Run > outfile
A TTY command can refer to a terminal device that writes input and output. such as: Tty/dev/ttyb

5, debug the Running program

Two ways:
(1) Use PS to view the PID (process ID) of the running Program under UNIX, and then hook up the running program with the GDB PID format.
(2) first with GDB associated with the source code, and GDB, in gdb with the attach command to hook up the process of PID. Use detach to cancel the hook process.

6. Pause/Resume program run when GDB stops, you can use info program to see if the application is running, the process number, and why it was paused. In GdB, there are several types of pauses: breakpoints (breakpoint), Observation points (Watchpoint), capture points (Catchpoint), signals (signals), thread stop (thread Stops), and if you want to restore the program to run, You can use the C or Continue command.

7. Threads (thread Stops)

If the program is multiple threads, you can define whether the breakpoint is on all threads or on a particular thread.
Break thread
Break Thread If ...
LINESPEC Specifies the line number of the source program to which the breakpoint is set. THREADNO Specifies the ID of the thread, note that this ID is assigned by GDB and can be viewed using the "Info Threads" command to view the thread information in the program that is running. If thread is not specified, the breakpoint is located above all threads. You can also specify a breakpoint condition for a thread. Such as:
(gdb) Break frik.c:13 thread if Bartab > Lim
When your program is stopped by GDB, all the running threads will be stopped. This makes it easy to see the overall status of the running program. And when you run the recovery program, all the threads are also restored.

10. Debug Core Files

The Core dump:core means RAM, and dump means to throw it out and heap it out. When you develop and use a UNIX program, sometimes the program is inexplicably down, without any hints (sometimes prompting the core dumped), At this point you can see if there is a core. The process number of the file generation, this file is the operating system when the program down when the memory content thrown out to generate, it can be used as a reference to debug the program

(1) Generating core files

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

#设置core大小为无限
Ulimit-c Unlimited
#设置文件大小为无限
Ulimit Unlimited

These require root permissions, and each time you reopen the interrupt under Ubuntu, you need to re-enter the first command above to set the core size to infinity.

Core file Build path: Enter the same path as the executable file Run command. If the system-generated core file does not have any other extended names, it is all named Core. The new core file generation will overwrite the original core file.

1)/proc/sys/kernel/core_uses_pid can control whether the file name of the core file adds PID as an extension. The file content is 1, which means adding PID as the extension, the resulting core file format is core.xxxx, and 0 indicates that the generated core file is named Core.
This file can be modified by the following command:
echo "1" >/proc/sys/kernel/core_uses_pid

2 Proc/sys/kernel/core_pattern can control the location of the core file and file name format.
This file can be modified by the following command:
echo "/corefile/core-%e-%p-%t" > Core_pattern, the core file can be built uniformly into the/corefile directory, the resulting file name is core-command name-pid-timestamp
The following is a list of parameters:
%p-insert pid into filename add PID
%u-insert present UID into filename add current UID
%g-insert present GID into filename add current GID
%s-insert signal that caused the coredump into the filename adds a signal leading to the core
%t-insert Unix time when the coredump occurred into filename to add a core file generation
%h-insert hostname where the coredump happened into filename add host name
%e-insert coredumping executable name into filename add command name

(2) View core files with GDB

After core dump occurs, use GDB to view the contents of the core file to locate the row in the file that is causing the core dump.
GDB [exec file] [core file]
Such as:
GdB./test Core

or GDB./a.out
Core-file core.xxxx
After GDB, use the BT command backtrace or where to see where the program runs to locate the file-> line for core dump.

Executable files to be debugged, you need to add-g,core files at compile time to properly display error messages

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

(3) Real-time observation of a process crash information with GDB

Start process
Gdb-p PID
C
Run process to crash
GDB will display 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.