summaryrefslogtreecommitdiffstats
path: root/src/common-lib/crashhandler.cpp
blob: 01fa1636ebfd68e4a8be000e856ce279915350ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/****************************************************************************
**
** Copyright (C) 2019 Luxoft Sweden AB
** Copyright (C) 2018 Pelagicore AG
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Luxoft Application Manager.
**
** $QT_BEGIN_LICENSE:LGPL-QTAS$
** Commercial License Usage
** Licensees holding valid commercial Qt Automotive Suite licenses may use
** this file in accordance with the commercial license agreement provided
** with the Software or, alternatively, in accordance with the terms
** contained in a written agreement between you and The Qt Company.  For
** licensing terms and conditions see https://www.qt.io/terms-conditions.
** For further information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
** SPDX-License-Identifier: LGPL-3.0
**
****************************************************************************/

#include "crashhandler.h"
#include "global.h"

#if !defined(Q_OS_LINUX) || defined(Q_OS_ANDROID)

QT_BEGIN_NAMESPACE_AM

void CrashHandler::setCrashActionConfiguration(const QVariantMap &config)
{
    Q_UNUSED(config)
}

void CrashHandler::setQmlEngine(QQmlEngine *engine)
{
    Q_UNUSED(engine)
}

QT_END_NAMESPACE_AM

#else

#if defined(QT_QML_LIB)
#  include <QQmlEngine>
#  include <QtQml/private/qv4engine_p.h>
#  include <QtQml/private/qv8engine_p.h>
#endif

#include <cxxabi.h>
#include <execinfo.h>
#include <setjmp.h>
#include <signal.h>
#include <inttypes.h>
#include <sys/syscall.h>
#include <pthread.h>
#include <stdio.h>

#if defined(AM_USE_LIBBACKTRACE)
#  include <libbacktrace/backtrace.h>
#  include <libbacktrace/backtrace-supported.h>
#endif

#include "unixsignalhandler.h"
#include "logging.h"
#include "utilities.h"
#include "processtitle.h"

QT_BEGIN_NAMESPACE_AM

enum PrintDestination { Console, Dlt };

static bool printBacktrace;
static bool printQmlStack;
static bool useAnsiColor;
static bool dumpCore;
static int waitForGdbAttach;

static char *demangleBuffer;
static size_t demangleBufferSize;

// this will make it run before all other static constructor functions
static void initBacktrace() __attribute__((constructor(101)));

static Q_NORETURN void crashHandler(const char *why, int stackFramesToIgnore);

void CrashHandler::setCrashActionConfiguration(const QVariantMap &config)
{
    printBacktrace = config.value(qSL("printBacktrace"), printBacktrace).toBool();
    printQmlStack = config.value(qSL("printQmlStack"), printQmlStack).toBool();
    waitForGdbAttach = config.value(qSL("waitForGdbAttach"), waitForGdbAttach).toInt() * timeoutFactor();
    dumpCore = config.value(qSL("dumpCore"), dumpCore).toBool();
}

#if defined(QT_QML_LIB)
static QQmlEngine *qmlEngine;

void CrashHandler::setQmlEngine(QQmlEngine *engine)
{
    qmlEngine = engine;
}
#endif

static void initBacktrace()
{
    // This can catch and pretty-print all of the following:

    // SIGFPE
    // volatile int i = 2;
    // int zero = 0;
    // i /= zero;

    // SIGSEGV
    // *((int *)1) = 1;

    // uncaught arbitrary exception
    // throw 42;

    // uncaught std::exception derived exception (prints what())
    // throw std::logic_error("test output");

    printBacktrace = true;
    printQmlStack = true;
    dumpCore = true;
    waitForGdbAttach = 0;

    getOutputInformation(&useAnsiColor, nullptr, nullptr);

    demangleBufferSize = 512;
    demangleBuffer = static_cast<char *>(malloc(demangleBufferSize));

    UnixSignalHandler::instance()->install(UnixSignalHandler::RawSignalHandler,
                                           { SIGFPE, SIGSEGV, SIGILL, SIGBUS, SIGPIPE, SIGABRT },
                                           [](int sig) {
        UnixSignalHandler::instance()->resetToDefault(sig);
        static char buffer[256];
        snprintf(buffer, sizeof(buffer), "uncaught signal %d (%s)", sig, UnixSignalHandler::signalName(sig));
        // 6 means to remove 6 stack frames: this way the backtrace starts at the point where
        // the signal reception interrupted the normal program flow
        crashHandler(buffer, 6);
    });

    std::set_terminate([]() {
        static char buffer [1024];

        auto type = abi::__cxa_current_exception_type();
        if (!type) {
            // 3 means to remove 3 stack frames: this way the backtrace starts at std::terminate
            crashHandler("terminate was called although no exception was thrown", 3);
        }

        const char *typeName = type->name();
        if (typeName) {
            int status;
            abi::__cxa_demangle(typeName, demangleBuffer, &demangleBufferSize, &status);
            if (status == 0 && *demangleBuffer) {
                typeName = demangleBuffer;
            }
        }
        try {
            throw;
        } catch (const std::exception &exc) {
            snprintf(buffer, sizeof(buffer), "uncaught exception of type %s (%s)", typeName, exc.what());
        } catch (...) {
            snprintf(buffer, sizeof(buffer), "uncaught exception of type %s", typeName);
        }

        // 4 means to remove 4 stack frames: this way the backtrace starts at std::terminate
        crashHandler(buffer, 4);
    });
}

