Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 1 | /* |
| 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 Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 20 | #include <dirent.h> |
Elliott Hughes | 6ed68cc | 2015-06-30 08:22:24 -0700 | [diff] [blame] | 21 | #include <errno.h> |
| 22 | #include <stdio.h> |
| 23 | #include <string.h> |
James Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 24 | #include <memory> |
Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 25 | |
| 26 | #include <utils/Log.h> |
| 27 | #include <utils/Errors.h> |
| 28 | #include <utils/ProcessCallStack.h> |
| 29 | #include <utils/Printer.h> |
| 30 | |
Igor Murashkin | 81f2c3d | 2013-10-30 16:01:54 -0700 | [diff] [blame] | 31 | #include <limits.h> |
| 32 | |
Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 33 | namespace android { |
| 34 | |
| 35 | enum { |
| 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 | |
| 44 | static const char* CALL_STACK_PREFIX = " "; |
| 45 | static const char* PATH_THREAD_NAME = "/proc/self/task/%d/comm"; |
| 46 | static const char* PATH_SELF_TASK = "/proc/self/task"; |
| 47 | |
| 48 | static 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 | |
| 75 | static void dumpProcessFooter(Printer& printer, pid_t pid) { |
| 76 | printer.printLine(); |
| 77 | printer.printFormatLine("----- end %d -----", pid); |
| 78 | printer.printLine(); |
| 79 | } |
| 80 | |
| 81 | static 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 Murashkin | 63fbdb6 | 2014-08-19 11:48:52 -0700 | [diff] [blame] | 95 | 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 Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 100 | // Strip ending newline |
| 101 | strtok(procName, "\n"); |
| 102 | |
| 103 | return String8(procName); |
| 104 | } |
| 105 | |
| 106 | static 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 | */ |
| 117 | ProcessCallStack::ProcessCallStack() { |
| 118 | } |
| 119 | |
| 120 | ProcessCallStack::ProcessCallStack(const ProcessCallStack& rhs) : |
| 121 | mThreadMap(rhs.mThreadMap), |
| 122 | mTimeUpdated(rhs.mTimeUpdated) { |
| 123 | } |
| 124 | |
| 125 | ProcessCallStack::~ProcessCallStack() { |
| 126 | } |
| 127 | |
| 128 | void ProcessCallStack::clear() { |
| 129 | mThreadMap.clear(); |
| 130 | mTimeUpdated = tm(); |
| 131 | } |
| 132 | |
Christopher Ferris | 9b0e074 | 2013-10-31 16:25:04 -0700 | [diff] [blame] | 133 | void ProcessCallStack::update() { |
Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 134 | struct dirent *ep; |
| 135 | struct dirent entry; |
| 136 | |
James Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 137 | std::unique_ptr<DIR, decltype(&closedir)> dp(opendir(PATH_SELF_TASK), closedir); |
Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 138 | if (dp == NULL) { |
Elliott Hughes | 6ed68cc | 2015-06-30 08:22:24 -0700 | [diff] [blame] | 139 | ALOGE("%s: Failed to update the process's call stacks: %s", |
| 140 | __FUNCTION__, strerror(errno)); |
Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 141 | 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 Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 162 | while ((code = readdir_r(dp.get(), &entry, &ep)) == 0 && ep != NULL) { |
Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 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 Hughes | 6ed68cc | 2015-06-30 08:22:24 -0700 | [diff] [blame] | 175 | ALOGE("%s: Failed to add new ThreadInfo: %s", |
| 176 | __FUNCTION__, strerror(-idx)); |
Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 177 | 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 Ferris | 9b0e074 | 2013-10-31 16:25:04 -0700 | [diff] [blame] | 189 | threadInfo.callStack.update(ignoreDepth, tid); |
Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 190 | |
| 191 | // Read/save thread name |
| 192 | threadInfo.threadName = getThreadName(tid); |
| 193 | |
| 194 | ALOGV("%s: Got call stack for tid %d (size %zu)", |
Christopher Ferris | 9b0e074 | 2013-10-31 16:25:04 -0700 | [diff] [blame] | 195 | __FUNCTION__, tid, threadInfo.callStack.size()); |
Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 196 | } |
| 197 | if (code != 0) { // returns positive error value on error |
Elliott Hughes | 6ed68cc | 2015-06-30 08:22:24 -0700 | [diff] [blame] | 198 | ALOGE("%s: Failed to readdir from %s: %s", |
| 199 | __FUNCTION__, PATH_SELF_TASK, strerror(code)); |
Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 200 | } |
Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | void 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 | |
| 209 | void 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 | |
| 218 | void 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 Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 225 | const String8& threadName = threadInfo.threadName; |
| 226 | |
| 227 | printer.printLine(""); |
| 228 | printer.printFormatLine("\"%s\" sysTid=%d", threadName.string(), tid); |
| 229 | |
Christopher Ferris | 9b0e074 | 2013-10-31 16:25:04 -0700 | [diff] [blame] | 230 | threadInfo.callStack.print(csPrinter); |
Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | dumpProcessFooter(printer, getpid()); |
| 234 | } |
| 235 | |
| 236 | void 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 | |
| 247 | String8 ProcessCallStack::toString(const char* prefix) const { |
| 248 | |
| 249 | String8 dest; |
| 250 | String8Printer printer(&dest, prefix); |
| 251 | print(printer); |
| 252 | |
| 253 | return dest; |
| 254 | } |
| 255 | |
| 256 | size_t ProcessCallStack::size() const { |
| 257 | return mThreadMap.size(); |
| 258 | } |
| 259 | |
| 260 | }; //namespace android |