From 1e5a3a15bd108c0d671b2b40b730af7ce8d6ca84 Mon Sep 17 00:00:00 2001 From: pancake Date: Wed, 8 Jan 2020 14:10:47 +0100 Subject: [PATCH] Fix issues in iCj, drlj and arlj ##json --- libr/bin/bin.c | 60 +++++++++++++++++++++++++++++-------------- libr/core/cbin.c | 6 ++++- libr/core/cmd_anal.c | 15 ++++++++++- libr/core/cmd_debug.c | 52 +++++++++++++++++-------------------- libr/core/cmd_info.c | 13 ++++------ 5 files changed, 88 insertions(+), 58 deletions(-) diff --git a/libr/bin/bin.c b/libr/bin/bin.c index fa317ad738..a7835afbf9 100644 --- a/libr/bin/bin.c +++ b/libr/bin/bin.c @@ -1009,19 +1009,23 @@ R_API void r_bin_list_archs(RBin *bin, int mode) { } Sdb *binfile_sdb = binfile? binfile->sdb: NULL; if (!binfile_sdb) { - eprintf ("Cannot find SDB!\n"); + // eprintf ("Cannot find SDB!\n"); return; } if (!binfile) { - eprintf ("Binary format not currently loaded!\n"); + // eprintf ("Binary format not currently loaded!\n"); return; } sdb_unset (binfile_sdb, ARCHS_KEY, 0); + PJ *pj = pj_new (); + pj_o (pj); if (mode == 'j') { - bin->cb_printf ("\"bins\":["); + pj_k (pj, "bins"); + pj_a (pj); } RBinFile *nbinfile = r_bin_file_find_by_name_n (bin, name, i); if (!nbinfile) { + pj_free (pj); return; } i = -1; @@ -1047,11 +1051,15 @@ R_API void r_bin_list_archs(RBin *bin, int mode) { bin->cb_printf ("%s\n", arch); break; case 'j': - bin->cb_printf ("%s{\"arch\":\"%s\",\"bits\":%d," - "\"offset\":%" PFMT64u ",\"size\":%" PFMT64u "," - "\"machine\":\"%s\"}", - i? ",": "", arch, bits, - boffset, obj_size, machine); + pj_o (pj); + pj_ks (pj, "arch", arch); + pj_ki (pj, "bits", bits); + pj_kn (pj, "offset", boffset); + pj_kn (pj, "size", obj_size); + if (machine) { + pj_ks (pj, "machine", machine); + } + pj_end (pj); break; default: r_table_add_rowf (table, "nXnss", i, boffset, obj_size, sdb_fmt ("%s_%i", arch, bits), machine); @@ -1069,11 +1077,15 @@ R_API void r_bin_list_archs(RBin *bin, int mode) { bin->cb_printf ("%s\n", arch); break; case 'j': - bin->cb_printf ("%s{\"arch\":\"%s\",\"bits\":%d," - "\"offset\":%" PFMT64u ",\"size\":%" PFMT64u "," - "\"machine\":\"%s\"}", - i? ",": "", arch, bits, - boffset, obj_size, machine); + pj_o (pj); + pj_ks (pj, "arch", arch); + pj_ki (pj, "bits", bits); + pj_kn (pj, "offset", boffset); + pj_kn (pj, "size", obj_size); + if (machine) { + pj_ks (pj, "machine", machine); + } + pj_end (pj); break; default: r_table_add_rowf (table, "nsnss", i, sdb_fmt ("0x%08" PFMT64x , boffset), obj_size, sdb_fmt("%s_%i", arch, bits), ""); @@ -1088,11 +1100,15 @@ R_API void r_bin_list_archs(RBin *bin, int mode) { bin->cb_printf ("%s\n", arch); break; case 'j': - bin->cb_printf ("%s{\"arch\":\"unk_%d\",\"bits\":%d," - "\"offset\":%" PFMT64u ",\"size\":%" PFMT64u "," - "\"machine\":\"%s\"}", - i? ",": "", i, bits, - boffset, obj_size, machine); + pj_o (pj); + pj_ks (pj, "arch", arch); + pj_ki (pj, "bits", bits); + pj_kn (pj, "offset", boffset); + pj_kn (pj, "size", obj_size); + if (machine) { + pj_ks (pj, "machine", machine); + } + pj_end (pj); break; default: r_table_add_rowf (table, "nsnss", i, sdb_fmt ("0x%08" PFMT64x , boffset), obj_size, "", ""); @@ -1107,8 +1123,14 @@ R_API void r_bin_list_archs(RBin *bin, int mode) { //sdb_array_push (binfile_sdb, ARCHS_KEY, archline, 0); } if (mode == 'j') { - bin->cb_printf ("]"); + pj_end (pj); + pj_end (pj); + const char *s = pj_string (pj); + if (s) { + bin->cb_printf ("%s\n", s); + } } + pj_free (pj); r_table_free (table); } diff --git a/libr/core/cbin.c b/libr/core/cbin.c index b5cade32da..15078dfeb5 100644 --- a/libr/core/cbin.c +++ b/libr/core/cbin.c @@ -3740,7 +3740,11 @@ static int bin_signature(RCore *r, int mode) { RBinPlugin *plg = r_bin_file_cur_plugin (cur); if (plg && plg->signature) { const char *signature = plg->signature (cur, IS_MODE_JSON (mode)); - r_cons_println (signature); + if (IS_MODE_JSON (mode)) { + r_cons_printf ("{\"signature\":%s}", signature); + } else { + r_cons_println (signature); + } free ((char*) signature); return true; } diff --git a/libr/core/cmd_anal.c b/libr/core/cmd_anal.c index 3882fd2713..2a550228d9 100644 --- a/libr/core/cmd_anal.c +++ b/libr/core/cmd_anal.c @@ -3984,13 +3984,26 @@ void cmd_anal_reg(RCore *core, const char *str) { switch (str[0]) { case 'l': // "arl" { + const bool use_json = str[1] == 'j'; RRegSet *rs = r_reg_regset_get (core->anal->reg, R_REG_TYPE_GPR); if (rs) { RRegItem *r; RListIter *iter; + PJ *pj = pj_new (); + pj_a (pj); r_list_foreach (rs->regs, iter, r) { - r_cons_println (r->name); + if (use_json) { + pj_s (pj, r->name); + } else { + r_cons_println (r->name); + } } + if (use_json) { + pj_end (pj); + const char *s = pj_string (pj); + r_cons_println (s); + } + pj_free (pj); } } break; case '0': // "ar0" diff --git a/libr/core/cmd_debug.c b/libr/core/cmd_debug.c index c15e90e3fd..b74e2be902 100644 --- a/libr/core/cmd_debug.c +++ b/libr/core/cmd_debug.c @@ -2408,36 +2408,30 @@ static void cmd_debug_reg(RCore *core, const char *str) { break; case 'l': // "drl[j]" { - bool json_out = false; - switch (str[1]) { - case 'j': - json_out = true; - /* fall trhu */ - case 0: - { - RRegSet *rs = r_reg_regset_get (core->dbg->reg, R_REG_TYPE_GPR); - if (rs) { - RRegItem *r; - RListIter *iter; - i = 0; - if (json_out) { - r_cons_printf ("["); - } - r_list_foreach (rs->regs, iter, r) { - if (json_out) { - r_cons_printf ("%s\"%s\"", - (i ? "," : ""), - r->name); - i++; - } else { - r_cons_println (r->name); - } - } - if (json_out) { - r_cons_printf ("]"); - } + const bool json_out = str[1] == 'j'; + RRegSet *rs = r_reg_regset_get (core->dbg->reg, R_REG_TYPE_GPR); + if (rs) { + RRegItem *r; + RListIter *iter; + i = 0; + PJ *pj = NULL; + if (json_out) { + pj = pj_new (); + pj_a (pj); + } + r_list_foreach (rs->regs, iter, r) { + if (json_out) { + pj_s (pj, r->name); + i++; + } else { + r_cons_println (r->name); } - break; + } + if (json_out) { + pj_end (pj); + const char *s = pj_string (pj); + r_cons_println (s); + pj_free (pj); } } } diff --git a/libr/core/cmd_info.c b/libr/core/cmd_info.c index de4821d775..b344c9ee70 100644 --- a/libr/core/cmd_info.c +++ b/libr/core/cmd_info.c @@ -23,7 +23,7 @@ static const char *help_msg_i[] = { "icg", "", "List classes as agn/age commands to create class hirearchy graphs", "icq", "", "List classes, in quiet mode (just the classname)", "icqq", "", "List classes, in quieter mode (only show non-system classnames)", - "iC", "", "Show signature info (entitlements, ...)", + "iC", "[j]", "Show signature info (entitlements, ...)", "id", "[?]", "Debug information (source lines)", "idp", "", "Load pdb file information", "iD", " lang sym", "demangle symbolname for given language", @@ -583,12 +583,9 @@ static int cmd_info(void *data, const char *input) { r_core_bin_info (core, x, mode, va, NULL, y); case 'A': // "iA" newline = false; - if (input[1] == 'j') { - r_cons_printf ("{"); - r_bin_list_archs (core->bin, 'j'); - r_cons_printf ("}\n"); - } else { - r_bin_list_archs (core->bin, 1); + { + int mode = (input[1] == 'j')? 'j': 1; + r_bin_list_archs (core->bin, mode); } break; case 'E': // "iE" @@ -953,7 +950,7 @@ static int cmd_info(void *data, const char *input) { RBININFO ("versioninfo", R_CORE_BIN_ACC_VERSIONINFO, NULL, 0); break; case 'T': // "iT" - case 'C': // "iC" // rabin2 -C create + case 'C': // "iC" // rabin2 -C create // should be deprecated and just use iT (or find a better name) RBININFO ("signature", R_CORE_BIN_ACC_SIGNATURE, NULL, 0); break; case 'z': // "iz"