Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dwarf sourceline info #3449

Closed
wants to merge 9 commits into from
1 change: 1 addition & 0 deletions librz/core/cconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -3032,6 +3032,7 @@ RZ_API int rz_core_config_init(RzCore *core) {
SETBPREF("asm.dwarf", "false", "Show dwarf comment at disassembly");
SETBPREF("asm.dwarf.abspath", "false", "Show absolute path in asm.dwarf");
SETBPREF("asm.dwarf.file", "true", "Show filename of asm.dwarf in pd");
SETBPREF("asm.dwarf.lines", "false", "Show DWARF source line information at disassembly");
SETBPREF("asm.esil", "false", "Show ESIL instead of mnemonic");
SETBPREF("asm.nodup", "false", "Do not show dupped instructions (collapse disasm)");
SETBPREF("asm.emu", "false", "Run ESIL emulation analysis on disasm");
Expand Down
57 changes: 50 additions & 7 deletions librz/core/disasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ typedef struct {
int _tabsoff;
bool dwarfFile;
bool dwarfAbspath;
bool dwarfShowLines;
bool showpayloads;
bool showrelocs;
int cmtcount;
Expand Down Expand Up @@ -349,7 +350,8 @@ static void ds_print_sysregs(RzDisasmState *ds);
static void ds_print_fcn_name(RzDisasmState *ds);
static void ds_print_as_string(RzDisasmState *ds);
static bool ds_print_core_vmode(RzDisasmState *ds, int pos);
static void ds_print_dwarf(RzDisasmState *ds);
// static void ds_print_dwarf(RzDisasmState *ds);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove

static void ds_print_dwarf(RzCore *core, RzCmdStateOutput *state, RzDisasmState *ds);
static void ds_print_asmop_payload(RzDisasmState *ds, const ut8 *buf);
static char *ds_esc_str(RzDisasmState *ds, const char *str, int len, const char **prefix_out, bool is_comment);
static void ds_print_ptr(RzDisasmState *ds, int len, int idx);
Expand Down Expand Up @@ -721,6 +723,7 @@ static RzDisasmState *ds_init(RzCore *core) {
ds->show_dwarf = rz_config_get_b(core->config, "asm.dwarf");
ds->dwarfFile = rz_config_get_b(ds->core->config, "asm.dwarf.file");
ds->dwarfAbspath = rz_config_get_b(ds->core->config, "asm.dwarf.abspath");
ds->dwarfShowLines = rz_config_get_b(ds->core->config, "asm.dwarf.lines");
ds->show_lines_call = ds->show_lines ? rz_config_get_b(core->config, "asm.lines.call") : false;
ds->show_lines_ret = ds->show_lines ? rz_config_get_b(core->config, "asm.lines.ret") : false;
ds->show_size = rz_config_get_b(core->config, "asm.size");
Expand Down Expand Up @@ -3811,7 +3814,50 @@ static void ds_align_comment(RzDisasmState *ds) {
rz_cons_print(" ");
}

static void ds_print_dwarf(RzDisasmState *ds) {
static void ds_print_dwarf(RzCore *core, RzCmdStateOutput *state, RzDisasmState *ds) {

bool binFileExists = true;
bool SourceLineInfoExists = true;

RzBinFile *binfile = core->bin->cur;
if (!binfile || !binfile->o) {
// rz_cons_printf("No file loaded.\n");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove

binFileExists = false;
// return false;
}
RzBinSourceLineInfo *li = binfile->o->lines;
if (!li) {
// rz_cons_printf("No Source line information available");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

SourceLineInfoExists = false;
// return true;
}
if (ds->dwarfShowLines && ds->show_dwarf && SourceLineInfoExists && binFileExists) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary empty line

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can extract that part into a separate static function for clarity

rz_cmd_state_output_array_start(state);
rz_cons_break_push(NULL, NULL);
RzBinSourceLineSample *temps = NULL;
for (size_t i = 0; i < li->samples_count; i++) {
if (rz_cons_is_breaked()) {
break;
}
temps = &li->samples[i];

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

ds_align_comment(ds);
if (ds->vat == temps->address) {
// rz_cons_printf(/*"0x%08" PFMT64x */ "\t%s\t"/*temps->address,*/,temps->file ? temps->file : "-");
rz_cons_printf("\tLine number%s", temps->file ? temps->file : "-");

if (temps->line) {
rz_cons_printf("%" PFMT32u "\n", temps->line);
} else {
rz_cons_print("-\n");
}
}
}
rz_cons_break_pop();
rz_cmd_state_output_array_end(state);
}

if (ds->show_dwarf) {
// TODO: cache value in ds
int dwarfFile = (int)ds->dwarfFile + (int)ds->dwarfAbspath;
Expand Down Expand Up @@ -5534,7 +5580,6 @@ RZ_API int rz_core_print_disasm(RZ_NONNULL RzCore *core, ut64 addr, RZ_NONNULL u
ds_adistrick_comments(ds);
/* XXX: This is really cpu consuming.. need to be fixed */
ds_show_functions(ds);

if (ds->show_comments && !ds->show_comment_right) {
ds_show_refs(ds);
ds_build_op_str(ds, false);
Expand All @@ -5556,7 +5601,6 @@ RZ_API int rz_core_print_disasm(RZ_NONNULL RzCore *core, ut64 addr, RZ_NONNULL u
}
ds_show_comments_describe(ds);
}

f = fcnIn(ds, ds->addr, 0);
ds_begin_line(ds);
ds_print_labels(ds, f);
Expand All @@ -5576,7 +5620,6 @@ RZ_API int rz_core_print_disasm(RZ_NONNULL RzCore *core, ut64 addr, RZ_NONNULL u
}
}
}
////
int mi_type;
bool mi_found = ds_print_meta_infos(ds, buf, len, idx, &mi_type);
if (ds->asm_hint_pos == 0) {
Expand All @@ -5592,7 +5635,7 @@ RZ_API int rz_core_print_disasm(RZ_NONNULL RzCore *core, ut64 addr, RZ_NONNULL u
ds_print_family(ds);
ds_print_stackptr(ds);
if (mi_found) {
ds_print_dwarf(ds);
ds_print_dwarf(core, state, ds);
ret = ds_print_middle(ds, ret);

ds_print_asmop_payload(ds, buf + addrbytes * idx);
Expand Down Expand Up @@ -5632,7 +5675,7 @@ RZ_API int rz_core_print_disasm(RZ_NONNULL RzCore *core, ut64 addr, RZ_NONNULL u
ds_build_op_str(ds, true);
ds_print_opstr(ds);
ds_end_line_highlight(ds);
ds_print_dwarf(ds);
ds_print_dwarf(core, state, ds);
ret = ds_print_middle(ds, ret);

ds_print_asmop_payload(ds, buf + addrbytes * idx);
Expand Down