Show source lines when asm.dwarf.lines set (#3511)

Co-authored-by: Sidhub723 <sidbhat.211ee151@nitk.edu.in>
This commit is contained in:
billow 2023-05-15 12:11:40 +08:00 committed by GitHub
parent 74c044ad63
commit c7b77ce8e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 117 additions and 31 deletions

View file

@ -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", "true", "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");

View file

@ -292,6 +292,7 @@ typedef struct {
int _tabsoff;
bool dwarfFile;
bool dwarfAbspath;
bool dwarfShowLines;
bool showpayloads;
bool showrelocs;
int cmtcount;
@ -721,6 +722,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");
@ -3812,39 +3814,40 @@ static void ds_align_comment(RzDisasmState *ds) {
}
static void ds_print_dwarf(RzDisasmState *ds) {
if (ds->show_dwarf) {
if (!ds->show_dwarf) {
return;
}
if (ds->dwarfShowLines) {
// TODO: cache value in ds
int dwarfFile = (int)ds->dwarfFile + (int)ds->dwarfAbspath;
free(ds->sl);
ds->sl = rz_bin_addr2text(ds->core->bin, ds->at, dwarfFile);
if (ds->sl) {
if ((!ds->osl || (ds->osl && strcmp(ds->sl, ds->osl)))) {
char *line = strdup(ds->sl);
if (!line) {
return;
}
rz_str_replace_char(line, '\t', ' ');
rz_str_replace_char(line, '\x1b', ' ');
rz_str_replace_char(line, '\r', ' ');
rz_str_replace_char(line, '\n', '\x00');
rz_str_trim(line);
if (!*line) {
free(line);
return;
}
// handle_set_pre (ds, " ");
ds_align_comment(ds);
if (ds->show_color) {
rz_cons_printf("%s; %s" Color_RESET, ds->pal_comment, line);
} else {
rz_cons_printf("; %s", line);
}
free(ds->osl);
ds->osl = ds->sl;
ds->sl = NULL;
free(line);
}
if (RZ_STR_ISEMPTY(ds->sl))
return;
if (ds->osl && !(ds->osl && strcmp(ds->sl, ds->osl)))
return;
char *line = strdup(ds->sl);
rz_str_replace_char(line, '\t', ' ');
rz_str_replace_char(line, '\x1b', ' ');
rz_str_replace_char(line, '\r', ' ');
rz_str_replace_char(line, '\n', '\x00');
rz_str_trim(line);
if (RZ_STR_ISEMPTY(line)) {
free(line);
return;
}
// handle_set_pre (ds, " ");
ds_align_comment(ds);
if (ds->show_color) {
rz_cons_printf("%s; %s" Color_RESET, ds->pal_comment, line);
} else {
rz_cons_printf("; %s", line);
}
free(ds->osl);
ds->osl = ds->sl;
ds->sl = NULL;
free(line);
}
}
@ -5534,7 +5537,6 @@ toro:
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);
@ -5556,7 +5558,6 @@ toro:
}
ds_show_comments_describe(ds);
}
f = fcnIn(ds, ds->addr, 0);
ds_begin_line(ds);
ds_print_labels(ds, f);
@ -5576,7 +5577,6 @@ toro:
}
}
}
////
int mi_type;
bool mi_found = ds_print_meta_infos(ds, buf, len, idx, &mi_type);
if (ds->asm_hint_pos == 0) {

View file

@ -0,0 +1,85 @@
NAME="dwarf: lines"
FILE=bins/elf/ada_test_dwarf
CMDS=<<EOF
aaa
s main
e asm.dwarf=false
e asm.dwarf.lines=false
pdf
e asm.dwarf=true
e asm.dwarf.lines=true
pdf
EOF
EXPECT=<<EOF
; DATA XREF from entry0 @ 0x2271
;-- main:
/ int dbg.main (int argc, char **argv, char **envp);
| ; arg int argc @ rdi
| ; arg char **argv @ rsi
| ; arg char **envp @ rdx
| ; var char **var_30h @ stack - 0x30
| ; var char **var_28h @ stack - 0x28
| ; var int var_1ch @ stack - 0x1c
| ; var system__address ensure_reference @ stack - 0x18
| ; var struct ada_main__main__seh___PAD seh @ stack - 0x10
| 0x00002742 push rbp ; integer main(const integer argc,void * const argv,void * const envp);
| 0x00002743 mov rbp, rsp
| 0x00002746 sub rsp, 0x30
| 0x0000274a mov dword [var_1ch], edi ; argc
| 0x0000274d mov qword [var_28h], rsi ; argv
| 0x00002751 mov qword [var_30h], rdx ; envp
| 0x00002755 lea rax, obj.__gnat_ada_main_program_name ; 0x3038 ; "_ada_ada_test"
| 0x0000275c mov qword [ensure_reference], rax
| 0x00002760 mov eax, dword [var_1ch]
| 0x00002763 mov dword [obj.gnat_argc], eax ; [0x51c0:4]=0
| 0x00002769 mov rax, qword [var_28h]
| 0x0000276d mov qword [obj.gnat_argv], rax ; [0x5128:8]=0
| 0x00002774 mov rax, qword [var_30h]
| 0x00002778 mov qword [obj.gnat_envp], rax ; [0x5090:8]=0
| 0x0000277f lea rax, [seh.F]
| 0x00002783 mov rdi, rax
| 0x00002786 call sym.imp.__gnat_initialize
| 0x0000278b call dbg.adainit
| 0x00002790 call dbg._ada_ada_test
| 0x00002795 call dbg.adafinal
| 0x0000279a call sym.imp.__gnat_finalize
| 0x0000279f mov eax, dword [obj.gnat_exit_status] ; [0x50f0:4]=0
| 0x000027a5 leave
\ 0x000027a6 ret
; DATA XREF from entry0 @ 0x2271
;-- main:
/ int dbg.main (int argc, char **argv, char **envp);
| ; arg int argc @ rdi
| ; arg char **argv @ rsi
| ; arg char **envp @ rdx
| ; var char **var_30h @ stack - 0x30
| ; var char **var_28h @ stack - 0x28
| ; var int var_1ch @ stack - 0x1c
| ; var system__address ensure_reference @ stack - 0x18
| ; var struct ada_main__main__seh___PAD seh @ stack - 0x10
| 0x00002742 push rbp ; b~ada_test.adb:207 ; integer main(const integer argc,void * const argv,void * const envp);
| 0x00002743 mov rbp, rsp
| 0x00002746 sub rsp, 0x30
| 0x0000274a mov dword [var_1ch], edi ; argc
| 0x0000274d mov qword [var_28h], rsi ; argv
| 0x00002751 mov qword [var_30h], rdx ; envp
| 0x00002755 lea rax, obj.__gnat_ada_main_program_name ; b~ada_test.adb:220 ; 0x3038 ; "_ada_ada_test"
| 0x0000275c mov qword [ensure_reference], rax
| 0x00002760 mov eax, dword [var_1ch] ; b~ada_test.adb:224
| 0x00002763 mov dword [obj.gnat_argc], eax ; [0x51c0:4]=0
| 0x00002769 mov rax, qword [var_28h] ; b~ada_test.adb:225
| 0x0000276d mov qword [obj.gnat_argv], rax ; [0x5128:8]=0
| 0x00002774 mov rax, qword [var_30h] ; b~ada_test.adb:226
| 0x00002778 mov qword [obj.gnat_envp], rax ; [0x5090:8]=0
| 0x0000277f lea rax, [seh.F] ; b~ada_test.adb:228
| 0x00002783 mov rdi, rax
| 0x00002786 call sym.imp.__gnat_initialize
| 0x0000278b call dbg.adainit ; b~ada_test.adb:229
| 0x00002790 call dbg._ada_ada_test ; b~ada_test.adb:230
| 0x00002795 call dbg.adafinal ; b~ada_test.adb:231
| 0x0000279a call sym.imp.__gnat_finalize ; b~ada_test.adb:232
| 0x0000279f mov eax, dword [obj.gnat_exit_status] ; b~ada_test.adb:233 ; [0x50f0:4]=0
| 0x000027a5 leave ; b~ada_test.adb:234
\ 0x000027a6 ret
EOF
RUN