static void cmd_write_pcache(RCore *core, const char *input) { RIODesc *desc; RIOCache *c; RList *caches; RListIter *iter; int fd, i; bool rad = false; if (core && core->io && core->io->p_cache && core->print && core->print->cb_printf) { switch (input[0]) { case 'i' : if (input[1]) { fd = (int)r_num_math (core->num, input + 1); desc = r_io_desc_get (core->io, fd); } else { desc = core->io->desc; } r_io_desc_cache_commit (desc); break; case '*': rad = true; case ' ': //fall-o-through case '\0': if (input[0] && input[1]) { fd = (int)r_num_math (core->num, input + 1); desc = r_io_desc_get (core->io, fd); } else { desc = core->io->desc; } if ((caches = r_io_desc_cache_list (desc))) { if (rad) { core->print->cb_printf ("e io.va = false\n"); r_list_foreach (caches, iter, c) { core->print->cb_printf ("wx %02x", c->data[0]); const int cacheSize = r_itv_size (c->itv); for (i = 1; i < cacheSize; i++) { core->print->cb_printf ("%02x", c->data[i]); } core->print->cb_printf (" @ 0x%08"PFMT64x" \n", r_itv_begin (c->itv)); } } else { r_list_foreach (caches, iter, c) { core->print->cb_printf ("0x%08"PFMT64x": %02x", r_itv_begin (c->itv), c->odata[0]); const int cacheSize = r_itv_size (c->itv); for (i = 1; i < cacheSize; i++) { core->print->cb_printf ("%02x", c->odata[i]); } core->print->cb_printf (" -> %02x", c->data[0]); for (i = 1; i < cacheSize; i++) { core->print->cb_printf ("%02x", c->data[i]); } core->print->cb_printf ("\n"); } } r_list_free (caches); }
static RThreadFunctionRet rabin_delegate(RThread *th) { RIODesc *d = r_io_desc_get (r.io, r.file->fd); if (rabin_cmd && r_file_exists (d->name)) { char *nptr, *ptr, *cmd = r_sys_cmd_str (rabin_cmd, NULL, NULL); ptr = cmd; if (ptr) { do { if (th) { r_th_lock_enter (th->user); } nptr = strchr (ptr, '\n'); if (nptr) { *nptr = 0; } r_core_cmd (&r, ptr, 0); if (nptr) { ptr = nptr + 1; } if (th) { r_th_lock_leave (th->user); } } while (nptr); } //r_core_cmd (&r, cmd, 0); free (rabin_cmd); rabin_cmd = NULL; } if (th) { eprintf ("rabin2: done\n"); } return R_TH_STOP; }
R_API int r_io_set_fdn(RIO *io, int fd) { if (fd != -1 && io->fd != NULL && fd != io->fd->fd) { RIODesc *desc = r_io_desc_get (io, fd); if (!desc) return R_FALSE; io->fd = desc; io->plugin = desc->plugin; return R_TRUE; } return R_FALSE; }
R_API int r_core_setup_debugger (RCore *r, const char *debugbackend, bool attach) { int pid, *p = NULL; bool is_gdb = !strcmp (debugbackend, "gdb"); RIODesc * fd = r->file ? r_io_desc_get (r->io, r->file->fd) : NULL; const char *prompt = NULL; p = fd ? fd->data : NULL; r_config_set_i (r->config, "cfg.debug", 1); if (!p) { eprintf ("Invalid debug io\n"); return false; } r_config_set (r->config, "io.ff", "true"); r_core_cmdf (r, "dL %s", debugbackend); if (!is_gdb) { pid = r_io_desc_get_pid (fd); r_core_cmdf (r, "dp=%d", pid); if (attach) { r_core_cmdf (r, "dpa %d", pid); } } //this makes to attach twice showing warnings in the output //we get "resource busy" so it seems isn't an issue r_core_cmd (r, ".dr*", 0); /* honor dbg.bep */ { const char *bep = r_config_get (r->config, "dbg.bep"); if (bep) { if (!strcmp (bep, "loader")) { /* do nothing here */ } else if (!strcmp (bep, "entry")) { r_core_cmd (r, "dcu entry0", 0); } else { r_core_cmdf (r, "dcu %s", bep); } } } r_core_cmd (r, "sr PC", 0); /* set the prompt if it's not been set already by the callbacks */ prompt = r_config_get (r->config, "cmd.prompt"); if (prompt && !strcmp (prompt, "")) { if (r_config_get_i (r->config, "dbg.status")) { r_config_set (r->config, "cmd.prompt", ".dr*;drd;sr PC;pi 1;s-"); } else { r_config_set (r->config, "cmd.prompt", ".dr*"); } } r_config_set (r->config, "cmd.vprompt", ".dr*"); return true; }
// TODO: remove int fd here??? R_API int r_io_close(RIO *io, RIODesc *fd) { if (io == NULL || fd == NULL) return -1; int nfd = fd->fd; if (r_io_set_fd (io, fd)) { RIODesc *desc = r_io_desc_get (io, fd->fd); if (desc) { r_io_map_del (io, fd->fd); r_io_plugin_close (io, fd->fd, io->plugin); if (io->plugin && io->plugin->close) return io->plugin->close (desc); r_io_desc_del (io, desc->fd); } } io->fd = NULL; // unset current fd return close (nfd); }
R_API int r_core_file_reopen(RCore *core, const char *args, int perm, int loadbin) { int isdebug = r_config_get_i (core->config, "cfg.debug"); char *path; ut64 laddr = r_config_get_i (core->config, "bin.laddr"); RCoreFile *file = NULL; RCoreFile *ofile = core->file; RBinFile *bf = ofile ? r_bin_file_find_by_fd (core->bin, ofile->fd) : NULL; RIODesc *odesc = (core->io && ofile) ? r_io_desc_get (core->io, ofile->fd) : NULL; char *ofilepath = NULL, *obinfilepath = (bf && bf->file)? strdup (bf->file): NULL; int ret = false; ut64 origoff = core->offset; if (odesc) { if (odesc->referer) { ofilepath = odesc->referer; } else if (odesc->uri) { ofilepath = odesc->uri; } } if (r_sandbox_enable (0)) { eprintf ("Cannot reopen in sandbox\n"); free (obinfilepath); return false; } if (!core->file) { eprintf ("No file opened to reopen\n"); free (ofilepath); free (obinfilepath); return false; } int newpid = odesc? odesc->fd: -1; if (isdebug) { r_debug_kill (core->dbg, core->dbg->pid, core->dbg->tid, 9); // KILL perm = 7; } else { if (!perm) { perm = 4; //R_IO_READ; } } if (!ofilepath) { eprintf ("Unknown file path"); free (obinfilepath); return false; } // HACK: move last mapped address to higher place // XXX - why does this hack work? // when the new memory maps are created. path = strdup (ofilepath); free (obinfilepath); obinfilepath = strdup (ofilepath); // r_str_trim (path); file = r_core_file_open (core, path, perm, laddr); if (file) { bool had_rbin_info = false; if (ofile) { if (r_bin_file_delete (core->bin, ofile->fd)) { had_rbin_info = true; } } r_core_file_close (core, ofile); r_core_file_set_by_file (core, file); ofile = NULL; odesc = NULL; // core->file = file; eprintf ("File %s reopened in %s mode\n", path, (perm & R_IO_WRITE)? "read-write": "read-only"); if (loadbin && (loadbin == 2 || had_rbin_info)) { ut64 baddr = r_config_get_i (core->config, "bin.baddr"); ret = r_core_bin_load (core, obinfilepath, baddr); r_core_bin_update_arch_bits (core); if (!ret) { eprintf ("Error: Failed to reload rbin for: %s", path); } } if (core->bin->cur && core->io && r_io_desc_get (core->io, file->fd) && !loadbin) { //force here NULL because is causing uaf look this better in future XXX @alvarofe core->bin->cur = NULL; } // close old file } else if (ofile) { eprintf ("r_core_file_reopen: Cannot reopen file: %s with perms 0x%04x," " attempting to open read-only.\n", path, perm); // lower it down back //ofile = r_core_file_open (core, path, R_IO_READ, addr); r_core_file_set_by_file (core, ofile); } else { eprintf ("Cannot reopen\n"); } if (isdebug) { int newtid = newpid; // XXX - select the right backend if (core->file) { newpid = r_io_fd_get_pid (core->io, core->file->fd); newtid = r_io_fd_get_tid (core->io, core->file->fd); #if __linux__ core->dbg->main_pid = newpid; newtid = newpid; #endif #pragma message ("fix debugger-concept in core") #if __WINDOWS__ r_debug_select (core->dbg, newpid, newtid); core->dbg->reason.type = R_DEBUG_REASON_NONE; #endif } //reopen and attach r_core_setup_debugger (core, "native", true); r_debug_select (core->dbg, newpid, newtid); } if (core->file) { r_io_use_fd (core->io, core->file->fd); core->switch_file_view = 1; r_core_block_read (core); #if 0 else {
static bool is_valid_gdb_file(RCoreFile *fh) { RIODesc *d = fh && fh->core ? r_io_desc_get (fh->core->io, fh->fd) : NULL; return d && strncmp (d->name, "gdb://", 6); }