forked from WuBingzheng/memleax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addr_maps.c
66 lines (57 loc) · 1.26 KB
/
addr_maps.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
/*
* address maps of libraries
*
* Author: Wu Bingzheng
* Date: 2016-5
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "addr_maps.h"
#include "proc_info.h"
#include "array.h"
struct addr_map_s {
uintptr_t start, end;
char name[100];
};
static ARRAY(g_addr_maps, struct addr_map_s, 50);
static void addr_maps_build_file(const char *path, size_t start, size_t end)
{
struct addr_map_s *am = array_push(&g_addr_maps);
am->start = start;
am->end = end;
const char *p = strrchr(path, '/');
const char *q = strstr(p, ".so");
if (q == NULL) {
strncpy(am->name, p + 1, 100);
am->name[99] = '\0';
} else {
memcpy(am->name, p + 1, q - p + 2);
am->name[q - p + 2] = '\0';
}
}
void addr_maps_build(pid_t pid)
{
const char *path;
size_t start, end;
while ((path = proc_maps(pid, &start, &end, NULL)) != NULL) {
addr_maps_build_file(path, start, end);
}
}
const char *addr_maps_search(uintptr_t address)
{
int min = 0, max = g_addr_maps.item_num - 1;
struct addr_map_s *maps = g_addr_maps.data;
while (min <= max) {
int mid = (min + max) / 2;
struct addr_map_s *am = &maps[mid];
if (address < am->start) {
max = mid - 1;
} else if (address > am->end) {
min = mid + 1;
} else {
return am->name;
}
}
return "??";
}