Skip to content

Commit a099e01

Browse files
authored
Refactor r_print_addr to work with strings to reduce RCons pressure
1 parent c9923fd commit a099e01

File tree

4 files changed

+70
-81
lines changed

4 files changed

+70
-81
lines changed

libr/core/cmd_print.inc.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -875,13 +875,16 @@ static void cmd_prcn(RCore *core, const ut8 *block, int len, bool bitsmode) {
875875
RInterval itv = { core->addr, (ut64)len };
876876
regions = r_io_bank_get_regions (core->io, core->io->bank, itv);
877877
}
878+
RStrBuf *sb = r_strbuf_new ("");
878879
for (i = 0; i < len; i += cols) {
879880
if (show_section) {
880881
const char *name = r_core_get_section_name (core, core->addr + i);
881-
r_cons_printf (cons, "%20s ", r_str_get (name));
882+
r_strbuf_appendf (sb, "%20s ", r_str_get (name));
882883
}
883884
if (show_offset) {
884-
r_print_addr (core->print, core->addr + i);
885+
char addrbuf[64];
886+
r_print_addr_tostring (core->print, core->addr + i, addrbuf, sizeof (addrbuf));
887+
r_strbuf_append (sb, addrbuf);
885888
}
886889
for (j = i; j < i + cols; j++) {
887890
if (j >= len) {
@@ -903,28 +906,30 @@ static void cmd_prcn(RCore *core, const ut8 *block, int len, bool bitsmode) {
903906
if (bitsmode) {
904907
char color0bits[8] = { 0 };
905908
char color1bits[8] = { 0 };
906-
ut8 b0 = ch0 | ch0 << 4;
907-
ut8 b1 = ch1 | ch1 << 4;
909+
const ut8 b0 = ch0 | ch0 << 4;
910+
const ut8 b1 = ch1 | ch1 << 4;
908911
r_str_bits (color0bits, &b0, 4, NULL);
909912
r_str_bits (color1bits, &b1, 4, NULL);
910-
r_cons_printf (cons, "%s%s%s%s%s ", color0, color0bits, color1, color1bits, show_color? Color_RESET: "");
913+
r_strbuf_appendf (sb, "%s%s%s%s%s ", color0, color0bits, color1, color1bits, show_color ? Color_RESET : "");
911914
} else {
912-
r_cons_printf (cons, "%s%01x%s%01x%s", color0, ch0, color1, ch1, show_color? Color_RESET: "");
915+
r_strbuf_appendf (sb, "%s%01x%s%01x%s", color0, ch0, color1, ch1, show_color ? Color_RESET : "");
913916
}
914917
free (color0);
915918
free (color1);
916919
}
917920
if (show_color) {
918-
r_cons_printf (cons, Color_RESET);
921+
r_strbuf_append (sb, Color_RESET);
919922
}
920923
if (show_flags) {
921924
RFlagItem *fi = r_flag_get_in (core->flags, core->addr + j);
922925
if (fi) {
923-
r_cons_printf (cons, " ; %s", fi->name);
926+
r_strbuf_appendf (sb, " ; %s", fi->name);
924927
}
925928
}
926-
r_cons_newline (cons);
929+
r_strbuf_append (sb, "\n");
927930
}
931+
r_cons_print (cons, r_strbuf_get (sb));
932+
r_strbuf_free (sb);
928933
RVecRIORegion_free (regions);
929934
}
930935

@@ -971,11 +976,7 @@ static void cmd_prc(RCore *core, const ut8 *block, int len) {
971976
color_val);
972977
color = r_cons_pal_parse (core->cons, str, NULL);
973978
free (str);
974-
if (show_cursor && core->print->cur == j) {
975-
ch = '_';
976-
} else {
977-
ch = ' ';
978-
}
979+
ch = (show_cursor && core->print->cur == j)?: '_': ' ';
979980
} else {
980981
color = strdup ("");
981982
if (show_cursor && core->print->cur == j) {

libr/include/r_lib.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ R_LIB_VERSION_HEADER (r_lib);
2323
// double-indirection required because cpp is crap
2424
#define STRINGIFY2(x) #x
2525
#define STRINGIFY(x) STRINGIFY2(x)
26-
#define R2_ABIVERSION 71
26+
#define R2_ABIVERSION 72
2727
#define R2_ABIVERSION_STRING STRINGIFY(R2_ABIVERSION)
2828

2929
#define R_LIB_ENV "R2_LIBR_PLUGINS"

libr/include/r_util/r_print.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ R_API void r_print_free(RPrint * R_NULLABLE p);
184184
R_API bool r_print_fini(RPrint * R_NONNULL p);
185185
R_API void r_print_set_flags(RPrint *p, int _flags);
186186
R_API void r_print_unset_flags(RPrint *p, int flags);
187+
R_API int r_print_addr_tostring(RPrint *p, ut64 addr, char *buf, size_t buf_size);
187188
R_API void r_print_addr(RPrint *p, ut64 addr);
188189
R_API void r_print_section(RPrint *p, ut64 at);
189190
R_API char *r_print_columns(RPrint *p, const ut8 *buf, int len, int height);

libr/util/print.c

Lines changed: 53 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* radare2 - LGPL - Copyright 2007-2025 - pancake */
1+
/* radare2 - LGPL - Copyright 2007-2026 - pancake */
22

33
#include <r_util/r_print.h>
44
#include <r_util/r_str.h>
@@ -383,84 +383,71 @@ R_API void r_print_cursor(RPrint *p, int cur, int len, int set) {
383383
}
384384
}
385385

386-
R_API void r_print_addr(RPrint *p, ut64 addr) {
387-
char space[32] = {
388-
0
389-
};
390-
char padstr[32];
391-
const char *white = "";
392-
#define PREOFF(x) (p && p->consb.cons && p->consb.cons->context && p->consb.cons->context->pal.x)? p->consb.cons->context->pal.x
393-
bool use_segoff = p? (p->flags & R_PRINT_FLAGS_SEGOFF): false;
394-
bool use_color = p? (p->flags & R_PRINT_FLAGS_COLOR): false;
395-
bool dec = p? (p->flags & R_PRINT_FLAGS_ADDRDEC): false;
396-
bool mod = p? (p->flags & R_PRINT_FLAGS_ADDRMOD): false;
397-
char ch = p? ((p->addrmod && mod)? ((addr % p->addrmod)? ' ': ','): ' '): ' ';
398-
if (p && p->flags & R_PRINT_FLAGS_COMPACT && p->col == 1) {
386+
R_API int r_print_addr_tostring(RPrint *p, ut64 addr, char *buf, size_t buf_size) {
387+
R_RETURN_VAL_IF_FAIL (buf && buf_size > 0, 0);
388+
bool use_segoff = p ? (p->flags & R_PRINT_FLAGS_SEGOFF) : false;
389+
bool use_color = p ? (p->flags & R_PRINT_FLAGS_COLOR) : false;
390+
bool dec = p ? (p->flags & R_PRINT_FLAGS_ADDRDEC) : false;
391+
bool mod = p ? (p->flags & R_PRINT_FLAGS_ADDRMOD) : false;
392+
char ch = p ? ((p->addrmod && mod) ? ((addr % p->addrmod) ? ' ' : ',') : ' ') : ' ';
393+
if (p && (p->flags & R_PRINT_FLAGS_COMPACT) && p->col == 1) {
399394
ch = '|';
400395
}
401396
if (p && p->pava) {
402397
p->iob.p2v (p->iob.io, addr, &addr);
403398
}
399+
const char *pre = Color_GREEN;
400+
const char *fin = Color_RESET; // AITODO: confirm this is used only in `use_color` code paths, because maybe we can just inline the value or simplify this code a little bit more
401+
if (use_color && p) {
402+
RCons *cons = p->consb.cons;
403+
if (cons) {
404+
if (p->flags & R_PRINT_FLAGS_RAINBOW) {
405+
if (cons->rgbstr) {
406+
static R_TH_LOCAL char rgbstr[32];
407+
pre = cons->rgbstr (cons, rgbstr, sizeof (rgbstr), addr);
408+
}
409+
} else if (cons->context && cons->context->pal.addr) {
410+
pre = cons->context->pal.addr;
411+
}
412+
}
413+
}
404414
if (use_segoff) {
405-
ut32 a = addr & 0xffff;
406-
ut32 s = (addr - a) >> ((p && p->config)? p->config->seggrn: 4);
415+
const ut32 a = addr & 0xffff;
416+
const ut32 s = (addr - a) >> ((p && p->config) ? p->config->seggrn : 4);
407417
if (dec) {
408-
snprintf (space, sizeof (space), "%d:%d", s & 0xffff, a & 0xffff);
409-
white = r_str_pad (padstr, sizeof (padstr), ' ', 9 - strlen (space));
410-
}
411-
if (use_color) {
412-
const char *pre = PREOFF (addr): Color_GREEN;
413-
const char *fin = Color_RESET;
414-
if (dec) {
415-
r_print_printf (p, "%s%s%s%s%c", pre, white, space, fin, ch);
416-
} else {
417-
r_print_printf (p, "%s%04x:%04x%s%c", pre, s & 0xffff, a & 0xffff, fin, ch);
418-
}
419-
} else {
420-
if (dec) {
421-
r_print_printf (p, "%s%s%c", white, space, ch);
422-
} else {
423-
r_print_printf (p, "%04x:%04x%c", s & 0xffff, a & 0xffff, ch);
418+
if (use_color) {
419+
return snprintf (buf, buf_size, "%s%9d:%-5d%s%c", pre, s & 0xffff, a & 0xffff, fin, ch);
424420
}
421+
return snprintf (buf, buf_size, "%9d:%-5d%c", s & 0xffff, a & 0xffff, ch);
425422
}
426-
} else {
427-
if (dec) {
428-
snprintf (space, sizeof (space), "%" PFMT64d, addr);
429-
int w = R_MAX (10 - strlen (space), 0);
430-
white = r_str_pad (padstr, sizeof (padstr), ' ', w);
423+
if (use_color) {
424+
return snprintf (buf, buf_size, "%s%04x:%04x%s%c", pre, s & 0xffff, a & 0xffff, fin, ch);
431425
}
426+
return snprintf (buf, buf_size, "%04x:%04x%c", s & 0xffff, a & 0xffff, ch);
427+
}
428+
if (dec) {
432429
if (use_color) {
433-
const char *pre = PREOFF (addr): Color_GREEN;
434-
const char *fin = Color_RESET;
435-
if (p && p->flags & R_PRINT_FLAGS_RAINBOW) {
436-
if (p->consb.cons && p->consb.cons->rgbstr) {
437-
static R_TH_LOCAL char rgbstr[32];
438-
pre = p->consb.cons->rgbstr (p->consb.cons, rgbstr, sizeof (rgbstr), addr);
439-
}
440-
}
441-
if (dec) {
442-
r_print_printf (p, "%s%s%" PFMT64d "%s%c", pre, white, addr, fin, ch);
443-
} else {
444-
if (p && p->wide_offsets) {
445-
// TODO: make %016 depend on asm.bits
446-
r_print_printf (p, "%s0x%016" PFMT64x "%s%c", pre, addr, fin, ch);
447-
} else {
448-
r_print_printf (p, "%s0x%08" PFMT64x "%s%c", pre, addr, fin, ch);
449-
}
450-
}
451-
} else {
452-
if (dec) {
453-
r_print_printf (p, "%s%" PFMT64d "%c", white, addr, ch);
454-
} else {
455-
if (p && p->wide_offsets) {
456-
// TODO: make %016 depend on asm.bits
457-
r_print_printf (p, "0x%016" PFMT64x "%c", addr, ch);
458-
} else {
459-
r_print_printf (p, "0x%08" PFMT64x "%c", addr, ch);
460-
}
461-
}
430+
return snprintf (buf, buf_size, "%s%10" PFMT64d "%s%c", pre, addr, fin, ch);
431+
}
432+
return snprintf (buf, buf_size, "%10" PFMT64d "%c", addr, ch);
433+
}
434+
if (use_color) {
435+
if (p && p->wide_offsets) {
436+
return snprintf (buf, buf_size, "%s0x%016" PFMT64x "%s%c", pre, addr, fin, ch);
462437
}
438+
return snprintf (buf, buf_size, "%s0x%08" PFMT64x "%s%c", pre, addr, fin, ch);
463439
}
440+
if (p && p->wide_offsets) {
441+
return snprintf (buf, buf_size, "0x%016" PFMT64x "%c", addr, ch);
442+
}
443+
return snprintf (buf, buf_size, "0x%08" PFMT64x "%c", addr, ch);
444+
}
445+
446+
// TODO: deprecate this function. r_print functions must not use RCons! just work with strbuf
447+
R_API void r_print_addr(RPrint *p, ut64 addr) {
448+
char buf[64];
449+
r_print_addr_tostring (p, addr, buf, sizeof (buf));
450+
r_print_printf (p, "%s", buf);
464451
}
465452

466453
R_API char* r_print_hexpair(RPrint *p, const char *str, int n) {

0 commit comments

Comments
 (0)