| 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 |  | 
| Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 70 | static void dump_thread(log_t* log, BacktraceMap* map, pid_t pid, pid_t tid) { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 71 | char path[PATH_MAX]; | 
|  | 72 | char threadnamebuf[1024]; | 
|  | 73 | char* threadname = NULL; | 
|  | 74 | FILE* fp; | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 75 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 76 | snprintf(path, sizeof(path), "/proc/%d/comm", tid); | 
|  | 77 | if ((fp = fopen(path, "r"))) { | 
|  | 78 | threadname = fgets(threadnamebuf, sizeof(threadnamebuf), fp); | 
|  | 79 | fclose(fp); | 
|  | 80 | if (threadname) { | 
|  | 81 | size_t len = strlen(threadname); | 
|  | 82 | if (len && threadname[len - 1] == '\n') { | 
|  | 83 | threadname[len - 1] = '\0'; | 
|  | 84 | } | 
| 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 | } | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 87 |  | 
| Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 88 | _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] | 89 |  | 
| Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 90 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, tid, map)); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 91 | if (backtrace->Unwind(0)) { | 
| Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 92 | dump_backtrace_to_log(backtrace.get(), log, "  "); | 
| Christopher Ferris | 30c942c | 2015-05-14 15:39:52 -0700 | [diff] [blame] | 93 | } else { | 
|  | 94 | ALOGE("Unwind failed: tid = %d", tid); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 95 | } | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 96 | } | 
|  | 97 |  | 
| Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 98 | void dump_backtrace(int fd, int amfd, BacktraceMap* map, pid_t pid, pid_t tid, | 
|  | 99 | const std::set<pid_t>& siblings) { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 100 | log_t log; | 
|  | 101 | log.tfd = fd; | 
|  | 102 | log.amfd = amfd; | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 103 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 104 | dump_process_header(&log, pid); | 
| Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 105 | dump_thread(&log, map, pid, tid); | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 106 |  | 
| Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 107 | for (pid_t sibling : siblings) { | 
|  | 108 | dump_thread(&log, map, pid, sibling); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 109 | } | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 110 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 111 | dump_process_footer(&log, pid); | 
| Christopher Ferris | 365e4ae | 2013-10-02 12:26:48 -0700 | [diff] [blame] | 112 | } | 
|  | 113 |  | 
| Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 114 | 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] | 115 | for (size_t i = 0; i < backtrace->NumFrames(); i++) { | 
| Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 116 | _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] | 117 | } | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 118 | } |