【规范】Linux平台C/C++程序版本发布调试规范手册 兼容银河麒麟

Linux平台C/C++程序版本发布调试规范手册

1. 概述

1.1 目的

本手册旨在规范Linux平台下C/C++程序(特别是Qt应用程序)的发布版本调试信息管理,实现在保留优化性能的同时,提供有效的崩溃诊断能力。

1.2 适用范围

  • 使用Qt框架的C++应用程序
  • 需要在生产环境调试的Linux应用程序
  • 需要分离调试信息的发布版本

1.3 核心思想

采用-g -O2同时编译,将调试符号从可执行文件中分离并归档存储,通过.gnu_debuglink建立关联,实现小体积可执行文件与完整调试信息的分离管理。

2. 编译与构建规范

2.1 编译选项配置

makefile 复制代码
# -O2: 启用二级优化,在代码大小和执行速度之间取得良好平衡
#      包含函数内联、指令调度、循环优化等,相比-O1增加约10-30%性能
#      相比-O3更稳定,避免激进优化可能导致的bug
# -g: 生成DWARF格式调试信息,包含符号表、行号、变量类型等信息
#     即使开启优化,也能提供基本调试能力,但部分变量可能被优化掉
#     与-O2同时使用,GCC会进行"优化感知调试",平衡性能和可调试性
# -Wall: 启用GCC认为重要的所有警告,帮助发现常见编程错误
#        包含未使用变量、隐式声明、缺少返回语句、格式字符串问题等
#        约50+个警告检查,是代码质量的基本保障
# -Wextra: 启用额外警告,补充-Wall未包含的检查
#          包含空语句、有符号/无符号比较歧义、缺失字段初始化器等
#          约30+个额外警告,提高代码严谨性
# -fno-omit-frame-pointer: 禁止省略帧指针寄存器(EBP/RBP)
#                          -O2默认会省略帧指针以释放寄存器提升性能
#                          保留帧指针使栈回溯更容易,对调试和性能分析至关重要
#                          性能影响极小(~1%),调试价值巨大
# 使用以下编译选项
CXXFLAGS = -O2 -g -Wall -Wextra -fno-omit-frame-pointer

# -Wl,--build-id: 为可执行文件生成唯一构建标识符(SHA1哈希)
#                 存储在.note.gnu.build-id节区,用于:
#                 1. 唯一标识构建版本
#                 2. 调试器自动匹配对应调试符号
#                 3. 系统包管理器验证文件完整性
#                 格式示例: 20字节十六进制如: 1a2b3c4d5e6f...
LDFLAGS = -Wl,--build-id

2.2 调试信息分离步骤

步骤1:编译带有调试信息的可执行文件
bash 复制代码
# 使用-g -O2编译
qmake "CONFIG+=release" "QMAKE_CXXFLAGS+=-g -Wall -Wextra -fno-omit-frame-pointer" "QMAKE_LDFLAGS+=-Wl,--build-id"
make clean
make -j$(nproc)
步骤2:提取调试符号
bash 复制代码
# 使用objcopy提取调试符号到独立文件
objcopy --only-keep-debug your-app your-app.debug

# 创建剥离调试信息的可执行文件
objcopy --strip-debug --add-gnu-debuglink=your-app.debug your-app

# 验证调试信息链接
readelf -n your-app | grep "Build ID"
objdump -s -j .gnu_debuglink your-app
步骤3:归档调试符号
bash 复制代码
# 创建版本化的调试符号目录
DEBUG_SYMBOL_DIR="/opt/debug-symbols/$(date +%Y%m%d_%H%M%S)_v$(cat version.txt)"
mkdir -p $DEBUG_SYMBOL_DIR

# 复制调试符号和源代码
cp your-app.debug $DEBUG_SYMBOL_DIR/
cp -r src/ $DEBUG_SYMBOL_DIR/source/
cp Makefile $DEBUG_SYMBOL_DIR/
echo "Build ID: $(readelf -n your-app | grep 'Build ID' | awk '{print $3}')" > $DEBUG_SYMBOL_DIR/build-info.txt

# 创建符号链接到最新版本
ln -sfn $DEBUG_SYMBOL_DIR /opt/debug-symbols/latest

3. 部署规范

3.1 生产环境部署

bash 复制代码
# 仅部署剥离调试信息的可执行文件
scp your-app user@production-server:/opt/app/bin/

# 在部署服务器上配置coredump收集
echo "/opt/app/coredumps/core.%e.%p.%t" > /proc/sys/kernel/core_pattern
ulimit -c unlimited

