| 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> | 
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 24 |  | 
|  | 25 | #include <utils/Log.h> | 
|  | 26 | #include <utils/Errors.h> | 
|  | 27 | #include <utils/ProcessCallStack.h> | 
|  | 28 | #include <utils/Printer.h> | 
|  | 29 |  | 
| Igor Murashkin | 81f2c3d | 2013-10-30 16:01:54 -0700 | [diff] [blame] | 30 | #include <limits.h> | 
|  | 31 |  | 
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 32 | namespace android { | 
|  | 33 |  | 
|  | 34 | enum { | 
|  | 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 |  | 
|  | 43 | static const char* CALL_STACK_PREFIX = "  "; | 
|  | 44 | static const char* PATH_THREAD_NAME = "/proc/self/task/%d/comm"; | 
|  | 45 | static const char* PATH_SELF_TASK = "/proc/self/task"; | 
|  | 46 |  | 
|  | 47 | static 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 |  | 
|  | 74 | static void dumpProcessFooter(Printer& printer, pid_t pid) { | 
|  | 75 | printer.printLine(); | 
|  | 76 | printer.printFormatLine("----- end %d -----", pid); | 
|  | 77 | printer.printLine(); | 
|  | 78 | } | 
|  | 79 |  | 
|  | 80 | static 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 Murashkin | 63fbdb6 | 2014-08-19 11:48:52 -0700 | [diff] [blame] | 94 | 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 Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 99 | // Strip ending newline | 
|  | 100 | strtok(procName, "\n"); | 
|  | 101 |  | 
|  | 102 | return String8(procName); | 
|  | 103 | } | 
|  | 104 |  | 
|  | 105 | static 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 | */ | 
|  | 116 | ProcessCallStack::ProcessCallStack() { | 
|  | 117 | } | 
|  | 118 |  | 
|  | 119 | ProcessCallStack::ProcessCallStack(const ProcessCallStack& rhs) : | 
|  | 120 | mThreadMap(rhs.mThreadMap), | 
|  | 121 | mTimeUpdated(rhs.mTimeUpdated) { | 
|  | 122 | } | 
|  | 123 |  | 
|  | 124 | ProcessCallStack::~ProcessCallStack() { | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | void ProcessCallStack::clear() { | 
|  | 128 | mThreadMap.clear(); | 
|  | 129 | mTimeUpdated = tm(); | 
|  | 130 | } | 
|  | 131 |  | 
| Christopher Ferris | 9b0e074 | 2013-10-31 16:25:04 -0700 | [diff] [blame] | 132 | void ProcessCallStack::update() { | 
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 133 | DIR *dp; | 
|  | 134 | struct dirent *ep; | 
|  | 135 | struct dirent entry; | 
|  | 136 |  | 
|  | 137 | dp = opendir(PATH_SELF_TASK); | 
|  | 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. | 
| Igor Murashkin | e65b7ea | 2013-10-30 18:09:52 -0700 | [diff] [blame] | 149 | #ifndef USE_MINGW | 
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 150 | { | 
|  | 151 | time_t t = time(NULL); | 
|  | 152 | struct tm tm; | 
|  | 153 | localtime_r(&t, &tm); | 
|  | 154 |  | 
|  | 155 | mTimeUpdated = tm; | 
|  | 156 | } | 
|  | 157 |  | 
|  | 158 | /* | 
|  | 159 | * Each tid is a directory inside of /proc/self/task | 
|  | 160 | * - Read every file in directory => get every tid | 
|  | 161 | */ | 
|  | 162 | int code; | 
|  | 163 | while ((code = readdir_r(dp, &entry, &ep)) == 0 && ep != NULL) { | 
|  | 164 | pid_t tid = -1; | 
|  | 165 | sscanf(ep->d_name, "%d", &tid); | 
|  | 166 |  | 
|  | 167 | if (tid < 0) { | 
|  | 168 | // Ignore '.' and '..' | 
|  | 169 | ALOGV("%s: Failed to read tid from %s/%s", | 
|  | 170 | __FUNCTION__, PATH_SELF_TASK, ep->d_name); | 
|  | 171 | continue; | 
|  | 172 | } | 
|  | 173 |  | 
|  | 174 | ssize_t idx = mThreadMap.add(tid, ThreadInfo()); | 
|  | 175 | if (idx < 0) { // returns negative error value on error | 
| Elliott Hughes | 6ed68cc | 2015-06-30 08:22:24 -0700 | [diff] [blame] | 176 | ALOGE("%s: Failed to add new ThreadInfo: %s", | 
|  | 177 | __FUNCTION__, strerror(-idx)); | 
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 178 | continue; | 
|  | 179 | } | 
|  | 180 |  | 
|  | 181 | ThreadInfo& threadInfo = mThreadMap.editValueAt(static_cast<size_t>(idx)); | 
|  | 182 |  | 
|  | 183 | /* | 
|  | 184 | * Ignore CallStack::update and ProcessCallStack::update for current thread | 
|  | 185 | * - Every other thread doesn't need this since we call update off-thread | 
|  | 186 | */ | 
|  | 187 | int ignoreDepth = (selfPid == tid) ? IGNORE_DEPTH_CURRENT_THREAD : 0; | 
|  | 188 |  | 
|  | 189 | // Update thread's call stacks | 
| Christopher Ferris | 9b0e074 | 2013-10-31 16:25:04 -0700 | [diff] [blame] | 190 | threadInfo.callStack.update(ignoreDepth, tid); | 
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 191 |  | 
|  | 192 | // Read/save thread name | 
|  | 193 | threadInfo.threadName = getThreadName(tid); | 
|  | 194 |  | 
|  | 195 | ALOGV("%s: Got call stack for tid %d (size %zu)", | 
| Christopher Ferris | 9b0e074 | 2013-10-31 16:25:04 -0700 | [diff] [blame] | 196 | __FUNCTION__, tid, threadInfo.callStack.size()); | 
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 197 | } | 
|  | 198 | if (code != 0) { // returns positive error value on error | 
| Elliott Hughes | 6ed68cc | 2015-06-30 08:22:24 -0700 | [diff] [blame] | 199 | ALOGE("%s: Failed to readdir from %s: %s", | 
|  | 200 | __FUNCTION__, PATH_SELF_TASK, strerror(code)); | 
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 201 | } | 
| Igor Murashkin | e65b7ea | 2013-10-30 18:09:52 -0700 | [diff] [blame] | 202 | #endif | 
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 203 |  | 
|  | 204 | closedir(dp); | 
|  | 205 | } | 
|  | 206 |  | 
|  | 207 | void ProcessCallStack::log(const char* logtag, android_LogPriority priority, | 
|  | 208 | const char* prefix) const { | 
|  | 209 | LogPrinter printer(logtag, priority, prefix, /*ignoreBlankLines*/false); | 
|  | 210 | print(printer); | 
|  | 211 | } | 
|  | 212 |  | 
|  | 213 | void ProcessCallStack::print(Printer& printer) const { | 
|  | 214 | /* | 
|  | 215 | * Print the header/footer with the regular printer. | 
|  | 216 | * Print the callstack with an additional two spaces as the prefix for legibility. | 
|  | 217 | */ | 
|  | 218 | PrefixPrinter csPrinter(printer, CALL_STACK_PREFIX); | 
|  | 219 | printInternal(printer, csPrinter); | 
|  | 220 | } | 
|  | 221 |  | 
|  | 222 | void ProcessCallStack::printInternal(Printer& printer, Printer& csPrinter) const { | 
|  | 223 | dumpProcessHeader(printer, getpid(), | 
|  | 224 | getTimeString(mTimeUpdated).string()); | 
|  | 225 |  | 
|  | 226 | for (size_t i = 0; i < mThreadMap.size(); ++i) { | 
|  | 227 | pid_t tid = mThreadMap.keyAt(i); | 
|  | 228 | const ThreadInfo& threadInfo = mThreadMap.valueAt(i); | 
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 229 | const String8& threadName = threadInfo.threadName; | 
|  | 230 |  | 
|  | 231 | printer.printLine(""); | 
|  | 232 | printer.printFormatLine("\"%s\" sysTid=%d", threadName.string(), tid); | 
|  | 233 |  | 
| Christopher Ferris | 9b0e074 | 2013-10-31 16:25:04 -0700 | [diff] [blame] | 234 | threadInfo.callStack.print(csPrinter); | 
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 235 | } | 
|  | 236 |  | 
|  | 237 | dumpProcessFooter(printer, getpid()); | 
|  | 238 | } | 
|  | 239 |  | 
|  | 240 | void ProcessCallStack::dump(int fd, int indent, const char* prefix) const { | 
|  | 241 |  | 
|  | 242 | if (indent < 0) { | 
|  | 243 | ALOGW("%s: Bad indent (%d)", __FUNCTION__, indent); | 
|  | 244 | return; | 
|  | 245 | } | 
|  | 246 |  | 
|  | 247 | FdPrinter printer(fd, static_cast<unsigned int>(indent), prefix); | 
|  | 248 | print(printer); | 
|  | 249 | } | 
|  | 250 |  | 
|  | 251 | String8 ProcessCallStack::toString(const char* prefix) const { | 
|  | 252 |  | 
|  | 253 | String8 dest; | 
|  | 254 | String8Printer printer(&dest, prefix); | 
|  | 255 | print(printer); | 
|  | 256 |  | 
|  | 257 | return dest; | 
|  | 258 | } | 
|  | 259 |  | 
|  | 260 | size_t ProcessCallStack::size() const { | 
|  | 261 | return mThreadMap.size(); | 
|  | 262 | } | 
|  | 263 |  | 
|  | 264 | }; //namespace android |