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