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 <string.h>
21#include <stdio.h>
22#include <time.h>
23#include <errno.h>
24#include <limits.h>
25#include <dirent.h>
26#include <unistd.h>
27#include <sys/types.h>
28#include <sys/ptrace.h>
29
30#include <corkscrew/backtrace.h>
31
32#include "tombstone.h"
33#include "utility.h"
34
35#define STACK_DEPTH 32
36
37static void dump_process_header(log_t* log, pid_t pid) {
38    char path[PATH_MAX];
39    char procnamebuf[1024];
40    char* procname = NULL;
41    FILE* fp;
42
43    snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
44    if ((fp = fopen(path, "r"))) {
45        procname = fgets(procnamebuf, sizeof(procnamebuf), fp);
46        fclose(fp);
47    }
48
49    time_t t = time(NULL);
50    struct tm tm;
51    localtime_r(&t, &tm);
52    char timestr[64];
53    strftime(timestr, sizeof(timestr), "%F %T", &tm);
54    _LOG(log, false, "\n\n----- pid %d at %s -----\n", pid, timestr);
55
56    if (procname) {
57        _LOG(log, false, "Cmd line: %s\n", procname);
58    }
59}
60
61static void dump_process_footer(log_t* log, pid_t pid) {
62    _LOG(log, false, "\n----- end %d -----\n", pid);
63}
64
65static void dump_thread(log_t* log, pid_t tid, ptrace_context_t* context, bool attached,
66        bool* detach_failed, int* total_sleep_time_usec) {
67    char path[PATH_MAX];
68    char threadnamebuf[1024];
69    char* threadname = NULL;
70    FILE* fp;
71
72    snprintf(path, sizeof(path), "/proc/%d/comm", tid);
73    if ((fp = fopen(path, "r"))) {
74        threadname = fgets(threadnamebuf, sizeof(threadnamebuf), fp);
75        fclose(fp);
76        if (threadname) {
77            size_t len = strlen(threadname);
78            if (len && threadname[len - 1] == '\n') {
79                threadname[len - 1] = '\0';
80            }
81        }
82    }
83
84    _LOG(log, false, "\n\"%s\" sysTid=%d\n", threadname ? threadname : "<unknown>", tid);
85
86    if (!attached && ptrace(PTRACE_ATTACH, tid, 0, 0) < 0) {
87        _LOG(log, false, "Could not attach to thread: %s\n", strerror(errno));
88        return;
89    }
90
91    wait_for_stop(tid, total_sleep_time_usec);
92
93    backtrace_frame_t backtrace[STACK_DEPTH];
94    ssize_t frames = unwind_backtrace_ptrace(tid, context, backtrace, 0, STACK_DEPTH);
95    if (frames <= 0) {
96        _LOG(log, false, "Could not obtain stack trace for thread.\n");
97    } else {
98        backtrace_symbol_t backtrace_symbols[STACK_DEPTH];
99        get_backtrace_symbols_ptrace(context, backtrace, frames, backtrace_symbols);
100        for (size_t i = 0; i < (size_t)frames; i++) {
101            char line[MAX_BACKTRACE_LINE_LENGTH];
102            format_backtrace_line(i, &backtrace[i], &backtrace_symbols[i],
103                    line, MAX_BACKTRACE_LINE_LENGTH);
104            _LOG(log, false, "  %s\n", line);
105        }
106        free_backtrace_symbols(backtrace_symbols, frames);
107    }
108
109    if (!attached && ptrace(PTRACE_DETACH, tid, 0, 0) != 0) {
110        LOG("ptrace detach from %d failed: %s\n", tid, strerror(errno));
111        *detach_failed = true;
112    }
113}
114
115void dump_backtrace(int fd, pid_t pid, pid_t tid, bool* detach_failed,
116        int* total_sleep_time_usec) {
117    log_t log;
118    log.tfd = fd;
119    log.quiet = true;
120
121    ptrace_context_t* context = load_ptrace_context(tid);
122    dump_process_header(&log, pid);
123    dump_thread(&log, tid, context, true, detach_failed, total_sleep_time_usec);
124
125    char task_path[64];
126    snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid);
127    DIR* d = opendir(task_path);
128    if (d) {
129        struct dirent debuf;
130        struct dirent *de;
131        while (!readdir_r(d, &debuf, &de) && de) {
132            if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
133                continue;
134            }
135
136            char* end;
137            pid_t new_tid = strtoul(de->d_name, &end, 10);
138            if (*end || new_tid == tid) {
139                continue;
140            }
141
142            dump_thread(&log, new_tid, context, false, detach_failed, total_sleep_time_usec);
143        }
144        closedir(d);
145    }
146
147    dump_process_footer(&log, pid);
148    free_ptrace_context(context);
149}
150