在 Debian 上使用 GCC 的高效调试技巧
一 环境准备与基础编译
sudo apt update && sudo apt install build-essential gdbgcc -g -O0 -o hello hello.c(调试阶段建议用 -O0,避免优化干扰调试)二 使用 GDB 进行源码级调试
gdb ./hellobreak main 或 break hello.c:42run(可带参数:run arg1 arg2)next(不进函数)/ step(进入函数)print x、print myfunc(3)info locals、info argsbacktrace(或 bt)、frame 2continue(或 c)watch i > 10quitgdb -tui ./hellogdb -p <PID>gdb ./hello core(需开启 core dump)三 动态与内存错误检测工具
gcc -g -fsanitize=address -fno-omit-frame-pointer -o app app.c./app,ASan 会在 stderr 输出详细报告。gcc -g -fsanitize=thread -o app app.c -lpthread./app,报告并发数据竞争位置。sudo apt install valgrindvalgrind --leak-check=full ./app四 性能分析与自动化
gcc -pg -O2 -o app app.c./app(生成 gmon.out)gprof ./app gmon.out > analysis.txtsudo apt install clang-tidy cppcheckclang-tidy app.c、cppcheck app.cCC = gcc
CFLAGS = -g -Wall -O0
TARGET = app
SRCS = app.c
all: $(TARGET)
$(TARGET): $(SRCS)
$(CC) $(CFLAGS) -o $(TARGET) $(SRCS)
clean:
rm -f $(TARGET)
make 构建、make clean 清理。五 实用建议与排错要点
ulimit -c unlimited/etc/security/limits.conf 增加 * soft core unlimitedgdb ./app core