-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMemoryMappings.C
84 lines (69 loc) · 1.74 KB
/
MemoryMappings.C
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
#include "MemoryMappings.h"
#include "Common.h"
#include <stdlib.h>
#include <string.h>
MemoryMappings::MemoryMappings (const int pid)
{
const vector<string> lines = RawLines(pid);
for (unsigned int i = 0; i < lines.size(); ++i)
{
const char* line = lines[i].c_str();
Mapping m;
m.start = strtoll(line, NULL, 16);
m.end = strtoll(line+9, NULL, 16);
if (lines[i].length() > 49)
{
m.name = string(line+49);
m.name = m.name.substr(0, m.name.length()-1);
}
m.isExecutable = (line[20] == 'x');
this->mappings.push_back(m);
}
}
const MemoryMappings::Mapping* MemoryMappings::Find (const string& name) const
{
for (const_iterator it = this->Begin(); it != this->End(); ++it)
{
if (it->name == name)
{
return &(*it);
}
}
return NULL;
}
vector<MemoryMappings::Mapping>::const_iterator MemoryMappings::Begin () const
{
return this->mappings.begin();
}
vector<MemoryMappings::Mapping>::const_iterator MemoryMappings::End () const
{
return this->mappings.end();
}
unsigned int MemoryMappings::Size () const
{
return this->mappings.size();
}
vector<string> MemoryMappings::RawLines (const int pid)
{
vector<string> lines;
char mapFileName[200];
sprintf(mapFileName, "/proc/%d/maps", pid);
FILE* mapFd = fopen(mapFileName, "r");
if (!mapFd)
{
return lines;
}
while (true)
{
char line[500];
memset(line, '\0', sizeof(line));
const char* result = fgets(line, sizeof(line), mapFd);
if (result == NULL || feof(mapFd))
{
break;
}
lines.push_back(line);
}
fclose(mapFd);
return lines;
}