2 * Copyright (C) ARM Limited 2013-2014. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
22 // From linux-dev/include/linux/sched.h
23 #define TASK_COMM_LEN 16
24 // TASK_COMM_LEN may grow, so be ready for it to get larger
25 char comm[2*TASK_COMM_LEN];
29 static bool readProcStat(ProcStat *const ps, const char *const pathname, DynBuf *const b) {
30 if (!b->read(pathname)) {
31 logg->logMessage("%s(%s:%i): DynBuf::read failed, likely because the thread exited", __FUNCTION__, __FILE__, __LINE__);
32 // This is not a fatal error - the thread just doesn't exist any more
36 char *comm = strchr(b->getBuf(), '(');
38 logg->logMessage("%s(%s:%i): parsing stat failed", __FUNCTION__, __FILE__, __LINE__);
42 char *const str = strrchr(comm, ')');
44 logg->logMessage("%s(%s:%i): parsing stat failed", __FUNCTION__, __FILE__, __LINE__);
48 strncpy(ps->comm, comm, sizeof(ps->comm) - 1);
49 ps->comm[sizeof(ps->comm) - 1] = '\0';
51 const int count = sscanf(str + 2, " %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %ld", &ps->numThreads);
53 logg->logMessage("%s(%s:%i): sscanf failed", __FUNCTION__, __FILE__, __LINE__);
60 static bool readProcTask(Buffer *const buffer, const int pid, const char *const image, DynBuf *const printb, DynBuf *const b) {
63 if (!b->printf("/proc/%i/task", pid)) {
64 logg->logMessage("%s(%s:%i): DynBuf::printf failed", __FUNCTION__, __FILE__, __LINE__);
67 DIR *task = opendir(b->getBuf());
69 logg->logMessage("%s(%s:%i): opendir failed", __FUNCTION__, __FILE__, __LINE__);
73 struct dirent *dirent;
74 while ((dirent = readdir(task)) != NULL) {
76 const int tid = strtol(dirent->d_name, &endptr, 10);
77 if (*endptr != '\0') {
78 // Ignore task items that are not integers like ., etc...
82 if (!printb->printf("/proc/%i/task/%i/stat", pid, tid)) {
83 logg->logMessage("%s(%s:%i): DynBuf::printf failed", __FUNCTION__, __FILE__, __LINE__);
87 if (!readProcStat(&ps, printb->getBuf(), b)) {
88 logg->logMessage("%s(%s:%i): readProcStat failed", __FUNCTION__, __FILE__, __LINE__);
92 buffer->comm(pid, tid, image, ps.comm);
103 bool readProc(Buffer *const buffer, DynBuf *const printb, DynBuf *const b1, DynBuf *const b2, DynBuf *const b3) {
106 DIR *proc = opendir("/proc");
108 logg->logMessage("%s(%s:%i): opendir failed", __FUNCTION__, __FILE__, __LINE__);
112 struct dirent *dirent;
113 while ((dirent = readdir(proc)) != NULL) {
115 const int pid = strtol(dirent->d_name, &endptr, 10);
116 if (*endptr != '\0') {
117 // Ignore proc items that are not integers like ., cpuinfo, etc...
121 if (!printb->printf("/proc/%i/stat", pid)) {
122 logg->logMessage("%s(%s:%i): DynBuf::printf failed", __FUNCTION__, __FILE__, __LINE__);
126 if (!readProcStat(&ps, printb->getBuf(), b1)) {
127 logg->logMessage("%s(%s:%i): readProcStat failed", __FUNCTION__, __FILE__, __LINE__);
131 if (!printb->printf("/proc/%i/exe", pid)) {
132 logg->logMessage("%s(%s:%i): DynBuf::printf failed", __FUNCTION__, __FILE__, __LINE__);
135 const int err = b1->readlink(printb->getBuf());
138 image = strrchr(b1->getBuf(), '/');
140 image = b1->getBuf();
144 } else if (err == -ENOENT) {
145 // readlink /proc/[pid]/exe returns ENOENT for kernel threads
148 logg->logMessage("%s(%s:%i): DynBuf::readlink failed", __FUNCTION__, __FILE__, __LINE__);
152 if (!printb->printf("/proc/%i/maps", pid)) {
153 logg->logMessage("%s(%s:%i): DynBuf::printf failed", __FUNCTION__, __FILE__, __LINE__);
156 if (!b2->read(printb->getBuf())) {
157 logg->logMessage("%s(%s:%i): DynBuf::read failed, likely because the process exited", __FUNCTION__, __FILE__, __LINE__);
158 // This is not a fatal error - the process just doesn't exist any more
162 buffer->maps(pid, pid, b2->getBuf());
163 if (ps.numThreads <= 1) {
164 buffer->comm(pid, pid, image, ps.comm);
166 if (!readProcTask(buffer, pid, image, printb, b3)) {
167 logg->logMessage("%s(%s:%i): readProcTask failed", __FUNCTION__, __FILE__, __LINE__);