File: crashcatcher.cpp

package info (click to toggle)
openmw 0.47.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 23,276 kB
  • sloc: cpp: 249,935; xml: 1,978; sh: 1,327; python: 63; makefile: 26
file content (578 lines) | stat: -rw-r--r-- 15,859 bytes parent folder | download
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/param.h>
#include <sys/utsname.h>
#include <string.h>
#include <errno.h>
#include <limits.h>

#include <pthread.h>
#include <stdbool.h>
#include <sys/ptrace.h>

#include <components/debug/debuglog.hpp>

#include <boost/filesystem/fstream.hpp>
#include <boost/filesystem/operations.hpp>

namespace bfs = boost::filesystem;

#include <SDL_messagebox.h>

#ifdef __linux__
#include <sys/prctl.h>
#include <sys/ucontext.h>
#ifndef PR_SET_PTRACER
#define PR_SET_PTRACER 0x59616d61
#endif
#elif defined (__APPLE__) || defined (__FreeBSD__) || defined(__OpenBSD__)
#include <signal.h>
#endif

#if defined(__APPLE__)
#include <sys/sysctl.h>
#include <libproc.h>
#endif

#if defined(__FreeBSD__)
#include <sys/sysctl.h>
#include <sys/user.h>
#endif

#include "crashcatcher.hpp"

static const char fatal_err[] = "\n\n*** Fatal Error ***\n";
static const char pipe_err[] = "!!! Failed to create pipe\n";
static const char fork_err[] = "!!! Failed to fork debug process\n";
static const char exec_err[] = "!!! Failed to exec debug process\n";

#ifndef PATH_MAX /* Not all platforms (GNU Hurd) have this. */
#   define PATH_MAX 256
#endif

static char argv0[PATH_MAX];


static struct {
    int signum;
    pid_t pid;
    int has_siginfo;
    siginfo_t siginfo;
    char buf[1024];
} crash_info;


static const struct {
    const char *name;
    int signum;
} signals[] = {
{ "Segmentation fault", SIGSEGV },
{ "Illegal instruction", SIGILL },
{ "FPU exception", SIGFPE },
{ "System BUS error", SIGBUS },
{ nullptr, 0 }
};

static const struct {
    int code;
    const char *name;
} sigill_codes[] = {
    #if !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__)
    { ILL_ILLOPC, "Illegal opcode" },
    { ILL_ILLOPN, "Illegal operand" },
    { ILL_ILLADR, "Illegal addressing mode" },
    { ILL_ILLTRP, "Illegal trap" },
    { ILL_PRVOPC, "Privileged opcode" },
    { ILL_PRVREG, "Privileged register" },
    { ILL_COPROC, "Coprocessor error" },
    { ILL_BADSTK, "Internal stack error" },
    #endif
    { 0, nullptr }
};

static const struct {
    int code;
    const char *name;
} sigfpe_codes[] = {
    { FPE_INTDIV, "Integer divide by zero" },
    { FPE_INTOVF, "Integer overflow" },
    { FPE_FLTDIV, "Floating point divide by zero" },
    { FPE_FLTOVF, "Floating point overflow" },
    { FPE_FLTUND, "Floating point underflow" },
    { FPE_FLTRES, "Floating point inexact result" },
    { FPE_FLTINV, "Floating point invalid operation" },
    { FPE_FLTSUB, "Subscript out of range" },
    { 0, nullptr }
};

static const struct {
    int code;
    const char *name;
} sigsegv_codes[] = {
    #ifndef __FreeBSD__
    { SEGV_MAPERR, "Address not mapped to object" },
    { SEGV_ACCERR, "Invalid permissions for mapped object" },
    #endif
    { 0, nullptr }
};

static const struct {
    int code;
    const char *name;
} sigbus_codes[] = {
    #ifndef __FreeBSD__
    { BUS_ADRALN, "Invalid address alignment" },
    { BUS_ADRERR, "Non-existent physical address" },
    { BUS_OBJERR, "Object specific hardware error" },
    #endif
    { 0, nullptr }
};

static int (*cc_user_info)(char*, char*);


