Skip to content

Instantly share code, notes, and snippets.

@GitBubble
Last active November 20, 2018 09:20
Show Gist options
  • Save GitBubble/db5718810705b88a0ddc3f43a1f2a5a2 to your computer and use it in GitHub Desktop.
Save GitBubble/db5718810705b88a0ddc3f43a1f2a5a2 to your computer and use it in GitHub Desktop.
gdb_learn.md

set coredump file

echo '/tmp/core.%e.%p.%t' | sudo tee /proc/sys/kernel/core_pattern

special

(gdb) info sharedlibrary

gdb -ex "layout asm" ./foo gdb -q ./foo -ex "b main" -ex "r"

set history save
set confirm off
set disassemble-next-line on
set disassembly-flavor intel

normal

l : load debug file default f : file

detail

1、examine命令(简写是x)来查看内存地址中的值

gdb x/nfu ADDR
n 是一个正整数,表示显示内存的长度,也就是说从当前地址向后显示几个地址的内容。 f 表示显示的格式,如果地址所指的是字符串,那么格式可以是s,如果地十是指令地址,那么格式可以是i。 u 表示从当前地址往后请求的字节数,如果不指定的话,GDB默认是4个bytes。u参数可以用下面的字符来代替,b表示单字节,h表示双字节,w表示四字 节,g表示八字节。当我们指定了字节长度后,GDB会从指内存定的内存地址开始,读写指定字节,并把其当作一个值取出来。

举例:

(gdb) x/9i 0x7c00 0x7c00: cli
=> 0x7c01: cld
0x7c02: xor %ax,%ax 0x7c04: mov %ax,%ds 0x7c06: mov %ax,%es 0x7c08: mov %ax,%ss 0x7c0a: in $0x64,%al 0x7c0c: test $0x2,%al 0x7c0e: jne 0x7c0a

gdb Commands

Using gdb:

  1. Compile with debugging symbols using the -g flag in gcc.
  2. Run program with gdb:
$ gdb program_name
$ (gdb) r[un] arg1 "arg2" ...

Stepping through code:

Resuming code:

c[ontinue]

Step into (C):

s[tep]

Step over (C):

n[ext]

Step out of a function (C):

f[inish]

Step into (Assembly):

s[tep]i

Step over (Assembly):

n[ext]i

Breakpoints:

Set breakpoint at a line:

b[reak] file:line_num

Set breakpoint at a function:

b[reak] function_name

Delete all break points:

d[elete]

Delete a specific breakpoint:

d[elete] b[reakpoints] breakpoint_number

Registers

Viewing registers:

i[nfo] r[egisters] register_name

Setting registers:

set $register_name = value

Variables:

View a variable:

p[rint] var

Dereference a variable:

p[rint] *var

View reference:

p[rint] &var

View struct fields:

p[rint] stuct_ptr->field
p[rint] stuct.field

Set variable by variable name:

set var = value

Set variable by address:

set {type}address = value

View address of a symbol:

i[nfo] address symbol_name

Call stack:

Print the call stack:

b[ack]t[race]

View code:

View current and following lines:

l[ist]

View current and previous lines:

l[ist] -

View code centered around line_num:

l[ist] line_num

View code centered around function_name:

l[ist] function_name

Miscellaneous

Stop execution:

kill

Quit gdb

q[uit]

References

@GitBubble
Copy link
Author

GitBubble commented Jun 15, 2018

also 👍 we could use

(gdb) generate-core-file -> to generate file

also 🥇 (gdb) core core.12345 -> to use core file

or simply : gdb binary.xx corefile / gdb binary.xx -c corefile

@GitBubble
Copy link
Author

GitBubble commented Jun 16, 2018

@GitBubble
Copy link
Author

@GitBubble
Copy link
Author

@GitBubble
Copy link
Author

@GitBubble
Copy link
Author

x/20s [variable] to see strings

@GitBubble
Copy link
Author

GDB compared with LLDB

http://lldb.llvm.org/lldb-gdb.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment