[LINUX] First gdb

For those who debug C language in linux environment, we will explain how to use gdb. For beginners.

What is gdb

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 and demerit

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

Usage 1: Start directly from gdb

① 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

How to use 2: Debug a program that is already running

① 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.

Command collection

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

Other tips

More detailed information

http://flex.phys.tohoku.ac.jp/texi/gdb-j/gdb-j_toc.html http://sourceware.org/gdb/current/onlinedocs/gdb/

Recommended Posts

First gdb
First Flask
First draft
Trial gdb
First python-review-
First time python
First Windows 10 hack
First Python 3 ~ First comparison ~
First Django Challenge
First command plugin
First Python ~ Coding 2 ~
First image classifier
First python [O'REILLY]
First Django development
[IOS] First Pyto