GDB tips (2) - GDB command

DB commands overview

After starting gdb, you will be brought into the gdb debugging environment, you can use the gdb debugger command to start up, gdb commands can use the help command to view, as follows:

/home/hchen> gdb
GNU gdb 5.1.1
Copyright 2002 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-suse-linux".
(gdb) help
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" followed by command name for full documentation.
Command name abbreviations are allowed if unambiguous.
(gdb)

gdb commands a lot, gdb put it into a number of categories . Example except a help command gdb command type, and if the command depends on the type, can help <class> command , such as: help breakpoints, all commands to set breakpoints. You can also directly help <command> to view the help command.


gdb, the input command, you can not play the whole command, only the first few characters of a command to play on it, of course, the first few characters of the command should be marked with a unique command in Linux, you can knock hit the TAB key twice to pad the full name of the command, if there are repeated, then gdb will come out of its cases.
   
Example 1: upon entering a function func, set a breakpoint. You can knock break func, or is directly b func

  • (gdb) b func
  • Breakpoint 1 at 0x8048458: file hello.c, line 10.

Example Two: Knock b press the TAB key twice, you will see that all starts with the b command:

  • (gdb) b
  • backtrace  break      bt
  • (gdb)

Example Three: just remember the prefix function, it can be:

  • (gdb) b make_ <按TAB键>
  • (Once again press the TAB key, you'll see :)
  • make_a_section_from_file     make_environ
  • make_abs_section             make_function_type
  • make_blockvector             make_pointer_type
  • make_cleanup                 make_reference_type
  • make_command                 make_symbol_completion_list
  • (gdb) b make_

GDB all the functions that begin to make out all the cases you see.

Example Four: When debugging C ++ programs, you can have the same function name. Such as:

  • (gdb) b 'bubble( M-?
  • bubble(double,double)    bubble(int,int)
  • (gdb) b 'bubble(

You can view all of overloaded functions and parameters in C ++. (Note: M- and "press the TAB key twice" is a meaning?)

To exit gdb, only for short hair quit or q command on the line.

Run the program in GDB

When to gdb <program> way to start gdb, gdb will search the current directory and path PATH <program> source files. To verify gdb read source file, or list command l may be used to see if the source code is listed gdb energy.

In gdb, run the program using r or run command. Run the program, you may need to set the following four aspects of the matter.

1, the program operating parameters.
    set args parameter to specify runtime. (Eg: SET 20 is 30 40 50 10 args)
    Show args command to set a good view the operating parameters.

2, the operating environment.
    path <dir> path may be set to run the program.
    show paths to view the running path of the program.
    set environment varname [= value] set environment variables. Such as: the SET env the USER = hchen
    Show Environment [varname] viewing environment variable.

3, the working directory.
    cd <dir> cd command corresponding to the shell.
    pwd Displays the current directory.

4, the input output program.
    info terminal display mode to use your terminal program.
    A control program output redirection. Such as: RUN> outfile
    TTY command to either write the input and output terminals. Such as: tty / dev / ttyb

Debugger is already running

Two methods:
1, viewing running programs with ps under UNIX PID (process ID), and then use gdb <program> PID format mount a running program.
2, first with gdb <program> on the associated source code, and gdb, gdb with the attach command to mount the PID of the process. And to unmount process with detach.

Pause / resume running

Debugger to halt the program is a must, GDB can easily suspend operation of the program. You can set the program in which line stopped, and under what conditions stopped, stopped to receive any signal at the time and so on. So that you see the run-time variables, as well as the process runs.

When the process is stopped gdb, you can use the info program to see whether the program is running, a process ID, the reason being suspended.

In gdb, we can have a pause following ways: a breakpoint (BreakPoint), observation point (WatchPoint), capture points (CatchPoint), signal (Signals), the thread to stop (Thread Stops). If you want to run the recovery program, or you can continue to use c command.

First, set a breakpoint (BreakPoint)

We use the break command to set a breakpoint. There are several positive ways to set breakpoints:

  • break <function>

    Stop when entering a specified function. C ++ function names can be specified using the class :: function or function (type, type) format.

  • break <linenum>

    Stopped at the specified line number.

  • break +offset
  • break -offset

    Stopped in front of or behind the line number of the current row offset. offiset is a natural number.

  • break filename:linenum

    Linenum stopped at the line of the source file filename.

  • break filename:function

    Stopped at the entrance of the source file filename function function.

  • break *address

    Address in memory to run the program stopped.

  • break

    When the break command has no parameters, it indicates stopped at the next instruction.

  • break ... if <condition>

    ... may be the above parameters, condition represents a condition stopped when the condition is satisfied. For example, in cycle environment body, may be provided break if i = 100, i indicates when the program is stopped is 100.

When viewing breakpoint info command may be used, as follows :( Note: n indicates the breakpoint)

  • info breakpoints [n]
  • info break [n]

Second, set the observation point (WatchPoint)

General observation point to see if the value of an expression (also a variable expression) there is a change, if there is a change, immediately stopped the program. We have the following several ways to set up observation points:

  • watch <expr>

    setting an observation point expr Expression (variable). When there is a change in the value of the amount of expression, immediately stopped the program.
   

  • rwatch <expr>

    When the expression (variable) expr is read, the program stopped.
   

  • awatch <expr>

    When the value of the expression (variable) is read or written, stop the program.

  • info watchpoints

    List all the observation points currently set.

Third, set the capture point (CatchPoint)

You can set the capture point to catch up some of the events when the program runs. Such as: load shared libraries (DLLs) or C ++ exception. Set snap point format:

catch <event>
    When the event occurs, the program stopped. event can have the following contents:
    1, the throw a C ++ exception thrown. (Throw as keywords)
    2, the catch to catch a C ++ exception. (Catch as keywords)
    3, exec exec system call time. (Exec for keywords, present, this function works only in-UX under the HP)
    4, when the fork system call fork. (Fork for keywords, present, this function works only in-UX under the HP)
    5, call the vfork system call vfork. (Vfork for keywords, present, this function works only in-UX under the HP)
    6, the Load or load <libname> load shared libraries (DLL). (Load for keywords, present, this function works only in-UX under the HP)
    7, unload or unload <libname> shared library (DLL) unloaded. (Unload for keywords, this feature is currently only useful in HP-UX)

tcatch <event>
    set point only once captured, the program When stopped, the point should be automatically deleted.

Fourth, the maintenance stop point

It says how to set up the program to stop point, the point is, the above-mentioned three stops in GDB. In GDB, if you feel good stopping point has been defined no use, you can use the delete, clear, disable, enable these commands to perform maintenance.

  • clear

Clear all stop points have been defined.

  • clear <function>
  • clear <filename:function>

Clear all settings stopping point on the function.

  • clear <linenum>
  • clear <filename:linenum>

Clear all settings specified stopping point on the line.

  • delete [breakpoints] [range...]

Delete the specified breakpoints, breakpoints breakpoints number. If you do not specify a breakpoint number, then delete all breakpoints. range number indicates the breakpoint range (eg: 3-7). Its abbreviated command d.


One way better than the deletion is disable stopping point, disable the stop point, GDB will not be deleted when you need to, enable can be, the same as if the Recycle Bin.

  • disable [breakpoints] [range...]

Stop disable the specified point, breakpoints stop point number. If nothing is specified, disable all the stopping point. Abbreviated command is dis.

  • enable [breakpoints] [range...]

Stop enable the designated point, breakpoints stop point number.

  • enable [breakpoints] once range...

enable a designated stopping point, when the program is stopped, the stop point immediately GDB automatically disable.

  • enable [breakpoints] delete range...

enable the designated stopping point once, when the program stops, the GDB stopping point immediately deleted automatically.

Fifth, the stop condition maintenance

Front when it comes to setting breakpoints, we mentioned that you can set a condition when the condition is met, the program automatically stops, this is a very powerful feature, here, I want to talk about this specifically related maintenance order conditions. In general, in order to set a breakpoint condition, if we use the keyword, followed by its break conditions. And, after the conditions set up, we can use the command to modify the condition conditional breakpoints. (Only break and watch commands support if, catch currently not supported if)

  • condition <bnum> <expression>

    Modify breakpoint number bnum stop conditions for expression.

  • condition <bnum>

    Clear breakpoint number bnum stop condition.


There is also a rather special maintenance commands ignore, you can specify the program to run, ignoring stop condition several times.

  • ignore <bnum> <count>

    Means to ignore the breakpoint number bnum stop condition count times.

Sixth, set the Run command to stop point

We can use the GDB command command to set the stop point provided the run command. That is, when the program stopped running when live, we can let it automatically runs some other command, which is very beneficial line of automated debugging. GDB-based debugging automation is a strong support.

  • commands [bnum]
  • ... command-list ...
  • end

Breakpoint number bnum either write a list of commands. When the program is stopped the breakpoint, gdb will list and then click Run Command.

E.g:

  •     break foo if x>0
  •     commands
  •     printf "x is %d/n",x
  •     continue
  •     end

    Function foo breakpoint is set, breakpoints that x> 0, if the program is off to live, i.e., once the value of x is greater than in the function foo 0, GDB will automatically print out the value of x, and continue the program .

If you want to clear sequence of commands on a breakpoint, then simply execute the following commands in the command, and a direct hit end on the line.

Seven Breakpoint menu

In C ++, it may be repeated several times with a function name (function overloading), in this case, break <function> can not tell GDB to stop at the entrance of which function. Of course, you can use the break <function (type)> is the type of the function parameter tells GDB, to specify a function. Otherwise, GDB breakpoint menu will give you a list for you to choose the breakpoint you need. You just enter your menu list of numbers on it. Such as:

(gdb) b String::after
[0] cancel
[1] all
[2] file:String.cc; line number:867
[3] file:String.cc; line number:860
[4] file:String.cc; line number:875
[5] file:String.cc; line number:853
[6] file:String.cc; line number:846
[7] file:String.cc; line number:735
> 2 4 6
Breakpoint 1 at 0xb26c: file String.cc, line 867.
Breakpoint 2 at 0xb344: file String.cc, line 875.
Breakpoint 3 at 0xafcc: file String.cc, line 846.
Multiple breakpoints were set.
Use the "delete" command to delete unwanted
 breakpoints.
(gdb)

Visible, GDB lists all after the overloaded function, you can choose to look at a list of numbers on the list. 0 a waiver to set breakpoints, 1 indicates that all functions are set breakpoints.

Eight, run the recovery program and step through

When the program is stopped, you can continue with the restore command to run the program until the end of the program, or the arrival of the next breakpoint. You can also use the command next step or single-step tracking program.

  • continue [ignore-count]
  • c [ignore-count]
  • fg [ignore-count]

    Recovery program runs until the end of the program, or the arrival of the next breakpoint. ignore-count represents the number of times the breakpoint is ignored thereafter. continue, c, fg three commands are the same meaning.

  • step <count>

        Single-step tracking, if there is a function call, he will enter the function. Function into the premise that this function has been compiled debug information. Like a step in VC and other tools. Count may be added later without, without showing the implementation of an article, the count instructions perform add showing the back, and then stopped.

  • next <count>

    The same single-step tracking, if there is a function call, he will not enter the function. step over like VC and other tools. Count may be added later without, without showing the implementation of an article, the count instructions perform add showing the back, and then stopped.

  • set step-mode
  • set step-mode on

    Open mode step-mode, then, in the one-step tracing, the program does not stop because no debug information. The parameters are very difficult to read machine code.

  • set step-mod off

    Close step-mode mode.

  • finish

    Run the program until the completion of the current function returns. And print function stack return address and returns the information value and argument values.

  • or until u

    When you get tired during a single-step tracking loop body, this command can run the program until you exit the loop.

  • stepi or si
  • nexti some ni

        Single-step tracking a machine instruction! A program code could be completed by a number of machine instructions, stepi nexti and can step machine instruction. Have the same functions as with the command is "display / i $ pc", when after running this command, step tracing will play while playing machine instruction program code (i.e., assembly code)

Nine, signal (Signals)

Signal is a software interrupt, is a way to handle asynchronous events. In general, operating systems support many signals. In particular UNIX, the more important the application process usually signals. UNIX defines a number of signals, such as SIGINT signal represents a break character, which is a signal of Ctrl + C, SIGBUS a signal indicative of a hardware failure; SIGCHLD represent the child process state change signal; SIGKILL signal indicating the termination of the program is running, and so on. Semaphore is a very important programming technique under UNIX.

GDB is capable of handling any kind of signal when you debug a program, you can tell what kind of signal processing GDB needs. You can ask GDB received signal you specify, immediately stop running programs, for your debugging. You can use the handle GDB command to complete this function.

  •     handle <signal> <keywords...>

        A signal processing defined in GDB. Signal <signal> can start with a range which does not begin or SIG SIG, may be defined by a signal to be processed (eg: SIGIO-SIGKILL, it indicates to processing signals from the signal SIGIO SIGKILL including SIGIO, SIGIOT, three signals SIGKILL ), you can also use the keyword all to indicate all of the signals to be processed. Once the program being debugged signal is received, the program running GDB will be stopped immediately, for debugging. Which <keywords> may be one or more of the following keywords.

  • nostop

    When the program being debugged received signal, GDB will not stop running the program, but will play a message telling you to receive such signals.

  • stop

    When the program being debugged received signal, GDB will stop your program.

  • print

    When the program being debugged received signal, GDB will display a message.

  • noprint

    When the program being debugged received signal, GDB will not tell you receive the information signal.

  • pass
  • noignore

    When the program being debugged received signal, GDB does not process the signal. This means that, GDB will be the signal to the debugger handles.

  • nopass
  • ignore

     When the program being debugged received signal, GDB will not allow the debugger to process the signal.

 

  • info signals
  • info handle

     See which signal is detected in GDB.


X. thread (Thread Stops)

If your application is multi-threaded, you can define your breakpoints on whether all the threads, or in a particular thread. GDB can easily help you complete this work.

  • break <linespec> thread <threadno>
  • break <linespec> thread <threadno> if ...

        linespec specify the line number to set a breakpoint in the source program. threadno specified thread ID, note that this ID is assigned to GDB, you can view information about the thread running through the program "info threads" command. If you do not specify a thread <threadno> indicates your breakpoint thread in all the above. You can also specify a thread break conditions. Such as:
   

  • (gdb) break frik.c:13 thread 28 if bartab > lim

When your program is stopped GDB, all running threads will be stopped. This facilitates you to view your overall running program. And when you run the recovery program, all threads will be resumed. Even the main process step debugging sheets.

 

 

 

Published 175 original articles · won praise 262 · views 700 000 +

Guess you like

Origin blog.csdn.net/li_wen01/article/details/105222041