Skip to content
Published at:

GDB

Compiling for Debugging

debugging information:

  • data type of each variable or function
  • the correspondence between source line numbers and addresses in the executable code
bash
$ cc -g main.c
$ cc -g3 main.c     # using the DWARF debugging format

Starting your Program

bash
(gdb) run
(gdb) start
(gdb) starti

set exec-wrapper wrapper
show exec-wrapper
unset exec-wrapper

set startup-with-shell
set startup-with-shell on
set startup-with-shell off
show startup-with-shell

set auto-connect-native-target
set auto-connect-native-target on
set auto-connect-native-target off
show auto-connect-native-target

set disable-randomization
set disable-randomization on
set disable-randomization off
show disable-randomization

set environment varname [=value]
show environment [varname]
unset environment varname

set cwd [directory]
show cwd
cd [directory]
pwd

info terminal
run > outfile
tty /dev/ttyb
set inferior-tty [ tty ]
show inferior-tty


attach process-id

kill

# Debugging Programs with Multiple Threads
 (gdb) info threads
 (gdb) info threads -gid
 thread thread-id   # select thread
 thread apply [thread-id-list | all [-ascending]] [flag]... command     # apply command to thread
 thread name [name] # assigns a name to the current thread
 thread find [regexp] # find thread by name


# Checkopint

(gdb) checkpoint                        # Save a snapshot of the debugged program’s current execution state
(gdb) info checkpoints                  # List all checkpoints
(gdb) restart checkpoint-id             # Restart the program from the checkpoint
(gdb) delete checkpoint checkpoint-id   # Delete the checkpoint

Stoppign and Continuing

breakpoint

A breakpoint makes your program stop whenever a certain point in the program is reached

  • line number
  • function name
  • exact address

watchpoint(data breakpoints)

A watchpoint is a special breakpoint that stops your program when the value of an expression changes

  • vaiable / expression
  • enable / disable / delete

catchpoint()

  • To stop when your program receives a signal, use the handle command
bash
(gdb) info program
# Breakpoints
break location  # Set a breakpoint at the given location
# - a function name
# - a line number
# - an address of an instruction
break
break ... if cond
(gdb) break func if a == 10

# Watchpoints
watch [-l|-location] expr [thread thread-id] [mask maskvalue]
rwatch [-l|-location] expr [thread thread-id] [mask maskvalue]
awatch [-l|-location] expr [thread thread-id] [mask maskvalue]

# Catchpoints

# deletign breakpoints

clear
(gdb) continue

docker 配置

https://stackoverflow.com/questions/45171339/gdb-cannot-attach-to-process

--cap-add=SYS_PTRACE

Note

  • inline
  • optimization