引言
不管编程语言如何日新月异、百花齐放、百家争鸣,偶有部分追风者乘云原生之东风采用 Go 语言, 更有少数弄潮儿敢为天下先采用 Rust 语言,但大多数存储底层系统的研发还是钟情于 C/C++ 语言。C 语言是一门面向过程编程语言,具备语法简单、运行效率高、面向过程编程、代码易读易懂等特点; C++ 语言则是一门面向对象编程语言,具备运行高效、数据封装、类型继承、运行时多态、静态绑定、模板泛型、标准模板库 STL 等丰富语言特性,适合开发大型软件代码工程。
在 C/C++ 语言开发调试过程中, 问题困惑是层出不穷的、无处不在的。你可能需要通过单步调试理清楚代码逻辑是否正确;你可能需要定位程序异常退出崩溃问题;你可能需要定位多线程并发死锁问题;你可能优化代码性能瓶颈;你可能确认自己编写或修改的代码是否包含在当前可执行程序里;你可能需要逆向分析其他软件,也可能需要防止被别人逆向破解;程序员在大部分工作时间需要同这些问题战斗。这些问题第一次解决也许需要花费很长时间定位分析,但只有你稍微用心,事前多留心眼,事中善于利用工具,善于思考,事后善于复盘总计,后续碰到类似问题你可能会驾轻就熟,轻车熟路。
作为一名合格的程序员,善于发现工具、熟悉使用工具是一项必备的技能,正所谓“工欲善其事,必先利其器”。言归正传,以下行文,笔者将分享研发过程中常用的工具。
gdb
单步调试程序
示例代码如所示,函数需要从命令函接受两个参数,并利用这两个参数进行赋值操作。
#include <stdlib.h>
#include <iostream>
int main(int argc, char** argv)
{
if (argc < 3) {
std::cerr << "please input more than two arguments" << std::endl;
return -1;
}
int a = atoi(argv[1]);
int b = atoi(argv[2]);
std::cout << "a:" << a << " b:" << b << std::endl;
return 0;
}
如果想要单步调试程序,使用 gcc 或 g++ 编译必选添加 -g 选项,开启代码调试符号功能,如下所示
g++ -g demo2.cc -o demo2
运行 gdb 启动调试命令行控制台,输入 file 命令加载待调试可执行程序
# gdb
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/>.
(gdb) file demo2
Reading symbols from /opt/demo/demo2...done.
使用 break 指令设置运行断点,断点可以是函数名,类名::成员函数名,文件代码行等方式
(gdb) b main
Breakpoint 1 at 0x4008bc: file demo2.cc, line 7.
使用 run 命令启动程序运行,命令后面可以输入运行参数列表
(gdb) r "10" "20"
Starting program: /opt/demo/demo2 "10" "20"
Breakpoint 1, main (argc=3, argv=0x7fffffffe428) at demo2.cc:7
7 if (argc < 3) {
Missing separate debuginfos, use: debuginfo-install glibc-2.17-326.el7_9.x86_64 libgcc-4.8.5-44.el7.x86_64 libstdc++-4.8.5-44.el7.x86_64
使用 n(next) 运行程序下一行,s(step) 跳转达到子函数内,c(continue) 继续运行,p(print) 打印变量,bt(backtrace)显示当前函数调用栈信息,info args 显示函数形参,info local 显示局部变量,info registers 显示寄存器信息,info threads 显示线程信息,f(frame)切换函数栈帧。
(gdb) n
13 std::cout << "a:" << a << " b:" << b << std::endl;
(gdb) p b
$3 = 20
(gdb) info args
argc = 3
argv = 0x7fffffffe428
(gdb) info locals
a = 10
b = 20
(gdb) info registers
rax 0x14 20
rdx 0xa 10
rsi 0x0 0
rdi 0x1999999999999999 1844674407370955161
rbp 0x7fffffffe340 0x7fffffffe340
rsp 0x7fffffffe320 0x7fffffffe320
r8 0x7ffff75b5060 140737343344736
r9 0x7fffffffe6d9 140737488348889
....
(gdb) bt
#0 main (argc=3, argv=0x7fffffffe428) at demo2.cc:13
(gdb)
#0 main (argc=3, argv=0x7fffffffe428) at demo2.cc:13
(gdb) info threads
Id Target Id Frame
* 1 process 2277 "demo2" main (argc=3, argv=0x7fffffffe428) at demo2.cc:13
分析运行崩溃
使用如下所示 ulimit 命令设置 coredump 文件大小
# 设置 coredump 文件大小为无限制
# ulimit -c unlimited
# 查看设置后结构
# ulimit -a
core file size (blocks, -c) unlimited
...
设置程序运行 crash 崩溃后产生 coredump 文件保存路径
# sysctl -w kernel.core_pattern=/var/crash/core.%u.%e.%p
程序崩溃绝大部分与内存访问相关,如代码访问野指针、无效指针、内存访问越界等。下列示例代码 demo3.cc 演示给未分配内存空间的指针赋值产生程序崩溃问题。
#include <stdlib.h>
#include <string.h>
#include <iostream>
void mock_crash()
{
char* buf = NULL;
*buf = 'a';
std::cout << "crash now" << std::endl;
}
int main(int argc, char** argv)
{
mock_crash();
return 0;
}
编译代码时需要添加 -g 选项,保留调试信息,用户后续调试。运行代码产生 core dumped 提示,并且在前面设置 coredump 保存路径下会出现产生的 coredump 文件。
# g++ -g demo3.cc -o demo3
# ./demo3
Segmentation fault (core dumped)
# ll /var/crash/
total 328
-rw------- 1 root root 413696 Jun 5 19:35 core.0.demo3.2710
使用 gdb 加载 coredump 文件,如下图所示,加载成功后,会提示在哪个文件哪个函数哪一行出现异常导致进程崩溃。
# gdb demo3 /var/crash/core.0.demo3.2710
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 /opt/demo/demo3...done.
[New LWP 2710]
Core was generated by `./demo3'.
Program terminated with signal 11, Segmentation fault.
#0 0x00000000004007f1 in mock_crash () at demo3.cc:9
9 *buf = 'a';
定位程序死锁
程序死锁问题在多线程并发程序较为常见,一般出现原因是线程间没处理好锁的同步及互斥逻辑。如下列实例代码 demo4.cc 所示,线程1 先持有锁1, 又去申请锁2, 线程2 持有锁2, 又去申请锁1,形成循环锁等待。
#include <iostream>
#include <thread>
#include <mutex>
#include <chrono>
std::mutex mutex1;
std::mutex mutex2;
void routine1() {
std::unique_lock<std::mutex> lock1(mutex1);
std::cout << "Thr1 acquired mutex1. Trying to acquire mutex2" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(3));
std::unique_lock<std::mutex> lock2(mutex2);
std::cout << "Thr1 acquired mutex2 as well" << std::endl;
}
void routine2() {
std::unique_lock<std::mutex> lock2(mutex2);
std::cout << "Thr2 acquired mutex2. Trying to acquire mutex1." << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(5));
std::unique_lock<std::mutex> lock1(mutex1);
std::cout << "Thr2 acquired mutex1 as well" << std::endl;
}
int main() {
std::thread th1(routine1);
std::thread th2(routine2);
th1.join();
th2.join();
return 0;
}
编译运行代码,出现如下提示,程序卡住不往下继续执行
# g++ -g demo4.cc -std=c++11 -pthread -o demo4
# ./demo4
Thr1 acquired mutex1. Trying to acquire mutex2
Thr2 acquired mutex2. Trying to acquire mutex1.
使用 gdb attach 到运行进程,执行 info threads 查看当前进程所有线程,使用 thread apply all bt 把所有线程函数调用栈打印处理,如下图所示 Thread 2 和 Thread 3 都运行在 __lll_lock_wait 等待,推测可能死锁。
# ps -ef | grep demo4
root 3019 1231 0 20:02 pts/0 00:00:00 ./demo4
# gdb attach 3019
(gdb) info threads
Id Target Id Frame
3 Thread 0x7fef7a45b700 (LWP 3020) "demo4" 0x00007fef7a83854d in __lll_lock_wait () from /lib64/libpthread.so.0
2 Thread 0x7fef79c5a700 (LWP 3021) "demo4" 0x00007fef7a83854d in __lll_lock_wait () from /lib64/libpthread.so.0
* 1 Thread 0x7fef7b470740 (LWP 3019) "demo4" 0x00007fef7a833017 in pthread_join () from /lib64/libpthread.so.0
(gdb) thread apply all bt
Thread 3 (Thread 0x7fef7a45b700 (LWP 3020)):
#0 0x00007fef7a83854d in __lll_lock_wait () from /lib64/libpthread.so.0
#1 0x00007fef7a833e9b in _L_lock_883 () from /lib64/libpthread.so.0
#2 0x00007fef7a833d68 in pthread_mutex_lock () from /lib64/libpthread.so.0
#3 0x000000000040105c in __gthread_mutex_lock (__mutex=0x605320 <mutex2>) at /usr/include/c++/4.8.2/x86_64-redhat-linux/bits/gthr-default.h:748
#4 0x0000000000401618 in std::mutex::lock (this=0x605320 <mutex2>) at /usr/include/c++/4.8.2/mutex:134
#5 0x00000000004019c9 in std::unique_lock<std::mutex>::lock (this=0x7fef7a45ae40) at /usr/include/c++/4.8.2/mutex:511
#6 0x00000000004016bf in std::unique_lock<std::mutex>::unique_lock (this=0x7fef7a45ae40, __m=...) at /usr/include/c++/4.8.2/mutex:443
#7 0x0000000000401188 in routine1 () at demo4.cc:15
#8 0x000000000040295f in std::_Bind_simple<void (*())()>::_M_invoke<>(std::_Index_tuple<>) (this=0xbdc040) at /usr/include/c++/4.8.2/functional:1732
#9 0x00000000004028b9 in std::_Bind_simple<void (*())()>::operator()() (this=0xbdc040) at /usr/include/c++/4.8.2/functional:1720
#10 0x0000000000402852 in std::thread::_Impl<std::_Bind_simple<void (*())()> >::_M_run() (this=0xbdc028) at /usr/include/c++/4.8.2/thread:115
#11 0x00007fef7b013330 in ?? () from /lib64/libstdc++.so.6
#12 0x00007fef7a831ea5 in start_thread () from /lib64/libpthread.so.0
#13 0x00007fef7a55ab0d in clone () from /lib64/libc.so.6
Thread 2 (Thread 0x7fef79c5a700 (LWP 3021)):
#0 0x00007fef7a83854d in __lll_lock_wait () from /lib64/libpthread.so.0
#1 0x00007fef7a833e9b in _L_lock_883 () from /lib64/libpthread.so.0
#2 0x00007fef7a833d68 in pthread_mutex_lock () from /lib64/libpthread.so.0
#3 0x000000000040105c in __gthread_mutex_lock (__mutex=0x6052e0 <mutex1>) at /usr/include/c++/4.8.2/x86_64-redhat-linux/bits/gthr-default.h:748
#4 0x0000000000401618 in std::mutex::lock (this=0x6052e0 <mutex1>) at /usr/include/c++/4.8.2/mutex:134
#5 0x00000000004019c9 in std::unique_lock<std::mutex>::lock (this=0x7fef79c59e40) at /usr/include/c++/4.8.2/mutex:511
#6 0x00000000004016bf in std::unique_lock<std::mutex>::unique_lock (this=0x7fef79c59e40, __m=...) at /usr/include/c++/4.8.2/mutex:443
#7 0x000000000040125d in routine2 () at demo4.cc:25
#8 0x000000000040295f in std::_Bind_simple<void (*())()>::_M_invoke<>(std::_Index_tuple<>) (this=0xbdc2d0) at /usr/include/c++/4.8.2/functional:1732
#9 0x00000000004028b9 in std::_Bind_simple<void (*())()>::operator()() (this=0xbdc2d0) at /usr/include/c++/4.8.2/functional:1720
#10 0x0000000000402852 in std::thread::_Impl<std::_Bind_simple<void (*())()> >::_M_run() (this=0xbdc2b8) at /usr/include/c++/4.8.2/thread:115
#11 0x00007fef7b013330 in ?? () from /lib64/libstdc++.so.6
#12 0x00007fef7a831ea5 in start_thread () from /lib64/libpthread.so.0
#13 0x00007fef7a55ab0d in clone () from /lib64/libc.so.6
Thread 1 (Thread 0x7fef7b470740 (LWP 3019)):
#0 0x00007fef7a833017 in pthread_join () from /lib64/libpthread.so.0
#1 0x00007fef7b0130f7 in std::thread::join() () from /lib64/libstdc++.so.6
#2 0x00000000004012fc in main () at demo4.cc:33
进一步确认,如下图所示
# 查看所有线程, LWP 为轻量级进程,也就是线程
(gdb) info threads
Id Target Id Frame
3 Thread 0x7fef7a45b700 (LWP 3020) "demo4" 0x00007fef7a83854d in __lll_lock_wait () from /lib64/libpthread.so.0
2 Thread 0x7fef79c5a700 (LWP 3021) "demo4" 0x00007fef7a83854d in __lll_lock_wait () from /lib64/libpthread.so.0
* 1 Thread 0x7fef7b470740 (LWP 3019) "demo4" 0x00007fef7a833017 in pthread_join () from /lib64/libpthread.so.0
# 切换到 Thread 3 也就是 LWP 3020
(gdb) thread 3
[Switching to thread 3 (Thread 0x7fef7a45b700 (LWP 3020))]
#0 0x00007fef7a83854d in __lll_lock_wait () from /lib64/libpthread.so.0
# 查看当前线程函数调用栈
(gdb) bt
#0 0x00007fef7a83854d in __lll_lock_wait () from /lib64/libpthread.so.0
#1 0x00007fef7a833e9b in _L_lock_883 () from /lib64/libpthread.so.0
#2 0x00007fef7a833d68 in pthread_mutex_lock () from /lib64/libpthread.so.0
#3 0x000000000040105c in __gthread_mutex_lock (__mutex=0x605320 <mutex2>) at /usr/include/c++/4.8.2/x86_64-redhat-linux/bits/gthr-default.h:748
#4 0x0000000000401618 in std::mutex::lock (this=0x605320 <mutex2>) at /usr/include/c++/4.8.2/mutex:134
#5 0x00000000004019c9 in std::unique_lock<std::mutex>::lock (this=0x7fef7a45ae40) at /usr/include/c++/4.8.2/mutex:511
#6 0x00000000004016bf in std::unique_lock<std::mutex>::unique_lock (this=0x7fef7a45ae40, __m=...) at /usr/include/c++/4.8.2/mutex:443
#7 0x0000000000401188 in routine1 () at demo4.cc:15
#8 0x000000000040295f in std::_Bind_simple<void (*())()>::_M_invoke<>(std::_Index_tuple<>) (this=0xbdc040) at /usr/include/c++/4.8.2/functional:1732
#9 0x00000000004028b9 in std::_Bind_simple<void (*())()>::operator()() (this=0xbdc040) at /usr/include/c++/4.8.2/functional:1720
#10 0x0000000000402852 in std::thread::_Impl<std::_Bind_simple<void (*())()> >::_M_run() (this=0xbdc028) at /usr/include/c++/4.8.2/thread:115
#11 0x00007fef7b013330 in ?? () from /lib64/libstdc++.so.6
#12 0x00007fef7a831ea5 in start_thread () from /lib64/libpthread.so.0
#13 0x00007fef7a55ab0d in clone () from /lib64/libc.so.6
# 切换函数栈帧
(gdb) f 7
#7 0x0000000000401188 in routine1 () at demo4.cc:15
15 std::unique_lock<std::mutex> lock2(mutex2);
# 打印 mutex2 锁,显示改锁当前状态 __lock=2 为已锁定,被 __owner = 3021 这个线程锁定,这个线程正好是 Thread 2 号线程。
(gdb) p mutex2
$1 = {<std::__mutex_base> = {_M_mutex = {__data = {__lock = 2, __count = 0, __owner = 3021, __nusers = 1, __kind = 0, __spins = 0, __elision = 0, __list = {__prev = 0x0,
__next = 0x0}}, __size = "\002\000\000\000\000\000\000\000\315\v\000\000\001", '\000' <repeats 26 times>, __align = 2}}, <No data fields>}