gdb/valgrind/coredump to debug c/cpp program

本文介绍了使用GDB调试工具的各种高级技巧,包括如何在循环中使用until命令跳过循环、查看后向追踪(backtrace)、切换到汇编视图(layout asm),以及使用Valgrind跟踪内存错误的具体实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

gdb/valgrind/coredump 调试
1、gdb 调试 while/for 循环

①如果在调试 while/for的时候,可以用until xxx(其中,xxx代表 行号)直接跳转到循环后面的第xxx行。

advance xxx,

参考:

2、gdb + backTrace(後向追蹤)

3、gdb + assembly (彙編/汇编)
在gdb进行 运行/run 的时候,输入 layout asm。同时,可以在 list 和 layout asm之间进行转换。

4、利用Valgrind進行內存的跟蹤
Valgrind ./xxx (其中,xxx是程序的名字)

==4210== Invalid write of size 2
==4210==    at 0x400A321: memcpy (vg_replace_strmem.c:1019)
==4210==    by 0x804E070: Pvs_QueryUserFromDev(char*, char*, char*, int, char*) (Pvs_SockCntl.cpp:1097)
==4210==    by 0x80498A9: QueryAllUserFromDev(char*, char*, char*) (test.cpp:251)
==4210==    by 0x804A6B7: main (test.cpp:669)
==4210==  Address 0x43894b4 is 8 bytes after a block of size 4 free'd
==4210==    at 0x4006AF7: operator delete[](void*) (vg_replace_malloc.c:620)
==4210==    by 0x804AC38: Pvs_TrimToInt(char*, int) (Pvs_CodeCntl.cpp:70)
==4210==    by 0x804DFA9: Pvs_QueryUserFromDev(char*, char*, char*, int, char*) (Pvs_SockCntl.cpp:1087)
==4210==    by 0x80498A9: QueryAllUserFromDev(char*, char*, char*) (test.cpp:251)
==4210==    by 0x804A6B7: main (test.cpp:669)
==4210==  Block was alloc'd at
==4210==    at 0x400801E: operator new[](unsigned int) (vg_replace_malloc.c:416)
==4210==    by 0x804AB98: Pvs_TrimToInt(char*, int) (Pvs_CodeCntl.cpp:55)
==4210==    by 0x804DFA9: Pvs_QueryUserFromDev(char*, char*, char*, int, char*) (Pvs_SockCntl.cpp:1087)
==4210==    by 0x80498A9: QueryAllUserFromDev(char*, char*, char*) (test.cpp:251)
==4210==    by 0x804A6B7: main (test.cpp:669)

5、开启 core dump 等 gdb 输出

REFER: Linux Core Dump


1、5.2 Continuing and Stepping

转载于:https://www.cnblogs.com/xuanyuanchen/p/6109032.html

alibi@alibi-virtual-machine:~/桌面$ ./shell 段错误 (核心已转储) alibi@alibi-virtual-machine:~/桌面$ gdb ./shell GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.2) 9.2 Copyright (C) 2020 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-linux-gnu". Type "show configuration" for configuration details. For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>. Find the GDB manual and other documentation resources online at: <http://www.gnu.org/software/gdb/documentation/>. For help, type "help". Type "apropos word" to search for commands related to "word"... Reading symbols from ./shell... (No debugging symbols found in ./shell) gdb-peda$ run Starting program: /home/alibi/桌面/shell Program received signal SIGSEGV, Segmentation fault. [----------------------------------registers-----------------------------------] EAX: 0xfffffffe EBX: 0xffffd23c --> 0x0 ECX: 0xffffd234 ("/bin//sh") EDX: 0xb ('\x0b') ESI: 0x0 EDI: 0x0 EBP: 0x0 ESP: 0xffffd234 ("/bin//sh") EIP: 0x8049017 --> 0x0 EFLAGS: 0x10246 (carry PARITY adjust ZERO sign trap INTERRUPT direction overflow) [-------------------------------------code-------------------------------------] 0x8049011 <_start+17>: mov al,0xb 0x8049013 <_start+19>: mov edx,eax 0x8049015 <_start+21>: int 0x80 => 0x8049017: add BYTE PTR [eax],al 0x8049019: add BYTE PTR [eax],al 0x804901b: add BYTE PTR [eax],al 0x804901d: add BYTE PTR [eax],al 0x804901f: add BYTE PTR [eax],al [------------------------------------stack-------------------------------------] 0000| 0xffffd234 ("/bin//sh") 0004| 0xffffd238 ("//sh") 0008| 0xffffd23c --> 0x0 0012| 0xffffd240 --> 0x1 0016| 0xffffd244 --> 0xffffd3fe ("/home/alibi/桌面/shell") 0020| 0xf
03-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值