blob: cdb586d98b266d70886fc4b7292e88e972895d1b [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.
149 {
150 time_t t = time(NULL);
151 struct tm tm;
152 localtime_r(&t, &tm);
153
154 mTimeUpdated = tm;
155 }
156
157 /*
158 * Each tid is a directory inside of /proc/self/task
159 * - Read every file in directory => get every tid
160 */
161 int code;
162 while ((code = readdir_r(dp, &entry, &ep)) == 0 && ep != NULL) {
163 pid_t tid = -1;
164 sscanf(ep->d_name, "%d", &tid);
165
166 if (tid < 0) {
167 // Ignore '.' and '..'
168 ALOGV("%s: Failed to read tid from %s/%s",
169 __FUNCTION__, PATH_SELF_TASK, ep->d_name);
170 continue;
171 }
172
173 ssize_t idx = mThreadMap.add(tid, ThreadInfo());
174 if (idx < 0) { // returns negative error value on error
Elliott Hughes6ed68cc2015-06-30 08:22:24 -0700175 ALOGE("%s: Failed to add new ThreadInfo: %s",
176 __FUNCTION__, strerror(-idx));
Igor Murashkinec79ef22013-10-24 17:09:15 -0700177 continue;
178 }
179
180 ThreadInfo& threadInfo = mThreadMap.editValueAt(static_cast<size_t>(idx));
181
182 /*
183 * Ignore CallStack::update and ProcessCallStack::update for current thread
184 * - Every other thread doesn't need this since we call update off-thread
185 */
186 int ignoreDepth = (selfPid == tid) ? IGNORE_DEPTH_CURRENT_THREAD : 0;
187
188 // Update thread's call stacks
Christopher Ferris9b0e0742013-10-31 16:25:04 -0700189 threadInfo.callStack.update(ignoreDepth, tid);
Igor Murashkinec79ef22013-10-24 17:09:15 -0700190
191 // Read/save thread name
192 threadInfo.threadName = getThreadName(tid);
193
194 ALOGV("%s: Got call stack for tid %d (size %zu)",
Christopher Ferris9b0e0742013-10-31 16:25:04 -0700195 __FUNCTION__, tid, threadInfo.callStack.size());
Igor Murashkinec79ef22013-10-24 17:09:15 -0700196 }
197 if (code != 0) { // returns positive error value on error
Elliott Hughes6ed68cc2015-06-30 08:22:24 -0700198 ALOGE("%s: Failed to readdir from %s: %s",
199 __FUNCTION__, PATH_SELF_TASK, strerror(code));
Igor Murashkinec79ef22013-10-24 17:09:15 -0700200 }
Igor Murashkinec79ef22013-10-24 17:09:15 -0700201
202 closedir(dp);
203}
204
205void ProcessCallStack::log(const char* logtag, android_LogPriority priority,
206 const char* prefix) const {
207 LogPrinter printer(logtag, priority, prefix, /*ignoreBlankLines*/false);
208 print(printer);
209}
210
211void ProcessCallStack::print(Printer& printer) const {
212 /*
213 * Print the header/footer with the regular printer.
214 * Print the callstack with an additional two spaces as the prefix for legibility.
215 */
216 PrefixPrinter csPrinter(printer, CALL_STACK_PREFIX);
217 printInternal(printer, csPrinter);
218}
219
220void ProcessCallStack::printInternal(Printer& printer, Printer& csPrinter) const {
221 dumpProcessHeader(printer, getpid(),
222 getTimeString(mTimeUpdated).string());
223
224 for (size_t i = 0; i < mThreadMap.size(); ++i) {
225 pid_t tid = mThreadMap.keyAt(i);
226 const ThreadInfo& threadInfo = mThreadMap.valueAt(i);
Igor Murashkinec79ef22013-10-24 17:09:15 -0700227 const String8& threadName = threadInfo.threadName;
228
229 printer.printLine("");
230 printer.printFormatLine("\"%s\" sysTid=%d", threadName.string(), tid);
231
Christopher Ferris9b0e0742013-10-31 16:25:04 -0700232 threadInfo.callStack.print(csPrinter);
Igor Murashkinec79ef22013-10-24 17:09:15 -0700233 }
234
235 dumpProcessFooter(printer, getpid());
236}
237
238void ProcessCallStack::dump(int fd, int indent, const char* prefix) const {
239
240 if (indent < 0) {
241 ALOGW("%s: Bad indent (%d)", __FUNCTION__, indent);
242 return;
243 }
244
245 FdPrinter printer(fd, static_cast<unsigned int>(indent), prefix);
246 print(printer);
247}
248
249String8 ProcessCallStack::toString(const char* prefix) const {
250
251 String8 dest;
252 String8Printer printer(&dest, prefix);
253 print(printer);
254
255 return dest;
256}
257
258size_t ProcessCallStack::size() const {
259 return mThreadMap.size();
260}
261
262}; //namespace android