首页 > 编程语言 >c++ 程序打印 core dump 信息

c++ 程序打印 core dump 信息

时间:2023-11-02 18:03:49浏览次数:32  
标签:core coreDumpHandle dump callStack signal c++ textStream include void

linux 环境下 c++ 程序打印 core dump 信息

linux 信号机制

c++ 打印堆栈信息

#include <signal.h>
#include <execinfo.h>
#include <dlfcn.h>
#include <cxxabi.h>
#include <QFile>
#include <QTextStream>

// 捕获信号,对于这些信号都执行 coreDumpHandle
signal(SIGQUIT, coreDumpHandle);
signal(SIGILL, coreDumpHandle);
signal(SIGABRT, coreDumpHandle);
signal(SIGSEGV, coreDumpHandle);
signal(SIGTRAP, coreDumpHandle);

void coreDumpHandle(int signum) {
    string path = "/xxx/xxx/coreDump.log";
    
    void* callStack[128];
    int frames = backstrace(callStack, sizeof(callStack) / sizeof(callStack[0]));
    char** strs = backtrack_symbols(callStack, frames);

    if (strs == nullptr)
        exit(0);

    QFile out(path.c_str());
    if (!out.open(QIODevice::WrietOnly | QIODevece::Text | QIODevice::Truncate)) {
        exit(0);
    }

    QTextStream textStream(&out);
    textStream << "Get signal: " << strsignal(signum) << "\n";
    textStream << "Call stack:\n";

    unsigned count = 0;
    for (int i = 0; i < frames; ++i) {
        Dl_info info;
        if (dladdr(callStack[i], &info) && info.dli_sname) {
            int status;
            char* demangled = abi::__cxa_demangle(info.dli_sname, nullptr, nullptr, &status);
            if (status == 0) {
                QString str = QString("[%1] %2 : %3 0x%4\n").arg(count++)
                        .arg(info.dli_fname)
                        .arg(demangled)
                        .arg(reinterpret_cast<quintptr>(((void*)callStack[i])), 0, 16);
                textStream << str;
            } else {
                QString str = QString("[%1] %2 : %3 0x%4\n").arg(count++)
                        .arg(info.dli_fname)
                        .arg(info.dli_fname)
                        .arg(reinterpret_cast<quintptr>(((void*)callStack[i])), 0, 16);
                textStream << str;
            }
            free(demangled);
        }
        else {
            // printf("[%d] %s : ???\n", i, info.dli_fname);
        }
    }
    out.close();
    free(strs);
    exit(0);
}

标签:core,coreDumpHandle,dump,callStack,signal,c++,textStream,include,void
From: https://www.cnblogs.com/AngleLin/p/17805932.html

相关文章

  • ASP.NET Core Filter
    Filter在ASP.NETCore中允许code在指定的请求阶段前或者后执行。Filter在ASP.NETCore方法请求管道中运行,有时被称作filterpipeline,filterpipeline在ASP.NETCore选择Action执行。 Filter分类每个filtertype在filterpipeline的不同阶段执行。AuthorizationFilter:第一......
  • 原型模式--C++实现
    浅克隆#include<iostream>#include<algorithm>usingnamespacestd;//计算类classCompute{public: int*p; intlen;//向量长度 Compute(intlen); Compute(constCompute&compute);//构造函数 ~Compute();//析构函数 intoperator[](inti)const{ return......
  • api-ms-win-core-path-l1-1-0.dll下载(win7-32位)
      下载 api-ms-win-core-path-l1-1-0.dll 后拷贝到文件夹:32位系统 c:\windows\system32  下载链接: https://www.dll-files.com/api-ms-win-core-path-l1-1-0.dll.html ......
  • 【转载】CUDA编程学习记录 C++
    参考Yuezero的CUDA编程基础(https://blog.csdn.net/weixin_54338498/article/details/127947551)CUDA编程模型host指代CPU及其内存,包含host程序device指代GPU及其内存,包含device程序经典CUDA程序的执行流程如下:分配host内存,并进行数据初始化;分配device内存,并从host将......
  • 字符与数字的相互转换C++
    一、字符转数字char类型字符转换为数字,其实是转换为ASCII码值有两种方式:1.强制类型转换,结果为对应的ASCII码值charv1='a';charv2='z';charv3='1';charv4='9';intnum1=(int)v1;intnum2=(int)v2;intnum3=(int)v3;intnum4=(int)v4;printf......
  • net core流相关
      Stream.Read与StreamReader.Read都可以读取流中的信息///<summary>///将流中的内容以字节码的形式读出来///</summary>///<paramname="stream"></param>///<returns></returns>publics......
  • 如何用c++开发远程协助软件,端对端的技术
     标题:使用C++开发远程协助软件的步骤与技巧摘要:随着科技的不断发展,远程协助软件在各个领域得到了广泛的应用。本文将介绍如何使用C++语言来开发一款简单的远程协助软件,并将重点放在基本功能的实现以及关键技术的解释上。关键词:C++、远程协助软件、网络通信、图形用户界面、安......
  • Json数据文件处理中遇到的一些问题总结(C++)
    一、海量数据处理的方法总结参考CSDN技术贴:海量数据处理方法C++面试必备-海量数据处理二、快速Json文件解析的库字节-json快速解析库三、无锁队列C++无锁队列四、C++打包静态库静态库和动态库Linux-(C/C++)生成并使用静态库/动态库c/c++依赖静态库、动态库符号问题在远......
  • C++类&对象
    C++类&对象C++在C语言的基础上增加了面向对象编程,C++支持面向对象程序设计。类是C++的核心特性,通常被称为用户定义的类型。类用于指定对象的形式,它包含了数据表示法和用于处理数据的方法。类中的数据和方法称为类的成员。函数在一个类中被称为类的成员。C++类定义定义一个类,......
  • C++ 记录
    STL队列(queue),一个先进先出的容器,需要用到头文件queue。函数成员名功能返回值类型que.empty()判断队列是否为空,空返回真,非空返回假boolque.size()返回队列中元素个数unsignedlonglongque.push()将元素x放进队尾voidque.front()返回队首元素qu......