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

推荐订阅源

T
Tor Project blog
B
Blog RSS Feed
M
MIT News - Artificial intelligence
WordPress大学
WordPress大学
H
Hackread – Cybersecurity News, Data Breaches, AI and More
罗磊的独立博客
GbyAI
GbyAI
N
Netflix TechBlog - Medium
博客园 - 司徒正美
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
宝玉的分享
宝玉的分享
W
WeLiveSecurity
Stack Overflow Blog
Stack Overflow Blog
Y
Y Combinator Blog
SecWiki News
SecWiki News
V
Vulnerabilities – Threatpost
Google DeepMind News
Google DeepMind News
C
CERT Recently Published Vulnerability Notes
T
Tailwind CSS Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Register - Security
The Register - Security
Cisco Talos Blog
Cisco Talos Blog
Martin Fowler
Martin Fowler
A
About on SuperTechFans
S
Security @ Cisco Blogs
T
Tenable Blog
C
Check Point Blog
N
News and Events Feed by Topic
S
SegmentFault 最新的问题
The GitHub Blog
The GitHub Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Attack and Defense Labs
Attack and Defense Labs
美团技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
Cisco Blogs
P
Palo Alto Networks Blog
V
V2EX
博客园 - 聂微东
Project Zero
Project Zero
酷 壳 – CoolShell
酷 壳 – CoolShell
D
Docker
N
News | PayPal Newsroom
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
小众软件
小众软件
Application and Cybersecurity Blog
Application and Cybersecurity Blog
人人都是产品经理
人人都是产品经理
V2EX - 技术
V2EX - 技术
I
Intezer
L
LINUX DO - 最新话题

博客园 - 一字千金

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