static void printMsgToConsole(const char *format, ...) Q_ATTRIBUTE_FORMAT_PRINTF(1, 2);
static void printMsgToConsole(const char *format, ...)
{
    va_list arglist;
    va_start(arglist, format);
    vfprintf(stderr, format, arglist);
    va_end(arglist);
    fputs("\n", stderr);
}

static void printMsgToDlt(const char *format, ...) Q_ATTRIBUTE_FORMAT_PRINTF(1, 2);
static void printMsgToDlt(const char *format, ...)
{
    va_list arglist;
    va_start(arglist, format);
    Logging::logToDlt(QtMsgType::QtFatalMsg, QMessageLogContext(), QString::vasprintf(format, arglist));
    va_end(arglist);
}

static void printCrashInfo(PrintDestination dest, const char *why, int stackFramesToIgnore)
{
    using printMsgType = void (*)(const char *format, ...);
    static printMsgType printMsg;
    printMsg = (dest == Dlt) ? printMsgToDlt : printMsgToConsole;

    const char *title = ProcessTitle::title();
    char who[256];
    if (!title) {
        int whoLen = readlink("/proc/self/exe", who, sizeof(who) -1);
        who[qMax(0, whoLen)] = '\0';
        title = who;
    }

    pid_t pid = getpid();
    long tid = syscall(SYS_gettid);
    pthread_t pthreadId = pthread_self();
    char threadName[16];
    if (tid == pid)
        strcpy(threadName, "main");
    else if (pthread_getname_np(pthreadId, threadName, sizeof(threadName)))
        strcpy(threadName, "unknown");

    printMsg("\n*** process %s (%d) crashed ***", title, pid);
    printMsg("\n > why: %s", why);
    printMsg("\n > where: %s thread, TID: %d, pthread ID: %p", threadName, tid, pthreadId);

    if (printBacktrace) {
#if defined(AM_USE_LIBBACKTRACE) && defined(BACKTRACE_SUPPORTED)
        struct btData
        {
            backtrace_state *state;
            int level;
        };

        static auto printBacktraceLine = [](int level, const char *symbol, uintptr_t offset,
                                            const char *file = nullptr, int line = -1) {
            if (file) {
                printMsg(useAnsiColor ? " %3d: \x1b[1m%s\x1b[0m [\x1b[36m%" PRIxPTR "\x1b[0m]"
                                        " in \x1b[35m%s\x1b[0m:\x1b[35;1m%d\x1b[0m"
                                      : " %3d: %s [%" PRIxPTR "] in %s:%d", level, symbol, offset, file, line);
            } else {
                printMsg(useAnsiColor ? " %3d: \x1b[1m%s\x1b[0m [\x1b[36m%" PRIxPTR "\x1b[0m]"
                                      : " %3d: %s [%" PRIxPTR "]", level, symbol, offset);
            }
        };

        static auto errorCallback = [](void *data, const char *msg, int errnum) {
            const char *fmt = " %3d: ERROR: %s (%d)\n";
            if (useAnsiColor)
                fmt = " %3d: \x1b[31;1mERROR: \x1b[0;1m%s (%d)\x1b[0m";

            printMsg(fmt, static_cast<btData *>(data)->level, msg, errnum);
        };

        static auto syminfoCallback = [](void *data, uintptr_t pc, const char *symname, uintptr_t symval,
                                         uintptr_t symsize) {
            Q_UNUSED(symval)
            Q_UNUSED(symsize)

            int level = static_cast<btData *>(data)->level;
            if (symname) {
                int status;
                abi::__cxa_demangle(symname, demangleBuffer, &demangleBufferSize, &status);

                if (status == 0 && *demangleBuffer)
                    printBacktraceLine(level, demangleBuffer, pc);
                else
                    printBacktraceLine(level, symname, pc);
            } else {
                printBacktraceLine(level, nullptr, pc);
            }
        };

        static auto fullCallback = [](void *data, uintptr_t pc, const char *filename, int lineno,
                                      const char *function) -> int {
            if (function) {
                int status;
                abi::__cxa_demangle(function, demangleBuffer, &demangleBufferSize, &status);

                printBacktraceLine(static_cast<btData *>(data)->level,
                                   (status == 0 && *demangleBuffer) ? demangleBuffer : function,
                                   pc, filename ? filename : "<unknown>", lineno);
            } else {
                backtrace_syminfo (static_cast<btData *>(data)->state, pc, syminfoCallback, errorCallback, data);
            }
            return 0;
        };

        static auto simpleCallback = [](void *data, uintptr_t pc) -> int {
            backtrace_pcinfo(static_cast<btData *>(data)->state, pc, fullCallback, errorCallback, data);
            static_cast<btData *>(data)->level++;
            return 0;
        };

        struct backtrace_state *state = backtrace_create_state(nullptr, BACKTRACE_SUPPORTS_THREADS,
                                                               errorCallback, nullptr);

        printMsg("\n > backtrace:");
        btData data = { state, 0 };
        //backtrace_print(state, stackFramesToIgnore, stderr);
        backtrace_simple(state, stackFramesToIgnore, simpleCallback, errorCallback, &data);
#else // !defined(AM_USE_LIBBACKTRACE) && defined(BACKTRACE_SUPPORTED)
        Q_UNUSED(stackFramesToIgnore);
        void *addrArray[1024];
        int addrCount = backtrace(addrArray, sizeof(addrArray) / sizeof(*addrArray));

        if (!addrCount) {
            printMsg(" > no backtrace available");
        } else {
            char **symbols = backtrace_symbols(addrArray, addrCount);
            //backtrace_symbols_fd(addrArray, addrCount, 2);

            if (!symbols) {
                printMsg(" > no symbol names available");
            } else {
                printMsg("\n > backtrace:");
                for (int i = 1; i < addrCount; ++i) {
                    char *function = nullptr;
                    char *offset = nullptr;
                    char *end = nullptr;

                    for (char *ptr = symbols[i]; ptr && *ptr; ++ptr) {
                        if (!function && *ptr == '(')
                            function = ptr + 1;
                        else if (function && !offset && *ptr == '+')
                            offset = ptr;
                        else if (function && !end && *ptr == ')')
                            end = ptr;
                    }

                    if (function && offset && end && (function != offset)) {
                        *offset = 0;
                        *end = 0;

                        int status;
                        abi::__cxa_demangle(function, demangleBuffer, &demangleBufferSize, &status);

                        if (status == 0 && *demangleBuffer) {
                            printMsg(" %3d: %s [+%s]", i, demangleBuffer, offset + 1);
                        } else {
                            printMsg(" %3d: %s [+%s]", i, function, offset + 1);
                        }
                    } else  {
                        printMsg(" %3d: %s", i, symbols[i]);
                    }
                }
            }
        }
#endif // defined(AM_USE_LIBBACKTRACE) && defined(BACKTRACE_SUPPORTED)
    }

#if defined(QT_QML_LIB)
    if (printQmlStack && qmlEngine) {
        const QV4::ExecutionEngine *qv4engine = qmlEngine->handle();
        if (qv4engine) {
            const QV4::StackTrace stackTrace = qv4engine->stackTrace();
            if (stackTrace.size()) {
                printMsg("\n > qml stack:");
                int frame = 0;
                for (const auto &stackFrame : stackTrace) {
                    printMsg(useAnsiColor ? " %3d: \x1b[1m%s\x1b[0m in \x1b[35m%s\x1b[0m:\x1b[35;1m%d\x1b[0m"
                                          : " %3d: %s in %s:%d",
                             frame, stackFrame.function.toLocal8Bit().constData(),
                             stackFrame.source.toLocal8Bit().constData(), stackFrame.line);
                    frame++;
                }
            } else {
                printMsg("\n > qml stack: empty");
            }
        }
    }
#endif
}

