| 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 <utils/ProcessCallStack.h> | 
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 21 |  | 
| Mathias Agopian | 22dbf39 | 2017-02-28 15:06:51 -0800 | [diff] [blame] | 22 | #include <dirent.h> | 
| Steven Moreland | d21cfab | 2017-03-10 08:58:36 -0800 | [diff] [blame] | 23 | #include <memory> | 
| Mathias Agopian | 22dbf39 | 2017-02-28 15:06:51 -0800 | [diff] [blame] | 24 |  | 
|  | 25 | #include <utils/Printer.h> | 
| Igor Murashkin | 81f2c3d | 2013-10-30 16:01:54 -0700 | [diff] [blame] | 26 |  | 
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 27 | namespace android { | 
|  | 28 |  | 
|  | 29 | enum { | 
|  | 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 |  | 
|  | 38 | static const char* CALL_STACK_PREFIX = "  "; | 
|  | 39 | static const char* PATH_THREAD_NAME = "/proc/self/task/%d/comm"; | 
|  | 40 | static const char* PATH_SELF_TASK = "/proc/self/task"; | 
|  | 41 |  | 
|  | 42 | static 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 |  | 
|  | 69 | static void dumpProcessFooter(Printer& printer, pid_t pid) { | 
|  | 70 | printer.printLine(); | 
|  | 71 | printer.printFormatLine("----- end %d -----", pid); | 
|  | 72 | printer.printLine(); | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | static 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 Murashkin | 63fbdb6 | 2014-08-19 11:48:52 -0700 | [diff] [blame] | 89 | 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 Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 94 | // Strip ending newline | 
|  | 95 | strtok(procName, "\n"); | 
|  | 96 |  | 
|  | 97 | return String8(procName); | 
|  | 98 | } | 
|  | 99 |  | 
|  | 100 | static 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 | */ | 
|  | 111 | ProcessCallStack::ProcessCallStack() { | 
|  | 112 | } | 
|  | 113 |  | 
|  | 114 | ProcessCallStack::ProcessCallStack(const ProcessCallStack& rhs) : | 
|  | 115 | mThreadMap(rhs.mThreadMap), | 
|  | 116 | mTimeUpdated(rhs.mTimeUpdated) { | 
|  | 117 | } | 
|  | 118 |  | 
|  | 119 | ProcessCallStack::~ProcessCallStack() { | 
|  | 120 | } | 
|  | 121 |  | 
|  | 122 | void ProcessCallStack::clear() { | 
|  | 123 | mThreadMap.clear(); | 
|  | 124 | mTimeUpdated = tm(); | 
|  | 125 | } | 
|  | 126 |  | 
| Christopher Ferris | 9b0e074 | 2013-10-31 16:25:04 -0700 | [diff] [blame] | 127 | void ProcessCallStack::update() { | 
| James Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 128 | std::unique_ptr<DIR, decltype(&closedir)> dp(opendir(PATH_SELF_TASK), closedir); | 
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 129 | if (dp == NULL) { | 
| Elliott Hughes | 6ed68cc | 2015-06-30 08:22:24 -0700 | [diff] [blame] | 130 | ALOGE("%s: Failed to update the process's call stacks: %s", | 
|  | 131 | __FUNCTION__, strerror(errno)); | 
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 132 | 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 Hughes | 9f20693 | 2016-09-28 13:29:54 -0700 | [diff] [blame] | 152 | dirent* ep; | 
|  | 153 | while ((ep = readdir(dp.get())) != NULL) { | 
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 154 | 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 Hughes | 6ed68cc | 2015-06-30 08:22:24 -0700 | [diff] [blame] | 166 | ALOGE("%s: Failed to add new ThreadInfo: %s", | 
|  | 167 | __FUNCTION__, strerror(-idx)); | 
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 168 | 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 Ferris | 9b0e074 | 2013-10-31 16:25:04 -0700 | [diff] [blame] | 180 | threadInfo.callStack.update(ignoreDepth, tid); | 
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 181 |  | 
|  | 182 | // Read/save thread name | 
|  | 183 | threadInfo.threadName = getThreadName(tid); | 
|  | 184 |  | 
|  | 185 | ALOGV("%s: Got call stack for tid %d (size %zu)", | 
| Christopher Ferris | 9b0e074 | 2013-10-31 16:25:04 -0700 | [diff] [blame] | 186 | __FUNCTION__, tid, threadInfo.callStack.size()); | 
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 187 | } | 
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 188 | } | 
|  | 189 |  | 
|  | 190 | void 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 |  | 
|  | 196 | void 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 |  | 
|  | 205 | void 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 Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 212 | const String8& threadName = threadInfo.threadName; | 
|  | 213 |  | 
|  | 214 | printer.printLine(""); | 
|  | 215 | printer.printFormatLine("\"%s\" sysTid=%d", threadName.string(), tid); | 
|  | 216 |  | 
| Christopher Ferris | 9b0e074 | 2013-10-31 16:25:04 -0700 | [diff] [blame] | 217 | threadInfo.callStack.print(csPrinter); | 
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 218 | } | 
|  | 219 |  | 
|  | 220 | dumpProcessFooter(printer, getpid()); | 
|  | 221 | } | 
|  | 222 |  | 
|  | 223 | void 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 |  | 
|  | 234 | String8 ProcessCallStack::toString(const char* prefix) const { | 
|  | 235 |  | 
|  | 236 | String8 dest; | 
|  | 237 | String8Printer printer(&dest, prefix); | 
|  | 238 | print(printer); | 
|  | 239 |  | 
|  | 240 | return dest; | 
|  | 241 | } | 
|  | 242 |  | 
|  | 243 | size_t ProcessCallStack::size() const { | 
|  | 244 | return mThreadMap.size(); | 
|  | 245 | } | 
|  | 246 |  | 
|  | 247 | }; //namespace android |