Skip to content

Commit

Permalink
Integrate table API for p-h (#15083) ##table
Browse files Browse the repository at this point in the history
* Added UTF8 and Curvy Support For Table API
* Added show footer with sum support
* Integrate star= command with table API
  • Loading branch information
deepakchethan authored and radare committed Sep 24, 2019
1 parent b263363 commit 9d7a72f
Show file tree
Hide file tree
Showing 13 changed files with 229 additions and 241 deletions.
7 changes: 5 additions & 2 deletions libr/core/canal.c
Original file line number Diff line number Diff line change
Expand Up @@ -3106,7 +3106,7 @@ static int fcn_list_detail(RCore *core, RList *fcns) {
static int fcn_list_table(RCore *core, const char *q, int fmt) {
RAnalFunction *fcn;
RListIter *iter;
RTable *t = r_table_new ();
RTable *t = r_core_table (core);
RTableColumnType *typeString = r_table_type ("string");
RTableColumnType *typeNumber = r_table_type ("number");
r_table_add_column (t, typeNumber, "addr", 0);
Expand Down Expand Up @@ -3215,8 +3215,11 @@ R_API int r_core_anal_fcn_list(RCore *core, const char *input, const char *rad)
}
r_list_append (flist, info);
}
r_core_visual_list (core, flist, core->offset, core->blocksize,
RTable *table = r_core_table (core);
r_table_visual_list (table, flist, core->offset, core->blocksize,
r_cons_get_size (NULL), r_config_get_i (core->config, "scr.color"));
r_cons_printf ("\n%s\n", r_table_tostring (table));
r_table_free (table);
r_list_free (flist);
break;
}
Expand Down
5 changes: 4 additions & 1 deletion libr/core/cbin.c
Original file line number Diff line number Diff line change
Expand Up @@ -2473,7 +2473,10 @@ static int bin_sections(RCore *r, int mode, ut64 laddr, int va, ut64 at, const c
RListInfo *info = r_listinfo_new (s->name, pitv, vitv, s->perm, strdup (humansz));
r_list_append (list, info);
}
r_core_visual_list (r, list, r->offset, -1, cols, r->print->flags & R_PRINT_FLAGS_COLOR);
RTable *table = r_core_table (r);
r_table_visual_list (table, list, r->offset, -1, cols, r->io->va);
r_cons_printf ("\n%s\n", r_table_tostring (table));
r_table_free (table);
r_list_free (list);
goto out;
}
Expand Down
5 changes: 4 additions & 1 deletion libr/core/cmd_anal.c
Original file line number Diff line number Diff line change
Expand Up @@ -1963,8 +1963,11 @@ static bool anal_fcn_list_bb(RCore *core, const char *input, bool one) {
}
r_list_append (flist, info);
}
r_core_visual_list (core, flist, core->offset, core->blocksize,
RTable *table = r_core_table (core);
r_table_visual_list (table, flist, core->offset, core->blocksize,
r_cons_get_size (NULL), r_config_get_i (core->config, "scr.color"));
r_cons_printf ("\n%s\n", r_table_tostring (table));
r_table_free (table);
r_list_free (flist);
return true;
}
Expand Down
10 changes: 8 additions & 2 deletions libr/core/cmd_open.c
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,11 @@ static void cmd_open_bin(RCore *core, const char *input) {
}
r_list_append (list, info);
}
r_core_visual_list (core, list, core->offset, core->blocksize,
RTable *table = r_core_table (core);
r_table_visual_list (table, list, core->offset, core->blocksize,
r_cons_get_size (NULL), r_config_get_i (core->config, "scr.color"));
r_cons_printf ("\n%s\n", r_table_tostring (table));
r_table_free (table);
r_list_free (list);
} break;
case '?': // "ob?"
Expand Down Expand Up @@ -833,8 +836,11 @@ static void cmd_open_map(RCore *core, const char *input) {
}
r_list_append (list, info);
}
r_core_visual_list (core, list, core->offset, core->blocksize,
RTable *table = r_core_table (core);
r_table_visual_list (table, list, core->offset, core->blocksize,
r_cons_get_size (NULL), r_config_get_i (core->config, "scr.color"));
r_cons_printf ("\n%s\n", r_table_tostring (table));
r_table_free (table);
r_list_free (list);
} break;
default:
Expand Down
33 changes: 11 additions & 22 deletions libr/core/cmd_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -3117,6 +3117,8 @@ static int cmd_print_blocks(RCore *core, const char *input) {
ut64 off = core->offset;
ut64 from = UT64_MAX;
ut64 to = 0;
RTable *t = r_core_table (core);
t->showSum = true;
RList *list = r_core_get_boundaries_prot (core, -1, NULL, "search");
if (!list) {
return 1;
Expand Down Expand Up @@ -3154,10 +3156,9 @@ static int cmd_print_blocks(RCore *core, const char *input) {
pj_k (pj, "blocks");
pj_a (pj);
break;
case 'h': // "p-h"
r_cons_printf (".-------------.----------------------------.\n");
r_cons_printf ("| offset | flags funcs cmts syms str |\n");
r_cons_printf ("|-------------)----------------------------|\n");
case 'h': { // "p-h"
r_table_set_columnsf (t, "sddddd", "offset", "flags", "funcs", "cmts", "syms", "str");
}
break;
case 'e':
default:
Expand All @@ -3167,7 +3168,6 @@ static int cmd_print_blocks(RCore *core, const char *input) {
bool use_color = r_config_get_i (core->config, "scr.color");
int len = 0;
int i;
RCoreAnalStatsItem total = {0};
for (i = 0; i < ((to - from) / piece); i++) {
ut64 at = from + (piece * i);
ut64 ate = at + piece;
Expand Down Expand Up @@ -3209,22 +3209,13 @@ static int cmd_print_blocks(RCore *core, const char *input) {
len++;
break;
case 'h':
total.flags += as->block[p].flags;
total.functions += as->block[p].functions;
total.comments += as->block[p].comments;
total.symbols += as->block[p].symbols;
total.strings += as->block[p].strings;
if ((as->block[p].flags)
|| (as->block[p].functions)
|| (as->block[p].comments)
|| (as->block[p].symbols)
|| (as->block[p].strings)) {
r_cons_printf ("| 0x%09"PFMT64x " | %4d %4d %4d %4d %4d |\n", at,
as->block[p].flags,
as->block[p].functions,
as->block[p].comments,
as->block[p].symbols,
as->block[p].strings);
r_table_add_rowf (t, "sddddd", sdb_fmt ("0x%09"PFMT64x"", at), as->block[p].flags,
as->block[p].functions, as->block[p].comments, as->block[p].symbols, as->block[p].strings);
}
break;
case 'e': // p-e
Expand Down Expand Up @@ -3273,13 +3264,11 @@ static int cmd_print_blocks(RCore *core, const char *input) {
r_cons_println (pj_string (pj));
pj_free (pj);
break;
case 'h':
// r_cons_printf (" total | flags funcs cmts syms str |\n");
r_cons_printf ("|-------------)----------------------------|\n");
r_cons_printf ("| total | %4d %4d %4d %4d %4d |\n",
total.flags, total.functions, total.comments, total.symbols, total.strings);
r_cons_printf ("`-------------'----------------------------'\n");
case 'h': {
r_cons_printf ("\n%s\n", r_table_tofancystring (t));
r_table_free (t);
break;
}
case 'e':
default:
if (use_color) {
Expand Down
8 changes: 8 additions & 0 deletions libr/core/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -3742,3 +3742,11 @@ R_API bool r_core_autocomplete_remove(RCoreAutocomplete *parent, const char* cmd
}
return false;
}

R_API RTable *r_core_table(RCore *core) {
RTable *table = r_table_new ();
if (table) {
table->cons = core->cons;
}
return table;
}
91 changes: 0 additions & 91 deletions libr/core/visual.c
Original file line number Diff line number Diff line change
Expand Up @@ -4364,94 +4364,3 @@ R_API void r_listinfo_free (RListInfo *info) {
}
R_FREE (info);
}

// TODO: move this to the table api
R_API void r_core_visual_list(RCore *core, RList *list, ut64 seek, ut64 len, int width, int use_color) {
ut64 mul, min = -1, max = -1;
RListIter *iter;
RListInfo *info;
int j, i;
RIO *io = core->io;
width -= 80;
if (width < 1) {
width = 30;
}

r_list_foreach (list, iter, info) {
if (min == -1 || info->pitv.addr < min) {
min = info->pitv.addr;
}
if (max == -1 || info->pitv.addr + info->pitv.size > max) {
max = info->pitv.addr + info->pitv.size;
}
}
mul = (max - min) / width;
if (min != -1 && mul > 0) {
const char * color = "", *color_end = "";
i = 0;
r_list_foreach (list, iter, info) {
if (use_color && info->perm != -1) {
color_end = Color_RESET;
if ((info->perm & R_PERM_X) && (info->perm & R_PERM_W)) { // exec & write bits
color = r_cons_singleton ()->context->pal.graph_trufae;
} else if ((info->perm & R_PERM_X)) { // exec bit
color = r_cons_singleton ()->context->pal.graph_true;
} else if ((info->perm & R_PERM_W)) { // write bit
color = r_cons_singleton ()->context->pal.graph_false;
} else {
color = "";
color_end = "";
}
} else {
color = "";
color_end = "";
}
if (io->va) {
io->cb_printf ("%03d%c %s0x%08"PFMT64x"%s |", i,
r_itv_contain (info->vitv, seek) ? '*' : ' ',
color, info->vitv.addr, color_end);
} else {
io->cb_printf ("%03d%c %s0x%08"PFMT64x"%s |", i,
r_itv_contain (info->pitv, seek) ? '*' : ' ',
color, info->pitv.addr, color_end);
}
for (j = 0; j < width; j++) {
ut64 pos = min + j * mul;
ut64 npos = min + (j + 1) * mul;
if (info->pitv.addr < npos && (info->pitv.addr + info->pitv.size) > pos) {
io->cb_printf ("#");
} else {
io->cb_printf ("-");
}
}
if (io->va) {
io->cb_printf ("| %s0x%08"PFMT64x"%s %s %6s %s\n",
color, r_itv_end (info->vitv), color_end,
(info->perm != -1)? r_str_rwx_i (info->perm) : " ",
(info->extra)?info->extra : " ",
(info->name)?info->name : " ");
} else {
io->cb_printf ("| %s0x%08"PFMT64x"%s %s %6s %s\n",
color, r_itv_end (info->pitv), color_end,
(info->perm != -1)? r_str_rwx_i (info->perm) : " ",
(info->extra)?info->extra : " ",
(info->name)?info->name : "");
}
i++;
}
/* current seek */
if (i > 0 && len != 0) {
if (seek == UT64_MAX) {
seek = 0;
}
io->cb_printf ("=> 0x%08"PFMT64x" |", seek);
for (j = 0; j < width; j++) {
io->cb_printf (
((j * mul) + min >= seek &&
(j * mul) + min <= seek+len)
?"^" : "-");
}
io->cb_printf ("| 0x%08"PFMT64x"\n", seek+len);
}
}
}
106 changes: 7 additions & 99 deletions libr/debug/trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,110 +103,13 @@ R_API RDebugTracepoint *r_debug_trace_get (RDebug *dbg, ut64 addr) {
return NULL;
}

typedef struct {
char *name;
RInterval pitv;
RInterval vitv;
int perm;
char *extra;
} RListInfo;

static int cmpaddr (const void *_a, const void *_b) {
const RListInfo *a = _a, *b = _b;
return (r_itv_begin (a->pitv) > r_itv_begin (b->pitv))? 1:
(r_itv_begin (a->pitv) < r_itv_begin (b->pitv))? -1: 0;
}

// Copy from visual to avoid circular dependency
void visual_list(RDebug *dbg, RList *list, ut64 seek, ut64 len, int width, int use_color) {
ut64 mul, min = -1, max = -1;
RListIter *iter;
RListInfo *info;
int j, i;
RIO *io = dbg->iob.io;
width -= 80;
if (width < 1) {
width = 30;
}

r_list_foreach (list, iter, info) {
if (min == -1 || info->pitv.addr < min) {
min = info->pitv.addr;
}
if (max == -1 || info->pitv.addr + info->pitv.size > max) {
max = info->pitv.addr + info->pitv.size;
}
}
mul = (max - min) / width;
if (min != -1 && mul > 0) {
const char * color = "", *color_end = "";
i = 0;
r_list_foreach (list, iter, info) {
if (use_color && info->perm != -1) {
color_end = Color_RESET;
if ((info->perm & R_PERM_X) && (info->perm & R_PERM_W)) { // exec & write bits
color = r_cons_singleton ()->context->pal.graph_trufae;
} else if ((info->perm & R_PERM_X)) { // exec bit
color = r_cons_singleton ()->context->pal.graph_true;
} else if ((info->perm & R_PERM_W)) { // write bit
color = r_cons_singleton ()->context->pal.graph_false;
} else {
color = "";
color_end = "";
}
} else {
color = "";
color_end = "";
}
if (io->va) {
io->cb_printf ("%05d%c %s0x%08"PFMT64x"%s |", i,
r_itv_contain (info->vitv, seek) ? '*' : ' ',
color, info->vitv.addr, color_end);
} else {
io->cb_printf ("%05d%c %s0x%08"PFMT64x"%s |", i,
r_itv_contain (info->pitv, seek) ? '*' : ' ',
color, info->pitv.addr, color_end);
}
for (j = 0; j < width; j++) {
ut64 pos = min + j * mul;
ut64 npos = min + (j + 1) * mul;
if (info->pitv.addr < npos && (info->pitv.addr + info->pitv.size) > pos) {
io->cb_printf ("#");
} else {
io->cb_printf ("-");
}
}
if (io->va) {
io->cb_printf ("| %s0x%08"PFMT64x"%s %s %6s %s\n",
color, r_itv_end (info->vitv), color_end,
(info->perm != -1)? r_str_rwx_i (info->perm) : " ",
(info->extra)?info->extra : " ",
(info->name)?info->name : " ");
} else {
io->cb_printf ("| %s0x%08"PFMT64x"%s %s %6s %s\n",
color, r_itv_end (info->pitv), color_end,
(info->perm != -1)? r_str_rwx_i (info->perm) : " ",
(info->extra)?info->extra : " ",
(info->name)?info->name : "");
}
i++;
}
/* current seek */
if (i > 0 && len != 0) {
if (seek == UT64_MAX) {
seek = 0;
}
io->cb_printf ("=> 0x%08"PFMT64x" |", seek);
for (j = 0; j < width; j++) {
io->cb_printf (
((j * mul) + min >= seek &&
(j * mul) + min <= seek+len)
?"^" : "-");
}
io->cb_printf ("| 0x%08"PFMT64x"\n", seek+len);
}
}
}

R_API void r_debug_trace_list (RDebug *dbg, int mode, ut64 offset) {
int tag = dbg->trace->tag;
Expand Down Expand Up @@ -249,8 +152,13 @@ R_API void r_debug_trace_list (RDebug *dbg, int mode, ut64 offset) {
}
if (flag) {
r_list_sort (info_list, cmpaddr);
visual_list (dbg, info_list, offset, 1,
r_cons_get_size (NULL), false);
RTable *table = r_table_new ();
table->cons = r_cons_singleton();
RIO *io = dbg->iob.io;
r_table_visual_list (table, info_list, offset, 1,
r_cons_get_size (NULL), io->va);
io->cb_printf ("\n%s\n", r_table_tostring (table));
r_table_free (table);
r_list_free (info_list);
}
}
Expand Down
Loading

0 comments on commit 9d7a72f

Please sign in to comment.