blob: b6916e5b4486885ababfbf9203efaf192ac6f694 [file] [log] [blame]
Jeff Brown053b8652012-06-06 16:25:03 -07001/*
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 Ferris30c942c2015-05-14 15:39:52 -070017#define LOG_TAG "DEBUG"
18
Jeff Brown053b8652012-06-06 16:25:03 -070019#include <stddef.h>
Jeff Brown053b8652012-06-06 16:25:03 -070020#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 Ferris6e964032015-05-15 17:30:21 -070031#include <memory>
32
Christopher Ferris20303f82014-01-10 16:33:16 -080033#include <backtrace/Backtrace.h>
Christopher Ferris30c942c2015-05-14 15:39:52 -070034
35#include <log/log.h>
Jeff Brown053b8652012-06-06 16:25:03 -070036
Christopher Ferris365e4ae2013-10-02 12:26:48 -070037#include "backtrace.h"
Brigid Smith62ba4892014-06-10 11:53:08 -070038
Jeff Brown053b8652012-06-06 16:25:03 -070039#include "utility.h"
40
Jeff Brown053b8652012-06-06 16:25:03 -070041static void dump_process_header(log_t* log, pid_t pid) {
Christopher Ferris20303f82014-01-10 16:33:16 -080042 char path[PATH_MAX];
43 char procnamebuf[1024];
44 char* procname = NULL;
45 FILE* fp;
Jeff Brown053b8652012-06-06 16:25:03 -070046
Christopher Ferris20303f82014-01-10 16:33:16 -080047 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 Brown053b8652012-06-06 16:25:03 -070052
Christopher Ferris20303f82014-01-10 16:33:16 -080053 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 Brown9b12d532014-09-10 15:47:49 -070058 _LOG(log, logtype::BACKTRACE, "\n\n----- pid %d at %s -----\n", pid, timestr);
Jeff Brown053b8652012-06-06 16:25:03 -070059
Christopher Ferris20303f82014-01-10 16:33:16 -080060 if (procname) {
Brigid Smith62ba4892014-06-10 11:53:08 -070061 _LOG(log, logtype::BACKTRACE, "Cmd line: %s\n", procname);
Christopher Ferris20303f82014-01-10 16:33:16 -080062 }
Jeff Brown9b12d532014-09-10 15:47:49 -070063 _LOG(log, logtype::BACKTRACE, "ABI: '%s'\n", ABI_STRING);
Jeff Brown053b8652012-06-06 16:25:03 -070064}
65
66static void dump_process_footer(log_t* log, pid_t pid) {
Brigid Smith62ba4892014-06-10 11:53:08 -070067 _LOG(log, logtype::BACKTRACE, "\n----- end %d -----\n", pid);
Jeff Brown053b8652012-06-06 16:25:03 -070068}
69
Josh Gao7c89f9e2016-01-13 17:57:14 -080070static void dump_thread(log_t* log, BacktraceMap* map, pid_t pid, pid_t tid) {
Christopher Ferris20303f82014-01-10 16:33:16 -080071 char path[PATH_MAX];
72 char threadnamebuf[1024];
73 char* threadname = NULL;
74 FILE* fp;
Jeff Brown053b8652012-06-06 16:25:03 -070075
Christopher Ferris20303f82014-01-10 16:33:16 -080076 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 Brown053b8652012-06-06 16:25:03 -070085 }
Christopher Ferris20303f82014-01-10 16:33:16 -080086 }
Jeff Brown053b8652012-06-06 16:25:03 -070087
Brigid Smith62ba4892014-06-10 11:53:08 -070088 _LOG(log, logtype::BACKTRACE, "\n\"%s\" sysTid=%d\n", threadname ? threadname : "<unknown>", tid);
Jeff Brown053b8652012-06-06 16:25:03 -070089
Josh Gao7c89f9e2016-01-13 17:57:14 -080090 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, tid, map));
Christopher Ferris20303f82014-01-10 16:33:16 -080091 if (backtrace->Unwind(0)) {
Brigid Smith62ba4892014-06-10 11:53:08 -070092 dump_backtrace_to_log(backtrace.get(), log, " ");
Christopher Ferris30c942c2015-05-14 15:39:52 -070093 } else {
94 ALOGE("Unwind failed: tid = %d", tid);
Christopher Ferris20303f82014-01-10 16:33:16 -080095 }
Jeff Brown053b8652012-06-06 16:25:03 -070096}
97
Josh Gao7c89f9e2016-01-13 17:57:14 -080098void dump_backtrace(int fd, int amfd, BacktraceMap* map, pid_t pid, pid_t tid,
99 const std::set<pid_t>& siblings) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800100 log_t log;
101 log.tfd = fd;
102 log.amfd = amfd;
Jeff Brown053b8652012-06-06 16:25:03 -0700103
Christopher Ferris20303f82014-01-10 16:33:16 -0800104 dump_process_header(&log, pid);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800105 dump_thread(&log, map, pid, tid);
Jeff Brown053b8652012-06-06 16:25:03 -0700106
Josh Gao7c89f9e2016-01-13 17:57:14 -0800107 for (pid_t sibling : siblings) {
108 dump_thread(&log, map, pid, sibling);
Christopher Ferris20303f82014-01-10 16:33:16 -0800109 }
Jeff Brown053b8652012-06-06 16:25:03 -0700110
Christopher Ferris20303f82014-01-10 16:33:16 -0800111 dump_process_footer(&log, pid);
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700112}
113
Brigid Smith62ba4892014-06-10 11:53:08 -0700114void dump_backtrace_to_log(Backtrace* backtrace, log_t* log, const char* prefix) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800115 for (size_t i = 0; i < backtrace->NumFrames(); i++) {
Brigid Smith62ba4892014-06-10 11:53:08 -0700116 _LOG(log, logtype::BACKTRACE, "%s%s\n", prefix, backtrace->FormatFrameData(i).c_str());
Christopher Ferris20303f82014-01-10 16:33:16 -0800117 }
Jeff Brown053b8652012-06-06 16:25:03 -0700118}