GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7 Copyright (C) 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty"for details. This GDB was configured as "x86_64-redhat-linux-gnu". For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>... Reading symbols from /mnt/memleaktest/test...done. # 符号表已经读取
# 设置断点 # 将断点设置在函数入口 `break 函数名` (gdb) break sumN Breakpoint 1 at 0x400534: file test.c, line 5. // 断点设置在了文件第五行
# 在某一行设置断点 `b 行号` (gdb) b 10 Breakpoint 2 at 0x400559: file test.c, line 10.
# 再次执行后,生成coredump文件 [root@localhost memleaktest]# ./crash 段错误(吐核) [root@localhost memleaktest]# ls core.9256 crash crash.c test test.c
# 使用gdb查看coredump # gdb 程序名 coredump文件名 [root@localhost memleaktest]# gdb crash core.9256 GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7 Copyright (C) 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty"for details. This GDB was configured as "x86_64-redhat-linux-gnu". For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>... Reading symbols from /mnt/memleaktest/crash...done. [New LWP 9256] Core was generated by './crash'. Program terminated with signal 11, Segmentation fault. #0 0x0000000000400501 in core_test () at crash.c:6 6 str[1] = 'T';
# bt 或者 where 命令,可以查看函数调用过程 (gdb) bt #0 0x0000000000400501 in core_test () at crash.c:6 #1 0x000000000040051b in main () at crash.c:13 (gdb) where #0 0x0000000000400501 in core_test () at crash.c:6 #1 0x000000000040051b in main () at crash.c:13
# 在coredump文件中查看变量的值 # 每调用一个函数都会调用一个栈空间 (gdb) print i No symbol "i"in current context. # i在main中 (gdb) f 1 # 跳到main层 #1 0x000000000040051b in main () at crash.c:13 13 core_test(); (gdb) p i # 正确查看 $1 = 10
[root@localhost memleaktest]# ls analysis.10781 core.9256 crash crash.c leak leak.c test test.c
# 查看报告 [root@localhost memleaktest]# cat analysis.10781 ==10781== Memcheck, a memory error detector ==10781== Copyright (C) 2002-2017, and GNU GPLd, by Julian Seward et al. ==10781== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info ==10781== Command: ./leak ==10781== Parent PID: 7555 ==10781== ==10781== Invalid write of size 4 # 4字节无效的写(越界) ==10781== at 0x40054B: leakfun (leak.c:7) ==10781== by 0x40055C: main (leak.c:13) ==10781== Address 0x5205068 is 0 bytes after a block of size 40 allocd ==10781== at 0x4C29F73: malloc (vg_replace_malloc.c:309) ==10781== by 0x40053E: leakfun (leak.c:6) ==10781== by 0x40055C: main (leak.c:13) ==10781== ==10781== ==10781== HEAP SUMMARY: ==10781== in use at exit: 40 bytes in 1 blocks # 分配了40个字节 ==10781== total heap usage: 1 allocs, 0 frees, 40 bytes allocated ==10781== ==10781== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1 # 泄漏了40个字节 ==10781== at 0x4C29F73: malloc (vg_replace_malloc.c:309) ==10781== by 0x40053E: leakfun (leak.c:6) ==10781== by 0x40055C: main (leak.c:13) ==10781== ==10781== LEAK SUMMARY: ==10781== definitely lost: 40 bytes in 1 blocks ==10781== indirectly lost: 0 bytes in 0 blocks ==10781== possibly lost: 0 bytes in 0 blocks ==10781== still reachable: 0 bytes in 0 blocks ==10781== suppressed: 0 bytes in 0 blocks ==10781== ==10781== For lists of detected and suppressed errors, rerun with: -s ==10781== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)