2014-01-12 01:20:40 +01:00
|
|
|
/* radare - LGPL - Copyright 2009-2014 - pancake */
|
|
|
|
|
2014-12-03 13:15:07 +01:00
|
|
|
#define PAIR_WIDTH 9
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-07-10 03:16:48 +02:00
|
|
|
#define STR(x) (x)?(x):""
|
2012-12-23 20:51:23 +01:00
|
|
|
static void r_core_file_info (RCore *core, int mode) {
|
|
|
|
const char *fn = NULL;
|
|
|
|
int dbg = r_config_get_i (core->config, "cfg.debug");
|
|
|
|
RBinInfo *info = r_bin_get_info (core->bin);
|
2014-04-27 02:06:50 -05:00
|
|
|
RBinFile *binfile = r_core_bin_cur (core);
|
|
|
|
RCoreFile *cf = core->file;
|
2014-05-08 18:35:04 -05:00
|
|
|
RBinPlugin *plugin = r_bin_file_cur_plugin (binfile);
|
2012-12-23 20:51:23 +01:00
|
|
|
if (mode == R_CORE_BIN_JSON)
|
|
|
|
r_cons_printf ("{");
|
2013-01-22 18:08:33 +01:00
|
|
|
if (mode == R_CORE_BIN_RADARE)
|
|
|
|
return;
|
2014-12-01 11:54:58 +01:00
|
|
|
if (mode == R_CORE_BIN_SIMPLE)
|
|
|
|
return;
|
2012-12-23 20:51:23 +01:00
|
|
|
if (info) {
|
|
|
|
fn = info->file;
|
|
|
|
switch (mode) {
|
|
|
|
case R_CORE_BIN_JSON:
|
2014-12-03 13:15:07 +01:00
|
|
|
r_cons_printf ("\"type\":\"%s\","
|
2012-12-23 20:51:23 +01:00
|
|
|
"\"os\":\"%s\","
|
|
|
|
"\"arch\":\"%s\","
|
|
|
|
"\"bits\":%d,"
|
|
|
|
"\"endian\":\"%s\","
|
2014-07-10 03:16:48 +02:00
|
|
|
, STR(info->type)
|
|
|
|
, STR(info->os)
|
|
|
|
, STR(info->machine)
|
2012-12-23 20:51:23 +01:00
|
|
|
, info->bits
|
|
|
|
, info->big_endian? "big": "little");
|
|
|
|
break;
|
|
|
|
default:
|
2014-12-03 13:15:07 +01:00
|
|
|
pair ("type", info->type);
|
|
|
|
pair ("os", info->os);
|
|
|
|
pair ("arch", info->machine);
|
|
|
|
pair ("bits", sdb_fmt (0, "%d", info->bits));
|
|
|
|
pair ("endian", info->big_endian? "big": "little");
|
2012-12-23 20:51:23 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-09-09 17:01:04 +02:00
|
|
|
} else fn = (cf && cf->desc) ? cf->desc->name : NULL;
|
2014-04-27 02:06:50 -05:00
|
|
|
if (cf && mode == R_CORE_BIN_JSON) {
|
2012-12-23 20:51:23 +01:00
|
|
|
r_cons_printf ("\"file\":\"%s\"", fn);
|
|
|
|
if (dbg) dbg = R_IO_WRITE | R_IO_EXEC;
|
2014-10-08 13:27:33 +02:00
|
|
|
if (cf->desc) {
|
2014-11-07 10:52:44 +01:00
|
|
|
r_cons_printf (",\"uri\":\"%s\"", cf->desc->uri);
|
2014-09-10 02:21:10 +02:00
|
|
|
r_cons_printf (",\"fd\":%d", cf->desc->fd);
|
2014-10-08 13:27:33 +02:00
|
|
|
r_cons_printf (",\"size\":%"PFMT64d, r_io_desc_size (core->io, cf->desc));
|
|
|
|
r_cons_printf (",\"mode\":\"%s\"", r_str_rwx_i (
|
|
|
|
cf->desc->flags & 7 ));
|
2014-11-10 05:17:25 +01:00
|
|
|
if (cf->desc->referer && *cf->desc->referer)
|
2014-11-07 10:52:44 +01:00
|
|
|
r_cons_printf ("\"referer\":\"%s\"", cf->desc->referer);
|
2014-10-08 13:27:33 +02:00
|
|
|
}
|
2012-12-23 20:51:23 +01:00
|
|
|
r_cons_printf (",\"block\":%d", core->blocksize);
|
2014-05-06 11:17:03 +02:00
|
|
|
if (binfile) {
|
|
|
|
if (binfile->curxtr)
|
|
|
|
r_cons_printf (",\"packet\":\"%s\"",
|
|
|
|
binfile->curxtr->name);
|
2014-05-08 18:35:04 -05:00
|
|
|
if (plugin)
|
2014-05-06 11:17:03 +02:00
|
|
|
r_cons_printf (",\"format\":\"%s\"",
|
2014-05-08 18:35:04 -05:00
|
|
|
plugin->name);
|
2014-05-06 11:17:03 +02:00
|
|
|
}
|
2012-12-23 20:51:23 +01:00
|
|
|
r_cons_printf ("}");
|
2014-12-01 11:54:58 +01:00
|
|
|
} else if (cf && mode != R_CORE_BIN_SIMPLE) {
|
2013-02-20 02:24:37 +01:00
|
|
|
//r_cons_printf ("# Core file info\n");
|
2014-12-03 13:15:07 +01:00
|
|
|
pair ("file", fn);
|
2012-12-23 20:51:23 +01:00
|
|
|
if (dbg) dbg = R_IO_WRITE | R_IO_EXEC;
|
2014-10-08 13:27:33 +02:00
|
|
|
if (cf->desc) {
|
2014-11-10 05:17:25 +01:00
|
|
|
if (cf->desc->referer && *cf->desc->referer)
|
2014-12-03 13:15:07 +01:00
|
|
|
pair ("referer", cf->desc->referer);
|
|
|
|
pair ("fd", sdb_fmt (0, "%d", cf->desc->fd));
|
|
|
|
pair ("size", sdb_fmt (0,"0x%"PFMT64x, r_io_desc_size (core->io, cf->desc)));
|
|
|
|
pair ("mode", r_str_rwx_i (cf->desc->flags & 7));
|
|
|
|
pair ("uri", cf->desc->uri);
|
2014-10-08 13:27:33 +02:00
|
|
|
}
|
2014-12-03 13:15:07 +01:00
|
|
|
pair ("block", sdb_fmt (0, "0x%x", core->blocksize));
|
2014-04-27 02:06:50 -05:00
|
|
|
if (binfile && binfile->curxtr)
|
2014-12-03 13:15:07 +01:00
|
|
|
pair ("packet", binfile->curxtr->name);
|
2014-05-08 18:35:04 -05:00
|
|
|
if (plugin)
|
2014-12-03 13:15:07 +01:00
|
|
|
pair ("format", plugin->name);
|
2012-12-23 20:51:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-18 12:56:38 +01:00
|
|
|
static void cmd_info_bin(RCore *core, ut64 offset, int va, int mode) {
|
|
|
|
if (core->file) {
|
|
|
|
if (mode == R_CORE_BIN_JSON)
|
2014-06-07 11:47:12 +02:00
|
|
|
r_cons_printf ("{\"bin\":");
|
2014-01-18 12:56:38 +01:00
|
|
|
r_core_bin_info (core, R_CORE_BIN_ACC_INFO,
|
2014-08-15 03:30:45 +02:00
|
|
|
mode, va, NULL, offset, NULL);
|
2014-01-18 12:56:38 +01:00
|
|
|
if (mode == R_CORE_BIN_JSON)
|
|
|
|
r_cons_printf (",\"core\":");
|
|
|
|
r_core_file_info (core, mode);
|
|
|
|
if (mode == R_CORE_BIN_JSON)
|
|
|
|
r_cons_printf ("}\n");
|
|
|
|
} else eprintf ("No selected file\n");
|
|
|
|
}
|
|
|
|
|
2012-02-27 03:07:32 +01:00
|
|
|
static int cmd_info(void *data, const char *input) {
|
|
|
|
RCore *core = (RCore *)data;
|
2013-12-20 01:20:17 +01:00
|
|
|
int newline = r_config_get_i (core->config, "scr.interactive");
|
2012-02-27 03:07:32 +01:00
|
|
|
ut64 offset = r_bin_get_offset (core->bin);
|
2014-04-27 02:06:50 -05:00
|
|
|
RBinObject *o = r_bin_cur_object (core->bin);
|
|
|
|
RCoreFile *cf = core->file;
|
|
|
|
|
2012-02-27 03:07:32 +01:00
|
|
|
int va = core->io->va || core->io->debug;
|
2014-01-18 12:56:38 +01:00
|
|
|
int mode = 0; //R_CORE_BIN_SIMPLE;
|
2014-01-18 04:53:33 +01:00
|
|
|
int is_array = 0;
|
2013-12-20 01:20:17 +01:00
|
|
|
Sdb *db;
|
2012-12-23 20:51:23 +01:00
|
|
|
|
2014-01-18 04:53:33 +01:00
|
|
|
if (strchr (input, '*'))
|
|
|
|
mode = R_CORE_BIN_RADARE;
|
|
|
|
if (strchr (input, 'j'))
|
|
|
|
mode = R_CORE_BIN_JSON;
|
2014-12-01 11:54:58 +01:00
|
|
|
if (strchr (input, 'q'))
|
|
|
|
mode = R_CORE_BIN_SIMPLE;
|
2014-01-18 04:53:33 +01:00
|
|
|
|
|
|
|
if (mode == R_CORE_BIN_JSON) {
|
|
|
|
if (strlen (input+1)>1)
|
|
|
|
is_array = 1;
|
|
|
|
}
|
|
|
|
if (is_array)
|
|
|
|
r_cons_printf ("{");
|
2014-01-18 12:56:38 +01:00
|
|
|
if (!*input)
|
|
|
|
cmd_info_bin (core, offset, va, mode);
|
2014-01-18 04:53:33 +01:00
|
|
|
while (*input) {
|
|
|
|
switch (*input) {
|
|
|
|
case 'b':
|
|
|
|
{
|
|
|
|
ut64 baddr = r_config_get_i (core->config, "bin.baddr");
|
2014-05-26 03:06:29 +02:00
|
|
|
if (input[1]==' ')
|
|
|
|
baddr = r_num_math (core->num, input+1);
|
2014-01-18 04:53:33 +01:00
|
|
|
// XXX: this will reload the bin using the buffer.
|
|
|
|
// An assumption is made that assumes there is an underlying
|
|
|
|
// plugin that will be used to load the bin (e.g. malloc://)
|
|
|
|
// TODO: Might be nice to reload a bin at a specified offset?
|
|
|
|
r_core_bin_reload (core, NULL, baddr);
|
|
|
|
r_core_block_read (core, 0);
|
|
|
|
}
|
2013-12-20 01:20:17 +01:00
|
|
|
break;
|
2014-01-18 04:53:33 +01:00
|
|
|
case 'k':
|
2014-04-27 02:06:50 -05:00
|
|
|
db = o ? o->kv : NULL;
|
2014-01-18 04:53:33 +01:00
|
|
|
//:eprintf ("db = %p\n", db);
|
|
|
|
switch (input[1]) {
|
|
|
|
case 'v':
|
2014-09-17 15:23:56 +02:00
|
|
|
if (db) {
|
|
|
|
char *o = sdb_querys (db, NULL, 0, input+3);
|
|
|
|
if (o && *o) r_cons_printf ("%s", o);
|
|
|
|
free (o);
|
|
|
|
}
|
2014-01-18 04:53:33 +01:00
|
|
|
break;
|
|
|
|
case '.':
|
|
|
|
case ' ':
|
2014-09-17 15:23:56 +02:00
|
|
|
if (db) {
|
|
|
|
char *o = sdb_querys (db, NULL, 0, input+2);
|
|
|
|
if (o && *o) r_cons_printf ("%s", o);
|
|
|
|
free (o);
|
|
|
|
}
|
2014-01-18 04:53:33 +01:00
|
|
|
break;
|
|
|
|
case '\0':
|
2014-09-17 15:23:56 +02:00
|
|
|
if (db) {
|
|
|
|
char *o = sdb_querys (db, NULL, 0, "*");
|
|
|
|
if (o && *o) r_cons_printf ("%s", o);
|
|
|
|
free (o);
|
|
|
|
}
|
2014-01-18 04:53:33 +01:00
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
eprintf ("Usage: ik [sdb-query]\n");
|
|
|
|
}
|
2013-12-20 01:20:17 +01:00
|
|
|
break;
|
2014-09-24 23:01:03 +02:00
|
|
|
case 'o':
|
2014-08-28 03:11:13 +02:00
|
|
|
{
|
2014-09-09 17:01:04 +02:00
|
|
|
const char *fn = input[1]==' '? input+2: cf->desc->name;
|
2014-08-28 03:11:13 +02:00
|
|
|
ut64 laddr = UT64_MAX;
|
|
|
|
laddr = r_config_get_i (core->config, "bin.baddr");
|
|
|
|
r_core_bin_load (core, fn, laddr);
|
|
|
|
}
|
2014-01-18 04:53:33 +01:00
|
|
|
break;
|
|
|
|
#define RBININFO(n,x) \
|
|
|
|
if (is_array) { \
|
|
|
|
if (is_array==1) is_array++; else r_cons_printf (","); \
|
|
|
|
r_cons_printf ("\"%s\":",n); \
|
|
|
|
}\
|
2014-08-15 03:30:45 +02:00
|
|
|
r_core_bin_info (core,x,mode,va,NULL,offset,NULL);
|
2014-04-23 03:54:06 +02:00
|
|
|
case 'A': newline=0; r_bin_list_archs (core->bin, 1); break;
|
2014-08-07 01:10:58 +02:00
|
|
|
case 'Z': RBININFO ("size",R_CORE_BIN_ACC_SIZE); break;
|
2014-01-18 04:53:33 +01:00
|
|
|
case 'S': RBININFO ("sections",R_CORE_BIN_ACC_SECTIONS); break;
|
|
|
|
case 'h': RBININFO ("fields", R_CORE_BIN_ACC_FIELDS); break;
|
|
|
|
case 'l': RBININFO ("libs", R_CORE_BIN_ACC_LIBS); break;
|
|
|
|
case 's': RBININFO ("symbols", R_CORE_BIN_ACC_SYMBOLS); break;
|
|
|
|
case 'R':
|
|
|
|
case 'r': RBININFO ("relocs", R_CORE_BIN_ACC_RELOCS); break;
|
|
|
|
case 'd': RBININFO ("dwarf", R_CORE_BIN_ACC_DWARF); break;
|
|
|
|
case 'i': RBININFO ("imports",R_CORE_BIN_ACC_IMPORTS); break;
|
|
|
|
case 'I': RBININFO ("info", R_CORE_BIN_ACC_INFO); break;
|
|
|
|
case 'e': RBININFO ("entries",R_CORE_BIN_ACC_ENTRIES); break;
|
2014-09-24 23:01:03 +02:00
|
|
|
case 'z':
|
2014-09-15 23:47:23 +02:00
|
|
|
if (input[1] == 'z') {
|
|
|
|
/* TODO: reimplement in C to avoid forks */
|
2014-09-15 23:49:51 +02:00
|
|
|
char *ret;
|
|
|
|
switch (input[2]) {
|
|
|
|
case '*':
|
|
|
|
ret = r_sys_cmd_strf ("rabin2 -rzz '%s'", core->file->desc->name);
|
|
|
|
break;
|
2014-12-01 11:54:58 +01:00
|
|
|
case 'q':
|
|
|
|
ret = r_sys_cmd_strf ("rabin2 -qzz '%s'", core->file->desc->name);
|
|
|
|
break;
|
2014-09-15 23:49:51 +02:00
|
|
|
case 'j':
|
|
|
|
ret = r_sys_cmd_strf ("rabin2 -jzz '%s'", core->file->desc->name);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = r_sys_cmd_strf ("rabin2 -zz '%s'", core->file->desc->name);
|
|
|
|
break;
|
|
|
|
}
|
2014-09-15 23:47:23 +02:00
|
|
|
if (ret && *ret) {
|
|
|
|
r_cons_strcat (ret);
|
|
|
|
}
|
|
|
|
free (ret);
|
|
|
|
input++;
|
|
|
|
} else {
|
2014-09-15 23:49:51 +02:00
|
|
|
RBININFO ("strings", R_CORE_BIN_ACC_STRINGS);
|
2014-09-15 23:47:23 +02:00
|
|
|
}
|
|
|
|
break;
|
2014-01-18 04:53:33 +01:00
|
|
|
case 'c':
|
2014-09-15 23:49:51 +02:00
|
|
|
case 'C': RBININFO ("classes", R_CORE_BIN_ACC_CLASSES); break;
|
2014-01-18 04:53:33 +01:00
|
|
|
case 'a':
|
|
|
|
{
|
|
|
|
switch (mode) {
|
|
|
|
case R_CORE_BIN_RADARE: cmd_info (core, "i*IiesSz"); break;
|
2014-08-14 21:47:38 +02:00
|
|
|
case R_CORE_BIN_JSON: cmd_info (core, "iIiesSzj"); break;
|
2014-01-18 04:53:33 +01:00
|
|
|
default:
|
|
|
|
case R_CORE_BIN_SIMPLE: cmd_info (core, "iIiesSz"); break;
|
|
|
|
}
|
|
|
|
}
|
2013-12-20 01:20:17 +01:00
|
|
|
break;
|
2014-06-30 12:25:46 -04:00
|
|
|
case '?': {
|
|
|
|
const char * help_message[] = {
|
|
|
|
"Usage: i", "", "Get info from opened file",
|
|
|
|
"Output mode:", "", "",
|
|
|
|
"'*'", "", "Output in radare commands",
|
|
|
|
"'j'", "", "Output in json",
|
|
|
|
"'q'", "", "Simple quiet output",
|
|
|
|
"Actions:", "", "",
|
|
|
|
"i|ij", "", "Show info of current file (in JSON)",
|
|
|
|
"iA", "", "List archs",
|
|
|
|
"ia", "", "Show all info (imports, exports, sections..)",
|
|
|
|
"ib", "", "Reload the current buffer for setting of the bin (use once only)",
|
|
|
|
"ic", "", "List classes",
|
|
|
|
"id", "", "Debug information (source lines)",
|
|
|
|
"ie", "", "Entrypoint",
|
|
|
|
"ih", "", "Headers",
|
|
|
|
"ii", "", "Imports",
|
|
|
|
"iI", "", "Binary info",
|
|
|
|
"ik", " [query]", "Key-value database from RBinObject",
|
|
|
|
"il", "", "Libraries",
|
|
|
|
"io", " [file]", "Load info from file (or last opened) use bin.baddr",
|
|
|
|
"ir|iR", "", "Relocs",
|
|
|
|
"is", "", "Symbols",
|
|
|
|
"iS", "", "Sections",
|
2014-09-24 23:01:03 +02:00
|
|
|
"iz", "", "Strings in data sections",
|
|
|
|
"izz", "", "Search for Strings in the whole binary",
|
2014-06-30 12:25:46 -04:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
r_core_cmd_help(core, help_message);
|
|
|
|
|
|
|
|
}
|
2014-06-19 01:43:59 +02:00
|
|
|
goto done;
|
2014-01-18 04:53:33 +01:00
|
|
|
case '*':
|
|
|
|
mode = R_CORE_BIN_RADARE;
|
2014-06-19 01:43:59 +02:00
|
|
|
goto done;
|
2014-12-01 11:54:58 +01:00
|
|
|
case 'q':
|
|
|
|
mode = R_CORE_BIN_SIMPLE;
|
|
|
|
cmd_info_bin (core, offset, va, mode);
|
|
|
|
goto done;
|
2014-01-18 04:53:33 +01:00
|
|
|
case 'j':
|
|
|
|
mode = R_CORE_BIN_JSON;
|
2014-05-13 00:38:06 +02:00
|
|
|
cmd_info_bin (core, offset, va, mode);
|
2014-06-19 01:43:59 +02:00
|
|
|
goto done;
|
2013-12-20 01:20:17 +01:00
|
|
|
default:
|
2014-01-18 12:56:38 +01:00
|
|
|
cmd_info_bin (core, offset, va, mode);
|
2014-05-13 00:38:06 +02:00
|
|
|
break;
|
2012-02-27 03:07:32 +01:00
|
|
|
}
|
2014-01-18 04:53:33 +01:00
|
|
|
input++;
|
2014-06-19 01:43:59 +02:00
|
|
|
if (!strcmp (input, "j"))
|
|
|
|
break;
|
2014-12-01 11:54:58 +01:00
|
|
|
if (!strcmp (input, "q"))
|
|
|
|
break;
|
2012-02-27 03:07:32 +01:00
|
|
|
}
|
2014-06-19 01:43:59 +02:00
|
|
|
done:
|
2014-01-18 04:53:33 +01:00
|
|
|
if (is_array)
|
|
|
|
r_cons_printf ("}\n");
|
|
|
|
if (newline) r_cons_newline();
|
2012-02-27 03:07:32 +01:00
|
|
|
return 0;
|
|
|
|
}
|