2014-09-15 14:38:28 +02:00
|
|
|
/* radare - LGPL - Copyright 2009-2014 - pancake, TheLemonMan */
|
2009-02-05 22:08:46 +01:00
|
|
|
|
|
|
|
#include <r_debug.h>
|
2010-05-24 12:07:54 +02:00
|
|
|
#include <r_anal.h>
|
2010-03-12 18:46:11 +01:00
|
|
|
#include <signal.h>
|
|
|
|
|
2013-06-14 02:51:33 +02:00
|
|
|
R_LIB_VERSION(r_debug);
|
|
|
|
|
2014-09-17 14:57:33 +02:00
|
|
|
// Size of the lookahead buffers used in r_debug functions
|
|
|
|
#define DBG_BUF_SIZE 512
|
|
|
|
|
2014-06-04 04:12:11 +02:00
|
|
|
R_API RDebugInfo *r_debug_info(RDebug *dbg, const char *arg) {
|
2014-06-05 03:46:17 +02:00
|
|
|
if (!dbg || !dbg->h || !dbg->h->info)
|
2014-06-04 04:12:11 +02:00
|
|
|
return NULL;
|
|
|
|
return dbg->h->info (dbg, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API void r_debug_info_free (RDebugInfo *rdi) {
|
|
|
|
free (rdi->cwd);
|
|
|
|
free (rdi->exe);
|
|
|
|
free (rdi->cmdline);
|
|
|
|
}
|
|
|
|
|
2010-03-12 18:46:11 +01:00
|
|
|
/* restore program counter after breakpoint hit */
|
2010-05-24 12:07:54 +02:00
|
|
|
static int r_debug_recoil(RDebug *dbg) {
|
2011-05-20 20:42:25 +02:00
|
|
|
int recoil;
|
2010-09-18 02:51:17 +02:00
|
|
|
RRegItem *ri;
|
2011-05-20 20:42:25 +02:00
|
|
|
if (r_debug_is_dead (dbg))
|
|
|
|
return R_FALSE;
|
2010-03-12 18:46:11 +01:00
|
|
|
r_debug_reg_sync (dbg, R_REG_TYPE_GPR, R_FALSE);
|
|
|
|
ri = r_reg_get (dbg->reg, dbg->reg->name[R_REG_NAME_PC], -1);
|
|
|
|
if (ri) {
|
|
|
|
ut64 addr = r_reg_get_value (dbg->reg, ri);
|
|
|
|
recoil = r_bp_recoil (dbg->bp, addr);
|
2013-09-18 02:11:23 +02:00
|
|
|
//eprintf ("[R2] Breakpoint recoil at 0x%"PFMT64x" = %d\n", addr, recoil);
|
2013-03-19 02:36:14 +00:00
|
|
|
#if __arm__
|
|
|
|
if (recoil<1) recoil = 0; // XXX Hack :D
|
|
|
|
#else
|
2013-08-29 04:44:18 +02:00
|
|
|
if (recoil<1) recoil = 0; //1; // XXX Hack :D (x86 only?)
|
2013-03-19 02:36:14 +00:00
|
|
|
#endif
|
2010-03-12 18:46:11 +01:00
|
|
|
if (recoil) {
|
2010-11-17 00:56:48 +01:00
|
|
|
dbg->reason = R_DBG_REASON_BP;
|
2010-03-12 18:46:11 +01:00
|
|
|
r_reg_set_value (dbg->reg, ri, addr-recoil);
|
2014-03-21 03:18:10 +01:00
|
|
|
if (r_reg_get_value (dbg->reg, ri ) != (addr-recoil)) {
|
|
|
|
eprintf ("r_debug_recoil: Cannot set program counter\n");
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
2010-03-12 18:46:11 +01:00
|
|
|
r_debug_reg_sync (dbg, R_REG_TYPE_GPR, R_TRUE);
|
2011-07-06 01:45:45 +02:00
|
|
|
//eprintf ("[BP Hit] Setting pc to 0x%"PFMT64x"\n", (addr-recoil));
|
2011-05-20 20:42:25 +02:00
|
|
|
return R_TRUE;
|
2012-07-06 19:30:17 +02:00
|
|
|
}
|
2010-03-12 18:46:11 +01:00
|
|
|
} else eprintf ("r_debug_recoil: Cannot get program counter\n");
|
2011-05-20 20:42:25 +02:00
|
|
|
return R_FALSE;
|
2010-03-12 18:46:11 +01:00
|
|
|
}
|
2009-02-05 22:08:46 +01:00
|
|
|
|
2010-05-24 12:07:54 +02:00
|
|
|
R_API RDebug *r_debug_new(int hard) {
|
|
|
|
RDebug *dbg = R_NEW (RDebug);
|
2010-01-19 11:25:17 +01:00
|
|
|
if (dbg) {
|
2011-05-06 00:59:10 +02:00
|
|
|
// R_SYS_ARCH
|
2011-04-04 18:33:27 +02:00
|
|
|
dbg->arch = r_sys_arch_id (R_SYS_ARCH); // 0 is native by default
|
2011-05-06 00:59:10 +02:00
|
|
|
dbg->bits = R_SYS_BITS;
|
2010-05-24 12:07:54 +02:00
|
|
|
dbg->anal = NULL;
|
2010-01-19 11:25:17 +01:00
|
|
|
dbg->pid = -1;
|
|
|
|
dbg->tid = -1;
|
2011-11-25 04:32:32 +01:00
|
|
|
dbg->graph = r_graph_new ();
|
2010-09-23 20:42:35 +02:00
|
|
|
dbg->swstep = 0;
|
2010-01-19 11:25:17 +01:00
|
|
|
dbg->newstate = 0;
|
2013-09-18 02:55:51 +02:00
|
|
|
dbg->signum = 0;
|
2010-11-17 00:56:48 +01:00
|
|
|
dbg->reason = R_DBG_REASON_UNKNOWN;
|
2010-09-23 20:42:35 +02:00
|
|
|
dbg->stop_all_threads = R_FALSE;
|
2010-06-04 00:56:44 +02:00
|
|
|
dbg->trace = r_debug_trace_new ();
|
2010-01-19 11:25:17 +01:00
|
|
|
dbg->printf = (void *)printf;
|
2010-02-22 12:42:43 +01:00
|
|
|
dbg->reg = r_reg_new ();
|
2010-01-19 11:25:17 +01:00
|
|
|
dbg->h = NULL;
|
2010-09-23 20:42:35 +02:00
|
|
|
/* TODO: needs a redesign? */
|
2010-02-04 13:23:53 +01:00
|
|
|
dbg->maps = r_debug_map_list_new ();
|
|
|
|
dbg->maps_user = r_debug_map_list_new ();
|
2013-09-18 02:11:23 +02:00
|
|
|
r_debug_signal_init (dbg);
|
2010-01-19 11:25:17 +01:00
|
|
|
if (hard) {
|
2010-02-22 12:42:43 +01:00
|
|
|
dbg->bp = r_bp_new ();
|
2010-05-26 18:25:35 +02:00
|
|
|
r_debug_plugin_init (dbg);
|
2010-01-19 11:25:17 +01:00
|
|
|
dbg->bp->iob.init = R_FALSE;
|
|
|
|
}
|
2009-09-15 13:24:28 +02:00
|
|
|
}
|
2010-01-19 11:25:17 +01:00
|
|
|
return dbg;
|
2009-02-05 22:08:46 +01:00
|
|
|
}
|
|
|
|
|
2014-06-05 03:46:17 +02:00
|
|
|
R_API RDebug *r_debug_free(RDebug *dbg) {
|
2011-12-16 16:33:06 +01:00
|
|
|
if (!dbg) return NULL;
|
2010-09-20 14:02:45 +02:00
|
|
|
// TODO: free it correctly.. we must ensure this is an instance and not a reference..
|
2014-09-17 15:47:05 +02:00
|
|
|
r_bp_free (dbg->bp);
|
2010-01-21 02:38:52 +01:00
|
|
|
//r_reg_free(&dbg->reg);
|
2014-04-23 03:54:06 +02:00
|
|
|
sdb_free (dbg->sgnls);
|
2010-05-26 18:25:35 +02:00
|
|
|
//r_debug_plugin_free();
|
2013-11-17 14:25:45 +04:00
|
|
|
r_debug_trace_free (dbg);
|
2011-11-25 04:32:32 +01:00
|
|
|
r_graph_free (dbg->graph);
|
2010-01-21 02:38:52 +01:00
|
|
|
free (dbg);
|
2009-08-22 03:11:33 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-05-06 00:59:10 +02:00
|
|
|
R_API int r_debug_attach(RDebug *dbg, int pid) {
|
2009-02-05 22:08:46 +01:00
|
|
|
int ret = R_FALSE;
|
2010-01-21 02:38:52 +01:00
|
|
|
if (dbg && dbg->h && dbg->h->attach) {
|
2011-05-06 00:59:10 +02:00
|
|
|
ret = dbg->h->attach (dbg, pid);
|
2010-11-17 02:31:56 +01:00
|
|
|
if (ret != -1) {
|
|
|
|
eprintf ("pid = %d tid = %d\n", pid, ret);
|
2009-09-14 00:37:28 +02:00
|
|
|
// TODO: get arch and set io pid
|
2010-03-03 14:48:17 +01:00
|
|
|
//int arch = dbg->h->arch;
|
2009-09-14 00:37:28 +02:00
|
|
|
//r_reg_set(dbg->reg->nregs, arch); //R_DBG_ARCH_X86);
|
|
|
|
// dbg->bp->iob->system("pid %d", pid);
|
2010-11-17 02:31:56 +01:00
|
|
|
//dbg->pid = pid;
|
|
|
|
//dbg->tid = ret;
|
|
|
|
r_debug_select (dbg, pid, ret); //dbg->pid, dbg->tid);
|
2011-05-25 08:43:12 +02:00
|
|
|
}// else if (pid != -1)
|
|
|
|
// eprintf ("Cannot attach to this pid %d\n", pid);
|
2010-01-21 02:38:52 +01:00
|
|
|
} else eprintf ("dbg->attach = NULL\n");
|
2009-02-05 22:08:46 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-10-20 18:04:26 +02:00
|
|
|
/* stop execution of child process */
|
|
|
|
R_API int r_debug_stop(RDebug *dbg) {
|
|
|
|
if (dbg && dbg->h && dbg->h->stop)
|
|
|
|
return dbg->h->stop (dbg);
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
|
2011-05-06 00:59:10 +02:00
|
|
|
R_API int r_debug_set_arch(RDebug *dbg, int arch, int bits) {
|
|
|
|
if (dbg && dbg->h) {
|
|
|
|
if (arch & dbg->h->arch) {
|
2011-04-04 18:33:27 +02:00
|
|
|
//eprintf ("arch supported by debug backend (%x)\n", arch);
|
2011-09-22 09:52:00 +02:00
|
|
|
switch (bits) {
|
|
|
|
case 32:
|
|
|
|
dbg->bits = R_SYS_BITS_32;
|
|
|
|
break;
|
|
|
|
case 64:
|
|
|
|
dbg->bits = R_SYS_BITS_64;
|
|
|
|
break;
|
|
|
|
}
|
2012-07-05 16:02:12 +00:00
|
|
|
if (!(dbg->h->bits & dbg->bits))
|
|
|
|
dbg->bits = dbg->h->bits;
|
2011-05-06 19:56:16 +02:00
|
|
|
dbg->arch = arch;
|
2011-05-06 00:59:10 +02:00
|
|
|
return R_TRUE;
|
|
|
|
}
|
2011-10-09 04:15:32 +02:00
|
|
|
//eprintf ("arch (%s, %d) not supported by debug backend\n",
|
|
|
|
// r_sys_arch_str (arch), bits);
|
2011-05-06 00:59:10 +02:00
|
|
|
}
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
|
2010-02-05 12:21:37 +01:00
|
|
|
/*
|
|
|
|
* Save 4096 bytes from %esp
|
|
|
|
* TODO: Add support for reverse stack architectures
|
2013-04-16 04:01:39 +02:00
|
|
|
* Also known as r_debug_inject()
|
2010-02-05 12:21:37 +01:00
|
|
|
*/
|
2013-04-16 04:01:39 +02:00
|
|
|
R_API ut64 r_debug_execute(RDebug *dbg, const ut8 *buf, int len, int restore) {
|
2010-02-05 12:21:37 +01:00
|
|
|
int orig_sz;
|
|
|
|
ut8 stackbackup[4096];
|
|
|
|
ut8 *backup, *orig = NULL;
|
2010-09-18 02:51:17 +02:00
|
|
|
RRegItem *ri, *risp, *ripc;
|
2010-02-05 12:21:37 +01:00
|
|
|
ut64 rsp, rpc, ra0 = 0LL;
|
2011-05-20 20:42:25 +02:00
|
|
|
if (r_debug_is_dead (dbg))
|
|
|
|
return R_FALSE;
|
2010-02-05 12:21:37 +01:00
|
|
|
ripc = r_reg_get (dbg->reg, dbg->reg->name[R_REG_NAME_PC], R_REG_TYPE_GPR);
|
2013-04-16 04:01:39 +02:00
|
|
|
risp = r_reg_get (dbg->reg, dbg->reg->name[R_REG_NAME_SP], R_REG_TYPE_GPR);
|
2010-02-05 12:21:37 +01:00
|
|
|
if (ripc) {
|
|
|
|
r_debug_reg_sync (dbg, R_REG_TYPE_GPR, R_FALSE);
|
|
|
|
orig = r_reg_get_bytes (dbg->reg, -1, &orig_sz);
|
|
|
|
if (orig == NULL) {
|
|
|
|
eprintf ("Cannot get register arena bytes\n");
|
|
|
|
return 0LL;
|
|
|
|
}
|
|
|
|
rpc = r_reg_get_value (dbg->reg, ripc);
|
|
|
|
rsp = r_reg_get_value (dbg->reg, risp);
|
|
|
|
|
|
|
|
backup = malloc (len);
|
2014-04-25 23:04:02 +00:00
|
|
|
if (backup == NULL) {
|
|
|
|
free (orig);
|
2010-02-05 12:21:37 +01:00
|
|
|
return 0LL;
|
2014-04-25 23:04:02 +00:00
|
|
|
}
|
2010-02-05 12:21:37 +01:00
|
|
|
dbg->iob.read_at (dbg->iob.io, rpc, backup, len);
|
|
|
|
dbg->iob.read_at (dbg->iob.io, rsp, stackbackup, len);
|
|
|
|
|
|
|
|
r_bp_add_sw (dbg->bp, rpc+len, 1, R_BP_PROT_EXEC);
|
|
|
|
|
|
|
|
/* execute code here */
|
|
|
|
dbg->iob.write_at (dbg->iob.io, rpc, buf, len);
|
2013-04-16 04:01:39 +02:00
|
|
|
//r_bp_add_sw (dbg->bp, rpc+len, 4, R_BP_PROT_EXEC);
|
2010-02-05 12:21:37 +01:00
|
|
|
r_debug_continue (dbg);
|
2013-04-16 04:01:39 +02:00
|
|
|
//r_bp_del (dbg->bp, rpc+len);
|
2010-02-05 12:21:37 +01:00
|
|
|
/* TODO: check if stopped in breakpoint or not */
|
|
|
|
|
|
|
|
r_bp_del (dbg->bp, rpc+len);
|
|
|
|
dbg->iob.write_at (dbg->iob.io, rpc, backup, len);
|
2013-04-16 04:01:39 +02:00
|
|
|
if (restore) {
|
|
|
|
dbg->iob.write_at (dbg->iob.io, rsp, stackbackup, len);
|
|
|
|
}
|
2010-02-05 12:21:37 +01:00
|
|
|
|
|
|
|
r_debug_reg_sync (dbg, R_REG_TYPE_GPR, R_FALSE);
|
|
|
|
ri = r_reg_get (dbg->reg, dbg->reg->name[R_REG_NAME_A0], R_REG_TYPE_GPR);
|
|
|
|
ra0 = r_reg_get_value (dbg->reg, ri);
|
2013-04-16 04:01:39 +02:00
|
|
|
if (restore) {
|
|
|
|
r_reg_set_bytes (dbg->reg, -1, orig, orig_sz);
|
|
|
|
} else {
|
|
|
|
r_reg_set_value (dbg->reg, ripc, rpc);
|
|
|
|
}
|
2010-02-05 12:21:37 +01:00
|
|
|
r_debug_reg_sync (dbg, R_REG_TYPE_GPR, R_TRUE);
|
|
|
|
free (backup);
|
|
|
|
free (orig);
|
2010-04-14 13:02:23 +02:00
|
|
|
eprintf ("ra0=0x%08"PFMT64x"\n", ra0);
|
2010-02-05 12:21:37 +01:00
|
|
|
} else eprintf ("r_debug_execute: Cannot get program counter\n");
|
|
|
|
return (ra0);
|
|
|
|
}
|
|
|
|
|
2010-02-22 12:42:43 +01:00
|
|
|
R_API int r_debug_startv(struct r_debug_t *dbg, int argc, char **argv) {
|
2010-02-05 12:21:37 +01:00
|
|
|
/* TODO : r_debug_startv unimplemented */
|
2009-02-05 22:08:46 +01:00
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
|
2010-02-22 12:42:43 +01:00
|
|
|
R_API int r_debug_start(struct r_debug_t *dbg, const char *cmd) {
|
|
|
|
/* TODO: this argc/argv parser is done in r_io */
|
2009-08-22 01:54:24 +00:00
|
|
|
// TODO: parse cmd and generate argc and argv
|
2009-02-05 22:08:46 +01:00
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
|
2010-02-22 12:42:43 +01:00
|
|
|
R_API int r_debug_detach(struct r_debug_t *dbg, int pid) {
|
2009-02-05 22:08:46 +01:00
|
|
|
if (dbg->h && dbg->h->detach)
|
|
|
|
return dbg->h->detach(pid);
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
|
2010-04-11 01:46:07 +02:00
|
|
|
R_API int r_debug_select(RDebug *dbg, int pid, int tid) {
|
2013-09-18 02:11:23 +02:00
|
|
|
if (!tid) tid = pid;
|
2011-07-06 01:45:45 +02:00
|
|
|
if (pid != dbg->pid || tid != dbg->tid)
|
|
|
|
eprintf ("r_debug_select: %d %d\n", pid, tid);
|
2009-02-05 22:08:46 +01:00
|
|
|
dbg->pid = pid;
|
2012-11-20 03:59:00 +01:00
|
|
|
if (tid == -1)
|
|
|
|
tid = dbg->pid;
|
2009-02-05 22:08:46 +01:00
|
|
|
dbg->tid = tid;
|
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
|
2010-07-16 00:02:33 +02:00
|
|
|
R_API int r_debug_stop_reason(RDebug *dbg) {
|
2009-02-05 22:08:46 +01:00
|
|
|
// TODO: return reason to stop debugging
|
|
|
|
// - new process
|
|
|
|
// - trap instruction
|
|
|
|
// - illegal instruction
|
|
|
|
// - fpu exception
|
2010-09-20 14:02:45 +02:00
|
|
|
// return dbg->reason
|
2010-11-17 00:56:48 +01:00
|
|
|
return dbg->reason;
|
2009-02-05 22:08:46 +01:00
|
|
|
}
|
|
|
|
|
2010-03-12 18:46:11 +01:00
|
|
|
/* Returns PID */
|
|
|
|
R_API int r_debug_wait(RDebug *dbg) {
|
|
|
|
int ret = 0;
|
2014-05-10 02:47:50 +02:00
|
|
|
if (!dbg)
|
|
|
|
return R_FALSE;
|
2011-05-20 20:42:25 +02:00
|
|
|
if (r_debug_is_dead (dbg))
|
|
|
|
return R_FALSE;
|
2014-05-10 02:47:50 +02:00
|
|
|
if (dbg->h && dbg->h->wait) {
|
2010-11-17 00:56:48 +01:00
|
|
|
dbg->reason = R_DBG_REASON_UNKNOWN;
|
2011-07-06 11:48:16 +02:00
|
|
|
ret = dbg->h->wait (dbg, dbg->pid);
|
2010-11-17 00:56:48 +01:00
|
|
|
dbg->reason = ret;
|
2009-04-15 11:09:36 +00:00
|
|
|
dbg->newstate = 1;
|
2011-05-20 20:42:25 +02:00
|
|
|
if (ret == -1) {
|
|
|
|
eprintf ("\n==> Process finished\n\n");
|
2013-09-18 02:11:23 +02:00
|
|
|
r_debug_select (dbg, -1, -1);
|
2011-05-20 20:42:25 +02:00
|
|
|
}
|
|
|
|
//eprintf ("wait = %d\n", ret);
|
2010-06-04 00:56:44 +02:00
|
|
|
if (dbg->trace->enabled)
|
2010-03-12 18:46:11 +01:00
|
|
|
r_debug_trace_pc (dbg);
|
2013-10-20 02:09:03 +02:00
|
|
|
if (ret == R_DBG_REASON_SIGNAL && dbg->signum != -1) {
|
2013-09-18 02:11:23 +02:00
|
|
|
/* handle signal on continuations here */
|
|
|
|
int what = r_debug_signal_what (dbg, dbg->signum);
|
2013-09-18 03:44:11 +02:00
|
|
|
const char *name = r_debug_signal_resolve_i (dbg, dbg->signum);
|
2013-11-03 03:05:00 +01:00
|
|
|
if (strcmp ("SIGTRAP", name))
|
|
|
|
r_cons_printf ("[+] signal %d aka %s received\n",
|
|
|
|
dbg->signum, name);
|
2013-09-18 02:55:51 +02:00
|
|
|
if (what & R_DBG_SIGNAL_SKIP) {
|
|
|
|
dbg->signum = 0;
|
|
|
|
// TODO: use ptrace-setsiginfo to ignore signal
|
|
|
|
}
|
2013-09-18 02:11:23 +02:00
|
|
|
if (what & R_DBG_SIGNAL_CONT) {
|
|
|
|
// XXX: support step, steptrace, continue_until_foo, etc..
|
|
|
|
r_debug_continue (dbg);
|
|
|
|
}
|
|
|
|
}
|
2009-04-15 11:09:36 +00:00
|
|
|
}
|
2009-04-01 00:28:13 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-11-11 03:13:44 +01:00
|
|
|
R_API int r_debug_step_soft(RDebug *dbg) {
|
|
|
|
ut8 buf[32];
|
2014-09-15 14:38:28 +02:00
|
|
|
ut64 pc, sp;
|
|
|
|
ut64 next[2];
|
2010-11-11 03:13:44 +01:00
|
|
|
RAnalOp op;
|
2014-09-17 13:28:07 +02:00
|
|
|
int br, i, ret;
|
2014-09-15 14:38:28 +02:00
|
|
|
union {
|
|
|
|
ut64 r64;
|
|
|
|
ut32 r32[2];
|
2014-09-17 13:28:07 +02:00
|
|
|
} sp_top;
|
2014-09-15 14:38:28 +02:00
|
|
|
|
2011-05-20 20:42:25 +02:00
|
|
|
if (r_debug_is_dead (dbg))
|
|
|
|
return R_FALSE;
|
2014-09-15 14:38:28 +02:00
|
|
|
|
|
|
|
pc = r_debug_reg_get (dbg, dbg->reg->name[R_REG_NAME_PC]);
|
|
|
|
sp = r_debug_reg_get (dbg, dbg->reg->name[R_REG_NAME_SP]);
|
|
|
|
|
|
|
|
if (dbg->iob.read_at (dbg->iob.io, pc, buf, sizeof (buf)) < 0)
|
|
|
|
return R_FALSE;
|
|
|
|
|
|
|
|
if (!r_anal_op (dbg->anal, &op, pc, buf, sizeof (buf)))
|
|
|
|
return R_FALSE;
|
|
|
|
|
|
|
|
if (op.type == R_ANAL_OP_TYPE_ILL)
|
|
|
|
return R_FALSE;
|
|
|
|
|
|
|
|
switch (op.type) {
|
|
|
|
case R_ANAL_OP_TYPE_RET:
|
2014-09-17 15:18:54 +02:00
|
|
|
dbg->iob.read_at (dbg->iob.io, sp, (ut8 *)&sp_top, 8);
|
2014-09-17 13:28:07 +02:00
|
|
|
next[0] = (dbg->bits == R_SYS_BITS_32) ? sp_top.r32[0] : sp_top.r64;
|
2014-09-15 14:38:28 +02:00
|
|
|
br = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case R_ANAL_OP_TYPE_CJMP:
|
|
|
|
case R_ANAL_OP_TYPE_CCALL:
|
|
|
|
next[0] = op.jump;
|
|
|
|
next[1] = op.fail;
|
|
|
|
br = 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case R_ANAL_OP_TYPE_CALL:
|
|
|
|
case R_ANAL_OP_TYPE_JMP:
|
|
|
|
next[0] = op.jump;
|
|
|
|
br = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
next[0] = op.addr + op.size;
|
|
|
|
br = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < br; i++)
|
|
|
|
r_bp_add_sw (dbg->bp, next[i], 1, R_BP_PROT_EXEC);
|
|
|
|
|
2014-09-17 13:28:07 +02:00
|
|
|
ret = r_debug_continue (dbg);
|
2010-11-11 03:13:44 +01:00
|
|
|
|
2014-09-15 14:38:28 +02:00
|
|
|
for (i = 0; i < br; i++)
|
|
|
|
r_bp_del (dbg->bp, next[i]);
|
|
|
|
|
2014-09-17 13:28:07 +02:00
|
|
|
return ret;
|
2010-11-11 03:13:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
R_API int r_debug_step_hard(RDebug *dbg) {
|
2011-05-20 20:42:25 +02:00
|
|
|
if (r_debug_is_dead (dbg))
|
|
|
|
return R_FALSE;
|
2010-11-17 02:31:56 +01:00
|
|
|
if (!dbg->h->step (dbg))
|
2010-11-11 03:13:44 +01:00
|
|
|
return R_FALSE;
|
2014-09-17 13:28:07 +02:00
|
|
|
return r_debug_wait (dbg);
|
2010-11-11 03:13:44 +01:00
|
|
|
}
|
|
|
|
|
2010-09-23 20:42:35 +02:00
|
|
|
R_API int r_debug_step(RDebug *dbg, int steps) {
|
2014-09-17 13:28:07 +02:00
|
|
|
int i, ret;
|
|
|
|
|
|
|
|
if (!dbg || !dbg->h)
|
|
|
|
return R_FALSE;
|
|
|
|
|
|
|
|
if (r_debug_is_dead (dbg))
|
|
|
|
return R_FALSE;
|
|
|
|
|
2014-09-17 14:57:33 +02:00
|
|
|
if (steps < 1)
|
|
|
|
steps = 1;
|
|
|
|
|
2014-09-17 13:28:07 +02:00
|
|
|
for (i = 0; i < steps; i++) {
|
|
|
|
ret = dbg->swstep?
|
|
|
|
r_debug_step_soft (dbg):
|
|
|
|
r_debug_step_hard (dbg);
|
|
|
|
if (!ret) {
|
|
|
|
eprintf ("Stepping failed!\n");
|
|
|
|
return R_FALSE;
|
|
|
|
} else dbg->steps++;
|
2009-02-05 22:08:46 +01:00
|
|
|
}
|
2014-09-17 13:28:07 +02:00
|
|
|
|
|
|
|
return i;
|
2009-02-05 22:08:46 +01:00
|
|
|
}
|
|
|
|
|
2010-02-05 12:21:37 +01:00
|
|
|
R_API void r_debug_io_bind(RDebug *dbg, RIO *io) {
|
|
|
|
r_io_bind (io, &dbg->bp->iob);
|
|
|
|
r_io_bind (io, &dbg->iob);
|
|
|
|
}
|
|
|
|
|
2010-05-24 12:07:54 +02:00
|
|
|
R_API int r_debug_step_over(RDebug *dbg, int steps) {
|
2010-05-24 17:51:51 +02:00
|
|
|
RAnalOp op;
|
2014-09-17 14:57:33 +02:00
|
|
|
ut64 buf_pc, pc;
|
|
|
|
ut8 buf[DBG_BUF_SIZE];
|
|
|
|
int i;
|
|
|
|
|
2011-05-20 20:42:25 +02:00
|
|
|
if (r_debug_is_dead (dbg))
|
|
|
|
return R_FALSE;
|
2014-09-17 14:57:33 +02:00
|
|
|
|
|
|
|
if (steps < 1)
|
|
|
|
steps = 1;
|
|
|
|
|
2011-10-09 04:15:32 +02:00
|
|
|
if (dbg->h && dbg->h->step_over) {
|
2014-09-17 14:57:33 +02:00
|
|
|
for (i = 0; i < steps; i++)
|
2011-10-09 04:15:32 +02:00
|
|
|
if (!dbg->h->step_over (dbg))
|
|
|
|
return R_FALSE;
|
2014-09-17 14:57:33 +02:00
|
|
|
return i;
|
2011-10-09 04:15:32 +02:00
|
|
|
}
|
2014-09-17 14:57:33 +02:00
|
|
|
|
|
|
|
if (!dbg->anal || !dbg->reg)
|
|
|
|
return R_FALSE;
|
|
|
|
|
|
|
|
// Initial refill
|
|
|
|
buf_pc = r_debug_reg_get (dbg, dbg->reg->name[R_REG_NAME_PC]);
|
|
|
|
dbg->iob.read_at (dbg->iob.io, buf_pc, buf, sizeof (buf));
|
|
|
|
|
|
|
|
for (i = 0; i < steps; i++) {
|
|
|
|
pc = r_debug_reg_get (dbg, dbg->reg->name[R_REG_NAME_PC]);
|
|
|
|
// Try to keep the buffer full
|
|
|
|
if (pc - buf_pc > sizeof (buf)) {
|
|
|
|
buf_pc = pc;
|
|
|
|
dbg->iob.read_at (dbg->iob.io, buf_pc, buf, sizeof (buf));
|
2012-05-30 11:14:41 +02:00
|
|
|
}
|
2014-09-17 14:57:33 +02:00
|
|
|
// Analyze the opcode
|
|
|
|
if (!r_anal_op (dbg->anal, &op, pc, buf + (pc - buf_pc), sizeof (buf) - (pc - buf_pc))) {
|
|
|
|
eprintf ("Decode error at %"PFMT64x"\n", pc);
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip over all the subroutine calls
|
|
|
|
if (op.type == R_ANAL_OP_TYPE_CALL ||
|
|
|
|
op.type == R_ANAL_OP_TYPE_CCALL ||
|
|
|
|
op.type == R_ANAL_OP_TYPE_UCALL ||
|
|
|
|
op.type == R_ANAL_OP_TYPE_UCCALL) {
|
|
|
|
|
|
|
|
// Use op.fail here instead of pc+op.size to enforce anal backends to fill in this field
|
|
|
|
if (!r_debug_continue_until (dbg, op.fail)) {
|
2014-09-17 15:18:54 +02:00
|
|
|
eprintf ("Could not step over call @ 0x%"PFMT64x"\n", pc);
|
2014-09-17 14:57:33 +02:00
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
} else r_debug_step (dbg, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
2009-02-05 22:08:46 +01:00
|
|
|
}
|
|
|
|
|
2010-05-24 12:07:54 +02:00
|
|
|
R_API int r_debug_continue_kill(RDebug *dbg, int sig) {
|
2009-02-05 22:08:46 +01:00
|
|
|
int ret = R_FALSE;
|
2014-05-10 02:47:20 +02:00
|
|
|
if (!dbg)
|
|
|
|
return R_FALSE;
|
2011-05-20 20:42:25 +02:00
|
|
|
if (r_debug_is_dead (dbg))
|
|
|
|
return R_FALSE;
|
2014-05-10 02:47:20 +02:00
|
|
|
if (dbg->h && dbg->h->cont) {
|
2014-09-15 14:38:28 +02:00
|
|
|
r_bp_restore (dbg->bp, R_TRUE); // set sw breakpoints
|
2011-04-03 16:38:24 +02:00
|
|
|
ret = dbg->h->cont (dbg, dbg->pid, dbg->tid, sig);
|
2013-09-18 02:55:51 +02:00
|
|
|
dbg->signum = 0;
|
2011-05-20 20:42:25 +02:00
|
|
|
r_debug_wait (dbg);
|
2014-09-15 14:38:28 +02:00
|
|
|
r_bp_restore (dbg->bp, R_FALSE); // unset sw breakpoints
|
2010-01-21 02:38:52 +01:00
|
|
|
r_debug_recoil (dbg);
|
2010-10-28 21:20:44 +02:00
|
|
|
#if 0
|
2010-03-24 19:20:46 +01:00
|
|
|
#if __UNIX__
|
2010-10-28 21:20:44 +02:00
|
|
|
/* XXX Uh? */
|
2010-05-24 12:07:54 +02:00
|
|
|
if (dbg->stop_all_threads && dbg->pid>0)
|
2013-03-02 19:44:10 +01:00
|
|
|
r_sandbox_kill (dbg->pid, SIGSTOP);
|
2010-10-28 21:20:44 +02:00
|
|
|
#endif
|
2010-03-24 19:20:46 +01:00
|
|
|
#endif
|
2010-03-11 01:04:59 +01:00
|
|
|
r_debug_select (dbg, dbg->pid, ret);
|
2009-03-22 02:36:00 +00:00
|
|
|
}
|
2009-02-05 22:08:46 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-05-24 12:07:54 +02:00
|
|
|
R_API int r_debug_continue(RDebug *dbg) {
|
2013-11-03 02:48:13 +01:00
|
|
|
return r_debug_continue_kill (dbg, 0); //dbg->signum);
|
2010-01-19 11:25:17 +01:00
|
|
|
}
|
|
|
|
|
2010-06-04 00:56:44 +02:00
|
|
|
R_API int r_debug_continue_until_nontraced(RDebug *dbg) {
|
|
|
|
eprintf ("TODO\n");
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
|
2010-05-24 17:51:51 +02:00
|
|
|
R_API int r_debug_continue_until_optype(RDebug *dbg, int type, int over) {
|
|
|
|
int ret, n = 0;
|
2014-09-15 14:38:28 +02:00
|
|
|
ut64 pc, buf_pc = 0;
|
2010-05-24 12:07:54 +02:00
|
|
|
RAnalOp op;
|
2014-09-17 14:57:33 +02:00
|
|
|
ut8 buf[DBG_BUF_SIZE];
|
2012-05-30 11:14:41 +02:00
|
|
|
|
2011-05-20 20:42:25 +02:00
|
|
|
if (r_debug_is_dead (dbg))
|
|
|
|
return R_FALSE;
|
2014-09-15 14:38:28 +02:00
|
|
|
|
|
|
|
if (!dbg->anal || !dbg->reg) {
|
|
|
|
eprintf ("Undefined pointer at dbg->anal\n");
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initial refill
|
|
|
|
buf_pc = r_debug_reg_get (dbg, dbg->reg->name[R_REG_NAME_PC]);
|
|
|
|
dbg->iob.read_at (dbg->iob.io, buf_pc, buf, sizeof (buf));
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
pc = r_debug_reg_get (dbg, dbg->reg->name[R_REG_NAME_PC]);
|
2014-09-17 14:57:33 +02:00
|
|
|
// Try to keep the buffer full
|
2014-09-15 14:38:28 +02:00
|
|
|
if (pc - buf_pc > sizeof (buf)) {
|
|
|
|
buf_pc = pc;
|
|
|
|
dbg->iob.read_at (dbg->iob.io, buf_pc, buf, sizeof (buf));
|
|
|
|
}
|
|
|
|
// Analyze the opcode
|
|
|
|
if (!r_anal_op (dbg->anal, &op, pc, buf + (pc - buf_pc), sizeof (buf) - (pc - buf_pc))) {
|
|
|
|
eprintf ("Decode error at %"PFMT64x"\n", pc);
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
if (op.type == type)
|
|
|
|
break;
|
|
|
|
// Step over and repeat
|
|
|
|
ret = over ?
|
|
|
|
r_debug_step_over (dbg, 1) :
|
|
|
|
r_debug_step (dbg, 1);
|
|
|
|
|
|
|
|
if (!ret) {
|
|
|
|
eprintf ("r_debug_step: failed\n");
|
|
|
|
break;
|
2012-05-30 11:14:41 +02:00
|
|
|
}
|
2014-09-15 14:38:28 +02:00
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
2010-05-24 12:07:54 +02:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2014-08-01 12:07:08 +02:00
|
|
|
R_API int r_debug_continue_until(RDebug *dbg, ut64 addr) {
|
2014-09-16 15:09:22 +02:00
|
|
|
int has_bp;
|
|
|
|
ut64 pc;
|
|
|
|
|
|
|
|
if (r_debug_is_dead (dbg))
|
|
|
|
return R_FALSE;
|
|
|
|
|
|
|
|
// Check if there was another breakpoint set at addr
|
|
|
|
has_bp = r_bp_at_addr (dbg->bp, addr, R_BP_PROT_EXEC) != NULL;
|
|
|
|
if (!has_bp)
|
|
|
|
r_bp_add_sw (dbg->bp, addr, 1, R_BP_PROT_EXEC);
|
|
|
|
|
|
|
|
// Continue until the bp is reached
|
|
|
|
for (;;) {
|
|
|
|
if (r_debug_is_dead (dbg))
|
|
|
|
break;
|
|
|
|
|
|
|
|
pc = r_debug_reg_get (dbg, dbg->reg->name[R_REG_NAME_PC]);
|
|
|
|
if (pc == addr)
|
|
|
|
break;
|
|
|
|
|
|
|
|
r_debug_continue (dbg);
|
2014-08-01 12:07:08 +02:00
|
|
|
}
|
2014-09-16 15:09:22 +02:00
|
|
|
|
|
|
|
// Clean up if needed
|
2014-09-17 16:57:41 +02:00
|
|
|
if (!has_bp)
|
2014-09-16 15:09:22 +02:00
|
|
|
r_bp_del (dbg->bp, addr);
|
|
|
|
|
|
|
|
return R_TRUE;
|
2009-02-05 22:08:46 +01:00
|
|
|
}
|
|
|
|
|
2014-06-14 02:01:03 +02:00
|
|
|
R_API int r_debug_continue_syscalls(RDebug *dbg, int *sc, int n_sc) {
|
|
|
|
const char *sysname;
|
|
|
|
int i, reg, ret = R_FALSE;
|
|
|
|
if (!dbg || !dbg->h || r_debug_is_dead (dbg))
|
2011-05-20 20:42:25 +02:00
|
|
|
return R_FALSE;
|
2014-06-14 02:01:03 +02:00
|
|
|
if (!dbg->h->contsc) {
|
|
|
|
/* user-level syscall tracing */
|
|
|
|
r_debug_continue_until_optype (dbg, R_ANAL_OP_TYPE_SWI, 0);
|
|
|
|
reg = (int)r_debug_reg_get (dbg, "a0"); // XXX
|
2014-06-14 02:27:43 +02:00
|
|
|
sysname = r_syscall_get_i (dbg->anal->syscall, reg, -1);
|
|
|
|
if (!sysname) sysname = "unknown";
|
2014-06-14 02:01:03 +02:00
|
|
|
eprintf ("--> syscall %d %s\n", reg, sysname);
|
|
|
|
return reg;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!r_debug_reg_sync (dbg, R_REG_TYPE_GPR, R_FALSE)) {
|
|
|
|
eprintf ("--> cannot read registers\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
reg = (int)r_debug_reg_get (dbg, "sn");
|
|
|
|
if (reg == (int)UT64_MAX) {
|
|
|
|
eprintf ("Cannot find 'sn' register for current arch-os.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
for (;;) {
|
|
|
|
dbg->h->contsc (dbg, dbg->pid, 0); // TODO handle return value
|
|
|
|
if (!r_debug_reg_sync (dbg, R_REG_TYPE_GPR, R_FALSE)) {
|
|
|
|
eprintf ("--> eol\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
reg = (int)r_debug_reg_get (dbg, "sn");
|
|
|
|
if (reg == (int)UT64_MAX)
|
|
|
|
return -1;
|
2014-06-14 02:27:43 +02:00
|
|
|
sysname = r_syscall_get_i (dbg->anal->syscall, reg, -1);
|
|
|
|
if (!sysname) sysname = "unknown";
|
2014-06-14 02:01:03 +02:00
|
|
|
eprintf ("--> syscall %d %s\n", reg, sysname);
|
2014-06-14 02:27:43 +02:00
|
|
|
for (i=0; i<n_sc; i++) {
|
2014-06-14 02:01:03 +02:00
|
|
|
if (sc[i] == reg)
|
|
|
|
return reg;
|
2010-05-24 12:07:54 +02:00
|
|
|
}
|
2014-06-14 02:01:03 +02:00
|
|
|
// TODO: must use r_core_cmd(as)..import code from rcore
|
2010-03-04 01:46:25 +01:00
|
|
|
}
|
2009-02-05 22:08:46 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-06-14 02:01:03 +02:00
|
|
|
R_API int r_debug_continue_syscall(RDebug *dbg, int sc) {
|
|
|
|
return r_debug_continue_syscalls (dbg, &sc, 1);
|
|
|
|
}
|
|
|
|
|
2010-01-21 02:38:52 +01:00
|
|
|
// TODO: remove from here? this is code injection!
|
2012-12-07 12:03:53 +01:00
|
|
|
R_API int r_debug_syscall(RDebug *dbg, int num) {
|
2010-05-24 12:07:54 +02:00
|
|
|
int ret = R_FALSE;
|
|
|
|
if (dbg->h->contsc) {
|
2011-04-03 16:38:24 +02:00
|
|
|
ret = dbg->h->contsc (dbg, dbg->pid, num);
|
2010-05-24 12:07:54 +02:00
|
|
|
} else {
|
|
|
|
ret = R_TRUE;
|
|
|
|
// TODO.check for num
|
|
|
|
}
|
2010-09-23 20:42:35 +02:00
|
|
|
eprintf ("TODO: show syscall information\n");
|
2010-03-12 18:46:11 +01:00
|
|
|
/* r2rc task? ala inject? */
|
2010-05-24 12:07:54 +02:00
|
|
|
return ret;
|
2009-09-10 20:51:34 +00:00
|
|
|
}
|
|
|
|
|
2012-12-07 12:03:53 +01:00
|
|
|
R_API int r_debug_kill(RDebug *dbg, int pid, int tid, int sig) {
|
2010-02-28 22:58:21 +01:00
|
|
|
int ret = R_FALSE;
|
2013-03-01 01:57:36 +02:00
|
|
|
if (r_debug_is_dead (dbg))
|
|
|
|
return R_FALSE;
|
2010-02-28 22:58:21 +01:00
|
|
|
if (dbg->h && dbg->h->kill)
|
2012-12-07 12:03:53 +01:00
|
|
|
ret = dbg->h->kill (dbg, pid, tid, sig);
|
2010-02-28 22:58:21 +01:00
|
|
|
else eprintf ("Backend does not implements kill()\n");
|
|
|
|
return ret;
|
2009-04-01 22:44:43 +00:00
|
|
|
}
|
2010-02-02 11:09:52 +01:00
|
|
|
|
2012-06-14 02:18:15 +02:00
|
|
|
R_API RList *r_debug_frames (RDebug *dbg, ut64 at) {
|
2010-03-02 11:18:49 +01:00
|
|
|
if (dbg && dbg->h && dbg->h->frames)
|
2012-06-14 02:18:15 +02:00
|
|
|
return dbg->h->frames (dbg, at);
|
2010-03-02 11:18:49 +01:00
|
|
|
return NULL;
|
2010-02-02 11:09:52 +01:00
|
|
|
}
|
2010-11-18 11:41:17 +01:00
|
|
|
|
|
|
|
/* TODO: Implement fork and clone */
|
2013-07-15 02:51:55 +02:00
|
|
|
R_API int r_debug_child_fork (RDebug *dbg) {
|
2010-11-18 11:41:17 +01:00
|
|
|
//if (dbg && dbg->h && dbg->h->frames)
|
|
|
|
//return dbg->h->frames (dbg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-15 02:51:55 +02:00
|
|
|
R_API int r_debug_child_clone (RDebug *dbg) {
|
2010-11-18 11:41:17 +01:00
|
|
|
//if (dbg && dbg->h && dbg->h->frames)
|
|
|
|
//return dbg->h->frames (dbg);
|
|
|
|
return 0;
|
|
|
|
}
|
2011-05-20 20:42:25 +02:00
|
|
|
|
|
|
|
R_API int r_debug_is_dead (RDebug *dbg) {
|
|
|
|
return (dbg->pid == -1);
|
|
|
|
}
|
2011-11-25 04:32:32 +01:00
|
|
|
|
|
|
|
R_API int r_debug_map_protect (RDebug *dbg, ut64 addr, int size, int perms) {
|
|
|
|
if (dbg && dbg->h && dbg->h->map_protect)
|
|
|
|
return dbg->h->map_protect (dbg, addr, size, perms);
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
2014-01-28 04:38:02 +01:00
|
|
|
|
|
|
|
R_API void r_debug_drx_list (RDebug *dbg) {
|
|
|
|
if (dbg && dbg->h && dbg->h->drx)
|
|
|
|
dbg->h->drx (dbg, 0, 0, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API int r_debug_drx_set (RDebug *dbg, int idx, ut64 addr, int len, int rwx, int g) {
|
|
|
|
if (dbg && dbg->h && dbg->h->drx)
|
|
|
|
return dbg->h->drx (dbg, idx, addr, len, rwx, g);
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API int r_debug_drx_unset (RDebug *dbg, int idx) {
|
|
|
|
if (dbg && dbg->h && dbg->h->drx)
|
|
|
|
return dbg->h->drx (dbg, idx, 0, -1, 0, 0);
|
|
|
|
return R_FALSE;
|
|
|
|
}
|