-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathProcMaps.cc
187 lines (149 loc) · 4.18 KB
/
ProcMaps.cc
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
/*
* SPDX-License-Identifier: GPL-2.0
*
* Copyright (c) 2018 Intel Corporation
*
* Authors: Fengguang Wu <fengguang.wu@intel.com>
* Yao Yuan <yuan.yao@intel.com>
*/
#include <stdio.h>
#include <linux/limits.h>
#include <fstream>
#include <iostream>
#include <string>
#include <string.h>
#include "ProcMaps.h"
#if 1
static int parse_proc_maps(pid_t pid, std::vector<proc_maps_entry>& maps)
{
char filename[PATH_MAX];
proc_maps_entry e;
char line[4096];
int path_index;
FILE *f;
int ret = 0;
snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
f = fopen(filename, "r");
if (!f) {
perror(filename);
return -1;
}
while (fgets(line, sizeof(line), f))
{
ret = sscanf(line, "%lx-%lx %4s %lx %x:%x %lu%*[ ]%n",
&e.start,
&e.end,
e.perms,
&e.offset,
(unsigned int*)&e.dev_major,
(unsigned int*)&e.dev_minor,
&e.ino,
&path_index);
if (ret < 7)
{
fprintf(stderr, "parse failed: %d %s\n%s", ret, filename, line);
ret = -EINVAL;
break;
}
e.read = (e.perms[0] == 'r');
e.write = (e.perms[1] == 'w');
e.exec = (e.perms[2] == 'x');
e.mayshare = (e.perms[3] != 'p');
e.path = line + path_index;
e.path.pop_back(); // trim trailing '\n'
maps.push_back(e);
}
fclose(f);
return ret;
}
#else
/*
reimplement this because the it can not handle heap area before
*/
static int parse_proc_maps(pid_t pid, std::vector<proc_maps_entry>& maps)
{
std::ifstream input_file;
std::string each_line, str_value[6];
size_t each_split, split_array[7];
int i, ret;
char filename[PATH_MAX];
proc_maps_entry e;
ret = -1;
snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
input_file.open(filename, std::ios_base::in);
while (std::getline(input_file, each_line))
{
split_array[0] = 0;
for (i = 1; i < 6; ++i)
{
each_split = each_line.find_first_of(" ", split_array[i-1]);
if (std::string::npos == each_split)
{break;}
//+1 for begin from next non-space item
split_array[i] = each_split + 1;
//-1 because we have +1 above
str_value[i-1] = each_line.substr(split_array[i-1], split_array[i] - split_array[i-1] - 1);
}
split_array[i] = each_line.find_first_not_of(" ", split_array[i-1]);
if (std::string::npos != split_array[i])
{
str_value[i-1] = each_line.substr(split_array[i], std::string::npos);
}
else
{
str_value[i-1] = "Anonymous";
}
sscanf(str_value[0].c_str(), "%lx-%lx", &e.start, &e.end);
sscanf(str_value[2].c_str(), "%lx", &e.offset);
sscanf(str_value[3].c_str(), "%d:%d", &e.dev_major, &e.dev_minor);
sscanf(str_value[4].c_str(), "%lu", &e.ino);
e.read = (str_value[1][0] == 'r');
e.write = (str_value[1][1] == 'w');
e.exec = (str_value[1][2] == 'x');
e.mayshare = (str_value[1][3] != 'p');
e.path = str_value[5];
maps.push_back(e);
ret = 0;
}
input_file.close();
return ret;
}
#endif
std::vector<proc_maps_entry> ProcMaps::load(pid_t pid)
{
std::vector<proc_maps_entry> maps;
parse_proc_maps(pid, maps);
return maps;
}
void ProcMaps::show(const proc_maps_entry& vma)
{
printf("%lx-%lx %4s %08lx %02x:%02x %-8lu\t\t%s\n",
vma.start,
vma.end,
vma.perms,
vma.offset,
vma.dev_major,
vma.dev_minor,
vma.ino,
vma.path.c_str());
}
void ProcMaps::show(const std::vector<proc_maps_entry>& maps)
{
for (const proc_maps_entry& vma: maps)
show(vma);
}
bool ProcMaps::is_anonymous(proc_maps_entry& vma)
{
if (vma.mayshare)
return false;
if (vma.exec)
return false;
// 560dd67c9000-560dd67ea000 rw-p 00000000 00:00 0 [heap]
// 7ffdcc7fe000-7ffdcc820000 rw-p 00000000 00:00 0 [stack]
if (vma.ino == 0)
return true;
// 7f3b6a034000-7f3b6a036000 rw-p 001eb000 00:11 3016841 /lib/x86_64-linux-gnu/libc-2.27.so
if (vma.write)
return true;
return false;
}