blob: 8f4a53f577654f2788376bff6fae351470d394e7 [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>
Christopher Ferris9818bd22016-05-03 16:32:13 -070032#include <string>
Christopher Ferris6e964032015-05-15 17:30:21 -070033
Christopher Ferris20303f82014-01-10 16:33:16 -080034#include <backtrace/Backtrace.h>
Christopher Ferris30c942c2015-05-14 15:39:52 -070035
36#include <log/log.h>
Jeff Brown053b8652012-06-06 16:25:03 -070037
Christopher Ferris365e4ae2013-10-02 12:26:48 -070038#include "backtrace.h"
Brigid Smith62ba4892014-06-10 11:53:08 -070039
Jeff Brown053b8652012-06-06 16:25:03 -070040#include "utility.h"
41
Jeff Brown053b8652012-06-06 16:25:03 -070042static void dump_process_header(log_t* log, pid_t pid) {
Christopher Ferris20303f82014-01-10 16:33:16 -080043 char path[PATH_MAX];
44 char procnamebuf[1024];
45 char* procname = NULL;
46 FILE* fp;
Jeff Brown053b8652012-06-06 16:25:03 -070047
Christopher Ferris20303f82014-01-10 16:33:16 -080048 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
49 if ((fp = fopen(path, "r"))) {
50 procname = fgets(procnamebuf, sizeof(procnamebuf), fp);
51 fclose(fp);
52 }
Jeff Brown053b8652012-06-06 16:25:03 -070053
Christopher Ferris20303f82014-01-10 16:33:16 -080054 time_t t = time(NULL);
55 struct tm tm;
56 localtime_r(&t, &tm);
57 char timestr[64];
58 strftime(timestr, sizeof(timestr), "%F %T", &tm);
Jeff Brown9b12d532014-09-10 15:47:49 -070059 _LOG(log, logtype::BACKTRACE, "\n\n----- pid %d at %s -----\n", pid, timestr);
Jeff Brown053b8652012-06-06 16:25:03 -070060
Christopher Ferris20303f82014-01-10 16:33:16 -080061 if (procname) {
Brigid Smith62ba4892014-06-10 11:53:08 -070062 _LOG(log, logtype::BACKTRACE, "Cmd line: %s\n", procname);
Christopher Ferris20303f82014-01-10 16:33:16 -080063 }
Jeff Brown9b12d532014-09-10 15:47:49 -070064 _LOG(log, logtype::BACKTRACE, "ABI: '%s'\n", ABI_STRING);
Jeff Brown053b8652012-06-06 16:25:03 -070065}
66
67static void dump_process_footer(log_t* log, pid_t pid) {
Brigid Smith62ba4892014-06-10 11:53:08 -070068 _LOG(log, logtype::BACKTRACE, "\n----- end %d -----\n", pid);
Jeff Brown053b8652012-06-06 16:25:03 -070069}
70
Josh Gao7c89f9e2016-01-13 17:57:14 -080071static void dump_thread(log_t* log, BacktraceMap* map, pid_t pid, pid_t tid) {
Christopher Ferris20303f82014-01-10 16:33:16 -080072 char path[PATH_MAX];
73 char threadnamebuf[1024];
74 char* threadname = NULL;
75 FILE* fp;
Jeff Brown053b8652012-06-06 16:25:03 -070076
Christopher Ferris20303f82014-01-10 16:33:16 -080077 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 Brown053b8652012-06-06 16:25:03 -070086 }
Christopher Ferris20303f82014-01-10 16:33:16 -080087 }
Jeff Brown053b8652012-06-06 16:25:03 -070088
Brigid Smith62ba4892014-06-10 11:53:08 -070089 _LOG(log, logtype::BACKTRACE, "\n\"%s\" sysTid=%d\n", threadname ? threadname : "<unknown>", tid);
Jeff Brown053b8652012-06-06 16:25:03 -070090
Josh Gao7c89f9e2016-01-13 17:57:14 -080091 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, tid, map));
Christopher Ferris20303f82014-01-10 16:33:16 -080092 if (backtrace->Unwind(0)) {
Brigid Smith62ba4892014-06-10 11:53:08 -070093 dump_backtrace_to_log(backtrace.get(), log, " ");
Christopher Ferris30c942c2015-05-14 15:39:52 -070094 } else {
Christopher Ferrisc463ba42016-03-09 14:35:54 -080095 ALOGE("Unwind failed: tid = %d: %s", tid,
96 backtrace->GetErrorString(backtrace->GetError()).c_str());
Christopher Ferris20303f82014-01-10 16:33:16 -080097 }
Jeff Brown053b8652012-06-06 16:25:03 -070098}
99
Christopher Ferris9818bd22016-05-03 16:32:13 -0700100void dump_backtrace(int fd, BacktraceMap* map, pid_t pid, pid_t tid,
101 const std::set<pid_t>& siblings, std::string* amfd_data) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800102 log_t log;
103 log.tfd = fd;
Christopher Ferris9818bd22016-05-03 16:32:13 -0700104 log.amfd_data = amfd_data;
Jeff Brown053b8652012-06-06 16:25:03 -0700105
Christopher Ferris20303f82014-01-10 16:33:16 -0800106 dump_process_header(&log, pid);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800107 dump_thread(&log, map, pid, tid);
Jeff Brown053b8652012-06-06 16:25:03 -0700108
Josh Gao7c89f9e2016-01-13 17:57:14 -0800109 for (pid_t sibling : siblings) {
110 dump_thread(&log, map, pid, sibling);
Christopher Ferris20303f82014-01-10 16:33:16 -0800111 }
Jeff Brown053b8652012-06-06 16:25:03 -0700112
Christopher Ferris20303f82014-01-10 16:33:16 -0800113 dump_process_footer(&log, pid);
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700114}
115
Brigid Smith62ba4892014-06-10 11:53:08 -0700116void dump_backtrace_to_log(Backtrace* backtrace, log_t* log, const char* prefix) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800117 for (size_t i = 0; i < backtrace->NumFrames(); i++) {
Brigid Smith62ba4892014-06-10 11:53:08 -0700118 _LOG(log, logtype::BACKTRACE, "%s%s\n", prefix, backtrace->FormatFrameData(i).c_str());
Christopher Ferris20303f82014-01-10 16:33:16 -0800119 }
Jeff Brown053b8652012-06-06 16:25:03 -0700120}