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