2020-02-17 11:47:57 +00:00
|
|
|
/* radare - LGPL - Copyright 2009-2020 - pancake */
|
2019-03-08 13:22:59 +00:00
|
|
|
|
|
|
|
#define USE_THREADS 1
|
|
|
|
#define ALLOW_THREADED 0
|
|
|
|
#define UNCOLORIZE_NONTTY 0
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#ifndef WIN32_LEAN_AND_MEAN
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2020-02-17 11:47:57 +00:00
|
|
|
#include <r_core.h>
|
2019-03-08 13:22:59 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char* get_file_in_cur_dir(const char *filepath) {
|
|
|
|
filepath = r_file_basename (filepath);
|
|
|
|
if (r_file_exists (filepath) && !r_file_is_directory (filepath)) {
|
|
|
|
return r_file_abspath (filepath);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int r_main_version_verify(int show) {
|
|
|
|
int i, ret;
|
|
|
|
typedef const char* (*vc)();
|
|
|
|
const char *base = R2_GITTAP;
|
|
|
|
struct vcs_t {
|
|
|
|
const char *name;
|
|
|
|
vc callback;
|
|
|
|
} vcs[] = {
|
2020-02-17 11:47:57 +00:00
|
|
|
{ "r_anal", r_anal_version },
|
|
|
|
{ "r_lib", r_lib_version },
|
|
|
|
{ "r_egg", r_egg_version },
|
|
|
|
{ "r_asm", r_asm_version },
|
|
|
|
{ "r_bin", r_bin_version },
|
|
|
|
{ "r_cons", r_cons_version },
|
|
|
|
{ "r_flag", r_flag_version },
|
|
|
|
{ "r_core", r_core_version },
|
|
|
|
{ "r_crypto", r_crypto_version },
|
|
|
|
{ "r_bp", r_bp_version },
|
|
|
|
{ "r_debug", r_debug_version },
|
|
|
|
{ "r_main", r_main_version },
|
|
|
|
{ "r_hash", r_hash_version },
|
|
|
|
{ "r_fs", r_fs_version },
|
|
|
|
{ "r_io", r_io_version },
|
2019-03-08 13:22:59 +00:00
|
|
|
#if !USE_LIB_MAGIC
|
2020-02-17 11:47:57 +00:00
|
|
|
{ "r_magic", r_magic_version },
|
2019-03-08 13:22:59 +00:00
|
|
|
#endif
|
2020-02-17 11:47:57 +00:00
|
|
|
{ "r_parse", r_parse_version },
|
|
|
|
{ "r_reg", r_reg_version },
|
|
|
|
{ "r_sign", r_sign_version },
|
|
|
|
{ "r_search", r_search_version },
|
|
|
|
{ "r_syscall", r_syscall_version },
|
|
|
|
{ "r_util", r_util_version },
|
2019-03-08 13:22:59 +00:00
|
|
|
/* ... */
|
|
|
|
{NULL,NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (show) {
|
|
|
|
printf ("%s r2\n", base);
|
|
|
|
}
|
|
|
|
for (i = ret = 0; vcs[i].name; i++) {
|
|
|
|
struct vcs_t *v = &vcs[i];
|
|
|
|
const char *name = v->callback ();
|
|
|
|
if (!ret && strcmp (base, name)) {
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
if (show) {
|
|
|
|
printf ("%s %s\n", name, v->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ret) {
|
|
|
|
if (show) {
|
|
|
|
eprintf ("WARNING: r2 library versions mismatch!\n");
|
|
|
|
} else {
|
|
|
|
eprintf ("WARNING: r2 library versions mismatch! See r2 -V\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int main_help(int line) {
|
|
|
|
if (line < 2) {
|
|
|
|
printf ("Usage: r2 [-ACdfLMnNqStuvwzX] [-P patch] [-p prj] [-a arch] [-b bits] [-i file]\n"
|
|
|
|
" [-s addr] [-B baddr] [-m maddr] [-c cmd] [-e k=v] file|pid|-|--|=\n");
|
|
|
|
}
|
|
|
|
if (line != 1) {
|
|
|
|
printf (
|
|
|
|
" -- run radare2 without opening any file\n"
|
|
|
|
" - same as 'r2 malloc://512'\n"
|
|
|
|
" = read file from stdin (use -i and -c to run cmds)\n"
|
|
|
|
" -= perform !=! command to run all commands remotely\n"
|
|
|
|
" -0 print \\x00 after init and every command\n"
|
|
|
|
" -2 close stderr file descriptor (silent warning messages)\n"
|
|
|
|
" -a [arch] set asm.arch\n"
|
|
|
|
" -A run 'aaa' command to analyze all referenced code\n"
|
|
|
|
" -b [bits] set asm.bits\n"
|
|
|
|
" -B [baddr] set base address for PIE binaries\n"
|
|
|
|
" -c 'cmd..' execute radare command\n"
|
|
|
|
" -C file is host:port (alias for -c+=http://%%s/cmd/)\n"
|
|
|
|
" -d debug the executable 'file' or running process 'pid'\n"
|
|
|
|
" -D [backend] enable debug mode (e cfg.debug=true)\n"
|
|
|
|
" -e k=v evaluate config var\n"
|
|
|
|
" -f block size = file size\n"
|
|
|
|
" -F [binplug] force to use that rbin plugin\n"
|
|
|
|
" -h, -hh show help message, -hh for long\n"
|
|
|
|
" -H ([var]) display variable\n"
|
|
|
|
" -i [file] run script file\n"
|
|
|
|
" -I [file] run script file before the file is opened\n"
|
|
|
|
" -k [OS/kern] set asm.os (linux, macos, w32, netbsd, ...)\n"
|
|
|
|
" -l [lib] load plugin file\n"
|
|
|
|
" -L list supported IO plugins\n"
|
|
|
|
" -m [addr] map file at given address (loadaddr)\n"
|
|
|
|
" -M do not demangle symbol names\n"
|
|
|
|
" -n, -nn do not load RBin info (-nn only load bin structures)\n"
|
|
|
|
" -N do not load user settings and scripts\n"
|
2020-04-05 13:02:46 +00:00
|
|
|
" -NN do not load any script or plugin\n"
|
2019-03-08 13:22:59 +00:00
|
|
|
" -q quiet mode (no prompt) and quit after -i\n"
|
2019-07-01 22:19:54 +00:00
|
|
|
" -qq quit after running all -c and -i\n"
|
2019-03-08 13:22:59 +00:00
|
|
|
" -Q quiet mode (no prompt) and quit faster (quickLeak=true)\n"
|
|
|
|
" -p [prj] use project, list if no arg, load if no file\n"
|
|
|
|
" -P [file] apply rapatch file and quit\n"
|
|
|
|
" -r [rarun2] specify rarun2 profile to load (same as -e dbg.profile=X)\n"
|
|
|
|
" -R [rr2rule] specify custom rarun2 directive\n"
|
|
|
|
" -s [addr] initial seek\n"
|
|
|
|
" -S start r2 in sandbox mode\n"
|
|
|
|
#if USE_THREADS && ALLOW_THREADED
|
|
|
|
" -t load rabin2 info in thread\n"
|
|
|
|
#endif
|
|
|
|
" -T do not compute file hashes\n"
|
|
|
|
" -u set bin.filter=false to get raw sym/sec/cls names\n"
|
|
|
|
" -v, -V show radare2 version (-V show lib versions)\n"
|
|
|
|
" -w open file in write mode\n"
|
|
|
|
" -x open without exec-flag (asm.emu will not work), See io.exec\n"
|
|
|
|
" -X same as -e bin.usextr=false (useful for dyldcache)\n"
|
|
|
|
" -z, -zz do not load strings or load them even in raw\n");
|
|
|
|
}
|
|
|
|
if (line == 2) {
|
|
|
|
char *datahome = r_str_home (R2_HOME_DATADIR);
|
|
|
|
const char *dirPrefix = r_sys_prefix (NULL);
|
|
|
|
printf (
|
|
|
|
"Scripts:\n"
|
|
|
|
" system ${R2_PREFIX}/share/radare2/radare2rc\n"
|
|
|
|
" user ~/.radare2rc " R_JOIN_2_PATHS ("~", R2_HOME_RC) " (and " R_JOIN_3_PATHS ("~", R2_HOME_RC_DIR,"") ")\n"
|
|
|
|
" file ${filename}.r2\n"
|
|
|
|
"Plugins:\n"
|
|
|
|
" binrc " R_JOIN_4_PATHS ("~", R2_HOME_BINRC, "bin-<format>", "") " (elf, elf64, mach0, ..)\n"
|
|
|
|
" R2_USER_PLUGINS " R_JOIN_2_PATHS ("~", R2_HOME_PLUGINS) "\n"
|
|
|
|
" R2_LIBR_PLUGINS " R_JOIN_2_PATHS ("%s", R2_PLUGINS) "\n"
|
|
|
|
" R2_USER_ZIGNS " R_JOIN_2_PATHS ("~", R2_HOME_ZIGNS) "\n"
|
|
|
|
"Environment:\n"
|
2020-05-18 11:21:50 +00:00
|
|
|
" R2_CFG_NEWSHELL sets cfg.newshell=true\n"
|
|
|
|
" R2_DEBUG if defined, show error messages and crash signal\n"
|
|
|
|
" R2_DEBUG_ASSERT=1 set a breakpoint when hitting an assert\n"
|
2019-03-08 13:22:59 +00:00
|
|
|
" R2_MAGICPATH " R_JOIN_2_PATHS ("%s", R2_SDB_MAGIC) "\n"
|
|
|
|
" R2_NOPLUGINS do not load r2 shared plugins\n"
|
2020-05-18 11:21:50 +00:00
|
|
|
" R2_RCFILE ~/.radare2rc (user preferences, batch script)\n" // TOO GENERIC
|
|
|
|
" R2_RDATAHOME %s\n" // TODO: rename to RHOME R2HOME?
|
2019-03-08 13:22:59 +00:00
|
|
|
"Paths:\n"
|
|
|
|
" R2_PREFIX "R2_PREFIX"\n"
|
|
|
|
" R2_INCDIR "R2_INCDIR"\n"
|
|
|
|
" R2_LIBDIR "R2_LIBDIR"\n"
|
|
|
|
" R2_LIBEXT "R_LIB_EXT"\n"
|
|
|
|
, dirPrefix, datahome, dirPrefix);
|
|
|
|
free (datahome);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int main_print_var(const char *var_name) {
|
|
|
|
int i = 0;
|
2019-09-22 08:42:49 +00:00
|
|
|
#ifdef __WINDOWS__
|
|
|
|
char *incdir = r_str_r2_prefix (R2_INCDIR);
|
|
|
|
char *libdir = r_str_r2_prefix (R2_LIBDIR);
|
|
|
|
#else
|
|
|
|
char *incdir = strdup (R2_INCDIR);
|
|
|
|
char *libdir = strdup (R2_LIBDIR);
|
|
|
|
#endif
|
2019-03-08 13:22:59 +00:00
|
|
|
char *confighome = r_str_home (R2_HOME_CONFIGDIR);
|
|
|
|
char *datahome = r_str_home (R2_HOME_DATADIR);
|
|
|
|
char *cachehome = r_str_home (R2_HOME_CACHEDIR);
|
|
|
|
char *homeplugins = r_str_home (R2_HOME_PLUGINS);
|
|
|
|
char *homezigns = r_str_home (R2_HOME_ZIGNS);
|
|
|
|
char *plugins = r_str_r2_prefix (R2_PLUGINS);
|
|
|
|
char *magicpath = r_str_r2_prefix (R2_SDB_MAGIC);
|
|
|
|
struct radare2_var_t {
|
|
|
|
const char *name;
|
|
|
|
const char *value;
|
|
|
|
} r2_vars[] = {
|
|
|
|
{ "R2_PREFIX", R2_PREFIX },
|
|
|
|
{ "R2_MAGICPATH", magicpath },
|
|
|
|
{ "R2_PREFIX", R2_PREFIX },
|
2019-09-22 08:42:49 +00:00
|
|
|
{ "R2_INCDIR", incdir },
|
|
|
|
{ "R2_LIBDIR", libdir },
|
2019-03-08 13:22:59 +00:00
|
|
|
{ "R2_LIBEXT", R_LIB_EXT },
|
|
|
|
{ "R2_RCONFIGHOME", confighome },
|
|
|
|
{ "R2_RDATAHOME", datahome },
|
|
|
|
{ "R2_RCACHEHOME", cachehome },
|
|
|
|
{ "R2_LIBR_PLUGINS", plugins },
|
|
|
|
{ "R2_USER_PLUGINS", homeplugins },
|
|
|
|
{ "R2_USER_ZIGNS", homezigns },
|
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
int delta = 0;
|
|
|
|
if (var_name && strncmp (var_name, "R2_", 3)) {
|
|
|
|
delta = 3;
|
|
|
|
}
|
|
|
|
if (var_name) {
|
|
|
|
while (r2_vars[i].name) {
|
|
|
|
if (!strcmp (r2_vars[i].name + delta, var_name)) {
|
|
|
|
printf ("%s\n", r2_vars[i].value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (r2_vars[i].name) {
|
|
|
|
printf ("%s=%s\n", r2_vars[i].name, r2_vars[i].value);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
2019-09-22 08:42:49 +00:00
|
|
|
free (incdir);
|
|
|
|
free (libdir);
|
2019-03-08 13:22:59 +00:00
|
|
|
free (confighome);
|
|
|
|
free (datahome);
|
|
|
|
free (cachehome);
|
|
|
|
free (homeplugins);
|
|
|
|
free (homezigns);
|
|
|
|
free (plugins);
|
|
|
|
free (magicpath);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-02-17 11:47:57 +00:00
|
|
|
static bool run_commands(RCore *r, RList *cmds, RList *files, bool quiet, int do_analysis) {
|
2019-03-08 13:22:59 +00:00
|
|
|
RListIter *iter;
|
|
|
|
const char *cmdn;
|
|
|
|
const char *file;
|
|
|
|
int ret;
|
|
|
|
/* -i */
|
|
|
|
r_list_foreach (files, iter, file) {
|
|
|
|
if (!r_file_exists (file)) {
|
|
|
|
eprintf ("Script '%s' not found.\n", file);
|
2020-02-03 12:15:03 +00:00
|
|
|
goto beach;
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
2020-02-17 11:47:57 +00:00
|
|
|
ret = r_core_run_script (r, file);
|
2019-03-08 13:22:59 +00:00
|
|
|
if (ret == -2) {
|
|
|
|
eprintf ("[c] Cannot open '%s'\n", file);
|
|
|
|
}
|
|
|
|
if (ret < 0 || (ret == 0 && quiet)) {
|
|
|
|
r_cons_flush ();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* -c */
|
|
|
|
r_list_foreach (cmds, iter, cmdn) {
|
2020-02-17 11:47:57 +00:00
|
|
|
//r_core_cmd0 (r, cmdn);
|
2020-03-24 14:17:20 +00:00
|
|
|
r_core_cmd_lines (r, cmdn);
|
2019-03-08 13:22:59 +00:00
|
|
|
r_cons_flush ();
|
|
|
|
}
|
2020-02-03 12:15:03 +00:00
|
|
|
beach:
|
2019-03-08 13:22:59 +00:00
|
|
|
if (quiet) {
|
|
|
|
if (do_analysis) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (cmds && !r_list_empty (cmds)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!r_list_empty (files)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool mustSaveHistory(RConfig *c) {
|
|
|
|
if (!r_config_get_i (c, "scr.histsave")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!r_cons_is_interactive ()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to set the correct scr.color for the current terminal.
|
2020-02-17 11:47:57 +00:00
|
|
|
static void set_color_default(RCore *r) {
|
2019-05-27 16:21:17 +00:00
|
|
|
#ifdef __WINDOWS__
|
|
|
|
char *alacritty = r_sys_getenv ("ALACRITTY_LOG");
|
|
|
|
if (alacritty) {
|
|
|
|
// Despite the setting of env vars to the contrary, Alacritty on
|
|
|
|
// Windows may not actually support >16 colors out-of-the-box
|
|
|
|
// (https://github.com/jwilm/alacritty/issues/1662).
|
|
|
|
// TODO: Windows 10 version check.
|
2020-02-17 11:47:57 +00:00
|
|
|
r_config_set_i (r->config, "scr.color", COLOR_MODE_16);
|
2019-05-27 16:21:17 +00:00
|
|
|
free (alacritty);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2019-03-08 13:22:59 +00:00
|
|
|
char *tmp = r_sys_getenv ("COLORTERM");
|
|
|
|
if (tmp) {
|
|
|
|
if ((r_str_endswith (tmp, "truecolor") || r_str_endswith (tmp, "24bit"))) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_config_set_i (r->config, "scr.color", COLOR_MODE_16M);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tmp = r_sys_getenv ("TERM");
|
|
|
|
if (!tmp) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (r_str_endswith (tmp, "truecolor") || r_str_endswith (tmp, "24bit")) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_config_set_i (r->config, "scr.color", COLOR_MODE_16M);
|
2019-03-08 13:22:59 +00:00
|
|
|
} else if (r_str_endswith (tmp, "256color")) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_config_set_i (r->config, "scr.color", COLOR_MODE_256);
|
2019-03-08 13:22:59 +00:00
|
|
|
} else if (!strcmp (tmp, "dumb")) {
|
|
|
|
// Dumb terminals don't get color by default.
|
2020-02-17 11:47:57 +00:00
|
|
|
r_config_set_i (r->config, "scr.color", COLOR_MODE_DISABLED);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
free (tmp);
|
|
|
|
}
|
|
|
|
|
2020-03-27 16:06:40 +00:00
|
|
|
R_API int r_main_radare2(int argc, const char **argv) {
|
2020-02-17 11:47:57 +00:00
|
|
|
RCore *r;
|
|
|
|
bool forcequit = false;
|
|
|
|
bool haveRarunProfile = false;
|
2019-03-08 13:22:59 +00:00
|
|
|
RListIter *iter;
|
2020-02-17 11:47:57 +00:00
|
|
|
int do_analysis = 0;
|
2019-03-08 13:22:59 +00:00
|
|
|
char *cmdn, *tmp;
|
|
|
|
RCoreFile *fh = NULL;
|
|
|
|
RIODesc *iod = NULL;
|
|
|
|
const char *patchfile = NULL;
|
|
|
|
const char *prj = NULL;
|
|
|
|
int debug = 0;
|
|
|
|
int zflag = 0;
|
2019-05-06 09:31:32 +00:00
|
|
|
bool do_connect = false;
|
2019-03-08 13:22:59 +00:00
|
|
|
bool fullfile = false;
|
|
|
|
int has_project;
|
|
|
|
bool zerosep = false;
|
|
|
|
int help = 0;
|
|
|
|
enum { LOAD_BIN_ALL, LOAD_BIN_NOTHING, LOAD_BIN_STRUCTURES_ONLY } load_bin = LOAD_BIN_ALL;
|
2019-10-10 19:46:34 +00:00
|
|
|
bool run_rc = true;
|
2019-03-08 13:22:59 +00:00
|
|
|
int ret, c, perms = R_PERM_RX;
|
|
|
|
bool sandbox = false;
|
|
|
|
ut64 baddr = UT64_MAX;
|
|
|
|
ut64 seek = UT64_MAX;
|
|
|
|
bool do_list_io_plugins = false;
|
2020-03-27 16:06:40 +00:00
|
|
|
char *file = NULL;
|
|
|
|
char *pfile = NULL;
|
2019-03-08 13:22:59 +00:00
|
|
|
const char *debugbackend = "native";
|
|
|
|
const char *asmarch = NULL;
|
|
|
|
const char *asmos = NULL;
|
|
|
|
const char *forcebin = NULL;
|
|
|
|
const char *asmbits = NULL;
|
|
|
|
char *customRarunProfile = NULL;
|
|
|
|
ut64 mapaddr = 0LL;
|
|
|
|
bool quiet = false;
|
|
|
|
bool quietLeak = false;
|
|
|
|
int is_gdb = false;
|
|
|
|
const char * s_seek = NULL;
|
|
|
|
bool compute_hashes = true;
|
|
|
|
RList *cmds = r_list_new ();
|
|
|
|
RList *evals = r_list_new ();
|
|
|
|
RList *files = r_list_new ();
|
|
|
|
RList *prefiles = r_list_new ();
|
|
|
|
|
|
|
|
#define LISTS_FREE() \
|
|
|
|
{ \
|
|
|
|
r_list_free (cmds); \
|
|
|
|
r_list_free (evals); \
|
|
|
|
r_list_free (files); \
|
|
|
|
r_list_free (prefiles); \
|
|
|
|
}
|
|
|
|
|
|
|
|
bool noStderr = false;
|
|
|
|
|
|
|
|
#ifdef __UNIX
|
|
|
|
sigset_t sigBlockMask;
|
|
|
|
sigemptyset (&sigBlockMask);
|
|
|
|
sigaddset (&sigBlockMask, SIGWINCH);
|
|
|
|
r_signal_sigmask (SIG_BLOCK, &sigBlockMask, NULL);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
char **envp = r_sys_get_environ ();
|
|
|
|
if (envp) {
|
|
|
|
r_sys_set_environ (envp);
|
|
|
|
}
|
|
|
|
|
2020-05-18 11:21:50 +00:00
|
|
|
if (r_sys_getenv_asbool ("R2_DEBUG")) {
|
|
|
|
char *sysdbg = r_sys_getenv ("R2_DEBUG_TOOL");
|
|
|
|
char *fmt = (sysdbg && *sysdbg)
|
|
|
|
? r_str_newf ("%s %%d", sysdbg)
|
|
|
|
#if __APPLE__
|
|
|
|
: r_str_newf ("lldb -p %%d");
|
|
|
|
#else
|
|
|
|
: r_str_newf ("gdb --pid %%d");
|
|
|
|
#endif
|
|
|
|
r_sys_crash_handler (fmt);
|
|
|
|
free (fmt);
|
|
|
|
free (sysdbg);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
if (argc < 2) {
|
|
|
|
LISTS_FREE ();
|
|
|
|
return main_help (1);
|
|
|
|
}
|
2020-02-17 11:47:57 +00:00
|
|
|
r = r_core_new ();
|
2020-06-14 11:21:57 +00:00
|
|
|
if (!r) {
|
|
|
|
eprintf ("Cannot initialize RCore\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2020-02-17 11:47:57 +00:00
|
|
|
r->r_main_radare2 = r_main_radare2;
|
|
|
|
r->r_main_radiff2 = r_main_radiff2;
|
|
|
|
r->r_main_rafind2 = r_main_rafind2;
|
|
|
|
r->r_main_rabin2 = r_main_rabin2;
|
|
|
|
r->r_main_ragg2 = r_main_ragg2;
|
|
|
|
r->r_main_rasm2 = r_main_rasm2;
|
|
|
|
r->r_main_rax2 = r_main_rax2;
|
2019-07-10 16:45:20 +00:00
|
|
|
|
2020-02-17 11:47:57 +00:00
|
|
|
r_core_task_sync_begin (&r->tasks);
|
2019-03-08 13:22:59 +00:00
|
|
|
if (argc == 2 && !strcmp (argv[1], "-p")) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_core_project_list (r, 0);
|
2019-03-08 13:22:59 +00:00
|
|
|
r_cons_flush ();
|
|
|
|
LISTS_FREE ();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// HACK TO PERMIT '#!/usr/bin/r2 - -i' hashbangs
|
|
|
|
if (argc > 2 && !strcmp (argv[1], "-") && !strcmp (argv[2], "-i")) {
|
|
|
|
argv[1] = argv[0];
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -H option without argument
|
|
|
|
if (argc == 2 && !strcmp (argv[1], "-H")) {
|
|
|
|
main_print_var (NULL);
|
|
|
|
LISTS_FREE ();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-02-17 11:47:57 +00:00
|
|
|
set_color_default (r);
|
2020-04-05 13:02:46 +00:00
|
|
|
bool load_l = true;
|
2019-03-08 13:22:59 +00:00
|
|
|
|
2020-03-27 16:06:40 +00:00
|
|
|
RGetopt opt;
|
|
|
|
r_getopt_init (&opt, argc, argv, "=02AMCwxfF:H:hm:e:nk:NdqQs:p:b:B:a:Lui:I:l:P:R:r:c:D:vVSTzuXt");
|
|
|
|
while ((c = r_getopt_next (&opt)) != -1) {
|
2019-03-08 13:22:59 +00:00
|
|
|
switch (c) {
|
|
|
|
case '=':
|
2020-02-17 11:47:57 +00:00
|
|
|
r->cmdremote = 1;
|
2019-03-08 13:22:59 +00:00
|
|
|
break;
|
|
|
|
case '2':
|
|
|
|
noStderr = true;
|
|
|
|
break;
|
|
|
|
case '0':
|
|
|
|
zerosep = true;
|
|
|
|
/* implicit -q */
|
2020-02-17 11:47:57 +00:00
|
|
|
r_config_set (r->config, "scr.interactive", "false");
|
|
|
|
r_config_set (r->config, "scr.prompt", "false");
|
|
|
|
r_config_set_i (r->config, "scr.color", COLOR_MODE_DISABLED);
|
2019-03-08 13:22:59 +00:00
|
|
|
quiet = true;
|
|
|
|
break;
|
|
|
|
case 'u':
|
2020-02-17 11:47:57 +00:00
|
|
|
r_config_set (r->config, "bin.filter", "false");
|
2019-03-08 13:22:59 +00:00
|
|
|
break;
|
|
|
|
case 'a':
|
2020-03-27 16:06:40 +00:00
|
|
|
asmarch = opt.arg;
|
2019-03-08 13:22:59 +00:00
|
|
|
break;
|
|
|
|
case 'z':
|
|
|
|
zflag++;
|
|
|
|
break;
|
|
|
|
case 'A':
|
|
|
|
do_analysis += do_analysis ? 1: 2;
|
|
|
|
break;
|
|
|
|
case 'b':
|
2020-03-27 16:06:40 +00:00
|
|
|
asmbits = opt.arg;
|
2019-03-08 13:22:59 +00:00
|
|
|
break;
|
|
|
|
case 'B':
|
2020-03-27 16:06:40 +00:00
|
|
|
baddr = r_num_math (r->num, opt.arg);
|
2019-03-08 13:22:59 +00:00
|
|
|
break;
|
|
|
|
case 'X':
|
2020-02-17 11:47:57 +00:00
|
|
|
r_config_set (r->config, "bin.usextr", "false");
|
2019-03-08 13:22:59 +00:00
|
|
|
break;
|
|
|
|
case 'c':
|
2020-03-27 16:06:40 +00:00
|
|
|
r_list_append (cmds, (void*)opt.arg);
|
2019-03-08 13:22:59 +00:00
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
do_connect = true;
|
|
|
|
break;
|
|
|
|
#if DEBUGGER
|
|
|
|
case 'd': debug = 1; break;
|
|
|
|
#else
|
|
|
|
case 'd': eprintf ("Sorry. No debugger backend available.\n"); return 1;
|
|
|
|
#endif
|
|
|
|
case 'D':
|
|
|
|
debug = 2;
|
2020-03-27 16:06:40 +00:00
|
|
|
debugbackend = opt.arg;
|
|
|
|
if (!strcmp (opt.arg, "?")) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_debug_plugin_list (r->dbg, 'q');
|
2019-03-08 13:22:59 +00:00
|
|
|
r_cons_flush();
|
|
|
|
LISTS_FREE ();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'e':
|
2020-03-27 16:06:40 +00:00
|
|
|
if (!strcmp (opt.arg, "q")) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_core_cmd0 (r, "eq");
|
2019-03-08 13:22:59 +00:00
|
|
|
} else {
|
2020-03-27 16:06:40 +00:00
|
|
|
r_config_eval (r->config, opt.arg, false);
|
|
|
|
r_list_append (evals, (void*)opt.arg);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
fullfile = true;
|
|
|
|
break;
|
|
|
|
case 'F':
|
2020-03-27 16:06:40 +00:00
|
|
|
forcebin = opt.arg;
|
2019-03-08 13:22:59 +00:00
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
help++;
|
|
|
|
break;
|
|
|
|
case 'H':
|
2020-03-27 16:06:40 +00:00
|
|
|
main_print_var (opt.arg);
|
2019-03-08 13:22:59 +00:00
|
|
|
LISTS_FREE ();
|
|
|
|
return 0;
|
|
|
|
case 'i':
|
2020-03-27 16:06:40 +00:00
|
|
|
r_list_append (files, (void*)opt.arg);
|
2019-03-08 13:22:59 +00:00
|
|
|
break;
|
|
|
|
case 'I':
|
2020-03-27 16:06:40 +00:00
|
|
|
r_list_append (prefiles, (void*)opt.arg);
|
2019-03-08 13:22:59 +00:00
|
|
|
break;
|
|
|
|
case 'k':
|
2020-03-27 16:06:40 +00:00
|
|
|
asmos = opt.arg;
|
2019-03-08 13:22:59 +00:00
|
|
|
break;
|
|
|
|
case 'l':
|
2020-03-27 16:06:40 +00:00
|
|
|
r_lib_open (r->lib, opt.arg);
|
2019-03-08 13:22:59 +00:00
|
|
|
break;
|
|
|
|
case 'L':
|
|
|
|
do_list_io_plugins = true;
|
|
|
|
break;
|
|
|
|
case 'm':
|
2020-03-27 16:06:40 +00:00
|
|
|
mapaddr = r_num_math (r->num, opt.arg);
|
|
|
|
s_seek = opt.arg;
|
2020-02-17 11:47:57 +00:00
|
|
|
r_config_set_i (r->config, "file.offset", mapaddr);
|
2019-03-08 13:22:59 +00:00
|
|
|
break;
|
|
|
|
case 'M':
|
2020-02-17 11:47:57 +00:00
|
|
|
r_config_set (r->config, "bin.demangle", "false");
|
|
|
|
r_config_set (r->config, "asm.demangle", "false");
|
2019-03-08 13:22:59 +00:00
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
if (load_bin == LOAD_BIN_ALL) { // "-n"
|
|
|
|
load_bin = LOAD_BIN_NOTHING;
|
|
|
|
} else if (load_bin == LOAD_BIN_NOTHING) { // second n => "-nn"
|
|
|
|
load_bin = LOAD_BIN_STRUCTURES_ONLY;
|
|
|
|
}
|
2020-02-17 11:47:57 +00:00
|
|
|
r_config_set (r->config, "file.info", "false");
|
2019-03-08 13:22:59 +00:00
|
|
|
break;
|
|
|
|
case 'N':
|
2020-04-05 13:02:46 +00:00
|
|
|
if (run_rc) {
|
|
|
|
run_rc = false;
|
|
|
|
} else {
|
|
|
|
load_l = false;
|
|
|
|
}
|
2019-03-08 13:22:59 +00:00
|
|
|
break;
|
|
|
|
case 'p':
|
2020-03-27 16:06:40 +00:00
|
|
|
if (!strcmp (opt.arg, "?")) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_core_project_list (r, 0);
|
2019-03-08 13:22:59 +00:00
|
|
|
r_cons_flush ();
|
|
|
|
LISTS_FREE ();
|
|
|
|
return 0;
|
|
|
|
}
|
2020-03-27 16:06:40 +00:00
|
|
|
r_config_set (r->config, "prj.name", opt.arg);
|
2019-03-08 13:22:59 +00:00
|
|
|
break;
|
|
|
|
case 'P':
|
2020-03-27 16:06:40 +00:00
|
|
|
patchfile = opt.arg;
|
2019-03-08 13:22:59 +00:00
|
|
|
break;
|
|
|
|
case 'Q':
|
|
|
|
quiet = true;
|
|
|
|
quietLeak = true;
|
|
|
|
break;
|
|
|
|
case 'q':
|
2020-02-17 11:47:57 +00:00
|
|
|
r_config_set (r->config, "scr.interactive", "false");
|
|
|
|
r_config_set (r->config, "scr.prompt", "false");
|
|
|
|
r_config_set (r->config, "cfg.fortunes", "false");
|
2019-07-01 22:19:54 +00:00
|
|
|
if (quiet) {
|
|
|
|
forcequit = true;
|
|
|
|
}
|
2019-03-08 13:22:59 +00:00
|
|
|
quiet = true;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
haveRarunProfile = true;
|
2020-03-27 16:06:40 +00:00
|
|
|
r_config_set (r->config, "dbg.profile", opt.arg);
|
2019-03-08 13:22:59 +00:00
|
|
|
break;
|
|
|
|
case 'R':
|
2020-03-27 16:06:40 +00:00
|
|
|
customRarunProfile = r_str_appendf (customRarunProfile, "%s\n", opt.arg);
|
2019-03-08 13:22:59 +00:00
|
|
|
break;
|
|
|
|
case 's':
|
2020-03-27 16:06:40 +00:00
|
|
|
s_seek = opt.arg;
|
2019-03-08 13:22:59 +00:00
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
sandbox = true;
|
|
|
|
break;
|
|
|
|
#if USE_THREADS
|
|
|
|
case 't':
|
|
|
|
#if ALLOW_THREADED
|
|
|
|
threaded = true;
|
|
|
|
#else
|
|
|
|
eprintf ("WARNING: -t is temporarily disabled!\n");
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case 'T':
|
|
|
|
compute_hashes = false;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
if (quiet) {
|
|
|
|
printf ("%s\n", R2_VERSION);
|
|
|
|
LISTS_FREE ();
|
2020-01-10 05:09:23 +00:00
|
|
|
free (customRarunProfile);
|
2019-03-08 13:22:59 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
r_main_version_verify (0);
|
|
|
|
LISTS_FREE ();
|
2020-01-10 05:09:23 +00:00
|
|
|
free (customRarunProfile);
|
2019-04-08 14:31:21 +00:00
|
|
|
return r_main_version_print ("radare2");
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
case 'V':
|
|
|
|
return r_main_version_verify (1);
|
|
|
|
case 'w':
|
|
|
|
perms |= R_PERM_W;
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
perms &= ~R_PERM_X;
|
2020-02-17 11:47:57 +00:00
|
|
|
r_config_set (r->config, "io.exec", "false");
|
2019-03-08 13:22:59 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
help++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (noStderr) {
|
|
|
|
if (-1 == close (2)) {
|
|
|
|
eprintf ("Failed to close stderr");
|
2020-01-08 04:52:00 +00:00
|
|
|
LISTS_FREE ();
|
2019-03-08 13:22:59 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
const char nul[] = R_SYS_DEVNULL;
|
|
|
|
int new_stderr = open (nul, O_RDWR);
|
|
|
|
if (-1 == new_stderr) {
|
|
|
|
eprintf ("Failed to open %s", nul);
|
2020-01-08 04:52:00 +00:00
|
|
|
LISTS_FREE ();
|
2019-03-08 13:22:59 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (2 != new_stderr) {
|
|
|
|
if (-1 == dup2 (new_stderr, 2)) {
|
|
|
|
eprintf ("Failed to dup2 stderr");
|
2020-01-08 04:52:00 +00:00
|
|
|
LISTS_FREE ();
|
2019-03-08 13:22:59 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (-1 == close (new_stderr)) {
|
|
|
|
eprintf ("Failed to close %s", nul);
|
2020-01-08 04:52:00 +00:00
|
|
|
LISTS_FREE ();
|
2019-03-08 13:22:59 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
2020-02-17 11:47:57 +00:00
|
|
|
const char *dbg_profile = r_config_get (r->config, "dbg.profile");
|
2019-03-08 13:22:59 +00:00
|
|
|
if (dbg_profile && *dbg_profile) {
|
|
|
|
char *msg = r_file_slurp (dbg_profile, NULL);
|
|
|
|
if (msg) {
|
|
|
|
char *program = strstr (msg, "program=");
|
|
|
|
if (program) {
|
|
|
|
program += 8;
|
|
|
|
char *p = 0;
|
|
|
|
p = strstr (program, "\r\n");
|
|
|
|
if (!p) {
|
|
|
|
p = strchr (program, '\n');
|
|
|
|
}
|
|
|
|
if (p) {
|
|
|
|
*p = 0;
|
|
|
|
pfile = strdup (program);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free (msg);
|
|
|
|
} else {
|
2020-03-02 20:39:37 +00:00
|
|
|
eprintf ("Cannot read dbg.profile '%s'\n", dbg_profile);
|
2019-03-08 13:22:59 +00:00
|
|
|
pfile = NULL; //strdup ("");
|
|
|
|
}
|
|
|
|
} else {
|
2020-03-27 16:06:40 +00:00
|
|
|
pfile = argv[opt.ind] ? strdup (argv[opt.ind]) : NULL;
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (do_list_io_plugins) {
|
2020-02-17 11:47:57 +00:00
|
|
|
if (r_config_get_i (r->config, "cfg.plugins")) {
|
|
|
|
r_core_loadlibs (r, R_CORE_LOADLIBS_ALL, NULL);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
2020-02-17 11:47:57 +00:00
|
|
|
run_commands (r, NULL, prefiles, false, do_analysis);
|
|
|
|
run_commands (r, cmds, files, quiet, do_analysis);
|
2019-03-08 13:22:59 +00:00
|
|
|
if (quietLeak) {
|
|
|
|
exit (0);
|
|
|
|
}
|
2020-02-17 11:47:57 +00:00
|
|
|
r_io_plugin_list (r->io);
|
2019-03-08 13:22:59 +00:00
|
|
|
r_cons_flush ();
|
|
|
|
LISTS_FREE ();
|
2019-09-17 00:14:32 +00:00
|
|
|
free (pfile);
|
2019-03-08 13:22:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (help > 0) {
|
|
|
|
LISTS_FREE ();
|
|
|
|
free (pfile);
|
|
|
|
return main_help (help > 1? 2: 0);
|
|
|
|
}
|
|
|
|
#if __WINDOWS__
|
|
|
|
pfile = r_acp_to_utf8 (pfile);
|
|
|
|
#endif // __WINDOWS__
|
|
|
|
if (customRarunProfile) {
|
|
|
|
char *tfn = r_file_temp (".rarun2");
|
|
|
|
if (!r_file_dump (tfn, (const ut8*)customRarunProfile, strlen (customRarunProfile), 0)) {
|
|
|
|
eprintf ("Cannot create %s\n", tfn);
|
|
|
|
} else {
|
|
|
|
haveRarunProfile = true;
|
2020-02-17 11:47:57 +00:00
|
|
|
r_config_set (r->config, "dbg.profile", tfn);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
free (tfn);
|
|
|
|
}
|
|
|
|
if (debug == 1) {
|
2020-03-27 16:06:40 +00:00
|
|
|
if (opt.ind >= argc && !haveRarunProfile) {
|
2019-03-08 13:22:59 +00:00
|
|
|
eprintf ("Missing argument for -d\n");
|
|
|
|
LISTS_FREE ();
|
|
|
|
return 1;
|
|
|
|
}
|
2020-03-27 16:06:40 +00:00
|
|
|
const char *src = haveRarunProfile? pfile: argv[opt.ind];
|
2019-03-08 13:22:59 +00:00
|
|
|
if (src && *src) {
|
|
|
|
char *uri = strdup (src);
|
|
|
|
if (uri) {
|
|
|
|
char *p = strstr (uri, "://");
|
|
|
|
if (p) {
|
|
|
|
*p = 0;
|
|
|
|
// TODO: this must be specified by the io plugin, not hardcoded here
|
|
|
|
if (!strcmp (uri, "winedbg")) {
|
|
|
|
debugbackend = "io";
|
|
|
|
} else {
|
|
|
|
debugbackend = uri;
|
|
|
|
}
|
|
|
|
debug = 2;
|
|
|
|
} else {
|
|
|
|
free (uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-05 13:02:46 +00:00
|
|
|
tmp = NULL;
|
|
|
|
if (!load_l || (tmp = r_sys_getenv ("R2_NOPLUGINS"))) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_config_set_i (r->config, "cfg.plugins", 0);
|
2019-03-08 13:22:59 +00:00
|
|
|
free (tmp);
|
|
|
|
}
|
2020-02-17 11:47:57 +00:00
|
|
|
if (r_config_get_i (r->config, "cfg.plugins")) {
|
|
|
|
r_core_loadlibs (r, R_CORE_LOADLIBS_ALL, NULL);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
2020-02-17 11:47:57 +00:00
|
|
|
ret = run_commands (r, NULL, prefiles, false, do_analysis);
|
2019-03-08 13:22:59 +00:00
|
|
|
r_list_free (prefiles);
|
|
|
|
prefiles = NULL;
|
|
|
|
|
2020-02-17 11:47:57 +00:00
|
|
|
r_bin_force_plugin (r->bin, forcebin);
|
2019-03-08 13:22:59 +00:00
|
|
|
|
2020-02-17 11:47:57 +00:00
|
|
|
prj = r_config_get (r->config, "prj.name");
|
2019-03-08 13:22:59 +00:00
|
|
|
if (prj && *prj) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_core_project_open (r, prj, false);
|
|
|
|
r_config_set (r->config, "bin.strings", "false");
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (do_connect) {
|
2020-03-27 16:06:40 +00:00
|
|
|
const char *uri = argv[opt.ind];
|
|
|
|
if (opt.ind >= argc) {
|
2019-03-08 13:22:59 +00:00
|
|
|
eprintf ("Missing URI for -C\n");
|
|
|
|
LISTS_FREE ();
|
|
|
|
return 1;
|
|
|
|
}
|
2019-05-06 09:31:32 +00:00
|
|
|
if (strstr (uri, "://")) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_core_cmdf (r, "=+%s", uri);
|
2019-03-08 13:22:59 +00:00
|
|
|
} else {
|
2020-03-27 16:06:40 +00:00
|
|
|
r_core_cmdf (r, "=+http://%s/cmd/", argv[opt.ind]);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
2020-02-17 11:47:57 +00:00
|
|
|
r_core_cmd0 (r, "=!=");
|
2019-05-06 09:31:32 +00:00
|
|
|
//LISTS_FREE ();
|
|
|
|
// return 0;
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (zflag) {
|
|
|
|
case 1:
|
2020-02-17 11:47:57 +00:00
|
|
|
r_config_set (r->config, "bin.strings", "false");
|
2019-03-08 13:22:59 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
2020-02-17 11:47:57 +00:00
|
|
|
r_config_set (r->config, "bin.rawstr", "true");
|
2019-03-08 13:22:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (zflag > 3) {
|
|
|
|
eprintf ("Sleeping now...\n");
|
|
|
|
r_sys_sleep (zflag);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (run_rc) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_core_parse_radare2rc (r);
|
2019-10-10 19:46:34 +00:00
|
|
|
} else {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_config_set (r->config, "scr.utf8", "false");
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 11:47:57 +00:00
|
|
|
if (r_config_get_i (r->config, "zign.autoload")) {
|
|
|
|
char *path = r_file_abspath (r_config_get (r->config, "dir->zigns"));
|
2019-03-08 13:22:59 +00:00
|
|
|
char *complete_path = NULL;
|
|
|
|
RList *list = r_sys_dir (path);
|
|
|
|
RListIter *iter;
|
|
|
|
char *file = NULL;
|
|
|
|
r_list_foreach (list, iter, file) {
|
|
|
|
if (file && *file && *file != '.') {
|
|
|
|
complete_path = r_str_newf ("%s"R_SYS_DIR"%s", path, file);
|
|
|
|
if (r_str_endswith (complete_path, "gz")) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_sign_load_gz (r->anal, complete_path);
|
2019-03-08 13:22:59 +00:00
|
|
|
} else {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_sign_load (r->anal, complete_path);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
free (complete_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r_list_free (list);
|
|
|
|
free (path);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pfile && r_file_is_directory (pfile)) {
|
|
|
|
if (debug) {
|
|
|
|
eprintf ("Error: Cannot debug directories, yet.\n");
|
|
|
|
LISTS_FREE ();
|
|
|
|
free (pfile);
|
|
|
|
return 1;
|
|
|
|
}
|
2020-03-27 16:06:40 +00:00
|
|
|
if (r_sys_chdir (argv[opt.ind])) {
|
2019-03-08 13:22:59 +00:00
|
|
|
eprintf ("[d] Cannot open directory\n");
|
|
|
|
LISTS_FREE ();
|
|
|
|
free (pfile);
|
|
|
|
return 1;
|
|
|
|
}
|
2020-03-27 16:06:40 +00:00
|
|
|
} else if (argv[opt.ind] && !strcmp (argv[opt.ind], "=")) {
|
2019-03-08 13:22:59 +00:00
|
|
|
int sz;
|
|
|
|
/* stdin/batch mode */
|
2020-06-15 09:24:43 +00:00
|
|
|
char *buf = r_stdin_slurp (&sz);
|
2019-03-08 13:22:59 +00:00
|
|
|
eprintf ("^D\n");
|
2019-08-24 09:18:32 +00:00
|
|
|
r_cons_set_raw (false);
|
2019-03-08 13:22:59 +00:00
|
|
|
#if __UNIX__
|
|
|
|
// TODO: keep flags :?
|
|
|
|
(void)freopen ("/dev/tty", "rb", stdin);
|
2020-06-15 09:24:43 +00:00
|
|
|
(void)freopen ("/dev/tty", "w", stdout);
|
|
|
|
(void)freopen ("/dev/tty", "w", stderr);
|
2019-03-08 13:22:59 +00:00
|
|
|
#else
|
|
|
|
eprintf ("Cannot reopen stdin without UNIX\n");
|
2020-06-15 09:24:43 +00:00
|
|
|
free (buf);
|
2019-03-08 13:22:59 +00:00
|
|
|
return 1;
|
|
|
|
#endif
|
|
|
|
if (buf && sz > 0) {
|
|
|
|
char *path = r_str_newf ("malloc://%d", sz);
|
2020-02-17 11:47:57 +00:00
|
|
|
fh = r_core_file_open (r, path, perms, mapaddr);
|
2019-03-08 13:22:59 +00:00
|
|
|
if (!fh) {
|
|
|
|
r_cons_flush ();
|
|
|
|
free (buf);
|
|
|
|
eprintf ("[=] Cannot open '%s'\n", path);
|
|
|
|
LISTS_FREE ();
|
|
|
|
free (path);
|
|
|
|
return 1;
|
|
|
|
}
|
2020-02-17 11:47:57 +00:00
|
|
|
r_io_map_new (r->io, fh->fd, 7, 0LL, mapaddr,
|
|
|
|
r_io_fd_size (r->io, fh->fd));
|
2020-06-22 11:06:45 +00:00
|
|
|
r_io_write_at (r->io, mapaddr, (const ut8 *)buf, sz);
|
2020-02-17 11:47:57 +00:00
|
|
|
r_core_block_read (r);
|
2019-03-08 13:22:59 +00:00
|
|
|
free (buf);
|
|
|
|
free (path);
|
|
|
|
// TODO: load rbin thing
|
|
|
|
} else {
|
|
|
|
eprintf ("Cannot slurp from stdin\n");
|
2020-01-10 05:09:23 +00:00
|
|
|
LISTS_FREE ();
|
2019-03-08 13:22:59 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2020-03-27 16:06:40 +00:00
|
|
|
} else if (strcmp (argv[opt.ind - 1], "--") && !(r_config_get (r->config, "prj.name") && r_config_get (r->config, "prj.name")[0]) ) {
|
2019-03-08 13:22:59 +00:00
|
|
|
if (debug) {
|
|
|
|
if (asmbits) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_config_set (r->config, "asm.bits", asmbits);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
2020-02-17 11:47:57 +00:00
|
|
|
r_config_set (r->config, "search.in", "dbg.map"); // implicit?
|
|
|
|
r_config_set (r->config, "cfg.debug", "true");
|
2019-03-08 13:22:59 +00:00
|
|
|
perms = R_PERM_RWX;
|
2020-03-27 16:06:40 +00:00
|
|
|
if (opt.ind >= argc) {
|
2019-03-08 13:22:59 +00:00
|
|
|
eprintf ("No program given to -d\n");
|
|
|
|
LISTS_FREE ();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (debug == 2) {
|
|
|
|
// autodetect backend with -D
|
2020-02-17 11:47:57 +00:00
|
|
|
r_config_set (r->config, "dbg.backend", debugbackend);
|
2019-03-08 13:22:59 +00:00
|
|
|
if (strcmp (debugbackend, "native")) {
|
|
|
|
if (!haveRarunProfile) {
|
2020-03-27 16:06:40 +00:00
|
|
|
pfile = strdup (argv[opt.ind++]);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
perms = R_PERM_RX; // XXX. should work with rw too
|
|
|
|
debug = 2;
|
|
|
|
if (!strstr (pfile, "://")) {
|
2020-03-27 16:06:40 +00:00
|
|
|
opt.ind--; // take filename
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
#if __WINDOWS__
|
|
|
|
pfile = r_acp_to_utf8 (pfile);
|
|
|
|
#endif // __WINDOWS__
|
2020-02-17 11:47:57 +00:00
|
|
|
fh = r_core_file_open (r, pfile, perms, mapaddr);
|
|
|
|
iod = (r->io && fh) ? r_io_desc_get (r->io, fh->fd) : NULL;
|
2019-03-08 13:22:59 +00:00
|
|
|
if (!strcmp (debugbackend, "gdb")) {
|
2020-02-17 11:47:57 +00:00
|
|
|
const char *filepath = r_config_get (r->config, "dbg.exe.path");
|
2019-03-08 13:22:59 +00:00
|
|
|
ut64 addr = baddr;
|
|
|
|
if (addr == UINT64_MAX) {
|
2020-02-17 11:47:57 +00:00
|
|
|
addr = r_config_get_i (r->config, "bin.baddr");
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
if (r_file_exists (filepath) && !r_file_is_directory (filepath)) {
|
|
|
|
char *newpath = r_file_abspath (filepath);
|
|
|
|
if (newpath) {
|
|
|
|
if (iod) {
|
|
|
|
free (iod->name);
|
|
|
|
iod->name = newpath;
|
|
|
|
}
|
|
|
|
if (addr == UINT64_MAX) {
|
2020-02-17 11:47:57 +00:00
|
|
|
addr = r_debug_get_baddr (r->dbg, newpath);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
2020-02-17 11:47:57 +00:00
|
|
|
r_core_bin_load (r, NULL, addr);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
} else if (is_valid_gdb_file (fh)) {
|
|
|
|
filepath = iod->name;
|
|
|
|
if (r_file_exists (filepath) && !r_file_is_directory (filepath)) {
|
|
|
|
if (addr == UINT64_MAX) {
|
2020-02-17 11:47:57 +00:00
|
|
|
addr = r_debug_get_baddr (r->dbg, filepath);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
2020-02-17 11:47:57 +00:00
|
|
|
r_core_bin_load (r, filepath, addr);
|
2019-03-08 13:22:59 +00:00
|
|
|
} else if ((filepath = get_file_in_cur_dir (filepath))) {
|
|
|
|
// Present in local directory
|
|
|
|
if (iod) {
|
|
|
|
free (iod->name);
|
|
|
|
iod->name = (char*) filepath;
|
|
|
|
}
|
|
|
|
if (addr == UINT64_MAX) {
|
2020-02-17 11:47:57 +00:00
|
|
|
addr = r_debug_get_baddr (r->dbg, filepath);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
2020-02-17 11:47:57 +00:00
|
|
|
r_core_bin_load (r, NULL, addr);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2020-03-27 16:06:40 +00:00
|
|
|
const char *f = (haveRarunProfile && pfile)? pfile: argv[opt.ind];
|
2019-03-08 13:22:59 +00:00
|
|
|
is_gdb = (!memcmp (f, "gdb://", R_MIN (f? strlen (f):0, 6)));
|
|
|
|
if (!is_gdb) {
|
|
|
|
pfile = strdup ("dbg://");
|
|
|
|
}
|
|
|
|
#if __UNIX__
|
|
|
|
/* implicit ./ to make unix behave like windows */
|
|
|
|
if (f) {
|
|
|
|
char *path, *escaped_path;
|
|
|
|
if (strchr (f, '/')) {
|
|
|
|
// f is a path
|
|
|
|
path = strdup (f);
|
|
|
|
} else {
|
|
|
|
// f is a filename
|
|
|
|
if (r_file_exists (f)) {
|
2019-05-13 14:16:13 +00:00
|
|
|
path = r_str_prepend (strdup (f), "./");
|
2019-03-08 13:22:59 +00:00
|
|
|
} else {
|
|
|
|
path = r_file_path (f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
escaped_path = r_str_arg_escape (path);
|
|
|
|
pfile = r_str_append (pfile, escaped_path);
|
|
|
|
file = pfile; // probably leaks
|
|
|
|
R_FREE (escaped_path);
|
|
|
|
R_FREE (path);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
# if __WINDOWS__
|
|
|
|
f = r_acp_to_utf8 (f);
|
|
|
|
# endif // __WINDOWS__
|
|
|
|
if (f) {
|
|
|
|
char *escaped_path = r_str_arg_escape (f);
|
|
|
|
pfile = r_str_append (pfile, escaped_path);
|
|
|
|
free (escaped_path);
|
|
|
|
file = pfile; // r_str_append (file, escaped_path);
|
|
|
|
}
|
|
|
|
#endif
|
2020-03-27 16:06:40 +00:00
|
|
|
opt.ind++;
|
|
|
|
while (opt.ind < argc) {
|
|
|
|
char *escaped_arg = r_str_arg_escape (argv[opt.ind]);
|
2019-03-08 13:22:59 +00:00
|
|
|
file = r_str_append (file, " ");
|
|
|
|
file = r_str_append (file, escaped_arg);
|
|
|
|
free (escaped_arg);
|
2020-03-27 16:06:40 +00:00
|
|
|
opt.ind++;
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
pfile = file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (asmarch) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_config_set (r->config, "asm.arch", asmarch);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
if (asmbits) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_config_set (r->config, "asm.bits", asmbits);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
if (asmos) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_config_set (r->config, "asm.os", asmos);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!debug || debug == 2) {
|
2020-02-17 11:47:57 +00:00
|
|
|
const char *dbg_profile = r_config_get (r->config, "dbg.profile");
|
2020-03-27 16:06:40 +00:00
|
|
|
if (opt.ind == argc && dbg_profile && *dbg_profile) {
|
2020-02-17 11:47:57 +00:00
|
|
|
fh = r_core_file_open (r, pfile, perms, mapaddr);
|
2019-03-08 13:22:59 +00:00
|
|
|
if (fh) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_core_bin_load (r, pfile, baddr);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-27 16:06:40 +00:00
|
|
|
if (opt.ind < argc) {
|
2019-03-08 13:22:59 +00:00
|
|
|
R_FREE (pfile);
|
2020-03-27 16:06:40 +00:00
|
|
|
while (opt.ind < argc) {
|
|
|
|
pfile = strdup (argv[opt.ind++]);
|
2019-03-08 13:22:59 +00:00
|
|
|
#if __WINDOWS__
|
|
|
|
pfile = r_acp_to_utf8 (pfile);
|
2020-03-27 16:06:40 +00:00
|
|
|
#endif
|
2020-02-17 11:47:57 +00:00
|
|
|
fh = r_core_file_open (r, pfile, perms, mapaddr);
|
2019-03-08 13:22:59 +00:00
|
|
|
if (!fh && perms & R_PERM_W) {
|
|
|
|
perms |= R_PERM_CREAT;
|
2020-02-17 11:47:57 +00:00
|
|
|
fh = r_core_file_open (r, pfile, perms, mapaddr);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
if (perms & R_PERM_CREAT) {
|
|
|
|
if (fh) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_config_set_i (r->config, "io.va", false);
|
2019-03-08 13:22:59 +00:00
|
|
|
} else {
|
|
|
|
eprintf ("r_io_create: Permission denied.\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fh) {
|
2020-02-17 11:47:57 +00:00
|
|
|
iod = r->io ? r_io_desc_get (r->io, fh->fd) : NULL;
|
2019-03-08 13:22:59 +00:00
|
|
|
if (iod && perms & R_PERM_X) {
|
|
|
|
iod->perm |= R_PERM_X;
|
|
|
|
}
|
|
|
|
if (load_bin == LOAD_BIN_ALL) {
|
2019-12-09 09:27:14 +00:00
|
|
|
const char *filepath = NULL;
|
|
|
|
if (debug) {
|
|
|
|
// XXX: incorrect for PIE binaries
|
|
|
|
filepath = file? strstr (file, "://"): NULL;
|
|
|
|
filepath = filepath ? filepath + 3 : pfile;
|
|
|
|
}
|
2020-02-17 11:47:57 +00:00
|
|
|
if (r->file && iod && (iod->fd == r->file->fd) && iod->name) {
|
2019-12-09 09:27:14 +00:00
|
|
|
filepath = iod->name;
|
|
|
|
}
|
|
|
|
/* Load rbin info from r2 dbg:// or r2 /bin/ls */
|
|
|
|
/* the baddr should be set manually here */
|
2020-02-17 11:47:57 +00:00
|
|
|
(void)r_core_bin_load (r, filepath, baddr);
|
2019-12-09 09:27:14 +00:00
|
|
|
// check if bin info is loaded and complain if -B was used
|
2020-02-17 11:47:57 +00:00
|
|
|
RBinFile *bi = r_bin_cur (r->bin);
|
2019-12-09 09:27:14 +00:00
|
|
|
bool haveBinInfo = bi && bi->o && bi->o->info && bi->o->info->type;
|
|
|
|
if (!haveBinInfo && baddr != UT64_MAX) {
|
|
|
|
eprintf ("Warning: Don't use -B on unknown files. Consider using -m.\n");
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_io_map_new (r->io, iod->fd, perms, 0LL, mapaddr, r_io_desc_size (iod));
|
2019-03-08 13:22:59 +00:00
|
|
|
if (load_bin == LOAD_BIN_STRUCTURES_ONLY) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_core_bin_load_structs (r, iod->name);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2020-02-17 11:47:57 +00:00
|
|
|
const char *prj = r_config_get (r->config, "prj.name");
|
2019-03-08 13:22:59 +00:00
|
|
|
if (prj && *prj) {
|
2020-02-17 11:47:57 +00:00
|
|
|
pfile = r_core_project_info (r, prj);
|
2019-03-08 13:22:59 +00:00
|
|
|
if (pfile) {
|
|
|
|
if (!fh) {
|
2020-02-17 11:47:57 +00:00
|
|
|
fh = r_core_file_open (r, pfile, perms, mapaddr);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
// load_bin = LOAD_BIN_NOTHING;
|
|
|
|
load_bin = LOAD_BIN_STRUCTURES_ONLY;
|
|
|
|
} else {
|
|
|
|
eprintf ("Cannot find project file\n");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (fh) {
|
2020-02-17 11:47:57 +00:00
|
|
|
iod = r->io ? r_io_desc_get (r->io, fh->fd) : NULL;
|
2019-03-08 13:22:59 +00:00
|
|
|
if (iod) {
|
|
|
|
perms = iod->perm;
|
2020-02-17 11:47:57 +00:00
|
|
|
r_io_map_new (r->io, iod->fd, perms, 0LL, 0LL, r_io_desc_size (iod));
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mapaddr) {
|
|
|
|
eprintf ("WARNING: using oba to load the syminfo from different mapaddress.\n");
|
|
|
|
eprintf ("TODO: Must use the API instead of running commands to speedup loading times.\n");
|
2020-02-17 11:47:57 +00:00
|
|
|
if (r_config_get_i (r->config, "file.info")) {
|
2019-03-08 13:22:59 +00:00
|
|
|
// load symbols when using r2 -m 0x1000 /bin/ls
|
2020-02-17 11:47:57 +00:00
|
|
|
r_core_cmdf (r, "oba 0 0x%"PFMT64x, mapaddr);
|
|
|
|
r_core_cmd0 (r, ".ies*");
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2020-02-17 11:47:57 +00:00
|
|
|
RCoreFile *f = r_core_file_open (r, pfile, perms, mapaddr);
|
2019-03-08 13:22:59 +00:00
|
|
|
if (f) {
|
|
|
|
fh = f;
|
|
|
|
}
|
|
|
|
if (fh) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_debug_use (r->dbg, is_gdb ? "gdb" : debugbackend);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
/* load symbols when doing r2 -d ls */
|
|
|
|
// NOTE: the baddr is redefined to support PIE/ASLR
|
2020-02-17 11:47:57 +00:00
|
|
|
baddr = r_debug_get_baddr (r->dbg, pfile);
|
2019-03-08 13:22:59 +00:00
|
|
|
|
2020-02-17 11:47:57 +00:00
|
|
|
if (baddr != UT64_MAX && baddr != 0 && r->dbg->verbose) {
|
2019-03-08 13:22:59 +00:00
|
|
|
eprintf ("bin.baddr 0x%08" PFMT64x "\n", baddr);
|
|
|
|
}
|
|
|
|
if (load_bin == LOAD_BIN_ALL) {
|
2020-02-17 11:47:57 +00:00
|
|
|
if (baddr && baddr != UT64_MAX && r->dbg->verbose) {
|
2019-03-08 13:22:59 +00:00
|
|
|
eprintf ("Using 0x%" PFMT64x "\n", baddr);
|
|
|
|
}
|
2020-02-17 11:47:57 +00:00
|
|
|
if (r_core_bin_load (r, pfile, baddr)) {
|
|
|
|
RBinObject *obj = r_bin_cur_object (r->bin);
|
2019-03-08 13:22:59 +00:00
|
|
|
if (obj && obj->info) {
|
2020-02-17 11:47:57 +00:00
|
|
|
if (r->dbg->verbose) {
|
2019-03-08 13:22:59 +00:00
|
|
|
eprintf ("asm.bits %d\n", obj->info->bits);
|
|
|
|
}
|
|
|
|
#if __linux__ && __GNU_LIBRARY__ && __GLIBC__ && __GLIBC_MINOR__ && __x86_64__
|
2020-02-17 11:47:57 +00:00
|
|
|
ut64 bitness = r_config_get_i (r->config, "asm.bits");
|
2019-03-08 13:22:59 +00:00
|
|
|
if (bitness == 32) {
|
|
|
|
eprintf ("glibc.fc_offset = 0x00148\n");
|
2020-02-17 11:47:57 +00:00
|
|
|
r_config_set_i (r->config, "dbg.glibc.fc_offset", 0x00148);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-17 11:47:57 +00:00
|
|
|
r_core_cmd0 (r, ".dm*");
|
2019-03-08 13:22:59 +00:00
|
|
|
// Set Thumb Mode if necessary
|
2020-02-17 11:47:57 +00:00
|
|
|
r_core_cmd0 (r, "dr? thumb;?? e asm.bits=16");
|
2019-03-08 13:22:59 +00:00
|
|
|
r_cons_reset ();
|
|
|
|
}
|
|
|
|
if (!pfile) {
|
|
|
|
pfile = file;
|
|
|
|
}
|
|
|
|
if (!fh) {
|
|
|
|
if (pfile && *pfile) {
|
|
|
|
r_cons_flush ();
|
|
|
|
if (perms & R_PERM_W) {
|
|
|
|
eprintf ("[w] Cannot open '%s' for writing.\n", pfile);
|
|
|
|
} else {
|
|
|
|
eprintf ("[r] Cannot open '%s'\n", pfile);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
eprintf ("Missing file to open\n");
|
|
|
|
}
|
|
|
|
ret = 1;
|
|
|
|
goto beach;
|
|
|
|
}
|
2020-02-17 11:47:57 +00:00
|
|
|
if (!r->file) { // no given file
|
2019-03-08 13:22:59 +00:00
|
|
|
ret = 1;
|
|
|
|
goto beach;
|
|
|
|
}
|
2020-02-17 11:47:57 +00:00
|
|
|
if (r->bin->cur && r->bin->cur->o && r->bin->cur->o->info && r->bin->cur->o->info->rclass && !strcmp ("fs", r->bin->cur->o->info->rclass)) {
|
|
|
|
const char *fstype = r->bin->cur->o->info->bclass;
|
|
|
|
r_core_cmdf (r, "m /root %s @ 0", fstype);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
2020-02-17 11:47:57 +00:00
|
|
|
r_core_cmd0 (r, "=!"); // initalize io subsystem
|
|
|
|
iod = r->io ? r_io_desc_get (r->io, fh->fd) : NULL;
|
2019-03-08 13:22:59 +00:00
|
|
|
if (mapaddr) {
|
2020-04-17 10:53:35 +00:00
|
|
|
r_core_seek (r, mapaddr, true);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
r_list_foreach (evals, iter, cmdn) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_config_eval (r->config, cmdn, false);
|
2019-03-08 13:22:59 +00:00
|
|
|
r_cons_flush ();
|
|
|
|
}
|
|
|
|
#if 0
|
|
|
|
// Do not autodetect utf8 terminals to avoid problems on initial
|
|
|
|
// stdin buffer and some terminals that just hang (android/ios)
|
|
|
|
if (!quiet && r_cons_is_utf8 ()) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_config_set_i (r->config, "scr.utf8", true);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (asmarch) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_config_set (r->config, "asm.arch", asmarch);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
if (asmbits) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_config_set (r->config, "asm.bits", asmbits);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
if (asmos) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_config_set (r->config, "asm.os", asmos);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 11:47:57 +00:00
|
|
|
debug = r->file && iod && (r->file->fd == iod->fd) && iod->plugin && \
|
2019-03-08 13:22:59 +00:00
|
|
|
iod->plugin->isdbg;
|
|
|
|
if (debug) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_core_setup_debugger (r, debugbackend, baddr == UT64_MAX);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
2020-02-17 11:47:57 +00:00
|
|
|
RBinObject *o = r_bin_cur_object (r->bin);
|
2019-08-18 20:44:41 +00:00
|
|
|
if (!debug && o && !o->regstate) {
|
2020-02-17 11:47:57 +00:00
|
|
|
RFlagItem *fi = r_flag_get (r->flags, "entry0");
|
2019-05-10 10:41:11 +00:00
|
|
|
if (fi) {
|
2020-04-17 10:53:35 +00:00
|
|
|
r_core_seek (r, fi->offset, true);
|
2019-05-10 10:41:11 +00:00
|
|
|
} else {
|
2019-05-16 17:09:47 +00:00
|
|
|
if (o) {
|
2020-02-17 11:47:57 +00:00
|
|
|
RList *sections = r_bin_get_sections (r->bin);
|
2019-05-10 10:14:25 +00:00
|
|
|
RListIter *iter;
|
|
|
|
RBinSection *s;
|
|
|
|
r_list_foreach (sections, iter, s) {
|
|
|
|
if (s->perm & R_PERM_X) {
|
|
|
|
ut64 addr = s->vaddr? s->vaddr: s->paddr;
|
2020-04-17 10:53:35 +00:00
|
|
|
r_core_seek (r, addr, true);
|
2019-05-10 10:14:25 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-05-10 09:11:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
2020-02-20 23:20:33 +00:00
|
|
|
if (o && compute_hashes) {
|
|
|
|
// TODO: recall with limit=0 ?
|
|
|
|
ut64 limit = r_config_get_i (r->config, "bin.hashlimit");
|
|
|
|
r_bin_file_set_hashes (r->bin, r_bin_file_compute_hashes (r->bin, limit));
|
|
|
|
}
|
2019-03-08 13:22:59 +00:00
|
|
|
if (s_seek) {
|
2020-02-17 11:47:57 +00:00
|
|
|
seek = r_num_math (r->num, s_seek);
|
2019-03-08 13:22:59 +00:00
|
|
|
if (seek != UT64_MAX) {
|
2020-04-17 10:53:35 +00:00
|
|
|
r_core_seek (r, seek, true);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fullfile) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_core_block_size (r, r_io_desc_size (iod));
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 10:53:35 +00:00
|
|
|
r_core_seek (r, r->offset, true); // read current block
|
2019-03-08 13:22:59 +00:00
|
|
|
|
2019-03-21 10:02:48 +00:00
|
|
|
/* check if file.path has changed */
|
2019-03-08 13:22:59 +00:00
|
|
|
if (iod && !strstr (iod->uri, "://")) {
|
2019-03-30 20:24:21 +00:00
|
|
|
const char *npath;
|
2020-02-17 11:47:57 +00:00
|
|
|
char *path = strdup (r_config_get (r->config, "file.path"));
|
|
|
|
has_project = r_core_project_open (r, r_config_get (r->config, "prj.name"), false);
|
|
|
|
iod = r->io ? r_io_desc_get (r->io, fh->fd) : NULL;
|
2019-03-08 13:22:59 +00:00
|
|
|
if (has_project) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_config_set (r->config, "bin.strings", "false");
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
2020-02-17 11:47:57 +00:00
|
|
|
npath = r_config_get (r->config, "file.path");
|
2019-03-08 13:22:59 +00:00
|
|
|
if (!quiet && path && *path && npath && strcmp (path, npath)) {
|
|
|
|
eprintf ("WARNING: file.path change: %s => %s\n", path, npath);
|
|
|
|
}
|
|
|
|
free (path);
|
|
|
|
}
|
|
|
|
|
|
|
|
r_list_foreach (evals, iter, cmdn) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_config_eval (r->config, cmdn, false);
|
2019-03-08 13:22:59 +00:00
|
|
|
r_cons_flush ();
|
|
|
|
}
|
|
|
|
|
|
|
|
// no flagspace selected by default the beginning
|
2020-02-17 11:47:57 +00:00
|
|
|
r_flag_space_set (r->flags, NULL);
|
2019-03-08 13:22:59 +00:00
|
|
|
/* load <file>.r2 */
|
|
|
|
{
|
2019-03-27 00:33:48 +00:00
|
|
|
char* f = r_str_newf ("%s.r2", pfile);
|
|
|
|
const char *uri_splitter = strstr (f, "://");
|
|
|
|
const char *path = uri_splitter? uri_splitter + 3: f;
|
|
|
|
if (r_file_exists (path)) {
|
2019-03-08 13:22:59 +00:00
|
|
|
// TODO: should 'q' unset the interactive bit?
|
|
|
|
bool isInteractive = r_cons_is_interactive ();
|
2019-03-27 00:33:48 +00:00
|
|
|
if (isInteractive && r_cons_yesno ('n', "Do you want to run the '%s' script? (y/N) ", path)) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_core_cmd_file (r, path);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-27 00:33:48 +00:00
|
|
|
free (f);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_core_block_read (r);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
char *global_rc = r_str_r2_prefix (R2_GLOBAL_RC);
|
|
|
|
if (r_file_exists (global_rc)) {
|
2020-02-17 11:47:57 +00:00
|
|
|
(void)r_core_run_script (r, global_rc);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
free (global_rc);
|
|
|
|
}
|
|
|
|
// only analyze if file contains entrypoint
|
|
|
|
{
|
2020-02-17 11:47:57 +00:00
|
|
|
char *s = r_core_cmd_str (r, "ieq");
|
2019-03-08 13:22:59 +00:00
|
|
|
if (s && *s) {
|
2020-02-17 11:47:57 +00:00
|
|
|
int da = r_config_get_i (r->config, "file.analyze");
|
2019-03-08 13:22:59 +00:00
|
|
|
if (da > do_analysis) {
|
|
|
|
do_analysis = da;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free (s);
|
|
|
|
}
|
|
|
|
if (do_analysis > 0) {
|
|
|
|
switch (do_analysis) {
|
2020-02-17 11:47:57 +00:00
|
|
|
case 1: r_core_cmd0 (r, "aa"); break;
|
|
|
|
case 2: r_core_cmd0 (r, "aaa"); break;
|
|
|
|
case 3: r_core_cmd0 (r, "aaaa"); break;
|
|
|
|
default: r_core_cmd0 (r, "aaaaa"); break;
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
r_cons_flush ();
|
|
|
|
}
|
|
|
|
#if UNCOLORIZE_NONTTY
|
|
|
|
#if __UNIX__
|
|
|
|
if (!r_cons_isatty ()) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_config_set_i (r->config, "scr.color", COLOR_MODE_DISABLED);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
if (fullfile) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_core_block_size (r, r_io_desc_size (iod));
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
if (perms & R_PERM_W) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_core_cmd0 (r, "omfg+w");
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
2020-02-17 11:47:57 +00:00
|
|
|
ret = run_commands (r, cmds, files, quiet, do_analysis);
|
2019-03-08 13:22:59 +00:00
|
|
|
r_list_free (cmds);
|
|
|
|
r_list_free (evals);
|
|
|
|
r_list_free (files);
|
|
|
|
cmds = evals = files = NULL;
|
2019-07-01 22:19:54 +00:00
|
|
|
if (forcequit) {
|
|
|
|
ret = 1;
|
|
|
|
}
|
2019-03-08 13:22:59 +00:00
|
|
|
if (ret) {
|
|
|
|
ret = 0;
|
|
|
|
goto beach;
|
|
|
|
}
|
2020-02-17 11:47:57 +00:00
|
|
|
if (r_config_get_i (r->config, "scr.prompt")) {
|
|
|
|
if (run_rc && r_config_get_i (r->config, "cfg.fortunes")) {
|
|
|
|
r_core_fortune_print_random (r);
|
2019-03-08 13:22:59 +00:00
|
|
|
r_cons_flush ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sandbox) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_config_set (r->config, "cfg.sandbox", "true");
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
if (quiet) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_config_set (r->config, "scr.wheel", "false");
|
|
|
|
r_config_set (r->config, "scr.interactive", "false");
|
|
|
|
r_config_set (r->config, "scr.prompt", "false");
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
2020-02-17 11:47:57 +00:00
|
|
|
r->num->value = 0;
|
2019-03-08 13:22:59 +00:00
|
|
|
if (patchfile) {
|
|
|
|
char *data = r_file_slurp (patchfile, NULL);
|
|
|
|
if (data) {
|
2020-02-17 11:47:57 +00:00
|
|
|
ret = r_core_patch (r, data);
|
2020-04-17 10:53:35 +00:00
|
|
|
r_core_seek (r, 0, true);
|
2019-03-08 13:22:59 +00:00
|
|
|
free (data);
|
|
|
|
} else {
|
|
|
|
eprintf ("[p] Cannot open '%s'\n", patchfile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((patchfile && !quiet) || !patchfile) {
|
|
|
|
if (zerosep) {
|
|
|
|
r_cons_zero ();
|
|
|
|
}
|
|
|
|
if (seek != UT64_MAX) {
|
2020-04-17 10:53:35 +00:00
|
|
|
r_core_seek (r, seek, true);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// no flagspace selected by default the beginning
|
2020-02-17 11:47:57 +00:00
|
|
|
r_flag_space_set (r->flags, NULL);
|
|
|
|
if (!debug && r->bin && r->bin->cur && r->bin->cur->o && r->bin->cur->o->info) {
|
|
|
|
if (r->bin->cur->o->info->arch) {
|
|
|
|
r_core_cmd0 (r, "aeip");
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (;;) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_core_prompt_loop (r);
|
|
|
|
ret = r->num->value;
|
|
|
|
debug = r_config_get_i (r->config, "cfg.debug");
|
2019-03-08 13:22:59 +00:00
|
|
|
if (ret != -1 && r_cons_is_interactive ()) {
|
|
|
|
char *question;
|
|
|
|
bool no_question_debug = ret & 1;
|
|
|
|
bool no_question_save = (ret & 2) >> 1;
|
|
|
|
bool y_kill_debug = (ret & 4) >> 2;
|
|
|
|
bool y_save_project = (ret & 8) >> 3;
|
|
|
|
|
2020-02-17 11:47:57 +00:00
|
|
|
if (r_core_task_running_tasks_count (&r->tasks) > 0) {
|
2019-03-08 13:22:59 +00:00
|
|
|
if (r_cons_yesno ('y', "There are running background tasks. Do you want to kill them? (Y/n)")) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_core_task_break_all (&r->tasks);
|
|
|
|
r_core_task_join (&r->tasks, r->tasks.main_task, -1);
|
2019-03-08 13:22:59 +00:00
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (debug) {
|
|
|
|
if (no_question_debug) {
|
2020-02-17 11:47:57 +00:00
|
|
|
if (r_config_get_i (r->config, "dbg.exitkills") && y_kill_debug){
|
|
|
|
r_debug_kill (r->dbg, r->dbg->pid, r->dbg->tid, 9); // KILL
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (r_cons_yesno ('y', "Do you want to quit? (Y/n)")) {
|
2020-02-17 11:47:57 +00:00
|
|
|
if (r_config_get_i (r->config, "dbg.exitkills") &&
|
2019-03-08 13:22:59 +00:00
|
|
|
r_cons_yesno ('y', "Do you want to kill the process? (Y/n)")) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_debug_kill (r->dbg, r->dbg->pid, r->dbg->tid, 9); // KILL
|
2019-03-08 13:22:59 +00:00
|
|
|
} else {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_debug_detach (r->dbg, r->dbg->pid);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-17 11:47:57 +00:00
|
|
|
prj = r_config_get (r->config, "prj.name");
|
2019-03-08 13:22:59 +00:00
|
|
|
if (no_question_save) {
|
|
|
|
if (prj && *prj && y_save_project){
|
2020-02-17 11:47:57 +00:00
|
|
|
r_core_project_save (r, prj);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
question = r_str_newf ("Do you want to save the '%s' project? (Y/n)", prj);
|
|
|
|
if (prj && *prj && r_cons_yesno ('y', "%s", question)) {
|
2020-02-17 11:47:57 +00:00
|
|
|
r_core_project_save (r, prj);
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
free (question);
|
|
|
|
}
|
2019-04-16 11:45:36 +00:00
|
|
|
|
2020-02-17 11:47:57 +00:00
|
|
|
if (r_config_get_i (r->config, "scr.confirmquit")) {
|
2019-04-16 11:45:36 +00:00
|
|
|
if (!r_cons_yesno ('n', "Do you want to quit? (Y/n)")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2019-03-08 13:22:59 +00:00
|
|
|
} else {
|
2020-02-17 11:47:57 +00:00
|
|
|
// r_core_project_save (r, prj);
|
|
|
|
if (debug && r_config_get_i (r->config, "dbg.exitkills")) {
|
|
|
|
r_debug_kill (r->dbg, 0, false, 9); // KILL
|
2019-03-08 13:22:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-17 11:47:57 +00:00
|
|
|
if (mustSaveHistory (r->config)) {
|
2019-03-08 13:22:59 +00:00
|
|
|
r_line_hist_save (R2_HOME_HISTORY);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* capture return value */
|
2020-02-17 11:47:57 +00:00
|
|
|
ret = r->num->value;
|
2019-03-08 13:22:59 +00:00
|
|
|
beach:
|
|
|
|
if (quietLeak) {
|
|
|
|
exit (ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-02-17 11:47:57 +00:00
|
|
|
r_core_task_sync_end (&r->tasks);
|
2019-03-08 13:22:59 +00:00
|
|
|
|
|
|
|
// not really needed, cause r_core_fini will close the file
|
|
|
|
// and this fh may be come stale during the command
|
|
|
|
// execution.
|
2020-02-17 11:47:57 +00:00
|
|
|
//r_core_file_close (r, fh);
|
|
|
|
r_core_free (r);
|
2019-03-08 13:22:59 +00:00
|
|
|
r_cons_set_raw (0);
|
|
|
|
free (file);
|
|
|
|
r_cons_free ();
|
|
|
|
LISTS_FREE ();
|
|
|
|
return ret;
|
|
|
|
}
|