2016-04-13 12:48:17 +00:00
|
|
|
/* radare - LGPL - Copyright 2009-2016 - pancake */
|
2010-02-04 12:23:53 +00:00
|
|
|
|
|
|
|
#include <r_debug.h>
|
|
|
|
#include <r_list.h>
|
|
|
|
|
2011-10-09 17:45:34 +00:00
|
|
|
R_API void r_debug_map_list(RDebug *dbg, ut64 addr, int rad) {
|
2015-02-23 09:39:56 +00:00
|
|
|
const char *fmtstr;
|
|
|
|
char buf[128];
|
2015-10-19 00:41:21 +00:00
|
|
|
bool notfirst = false;
|
2012-02-14 17:19:16 +00:00
|
|
|
RListIter *iter;
|
|
|
|
RDebugMap *map;
|
2015-04-10 23:41:02 +00:00
|
|
|
if (!dbg) return;
|
|
|
|
|
2013-10-06 13:04:19 +00:00
|
|
|
switch (rad) {
|
|
|
|
case 'j':
|
2015-08-08 18:15:13 +00:00
|
|
|
dbg->cb_printf ("[");
|
2013-10-06 13:04:19 +00:00
|
|
|
r_list_foreach (dbg->maps, iter, map) {
|
2015-08-08 18:15:13 +00:00
|
|
|
if (notfirst) dbg->cb_printf (",");
|
|
|
|
dbg->cb_printf ("{\"name\":\"%s\",",map->name);
|
2015-10-19 00:41:21 +00:00
|
|
|
if (map->file && *map->file)
|
|
|
|
dbg->cb_printf ("\"file\":\"%s\",", map->file);
|
2015-08-08 18:15:13 +00:00
|
|
|
dbg->cb_printf ("\"addr\":%"PFMT64u",", map->addr);
|
|
|
|
dbg->cb_printf ("\"addr_end\":%"PFMT64u",", map->addr_end);
|
|
|
|
dbg->cb_printf ("\"type\":\"%c\",", map->user?'u':'s');
|
|
|
|
dbg->cb_printf ("\"perm\":\"%s\"}", r_str_rwx_i (map->perm));
|
2015-10-19 00:41:21 +00:00
|
|
|
notfirst = true;
|
2013-10-06 13:04:19 +00:00
|
|
|
}
|
|
|
|
r_list_foreach (dbg->maps_user, iter, map) {
|
2015-08-08 18:15:13 +00:00
|
|
|
if (notfirst) dbg->cb_printf (",");
|
2015-10-19 00:41:21 +00:00
|
|
|
dbg->cb_printf ("{\"name\":\"%s\",", map->name);
|
|
|
|
if (map->file && *map->file)
|
|
|
|
dbg->cb_printf ("\"file\":\"%s\",", map->file);
|
2015-08-08 18:15:13 +00:00
|
|
|
dbg->cb_printf ("\"addr\":%"PFMT64u",", map->addr);
|
|
|
|
dbg->cb_printf ("\"addr_end\":%"PFMT64u",", map->addr_end);
|
|
|
|
dbg->cb_printf ("\"type\":\"%c\",", map->user?'u':'s');
|
|
|
|
dbg->cb_printf ("\"perm\":\"%s\"}", r_str_rwx_i (map->perm));
|
2015-10-19 00:41:21 +00:00
|
|
|
notfirst = true;
|
2013-10-06 13:04:19 +00:00
|
|
|
}
|
2015-08-08 18:15:13 +00:00
|
|
|
dbg->cb_printf ("]\n");
|
2013-10-06 13:04:19 +00:00
|
|
|
break;
|
|
|
|
case '*':
|
2012-02-14 17:19:16 +00:00
|
|
|
r_list_foreach (dbg->maps, iter, map) {
|
2015-01-29 01:25:00 +00:00
|
|
|
char *name = r_str_newf ("%s.%s", map->name,
|
|
|
|
r_str_rwx_i (map->perm));
|
|
|
|
r_name_filter (name, 0);
|
2015-08-08 18:15:13 +00:00
|
|
|
dbg->cb_printf ("f map.%s 0x%08"PFMT64x" 0x%08"PFMT64x"\n",
|
2015-01-29 01:25:00 +00:00
|
|
|
name, map->addr_end - map->addr, map->addr);
|
|
|
|
free (name);
|
2011-10-09 17:45:34 +00:00
|
|
|
}
|
2012-02-14 17:19:16 +00:00
|
|
|
r_list_foreach (dbg->maps_user, iter, map) {
|
2015-01-29 01:25:00 +00:00
|
|
|
char *name = r_str_newf ("%s.%s", map->name,
|
|
|
|
r_str_rwx_i (map->perm));
|
|
|
|
r_name_filter (name, 0);
|
2015-08-08 18:15:13 +00:00
|
|
|
dbg->cb_printf ("f map.%s 0x%08"PFMT64x" 0x%08"PFMT64x"\n",
|
2015-01-29 01:25:00 +00:00
|
|
|
name, map->addr_end - map->addr, map->addr);
|
|
|
|
free (name);
|
2011-10-09 17:45:34 +00:00
|
|
|
}
|
2013-10-06 13:04:19 +00:00
|
|
|
break;
|
|
|
|
default:
|
2015-02-23 09:39:56 +00:00
|
|
|
fmtstr = dbg->bits& R_SYS_BITS_64?
|
2015-10-19 00:41:21 +00:00
|
|
|
"sys %04s 0x%016"PFMT64x" %c 0x%016"PFMT64x" %c %s %s %s\n":
|
|
|
|
"sys %04s 0x%08"PFMT64x" %c 0x%08"PFMT64x" %c %s %s %s\n";
|
2012-02-14 17:19:16 +00:00
|
|
|
r_list_foreach (dbg->maps, iter, map) {
|
2015-02-23 09:39:56 +00:00
|
|
|
r_num_units (buf, map->size);
|
2015-08-08 18:15:13 +00:00
|
|
|
dbg->cb_printf (fmtstr,
|
2015-02-23 09:39:56 +00:00
|
|
|
buf, map->addr, (addr>=map->addr && addr<map->addr_end)?'*':'-',
|
|
|
|
map->addr_end, map->user?'u':'s',
|
2015-10-19 00:41:21 +00:00
|
|
|
r_str_rwx_i (map->perm),
|
|
|
|
map->file?map->file:"?",
|
|
|
|
map->name);
|
2011-10-09 17:45:34 +00:00
|
|
|
}
|
2015-02-23 09:39:56 +00:00
|
|
|
fmtstr = dbg->bits& R_SYS_BITS_64?
|
2015-10-19 00:41:21 +00:00
|
|
|
"usr %04s 0x%016"PFMT64x" - 0x%016"PFMT64x" %c %x %s %s\n":
|
|
|
|
"usr %04s 0x%08"PFMT64x" - 0x%08"PFMT64x" %c %x %s %s\n";
|
2012-02-14 17:19:16 +00:00
|
|
|
r_list_foreach (dbg->maps_user, iter, map) {
|
2015-02-23 09:39:56 +00:00
|
|
|
r_num_units (buf, map->size);
|
2015-08-29 16:08:38 +00:00
|
|
|
dbg->cb_printf (fmtstr, buf, map->addr, map->addr_end,
|
2015-10-19 00:41:21 +00:00
|
|
|
map->user?'u':'s', (ut32)map->perm,
|
|
|
|
map->file?map->file:"?",
|
|
|
|
map->name);
|
2011-10-09 17:45:34 +00:00
|
|
|
}
|
2013-10-06 13:04:19 +00:00
|
|
|
break;
|
2010-02-04 12:23:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-06 15:30:07 +00:00
|
|
|
static void print_debug_map_ascii_art(RList *maps, ut64 addr, int use_color, PrintfCallback cb_printf, int bits, int cons_width) {
|
2015-10-13 15:18:20 +00:00
|
|
|
ut64 mul, min = -1, max = 0;
|
2016-06-06 15:30:07 +00:00
|
|
|
int width = cons_width - 80;
|
2015-10-13 15:18:20 +00:00
|
|
|
RListIter *iter;
|
|
|
|
RDebugMap *map;
|
|
|
|
if (width < 1) width = 30;
|
|
|
|
r_list_foreach (maps, iter, map) {
|
|
|
|
if (map->addr < min)
|
|
|
|
min = map->addr;
|
|
|
|
if (map->addr_end > max)
|
|
|
|
max = map->addr_end;
|
|
|
|
}
|
|
|
|
mul = (max - min) / width;
|
|
|
|
if (min != -1 && mul != 0) {
|
|
|
|
const char *c = "", *c_end = "";
|
|
|
|
char buf[56];
|
|
|
|
const char *fmtstr;
|
|
|
|
int j;
|
|
|
|
r_list_foreach (maps, iter, map) {
|
|
|
|
r_num_units (buf, map->size);
|
|
|
|
if (use_color) {
|
|
|
|
c_end = Color_RESET;
|
|
|
|
if (map->perm & 1) c = Color_GREEN;
|
|
|
|
if (map->perm & 2) c = Color_RED;
|
|
|
|
else { c = "" ; c_end = ""; }
|
|
|
|
} else {
|
|
|
|
c = "";
|
|
|
|
c_end = "";
|
|
|
|
}
|
|
|
|
fmtstr = bits & R_SYS_BITS_64 ?
|
|
|
|
"sys %04s %c %s0x%016"PFMT64x"%s |" :
|
|
|
|
"sys %04s %c %s0x%08"PFMT64x"%s |";
|
|
|
|
cb_printf (fmtstr, buf,
|
|
|
|
(addr >= map->addr && \
|
|
|
|
addr < map->addr_end) ? '*' : '-',
|
|
|
|
c, map->addr, c_end);
|
|
|
|
for (j = 0; j < width; j++) {
|
|
|
|
ut64 pos = min + (j * mul);
|
|
|
|
ut64 npos = min + ((j + 1) * mul);
|
|
|
|
if (map->addr < npos && map->addr_end > pos) {
|
|
|
|
cb_printf ("#");
|
|
|
|
} else {
|
|
|
|
cb_printf ("-");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmtstr = bits & R_SYS_BITS_64 ?
|
|
|
|
"| %s0x%016"PFMT64x"%s %s %s\n" :
|
|
|
|
"| %s0x%08"PFMT64x"%s %s %s\n";
|
|
|
|
cb_printf (fmtstr, c, map->addr_end, c_end,
|
|
|
|
r_str_rwx_i (map->perm), map->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-19 00:41:21 +00:00
|
|
|
|
2016-06-06 15:30:07 +00:00
|
|
|
R_API void r_debug_map_list_visual(RDebug *dbg, ut64 addr, int use_color, int cons_cols) {
|
2015-10-19 00:41:21 +00:00
|
|
|
if (dbg) {
|
|
|
|
if (dbg->maps) {
|
|
|
|
print_debug_map_ascii_art (dbg->maps, addr,
|
|
|
|
use_color, dbg->cb_printf,
|
2016-06-06 15:30:07 +00:00
|
|
|
dbg->bits, cons_cols);
|
2015-10-19 00:41:21 +00:00
|
|
|
}
|
|
|
|
if (dbg->maps_user) {
|
|
|
|
print_debug_map_ascii_art (dbg->maps_user,
|
|
|
|
addr, use_color,
|
2016-06-06 15:30:07 +00:00
|
|
|
dbg->cb_printf, dbg->bits, cons_cols);
|
2015-10-19 00:41:21 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-13 15:18:20 +00:00
|
|
|
}
|
|
|
|
|
2015-08-25 19:29:08 +00:00
|
|
|
R_API RDebugMap *r_debug_map_new(char *name, ut64 addr, ut64 addr_end, int perm, int user) {
|
2010-02-04 12:23:53 +00:00
|
|
|
RDebugMap *map;
|
2010-11-11 02:13:44 +00:00
|
|
|
if (name == NULL || addr >= addr_end) {
|
2015-08-25 19:29:08 +00:00
|
|
|
eprintf ("r_debug_map_new: error assert(\
|
|
|
|
%"PFMT64x">=%"PFMT64x")\n", addr, addr_end);
|
2010-02-04 12:23:53 +00:00
|
|
|
return NULL;
|
2010-11-11 02:13:44 +00:00
|
|
|
}
|
2015-10-19 00:41:21 +00:00
|
|
|
map = R_NEW0 (RDebugMap);
|
2015-08-25 19:29:08 +00:00
|
|
|
if (!map) return NULL;
|
|
|
|
map->name = strdup (name);
|
|
|
|
map->addr = addr;
|
|
|
|
map->addr_end = addr_end;
|
|
|
|
map->size = addr_end-addr;
|
|
|
|
map->perm = perm;
|
|
|
|
map->user = user;
|
2010-02-04 12:23:53 +00:00
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2015-08-23 23:27:29 +00:00
|
|
|
R_API RList *r_debug_modules_list(RDebug *dbg) {
|
2015-10-19 00:41:21 +00:00
|
|
|
return (dbg && dbg->h && dbg->h->modules_get)?
|
|
|
|
dbg->h->modules_get (dbg): NULL;
|
2015-08-23 23:27:29 +00:00
|
|
|
}
|
|
|
|
|
2010-02-04 12:23:53 +00:00
|
|
|
R_API int r_debug_map_sync(RDebug *dbg) {
|
2015-10-19 00:41:21 +00:00
|
|
|
bool ret = false;
|
2015-04-10 23:41:02 +00:00
|
|
|
if (dbg && dbg->h && dbg->h->map_get) {
|
2010-06-27 19:33:11 +00:00
|
|
|
RList *newmaps = dbg->h->map_get (dbg);
|
2010-02-04 12:23:53 +00:00
|
|
|
if (newmaps) {
|
2015-08-25 19:29:08 +00:00
|
|
|
r_list_free (dbg->maps);
|
2010-02-04 12:23:53 +00:00
|
|
|
dbg->maps = newmaps;
|
2015-10-19 00:41:21 +00:00
|
|
|
ret = true;
|
2010-02-04 12:23:53 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-19 00:41:21 +00:00
|
|
|
return (int)ret;
|
2010-02-04 12:23:53 +00:00
|
|
|
}
|
|
|
|
|
2013-07-21 01:05:56 +00:00
|
|
|
R_API RDebugMap* r_debug_map_alloc(RDebug *dbg, ut64 addr, int size) {
|
|
|
|
RDebugMap *map = NULL;
|
2015-04-10 23:41:02 +00:00
|
|
|
if (dbg && dbg->h && dbg->h->map_alloc) {
|
2013-07-21 01:05:56 +00:00
|
|
|
map = dbg->h->map_alloc (dbg, addr, size);
|
2010-02-04 12:23:53 +00:00
|
|
|
}
|
2013-07-21 01:05:56 +00:00
|
|
|
return map;
|
2010-02-04 12:23:53 +00:00
|
|
|
}
|
|
|
|
|
2010-03-02 10:18:49 +00:00
|
|
|
R_API int r_debug_map_dealloc(RDebug *dbg, RDebugMap *map) {
|
2015-10-19 00:41:21 +00:00
|
|
|
bool ret = false;
|
2010-02-04 12:23:53 +00:00
|
|
|
ut64 addr = map->addr;
|
2015-10-19 00:41:21 +00:00
|
|
|
if (dbg && dbg->h && dbg->h->map_dealloc)
|
|
|
|
if (dbg->h->map_dealloc (dbg, addr, map->size))
|
|
|
|
ret = true;
|
|
|
|
return (int)ret;
|
2010-02-04 12:23:53 +00:00
|
|
|
}
|
2010-03-02 10:18:49 +00:00
|
|
|
|
2010-02-04 12:23:53 +00:00
|
|
|
R_API RDebugMap *r_debug_map_get(RDebug *dbg, ut64 addr) {
|
2012-02-14 17:19:16 +00:00
|
|
|
RDebugMap *map, *ret = NULL;
|
|
|
|
RListIter *iter;
|
|
|
|
r_list_foreach (dbg->maps, iter, map) {
|
2010-02-04 12:23:53 +00:00
|
|
|
if (addr >= map->addr && addr <= map->addr_end) {
|
|
|
|
ret = map;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-03-02 10:18:49 +00:00
|
|
|
R_API void r_debug_map_free(RDebugMap *map) {
|
2010-02-04 12:23:53 +00:00
|
|
|
free (map->name);
|
|
|
|
free (map);
|
|
|
|
}
|
|
|
|
|
2010-03-02 10:18:49 +00:00
|
|
|
R_API RList *r_debug_map_list_new() {
|
2010-02-04 12:23:53 +00:00
|
|
|
RList *list = r_list_new ();
|
2015-08-25 19:29:08 +00:00
|
|
|
if (!list) return NULL;
|
2010-05-24 10:07:54 +00:00
|
|
|
list->free = (RListFree)r_debug_map_free;
|
2010-02-04 12:23:53 +00:00
|
|
|
return list;
|
|
|
|
}
|