static void gdb_info(pid_t pid)
{
    char respfile[64];
    FILE *f;
    int fd;

    /*
     * Create a temp file to put gdb commands into.
     * Note: POSIX.1-2008 declares that the file should be already created with mode 0600 by default.
     * Modern systems implement it and suggest to do not touch masks in multithreaded applications.
     * So CoverityScan warning is valid only for ancient versions of stdlib.
    */
    strcpy(respfile, "/tmp/gdb-respfile-XXXXXX");
#ifdef __COVERITY__
    umask(0600);
#endif
    if((fd=mkstemp(respfile)) >= 0 && (f=fdopen(fd, "w")) != nullptr)
    {
        fprintf(f, "attach %d\n"
                "shell echo \"\"\n"
                "shell echo \"* Loaded Libraries\"\n"
                "info sharedlibrary\n"
                "shell echo \"\"\n"
                "shell echo \"* Threads\"\n"
                "info threads\n"
                "shell echo \"\"\n"
                "shell echo \"* FPU Status\"\n"
                "info float\n"
                "shell echo \"\"\n"
                "shell echo \"* Registers\"\n"
                "info registers\n"
                "shell echo \"\"\n"
                "shell echo \"* Backtrace\"\n"
                "thread apply all backtrace full 1000\n"
                "detach\n"
                "quit\n", pid);
        fclose(f);

        /* Run gdb and print process info. */
        char cmd_buf[128];
        snprintf(cmd_buf, sizeof(cmd_buf), "gdb --quiet --batch --command=%s", respfile);
        printf("Executing: %s\n", cmd_buf);
        fflush(stdout);

        int ret = system(cmd_buf);

        if (ret != 0)
            printf("\nFailed to create a crash report. Please make sure that 'gdb' is installed and present in PATH then crash again."
                   "\nCurrent PATH: %s\n", getenv("PATH"));
        fflush(stdout);

        /* Clean up */
        if (remove(respfile) != 0)
            Log(Debug::Warning) << "Warning: can not remove file '" << respfile << "': " << std::strerror(errno);
    }
    else
    {
        /* Error creating temp file */
        if(fd >= 0)
        {
            if (close(fd) != 0)
                Log(Debug::Warning) << "Warning: can not close file '" << respfile << "': " << std::strerror(errno);
            else if (remove(respfile) != 0)
                Log(Debug::Warning) << "Warning: can not remove file '" << respfile << "': " << std::strerror(errno);
        }
        printf("!!! Could not create gdb command file\n");
    }
    fflush(stdout);
}

static void sys_info(void)
{
#ifdef __unix__
    struct utsname info;
    if(uname(&info))
        printf("!!! Failed to get system information\n");
    else
        printf("System: %s %s %s %s %s\n",
               info.sysname, info.nodename, info.release, info.version, info.machine);

    fflush(stdout);
#endif
}

static size_t safe_write(int fd, const void *buf, size_t len)
{
    size_t ret = 0;
    while(ret < len)
    {
        ssize_t rem;
        if((rem=write(fd, (const char*)buf+ret, len-ret)) == -1)
        {
            if(errno == EINTR)
                continue;
            break;
        }
        ret += rem;
    }
    return ret;
}

static void crash_catcher(int signum, siginfo_t *siginfo, void *context)
{
    //ucontext_t *ucontext = (ucontext_t*)context;
    pid_t dbg_pid;
    int fd[2];

    /* Make sure the effective uid is the real uid */
    if(getuid() != geteuid())
    {
        raise(signum);
        return;
    }

    safe_write(STDERR_FILENO, fatal_err, sizeof(fatal_err)-1);
    if(pipe(fd) == -1)
    {
        safe_write(STDERR_FILENO, pipe_err, sizeof(pipe_err)-1);
        raise(signum);
        return;
    }

    crash_info.signum = signum;
    crash_info.pid = getpid();
    crash_info.has_siginfo = !!siginfo;
    if(siginfo)
        crash_info.siginfo = *siginfo;
    if(cc_user_info)
        cc_user_info(crash_info.buf, crash_info.buf+sizeof(crash_info.buf));

    /* Fork off to start a crash handler */
    switch((dbg_pid=fork()))
    {
    /* Error */
    case -1:
        safe_write(STDERR_FILENO, fork_err, sizeof(fork_err)-1);
        raise(signum);
        return;

    case 0:
        dup2(fd[0], STDIN_FILENO);
        close(fd[0]);
        close(fd[1]);

        execl(argv0, argv0, crash_switch, nullptr);

        safe_write(STDERR_FILENO, exec_err, sizeof(exec_err)-1);
        _exit(1);

    default:
#ifdef __linux__
        prctl(PR_SET_PTRACER, dbg_pid, 0, 0, 0);
#endif
        safe_write(fd[1], &crash_info, sizeof(crash_info));
        close(fd[0]);
        close(fd[1]);

        /* Wait; we'll be killed when gdb is done */
        do {
            int status;
            if(waitpid(dbg_pid, &status, 0) == dbg_pid &&
                    (WIFEXITED(status) || WIFSIGNALED(status)))
            {
                /* The debug process died before it could kill us */
                raise(signum);
                break;
            }
        } while(1);
    }
}

