You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在GitLab CI/CD流水线中获取GDB运行程序的退出码及崩溃栈信息

GDB包裹测试程序:保留原生退出码+崩溃自动栈跟踪

核心思路

通过GDB内置变量和脚本逻辑,区分程序正常退出与崩溃退出场景:

  • 正常退出时,让GDB返回原程序的退出码,供CI/CD判断测试结果
  • 崩溃(如段错误、断言失败)时,自动打印完整栈跟踪,再返回失败退出码

实现步骤

1. 编写GDB自动处理脚本(可选)

创建gdb_backtrace_script.gdb文件,内容如下:

# 关闭分页,避免输出暂停
set pagination off
# 启动目标测试程序
run
# 分支处理退出状态
if $_exitcode != -1
    # 正常退出:返回原程序的退出码
    quit $_exitcode
else
    # 崩溃退出:打印完整栈跟踪(包含局部变量)
    bt full
    # 可选:添加寄存器状态等更多调试信息
    # info registers
    # 返回失败码,标记CI任务失败
    quit 1
end

2. 调用GDB包裹测试程序

直接通过脚本文件调用:

gdb -q -x gdb_backtrace_script.gdb --args ./test test_case_1.yaml

也可以内嵌命令(适合CI环境,无需额外文件):

gdb -q \
  -ex "set pagination off" \
  -ex "run" \
  -ex "if $_exitcode != -1; quit $_exitcode; else; bt full; quit 1; end" \
  --args ./test test_case_1.yaml

3. GitLab CI/CD集成

在.gitlab-ci.yml的测试任务中配置:

system_test:
  stage: test
  before_script:
    # 确保Runner环境安装GDB(根据镜像调整命令)
    - apt-get update && apt-get install -y gdb
  script:
    - gdb -q -ex "set pagination off" -ex "run" -ex "if $_exitcode != -1; quit $_exitcode; else; bt full; quit 1; end" --args ./test test_case_1.yaml
  allow_failure: false

关键说明

  • $_exitcode:GDB内置变量,程序正常退出时存储其退出码;收到致命信号崩溃时为-1
  • -q:静默启动GDB,屏蔽初始化冗余输出
  • bt full:打印包含局部变量的完整栈跟踪,便于精准定位崩溃原因
  • 崩溃时返回1:确保CI/CD将任务标记为失败,同时保留栈跟踪日志用于排查

内容的提问来源于stack exchange,提问作者Flo Ryan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 23:02:48