Adding proxy support to the core
[quassel.git] / src / common / logbacktrace_unix.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel IRC Team                         *
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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include "quassel.h"
22
23 #if defined(HAVE_EXECINFO) && !defined(Q_OS_MAC)
24 #  define BUILD_CRASHHANDLER
25 #  include <execinfo.h>
26 #  include <dlfcn.h>
27 #  include <cxxabi.h>
28 #  include <QFile>
29 #  include <QTextStream>
30 #  include <QDebug>
31 #endif
32
33 void Quassel::logBacktrace(const QString &filename) {
34 #ifndef BUILD_CRASHHANDLER
35   Q_UNUSED(filename)
36 #else
37   void* callstack[128];
38   int i, frames = backtrace(callstack, 128);
39
40   QFile dumpFile(filename);
41   dumpFile.open(QIODevice::Append);
42   QTextStream dumpStream(&dumpFile);
43
44   for (i = 0; i < frames; ++i) {
45     Dl_info info;
46     dladdr (callstack[i], &info);
47     // as a reference:
48     //     typedef struct
49     //     {
50     //       __const char *dli_fname;   /* File name of defining object.  */
51     //       void *dli_fbase;           /* Load address of that object.  */
52     //       __const char *dli_sname;   /* Name of nearest symbol.  */
53     //       void *dli_saddr;           /* Exact value of nearest symbol.  */
54     //     } Dl_info;
55
56     #if __LP64__
57     int addrSize = 16;
58     #else
59     int addrSize = 8;
60     #endif
61
62     QString funcName;
63     if(info.dli_sname) {
64       char *func = abi::__cxa_demangle(info.dli_sname, 0, 0, 0);
65       if(func) {
66         funcName = QString(func);
67         free(func);
68       } else {
69         funcName = QString(info.dli_sname);
70       }
71     } else {
72       funcName = QString("0x%1").arg((ulong)(info.dli_saddr), addrSize, 16, QLatin1Char('0'));
73     }
74
75     // prettificating the filename
76     QString fileName("???");
77     if(info.dli_fname) {
78       fileName = QString(info.dli_fname);
79       int slashPos = fileName.lastIndexOf('/');
80       if(slashPos != -1)
81         fileName = fileName.mid(slashPos + 1);
82     }
83
84     QString debugLine = QString("#%1 %2 0x%3 %4").arg(i, 3, 10)
85       .arg(fileName, - 20)
86       .arg((ulong)(callstack[i]), addrSize, 16, QLatin1Char('0'))
87       .arg(funcName);
88
89     dumpStream << debugLine << "\n";
90     qDebug() << qPrintable(debugLine);
91   }
92   dumpFile.close();
93 #endif /* BUILD_CRASHHANDLER */
94 }