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