static void crash_handler(const char *logfile)
{
    const char *sigdesc = "";
    int i;

    if(fread(&crash_info, sizeof(crash_info), 1, stdin) != 1)
    {
        fprintf(stderr, "!!! Failed to retrieve info from crashed process\n");
        exit(1);
    }

    /* Get the signal description */
    for(i = 0;signals[i].name;++i)
    {
        if(signals[i].signum == crash_info.signum)
        {
            sigdesc = signals[i].name;
            break;
        }
    }

    if(crash_info.has_siginfo)
    {
        switch(crash_info.signum)
        {
        case SIGSEGV:
            for(i = 0;sigsegv_codes[i].name;++i)
            {
                if(sigsegv_codes[i].code == crash_info.siginfo.si_code)
                {
                    sigdesc = sigsegv_codes[i].name;
                    break;
                }
            }
            break;

        case SIGFPE:
            for(i = 0;sigfpe_codes[i].name;++i)
            {
                if(sigfpe_codes[i].code == crash_info.siginfo.si_code)
                {
                    sigdesc = sigfpe_codes[i].name;
                    break;
                }
            }
            break;

        case SIGILL:
            for(i = 0;sigill_codes[i].name;++i)
            {
                if(sigill_codes[i].code == crash_info.siginfo.si_code)
                {
                    sigdesc = sigill_codes[i].name;
                    break;
                }
            }
            break;

        case SIGBUS:
            for(i = 0;sigbus_codes[i].name;++i)
            {
                if(sigbus_codes[i].code == crash_info.siginfo.si_code)
                {
                    sigdesc = sigbus_codes[i].name;
                    break;
                }
            }
            break;
        }
    }
    fprintf(stderr, "%s (signal %i)\n", sigdesc, crash_info.signum);
    if(crash_info.has_siginfo)
        fprintf(stderr, "Address: %p\n", crash_info.siginfo.si_addr);
    fputc('\n', stderr);

    if(logfile)
    {
        /* Create crash log file and redirect shell output to it */
        if(freopen(logfile, "wa", stdout) != stdout)
        {
            fprintf(stderr, "!!! Could not create %s following signal\n", logfile);
            exit(1);
        }
        fprintf(stderr, "Generating %s and killing process %d, please wait... ", logfile, crash_info.pid);

        printf("*** Fatal Error ***\n"
               "%s (signal %i)\n", sigdesc, crash_info.signum);
        if(crash_info.has_siginfo)
            printf("Address: %p\n", crash_info.siginfo.si_addr);
        fputc('\n', stdout);
        fflush(stdout);
    }

    sys_info();

    crash_info.buf[sizeof(crash_info.buf)-1] = '\0';
    printf("%s\n", crash_info.buf);
    fflush(stdout);

    if(crash_info.pid > 0)
    {
        gdb_info(crash_info.pid);
        kill(crash_info.pid, SIGKILL);
    }

    // delay between killing of the crashed process and showing the message box to
    // work around occasional X server lock-up. this can only be a bug in X11 since
    // even faulty applications shouldn't be able to freeze the X server.
    usleep(100000);

    if(logfile)
    {
        std::string message = "OpenMW has encountered a fatal error.\nCrash log saved to '" + std::string(logfile) + "'.\n Please report this to https://gitlab.com/OpenMW/openmw/issues !";
        SDL_ShowSimpleMessageBox(0, "Fatal Error", message.c_str(), nullptr);
    }

    exit(0);
}

