C language GDB program debugging notes

Source: Internet
Author: User
Tags print format regular expression


Use GDB to debug the program
Enable the C program debugging function

Compile the program. We can use gcc-S main. c to enable debugging and see the binary assembly. it is more convenient to use-g when compiling a program, not only binary assembly, but also the code itself (note, we want to see the binary structure, you can use objdump and The-dS parameter ).
Test Sample code

# Include
 
Int add_range (int low, int high ){
Int I, sum;
For (I = low; I <= high; I ++ ){
Sum = sum + I;
    }
Return sum;
}
 
Int main (void ){
Int result [100];
Result [0] = add_range (1, 10 );
Result [1] = add_range (1,100 );
Printf ("result [0] = % d \ n result [1] = % d \ n", result [0], result [1]);
Return 0;
}
Common GDB debugging commands
When the computer allocates variables, local variables are stored in the stack, and global variables are stored in the global volume segment. When you enter the function, the variable is placed on the top of the stack, and the variable is removed from the top of the stack when you exit. It grows down from the top of the memory.
When starting GDB, you must add-g to add the code to the binary file. The code is in the specified path, so the code file does not exist.

Start Debugging:

# Gcc 11.c-g-o 11
# Gdb main
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-64. el6_5.2)
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3 +: gnu gpl version 3 or later
This is free software: you are free to change and redistribute it.
There is no warranty, to the extent permitted by law. Type "show copying"
And "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu ".
For bug reporting instructions, please see:
...
Reading symbols from/root/fukai/c/11... done.
Help and display

Gdb provides a shell-like command line environment. You can enter the help command at this prompt to view the available command categories. We can further view the categories of help content. For example, you only need to enter help files.

(Gdb) help files
Specifying and examining files.
 
List of commands:
 
