qa: Replace deprecated qVariantFromValue() by QVariant::fromValue()
[quassel.git] / src / common / logbacktrace_unix.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2019 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "quassel.h"
22
23 #if defined(HAVE_BACKTRACE) && !defined(Q_OS_MAC)
24 #    define BUILD_CRASHHANDLER
25 #    include <QDebug>
26 #    include <QFile>
27 #    include <QTextStream>
28
29 #    include <cxxabi.h>
30 #    include <dlfcn.h>
31
32 #    include "backtrace_config.h"
33 #endif
34
35 void Quassel::logBacktrace(const QString& filename)
36 {
37 #ifndef BUILD_CRASHHANDLER
38     Q_UNUSED(filename)
39 #else
40     void* callstack[128];
41     int i, frames = backtrace(callstack, 128);
42
43     QFile dumpFile(filename);
44     dumpFile.open(QIODevice::Append);
45     QTextStream dumpStream(&dumpFile);
46
47     for (i = 0; i < frames; ++i) {
48         Dl_info info;
49         dladdr(callstack[i], &info);
50         // as a reference:
51         //     typedef struct
52         //     {
53         //       __const char *dli_fname;   /* File name of defining object.  */
54         //       void *dli_fbase;           /* Load address of that object.  */
55         //       __const char *dli_sname;   /* Name of nearest symbol.  */
56         //       void *dli_saddr;           /* Exact value of nearest symbol.  */
57         //     } Dl_info;
58
59 #    ifdef __LP64__
60         int addrSize = 16;
61 #    else
62         int addrSize = 8;
63 #    endif
64
65         QString funcName;
66         if (info.dli_sname) {
67             char* func = abi::__cxa_demangle(info.dli_sname, nullptr, nullptr, nullptr);
68             if (func) {
69                 funcName = QString(func);
70                 free(func);
71             }
72             else {
73                 funcName = QString(info.dli_sname);
74             }
75         }
76         else {
77             funcName = QString("0x%1").arg((ulong)(info.dli_saddr), addrSize, 16, QLatin1Char('0'));
78         }
79
80         // prettificating the filename
81         QString fileName("???");
82         if (info.dli_fname) {
83             fileName = QString(info.dli_fname);
84             int slashPos = fileName.lastIndexOf('/');
85             if (slashPos != -1)
86                 fileName = fileName.mid(slashPos + 1);
87         }
88
89         QString debugLine = QString("#%1 %2 0x%3 %4")
90                                 .arg(i, 3, 10)
91                                 .arg(fileName, -20)
92                                 .arg((ulong)(callstack[i]), addrSize, 16, QLatin1Char('0'))
93                                 .arg(funcName);
94
95         dumpStream << debugLine << "\n";
96         qDebug() << qPrintable(debugLine);
97     }
98     dumpFile.close();
99 #endif /* BUILD_CRASHHANDLER */
100 }