static void getExecPath(char **argv)
{
#if defined (__FreeBSD__)
    int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
    size_t size = sizeof(argv0);

    if (sysctl(mib, 4, argv0, &size, nullptr, 0) == 0)
        return;
#endif

#if defined (__APPLE__)
    if(proc_pidpath(getpid(), argv0, sizeof(argv0)) > 0)
        return;
#endif
    int cwdlen;
    const char *statusPaths[] = {"/proc/self/exe", "/proc/self/file", "/proc/curproc/exe", "/proc/curproc/file"};
    memset(argv0, 0, sizeof(argv0));

    for(const char *path : statusPaths)
    {
        if (readlink(path, argv0, sizeof(argv0)) != -1)
            return;
    }

    if(argv[0][0] == '/')
        snprintf(argv0, sizeof(argv0), "%s", argv[0]);
    else if (getcwd(argv0, sizeof(argv0)) != nullptr)
    {
        cwdlen = strlen(argv0);
        snprintf(argv0+cwdlen, sizeof(argv0)-cwdlen, "/%s", argv[0]);
    }
}

int crashCatcherInstallHandlers(int argc, char **argv, int num_signals, int *signals, const char *logfile, int (*user_info)(char*, char*))
{
    struct sigaction sa;
    stack_t altss;
    int retval;

    if(argc == 2 && strcmp(argv[1], crash_switch) == 0)
        crash_handler(logfile);

    cc_user_info = user_info;

    getExecPath(argv);

    /* Set an alternate signal stack so SIGSEGVs caused by stack overflows
     * still run */
    static char* altstack = new char [SIGSTKSZ];
    altss.ss_sp = altstack;
    altss.ss_flags = 0;
    altss.ss_size = SIGSTKSZ;
    sigaltstack(&altss, nullptr);

    memset(&sa, 0, sizeof(sa));
    sa.sa_sigaction = crash_catcher;
    sa.sa_flags = SA_RESETHAND | SA_NODEFER | SA_SIGINFO | SA_ONSTACK;
    sigemptyset(&sa.sa_mask);

    retval = 0;
    while(num_signals--)
    {
        if((*signals != SIGSEGV && *signals != SIGILL && *signals != SIGFPE && *signals != SIGABRT &&
            *signals != SIGBUS) || sigaction(*signals, &sa, nullptr) == -1)
        {
            *signals = 0;
            retval = -1;
        }
        ++signals;
    }
    return retval;
}

static bool is_debugger_present()
{
#if defined (__linux__)
    bfs::path procstatus = bfs::path("/proc/self/status");
    if (bfs::exists(procstatus))
    {
        bfs::ifstream file((procstatus));
        while (!file.eof())
        {
            std::string word;
            file >> word;
            if (word == "TracerPid:")
            {
                file >> word;
                return word != "0";
            }
        }
    }
    return false;
#elif defined(__APPLE__)
    int junk;
    int mib[4];
    struct kinfo_proc info;
    size_t size;

    // Initialize the flags so that, if sysctl fails for some bizarre
    // reason, we get a predictable result.

    info.kp_proc.p_flag = 0;

    // Initialize mib, which tells sysctl the info we want, in this case
    // we're looking for information about a specific process ID.

    mib[0] = CTL_KERN;
    mib[1] = KERN_PROC;
    mib[2] = KERN_PROC_PID;
    mib[3] = getpid();

    // Call sysctl.

    size = sizeof(info);
    junk = sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, nullptr, 0);
    assert(junk == 0);

    // We're being debugged if the P_TRACED flag is set.

    return (info.kp_proc.p_flag & P_TRACED) != 0;
#elif defined(__FreeBSD__)
    struct kinfo_proc info;
    size_t size = sizeof(info);
    int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid() };

    if (sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, nullptr, 0) == 0)
        return (info.ki_flag & P_TRACED) != 0;
    else
        perror("Failed to retrieve process info");
    return false;
#else
    return false;
#endif
}

void crashCatcherInstall(int argc, char **argv, const std::string &crashLogPath)
{
    if (const auto env = std::getenv("OPENMW_DISABLE_CRASH_CATCHER"))
        if (std::atol(env) != 0)
            return;
    if ((argc == 2 && strcmp(argv[1], crash_switch) == 0) || !is_debugger_present())
    {
        int s[5] = { SIGSEGV, SIGILL, SIGFPE, SIGBUS, SIGABRT };
        if (crashCatcherInstallHandlers(argc, argv, 5, s, crashLogPath.c_str(), nullptr) == -1)
        {
            Log(Debug::Warning) << "Installing crash handler failed";
        }
        else
            Log(Debug::Info) << "Crash handler installed";
    }
}