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

推荐订阅源

Forbes - Security
Forbes - Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
F
Fortinet All Blogs
B
Blog
T
The Blog of Author Tim Ferriss
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
Y
Y Combinator Blog
Microsoft Azure Blog
Microsoft Azure Blog
L
LangChain Blog
Recent Announcements
Recent Announcements
U
Unit 42
Martin Fowler
Martin Fowler
M
MIT News - Artificial intelligence
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The Register - Security
The Register - Security
Recorded Future
Recorded Future
C
Check Point Blog
V
V2EX
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
Google DeepMind News
Google DeepMind News
酷 壳 – CoolShell
酷 壳 – CoolShell
F
Full Disclosure
小众软件
小众软件
A
About on SuperTechFans
云风的 BLOG
云风的 BLOG
宝玉的分享
宝玉的分享
Last Week in AI
Last Week in AI
有赞技术团队
有赞技术团队
MongoDB | Blog
MongoDB | Blog
爱范儿
爱范儿
P
Proofpoint News Feed
罗磊的独立博客
量子位
D
Docker
博客园_首页
D
DataBreaches.Net
Project Zero
Project Zero
博客园 - 司徒正美
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园 - Franky
Security Latest
Security Latest
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
N
Netflix TechBlog - Medium
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 三生石上(FineUI控件)
H
Hackread – Cybersecurity News, Data Breaches, AI and More
大猫的无限游戏
大猫的无限游戏

博客园 - 一字千金

看懂成交密度,秒辨股市资金真假动向 安装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");
}