blob: 011c30291989f607aac0811602438f251d204137 [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>
Igor Murashkinec79ef22013-10-24 17:09:15 -070024
25#include <utils/Log.h>
26#include <utils/Errors.h>
27#include <utils/ProcessCallStack.h>
28#include <utils/Printer.h>
29
Igor Murashkin81f2c3d2013-10-30 16:01:54 -070030#include <limits.h>
31
Igor Murashkinec79ef22013-10-24 17:09:15 -070032namespace android {
33
34enum {
35 // Max sizes for various dynamically generated strings
36 MAX_TIME_STRING = 64,
37 MAX_PROC_PATH = 1024,
38
39 // Dump related prettiness constants
40 IGNORE_DEPTH_CURRENT_THREAD = 2,
41};
42
43static const char* CALL_STACK_PREFIX = " ";
44static const char* PATH_THREAD_NAME = "/proc/self/task/%d/comm";
45static const char* PATH_SELF_TASK = "/proc/self/task";
46
47static void dumpProcessHeader(Printer& printer, pid_t pid, const char* timeStr) {
48 if (timeStr == NULL) {
49 ALOGW("%s: timeStr was NULL", __FUNCTION__);
50 return;
51 }
52
53 char path[PATH_MAX];
54 char procNameBuf[MAX_PROC_PATH];
55 char* procName = NULL;
56 FILE* fp;
57
58 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
59 if ((fp = fopen(path, "r"))) {
60 procName = fgets(procNameBuf, sizeof(procNameBuf), fp);
61 fclose(fp);
62 }
63
64 if (!procName) {
65 procName = const_cast<char*>("<unknown>");
66 }
67
68 printer.printLine();
69 printer.printLine();
70 printer.printFormatLine("----- pid %d at %s -----", pid, timeStr);
71 printer.printFormatLine("Cmd line: %s", procName);
72}
73
74static void dumpProcessFooter(Printer& printer, pid_t pid) {
75 printer.printLine();
76 printer.printFormatLine("----- end %d -----", pid);
77 printer.printLine();
78}
79
80static String8 getThreadName(pid_t tid) {
81 char path[PATH_MAX];
82 char* procName = NULL;
83 char procNameBuf[MAX_PROC_PATH];
84 FILE* fp;
85
86 snprintf(path, sizeof(path), PATH_THREAD_NAME, tid);
87 if ((fp = fopen(path, "r"))) {
88 procName = fgets(procNameBuf, sizeof(procNameBuf), fp);
89 fclose(fp);
90 } else {
91 ALOGE("%s: Failed to open %s", __FUNCTION__, path);
92 }
93
Igor Murashkin63fbdb62014-08-19 11:48:52 -070094 if (procName == NULL) {
95 // Reading /proc/self/task/%d/comm failed due to a race
96 return String8::format("[err-unknown-tid-%d]", tid);
97 }
98
Igor Murashkinec79ef22013-10-24 17:09:15 -070099 // Strip ending newline
100 strtok(procName, "\n");
101
102 return String8(procName);
103}
104
105static String8 getTimeString(struct tm tm) {
106 char timestr[MAX_TIME_STRING];
107 // i.e. '2013-10-22 14:42:05'
108 strftime(timestr, sizeof(timestr), "%F %T", &tm);
109
110 return String8(timestr);
111}
112
113/*
114 * Implementation of ProcessCallStack
115 */
116ProcessCallStack::ProcessCallStack() {
117}
118
119ProcessCallStack::ProcessCallStack(const ProcessCallStack& rhs) :
120 mThreadMap(rhs.mThreadMap),
121 mTimeUpdated(rhs.mTimeUpdated) {
122}
123
124ProcessCallStack::~ProcessCallStack() {
125}
126
127void ProcessCallStack::clear() {
128 mThreadMap.clear();
129 mTimeUpdated = tm();
130}
131
Christopher Ferris9b0e0742013-10-31 16:25:04 -0700132void ProcessCallStack::update() {
Igor Murashkinec79ef22013-10-24 17:09:15 -0700133 DIR *dp;
134 struct dirent *ep;
135 struct dirent entry;
136
137 dp = opendir(PATH_SELF_TASK);
138 if (dp == NULL) {
Elliott Hughes6ed68cc2015-06-30 08:22:24 -0700139 ALOGE("%s: Failed to update the process's call stacks: %s",
140 __FUNCTION__, strerror(errno));
Igor Murashkinec79ef22013-10-24 17:09:15 -0700141 return;
142 }
143
144 pid_t selfPid = getpid();
145
146 clear();
147
148 // Get current time.
Igor Murashkine65b7ea2013-10-30 18:09:52 -0700149#ifndef USE_MINGW
Igor Murashkinec79ef22013-10-24 17:09:15 -0700150 {
151 time_t t = time(NULL);
152 struct tm tm;
153 localtime_r(&t, &tm);
154
155 mTimeUpdated = tm;
156 }
157
158 /*
159 * Each tid is a directory inside of /proc/self/task
160 * - Read every file in directory => get every tid
161 */
162 int code;
163 while ((code = readdir_r(dp, &entry, &ep)) == 0 && ep != NULL) {
164 pid_t tid = -1;
165 sscanf(ep->d_name, "%d", &tid);
166
167 if (tid < 0) {
168 // Ignore '.' and '..'
169 ALOGV("%s: Failed to read tid from %s/%s",
170 __FUNCTION__, PATH_SELF_TASK, ep->d_name);
171 continue;
172 }
173
174 ssize_t idx = mThreadMap.add(tid, ThreadInfo());
175 if (idx < 0) { // returns negative error value on error
Elliott Hughes6ed68cc2015-06-30 08:22:24 -0700176 ALOGE("%s: Failed to add new ThreadInfo: %s",
177 __FUNCTION__, strerror(-idx));
Igor Murashkinec79ef22013-10-24 17:09:15 -0700178 continue;
179 }
180
181 ThreadInfo& threadInfo = mThreadMap.editValueAt(static_cast<size_t>(idx));
182
183 /*
184 * Ignore CallStack::update and ProcessCallStack::update for current thread
185 * - Every other thread doesn't need this since we call update off-thread
186 */
187 int ignoreDepth = (selfPid == tid) ? IGNORE_DEPTH_CURRENT_THREAD : 0;
188
189 // Update thread's call stacks
Christopher Ferris9b0e0742013-10-31 16:25:04 -0700190 threadInfo.callStack.update(ignoreDepth, tid);
Igor Murashkinec79ef22013-10-24 17:09:15 -0700191
192 // Read/save thread name
193 threadInfo.threadName = getThreadName(tid);
194
195 ALOGV("%s: Got call stack for tid %d (size %zu)",
Christopher Ferris9b0e0742013-10-31 16:25:04 -0700196 __FUNCTION__, tid, threadInfo.callStack.size());
Igor Murashkinec79ef22013-10-24 17:09:15 -0700197 }
198 if (code != 0) { // returns positive error value on error
Elliott Hughes6ed68cc2015-06-30 08:22:24 -0700199 ALOGE("%s: Failed to readdir from %s: %s",
200 __FUNCTION__, PATH_SELF_TASK, strerror(code));
Igor Murashkinec79ef22013-10-24 17:09:15 -0700201 }
Igor Murashkine65b7ea2013-10-30 18:09:52 -0700202#endif
Igor Murashkinec79ef22013-10-24 17:09:15 -0700203
204 closedir(dp);
205}
206
207void ProcessCallStack::log(const char* logtag, android_LogPriority priority,
208 const char* prefix) const {
209 LogPrinter printer(logtag, priority, prefix, /*ignoreBlankLines*/false);
210 print(printer);
211}
212
213void ProcessCallStack::print(Printer& printer) const {
214 /*
215 * Print the header/footer with the regular printer.
216 * Print the callstack with an additional two spaces as the prefix for legibility.
217 */
218 PrefixPrinter csPrinter(printer, CALL_STACK_PREFIX);
219 printInternal(printer, csPrinter);
220}
221
222void ProcessCallStack::printInternal(Printer& printer, Printer& csPrinter) const {
223 dumpProcessHeader(printer, getpid(),
224 getTimeString(mTimeUpdated).string());
225
226 for (size_t i = 0; i < mThreadMap.size(); ++i) {
227 pid_t tid = mThreadMap.keyAt(i);
228 const ThreadInfo& threadInfo = mThreadMap.valueAt(i);
Igor Murashkinec79ef22013-10-24 17:09:15 -0700229 const String8& threadName = threadInfo.threadName;
230
231 printer.printLine("");
232 printer.printFormatLine("\"%s\" sysTid=%d", threadName.string(), tid);
233
Christopher Ferris9b0e0742013-10-31 16:25:04 -0700234 threadInfo.callStack.print(csPrinter);
Igor Murashkinec79ef22013-10-24 17:09:15 -0700235 }
236
237 dumpProcessFooter(printer, getpid());
238}
239
240void ProcessCallStack::dump(int fd, int indent, const char* prefix) const {
241
242 if (indent < 0) {
243 ALOGW("%s: Bad indent (%d)", __FUNCTION__, indent);
244 return;
245 }
246
247 FdPrinter printer(fd, static_cast<unsigned int>(indent), prefix);
248 print(printer);
249}
250
251String8 ProcessCallStack::toString(const char* prefix) const {
252
253 String8 dest;
254 String8Printer printer(&dest, prefix);
255 print(printer);
256
257 return dest;
258}
259
260size_t ProcessCallStack::size() const {
261 return mThreadMap.size();
262}
263
264}; //namespace android