3.2 调试信息存储服务器

bash 复制代码
# 调试符号服务器目录结构
/opt/debug-symbols/
├── 20240115_143022_v1.2.3/
│   ├── your-app.debug
│   ├── source/
│   │   └── (完整源代码)
│   ├── build-info.txt
│   └── dependencies.txt
└── latest -> 20240115_143022_v1.2.3/

4. 崩溃诊断流程

4.1 收集崩溃信息

bash 复制代码
# 生产服务器上收集coredump和相关信息
cd /opt/app/coredumps
cp /proc/$(pidof your-app)/maps ./core.<appname>.<pid>.<timestamp>.maps
cp /opt/app/bin/your-app ./core.<appname>.<pid>.<timestamp>.binary

4.2 配置调试环境

bash 复制代码
# 在调试服务器上准备环境
DEBUG_DIR="/tmp/debug_$(date +%s)"
mkdir -p $DEBUG_DIR

# 复制coredump和相关信息
scp user@production-server:/opt/app/coredumps/core.* $DEBUG_DIR/

# 链接对应的调试符号
ln -s /opt/debug-symbols/latest/your-app.debug $DEBUG_DIR/
ln -s /opt/debug-symbols/latest/source $DEBUG_DIR/source

4.3 使用GDB调试

bash 复制代码
cd $DEBUG_DIR
gdb -q ./core.*.binary -c ./core.* \
  -ex "set debug-file-directory ." \
  -ex "directory ./source" \
  -ex "set substitute-path /build/path ./source" \
  -ex "bt full" \
  -ex "info registers" \
  -ex "info sharedlibrary" \
  -ex "quit"

5. 示例程序:简单Qt应用

5.1 程序代码

main.cpp:

cpp 复制代码
#include <QCoreApplication>
#include <QDebug>
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    int *p = 0x00;
    int v = *p;
    qDebug() << v;
    return a.exec();
}

5.2 项目文件

CrashDemo.pro:

qmake 复制代码
QT -= gui

CONFIG += c++11 console
CONFIG -= app_bundle

QMAKE_CXXFLAGS += -g -O2 -Wall -Wextra -fno-omit-frame-pointer
QMAKE_LFLAGS += -Wl,--build-id -Wl,--strip-debug

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
        main.cpp

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

5.3 构建脚本

build.sh:

bash 复制代码
#!/bin/bash
mkdir build
(
cd build
# 编译配置
export QMAKE_CXXFLAGS="-g -O2 -Wall -Wextra -fno-omit-frame-pointer"
export QMAKE_LFLAGS="-Wl,--build-id -Wl,--strip-debug"

# 清理并编译
qmake "CONFIG+=release" ..
make clean
make -j$(nproc)

# 提取调试符号
objcopy --only-keep-debug CrashDemo CrashDemo.debug
objcopy --strip-debug --add-gnu-debuglink=CrashDemo.debug CrashDemo
)
# 创建版本目录
VERSION="1.0.0"
BUILD_DATE=$(date +%Y%m%d_%H%M%S)
DEBUG_DIR=~/debug-symbols/${BUILD_DATE}_v${VERSION}

mkdir -p ${DEBUG_DIR}
cp -r . $DEBUG_DIR/

# 记录构建信息
echo "Version: $VERSION" > $DEBUG_DIR/build-info.txt
echo "Build Date: $BUILD_DATE" >> $DEBUG_DIR/build-info.txt
echo "Build ID: $(readelf -n build/CrashDemo | grep 'Build ID' | awk '{print $3}')" >> $DEBUG_DIR/build-info.txt
echo "MD5: $(md5sum build/CrashDemo | awk '{print $1}')" >> $DEBUG_DIR/build-info.txt

# 更新最新链接
ln -sfn $DEBUG_DIR ~/debug-symbols/latest

echo "构建完成!"
echo "可执行文件: $(pwd)/build/CrashDemo"
echo "调试符号: $DEBUG_DIR/CrashDemo.debug"

执行构建

