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