-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpmap.cpp
More file actions
155 lines (132 loc) · 4.42 KB
/
pmap.cpp
File metadata and controls
155 lines (132 loc) · 4.42 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
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <unistd.h>
#include <vector>
#include <cfbox/applet.hpp>
#include <cfbox/args.hpp>
#include <cfbox/help.hpp>
#include <cfbox/proc.hpp>
#include <cfbox/error.hpp>
namespace {
constexpr cfbox::help::HelpEntry HELP = {
.name = "pmap",
.version = CFBOX_VERSION_STRING,
.one_line = "display memory map of a process",
.usage = "pmap [-x] PID",
.options = " -x extended format",
.extra = "",
};
struct MapEntry {
std::uint64_t address = 0;
std::uint64_t end_address = 0;
std::string perms;
std::uint64_t offset = 0;
std::string dev;
std::uint64_t inode = 0;
std::string pathname;
};
auto parse_maps(pid_t pid) -> std::vector<MapEntry> {
auto path = "/proc/" + std::to_string(pid) + "/maps";
auto* f = std::fopen(path.c_str(), "r");
if (!f) return {};
std::vector<MapEntry> entries;
char line[1024];
while (std::fgets(line, sizeof(line), f)) {
auto len = std::strlen(line);
while (len > 0 && (line[len - 1] == '\n' || line[len - 1] == '\r')) {
line[--len] = '\0';
}
if (len == 0) continue;
MapEntry e;
char perms[8] = {};
char dev[16] = {};
unsigned long long addr = 0;
unsigned long long off = 0;
unsigned long long ino = 0;
auto n = std::sscanf(line, "%llx-%*x %7s %llx %15s %llu",
&addr, perms, &off, dev, &ino);
if (n < 1) continue;
e.address = addr;
e.perms = perms;
e.offset = off;
e.dev = dev;
e.inode = ino;
// Extract pathname: find the field after inode
const char* p = line;
// Skip address field
while (*p && *p != ' ') ++p;
// Skip to perms
while (*p == ' ') ++p;
// Skip perms
while (*p && *p != ' ') ++p;
// Skip to offset
while (*p == ' ') ++p;
// Skip offset
while (*p && *p != ' ') ++p;
// Skip to dev
while (*p == ' ') ++p;
// Skip dev
while (*p && *p != ' ') ++p;
// Skip to inode
while (*p == ' ') ++p;
// Skip inode
while (*p && *p != ' ') ++p;
// Skip spaces to pathname
while (*p == ' ') ++p;
if (*p) e.pathname = p;
entries.push_back(std::move(e));
}
std::fclose(f);
// Calculate end_address from next entry
for (size_t i = 0; i + 1 < entries.size(); ++i) {
entries[i].end_address = entries[i + 1].address;
}
return entries;
}
} // anonymous namespace
auto pmap_main(int argc, char* argv[]) -> int {
auto parsed = cfbox::args::parse(argc, argv, {
cfbox::args::OptSpec{'x', false, "extended"},
});
if (parsed.has_long("help")) { cfbox::help::print_help(HELP); return 0; }
if (parsed.has_long("version")) { cfbox::help::print_version(HELP); return 0; }
bool extended = parsed.has('x') || parsed.has_long("extended");
const auto& args = parsed.positional();
if (args.empty()) {
CFBOX_ERR("pmap", "no PID specified");
return 1;
}
pid_t pid = static_cast<pid_t>(std::strtol(args[0].data(), nullptr, 10));
auto entries = parse_maps(pid);
if (entries.empty()) {
CFBOX_ERR("pmap", "cannot read maps for PID %d", pid);
return 1;
}
std::printf("%d: %s\n", pid, std::string(args[0]).c_str());
std::uint64_t total_kb = 0;
for (const auto& e : entries) {
auto kb = (e.end_address - e.address) / 1024;
total_kb += kb;
if (extended) {
std::printf("%016lx %5llu %08lx %-5s %-6s %6llu %s\n",
static_cast<unsigned long>(e.address),
static_cast<unsigned long long>(kb),
static_cast<unsigned long>(e.offset),
e.perms.c_str(),
e.dev.c_str(),
static_cast<unsigned long long>(e.inode),
e.pathname.c_str());
} else {
std::printf("%016lx %5luK %s %s\n",
static_cast<unsigned long>(e.address),
static_cast<unsigned long>(kb),
e.perms.c_str(),
e.pathname.empty() ? "" : e.pathname.c_str());
}
}
std::printf(" mapped: %lluK\n",
static_cast<unsigned long long>(total_kb));
return 0;
}