Using the GDB Debug program

Source: Internet
Author: User

Common commands

start
command description
starts executing the program and stops at the first statement of the main function
Run (r) arg  Run the program using the arg parameter
Finish continues to run until the current function is returned
quit (q) exit GDB debugging Environment
list (l) lists the source code, followed by the last location, 10 rows per column
List line number lists source code starting from the first line
list Function name lists the source code for one of the functions
Print (P) var Prints The value of a variable
set var to modify the value of a variable
Break (b) Line number or function name set Breakpoint
Step (s) executes the next line of statements and, if there is a function, enters the function
Next (n) executes the next line of statements
Continue (c) continue to run
Enter repeatedly executes the last command

Start GDB

Executables that are debugged with gdb need the-G option at compile time, otherwise the function name and variable name of the program will not be visible.

1. GDB <program> Debug Program in current directory

2. GDB <program> Core uses GDB to debug both a running program and a core file, and the core is the file that is generated after the program illegally executes the core dump.

3. GDB <program> <PID> If your program is a service program, you can specify the process ID at which the service program runs. GDB will automatically attach up and debug him. The program should be searched in the PATH environment variable.

An example

Source

#include <stdio.h>intAdd_range (intLowintHigh ) {    inti,sum;  for(i=low;i<=high;++i) Sum+=i; returnsum;} intMainvoid) {printf ("1+2+3+...+10=%d\n", Add_range (1,Ten)); return 0;}

Using GCC to compile the program

gcc -g test.c-o test

Running the program, you can get the following results

Using the GDB Debug program

GDB test

Using start to run the program, the program executes to the printf statement

Use Step (s) to perform the next step and enter the Add_range function. The print (p) sum is then printed with the value of sum, and the visible program error occurs because the sum is not initialized.

Using set var to modify the value of sum, type the finish command to continue execution, and it is visible that the function has returned the correct value.

Using the GDB Debug program

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.