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

core/disasm: remove static variables #4214

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions librz/core/cmd/cmd_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,20 @@ static const ut32 colormap[256] = {
0xffffff,
};

static inline char *get_section_name(RzCore *core, ut64 offset) {
char *csection = rz_core_get_section_name(core, offset);
if (RZ_STR_ISEMPTY(csection)) {
free(csection);
return rz_str_dup("unknown");
}
rz_str_trim(csection);
if (RZ_STR_ISEMPTY(csection)) {
free(csection);
return rz_str_dup("unknown");
}
return csection;
}

static void colordump(RzCore *core, const ut8 *block, int len) {
const char *chars = " .,:;!O@#";
bool square = rz_config_get_i(core->config, "scr.square");
Expand All @@ -574,8 +588,9 @@ static void colordump(RzCore *core, const ut8 *block, int len) {
}
for (i = 0; i < len; i += cols) {
if (show_section) {
const char *name = rz_core_get_section_name(core, core->offset + i);
char *name = get_section_name(core, core->offset + i);
rz_cons_printf("%20s ", name ? name : "");
free(name);
}
if (show_offset) {
rz_print_addr(core->print, core->offset + i);
Expand Down Expand Up @@ -1403,10 +1418,11 @@ static void annotated_hexdump(RzCore *core, int len) {
append(ebytes, core->cons->context->pal.offset);
}
if (showSection) {
const char *name = rz_core_get_section_name(core, ea);
char *name = get_section_name(core, ea);
char *s = rz_str_newf("%20s ", name);
append(ebytes, s);
free(s);
free(name);
}
ebytes += sprintf(ebytes, "0x%08" PFMT64x, ea);
if (usecolor) {
Expand Down Expand Up @@ -2305,23 +2321,8 @@ static inline int cmd_pxb_k(const ut8 *buffer, int x) {
return buffer[3 - x] << (8 * x);
}

static inline char *get_section_name(RzCore *core) {
const char *csection = rz_core_get_section_name(core, core->offset);
if (RZ_STR_ISEMPTY(csection)) {
return strdup("unknown");
}
csection = rz_str_trim_head_ro(csection);
char *section_name = strdup(csection);
rz_str_trim_tail(section_name);
if (RZ_STR_ISEMPTY(section_name)) {
free(section_name);
return strdup("unknown");
}
return section_name;
}

static void print_json_string(RzCore *core, const ut8 *block, ut32 len, RzStrEnc encoding, bool stop_at_nil) {
char *section = get_section_name(core);
char *section = get_section_name(core, core->offset);
if (!section) {
return;
}
Expand Down
21 changes: 8 additions & 13 deletions librz/core/disasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -425,29 +425,23 @@ static void get_bits_comment(RzCore *core, RzAnalysisFunction *f, char *cmt, int
}
}

RZ_API const char *rz_core_get_section_name(RzCore *core, ut64 addr) {
static char section[128] = "";
static ut64 oaddr = UT64_MAX;
if (oaddr == addr) {
return section;
}
RZ_API RZ_OWN char *rz_core_get_section_name(RzCore *core, ut64 addr) {
char *section = NULL;
RzBinObject *bo = rz_bin_cur_object(core->bin);
RzBinSection *s = bo ? rz_bin_get_section_at(bo, addr, core->io->va) : NULL;
if (s && s->name && *s->name) {
snprintf(section, sizeof(section) - 1, "%10s ", s->name);
if (s && RZ_STR_ISNOTEMPTY(s->name)) {
return rz_str_dup(s->name);
} else {
RzListIter *iter;
RzDebugMap *map;
*section = 0;
rz_list_foreach (core->dbg->maps, iter, map) {
if (addr >= map->addr && addr < map->addr_end) {
const char *mn = rz_str_lchr(map->name, '/');
rz_str_ncpy(section, mn ? mn + 1 : map->name, sizeof(section));
section = rz_str_dup(mn ? mn + 1 : map->name);
break;
}
}
}
oaddr = addr;
return section;
}

Expand All @@ -458,14 +452,15 @@ static void _ds_comment_align_(RzDisasmState *ds, bool up, bool nl) {
theme_print_color(comment);
return;
}
const char *sn = ds->show_section ? rz_core_get_section_name(ds->core, ds->at) : "";
char *sn = ds->show_section ? rz_core_get_section_name(ds->core, ds->at) : NULL;
ds_align_comment(ds);
ds_align_comment(ds);
rz_cons_print(COLOR_RESET(ds));
ds_print_pre(ds, true);
rz_cons_printf("%s%s", nl ? "\n" : "", sn);
rz_cons_printf("%s%s", nl ? "\n" : "", rz_str_get(sn));
ds_print_ref_lines(ds->refline, ds->line_col, ds);
rz_cons_printf(" %s %s", up ? "" : ".-", COLOR(ds, comment));
free(sn);
}
#define CMT_ALIGN _ds_comment_align_(ds, true, false)

Expand Down
2 changes: 1 addition & 1 deletion librz/include/rz_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ RZ_API RZ_OWN RzList /*<char *>*/ *rz_core_theme_list(RZ_NONNULL RzCore *core);
RZ_API char *rz_core_theme_get(RzCore *core);
RZ_API bool rz_core_theme_load(RzCore *core, const char *name);
RZ_API void rz_core_theme_nextpal(RzCore *core, RzConsPalSeekMode mode);
RZ_API const char *rz_core_get_section_name(RzCore *core, ut64 addr);
RZ_API RZ_OWN char *rz_core_get_section_name(RzCore *core, ut64 addr);
RZ_API RzCons *rz_core_get_cons(RzCore *core);
RZ_API RzBin *rz_core_get_bin(RzCore *core);
RZ_API RzConfig *rz_core_get_config(RzCore *core);
Expand Down
6 changes: 3 additions & 3 deletions librz/include/rz_util/rz_str.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ RZ_API void rz_str_trim(RZ_NONNULL RZ_INOUT char *str);
RZ_API void rz_str_trim_char(RZ_NONNULL RZ_INOUT char *str, const char c);
RZ_API char *rz_str_trim_dup(const char *str);
RZ_API char *rz_str_trim_lines(char *str);
RZ_API void rz_str_trim_head(RZ_NONNULL char *str);
RZ_API void rz_str_trim_head(RZ_NONNULL RZ_INOUT char *str);
RZ_API void rz_str_trim_head_char(RZ_NONNULL RZ_INOUT char *str, const char c);
RZ_API const char *rz_str_trim_head_ro(const char *str);
RZ_API const char *rz_str_trim_head_wp(const char *str);
RZ_API const char *rz_str_trim_head_ro(RZ_NONNULL const char *str);
RZ_API const char *rz_str_trim_head_wp(RZ_NONNULL const char *str);
RZ_API RZ_BORROW char *rz_str_trim_tail(RZ_NONNULL char *str);
RZ_API void rz_str_trim_tail_char(RZ_NONNULL RZ_INOUT char *str, const char c);
RZ_API ut64 rz_str_djb2_hash(const char *str);
Expand Down
21 changes: 14 additions & 7 deletions librz/util/str_trim.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,23 @@ RZ_API char *rz_str_trim_dup(const char *str) {
return a;
}

// Returns a pointer to the first non-whitespace character of str.
// TODO: Find a better name: to rz_str_trim_head_ro(), rz_str_skip_head or so
RZ_API const char *rz_str_trim_head_ro(const char *str) {
/* \brief Returns a pointer to the first non-whitespace character of \p str
*
* It considers space, TAB, and newline characters as the whitespace
*/
RZ_API const char *rz_str_trim_head_ro(RZ_NONNULL const char *str) {
Copy link
Member

Choose a reason for hiding this comment

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

This should really be named rz_str_first_non_whitespace. i will open a PR after this is merged

rz_return_val_if_fail(str, NULL);
for (; *str && IS_WHITECHAR(*str); str++) {
;
}
return str;
}

// TODO: find better name
RZ_API const char *rz_str_trim_head_wp(const char *str) {
/* \brief Returns a pointer to the first non-whitespace character of \p str
*
* It considers only space and TAB as the whitespace
*/
RZ_API const char *rz_str_trim_head_wp(RZ_NONNULL const char *str) {
Copy link
Member

Choose a reason for hiding this comment

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

Same here.

rz_return_val_if_fail(str, NULL);
for (; *str && !IS_WHITESPACE(*str); str++) {
;
Expand All @@ -107,7 +112,8 @@ RZ_API const char *rz_str_trim_head_wp(const char *str) {
*
* \param str The string to trim.
*/
RZ_API void rz_str_trim_head(RZ_NONNULL char *str) {
RZ_API void rz_str_trim_head(RZ_NONNULL RZ_INOUT char *str) {
rz_return_if_fail(str);
char *p = (char *)rz_str_trim_head_ro(str);
if (p) {
memmove(str, p, strlen(p) + 1);
Expand All @@ -123,7 +129,7 @@ RZ_API void rz_str_trim_head(RZ_NONNULL char *str) {
* \return The edited string.
*/
RZ_API RZ_BORROW char *rz_str_trim_tail(RZ_NONNULL char *str) {
rz_return_val_if_fail(str, str);
rz_return_val_if_fail(str, NULL);
size_t length = strlen(str);
while (length-- > 0) {
if (IS_WHITECHAR(str[length])) {
Expand Down Expand Up @@ -188,6 +194,7 @@ RZ_API void rz_str_trim_char(RZ_NONNULL RZ_INOUT char *str, const char c) {
* \param str The string to trim.
*/
RZ_API void rz_str_trim(RZ_NONNULL RZ_INOUT char *str) {
rz_return_if_fail(str);
rz_str_trim_head(str);
rz_str_trim_tail(str);
}
Expand Down
6 changes: 3 additions & 3 deletions test/db/cmd/cmd_px
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ EXPECT=<<EOF
0x00000708 0x1f0f obj.__FRAME_END+5799
0x0000070a 0x0084 segment.PHDR+68
0x0000070c 0x0000 segment.LOAD0
.rodata 0x00000720 0x0001 segment.LOAD0+1
.rodata 0x00000722 0x0002 segment.LOAD0+2
.eh_frame_hdr 0x00000724 0x1b01 obj.__FRAME_END+4761
.rodata 0x00000720 0x0001 segment.LOAD0+1
.rodata 0x00000722 0x0002 segment.LOAD0+2
.eh_frame_hdr 0x00000724 0x1b01 obj.__FRAME_END+4761
EOF
RUN

Expand Down
6 changes: 3 additions & 3 deletions test/db/cmd/cmd_pxw
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ EXPECT=<<EOF
0x00000760 0x00000014 segment.LOAD0+20
0x00000764 0x00000000 segment.LOAD0
0x00000768 0x00527a01
.eh_frame_hdr 0x00000724 0x3b031b01
.eh_frame_hdr 0x00000728 0x00000038 segment.LOAD0+56
.eh_frame_hdr 0x0000072c 0x00000006 segment.LOAD0+6
.eh_frame_hdr 0x00000724 0x3b031b01
.eh_frame_hdr 0x00000728 0x00000038 segment.LOAD0+56
.eh_frame_hdr 0x0000072c 0x00000006 segment.LOAD0+6
EOF
RUN

Expand Down
Loading