复制代码
yeqiang@yeqiang-pc:~/CrashDemo$ bash build.sh 
Info: creating stash file /home/yeqiang/CrashDemo/build/.qmake.stash
rm -f moc_predefs.h
rm -f main.o
rm -f *~ core *.core
g++ -c -pipe -g -O2 -Wall -Wextra -fno-omit-frame-pointer -O2 -std=gnu++11 -Wall -W -D_REENTRANT -fPIC -DQT_DEPRECATED_WARNINGS -DQT_NO_DEBUG -DQT_CORE_LIB -I../../CrashDemo -I. -isystem /usr/include/aarch64-linux-gnu/qt5 -isystem /usr/include/aarch64-linux-gnu/qt5/QtCore -I. -I/usr/lib/aarch64-linux-gnu/qt5/mkspecs/linux-g++ -o main.o ../main.cpp
g++ -Wl,--build-id -Wl,--strip-debug -Wl,-O1 -o CrashDemo main.o   /usr/lib/aarch64-linux-gnu/libQt5Core.so -lpthread   
构建完成!
可执行文件: /home/yeqiang/CrashDemo/build/CrashDemo
调试符号: /home/yeqiang/debug-symbols/20251211_144747_v1.0.0/CrashDemo.debug

查看debug-symbols

复制代码
eqiang@yeqiang-pc:~/CrashDemo$ find ~/debug-symbols/
/home/yeqiang/debug-symbols/
/home/yeqiang/debug-symbols/latest
/home/yeqiang/debug-symbols/20251211_171312_v1.0.0
/home/yeqiang/debug-symbols/20251211_171312_v1.0.0/CrashDemo.pro
/home/yeqiang/debug-symbols/20251211_171312_v1.0.0/CrashDemo.pro.user
/home/yeqiang/debug-symbols/20251211_171312_v1.0.0/main.cpp
/home/yeqiang/debug-symbols/20251211_171312_v1.0.0/build
/home/yeqiang/debug-symbols/20251211_171312_v1.0.0/build/CrashDemo.debug
/home/yeqiang/debug-symbols/20251211_171312_v1.0.0/build/main.o
/home/yeqiang/debug-symbols/20251211_171312_v1.0.0/build/.qmake.stash
/home/yeqiang/debug-symbols/20251211_171312_v1.0.0/build/CrashDemo
/home/yeqiang/debug-symbols/20251211_171312_v1.0.0/build/Makefile
/home/yeqiang/debug-symbols/20251211_171312_v1.0.0/build-info.txt
/home/yeqiang/debug-symbols/20251211_171312_v1.0.0/build.sh

实际git客户端,可以知记录源代码的分支,版本,git checkout即可。

5.4 测试验证

执行模拟崩溃的程序

复制代码
yeqiang@yeqiang-pc:~/CrashDemo$ ./build/CrashDemo
段错误 (核心已转储)
yeqiang@yeqiang-pc:~/CrashDemo$ 

查看崩溃信息

复制代码
yeqiang@yeqiang-pc:~/CrashDemo$ coredumpctl list | grep CrashDemo | tail
Thu 2025-12-11 11:28:02 CST   91478  1000  1000  11 present   /home/yeqiang/build-CrashDemo-unknown-Debug/CrashDemo
Thu 2025-12-11 11:31:18 CST   98283  1000  1000  11 present   /home/yeqiang/CrashDemo/build/CrashDemo
Thu 2025-12-11 14:54:09 CST  157843  1000  1000  11 present   /home/yeqiang/CrashDemo/build/CrashDemo
Thu 2025-12-11 17:13:46 CST  211852  1000  1000  11 present   /home/yeqiang/CrashDemo/build/CrashDemo

导出coredump

复制代码
yeqiang@yeqiang-pc:~/CrashDemo$ coredumpctl dump 211852 > 211852.dump
           PID: 211852 (CrashDemo)
           UID: 1000 (yeqiang)
           GID: 1000 (yeqiang)
        Signal: 11 (SEGV)
     Timestamp: Thu 2025-12-11 17:13:46 CST (53s ago)
  Command Line: ./build/CrashDemo
    Executable: /home/yeqiang/CrashDemo/build/CrashDemo
 Control Group: /user.slice/user-1000.slice/session-4.scope
          Unit: session-4.scope
         Slice: user-1000.slice
       Session: 4
     Owner UID: 1000 (yeqiang)
       Boot ID: 41382c864cab4c01a73df0f7dcd18060
    Machine ID: a4db26d5748b4607a57f7ec218a98c6a
      Hostname: yeqiang-pc
       Storage: /var/lib/systemd/coredump/core.CrashDemo.1000.41382c864cab4c01a73df0f7dcd18060.211852.1765444426000000000000.lz4
       Message: Process 211852 (CrashDemo) of user 1000 dumped core.
                
                Stack trace of thread 211852:
                #0  0x000000557a37eb20 n/a (/home/yeqiang/CrashDemo/build/CrashDemo + 0xb20)
                #1  0x000000557a37eb08 n/a (/home/yeqiang/CrashDemo/build/CrashDemo + 0xb08)
                #2  0x0000007f9f834d90 __libc_start_main (libc.so.6 + 0x20d90)
