Using GDB for program debugging 1-running a program in GDB

Source: Internet
Author: User

Run the program in GDB

First, start the program

If you do not specify a program when you start GDB, you can use the command file or the Exec-file loader.

Run (R):

The Run (r) command causes the program to start, and you can specify the program parameters in the Run command; It is important to note that after running the run for the first time, running it again without parameters will run the program with the previous parameters.

Set args:

Specifies the parameters of the program. After set args, run runs the program with the specified parameters without parameters. If set args is not too parameter, clear program parameters, run after run the program is not too parameters.

After loading the program, if the program is recompiled, GDB automatically reloads the loaded symbol file based on the change time of the symbol file, so there is no need to reload the file.

Start

Use the start command to make the program stop at the first statement of the main function.

Second, the commissioning of the program is already running

Attach pid:

  Obtain the process number from PS and connect to the process via the Attach command. Attach a process, GDB stops the process first, so it can set breakpoints, execute step, continue, and so on, and if the R command is executed, it will kill the original process.

Detach

Release the process, and the original process will continue to execute.

Third, debug multiple inferiors and programs

GDB allows multiple programs to be debugged under a single session, such as a process fork out two or more processes, or a copy of the current program, which is controlled through inferior.

Info inferiors:

Displays the currently managed inferiors.

Inferior Infono:

Switch to the corresponding inferior.

Add-inferior [-copies N] [-exec executable]:

Add n (the default is 1) inferior, and if you indicate a executable file, you can load the symbol file later via file.

Clone-inferiror [-copies N] [infno]:

Copy the inferior that is numbered infno(the default is current).

Other Related commands:

Remove-inferiors infno

  Detach inferior infno

  Kill Inferiors Infno

  Maint Info program-spaces

Four, commissioning fork

By default, when a program Fork,gdb tracks the parent process, the child processes are unblocked and run endlessly. If you set a breakpoint in the child and execute to the breakpoint, child receives a sigtrap signal, which terminates the program by default.

The first way to debug a child process is to set a sleep in child and then connect to child via attach.

Set Follow-fork-mode mode

Show Follow-fork-mode:

After Linux2.5.46, GDB supports the debugging of the fork function, and with the Set-follow-mode command, sets the following parent or child if the default is not set to follow parent.

Set detach-on-fork mode

Show Detach-on-fork:

On Linux, you can debug the parent and child after the fork by using the Set detach-on fork off command. The parent and child processes are multiple inferiors, see the third section.

Five, multi-threaded debugging

Info Threads:

Displays thread information.

Thread Thread-id:

Switch to the corresponding thread.

Thread name [name]:

Modifies the name of the current thread. The default is the program name.

/******************************************************************

* This article for Bo Master Learning debugging with GdB's reading notes, copyright, reproduced please indicate the source.

*

******************************************************************/

Using GDB for program debugging 1-running a program in GDB

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.