잊어버리기 전에 써놓는 GDB 사용 꿀팁들
목차
메모리 내 구조체 확인
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
(gdb) print (struct thread_info *) 0xffff880076cc8000
$7 = (struct thread_info *) 0xffff880076cc8000
(gdb) p *$7
$8 = {task = 0x0 <irq_stack_union>, exec_domain = 0xffff880078dd6000,
flags = 2, status = 4202496, cpu = 0, preempt_count = 0,
addr_limit = {seg = 0}, restart_block = {fn = 0x100000001, {futex = {
uaddr = 0x7800000078, val = 120, flags = 0,
bitset = 2172701856, time = 1024, uaddr2 = 0x400000},
nanosleep = {clockid = 120, rmtp = 0x78 <irq_stack_union+120>,
compat_rmtp = 0xffffffff8180cca0 <fair_sched_class>,
expires = 1024}, poll = {ufds = 0x7800000078, nfds = 120,
has_timeout = 0, tv_sec = 18446744071587286176,
tv_nsec = 1024}}}, sysenter_return = 0x1 <irq_stack_union+1>,
sig_on_uaccess_error = 0, uaccess_err = 0}
(gdb)
조건 break
1
(gdb) break context_switch if next == init_task
bp hit시마다 명령 실행
1
2
3
4
5
6
7
8
9
10
(gdb) b do_mmap_pgoff
Breakpoint 1 at 0xffffffff8111a441: file mm/mmap.c, line 940.
(gdb) command 1
Type commands for when breakpoint 1 is hit, one per line.
End with a line saying just "end".
>print addr
>print len
>print prot
>end
(gdb)
소스코드 확인 (디버그 심볼 있어야함)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[~/src]$ apt-get source coreutils
[~/src]$ sudo apt-get install coreutils-dbgsym
[~/src]$ gdb /bin/ls
GNU gdb (GDB) 7.1-ubuntu
(gdb) list main
1192 ls.c: No such file or directory.
in ls.c
(gdb) directory ~/src/coreutils-7.4/src/
Source directories searched: /home/nelhage/src/coreutils-7.4:$cdir:$cwd
(gdb) list main
1192 }
1193 }
1194
1195 int
1196 main (int argc, char **argv)
1197 {
1198 int i;
1199 struct pending *thispend;
1200 int n_files;
1201
gdb에서 malloc으로 Heap 포인터 받아오기(malloc, kmalloc, vmalloc)
1
2
3
4
5
(gdb) p vmalloc(32)
$4 = (void *) 0xffffc90000372000
(gdb) set {char[25]}$4="thisisaverylongstring\n"
(gdb) x/s $4
0xffffc90000372000: "thisisaverylongstring\n"
References
https://blogs.oracle.com/linux/8-gdb-tricks-you-should-know-v2