Skip to content

Commit

Permalink
Convert dd commands to rzshell (#3049)
Browse files Browse the repository at this point in the history
* Convert `dd` command to rzshell
* Remove unimplemented `dH` command
* Remove `ddt` command
* Move `date` command to the "shell" category
  • Loading branch information
XVilka authored Sep 29, 2022
1 parent 69d7c72 commit 4bf6b67
Show file tree
Hide file tree
Showing 10 changed files with 358 additions and 153 deletions.
45 changes: 45 additions & 0 deletions librz/core/cdebug.c
Original file line number Diff line number Diff line change
Expand Up @@ -1120,3 +1120,48 @@ RZ_IPI bool rz_core_debug_thread_print(RzDebug *dbg, int pid, RzCmdStateOutput *
rz_list_free(list);
return true;
}

RZ_IPI bool rz_core_debug_desc_print(RzDebug *dbg, RzCmdStateOutput *state) {
if (!dbg || !dbg->cur || !dbg->cur->desc.list) {
return false;
}
RzList *list = dbg->cur->desc.list(dbg->pid);
if (!list) {
return false;
}
RzListIter *iter;
RzDebugDesc *p;
char desctype[2];
rz_cmd_state_output_array_start(state);
rz_cmd_state_output_set_columnsf(state, "ddsss", "fd",
"offset", "perms", "type", "path");
rz_list_foreach (list, iter, p) {
rz_strf(desctype, "%c", p->type);
switch (state->mode) {
case RZ_OUTPUT_MODE_JSON:
pj_o(state->d.pj);
pj_ki(state->d.pj, "fd", p->fd);
pj_ki(state->d.pj, "offset", p->off);
pj_ks(state->d.pj, "perms", rz_str_rwx_i(p->perm));
pj_ks(state->d.pj, "type", desctype);
pj_ks(state->d.pj, "path", p->path);
pj_end(state->d.pj);
break;
case RZ_OUTPUT_MODE_TABLE:
rz_table_add_rowf(state->d.t, "ddsss", p->fd, p->off,
rz_str_rwx_i(p->perm), desctype, p->path);
break;
case RZ_OUTPUT_MODE_STANDARD:
rz_cons_printf("%i 0x%" PFMT64x " %s %s %s\n", p->fd, p->off,
rz_str_rwx_i(p->perm),
desctype, p->path);
break;
default:
rz_warn_if_reached();
break;
}
}
rz_cmd_state_output_array_end(state);
rz_list_free(list);
return true;
}
191 changes: 69 additions & 122 deletions librz/core/cmd/cmd_debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ static const char *help_msg_d[] = {
"dd", "[?]", "File descriptors (!fd in r1)",
"de", "[-sc] [perm] [rm] [e]", "Debug with ESIL (see de?)",
"dg", " <file>", "Generate a core-file (WIP)",
"dH", " [handler]", "Transplant process to a new handler",
"di", "[?]", "Show debugger backend information (See dh)",
"dk", "[?]", "List, send, get, set, signal handlers of child",
"dL", "[?]", "List or set debugger handler",
Expand Down Expand Up @@ -69,19 +68,6 @@ static const char *help_msg_dcu[] = {
NULL
};

static const char *help_msg_dd[] = {
"Usage: dd", "", "Descriptors commands",
"dd", "", "List file descriptors",
"dd", " <file>", "Open and map that file into the UI",
"dd-", "<fd>", "Close stdout fd",
"dd*", "", "List file descriptors (in rizin commands)",
"dds", " <fd> <off>", "Seek given fd)",
"ddd", " <fd1> <fd2>", "Dup2 from fd1 to fd2",
"ddr", " <fd> <size>", "Read N bytes from fd",
"ddw", " <fd> <hexpairs>", "Write N bytes to fd",
NULL
};

static const char *help_msg_de[] = {
"Usage:", "de", "[-sc] [perm] [rm] [expr]",
"de", "", "List esil watchpoints",
Expand Down Expand Up @@ -2061,115 +2047,7 @@ RZ_IPI int rz_cmd_debug(void *data, const char *input) {
RzCore *core = (RzCore *)data;
int follow = 0;

if (!strncmp(input, "ate", 3)) {
char *now = rz_time_date_now_to_string();
rz_cons_printf("%s\n", now);
free(now);
return 0;
}

switch (input[0]) {
case 'd': // "ddd"
switch (input[1]) {
case '\0': // "ddd"
rz_debug_desc_list(core->dbg, 0);
break;
case '*': // "dtd*"
rz_debug_desc_list(core->dbg, 1);
break;
case 's': // "dtds"
{
ut64 off = UT64_MAX;
int fd = atoi(input + 2);
char *str = strchr(input + 2, ' ');
if (str)
off = rz_num_math(core->num, str + 1);
if (off == UT64_MAX || !rz_debug_desc_seek(core->dbg, fd, off)) {
RzBuffer *buf = rz_core_syscallf(core, "lseek", "%d, 0x%" PFMT64x ", %d", fd, off, 0);
consumeBuffer(buf, "dx ", "Cannot seek");
}
} break;
case 't': { // "ddt" <ttypath>
RzBuffer *buf = rz_core_syscall(core, "close", 0);
consumeBuffer(buf, "dx ", "Cannot close");
break;
}
case 'd': // "ddd"
{
ut64 newfd = UT64_MAX;
int fd = atoi(input + 2);
char *str = strchr(input + 3, ' ');
if (str)
newfd = rz_num_math(core->num, str + 1);
if (newfd == UT64_MAX || !rz_debug_desc_dup(core->dbg, fd, newfd)) {
RzBuffer *buf = rz_core_syscallf(core, "dup2", "%d, %d", fd, (int)newfd);
if (buf) {
consumeBuffer(buf, "dx ", NULL);
} else {
RZ_LOG_ERROR("core: Cannot dup %d %d\n", fd, (int)newfd);
}
}
} break;
case 'r': {
ut64 off = UT64_MAX;
ut64 len = UT64_MAX;
int fd = atoi(input + 2);
char *str = strchr(input + 2, ' ');
if (str)
off = rz_num_math(core->num, str + 1);
if (str)
str = strchr(str + 1, ' ');
if (str)
len = rz_num_math(core->num, str + 1);
if (len == UT64_MAX || off == UT64_MAX ||
!rz_debug_desc_read(core->dbg, fd, off, len)) {
consumeBuffer(rz_core_syscallf(core, "read", "%d, 0x%" PFMT64x ", %d",
fd, off, (int)len),
"dx ", "Cannot read");
}
} break;
case 'w': {
ut64 off = UT64_MAX;
ut64 len = UT64_MAX;
int fd = atoi(input + 2);
char *str = strchr(input + 2, ' ');
if (str)
off = rz_num_math(core->num, str + 1);
if (str)
str = strchr(str + 1, ' ');
if (str)
len = rz_num_math(core->num, str + 1);
if (len == UT64_MAX || off == UT64_MAX ||
!rz_debug_desc_write(core->dbg, fd, off, len)) {
RzBuffer *buf = rz_core_syscallf(core, "write", "%d, 0x%" PFMT64x ", %d", fd, off, (int)len);
consumeBuffer(buf, "dx ", "Cannot write");
}
break;
}
case '-': // "dd-"
{
int fd = atoi(input + 2);
// rz_core_cmdf (core, "dxs close %d", (int)rz_num_math ( core->num, input + 2));
RzBuffer *buf = rz_core_syscallf(core, "close", "%d", fd);
consumeBuffer(buf, "dx ", "Cannot close");
break;
}
case ' ': // "dd"
{
// TODO: handle read, readwrite, append
RzBuffer *buf = rz_core_syscallf(core, "open", "%s, %d, %d", input + 2, 2, 0644);
consumeBuffer(buf, "dx ", "Cannot open");
break;
}
case '?':
default:
rz_core_cmd_help(core, help_msg_dd);
break;
}
break;
case 'H': // "dH"
RZ_LOG_ERROR("core: dH has not been implemented\n");
break;
case 'e': // "de"
rz_core_debug_esil(core, input + 1);
break;
Expand Down Expand Up @@ -3457,3 +3335,72 @@ RZ_IPI RzCmdStatus rz_cmd_debug_inject_syscall_handler(RzCore *core, int argc, c
free(str);
return bool2status(result);
}

RZ_IPI RzCmdStatus rz_cmd_debug_descriptor_open_handler(RzCore *core, int argc, const char **argv) {
int fd = rz_num_math(core->num, argv[1]);
RzBuffer *buf = rz_core_syscallf(core, "open", "%d, %d, %d", fd, 2, 0644);
consumeBuffer(buf, "dx ", "Cannot open");
return RZ_CMD_STATUS_OK;
}

RZ_IPI RzCmdStatus rz_cmd_debug_descriptor_close_handler(RzCore *core, int argc, const char **argv) {
int fd = rz_num_math(core->num, argv[1]);
RzBuffer *buf = rz_core_syscallf(core, "close", "%d", fd);
consumeBuffer(buf, "dx ", "Cannot close");
return RZ_CMD_STATUS_OK;
}

RZ_IPI RzCmdStatus rz_cmd_debug_descriptor_list_handler(RzCore *core, int argc, const char **argv, RzCmdStateOutput *state) {
return bool2status(rz_core_debug_desc_print(core->dbg, state));
}

RZ_IPI RzCmdStatus rz_cmd_debug_descriptor_seek_handler(RzCore *core, int argc, const char **argv) {
int fd = rz_num_math(core->num, argv[1]);
ut64 off = rz_num_math(core->num, argv[2]);
if (off == UT64_MAX || !rz_debug_desc_seek(core->dbg, fd, off)) {
RzBuffer *buf = rz_core_syscallf(core, "lseek", "%d, 0x%" PFMT64x ", %d", fd, off, 0);
consumeBuffer(buf, "dx ", "Cannot seek");
}
return RZ_CMD_STATUS_OK;
}

RZ_IPI RzCmdStatus rz_cmd_debug_descriptor_dup_handler(RzCore *core, int argc, const char **argv) {
int fd_src = rz_num_math(core->num, argv[1]);
int fd_dst = rz_num_math(core->num, argv[2]);
if (fd_dst == UT64_MAX || !rz_debug_desc_dup(core->dbg, fd_src, fd_dst)) {
RzBuffer *buf = rz_core_syscallf(core, "dup2", "%d, %d", fd_src, fd_dst);
if (!buf) {
RZ_LOG_ERROR("core: Cannot dup %d %d\n", fd_src, fd_dst);
return RZ_CMD_STATUS_ERROR;
}
consumeBuffer(buf, "dx ", NULL);
}
return RZ_CMD_STATUS_OK;
}

RZ_IPI RzCmdStatus rz_cmd_debug_descriptor_read_handler(RzCore *core, int argc, const char **argv) {
int fd = rz_num_math(core->num, argv[1]);
ut64 off = rz_num_math(core->num, argv[2]);
ut64 len = rz_num_math(core->num, argv[3]);
if (len == UT64_MAX || off == UT64_MAX ||
!rz_debug_desc_read(core->dbg, fd, off, len)) {
consumeBuffer(rz_core_syscallf(core, "read", "%d, 0x%" PFMT64x ", %d",
fd, off, (int)len),
"dx ", "Cannot read");
return RZ_CMD_STATUS_ERROR;
}
return RZ_CMD_STATUS_OK;
}

RZ_IPI RzCmdStatus rz_cmd_debug_descriptor_write_handler(RzCore *core, int argc, const char **argv) {
int fd = rz_num_math(core->num, argv[1]);
ut64 off = rz_num_math(core->num, argv[2]);
ut64 len = rz_num_math(core->num, argv[3]);
if (len == UT64_MAX || off == UT64_MAX ||
!rz_debug_desc_write(core->dbg, fd, off, len)) {
RzBuffer *buf = rz_core_syscallf(core, "write", "%d, 0x%" PFMT64x ", %d", fd, off, (int)len);
consumeBuffer(buf, "dx ", "Cannot write");
return RZ_CMD_STATUS_ERROR;
}
return RZ_CMD_STATUS_OK;
}
8 changes: 8 additions & 0 deletions librz/core/cmd/cmd_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,11 @@ RZ_IPI RzCmdStatus rz_cmd_shell_diff_handler(RzCore *core, int argc, const char
free(b);
return RZ_CMD_STATUS_OK;
}

// date
RZ_IPI RzCmdStatus rz_cmd_shell_date_handler(RzCore *core, int argc, const char **argv) {
char *now = rz_time_date_now_to_string();
rz_cons_printf("%s\n", now);
free(now);
return RZ_CMD_STATUS_OK;
}
61 changes: 61 additions & 0 deletions librz/core/cmd_descs/cmd_debug.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,67 @@ commands:
summary: Debug continue until
cname: cmd_debug_continue_until
type: RZ_CMD_DESC_TYPE_OLDINPUT
- name: dd
summary: Debug file descriptors commands
subcommands:
- name: dd
summary: Open and map <file> given the path
cname: cmd_debug_descriptor_open
args:
- name: file
type: RZ_CMD_ARG_TYPE_FILE
- name: dd-
summary: Close the <fd> file descriptor
cname: cmd_debug_descriptor_close
args:
- name: fd
type: RZ_CMD_ARG_TYPE_NUM
- name: ddl
summary: List all file descriptors
cname: cmd_debug_descriptor_list
args: []
type: RZ_CMD_DESC_TYPE_ARGV_STATE
default_mode: RZ_OUTPUT_MODE_STANDARD
modes:
- RZ_OUTPUT_MODE_STANDARD
- RZ_OUTPUT_MODE_JSON
- RZ_OUTPUT_MODE_TABLE
- name: dds
summary: Seek given <fd> to the <offset>
cname: cmd_debug_descriptor_seek
args:
- name: fd
type: RZ_CMD_ARG_TYPE_NUM
- name: offset
type: RZ_CMD_ARG_TYPE_NUM
- name: ddd
summary: Duplicate <fd_src> to <fd_dst>
cname: cmd_debug_descriptor_dup
args:
- name: fd_src
type: RZ_CMD_ARG_TYPE_NUM
- name: fd_dst
type: RZ_CMD_ARG_TYPE_NUM
- name: ddr
summary: Read <len> bytes from <fd> file at <offset>
cname: cmd_debug_descriptor_read
args:
- name: fd
type: RZ_CMD_ARG_TYPE_NUM
- name: offset
type: RZ_CMD_ARG_TYPE_NUM
- name: len
type: RZ_CMD_ARG_TYPE_NUM
- name: ddw
summary: Write <len> bytes to <fd> file at <offset>
cname: cmd_debug_descriptor_write
args:
- name: fd
type: RZ_CMD_ARG_TYPE_NUM
- name: offset
type: RZ_CMD_ARG_TYPE_NUM
- name: len
type: RZ_CMD_ARG_TYPE_NUM
- name: do
summary: Debug (re)open commands
subcommands:
Expand Down
Loading

0 comments on commit 4bf6b67

Please sign in to comment.