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 | |
Christopher Ferris | 30c942c | 2015-05-14 15:39:52 -0700 | [diff] [blame] | 17 | #define LOG_TAG "DEBUG" |
| 18 | |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 19 | #include <stddef.h> |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <stdio.h> |
| 23 | #include <time.h> |
| 24 | #include <errno.h> |
| 25 | #include <limits.h> |
| 26 | #include <dirent.h> |
| 27 | #include <unistd.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <sys/ptrace.h> |
| 30 | |
Christopher Ferris | 6e96403 | 2015-05-15 17:30:21 -0700 | [diff] [blame] | 31 | #include <memory> |
| 32 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 33 | #include <backtrace/Backtrace.h> |
Christopher Ferris | 30c942c | 2015-05-14 15:39:52 -0700 | [diff] [blame] | 34 | |
| 35 | #include <log/log.h> |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 36 | |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame] | 37 | #include "backtrace.h" |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 38 | |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 39 | #include "utility.h" |
| 40 | |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 41 | static void dump_process_header(log_t* log, pid_t pid) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 42 | char path[PATH_MAX]; |
| 43 | char procnamebuf[1024]; |
| 44 | char* procname = NULL; |
| 45 | FILE* fp; |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 46 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 47 | snprintf(path, sizeof(path), "/proc/%d/cmdline", pid); |
| 48 | if ((fp = fopen(path, "r"))) { |
| 49 | procname = fgets(procnamebuf, sizeof(procnamebuf), fp); |
| 50 | fclose(fp); |
| 51 | } |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 52 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 53 | time_t t = time(NULL); |
| 54 | struct tm tm; |
| 55 | localtime_r(&t, &tm); |
| 56 | char timestr[64]; |
| 57 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Jeff Brown | 9b12d53 | 2014-09-10 15:47:49 -0700 | [diff] [blame] | 58 | _LOG(log, logtype::BACKTRACE, "\n\n----- pid %d at %s -----\n", pid, timestr); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 59 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 60 | if (procname) { |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 61 | _LOG(log, logtype::BACKTRACE, "Cmd line: %s\n", procname); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 62 | } |
Jeff Brown | 9b12d53 | 2014-09-10 15:47:49 -0700 | [diff] [blame] | 63 | _LOG(log, logtype::BACKTRACE, "ABI: '%s'\n", ABI_STRING); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | static void dump_process_footer(log_t* log, pid_t pid) { |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 67 | _LOG(log, logtype::BACKTRACE, "\n----- end %d -----\n", pid); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 70 | static void dump_thread( |
| 71 | log_t* log, pid_t tid, bool attached, bool* detach_failed, int* total_sleep_time_usec) { |
| 72 | char path[PATH_MAX]; |
| 73 | char threadnamebuf[1024]; |
| 74 | char* threadname = NULL; |
| 75 | FILE* fp; |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 76 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 77 | snprintf(path, sizeof(path), "/proc/%d/comm", tid); |
| 78 | if ((fp = fopen(path, "r"))) { |
| 79 | threadname = fgets(threadnamebuf, sizeof(threadnamebuf), fp); |
| 80 | fclose(fp); |
| 81 | if (threadname) { |
| 82 | size_t len = strlen(threadname); |
| 83 | if (len && threadname[len - 1] == '\n') { |
| 84 | threadname[len - 1] = '\0'; |
| 85 | } |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 86 | } |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 87 | } |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 88 | |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 89 | _LOG(log, logtype::BACKTRACE, "\n\"%s\" sysTid=%d\n", threadname ? threadname : "<unknown>", tid); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 90 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 91 | if (!attached && ptrace(PTRACE_ATTACH, tid, 0, 0) < 0) { |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 92 | _LOG(log, logtype::BACKTRACE, "Could not attach to thread: %s\n", strerror(errno)); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 93 | return; |
| 94 | } |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 95 | |
Christopher Ferris | 1072f91 | 2014-10-31 21:34:38 -0700 | [diff] [blame] | 96 | if (!attached && wait_for_sigstop(tid, total_sleep_time_usec, detach_failed) == -1) { |
| 97 | return; |
| 98 | } |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 99 | |
Christopher Ferris | 6e96403 | 2015-05-15 17:30:21 -0700 | [diff] [blame] | 100 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(tid, BACKTRACE_CURRENT_THREAD)); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 101 | if (backtrace->Unwind(0)) { |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 102 | dump_backtrace_to_log(backtrace.get(), log, " "); |
Christopher Ferris | 30c942c | 2015-05-14 15:39:52 -0700 | [diff] [blame] | 103 | } else { |
| 104 | ALOGE("Unwind failed: tid = %d", tid); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 105 | } |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 106 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 107 | if (!attached && ptrace(PTRACE_DETACH, tid, 0, 0) != 0) { |
Christopher Ferris | b36b592 | 2015-06-17 18:35:59 -0700 | [diff] [blame] | 108 | ALOGE("ptrace detach from %d failed: %s\n", tid, strerror(errno)); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 109 | *detach_failed = true; |
| 110 | } |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 113 | void dump_backtrace(int fd, int amfd, pid_t pid, pid_t tid, bool* detach_failed, |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 114 | int* total_sleep_time_usec) { |
| 115 | log_t log; |
| 116 | log.tfd = fd; |
| 117 | log.amfd = amfd; |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 118 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 119 | dump_process_header(&log, pid); |
| 120 | dump_thread(&log, tid, true, detach_failed, total_sleep_time_usec); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 121 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 122 | char task_path[64]; |
| 123 | snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid); |
| 124 | DIR* d = opendir(task_path); |
| 125 | if (d != NULL) { |
| 126 | struct dirent* de = NULL; |
| 127 | while ((de = readdir(d)) != NULL) { |
| 128 | if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) { |
| 129 | continue; |
| 130 | } |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 131 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 132 | char* end; |
| 133 | pid_t new_tid = strtoul(de->d_name, &end, 10); |
| 134 | if (*end || new_tid == tid) { |
| 135 | continue; |
| 136 | } |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 137 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 138 | dump_thread(&log, new_tid, false, detach_failed, total_sleep_time_usec); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 139 | } |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 140 | closedir(d); |
| 141 | } |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 142 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 143 | dump_process_footer(&log, pid); |
Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 146 | void dump_backtrace_to_log(Backtrace* backtrace, log_t* log, const char* prefix) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 147 | for (size_t i = 0; i < backtrace->NumFrames(); i++) { |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 148 | _LOG(log, logtype::BACKTRACE, "%s%s\n", prefix, backtrace->FormatFrameData(i).c_str()); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 149 | } |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 150 | } |