tombstone.c revision 536b81afed42ba6be1f824cf90d9a1e17a913c9c
1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stddef.h>
18#include <stdbool.h>
19#include <stdlib.h>
20#include <signal.h>
21#include <string.h>
22#include <stdio.h>
23#include <fcntl.h>
24#include <errno.h>
25#include <dirent.h>
26#include <time.h>
27#include <sys/ptrace.h>
28#include <sys/stat.h>
29
30#include <private/android_filesystem_config.h>
31
32#include <cutils/logger.h>
33#include <cutils/properties.h>
34
35#include <corkscrew/demangle.h>
36#include <corkscrew/backtrace.h>
37
38#include "machine.h"
39#include "tombstone.h"
40#include "utility.h"
41
42#define STACK_DEPTH 32
43#define STACK_WORDS 16
44
45#define MAX_TOMBSTONES  10
46#define TOMBSTONE_DIR   "/data/tombstones"
47
48#define typecheck(x,y) {    \
49    typeof(x) __dummy1;     \
50    typeof(y) __dummy2;     \
51    (void)(&__dummy1 == &__dummy2); }
52
53
54static bool signal_has_address(int sig) {
55    switch (sig) {
56        case SIGILL:
57        case SIGFPE:
58        case SIGSEGV:
59        case SIGBUS:
60            return true;
61        default:
62            return false;
63    }
64}
65
66static const char *get_signame(int sig)
67{
68    switch(sig) {
69    case SIGILL:     return "SIGILL";
70    case SIGABRT:    return "SIGABRT";
71    case SIGBUS:     return "SIGBUS";
72    case SIGFPE:     return "SIGFPE";
73    case SIGSEGV:    return "SIGSEGV";
74    case SIGPIPE:    return "SIGPIPE";
75    case SIGSTKFLT:  return "SIGSTKFLT";
76    case SIGSTOP:    return "SIGSTOP";
77    default:         return "?";
78    }
79}
80
81static const char *get_sigcode(int signo, int code)
82{
83    switch (signo) {
84    case SIGILL:
85        switch (code) {
86        case ILL_ILLOPC: return "ILL_ILLOPC";
87        case ILL_ILLOPN: return "ILL_ILLOPN";
88        case ILL_ILLADR: return "ILL_ILLADR";
89        case ILL_ILLTRP: return "ILL_ILLTRP";
90        case ILL_PRVOPC: return "ILL_PRVOPC";
91        case ILL_PRVREG: return "ILL_PRVREG";
92        case ILL_COPROC: return "ILL_COPROC";
93        case ILL_BADSTK: return "ILL_BADSTK";
94        }
95        break;
96    case SIGBUS:
97        switch (code) {
98        case BUS_ADRALN: return "BUS_ADRALN";
99        case BUS_ADRERR: return "BUS_ADRERR";
100        case BUS_OBJERR: return "BUS_OBJERR";
101        }
102        break;
103    case SIGFPE:
104        switch (code) {
105        case FPE_INTDIV: return "FPE_INTDIV";
106        case FPE_INTOVF: return "FPE_INTOVF";
107        case FPE_FLTDIV: return "FPE_FLTDIV";
108        case FPE_FLTOVF: return "FPE_FLTOVF";
109        case FPE_FLTUND: return "FPE_FLTUND";
110        case FPE_FLTRES: return "FPE_FLTRES";
111        case FPE_FLTINV: return "FPE_FLTINV";
112        case FPE_FLTSUB: return "FPE_FLTSUB";
113        }
114        break;
115    case SIGSEGV:
116        switch (code) {
117        case SEGV_MAPERR: return "SEGV_MAPERR";
118        case SEGV_ACCERR: return "SEGV_ACCERR";
119        }
120        break;
121    }
122    return "?";
123}
124
125static void dump_build_info(log_t* log)
126{
127    char fingerprint[PROPERTY_VALUE_MAX];
128
129    property_get("ro.build.fingerprint", fingerprint, "unknown");
130
131    _LOG(log, false, "Build fingerprint: '%s'\n", fingerprint);
132}
133
134static void dump_fault_addr(log_t* log, pid_t tid, int sig)
135{
136    siginfo_t si;
137
138    memset(&si, 0, sizeof(si));
139    if(ptrace(PTRACE_GETSIGINFO, tid, 0, &si)){
140        _LOG(log, false, "cannot get siginfo: %s\n", strerror(errno));
141    } else if (signal_has_address(sig)) {
142        _LOG(log, false, "signal %d (%s), code %d (%s), fault addr %08x\n",
143             sig, get_signame(sig),
144             si.si_code, get_sigcode(sig, si.si_code),
145             (uintptr_t) si.si_addr);
146    } else {
147        _LOG(log, false, "signal %d (%s), code %d (%s), fault addr --------\n",
148             sig, get_signame(sig), si.si_code, get_sigcode(sig, si.si_code));
149    }
150}
151
152static void dump_thread_info(log_t* log, pid_t pid, pid_t tid, bool at_fault) {
153    char path[64];
154    char threadnamebuf[1024];
155    char* threadname = NULL;
156    FILE *fp;
157
158    snprintf(path, sizeof(path), "/proc/%d/comm", tid);
159    if ((fp = fopen(path, "r"))) {
160        threadname = fgets(threadnamebuf, sizeof(threadnamebuf), fp);
161        fclose(fp);
162        if (threadname) {
163            size_t len = strlen(threadname);
164            if (len && threadname[len - 1] == '\n') {
165                threadname[len - 1] = '\0';
166            }
167        }
168    }
169
170    if (at_fault) {
171        char procnamebuf[1024];
172        char* procname = NULL;
173
174        snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
175        if ((fp = fopen(path, "r"))) {
176            procname = fgets(procnamebuf, sizeof(procnamebuf), fp);
177            fclose(fp);
178        }
179
180        _LOG(log, false, "pid: %d, tid: %d, name: %s  >>> %s <<<\n", pid, tid,
181                threadname ? threadname : "UNKNOWN",
182                procname ? procname : "UNKNOWN");
183    } else {
184        _LOG(log, true, "pid: %d, tid: %d, name: %s\n", pid, tid,
185                threadname ? threadname : "UNKNOWN");
186    }
187}
188
189static void dump_backtrace(const ptrace_context_t* context __attribute((unused)),
190        log_t* log, pid_t tid __attribute((unused)), bool at_fault,
191        const backtrace_frame_t* backtrace, size_t frames) {
192    _LOG(log, !at_fault, "\nbacktrace:\n");
193
194    backtrace_symbol_t backtrace_symbols[STACK_DEPTH];
195    get_backtrace_symbols_ptrace(context, backtrace, frames, backtrace_symbols);
196    for (size_t i = 0; i < frames; i++) {
197        char line[MAX_BACKTRACE_LINE_LENGTH];
198        format_backtrace_line(i, &backtrace[i], &backtrace_symbols[i],
199                line, MAX_BACKTRACE_LINE_LENGTH);
200        _LOG(log, !at_fault, "    %s\n", line);
201    }
202    free_backtrace_symbols(backtrace_symbols, frames);
203}
204
205static void dump_stack_segment(const ptrace_context_t* context, log_t* log, pid_t tid,
206        bool only_in_tombstone, uintptr_t* sp, size_t words, int label) {
207    for (size_t i = 0; i < words; i++) {
208        uint32_t stack_content;
209        if (!try_get_word_ptrace(tid, *sp, &stack_content)) {
210            break;
211        }
212
213        const map_info_t* mi;
214        const symbol_t* symbol;
215        find_symbol_ptrace(context, stack_content, &mi, &symbol);
216
217        if (symbol) {
218            char* demangled_name = demangle_symbol_name(symbol->name);
219            const char* symbol_name = demangled_name ? demangled_name : symbol->name;
220            uint32_t offset = stack_content - (mi->start + symbol->start);
221            if (!i && label >= 0) {
222                if (offset) {
223                    _LOG(log, only_in_tombstone, "    #%02d  %08x  %08x  %s (%s+%u)\n",
224                            label, *sp, stack_content, mi ? mi->name : "", symbol_name, offset);
225                } else {
226                    _LOG(log, only_in_tombstone, "    #%02d  %08x  %08x  %s (%s)\n",
227                            label, *sp, stack_content, mi ? mi->name : "", symbol_name);
228                }
229            } else {
230                if (offset) {
231                    _LOG(log, only_in_tombstone, "         %08x  %08x  %s (%s+%u)\n",
232                            *sp, stack_content, mi ? mi->name : "", symbol_name, offset);
233                } else {
234                    _LOG(log, only_in_tombstone, "         %08x  %08x  %s (%s)\n",
235                            *sp, stack_content, mi ? mi->name : "", symbol_name);
236                }
237            }
238            free(demangled_name);
239        } else {
240            if (!i && label >= 0) {
241                _LOG(log, only_in_tombstone, "    #%02d  %08x  %08x  %s\n",
242                        label, *sp, stack_content, mi ? mi->name : "");
243            } else {
244                _LOG(log, only_in_tombstone, "         %08x  %08x  %s\n",
245                        *sp, stack_content, mi ? mi->name : "");
246            }
247        }
248
249        *sp += sizeof(uint32_t);
250    }
251}
252
253static void dump_stack(const ptrace_context_t* context, log_t* log, pid_t tid, bool at_fault,
254        const backtrace_frame_t* backtrace, size_t frames) {
255    bool have_first = false;
256    size_t first, last;
257    for (size_t i = 0; i < frames; i++) {
258        if (backtrace[i].stack_top) {
259            if (!have_first) {
260                have_first = true;
261                first = i;
262            }
263            last = i;
264        }
265    }
266    if (!have_first) {
267        return;
268    }
269
270    _LOG(log, !at_fault, "\nstack:\n");
271
272    // Dump a few words before the first frame.
273    bool only_in_tombstone = !at_fault;
274    uintptr_t sp = backtrace[first].stack_top - STACK_WORDS * sizeof(uint32_t);
275    dump_stack_segment(context, log, tid, only_in_tombstone, &sp, STACK_WORDS, -1);
276
277    // Dump a few words from all successive frames.
278    // Only log the first 3 frames, put the rest in the tombstone.
279    for (size_t i = first; i <= last; i++) {
280        const backtrace_frame_t* frame = &backtrace[i];
281        if (sp != frame->stack_top) {
282            _LOG(log, only_in_tombstone, "         ........  ........\n");
283            sp = frame->stack_top;
284        }
285        if (i - first == 3) {
286            only_in_tombstone = true;
287        }
288        if (i == last) {
289            dump_stack_segment(context, log, tid, only_in_tombstone, &sp, STACK_WORDS, i);
290            if (sp < frame->stack_top + frame->stack_size) {
291                _LOG(log, only_in_tombstone, "         ........  ........\n");
292            }
293        } else {
294            size_t words = frame->stack_size / sizeof(uint32_t);
295            if (words == 0) {
296                words = 1;
297            } else if (words > STACK_WORDS) {
298                words = STACK_WORDS;
299            }
300            dump_stack_segment(context, log, tid, only_in_tombstone, &sp, words, i);
301        }
302    }
303}
304
305static void dump_backtrace_and_stack(const ptrace_context_t* context, log_t* log, pid_t tid,
306        bool at_fault) {
307    backtrace_frame_t backtrace[STACK_DEPTH];
308    ssize_t frames = unwind_backtrace_ptrace(tid, context, backtrace, 0, STACK_DEPTH);
309    if (frames > 0) {
310        dump_backtrace(context, log, tid, at_fault, backtrace, frames);
311        dump_stack(context, log, tid, at_fault, backtrace, frames);
312    }
313}
314
315static void dump_nearby_maps(const ptrace_context_t* context, log_t* log, pid_t tid) {
316    siginfo_t si;
317    memset(&si, 0, sizeof(si));
318    if (ptrace(PTRACE_GETSIGINFO, tid, 0, &si)) {
319        _LOG(log, false, "cannot get siginfo for %d: %s\n",
320                tid, strerror(errno));
321        return;
322    }
323    if (!signal_has_address(si.si_signo)) {
324        return;
325    }
326
327    uintptr_t addr = (uintptr_t) si.si_addr;
328    addr &= ~0xfff;     /* round to 4K page boundary */
329    if (addr == 0) {    /* null-pointer deref */
330        return;
331    }
332
333    _LOG(log, false, "\nmemory map around fault addr %08x:\n", (int)si.si_addr);
334
335    /*
336     * Search for a match, or for a hole where the match would be.  The list
337     * is backward from the file content, so it starts at high addresses.
338     */
339    map_info_t* map = context->map_info_list;
340    map_info_t *next = NULL;
341    map_info_t *prev = NULL;
342    while (map != NULL) {
343        if (addr >= map->start && addr < map->end) {
344            next = map->next;
345            break;
346        } else if (addr >= map->end) {
347            /* map would be between "prev" and this entry */
348            next = map;
349            map = NULL;
350            break;
351        }
352
353        prev = map;
354        map = map->next;
355    }
356
357    /*
358     * Show "next" then "match" then "prev" so that the addresses appear in
359     * ascending order (like /proc/pid/maps).
360     */
361    if (next != NULL) {
362        _LOG(log, false, "    %08x-%08x %s\n", next->start, next->end, next->name);
363    } else {
364        _LOG(log, false, "    (no map below)\n");
365    }
366    if (map != NULL) {
367        _LOG(log, false, "    %08x-%08x %s\n", map->start, map->end, map->name);
368    } else {
369        _LOG(log, false, "    (no map for address)\n");
370    }
371    if (prev != NULL) {
372        _LOG(log, false, "    %08x-%08x %s\n", prev->start, prev->end, prev->name);
373    } else {
374        _LOG(log, false, "    (no map above)\n");
375    }
376}
377
378static void dump_thread(const ptrace_context_t* context, log_t* log, pid_t tid, bool at_fault,
379        int* total_sleep_time_usec) {
380    wait_for_stop(tid, total_sleep_time_usec);
381
382    dump_registers(context, log, tid, at_fault);
383    dump_backtrace_and_stack(context, log, tid, at_fault);
384    if (at_fault) {
385        dump_memory_and_code(context, log, tid, at_fault);
386        dump_nearby_maps(context, log, tid);
387    }
388}
389
390/* Return true if some thread is not detached cleanly */
391static bool dump_sibling_thread_report(const ptrace_context_t* context,
392        log_t* log, pid_t pid, pid_t tid, int* total_sleep_time_usec) {
393    char task_path[64];
394    snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid);
395
396    DIR* d = opendir(task_path);
397    /* Bail early if cannot open the task directory */
398    if (d == NULL) {
399        XLOG("Cannot open /proc/%d/task\n", pid);
400        return false;
401    }
402
403    bool detach_failed = false;
404    struct dirent debuf;
405    struct dirent *de;
406    while (!readdir_r(d, &debuf, &de) && de) {
407        /* Ignore "." and ".." */
408        if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
409            continue;
410        }
411
412        /* The main thread at fault has been handled individually */
413        char* end;
414        pid_t new_tid = strtoul(de->d_name, &end, 10);
415        if (*end || new_tid == tid) {
416            continue;
417        }
418
419        /* Skip this thread if cannot ptrace it */
420        if (ptrace(PTRACE_ATTACH, new_tid, 0, 0) < 0) {
421            continue;
422        }
423
424        _LOG(log, true, "--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---\n");
425        dump_thread_info(log, pid, new_tid, false);
426        dump_thread(context, log, new_tid, false, total_sleep_time_usec);
427
428        if (ptrace(PTRACE_DETACH, new_tid, 0, 0) != 0) {
429            LOG("ptrace detach from %d failed: %s\n", new_tid, strerror(errno));
430            detach_failed = true;
431        }
432    }
433
434    closedir(d);
435    return detach_failed;
436}
437
438/*
439 * Reads the contents of the specified log device, filters out the entries
440 * that don't match the specified pid, and writes them to the tombstone file.
441 *
442 * If "tailOnly" is set, we only print the last few lines.
443 */
444static void dump_log_file(log_t* log, pid_t pid, const char* filename,
445    bool tailOnly)
446{
447    bool first = true;
448
449    /* circular buffer, for "tailOnly" mode */
450    const int kShortLogMaxLines = 5;
451    const int kShortLogLineLen = 256;
452    char shortLog[kShortLogMaxLines][kShortLogLineLen];
453    int shortLogCount = 0;
454    int shortLogNext = 0;
455
456    int logfd = open(filename, O_RDONLY | O_NONBLOCK);
457    if (logfd < 0) {
458        XLOG("Unable to open %s: %s\n", filename, strerror(errno));
459        return;
460    }
461
462    union {
463        unsigned char buf[LOGGER_ENTRY_MAX_LEN + 1];
464        struct logger_entry entry;
465    } log_entry;
466
467    while (true) {
468        ssize_t actual = read(logfd, log_entry.buf, LOGGER_ENTRY_MAX_LEN);
469        if (actual < 0) {
470            if (errno == EINTR) {
471                /* interrupted by signal, retry */
472                continue;
473            } else if (errno == EAGAIN) {
474                /* non-blocking EOF; we're done */
475                break;
476            } else {
477                _LOG(log, true, "Error while reading log: %s\n",
478                    strerror(errno));
479                break;
480            }
481        } else if (actual == 0) {
482            _LOG(log, true, "Got zero bytes while reading log: %s\n",
483                strerror(errno));
484            break;
485        }
486
487        /*
488         * NOTE: if you XLOG something here, this will spin forever,
489         * because you will be writing as fast as you're reading.  Any
490         * high-frequency debug diagnostics should just be written to
491         * the tombstone file.
492         */
493
494        struct logger_entry* entry = &log_entry.entry;
495
496        if (entry->pid != (int32_t) pid) {
497            /* wrong pid, ignore */
498            continue;
499        }
500
501        if (first) {
502            _LOG(log, true, "--------- %slog %s\n",
503                tailOnly ? "tail end of " : "", filename);
504            first = false;
505        }
506
507        /*
508         * Msg format is: <priority:1><tag:N>\0<message:N>\0
509         *
510         * We want to display it in the same format as "logcat -v threadtime"
511         * (although in this case the pid is redundant).
512         *
513         * TODO: scan for line breaks ('\n') and display each text line
514         * on a separate line, prefixed with the header, like logcat does.
515         */
516        static const char* kPrioChars = "!.VDIWEFS";
517        unsigned char prio = entry->msg[0];
518        char* tag = entry->msg + 1;
519        char* msg = tag + strlen(tag) + 1;
520
521        /* consume any trailing newlines */
522        char* eatnl = msg + strlen(msg) - 1;
523        while (eatnl >= msg && *eatnl == '\n') {
524            *eatnl-- = '\0';
525        }
526
527        char prioChar = (prio < strlen(kPrioChars) ? kPrioChars[prio] : '?');
528
529        char timeBuf[32];
530        time_t sec = (time_t) entry->sec;
531        struct tm tmBuf;
532        struct tm* ptm;
533        ptm = localtime_r(&sec, &tmBuf);
534        strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", ptm);
535
536        if (tailOnly) {
537            snprintf(shortLog[shortLogNext], kShortLogLineLen,
538                "%s.%03d %5d %5d %c %-8s: %s",
539                timeBuf, entry->nsec / 1000000, entry->pid, entry->tid,
540                prioChar, tag, msg);
541            shortLogNext = (shortLogNext + 1) % kShortLogMaxLines;
542            shortLogCount++;
543        } else {
544            _LOG(log, true, "%s.%03d %5d %5d %c %-8s: %s\n",
545                timeBuf, entry->nsec / 1000000, entry->pid, entry->tid,
546                prioChar, tag, msg);
547        }
548    }
549
550    if (tailOnly) {
551        int i;
552
553        /*
554         * If we filled the buffer, we want to start at "next", which has
555         * the oldest entry.  If we didn't, we want to start at zero.
556         */
557        if (shortLogCount < kShortLogMaxLines) {
558            shortLogNext = 0;
559        } else {
560            shortLogCount = kShortLogMaxLines;  /* cap at window size */
561        }
562
563        for (i = 0; i < shortLogCount; i++) {
564            _LOG(log, true, "%s\n", shortLog[shortLogNext]);
565            shortLogNext = (shortLogNext + 1) % kShortLogMaxLines;
566        }
567    }
568
569    close(logfd);
570}
571
572/*
573 * Dumps the logs generated by the specified pid to the tombstone, from both
574 * "system" and "main" log devices.  Ideally we'd interleave the output.
575 */
576static void dump_logs(log_t* log, pid_t pid, bool tailOnly)
577{
578    dump_log_file(log, pid, "/dev/log/system", tailOnly);
579    dump_log_file(log, pid, "/dev/log/main", tailOnly);
580}
581
582/*
583 * Dumps all information about the specified pid to the tombstone.
584 */
585static bool dump_crash(log_t* log, pid_t pid, pid_t tid, int signal,
586        bool dump_sibling_threads, int* total_sleep_time_usec)
587{
588    /* don't copy log messages to tombstone unless this is a dev device */
589    char value[PROPERTY_VALUE_MAX];
590    property_get("ro.debuggable", value, "0");
591    bool want_logs = (value[0] == '1');
592
593    _LOG(log, false,
594            "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n");
595    dump_build_info(log);
596    dump_thread_info(log, pid, tid, true);
597    if(signal) {
598        dump_fault_addr(log, tid, signal);
599    }
600
601    ptrace_context_t* context = load_ptrace_context(tid);
602    dump_thread(context, log, tid, true, total_sleep_time_usec);
603
604    if (want_logs) {
605        dump_logs(log, pid, true);
606    }
607
608    bool detach_failed = false;
609    if (dump_sibling_threads) {
610        detach_failed = dump_sibling_thread_report(context, log, pid, tid, total_sleep_time_usec);
611    }
612
613    free_ptrace_context(context);
614
615    if (want_logs) {
616        dump_logs(log, pid, false);
617    }
618    return detach_failed;
619}
620
621/*
622 * find_and_open_tombstone - find an available tombstone slot, if any, of the
623 * form tombstone_XX where XX is 00 to MAX_TOMBSTONES-1, inclusive. If no
624 * file is available, we reuse the least-recently-modified file.
625 *
626 * Returns the path of the tombstone file, allocated using malloc().  Caller must free() it.
627 */
628static char* find_and_open_tombstone(int* fd)
629{
630    unsigned long mtime = ULONG_MAX;
631    struct stat sb;
632
633    /*
634     * XXX: Our stat.st_mtime isn't time_t. If it changes, as it probably ought
635     * to, our logic breaks. This check will generate a warning if that happens.
636     */
637    typecheck(mtime, sb.st_mtime);
638
639    /*
640     * In a single wolf-like pass, find an available slot and, in case none
641     * exist, find and record the least-recently-modified file.
642     */
643    char path[128];
644    int oldest = 0;
645    for (int i = 0; i < MAX_TOMBSTONES; i++) {
646        snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", i);
647
648        if (!stat(path, &sb)) {
649            if (sb.st_mtime < mtime) {
650                oldest = i;
651                mtime = sb.st_mtime;
652            }
653            continue;
654        }
655        if (errno != ENOENT)
656            continue;
657
658        *fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0600);
659        if (*fd < 0)
660            continue;   /* raced ? */
661
662        fchown(*fd, AID_SYSTEM, AID_SYSTEM);
663        return strdup(path);
664    }
665
666    /* we didn't find an available file, so we clobber the oldest one */
667    snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", oldest);
668    *fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
669    if (*fd < 0) {
670        LOG("failed to open tombstone file '%s': %s\n", path, strerror(errno));
671        return NULL;
672    }
673    fchown(*fd, AID_SYSTEM, AID_SYSTEM);
674    return strdup(path);
675}
676
677char* engrave_tombstone(pid_t pid, pid_t tid, int signal,
678        bool dump_sibling_threads, bool quiet, bool* detach_failed,
679        int* total_sleep_time_usec) {
680    mkdir(TOMBSTONE_DIR, 0755);
681    chown(TOMBSTONE_DIR, AID_SYSTEM, AID_SYSTEM);
682
683    int fd;
684    char* path = find_and_open_tombstone(&fd);
685    if (!path) {
686        *detach_failed = false;
687        return NULL;
688    }
689
690    log_t log;
691    log.tfd = fd;
692    log.quiet = quiet;
693    *detach_failed = dump_crash(&log, pid, tid, signal, dump_sibling_threads,
694            total_sleep_time_usec);
695
696    close(fd);
697    return path;
698}
699