Skip to content

Commit

Permalink
Merge CL and iX into ix
Browse files Browse the repository at this point in the history
  • Loading branch information
thestr4ng3r committed Mar 25, 2021
1 parent 4a76530 commit 653ca25
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 108 deletions.
72 changes: 8 additions & 64 deletions librz/core/cbin.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ static RZ_NULLABLE RZ_BORROW const RzList *core_bin_strings(RzCore *r, RzBinFile
static void _print_strings(RzCore *r, const RzList *list, PJ *pj, int mode, int va);
static bool bin_raw_strings(RzCore *r, PJ *pj, int mode, int va);
static bool bin_dwarf(RzCore *core, RzBinFile *binfile, PJ *pj, int mode);
static int bin_source(RzCore *r, PJ *pj, int mode);
static int bin_entry(RzCore *r, PJ *pj, int mode, ut64 laddr, int va, bool inifin);
static int bin_sections(RzCore *r, PJ *pj, int mode, ut64 laddr, int va, ut64 at, const char *name, const char *chksum, bool print_segments);
static int bin_map_sections_to_segments(RzBin *bin, PJ *pj, int mode);
Expand Down Expand Up @@ -1164,14 +1163,14 @@ static bool bin_dwarf(RzCore *core, RzBinFile *binfile, PJ *pj, int mode) {
return false;
}
if (!IS_MODE_SET(mode)) {
rz_core_bin_print_source_line_info(core, li, mode, pj);
rz_core_bin_print_source_line_info(core, li, IS_MODE_JSON(mode) ? RZ_OUTPUT_MODE_JSON : RZ_OUTPUT_MODE_STANDARD, pj);
}
return true;
}

