-
Notifications
You must be signed in to change notification settings - Fork 37
/
pmparser.c
301 lines (262 loc) · 7.16 KB
/
pmparser.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
@Author : ouadev
@date : December 2015
Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation. No representations are made about the suitability of this
software for any purpose. It is provided "as is" without express or
implied warranty.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pmparser.h"
// maximum line length in a procmaps file
#define PROCMAPS_LINE_INIT_LENGTH (300)
// maximum length of the path of a maps file : /proc/[pid]/maps
#define PROCMAPS_MAPS_FILE_PATH_MAX_LENGTH 30
// maximum token size while parsing the proc/pid/maps
#define PROCMAPS_LINE_TOKEN_MAX_LEN 100
/**
* pmparser_parse_line
* @description internal usage
*/
static void pmparser_parse_line(char *buf, procmaps_struct *mem_reg);
/**
* @brief Copy into dest_ptr the string from src_ptr to the first occurence of delimiter
*/
static char *pmparser_helper_extract(char *src_ptr, const char *delimiter, char *dest_ptr);
/**
* @brief Main function to parse process memory
*
* @param pid process ID
* @param maps_it output : the memory region iterator over the chained list, it should only be read when return is 0
* @return procmaps_error_t outcome of the function
*/
procmaps_error_t pmparser_parse(int pid, procmaps_iterator *maps_it)
{
char maps_path[PROCMAPS_MAPS_FILE_PATH_MAX_LENGTH];
size_t line_len = 0;
char *line_ptr = NULL;
procmaps_struct *mem_reg = NULL;
procmaps_struct *tail_node = NULL;
procmaps_struct *head_node = NULL;
size_t node_count = 0;
if (pid >= 0)
{
snprintf(maps_path, sizeof(maps_path), "/proc/%d/maps", pid);
}
else
{
sprintf(maps_path, "/proc/self/maps");
}
FILE *file = fopen(maps_path, "r");
if (!file)
{
return PROCMAPS_ERROR_OPEN_MAPS_FILE;
}
// scan maps file line by line
line_len = PROCMAPS_LINE_INIT_LENGTH;
if ((line_ptr = (char *)malloc(line_len)) == NULL)
{
fclose(file);
return PROCMAPS_ERROR_MALLOC_FAIL;
}
while (1)
{
int read = getline(&line_ptr, &line_len, file);
if (read == -1)
{
if (!feof(file))
{
return PROCMAPS_ERROR_READ_MAPS_FILE;
}
else
{
// end of file occured while no characters have been read
break;
}
}
// allocate a node
mem_reg = (procmaps_struct *)malloc(sizeof(procmaps_struct));
// fill the node
pmparser_parse_line(line_ptr, mem_reg);
mem_reg->next = NULL;
// Attach the node
if (tail_node == NULL)
{
head_node = tail_node = mem_reg;
}
else
{
tail_node->next = mem_reg;
tail_node = mem_reg;
}
node_count++;
}
// close file
fclose(file);
free(line_ptr);
// set iterator
maps_it->head = maps_it->current = head_node;
maps_it->count = node_count;
return PROCMAPS_SUCCESS;
}
/**
* @brief move the iterator to the next memory region
*
* @param p_procmaps_it
* @return procmaps_struct*
*/
procmaps_struct *pmparser_next(procmaps_iterator *p_procmaps_it)
{
if (p_procmaps_it->current == NULL)
return NULL;
procmaps_struct *p_current = p_procmaps_it->current;
p_procmaps_it->current = p_procmaps_it->current->next;
return p_current;
}
/**
* @brief free the parser data
*
* @param p_procmaps_it
*/
void pmparser_free(procmaps_iterator *p_procmaps_it)
{
procmaps_struct *cursor = p_procmaps_it->head;
procmaps_struct *next = NULL;
if (p_procmaps_it->head == NULL)
return;
while (cursor != NULL)
{
next = cursor->next;
free(cursor->pathname);
free(cursor);
cursor = next;
}
memset(p_procmaps_it, 0x00, sizeof(procmaps_iterator));
}
static char *pmparser_helper_extract(char *src_ptr, const char *delimiter, char *dest_ptr)
{
char *p_separator = NULL;
size_t copy_len = 0;
p_separator = strstr(src_ptr, delimiter);
copy_len = (p_separator - src_ptr);
memcpy(dest_ptr, src_ptr, copy_len);
dest_ptr[copy_len] = 0x00;
return p_separator;
}
static void pmparser_parse_line(char *buf, procmaps_struct *mem_reg)
{
char token[PROCMAPS_LINE_TOKEN_MAX_LEN];
size_t pathname_len = 0;
char *p_cursor = buf;
// addr1
p_cursor = pmparser_helper_extract(p_cursor, "-", token);
p_cursor++;
sscanf(token, "%lx", (long unsigned *)&mem_reg->addr_start);
// addr2
p_cursor = pmparser_helper_extract(p_cursor, " ", token);
p_cursor++;
sscanf(token, "%lx", (long unsigned *)&mem_reg->addr_end);
// region size
mem_reg->length = (unsigned long)((char*)mem_reg->addr_end - (char*)mem_reg->addr_start);
// perm
p_cursor = pmparser_helper_extract(p_cursor, " ", token);
p_cursor++;
mem_reg->is_r = (token[0] == 'r');
mem_reg->is_w = (token[1] == 'w');
mem_reg->is_x = (token[2] == 'x');
mem_reg->is_p = (token[3] == 'p');
// offset
p_cursor = pmparser_helper_extract(p_cursor, " ", token);
p_cursor++;
sscanf(token, "%lx", &mem_reg->offset);
// dev
p_cursor = pmparser_helper_extract(p_cursor, " ", token);
p_cursor++;
sscanf(token, "%u:%u", &mem_reg->dev_major, &mem_reg->dev_minor);
// inode
p_cursor = pmparser_helper_extract(p_cursor, " ", token);
p_cursor++;
sscanf(token, "%llu", &mem_reg->inode);
// pathname
// find the start of the pathname
while (*p_cursor == '\t' || *p_cursor == ' ')
p_cursor++;
// calculate its size
char *ptr_sz = p_cursor;
while (*ptr_sz != '\n')
{
ptr_sz++;
}
pathname_len = (ptr_sz - p_cursor);
// copy it
mem_reg->pathname = (char *)malloc(pathname_len * sizeof(char) + 1);
memcpy(mem_reg->pathname, p_cursor, pathname_len);
mem_reg->pathname[pathname_len] = 0x00;
// Pathname decoding
if (mem_reg->pathname[0] == 0x00)
{
// empty path name
mem_reg->map_type = PROCMAPS_MAP_ANON_MMAPS;
}
else if (strncmp(mem_reg->pathname, "[stack]", 7) == 0)
{
// mapping backed by main thread stack
mem_reg->map_type = PROCMAPS_MAP_STACK;
}
else if (strncmp(mem_reg->pathname, "[stack:", 7) == 0)
{
mem_reg->map_type = PROCMAPS_MAP_STACK_TID;
}
else if (strncmp(mem_reg->pathname, "[vdso]", 6) == 0)
{
mem_reg->map_type = PROCMAPS_MAP_VDSO;
}
else if (strncmp(mem_reg->pathname, "[heap]", 6) == 0)
{
mem_reg->map_type = PROCMAPS_MAP_HEAP;
}
else if (strncmp(mem_reg->pathname, "[anon:", 6) == 0)
{
mem_reg->map_type = PROCMAPS_MAP_ANON_PRIV;
pmparser_helper_extract(mem_reg->pathname + 6, "]", token);
strncpy(mem_reg->map_anon_name, token, MAPPING_ANON_NAME_MAX_LEN);
}
else if (strncmp(mem_reg->pathname, "[anon_shmem:", 12) == 0)
{
mem_reg->map_type = PROCMAPS_MAP_ANON_SHMEM;
pmparser_helper_extract(mem_reg->pathname + 12, "]", token);
strncpy(mem_reg->map_anon_name, token, MAPPING_ANON_NAME_MAX_LEN);
}
else if (strncmp(mem_reg->pathname, "[vvar]", 6) == 0)
{
mem_reg->map_type = PROCMAPS_MAP_VVAR;
}
else if (strncmp(mem_reg->pathname, "[vsyscall]", 10) == 0)
{
mem_reg->map_type = PROCMAPS_MAP_VSYSCALL;
}
else if (strncmp(mem_reg->pathname, "[", 1) == 0)
{
mem_reg->map_type = PROCMAPS_MAP_OTHER;
}
else
{
// file backed mapping then
mem_reg->map_type = PROCMAPS_MAP_FILE;
// is the file deleted ?
// file_deleted
if (memcmp(mem_reg->pathname + strlen(mem_reg->pathname) - 9, "(deleted)", 9) == 0)
{
mem_reg->file_deleted = 1;
}
else
{
mem_reg->file_deleted = 0;
}
}
}