blob: 4e87a9875a2c49074cb59f98d6f83f119e39befd [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() {
Igor Murashkinec79ef22013-10-24 17:09:15 -0700134 struct dirent *ep;
135 struct dirent entry;
136
James Hawkins588a2ca2016-02-18 14:52:46 -0800137 std::unique_ptr<DIR, decltype(&closedir)> dp(opendir(PATH_SELF_TASK), closedir);
Igor Murashkinec79ef22013-10-24 17:09:15 -0700138 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;
James Hawkins588a2ca2016-02-18 14:52:46 -0800162 while ((code = readdir_r(dp.get(), &entry, &ep)) == 0 && ep != NULL) {
Igor Murashkinec79ef22013-10-24 17:09:15 -0700163 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
203void ProcessCallStack::log(const char* logtag, android_LogPriority priority,
204 const char* prefix) const {
205 LogPrinter printer(logtag, priority, prefix, /*ignoreBlankLines*/false);
206 print(printer);
207}
208
209void ProcessCallStack::print(Printer& printer) const {
210 /*
211 * Print the header/footer with the regular printer.
212 * Print the callstack with an additional two spaces as the prefix for legibility.
213 */
214 PrefixPrinter csPrinter(printer, CALL_STACK_PREFIX);
215 printInternal(printer, csPrinter);
216}
217
218void ProcessCallStack::printInternal(Printer& printer, Printer& csPrinter) const {
219 dumpProcessHeader(printer, getpid(),
220 getTimeString(mTimeUpdated).string());
221
222 for (size_t i = 0; i < mThreadMap.size(); ++i) {
223 pid_t tid = mThreadMap.keyAt(i);
224 const ThreadInfo& threadInfo = mThreadMap.valueAt(i);
Igor Murashkinec79ef22013-10-24 17:09:15 -0700225 const String8& threadName = threadInfo.threadName;
226
227 printer.printLine("");
228 printer.printFormatLine("\"%s\" sysTid=%d", threadName.string(), tid);
229
Christopher Ferris9b0e0742013-10-31 16:25:04 -0700230 threadInfo.callStack.print(csPrinter);
Igor Murashkinec79ef22013-10-24 17:09:15 -0700231 }
232
233 dumpProcessFooter(printer, getpid());
234}
235
236void ProcessCallStack::dump(int fd, int indent, const char* prefix) const {
237
238 if (indent < 0) {
239 ALOGW("%s: Bad indent (%d)", __FUNCTION__, indent);
240 return;
241 }
242
243 FdPrinter printer(fd, static_cast<unsigned int>(indent), prefix);
244 print(printer);
245}
246
247String8 ProcessCallStack::toString(const char* prefix) const {
248
249 String8 dest;
250 String8Printer printer(&dest, prefix);
251 print(printer);
252
253 return dest;
254}
255
256size_t ProcessCallStack::size() const {
257 return mThreadMap.size();
258}
259
260}; //namespace android