1ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin/* 2ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin * Copyright (C) 2013 The Android Open Source Project 3ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin * 4ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin * Licensed under the Apache License, Version 2.0 (the "License"); 5ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin * you may not use this file except in compliance with the License. 6ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin * You may obtain a copy of the License at 7ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin * 8ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin * http://www.apache.org/licenses/LICENSE-2.0 9ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin * 10ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin * Unless required by applicable law or agreed to in writing, software 11ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin * distributed under the License is distributed on an "AS IS" BASIS, 12ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin * See the License for the specific language governing permissions and 14ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin * limitations under the License. 15ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin */ 16ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 17ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin#define LOG_TAG "ProcessCallStack" 18ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin// #define LOG_NDEBUG 0 19ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 20ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin#include <string.h> 21ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin#include <stdio.h> 22ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin#include <dirent.h> 23ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 24ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin#include <utils/Log.h> 25ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin#include <utils/Errors.h> 26ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin#include <utils/ProcessCallStack.h> 27ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin#include <utils/Printer.h> 28ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 2981f2c3d2117cfe7307ee6efbd82567bd02950965Igor Murashkin#include <limits.h> 3081f2c3d2117cfe7307ee6efbd82567bd02950965Igor Murashkin 31ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkinnamespace android { 32ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 33ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkinenum { 34ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin // Max sizes for various dynamically generated strings 35ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin MAX_TIME_STRING = 64, 36ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin MAX_PROC_PATH = 1024, 37ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 38ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin // Dump related prettiness constants 39ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin IGNORE_DEPTH_CURRENT_THREAD = 2, 40ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin}; 41ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 42ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkinstatic const char* CALL_STACK_PREFIX = " "; 43ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkinstatic const char* PATH_THREAD_NAME = "/proc/self/task/%d/comm"; 44ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkinstatic const char* PATH_SELF_TASK = "/proc/self/task"; 45ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 46ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkinstatic void dumpProcessHeader(Printer& printer, pid_t pid, const char* timeStr) { 47ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin if (timeStr == NULL) { 48ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin ALOGW("%s: timeStr was NULL", __FUNCTION__); 49ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin return; 50ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin } 51ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 52ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin char path[PATH_MAX]; 53ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin char procNameBuf[MAX_PROC_PATH]; 54ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin char* procName = NULL; 55ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin FILE* fp; 56ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 57ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin snprintf(path, sizeof(path), "/proc/%d/cmdline", pid); 58ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin if ((fp = fopen(path, "r"))) { 59ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin procName = fgets(procNameBuf, sizeof(procNameBuf), fp); 60ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin fclose(fp); 61ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin } 62ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 63ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin if (!procName) { 64ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin procName = const_cast<char*>("<unknown>"); 65ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin } 66ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 67ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin printer.printLine(); 68ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin printer.printLine(); 69ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin printer.printFormatLine("----- pid %d at %s -----", pid, timeStr); 70ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin printer.printFormatLine("Cmd line: %s", procName); 71ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin} 72ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 73ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkinstatic void dumpProcessFooter(Printer& printer, pid_t pid) { 74ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin printer.printLine(); 75ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin printer.printFormatLine("----- end %d -----", pid); 76ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin printer.printLine(); 77ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin} 78ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 79ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkinstatic String8 getThreadName(pid_t tid) { 80ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin char path[PATH_MAX]; 81ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin char* procName = NULL; 82ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin char procNameBuf[MAX_PROC_PATH]; 83ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin FILE* fp; 84ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 85ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin snprintf(path, sizeof(path), PATH_THREAD_NAME, tid); 86ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin if ((fp = fopen(path, "r"))) { 87ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin procName = fgets(procNameBuf, sizeof(procNameBuf), fp); 88ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin fclose(fp); 89ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin } else { 90ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin ALOGE("%s: Failed to open %s", __FUNCTION__, path); 91ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin } 92ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 9363fbdb6440ce5009ef0bfd3d1e1cc60b46004973Igor Murashkin if (procName == NULL) { 9463fbdb6440ce5009ef0bfd3d1e1cc60b46004973Igor Murashkin // Reading /proc/self/task/%d/comm failed due to a race 9563fbdb6440ce5009ef0bfd3d1e1cc60b46004973Igor Murashkin return String8::format("[err-unknown-tid-%d]", tid); 9663fbdb6440ce5009ef0bfd3d1e1cc60b46004973Igor Murashkin } 9763fbdb6440ce5009ef0bfd3d1e1cc60b46004973Igor Murashkin 98ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin // Strip ending newline 99ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin strtok(procName, "\n"); 100ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 101ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin return String8(procName); 102ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin} 103ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 104ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkinstatic String8 getTimeString(struct tm tm) { 105ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin char timestr[MAX_TIME_STRING]; 106ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin // i.e. '2013-10-22 14:42:05' 107ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin strftime(timestr, sizeof(timestr), "%F %T", &tm); 108ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 109ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin return String8(timestr); 110ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin} 111ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 112ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin/* 113ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin * Implementation of ProcessCallStack 114ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin */ 115ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor MurashkinProcessCallStack::ProcessCallStack() { 116ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin} 117ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 118ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor MurashkinProcessCallStack::ProcessCallStack(const ProcessCallStack& rhs) : 119ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin mThreadMap(rhs.mThreadMap), 120ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin mTimeUpdated(rhs.mTimeUpdated) { 121ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin} 122ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 123ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor MurashkinProcessCallStack::~ProcessCallStack() { 124ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin} 125ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 126ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkinvoid ProcessCallStack::clear() { 127ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin mThreadMap.clear(); 128ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin mTimeUpdated = tm(); 129ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin} 130ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 1319b0e074c6d38143e01616404a08b0c7aa992f3c3Christopher Ferrisvoid ProcessCallStack::update() { 132ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin DIR *dp; 133ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin struct dirent *ep; 134ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin struct dirent entry; 135ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 136ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin dp = opendir(PATH_SELF_TASK); 137ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin if (dp == NULL) { 138ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin ALOGE("%s: Failed to update the process's call stacks (errno = %d, '%s')", 139ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin __FUNCTION__, errno, strerror(errno)); 140ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin return; 141ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin } 142ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 143ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin pid_t selfPid = getpid(); 144ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 145ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin clear(); 146ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 147ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin // Get current time. 148e65b7ea8801145626504c724c28aedd0e5038a28Igor Murashkin#ifndef USE_MINGW 149ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin { 150ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin time_t t = time(NULL); 151ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin struct tm tm; 152ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin localtime_r(&t, &tm); 153ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 154ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin mTimeUpdated = tm; 155ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin } 156ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 157ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin /* 158ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin * Each tid is a directory inside of /proc/self/task 159ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin * - Read every file in directory => get every tid 160ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin */ 161ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin int code; 162ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin while ((code = readdir_r(dp, &entry, &ep)) == 0 && ep != NULL) { 163ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin pid_t tid = -1; 164ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin sscanf(ep->d_name, "%d", &tid); 165ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 166ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin if (tid < 0) { 167ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin // Ignore '.' and '..' 168ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin ALOGV("%s: Failed to read tid from %s/%s", 169ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin __FUNCTION__, PATH_SELF_TASK, ep->d_name); 170ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin continue; 171ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin } 172ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 173ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin ssize_t idx = mThreadMap.add(tid, ThreadInfo()); 174ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin if (idx < 0) { // returns negative error value on error 175ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin ALOGE("%s: Failed to add new ThreadInfo (errno = %zd, '%s')", 176ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin __FUNCTION__, idx, strerror(-idx)); 177ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin continue; 178ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin } 179ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 180ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin ThreadInfo& threadInfo = mThreadMap.editValueAt(static_cast<size_t>(idx)); 181ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 182ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin /* 183ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin * Ignore CallStack::update and ProcessCallStack::update for current thread 184ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin * - Every other thread doesn't need this since we call update off-thread 185ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin */ 186ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin int ignoreDepth = (selfPid == tid) ? IGNORE_DEPTH_CURRENT_THREAD : 0; 187ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 188ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin // Update thread's call stacks 1899b0e074c6d38143e01616404a08b0c7aa992f3c3Christopher Ferris threadInfo.callStack.update(ignoreDepth, tid); 190ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 191ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin // Read/save thread name 192ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin threadInfo.threadName = getThreadName(tid); 193ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 194ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin ALOGV("%s: Got call stack for tid %d (size %zu)", 1959b0e074c6d38143e01616404a08b0c7aa992f3c3Christopher Ferris __FUNCTION__, tid, threadInfo.callStack.size()); 196ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin } 197ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin if (code != 0) { // returns positive error value on error 198ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin ALOGE("%s: Failed to readdir from %s (errno = %d, '%s')", 199ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin __FUNCTION__, PATH_SELF_TASK, -code, strerror(code)); 200ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin } 201e65b7ea8801145626504c724c28aedd0e5038a28Igor Murashkin#endif 202ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 203ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin closedir(dp); 204ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin} 205ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 206ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkinvoid ProcessCallStack::log(const char* logtag, android_LogPriority priority, 207ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin const char* prefix) const { 208ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin LogPrinter printer(logtag, priority, prefix, /*ignoreBlankLines*/false); 209ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin print(printer); 210ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin} 211ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 212ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkinvoid ProcessCallStack::print(Printer& printer) const { 213ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin /* 214ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin * Print the header/footer with the regular printer. 215ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin * Print the callstack with an additional two spaces as the prefix for legibility. 216ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin */ 217ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin PrefixPrinter csPrinter(printer, CALL_STACK_PREFIX); 218ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin printInternal(printer, csPrinter); 219ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin} 220ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 221ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkinvoid ProcessCallStack::printInternal(Printer& printer, Printer& csPrinter) const { 222ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin dumpProcessHeader(printer, getpid(), 223ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin getTimeString(mTimeUpdated).string()); 224ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 225ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin for (size_t i = 0; i < mThreadMap.size(); ++i) { 226ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin pid_t tid = mThreadMap.keyAt(i); 227ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin const ThreadInfo& threadInfo = mThreadMap.valueAt(i); 228ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin const String8& threadName = threadInfo.threadName; 229ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 230ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin printer.printLine(""); 231ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin printer.printFormatLine("\"%s\" sysTid=%d", threadName.string(), tid); 232ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 2339b0e074c6d38143e01616404a08b0c7aa992f3c3Christopher Ferris threadInfo.callStack.print(csPrinter); 234ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin } 235ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 236ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin dumpProcessFooter(printer, getpid()); 237ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin} 238ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 239ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkinvoid ProcessCallStack::dump(int fd, int indent, const char* prefix) const { 240ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 241ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin if (indent < 0) { 242ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin ALOGW("%s: Bad indent (%d)", __FUNCTION__, indent); 243ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin return; 244ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin } 245ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 246ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin FdPrinter printer(fd, static_cast<unsigned int>(indent), prefix); 247ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin print(printer); 248ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin} 249ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 250ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor MurashkinString8 ProcessCallStack::toString(const char* prefix) const { 251ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 252ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin String8 dest; 253ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin String8Printer printer(&dest, prefix); 254ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin print(printer); 255ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 256ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin return dest; 257ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin} 258ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 259ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkinsize_t ProcessCallStack::size() const { 260ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin return mThreadMap.size(); 261ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin} 262ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin 263ec79ef2e7b6b1d81266637ca0e002b5c0c5a789bIgor Murashkin}; //namespace android 264