yeqiang@yeqiang-pc:~/CrashDemo$ ll 211852.dump 
-rw-rw-r-- 1 yeqiang yeqiang 856064 12月 11 17:14 211852.dump
yeqiang@yeqiang-pc:~/CrashDemo$ 

gdb调试

bash 复制代码
yeqiang@yeqiang-pc:~/CrashDemo$ gdb -q ~/debug-symbols/latest/build/CrashDemo -c 211852.dump \
> -ex "set debug-file-directory ~/debug-symbols/latest/build/" \
> -ex "directory ~/debug-symbols/latest/" \
> -ex "bt full" \
> -ex "info source" \
> -ex "quit"
Reading symbols from /home/yeqiang/debug-symbols/latest/build/CrashDemo...
Reading symbols from /home/yeqiang/debug-symbols/20251211_171312_v1.0.0/build/CrashDemo.debug...
[New LWP 211852]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/aarch64-linux-gnu/libthread_db.so.1".
Core was generated by `./build/CrashDemo'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x000000557a37eb20 in main (argc=<optimized out>, argv=<optimized out>) at ../main.cpp:7
7	    int v = *p;
Source directories searched: /home/yeqiang/debug-symbols/latest:$cdir:$cwd
#0  0x000000557a37eb20 in main (argc=<optimized out>, argv=<optimized out>) at ../main.cpp:7
        a = <incomplete type>
        v = <optimized out>
Current source file is ../main.cpp
Compilation directory is /home/yeqiang/CrashDemo/build
Contains 10 lines.
Source language is c++.
Producer is GNU C++11 9.3.0 -mlittle-endian -mabi=lp64 -g -O2 -O2 -std=gnu++11 -fno-omit-frame-pointer -fPIC -fasynchronous-unwind-tables -fstack-protector-strong -fstack-clash-protection.
Compiled with DWARF 2 debugging format.
Does not include preprocessor macro info.

6. 自动化脚本

6.1 自动化构建部署脚本

deploy_pipeline.sh:

bash 复制代码
#!/bin/bash

# 参数检查
if [ $# -ne 2 ]; then
    echo "用法: $0 <版本号> <部署目标>"
    exit 1
fi

VERSION=$1
TARGET=$2

# 1. 编译
echo "步骤1: 编译..."
source build.sh

# 2. 打包发布文件
echo "步骤2: 打包..."
tar czf CrashDemo-${VERSION}.tar.gz CrashDemo README.md

# 3. 部署到目标服务器
echo "步骤3: 部署到${TARGET}..."
scp CrashDemo-${VERSION}.tar.gz ${TARGET}:/opt/app/
ssh ${TARGET} "cd /opt/app && tar xzf CrashDemo-${VERSION}.tar.gz"

# 4. 记录部署信息
echo "${VERSION} $(date '+%Y-%m-%d %H:%M:%S') ${TARGET}" >> /opt/debug-symbols/deploy.log

echo "部署完成!"

6. 注意事项

6.1 安全考虑

  1. 调试符号服务器应限制访问权限
  2. 源代码存储应加密或放在内网环境
  3. 生产环境不应包含任何调试信息

6.2 版本管理

  1. 每次构建必须记录Build ID
  2. 调试符号必须与可执行文件版本严格对应
  3. 保留重要版本的调试符号至少6个月

6.3 性能影响

  1. -g -O2组合对性能影响极小(约1-3%)
  2. 分离调试信息后,可执行文件大小与纯Release版本基本一致
  3. 运行时无额外内存开销

6.4 兼容性

  1. 确保调试服务器与生产环境GCC版本一致
  2. 保持glibc版本兼容
  3. 注意内核版本差异对coredump的影响

7. 附录

7.1 常用命令参考

bash 复制代码
# 查看调试信息
readelf -S binary | grep debug
objdump -g binary | head -50

# 验证调试链接
readelf -x .gnu_debuglink binary

# 查找对应调试文件
debugedit --find-debuginfo ./binary

# 批量处理调试符号
find /opt/debug-symbols -name "*.debug" -exec objcopy --add-gnu-debuglink={} /opt/app/bin/{} \;

7.2 环境配置检查清单

  • 生产环境core pattern配置正确
  • ulimit -c设置为unlimited
  • 调试符号服务器目录可访问
  • 版本对应关系文档完整
  • 构建服务器GCC版本与生产环境一致

总结

本方案通过-g -O2编译并分离调试信息,实现了:

  1. 性能接近纯Release版本(-O2优化)
  2. 完整的调试能力(保留-g信息)
  3. 安全的部署(生产环境无源码和符号)
  4. 高效的崩溃诊断(快速定位问题)

通过标准化流程和自动化脚本,确保从开发到部署再到问题诊断的完整链路可追溯、可重现、可维护。

附:C语言程序快速上手

a.c

bash 复制代码
#include<stdio.h>

int main(){
	int a=0;
	int *b = 0x00;
	a = *b;
	printf("%d\n", a);
	return 0;
}

编译

bash 复制代码
yeqiang@yeqiang-pc:~/code/crashTest$ gcc -O2 -g -Wall -Wextra -fno-omit-frame-pointer -Wl,--build-id a.c
yeqiang@yeqiang-pc:~/code/crashTest$ ll
总用量 24
drwxrwxr-x 2 yeqiang yeqiang  4096 12月 10 16:28 ./
drwxrwxr-x 4 yeqiang yeqiang  4096 12月 10 16:12 ../
-rw-rw-r-- 1 yeqiang yeqiang    99 12月 10 16:27 a.c
-rwxrwxr-x 1 yeqiang yeqiang 11912 12月 10 16:28 a.out*
yeqiang@yeqiang-pc:~/code/crashTest$ 

提取调试符号

bash 复制代码
# 使用objcopy提取调试符号到独立文件
yeqiang@yeqiang-pc:~/code/crashTest$ objcopy --only-keep-debug a.out a.out.debug
yeqiang@yeqiang-pc:~/code/crashTest$ ll -a
总用量 36
drwxrwxr-x 2 yeqiang yeqiang  4096 12月 10 16:28 ./
drwxrwxr-x 4 yeqiang yeqiang  4096 12月 10 16:12 ../
-rw-rw-r-- 1 yeqiang yeqiang    99 12月 10 16:27 a.c
-rwxrwxr-x 1 yeqiang yeqiang 11912 12月 10 16:28 a.out*
-rwxrwxr-x 1 yeqiang yeqiang  8464 12月 10 16:28 a.out.debug*



# 创建剥离调试信息的可执行文件
yeqiang@yeqiang-pc:~/code/crashTest$ objcopy --strip-debug --add-gnu-debuglink=a.out.debug a.out
yeqiang@yeqiang-pc:~/code/crashTest$ ll -a
总用量 36
drwxrwxr-x 2 yeqiang yeqiang 4096 12月 10 16:29 ./
drwxrwxr-x 4 yeqiang yeqiang 4096 12月 10 16:12 ../
-rw-rw-r-- 1 yeqiang yeqiang   99 12月 10 16:27 a.c
-rwxrwxr-x 1 yeqiang yeqiang 8856 12月 10 16:29 a.out*
-rwxrwxr-x 1 yeqiang yeqiang 8464 12月 10 16:28 a.out.debug*

# 验证调试信息链接
yeqiang@yeqiang-pc:~/code/crashTest$ readelf -n a.out | grep "Build ID"
    Build ID: 5717d1e82a85e7ee7b771430b235c42df50d6484
yeqiang@yeqiang-pc:~/code/crashTest$ readelf -n a.out.debug | grep "Build ID"
    Build ID: 5717d1e82a85e7ee7b771430b235c42df50d6484
yeqiang@yeqiang-pc:~/code/crashTest$ objdump -s -j .gnu_debuglink a.out

a.out:     文件格式 elf64-littleaarch64

Contents of section .gnu_debuglink:
 0000 612e6f75 742e6465 62756700 3d240619  a.out.debug.=$..

运行程序,触发crash

bash 复制代码
yeqiang@yeqiang-pc:~/code/crashTest$ ./a.out
段错误 (核心已转储)
yeqiang@yeqiang-pc:~/code/crashTest$ 

查看coredump列表

bash 复制代码
yeqiang@yeqiang-pc:~/code/crashTest$ coredumpctl list | tail
Tue 2025-09-30 16:43:54 CST    1914  1000  1000   6 missing   /usr/bin/kylin-software-center-plugin-synchrodata
Sun 2025-10-12 12:20:48 CST    1907  1000  1000   6 missing   /usr/bin/kylin-software-center-plugin-synchrodata
Sat 2025-11-22 19:53:15 CST    1867  1000  1000   6 missing   /usr/bin/kylin-software-center-plugin-synchrodata
Mon 2025-12-08 09:55:00 CST    1800  1000  1000   6 missing   /usr/bin/kylin-software-center-plugin-synchrodata
Tue 2025-12-09 08:06:12 CST    1903  1000  1000   6 missing   /usr/bin/kylin-software-center-plugin-synchrodata
Wed 2025-12-10 14:19:53 CST    1898  1000  1000   6 missing   /usr/bin/kylin-software-center-plugin-synchrodata
Wed 2025-12-10 14:19:56 CST    2117  1000  1000  11 missing   /usr/bin/fcitx-dbus-watcher
Wed 2025-12-10 16:27:06 CST   86083  1000  1000  11 missing   /home/yeqiang/code/crashTest/a.out
Wed 2025-12-10 16:30:37 CST   92797  1000  1000  11 missing   /home/yeqiang/code/crashTest/a.out
Wed 2025-12-10 16:38:27 CST  100353  1000  1000  11 present   /home/yeqiang/code/crashTest/a.out

调试

方法1:开发机上直接调试(有编译环境,源码、debug文件路径一直)

bash 复制代码
yeqiang@yeqiang-pc:~/code/crashTest$ coredumpctl debug a.out
           PID: 100353 (a.out)
           UID: 1000 (yeqiang)
           GID: 1000 (yeqiang)
        Signal: 11 (SEGV)
     Timestamp: Wed 2025-12-10 16:38:27 CST (50min ago)
  Command Line: ./a.out
    Executable: /home/yeqiang/code/crashTest/a.out
 Control Group: /user.slice/user-1000.slice/session-4.scope
          Unit: session-4.scope
         Slice: user-1000.slice
       Session: 4
     Owner UID: 1000 (yeqiang)
       Boot ID: 81b7199eee6e446a8d1e2f6a6c4a1180
    Machine ID: a4db26d5748b4607a57f7ec218a98c6a
      Hostname: yeqiang-pc
       Storage: /var/lib/systemd/coredump/core.a\x2eout.1000.81b7199eee6e446a8d1e2f6a6c4a1180.100353.1765355907000000000000.lz4
       Message: Process 100353 (a.out) of user 1000 dumped core.
                
                Stack trace of thread 100353:
                #0  0x000000557447b614 n/a (/home/yeqiang/code/crashTest/a.out + 0x614)
                #1  0x0000007fba9ced90 __libc_start_main (libc.so.6 + 0x20d90)

GNU gdb (Ubuntu 9.1-0kylin1) 9.1
Copyright (C) 2020 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 "aarch64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /home/yeqiang/code/crashTest/a.out...
Reading symbols from /home/yeqiang/code/crashTest/a.out.debug...
[New LWP 100353]
Core was generated by `./a.out'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x000000557447b614 in main () at a.c:6
6		a = *b;
(gdb) 

