Linux Programming Basics--GDB (set breakpoints)

Source: Internet
Author: User
Tags throw exception

Linux Programming Basics--GDB (set breakpoints)

After starting GDB, the first thing to do is to set a breakpoint before the program can be debugged. In gdb, breakpoints usually have three types:

Breakpoint (Breakpoint):

Interrupt at the specified location of the code, this is the one we use the most. The command to set a breakpoint is break, which usually has the following way:

    • Break <function> stop when entering a specified function
    • Break <linenum> stops at the specified line number.
    • Break +/-offset stops at the offset line before or after the current line number. Offiset is the natural number.
    • Break Filename:linenum stops at the LineNum line of the source file filename.
    • Break ... if <condition> ... 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.

You can view the current breakpoint information by using the info breakpoints [n] command. In addition, there are a few of the following common commands:

    • Delete Deletes all breakpoints
    • Delete breakpoint [n] Deletes a breakpoint
    • Disable breakpoint [n] Disables a breakpoint
    • Enable breakpoint [n] Enables a breakpoint

Observation Point (Watchpoint):

Interrupts when variables are read, written, or changed, which are commonly used to locate bugs.

    • Break when watch <expr> variable changes
    • Rwatch <expr> variables are interrupted when read
    • Awatch <expr> variable values are interrupted when they are read or written

You can view the current observer information by using the info watchpoints [n] command.

Snap Point (catchpoint):

Snap points are used to catch some events while the program is running. such as: Loading shared libraries (dynamic link libraries), exceptions for C + +, and so on. It is also often used to locate bugs.

The command format of the snap point is: Catch <event>,event can be the following content

    • Throw exception when thrown by C + + Interrupt
    • Catch C + + snaps to an exception when interrupted
    • exec calls the system when calling exec (only useful under certain operating systems)
    • Fork calls when the system calls fork (only useful under certain operating systems)
    • Vfork call System when calling Vfork (only useful under certain operating systems)
    • Load or load <libname> load shared library (only useful under certain operating systems)
    • Unload or unload <libname> uninstalling shared libraries (only useful under certain operating systems)

In addition, there is a tcatch <event>, similar, but he only set a snap point, when the program stops, should be automatically deleted.

The snap point information is viewed in the same way as the code breakpoint commands, and is not introduced here.

Break in a specific thread

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 <linespec> Thread <threadno>
    • Break <linespec> thread <threadno> 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 you can view the thread information in the running program through the "Info Threads" command. If you do not specify thread <threadno> it means that 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 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.

Recovery program run and single-step debugging

In GdB, there are several main commands related to debugging stepping:

    • Continue continue to run the program until the next breakpoint (similar to the F5 in VS)
    • Next stepping step, does not enter the sub-function (similar to the F10 in VS)
    • SETP stepping through a statement, it enters a sub-function (similar to the F11 in VS)
    • Until run to the end of the current statement block
    • Finish runs to the end of the function and jumps out, and prints the function's return value (similar to the shift+f11 of VS)

PS: Most of these commands can be abbreviated to the first letter, in the daily use process, often only enter the first character to execute the command, I marked red is the usual way to use. These commands are used very frequently and can take some additional parameters to achieve advanced functionality, which requires mastery.

Linux Programming Basics--GDB (set breakpoints)

Related Article

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.