For those who debug C language in linux environment, we will explain how to use gdb. For beginners.
It is a debugger. Set breakpoints, step through, You can look inside variables, rewrite them, and so on. Like gcc, it is installed by default on linux.
It is an old tool that operates from the command line. This is useful for debugging in an environment where the IDE debugger cannot be used.
merit: -Can be used in most linux environments -Can be debugged by attaching to a running program
Demerit: -There is no GUI screen. Need to remember commands
① Compile the source file in a debuggable manner gcc -g3 test.c → a.out is generated
(2) Execute the generated object file from the gdb command gdb a.out → After that, gdb will ask you to enter a command.
③ Set a breakpoint before execution b test.c: 256 # Break on line 256 of test.c b main # function main Put a break at the beginning
④ Execution run
① Check the process ID (PID) of the program you want to debug ps -ef | grep a.out
② attach gdb # run with no arguments attach 12345 (pid confirmed in ↑)
After that, you can use gdb to put breakpoints and look at variables. If you want to change the process, you can detach and then attach another process.
Here are the main commands. All are written in abbreviated form. (ex. break-> b)
・ Break related
command | effect |
---|---|
b func1 | Put a breakpoint on the function func1 |
b test.c:123 | test.Put a breakpoint on line 123 of c |
w var1 | Set watchpoint to variable var1 |
i b | Show breakpoint list |
d no | Remove breakpoints corresponding to numbers |
・ Execution related
command | effect |
---|---|
n | Step execution(Run line by line/Function skips) |
s | Step execution(Run line by line/Go inside a function) |
c | Execute processing until the next breakpoint |
f | Execute processing until exiting the current function |
u | Execute processing until exiting the current loop |
ret -1 | Return value of the current function-Forcibly exit as 1 * Subsequent processing will not be executed |
·reference
command | effect |
---|---|
p var1 | See the value of variable var1 (If it is a structure, a.You can refer to member variables with b. If it is a pointer*You can see the contents with fp etc. p strlen(buf)、p buf[3]、p a->You can see b etc. quite flexibly) |
bt | Back trace(Path to call the current function)Show |
l | View source code |
info macro macro name | Check macro definition |
i lo | See all local variables |
・ Rewriting the value
command | effect |
---|---|
p var1=-1 | The value of the variable var1-Change to 1 |
You can use the shortcuts used in bash etc. (Example: Move the cursor to the beginning of the line with Ctrl + a. Reference: How to use readline) It also completes variable names with the Tab key and commands in gdb. (It may not be possible in an old environment)
Execute set print elements 0
to display all information of memory and structure.
(Large arrays and structs are only partially displayed by default)
Press Ctrl-x and then 1 or 2 to enter TUI mode. The screen is divided into two, and you can execute the above command while looking at the source code. http://d.hatena.ne.jp/murase_syuka/20150912/1442021005
For programs that spawn child processes, you can debug the child processes with the following command.
set follow-fork-mode child
http://flex.phys.tohoku.ac.jp/texi/gdb-j/gdb-j_toc.html http://sourceware.org/gdb/current/onlinedocs/gdb/
Recommended Posts