2017-04-10 15:32:07 +00:00
|
|
|
/* radare - LGPL - Copyright 2010-2017 pancake */
|
2010-07-21 23:14:13 +00:00
|
|
|
|
|
|
|
#include <r_io.h>
|
|
|
|
#include <r_lib.h>
|
|
|
|
#include <r_socket.h>
|
|
|
|
#include <r_util.h>
|
2017-07-20 09:57:29 +00:00
|
|
|
#include <ctype.h>
|
2013-06-09 00:55:03 +00:00
|
|
|
#define IRAPI static inline
|
2014-02-21 14:20:41 +00:00
|
|
|
#include <libgdbr.h>
|
2017-05-07 09:53:17 +00:00
|
|
|
#include <gdbclient/commands.h>
|
2014-02-21 14:20:41 +00:00
|
|
|
|
2011-01-20 21:52:16 +00:00
|
|
|
typedef struct {
|
2014-02-21 14:20:41 +00:00
|
|
|
libgdbr_t desc;
|
2011-01-20 21:52:16 +00:00
|
|
|
} RIOGdb;
|
2014-02-21 14:20:41 +00:00
|
|
|
|
|
|
|
static libgdbr_t *desc = NULL;
|
2015-09-07 03:35:43 +00:00
|
|
|
static RIODesc *riogdb = NULL;
|
2014-02-21 14:20:41 +00:00
|
|
|
|
2016-06-19 22:29:41 +00:00
|
|
|
static bool __plugin_open(RIO *io, const char *file, bool many) {
|
2014-02-18 02:03:13 +00:00
|
|
|
return (!strncmp (file, "gdb://", 6));
|
2010-07-21 23:14:13 +00:00
|
|
|
}
|
|
|
|
|
2014-02-21 14:20:41 +00:00
|
|
|
static int debug_gdb_read_at(ut8 *buf, int sz, ut64 addr) {
|
2017-06-11 00:12:44 +00:00
|
|
|
ut32 size_max;
|
|
|
|
ut32 packets;
|
|
|
|
ut32 last;
|
2014-02-21 14:20:41 +00:00
|
|
|
ut32 x;
|
2017-06-11 00:12:44 +00:00
|
|
|
int ret = 0;
|
|
|
|
if (sz < 1 || addr >= UT64_MAX || !desc) {
|
2016-09-14 11:11:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2017-06-11 00:12:44 +00:00
|
|
|
size_max = desc->data_max / 2;
|
|
|
|
packets = sz / size_max;
|
|
|
|
last = sz % size_max;
|
2014-02-21 14:20:41 +00:00
|
|
|
for (x = 0; x < packets; x++) {
|
2017-06-11 00:12:44 +00:00
|
|
|
if (gdbr_read_memory (desc, addr + (x * size_max), size_max) < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2016-09-14 11:11:04 +00:00
|
|
|
memcpy ((buf + (x * size_max)), desc->data + (x * size_max), R_MIN (sz, size_max));
|
2017-06-11 00:12:44 +00:00
|
|
|
ret += desc->data_len;
|
2014-02-21 14:20:41 +00:00
|
|
|
}
|
|
|
|
if (last) {
|
2017-06-11 00:12:44 +00:00
|
|
|
if (gdbr_read_memory (desc, addr + x * size_max, last) < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2016-09-14 11:11:04 +00:00
|
|
|
memcpy ((buf + x * size_max), desc->data + (x * size_max), last);
|
2017-06-11 00:12:44 +00:00
|
|
|
ret += desc->data_len;
|
2014-02-21 14:20:41 +00:00
|
|
|
}
|
2017-06-11 00:12:44 +00:00
|
|
|
return ret;
|
2014-02-21 14:20:41 +00:00
|
|
|
}
|
|
|
|
|
2014-04-28 09:37:48 +00:00
|
|
|
static int debug_gdb_write_at(const ut8 *buf, int sz, ut64 addr) {
|
2017-06-11 00:12:44 +00:00
|
|
|
ut32 x, size_max;
|
|
|
|
ut32 packets;
|
|
|
|
ut32 last;
|
|
|
|
if (sz < 1 || addr >= UT64_MAX || !desc) {
|
2016-01-26 23:26:49 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2017-06-11 00:12:44 +00:00
|
|
|
size_max = desc->read_max;
|
|
|
|
packets = sz / size_max;
|
|
|
|
last = sz % size_max;
|
2014-02-21 14:20:41 +00:00
|
|
|
for (x = 0; x < packets; x++) {
|
2014-04-28 09:37:48 +00:00
|
|
|
gdbr_write_memory (desc, addr + x * size_max,
|
|
|
|
(const uint8_t*)(buf + x * size_max), size_max);
|
2014-02-21 14:20:41 +00:00
|
|
|
}
|
|
|
|
if (last) {
|
2014-04-28 09:37:48 +00:00
|
|
|
gdbr_write_memory (desc, addr + x * size_max,
|
|
|
|
(buf + x * size_max), last);
|
2014-02-21 14:20:41 +00:00
|
|
|
}
|
|
|
|
return sz;
|
|
|
|
}
|
|
|
|
|
2011-01-20 21:52:16 +00:00
|
|
|
static RIODesc *__open(RIO *io, const char *file, int rw, int mode) {
|
2016-01-26 22:36:02 +00:00
|
|
|
RIOGdb *riog;
|
2017-06-17 22:59:13 +00:00
|
|
|
char host[128], *port, *pid;
|
2017-06-24 15:18:31 +00:00
|
|
|
int i_port = -1;
|
|
|
|
bool isdev = false;
|
2016-01-26 22:36:02 +00:00
|
|
|
|
2014-01-24 03:05:35 +00:00
|
|
|
if (!__plugin_open (io, file, 0))
|
2011-01-20 21:52:16 +00:00
|
|
|
return NULL;
|
2016-01-26 22:36:02 +00:00
|
|
|
if (riogdb) {
|
|
|
|
// FIX: Don't allocate more than one gdb RIODesc
|
|
|
|
return riogdb;
|
|
|
|
}
|
2011-01-20 21:52:16 +00:00
|
|
|
strncpy (host, file+6, sizeof (host)-1);
|
2016-01-26 22:36:02 +00:00
|
|
|
host [sizeof (host)-1] = '\0';
|
2017-06-24 15:18:31 +00:00
|
|
|
if (host[0] == '/') {
|
|
|
|
isdev = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isdev) {
|
|
|
|
port = strchr (host, '@');
|
|
|
|
if (port) {
|
|
|
|
*port = '\0';
|
|
|
|
port++;
|
|
|
|
|
|
|
|
pid = strchr (port, ':');
|
|
|
|
} else {
|
|
|
|
pid = strchr (host, ':');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (r_sandbox_enable (0)) {
|
|
|
|
eprintf ("sandbox: Cannot use network\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
port = strchr (host , ':');
|
|
|
|
if (!port) {
|
|
|
|
eprintf ("Invalid debugger URI. Port missing?\nPlease use either\n"
|
|
|
|
" - gdb://host:port[/pid] for a network gdbserver.\n"
|
|
|
|
" - gdb:///dev/DEVICENAME[@speed][:pid] for a serial gdbserver.\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
*port = '\0';
|
|
|
|
port++;
|
|
|
|
|
|
|
|
pid = strchr (port, '/');
|
2010-07-21 23:14:13 +00:00
|
|
|
}
|
2017-06-17 22:59:13 +00:00
|
|
|
|
2017-06-24 15:18:31 +00:00
|
|
|
int i_pid = -1;
|
2017-06-17 22:59:13 +00:00
|
|
|
if (pid) {
|
|
|
|
*pid = 0;
|
|
|
|
pid++;
|
2017-06-24 15:18:31 +00:00
|
|
|
i_pid = atoi (pid);
|
2017-06-17 22:59:13 +00:00
|
|
|
}
|
2012-10-19 22:31:18 +00:00
|
|
|
|
2017-06-24 15:18:31 +00:00
|
|
|
if (port) {
|
|
|
|
i_port = atoi (port);
|
2012-10-19 22:31:18 +00:00
|
|
|
}
|
2017-06-24 15:18:31 +00:00
|
|
|
|
2016-01-26 22:36:02 +00:00
|
|
|
riog = R_NEW0 (RIOGdb);
|
2017-06-11 00:12:44 +00:00
|
|
|
gdbr_init (&riog->desc, false);
|
2017-06-17 22:59:13 +00:00
|
|
|
|
2016-01-26 22:36:02 +00:00
|
|
|
if (gdbr_connect (&riog->desc, host, i_port) == 0) {
|
2014-02-21 14:20:41 +00:00
|
|
|
desc = &riog->desc;
|
2017-06-17 22:59:13 +00:00
|
|
|
if (pid) { // FIXME this is here for now because RDebug's pid and libgdbr's aren't properly synced.
|
2017-06-19 17:43:55 +00:00
|
|
|
desc->pid = i_pid;
|
2017-06-17 22:59:13 +00:00
|
|
|
int ret = gdbr_attach (desc, i_pid);
|
2017-06-18 00:45:37 +00:00
|
|
|
if (ret < 0) {
|
2017-06-17 22:59:13 +00:00
|
|
|
eprintf ("gdbr: Failed to attach to PID %i\n", i_pid);
|
2017-06-18 00:45:37 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-07-04 17:44:09 +00:00
|
|
|
} else if ((i_pid = desc->pid) < 0) {
|
2017-06-23 06:19:21 +00:00
|
|
|
i_pid = -1;
|
2017-06-17 22:59:13 +00:00
|
|
|
}
|
2017-07-25 16:39:09 +00:00
|
|
|
// Get name
|
|
|
|
char *name = gdbr_exec_file_read (desc, i_pid);
|
|
|
|
if (name) {
|
|
|
|
file = name;
|
|
|
|
}
|
2017-06-23 06:19:21 +00:00
|
|
|
riogdb = r_io_desc_new (&r_io_plugin_gdb, i_pid, file, rw, mode, riog);
|
2017-07-25 16:39:09 +00:00
|
|
|
free (name);
|
2015-09-07 03:35:43 +00:00
|
|
|
return riogdb;
|
2011-01-20 21:52:16 +00:00
|
|
|
}
|
2011-04-06 10:26:19 +00:00
|
|
|
eprintf ("gdb.io.open: Cannot connect to host.\n");
|
2014-04-29 22:19:55 +00:00
|
|
|
free (riog);
|
2011-04-06 10:26:19 +00:00
|
|
|
return NULL;
|
2010-07-21 23:14:13 +00:00
|
|
|
}
|
|
|
|
|
2011-01-20 21:52:16 +00:00
|
|
|
static int __write(RIO *io, RIODesc *fd, const ut8 *buf, int count) {
|
2014-02-21 14:20:41 +00:00
|
|
|
ut64 addr = io->off;
|
|
|
|
if (!desc || !desc->data) return -1;
|
|
|
|
return debug_gdb_write_at(buf, count, addr);
|
2010-07-21 23:14:13 +00:00
|
|
|
}
|
|
|
|
|
2011-01-20 21:52:16 +00:00
|
|
|
static ut64 __lseek(RIO *io, RIODesc *fd, ut64 offset, int whence) {
|
2017-03-20 14:26:18 +00:00
|
|
|
switch (whence) {
|
|
|
|
case R_IO_SEEK_SET:
|
|
|
|
return offset;
|
|
|
|
case R_IO_SEEK_CUR:
|
|
|
|
return io->off + offset;
|
|
|
|
case R_IO_SEEK_END:
|
2017-04-10 15:32:07 +00:00
|
|
|
return UT64_MAX;
|
2017-03-20 14:26:18 +00:00
|
|
|
default:
|
|
|
|
return offset;
|
|
|
|
}
|
2010-07-21 23:14:13 +00:00
|
|
|
}
|
|
|
|
|
2011-01-20 21:52:16 +00:00
|
|
|
static int __read(RIO *io, RIODesc *fd, ut8 *buf, int count) {
|
2010-07-21 23:14:13 +00:00
|
|
|
memset (buf, 0xff, count);
|
2014-02-21 14:20:41 +00:00
|
|
|
ut64 addr = io->off;
|
2017-04-10 15:32:07 +00:00
|
|
|
if (!desc || !desc->data) {
|
|
|
|
return -1;
|
|
|
|
}
|
2014-02-21 14:20:41 +00:00
|
|
|
return debug_gdb_read_at(buf, count, addr);
|
2010-07-21 23:14:13 +00:00
|
|
|
}
|
|
|
|
|
2011-01-20 21:52:16 +00:00
|
|
|
static int __close(RIODesc *fd) {
|
2011-04-06 10:26:19 +00:00
|
|
|
// TODO
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-05-22 17:37:48 +00:00
|
|
|
int send_msg(libgdbr_t* g, const char* command);
|
2016-09-14 11:11:04 +00:00
|
|
|
int read_packet(libgdbr_t* instance);
|
|
|
|
|
2011-04-06 10:26:19 +00:00
|
|
|
static int __system(RIO *io, RIODesc *fd, const char *cmd) {
|
2016-01-26 22:36:02 +00:00
|
|
|
//printf("ptrace io command (%s)\n", cmd);
|
|
|
|
/* XXX ugly hack for testing purposes */
|
2016-09-14 11:11:04 +00:00
|
|
|
if (!cmd[0] || cmd[0] == '?' || !strcmp (cmd, "help")) {
|
2016-01-26 22:36:02 +00:00
|
|
|
eprintf ("Usage: =!cmd args\n"
|
2017-07-12 22:33:53 +00:00
|
|
|
" =!pid - show targeted pid\n"
|
|
|
|
" =!pkt s - send packet 's'\n"
|
|
|
|
" =!qRcmd cmd - hex-encode cmd and pass to target"
|
|
|
|
" interpreter\n"
|
|
|
|
" =!inv.reg - invalidate reg cache\n"
|
2017-07-20 09:57:29 +00:00
|
|
|
" =!pktsz - get max packet size used\n"
|
|
|
|
" =!pktsz bytes - set max. packet size as 'bytes' bytes\n"
|
|
|
|
" =!exec_file [pid] - get file which was executed for"
|
2017-07-12 22:33:53 +00:00
|
|
|
" current/specified pid\n");
|
2017-07-08 08:29:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-07-20 09:57:29 +00:00
|
|
|
if (!strncmp (cmd, "pktsz", 5)) {
|
|
|
|
const char *ptr = r_str_chop_ro (cmd + 5);
|
|
|
|
if (!isdigit (*ptr)) {
|
|
|
|
eprintf ("packet size: %u bytes\n",
|
|
|
|
desc->stub_features.pkt_sz);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
ut32 pktsz;
|
|
|
|
if (!(pktsz = (ut32) strtoul (ptr, NULL, 10))) {
|
|
|
|
// pktsz = 0 doesn't make sense
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
desc->stub_features.pkt_sz = pktsz;
|
|
|
|
return true;
|
|
|
|
}
|
2017-07-08 08:29:11 +00:00
|
|
|
if (!strncmp (cmd, "pkt ", 4)) {
|
2017-05-23 09:24:04 +00:00
|
|
|
if (send_msg (desc, cmd + 4) == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-14 11:11:04 +00:00
|
|
|
int r = read_packet (desc);
|
2017-07-08 08:29:11 +00:00
|
|
|
desc->data[desc->data_len] = '\0';
|
|
|
|
eprintf ("reply:\n\n%s\n", desc->data);
|
|
|
|
if (!desc->no_ack) {
|
|
|
|
eprintf ("[waiting for ack]\n");
|
|
|
|
}
|
|
|
|
return r >= 0;
|
|
|
|
}
|
|
|
|
if (!strncmp (cmd, "pid", 3)) {
|
2017-06-26 08:56:35 +00:00
|
|
|
int pid = desc ? desc->pid : -1;
|
2016-06-16 20:56:02 +00:00
|
|
|
if (!cmd[3]) {
|
|
|
|
io->cb_printf ("%d\n", pid);
|
|
|
|
}
|
2016-01-26 22:36:02 +00:00
|
|
|
return pid;
|
2017-07-08 08:29:11 +00:00
|
|
|
}
|
|
|
|
if (!strncmp (cmd, "qRcmd ", 6)) {
|
|
|
|
if (gdbr_send_qRcmd (desc, cmd + 6) < 0) {
|
|
|
|
eprintf ("remote error\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!strncmp (cmd, "inv.reg", 7)) {
|
2017-06-26 08:56:35 +00:00
|
|
|
gdbr_invalidate_reg_cache ();
|
2017-07-08 08:29:11 +00:00
|
|
|
return true;
|
2016-09-14 11:11:04 +00:00
|
|
|
}
|
2017-07-12 22:33:53 +00:00
|
|
|
if (r_str_startswith (cmd, "exec_file")) {
|
|
|
|
const char *ptr = cmd + strlen ("exec_file");
|
|
|
|
char *file;
|
|
|
|
int pid;
|
|
|
|
if (!isspace (*ptr)) {
|
|
|
|
if (!(file = gdbr_exec_file_read (desc, 0))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (isspace (*ptr)) {
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
if (isdigit (*ptr)) {
|
|
|
|
pid = atoi (ptr);
|
|
|
|
if (!(file = gdbr_exec_file_read (desc, pid))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!(file = gdbr_exec_file_read (desc, 0))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
eprintf ("%s\n", file);
|
|
|
|
free (file);
|
|
|
|
return true;
|
|
|
|
}
|
2017-07-29 15:58:20 +00:00
|
|
|
// This is internal, not available to user. Sets a flag that next call to
|
|
|
|
// get memmap will be for getting baddr
|
|
|
|
if (!strcmp (cmd, "baddr")) {
|
|
|
|
desc->get_baddr = true;
|
|
|
|
return true;
|
|
|
|
}
|
2017-07-08 08:29:11 +00:00
|
|
|
eprintf ("Try: '=!?'\n");
|
|
|
|
return true;
|
2010-07-21 23:14:13 +00:00
|
|
|
}
|
|
|
|
|
2013-12-10 03:19:04 +00:00
|
|
|
RIOPlugin r_io_plugin_gdb = {
|
2014-04-29 22:19:55 +00:00
|
|
|
//void *plugin;
|
2010-07-21 23:14:13 +00:00
|
|
|
.name = "gdb",
|
2014-12-07 18:37:03 +00:00
|
|
|
.license = "LGPL3",
|
2014-04-29 22:19:55 +00:00
|
|
|
.desc = "Attach to gdbserver, 'qemu -s', gdb://localhost:1234",
|
|
|
|
.open = __open,
|
|
|
|
.close = __close,
|
2010-07-21 23:14:13 +00:00
|
|
|
.read = __read,
|
|
|
|
.write = __write,
|
2016-06-19 22:29:41 +00:00
|
|
|
.check = __plugin_open,
|
2010-07-21 23:14:13 +00:00
|
|
|
.lseek = __lseek,
|
2011-04-06 10:26:19 +00:00
|
|
|
.system = __system,
|
2015-09-14 00:08:31 +00:00
|
|
|
.isdbg = true
|
2010-07-21 23:14:13 +00:00
|
|
|
};
|
2017-04-10 15:32:07 +00:00
|
|
|
|
|
|
|
#ifndef CORELIB
|
|
|
|
RLibStruct radare_plugin = {
|
|
|
|
.type = R_LIB_TYPE_IO,
|
|
|
|
.data = &r_io_plugin_gdb,
|
|
|
|
.version = R2_VERSION
|
|
|
|
};
|
|
|
|
#endif
|