Linux Advanced Programming--04.gdb Debug Program (set breakpoints)

Source: Internet
Author: User
Tags thread stop

To debug a program that is already running
    1. Under UNIX, use PS to see the PID (Process ID) of the running program, and then use the GDB PID format to hook up the running program.
    2. First, using GDB to correlate the source code, and GDB, in gdb with the attach command to hook up the process PID. Use the detach to cancel the hook process.
Pause/Resume Program run

In the debugger, it is necessary to pause the program, and GDB can conveniently pause the program's operation. You can set up which line of the program stops, under what conditions, stop when you receive a signal, and so on. To make it easier for you to see the variables at run time, as well as the process at run time.

When the process is stopped by GDB, you can use the Info program to see if it is running, the process number, and why it was paused.

In gdb, we can pause in the following ways: Breakpoints (breakpoint), Observation points (watchpoint), Snap points (catchpoint), signal (signals), thread stop (threads Stops). If you want to restore the program to run, you can use the C or Continue command.

Set Breakpoints (breakpoint)

We use the break command to set breakpoints. There are several ways to set breakpoints on the front:

    • Break : Stops when the specified function is entered. The function name can be specified in C + + using the Class::function or functions (type,type) format.
    • Break : Stops at the specified line number.
    • Break +offset/break-offset: The offset line that precedes or follows the current line number is stopped. Offiset is the natural number.
    • Break Filename:linenum: Stops at the LineNum line of the source file filename.
    • Break filename:function: Stops at the entrance of the function function of the source file filename.
    • Break *address: Stops at the memory address where the program is running.
    • When the Break:break command has no parameters, it stops at the next instruction.
    • Break ... if: ... Can be the above parameters, condition indicates the condition, when the condition is established, stop. For example, in an environment-based body, you can set the break if i=100, which means that when I is 100, the program is stopped.

When viewing breakpoints, you can use the info command as follows: (Note: n indicates a breakpoint number)

    • info breakpoints [n]
    • Info break [n]
Setting the Observer point (Watchpoint)

The observer generally observes whether the value of an expression (the variable is also an expression) has changed, and if so, stops the program immediately. We have the following methods to set the observation point:

    • Watch : Set an observer point for the expression (variable) expr. When the value of a quantity expression changes, stop the program immediately.
    • Rwatch : Stops the program when the expression (variable) expr is read.
    • Awatch : Stops the program when the value of an expression (variable) is read or written.
    • Info watchpoints: Lists all the observed points that are currently set.
Set snap point (Catchpoint)

You can set a snap point to catch some events while the program is running. such as: Loading shared libraries (dynamic link libraries) or C + + exceptions. The format of the snap point is:

    • Catch : Stops the program when the event occurs. The event can be something as follows:

      1. Throw an exception thrown by C + +. (Throw is keyword)
      2. Catch an exception that is caught by C + +. (Catch as keyword)
      3. When exec calls the system to call exec. (exec is the keyword, currently this feature is only useful under HP-UX)
      4. Fork calls when the system calls fork. (Fork is the keyword, currently this feature is only useful under HP-UX)
      5. Vfork called when the system calls Vfork. (Vfork is the keyword, currently this feature is only useful under HP-UX)
      6. Load or load when loading a shared library (dynamic link library). (load is a keyword that currently works only on HP-UX)
      7. Unload or unload when uninstalling a shared library (dynamic link library). (Unload is the keyword, currently this feature is only useful under HP-UX)
    • Tcatch : Only one snap point is set, and when the program stops, the point is automatically deleted.

Maintenance Stop Point

It says how to set the stop point of the program, and the stop point in GDB is the three categories mentioned above. In GDB, you can use the Delete, clear, disable, enable commands to maintain a defined stop point if you feel it is useless.

    • Clear: Clears all defined stop points.
    • Clear /Clear : Clears all stop points set on the function.
    • Clear /Clear : Clears all stops that are set on the specified line.
    • delete [breakpoints] [range ...]: Deletes the specified breakpoint and breakpoints the breakpoint number. If you do not specify a breakpoint number, all breakpoints are deleted. Range represents the range of breakpoint numbers (for example: 3-7). Its shorthand command is D.

A better way than delete is disable stop point, disable the stop point, GDB will not delete, when you need, enable, just like the Recycle Bin.

    • disable [breakpoints] [range ...]: Disable the stop point specified, breakpoints is the stop number. If nothing is specified, all disable stops are indicated. The shorthand command is dis.
    • Enable [breakpoints] [range ...]: The stop point specified by enable, breakpoints is the stop number.
    • Enable [breakpoints] once [range ...]: Enable the stop point specified once, when the program stops, the stop is automatically disable by GDB.
    • Enable [breakpoints] Delete [range ...]: Enable the stop point specified once, when the program stops, the stop is automatically deleted by GDB.
Stop Condition Maintenance

In front of the set breakpoint, we mentioned that we can set a condition, when the condition is set up, the program automatically stop, this is a very powerful function, here, I would like to specifically say that the conditions of the relevant maintenance command. Generally, to set a condition for a breakpoint, we use the IF keyword followed by its breakpoint condition. And, when the conditions are set, we can use the condition command to modify the condition of the breakpoint. (Only the break and watch command support If,catch currently does not support if)

    • Condition : The stop condition that modifies the breakpoint number to Bnum is expression.
    • Condition : Clears the stop condition for which the breakpoint number is bnum.

