Skip to content

Commit

Permalink
[WIP] Colorize function arguments and variables part 2 (#10777)
Browse files Browse the repository at this point in the history
* Colorize comments that hint type of data
* Use new colors for comments that hint type of data
* Colorize local variables and args in disasm
* Fixed leak in r_anal_var_all_list()
* Refactoring + rename color func_arg* to func_var*
* Updated all color themes for func_var* colors
* Directly query sdb for faster and cleaner var colorizing in r_colorize_opcode
* Remove hackish r_core.h include in print.c and use callbacks instead
  • Loading branch information
cyanpencil authored and radare committed Jul 20, 2018
1 parent 87bd92a commit 13a2cb3
Show file tree
Hide file tree
Showing 37 changed files with 214 additions and 65 deletions.
12 changes: 9 additions & 3 deletions libr/anal/var.c
Expand Up @@ -793,9 +793,15 @@ R_API RList *r_anal_var_all_list(RAnal *anal, RAnalFunction *fcn) {
if (!list) {
return NULL;
}
r_list_join (list, r_anal_var_list (anal, fcn, R_ANAL_VAR_KIND_REG));
r_list_join (list, r_anal_var_list (anal, fcn, R_ANAL_VAR_KIND_BPV));
r_list_join (list, r_anal_var_list (anal, fcn, R_ANAL_VAR_KIND_SPV));
RList *reg_vars = r_anal_var_list (anal, fcn, R_ANAL_VAR_KIND_REG);
RList *bpv_vars = r_anal_var_list (anal, fcn, R_ANAL_VAR_KIND_BPV);
RList *spv_vars = r_anal_var_list (anal, fcn, R_ANAL_VAR_KIND_SPV);
r_list_join (list, reg_vars);
r_list_join (list, bpv_vars);
r_list_join (list, spv_vars);
r_list_free (reg_vars);
r_list_free (bpv_vars);
r_list_free (spv_vars);
return list;
}

Expand Down
4 changes: 4 additions & 0 deletions libr/cons/d/ayu
Expand Up @@ -44,3 +44,7 @@ ec ret rgb:d66 # ret opcode
ec swi rgb:d66
ec trap rgb:d66
ec usrcmt rgb:fa6e32 # user comment

ec func_var rgb:95e6cb
ec func_var_type rgb:ffd580
ec func_var_addr rgb:f28779
6 changes: 4 additions & 2 deletions libr/cons/d/basic
Expand Up @@ -31,13 +31,11 @@ ec num cyan
ec invalid red
ec swi cyan
ec cmp yellow
ec leave gray
ec call cyan
ec cjmp green
ec jmp green
ec pop yellow
ec linehl rgb:004

ec graph.true green
ec graph.false red
ec graph.trufae blue
Expand All @@ -47,3 +45,7 @@ ec graph.box gray
ec graph.box2 blue
ec graph.box3 gray
ec graph.box4 gray

ec func_var white
ec func_var_type blue
ec func_var_addr cyan
4 changes: 4 additions & 0 deletions libr/cons/d/behelit
Expand Up @@ -54,3 +54,7 @@ ec linehl rgb:1f1f1f
# background of the terminal must be #121212
# foreground of the terminal must be #5fff5f
# xterm -bg rgb:12/12/12 -fg rgb:5f/ff/5f

ec func_var rgb:ff005f
ec func_var_type blue
ec func_var_addr rgb:5f87ff
4 changes: 4 additions & 0 deletions libr/cons/d/bold
Expand Up @@ -56,3 +56,7 @@ ec crypto rgb:f72
ec floc rgb:777
ec invalid red
ec linehl rgb:008

ec func_var yellow . bold
ec func_var_type blue . bold
ec func_var_addr cyan . bold
4 changes: 4 additions & 0 deletions libr/cons/d/cga
Expand Up @@ -61,3 +61,7 @@ ec ai.write rgb:f0f
ec ai.exec rgb:f0f
ec ai.seq rgb:f0f
ec ai.ascii rgb:f0f

ec func_var rgb:f0f
ec func_var_type rgb:f0f
ec func_var_addr rgb:0ff
4 changes: 4 additions & 0 deletions libr/cons/d/consonance
Expand Up @@ -47,3 +47,7 @@ ec graph.false rgb:d41
ec graph.trufae rgb:4cf
ec graph.current rgb:af2
ec graph.traced rgb:090

ec func_var rgb:fcc
ec func_var_type rgb:f2a
ec func_var_addr white
4 changes: 4 additions & 0 deletions libr/cons/d/cutter
Expand Up @@ -42,3 +42,7 @@ ec graph.false rgb:e33
ec graph.trufae rgb:28d
ec graph.current rgb:0ff
ec graph.traced rgb:e33

ec func_var rgb:d38
ec func_var_type blue
ec func_var_addr rgb:bbb
4 changes: 4 additions & 0 deletions libr/cons/d/dark
Expand Up @@ -43,3 +43,7 @@ ec graph.false rgb:035
ec graph.trufae rgb:06c
ec graph.current rgb:99a
ec graph.traced rgb:bbb

ec func_var rgb:99a
ec func_var_type rgb:636
ec func_var_addr rgb:366
4 changes: 4 additions & 0 deletions libr/cons/d/darkda
Expand Up @@ -44,3 +44,7 @@ ec graph.true rgb:009100
ec graph.false rgb:bc0000
ec graph.trufae rgb:0043cb
ec graph.current rgb:fff

ec func_var rgb:99a
ec func_var_type rgb:df077e
ec func_var_addr rgb:ababab
4 changes: 4 additions & 0 deletions libr/cons/d/defragger
Expand Up @@ -54,3 +54,7 @@ ec gui.dataoffset rgb:ffff00
ec gui.background rgb:303030
ec gui.alt_background rgb:ffffff
ec gui.border rgb:5fd700

ec func_var rgb:64604f
ec func_var_type rgb:00afd7
ec func_var_addr rgb:afafaf
4 changes: 4 additions & 0 deletions libr/cons/d/focus
Expand Up @@ -44,3 +44,7 @@ ec graph.false red
ec graph.trufae blue
ec graph.current blue
ec graph.traced red

ec func_var cyan
ec func_var_type rgb:f3f
ec func_var_addr white
4 changes: 4 additions & 0 deletions libr/cons/d/lima
Expand Up @@ -48,3 +48,7 @@ ec graph.false rgb:5d5
ec graph.trufae rgb:af2
ec graph.current rgb:af2
ec graph.traced rgb:090

ec func_var yellow
ec func_var_type green
ec func_var_addr yellow
4 changes: 4 additions & 0 deletions libr/cons/d/matrix
Expand Up @@ -44,3 +44,7 @@ ec graph.false rgb:050
ec graph.trufae green
ec graph.current green
ec graph.traced rgb:060

ec func_var rgb:060
ec func_var_type rgb:383
ec func_var_addr rgb:060
4 changes: 4 additions & 0 deletions libr/cons/d/monokai
Expand Up @@ -44,3 +44,7 @@ ec graph.true rgb:A6df2E
ec graph.false rgb:f92672
ec graph.trufae rgb:a398e5
ec graph.current rgb:fff

ec func_var cyan
ec func_var_type rgb:a398e5
ec func_var_addr rgb:f92672
4 changes: 4 additions & 0 deletions libr/cons/d/ogray
Expand Up @@ -46,3 +46,7 @@ ec gui.dataoffset rgb:f72
ec crypto rgb:f72
ec floc rgb:777
ec linehl rgb:111

ec func_var rgb:f72
ec func_var_type rgb:777
ec func_var_addr rgb:555
4 changes: 4 additions & 0 deletions libr/cons/d/pink
Expand Up @@ -52,3 +52,7 @@ ec gui.background black
## ai.ascii
## gui.alt_background
## gui.border

ec func_var rgb:72d
ec func_var_type blue
ec func_var_addr rgb:72d
4 changes: 4 additions & 0 deletions libr/cons/d/rasta
Expand Up @@ -48,3 +48,7 @@ ec graph.false red
ec graph.trufae green
ec graph.current green
ec graph.traced yellow

ec func_var green
ec func_var_type red
ec func_var_addr yellow
4 changes: 4 additions & 0 deletions libr/cons/d/sepia
Expand Up @@ -45,3 +45,7 @@ ec graph.box rgb:431
ec graph.box2 rgb:ca6
ec graph.box3 rgb:ca6
ec graph.box4 rgb:ca6

ec func_var rgb:fd9
ec func_var_type rgb:850
ec func_var_addr rgb:960
4 changes: 4 additions & 0 deletions libr/cons/d/smyck
Expand Up @@ -45,3 +45,7 @@ ec graph.true rgb:9c4
ec graph.false rgb:d66
ec graph.trufae rgb:9c4
ec graph.current rgb:fff

ec func_var rgb:9df
ec func_var_type rgb:fd6
ec func_var_addr rgb:bb5
4 changes: 4 additions & 0 deletions libr/cons/d/solarized
Expand Up @@ -44,5 +44,9 @@ ec graph.trufae rgb:899
ec graph.current rgb:f00
ec graph.traced red

ec func_var rgb:28d
ec func_var_type rgb:cc8
ec func_var_addr rgb:9aa

# background of the terminal must be 134 o 033
# xterm -fn 10x20 -bg rgb:00/30/30 -fg rgb:e0/e0/e0 -e "r2 -c 'eco solarized' /bin/ls"
4 changes: 4 additions & 0 deletions libr/cons/d/tango
Expand Up @@ -61,3 +61,7 @@ ec graph.box rgb:950
ec graph.box2 rgb:7d1
ec graph.box3 rgb:950
ec graph.box4 rgb:950

ec func_var rgb:370
ec func_var_type rgb:a41
ec func_var_addr rgb:aaa
4 changes: 4 additions & 0 deletions libr/cons/d/twilight
Expand Up @@ -47,3 +47,7 @@ ec graph.box rgb:9b7
ec graph.box2 rgb:b97
ec graph.box3 rgb:b97
ec graph.box4 rgb:b97

ec func_var rgb:c64
ec func_var_type rgb:aa6
ec func_var_addr rgb:788
4 changes: 4 additions & 0 deletions libr/cons/d/white
Expand Up @@ -42,3 +42,7 @@ ec graph.false red
ec graph.trufae blue
ec graph.current rgb:0ff
ec graph.traced red

ec func_var magenta
ec func_var_type blue
ec func_var_addr magenta
4 changes: 4 additions & 0 deletions libr/cons/d/xvilka
Expand Up @@ -56,3 +56,7 @@ ec gui.dataoffset yellow
ec gui.background black
ec gui.alt_background white
ec gui.border black

ec func_var rgb:0cf
ec func_var_type rgb:0c0
ec func_var_addr rgb:366
4 changes: 4 additions & 0 deletions libr/cons/d/zenburn
Expand Up @@ -47,3 +47,7 @@ ec graph.false rgb:d52
ec graph.trufae rgb:aaa
ec graph.current blue
ec graph.traced red

ec func_var rgb:7a7
ec func_var_type rgb:eec
ec func_var_addr rgb:aaa
12 changes: 6 additions & 6 deletions libr/cons/pal.c
Expand Up @@ -47,9 +47,9 @@ static struct {
{ "creg", r_offsetof (RConsPrintablePalette, creg), r_offsetof (RConsPalette, creg) },
{ "num", r_offsetof (RConsPrintablePalette, num), r_offsetof (RConsPalette, num) },
{ "mov", r_offsetof (RConsPrintablePalette, mov), r_offsetof (RConsPalette, mov) },
{ "func_arg", r_offsetof (RConsPrintablePalette, func_arg), r_offsetof (RConsPalette, func_arg) },
{ "func_arg_type", r_offsetof (RConsPrintablePalette, func_arg_type), r_offsetof (RConsPalette, func_arg_type) },
{ "func_arg_addr", r_offsetof (RConsPrintablePalette, func_arg_addr), r_offsetof (RConsPalette, func_arg_addr) },
{ "func_var", r_offsetof (RConsPrintablePalette, func_var), r_offsetof (RConsPalette, func_var) },
{ "func_var_type", r_offsetof (RConsPrintablePalette, func_var_type), r_offsetof (RConsPalette, func_var_type) },
{ "func_var_addr", r_offsetof (RConsPrintablePalette, func_var_addr), r_offsetof (RConsPalette, func_var_addr) },

{ "ai.read", r_offsetof (RConsPrintablePalette, ai_read), r_offsetof (RConsPalette, ai_read) },
{ "ai.write", r_offsetof (RConsPrintablePalette, ai_write), r_offsetof (RConsPalette, ai_write) },
Expand Down Expand Up @@ -169,9 +169,9 @@ R_API void r_cons_pal_init() {
cons->cpal.wordhl = (RColor) RColor_BGRED;
cons->cpal.linehl = (RColor) RCOLOR (ALPHA_BG, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x00);

cons->cpal.func_arg = (RColor) RColor_WHITE;
cons->cpal.func_arg_type = (RColor) RColor_BLUE;
cons->cpal.func_arg_addr = (RColor) RColor_CYAN;
cons->cpal.func_var = (RColor) RColor_WHITE;
cons->cpal.func_var_type = (RColor) RColor_BLUE;
cons->cpal.func_var_addr = (RColor) RColor_CYAN;

cons->cpal.graph_box = (RColor) RColor_NULL;
cons->cpal.graph_box2 = (RColor) RColor_BLUE;
Expand Down
2 changes: 1 addition & 1 deletion libr/core/anal_tp.c
Expand Up @@ -225,7 +225,7 @@ static void type_match(RCore *core, ut64 addr, char *fcn_name, ut64 baddr, const
// Match type from function param to instr
if (type_pos_hit (anal, trace, in_stack, j, size, place)) {
if (!cmt_set && type && name) {
r_meta_set_string (anal, R_META_TYPE_COMMENT, instr_addr,
r_meta_set_string (anal, R_META_TYPE_VARTYPE, instr_addr,
sdb_fmt ("%s%s%s", type, r_str_endswith (type, "*") ? "" : " ", name));
cmt_set = true;
}
Expand Down
6 changes: 3 additions & 3 deletions libr/core/cmd_anal.c
Expand Up @@ -5335,7 +5335,7 @@ static bool cmd_anal_refs(RCore *core, const char *input) {
asmop.buf_asm, str, sizeof (str), core->print->big_endian);
if (has_color) {
buf_asm = r_print_colorize_opcode (core->print, str,
core->cons->pal.reg, core->cons->pal.num, false);
core->cons->pal.reg, core->cons->pal.num, false, fcn ? fcn->addr : 0);
} else {
buf_asm = r_str_new (str);
}
Expand Down Expand Up @@ -5364,6 +5364,7 @@ static bool cmd_anal_refs(RCore *core, const char *input) {
RAnalRef *ref;
RListIter *iter;
char *space = strchr (input, ' ');
RAnalFunction * fcn = r_anal_get_fcn_in (core->anal, addr, 0);

if (space) {
addr = r_num_math (core->num, space + 1);
Expand All @@ -5373,7 +5374,6 @@ static bool cmd_anal_refs(RCore *core, const char *input) {
if (input[1] == '.') { // axf.
list = list_ = r_anal_xrefs_get_from (core->anal, addr);
if (!list) {
RAnalFunction * fcn = r_anal_get_fcn_in (core->anal, addr, 0);
list = r_anal_fcn_get_refs (core->anal, fcn);
}
} else {
Expand Down Expand Up @@ -5412,7 +5412,7 @@ static bool cmd_anal_refs(RCore *core, const char *input) {
asmop.buf_asm, str, sizeof (str), core->print->big_endian);
if (has_color) {
buf_asm = r_print_colorize_opcode (core->print, str,
core->cons->pal.reg, core->cons->pal.num, false);
core->cons->pal.reg, core->cons->pal.num, false, fcn ? fcn->addr : 0);
} else {
buf_asm = r_str_new (str);
}
Expand Down
4 changes: 2 additions & 2 deletions libr/core/cmd_search.c
Expand Up @@ -1091,7 +1091,7 @@ static void print_rop(RCore *core, RList *hitlist, char mode, bool *json_first)
r_cons_printf ("%s\n", opstr);
} else if (colorize) {
buf_asm = r_print_colorize_opcode (core->print, asmop.buf_asm,
core->cons->pal.reg, core->cons->pal.num, false);
core->cons->pal.reg, core->cons->pal.num, false, 0);
r_cons_printf (" %s%s;", buf_asm, Color_RESET);
free (buf_asm);
} else {
Expand Down Expand Up @@ -1128,7 +1128,7 @@ static void print_rop(RCore *core, RList *hitlist, char mode, bool *json_first)
}
if (colorize) {
buf_asm = r_print_colorize_opcode (core->print, asmop.buf_asm,
core->cons->pal.reg, core->cons->pal.num, false);
core->cons->pal.reg, core->cons->pal.num, false, 0);
otype = r_print_color_op_type (core->print, analop.type);
if (comment) {
r_cons_printf (" 0x%08"PFMT64x " %18s%s %s%s ; %s\n",
Expand Down
15 changes: 12 additions & 3 deletions libr/core/core.c
Expand Up @@ -1390,9 +1390,9 @@ static int autocomplete(RLine *line) {
ADDARG("creg")
ADDARG("num")
ADDARG("mov")
ADDARG("func_arg")
ADDARG("func_arg_type")
ADDARG("func_arg_addr")
ADDARG("func_var")
ADDARG("func_var_type")
ADDARG("func_var_addr")
ADDARG("ai.read")
ADDARG("ai.write")
ADDARG("ai.exec")
Expand Down Expand Up @@ -1867,6 +1867,14 @@ static int mywrite(const ut8 *buf, int len) {
return r_cons_memcat ((const char *)buf, len);
}

static bool exists_var(RPrint *print, ut64 func_addr, char *str) {
char *name_key = sdb_fmt ("var.0x%"PFMT64x ".%d.%s", func_addr, 1, str);
if (sdb_const_get_len (((RCore*)(print->user))->anal->sdb_fcns, name_key, NULL, 0)) {
return true;
}
return false;
}

static bool r_core_anal_log(struct r_anal_t *anal, const char *msg) {
RCore *core = anal->user;
if (core->cfglog) {
Expand Down Expand Up @@ -2028,6 +2036,7 @@ R_API bool r_core_init(RCore *core) {
core->print->cb_printf = r_cons_printf;
core->print->cb_color = r_cons_rainbow_get;
core->print->write = mywrite;
core->print->exists_var = exists_var;
core->print->disasm = __disasm;
core->print->colorfor = (RPrintColorFor)r_core_anal_optype_colorfor;
core->print->hasrefs = (RPrintColorFor)r_core_anal_hasrefs;
Expand Down

0 comments on commit 13a2cb3

Please sign in to comment.