惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
J
Java Code Geeks
MongoDB | Blog
MongoDB | Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Blog — PlanetScale
Blog — PlanetScale
A
About on SuperTechFans
Engineering at Meta
Engineering at Meta
美团技术团队
N
Netflix TechBlog - Medium
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
F
Fortinet All Blogs
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
MyScale Blog
MyScale Blog
aimingoo的专栏
aimingoo的专栏
Last Week in AI
Last Week in AI
The Cloudflare Blog
量子位
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
D
DataBreaches.Net
H
Hackread – Cybersecurity News, Data Breaches, AI and More

博客园 - 一字千金

day6矩阵论---SVD(奇异值分解)lora关系 day5 线性代数--范数和内积 day4--特征值和特征向量 day3 向量 day2 线性代数--矩阵 day1线性代数---行列式 C++程序员大模型学习计划(每天2小时) libprotobuf.so出现未定义符号 容器运行报错线程创建失败 看懂成交密度,秒辨股市资金真假动向 安装android studio时出现下面报错source-36_r01.zip安装失败 异动拉升横盘突破筛选股票 sh看门狗脚本 QLabel设置QToolTip显示的样式 自动化http请求脚本 批量遍历文件夹内得文件生成md5值 g_JavaVM->AttachCurrentThread((void **) &pEnv, NULL))返回-1 python使用pg数据库 批量获取git所有分支命令 大模型之智能体 深⼊了解 GPT-4和ChatGPT的API 初识GPT-4和ChatGTP 什么是大模型以及需要掌握哪些基础知识 linux下检测程序内存泄漏方法步骤 pycharm2024下载安装一键激活2099年 linux根据进程名查找进程并杀死进程脚本 linux根据进程名称定时获取进程内存脚本 QGridLayout删除旧窗口还有清干净 linux编译安装ccache3.2.4
linux C++崩溃堆栈信息打印
一字千金 · 2026-05-07 · via 博客园 - 一字千金

#include <execinfo.h>
#include <signal.h>
#include <ucontext.h>
#include <cxxabi.h>
#include <stdio.h>


//
崩溃堆栈打印功能 static const int MAX_STACK_FRAMES = 128; /** * @brief 信号处理函数,用于捕获崩溃信号并打印堆栈信息 */ void SignalHandler(int sig, siginfo_t* info, void* context) { void* array[MAX_STACK_FRAMES]; char** messages; int size, i; ucontext_t* ucontext = (ucontext_t*)context; printf("\n\n=== 程序崩溃,开始打印堆栈信息 ===\n"); printf("捕获到信号 %d:\n", sig); switch(sig) { case SIGSEGV: printf("信号: SIGSEGV (段错误/内存访问违规)\n"); break; case SIGABRT: printf("信号: SIGABRT (程序异常终止)\n"); break; case SIGFPE: printf("信号: SIGFPE (算术运算错误)\n"); break; case SIGILL: printf("信号: SIGILL (非法指令)\n"); break; case SIGBUS: printf("信号: SIGBUS (总线错误)\n"); break; default: printf("信号: %d\n", sig); break; } if (info) { printf("错误地址: %p\n", info->si_addr); } if (ucontext) { #if defined(__x86_64__) printf("指令指针: %p\n", (void*)ucontext->uc_mcontext.gregs[REG_RIP]); #elif defined(__i386__) printf("指令指针: %p\n", (void*)ucontext->uc_mcontext.gregs[REG_EIP]); #elif defined(__aarch64__) printf("指令指针: %p\n", (void*)ucontext->uc_mcontext.pc); #endif } // 获取堆栈信息 size = backtrace(array, MAX_STACK_FRAMES); messages = backtrace_symbols(array, size); printf("\n堆栈跟踪 (%d 帧):\n", size); for(i = 0; i < size; i++) { printf("[%02d] %s\n", i, messages[i]); // 尝试demangle C++符号 char* begin_name = nullptr; char* begin_offset = nullptr; char* end_offset = nullptr; // 查找函数名的开始和结束位置 for(char* p = messages[i]; *p; ++p) { if(*p == '(') { begin_name = p; } else if(*p == '+' && begin_name) { begin_offset = p; } else if(*p == ')' && begin_offset) { end_offset = p; break; } } if(begin_name && begin_offset && end_offset && begin_name < begin_offset) { *begin_name++ = '\0'; *begin_offset++ = '\0'; *end_offset = '\0'; int status; char* demangled_name = abi::__cxa_demangle(begin_name, nullptr, nullptr, &status); if(status == 0 && demangled_name) { printf(" 函数: %s\n", demangled_name); free(demangled_name); } else { printf(" 函数: %s\n", begin_name); } } } free(messages); printf("=== 堆栈信息打印结束 ===\n\n"); // 退出程序 exit(sig); } /** * @brief 安装信号处理器 */ void InstallSignalHandler() { struct sigaction sa; sa.sa_sigaction = SignalHandler; sigemptyset(&sa.sa_mask); sa.sa_flags = SA_RESTART | SA_SIGINFO; // 注册需要捕获的信号 sigaction(SIGSEGV, &sa, nullptr); // 段错误 sigaction(SIGABRT, &sa, nullptr); // 异常终止 sigaction(SIGFPE, &sa, nullptr); // 算术异常 sigaction(SIGILL, &sa, nullptr); // 非法指令 sigaction(SIGBUS, &sa, nullptr); // 总线错误 }

main函数中执行安装句柄函数

int main(int argc, char* argv[])
{
    InstallSignalHandler();
    printf("崩溃堆栈打印功能已启用\n");
}