-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproc.cpp
More file actions
99 lines (81 loc) · 2.03 KB
/
Copy pathproc.cpp
File metadata and controls
99 lines (81 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <proc.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/auxv.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/prctl.h>
#include <sys/user.h>
#include <dirent.h>
#include <errno.h>
#include <assert.h>
#include <string-utils.h>
#include <easylogging++.h>
#include <sstream>
#include <fstream> // std::ifstream
#define MAX_DELAY 100000 /* 100000 microseconds = 0.1 seconds */
std::string proc_util::pid2name(pid_t pid){
if (!kill(pid, 0)) {
int delay = 0;
std::ostringstream oss;
oss << "/proc/" << pid << "/exe";
std::string proc_exe = oss.str();
while (delay < MAX_DELAY) {
if (!access(proc_exe.c_str(), F_OK)) {
return proc_exe;
}
delay += 1000; /* 1 milisecond */
}
}
return "";
}
int proc_util::wait_for_proc(pid_t pid){
if (waitpid(pid, NULL, __WALL) != pid) {
LOG(ERROR) << "trace_pid: waitpid";
return -1;
}
return 0;
}
int proc_util::get_sub_tids(const pid_t pid, thread_info_map_t& thr_map){
DIR *dir;
pid_t *list;
size_t size, used = 0;
if (pid < (pid_t)1) {
return -1;
}
std::string dir_path = "/proc/";
dir_path += std::to_string(pid);
dir_path += "/task/";
dir = opendir(dir_path.c_str());
if (!dir) {
return -1;
}
while (true) {
struct dirent *ent;
int value;
char dummy;
ent = readdir(dir);
if (!ent)
break;
/* Parse TIDs. Ignore non-numeric entries. */
if (sscanf(ent->d_name, "%d%c", &value, &dummy) != 1)
continue;
/* Ignore obviously invalid entries. */
if (value < 1)
continue;
thread_info_t thr_info;
thr_info.tid = (pid_t)value;
//获取线程名称
std::string comm_path = dir_path;
comm_path += ent->d_name;
comm_path += "/comm";
std::ifstream t(comm_path.c_str());
std::stringstream buffer;
buffer << t.rdbuf();
thr_info.name = Trim(buffer.str());
thr_map[thr_info.tid] = thr_info;
}
closedir(dir);
return 0;
}