方法2:从故障服务器复制到开发机调试

bash 复制代码
yeqiang@yeqiang-pc:~/release$ pwd
/home/yeqiang/release
yeqiang@yeqiang-pc:~/release$ find
.
./debuginfo
./debuginfo/a.out.debug
./a.out


yeqiang@yeqiang-pc:~/release$ ./a.out 
段错误 (核心已转储)
yeqiang@yeqiang-pc:~/release$ coredumpctl list | tail
Sun 2025-10-12 12:20:48 CST    1907  1000  1000   6 missing   /usr/bin/kylin-software-center-plugin-synchrodata
Sat 2025-11-22 19:53:15 CST    1867  1000  1000   6 missing   /usr/bin/kylin-software-center-plugin-synchrodata
Mon 2025-12-08 09:55:00 CST    1800  1000  1000   6 missing   /usr/bin/kylin-software-center-plugin-synchrodata
Tue 2025-12-09 08:06:12 CST    1903  1000  1000   6 missing   /usr/bin/kylin-software-center-plugin-synchrodata
Wed 2025-12-10 14:19:53 CST    1898  1000  1000   6 missing   /usr/bin/kylin-software-center-plugin-synchrodata
Wed 2025-12-10 14:19:56 CST    2117  1000  1000  11 missing   /usr/bin/fcitx-dbus-watcher
Wed 2025-12-10 16:27:06 CST   86083  1000  1000  11 missing   /home/yeqiang/code/crashTest/a.out
Wed 2025-12-10 16:30:37 CST   92797  1000  1000  11 missing   /home/yeqiang/code/crashTest/a.out
Wed 2025-12-10 16:38:27 CST  100353  1000  1000  11 present   /home/yeqiang/code/crashTest/a.out
Wed 2025-12-10 17:37:00 CST  125313  1000  1000  11 present   /home/yeqiang/release/a.out
直接导出coredump.lz4文件方案

