gdb - yszheda/wiki GitHub Wiki

References

pid

(gdb) info inferior

breakpoint

set breakpoint pending on

conditional breakpoint

break at assersion

(gdb) b __assert_fail@plt
#ifdef DEBUG
#  if __GNUC__
#    define assert(c) if (!(c)) __builtin_trap()
#  elif _MSC_VER
#    define assert(c) if (!(c)) __debugbreak()
#  else
#    define assert(c) if (!(c)) *(volatile int *)0 = 0
#  endif
#else
#  define assert(c)
#endif

break at exit

(gdb) break exit

watch point

catch exception

(gdb) catch throw

catch std::bad_alloc

b 'std::bad_alloc::bad_alloc()'

syscall

(gdb) catch syscall mmap

type cast

(gdb) p ('MyScope::MyClass'*) ptr;

Memory

Heap

(gdb) call malloc_stats()
Arena 0:
system bytes     =     135168
in use bytes     =         96
Total (incl. mmap):
system bytes     =     135168
in use bytes     =         96
max mmap regions =          0
max mmap bytes   =          0

(gdb) call malloc_info(0, stdout)
<malloc version="1">
<heap nr="0">
<sizes>
<unsorted from="1228788" to="1229476" total="3917678" count="3221220448"/>
</sizes>
<total type="fast" count="0" size="0"/>
<total type="rest" count="3221220448" size="3917678"/>
<system type="current" size="135168"/>
<system type="max" size="135168"/>
<aspace type="total" size="135168"/>
<aspace type="mprotect" size="135168"/>
</heap>
<total type="fast" count="0" size="0"/>
<total type="rest" count="3221220448" size="3917678"/>
<system type="current" size="135168
/>
<system type="max" size="135168
/>
<aspace type="total" size="135168"/>
<aspace type="mprotect" size="135168"/>
</malloc>

env

step

# step out
(gdb) fin

debug shared library

deadlock

multi-threaded

(gdb) thread apply all bt

multi process

set follow-fork-mode child

debug fork

core

ulimit -c unlimited

dump memory to file

log

(gdb) set logging on
Copying output to gdb.txt
(gdb) set logging off
Done logging to gdb.txt
# Record command
(gdb) set trace-commands on

user-defined commands

(gdb) define fn
> finish
> next
> end

print

print {var1,var2,var3,var4}

build id

objcopy --only-keep-debug "${tostripfile}" "${debugdir}/${debugfile}"
strip --strip-debug --strip-unneeded "${tostripfile}"
objcopy --add-gnu-debuglink="${debugdir}/${debugfile}" "${tostripfile}"

tui

Tools

STL pretty-print

smart pointer

STL pretty-print config

GEF

Python Exception <class 'UnicodeEncodeError'> 'ascii' codec can't encode character

LC_ALL=en_US.UTF-8 gdb 

Trouble-shooting

Left operand of assignment is not an lvalue

Could not attach to process. If your uid matches the uid of the target process, check the setting of /proc/sys/kernel/yama/ptrace_scope, or try again as the root user.

echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope

Missing separate debuginfos

Dwarf Error: wrong version in compilation unit header (is 0, should be 2, 3, or 4)

This package should not be accessible on Python 3

unset PYTHONPATH

reload warning: Source file is more recent than executable.

exit code 139

Warning: couldn't activate thread debugging using libthread_db: Cannot find new threads: generic error

Could not find operator[]

print *(x._M_impl._M_start+0)

string compare in conditional breakpoints

break x:20 if strcmp(y, "hello") == 0

call function containing string params

Failed to find objfile or not a valid file format: [Errno 2] No such file or directory: 'system-supplied DSO at '

coroutine debug

⚠️ **GitHub.com Fallback** ⚠️