-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomm_func.cpp
More file actions
215 lines (201 loc) · 5.7 KB
/
comm_func.cpp
File metadata and controls
215 lines (201 loc) · 5.7 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include "comm_func.h"
#include <sys/syscall.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <fstream>
#include <iostream>
#include <fcntl.h>
#include <cstring>
namespace event_engine
{
int OpenPerfEvent(perf_event_attr *attr, int pid_or_fd, int cpu, int group_fd, unsigned long flags, std::string &err)
{
int res = syscall(SYS_perf_event_open, attr, pid_or_fd, cpu, group_fd, flags);
if (res < 0)
{
err = strerror(errno);
}
return res;
}
uint64_t GetStreamId(int fd, std::string &err)
{
uint64_t value{0};
if (ioctl(fd, PERF_EVENT_IOC_ID, &value) < 0)
{
err = strerror(errno);
}
return value;
}
int EnablePerfEvent(int fd, std::string &err)
{
int res = ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);
if (res < 0)
{
err = strerror(errno);
}
return res;
}
int DisablePerfEvent(int fd, std::string &err)
{
int res = ioctl(fd, PERF_EVENT_IOC_DISABLE, 0);
if (res < 0)
{
err = strerror(errno);
}
return res;
}
int SetPerfFilter(int fd, const std::string &filter, std::string &err)
{
int res = ioctl(fd, PERF_EVENT_IOC_SET_FILTER, filter.c_str());
if (res < 0)
{
err = strerror(errno);
}
return res;
}
std::vector<MountInfo> Mounts()
{
std::vector<MountInfo> res;
std::ifstream fin;
fin.open("/proc/self/mountinfo");
if (!fin.is_open())
{
std::cerr << "fatal: can't open mount file /proc/self/mountinfo" << std::endl;
exit(errno);
}
char buff[1024*1024]{0};
while (fin.getline(buff, sizeof(buff)))
{
MountInfo info;
info.ParseFromLine(StringTrimSpace(std::string(buff)));
res.push_back(info);
}
return res;
}
std::string TracingDir()
{
auto mounts = Mounts();
for (auto m : mounts)
{
if (m.filesystem_type_ == "tracefs")
{
return m.mount_point_;
}
}
for (auto m : mounts)
{
if (m.filesystem_type_ == "debugfs")
{
std::string path = m.mount_point_ + "/tracing";
struct stat st_buff;
if (stat((path + "/events").c_str(), &st_buff) < 0 || !S_ISDIR(st_buff.st_mode))
{
continue;
}
return path;
}
}
return "";
}
std::string PerfEventDir()
{
auto mounts = Mounts();
for (auto m : mounts)
{
if (m.filesystem_type_ == "cgroup")
{
for (auto option : m.super_options_)
{
if (option.first == "perf_event")
{
return m.mount_point_;
}
}
}
}
return "";
}
int WriteTraceCommand(const std::string &relative_path, const std::string &cmd, std::string &err)
{
std::string path = TracingDir() + "/" + relative_path;
int fd = open(path.c_str(), O_WRONLY | O_APPEND);
if (fd == -1)
{
err = strerror(errno);
return -1;
}
auto s = write(fd, cmd.c_str(), cmd.size());
close(fd);
if (s == -1)
{
err = strerror(errno);
return -1;
}
return 0;
}
int AddProbe(const std::string &group, const std::string &name, const std::string &address, const std::string &output, bool on_return, bool is_kprobe, std::string &err)
{
std::string cmd = on_return ? "r:" : "p:";
cmd += group + "/" + name + " ";
cmd += address + " ";
cmd += output;
return WriteTraceCommand(is_kprobe ? "kprobe_events" : "uprobe_events", cmd, err);
}
int RemoveProbe(const std::string &group, const std::string &name, bool is_kprobe, std::string &err)
{
return WriteTraceCommand(is_kprobe ? "kprobe_events" : "uprobe_events", "-:" + group + "/" + name, err);
}
int GetTraceEventID(const std::string &group, const std::string &name, std::string &err)
{
std::string file = TracingDir() + "/events/" + group + "/" + name + "/id";
int fd = open(file.c_str(), O_RDONLY);
if (fd == -1)
{
err = strerror(errno);
return -1;
}
char buff[8] = {0};
int n = read(fd, buff, 6);
close(fd);
if (n == -1)
{
err = strerror(errno);
return -1;
}
int i = 0;
for (; '0' <= buff[i] && buff[i] <= '9' && i < 6; i++)
{
}
buff[i] = 0;
int id = std::atoi(buff);
if (errno == ERANGE)
{
err = strerror(errno);
return -1;
}
return id;
}
std::vector<TraceEventField> GetTraceEventFormat(const std::string &group, const std::string &name, std::string &err)
{
std::vector<TraceEventField> res;
std::string file = TracingDir() + "/events/" + group + "/" + name + "/" + "format";
std::ifstream fin;
fin.open(file);
if (!fin.is_open())
{
err = strerror(errno);
return res;
}
char buff[1024]{0};
while (fin.getline(buff, sizeof(buff)))
{
TraceEventField field;
if (field.ParseFromLine(StringTrimSpace(std::string(buff))))
{
res.push_back(field);
}
}
return res;
}
}