blob: 1c2d2e6c198a786b9c5482c15704bf1afbc98543 [file] [log] [blame]
Isabelle Taylord15631b2018-02-12 10:32:41 +00001// Copyright 2017 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "procfs_utils.h"
6
7#include <stdio.h>
8#include <string.h>
9#include <fstream>
10
11#include "file_utils.h"
12
Isabelle Taylor2b2ec982018-02-21 16:25:05 +000013#include "perfetto/base/logging.h"
14
Isabelle Taylord15631b2018-02-12 10:32:41 +000015using file_utils::ForEachPidInProcPath;
16using file_utils::ReadProcFile;
17using file_utils::ReadProcFileTrimmed;
18
19namespace procfs_utils {
20
21namespace {
22
23const char kJavaAppPrefix[] = "/system/bin/app_process";
24const char kZygotePrefix[] = "zygote";
25
26inline void ReadProcString(int pid, const char* path, char* buf, size_t size) {
27 if (!file_utils::ReadProcFileTrimmed(pid, path, buf, size))
28 buf[0] = '\0';
29}
30
31inline void ReadExePath(int pid, char* buf, size_t size) {
32 char exe_path[64];
33 sprintf(exe_path, "/proc/%d/exe", pid);
34 ssize_t res = readlink(exe_path, buf, size - 1);
35 if (res >= 0)
36 buf[res] = '\0';
37 else
38 buf[0] = '\0';
39}
40
41inline bool IsApp(const char* name, const char* exe) {
42 return strncmp(exe, kJavaAppPrefix, sizeof(kJavaAppPrefix) - 1) == 0 &&
43 strncmp(name, kZygotePrefix, sizeof(kZygotePrefix) - 1) != 0;
44}
45
Isabelle Taylord404ea12018-02-19 17:28:01 +000046inline int ReadStatusLine(int pid, const char* status_string) {
Isabelle Taylord15631b2018-02-12 10:32:41 +000047 char buf[512];
48 ssize_t rsize = ReadProcFile(pid, "status", buf, sizeof(buf));
49 if (rsize <= 0)
50 return -1;
Isabelle Taylord404ea12018-02-19 17:28:01 +000051 const char* line = strstr(buf, status_string);
Isabelle Taylor2b2ec982018-02-21 16:25:05 +000052 PERFETTO_DCHECK(line);
Isabelle Taylord404ea12018-02-19 17:28:01 +000053 return atoi(line + sizeof(status_string) - 1);
54}
55
56} // namespace
57
58int ReadTgid(int pid) {
59 return ReadStatusLine(pid, "\nTgid:");
60}
61
62int ReadPpid(int pid) {
Isabelle Taylor2b2ec982018-02-21 16:25:05 +000063 return ReadStatusLine(pid, "\nPPid:");
Isabelle Taylord15631b2018-02-12 10:32:41 +000064}
65
66std::unique_ptr<ProcessInfo> ReadProcessInfo(int pid) {
67 ProcessInfo* process = new ProcessInfo();
68 process->pid = pid;
Isabelle Taylord404ea12018-02-19 17:28:01 +000069 // TODO(taylori) Parse full cmdline into a vector.
70 ReadProcString(pid, "cmdline", process->cmdline, sizeof(process->cmdline));
71 if (process->cmdline[0] == 0) {
72 ReadProcString(pid, "comm", process->cmdline, sizeof(process->cmdline));
Isabelle Taylord15631b2018-02-12 10:32:41 +000073 process->in_kernel = true;
Isabelle Taylord404ea12018-02-19 17:28:01 +000074 } else {
75 ReadExePath(pid, process->exe, sizeof(process->exe));
76 process->is_app = IsApp(process->cmdline, process->exe);
Isabelle Taylord15631b2018-02-12 10:32:41 +000077 }
Isabelle Taylord404ea12018-02-19 17:28:01 +000078 process->ppid = ReadPpid(pid);
Isabelle Taylord15631b2018-02-12 10:32:41 +000079 return std::unique_ptr<ProcessInfo>(process);
80}
81
82void ReadProcessThreads(ProcessInfo* process) {
83 if (process->in_kernel)
84 return;
85
86 char tasks_path[64];
87 sprintf(tasks_path, "/proc/%d/task", process->pid);
88 ForEachPidInProcPath(tasks_path, [process](int tid) {
89 if (process->threads.count(tid))
90 return;
91 ThreadInfo thread = {tid, ""};
92 char task_comm[64];
93 sprintf(task_comm, "task/%d/comm", tid);
94 ReadProcString(process->pid, task_comm, thread.name, sizeof(thread.name));
95 if (thread.name[0] == '\0' && process->is_app)
96 strcpy(thread.name, "UI Thread");
97 process->threads[tid] = thread;
98 });
99}
100
Isabelle Taylord15631b2018-02-12 10:32:41 +0000101} // namespace procfs_utils