DebugServer2
Loading...
Searching...
No Matches
ProcFS.h
1//
2// Copyright (c) 2014-present, Facebook, Inc.
3// All rights reserved.
4//
5// This source code is licensed under the University of Illinois/NCSA Open
6// Source License found in the LICENSE file in the root directory of this
7// source tree. An additional grant of patent rights can be found in the
8// PATENTS file in the same directory.
9//
10
11#pragma once
12
13#include "DebugServer2/Types.h"
14
15#include <cstdio>
16#include <dirent.h>
17#include <fcntl.h>
18#include <functional>
19#include <sys/stat.h>
20#include <unistd.h>
21
22namespace ds2 {
23namespace Host {
24namespace Linux {
25
26// On Linux, COMM field is 16 chars long
27static size_t const kCOMMLengthMax = 16;
28
29enum ProcState {
30 kProcStateUninterruptible = 'D',
31 kProcStateRunning = 'R',
32 kProcStateSleeping = 'S',
33 kProcStateStopped = 'T',
34 kProcStateTraced = 't',
35 kProcStatePaging = 'W',
36 kProcStateDead = 'X',
37 kProcStateZombie = 'Z',
38};
39
40class ProcFS {
41public:
42 struct Uptime {
43 struct timespec run_time;
44 struct timespec idle_time;
45 };
46
47 struct Stat {
48 pid_t pid;
49 char tcomm[kCOMMLengthMax + 1];
50 char state;
51 pid_t ppid;
52 pid_t pgrp;
53 pid_t sid;
54 uint32_t tty_nr;
55 pid_t tty_pgrp;
56 uint64_t flags;
57 uint64_t min_flt;
58 uint64_t cmin_flt;
59 uint64_t maj_flt;
60 uint64_t cmaj_flt;
61 uint64_t utime;
62 uint64_t stime;
63 uint64_t cutime;
64 uint64_t cstime;
65 int32_t priority;
66 int32_t nice;
67 uint32_t num_threads;
68 uint64_t it_real_value;
69 uint64_t start_time;
70 uint64_t vsize;
71 uint64_t rss;
72 uint64_t rsslim;
73 uint64_t start_code;
74 uint64_t end_code;
75 uint64_t start_stack;
76 uint64_t esp;
77 uint64_t eip;
78 uint64_t pending;
79 uint64_t blocked;
80 uint64_t sigign;
81 uint64_t sigcatch;
82 uint64_t wchan;
83 uint32_t exit_signal;
84 uint32_t task_cpu;
85 int32_t rt_priority;
86 uint32_t policy;
87 uint64_t blkio_ticks;
88 uint64_t gtime;
89 uint64_t cgtime;
90 uint64_t start_data;
91 uint64_t end_data;
92 uint64_t start_brk;
93 };
94
95public:
96 static int OpenFd(const char *what, int mode = O_RDONLY);
97 static int OpenFd(pid_t pid, const char *what, int mode = O_RDONLY);
98 static int OpenFd(pid_t pid, pid_t tid, const char *what,
99 int mode = O_RDONLY);
100
101 static FILE *OpenFILE(char const *what, char const *mode = "r");
102 static FILE *OpenFILE(pid_t pid, char const *what, char const *mode = "r");
103 static FILE *OpenFILE(pid_t pid, pid_t tid, const char *what,
104 char const *mode = "r");
105
106 static DIR *OpenDIR(const char *what);
107 static DIR *OpenDIR(pid_t pid, const char *what);
108 static DIR *OpenDIR(pid_t pid, pid_t tid, const char *what);
109
110 static bool ReadLink(pid_t pid, char const *what, char *buf, size_t bufsiz);
111 static bool ReadLink(pid_t pid, pid_t tid, char const *what, char *buf,
112 size_t bufsiz);
113
114public:
115 static void
116 ParseKeyValue(FILE *fp, size_t maxsize, char sep,
117 std::function<bool(char const *, char const *)> const &cb);
118 static void ParseValues(FILE *fp, size_t maxsize, char sep, bool includeSep,
119 std::function<bool(size_t, char const *)> const &cb);
120
121public:
122 static bool ReadUptime(Uptime &uptime);
123 static bool ReadStat(pid_t pid, Stat &stat);
124 static bool ReadStat(pid_t pid, pid_t tid, Stat &stat);
125 static bool ReadProcessIds(pid_t pid, pid_t &ppid, uid_t &uid, uid_t &euid,
126 gid_t &gid, gid_t &egid);
127
128public:
129 struct ELFInfo {
130 uint32_t machine;
131 Endian endian;
132 bool is64Bit;
133 };
134
135 static bool GetProcessELFInfo(pid_t pid, ELFInfo &info);
136 static int32_t GetProcessELFMachineType(pid_t pid, bool *is64Bit = nullptr);
137 static CPUType GetProcessCPUType(pid_t pid);
138
139public:
140 static bool ReadProcessInfo(pid_t pid, ProcessInfo &info);
141
142public:
143 static std::string GetProcessName(pid_t pid);
144 static pid_t GetProcessParentPid(pid_t pid);
145 static std::string GetThreadName(pid_t pid, pid_t tid);
146 static std::string GetProcessExecutableName(pid_t pid);
147 static std::string GetProcessExecutablePath(pid_t pid);
148 static bool GetProcessArguments(pid_t pid, ds2::StringCollection &args);
149 static std::string GetProcessArgumentsAsString(pid_t pid, bool arg0 = false);
150
151public:
152 static bool EnumerateProcesses(bool allUsers, uid_t uid,
153 std::function<void(pid_t, uid_t)> const &cb);
154 static bool EnumerateThreads(pid_t pid, std::function<void(pid_t)> const &cb);
155};
156} // namespace Linux
157} // namespace Host
158} // namespace ds2
Definition ProcFS.h:40
Definition ProcFS.h:129
Definition ProcFS.h:47
Definition ProcFS.h:42
Definition Types.h:263