/* Returns R_DEBUG_REASON_* */ R_API int r_debug_wait(RDebug *dbg) { int ret = 0; if (!dbg) return false; dbg->reason.type = R_DEBUG_REASON_UNKNOWN; if (r_debug_is_dead (dbg)) return dbg->reason.type = R_DEBUG_REASON_DEAD; if (dbg->h && dbg->h->wait) { dbg->reason.type = R_DEBUG_REASON_UNKNOWN; ret = dbg->h->wait (dbg, dbg->pid); dbg->newstate = 1; if (ret == -1) { eprintf ("\n==> Process finished\n\n"); r_debug_select (dbg, -1, -1); } if (dbg->trace->enabled) r_debug_trace_pc (dbg); dbg->reason.type = ret; if (ret == R_DEBUG_REASON_SIGNAL && dbg->reason.signum != -1) { /* handle signal on continuations here */ int what = r_debug_signal_what (dbg, dbg->reason.signum); const char *name = r_debug_signal_resolve_i (dbg, dbg->reason.signum); if (name && strcmp ("SIGTRAP", name)) r_cons_printf ("[+] signal %d aka %s received %d\n", dbg->reason.signum, name, what); } } return ret; }
static int flag_to_flag(RCore *core, const char *glob) { r_return_val_if_fail (glob, 0); glob = r_str_trim_ro (glob); struct flag_to_flag_t u = { .next = UT64_MAX, .offset = core->offset }; r_flag_foreach_glob (core->flags, glob, flag_to_flag_foreach, &u); if (u.next != UT64_MAX && u.next > core->offset) { return u.next - core->offset; } return 0; } static void cmd_flag_tags (RCore *core, const char *input) { char mode = input[1]; for (; *input && !IS_WHITESPACE (*input); input++) {} char *inp = strdup (input); char *arg = r_str_trim (inp); if (!*arg && !mode) { const char *tag; RListIter *iter; RList *list = r_flag_tags_list (core->flags); r_list_foreach (list, iter, tag) { r_cons_printf ("%s\n", tag); } r_list_free (list); free (inp); return; }
R_API int r_core_visual_hud(RCore *core) { const char *c = r_config_get (core->config, "hud.path"); const char *f = R2_LIBDIR"/radare2/"R2_VERSION"/hud/main"; char *homehud = r_str_home (R2_HOMEDIR"/hud"); char *res = NULL; char *p = 0; showcursor (core, true); if (c && *c && r_file_exists (c)) res = r_cons_hud_file (c); if (!res && homehud) res = r_cons_hud_file (homehud); if (!res && r_file_exists (f)) res = r_cons_hud_file (f); if (!res) r_cons_message ("Cannot find hud file"); r_cons_clear (); if (res) { p = strchr (res, '\t'); r_cons_printf ("%s\n", res); r_cons_flush (); if (p) r_core_cmd0 (core, p+1); free (res); } showcursor (core, false); r_cons_flush (); free (homehud); return (int)(size_t)p; }
/* Returns PID */ R_API int r_debug_wait(RDebug *dbg) { int ret = 0; if (r_debug_is_dead (dbg)) return R_FALSE; if (dbg && dbg->h && dbg->h->wait) { dbg->reason = R_DBG_REASON_UNKNOWN; ret = dbg->h->wait (dbg, dbg->pid); dbg->reason = ret; dbg->newstate = 1; if (ret == -1) { eprintf ("\n==> Process finished\n\n"); r_debug_select (dbg, -1, -1); } //eprintf ("wait = %d\n", ret); if (dbg->trace->enabled) r_debug_trace_pc (dbg); if (ret == R_DBG_REASON_SIGNAL && dbg->signum != -1) { /* handle signal on continuations here */ int what = r_debug_signal_what (dbg, dbg->signum); const char *name = r_debug_signal_resolve_i (dbg, dbg->signum); if (strcmp ("SIGTRAP", name)) r_cons_printf ("[+] signal %d aka %s received\n", dbg->signum, name); if (what & R_DBG_SIGNAL_SKIP) { dbg->signum = 0; // TODO: use ptrace-setsiginfo to ignore signal } if (what & R_DBG_SIGNAL_CONT) { // XXX: support step, steptrace, continue_until_foo, etc.. r_debug_continue (dbg); } } } return ret; }
R_API void r_core_visual_prompt_input (RCore *core) { int ret; ut64 addr = core->offset; ut64 bsze = core->blocksize; r_cons_reset_colors(); r_cons_printf("\nPress <enter> to return to Visual mode.\n"); r_cons_show_cursor (true); core->vmode = false; ut64 newaddr = addr; if (curset) { if (ocursor != -1) { newaddr = core->offset + ocursor; r_core_block_size (core, cursor-ocursor); } else newaddr = core->offset + cursor; r_core_seek (core, newaddr, 1); } do { ret = r_core_visual_prompt (core); if (core->offset != newaddr) { // do not restore seek anymore newaddr = addr; } } while (ret); if (curset) { if (addr != newaddr) { r_core_seek (core, addr, 1); r_core_block_size (core, bsze); } } r_cons_show_cursor (false); core->vmode = true; }
static int r_core_visual_hud(RCore *core) { const char *f = R2_LIBDIR"/radare2/"R2_VERSION"/hud/main"; char *homehud = r_str_home(R2_HOMEDIR"/hud"); char *res = NULL; char *p = 0; r_cons_show_cursor (R_TRUE); if (homehud) res = r_cons_hud_file (homehud); if (!res) { if (r_file_exists (f)) res = r_cons_hud_file (f); else r_cons_message ("Cannot find hud file"); } r_cons_clear (); if (res) { p = strchr (res, '\t'); core->printidx = 1; r_cons_printf ("%s\n", res); r_cons_flush (); if (p) r_core_cmd0 (core, p+1); free (res); } r_cons_show_cursor (R_FALSE); r_cons_flush (); return (int)(size_t)p; }
static void r_cons_pal_show_rgb () { const int inc = 3; int i, j, k, n = 0; r_cons_print ("\n\nRGB:\n"); for (i = n = 0; i <= 0xf; i += inc) { for (k = 0; k <= 0xf; k += inc) { for (j = 0; j <= 0xf; j += inc) { char fg[32], bg[32]; int r = i * 16; int g = j * 16; int b = k * 16; if ((i < 6) && (j <5)) strcpy (fg, Color_WHITE); else strcpy (fg, Color_BLACK); r_cons_rgb_str (bg, r, g, b, 1); r_cons_printf ("%s%s rgb:%02x%02x%02x " Color_RESET, fg, bg, r, g, b); //if (n++==7) { if (n ++== 5) { n = 0; r_cons_newline (); } } } } }
static bool nextpal_item(RCore *core, int mode, const char *file, int ctr) { const char *fn = r_str_lchr (file, '/'); if (!fn) fn = file; switch (mode) { case 'j': // json r_cons_printf ("%s\"%s\"", ctr?",":"", fn); break; case 'l': // list r_cons_println (fn); break; case 'p': // previous // TODO: move logic here break; case 'n': // next if (getNext) { curtheme = r_str_dup (curtheme, fn); getNext = false; return false; } else if (curtheme) { if (!strcmp (curtheme, fn)) { getNext = true; } } else { curtheme = r_str_dup (curtheme, fn); return false; } break; } return true; }
R_API void r_cons_gotoxy(int x, int y) { #if 0 #if __WINDOWS__ static HANDLE hStdout = NULL; COORD coord; coord.X = x; coord.Y = y; if (!hStdout) hStdout = GetStdHandle (STD_OUTPUT_HANDLE); SetConsoleCursorPosition (hStdout, coord); #else r_cons_printf ("\x1b[%d;%dH", y, x); #endif #endif r_cons_printf ("\x1b[%d;%dH", y, x); }
R_API void r_cons_pal_show () { const int inc = 3; int i, j, k, n = 0; for (i=0; colors[i].name; i++) { r_cons_printf ("%s%s__"Color_RESET" %s\n", colors[i].code, colors[i].bgcode, colors[i].name); } r_cons_printf ("\nGreyscale:\n"); for (i=n=0; i<=0xf; i+=1) { char fg[32], bg[32]; int r = i*16; if (i<5) strcpy (fg, Color_WHITE); else r_cons_rgb_str (fg, 0, 0, 0, 0); r_cons_rgb_str (bg, r, r, r, 1); r_cons_printf ("%s%s rgb:%x%x%x " Color_RESET, fg, bg, i, i, i); if (n++==5) { n = 0; r_cons_newline(); } } r_cons_printf ("\n\nRGB:\n"); for (i=n=0; i<=0xf; i+=inc) { for (k=0; k<=0xf; k+=inc) { for (j=0; j<=0xf; j+=inc) { char fg[32], bg[32]; int r = i*16; int g = j*16; int b = k*16; if ((i<6) && (j<5) ) strcpy (fg, Color_WHITE); //if (i<2 && j<6 && k<13) else r_cons_rgb_str (fg, 0, 0, 0, 0); r_cons_rgb_str (bg, r, g, b, 1); r_cons_printf ("%s%s rgb:%x%x%x "Color_RESET, fg, bg, i, j, k); //if (n++==7) { if (n++==5) { n = 0; r_cons_newline(); } } } } }
static int config_scrfkey_callback(void *user, void *data) { RConfigNode *node = (RConfigNode*) data; if (!strcmp (node->value, "help") || *node->value == '?') { r_cons_printf ("scr.fkey = fun, hit, flag\n"); return R_FALSE; } return R_TRUE; }
static void handle_sha512 (const ut8 *block, int len) { int i = 0; RHash *ctx = r_hash_new (R_TRUE, R_HASH_SHA512); const ut8 *c = r_hash_do_sha512 (ctx, block, len); for (i=0; i<R_HASH_SIZE_SHA512; i++) r_cons_printf ("%02x", c[i]); r_cons_newline (); r_hash_free (ctx); }
R_API int r_core_yank_dump (RCore *core, ut64 pos) { int res = R_FALSE, i = 0; int ybl = core->yank_buf->length; if (ybl>0) { if (pos<ybl) { r_cons_printf ("0x%08"PFMT64x " %d ", core->yank_buf->base+pos, core->yank_buf->length-pos); for (i = pos; i < core->yank_buf->length; i++) r_cons_printf ("%02x", core->yank_buf->buf[i]); r_cons_newline (); res = R_TRUE; } else eprintf ("Position exceeds buffer length.\n"); } else eprintf ("No buffer yanked already\n"); return res; }
static void rtti_itanium_print_class_type_info (class_type_info *cti, ut64 addr, const char *prefix) { r_cons_printf ("%sType Info at 0x%08"PFMT64x ":\n" "%s Reference to RTTI's type class: 0x%08"PFMT32x "\n" "%s Reference to type's name: 0x%08"PFMT32x "\n", prefix, addr, prefix, cti->vtable_addr, prefix, cti->name_addr); }
static void pair(const char *a, const char *b, int mode, bool last) { if (!b || !(*b)) return; if (IS_MODE_JSON (mode)) { const char *lst = last ? "" : ","; r_cons_printf ("\"%s\":%s%s", a, b, lst); } else { char ws[16]; int al = strlen (a); al = PAIR_WIDTH - al; if (al < 0) al = 0; memset (ws, ' ', al); ws[al] = 0; r_cons_printf ("%s%s%s\n", a, ws, b); } }
static void handle_md5 (const ut8 *block, int len) { int i = 0; RHash *ctx = r_hash_new (true, R_HASH_MD5); const ut8 *c = r_hash_do_md5 (ctx, block, len); for (i=0; i<R_HASH_SIZE_MD5; i++) r_cons_printf ("%02x", c[i]); r_cons_newline (); r_hash_free (ctx); }
static void algolist(int mode) { const char *name; ut64 bits; int i; for (i=0; ; i++) { bits = 1<<i; name = r_hash_name (bits); if (!name||!*name) break; if (mode) { r_cons_printf ("%s\n", name); } else { r_cons_printf (" #%s", name); if (!((i+1)%10)) r_cons_newline (); } } if (!mode) r_cons_printf ("\n"); }
R_API void r_core_rtr_cmd(RCore *core, const char *input) { char bufw[1024], bufr[8]; const char *cmd = NULL, *cmd_output = NULL; int i, cmd_len, fd = atoi (input); if (*input==':' && !strchr (input+1, ':')) { r_core_cmdf (core, "o rap://%s", input); return; } if (fd != 0) { if (rtr_host[rtr_n].fd) for (rtr_n = 0; rtr_host[rtr_n].fd->fd != fd && rtr_n < RTR_MAX_HOSTS - 1; rtr_n++); if (!(cmd = strchr (input, ' '))) { eprintf ("Error\n"); return; } } else cmd = input; if (!rtr_host[rtr_n].fd){ eprintf ("Error: Unknown host\n"); core->num->value = 1; // fail return; } if (!rtr_host[rtr_n].proto == RTR_PROT_RAP){ eprintf ("Error: Not a rap:// host\n"); return; } core->num->value = 0; // that's fine if (!strlen (cmd)) { // just check if we can connect r_socket_close (rtr_host[rtr_n].fd); return; } /* send */ bufw[0] = RTR_RAP_CMD; i = strlen (cmd) + 1; r_mem_copyendian ((ut8*)bufw+1, (ut8*)&i, 4, endian); memcpy (bufw+5, cmd, i); r_socket_write (rtr_host[rtr_n].fd, bufw, 5+i); /* read */ r_socket_read (rtr_host[rtr_n].fd, (ut8*)bufr, 5); if (bufr[0] != (char)(RTR_RAP_CMD|RTR_RAP_REPLY)) { eprintf ("Error: Wrong reply\n"); return; } r_mem_copyendian ((ut8*)&cmd_len, (ut8*)bufr+1, 4, endian); cmd_output = malloc (cmd_len); if (!cmd_output) { eprintf ("Error: Allocating cmd output\n"); return; } r_socket_read (rtr_host[rtr_n].fd, (ut8*)cmd_output, cmd_len); r_cons_printf ("%s\n", cmd_output); free ((void *)cmd_output); }
static void rtti_itanium_print_vmi_class_type_info_json (vmi_class_type_info *vmi_cti, ut64 addr) { r_cons_printf ("{\"vmi_type_info\": {\"found_at\":%"PFMT32u",\"ref_to_type_class\":%"PFMT32u"," "\"ref_to_type_name\":%"PFMT32u",\"flags\":%"PFMT32d"," "\"count_of_base_classes\":%"PFMT32d",", addr, vmi_cti->vtable_addr, vmi_cti->name_addr, vmi_cti->vmi_flags, vmi_cti->vmi_base_count); r_cons_printf ("\"base_classes\":["); int i; for (i = 0; i < vmi_cti->vmi_base_count; i++) { r_cons_printf("{\"type_desc_addr\":%"PFMT32u",\"flags\":%"PFMT32d"}", vmi_cti->vmi_bases[i].base_class_addr, vmi_cti->vmi_bases[i].flags); if (i < vmi_cti->vmi_base_count - 1) { r_cons_printf(","); } } r_cons_printf ("]}}"); }
// Below this line are the r_io_zip plugin APIs static RIODesc *r_io_zip_open(RIO *io, const char *file, int rw, int mode) { RIODesc *result = NULL; RIOZipFileObj *zipFileObj = NULL; char *zip_uri = NULL, *zip_filename = NULL, *filename_in_zipfile = NULL; if (!r_io_zip_plugin_open (io, file)) { return result; } zip_uri = strdup(file); // 1) Tokenize to the '//' and find the base file directory ('/') zip_filename = strstr(zip_uri, "//"); if (zip_filename && zip_filename[2]) { if (zip_filename[0] && zip_filename[0] == '/' && zip_filename[1] && zip_filename[1] == '/' ) { *zip_filename++ = 0; } *zip_filename++ = 0; filename_in_zipfile = strstr(zip_filename, "//"); if (filename_in_zipfile && filename_in_zipfile[2]) { // null terminating uri to filename here. *filename_in_zipfile++ = 0; *filename_in_zipfile++ = 0; } } if (!zip_filename) {// && !filename_in_zipfile) { if (zip_uri) free(zip_uri); eprintf("usage: zip:///path/to/archive//filepath\n"); eprintf("Archive was not found.\n"); return result; } // Failed to find the file name the archive. if (!filename_in_zipfile) { RList *files = NULL; RListIter *iter, *iter_tmp; char *name; //eprintf("usage: zip:///path/to/archive//filepath\n"); eprintf("\nFiles in archive\n\n"); files = r_io_zip_get_files(zip_filename, 0, mode, rw ); if (files) { int i = 0; r_list_foreach_safe(files, iter, iter_tmp, name) { // XXX - io->printf does not flush // io->printf("%s\n", name); r_cons_printf("%s\n", name); r_cons_flush (); free (name); r_list_delete (files, iter); } r_list_free (files); }
static inline void rgbinit(int r, int g, int b) { #if __UNIX__ r_cons_printf ("\x1b]4;%d;rgb:%2.2x/%2.2x/%2.2x\x1b\\", 16 + (r * 36) + (g * 6) + b, (r ? (r * 40 + 55) : 0), (g ? (g * 40 + 55) : 0), (b ? (b * 40 + 55) : 0)); #endif }
static void printpage (const char *line, int *index, int from, int to) { int i; r_cons_clear00 (); for (i=from; i<to; i++) { // TODO: chop column width, clear lines r_cons_printf ("%s\n", line+index[i]); } r_cons_flush (); }
static int bin_pdb(RCore *core, int mode) { R_PDB pdb = {0}; ut64 baddr = r_bin_get_baddr (core->bin); pdb.cb_printf = r_cons_printf; if (!init_pdb_parser (&pdb, core->bin->file)) { return false; } if (!pdb.pdb_parse (&pdb)) { eprintf ("pdb was not parsed\n"); pdb.finish_pdb_parse (&pdb); return false; } if (mode == R_CORE_BIN_JSON) r_cons_printf("["); switch (mode) { case R_CORE_BIN_SET: mode = 's'; r_core_cmd0 (core, ".iP*"); return true; case R_CORE_BIN_JSON: mode = 'j'; break; case '*': case 1: mode = 'r'; break; default: mode = 'd'; // default break; } pdb.print_types (&pdb, mode); if (mode == 'j') r_cons_printf (","); pdb.print_gvars (&pdb, baddr, mode); if (mode == 'j') r_cons_printf ("]"); pdb.finish_pdb_parse (&pdb); return true; }
static void pair(const char *a, const char *b) { char ws[16]; int al = strlen (a); if (!b) b = ""; memset (ws, ' ', sizeof (ws)); al = PAIR_WIDTH-al; if (al<0) al = 0; ws[al] = 0; r_cons_printf ("%s%s%s\n", a, ws, b); }
static void printPadded(RCore *core, int pad) { if (pad < 1) { pad = 8; } char *fmt = r_str_newf ("0x%%0%d" PFMT64x, pad); char *off = r_str_newf (fmt, core->offset); r_cons_printf ("%s\n", off); free (off); free (fmt); }
R_API int r_cons_any_key(const char *msg) { if (msg && *msg) { r_cons_printf ("\n-- %s --\n", msg); } else { r_cons_print ("\n--press any key--\n"); } r_cons_flush (); return r_cons_readchar (); //r_cons_strcat ("\x1b[2J\x1b[0;0H"); // wtf? }
R_API char *r_core_project_info(RCore *core, const char *prjfile) { char buf[256], *file = NULL, *prj = r_core_project_file (prjfile); FILE *fd = prj? r_sandbox_fopen (prj, "r"): NULL; for (;fd;) { fgets (buf, sizeof (buf), fd); if (feof (fd)) break; if (!memcmp (buf, "e file.path = ", 14)) { buf[strlen(buf)-1]=0; file = r_str_new (buf+14); break; } } fclose (fd); r_cons_printf ("Project : %s\n", prj); if (file) r_cons_printf ("FilePath: %s\n", file); free (prj); return file; }
R_API void r_core_rtr_help(RCore *core) { r_cons_printf ( " = ; list all open connections\n" " =<[fd] cmd ; send output of local command to remote fd\n" " =[fd] cmd ; exec cmd at remote 'fd' (last open is default one)\n" " =! cmd ; run command via r_io_system\n" " =+ [proto://]host ; add host (default=rap://, tcp://, udp://)\n" " =-[fd] ; remove all hosts or host 'fd'\n" " ==[fd] ; open remote session with host 'fd', 'q' to quit\n"); }
static void r_cons_pal_show_gs () { int i, n; r_cons_printf ("\nGreyscale:\n"); for (i = 0x08, n = 0; i <= 0xee; i += 0xa) { char fg[32], bg[32]; if (i < 0x76) strcpy (fg, Color_WHITE); else strcpy (fg, Color_BLACK); r_cons_rgb_str (bg, i, i, i, 1); r_cons_printf ("%s%s rgb:%02x%02x%02x "Color_RESET, fg, bg, i, i, i); if (n++ == 5) { n = 0; r_cons_newline (); } } }
static void rtti_itanium_print_vmi_class_type_info (vmi_class_type_info *vmi_cti, ut64 addr, const char *prefix) { r_cons_printf ("%sVMI Type Info at 0x%08"PFMT64x ":\n" "%s Reference to RTTI's type class: 0x%08"PFMT32x "\n" "%s Reference to type's name: 0x%08"PFMT32x "\n" "%s Flags: 0x%x" "\n" "%s Count of base classes: 0x%x" "\n", prefix, addr, prefix, vmi_cti->vtable_addr, prefix, vmi_cti->name_addr, prefix, vmi_cti->vmi_flags, prefix, vmi_cti->vmi_base_count); int i; for (i = 0; i < vmi_cti->vmi_base_count; i++) { r_cons_printf("%s Base class type descriptor address: 0x%08"PFMT32x "\n" "%s Base class flags: 0x%x" "\n", prefix, vmi_cti->vmi_bases[i].base_class_addr, prefix, vmi_cti->vmi_bases[i].flags); } }