在Linux环境下进行C++编程时,调试是一个非常重要的环节。以下是一些常用的调试方法和工具:
GDB是Linux下最强大的调试器之一,可以用来调试C/C++程序。
gdb your_program
break main
run
step
next
print variable_name
backtrace
continue
quit
gdb ./my_program
(gdb) break main
Breakpoint 1 at 0x401136: file my_program.cpp, line 10.
(gdb) run
Starting program: /path/to/my_program
Breakpoint 1, main () at my_program.cpp:10
10 int x = 5;
(gdb) next
11 int y = x + 3;
(gdb) print x
$1 = 5
(gdb) continue
Continuing.
LLDB是LLVM项目的调试器,也可以用来调试C/C++程序。
lldb your_program
breakpoint set --name main
run
step
next
frame variable
thread backtrace
continue
quit
lldb ./my_program
(lldb) breakpoint set --name main
Breakpoint 1: where = my_program`main + 10 at my_program.cpp:10, address = 0x0000000100001136
(lldb) run
Process 12345 launched: '/path/to/my_program' (x86_64)
Process 12345 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
frame #0: 0x0000000100001136 my_program`main + 10
7 int x = 5;
8 int y = x + 3;
9 return 0;
-> 10 }
(lldb) frame variable
x = 5
y = 8
(lldb) continue
Process 12345 resuming
VS Code是一个非常流行的代码编辑器,支持多种调试器,包括GDB和LLDB。
.vscode/launch.json
文件,配置调试器。launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Launch (GDB)",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/your_program",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build"
}
]
}
Valgrind是一个强大的内存调试和分析工具,可以用来检测内存泄漏和非法内存访问。
valgrind --leak-check=full ./your_program
valgrind --leak-check=full ./my_program
==12345== Memcheck, a memory error detector
==12345== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==12345== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==12345== Command: ./my_program
==12345==
==12345== HEAP SUMMARY:
==12345== in use at exit: 0 bytes
==12345== total heap usage: 1 allocs, 1 frees, 1,073,741,824 bytes allocated
==12345==
==12345== All heap blocks were freed -- no leaks are possible
==12345==
==12345== For lists of detected and suppressed errors, rerun with: -s
==12345== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
AddressSanitizer是一个快速的内存错误检测器,可以检测缓冲区溢出、使用未初始化的内存等问题。
在编译时添加-fsanitize=address
选项:
g++ -fsanitize=address -g your_program.cpp -o your_program
./your_program
=================================================================
==12345==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60200000eff0 at pc 0x000000401139 bp 0x7ffd9b5f8a40 sp 0x7ffd9b5f8a38
WRITE of size 4 at 0x60200000eff0 thread T0
#0 0x401138 in main /path/to/your_program.cpp:10
#1 0x7ffff7dc882f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x27882f)
#2 0x401069 in _start (/path/to/your_program+0x401069)
0x60200000eff0 is located 0 bytes to the right of 4-byte region [0x60200000eff0,0x60200000eff4)
allocated by thread T0 here:
#0 0x7ffff7b8c960 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0xe9960)
#1 0x401120 in main /path/to/your_program.cpp:8
SUMMARY: AddressSanitizer: heap-buffer-overflow /path/to/your_program.cpp:10 in main
Shadow bytes around the buggy address:
0x10005fffe000: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x10005fffe010: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x10005fffe020: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x10005fffe030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x10005fffe040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
...
0x10005fffe200: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x10005fffe210: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x10005fffe220: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x10005fffe230: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x10005fffe240: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
=>0x10005fffe250: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x10005fffe260: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x10005fffe270: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x10005fffe280: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x10005fffe290: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x10005fffe2a0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable: 00
Partially addressable: 01 02 03 04 05 06 07
Heap left redzone: fa
Freed heap region: fd
Stack left redzone: f1
Stack mid redzone: f2
Stack right redzone: f3
Stack after return: f5
Stack use after scope: f8
Global redzone: f9
Global init order: f6
Poisoned by user: f7
Container overflow: fc
Array cookie: ac
Intra object redzone: bb
ASan internal: fe
Left alloca redzone: ca
Right alloca redzone: cb
==12345==ABORTING
通过这些工具和方法,你可以在Linux环境下高效地进行C++程序的调试。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:linux c++编程的调试技巧