blob: 73ed4ebefd5076704e66aaae758a98a695a21a36 [file] [log] [blame]
Igor Murashkinec79ef22013-10-24 17:09:15 -07001/*
2 * Copyright (C) 2013 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#define LOG_TAG "ProcessCallStack"
18// #define LOG_NDEBUG 0
19
Igor Murashkinec79ef22013-10-24 17:09:15 -070020#include <dirent.h>
Elliott Hughes6ed68cc2015-06-30 08:22:24 -070021#include <errno.h>
22#include <stdio.h>
23#include <string.h>
James Hawkins588a2ca2016-02-18 14:52:46 -080024#include <memory>
Igor Murashkinec79ef22013-10-24 17:09:15 -070025
26#include <utils/Log.h>
27#include <utils/Errors.h>
28#include <utils/ProcessCallStack.h>
29#include <utils/Printer.h>
30
Igor Murashkin81f2c3d2013-10-30 16:01:54 -070031#include <limits.h>
32
Igor Murashkinec79ef22013-10-24 17:09:15 -070033namespace android {
34
35enum {
36 // Max sizes for various dynamically generated strings
37 MAX_TIME_STRING = 64,
38 MAX_PROC_PATH = 1024,
39
40 // Dump related prettiness constants
41 IGNORE_DEPTH_CURRENT_THREAD = 2,
42};
43
44static const char* CALL_STACK_PREFIX = " ";
45static const char* PATH_THREAD_NAME = "/proc/self/task/%d/comm";
46static const char* PATH_SELF_TASK = "/proc/self/task";
47
48static void dumpProcessHeader(Printer& printer, pid_t pid, const char* timeStr) {
49 if (timeStr == NULL) {
50 ALOGW("%s: timeStr was NULL", __FUNCTION__);
51 return;
52 }
53
54 char path[PATH_MAX];
55 char procNameBuf[MAX_PROC_PATH];
56 char* procName = NULL;
57 FILE* fp;
58
59 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
60 if ((fp = fopen(path, "r"))) {
61 procName = fgets(procNameBuf, sizeof(procNameBuf), fp);
62 fclose(fp);
63 }
64
65 if (!procName) {
66 procName = const_cast<char*>("<unknown>");
67 }
68
69 printer.printLine();
70 printer.printLine();
71 printer.printFormatLine("----- pid %d at %s -----", pid, timeStr);
72 printer.printFormatLine("Cmd line: %s", procName);
73}
74
75static void dumpProcessFooter(Printer& printer, pid_t pid) {
76 printer.printLine();
77 printer.printFormatLine("----- end %d -----", pid);
78 printer.printLine();
79}
80
81static String8 getThreadName(pid_t tid) {
82 char path[PATH_MAX];
83 char* procName = NULL;
84 char procNameBuf[MAX_PROC_PATH];
85 FILE* fp;
86
87 snprintf(path, sizeof(path), PATH_THREAD_NAME, tid);
88 if ((fp = fopen(path, "r"))) {
89 procName = fgets(procNameBuf, sizeof(procNameBuf), fp);
90 fclose(fp);
91 } else {
92 ALOGE("%s: Failed to open %s", __FUNCTION__, path);
93 }
94
Igor Murashkin63fbdb62014-08-19 11:48:52 -070095 if (procName == NULL) {
96 // Reading /proc/self/task/%d/comm failed due to a race
97 return String8::format("[err-unknown-tid-%d]", tid);
98 }
99
Igor Murashkinec79ef22013-10-24 17:09:15 -0700100 // Strip ending newline
101 strtok(procName, "\n");
102
103 return String8(procName);
104}
105
106static String8 getTimeString(struct tm tm) {
107 char timestr[MAX_TIME_STRING];
108 // i.e. '2013-10-22 14:42:05'
109 strftime(timestr, sizeof(timestr), "%F %T", &tm);
110
111 return String8(timestr);
112}
113
114/*
115 * Implementation of ProcessCallStack
116 */
117ProcessCallStack::ProcessCallStack() {
118}
119
120ProcessCallStack::ProcessCallStack(const ProcessCallStack& rhs) :
121 mThreadMap(rhs.mThreadMap),
122 mTimeUpdated(rhs.mTimeUpdated) {
123}
124
125ProcessCallStack::~ProcessCallStack() {
126}
127
128void ProcessCallStack::clear() {
129 mThreadMap.clear();
130 mTimeUpdated = tm();
131}
132
Christopher Ferris9b0e0742013-10-31 16:25:04 -0700133void ProcessCallStack::update() {
James Hawkins588a2ca2016-02-18 14:52:46 -0800134 std::unique_ptr<DIR, decltype(&closedir)> dp(opendir(PATH_SELF_TASK), closedir);
Igor Murashkinec79ef22013-10-24 17:09:15 -0700135 if (dp == NULL) {
Elliott Hughes6ed68cc2015-06-30 08:22:24 -0700136 ALOGE("%s: Failed to update the process's call stacks: %s",
137 __FUNCTION__, strerror(errno));
Igor Murashkinec79ef22013-10-24 17:09:15 -0700138 return;
139 }
140
141 pid_t selfPid = getpid();
142
143 clear();
144
145 // Get current time.
146 {
147 time_t t = time(NULL);
148 struct tm tm;
149 localtime_r(&t, &tm);
150
151 mTimeUpdated = tm;
152 }
153
154 /*
155 * Each tid is a directory inside of /proc/self/task
156 * - Read every file in directory => get every tid
157 */
Elliott Hughes9f206932016-09-28 13:29:54 -0700158 dirent* ep;
159 while ((ep = readdir(dp.get())) != NULL) {
Igor Murashkinec79ef22013-10-24 17:09:15 -0700160 pid_t tid = -1;
161 sscanf(ep->d_name, "%d", &tid);
162
163 if (tid < 0) {
164 // Ignore '.' and '..'
165 ALOGV("%s: Failed to read tid from %s/%s",
166 __FUNCTION__, PATH_SELF_TASK, ep->d_name);
167 continue;
168 }
169
170 ssize_t idx = mThreadMap.add(tid, ThreadInfo());
171 if (idx < 0) { // returns negative error value on error
Elliott Hughes6ed68cc2015-06-30 08:22:24 -0700172 ALOGE("%s: Failed to add new ThreadInfo: %s",
173 __FUNCTION__, strerror(-idx));
Igor Murashkinec79ef22013-10-24 17:09:15 -0700174 continue;
175 }
176
177 ThreadInfo& threadInfo = mThreadMap.editValueAt(static_cast<size_t>(idx));
178
179 /*
180 * Ignore CallStack::update and ProcessCallStack::update for current thread
181 * - Every other thread doesn't need this since we call update off-thread
182 */
183 int ignoreDepth = (selfPid == tid) ? IGNORE_DEPTH_CURRENT_THREAD : 0;
184
185 // Update thread's call stacks
Christopher Ferris9b0e0742013-10-31 16:25:04 -0700186 threadInfo.callStack.update(ignoreDepth, tid);
Igor Murashkinec79ef22013-10-24 17:09:15 -0700187
188 // Read/save thread name
189 threadInfo.threadName = getThreadName(tid);
190
191 ALOGV("%s: Got call stack for tid %d (size %zu)",
Christopher Ferris9b0e0742013-10-31 16:25:04 -0700192 __FUNCTION__, tid, threadInfo.callStack.size());
Igor Murashkinec79ef22013-10-24 17:09:15 -0700193 }
Igor Murashkinec79ef22013-10-24 17:09:15 -0700194}
195
196void ProcessCallStack::log(const char* logtag, android_LogPriority priority,
197 const char* prefix) const {
198 LogPrinter printer(logtag, priority, prefix, /*ignoreBlankLines*/false);
199 print(printer);
200}
201
202void ProcessCallStack::print(Printer& printer) const {
203 /*
204 * Print the header/footer with the regular printer.
205 * Print the callstack with an additional two spaces as the prefix for legibility.
206 */
207 PrefixPrinter csPrinter(printer, CALL_STACK_PREFIX);
208 printInternal(printer, csPrinter);
209}
210
211void ProcessCallStack::printInternal(Printer& printer, Printer& csPrinter) const {
212 dumpProcessHeader(printer, getpid(),
213 getTimeString(mTimeUpdated).string());
214
215 for (size_t i = 0; i < mThreadMap.size(); ++i) {
216 pid_t tid = mThreadMap.keyAt(i);
217 const ThreadInfo& threadInfo = mThreadMap.valueAt(i);
Igor Murashkinec79ef22013-10-24 17:09:15 -0700218 const String8& threadName = threadInfo.threadName;
219
220 printer.printLine("");
221 printer.printFormatLine("\"%s\" sysTid=%d", threadName.string(), tid);
222
Christopher Ferris9b0e0742013-10-31 16:25:04 -0700223 threadInfo.callStack.print(csPrinter);
Igor Murashkinec79ef22013-10-24 17:09:15 -0700224 }
225
226 dumpProcessFooter(printer, getpid());
227}
228
229void ProcessCallStack::dump(int fd, int indent, const char* prefix) const {
230
231 if (indent < 0) {
232 ALOGW("%s: Bad indent (%d)", __FUNCTION__, indent);
233 return;
234 }
235
236 FdPrinter printer(fd, static_cast<unsigned int>(indent), prefix);
237 print(printer);
238}
239
240String8 ProcessCallStack::toString(const char* prefix) const {
241
242 String8 dest;
243 String8Printer printer(&dest, prefix);
244 print(printer);
245
246 return dest;
247}
248
249size_t ProcessCallStack::size() const {
250 return mThreadMap.size();
251}
252
253}; //namespace android