查看路径

复制代码
yeqiang@yeqiang-pc:~/release$ coredumpctl list | tail
Sat 2025-11-22 19:53:15 CST    1867  1000  1000   6 missing   /usr/bin/kylin-software-center-plugin-synchrodata
Mon 2025-12-08 09:55:00 CST    1800  1000  1000   6 missing   /usr/bin/kylin-software-center-plugin-synchrodata
Tue 2025-12-09 08:06:12 CST    1903  1000  1000   6 missing   /usr/bin/kylin-software-center-plugin-synchrodata
Wed 2025-12-10 14:19:53 CST    1898  1000  1000   6 missing   /usr/bin/kylin-software-center-plugin-synchrodata
Wed 2025-12-10 14:19:56 CST    2117  1000  1000  11 missing   /usr/bin/fcitx-dbus-watcher
Wed 2025-12-10 16:27:06 CST   86083  1000  1000  11 missing   /home/yeqiang/code/crashTest/a.out
Wed 2025-12-10 16:30:37 CST   92797  1000  1000  11 missing   /home/yeqiang/code/crashTest/a.out
Wed 2025-12-10 16:38:27 CST  100353  1000  1000  11 present   /home/yeqiang/code/crashTest/a.out
Wed 2025-12-10 17:37:00 CST  125313  1000  1000  11 present   /home/yeqiang/release/a.out
Thu 2025-12-11 08:05:17 CST    1857  1000  1000   6 present   /usr/bin/kylin-software-center-plugin-synchrodata
yeqiang@yeqiang-pc:~/release$ coredumpctl info 125313
           PID: 125313 (a.out)
           UID: 1000 (yeqiang)
           GID: 1000 (yeqiang)
        Signal: 11 (SEGV)
     Timestamp: Wed 2025-12-10 17:37:00 CST (16h ago)
  Command Line: ./a.out
    Executable: /home/yeqiang/release/a.out
 Control Group: /user.slice/user-1000.slice/session-4.scope
          Unit: session-4.scope
         Slice: user-1000.slice
       Session: 4
     Owner UID: 1000 (yeqiang)
       Boot ID: 81b7199eee6e446a8d1e2f6a6c4a1180
    Machine ID: a4db26d5748b4607a57f7ec218a98c6a
      Hostname: yeqiang-pc
       Storage: /var/lib/systemd/coredump/core.a\x2eout.1000.81b7199eee6e446a8d1e2f6a6c4a1180.125313.1765359420000000000000.lz4
       Message: Process 125313 (a.out) of user 1000 dumped core.
                
                Stack trace of thread 125313:
                #0  0x000000558feca614 n/a (/home/yeqiang/release/a.out + 0x614)
                #1  0x0000007f86664d90 __libc_start_main (libc.so.6 + 0x20d90)

