Isabelle Taylor | d15631b | 2018-02-12 10:32:41 +0000 | [diff] [blame] | 1 | // 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 Taylor | 2b2ec98 | 2018-02-21 16:25:05 +0000 | [diff] [blame] | 13 | #include "perfetto/base/logging.h" |
| 14 | |
Isabelle Taylor | d15631b | 2018-02-12 10:32:41 +0000 | [diff] [blame] | 15 | using file_utils::ForEachPidInProcPath; |
| 16 | using file_utils::ReadProcFile; |
| 17 | using file_utils::ReadProcFileTrimmed; |
| 18 | |
| 19 | namespace procfs_utils { |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | const char kJavaAppPrefix[] = "/system/bin/app_process"; |
| 24 | const char kZygotePrefix[] = "zygote"; |
| 25 | |
| 26 | inline 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 | |
| 31 | inline 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 | |
| 41 | inline 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 Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 46 | inline int ReadStatusLine(int pid, const char* status_string) { |
Isabelle Taylor | d15631b | 2018-02-12 10:32:41 +0000 | [diff] [blame] | 47 | char buf[512]; |
| 48 | ssize_t rsize = ReadProcFile(pid, "status", buf, sizeof(buf)); |
| 49 | if (rsize <= 0) |
| 50 | return -1; |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 51 | const char* line = strstr(buf, status_string); |
Isabelle Taylor | 2b2ec98 | 2018-02-21 16:25:05 +0000 | [diff] [blame] | 52 | PERFETTO_DCHECK(line); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 53 | return atoi(line + sizeof(status_string) - 1); |
| 54 | } |
| 55 | |
| 56 | } // namespace |
| 57 | |
| 58 | int ReadTgid(int pid) { |
| 59 | return ReadStatusLine(pid, "\nTgid:"); |
| 60 | } |
| 61 | |
| 62 | int ReadPpid(int pid) { |
Isabelle Taylor | 2b2ec98 | 2018-02-21 16:25:05 +0000 | [diff] [blame] | 63 | return ReadStatusLine(pid, "\nPPid:"); |
Isabelle Taylor | d15631b | 2018-02-12 10:32:41 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | std::unique_ptr<ProcessInfo> ReadProcessInfo(int pid) { |
| 67 | ProcessInfo* process = new ProcessInfo(); |
| 68 | process->pid = pid; |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 69 | // 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 Taylor | d15631b | 2018-02-12 10:32:41 +0000 | [diff] [blame] | 73 | process->in_kernel = true; |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 74 | } else { |
| 75 | ReadExePath(pid, process->exe, sizeof(process->exe)); |
| 76 | process->is_app = IsApp(process->cmdline, process->exe); |
Isabelle Taylor | d15631b | 2018-02-12 10:32:41 +0000 | [diff] [blame] | 77 | } |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 78 | process->ppid = ReadPpid(pid); |
Isabelle Taylor | d15631b | 2018-02-12 10:32:41 +0000 | [diff] [blame] | 79 | return std::unique_ptr<ProcessInfo>(process); |
| 80 | } |
| 81 | |
| 82 | void 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 Taylor | d15631b | 2018-02-12 10:32:41 +0000 | [diff] [blame] | 101 | } // namespace procfs_utils |