There is also a special maintenance command ignore that you can specify when the program runs, ignoring the stop condition several times.

    • Ignore : Indicates that the stop condition of the breakpoint number bnum is ignored count times.
To set a Run command for a stop point

We can use the command command provided by GDB to set the run of the stop point. In other words, when the running program is stopped, we can let it run some other commands automatically, which is very advantageous for automated debugging. is a powerful support for GDB-based automated debugging.

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

Write a list of commands for the breakpoint number bnum. When the program is stopped by the breakpoint, GDB runs the command in the command list in turn.

For example:

break foo if x>0commandsprintf "x is %d/n",xcontinueend

Breakpoint set in function foo, the breakpoint condition is x>0, if the program is broken, that is, once the value of x in the Foo function is greater than 0,GDB will automatically print out the value of X, and continue to run the program.
If you want to clear a sequence of commands on a breakpoint, simply execute the commands command and just hit the end of the line.

Breakpoint Menu

In C + +, a function with the same name may be repeated several times (function overloading), in which case the break does not tell gdb which function to stop at the entrance. Of course, you can use break to tell gdb the argument type of the function to specify a function. Otherwise, GDB will give you a list of broken orders for you to choose the breakpoints you need. You just have to enter the number in your menu list. 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 6Breakpoint 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)

As you can see, GDB lists all the overloaded functions in after, so you could select the list number. 0 means to discard the set breakpoint, and 1 to indicate that all functions have breakpoints set.

Recovery program run and single-step debugging

When the program is stopped, you can use the Continue command to resume running the program until the program ends, or the next breakpoint arrives. You can also use the step or next command to track the program.

    • Continue [Ignore-count]/C [Ignore-count]/FG [Ignore-count]: The recovery program runs until the end of the program, or the next breakpoint arrives. Ignore-count indicates that the number of subsequent breakpoints is ignored. CONTINUE,C,FG three commands are the same meaning.
    • Step : Single-step tracking, if there is a function call, he will enter the function. The prerequisite for entering a function is that this function is compiled with debug information. Very much like the VC and other tools in the step in. The following can be added to count or not, without adding a line to execute, plus the execution of the following count instructions, and then stop.
    • Next : Also single-step tracking, if there is a function call, he will not enter the function. Very much like the VC and other tools in step over. The following can be added to count or not, without adding a line to execute, plus the execution of the following count instructions, and then stop.
    • Set Step-mode/set Step-mode on: Step-mode mode is turned on, so the program does not stop because it does not have debug information when it is in a single-step tracking. This parameter has a good view of the machine code.
    • Set Step-mod off: Turns off Step-mode mode.
    • Finish: Runs the program until the current function finishes returning. and prints information such as the stack address and return value and parameter values when the function returns.
    • Until/u: When you're tired of stepping through a loop, this command can run the program until you exit the loop body.
    • Stepi/si:
    • Nexti/ni: Single-step tracking of a machine instruction! A program code may be completed by a number of machine instructions, STEPI and Nexti can step into the machine instructions. The same function as the command is "display/i $pc", when the command is finished, the single-step tracking will be typed in the program code while the machine instructions (that is, assembly code)
Signal (signals)

A signal is a soft interrupt and is a method of handling asynchronous events. In general, the operating system supports many signals. In particular, UNIX, a more important application typically processes signals. UNIX defines a number of signals, such as SIGINT to interrupt the character signal, that is, the CTRL + C signal, Sigbus indicates a hardware failure signal, SIGCHLD indicates the child process status change signal, sigkill means to terminate the program running signal, and so on. Semaphore programming is a very important technology under UNIX.

GDB has the ability to handle any kind of signal when you debug a program, and you can tell GDB which signal to handle. You can ask GDB to stop the running program as soon as it receives the signal you specified for debugging. You can use GDB's handle command to accomplish this function.

    • Handle : Defines a signal processing in GDB. The signal can start with the sig or not start with the sig, you can define a range to process the signal (such as: Sigio-sigkill, which represents the processing of signals from the SIGIO signal to SIGKILL, including Sigio,sigiot,sigkill three signals), 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: When the program being debugged receives a signal, GDB does not stop the program from running, but it will give you a message telling you to receive the signal.
      • STOP: When the program being debugged receives a signal, GDB stops your program.
      • Print: GDB displays a message when the program being debugged receives a signal.
      • Noprint: When the program being debugged receives a signal, GDB will not tell you the message that you received the signal.
      • Pass/noignore: GDB does not process signals when the program being debugged receives a signal. This means that GDB will give this signal to the debug program to handle.
      • Nopass/ignore: When the program being debugged receives a signal, GDB does not let the debugger process the signal.
    • Info Signals/info handle: See what signals are being detected by GDB.

Threads (thread Stops)

If your program is multithreaded, you can define whether your breakpoint is on all threads, or on a particular thread. GDB is easy to help you with this work.

Break thread /break thread If ... : linespec Specifies the line number of the source program where the breakpoint is set. THREADNO Specifies the ID of the thread, note that this ID is assigned by GDB, and you can view the thread information in the running program through the "Info Threads" command. If you do not specify thread , your breakpoint is set on all threads.
You can also specify a breakpoint condition for a thread. Such as:

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

When your program is stopped by GDB, all running threads will be stopped. This facilitates you to view the overall situation of the running program. And when you resume the program, all the threads will be restored to run. That is even when the main process is being debugged.



From for notes (Wiz)

Linux Advanced Programming--04.gdb Debug Program (set breakpoints)

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.