得到

复制代码
/var/lib/systemd/coredump/core.a\x2eout.1000.81b7199eee6e446a8d1e2f6a6c4a1180.125313.1765359420000000000000.lz4

复制原始dump文件(lz4格式)

复制代码
yeqiang@yeqiang-pc:~/release$  cp "/var/lib/systemd/coredump/core.a\x2eout.1000.81b7199eee6e446a8d1e2f6a6c4a1180.125313.1765359420000000000000.lz4" 125313.dump.lz4
yeqiang@yeqiang-pc:~/release$ ll
总用量 48
drwxrwxr-x  3 yeqiang yeqiang  4096 12月 11 10:07 ./
drwx------ 45 yeqiang yeqiang  4096 12月 11 08:05 ../
-rw-r-----  1 yeqiang yeqiang 23663 12月 11 10:07 125313.dump.lz4
-rwxrwxr-x  1 yeqiang yeqiang  8856 12月 10 16:29 a.out*
drwxrwxr-x  2 yeqiang yeqiang  4096 12月 10 17:36 debuginfo/

解压

复制代码
yeqiang@yeqiang-pc:~/release$ lz4 -d 125313.dump.lz4 125313.dump
125313.dump.lz4      : decoded 225280 bytes                                    
yeqiang@yeqiang-pc:~/release$ ll
总用量 380
drwxrwxr-x  3 yeqiang yeqiang   4096 12月 11 10:18 ./
drwx------ 45 yeqiang yeqiang   4096 12月 11 08:05 ../
-rw-r-----  1 yeqiang yeqiang 225280 12月 11 10:07 125313.dump
-rw-r-----  1 yeqiang yeqiang  23663 12月 11 10:07 125313.dump.lz4
-rwxrwxr-x  1 yeqiang yeqiang   8856 12月 10 16:29 a.out*
drwxrwxr-x  2 yeqiang yeqiang   4096 12月 10 17:36 debuginfo/