static void crashHandler(const char *why, int stackFramesToIgnore)
{
    // We also need to reset all the "crash" signals plus SIGINT for three reasons:
    //  1) avoid recursions
    //  2) SIGABRT to re-enable standard abort() handling
    //  3) SIGINT, so that you can Ctrl+C the app if the crash handler ends up freezing
    UnixSignalHandler::instance()->resetToDefault({ SIGFPE, SIGSEGV, SIGILL, SIGBUS, SIGPIPE, SIGABRT, SIGINT });

    printCrashInfo(Console, why, stackFramesToIgnore);

    if (waitForGdbAttach > 0) {
        fprintf(stderr, "\n > the process will be suspended for %d seconds and you can attach a debugger"
                        " to it via\n\n   gdb -p %d\n", waitForGdbAttach, getpid());
        static jmp_buf jmpenv;
        signal(SIGALRM, [](int) {
            longjmp(jmpenv, 1);
        });
        if (!setjmp(jmpenv)) {
            alarm(static_cast<unsigned int>(waitForGdbAttach));

            sigset_t mask;
            sigemptyset(&mask);
            sigaddset(&mask, SIGALRM);
            sigsuspend(&mask);
        } else {
            fprintf(stderr, "\n > no gdb attached\n");
        }
    }

    if (Logging::isDltEnabled()) {
        useAnsiColor = false;
        printCrashInfo(Dlt, why, stackFramesToIgnore);
    }

    if (dumpCore) {
        fprintf(stderr, "\n > the process will be aborted (core dumped)\n\n");
        abort();
    }

    _exit(-1);
}

QT_END_NAMESPACE_AM

#endif // !Q_OS_LINUX || defined(Q_OS_ANDROID)