RZ_API void rz_core_bin_print_source_line_sample(RzCore *core, const RzBinSourceLineSample *s, int mode, PJ *pj) {
rz_return_if_fail(core && s && (!IS_MODE_JSON(mode) || pj));
if (IS_MODE_JSON(mode)) {
RZ_API void rz_core_bin_print_source_line_sample(RzCore *core, const RzBinSourceLineSample *s, RzOutputMode mode, PJ *pj) {
rz_return_if_fail(core && s && (mode != RZ_OUTPUT_MODE_JSON || pj));
if (mode == RZ_OUTPUT_MODE_JSON) {
bool chopPath = !rz_config_get_i(core->config, "dir.dwarf.abspath");
char *file = s->file ? strdup(s->file) : NULL;
if (chopPath && file) {
Expand Down Expand Up @@ -1202,9 +1201,9 @@ RZ_API void rz_core_bin_print_source_line_sample(RzCore *core, const RzBinSource
}
}

RZ_API void rz_core_bin_print_source_line_info(RzCore *core, const RzBinSourceLineInfo *li, int mode, PJ *pj) {
rz_return_if_fail(li && (!IS_MODE_JSON(mode) || pj));
if (IS_MODE_JSON(mode)) {
RZ_API void rz_core_bin_print_source_line_info(RzCore *core, const RzBinSourceLineInfo *li, RzOutputMode mode, PJ *pj) {
rz_return_if_fail(li && (mode != RZ_OUTPUT_MODE_JSON || pj));
if (mode == RZ_OUTPUT_MODE_JSON) {
pj_a(pj);
}
rz_cons_break_push(NULL, NULL);
Expand All @@ -1215,7 +1214,7 @@ RZ_API void rz_core_bin_print_source_line_info(RzCore *core, const RzBinSourceLi
rz_core_bin_print_source_line_sample(core, &li->samples[i], mode, pj);
}
rz_cons_break_pop();
if (IS_MODE_JSON(mode)) {
if (mode == RZ_OUTPUT_MODE_JSON) {
pj_end(pj);
}
}
Expand Down Expand Up @@ -1273,58 +1272,6 @@ RZ_API bool rz_core_pdb_info(RzCore *core, const char *file, PJ *pj, int mode) {
return true;
}

static bool source_file_collect_cb(void *user, const void *k, const void *v) {
RzPVector *r = user;
char *f = strdup(k);
if (f) {
rz_pvector_push(r, f);
}
return true;
}

static int bin_source(RzCore *r, PJ *pj, int mode) {
RzList *final_list = rz_list_new();
RzBinFile *binfile = r->bin->cur;
if (!binfile || !binfile->o) {
bprintf("[Error bin file]\n");
rz_list_free(final_list);
return false;
}
RzBinSourceLineInfo *li = binfile->o->lines;
if (!li) {
rz_cons_printf("No source info available.\n");
return true;
}

// collect all filenames uniquely
HtPP *files = ht_pp_new0();
if (!files) {
return false;
}
for (size_t i = 0; i < li->samples_count; i++) {
RzBinSourceLineSample *s = &li->samples[i];
if (!s->line || !s->file) {
continue;
}
ht_pp_insert(files, s->file, NULL);
}
// sort them alphabetically
RzPVector sorter;
rz_pvector_init(&sorter, free);
ht_pp_foreach(files, source_file_collect_cb, &sorter);
rz_pvector_sort(&sorter, (RzPVectorComparator)strcmp);
ht_pp_free(files);
// print them!
rz_cons_printf("[Source file]\n");
void **it;
rz_pvector_foreach (&sorter, it) {
const char *file = *it;
rz_cons_printf("%s\n", file);
}
rz_pvector_fini(&sorter);
return true;
}

static int bin_main(RzCore *r, RzBinFile *binfile, PJ *pj, int mode, int va) {
if (!binfile) {
return false;
Expand Down Expand Up @@ -4237,9 +4184,6 @@ RZ_API int rz_core_bin_info(RzCore *core, int action, PJ *pj, int mode, int va,
if ((action & RZ_CORE_BIN_ACC_PDB)) {
ret &= rz_core_pdb_info(core, core->bin->file, pj, mode);
}
if ((action & RZ_CORE_BIN_ACC_SOURCE)) {
ret &= bin_source(core, pj, mode);
}
if ((action & RZ_CORE_BIN_ACC_ENTRIES)) {
ret &= bin_entry(core, pj, mode, loadaddr, va, false);
}
Expand Down
130 changes: 126 additions & 4 deletions librz/core/cmd_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static const char *help_msg_i[] = {
"iT", "", "File signature",
"iV", "", "Display file version info",
"iw", "", "try/catch blocks",
"iX", "", "Display source files used (via dwarf)",
"ix", "[.fj?]", "Display source file line info (from debug info)",
"iz|izj", "", "Strings in data sections (in JSON/Base64)",
"izz", "", "Search for Strings in the whole binary",
"izzz", "", "Dump Strings from whole binary to rizin shell (for huge files)",
Expand All @@ -78,6 +78,14 @@ static const char *help_msg_id[] = {
NULL
};

static const char *help_msg_ix[] = {
"Usage: ix", "", "Display source file line info (from debug info)",
"ix[j]", "", "List all source line info available",
"ix.[j]", "", "Show source line info for current address",
"ixf[j]", "", "Show summary of all source files used",
NULL
};

#define PAIR_WIDTH 9
// TODO: reuse implementation in core/bin.c
static void pair(const char *a, const char *b) {
Expand Down Expand Up @@ -347,6 +355,92 @@ static bool isKnownPackage(const char *cn) {
return false;
}

static bool source_file_collect_cb(void *user, const void *k, const void *v) {
RzPVector *r = user;
char *f = strdup(k);
if (f) {
rz_pvector_push(r, f);
}
return true;
}

typedef enum {
PRINT_SOURCE_INFO_LINES_ALL,
PRINT_SOURCE_INFO_LINES_HERE,
PRINT_SOURCE_INFO_FILES
} PrintSourceInfoType;

static bool print_source_info(RzCore *core, PrintSourceInfoType type, RzOutputMode mode) {
RzBinFile *binfile = core->bin->cur;
if (!binfile || !binfile->o) {
rz_cons_printf("No file loaded.\n");
return false;
}
RzBinSourceLineInfo *li = binfile->o->lines;
if (!li) {
rz_cons_printf("No source info available.\n");
return true;
}
PJ *j = NULL;
if (mode == RZ_OUTPUT_MODE_JSON) {
j = pj_new();
}
switch (type) {
case PRINT_SOURCE_INFO_FILES: {
// collect all filenames uniquely
HtPP *files = ht_pp_new0();
if (!files) {
return false;
}
for (size_t i = 0; i < li->samples_count; i++) {
RzBinSourceLineSample *s = &li->samples[i];
if (!s->line || !s->file) {
continue;
}
ht_pp_insert(files, s->file, NULL);
}
// sort them alphabetically
RzPVector sorter;
rz_pvector_init(&sorter, free);
ht_pp_foreach(files, source_file_collect_cb, &sorter);
rz_pvector_sort(&sorter, (RzPVectorComparator)strcmp);
ht_pp_free(files);
// print them!
if (mode == RZ_OUTPUT_MODE_JSON) {
pj_a(j);
void **it;
rz_pvector_foreach (&sorter, it) {
pj_s(j, *it);
}
pj_end(j);
} else {
rz_cons_printf("[Source file]\n");
void **it;
rz_pvector_foreach (&sorter, it) {
const char *file = *it;
rz_cons_printf("%s\n", file);
}
}
rz_pvector_fini(&sorter);
break;
}
case PRINT_SOURCE_INFO_LINES_ALL:
rz_core_bin_print_source_line_info(core, li, mode, j);
break;
case PRINT_SOURCE_INFO_LINES_HERE:
for (const RzBinSourceLineSample *s = rz_bin_source_line_info_get_first_at(li, core->offset);
s; s = rz_bin_source_line_info_get_next(li, s)) {
rz_core_bin_print_source_line_sample(core, s, mode, j);
}
break;
}
if (mode == RZ_OUTPUT_MODE_JSON) {
rz_cons_println(pj_string(j));
pj_free(j);
}
return true;
}

RZ_IPI int rz_cmd_info(void *data, const char *input) {
RzCore *core = (RzCore *)data;
bool newline = rz_cons_is_interactive();
Expand Down Expand Up @@ -697,8 +791,36 @@ RZ_IPI int rz_cmd_info(void *data, const char *input) {
case 'r': // "ir"
RZBININFO("relocs", RZ_CORE_BIN_ACC_RELOCS, NULL);
break;
case 'X': // "iX"
RZBININFO("source", RZ_CORE_BIN_ACC_SOURCE, NULL);
case 'x':
newline = false;
switch (*++input) {
case '\0': // "ix"
case ' ':
print_source_info(core, PRINT_SOURCE_INFO_LINES_ALL, RZ_OUTPUT_MODE_STANDARD);
break;
case 'j': // "ixj"
print_source_info(core, PRINT_SOURCE_INFO_LINES_ALL, RZ_OUTPUT_MODE_JSON);
break;
case '.':
if (*++input == 'j') { // "ix.j"
print_source_info(core, PRINT_SOURCE_INFO_LINES_HERE, RZ_OUTPUT_MODE_JSON);
} else { // "ix."
print_source_info(core, PRINT_SOURCE_INFO_LINES_HERE, RZ_OUTPUT_MODE_STANDARD);
}
break;
case 'f':
if (*++input == 'j') { // "ixfj"
print_source_info(core, PRINT_SOURCE_INFO_FILES, RZ_OUTPUT_MODE_JSON);
} else { // "ixf"
print_source_info(core, PRINT_SOURCE_INFO_FILES, RZ_OUTPUT_MODE_STANDARD);
}
break;
case '?': // "ix?"
default:
rz_core_cmd_help(core, help_msg_ix);
input++;
break;
}
break;
case 'd': // "id"
if (input[1] == 'p') { // "idp"
Expand Down Expand Up @@ -1110,7 +1232,7 @@ RZ_IPI int rz_cmd_info(void *data, const char *input) {
break;
}
// input can be overwritten like the 'input = " ";' a few lines above
if (input[0] != ' ') {
if (input[0] && input[0] != ' ') {
input++;
if ((*input == 'j' || *input == 'q') && (input[0] && !input[1])) {
break;
Expand Down
25 changes: 0 additions & 25 deletions librz/core/cmd_meta.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ static const char *help_msg_C[] = {
"CCa", "[+-] [addr] [text]", "add/remove comment at given address",
"CCu", " [comment-text] [@addr]", "add unique comment",
"CF", "[sz] [fcn-sign..] [@addr]", "function signature",
"CL", "[.]", "show source line information (bininfo)",
"CS", "[-][space]", "manage meta-spaces to filter comments, etc..",
"C[Cthsdmf]", "", "list comments/types/hidden/strings/data/magic/formatted in human friendly form",
"C[Cthsdmf]*", "", "list comments/types/hidden/strings/data/magic/formatted in rizin commands",
Expand Down Expand Up @@ -142,27 +141,6 @@ RZ_IPI void rz_core_meta_comment_add(RzCore *core, const char *comment, ut64 add
}
}

static int cmd_meta_lineinfo(RzCore *core, const char *input) {
rz_return_val_if_fail(core && core->bin, 0);
if (!core->bin->cur || !core->bin->cur->o || !core->bin->cur->o->lines) {
return 0;
}
RzBinSourceLineInfo *li = core->bin->cur->o->lines;
if (*input == '?') {
eprintf("Usage: CL[.?]\n");
return 0;
}
if (*input == '.') {
for (const RzBinSourceLineSample *s = rz_bin_source_line_info_get_first_at(li, core->offset);
s; s = rz_bin_source_line_info_get_next(li, s)) {
rz_core_bin_print_source_line_sample(core, s, 0, NULL);
}
} else {
rz_core_bin_print_source_line_info(core, li, 0, NULL);
}
return 0;
}

static int cmd_meta_comment(RzCore *core, const char *input) {
ut64 addr = core->offset;
switch (input[1]) {
Expand Down Expand Up @@ -836,9 +814,6 @@ RZ_IPI int rz_cmd_meta(void *data, const char *input) {
rz_meta_print_list_at(core->analysis, core->offset, 0);
break;
}
case 'L': // "CL"
cmd_meta_lineinfo(core, input + 1);
break;
case 'C': // "CC"
cmd_meta_comment(core, input);
break;
Expand Down
5 changes: 2 additions & 3 deletions librz/include/rz_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -710,8 +710,8 @@ RZ_API int rz_core_bin_list(RzCore *core, int mode);
RZ_API bool rz_core_bin_delete(RzCore *core, ut32 binfile_idx);
RZ_API ut64 rz_core_bin_impaddr(RzBin *bin, int va, const char *name);

RZ_API void rz_core_bin_print_source_line_sample(RzCore *core, const RzBinSourceLineSample *s, int mode, PJ *pj);
RZ_API void rz_core_bin_print_source_line_info(RzCore *core, const RzBinSourceLineInfo *li, int mode, PJ *pj);
RZ_API void rz_core_bin_print_source_line_sample(RzCore *core, const RzBinSourceLineSample *s, RzOutputMode mode, PJ *pj);
RZ_API void rz_core_bin_print_source_line_info(RzCore *core, const RzBinSourceLineInfo *li, RzOutputMode mode, PJ *pj);

// bin_dwarf
RZ_API void rz_core_bin_dwarf_print_abbrev_section(const RzBinDwarfDebugAbbrev *da);
Expand Down Expand Up @@ -759,7 +759,6 @@ RZ_API void rz_core_recover_vars(RzCore *core, RzAnalysisFunction *fcn, bool arg
#define RZ_CORE_BIN_ACC_RESOURCES 0x100000
#define RZ_CORE_BIN_ACC_INITFINI 0x200000
#define RZ_CORE_BIN_ACC_SEGMENTS 0x400000
#define RZ_CORE_BIN_ACC_SOURCE 0x800000
#define RZ_CORE_BIN_ACC_HASHES 0x10000000
#define RZ_CORE_BIN_ACC_TRYCATCH 0x20000000
#define RZ_CORE_BIN_ACC_SECTIONS_MAPPING 0x40000000
Expand Down
2 changes: 1 addition & 1 deletion test/db/cmd/cmd_i
Original file line number Diff line number Diff line change
Expand Up @@ -4251,7 +4251,7 @@ RUN

NAME=iX
FILE=bins/elf/analysis/dwarf_load
CMDS=iX
CMDS=ixf
EXPECT=<<EOF
[Source file]
/home/landley/work/ab7/build/temp-armv6l/gcc-core/gcc/config/arm/ieee754-df.S
Expand Down
2 changes: 1 addition & 1 deletion test/db/cmd/dwarf
Original file line number Diff line number Diff line change
Expand Up @@ -1405,7 +1405,7 @@ RUN

NAME="Mach-O dSYM lines (armv7)"
FILE=bins/mach0/TestRTTI-armv7-dSYM
CMDS=CL.@0x0000a24e
CMDS=ix.@0x0000a24e
EXPECT=<<EOF
0x0000a24e /Users/ftamagni/src/TestRTTI/TestRTTI//TestRTTI.cpp 18
EOF
Expand Down
8 changes: 4 additions & 4 deletions test/db/formats/dex
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,13 @@ RUN
NAME=DEX HelloWorld.dex debug info CL
FILE=bins/dex/HelloWorld.dex
CMDS=<<EOF
CL
ix
?e --
CL.@0x000002f4
ix.@0x000002f4
?e --
CL.@0x00000318
ix.@0x00000318
?e --
CL~?
ix~?
EOF
EXPECT=<<EOF
0x00000238 Hello.java 4
Expand Down
Loading

0 comments on commit 653ca25

Please sign in to comment.