Skip to content

Commit

Permalink
Move RLang.list into RCore and use RCoreTableNew from debug/anal ##re…
Browse files Browse the repository at this point in the history
…factor
  • Loading branch information
trufae authored Nov 25, 2024
1 parent db9f1a2 commit a2e5c2c
Show file tree
Hide file tree
Showing 22 changed files with 177 additions and 150 deletions.
25 changes: 13 additions & 12 deletions libr/anal/meta.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ R_API const char *r_meta_type_tostring(int type) {
return "# unknown meta # ";
}

R_API void r_meta_print(RAnal *a, RAnalMetaItem *d, ut64 start, ut64 size, int rad, PJ *pj, bool show_full) {
R_API void r_meta_print(RAnal *a, RAnalMetaItem *d, ut64 start, ut64 size, int rad, PJ *pj, RTable *t, bool show_full) {
R_RETURN_IF_FAIL (!(rad == 'j' && !pj)); // rad == 'j' => pj
char *pstr, *base64_str;
RCore *core = a->coreb.core;
Expand Down Expand Up @@ -483,24 +483,25 @@ R_API void r_meta_print(RAnal *a, RAnalMetaItem *d, ut64 start, ut64 size, int r
}
}

R_API void r_meta_print_list_at(RAnal *a, ut64 addr, int rad, const char *tq) {
R_API void r_meta_print_list_at(RAnal *a, ut64 addr, int rad, const char *tq, RTable *t) {
RPVector *nodes = collect_nodes_at (a, R_META_TYPE_ANY, r_spaces_current (&a->meta_spaces), addr);
if (!nodes) {
return;
}
void **it;
r_pvector_foreach (nodes, it) {
RIntervalNode *node = *it;
r_meta_print (a, node->data, node->start, r_meta_node_size (node), rad, NULL, true);
r_meta_print (a, node->data, node->start, r_meta_node_size (node), rad, NULL, t, true);
}
r_pvector_free (nodes);
}

static void print_meta_list(RAnal *a, int type, int rad, ut64 addr, const char *tq) {
static void print_meta_list(RAnal *a, int type, int rad, ut64 addr, const char *tq, RTable *t) {
PJ *pj = NULL;
RTable *t = NULL;
if (rad == ',') {
t = r_table_new ("meta");
if (!t) {
t = r_table_new ("meta");
}
RTableColumnType *s = r_table_type ("string");
RTableColumnType *n = r_table_type ("number");
r_table_add_column (t, n, "addr", 0);
Expand Down Expand Up @@ -541,7 +542,7 @@ static void print_meta_list(RAnal *a, int type, int rad, ut64 addr, const char *
r_meta_node_size (node),
type, name);
} else {
r_meta_print (a, item, node->start, r_meta_node_size (node), rad, pj, true);
r_meta_print (a, item, node->start, r_meta_node_size (node), rad, pj, t, true);
}
}

Expand All @@ -556,7 +557,7 @@ static void print_meta_list(RAnal *a, int type, int rad, ut64 addr, const char *
if (!tq || !strstr (tq, "?")) {
if (t) {
char *s = r_table_tostring (t);
r_cons_printf ("%s\n", s);
r_cons_print (s);
free (s);
} else if (pj) {
pj_end (pj);
Expand All @@ -566,12 +567,12 @@ static void print_meta_list(RAnal *a, int type, int rad, ut64 addr, const char *
pj_free (pj);
}

R_API void r_meta_print_list_all(RAnal *a, int type, int rad, const char *tq) {
print_meta_list (a, type, rad, UT64_MAX, tq);
R_API void r_meta_print_list_all(RAnal *a, int type, int rad, const char *tq, RTable *t) {
print_meta_list (a, type, rad, UT64_MAX, tq, t);
}

R_API void r_meta_print_list_in_function(RAnal *a, int type, int rad, ut64 addr, const char *tq) {
print_meta_list (a, type, rad, addr, tq);
R_API void r_meta_print_list_in_function(RAnal *a, int type, int rad, ut64 addr, const char *tq, RTable *t) {
print_meta_list (a, type, rad, addr, tq, t);
}

R_API void r_meta_rebase(RAnal *anal, ut64 diff) {
Expand Down
14 changes: 8 additions & 6 deletions libr/anal/xrefs.c
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,10 @@ R_API bool r_anal_xrefs_has_xrefs_at(RAnal *anal, ut64 at) {
return !!entry;
}

static void r_anal_xrefs_list_table(RAnal *anal, RVecAnalRef *anal_refs, const char *arg) {
RTable *table = r_table_new ("xrefs");
static void r_anal_xrefs_list_table(RAnal *anal, RVecAnalRef *anal_refs, const char *arg, RTable *table) {
if (!table) {
table = r_table_new ("xrefs");
}
r_table_set_columnsf (table, "dddssss", "from", "to", "size", "type", "perm", "fromname", "toname");

RAnalRef *ref;
Expand All @@ -358,8 +360,8 @@ static void r_anal_xrefs_list_table(RAnal *anal, RVecAnalRef *anal_refs, const c
show_table = r_table_query (table, arg);
}
if (show_table) {
char *s = r_table_tofancystring (table);
r_cons_println (s);
char *s = r_table_tostring (table);
r_cons_print (s);
free (s);
}
r_table_free (table);
Expand Down Expand Up @@ -459,7 +461,7 @@ static void r_anal_xrefs_list_plaintext(RAnal *anal, RVecAnalRef *anal_refs) {
}
}

R_API void r_anal_xrefs_list(RAnal *anal, int rad, const char *arg) {
R_API void r_anal_xrefs_list(RAnal *anal, int rad, const char *arg, RTable *t) {
R_RETURN_IF_FAIL (anal && anal->rm);

RVecAnalRef *anal_refs = ref_manager_get_refs (anal->rm, UT64_MAX);
Expand All @@ -472,7 +474,7 @@ R_API void r_anal_xrefs_list(RAnal *anal, int rad, const char *arg) {

switch (rad) {
case ',':
r_anal_xrefs_list_table (anal, anal_refs, arg);
r_anal_xrefs_list_table (anal, anal_refs, arg, t);
break;
case 'j':
r_anal_xrefs_list_json (anal, anal_refs);
Expand Down
4 changes: 2 additions & 2 deletions libr/bin/bin.c
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,7 @@ static char *get_arch_string(const char *arch, int bits, RBinInfo *info) {
return r_strbuf_drain (sb);
}

R_API void r_bin_list_archs(RBin *bin, PJ *pj, int mode) {
R_API void r_bin_list_archs(RBin *bin, PJ *pj, RTable *t, int mode) {
R_RETURN_IF_FAIL (bin);

char unk[128];
Expand Down Expand Up @@ -1072,7 +1072,7 @@ R_API void r_bin_list_archs(RBin *bin, PJ *pj, int mode) {
if (!nbinfile) {
return;
}
RTable *table = r_table_new ("bins");
RTable *table = t? t: r_table_new ("bins");
const char *fmt = "dXnss";
if (mode == 'j') {
pj_ka (pj, "bins");
Expand Down
2 changes: 1 addition & 1 deletion libr/core/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ OBJS+=fortune.o hack.o vasm.o patch.o cbin.o corelog.o rtr.o cmd_api.o
OBJS+=carg.o canal.o project.o gdiff.o casm.o disasm.o cplugin.o cmd_print_list.o
OBJS+=vmenus.o vmenus_graph.o vmenus_zigns.o zdiff.o citem.o vslides.o
OBJS+=task.o panels.o pseudo.o vmarks.o anal_tp.o anal_objc.o blaze.o cundo.o
OBJS+=cproject.o
OBJS+=cproject.o clist.o

CFLAGS+=-DR2_PLUGIN_INCORE -I../../shlr
LDFLAGS+=${DL_LIBS}
Expand Down
54 changes: 54 additions & 0 deletions libr/core/clist.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* radare - LGPL - Copyright 2024 - pancake */

#include <r_core.h>

R_API void r_core_list_lang(RCore *core, int mode) {
RLang *lang = core->lang;
RListIter *iter;
RLangPlugin *h;
if (!lang) {
return;
}
PJ *pj = NULL;
RTable *table = NULL;
if (mode == 'j') {
pj = pj_new ();
pj_a (pj);
} else if (mode == ',') {
table = r_core_table_new (core, "langs");
RTableColumnType *typeString = r_table_type ("string");
r_table_add_column (table, typeString, "name", 0);
r_table_add_column (table, typeString, "desc", 0);
// r_table_add_column (table, typeString, "license", 0);
}
r_list_foreach (lang->langs, iter, h) {
const char *license = h->meta.license
? h->meta.license : "???";
if (mode == 'j') {
pj_o (pj);
r_lib_meta_pj (pj, &h->meta);
pj_end (pj);
} else if (mode == 'q') {
lang->cb_printf ("%s\n", h->meta.name);
} else if (mode == ',') {
r_table_add_row (table,
r_str_get (h->meta.name),
r_str_get (h->meta.license),
r_str_get (h->meta.desc), 0);
} else {
lang->cb_printf ("%-8s %6s %s\n",
h->meta.name, license, h->meta.desc);
}
}
if (pj) {
pj_end (pj);
char *s = pj_drain (pj);
lang->cb_printf ("%s\n", s);
free (s);
} else if (table) {
char *s = r_table_tostring (table);
lang->cb_printf ("%s", s);
free (s);
r_table_free (table);
}
}
10 changes: 7 additions & 3 deletions libr/core/cmd_anal.inc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,7 @@ static RCoreHelpMessage help_msg_ax = {
// "axg*", " [addr]", "show xrefs graph to given address, use .axg*;aggv",
// "axgj", " [addr]", "show xrefs graph to reach current function in json format",
"axi", " addr [at]", "add indirect code reference (see ax?)",
"axj", "", "add jmp reference", // list refs in json format",
"axj", "", "add jmp reference", // list refs in json format", // R2_600 XXX this is wrong. axj must be listing xrefs with json
"axl", "[jcq]", "list xrefs (axlc = count, axlq = quiet, axlj = json)",
"axm", " addr [at]", "copy data/code references pointing to addr to also point to curseek (or at)",
"axq", "", "list refs in quiet/human-readable format",
Expand Down Expand Up @@ -10424,7 +10424,10 @@ static bool cmd_anal_refs(RCore *core, const char *input) {
case 'q': // "axq"
case '*': // "ax*"
case ',': // "ax,"
r_anal_xrefs_list (core->anal, input[0], *input? r_str_trim_head_ro (input + 1): "");
{
RTable *table = (*input == ',')? r_core_table_new (core, "xrefs"): NULL;
r_anal_xrefs_list (core->anal, input[0], *input? r_str_trim_head_ro (input + 1): "", table);
}
break;
case '.':
if (input[1] == 'j') { // "ax.j"
Expand Down Expand Up @@ -10487,7 +10490,8 @@ static bool cmd_anal_refs(RCore *core, const char *input) {
r_core_cmd_help (core, help_msg_axl);
break;
case 'j': // "axlj"
r_anal_xrefs_list (core->anal, 'j', "");
// XXX axj != axlj r_core_cmd_call (core, "axj");
r_anal_xrefs_list (core->anal, 'j', 0, NULL);
break;
case 'c': // "axlc"
{
Expand Down
16 changes: 8 additions & 8 deletions libr/core/cmd_debug.inc.c
Original file line number Diff line number Diff line change
Expand Up @@ -5444,19 +5444,19 @@ static int cmd_debug(void *data, const char *input) {
// TODO: define ranges? to display only some traces, allow to scroll on this disasm? ~.. ?
switch (input[1]) {
case '\0': // "dt"
r_debug_trace_list (core->dbg, 0, core->offset);
break;
case '=': // "dt="
r_debug_trace_list (core->dbg, '=', core->offset);
break;
case 'q': // "dtq"
r_debug_trace_list (core->dbg, 'q', core->offset);
break;
case 'j': // "dtj"
r_debug_trace_list (core->dbg, 'j', core->offset);
r_debug_trace_list (core->dbg, input[1], core->offset, NULL);
break;
case '*': // "dt*"
r_debug_trace_list (core->dbg, 1, core->offset);
r_debug_trace_list (core->dbg, 1, core->offset, NULL);
break;
case ',': // "dt,"
{
RTable *t = r_core_table_new (core, "traces");
r_debug_trace_list (core->dbg, ',', core->offset, t);
}
break;
case ' ': // "dt [addr]"
if ((t = r_debug_trace_get (core->dbg,
Expand Down
8 changes: 4 additions & 4 deletions libr/core/cmd_eval.inc.c
Original file line number Diff line number Diff line change
Expand Up @@ -530,19 +530,19 @@ static bool cmd_ec(RCore *core, const char *input) {
r_str_argv_free (argv);
return false;
case '.':
r_meta_print_list_in_function (core->anal, R_META_TYPE_HIGHLIGHT, 0, core->offset, NULL);
r_meta_print_list_in_function (core->anal, R_META_TYPE_HIGHLIGHT, 0, core->offset, NULL, NULL);
r_str_argv_free (argv);
return false;
case '\0':
r_meta_print_list_all (core->anal, R_META_TYPE_HIGHLIGHT, 0, NULL);
r_meta_print_list_all (core->anal, R_META_TYPE_HIGHLIGHT, 0, NULL, NULL);
r_str_argv_free (argv);
return false;
case 'j':
r_meta_print_list_all (core->anal, R_META_TYPE_HIGHLIGHT, 'j', NULL);
r_meta_print_list_all (core->anal, R_META_TYPE_HIGHLIGHT, 'j', NULL, NULL);
r_str_argv_free (argv);
return false;
case '*':
r_meta_print_list_all (core->anal, R_META_TYPE_HIGHLIGHT, '*', NULL);
r_meta_print_list_all (core->anal, R_META_TYPE_HIGHLIGHT, '*', NULL, NULL);
r_str_argv_free (argv);
return false;
case ' ':
Expand Down
13 changes: 8 additions & 5 deletions libr/core/cmd_hash.inc.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2009-2023 - pancake, nibble */
/* radare - LGPL - Copyright 2009-2024 - pancake, nibble */

#if R_INCLUDE_BEGIN

Expand Down Expand Up @@ -99,18 +99,21 @@ static int cmd_hash_bang(RCore *core, const char *input) {
switch (mod) {
case 'j':
case 'q':
r_lang_list (core->lang, mod);
r_core_list_lang (core, mod);
break;
case '*':
r_lang_list (core->lang, 0);
r_core_list_lang (core, 0);
break;
default:
case '?':
R_LOG_INFO ("Missing halp");
break;
default:
r_core_return_invalid_command (core, "#!", av[0][0]);
break;
}
}
} else {
r_lang_list (core->lang, 0);
r_core_list_lang (core, 0);
}
r_str_argv_free (av);
return true;
Expand Down
6 changes: 4 additions & 2 deletions libr/core/cmd_info.inc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2168,11 +2168,13 @@ static int cmd_info(void *data, const char *input) {
if (r_str_startswith (input, "iaito")) {
R_LOG_ERROR ("Missing plugin. Run: r2pm -ci r2iaito");
} else if (input[1] == 'j') {
RTable *t = r_core_table_new (core, "archs");
pj_o (pj); // weird
r_bin_list_archs (core->bin, pj, 'j');
r_bin_list_archs (core->bin, pj, t, 'j');
pj_end (pj);
} else {
r_bin_list_archs (core->bin, NULL, 1);
RTable *t = r_core_table_new (core, "archs");
r_bin_list_archs (core->bin, NULL, t, 1);
}
break;
case 'e': // "ie"
Expand Down
8 changes: 4 additions & 4 deletions libr/core/cmd_log.inc.c
Original file line number Diff line number Diff line change
Expand Up @@ -581,15 +581,15 @@ static int cmd_plugins(void *data, const char *input) {
break;
case 'l': // "Ll"
if (input[1] == 'j') { // "Llj" "#!?j"
r_lang_list (core->lang, 'j');
r_core_list_lang (core, 'j');
} else if (input[1] == 'q') { // "Llq" "#!?q"
r_lang_list (core->lang, 'q');
r_core_list_lang (core, 'q');
} else if (input[1] == ',') { // "Ll,"
r_lang_list (core->lang, ','); // TODO: take table query as argument
r_core_list_lang (core, ',');
} else if (input[1] == '?') { // "Ll?"
r_cons_printf ("Usage: Ll[,jq] - list r_lang plugins\n");
} else {
r_lang_list (core->lang, 0);
r_core_list_lang (core, 0);
}
break;
case 'L': // "LL"
Expand Down
Loading

0 comments on commit a2e5c2c

Please sign in to comment.