在编程中,调试(Debugging)、性能分析(Profiling)、测试(Testing)、测试驱动开发(TDD)、日志(Logging)、GDB调试器、Sanitizers、Core Dump以及动态追踪(Dynamic Tracing)等工具和技术都是帮助开发者找出并修复代码中的错误(bug)、优化程序性能的重要方法。下面我将分别简要介绍这些概念和技术。
这些技术和工具在软件开发中起着至关重要的作用,它们帮助开发者更好地理解和控制他们的代码,确保程序的质量和性能。
MIT 6.NULL - Debugging and Profiling
脑力劳动debug,借助打印信息思考和推断问题所在
信息除了Printf,还可以Logging,更灵活(可以输出到文件、sockets、remote servers),可复用
Here is an example code that logs messages:
$ python logger.py
# Raw output as with just prints
python logger.py log
# Log formatted output
$ python logger.py log ERROR
# Print only ERROR levels and above
$ python logger.py color
# Color formatted output
利用颜色信息: ANSI escape codes
#!/usr/bin/env bash
for R in $(seq 0 20 255); do
for G in $(seq 0 20 255); do
for B in $(seq 0 20 255); do
printf "\e[38;2;${R};${G};${B}m█\e[0m";
done
done
done
一个简化版 Google’s logging
UNIX系统中第三方库的log常存在/var/log
/var/log/nginx
systemd
, a system daemon that controls many things in your system such as which services are enabled and running
/var/log/journal
,可用journalctl
显示var/log/system.log
,但更多人用system log,用 log show
显示logger "Hello Logs"
# On macOS
log show --last 1m | grep Hello
# On Linux
journalctl --since "1m ago" | grep Hello
C++: gdb
(and its quality of life modification pwndbg
) and lldb
Debugging with GDB 教程
gdb:c(continue), l(ist), s(tep), n(ext), b(reak), p(rint), r(eturn), run, q(uit), watch
up, down
up n
up-silently n
特有:start, finish, cond, disable, where
cond 3 this==0xXXX
参数
gdb --args sleep 20
debug带参数的binary-q
quiet模式-ex
: 执行gdb命令
-ex "run"
bt(backtrace), frame X 进帧
info b
watch -l
同时监视表达式本身和表达式指向的内容
attach $pid
debug正在运行的进程
ptype
打印变量类型;打印stl使用 python pretty print
gcore
attach后制造core文件
call
call (void)'xxx::Logger::setLogLevel'(3)
注意引号让gdb不会因为找不到符号而报错//增加print的可读性
set print pretty on/off
//显示完整 STL 结构
set print elements 0
//显示智能指针对象指向的变量
p ((Object*) my_ptr)->attribute //利用类型转换
p *(my_ptr._M_ptr)
//显示vector内部值
p *(my_vec._M_impl._M_start)@my_vec.size() //打印大小
p *(my_vec._M_impl._M_start+0)
p (my_vec._M_impl._M_start)[N-1] //打印第N个成