File: map.c

package info (click to toggle)
radare2 0.9.6-3.1%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 17,496 kB
  • ctags: 45,959
  • sloc: ansic: 240,999; sh: 3,645; makefile: 2,520; python: 1,212; asm: 312; ruby: 214; awk: 209; perl: 188; lisp: 169; java: 23; xml: 17; php: 6
file content (144 lines) | stat: -rw-r--r-- 3,913 bytes parent folder | download | duplicates (2)
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
/* radare - LGPL - Copyright 2009-2013 - pancake */

#include <r_debug.h>
#include <r_list.h>

R_API void r_debug_map_list(RDebug *dbg, ut64 addr, int rad) {
	int notfirst = R_FALSE;
	RListIter *iter;
	RDebugMap *map;
	switch (rad) {
	case 'j':
		dbg->printf ("[");
		r_list_foreach (dbg->maps, iter, map) {
			if (notfirst) dbg->printf (",");
			dbg->printf ("{\"name\":\"%s\",",map->name);
			dbg->printf ("\"addr\":%"PFMT64d",", map->addr);
			dbg->printf ("\"addr_end\":%"PFMT64d",", map->addr_end);
			dbg->printf ("\"type\":\"%c\",", map->user?'u':'s');
			dbg->printf ("\"perm\":\"%s\"}", r_str_rwx_i (map->perm));
			notfirst = R_TRUE;
		}
		r_list_foreach (dbg->maps_user, iter, map) {
			if (notfirst) dbg->printf (",");
			dbg->printf ("{\"name\":\"%s\",",map->name);
			dbg->printf ("\"addr\":%"PFMT64d",", map->addr);
			dbg->printf ("\"addr_end\":%"PFMT64d",", map->addr_end);
			dbg->printf ("\"type\":\"%c\",", map->user?'u':'s');
			dbg->printf ("\"perm\":\"%s\"}", r_str_rwx_i (map->perm));
			notfirst = R_TRUE;
		}
		dbg->printf ("]\n");
		break;
	case '*':
		r_list_foreach (dbg->maps, iter, map) {
			dbg->printf ("f map.%s.%s 0x%08"PFMT64x" 0x%08"PFMT64x"\n",
				map->name, r_str_rwx_i (map->perm),
				map->addr_end - map->addr, map->addr);
		}
		r_list_foreach (dbg->maps_user, iter, map) {
			dbg->printf ("f map.%s.%s 0x%08"PFMT64x" 0x%08"PFMT64x"\n",
				map->name, r_str_rwx_i (map->perm),
				map->addr_end - map->addr, map->addr);
		}
		break;
	default:
		r_list_foreach (dbg->maps, iter, map) {
			dbg->printf ("sys 0x%08"PFMT64x" %c 0x%08"PFMT64x" %c %s %s\n",
				map->addr, (addr>=map->addr && addr<=map->addr_end)?'*':'-',
				map->addr_end, map->user?'u':'s', r_str_rwx_i (map->perm), map->name);
		}
		r_list_foreach (dbg->maps_user, iter, map) {
			dbg->printf ("usr 0x%08"PFMT64x" - 0x%08"PFMT64x" %c %x %s\n",
				map->addr, map->addr_end,
				map->user?'u':'s',
				map->perm, map->name);
		}
		break;
	}
}

R_API RDebugMap *r_debug_map_new (char *name, ut64 addr, ut64 addr_end, int perm, int user) {
	RDebugMap *map;
	if (name == NULL || addr >= addr_end) {
		eprintf ("r_debug_map_new: error assert(%"PFMT64x">=%"PFMT64x")\n", addr, addr_end);
		return NULL;
	}
	map = R_NEW (RDebugMap);
	if (map) {
		map->name = strdup (name);
		map->addr = addr;
		map->addr_end = addr_end;
		map->size = addr_end-addr;
		map->perm = perm;
		map->user = user;
	}
	return map;
}

R_API int r_debug_map_sync(RDebug *dbg) {
	int ret = R_FALSE;
	if (dbg->h && dbg->h->map_get) {
		RList *newmaps = dbg->h->map_get (dbg);
		if (newmaps) {
			// XXX free all non-user maps // but not unallocate!! only unlink from list
			r_debug_map_list_free (dbg->maps);
			dbg->maps = newmaps;
			ret = R_TRUE;
		}
	}
	return ret;
}

R_API RDebugMap* r_debug_map_alloc(RDebug *dbg, ut64 addr, int size) {
	RDebugMap *map = NULL;
	if (dbg->h && dbg->h->map_alloc) {
		map = dbg->h->map_alloc (dbg, addr, size);
	}
	return map;
}

R_API int r_debug_map_dealloc(RDebug *dbg, RDebugMap *map) {
	int ret = R_FALSE;
	ut64 addr = map->addr;
	if (dbg->h && dbg->h->map_dealloc) {
		if (dbg->h->map_dealloc (dbg, addr, map->size)) {
			ret = R_TRUE;
		}
	}
	return ret;
}

R_API RDebugMap *r_debug_map_get(RDebug *dbg, ut64 addr) {
	RDebugMap *map, *ret = NULL;
	RListIter *iter;
	r_list_foreach (dbg->maps, iter, map) {
		if (addr >= map->addr && addr <= map->addr_end) {
			ret = map;
			break;
		}
	}
	return ret;
}

R_API void r_debug_map_free(RDebugMap *map) {
	//r_list_unlink (dbg->maps_user, map);
	free (map->name);
	free (map);
}

R_API RList *r_debug_map_list_new() {
	RList *list = r_list_new ();
	list->free = (RListFree)r_debug_map_free;
	return list;
}

/* XXX Use r_list_destroy? FIXME: use correct maps->free function */
R_API void r_debug_map_list_free(RList *maps) {
	RListIter *iter;
	RDebugMap *map;
	r_list_foreach (maps, iter, map) {
		r_debug_map_free (map);
	}
	r_list_free (maps);
}