Add-symbol-file -- Load symbols from FILE
Add-symbol-file-from-memory -- Load the symbols out of memory from a dynamically loaded object file
Cd -- Set working directory to DIR for debugger and program being debugged
Core-file -- Use FILE as core dump for examining memory and registers
Directory -- Add directory DIR to beginning of search path for source files
Edit -- Edit specified file or function
Exec-file -- Use FILE as program for getting contents of pure memory
File -- Use FILE as program to be debugged
Forward-search -- Search for regular expression (see regex (3) from last line listed
Generate-core-file -- Save a core file with the current state of the debugged process
List -- List specified function or line
Load -- Dynamically load FILE into the running program
Nosharedlibrary -- Unload all shared object library symbols
Path -- Add directory DIR (s) to beginning of search path for object files
Pwd -- Print working directory
After entering, you can use list 1 to list the source code. Only 10 rows are listed at a time. If you want to start from the first line of display, you need to enter the list at the second time, or press enter without entering anything.
List the code of a specified function: l function name

GDB basic commands
Enter start to start the program. after the function is started, we can use next (abbreviated as n) to control code execution in a row. Note that n does not enter the function. if you want to enter a specified function, you can use s to enter the function.

(Gdb) start
The program being debugged has been started already.
Start it from the beginning? (Y or n) y
Temporary breakpoint 2 at 0x4004f8: file 11.c, line 13.
Starting program:/root/fukai/c/11
 
Temporary breakpoint 2, main () at 11.c: 13
13 result [0] = add_range (1, 10 );
(Gdb) s
Add_range (low = 1, high = 10) at 11.c: 5
5 for (I = low; I <= high; I ++ ){

View the status after entering the function

After using step to enter the current function, the following describes how to view the internal content of the function.
Run the backtrace (bt) command to view the stack frames of function calls.

(Gdb) bt
#0 add_range (low = 1, high = 10) at 11.c: 5
#1 0x0000000000400507 in main () at 11.c: 13

In this case, we can see that the stack frame of the main function is 1 and the stack frame of add_range is 0. We can use the info command to view the local variable value inside the function.

(Gdb) I locals
I = 0
Sum = 0

To view the local variables in the main function, you can also use fram 1 (abbreviated as f) to go to stack frame 1. Next, we basically use p to display the variable content.


(Gdb) p sum
$5 = 6
(Gdb) finish
Run till exit from #0 add_range (low = 1, high = 10) at 11.c: 6
0x0000000000400507 in main () at 11.c: 13
13 result [0] = add_range (1, 10 );
Value returned is $6 = 55

If you want to exit the current function, execute finish to exit the current function and execute other functions.
If you want to modify a variable during use, you can perform the following operations:

(Gdb) set var sum = 0
(Gdb) p sum
$8 = 0
(Gdb) finish
Run till exit from #0 add_range (low = 1, high = 100) at 11.c: 6
0x0000000000000040051c in main () at 11.c: 14
14 result [1] = add_range (1,100 );
Value returned is $9 = 5050
Gdb basic commands

Use brcktrace or bt to View function calls and parameters
Finish runs continuously until the current function ends and waits for the next command.
Frame or f frame number select the specified stack frame
Info or locals to view the local variables of the current stack frame
List or l lists the source code of 10 lines below the last position
The list row number lists the source code of the first 10 lines
List function name to list the source code of a specified function
Run next or n
Print or p print expression. You can use the expression to modify the variable value or call a function.
Quit or q exit the gdb environment
Set var
Start is executed to the first line of the main function.
Step or s: execute the next statement. If there is a function, enter
Advanced Debugging
If we want to view the changes of the specified variable every time, we don't need to use printf to print it every time. We just need to use display.


(Gdb) display sum
1: sum = 1634469985
(Gdb) n
9 sum = sum * 10 + input [1]-comment'
1: sum = 1634469985
(Gdb) n
8 for (I = 0; input [I]! = '\ 0' I ++)
1: sum =-835169374
In this way, each sum is printed and displayed to us. When you do not want to display it, you can use undisplay to cancel the trace display.
Every time this is very tired, we can directly use the break command to open an external breakpoint on the specified line, as shown in

(Gdb) l
3 int main (void ){
4 int sum = 0, I = 0;
5 char input [5];
6 while (1 ){
7 scanf ("% s", input );
8 for (I = 0; input [I]! = '\ 0' I ++)
9 sum = sum * 10 + input [1]-comment'
10 printf ("input = % d \ n", sum );
11}
12 return 0;
(Gdb) B 9
Breakpoint 2 at 0x40056c: file 12.c, line 9.
(Gdb) c
Continuing.
 
Breakpoint 2, main () at 12.c: 9
9 sum = sum * 10 + input [1]-comment'
1: sum =-1254259506

The break can specify a row or a function name. At this time, you need to use the continue command internally to run continuously, instead of running in a single step, the program will automatically stop when it reaches the breakpoint. This will automatically stop the next loop.
You can set multiple breakpoints for one debugging. You can use info to view the configured breakpoints.


(Gdb) I break
Num Type Disp Enb Address What
2 breakpoint keep y 0x000000000040056c in main at 12.c: 9
Breakpoint already hit 6 times

You can use delete break 2 to delete a specified breakpoint.

(Gdb) disable B 3
(Gdb) I B
Num Type Disp Enb Address What
3 breakpoint keep n 0x000000000040059c in main at 12.c: 10
4 breakpoint keep y 0x0000000000400563 in main at 12.c: 8
In this case, disable B 3 is used to disable the breakpoint of the specified Num. As shown above, the disabled breakpoint under Enb is changed to n. In this case, enable 3 can be used to enable the breakpoint.
Conditional breakpoint. We can select the specified condition to enable the breakpoint based on some conditions. For example, if sum is not 0, it is displayed. Now we use the run command to restart the program, and then we can.

(Gdb) break 9 if sum! = 0
 
Breakpoint 4, main () at 12.c: 8
Gdb commands in this section

The break or B row number sets the breakpoint on the specified row.
The break function name sets a breakpoint at the beginning of the specified function.
Break... If... Set breakpoint conditions
Cotinue or c runs programs continuously from the current position
Delete breakpoints checkpoint
Display variable name tracking to view the variable. Its value is displayed every time it arrives.
Disable breakpoints
Enable breakpoint number
Info or I breakpoints
Run or r start from scratch to run the program
Undisplay display trace number cancel trace display
Apart from breakpoints, we may sometimes want to know where the storage unit is modified when the program accesses some storage units, so we can use the observation points.

(Gdb) watch input [5]
Hardware watchpoint 2: input [5]
(Gdb) c
Continuing.
123
Inputs = 123
12312
Hardware watchpoint 2: input [5]
 
Old value = 127 '\ 177'
New value = 0' \ 000'
0x00007ffff7aa1e09 in _ IO_vfscanf_internal () from/lib64/libc. so.6

In this way, modifications are displayed. Note that there is a segment error in the above part. gdb stops when the segment error occurs each time. At this time, we can see which line caused the segment error. gdb shows an error in the _ IO_vfscanf function. We can use the bt command to see if the line caused the error.

Old value = 127 '\ 177'
New value = 0' \ 000'
0x00007ffff7aa1e09 in _ IO_vfscanf_internal () from/lib64/libc. so.6
(Gdb) bt
#0 0x00007ffff7aa1e09 in _ IO_vfscanf_internal () from/lib64/libc. so.6
#1 0x00007ffff7aad44d in _ isoc99_scanf () from/lib64/libc. so.6
#2 0x000000000040056a in main () at 13.c: 10
The error is shown in row 10th of 13.c. Sometimes, when debugging and identifying a segment error, there is no detailed content. The segment error occurs at the end of} brackets. This is a common problem, A segment error may not be generated immediately, but is generated when the function returns.
If you want to display the entire array information, you can perform the following operations.


(Gdb) x/7b input
0x7fffffffe5c0: 49 50 51 0-1 127 0

In this case, we can use the x command to print the content of the specified storage unit. 7b is the print format, B indicates a group of each byte, and 7 indicates printing 7 groups. Print 7 consecutive groups starting from the first byte of the input array.

Watch setting observation points
Info or I watchpoints
X prints the content from the storage unit at the specified position, and regards the content as a byte. It does not tell which variable is used.
Disassembly: disassemble by default, this is the current function of disassembly. To view other functions, you need to specify the function name or address.
Display register information: info registers note that $. For example, p $ esp can print the value of this register in gdb.

View the content of the current program stack: x/10x $ sp

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.