Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 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 | |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame] | 30 | #include <backtrace/backtrace.h> |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 31 | |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame] | 32 | #include "backtrace.h" |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 33 | #include "utility.h" |
| 34 | |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 35 | static void dump_process_header(log_t* log, pid_t pid) { |
| 36 | char path[PATH_MAX]; |
| 37 | char procnamebuf[1024]; |
| 38 | char* procname = NULL; |
| 39 | FILE* fp; |
| 40 | |
| 41 | snprintf(path, sizeof(path), "/proc/%d/cmdline", pid); |
| 42 | if ((fp = fopen(path, "r"))) { |
| 43 | procname = fgets(procnamebuf, sizeof(procnamebuf), fp); |
| 44 | fclose(fp); |
| 45 | } |
| 46 | |
| 47 | time_t t = time(NULL); |
| 48 | struct tm tm; |
| 49 | localtime_r(&t, &tm); |
| 50 | char timestr[64]; |
| 51 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Christopher Tate | 7716aef | 2013-04-02 14:00:27 -0700 | [diff] [blame] | 52 | _LOG(log, SCOPE_AT_FAULT, "\n\n----- pid %d at %s -----\n", pid, timestr); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 53 | |
| 54 | if (procname) { |
Christopher Tate | 7716aef | 2013-04-02 14:00:27 -0700 | [diff] [blame] | 55 | _LOG(log, SCOPE_AT_FAULT, "Cmd line: %s\n", procname); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
| 59 | static void dump_process_footer(log_t* log, pid_t pid) { |
Christopher Tate | 7716aef | 2013-04-02 14:00:27 -0700 | [diff] [blame] | 60 | _LOG(log, SCOPE_AT_FAULT, "\n----- end %d -----\n", pid); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame] | 63 | static void dump_thread(log_t* log, pid_t tid, bool attached, |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 64 | bool* detach_failed, int* total_sleep_time_usec) { |
| 65 | char path[PATH_MAX]; |
| 66 | char threadnamebuf[1024]; |
| 67 | char* threadname = NULL; |
| 68 | FILE* fp; |
| 69 | |
| 70 | snprintf(path, sizeof(path), "/proc/%d/comm", tid); |
| 71 | if ((fp = fopen(path, "r"))) { |
| 72 | threadname = fgets(threadnamebuf, sizeof(threadnamebuf), fp); |
| 73 | fclose(fp); |
| 74 | if (threadname) { |
| 75 | size_t len = strlen(threadname); |
| 76 | if (len && threadname[len - 1] == '\n') { |
| 77 | threadname[len - 1] = '\0'; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
Christopher Tate | 7716aef | 2013-04-02 14:00:27 -0700 | [diff] [blame] | 82 | _LOG(log, SCOPE_AT_FAULT, "\n\"%s\" sysTid=%d\n", |
| 83 | threadname ? threadname : "<unknown>", tid); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 84 | |
| 85 | if (!attached && ptrace(PTRACE_ATTACH, tid, 0, 0) < 0) { |
Christopher Tate | 7716aef | 2013-04-02 14:00:27 -0700 | [diff] [blame] | 86 | _LOG(log, SCOPE_AT_FAULT, "Could not attach to thread: %s\n", strerror(errno)); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 87 | return; |
| 88 | } |
| 89 | |
| 90 | wait_for_stop(tid, total_sleep_time_usec); |
| 91 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 92 | backtrace_context_t context; |
| 93 | if (!backtrace_create_context(&context, tid, -1, 0)) { |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame] | 94 | _LOG(log, SCOPE_AT_FAULT, "Could not create backtrace context.\n"); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 95 | } else { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 96 | dump_backtrace_to_log(&context, log, SCOPE_AT_FAULT, " "); |
| 97 | backtrace_destroy_context(&context); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | if (!attached && ptrace(PTRACE_DETACH, tid, 0, 0) != 0) { |
| 101 | LOG("ptrace detach from %d failed: %s\n", tid, strerror(errno)); |
| 102 | *detach_failed = true; |
| 103 | } |
| 104 | } |
| 105 | |
Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 106 | void dump_backtrace(int fd, int amfd, pid_t pid, pid_t tid, bool* detach_failed, |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 107 | int* total_sleep_time_usec) { |
| 108 | log_t log; |
| 109 | log.tfd = fd; |
Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 110 | log.amfd = amfd; |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 111 | log.quiet = true; |
| 112 | |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 113 | dump_process_header(&log, pid); |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame] | 114 | dump_thread(&log, tid, true, detach_failed, total_sleep_time_usec); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 115 | |
| 116 | char task_path[64]; |
| 117 | snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid); |
| 118 | DIR* d = opendir(task_path); |
Elliott Hughes | c463d2c | 2012-10-26 16:47:09 -0700 | [diff] [blame] | 119 | if (d != NULL) { |
| 120 | struct dirent* de = NULL; |
| 121 | while ((de = readdir(d)) != NULL) { |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 122 | if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) { |
| 123 | continue; |
| 124 | } |
| 125 | |
| 126 | char* end; |
| 127 | pid_t new_tid = strtoul(de->d_name, &end, 10); |
| 128 | if (*end || new_tid == tid) { |
| 129 | continue; |
| 130 | } |
| 131 | |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame] | 132 | dump_thread(&log, new_tid, false, detach_failed, total_sleep_time_usec); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 133 | } |
| 134 | closedir(d); |
| 135 | } |
| 136 | |
| 137 | dump_process_footer(&log, pid); |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 140 | void dump_backtrace_to_log(const backtrace_context_t* context, log_t* log, |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame] | 141 | int scope_flags, const char* prefix) { |
| 142 | char buf[512]; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 143 | for (size_t i = 0; i < context->backtrace->num_frames; i++) { |
| 144 | backtrace_format_frame_data(context, i, buf, sizeof(buf)); |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame] | 145 | _LOG(log, scope_flags, "%s%s\n", prefix, buf); |
| 146 | } |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 147 | } |