gdb调试

复制代码
yeqiang@yeqiang-pc:~/release$ gdb a.out 
GNU gdb (Ubuntu 9.1-0kylin1) 9.1
Copyright (C) 2020 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 "aarch64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from a.out...
(No debugging symbols found in a.out)
(gdb) symbol-file debuginfo/a.out.debug 
Reading symbols from debuginfo/a.out.debug...
(gdb) core-file 125313.dump
[New LWP 125313]
Core was generated by `./a.out'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x000000558feca614 in main () at a.c:6
6		a = *b;
(gdb) bt full
#0  0x000000558feca614 in main () at a.c:6
No locals.
(gdb) list 
1	#include<stdio.h>
2	
3	int main(){
4		int a=0;
5		int *b = 0x00;
6		a = *b;
7		printf("%d\n", a);
8		return 0;
9	}
(gdb) 
方法2:使用dump指令
复制代码
yeqiang@yeqiang-pc:~/release$ coredumpctl dump 125313 -o 125313_core.dump
           PID: 125313 (a.out)
           UID: 1000 (yeqiang)
           GID: 1000 (yeqiang)
        Signal: 11 (SEGV)
     Timestamp: Wed 2025-12-10 17:37:00 CST (16h ago)
  Command Line: ./a.out
    Executable: /home/yeqiang/release/a.out
 Control Group: /user.slice/user-1000.slice/session-4.scope
          Unit: session-4.scope
         Slice: user-1000.slice
       Session: 4
     Owner UID: 1000 (yeqiang)
       Boot ID: 81b7199eee6e446a8d1e2f6a6c4a1180
    Machine ID: a4db26d5748b4607a57f7ec218a98c6a
      Hostname: yeqiang-pc
       Storage: /var/lib/systemd/coredump/core.a\x2eout.1000.81b7199eee6e446a8d1e2f6a6c4a1180.125313.1765359420000000000000.lz4
       Message: Process 125313 (a.out) of user 1000 dumped core.
                
                Stack trace of thread 125313:
                #0  0x000000558feca614 n/a (/home/yeqiang/release/a.out + 0x614)
                #1  0x0000007f86664d90 __libc_start_main (libc.so.6 + 0x20d90)
yeqiang@yeqiang-pc:~/release$ ll
总用量 268
drwxrwxr-x  3 yeqiang yeqiang   4096 12月 11 10:09 ./
drwx------ 45 yeqiang yeqiang   4096 12月 11 08:05 ../
-rw-rw-r--  1 yeqiang yeqiang 225280 12月 11 10:09 125313_core.dump
-rw-r-----  1 yeqiang yeqiang  23663 12月 11 10:07 125313.dump.lz4
-rwxrwxr-x  1 yeqiang yeqiang   8856 12月 10 16:29 a.out*
drwxrwxr-x  2 yeqiang yeqiang   4096 12月 10 17:36 debuginfo/

后续操作方法与方法1一致。

相关推荐
model20052 小时前
Alibaba linux 3安装LAMP(6)
linux·运维·服务器
wjykp2 小时前
1.vmware虚拟机安装和配置os
linux·运维·服务器
诺....2 小时前
C语言不确定循环会影响输入输出缓冲区的刷新
c语言·数据结构·算法
老王熬夜敲代码2 小时前
C++中的mutex、condition_val
c++·笔记·面试
闻缺陷则喜何志丹2 小时前
【计算几何 二分查找】P12261 [蓝桥杯 2024 国 Java B] 激光炮|普及+
c++·数学·蓝桥杯·计算几何·洛谷
Better Bench3 小时前
Ubuntu aarch64 (arm64)架构开发板的Linux系统安装拼音输入法
linux·ubuntu·arm64·aarch64·拼音输入法
清轩轩3 小时前
UDS时间参数学习(应用层+网络层+会话层+传输层)
c语言·can·信息与通信·诊断·uds
Ivy_belief3 小时前
C++新特性汇总:涵盖C++11到C++23
java·c++·c++11·c++23
koddnty3 小时前
在c++中使用HOOK修改sleep函数
linux·c++