radare2/libr/core/cmd_print.c

4624 lines
128 KiB
C
Raw Normal View History

2017-02-10 10:52:29 +01:00
/* radare - LGPL - Copyright 2009-2017 - pancake */
2016-05-17 00:37:47 +02:00
2016-05-02 22:52:41 -04:00
#include "r_asm.h"
#include "r_core.h"
#include "r_config.h"
#include "r_print.h"
#include "r_types.h"
#include "r_util.h"
#include "ht.h"
#include <limits.h>
2016-02-18 11:08:12 +01:00
#define R_CORE_MAX_DISASM (1024*1024*8)
#define PF_USAGE_STR "pf[.k[.f[=v]]|[v]]|[n]|[0|cnt][fmt] [a0 a1 ...]"
static void cmd_pCd(RCore *core, const char *input) {
#define C(x) r_cons_canvas_##x
int h, w = r_cons_get_size (&h);
int colwidth = r_config_get_i (core->config, "hex.cols") * 2.5;
int i, columns = w / colwidth;
int rows = h - 2;
int obsz = core->blocksize;
int user_rows = r_num_math (core->num, input);
2016-10-27 01:56:38 +02:00
bool asm_minicols = r_config_get_i (core->config, "asm.minicols");
char *o_ao = strdup (r_config_get (core->config, "asm.offset"));
char *o_ab = strdup (r_config_get (core->config, "asm.bytes"));
if (asm_minicols) {
r_config_set (core->config, "asm.offset", "false");
2016-12-02 21:08:09 +01:00
// r_config_set (core->config, "asm.bytes", "false");
2016-10-27 01:56:38 +02:00
}
2016-12-02 21:08:09 +01:00
r_config_set (core->config, "asm.bytes", "false");
if (user_rows > 0) {
rows = user_rows + 1;
}
r_cons_push ();
RConsCanvas *c = r_cons_canvas_new (w, rows);
ut64 osek = core->offset;
c->color = r_config_get_i (core->config, "scr.color");
r_core_block_size (core, rows * 32);
for (i = 0; i < columns; i++) {
(void)C(gotoxy)(c, i * (w / columns), 0);
char *cmd = r_str_newf ("pid %d @i:%d", rows, rows * i);
char *dis = r_core_cmd_str (core, cmd);
C(write)(c, dis);
free (cmd);
free (dis);
}
r_core_block_size (core, obsz);
r_core_seek (core, osek, 1);
r_cons_pop ();
C(print)(c);
C(free)(c);
2016-10-27 01:56:38 +02:00
if (asm_minicols) {
r_config_set (core->config, "asm.offset", o_ao);
r_config_set (core->config, "asm.bytes", o_ab);
}
r_config_set (core->config, "asm.bytes", o_ab);
2016-10-27 01:56:38 +02:00
free (o_ao);
free (o_ab);
}
static void cmd_pCD(RCore *core, const char *input) {
#define C(x) r_cons_canvas_##x
int h, w = r_cons_get_size (&h);
int colwidth = r_config_get_i (core->config, "hex.cols") * 2.5;
int i, columns = w / colwidth / 2;
int rows = h - 2;
int obsz = core->blocksize;
int user_rows = r_num_math (core->num, input);
bool asm_minicols = r_config_get_i (core->config, "asm.minicols");
char *o_ao = strdup (r_config_get (core->config, "asm.offset"));
char *o_ab = strdup (r_config_get (core->config, "asm.bytes"));
if (asm_minicols) {
r_config_set (core->config, "asm.offset", "false");
r_config_set (core->config, "asm.bytes", "false");
}
r_config_set (core->config, "asm.bytes", "false");
if (user_rows > 0) {
rows = user_rows + 1;
}
r_cons_push ();
RConsCanvas *c = r_cons_canvas_new (w, rows);
ut64 osek = core->offset;
c->color = r_config_get_i (core->config, "scr.color");
r_core_block_size (core, rows * 32);
2017-02-05 02:01:53 +00:00
char *cmd = NULL;
columns = 2;
for (i = 0; i < columns; i++) {
switch (i) {
case 0:
(void)C(gotoxy)(c, 0, 0);
cmd = r_str_newf ("dr;?e;?e backtrace:;dbt");
break;
case 1:
(void)C(gotoxy)(c, 28, 0);
if (core->assembler->bits == 64) {
cmd = r_str_newf ("pxq 128@r:SP;pd@r:PC");
} else {
cmd = r_str_newf ("pxw 128@r:SP;pd@r:PC");
}
break;
}
char *dis = r_core_cmd_str (core, cmd);
C(write)(c, dis);
free (cmd);
free (dis);
}
r_core_block_size (core, obsz);
r_core_seek (core, osek, 1);
r_cons_pop ();
C(print)(c);
C(free)(c);
if (asm_minicols) {
r_config_set (core->config, "asm.offset", o_ao);
r_config_set (core->config, "asm.bytes", o_ab);
}
r_config_set (core->config, "asm.bytes", o_ab);
free (o_ao);
free (o_ab);
}
static void cmd_pCx(RCore *core, const char *input, const char *xcmd) {
#define C(x) r_cons_canvas_##x
int h, w = r_cons_get_size (&h);
int hex_cols = r_config_get_i (core->config, "hex.cols");
int colwidth = hex_cols * 5;
int i, columns = w / (colwidth * 0.9);
int rows = h - 2;
int user_rows = r_num_math (core->num, input);
r_config_set_i (core->config, "hex.cols", colwidth / 5);
if (user_rows > 0) {
rows = user_rows + 1;
}
r_cons_push ();
RConsCanvas *c = r_cons_canvas_new (w, rows);
ut64 tsek = core->offset;
c->color = r_config_get_i (core->config, "scr.color");
int bsize = hex_cols * rows;
if (!strcmp (xcmd, "pxA")) {
bsize *= 12;
}
for (i = 0; i < columns; i++) {
(void)C(gotoxy)(c, i * (w / columns), 0);
char *cmd = r_str_newf ("%s %d @ %"PFMT64d, xcmd, bsize, tsek);
char *dis = r_core_cmd_str (core, cmd);
C(write)(c, dis);
free (cmd);
free (dis);
tsek += bsize - 32;
}
r_cons_pop ();
C(print)(c);
C(free)(c);
r_config_set_i (core->config, "hex.cols", hex_cols);
}
2015-03-23 19:53:19 +01:00
static char get_string_type (const ut8 *buf, ut64 len){
ut64 needle = 0;
int rc, i;
2015-06-02 10:25:40 +02:00
char str_type = 0;
2015-03-23 19:53:19 +01:00
if (!buf) {
2015-03-23 19:53:19 +01:00
return '?';
}
2015-03-23 19:53:19 +01:00
while (needle < len){
rc = r_utf8_decode (buf+needle, len-needle, NULL);
if (!rc) {
2015-03-23 19:53:19 +01:00
needle++;
continue;
}
if (needle+rc+2 < len &&
buf[needle+rc+0] == 0x00 &&
buf[needle+rc+1] == 0x00 &&
2016-09-01 22:34:28 +02:00
buf[needle+rc+2] == 0x00) {
2015-03-23 19:53:19 +01:00
str_type = 'w';
2016-09-01 22:34:28 +02:00
} else {
2015-03-23 19:53:19 +01:00
str_type = 'a';
2016-09-01 22:34:28 +02:00
}
2015-03-23 19:53:19 +01:00
for (rc = i = 0; needle < len ; i+= rc){
RRune r;
if (str_type == 'w'){
if (needle+1 < len){
r = buf[needle+1] << 8 | buf[needle];
rc = 2;
} else {
2015-03-23 19:53:19 +01:00
break;
}
} else {
2015-03-23 19:53:19 +01:00
rc = r_utf8_decode (buf+needle, len-needle, &r);
if(rc > 1) str_type = 'u';
}
/*Invalid sequence detected*/
2016-09-01 22:34:28 +02:00
if (!rc) {
2015-03-23 19:53:19 +01:00
needle++;
break;
}
needle += rc;
}
}
return str_type;
}
2017-01-11 00:22:18 +01:00
static void cmd_print_eq_dict(RCore *core, const ut8* block, int bsz) {
int i;
int min = 0;
int max = 0;
int dict = 0;
int range = 0;
2017-01-11 00:22:18 +01:00
bool histogram[0xff+1];
for (i = 0; i < 0xff; i++) {
2017-01-11 00:45:35 +01:00
histogram[i] = false;
}
2017-01-11 00:22:18 +01:00
for (i = 0; i < bsz; i++) {
histogram[block[i]] = true;
}
2017-01-11 00:22:18 +01:00
for (i = 0; i < 0xff; i++) {
if (histogram[i]) {
if (min == 0) {
min = i;
2017-01-11 00:22:18 +01:00
}
max = i;
dict++;
}
}
range = max - min;
r_cons_printf ("min: %d 0x%x\n", min, min);
r_cons_printf ("max: %d 0x%x\n", max, max);
r_cons_printf ("dict: %d 0x%x\n", dict, dict);
r_cons_printf ("range: %d 0x%x\n", range, range);
2017-01-11 00:22:18 +01:00
r_cons_printf ("size: %d 0x%x\n", bsz, bsz);
}
2016-03-23 01:44:56 +02:00
R_API void r_core_set_asm_configs(RCore *core, char *arch, ut32 bits, int segoff){
r_config_set (core->config, "asm.arch", arch);
r_config_set_i (core->config, "asm.bits", bits);
// XXX - this needs to be done here, because
// if arch == x86 and bits == 16, segoff automatically changes
r_config_set_i (core->config, "asm.segoff", segoff);
}
static void cmd_pDj (RCore *core, const char *arg) {
2014-08-22 00:21:37 +02:00
int bsize = r_num_math (core->num, arg);
2017-02-05 01:24:50 +01:00
if (bsize < 0) {
bsize = -bsize;
}
r_cons_print ("[");
if (bsize <= core->blocksize) {
r_core_print_disasm_json (core, core->offset, core->block,
bsize, 0);
} else {
ut8 *buf = malloc (bsize);
if (buf) {
r_io_read_at (core->io, core->offset, buf, bsize);
r_core_print_disasm_json (core, core->offset, buf, bsize, 0);
free (buf);
} else {
eprintf ("cannot allocate %d bytes\n", bsize);
}
}
2017-02-05 03:00:45 +01:00
r_cons_print ("]");
2014-08-22 00:00:42 +02:00
}
2014-08-22 00:39:31 +02:00
static void cmd_pdj (RCore *core, const char *arg) {
2015-11-22 21:28:11 +01:00
int nblines = r_num_math (core->num, arg);
2017-02-05 01:24:50 +01:00
r_cons_print ("[");
2015-11-22 21:28:11 +01:00
r_core_print_disasm_json (core, core->offset, core->block, core->blocksize, nblines);
2017-02-05 01:24:50 +01:00
r_cons_print ("]\n");
2014-08-22 00:39:31 +02:00
}
static int process_input(RCore *core, const char *input, ut64* blocksize, char **asm_arch, ut32 *bits) {
// input: start of the input string e.g. after the command symbols have been consumed
// size: blocksize if present, otherwise -1
// asm_arch: asm_arch to interpret as if present and valid, otherwise NULL;
// bits: bits to use if present, otherwise -1
2015-09-14 12:35:38 +02:00
int result = false;
2013-12-02 00:43:44 +01:00
char *input_one = NULL, *input_two = NULL, *input_three = NULL;
2016-09-01 22:34:28 +02:00
char *str_clone = NULL, *ptr_str_clone = NULL, *trimmed_clone = NULL;
2016-09-01 22:34:28 +02:00
if (!input || !blocksize || !asm_arch || !bits) {
2015-09-14 12:35:38 +02:00
return false;
}
str_clone = strdup (input);
trimmed_clone = r_str_trim_head_tail (str_clone);
input_one = trimmed_clone;
ptr_str_clone = strchr (trimmed_clone, ' ');
// terminate input_one
if (ptr_str_clone) {
*ptr_str_clone = '\0';
input_two = (++ptr_str_clone);
ptr_str_clone = strchr (input_two, ' ');
}
// terminate input_two
if (ptr_str_clone && input_two) {
*ptr_str_clone = '\0';
input_three = (++ptr_str_clone);
ptr_str_clone = strchr (input_three, ' ');
}
// terminate input_three
if (ptr_str_clone && input_three) {
*ptr_str_clone = '\0';
ptr_str_clone = strchr (input_three, ' ');
}
// command formats
// <size> <arch> <bits>
// <size> <arch>
// <size> <bits>
// <arch> <bits>
// <arch>
// initialize
*asm_arch = NULL;
*blocksize = *bits = -1;
if (input_one && input_two && input_three) {
// <size> <arch> <bits>
2014-04-20 17:22:51 -05:00
*blocksize = r_num_is_valid_input (core->num, input_one) ? r_num_get_input_value (core->num, input_one): 0;
*asm_arch = r_asm_is_valid (core->assembler, input_two) ? strdup (input_two) : NULL;
2014-04-20 17:22:51 -05:00
*bits = r_num_get_input_value (core->num, input_three);
2015-09-14 12:35:38 +02:00
result = true;
} else if (input_one && input_two) {
2014-04-20 17:22:51 -05:00
*blocksize = r_num_is_valid_input (core->num, input_one) ? r_num_get_input_value (core->num, input_one): 0;
2014-04-20 17:22:51 -05:00
if (!r_num_is_valid_input (core->num, input_one) ) {
// input_one can only be one other thing
*asm_arch = r_asm_is_valid (core->assembler, input_one) ? strdup (input_one) : NULL;
2014-04-20 17:22:51 -05:00
*bits = r_num_is_valid_input (core->num, input_two) ? r_num_get_input_value (core->num, input_two): -1;
} else {
if (r_str_contains_macro (input_two) ){
r_str_truncate_cmd (input_two);
}
2015-03-25 06:56:37 +05:30
*bits = r_num_is_valid_input (core->num, input_two) ? r_num_get_input_value (core->num, input_two): -1;
*asm_arch = r_asm_is_valid (core->assembler, input_two) ? strdup (input_two) : NULL;
}
2015-09-14 12:35:38 +02:00
result = true;
} else if (input_one) {
2014-04-20 17:22:51 -05:00
*blocksize = r_num_is_valid_input (core->num, input_one) ? r_num_get_input_value (core->num, input_one): 0;
if (!r_num_is_valid_input (core->num, input_one) ) {
// input_one can only be one other thing
2013-12-02 00:43:44 +01:00
if (r_str_contains_macro (input_one))
r_str_truncate_cmd (input_one);
*asm_arch = r_asm_is_valid (core->assembler, input_one) ? strdup (input_one) : NULL;
}
2015-09-14 12:35:38 +02:00
result = true;
}
2016-04-07 13:20:11 +05:30
free (str_clone);
return result;
}
/* This function is not necessary anymore, but it's kept for discussion */
2016-03-23 01:44:56 +02:00
R_API int r_core_process_input_pade(RCore *core, const char *input, char** hex, char **asm_arch, ut32 *bits) {
2015-03-25 06:56:37 +05:30
// input: start of the input string e.g. after the command symbols have been consumed
// size: hex if present, otherwise -1
// asm_arch: asm_arch to interpret as if present and valid, otherwise NULL;
// bits: bits to use if present, otherwise -1
2015-09-14 12:35:38 +02:00
int result = false;
2015-03-25 06:56:37 +05:30
char *input_one = NULL, *input_two = NULL, *input_three = NULL;
char *str_clone = NULL,
*trimmed_clone = NULL;
if (!input || !hex || !asm_arch || !bits) {
2015-09-14 12:35:38 +02:00
return false;
2015-03-25 06:56:37 +05:30
}
str_clone = strdup (input);
trimmed_clone = r_str_trim_head_tail (str_clone);
input_one = trimmed_clone;
#if 0
char *ptr_str_clone = NULL;
2015-03-25 06:56:37 +05:30
ptr_str_clone = strchr (trimmed_clone, ' ');
// terminate input_one
if (ptr_str_clone) {
*ptr_str_clone = '\0';
input_two = (++ptr_str_clone);
ptr_str_clone = strchr (input_two, ' ');
}
// terminate input_two
if (ptr_str_clone && input_two) {
*ptr_str_clone = '\0';
input_three = (++ptr_str_clone);
ptr_str_clone = strchr (input_three, ' ');
}
// terminate input_three
if (ptr_str_clone && input_three) {
*ptr_str_clone = '\0';
ptr_str_clone = strchr (input_three, ' ');
}
#endif
2015-03-25 06:56:37 +05:30
// command formats
// <hex> <arch> <bits>
// <hex> <arch>
// <hex> <bits>
// <hex>
// initialize
*hex = *asm_arch = NULL;
*bits = -1;
if (input_one && input_two && input_three) {
// <size> <arch> <bits>
2015-03-27 13:41:42 +01:00
*hex = input_one;
2015-03-25 06:56:37 +05:30
*asm_arch = r_asm_is_valid (core->assembler, input_two) ? strdup (input_two) : NULL;
*bits = r_num_get_input_value (core->num, input_three);
2015-09-14 12:35:38 +02:00
result = true;
2015-03-25 06:56:37 +05:30
} else if (input_one && input_two) {
2015-03-27 13:41:42 +01:00
*hex = input_one;
2015-03-25 06:56:37 +05:30
if (r_str_contains_macro (input_two) ){
r_str_truncate_cmd (input_two);
}
*bits = r_num_is_valid_input (core->num, input_two) ? r_num_get_input_value (core->num, input_two): -1;
*asm_arch = r_asm_is_valid (core->assembler, input_two) ? strdup (input_two) : NULL;
2015-09-14 12:35:38 +02:00
result = true;
2015-03-25 06:56:37 +05:30
} else if (input_one) {
2015-03-27 13:41:42 +01:00
*hex = input_one;
2015-09-14 12:35:38 +02:00
result = true;
2015-03-27 13:41:42 +01:00
} else {
free (input_one);
2015-03-25 06:56:37 +05:30
}
return result;
}
2014-10-26 16:26:03 +01:00
static void print_format_help(RCore *core) {
const char* help_msg[] = {
"pf:", PF_USAGE_STR, "",
2015-03-19 23:20:52 +01:00
"Commands:","","",
2014-10-28 14:36:19 +01:00
"pf", "?", "Show this help",
2015-03-19 23:20:52 +01:00
"pf", "??", "Format characters",
"pf", "???", "pf usage examples",
"pf", " fmt", "Show data using the given format-string. See 'pf\?\?' and 'pf\?\?\?'.",
"pf.", "fmt_name", "Show data using named format",
"pf.", "fmt_name.field_name", "Show specific data field using named format",
"pfj ", "fmt_name|fmt", "Show data using (named) format in JSON",
"pf* ", "fmt_name|fmt", "Show data using (named) format as r2 flag create commands",
"pfd.", "fmt_name", "Show data using named format as graphviz commands",
"pf.", "name [0|cnt]fmt", "Define a new named format",
"pf.", "", "List all format definitions",
"pf?", "fmt_name", "Show the definition of a named format",
"pfo", "", "List all format definition files (fdf)",
"pfo", " fdf_name", "Load a Format Definition File (fdf)",
2017-02-09 09:09:54 +00:00
"pf.", "fmt_name.field_name=33", "Set new value for the specified field in named format",
"pfv.", "fmt_name[.field]", "Print value(s) only for named format. Useful for one-liners",
"pfs", " fmt_name|fmt", "Print the size of (named) format in bytes",
2015-03-19 22:43:47 +01:00
NULL};
r_core_cmd_help (core, help_msg);
}
static void print_format_help_help(RCore *core) {
const char* help_msg[] = {
"pf:", PF_USAGE_STR, "",
2015-03-19 23:20:52 +01:00
"Format:", "", "",
" ", "b", "byte (unsigned)",
" ", "B", "resolve enum bitfield (see t?)",
" ", "c", "char (signed byte)",
2017-01-12 00:31:53 +01:00
" ", "d", "0x%%08x hexadecimal value (4 bytes) (see %%i and %%x)",
2015-03-19 23:20:52 +01:00
" ", "D", "disassemble one opcode",
" ", "e", "temporally swap endian",
" ", "E", "resolve enum name (see t?)",
" ", "f", "float value (4 bytes)",
2017-01-12 00:31:53 +01:00
" ", "i", "%%i signed integer value (4 bytes) (see %%d and %%x)",
" ", "n", "next char specifies size of signed value (1, 2, 4 or 8 byte(s))",
" ", "N", "next char specifies size of unsigned value (1, 2, 4 or 8 byte(s))",
2015-03-19 23:20:52 +01:00
" ", "o", "0x%%08o octal value (4 byte)",
" ", "p", "pointer reference (2, 4 or 8 bytes)",
" ", "q", "quadword (8 bytes)",
2015-07-25 19:29:58 +02:00
" ", "r", "CPU register `pf r (eax)plop`",
2015-03-19 23:20:52 +01:00
" ", "s", "32bit pointer to string (4 bytes)",
" ", "S", "64bit pointer to string (8 bytes)",
" ", "t", "UNIX timestamp (4 bytes)",
" ", "T", "show Ten first bytes of buffer",
" ", "u", "uleb128 (variable length)",
2015-03-19 23:20:52 +01:00
" ", "w", "word (2 bytes unsigned short in hex)",
2017-01-12 00:31:53 +01:00
" ", "x", "0x%%08x hex value and flag (fd @ addr) (see %%d and %%i)",
2015-03-19 23:20:52 +01:00
" ", "X", "show formatted hexpairs",
" ", "z", "\\0 terminated string",
" ", "Z", "\\0 terminated wide string",
2015-07-25 19:29:58 +02:00
" ", "?", "data structure `pf ? (struct_name)example_name`",
2015-03-19 23:20:52 +01:00
" ", "*", "next char is pointer (honors asm.bits)",
" ", "+", "toggle show flags for each offset",
" ", ":", "skip 4 bytes",
" ", ".", "skip 1 byte",
NULL};
r_core_cmd_help (core, help_msg);
}
static void print_format_help_help_help(RCore *core) {
const char* help_msg[] = {
"pf:", PF_USAGE_STR, "",
2015-03-19 22:43:47 +01:00
"Examples:","","",
"pf", " 3xi foo bar", "3-array of struct, each with named fields: 'foo' as hex, and 'bar' as int",
2015-03-19 23:20:52 +01:00
"pf", " B (BitFldType)arg_name`", "bitfield type",
"pf", " E (EnumType)arg_name`", "enum type",
2015-03-19 22:43:47 +01:00
"pf.", "obj xxdz prev next size name", "Define the obj format as xxdz",
"pf", " obj=xxdz prev next size name", "Same as above",
2014-10-26 16:26:03 +01:00
"pf", " iwq foo bar troll", "Print the iwq format with foo, bar, troll as the respective names for the fields",
2015-02-05 01:59:32 +01:00
"pf", " 0iwq foo bar troll", "Same as above, but considered as a union (all fields at offset 0)",
2015-03-19 22:43:47 +01:00
"pf.", "plop ? (troll)mystruct", "Use structure troll previously defined",
2014-10-26 16:26:03 +01:00
"pf", " 10xiz pointer length string", "Print a size 10 array of the xiz struct with its field names",
"pf", " {integer}? (bifc)", "Print integer times the following format (bifc)",
2014-10-26 16:26:03 +01:00
"pf", " [4]w[7]i", "Print an array of 4 words and then an array of 7 integers",
"pf", " ic...?i foo bar \"(pf xw yo foo)troll\" yo", "Print nested anonymous structres",
"pf", "n2", "print signed short (2 bytes) value. Use N insted of n for printing unsigned values",
2015-03-19 22:43:47 +01:00
NULL};
r_core_cmd_help (core, help_msg);
}
static void print_format_help_help_help_help(RCore *core) {
const char* help_msg[] = {
" STAHP IT!!!", "", "",
NULL};
r_core_cmd_help (core, help_msg);
}
2016-05-16 01:18:18 +02:00
static void cmd_print_format(RCore *core, const char *_input, int len) {
2014-10-22 15:57:28 +02:00
char *input;
int mode = R_PRINT_MUSTSEE;
2014-10-26 16:26:03 +01:00
switch (_input[1]) {
case '*':
2014-10-22 15:57:28 +02:00
_input++;
mode = R_PRINT_SEEFLAGS;
2014-10-26 16:26:03 +01:00
break;
case 'd':
_input++;
mode = R_PRINT_DOT;
break;
2014-10-26 16:26:03 +01:00
case 'j':
2014-10-26 02:22:58 +02:00
_input++;
mode = R_PRINT_JSON;
2014-10-26 16:26:03 +01:00
break;
case 'v':
_input++;
mode = R_PRINT_VALUE | R_PRINT_MUSTSEE;
break;
2014-11-01 03:48:41 +01:00
case 's':
{
const char *val = NULL;
_input+=2;
2014-11-01 13:01:42 +01:00
if (*_input == '.') {
_input++;
val = r_strht_get (core->print->formats, _input);
if (val != NULL)
2015-02-05 01:59:32 +01:00
r_cons_printf ("%d bytes\n", r_print_format_struct_size (val, core->print, mode));
2014-11-01 13:01:42 +01:00
else {
eprintf ("Struct %s not defined\nUsage: pfs.struct_name | pfs format\n", _input);
}
} else if (*_input == ' ') {
2016-10-26 23:22:04 +02:00
while (*_input == ' ' && *_input != '\0') {
_input++;
}
if (*_input) {
2015-02-05 01:59:32 +01:00
r_cons_printf ("%d bytes\n", r_print_format_struct_size (_input, core->print, mode));
2016-10-26 23:22:04 +02:00
} else {
2014-11-01 13:01:42 +01:00
eprintf ("Struct %s not defined\nUsage: pfs.struct_name | pfs format\n", _input);
}
} else {
eprintf ("Usage: pfs.struct_name | pfs format\n");
2014-11-01 03:48:41 +01:00
}
}
return;
2014-10-26 16:26:03 +01:00
case '?':
2017-01-20 15:43:25 +01:00
_input += 2;
2014-10-28 14:36:19 +01:00
if (*_input) {
2015-03-19 22:43:47 +01:00
if (*_input == '?') {
_input++;
if (_input && *_input == '?') {
_input++;
if (_input && *_input == '?') {
print_format_help_help_help_help (core);
} else {
print_format_help_help_help (core);
}
} else {
print_format_help_help (core);
}
} else {
RListIter *iter;
RStrHT *sht = core->print->formats;
int *i;
r_list_foreach (sht->ls, iter, i) {
int idx = ((int)(size_t)i)-1;
const char *key = r_strpool_get (sht->sp, idx);
if (!strcmp (_input, key)) {
const char *val = r_strht_get (core->print->formats, key);
r_cons_println (val);
2015-03-19 22:43:47 +01:00
}
2014-10-28 14:36:19 +01:00
}
}
} else {
print_format_help (core);
}
2014-10-26 16:26:03 +01:00
return;
}
2014-10-26 16:26:03 +01:00
2014-10-22 15:57:28 +02:00
input = strdup (_input);
// "pfo" // open formatted thing
2017-01-20 15:43:25 +01:00
if (input[1] == 'o') { // "pfo"
if (input[2] == '?') {
eprintf ("|Usage: pfo [format-file]\n"
" ~/.config/radare2/format\n"
" "R2_DATDIR"/radare2/"R2_VERSION"/format/\n");
} else if (input[2] == ' ') {
char *home, path[512];
2017-01-20 15:43:25 +01:00
// XXX hardcoded path here
snprintf (path, sizeof (path), ".config/radare2/format/%s", input+3);
home = r_str_home (path);
snprintf (path, sizeof (path), R2_DATDIR"/radare2/"
2017-01-20 15:43:25 +01:00
R2_VERSION"/format/%s", input + 3);
if (!r_core_cmd_file (core, home) && !r_core_cmd_file (core, path)) {
if (!r_core_cmd_file (core, input + 3)) {
eprintf ("ecf: cannot open colorscheme profile (%s)\n", path);
}
}
free (home);
} else {
RList *files;
RListIter *iter;
const char *fn;
char *home = r_str_home (".config/radare2/format/");
if (home) {
files = r_sys_dir (home);
r_list_foreach (files, iter, fn) {
if (*fn && *fn != '.') {
r_cons_println (fn);
}
}
r_list_free (files);
free (home);
}
files = r_sys_dir (R2_DATDIR"/radare2/"R2_VERSION"/format/");
r_list_foreach (files, iter, fn) {
if (*fn && *fn != '.') {
r_cons_println (fn);
}
}
r_list_free (files);
}
2015-01-16 00:56:54 +01:00
free (input);
return;
}
/* syntax aliasing bridge for 'pf foo=xxd' -> 'pf.foo xxd' */
if (input[1]==' ') {
char *eq = strchr (input + 2, '=');
if (eq) {
input[1] = '.';
*eq = ' ';
}
}
int listFormats = 0;
2017-01-20 15:43:25 +01:00
if (input[1]=='.') {
listFormats = 1;
2017-01-20 15:43:25 +01:00
}
if (!strcmp (input, "*") && mode == R_PRINT_SEEFLAGS) {
listFormats = 1;
2017-01-20 15:43:25 +01:00
}
core->print->reg = core->dbg->reg;
core->print->get_register = r_reg_get;
core->print->get_register_value = r_reg_get_value;
2015-07-28 18:28:33 +02:00
int o_blocksize = core->blocksize;
if (listFormats) {
core->print->num = core->num;
2015-03-05 20:40:27 -05:00
/* print all stored format */
2017-01-20 15:43:25 +01:00
if (!input[1] || !input[2]) {
RListIter *iter;
RStrHT *sht = core->print->formats;
int *i;
r_list_foreach (sht->ls, iter, i) {
int idx = ((int)(size_t)i)-1;
const char *key = r_strpool_get (sht->sp, idx);
const char *val = r_strht_get (core->print->formats, key);
r_cons_printf ("pf.%s %s\n", key, val);
}
2015-03-05 20:40:27 -05:00
/* delete a format */
2015-05-07 12:26:21 +02:00
} else if (input[1] && input[2]=='-') {
2017-01-20 15:43:25 +01:00
if (input[3] == '*') {
r_strht_clear (core->print->formats);
} else {
r_strht_del (core->print->formats, input + 3);
}
} else {
2017-01-20 15:43:25 +01:00
char *name = strdup (input + (input[1]? 2: 1));
char *space = strchr (name, ' ');
2015-10-22 17:39:05 +02:00
char *eq = strchr (name, '=');
char *dot = strchr (name, '.');
if (eq && !dot) {
*eq = ' ';
space = eq;
eq = NULL;
}
2015-03-05 20:40:27 -05:00
/* store a new format */
if (space && (!eq || space < eq)) {
//char *fields = NULL;
*space++ = 0;
// fields = strchr (space, ' ');
if (strchr (name, '.') != NULL) {// || (fields != NULL && strchr(fields, '.') != NULL)) // if anon struct, then field can have '.'
eprintf ("Struct or fields name can not contain dot symbol (.)\n");
} else {
r_strht_set (core->print->formats, name, space);
}
2015-04-11 02:31:34 +03:00
free (name);
free (input);
return;
}
2015-03-03 00:34:09 +01:00
if (!strchr (name, '.') && !r_strht_get (core->print->formats, name)) {
2015-10-22 17:39:05 +02:00
eprintf ("Cannot find '%s' format.\n", name);
2015-03-05 20:40:27 -05:00
free (name);
2015-04-11 02:31:34 +03:00
free (input);
2015-03-05 20:40:27 -05:00
return;
}
2015-07-28 18:28:33 +02:00
/* Load format from name into fmt to get the size */
/* This make sure the whole structure will be printed */
const char *fmt = NULL;
fmt = r_strht_get (core->print->formats, name);
if (fmt != NULL) {
int size = r_print_format_struct_size (fmt, core->print, mode)+10;
2017-01-20 15:43:25 +01:00
if (size > core->blocksize) {
2015-07-28 18:28:33 +02:00
r_core_block_size (core, size);
2017-01-20 15:43:25 +01:00
}
2015-07-28 18:28:33 +02:00
}
2015-03-05 20:40:27 -05:00
/* display a format */
if (dot) {
*dot++ = 0;
eq = strchr (dot, '=');
if (eq) { // Write mode (pf.field=value)
*eq++ = 0;
mode = R_PRINT_MUSTSET;
r_print_format (core->print, core->offset,
2015-03-03 00:34:09 +01:00
core->block, core->blocksize, name, mode, eq, dot);
} else {
r_print_format (core->print, core->offset,
2015-03-03 00:34:09 +01:00
core->block, core->blocksize, name, mode, NULL, dot);
}
} else {
2015-03-05 20:40:27 -05:00
r_print_format (core->print, core->offset,
2015-07-24 17:04:33 +02:00
core->block, core->blocksize, name, mode, NULL, NULL);
}
free (name);
}
2015-07-28 18:28:33 +02:00
} else {
/* This make sure the structure will be printed entirely */
2017-01-20 15:43:25 +01:00
char *fmt = input + 1;
2017-02-06 00:50:54 +03:00
while (*fmt && ISWHITECHAR (*fmt)) fmt++;
2017-02-09 09:09:54 +00:00
int size = r_print_format_struct_size (fmt, core->print, mode) + 10;
2017-01-20 15:43:25 +01:00
if (size > core->blocksize) {
2015-07-28 18:28:33 +02:00
r_core_block_size (core, size);
2017-01-20 15:43:25 +01:00
}
2017-02-09 09:09:54 +00:00
/* check if fmt is '\d+ \d+<...>', common mistake due to usage string*/
bool syntax_ok = true;
char *args = strdup (fmt);
if(!args) {
r_cons_printf ("Error: Mem Allocation.");
free (args);
goto stage_left;
}
const char *arg1 = strtok (args," ");
if(arg1 && r_str_isnumber (arg1)) {
syntax_ok = false;
r_cons_printf ("Usage: pf [0|cnt][format-string]\n");
}
free (args);
if (syntax_ok) {
r_print_format (core->print, core->offset,
core->block, core->blocksize, fmt, mode, NULL, NULL);
}
2015-07-28 18:28:33 +02:00
}
2017-02-09 09:09:54 +00:00
stage_left:
free (input);
2015-07-28 18:28:33 +02:00
r_core_block_size (core, o_blocksize);
}
// > pxa
2014-08-26 23:52:34 +02:00
/* In this function, most of the buffers have 4 times
* the required length. This is because we supports colours,
2014-08-26 23:52:34 +02:00
* that are 4 chars long. */
#define append(x,y) { strcat (x,y);x += strlen (y); }
static void annotated_hexdump(RCore *core, const char *str, int len) {
const int usecolor = r_config_get_i (core->config, "scr.color");
2014-08-24 05:35:48 +02:00
int nb_cols = r_config_get_i (core->config, "hex.cols");
int flagsz = r_config_get_i (core->config, "hex.flagsz");
const ut8 *buf = core->block;
ut64 addr = core->offset;
2014-09-08 01:33:50 +02:00
int color_idx = 0;
2014-08-26 23:52:34 +02:00
char *bytes, *chars;
char *ebytes, *echars; //They'll walk over the vars above
ut64 fend = UT64_MAX;
char *comment;
2014-08-26 23:52:34 +02:00
int i, j, low, max, here, rows;
2016-01-03 02:05:13 +01:00
bool marks = false, setcolor = true, hascolor = false;
ut8 ch;
const char **colors = (const char **)&core->cons->pal.list;
2014-09-08 01:33:50 +02:00
#if 0
const char *colors[] = {
Color_WHITE, /*Color_GREEN,*/ Color_YELLOW, Color_RED,
Color_CYAN, Color_MAGENTA, Color_GRAY, Color_BLUE
};
#endif
// const char* colors[] = Colors_PLAIN;
2014-08-24 05:35:48 +02:00
const int col = core->print->col;
2014-08-26 23:52:34 +02:00
RFlagItem *flag, *current_flag = NULL;
char** note;
int html = r_config_get_i (core->config, "scr.html");
2014-08-26 23:52:34 +02:00
int nb_cons_cols;
2014-08-26 23:52:34 +02:00
// Adjust the number of columns
if (nb_cols < 1)
nb_cols = 16;
2014-08-24 05:35:48 +02:00
nb_cols -= (nb_cols % 2); //nb_cols should be even
2014-08-26 23:52:34 +02:00
nb_cons_cols = 12 + nb_cols * 2 + (nb_cols/2);
nb_cons_cols += 17;
2014-08-26 23:52:34 +02:00
rows = len/nb_cols;
2014-09-30 11:51:24 +02:00
chars = calloc (nb_cols * 20, sizeof(char));
2014-08-26 23:52:34 +02:00
if (!chars)
return;
note = calloc (nb_cols, sizeof(char*));
if (!note) {
free (chars);
return;
}
2014-09-30 11:51:24 +02:00
bytes = calloc (nb_cons_cols*20, sizeof(char));
2014-08-26 23:52:34 +02:00
if (!bytes) {
free (chars);
2014-08-24 05:35:48 +02:00
free (note);
return;
}
#if 1
int addrpadlen = strlen (sdb_fmt (0, "%08"PFMT64x, addr))-8;
char addrpad[32];
if (addrpadlen>0) {
2014-09-28 01:45:35 +02:00
memset (addrpad, ' ', addrpadlen);
addrpad[addrpadlen] = 0;
//Compute, then show the legend
strcpy (bytes, addrpad);
} else {
*addrpad = 0;
addrpadlen = 0;
}
strcpy (bytes+addrpadlen, "- offset - ");
#endif
j = strlen (bytes);
2014-08-24 05:35:48 +02:00
for (i=0; i<nb_cols; i+=2) {
sprintf (bytes+j, " %X %X ", (i&0xf), (i+1)&0xf);
2014-09-24 22:45:38 +02:00
j += 5;
2014-08-24 05:35:48 +02:00
}
2014-08-26 23:52:34 +02:00
sprintf (bytes+j+i, " ");
2014-08-24 05:35:48 +02:00
j++;
for (i=0; i<nb_cols; i++)
2014-08-26 23:52:34 +02:00
sprintf (bytes+j+i, "%0X", i%17);
if (usecolor) r_cons_strcat (Color_GREEN);
2014-08-26 23:52:34 +02:00
r_cons_strcat (bytes);
if (usecolor) r_cons_strcat (Color_RESET);
2014-08-26 23:52:34 +02:00
r_cons_newline ();
2014-08-24 05:35:48 +02:00
//hexdump
for (i=0; i<rows; i++) {
2014-08-26 23:52:34 +02:00
bytes[0] = '\0';
chars[0] = '\0';
ebytes = bytes;
echars = chars;
2015-09-14 12:35:38 +02:00
hascolor = false;
2014-08-26 23:52:34 +02:00
2014-09-08 02:08:38 +02:00
if (usecolor) append (ebytes, core->cons->pal.offset);
2014-08-26 23:52:34 +02:00
ebytes += sprintf (ebytes, "0x%08"PFMT64x, addr);
if (usecolor) append (ebytes, Color_RESET);
append (ebytes, (col==1)?" |":" ");
2014-08-24 05:35:48 +02:00
for (j=0; j<nb_cols; j++) {
2015-09-14 12:35:38 +02:00
setcolor = true;
free (note[j]);
note[j] = NULL;
2014-08-26 23:52:34 +02:00
// collect comments
2014-01-23 04:00:01 +01:00
comment = r_meta_get_string (core->anal, R_META_TYPE_COMMENT, addr+j);
if (comment) {
2014-08-26 23:52:34 +02:00
comment = r_str_prefix (comment, ";");
note[j] = comment;
2015-09-14 12:35:38 +02:00
marks = true;
}
2014-08-26 23:52:34 +02:00
// collect flags
2014-08-26 23:52:34 +02:00
flag = r_flag_get_i (core->flags, addr+j);
if (flag) { // Beginning of a flag
if (flagsz) {
fend = addr + flagsz; //core->blocksize;
} else {
fend = addr + j + flag->size;
}
2014-09-10 02:04:38 +02:00
note[j] = r_str_prefix (strdup (flag->name), "/");
2015-09-14 12:35:38 +02:00
marks = true;
2014-09-08 01:33:50 +02:00
color_idx++;
color_idx %= R_CONS_PALETTE_LIST_SIZE;
2014-08-26 23:52:34 +02:00
current_flag = flag;
} else {
2014-08-26 23:52:34 +02:00
// Are we past the current flag?
if (current_flag && addr+j > (current_flag->offset + current_flag->size)){
2015-09-14 12:35:38 +02:00
setcolor = false;
2014-08-26 23:52:34 +02:00
current_flag = NULL;
}
// Turn colour off if we're at the end of the current flag
if (fend == UT64_MAX || fend <= addr + j)
2015-09-14 12:35:38 +02:00
setcolor = false;
}
if (setcolor && !hascolor) {
2015-09-14 12:35:38 +02:00
hascolor = true;
if (usecolor) {
2014-08-26 23:52:34 +02:00
if (current_flag && current_flag->color) {
char *ansicolor = r_cons_pal_parse (current_flag->color);
append (ebytes, ansicolor);
2014-08-26 23:52:34 +02:00
append (echars, ansicolor);
free (ansicolor);
2014-08-26 23:52:34 +02:00
} else { // Use "random" colours
2014-09-08 01:33:50 +02:00
append (ebytes, colors[color_idx]);
append (echars, colors[color_idx]);
}
} else {
if (html) {
append (ebytes, "[");
} else {
append (ebytes, Color_INVERT);
}
}
}
2014-08-26 23:52:34 +02:00
here = R_MIN ((i * nb_cols) + j, core->blocksize);
ch = buf[here];
if (core->print->ocur!=-1) {
low = R_MIN (core->print->cur, core->print->ocur);
max = R_MAX (core->print->cur, core->print->ocur);
} else {
low = max = core->print->cur;
}
if (core->print->cur_enabled) {
if (low==max) {
if (low == here) {
if (html) {
append (ebytes, "[");
append (echars, "[");
} else {
append (echars, Color_INVERT);
append (ebytes, Color_INVERT);
}
}
} else {
if (here >= low && here <max) {
if (html) {
append (ebytes, "[");
append (echars, "[");
} else {
append (ebytes, Color_INVERT);
append (echars, Color_INVERT);
}
}
}
}
sprintf (ebytes, "%02x", ch);
ebytes += strlen (ebytes);
sprintf (echars, "%c", IS_PRINTABLE (ch)?ch:'.');
echars++;
2014-08-26 23:52:34 +02:00
if (core->print->cur_enabled && max == here) {
if (!html) {
append (ebytes, Color_RESET);
append (echars, Color_RESET);
}
2015-09-14 12:35:38 +02:00
hascolor = false;
}
2014-08-24 05:35:48 +02:00
if (j < (nb_cols-1) && (j%2))
append (ebytes, " ");
2014-08-26 23:52:34 +02:00
if (fend != UT64_MAX && fend == addr+j+1) {
if (!html) {
append (ebytes, Color_RESET);
append (echars, Color_RESET);
}
fend = UT64_MAX;
2015-09-14 12:35:38 +02:00
hascolor = false;
}
2014-08-24 05:35:48 +02:00
}
if (!html) {
append (ebytes, Color_RESET);
append (echars, Color_RESET);
}
2014-08-26 23:52:34 +02:00
append (ebytes, (col==1)?"| ":(col==2)?" |":" ");
if (col==2) append (echars, "|");
if (marks) { // show comments and flags
int hasline = 0;
int out_sz = nb_cons_cols+20;
char* out = calloc (out_sz, sizeof(char));
2014-08-26 23:52:34 +02:00
memset (out, ' ', nb_cons_cols-1);
2014-08-24 05:35:48 +02:00
for (j=0; j<nb_cols; j++) {
if (note[j]) {
2014-09-10 02:04:38 +02:00
int off = (j*3) - (j/2) + 13;
int notej_len = strlen (note[j]);
int sz = R_MIN (notej_len, nb_cons_cols-off);
if (j%2) off--;
memcpy (out+off, note[j], sz);
if (sz < notej_len) {
out[off+sz-2] = '.';
out[off+sz-1] = '.';
}
hasline = (out[off] != ' ');
R_FREE (note[j]);
}
}
out[out_sz-1] = 0;
if (hasline) {
r_cons_strcat (addrpad);
r_cons_strcat (out);
r_cons_newline ();
}
2015-09-14 12:35:38 +02:00
marks = false;
2014-08-26 23:52:34 +02:00
free (out);
}
r_cons_strcat (bytes);
r_cons_strcat (chars);
r_cons_newline ();
2014-08-24 05:35:48 +02:00
addr += nb_cols;
}
2014-08-24 05:35:48 +02:00
free (note);
2014-08-26 23:52:34 +02:00
free (bytes);
free (chars);
}
R_API void r_core_print_examine(RCore *core, const char *str) {
2013-10-14 22:23:05 +02:00
char cmd[128], *p;
ut64 addr = core->offset;
int size = (core->anal->bits/4);
int count = atoi (str);
int i, n;
if (count<1) count = 1;
// skipsapces
while (*str>='0' && *str<='9') str++;
// "px/" alone isn't a full command.
if (!str[0]) return;
#if 0
Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).
#endif
switch (str[1]) {
case 'b': size = 1; break;
case 'h': size = 2; break;
case 'd': size = 4; break;
case 'w': size = 4; break;
case 'g': size = 8; break;
}
2013-10-14 22:23:05 +02:00
if ((p=strchr (str, ' ')))
addr = r_num_math (core->num, p+1);
switch (*str) {
case '?':
eprintf (
"Format is x/[num][format][size]\n"
2013-10-14 22:23:05 +02:00
"Num specifies the number of format elements to display\n"
"Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),\n"
" t(binary), f(float), a(address), i(instruction), c(char) and s(string),\n"
" T(OSType), A(floating point values in hex).\n"
2013-10-14 22:23:05 +02:00
"Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).\n"
);
break;
case 's':
r_core_cmdf (core, "psb %d @ 0x%"PFMT64x, count*size, addr);
break;
case 'o':
r_core_cmdf (core, "pxo %d @ 0x%"PFMT64x, count*size, addr);
break;
case 'f':
case 'A': // XXX (float in hex wtf)
n = 3;
snprintf (cmd, sizeof (cmd), "pxo %d @ 0x%"PFMT64x,
count*size, addr);
strcpy (cmd, "pf ");
for (i=0;i<count && n<sizeof (cmd);i++)
cmd[n++] = 'f';
cmd[n] = 0;
r_core_cmd0 (core, cmd);
break;
case 'x':
r_core_cmdf (core, "px %d @ 0x%"PFMT64x, count, addr);
break;
case 'a':
case 'd':
r_core_cmdf (core, "pxw %d @ 0x%"PFMT64x, count*size, addr);
break;
case 'i':
r_core_cmdf (core, "pid %d @ 0x%"PFMT64x, count, addr);
break;
}
}
2012-02-27 02:40:27 +01:00
static int printzoomcallback(void *user, int mode, ut64 addr, ut8 *bufz, ut64 size) {
RCore *core = (RCore *) user;
int j, ret = 0;
RListIter *iter;
RFlagItem *flag;
switch (mode) {
case 'p':
2016-10-27 15:31:46 +02:00
for (j = 0; j < size; j++) {
if (IS_PRINTABLE (bufz[j])) {
2012-02-27 02:40:27 +01:00
ret++;
2016-10-27 15:31:46 +02:00
}
}
2012-02-27 02:40:27 +01:00
break;
case 'f':
r_list_foreach (core->flags->flags, iter, flag)
if (flag->offset <= addr && addr < flag->offset+flag->size)
ret++;
break;
case 's':
j = r_flag_space_get (core->flags, "strings");
r_list_foreach (core->flags->flags, iter, flag) {
if (flag->space == j && ((addr <= flag->offset
&& flag->offset < addr+size)
|| (addr <= flag->offset+flag->size
&& flag->offset+flag->size < addr+size)))
ret++;
}
break;
2014-09-12 19:25:43 +02:00
case '0': // 0x00
2012-02-27 02:40:27 +01:00
for (j=0; j<size; j++)
if (bufz[j] == 0)
ret++;
break;
case 'F': // 0xFF
for (j=0; j<size; j++)
if (bufz[j] == 0xff)
ret++;
break;
case 'e': // entropy
ret = (ut8) (r_hash_entropy_fraction (bufz, size)*255);
break;
case 'h': // head
default:
ret = *bufz;
}
return ret;
}
R_API void r_core_print_cmp(RCore *core, ut64 from, ut64 to) {
long int delta = 0;
int col = core->cons->columns > 123;
ut8 *b = malloc (core->blocksize);
ut64 addr = core->offset;
memset (b, 0xff, core->blocksize);
delta = addr - from;
r_core_read_at (core, to + delta, b, core->blocksize);
2013-10-04 13:57:49 +02:00
r_print_hexdiff (core->print, core->offset, core->block,
to + delta, b, core->blocksize, col);
free (b);
}
static int pdi(RCore *core, int nb_opcodes, int nb_bytes, int fmt) {
2013-05-01 00:40:26 +02:00
int show_offset = r_config_get_i (core->config, "asm.offset");
int show_bytes = r_config_get_i (core->config, "asm.bytes");
int decode = r_config_get_i (core->config, "asm.decode");
2015-06-13 23:32:01 +02:00
int filter = r_config_get_i (core->config, "asm.filter");
2015-06-04 13:05:02 +02:00
int show_color = r_config_get_i (core->config, "scr.color");
bool asm_ucase = r_config_get_i (core->config, "asm.ucase");
2013-11-13 01:59:16 +01:00
int esil = r_config_get_i (core->config, "asm.esil");
int flags = r_config_get_i (core->config, "asm.flags");
int i=0, j, ret, err = 0;
ut64 old_offset = core->offset;
2013-05-01 00:40:26 +02:00
RAsmOp asmop;
2015-06-14 02:42:39 +02:00
#define PAL(x) (core->cons && core->cons->pal.x)? core->cons->pal.x
const char *color_reg = PAL(reg): Color_YELLOW;
const char *color_num = PAL(num): Color_CYAN;
if (fmt == 'e') {
show_bytes = 0;
decode = 1;
}
2016-12-13 13:06:22 +01:00
if (!nb_opcodes && !nb_bytes) {
return 0;
}
if (!nb_opcodes) {
nb_opcodes = 0xffff;
if (nb_bytes < 0) {
// Backward disasm `nb_bytes` bytes
nb_bytes = -nb_bytes;
core->offset -= nb_bytes;
r_core_read_at (core, core->offset, core->block, nb_bytes);
}
} else if (!nb_bytes) {
if (nb_opcodes < 0) {
ut64 start;
/* Backward disassembly of `ilen` opcodes
* - We compute the new starting offset
* - Read at the new offset */
nb_opcodes = -nb_opcodes;
if (r_core_prevop_addr (core, core->offset, nb_opcodes, &start)) {
// We have some anal_info.
nb_bytes = core->offset - start;
} else {
// anal ignorance.
r_core_asm_bwdis_len (core, &nb_bytes, &core->offset,
nb_opcodes);
}
if (nb_bytes > core->blocksize) {
r_core_block_size (core, nb_bytes);
}
r_core_read_at (core, core->offset, core->block, nb_bytes);
2015-08-14 13:17:48 +02:00
} else {
// workaround for the `for` loop below
nb_bytes = core->blocksize;
2015-08-14 13:17:48 +02:00
}
}
// XXX - is there a better way to reset a the analysis counter so that
// when code is disassembled, it can actually find the correct offsets
if (core->anal && core->anal->cur && core->anal->cur->reset_counter) {
core->anal->cur->reset_counter (core->anal, core->offset);
}
2015-10-21 00:26:32 +02:00
int len = (nb_opcodes + nb_bytes) * 5;
2015-10-21 01:51:34 +02:00
if (core->fixedblock) {
len = core->blocksize;
} else {
if (len > core->blocksize) {
r_core_block_size (core, len);
r_core_block_read (core);
2015-10-21 01:51:34 +02:00
}
}
r_cons_break_push (NULL, NULL);
2015-10-21 01:36:24 +02:00
#define isTheEnd (nb_opcodes? nb_bytes? (j<nb_opcodes && i<nb_bytes) : j<nb_opcodes: i<nb_bytes)
for (i = j = 0; isTheEnd; j++) {
RFlagItem *item;
if (r_cons_is_breaked ()) {
err = 1;
break;
}
2016-12-13 02:37:28 +01:00
RAnalMetaItem *meta = r_meta_find (core->anal, core->offset + i,
R_META_TYPE_ANY, R_META_WHERE_HERE);
2016-12-13 12:35:13 +01:00
if (meta && meta->size > 0) {
switch (meta->type) {
case R_META_TYPE_DATA:
r_cons_printf (".data: %s\n", meta->str);
2016-12-13 02:37:28 +01:00
i += meta->size;
continue;
2016-12-13 12:35:13 +01:00
case R_META_TYPE_STRING:
r_cons_printf (".string: %s\n", meta->str);
i += meta->size;
continue;
case R_META_TYPE_FORMAT:
r_cons_printf (".format : %s\n", meta->str);
i += meta->size;
continue;
case R_META_TYPE_MAGIC:
r_cons_printf (".magic : %s\n", meta->str);
i += meta->size;
continue;
case R_META_TYPE_RUN:
/* TODO */
break;
2016-12-13 02:37:28 +01:00
}
}
r_asm_set_pc (core->assembler, core->offset + i);
ret = r_asm_disassemble (core->assembler, &asmop, core->block + i,
core->blocksize - i);
2016-12-13 13:06:22 +01:00
if (fmt == 'C') {
char *comment = r_meta_get_string (core->anal, R_META_TYPE_COMMENT, core->offset + i);
if (comment) {
r_cons_printf ("0x%08"PFMT64x" %s\n", core->offset + i, comment);
free (comment);
}
i += ret;
continue;
}
if (flags) {
2015-08-13 19:42:29 +02:00
if (fmt != 'e') { // pie
item = r_flag_get_i (core->flags, core->offset + i);
2015-08-13 19:42:29 +02:00
if (item) {
if (show_offset) {
r_cons_printf ("0x%08"PFMT64x" ", core->offset + i);
}
2015-08-13 19:42:29 +02:00
r_cons_printf (" %s:\n", item->name);
}
} // do not show flags in pie
}
2015-06-04 13:05:02 +02:00
if (show_offset) {
2016-11-02 04:07:58 +01:00
const int show_offseg = (core->print->flags & R_PRINT_FLAGS_SEGOFF) != 0;
const int show_offdec = (core->print->flags & R_PRINT_FLAGS_ADDRDEC) != 0;
2015-06-04 13:05:02 +02:00
ut64 at = core->offset + i;
2016-11-01 23:30:36 +01:00
r_print_offset (core->print, at, 0, show_offseg, show_offdec, 0, NULL);
2015-06-04 13:05:02 +02:00
}
2015-10-21 00:26:32 +02:00
// r_cons_printf ("0x%08"PFMT64x" ", core->offset+i);
if (ret < 1) {
err = 1;
ret = asmop.size;
if (ret < 1) ret = 1;
if (show_bytes) {
r_cons_printf ("%14s%02x ", "", core->block[i]);
}
r_cons_println ("invalid"); //???");
2013-05-01 00:40:26 +02:00
} else {
if (show_bytes) {
2013-05-01 00:40:26 +02:00
r_cons_printf ("%16s ", asmop.buf_hex);
}
ret = asmop.size;
2013-11-13 01:59:16 +01:00
if (decode || esil) {
2013-05-01 00:40:26 +02:00
RAnalOp analop = {0};
2014-04-28 11:37:48 +02:00
char *tmpopstr, *opstr = NULL;
2013-05-01 00:40:26 +02:00
r_anal_op (core->anal, &analop, core->offset+i,
core->block+i, core->blocksize-i);
2013-05-01 00:40:26 +02:00
tmpopstr = r_anal_op_to_string (core->anal, &analop);
if (fmt == 'e') { // pie
2015-08-13 19:42:29 +02:00
char *esil = (R_STRBUF_SAFEGET (&analop.esil));
r_cons_println (esil);
} else {
if (decode) {
opstr = (tmpopstr)? tmpopstr: (asmop.buf_asm);
} else if (esil) {
opstr = (R_STRBUF_SAFEGET (&analop.esil));
}
r_cons_println (opstr);
}
2015-06-04 13:05:02 +02:00
} else {
2015-06-13 23:32:01 +02:00
if (filter) {
char opstr[128] = {0};
if (asm_ucase) {
r_str_case (asmop.buf_asm, 1);
}
2015-06-14 02:42:39 +02:00
if (show_color) {
RAnalOp aop = {0};
char *asm_str = r_print_colorize_opcode (asmop.buf_asm, color_reg, color_num);
r_anal_op (core->anal, &aop, core->offset+i,
core->block+i, core->blocksize-i);
r_parse_filter (core->parser, core->flags,
asm_str, opstr, sizeof (opstr)-1, core->print->big_endian);
2015-06-14 02:42:39 +02:00
r_cons_printf ("%s%s"Color_RESET"\n", r_print_color_op_type (core->print, aop.type), opstr);
} else {
r_parse_filter (core->parser, core->flags,
asmop.buf_asm, opstr, sizeof (opstr)-1, core->print->big_endian);
r_cons_println (opstr);
2015-06-14 02:42:39 +02:00
}
2015-06-13 23:32:01 +02:00
} else {
if (show_color) {
RAnalOp aop;
r_anal_op (core->anal, &aop, core->offset+i,
2015-06-04 13:05:02 +02:00
core->block+i, core->blocksize-i);
2015-10-05 14:43:17 +02:00
r_cons_printf ("%s%s"Color_RESET"\n",
2015-06-04 13:05:02 +02:00
r_print_color_op_type (core->print, aop.type),
2016-09-09 19:28:47 +02:00
asmop.buf_asm);
2015-06-13 23:32:01 +02:00
} else {
r_cons_println (asmop.buf_asm);
2015-06-13 23:32:01 +02:00
}
2015-06-04 13:05:02 +02:00
}
}
2013-05-01 00:40:26 +02:00
}
2015-06-04 13:05:02 +02:00
i += ret;
2015-10-21 00:26:32 +02:00
#if 0
if ((nb_bytes && (nb_bytes <= i)) || (i >= core->blocksize))
break;
2015-10-21 00:26:32 +02:00
#endif
2013-05-01 00:40:26 +02:00
}
r_cons_break_pop ();
core->offset = old_offset;
2013-05-01 00:40:26 +02:00
return err;
}
2014-08-06 22:55:30 +02:00
static void cmd_print_pwn(const RCore* core) {
2015-06-28 01:42:21 +02:00
r_cons_printf ("easter egg license has expired\n");
2014-08-06 22:55:30 +02:00
}
static int cmd_print_pxA(RCore *core, int len, const char *data) {
RConsPalette *pal = &core->cons->pal;
2015-09-14 12:35:38 +02:00
int show_offset = true;
2015-03-21 00:27:49 +01:00
int cols = r_config_get_i (core->config, "hex.cols");
int show_color = r_config_get_i (core->config, "scr.color");
int onechar = r_config_get_i (core->config, "hex.onechar");
2015-09-14 12:35:38 +02:00
int bgcolor_in_heap = false;
bool show_cursor = core->print->cur_enabled;
2015-04-18 23:11:47 +03:00
char buf[2];
char *bgcolor, *fgcolor, *text;
ut64 i, c, oi;
2015-03-21 00:27:49 +01:00
RAnalOp op;
2015-04-11 01:29:28 +03:00
if (len < 0 || len > core->blocksize) {
2015-03-21 00:27:49 +01:00
eprintf ("Invalid length\n");
return 0;
}
if (onechar) {
cols *= 4;
} else {
cols *= 2;
}
if (show_offset) {
char offstr[128];
snprintf (offstr, sizeof(offstr),
"0x%08"PFMT64x" ", core->offset);
if (strlen (offstr)>12)
cols -= ((strlen(offstr)-12)*2);
}
2015-03-21 00:27:49 +01:00
for (oi = i = c = 0; i< len; c++) {
2015-04-11 01:29:28 +03:00
if (i && (cols != 0) && !(c % cols)) {
2015-09-14 12:35:38 +02:00
show_offset = true;
2015-03-21 00:27:49 +01:00
r_cons_printf (" %d\n", i-oi);
oi = i;
}
if (show_offset) {
r_cons_printf ("0x%08"PFMT64x" ", core->offset+i);
2015-09-14 12:35:38 +02:00
show_offset = false;
2015-03-21 00:27:49 +01:00
}
if (bgcolor_in_heap) {
free (bgcolor);
2015-09-14 12:35:38 +02:00
bgcolor_in_heap = false;
}
bgcolor = Color_BGBLACK;
fgcolor = Color_WHITE;
text = NULL;
if (!r_anal_op (core->anal, &op, core->offset+i, core->block+i, len-i)) {
2015-03-21 00:27:49 +01:00
op.type = 0;
bgcolor = Color_BGRED;
op.size = 1;
}
switch (op.type) {
case R_ANAL_OP_TYPE_LEA:
2015-03-21 00:27:49 +01:00
case R_ANAL_OP_TYPE_MOV:
2015-03-24 00:08:48 +01:00
case R_ANAL_OP_TYPE_CAST:
case R_ANAL_OP_TYPE_LENGTH:
2015-03-21 00:27:49 +01:00
case R_ANAL_OP_TYPE_CMOV:
text = "mv";
bgcolor = pal->mov;
fgcolor = Color_YELLOW;
2015-03-21 00:27:49 +01:00
break;
case R_ANAL_OP_TYPE_PUSH:
case R_ANAL_OP_TYPE_UPUSH:
bgcolor = pal->push;
fgcolor = Color_WHITE;
text = "->";
2015-03-21 00:27:49 +01:00
break;
case R_ANAL_OP_TYPE_IO:
bgcolor = pal->swi;
fgcolor = Color_WHITE;
text = "io";
break;
2015-03-21 00:27:49 +01:00
case R_ANAL_OP_TYPE_TRAP:
case R_ANAL_OP_TYPE_SWI:
2015-03-24 00:08:48 +01:00
case R_ANAL_OP_TYPE_NEW:
//bgcolor = Color_BGRED;
bgcolor = pal->trap; //r_cons_swap_ground (pal->trap);
fgcolor = Color_WHITE;
text = "$$";
2015-03-21 00:27:49 +01:00
break;
case R_ANAL_OP_TYPE_POP:
text = "<-";
bgcolor = r_cons_swap_ground (pal->pop);
2015-09-14 12:35:38 +02:00
bgcolor_in_heap = true;
fgcolor = Color_WHITE;
2015-03-21 00:27:49 +01:00
break;
case R_ANAL_OP_TYPE_NOP:
fgcolor = Color_WHITE;
bgcolor = r_cons_swap_ground (pal->nop);
2015-09-14 12:35:38 +02:00
bgcolor_in_heap = true;
text = "..";
break;
case R_ANAL_OP_TYPE_MUL:
fgcolor = Color_BLACK;
bgcolor = r_cons_swap_ground (pal->math);
2015-09-14 12:35:38 +02:00
bgcolor_in_heap = true;
text = "_*";
break;
case R_ANAL_OP_TYPE_DIV:
bgcolor = r_cons_swap_ground (pal->math);
2015-09-14 12:35:38 +02:00
bgcolor_in_heap = true;
fgcolor = Color_BLACK;
text = "_/";
break;
case R_ANAL_OP_TYPE_AND:
bgcolor = r_cons_swap_ground (pal->bin);
2015-09-14 12:35:38 +02:00
bgcolor_in_heap = true;
fgcolor = Color_BLACK;
text = "_&";
break;
case R_ANAL_OP_TYPE_XOR:
bgcolor = r_cons_swap_ground (pal->bin);
2015-09-14 12:35:38 +02:00
bgcolor_in_heap = true;
fgcolor = Color_BLACK;
text = "_^";
break;
case R_ANAL_OP_TYPE_OR:
bgcolor = r_cons_swap_ground (pal->bin);
2015-09-14 12:35:38 +02:00
bgcolor_in_heap = true;
fgcolor = Color_BLACK;
text = "_|";
break;
case R_ANAL_OP_TYPE_SHR:
bgcolor = r_cons_swap_ground (pal->bin);
2015-09-14 12:35:38 +02:00
bgcolor_in_heap = true;
fgcolor = Color_BLACK;
text = ">>";
break;
case R_ANAL_OP_TYPE_SHL:
bgcolor = r_cons_swap_ground (pal->bin);
2015-09-14 12:35:38 +02:00
bgcolor_in_heap = true;
fgcolor = Color_BLACK;
text = "<<";
break;
case R_ANAL_OP_TYPE_SUB:
bgcolor = r_cons_swap_ground (pal->math);
2015-09-14 12:35:38 +02:00
bgcolor_in_heap = true;
fgcolor = Color_WHITE;
text = "--";
break;
case R_ANAL_OP_TYPE_ADD:
bgcolor = r_cons_swap_ground (pal->math);
2015-09-14 12:35:38 +02:00
bgcolor_in_heap = true;
fgcolor = Color_WHITE;
text = "++";
2015-03-21 00:27:49 +01:00
break;
case R_ANAL_OP_TYPE_JMP:
case R_ANAL_OP_TYPE_UJMP:
case R_ANAL_OP_TYPE_IJMP:
case R_ANAL_OP_TYPE_RJMP:
case R_ANAL_OP_TYPE_IRJMP:
case R_ANAL_OP_TYPE_MJMP:
bgcolor = r_cons_swap_ground (pal->jmp);
2015-09-14 12:35:38 +02:00
bgcolor_in_heap = true;
fgcolor = Color_BLACK;
text = "_J";
break;
2015-03-21 00:27:49 +01:00
case R_ANAL_OP_TYPE_CJMP:
case R_ANAL_OP_TYPE_UCJMP:
bgcolor = r_cons_swap_ground (pal->cjmp);
2015-09-14 12:35:38 +02:00
bgcolor_in_heap = true;
fgcolor = Color_BLACK;
text = "cJ";
break;
2015-03-21 00:27:49 +01:00
case R_ANAL_OP_TYPE_CALL:
case R_ANAL_OP_TYPE_UCALL:
case R_ANAL_OP_TYPE_ICALL:
case R_ANAL_OP_TYPE_RCALL:
case R_ANAL_OP_TYPE_IRCALL:
2015-03-21 00:27:49 +01:00
case R_ANAL_OP_TYPE_UCCALL:
bgcolor = r_cons_swap_ground (pal->call);
2015-09-14 12:35:38 +02:00
bgcolor_in_heap = true;
fgcolor = Color_WHITE;
text = "_C";
break;
case R_ANAL_OP_TYPE_ACMP:
case R_ANAL_OP_TYPE_CMP:
bgcolor = r_cons_swap_ground (pal->cmp);
2015-09-14 12:35:38 +02:00
bgcolor_in_heap = true;
fgcolor = Color_BLACK;
text = "==";
2015-03-21 00:27:49 +01:00
break;
case R_ANAL_OP_TYPE_RET:
bgcolor = r_cons_swap_ground (pal->ret);
2015-09-14 12:35:38 +02:00
bgcolor_in_heap = true;
2015-03-21 00:27:49 +01:00
fgcolor = Color_WHITE;
text = "_R";
2015-03-21 00:27:49 +01:00
break;
case -1:
case R_ANAL_OP_TYPE_ILL:
case R_ANAL_OP_TYPE_UNK:
bgcolor = r_cons_swap_ground (pal->invalid);
2015-09-14 12:35:38 +02:00
bgcolor_in_heap = true;
2015-03-21 00:27:49 +01:00
fgcolor = Color_WHITE;
text = "XX";
2015-03-21 00:27:49 +01:00
break;
#if 0
default:
color = Color_BGCYAN;
fgcolor = Color_BLACK;
break;
#endif
}
int opsz = R_MAX (op.size, 1);
if (show_cursor) {
2015-04-11 13:46:33 +03:00
if (core->print->cur >=i && core->print->cur < i+opsz)
r_cons_invert (1, 1);
}
if (onechar) {
if (text) {
if (text[0] == '_' || text[0] == '.')
buf[0] = text[1];
else buf[0] = text[0];
} else buf[0] = '.';
buf[1] = 0;
text = buf;
}
if (show_color) {
if (!text) text = " ";
2015-04-11 13:46:33 +03:00
r_cons_printf ("%s%s%s\x1b[0m", bgcolor, fgcolor, text);
} else {
if (text) {
r_cons_print (text);
} else {
r_cons_print (" ");
}
}
if (show_cursor) {
2015-04-11 13:46:33 +03:00
if (core->print->cur >=i && core->print->cur < i+opsz)
r_cons_invert (0, 1);
}
i += opsz;
2015-03-21 00:27:49 +01:00
}
r_cons_printf (" %d\n", i-oi);
2015-04-11 13:46:33 +03:00
if (bgcolor_in_heap) free (bgcolor);
2015-09-14 12:35:38 +02:00
return true;
2015-03-21 00:27:49 +01:00
}
2014-08-06 22:55:30 +02:00
2015-03-23 01:09:35 +01:00
static void printraw (RCore *core, int len, int mode) {
int obsz = core->blocksize;
int restore_obsz = 0;
if (len != obsz) {
2016-09-23 13:23:32 +02:00
if (!r_core_block_size (core, len)) {
2015-03-23 01:09:35 +01:00
len = core->blocksize;
2016-09-23 13:23:32 +02:00
} else {
restore_obsz = 1;
}
2015-03-23 01:09:35 +01:00
}
r_print_raw (core->print, core->offset, core->block, len, mode);
if (restore_obsz) {
(void)r_core_block_size (core, obsz);
}
core->cons->newline = true;
2015-03-23 01:09:35 +01:00
}
2016-09-23 13:23:32 +02:00
static void _handle_call(RCore *core, char * line, char **str) {
if (!core || !core->assembler || !core->assembler->cur) {
*str = NULL;
return;
}
if (strstr (core->assembler->cur->arch, "x86")) {
*str = strstr (line , "call ");
} else if (strstr (core->assembler->cur->arch, "arm")) {
*str = strstr (line, " b ");
if (*str && strstr (*str, " 0x")) {
/*
* avoid treating branches to
* non-symbols as calls
*/
*str = NULL;
}
2016-09-23 13:23:32 +02:00
if (!*str) {
*str = strstr (line, "bl ");
}
if (!*str) {
*str = strstr (line, "bx ");
}
}
}
// TODO: this is just a PoC, the disasm loop should be rewritten
// TODO: this is based on string matching, it should be written upon RAnalOp to know
2016-09-23 13:23:32 +02:00
// when we have a call and such
2016-03-23 01:13:16 +01:00
static void disasm_strings(RCore *core, const char *input, RAnalFunction *fcn) {
#define MYPAL(x) (core->cons && core->cons->pal.x)? core->cons->pal.x: ""
const char *linecolor = NULL;
2015-12-31 16:28:16 +01:00
char *ox, *qo, *string = NULL;
2016-09-23 13:23:32 +02:00
char *line, *s, *str, *string2 = NULL;
int i, count, use_color = r_config_get_i (core->config, "scr.color");
2016-10-03 20:13:45 +02:00
bool is_free_pending = false;
r_config_set_i (core->config, "scr.color", 0);
if (!strncmp (input, "dsf", 3)) {
RAnalFunction *fcn = r_anal_get_fcn_in (core->anal, core->offset, R_ANAL_FCN_TYPE_NULL);
if (fcn) {
line = s = r_core_cmd_str (core, "pdr");
} else {
eprintf ("Cannot find function.\n");
r_config_set_i (core->config, "scr.color", use_color);
return;
}
2016-03-23 01:18:27 +01:00
} else if (!strncmp (input, "ds ", 3)) {
char *cmd = r_str_newf ("pD %s", input+3);
line = s = r_core_cmd_strf (core, cmd);
free (cmd);
} else {
line = s = r_core_cmd_str (core, "pd");
}
r_config_set_i (core->config, "scr.color", use_color);
count = r_str_split (s, '\n');
2016-09-23 13:23:32 +02:00
if (!line || !*line || count < 1) {
2016-10-26 23:46:41 +02:00
free (s);
return;
}
2016-08-31 03:17:51 +02:00
for (i = 0; i < count; i++) {
ut64 addr = UT64_MAX;
ox = strstr (line, "0x");
qo = strstr (line, "\"");
2015-12-31 16:28:16 +01:00
R_FREE (string);
if (ox) {
addr = r_num_get (NULL, ox);
}
if (qo) {
2016-09-23 13:23:32 +02:00
char *qoe = strchr (qo + 1, '"');
if (qoe) {
int len = qoe - qo - 1;
2016-09-23 13:23:32 +02:00
if (len > 2) {
string = r_str_ndup (qo, len+2);
}
2015-12-31 16:28:16 +01:00
linecolor = MYPAL (comment);
}
}
ox = strstr (line, "; 0x");
2016-09-23 13:23:32 +02:00
if (!ox) {
ox = strstr (line, "@ 0x");
}
if (ox) {
2016-09-23 13:23:32 +02:00
char *qoe = strchr (ox + 3, ' ');
if (!qoe) {
qoe = strchr (ox + 3, '\x1b');
}
int len = qoe? qoe - ox: strlen (ox + 3);
string2 = r_str_ndup (ox + 2, len - 1);
if (r_num_get (NULL, string2) < 0x100) {
R_FREE (string2);
}
}
2016-12-04 19:54:01 +01:00
//XXX leak
str = strstr (line, " str.");
if (str) {
char *qoe = NULL;
2016-09-23 13:23:32 +02:00
if (!qoe) {
qoe = strchr (str + 1, '\x1b');
}
if (!qoe) {
qoe = strchr (str + 1, ';');
}
if (!qoe) {
qoe = strchr (str + 1, ' ');
}
if (qoe) {
2016-09-23 13:23:32 +02:00
string2 = r_str_ndup (str + 1, qoe - str - 1);
} else {
2016-09-23 13:23:32 +02:00
string2 = strdup (str + 1);
}
if (!string && string2) {
string = string2;
string2 = NULL;
}
}
2016-08-31 03:17:51 +02:00
if (string2) {
R_FREE (string2);
}
2016-09-23 13:23:32 +02:00
_handle_call (core, line, &str);
2016-08-31 03:17:51 +02:00
if (!str) {
2016-09-23 13:23:32 +02:00
str = strstr (line, "sym.");
if (!str) {
str = strstr (line, "fcn.");
}
2016-08-31 03:17:51 +02:00
}
2016-09-24 00:30:13 +02:00
if (str) {
char *qoe = strstr (str, ";");
if (qoe) {
2016-09-29 12:55:47 +02:00
//XXX str leaks
2016-09-24 00:30:13 +02:00
str = r_str_ndup (str, qoe - str);
2016-10-03 20:13:45 +02:00
is_free_pending = true;
2016-09-24 00:30:13 +02:00
}
}
if (str) {
2016-09-23 13:23:32 +02:00
string2 = strdup (str);
linecolor = MYPAL(call);
}
if (!string && string2) {
string = string2;
string2 = NULL;
}
2016-05-17 23:41:49 +02:00
if (strstr (line, "XREF")) {
addr = UT64_MAX;
}
2016-03-23 01:13:16 +01:00
if (addr != UT64_MAX) {
const char *str = NULL;
2016-03-23 01:13:16 +01:00
if (fcn) {
bool label = false;
/* show labels, basic blocks and (conditional) branches */
RAnalBlock *bb;
RListIter *iter;
r_list_foreach (fcn->bbs, iter, bb) {
if (addr == bb->jump) {
r_cons_printf ("%s0x%08"PFMT64x":\n", use_color? Color_YELLOW:"", addr);
label = true;
break;
}
}
2016-03-23 01:13:16 +01:00
if (!label && strstr (line, "->")) {
r_cons_printf ("%s0x%08"PFMT64x":\n", use_color? Color_YELLOW:"", addr);
}
if (strstr (line, "=<")) {
r_list_foreach (fcn->bbs, iter, bb) {
if (addr >= bb->addr && addr < bb->addr + bb->size) {
const char *op;
if (use_color) {
op = (bb->fail == UT64_MAX)? Color_GREEN"jmp": "cjmp";
} else {
op = (bb->fail == UT64_MAX)? "jmp": "cjmp";
}
r_cons_printf ("%s0x%08"PFMT64x" %s 0x%08"PFMT64x"%s\n",
use_color? MYPAL(offset):"", addr, op,
bb->jump, use_color?Color_RESET:"");
break;
}
}
}
}
2016-03-23 01:13:16 +01:00
if (string && *string) {
2016-09-23 13:23:32 +02:00
if (string && !strncmp (string, "0x", 2)) {
str = string;
}
if (string2 && !strncmp (string2, "0x", 2)) {
str = string2;
}
2016-03-23 01:13:16 +01:00
ut64 ptr = r_num_math (NULL, str);
RFlagItem *flag = NULL;
if (str) {
flag = r_flag_get_i2 (core->flags, ptr);
}
if (!flag) {
if (string && !strncmp (string, "0x", 2)) {
R_FREE (string);
}
if (string2 && !strncmp (string2, "0x", 2)) {
R_FREE (string2);
}
}
if (string) {
2016-10-06 15:25:43 +02:00
string = r_str_chop (string);
string2 = r_str_chop (string2);
2016-03-23 01:13:16 +01:00
if (use_color) {
r_cons_printf ("%s0x%08"PFMT64x"%s %s%s%s%s%s%s%s\n",
MYPAL(offset), addr, Color_RESET,
linecolor? linecolor: "",
2016-09-23 13:23:32 +02:00
string2? string2: "", string2?" ":"", string,
2016-03-23 01:13:16 +01:00
flag?" ":"", flag?flag->name:"", Color_RESET);
} else {
r_cons_printf ("0x%08"PFMT64x" %s%s%s%s%s\n", addr,
2016-09-23 13:23:32 +02:00
string2? string2 :"", string2? " ":"", string,
2016-03-23 01:13:16 +01:00
flag?" ":"", flag?flag->name:"");
}
}
}
}
line = line + strlen (line) + 1;
}
//r_cons_printf ("%s", s);
2015-12-31 16:28:16 +01:00
free (string2);
free (string);
free (s);
2016-10-03 20:13:45 +02:00
if (is_free_pending) {
free (str);
}
}
2016-05-16 01:18:18 +02:00
static void algolist(int mode) {
int i;
for (i = 0; i < R_HASH_NBITS ; i++) {
ut64 bits = 1ULL << i;
2016-05-16 01:18:18 +02:00
const char *name = r_hash_name (bits);
if (name && *name) {
2016-11-07 02:33:09 +01:00
if (mode) {
r_cons_println (name);
} else {
r_cons_printf ("%s ", name);
}
}
2016-05-16 01:18:18 +02:00
}
if (!mode) r_cons_newline ();
}
static bool cmd_print_ph(RCore *core, const char *input) {
char algo[128];
ut32 osize = 0, len = core->blocksize;
const char *ptr;
int pos = 0, handled_cmd = false;
if (!*input || *input == '?') {
algolist (1);
return true;
}
if (*input == '=') {
algolist (0);
return true;
}
input = r_str_chop_ro (input);
ptr = strchr (input, ' ');
sscanf (input, "%31s", algo);
2016-11-07 02:33:09 +01:00
if (ptr && ptr[1]) { // && r_num_is_valid_input (core->num, ptr + 1)) {
2016-05-16 01:18:18 +02:00
int nlen = r_num_math (core->num, ptr + 1);
2016-11-07 02:33:09 +01:00
if (nlen > 0) {
len = nlen;
}
2016-05-16 01:18:18 +02:00
osize = core->blocksize;
if (nlen > core->blocksize) {
r_core_block_size (core, nlen);
if (nlen != core->blocksize) {
eprintf ("Invalid block size\n");
r_core_block_size (core, osize);
return false;
}
2016-11-07 03:36:49 +01:00
r_core_block_read (core);
2016-05-16 01:18:18 +02:00
}
2016-11-07 03:36:49 +01:00
} else if (!ptr || !*(ptr + 1)) {
2016-05-16 01:18:18 +02:00
osize = len;
}
/* TODO: Simplify this spaguetti monster */
while (osize > 0 && hash_handlers[pos].name) {
if (!r_str_ccmp (input, hash_handlers[pos].name, ' ')) {
hash_handlers[pos].handler (core->block, len);
handled_cmd = true;
break;
}
pos++;
}
if (osize) {
r_core_block_size (core, osize);
}
2016-05-23 21:49:02 +02:00
return handled_cmd;
2016-05-16 01:18:18 +02:00
}
static void cmd_print_pv(RCore *core, const char *input) {
const char *stack[] = { "ret", "arg0", "arg1", "arg2", "arg3", "arg4", NULL };
int i, n = core->assembler->bits / 8;
int type = 'v';
2016-04-26 15:04:05 +02:00
bool fixed_size = true;
2016-07-22 00:48:52 +02:00
const char* help_msg[] = {
"Usage: pv[j][1,2,4,8,z]", "", "",
"pv", "", "print bytes based on asm.bits",
"pv1", "", "print 1 byte in memory",
"pv2", "", "print 2 bytes in memory",
"pv4", "", "print 4 bytes in memory",
"pv8", "", "print 8 bytes in memory",
"pvz", "", "print value as string (alias for ps)",
NULL};
2016-04-26 14:35:51 +02:00
switch (input[0]) {
case '1':
n = 1;
input++;
break;
case '2':
n = 2;
input++;
break;
case '4':
n = 4;
input++;
break;
case '8':
n = 8;
input++;
break;
2016-04-26 15:04:05 +02:00
default:
fixed_size = false;
break;
2016-04-26 14:35:51 +02:00
}
// variables can be
switch (input[0]) {
2016-07-22 00:48:52 +02:00
case 'z': // "pvz"
type = 'z';
if (input[1]) {
input++;
} else {
r_core_cmdf (core, "ps");
break;
}
2016-07-22 00:48:52 +02:00
/* fallthrough */
case ' ':
2016-03-24 17:28:41 +01:00
for (i = 0; stack[i]; i++) {
2016-04-26 14:35:51 +02:00
if (!strcmp (input + 1, stack[i])) {
if (type == 'z') {
r_core_cmdf (core, "ps @ [`drn sp`+%d]", n * i);
} else {
r_core_cmdf (core, "?v [`drn sp`+%d]", n * i);
}
}
}
break;
2016-03-19 02:26:28 +01:00
case 'j':
2016-03-24 17:28:41 +01:00
{
2016-05-24 03:09:24 +02:00
char *str = r_str_chop (r_core_cmd_str (core, "ps @ [$$]"));
char *p = str;
if (p) {
while (*p) {
if (*p == '\\' && p[1] == 'x') {
memmove (p, p + 4, strlen (p + 4) + 1);
}
}
}
//r_num_get is gonna use a dangling pointer since the internal
//token that RNum holds ([$$]) has been already freed by r_core_cmd_str
//r_num_math reload a new token so the dangling pointer is gone
2016-03-19 02:26:28 +01:00
r_cons_printf ("{\"value\":%"PFMT64d",\"string\":\"%s\"}\n",
r_num_math (core->num, "[$$]"),
2016-03-24 17:28:41 +01:00
str
2016-05-24 03:09:24 +02:00
);
2016-03-24 17:28:41 +01:00
free (str);
}
2016-03-19 02:26:28 +01:00
break;
case '?':
2016-07-22 00:48:52 +02:00
r_core_cmd_help (core, help_msg);
break;
default:
2016-04-26 14:35:51 +02:00
{
ut64 v;
2016-04-26 15:04:05 +02:00
if (!fixed_size) n = 0;
2016-04-26 14:35:51 +02:00
switch (n) {
case 1:
v = r_read_ble8 (core->block);
r_cons_printf ("0x%02" PFMT64x "\n", v);
break;
case 2:
v = r_read_ble16 (core->block, core->print->big_endian);
r_cons_printf ("0x%04" PFMT64x "\n", v);
break;
case 4:
v = r_read_ble32 (core->block, core->print->big_endian);
r_cons_printf ("0x%08" PFMT64x "\n", v);
break;
case 8:
v = r_read_ble64 (core->block, core->print->big_endian);
r_cons_printf ("0x%016" PFMT64x "\n", v);
break;
default:
2016-07-22 00:48:52 +02:00
v = r_read_ble64 (core->block, core->print->big_endian);
switch (core->assembler->bits / 8) {
2016-07-22 00:48:52 +02:00
case 1: r_cons_printf ("0x%02" PFMT64x "\n", v & UT8_MAX); break;
case 2: r_cons_printf ("0x%04" PFMT64x "\n", v & UT16_MAX); break;
case 4: r_cons_printf ("0x%08" PFMT64x "\n", v & UT32_MAX); break;
case 8: r_cons_printf ("0x%016" PFMT64x "\n", v & UT64_MAX); break;
default: break;
}
break;
2016-04-26 14:35:51 +02:00
}
}
//r_core_cmd0 (core, "?v [$$]");
break;
}
}
2015-03-23 01:09:35 +01:00
static void cmd_print_bars(RCore *core, const char *input) {
bool print_bars = false;
ut8 *ptr = core->block;
// p=e [nblocks] [totalsize] [skip]
int nblocks = -1;
int totalsize = -1;
int skipblocks = -1;
int blocksize = -1;
int mode = 'b'; // e, p, b, ...
int submode = 0; // q, j, ...
if (input[0]) {
char *spc = strchr (input, ' ');
if (spc) {
nblocks = r_num_get (core->num, spc + 1);
2016-04-01 00:02:56 +02:00
if (nblocks < 1) {
nblocks = core->blocksize;
2016-04-01 00:02:56 +02:00
return;
}
spc = strchr (spc + 1, ' ');
if (spc) {
totalsize = r_num_get (core->num, spc + 1);
spc = strchr (spc + 1, ' ');
if (spc) {
skipblocks = r_num_get (core->num, spc + 1);
}
}
}
mode = input[1];
if (mode && mode != ' ' && input[2]) {
submode = input[2];
}
}
2016-03-13 02:31:13 +01:00
if (skipblocks < 0) {
skipblocks = 0;
}
if (totalsize == UT64_MAX) {
if (core->file && core->io) {
totalsize = r_io_desc_size (core->io, core->file->desc);
if ((st64) totalsize < 1) {
totalsize = -1;
}
}
if (totalsize == UT64_MAX) {
eprintf ("Cannot determine file size\n");
return;
}
}
blocksize = (blocksize > 0) ? (totalsize / blocksize) : (core->blocksize);
if (blocksize < 1) {
eprintf ("Invalid block size: %d\n", blocksize);
return;
}
if (nblocks < 1) {
nblocks = totalsize / blocksize;
} else {
blocksize = totalsize / nblocks;
}
switch (mode) {
case '?': { // bars
const char* help_msg[] = {
2017-01-11 00:22:18 +01:00
"Usage:", "p=[bep?][qj] [nblocks] ([len]) ([offset]) ", "show entropy/printable chars/chars bars",
"p=", "", "print bytes of current block in bars",
"p=", "b", "same as above",
"p=", "d", "print different bytes from block",
"p=", "e", "print entropy for each filesize/blocksize",
"p=", "p", "print number of printable bytes for each filesize/blocksize",
2016-09-06 05:17:44 +02:00
"p=", "0", "print number of 0x00 bytes for each filesize/blocksize",
"p=", "F", "print number of 0xFF bytes for each filesize/blocksize",
NULL};
r_core_cmd_help (core, help_msg);
}
break;
case 'd':
2017-01-11 00:45:35 +01:00
if (input[1]) {
2017-01-11 00:22:18 +01:00
ut64 bufsz = r_num_math (core->num, input + 3);
ut64 curbsz = core->blocksize;
2017-01-11 00:45:35 +01:00
if (bufsz < 1) {
bufsz = curbsz;
}
2017-01-11 00:22:18 +01:00
if (bufsz > core->blocksize) {
r_core_block_size (core, bufsz);
2017-01-11 00:45:35 +01:00
r_core_block_read (core);
2017-01-11 00:22:18 +01:00
}
cmd_print_eq_dict (core, core->block, bufsz);
if (bufsz != curbsz) {
r_core_block_size (core, curbsz);
}
2017-01-11 00:45:35 +01:00
} else {
cmd_print_eq_dict (core, core->block, core->blocksize);
}
2017-01-11 00:22:18 +01:00
break;
2016-04-01 00:02:56 +02:00
case 'e': // "p=e" entropy
{
ut8 *p;
int i = 0;
2016-09-19 00:47:38 +02:00
ptr = calloc (1, nblocks);
if (!ptr) {
eprintf ("Error: failed to malloc memory");
goto beach;
}
p = malloc (blocksize);
if (!p) {
R_FREE (ptr);
eprintf ("Error: failed to malloc memory");
goto beach;
}
for (i = 0; i < nblocks; i++) {
ut64 off = core->offset + (i + skipblocks) * blocksize;
r_core_read_at (core, off, p, blocksize);
ptr[i] = (ut8) (256 * r_hash_entropy_fraction (p, blocksize));
}
free (p);
print_bars = true;
}
break;
2016-09-06 05:17:44 +02:00
case '0': // 0x00 bytes
case 'F': // 0xff bytes
case 'p': // printable chars
{
ut8 *p;
int i, j, k;
2016-09-19 00:47:38 +02:00
ptr = calloc (1, nblocks);
if (!ptr) {
eprintf ("Error: failed to malloc memory");
goto beach;
}
2016-09-19 00:47:38 +02:00
p = calloc (1, blocksize);
if (!p) {
R_FREE (ptr);
eprintf ("Error: failed to malloc memory");
goto beach;
}
for (i = 0; i < nblocks; i++) {
2016-03-13 02:31:13 +01:00
ut64 off = (i + skipblocks) * blocksize;
r_core_read_at (core, off, p, blocksize);
for (j = k = 0; j < blocksize; j++) {
2016-09-06 05:17:44 +02:00
switch (mode) {
case '0':
if (!p[j]) k++;
break;
case 'f':
if (p[j] == 0xff) k++;
break;
case 'p':
if (IS_PRINTABLE (p[j])) k++;
break;
}
}
ptr[i] = 256 * k / blocksize;
}
free (p);
print_bars = true;
}
break;
case 'b': // bytes
case '\0':
2016-09-19 00:47:38 +02:00
ptr = calloc (1, nblocks);
r_core_read_at (core, core->offset, ptr, nblocks);
2016-03-13 02:31:13 +01:00
// TODO: support print_bars
r_print_fill (core->print, ptr, nblocks, core->offset, blocksize);
2016-09-19 00:47:38 +02:00
R_FREE (ptr);
2016-03-13 02:31:13 +01:00
break;
}
if (print_bars) {
int i;
switch (submode) {
case 'j':
2016-03-27 00:32:12 +01:00
r_cons_printf ("{\"blocksize\":%d,\"address\":%"PFMT64d",\"size\":%"PFMT64d",\"entropy\":[",
blocksize, core->offset, totalsize);
2016-03-13 02:31:13 +01:00
for (i = 0; i < nblocks; i++) {
ut8 ep = ptr[i];
ut64 off = blocksize * i;
const char *comma = (i+1< (nblocks))?",": "";
2016-03-27 00:32:12 +01:00
off += core->offset;
r_cons_printf ("{\"addr\":%"PFMT64d",\"value\":%d}%s",
off, ep, comma);
}
r_cons_printf ("]}\n");
break;
case 'q':
2016-03-13 02:31:13 +01:00
for (i = 0; i < nblocks; i++) {
ut64 off = core->offset + (blocksize * i);
r_cons_printf ("0x%08"PFMT64x" %d %d\n", off, i, ptr[i]);
}
break;
default:
r_print_fill (core->print, ptr, nblocks, core->offset, blocksize);
break;
}
}
beach:
return;
}
static int bbcmp(RAnalBlock *a, RAnalBlock *b) {
return a->addr - b->addr;
}
/* TODO: integrate this into r_anal */
static void _pointer_table (RCore *core, ut64 origin, ut64 offset, const ut8 *buf, int len, int step, int mode) {
int i;
ut64 addr;
st32 *delta; // only for step == 4
if (step <1) {
step = 4;
}
if (origin != offset) {
switch (mode) {
case '*':
r_cons_printf ("CC-@ 0x%08"PFMT64x"\n", origin);
r_cons_printf ("CC switch table @ 0x%08"PFMT64x"\n", origin);
r_cons_printf ("axd 0x%"PFMT64x" 0x%08"PFMT64x"\n", origin, offset);
break;
case '.':
r_core_cmdf (core, "CC-@ 0x%08"PFMT64x"\n", origin);
r_core_cmdf (core, "CC switch table @ 0x%08"PFMT64x"\n", origin);
r_core_cmdf (core, "axd 0x%"PFMT64x" 0x%08"PFMT64x"\n", origin, offset);
break;
}
} else if (mode == '.') {
r_core_cmdf (core, "CC-@ 0x%08"PFMT64x"\n", origin);
r_core_cmdf (core, "CC switch table @ 0x%08"PFMT64x"\n", offset);
}
for (i = 0; i < len; i += step) {
delta = (st32*)(buf + i);
addr = offset + *delta;
if (!r_io_is_valid_offset (core->io, addr, 0)) {
break;
}
if (mode == '*') {
r_cons_printf ("af case.%d.0x%"PFMT64x" 0x%08"PFMT64x"\n", i, offset, addr);
r_cons_printf ("ax 0x%"PFMT64x" 0x%08"PFMT64x"\n", offset, addr);
r_cons_printf ("ax 0x%"PFMT64x" 0x%08"PFMT64x"\n", addr, offset); // wrong, but useful because forward xrefs dont work :?
r_cons_printf ("aho case 0x%"PFMT64x" 0x%08"PFMT64x" @ 0x%08"PFMT64x"\n", i, addr, offset + i); // wrong, but useful because forward xrefs dont work :?
r_cons_printf ("ahs %d @ 0x%08"PFMT64x"\n", step, offset + i);
} else if (mode == '.') {
r_core_cmdf (core, "af case.%d.0x%"PFMT64x" @ 0x%08"PFMT64x"\n", i, offset, addr);
r_core_cmdf (core, "ax 0x%"PFMT64x" 0x%08"PFMT64x"\n", offset, addr);
r_core_cmdf (core, "ax 0x%"PFMT64x" 0x%08"PFMT64x"\n", addr, offset); // wrong, but useful because forward xrefs dont work :?
r_core_cmdf (core, "CC+ case %d: 0x%08"PFMT64x" @ 0x%08"PFMT64x"\n", i / step, addr, origin);
r_core_cmdf (core, "aho case %d 0x%08"PFMT64x" @ 0x%08"PFMT64x"\n", i, addr, offset + i); // wrong, but useful because forward xrefs dont work :?
r_core_cmdf (core, "ahs %d @ 0x%08"PFMT64x"\n", step, offset + i);
} else {
r_cons_printf ("0x%08"PFMT64x" -> 0x%08"PFMT64x"\n", offset + i, addr);
}
}
}
2016-09-09 19:28:47 +02:00
//TODO: this function is a temporary fix. All analysis should be based on realsize. However, now for same architectures realisze is not used
2016-09-12 20:52:12 +02:00
static ut32 tmp_get_contsize (RAnalFunction *f) {
int size = r_anal_fcn_contsize (f);
2016-09-09 19:28:47 +02:00
size = (size > 0) ? size : r_anal_fcn_size (f);
return (size < 0) ? 0 : size;
}
2016-12-05 19:41:37 +01:00
static void pr_bb (RCore * core, RAnalFunction * fcn, RAnalBlock * b, bool emu, ut64 saved_gp, ut8 *saved_arena, char p_type) {
core->anal->gp = saved_gp;
if (emu) {
if (b->parent_reg_arena) {
ut64 gp;
r_reg_arena_poke (core->anal->reg, b->parent_reg_arena);
R_FREE (b->parent_reg_arena);
gp = r_reg_getv (core->anal->reg, "gp");
if (gp) {
core->anal->gp = gp;
}
} else {
r_reg_arena_poke (core->anal->reg, saved_arena);
}
}
if (b->parent_stackptr != INT_MAX) {
core->anal->stackptr = b->parent_stackptr;
}
2016-12-05 19:41:37 +01:00
p_type == 'D'
? r_core_cmdf (core, "pD %"PFMT64d" @0x%"PFMT64x, b->size, b->addr)
: r_core_cmdf (core, "pI %"PFMT64d" @0x%"PFMT64x, b->size, b->addr);
if (b->jump != UT64_MAX) {
if (b->jump > b->addr) {
RAnalBlock * jumpbb = r_anal_bb_get_jumpbb (fcn, b);
if (jumpbb) {
if (emu && core->anal->last_disasm_reg != NULL && !jumpbb->parent_reg_arena) {
jumpbb->parent_reg_arena = r_reg_arena_dup (core->anal->reg, core->anal->last_disasm_reg);
}
if (jumpbb->parent_stackptr == INT_MAX) {
jumpbb->parent_stackptr = core->anal->stackptr + b->stackptr;
}
}
}
2016-12-05 19:41:37 +01:00
if (p_type == 'D') {
r_cons_printf ("| ----------- true: 0x%08"PFMT64x, b->jump);
}
}
if (b->fail != UT64_MAX) {
if (b->fail > b->addr) {
RAnalBlock * failbb = r_anal_bb_get_failbb (fcn, b);
if (failbb) {
if (emu && core->anal->last_disasm_reg != NULL && !failbb->parent_reg_arena) {
failbb->parent_reg_arena = r_reg_arena_dup (core->anal->reg, core->anal->last_disasm_reg);
}
if (failbb->parent_stackptr == INT_MAX) {
failbb->parent_stackptr = core->anal->stackptr + b->stackptr;
}
}
}
2016-12-05 19:41:37 +01:00
if (p_type == 'D') {
r_cons_printf (" false: 0x%08"PFMT64x, b->fail);
}
}
if (p_type == 'D') {
r_cons_newline ();
}
}
static void func_walk_blocks (RCore *core, RAnalFunction *f, char input, char type_print) {
RListIter *iter;
RAnalBlock *b;
RAnalFunction *tmp_func;
RListIter *locs_it = NULL;
2016-12-05 19:41:37 +01:00
if (f->fcn_locs) {
locs_it = f->fcn_locs->head;
}
// XXX: hack must be reviewed/fixed in code analysis
if (r_list_length (f->bbs) == 1) {
ut32 fcn_size = r_anal_fcn_size (f);
b = r_list_get_top (f->bbs);
if (b->size > fcn_size) {
b->size = fcn_size;
}
}
r_list_sort (f->bbs, (RListComparator)bbcmp);
if (input == 'j') {
r_cons_print ("[");
bool isFirst = true;
for (; locs_it && (tmp_func = locs_it->data); locs_it = locs_it->n) {
if (tmp_func->addr > f->addr) {
break;
}
r_list_foreach (tmp_func->bbs, iter, b) {
if (isFirst) {
isFirst = false;
} else {
r_cons_print (",");
}
// const char *cmd = (type_print == 'D')? "pDj": "pIj";
// r_core_cmdf (core, "%s %"PFMT64d" @ 0x%"PFMT64x, cmd, b->size, b->addr);
ut8 *buf = malloc (b->size);
if (buf) {
r_io_read_at (core->io, b->addr, buf, b->size);
r_core_print_disasm_json (core, b->addr, buf, b->size, 0);
free (buf);
} else {
eprintf ("cannot allocate %d bytes\n", b->size);
}
2016-12-05 19:41:37 +01:00
}
}
r_list_foreach (f->bbs, iter, b) {
if (isFirst) {
isFirst = false;
} else {
r_cons_print (",");
}
#if 0
r_core_print_disasm_json (core, core->offset, buf, bsize, 0);
2016-12-13 02:24:25 +01:00
const char *cmd = (type_print == 'D')? "pDj": "pIj";
r_core_cmdf (core, "%s %"PFMT64d" @ 0x%"PFMT64x, cmd, b->size, b->addr);
#endif
ut8 *buf = malloc (b->size);
if (buf) {
r_io_read_at (core->io, b->addr, buf, b->size);
r_core_print_disasm_json (core, b->addr, buf, b->size, 0);
free (buf);
} else {
eprintf ("cannot allocate %d bytes\n", b->size);
}
2016-12-05 19:41:37 +01:00
}
for (; locs_it && (tmp_func = locs_it->data); locs_it = locs_it->n) {
r_list_foreach (tmp_func->bbs, iter, b) {
if (isFirst) {
isFirst = false;
} else {
r_cons_print (",");
}
#if 0
2016-12-13 02:24:25 +01:00
const char *cmd = (type_print == 'D')? "pDj": "pIj";
r_core_cmdf (core, "%s %"PFMT64d" @0x%"PFMT64x, cmd, b->size, b->addr);
#endif
ut8 *buf = malloc (b->size);
if (buf) {
r_io_read_at (core->io, b->addr, buf, b->size);
r_core_print_disasm_json (core, b->addr, buf, b->size, 0);
free (buf);
} else {
eprintf ("cannot allocate %d bytes\n", b->size);
}
2016-12-05 19:41:37 +01:00
}
}
r_cons_print ("]");
} else {
bool asm_lines = r_config_get_i (core->config, "asm.lines");
bool emu = r_config_get_i (core->config, "asm.emu");
2016-12-09 15:39:48 +01:00
ut64 saved_gp = 0;
2017-02-05 02:01:53 +00:00
ut8 *saved_arena = NULL;
int saved_stackptr = core->anal->stackptr;
2016-12-05 19:41:37 +01:00
if (emu) {
saved_gp = core->anal->gp;
saved_arena = r_reg_arena_peek (core->anal->reg);
}
r_config_set_i (core->config, "asm.lines", 0);
for (; locs_it && (tmp_func = locs_it->data); locs_it = locs_it->n) {
2016-12-13 02:24:25 +01:00
if (tmp_func->addr >= f->addr) {
2016-12-05 19:41:37 +01:00
break;
}
2016-12-13 02:24:25 +01:00
r_list_foreach (tmp_func->bbs, iter, b) {
pr_bb (core, tmp_func, b, emu, saved_gp, saved_arena, type_print);
}
2016-12-05 19:41:37 +01:00
}
r_list_foreach (f->bbs, iter, b) {
pr_bb (core, f, b, emu, saved_gp, saved_arena, type_print);
}
for (; locs_it && (tmp_func = locs_it->data); locs_it = locs_it->n) {
//this should be more advanced
r_list_foreach (tmp_func->bbs, iter, b) {
pr_bb (core, tmp_func, b, emu, saved_gp, saved_arena, type_print);
}
}
if (emu) {
core->anal->gp = saved_gp;
if (saved_arena) {
r_reg_arena_poke (core->anal->reg, saved_arena);
R_FREE (saved_arena);
}
}
core->anal->stackptr = saved_stackptr;
2016-12-05 19:41:37 +01:00
r_config_set_i (core->config, "asm.lines", asm_lines);
}
}
2012-02-27 02:40:27 +01:00
static int cmd_print(void *data, const char *input) {
int mode, w, p, i, l, len, total[10];
ut64 off, from, to, at, ate, piece;
2016-05-16 01:18:18 +02:00
RCore *core = (RCore *)data;
2012-02-27 02:40:27 +01:00
ut32 tbs = core->blocksize;
2016-05-16 01:18:18 +02:00
ut64 tmpseek = UT64_MAX;
RCoreAnalStats *as;
2016-05-16 01:18:18 +02:00
int ret = 0;
ut64 n;
2012-02-27 02:40:27 +01:00
r_print_init_rowoffsets (core->print);
2015-06-02 18:06:10 +02:00
off = UT64_MAX;
l = len = core->blocksize;
if (input[0] && input[1]) {
int idx = (input[0] == 'h')? 2: 1;
const char *p = off? strchr (input + idx, ' '): NULL;
if (p) {
2016-11-07 02:33:09 +01:00
l = (int) r_num_math (core->num, p + 1);
2012-02-27 02:40:27 +01:00
/* except disasm and memoryfmt (pd, pm) */
2016-11-04 23:15:54 +01:00
if (input[0] != 'd' && input[0] != 'D' && input[0] != 'm' && input[0]!='a' && input[0]!='f' && input[0] != 'i' && input[0] != 'I') {
2015-09-02 14:52:17 +02:00
int n = (st32) l; //r_num_math (core->num, input+1);
2016-12-13 02:24:25 +01:00
if (l < 0) {
2015-06-03 12:34:15 +02:00
off = core->offset + n;
2015-09-02 14:52:17 +02:00
len = l = - n;
tmpseek = core->offset;
2016-05-16 01:18:18 +02:00
} else if (l > 0) {
len = l;
2016-05-16 01:18:18 +02:00
if (l > tbs) {
2015-01-09 00:35:37 +03:00
if (input[0] == 'x' && input[1] == 'l') {
l *= core->print->cols;
}
if (!r_core_block_size (core, l)) {
eprintf ("This block size is too big. Did you mean 'p%c @ %s' instead?\n",
*input, input+2);
goto beach;
}
l = core->blocksize;
} else {
l = len;
2013-10-21 12:54:34 +02:00
}
}
2012-02-27 02:40:27 +01:00
}
}// else l = 0;
2016-11-07 02:33:09 +01:00
} else {
l = len;
}
2013-11-06 11:15:36 +01:00
if (len > core->blocksize) {
len = core->blocksize;
}
2012-02-27 02:40:27 +01:00
if (input[0] != 'd' && input[0] != 'm' && input[0]!='a' && input[0] != 'f') {
2013-11-06 11:15:36 +01:00
n = core->blocksize_max;
i = (int)n;
if (i != n) i = 0;
if (i && l > i) {
eprintf ("This block size is too big (0x%"PFMT64x
" < 0x%x). Did you mean 'p%c @ %s' instead?\n",
n, l, *input, input+2);
goto beach;
2013-11-06 11:15:36 +01:00
}
2012-02-27 02:40:27 +01:00
}
2015-05-10 22:40:02 +02:00
if (input[0] == 'x' || input[0] == 'D'){
if (l > 0 && tmpseek == UT64_MAX){
2015-09-02 14:52:17 +02:00
if (!r_core_block_size (core, l)){
2015-05-10 22:40:02 +02:00
eprintf ("This block size is too big. Did you mean 'p%c @ %s' instead?\n",
*input, input+2);
goto beach;
2015-05-10 22:40:02 +02:00
}
}
}
2012-02-27 02:40:27 +01:00
2014-09-12 19:25:43 +02:00
if (input[0] && input[0]!='z' && input[1] == 'f') {
RAnalFunction *f = r_anal_get_fcn_in (core->anal, core->offset, 0);
// R_ANAL_FCN_TYPE_FCN|R_ANAL_FCN_TYPE_SYM);
2014-02-21 01:02:47 +01:00
if (f) {
len = r_anal_fcn_size (f);
2014-02-21 01:02:47 +01:00
} else {
eprintf ("p: Cannot find function at 0x%08"PFMT64x"\n", core->offset);
2016-04-06 02:12:52 +02:00
core->num->value = 0;
goto beach;
2014-02-21 01:02:47 +01:00
}
2012-02-27 02:40:27 +01:00
}
core->num->value = len;
2016-11-07 02:33:09 +01:00
if (len > core->blocksize) {
len = core->blocksize;
}
2015-06-02 18:06:10 +02:00
if (off != UT64_MAX) {
r_core_seek (core, off, SEEK_SET);
r_core_block_read (core);
}
2012-02-27 02:40:27 +01:00
switch (*input) {
case 'w': // "pw"
if (input[1] == 'n') {
cmd_print_pwn (core);
2015-03-31 00:49:45 +02:00
} else if (input[1]=='d') {
2014-06-14 02:59:12 +02:00
if (!r_sandbox_enable (0)) {
char *cwd = r_sys_getdir ();
if (cwd) {
r_cons_println (cwd);
2014-06-14 02:59:12 +02:00
free (cwd);
}
2013-11-19 02:55:22 +01:00
}
2014-10-31 19:16:18 +01:00
} else {
r_cons_printf("| pwd display current working directory\n");
}
break;
2016-05-16 01:18:18 +02:00
case 'h': // "ph"
cmd_print_ph (core, input + 1);
break;
case 'v': // "pv"
2016-05-16 01:18:18 +02:00
cmd_print_pv (core, input + 1);
break;
case '-': // "p-"
mode = input[1];
w = len? len: core->print->cols * 4;
2016-11-21 02:37:11 +01:00
w = core->print->cols * 2.7;
if (mode == 'j') {
r_cons_strcat ("{");
}
off = core->offset;
2016-11-21 02:37:11 +01:00
for (i = 0; i < 10; i++) {
2015-02-19 01:52:44 +01:00
total[i] = 0;
2016-11-21 02:37:11 +01:00
}
2015-02-19 01:52:44 +01:00
r_list_free (r_core_get_boundaries (core, "file", &from, &to));
piece = R_MAX((to - from) / w, 1);
as = r_core_anal_get_stats (core, from, to, piece);
2016-11-21 02:37:11 +01:00
if (!as && mode !='?') {
return 0;
}
//eprintf ("RANGE = %llx %llx\n", from, to);
switch (mode) {
2014-06-20 18:57:56 +02:00
case '?':{
const char* help_msg[] = {
"Usage:", "p%%[jh] [pieces]", "bar|json|histogram blocks",
"p-", "", "show ascii-art bar of metadata in file boundaries",
"p-j", "", "show json format",
"p-h", "", "show histogram analysis of metadata per block",
2014-06-20 18:57:56 +02:00
NULL};
r_core_cmd_help (core, help_msg);
}
return 0;
2016-03-19 02:26:28 +01:00
case 'j': //p-j
r_cons_printf (
"\"from\":%"PFMT64d","
"\"to\":%"PFMT64d","
"\"blocksize\":%d,"
"\"blocks\":[", from, to, piece);
break;
2016-03-19 02:26:28 +01:00
case 'h': //p-h
r_cons_printf (".-------------.----------------------------.\n");
r_cons_printf ("| offset | flags funcs cmts syms str |\n");
r_cons_printf ("|-------------)----------------------------|\n");
break;
default:
r_cons_printf ("0x%"PFMT64x" [", from);
}
2016-11-22 21:25:04 +01:00
bool use_color = r_config_get_i (core->config, "scr.color");
len = 0;
2016-11-22 21:25:04 +01:00
for (i = 0; i < w; i++) {
2016-12-02 20:44:32 +01:00
at = from + (piece * i);
ate = at + piece;
2016-11-22 21:25:04 +01:00
p = (at - from) / piece;
switch (mode) {
case 'j':
2016-11-22 21:25:04 +01:00
r_cons_printf ("%s{", len? ",": "");
if ((as->block[p].flags)
|| (as->block[p].functions)
|| (as->block[p].comments)
|| (as->block[p].symbols)
2016-12-02 21:08:09 +01:00
|| (as->block[p].rwx)
2016-11-22 21:25:04 +01:00
|| (as->block[p].strings)) {
r_cons_printf ("\"offset\":%"PFMT64d",", at), l++;
2016-12-02 20:44:32 +01:00
r_cons_printf ("\"size\":%"PFMT64d",", piece), l++;
2016-11-22 21:25:04 +01:00
}
// TODO: simplify with macro
l = 0;
if (as->block[p].flags) r_cons_printf ("%s\"flags\":%d", l?",":"", as->block[p].flags), l++;
if (as->block[p].functions) r_cons_printf ("%s\"functions\":%d", l?",":"", as->block[p].functions), l++;
if (as->block[p].comments) r_cons_printf ("%s\"comments\":%d", l?",":"", as->block[p].comments), l++;
if (as->block[p].symbols) r_cons_printf ("%s\"symbols\":%d", l?",":"", as->block[p].symbols), l++;
if (as->block[p].strings) r_cons_printf ("%s\"strings\":%d", l?",":"", as->block[p].strings), l++;
2016-12-02 20:44:32 +01:00
if (as->block[p].rwx) r_cons_printf ("%s\"rwx\":\"%s\"", l?",":"", r_str_rwx_i (as->block[p].rwx)), l++;
r_cons_strcat ("}");
len++;
break;
case 'h':
total[0] += as->block[p].flags;
total[1] += as->block[p].functions;
total[2] += as->block[p].comments;
total[4] += as->block[p].symbols;
total[5] += as->block[p].strings;
if ((as->block[p].flags)
|| (as->block[p].functions)
|| (as->block[p].comments)
|| (as->block[p].symbols)
2016-11-22 21:25:04 +01:00
|| (as->block[p].strings)) {
r_cons_printf ("| 0x%09"PFMT64x" | %4d %4d %4d %4d %4d |\n", at,
as->block[p].flags,
as->block[p].functions,
as->block[p].comments,
as->block[p].symbols,
as->block[p].strings);
2016-11-22 21:25:04 +01:00
}
break;
default:
if (off >= at && off < ate) {
r_cons_memcat ("^", 1);
} else {
2016-11-22 21:25:04 +01:00
RIOSection *s = r_io_section_vget (core->io, at);
if (use_color) {
if (s) {
if (s->rwx & 1) {
r_cons_print (Color_BGBLUE);
} else {
r_cons_print (Color_BGGREEN);
}
} else {
r_cons_print (Color_BGRED);
}
}
if (as->block[p].strings > 0) {
r_cons_memcat ("z", 1);
} else if (as->block[p].symbols > 0) {
r_cons_memcat ("s", 1);
} else if (as->block[p].functions > 0) {
r_cons_memcat ("F", 1);
2016-11-22 21:25:04 +01:00
} else if (as->block[p].comments > 0) {
r_cons_memcat ("c", 1);
} else if (as->block[p].flags > 0) {
2016-11-22 21:25:04 +01:00
r_cons_memcat (".", 1);
} else {
r_cons_memcat ("_", 1);
2016-11-22 21:25:04 +01:00
}
}
break;
2012-02-27 02:40:27 +01:00
}
}
switch (mode) {
2016-11-22 21:25:04 +01:00
case 'j':
r_cons_strcat ("]}\n");
break;
case 'h':
//r_cons_printf (" total | flags funcs cmts syms str |\n");
r_cons_printf ("|-------------)----------------------------|\n");
r_cons_printf ("| total | %4d %4d %4d %4d %4d |\n",
2016-11-22 21:25:04 +01:00
total[0], total[1], total[2], total[3], total[4], total[5]);
r_cons_printf ("`-------------'----------------------------'\n");
2016-11-22 21:25:04 +01:00
break;
default:
if (use_color) {
r_cons_print (Color_RESET);
}
2016-12-26 05:19:48 +01:00
r_cons_printf ("] 0x%"PFMT64x"\n", to);
}
r_core_anal_stats_free (as);
2012-02-27 02:40:27 +01:00
break;
case '=': //p=
cmd_print_bars (core, input);
2012-02-27 02:40:27 +01:00
break;
2015-03-04 01:08:47 +01:00
case 'A': // "pA"
{
ut64 from = r_config_get_i (core->config, "search.from");
ut64 to = r_config_get_i (core->config, "search.to");
int count = r_config_get_i (core->config, "search.count");
int want = r_num_math (core->num, input+1);
if (input[1]=='?') {
r_core_cmd0 (core, "/A?");
} else {
r_config_set_i (core->config, "search.count", want);
r_config_set_i (core->config, "search.from", core->offset);
r_config_set_i (core->config, "search.to", core->offset+core->blocksize);
r_core_cmd0 (core, "/A");
r_config_set_i (core->config, "search.count", count);
r_config_set_i (core->config, "search.from", from);
r_config_set_i (core->config, "search.to", to);
}
}
break;
case 'a': // "pa"
2015-03-25 06:56:37 +05:30
{
ut32 new_bits = -1;
int segoff, old_bits, pos = 0;
2015-09-14 12:35:38 +02:00
ut8 settings_changed = false;
2015-03-25 06:56:37 +05:30
char *new_arch = NULL, *old_arch = NULL, *hex = NULL;
old_arch = strdup (r_config_get (core->config, "asm.arch"));
old_bits = r_config_get_i (core->config, "asm.bits");
segoff = r_config_get_i (core->config, "asm.segoff");
if (input[1] != ' ') {
if (input[0])
for (pos = 1; pos < R_BIN_SIZEOF_STRINGS && input[pos]; pos++)
if (input[pos] == ' ') break;
2016-03-23 01:44:56 +02:00
if (!r_core_process_input_pade (core, input+pos, &hex, &new_arch, &new_bits)) {
// XXX - print help message
2015-09-14 12:35:38 +02:00
//return false;
}
2015-10-05 14:43:17 +02:00
if (!new_arch) new_arch = strdup (old_arch);
if (new_bits == -1) new_bits = old_bits;
2015-10-05 14:43:17 +02:00
if (strcmp (new_arch, old_arch) != 0 || new_bits != old_bits){
2016-03-23 01:44:56 +02:00
r_core_set_asm_configs (core, new_arch, new_bits, segoff);
2015-09-14 12:35:38 +02:00
settings_changed = true;
}
2015-03-25 06:56:37 +05:30
}
if (input[1]=='e') { // "pae"
2014-10-31 00:20:51 +01:00
if (input[2]=='?') {
r_cons_printf ("|Usage: pae [hex] assemble esil from hexpairs\n");
2014-10-31 00:20:51 +01:00
} else {
int ret, bufsz;
RAnalOp aop = {0};
const char *str;
// char *buf = strdup (input+2);
2015-03-25 06:56:37 +05:30
bufsz = r_hex_str2bin (hex, (ut8*)hex);
2014-10-31 00:20:51 +01:00
ret = r_anal_op (core->anal, &aop, core->offset,
2015-03-25 06:56:37 +05:30
(const ut8*)hex, bufsz);
if (ret > 0) {
2014-10-31 00:20:51 +01:00
str = R_STRBUF_SAFEGET (&aop.esil);
r_cons_println (str);
2014-10-31 00:20:51 +01:00
}
r_anal_op_fini (&aop);
}
} else if (input[1] == 'D') {
2016-09-22 18:06:10 +02:00
if (input[2] == '?') {
r_cons_printf ("|Usage: paD [asm] disasm like in pdi\n");
} else {
r_core_cmdf (core, "pdi@x:%s", input+2);
}
} else if (input[1]=='d') { // "pad"
2014-10-31 00:20:51 +01:00
if (input[2]=='?') {
r_cons_printf ("|Usage: pad [asm] disasm\n");
2014-10-31 00:20:51 +01:00
} else {
RAsmCode *c;
r_asm_set_pc (core->assembler, core->offset);
2015-03-25 06:56:37 +05:30
c = r_asm_mdisassemble_hexstr (core->assembler, hex);
2014-10-31 00:20:51 +01:00
if (c) {
r_cons_print (c->buf_asm);
2014-10-31 00:20:51 +01:00
r_asm_code_free (c);
} else eprintf ("Invalid hexstr\n");
}
2014-10-29 19:04:51 +01:00
} else if (input[1]=='?') {
2015-03-27 17:48:59 +01:00
r_cons_printf("|Usage: pa[ed] [hex|asm] assemble (pa) disasm (pad)"
" esil (pae) from hexpairs\n");
} else {
RAsmCode *acode;
int i;
int bytes;
r_asm_set_pc (core->assembler, core->offset);
2016-09-01 22:34:28 +02:00
acode = r_asm_massemble (core->assembler, input + 1);
if (acode && *acode->buf_hex) {
bytes = strlen (acode->buf_hex) >> 1;
for (i = 0; i < bytes; i++) {
2016-09-01 22:34:28 +02:00
ut8 b = acode->buf[i]; // core->print->big_endian? (bytes - 1 - i): i ];
r_cons_printf ("%02x", b);
}
r_cons_newline ();
r_asm_code_free (acode);
}
}
2015-03-25 06:56:37 +05:30
if (settings_changed)
2016-03-23 01:44:56 +02:00
r_core_set_asm_configs (core, old_arch, old_bits, segoff);
2015-03-25 06:56:37 +05:30
free (old_arch);
free (new_arch);
}
break;
case 'b': { // "pb"
if (input[1]=='?') {
r_cons_printf("|Usage: p[bB] [len] ([skip]) ; see also pB and pxb\n");
2016-05-17 00:37:47 +02:00
} else if (l != 0) {
int from, to;
const int size = len*8;
char *spc, *buf = malloc (size+1);
spc = strchr (input, ' ');
if (spc) {
len = r_num_math (core->num, spc+1);
if (len<1)
len = 1;
spc = strchr (spc+1, ' ');
if (spc) {
from = r_num_math (core->num, spc+1);
} else {
from = 0;
}
to = from+len;
} else {
from = 0;
to = size;
}
if (buf) {
int buf_len;
r_str_bits (buf, core->block, size, NULL);
buf_len = strlen (buf);
if (from>=buf_len) {
from = buf_len;
}
if (to<buf_len) {
buf[to] = 0;
}
r_cons_println (buf+from);
free (buf);
} else eprintf ("ERROR: Cannot malloc %d bytes\n", size);
}
}
break;
case 'B': { // "pB"
2014-10-29 19:04:51 +01:00
if (input[1]=='?') {
r_cons_printf ("|Usage: p[bB] [len] bitstream of N bytes\n");
2016-05-17 00:37:47 +02:00
} else if (l != 0) {
int size;
char *buf;
if (!r_core_block_size (core, len)) {
len = core->blocksize;
}
size = len*8;
buf = malloc (size+1);
2014-10-29 19:04:51 +01:00
if (buf) {
r_str_bits (buf, core->block, size, NULL);
r_cons_println (buf);
2014-10-29 19:04:51 +01:00
free (buf);
} else eprintf ("ERROR: Cannot malloc %d bytes\n", size);
} }
2013-07-12 01:04:05 +02:00
break;
case 'I': // "pI"
switch (input[1]) {
case 'j': // "pIj" is the same as pDj
2016-05-17 00:37:47 +02:00
if (l != 0) {
if (input[2]) {
cmd_pDj (core, input + 2);
} else {
cmd_pDj (core, sdb_fmt (0, "%d", core->blocksize));
}
}
break;
2015-10-21 15:56:03 +02:00
case 'f': // "pIf"
{
const RAnalFunction *f = r_anal_get_fcn_in (core->anal, core->offset,
R_ANAL_FCN_TYPE_FCN|R_ANAL_FCN_TYPE_SYM);
if (f) {
2015-10-21 15:56:03 +02:00
r_core_print_disasm_instructions (core,
r_anal_fcn_size (f), 0);
break;
}
}
case 'd': // "pId" is the same as pDi
2016-05-17 00:37:47 +02:00
if (l) {
pdi (core, 0, l, 0);
}
break;
case '?': // "pi?"
2016-05-17 00:37:47 +02:00
r_cons_printf ("|Usage: p[iI][df] [len] print N instructions/bytes"
"(f=func) (see pi? and pdi)\n");
break;
default:
2016-05-17 00:37:47 +02:00
if (l) {
r_core_print_disasm_instructions (core, l, 0);
}
}
break;
case 'i': // "pi"
switch (input[1]) {
case '?':
2016-12-05 19:41:37 +01:00
// r_cons_printf ("|Usage: pi[defj] [num]\n");
{
const char *help_msg[] = {
"Usage:", "pi[defrj] [num]", "",
"pir", "", "like 'pdr' but with 'pI' output",
NULL};
r_core_cmd_help (core, help_msg);
}
break;
case 'a': // "pia" is like "pda", but with "pi" output
2016-05-17 00:37:47 +02:00
if (l != 0) {
r_core_print_disasm_all (core, core->offset,
l, len, 'i');
}
break;
case 'j': //pij is the same as pdj
2016-05-17 00:37:47 +02:00
if (l != 0) {
cmd_pdj (core, input+2);
}
2014-08-22 00:00:42 +02:00
break;
2016-12-13 02:37:28 +01:00
case 'd': // "pid" is the same as pdi
2016-05-17 00:37:47 +02:00
if (l != 0) {
pdi (core, l, 0, 0);
}
break;
case 'e':
2016-05-17 00:37:47 +02:00
if (l != 0) {
pdi (core, l, 0, 'e');
}
break;
case 'f': // "pif"
2016-05-17 00:37:47 +02:00
if (l != 0) {
RAnalFunction *f = r_anal_get_fcn_in (core->anal, core->offset,
R_ANAL_FCN_TYPE_FCN | R_ANAL_FCN_TYPE_SYM);
2016-05-17 00:37:47 +02:00
if (f) {
ut32 bsz = core->blocksize;
// int fsz = r_anal_fcn_realsize (f);
int fsz = r_anal_fcn_size (f); // we want max-min here
r_core_block_size (core, fsz);
r_core_print_disasm_instructions (core, fsz, 0);
2016-05-17 00:37:47 +02:00
r_core_block_size (core, bsz);
} else {
r_core_print_disasm_instructions (core,
core->blocksize, l);
}
}
break;
2016-12-05 19:41:37 +01:00
case 'r': //pir
{
RAnalFunction *f = r_anal_get_fcn_in (core->anal, core->offset,
R_ANAL_FCN_TYPE_FCN|R_ANAL_FCN_TYPE_SYM);
if (f) {
func_walk_blocks (core, f, input[1], 'I');
} else {
eprintf ("Cannot find function at 0x%08"PFMT64x"\n", core->offset);
core->num->value = 0;
}
}
break;
default:
if (l != 0) {
2016-05-17 00:37:47 +02:00
r_core_print_disasm_instructions (core, 0, l);
}
break;
2013-05-01 00:40:26 +02:00
}
goto beach;
case 'D': // "pD"
case 'd': // "pd"
{
ut64 current_offset = core->offset;
2013-12-02 00:43:44 +01:00
ut32 new_bits = -1;
ut64 use_blocksize = core->blocksize;
2013-12-02 01:28:06 +01:00
int segoff, old_bits, pos = 0;
2015-09-14 12:35:38 +02:00
ut8 settings_changed = false, bw_disassemble = false;
2014-03-18 01:36:54 +01:00
char *new_arch = NULL, *old_arch = NULL;
2015-09-14 12:35:38 +02:00
ut32 pd_result = false, processed_cmd = false;
old_arch = strdup (r_config_get (core->config, "asm.arch"));
2013-12-03 03:36:56 +01:00
segoff = r_config_get_i (core->config, "asm.segoff");
old_bits = r_config_get_i (core->config, "asm.bits");
2016-02-22 23:29:05 +01:00
if (input[1] && input[2]) {
2016-03-10 11:34:21 -05:00
char* p = strchr(input,' ');
if (p) {
int len = (int)r_num_math (core->num, p);
if (len == 0) {
break;
}
2016-02-22 23:29:05 +01:00
}
}
// XXX - this is necessay b/c radare will automatically
// swap flags if arch is x86 and bits == 16 see: __setsegoff in config.c
// get to the space
2016-02-22 23:29:05 +01:00
if (input[0]) {
for (pos = 1; pos < R_BIN_SIZEOF_STRINGS && input[pos]; pos++) {
if (input[pos] == ' ') {
break;
}
}
2016-02-22 23:29:05 +01:00
}
if (!process_input (core, input+pos, &use_blocksize, &new_arch, &new_bits)) {
// XXX - print help message
2015-09-14 12:35:38 +02:00
//return false;
}
if (!use_blocksize) {
2013-12-02 00:43:44 +01:00
use_blocksize = core->blocksize;
}
if (core->blocksize_max < use_blocksize && (int)use_blocksize < -core->blocksize_max) {
2013-12-02 00:43:44 +01:00
eprintf ("This block size is too big (%"PFMT64d"<%"PFMT64d"). Did you mean 'p%c @ 0x%08"PFMT64x"' instead?\n",
(ut64)core->blocksize_max, (ut64)use_blocksize, input[0], (ut64) use_blocksize);
2014-04-29 01:21:57 +02:00
free (old_arch);
2014-04-29 00:52:13 +02:00
free (new_arch);
goto beach;
} else if (core->blocksize_max < use_blocksize && (int)use_blocksize > -core->blocksize_max) {
2015-09-14 12:35:38 +02:00
bw_disassemble = true;
use_blocksize = -use_blocksize;
}
2015-03-25 06:56:37 +05:30
l = use_blocksize;
if (!new_arch) new_arch = strdup (old_arch);
if (new_bits == -1) new_bits = old_bits;
2013-11-22 12:24:32 -06:00
if (strcmp (new_arch, old_arch) != 0 || new_bits != old_bits){
2016-03-23 01:44:56 +02:00
r_core_set_asm_configs (core, new_arch, new_bits, segoff);
2015-09-14 12:35:38 +02:00
settings_changed = true;
2013-11-22 12:24:32 -06:00
}
2012-02-27 02:40:27 +01:00
switch (input[1]) {
2016-12-13 13:06:22 +01:00
case 'C': // "pdC"
pdi (core, l, 0, 'C');
pd_result = 0;
processed_cmd = true;
break;
2015-10-21 01:36:24 +02:00
case 'c': // "pdc" // "pDc"
r_core_pseudo_code (core, input + 2);
pd_result = 0;
2015-09-14 12:35:38 +02:00
processed_cmd = true;
break;
2015-10-21 01:36:24 +02:00
case 'i': // "pdi" // "pDi"
2015-09-14 12:35:38 +02:00
processed_cmd = true;
if (*input == 'D') {
pdi (core, 0, l, 0);
} else {
pdi (core, l, 0, 0);
}
pd_result = 0;
break;
case 'a': // "pda"
2015-09-14 12:35:38 +02:00
processed_cmd = true;
r_core_print_disasm_all (core, core->offset, l, len, input[2]);
pd_result = true;
2012-02-27 02:40:27 +01:00
break;
case 'r': // "pdr"
2015-09-14 12:35:38 +02:00
processed_cmd = true;
{
2017-02-05 02:44:26 +01:00
RAnalFunction *f = r_anal_get_fcn_in (core->anal, core->offset, 0);
// R_ANAL_FCN_TYPE_FCN|R_ANAL_FCN_TYPE_SYM);
2016-12-05 19:41:37 +01:00
if (f) {
func_walk_blocks (core, f, input[2], 'D');
} else {
eprintf ("Cannot find function at 0x%08"PFMT64x"\n", core->offset);
}
pd_result = true;
}
break;
case 'b': // "pdb"
2015-09-14 12:35:38 +02:00
processed_cmd = true;
{
RAnalBlock *b = r_anal_bb_from_offset (core->anal, core->offset);
if (b) {
ut8 *block = malloc (b->size+1);
if (block) {
r_core_read_at (core, b->addr, block, b->size);
core->num->value = r_core_print_disasm (
core->print, core, b->addr, block,
b->size, 9999, 0, 2);
free (block);
pd_result = 0;
}
} else {
eprintf ("Cannot find function at 0x%08"PFMT64x"\n", core->offset);
2016-04-06 02:12:52 +02:00
core->num->value = 0;
}
}
2012-02-27 02:40:27 +01:00
break;
2016-03-23 01:13:16 +01:00
case 's': // "pds" and "pdsf"
processed_cmd = true;
2016-03-23 01:13:16 +01:00
if (input[2] == '?') {
r_cons_printf ("Usage: pds[f] - sumarize N bytes or function (pdfs)\n");
} else {
disasm_strings (core, input, NULL);
}
break;
case 'f': // "pdf"
2015-09-14 12:35:38 +02:00
processed_cmd = true;
2016-03-23 01:13:16 +01:00
if (input[2] == '?') {
r_cons_printf ("Usage: pdf[sj] - disassemble function (summary+cjmp), json)\n");
2016-04-06 02:12:52 +02:00
} else if (input[2] == 's') { // "pdfs"
2016-03-23 01:13:16 +01:00
ut64 oseek = core->offset;
int oblock = core->blocksize;
RAnalFunction *f = r_anal_get_fcn_in (core->anal, core->offset,
R_ANAL_FCN_TYPE_FCN | R_ANAL_FCN_TYPE_SYM);
if (f) {
r_core_seek (core, oseek, SEEK_SET);
r_core_block_size (core, r_anal_fcn_size (f));
2016-03-23 01:13:16 +01:00
disasm_strings (core, input, f);
r_core_block_size (core, oblock);
r_core_seek (core, oseek, SEEK_SET);
}
processed_cmd = true;
} else {
ut32 bsz = core->blocksize;
RAnalFunction *f = r_anal_get_fcn_in (core->anal, core->offset, 0);
// R_ANAL_FCN_TYPE_FCN | R_ANAL_FCN_TYPE_SYM);
2016-09-09 19:28:47 +02:00
RAnalFunction *tmp_func;
ut32 cont_size = 0;
RListIter *locs_it = NULL;
if (f && f->fcn_locs) {
locs_it = f->fcn_locs->head;
}
if (f && input[2] == 'j') { // "pdfj"
2016-09-09 19:28:47 +02:00
ut8 *func_buf = NULL, *loc_buf = NULL;
ut32 fcn_size = r_anal_fcn_realsize (f);
cont_size = tmp_get_contsize (f);
r_cons_printf ("{");
r_cons_printf ("\"name\":\"%s\"", f->name);
r_cons_printf (",\"size\":%d", fcn_size);
r_cons_printf (",\"addr\":%"PFMT64d, f->addr);
2017-02-05 01:24:50 +01:00
r_cons_printf (",\"ops\":[");
// instructions are all outputted as a json list
2016-09-09 19:28:47 +02:00
func_buf = calloc (cont_size, 1);
2017-02-05 01:16:10 +01:00
bool first = true;
2016-09-09 19:28:47 +02:00
if (func_buf) {
//TODO: can loc jump to another locs?
for (; locs_it && (tmp_func = locs_it->data); locs_it = locs_it->n) {
2016-09-09 19:28:47 +02:00
if (tmp_func->addr > f->addr) {
break;
}
cont_size = tmp_get_contsize (tmp_func);
loc_buf = calloc (cont_size, 1);;
r_io_read_at (core->io, tmp_func->addr, loc_buf, cont_size);
2017-02-05 01:16:10 +01:00
if (!first) {
r_cons_print (",");
}
2016-09-09 19:28:47 +02:00
r_core_print_disasm_json (core, tmp_func->addr, loc_buf, cont_size, 0);
2017-02-05 01:16:10 +01:00
first = false;
2016-09-09 19:28:47 +02:00
free (loc_buf);
}
cont_size = tmp_get_contsize (f);
r_io_read_at (core->io, f->addr, func_buf, cont_size);
2017-02-05 01:16:10 +01:00
if (!first) {
r_cons_print (",");
}
2016-09-09 19:28:47 +02:00
r_core_print_disasm_json (core, f->addr, func_buf, cont_size, 0);
2017-02-05 01:16:10 +01:00
first = false;
2016-09-09 19:28:47 +02:00
free (func_buf);
for (; locs_it && (tmp_func = locs_it->data); locs_it = locs_it->n) {
2016-09-09 19:28:47 +02:00
cont_size = tmp_get_contsize (tmp_func);
loc_buf = calloc (cont_size, 1);;
r_io_read_at (core->io, tmp_func->addr, loc_buf, cont_size);
2017-02-05 01:16:10 +01:00
if (!first) {
r_cons_print (",");
}
2016-09-09 19:28:47 +02:00
r_core_print_disasm_json (core, tmp_func->addr, loc_buf, cont_size, 0);
2017-02-05 01:16:10 +01:00
first = false;
2016-09-09 19:28:47 +02:00
free (loc_buf);
}
} else {
eprintf ("cannot allocate %d bytes\n", fcn_size);
}
2017-02-05 01:24:50 +01:00
r_cons_printf ("]}\n");
pd_result = 0;
} else if (f) {
for (; locs_it && (tmp_func = locs_it->data); locs_it = locs_it->n) {
2016-09-09 19:28:47 +02:00
if (tmp_func->addr > f->addr) {
break;
}
cont_size = tmp_get_contsize (tmp_func);
r_core_cmdf (core, "pD %d @ 0x%08" PFMT64x, cont_size, tmp_func->addr);
}
cont_size = tmp_get_contsize (f);
r_core_cmdf (core, "pD %d @ 0x%08" PFMT64x, cont_size, f->addr);
for (; locs_it && (tmp_func = locs_it->data); locs_it = locs_it->n) {
2016-09-09 19:28:47 +02:00
cont_size = tmp_get_contsize (tmp_func);
r_core_cmdf (core, "pD %d @ 0x%08" PFMT64x, cont_size, tmp_func->addr);
}
pd_result = 0;
} else {
eprintf ("pdf: Cannot find function at 0x%08"PFMT64x"\n", core->offset);
processed_cmd = true;
2016-04-06 02:12:52 +02:00
core->num->value = 0;
}
if (bsz != core->blocksize)
r_core_block_size (core, bsz);
2012-02-27 02:40:27 +01:00
}
l = 0;
break;
case 'l': //pdl
2015-09-14 12:35:38 +02:00
processed_cmd = true;
{
RAsmOp asmop;
int j, ret;
const ut8 *buf = core->block;
if (!l) {
l= len;
}
r_cons_break_push (NULL, NULL);
for (i = j = 0; i < core->blocksize && j < l; i += ret, j++ ) {
ret = r_asm_disassemble (core->assembler, &asmop, buf + i, len - i);
if (r_cons_is_breaked ()) {
break;
}
r_cons_printf ("%d\n", ret);
if (ret < 1) {
ret = 1;
}
}
r_cons_break_pop ();
pd_result = 0;
2012-02-27 02:40:27 +01:00
}
break;
case 'j': //pdj
2015-09-14 12:35:38 +02:00
processed_cmd = true;
2016-02-22 23:29:05 +01:00
if (*input == 'D') {
cmd_pDj (core, input+2);
} else cmd_pdj (core, input+2);
r_cons_newline ();
pd_result = 0;
2014-05-21 16:34:06 +02:00
break;
case 0:
/* "pd" -> will disassemble blocksize/4 instructions */
if (*input=='d') {
l /= 4;
}
break;
case '?': // "pd?"
2015-09-14 12:35:38 +02:00
processed_cmd = true;
2014-06-20 18:57:56 +02:00
const char* help_msg[] = {
"Usage:", "p[dD][ajbrfils] [sz] [arch] [bits]", " # Print Disassembly",
"NOTE: ", "len", "parameter can be negative",
"NOTE: ", "", "Pressing ENTER on empty command will repeat last pd command and also seek to end of disassembled range.",
2015-02-03 17:02:34 +01:00
"pd", " N", "disassemble N instructions",
"pd", " -N", "disassemble N instructions backward",
"pD", " N", "disassemble N bytes",
2014-06-20 18:57:56 +02:00
"pda", "", "disassemble all possible opcodes (byte per byte)",
"pdb", "", "disassemble basic block",
2016-05-18 14:54:28 -04:00
"pdc", "", "pseudo disassembler output in C-like syntax",
2016-12-13 13:06:22 +01:00
"pdC", "", "show comments found in N instructions",
"pdj", "", "disassemble to json",
2014-06-20 18:57:56 +02:00
"pdr", "", "recursive disassemble across the function graph",
"pdf", "", "disassemble function",
"pdi", "", "like 'pi', with offset and bytes",
"pdl", "", "show instruction sizes",
//"pds", "", "disassemble with back sweep (greedy disassembly backwards)",
2016-11-26 14:57:21 +01:00
"pds", "[?]", "disassemble summary (strings, calls, jumps, refs) (see pdsf and pdfs)",
"pdt", "", "disassemble the debugger traces (see atd)",
2014-06-20 18:57:56 +02:00
NULL};
r_core_cmd_help (core, help_msg);
pd_result = 0;
2012-02-27 02:40:27 +01:00
}
2013-11-22 12:24:32 -06:00
if (!processed_cmd) {
2013-12-02 05:43:29 +01:00
ut64 addr = core->offset;
ut8 *block = NULL;
ut64 start;
2013-12-02 05:43:29 +01:00
if (bw_disassemble) {
block = malloc (core->blocksize);
if (l < 0) {
l = -l;
}
2013-11-22 12:24:32 -06:00
if (block) {
if (*input == 'D'){ //pD
2016-01-15 03:16:06 +01:00
free (block);
block = malloc (l);
r_core_read_at (core, addr-l, block, l); //core->blocksize);
core->num->value = r_core_print_disasm (core->print, core, addr-l, block, l, l, 0, 1);
} else { //pd
const int bs = core->blocksize;
int instr_len;
if (r_core_prevop_addr (core, core->offset, l, &start)) {
// We have some anal_info.
instr_len = core->offset - start;
} else {
// anal ignorance.
r_core_asm_bwdis_len (core, &instr_len, &addr, l);
}
2016-06-06 10:36:09 +02:00
ut64 prevaddr = core->offset;
2016-06-27 23:26:13 +02:00
r_core_seek (core, prevaddr - instr_len, true);
block = realloc (block, R_MAX(instr_len, bs));
memcpy (block, core->block, bs);
r_core_read_at (core, addr+bs, block+bs, instr_len-bs); //core->blocksize);
core->num->value = r_core_print_disasm (core->print,
core, core->offset, block, instr_len, l, 0, 1);
r_core_seek (core, prevaddr, true);
2012-02-27 02:40:27 +01:00
}
}
2013-12-02 05:43:29 +01:00
} else {
const int bs = core->blocksize;
2013-12-02 05:43:29 +01:00
// XXX: issue with small blocks
if (*input == 'D' && l>0) {
if (l < 1) {
//eprintf ("Block size too small\n");
return 1;
}
2016-02-18 11:08:12 +01:00
if (l > R_CORE_MAX_DISASM) { // pD
eprintf ("Block size too big\n");
return 1;
}
2013-12-02 05:43:29 +01:00
block = malloc (l);
if (block) {
if (l>core->blocksize) {
r_core_read_at (core, addr, block, l); //core->blocksize);
} else {
memcpy (block, core->block, l);
}
core->num->value = r_core_print_disasm (core->print,
core, addr, block, l, l, 0, 1);
} else {
eprintf ("Cannot allocate %d bytes\n", l);
}
2013-12-02 05:43:29 +01:00
} else {
block = malloc (R_MAX(l*10, bs));
memcpy (block, core->block, bs);
2016-12-15 10:22:40 +01:00
r_core_read_at (core, addr + bs, block+bs, (l*10)-bs); //core->blocksize);
2016-09-22 18:06:10 +02:00
core->num->value = r_core_print_disasm (core->print, core, addr, block, l*10, l, 0, 0);
2012-02-27 02:40:27 +01:00
}
}
2013-12-02 05:43:29 +01:00
free (block);
2012-02-27 02:40:27 +01:00
}
2013-12-07 21:45:25 -06:00
core->offset = current_offset;
// change back asm setting if they were changed
2013-11-22 12:24:32 -06:00
if (settings_changed)
2016-03-23 01:44:56 +02:00
r_core_set_asm_configs (core, old_arch, old_bits, segoff);
free (old_arch);
free (new_arch);
2015-06-19 21:30:57 +02:00
if (processed_cmd) {
ret = pd_result;
goto beach;
}
2015-06-19 21:30:57 +02:00
}
2012-02-27 02:40:27 +01:00
break;
2016-05-17 00:37:47 +02:00
case 's': // "ps"
switch (input[1]) {
2014-06-20 18:57:56 +02:00
case '?':{
const char* help_msg[] = {
"Usage:", "ps[zpw] [N]", "Print String",
"ps", "", "print string",
"psi", "", "print string inside curseek",
"psb", "", "print strings in current block",
"psx", "", "show string with scaped chars",
"psz", "", "print zero terminated string",
"psp", "", "print pascal string",
"psu", "", "print utf16 unicode (json)",
2014-06-20 18:57:56 +02:00
"psw", "", "print wide string",
2015-03-23 19:53:19 +01:00
"psj", "", "print string in JSON format",
2014-06-20 18:57:56 +02:00
NULL};
r_core_cmd_help (core, help_msg);
}
break;
2015-03-23 19:53:19 +01:00
case 'j':
2016-05-17 00:37:47 +02:00
if (l > 0) {
2015-03-23 19:53:19 +01:00
char *str, *type;
ut64 vaddr;
RIOSection *section;
2015-03-23 19:53:19 +01:00
if (input[2] == ' ' && input[3]){
len = r_num_math (core->num, input+3);
len = R_MIN (len, core->blocksize);
}
/* try to get the section that contains the
* string, by considering current offset as
* paddr and if it isn't, trying to consider it
* as vaddr. */
vaddr = r_io_section_maddr_to_vaddr (core->io, core->offset);
2015-03-23 19:53:19 +01:00
section = core->io->section;
if (vaddr == UT64_MAX) {
section = r_io_section_vget (core->io, core->offset);
if (section) {
vaddr = core->offset;
}
}
2015-03-23 19:53:19 +01:00
r_cons_printf ("{\"string\":");
str = r_str_utf16_encode ((const char*)core->block, len);
r_cons_printf ("\"%s\"", str);
r_cons_printf (",\"offset\":%"PFMT64d, core->offset);
r_cons_printf (",\"section\":\"%s\"", vaddr == UT64_MAX ? "unknown" : section->name);
2015-03-23 19:53:19 +01:00
r_cons_printf (",\"length\":%d", len);
switch (get_string_type (core->block, len)){
2015-03-23 19:53:19 +01:00
case 'w' : type = "wide" ; break;
case 'a' : type = "ascii"; break;
case 'u' : type = "utf" ; break;
2015-12-12 22:27:17 +01:00
default : type = "unknown" ; break;
2015-03-23 19:53:19 +01:00
}
r_cons_printf (",\"type\":\"%s\"}", type);
r_cons_newline ();
2015-03-23 19:53:19 +01:00
free (str);
}
break;
case 'i': //psi
2016-05-17 00:37:47 +02:00
if (l > 0) {
ut8 *buf = malloc (1024);
int delta = 512;
ut8 *p, *e, *b;
if (!buf) return 0;
if (core->offset<delta)
delta = core->offset;
2016-02-22 23:29:05 +01:00
p = buf + delta;
r_core_read_at (core, core->offset-delta, buf, 1024);
for (b = p; b>buf; b--) {
if (!IS_PRINTABLE (*b)) {
b++;
break;
}
}
for (e = p; e<(buf+1024); e++) {
if (!IS_PRINTABLE (*b)) {
*e = 0;
e--;
break;
}
}
r_cons_strcat ((const char *)b);
r_cons_newline ();
//r_print_string (core->print, core->offset, b,
// (size_t)(e-b), 0);
free (buf);
}
break;
2014-10-27 17:23:13 +01:00
case 'x': // "psx"
2016-05-17 00:37:47 +02:00
if (l > 0) {
r_print_string (core->print, core->offset, core->block, len, 0);
}
break;
2014-10-27 17:23:13 +01:00
case 'b': // "psb"
2016-05-17 00:37:47 +02:00
if (l > 0) {
char *s = malloc (core->blocksize+1);
int i, j, hasnl = 0;;
if (s) {
memset (s, 0, core->blocksize);
// TODO: filter more chars?
for (i=j=0;i<core->blocksize; i++) {
char ch = (char)core->block[i];
if (!ch) {
if (!hasnl) {
2014-10-08 02:42:16 +02:00
s[j] = 0;
if (*s) {
r_cons_println (s);
}
j = 0;
s[0] = 0;
}
hasnl = 1;
continue;
}
hasnl = 0;
if (IS_PRINTABLE (ch))
s[j++] = ch;
}
2014-10-08 02:42:16 +02:00
s[j] = 0;
r_cons_print (s); // TODO: missing newline?
free (s);
}
}
break;
case 'z': //psz
2016-05-17 00:37:47 +02:00
if (l > 0) {
char *s = malloc (core->blocksize + 1);
int i, j;
if (s) {
memset (s, 0, core->blocksize);
// TODO: filter more chars?
for (i=j=0;i<core->blocksize; i++) {
char ch = (char)core->block[i];
if (!ch) break;
if (IS_PRINTABLE (ch))
s[j++] = ch;
}
r_cons_println (s);
free (s);
}
}
break;
2016-05-17 00:37:47 +02:00
case 'p': // "psp"
if (l > 0) {
int mylen = core->block[0];
// TODO: add support for 2-4 byte length pascal strings
if (mylen < core->blocksize) {
r_print_string (core->print, core->offset,
core->block + 1, mylen, R_PRINT_STRING_ZEROEND);
core->num->value = mylen;
} else core->num->value = 0; // error
}
break;
2016-05-17 00:37:47 +02:00
case 'w': // "psw"
if (l > 0) {
r_print_string (core->print, core->offset, core->block, len,
R_PRINT_STRING_WIDE | R_PRINT_STRING_ZEROEND);
}
break;
case ' ':
2016-05-17 00:37:47 +02:00
r_print_string (core->print, core->offset, core->block, l, 0);
break;
case 'u':
2016-05-17 00:37:47 +02:00
if (l > 0) {
char *str = r_str_utf16_encode (
2015-03-13 21:18:25 +01:00
(const char*)core->block, len);
r_cons_println (str);
free (str);
}
break;
default:
2016-05-17 00:37:47 +02:00
if (l > 0) {
r_print_string (core->print, core->offset, core->block,
2015-10-05 14:43:17 +02:00
len, R_PRINT_STRING_ZEROEND);
2016-05-17 00:37:47 +02:00
}
break;
}
2012-02-27 02:40:27 +01:00
break;
2016-05-17 00:37:47 +02:00
case 'm': // "pm"
2012-02-27 02:40:27 +01:00
if (input[1]=='?') {
r_cons_printf ("|Usage: pm [file|directory]\n"
"| r_magic will use given file/dir as reference\n"
"| output of those magic can contain expressions like:\n"
"| foo@0x40 # use 'foo' magic file on address 0x40\n"
"| @0x40 # use current magic file on address 0x40\n"
"| \\n # append newline\n"
"| e dir.magic # defaults to "R_MAGIC_PATH"\n"
2015-07-15 01:30:53 +02:00
"| /m # search for magic signatures\n"
2012-02-27 02:40:27 +01:00
);
2016-05-02 22:52:41 -04:00
} else {
// XXX: need cmd_magic header for r_core_magic
2016-05-17 00:37:47 +02:00
if (l > 0) {
r_core_magic (core, input + 1, true);
}
2016-05-02 22:52:41 -04:00
}
2012-02-27 02:40:27 +01:00
break;
2016-05-17 00:37:47 +02:00
case 'u': // "pu"
if (input[1] == '?') {
r_cons_printf ("|Usage: pu[w] [len] print N url"
"encoded bytes (w=wide)\n");
} else {
2016-05-17 00:37:47 +02:00
if (l > 0) {
r_print_string (core->print, core->offset, core->block, len,
R_PRINT_STRING_URLENCODE |
((input[1]=='w')?R_PRINT_STRING_WIDE:0));
}
2014-10-29 19:04:51 +01:00
}
2012-02-27 02:40:27 +01:00
break;
2016-05-10 00:39:31 +02:00
case 'c': // "pc"
if (l) {
const ut8 *buf = core->block;
int i = 0;
int j = 0;
2017-01-05 00:25:45 +01:00
if (input[1] == 'A') { // "pcA"
2017-01-05 00:18:06 +01:00
r_cons_printf ("sub_0x%08"PFMT64x":\n", core->offset);
for (i = 0; i < len; i++) {
RAsmOp asmop = {0};
(void)r_asm_disassemble (core->assembler, &asmop, buf + i, len - i);
2017-01-05 00:18:06 +01:00
int sz = asmop.size;
if (sz < 1) {
sz = 1;
}
r_cons_printf (" .byte ");
for (j = 0; j < sz; j++) {
r_cons_printf ("%s0x%02x", j? ", ":"", buf[i]);
i++;
}
r_cons_printf (" // %s\n", asmop.buf_asm);
2017-01-05 00:18:06 +01:00
i--;
}
r_cons_printf (".equ shellcode_len, %d\n", len);
} else {
r_print_code (core->print, core->offset, core->block, len, input[1]);
}
2016-05-17 00:37:47 +02:00
}
2012-02-27 02:40:27 +01:00
break;
case 'C': // "pC"
switch (input[1]) {
case 0:
case ' ':
case 'd':
cmd_pCd (core, input + 2);
break;
case 'D':
cmd_pCD (core, input + 2);
break;
case 'a':
cmd_pCx (core, input + 2, "pxa");
break;
case 'A':
cmd_pCx (core, input + 2, "pxA");
break;
case 'x':
cmd_pCx (core, input + 2, "px");
break;
case 'w':
cmd_pCx (core, input + 2, "pxw");
break;
case 'c':
cmd_pCx (core, input + 2, "pc");
break;
default:
eprintf ("Usage: pCd\n");
break;
}
break;
2015-02-19 12:29:57 +01:00
case 'r': // "pr"
2014-08-26 03:52:25 +02:00
switch (input[1]) {
case '?':
r_cons_printf ("|Usage: pr[glx] [size]\n"
"| prl: print raw with lines offsets\n"
"| prx: printable chars with real offset (hyew)\n"
2016-11-26 14:57:21 +01:00
"| prg[?]: print raw GUNZIPped block\n"
2016-05-18 12:21:49 +02:00
"| prz: print raw zero terminated string\n");
2014-08-26 03:52:25 +02:00
break;
case 'g': // "prg" // gunzip
switch (input[2]) {
case '?':
r_cons_printf ("|Usage: prg[io]\n"
"| prg: print gunzipped data of current block\n"
"| prgi: show consumed bytes when inflating\n"
"| prgo: show output bytes after inflating\n");
break;
case 'i':
{
int sz, outlen = 0;
int inConsumed = 0;
ut8 *in, *out;
in = core->block;
sz = core->blocksize;
out = r_inflate (in, sz, &inConsumed, &outlen);
r_cons_printf ("%d\n", inConsumed);
free (out);
}
break;
case 'o':
{
int sz, outlen = 0;
ut8 *in, *out;
in = core->block;
sz = core->blocksize;
out = r_inflate (in, sz, NULL, &outlen);
r_cons_printf ("%d\n", outlen);
free (out);
}
break;
default:
{
int sz, outlen = 0;
ut8 *in, *out;
in = core->block;
sz = core->blocksize;
out = r_inflate (in, sz, NULL, &outlen);
if (out) {
r_cons_memcat ((const char*)out, outlen);
}
free (out);
}
}
break;
2015-03-23 01:09:35 +01:00
/* TODO: compact */
case 'l': // "prl"
2016-05-17 00:37:47 +02:00
if (l != 0) {
printraw (core, len, 1);
}
2015-03-23 01:09:35 +01:00
break;
case 'x': // "prx"
2016-05-17 00:37:47 +02:00
if (l != 0) {
printraw (core, len, 2);
}
2015-03-23 01:09:35 +01:00
break;
2016-05-18 12:21:49 +02:00
case 'z': // "prz"
if (l != 0) {
2016-05-19 03:42:23 +02:00
printraw (core, strlen ((const char*)core->block), 0);
2016-05-18 12:21:49 +02:00
}
break;
2014-08-26 03:52:25 +02:00
default:
2016-05-17 00:37:47 +02:00
if (l != 0) {
printraw (core, len, 0);
}
2015-03-23 01:09:35 +01:00
break;
2014-08-26 03:52:25 +02:00
}
2012-02-27 02:40:27 +01:00
break;
case '3': // "p3" [file]
if (input[1]=='?') {
eprintf ("Usage: p3 [file] - print 3D stereogram image of current block\n");
} else
if (input[1]==' ') {
char *data = r_file_slurp (input+2, NULL);
char *res = r_print_stereogram (data, 78, 20);
r_print_stereogram_print (core->print, res);
//if (data) eprintf ("%s\n", data);
free (res);
free (data);
} else {
char *res = r_print_stereogram_bytes (core->block, core->blocksize);
r_print_stereogram_print (core->print, res);
free (res);
}
break;
2014-10-27 17:23:13 +01:00
case 'x': // "px"
{
int show_offset = r_config_get_i (core->config, "asm.offset");
if (show_offset) {
core->print->flags |= R_PRINT_FLAGS_HEADER;
core->print->flags |= R_PRINT_FLAGS_OFFSET;
} else {
core->print->flags &= ~R_PRINT_FLAGS_OFFSET;
core->print->flags &= ~R_PRINT_FLAGS_HEADER;
}
/* Don't show comments in default case */
core->print->use_comments = false;
}
r_cons_break_push (NULL, NULL);
switch (input[1]) {
case '/':
r_core_print_examine (core, input+2);
break;
2014-06-21 14:42:00 +02:00
case '?':{
const char* help_msg[] = {
"Usage:", "px[afoswqWqQ][f]", " # Print heXadecimal",
"px", "", "show hexdump",
"px/", "", "same as x/ in gdb (help x)",
"pxa", "", "show annotated hexdump",
2015-03-21 00:27:49 +01:00
"pxA", "", "show op analysis color map",
"pxb", "", "dump bits in hexdump form",
"pxc", "", "show hexdump with comments",
2016-02-27 18:35:50 +01:00
"pxd", "[124]", "signed integer dump (1 byte, 2 and 4)",
2014-06-21 14:42:00 +02:00
"pxe", "", "emoji hexdump! :)",
"pxi", "", "HexII compact binary representation",
2014-06-21 14:42:00 +02:00
"pxf", "", "show hexdump of current function",
"pxh", "", "show hexadecimal half-words dump (16bit)",
"pxH", "", "same as above, but one per line",
"pxl", "", "display N lines (rows) of hexdump",
2014-06-21 14:42:00 +02:00
"pxo", "", "show octal dump",
"pxq", "", "show hexadecimal quad-words dump (64bit)",
"pxQ", "", "same as above, but one per line",
"pxr", "[j]", "show words with references to flags and code",
"pxs", "", "show hexadecimal in sparse mode",
"pxt", "[*.] [origin]", "show delta pointer table in r2 commands",
2014-06-21 14:42:00 +02:00
"pxw", "", "show hexadecimal words dump (32bit)",
"pxW", "", "same as above, but one per line",
NULL};
r_core_cmd_help (core, help_msg);
}
break;
case 'a': // "pxa"
2016-05-17 00:37:47 +02:00
if (l != 0) {
if (len % 16) {
len += 16 - (len % 16);
2016-11-04 01:30:35 +01:00
}
annotated_hexdump (core, input + 2, len);
2016-05-17 00:37:47 +02:00
}
break;
case 'A': // "pxA"
if (input[2]=='?') {
eprintf ("Usage: pxA [len] # f.ex: pxA 4K\n"
" mv move,lea,li\n"
" -> push\n"
" <- pop\n"
" io in/out ops\n"
" $$ int/swi/trap/new\n"
" .. nop\n"
" +-*/ math ops\n"
" |&^ bin ops\n"
" <<>> shift ops\n"
" _J jump\n"
" cJ conditional jump\n"
" _C call\n"
2016-06-07 15:00:48 +03:00
" _R ret\n"
" == cmp/test\n"
" XX invalid\n");
} else if (l) {
cmd_print_pxA (core, len, input+1);
}
2015-03-21 00:27:49 +01:00
break;
case 'b': // "pxb"
if (l) {
ut32 n;
int i, c;
char buf[32];
#define P(x) (IS_PRINTABLE(x)?x:'.')
2015-10-05 14:43:17 +02:00
#define SPLIT_BITS(x) memmove (x + 5, x + 4, 5); x[4]=0
for (i = c = 0; i < len; i++,c++) {
if (c == 0) {
r_print_offset (core->print,
2016-11-01 23:30:36 +01:00
core->offset + i, 0, 0, 0, 0, NULL);
}
r_str_bits (buf, core->block+i, 8, NULL);
SPLIT_BITS (buf);
r_cons_printf ("%s.%s ", buf, buf+5);
if (c == 3) {
const ut8 *b = core->block + i-3;
#define K(x) (b[3-x]<<(8*x))
n = K (0) | K (1) | K (2) | K (3);
r_cons_printf ("0x%08x %c%c%c%c\n",
2015-10-05 14:43:17 +02:00
n, P (b[0]), P (b[1]), P (b[2]),
P (b[3]));
c = -1;
}
}
}
break;
case 'c':
core->print->use_comments = core->print->flags & R_PRINT_FLAGS_COMMENT;
if (l) {
ut64 from = r_config_get_i (core->config, "diff.from");
ut64 to = r_config_get_i (core->config, "diff.to");
if (from == to && !from) {
if (!r_core_block_size (core, len)) {
len = core->blocksize;
}
r_print_hexdump (core->print, core->offset,
core->block, len, 16, 1);
} else {
r_core_print_cmp (core, from, to);
}
core->num->value = len;
}
break;
case 'i': // "pxi"
2016-05-17 00:37:47 +02:00
if (l != 0) {
r_print_hexii (core->print, core->offset, core->block,
core->blocksize, r_config_get_i (core->config, "hex.cols"));
}
break;
case 'o': // "pxo"
2016-05-17 00:37:47 +02:00
if (l != 0) {
r_print_hexdump (core->print, core->offset,
2015-10-05 14:43:17 +02:00
core->block, len, 8, 1);
2016-05-17 00:37:47 +02:00
}
break;
case 't': // "pxt"
if (input[2] == '?') {
r_cons_printf ("Usage: pxt[.*] - print delta pointer table\n");
} else {
ut64 origin = core->offset;
const char *arg = strchr (input, ' ');
if (arg) {
origin = r_num_math (core->num, arg + 1);
}
_pointer_table (core, origin, core->offset, core->block, len, 4, input[2]);
}
break;
2015-04-28 20:46:47 +02:00
case 'd': // "pxd"
2016-05-17 00:37:47 +02:00
if (l != 0) {
2016-02-27 18:35:50 +01:00
switch (input[2]) {
case '1':
// 1 byte signed words (byte)
r_print_hexdump (core->print, core->offset,
core->block, len, -1, 4);
break;
case '2':
// 2 byte signed words (short)
r_print_hexdump (core->print, core->offset,
core->block, len, -10, 2);
break;
case '8':
r_print_hexdump (core->print, core->offset,
core->block, len, -8, 4);
break;
case '4':
default:
// 4 byte signed words
r_print_hexdump (core->print, core->offset,
core->block, len, 10, 4);
}
2016-05-17 00:37:47 +02:00
}
2014-04-12 01:13:34 +02:00
break;
2016-06-07 00:12:45 +02:00
case 'w': // "pxw"
2016-05-17 00:37:47 +02:00
if (l != 0) {
r_print_hexdump (core->print, core->offset, core->block, len, 32, 4);
}
break;
2015-04-28 20:46:47 +02:00
case 'W': // "pxW"
if (l) {
len = len - (len % 4);
for (i = 0; i < len; i += 4) {
const char *a, *b;
char *fn;
RPrint *p = core->print;
RFlagItem *f;
ut32 v = r_read_ble32 (core->block + i, core->print->big_endian);
if (p && p->colorfor) {
2017-01-13 14:13:53 +01:00
a = p->colorfor (p->user, v, true);
if (a && *a) {
b = Color_RESET;
} else {
a = b = "";
}
2015-10-05 14:43:17 +02:00
} else {
a = b = "";
}
f = r_flag_get_at (core->flags, v, true);
fn = NULL;
if (f) {
st64 delta = (v - f->offset);
if (delta >= 0 && delta < 8192) {
if (v == f->offset) {
fn = strdup (f->name);
} else {
fn = r_str_newf ("%s+%d",
f->name, v-f->offset);
}
2015-10-05 14:43:17 +02:00
}
2015-02-01 02:28:49 +01:00
}
r_cons_printf ("0x%08"PFMT64x" %s0x%08"PFMT64x"%s %s\n",
(ut64)core->offset+i, a, (ut64)v, b, fn? fn: "");
free (fn);
2015-02-01 02:28:49 +01:00
}
2016-05-17 00:37:47 +02:00
}
break;
case 'r': // "pxr"
if (l) {
2016-09-03 17:08:02 +02:00
if (input[2] == 'j') {
int base = core->anal->bits;
r_cons_printf ("[");
const char *comma = "";
const ut8 *buf = core->block;
int withref = 0;
for (i = 0; i < core->blocksize; i+= (base/4)) {
2016-09-03 17:08:02 +02:00
ut64 addr = core->offset + i;
ut64 *foo = (ut64*)(buf+i);
ut64 val = *foo;
if (base == 32) val &= UT32_MAX;
2016-09-03 17:08:02 +02:00
r_cons_printf ("%s{\"addr\":%"PFMT64d",\"value\":%" \
PFMT64d, comma, addr, val);
comma = ",";
// XXX: this only works in little endian
withref = 0;
if (core->print->hasrefs) {
2017-01-13 14:13:53 +01:00
const char *rstr = core->print->hasrefs (core->print->user, val, true);
2016-09-03 17:08:02 +02:00
if (rstr && *rstr) {
char *ns; //r_str_ansi_chop (ns, -1, 0);
ns = r_str_escape (rstr);
r_cons_printf (",\"ref\":\"%s\"}", *ns==' '?ns+1:ns);
free (ns);
withref = 1;
}
}
if (!withref) {
r_cons_printf ("}");
}
}
2016-09-03 17:08:02 +02:00
r_cons_printf ("]\n");
} else {
const int ocols = core->print->cols;
int bitsize = core->assembler->bits;
/* Thumb is 16bit arm but handles 32bit data */
if (bitsize == 16) bitsize = 32;
core->print->cols = 1;
core->print->flags |= R_PRINT_FLAGS_REFS;
r_cons_break_push (NULL, NULL);
2016-09-03 17:08:02 +02:00
r_print_hexdump (core->print, core->offset,
core->block, len,
bitsize, bitsize / 8);
r_cons_break_pop ();
2016-09-03 17:08:02 +02:00
core->print->flags &= ~R_PRINT_FLAGS_REFS;
core->print->cols = ocols;
}
2014-10-27 17:23:13 +01:00
}
break;
case 'h':
if (l) {
2016-05-17 00:37:47 +02:00
r_print_hexdump (core->print, core->offset,
core->block, len, 32, 2);
}
break;
case 'H':
2016-05-17 00:37:47 +02:00
if (l != 0) {
len = len - (len % 2);
for (i = 0; i < len; i += 2) {
const char *a, *b;
char *fn;
RPrint *p = core->print;
RFlagItem *f;
ut64 v = (ut64)r_read_ble16 (core->block + i, p->big_endian);
if (p && p->colorfor) {
2017-01-13 14:13:53 +01:00
a = p->colorfor (p->user, v, true);
if (a && *a) {
b = Color_RESET;
} else {
a = b = "";
}
} else {
a = b = "";
}
f = r_flag_get_at (core->flags, v, true);
fn = NULL;
if (f) {
st64 delta = (v - f->offset);
if (delta>=0 && delta<8192) {
if (v == f->offset) {
fn = strdup (f->name);
} else {
fn = r_str_newf ("%s+%d", f->name, v-f->offset);
}
}
}
r_cons_printf ("0x%08"PFMT64x" %s0x%04"PFMT64x"%s %s\n",
(ut64)core->offset+i, a, v, b, fn? fn: "");
free (fn);
}
2016-05-17 00:37:47 +02:00
}
break;
case 'q':
if (l) {
2016-05-17 00:37:47 +02:00
r_print_hexdump (core->print, core->offset, core->block, len, 64, 8);
}
break;
case 'Q':
2015-01-25 04:30:05 +01:00
// TODO. show if flag name, or inside function
if (l) {
len = len - (len % 8);
for (i = 0; i < len; i += 8) {
const char *a, *b;
char *fn;
RPrint *p = core->print;
RFlagItem *f;
ut64 v = r_read_ble64 (core->block + i, p->big_endian);
if (p && p->colorfor) {
2017-01-13 14:13:53 +01:00
a = p->colorfor (p->user, v, true);
if (a && *a) {
b = Color_RESET;
} else {
a = b = "";
}
} else {
a = b = "";
}
f = r_flag_get_at (core->flags, v, true);
fn = NULL;
if (f) {
st64 delta = (v - f->offset);
if (delta>=0 && delta<8192) {
if (v == f->offset) {
fn = strdup (f->name);
} else {
fn = r_str_newf ("%s+%d", f->name, v-f->offset);
}
}
2015-02-01 02:28:49 +01:00
}
r_cons_printf ("0x%08"PFMT64x" %s0x%016"PFMT64x"%s %s\n",
(ut64)core->offset+i, a, v, b, fn? fn: "");
free (fn);
2015-02-01 02:28:49 +01:00
}
2016-05-17 00:37:47 +02:00
}
break;
case 's':
if (l) {
2016-05-17 00:37:47 +02:00
core->print->flags |= R_PRINT_FLAGS_SPARSE;
r_print_hexdump (core->print, core->offset, core->block, len, 16, 1);
2016-05-17 00:37:47 +02:00
core->print->flags &= (((ut32)-1) & (~R_PRINT_FLAGS_SPARSE));
}
break;
2016-09-15 19:31:25 +02:00
case 'e': // "pxe"
2016-05-17 00:37:47 +02:00
if (l != 0) {
int j;
char emoji[] = {'\x8c','\x80','\x8c','\x82','\x8c','\x85','\x8c','\x88',
'\x8c','\x99','\x8c','\x9e','\x8c','\x9f','\x8c','\xa0',
'\x8c','\xb0','\x8c','\xb1','\x8c','\xb2','\x8c','\xb3',
'\x8c','\xb4','\x8c','\xb5','\x8c','\xb7','\x8c','\xb8',
'\x8c','\xb9','\x8c','\xba','\x8c','\xbb','\x8c','\xbc',
'\x8c','\xbd','\x8c','\xbe','\x8c','\xbf','\x8d','\x80',
'\x8d','\x81','\x8d','\x82','\x8d','\x83','\x8d','\x84',
'\x8d','\x85','\x8d','\x86','\x8d','\x87','\x8d','\x88',
'\x8d','\x89','\x8d','\x8a','\x8d','\x8b','\x8d','\x8c',
'\x8d','\x8d','\x8d','\x8e','\x8d','\x8f','\x8d','\x90',
'\x8d','\x91','\x8d','\x92','\x8d','\x93','\x8d','\x94',
'\x8d','\x95','\x8d','\x96','\x8d','\x97','\x8d','\x98',
'\x8d','\x9c','\x8d','\x9d','\x8d','\x9e','\x8d','\x9f',
'\x8d','\xa0','\x8d','\xa1','\x8d','\xa2','\x8d','\xa3',
'\x8d','\xa4','\x8d','\xa5','\x8d','\xa6','\x8d','\xa7',
'\x8d','\xa8','\x8d','\xa9','\x8d','\xaa','\x8d','\xab',
'\x8d','\xac','\x8d','\xad','\x8d','\xae','\x8d','\xaf',
'\x8d','\xb0','\x8d','\xb1','\x8d','\xb2','\x8d','\xb3',
'\x8d','\xb4','\x8d','\xb5','\x8d','\xb6','\x8d','\xb7',
'\x8d','\xb8','\x8d','\xb9','\x8d','\xba','\x8d','\xbb',
'\x8d','\xbc','\x8e','\x80','\x8e','\x81','\x8e','\x82',
'\x8e','\x83','\x8e','\x84','\x8e','\x85','\x8e','\x88',
'\x8e','\x89','\x8e','\x8a','\x8e','\x8b','\x8e','\x8c',
'\x8e','\x8d','\x8e','\x8e','\x8e','\x8f','\x8e','\x92',
'\x8e','\x93','\x8e','\xa0','\x8e','\xa1','\x8e','\xa2',
'\x8e','\xa3','\x8e','\xa4','\x8e','\xa5','\x8e','\xa6',
'\x8e','\xa7','\x8e','\xa8','\x8e','\xa9','\x8e','\xaa',
'\x8e','\xab','\x8e','\xac','\x8e','\xad','\x8e','\xae',
'\x8e','\xaf','\x8e','\xb0','\x8e','\xb1','\x8e','\xb2',
'\x8e','\xb3','\x8e','\xb4','\x8e','\xb5','\x8e','\xb7',
'\x8e','\xb8','\x8e','\xb9','\x8e','\xba','\x8e','\xbb',
'\x8e','\xbd','\x8e','\xbe','\x8e','\xbf','\x8f','\x80',
'\x8f','\x81','\x8f','\x82','\x8f','\x83','\x8f','\x84',
'\x8f','\x86','\x8f','\x87','\x8f','\x88','\x8f','\x89',
'\x8f','\x8a','\x90','\x80','\x90','\x81','\x90','\x82',
'\x90','\x83','\x90','\x84','\x90','\x85','\x90','\x86',
'\x90','\x87','\x90','\x88','\x90','\x89','\x90','\x8a',
'\x90','\x8b','\x90','\x8c','\x90','\x8d','\x90','\x8e',
'\x90','\x8f','\x90','\x90','\x90','\x91','\x90','\x92',
'\x90','\x93','\x90','\x94','\x90','\x95','\x90','\x96',
'\x90','\x97','\x90','\x98','\x90','\x99','\x90','\x9a',
'\x90','\x9b','\x90','\x9c','\x90','\x9d','\x90','\x9e',
'\x90','\x9f','\x90','\xa0','\x90','\xa1','\x90','\xa2',
'\x90','\xa3','\x90','\xa4','\x90','\xa5','\x90','\xa6',
'\x90','\xa7','\x90','\xa8','\x90','\xa9','\x90','\xaa',
'\x90','\xab','\x90','\xac','\x90','\xad','\x90','\xae',
'\x90','\xaf','\x90','\xb0','\x90','\xb1','\x90','\xb2',
'\x90','\xb3','\x90','\xb4','\x90','\xb5','\x90','\xb6',
'\x90','\xb7','\x90','\xb8','\x90','\xb9','\x90','\xba',
'\x90','\xbb','\x90','\xbc','\x90','\xbd','\x90','\xbe',
'\x91','\x80','\x91','\x82','\x91','\x83','\x91','\x84',
'\x91','\x85','\x91','\x86','\x91','\x87','\x91','\x88',
'\x91','\x89','\x91','\x8a','\x91','\x8b','\x91','\x8c',
'\x91','\x8d','\x91','\x8e','\x91','\x8f','\x91','\x90',
'\x91','\x91','\x91','\x92','\x91','\x93','\x91','\x94',
'\x91','\x95','\x91','\x96','\x91','\x97','\x91','\x98',
'\x91','\x99','\x91','\x9a','\x91','\x9b','\x91','\x9c',
'\x91','\x9d','\x91','\x9e','\x91','\x9f','\x91','\xa0',
'\x91','\xa1','\x91','\xa2','\x91','\xa3','\x91','\xa4',
'\x91','\xa5','\x91','\xa6','\x91','\xa7','\x91','\xa8',
'\x91','\xa9','\x91','\xaa','\x91','\xae','\x91','\xaf',
'\x91','\xba','\x91','\xbb','\x91','\xbc','\x91','\xbd',
'\x91','\xbe','\x91','\xbf','\x92','\x80','\x92','\x81',
'\x92','\x82','\x92','\x83','\x92','\x84','\x92','\x85'};
2016-09-15 19:31:25 +02:00
int cols = core->print->cols;
if (cols < 1) {
cols = 1;
}
for (i = 0; i < len; i += cols) {
r_print_addr (core->print, core->offset + i);
2016-09-15 19:31:25 +02:00
for (j = i; j < i + cols; j += 1) {
ut8 *p = (ut8*)core->block + j;
if (j < len) {
r_cons_printf ("\xf0\x9f%c%c ", emoji[*p * 2], emoji[*p * 2 + 1]);
2016-09-15 19:31:25 +02:00
} else {
r_cons_print (" ");
}
}
2016-09-15 19:31:25 +02:00
r_cons_print (" ");
for (j = i; j < len && j < i + cols; j += 1) {
ut8 *p = (ut8*)core->block + j;
r_print_byte (core->print, "%c", j, *p);
}
2016-09-15 19:31:25 +02:00
r_cons_newline ();
}
}
break;
case 'l':
len = core->print->cols * len;
2016-09-15 19:31:25 +02:00
/* faltrhou */
2016-05-17 00:37:47 +02:00
default:
if (l) {
2015-05-10 22:40:02 +02:00
ut64 from = r_config_get_i (core->config, "diff.from");
ut64 to = r_config_get_i (core->config, "diff.to");
if (from == to && !from) {
if (!r_core_block_size (core, len)) {
len = core->blocksize;
}
r_print_hexdump (core->print, core->offset,
core->block, len, 16, 1);
2015-05-10 22:40:02 +02:00
} else {
r_core_print_cmp (core, from, to);
2015-05-10 22:40:02 +02:00
}
core->num->value = len;
2016-11-04 01:30:35 +01:00
}
break;
}
r_cons_break_pop ();
2012-02-27 02:40:27 +01:00
break;
2016-05-17 00:37:47 +02:00
case '2': // "p2"
if (l) {
if (input[1] == '?') {
2016-05-17 00:37:47 +02:00
r_cons_printf ("|Usage: p2 [number of bytes representing tiles]\n"
"NOTE: Only full tiles will be printed\n");
} else {
r_print_2bpp_tiles (core->print, core->block, len / 16);
}
2016-05-17 00:37:47 +02:00
}
break;
2012-02-27 02:40:27 +01:00
case '6':
if (l) {
int malen = (core->blocksize*4)+1;
ut8 *buf = malloc (malen);
if (!buf) {
break;
}
2012-02-27 02:40:27 +01:00
memset (buf, 0, malen);
switch (input[1]) {
case 'd':
if (input[2] == '?') {
r_cons_printf ("|Usage: p6d [len] base 64 decode\n");
} else if (r_base64_decode (buf, (const char *)core->block, len)) {
r_cons_println ((const char*)buf);
} else {
eprintf ("r_base64_decode: invalid stream\n");
}
2012-02-27 02:40:27 +01:00
break;
case 'e':
if (input[2] == '?') {
r_cons_printf ("|Usage: p6e [len] base 64 encode\n");
break;
} else {
len = len > core->blocksize ? core->blocksize : len;
r_base64_encode ((char *)buf, core->block, len);
r_cons_println ((const char*)buf);
}
break;
case '?':
default:
r_cons_printf ("|Usage: p6[ed] [len] base 64 encode/decode\n");
break;
2012-02-27 02:40:27 +01:00
}
free (buf);
}
break;
2015-11-05 12:36:59 +01:00
case '8': // "p8"
if (input[1] == '?') {
2016-05-03 03:29:43 +02:00
r_cons_printf("|Usage: p8[fj] [len] 8bit hexpair list of bytes (see pcj)\n");
} else if (l) {
if (!r_core_block_size (core, len)) {
len = core->blocksize;
}
2016-05-17 00:37:47 +02:00
if (input[1] == 'j') {
r_core_cmdf (core, "pcj %s", input+2);
2016-05-17 00:37:47 +02:00
} else if (input[1] == 'f') {
r_core_cmdf (core, "p8 $F @ $B");
} else {
r_print_bytes (core->print, core->block, len, "%02x");
}
2016-05-17 00:37:47 +02:00
}
2012-02-27 02:40:27 +01:00
break;
2015-11-05 12:36:59 +01:00
case 'f': // "pf"
cmd_print_format (core, input, len);
2012-02-27 02:40:27 +01:00
break;
2016-05-17 00:37:47 +02:00
case 'k': // "pk"
if (input[1] == '?') {
r_cons_printf ("|Usage: pk [len] print key in randomart\n");
2016-12-09 18:07:06 +01:00
r_cons_printf ("|Usage: pkill [process-name]\n");
} else if (!strncmp (input, "kill", 4)) {
RListIter *iter;
RDebugPid *pid;
const char *arg = strchr (input, ' ');
RList *pids = (core->dbg->h && core->dbg->h->pids)
? core->dbg->h->pids (core->dbg, 0): NULL;
if (arg && *++arg) {
r_list_foreach (pids, iter, pid) {
if (strstr (pid->path, arg)) {
r_cons_printf ("dk 9 %d\n", pid->pid);
}
// r_debug_kill (core->dbg, pid->pid, pid->pid, 9); // kill -9
}
}
r_list_free (pids);
2016-05-17 00:37:47 +02:00
} else if (l > 0) {
len = len > core->blocksize ? core->blocksize : len;
char *s = r_print_randomart (core->block, len, core->offset);
r_cons_println (s);
free (s);
}
break;
2016-05-17 00:37:47 +02:00
case 'K': // "pK"
if (input[1] == '?') {
r_cons_printf ("|Usage: pK [len] print key in randomart mosaic\n");
2016-05-17 00:37:47 +02:00
} else if (l > 0) {
len = len > core->blocksize ? core->blocksize : len;
int w, h;
RConsCanvas *c;
w = r_cons_get_size (&h);
ut64 offset0 = core->offset;
int cols = (w/20);
int rows = (h/12);
int i, j;
char *s;
if (rows < 1) {
rows = 1;
}
c = r_cons_canvas_new (w, rows * 11);
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
r_cons_canvas_gotoxy (c, j*20, i*11);
core->offset += len;
r_core_read_at (core, core->offset, core->block, len);
s = r_print_randomart (core->block, len, core->offset);
r_cons_canvas_write (c, s);
free (s);
}
}
r_cons_canvas_print (c);
r_cons_canvas_free (c);
r_core_read_at (core, offset0, core->block, len);
core->offset = offset0;
r_cons_printf("\n");
}
break;
2015-03-31 00:49:45 +02:00
case 'n': // easter
eprintf ("easter egg license has expired\n");
2012-02-27 02:40:27 +01:00
break;
case 't':
switch (input[1]) {
case ' ':
case '\0':
2015-06-28 12:33:57 +02:00
//len must be multiple of 4 since r_mem_copyendian move data in fours - sizeof(ut32)
if (len < sizeof (ut32)) {
eprintf ("You should change the block size: b %d\n", (int)sizeof (ut32));
}
if (len % sizeof (ut32)) {
len = len - (len % sizeof (ut32));
}
for (l = 0; l < len; l += sizeof (ut32)) {
r_print_date_unix (core->print, core->block + l, sizeof (ut32));
}
break;
case 'd':
2015-06-28 12:22:19 +02:00
//len must be multiple of 4 since r_print_date_dos read buf+3
//if block size is 1 or 5 for example it reads beyond the buffer
if (len < sizeof (ut32)) {
eprintf ("You should change the block size: b %d\n", (int)sizeof (ut32));
}
if (len % sizeof (ut32)) {
len = len - (len % sizeof (ut32));
}
for (l = 0; l < len; l += sizeof (ut32)) {
r_print_date_dos (core->print, core->block + l, sizeof (ut32));
}
break;
case 'n':
if (len < sizeof (ut64)) {
eprintf ("You should change the block size: b %d\n", (int)sizeof (ut64));
}
if (len % sizeof (ut64)) {
len = len - (len % sizeof (ut64));
}
for (l = 0; l < len; l += sizeof (ut64)) {
r_print_date_w32 (core->print, core->block + l, sizeof (ut64));
}
break;
2014-08-17 02:42:17 +02:00
case '?':{
const char* help_msg[] = {
"Usage: pt", "[dn]", "print timestamps",
"pt", "", "print unix time (32 bit `cfg.bigendian`)",
"ptd","", "print dos time (32 bit `cfg.bigendian`)",
"ptn","", "print ntfs time (64 bit `cfg.bigendian`)",
2014-08-17 02:42:17 +02:00
NULL};
r_core_cmd_help (core, help_msg);
}
2012-02-27 02:40:27 +01:00
break;
}
break;
2016-05-17 00:37:47 +02:00
case 'z': // "pz"
2012-02-27 02:40:27 +01:00
if (input[1]=='?') {
2014-08-17 17:30:26 +02:00
const char *help_msg[] = {
"Usage: pz [len]", "", "print zoomed blocks (filesize/N)",
2014-09-12 19:25:42 +02:00
"e ","zoom.maxsz","max size of block",
"e ","zoom.from","start address",
"e ","zoom.to","end address",
"e ","zoom.byte","specify how to calculate each byte",
"pzp","","number of printable chars",
"pzf","","count of flags in block",
"pzs","","strings in range",
"pz0","","number of bytes with value '0'",
"pzF","","number of bytes with value 0xFF",
"pze","","calculate entropy and expand to 0-255 range",
"pzh","","head (first byte value); This is the default mode",
2014-08-17 17:30:26 +02:00
//"WARNING: On big files, use 'zoom.byte=h' or restrict ranges\n");
NULL};
r_core_cmd_help (core, help_msg);
2012-02-27 02:40:27 +01:00
} else {
char *oldzoom = NULL;
ut64 maxsize = r_config_get_i (core->config, "zoom.maxsz");
ut64 from, to;
int oldva = core->io->va;
int do_zoom = 1;
2012-02-27 02:40:27 +01:00
core->io->va = 0;
2015-09-04 15:12:42 +02:00
from = 0;
2012-02-27 02:40:27 +01:00
to = r_io_size (core->io);
from = r_config_get_i (core->config, "zoom.from");
to = r_config_get_i (core->config, "zoom.to");
if (input[1] && input[1] != ' ') {
2012-02-27 02:40:27 +01:00
oldzoom = strdup (r_config_get (core->config, "zoom.byte"));
if (!r_config_set (core->config, "zoom.byte", input+1)) {
eprintf ("Invalid zoom.byte mode (%s)\n", input+1);
2015-04-18 00:03:22 +02:00
R_FREE (oldzoom);
do_zoom = 0;
2012-02-27 02:40:27 +01:00
}
}
2016-05-17 00:37:47 +02:00
if (do_zoom && l > 0) {
r_print_zoom (core->print, core, printzoomcallback,
from, to, core->blocksize, (int)maxsize);
}
2012-02-27 02:40:27 +01:00
if (oldzoom) {
r_config_set (core->config, "zoom.byte", oldzoom);
2015-04-18 00:03:22 +02:00
R_FREE (oldzoom);
2012-02-27 02:40:27 +01:00
}
2016-05-17 00:37:47 +02:00
if (oldva) {
2012-02-27 02:40:27 +01:00
core->io->va = oldva;
2016-05-17 00:37:47 +02:00
}
2012-02-27 02:40:27 +01:00
}
break;
default: {
const char* help_msg[] = {
"Usage:", "p[=68abcdDfiImrstuxz] [arg|len] [@addr]", "",
"p=","[?][bep] [blks] [len] [blk]","show entropy/printable chars/chars bars",
"p2"," [len]", "8x8 2bpp-tiles",
"p3"," [file]", "print stereogram (3D)",
"p6","[de] [len]", "base64 decode/encode",
"p8","[?][j] [len]","8bit hexpair list of bytes",
"pa","[edD] [arg]", "pa:assemble pa[dD]:disasm or pae: esil from hexpairs",
2015-03-04 01:08:47 +01:00
"pA","[n_ops]", "show n_ops address and type",
"p","[b|B|xb] [len] ([skip])", "bindump N bits skipping M",
"pb","[?] [n]","bitstream of N bits",
"pB","[?] [n]","bitstream of N bytes",
2016-11-26 14:57:21 +01:00
"pc","[?][p] [len]","output C (or python) format",
"pC","[d] [rows]","print disassembly in columns (see hex.cols and pdi)",
"pd","[?] [sz] [a] [b]","disassemble N opcodes (pd) or N bytes (pD)",
"pf","[?][.nam] [fmt]","print formatted data (pf.name, pf.name $<expr>)",
"ph","[?][=|hash] ([len])","calculate hash for a block",
"p","[iI][df] [len]", "print N ops/bytes (f=func) (see pi? and pdi)",
"pm","[?] [magic]","print libmagic data (see pm? and /m?)",
2016-11-26 14:57:21 +01:00
"pr","[?][glx] [len]","print N raw bytes (in lines or hexblocks, 'g'unzip)",
"p","[kK] [len]","print key in randomart (K is for mosaic)",
2016-11-26 14:57:21 +01:00
"ps","[?][pwz] [len]","print pascal/wide/zero-terminated strings",
"pt","[?][dn] [len]","print different timestamps",
"pu","[?][w] [len]","print N url encoded bytes (w=wide)",
"pv","[?][jh] [mode]","show variable/pointer/value in memory",
2016-11-26 14:57:21 +01:00
"p-","[?][jh] [mode]","bar|json|histogram blocks (mode: e?search.in)",
"px","[?][owq] [len]","hexdump of N bytes (o=octal, w=32bit, q=64bit)",
"pz","[?] [len]","print zoom view (see pz? for help)",
"pwd","","display current working directory",
NULL
2015-05-10 22:40:02 +02:00
};
r_core_cmd_help (core, help_msg);
}
break;
2012-02-27 02:40:27 +01:00
}
beach:
if (tmpseek != UT64_MAX) {
r_core_seek (core, tmpseek, SEEK_SET);
r_core_block_read (core);
}
if (tbs != core->blocksize) {
r_core_block_size (core, tbs);
}
return ret;
2012-02-27 02:40:27 +01:00
}
static int cmd_hexdump(void *data, const char *input) {
return cmd_print (data, input-1);
}
2015-01-12 00:11:06 +01:00
static int lenof (ut64 off, int two) {
char buf[64];
buf[0] = 0;
if (two) snprintf (buf, sizeof (buf), "+0x%"PFMT64x, off);
else snprintf (buf, sizeof (buf), "0x%08"PFMT64x, off);
return strlen (buf);
}
2013-11-04 04:07:58 +01:00
// TODO : move to r_util? .. depends on r_cons...
2016-11-01 23:30:36 +01:00
// XXX: dupe of r_print_addr
R_API void r_print_offset(RPrint *p, ut64 off, int invert, int offseg, int offdec, int delta, const char *label) {
char space[32] = { 0 };
const char *white;
bool show_color = p->flags & R_PRINT_FLAGS_COLOR;
if (show_color) {
const char *k = r_cons_singleton ()->pal.offset; // TODO etooslow. must cache
if (invert) {
2015-09-14 12:35:38 +02:00
r_cons_invert (true, true);
}
2015-01-12 00:11:06 +01:00
if (offseg) {
ut32 s, a;
a = off & 0xffff;
s = (off - a) >> 4;
2016-11-01 23:30:36 +01:00
if (offdec) {
snprintf (space, sizeof (space), "%d:%d", s & 0xffff, a & 0xffff);
white = r_str_pad (' ', 9 - strlen (space));
r_cons_printf ("%s%s%s"Color_RESET, k, white, space);
} else {
r_cons_printf ("%s%04x:%04x"Color_RESET,
k, s & 0xFFFF, a & 0xFFFF);
}
2015-01-12 00:11:06 +01:00
} else {
int sz = lenof (off, 0);
int sz2 = lenof (delta, 1);
if (delta > 0 || label) {
if (label) {
const int label_padding = 10;
if (delta > 0) {
2016-11-01 23:30:36 +01:00
if (offdec) {
const char *pad = r_str_pad (' ', sz - sz2 + label_padding);
r_cons_printf ("%s%s"Color_RESET"+%d%s", k, label, delta, pad);
} else {
const char *pad = r_str_pad (' ', sz - sz2 + label_padding);
r_cons_printf ("%s%s"Color_RESET"+0x%x%s", k, label, delta, pad);
}
} else {
const char *pad = r_str_pad (' ', sz + label_padding);
r_cons_printf ("%s%s"Color_RESET"%s", k, label, pad);
}
} else {
const char *pad = r_str_pad (' ', sz - sz2);
2016-11-01 23:30:36 +01:00
if (offdec) {
r_cons_printf ("%s+%d"Color_RESET, pad, delta);
} else {
r_cons_printf ("%s+0x%x"Color_RESET, pad, delta);
}
}
} else {
2016-11-01 23:30:36 +01:00
if (offdec) {
snprintf (space, sizeof (space), "%"PFMT64d, off);
white = r_str_pad (' ', 10 - strlen (space));
r_cons_printf ("%s%s%s"Color_RESET, k, white, space, off);
} else {
r_cons_printf ("%s0x%08"PFMT64x""Color_RESET, k, off);
}
}
2015-01-12 00:11:06 +01:00
}
r_cons_print (" ");
2013-11-04 04:07:58 +01:00
} else {
2015-01-12 00:11:06 +01:00
if (offseg) {
2013-11-04 04:07:58 +01:00
ut32 s, a;
a = off & 0xffff;
s = (off - a) >> 4;
2016-11-01 23:30:36 +01:00
if (offdec) {
snprintf (space, sizeof (space), "%d:%d", s & 0xffff, a & 0xffff);
white = r_str_pad (' ', 9 - strlen (space));
r_cons_printf ("%s%s"Color_RESET, white, space);
} else {
r_cons_printf ("%04x:%04x", s & 0xFFFF, a & 0xFFFF);
}
2013-11-04 04:07:58 +01:00
} else {
2015-01-12 00:11:06 +01:00
int sz = lenof (off, 0);
int sz2 = lenof (delta, 1);
const char *pad = r_str_pad (' ', sz - 5 - sz2 - 3);
2016-11-01 23:30:36 +01:00
if (delta > 0) {
if (offdec) {
r_cons_printf ("%s+%d"Color_RESET, pad, delta);
} else {
r_cons_printf ("%s+0x%x"Color_RESET, pad, delta);
}
} else {
2016-11-01 23:30:36 +01:00
if (offdec) {
snprintf (space, sizeof (space), "%"PFMT64d, off);
white = r_str_pad (' ', 10 - strlen (space));
r_cons_printf ("%s%s", white, space);
} else {
r_cons_printf ("0x%08"PFMT64x" ", off);
}
}
2013-11-04 04:07:58 +01:00
}
}
}