2013-04-16 04:01:39 +02:00
|
|
|
/* radare - LGPL - Copyright 2009-2013 - pancake */
|
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);
|
|
|
|
|
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-04-23 03:54:06 +02:00
|
|
|
R_API struct r_debug_t *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..
|
2009-09-09 00:35:00 +00: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;
|
2011-05-20 20:42:25 +02:00
|
|
|
if (r_debug_is_dead (dbg))
|
|
|
|
return R_FALSE;
|
2010-01-21 02:38:52 +01:00
|
|
|
if (dbg && 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
|
|
|
// XXX: very experimental
|
|
|
|
R_API int r_debug_step_soft(RDebug *dbg) {
|
2012-07-06 02:17:44 +02:00
|
|
|
int ret;
|
2010-11-11 03:13:44 +01:00
|
|
|
ut8 buf[32];
|
|
|
|
RAnalOp op;
|
|
|
|
ut64 pc0, pc1, pc2;
|
2011-05-20 20:42:25 +02:00
|
|
|
if (r_debug_is_dead (dbg))
|
|
|
|
return R_FALSE;
|
2010-11-11 03:13:44 +01:00
|
|
|
pc0 = r_debug_reg_get (dbg, dbg->reg->name[R_REG_NAME_PC]);
|
2012-07-06 02:17:44 +02:00
|
|
|
dbg->iob.read_at (dbg->iob.io, pc0, buf, sizeof (buf));
|
|
|
|
ret = r_anal_op (dbg->anal, &op, pc0, buf, sizeof (buf));
|
2012-07-06 19:30:17 +02:00
|
|
|
//eprintf ("read from pc0 = 0x%llx\n", pc0);
|
2013-12-06 05:18:57 +01:00
|
|
|
pc1 = pc0 + op.size;
|
2012-07-06 19:30:17 +02:00
|
|
|
//eprintf ("oplen = %d\n", op.length);
|
|
|
|
//eprintf ("breakpoint at pc1 = 0x%llx\n", pc1);
|
2010-11-11 03:13:44 +01:00
|
|
|
// XXX: Does not works for 'ret'
|
2012-07-06 19:30:17 +02:00
|
|
|
pc2 = op.jump? op.jump: 0;
|
2013-03-19 02:36:14 +00:00
|
|
|
//eprintf ("ADD SECOND BREAKPOINT FRO CALLS %llx\n", op.jump);
|
2012-07-06 19:30:17 +02:00
|
|
|
//eprintf ("breakpoint 2 at pc2 = 0x%llx\n", pc2);
|
2010-11-11 03:13:44 +01:00
|
|
|
|
|
|
|
r_bp_add_sw (dbg->bp, pc1, 4, R_BP_PROT_EXEC);
|
2013-03-19 02:36:14 +00:00
|
|
|
if (pc2) r_bp_add_sw (dbg->bp, pc2, 4, R_BP_PROT_EXEC);
|
2010-11-11 03:13:44 +01:00
|
|
|
r_debug_continue (dbg);
|
2012-07-12 02:47:04 +02:00
|
|
|
//eprintf ("wait\n");
|
2012-07-06 19:30:17 +02:00
|
|
|
//r_debug_wait (dbg);
|
2012-07-12 02:47:04 +02:00
|
|
|
//eprintf ("del\n");
|
2010-11-11 03:13:44 +01:00
|
|
|
r_bp_del (dbg->bp, pc1);
|
|
|
|
if (pc2) r_bp_del (dbg->bp, pc2);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2012-05-30 11:14:41 +02:00
|
|
|
r_debug_wait (dbg);
|
|
|
|
/* return value ignored? */
|
|
|
|
return R_TRUE;
|
2010-11-11 03:13:44 +01:00
|
|
|
}
|
|
|
|
|
2009-02-05 22:08:46 +01:00
|
|
|
// TODO: count number of steps done to check if no error??
|
2010-09-23 20:42:35 +02:00
|
|
|
R_API int r_debug_step(RDebug *dbg, int steps) {
|
2009-02-05 22:08:46 +01:00
|
|
|
int i, ret = R_FALSE;
|
2010-01-21 02:38:52 +01:00
|
|
|
if (dbg && dbg->h && dbg->h->step) {
|
2011-11-25 04:32:32 +01:00
|
|
|
for (i=0; i<steps; i++) {
|
|
|
|
ret = (dbg->swstep)?
|
|
|
|
r_debug_step_soft (dbg):
|
|
|
|
r_debug_step_hard (dbg);
|
2009-02-18 01:43:57 +01:00
|
|
|
// TODO: create wrapper for dbg_wait
|
|
|
|
// TODO: check return value of wait and show error
|
2010-11-11 03:13:44 +01:00
|
|
|
if (ret)
|
|
|
|
dbg->steps++;
|
2009-02-05 22:08:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
ut8 buf[64];
|
|
|
|
int ret = -1;
|
2011-05-20 20:42:25 +02:00
|
|
|
if (r_debug_is_dead (dbg))
|
|
|
|
return R_FALSE;
|
2011-10-09 04:15:32 +02:00
|
|
|
if (dbg->h && dbg->h->step_over) {
|
|
|
|
if (steps<1) steps = 1;
|
|
|
|
while (steps--)
|
|
|
|
if (!dbg->h->step_over (dbg))
|
|
|
|
return R_FALSE;
|
|
|
|
return R_TRUE;
|
|
|
|
}
|
2010-06-22 20:27:14 +02:00
|
|
|
if (dbg->anal && dbg->reg) {
|
2010-05-24 17:51:51 +02:00
|
|
|
ut64 pc = r_debug_reg_get (dbg, dbg->reg->name[R_REG_NAME_PC]);
|
|
|
|
dbg->iob.read_at (dbg->iob.io, pc, buf, sizeof (buf));
|
2011-02-24 14:06:49 +01:00
|
|
|
r_anal_op (dbg->anal, &op, pc, buf, sizeof (buf));
|
2011-06-09 01:20:02 +02:00
|
|
|
if (op.type & R_ANAL_OP_TYPE_CALL
|
|
|
|
|| op.type & R_ANAL_OP_TYPE_UCALL) {
|
2013-12-06 05:18:57 +01:00
|
|
|
ut64 bpaddr = pc + op.size;
|
2010-05-24 17:51:51 +02:00
|
|
|
r_bp_add_sw (dbg->bp, bpaddr, 1, R_BP_PROT_EXEC);
|
|
|
|
ret = r_debug_continue (dbg);
|
|
|
|
r_bp_del (dbg->bp, bpaddr);
|
2012-05-30 11:14:41 +02:00
|
|
|
} else {
|
|
|
|
ret = r_debug_step (dbg, 1);
|
|
|
|
}
|
2010-09-23 20:42:35 +02:00
|
|
|
} else eprintf ("Undefined debugger backend\n");
|
2010-05-24 17:51:51 +02:00
|
|
|
return ret;
|
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;
|
2011-05-20 20:42:25 +02:00
|
|
|
if (r_debug_is_dead (dbg))
|
|
|
|
return R_FALSE;
|
2010-01-21 02:38:52 +01:00
|
|
|
if (dbg && dbg->h && dbg->h->cont) {
|
|
|
|
r_bp_restore (dbg->bp, R_FALSE); // 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);
|
2010-01-21 02:38:52 +01:00
|
|
|
r_bp_restore (dbg->bp, R_TRUE); // unset sw breakpoints
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-05-30 11:14:41 +02:00
|
|
|
/* optimization: avoid so many reads */
|
2010-05-24 17:51:51 +02:00
|
|
|
R_API int r_debug_continue_until_optype(RDebug *dbg, int type, int over) {
|
2012-05-30 11:14:41 +02:00
|
|
|
int (*step)(RDebug *d, int n);
|
2010-05-24 17:51:51 +02:00
|
|
|
int ret, n = 0;
|
2012-05-30 11:14:41 +02:00
|
|
|
ut64 pc = 0;
|
2010-05-24 12:07:54 +02:00
|
|
|
RAnalOp op;
|
|
|
|
ut8 buf[64];
|
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;
|
2012-05-30 11:14:41 +02:00
|
|
|
if (dbg->anal && dbg->reg) {
|
|
|
|
const char *pcreg = dbg->reg->name[R_REG_NAME_PC];
|
|
|
|
step = over? r_debug_step_over: r_debug_step;
|
|
|
|
for (;;) {
|
|
|
|
pc = r_debug_reg_get (dbg, pcreg);
|
|
|
|
dbg->iob.read_at (dbg->iob.io, pc, buf, sizeof (buf));
|
|
|
|
ret = r_anal_op (dbg->anal, &op, pc, buf, sizeof (buf));
|
|
|
|
if (ret>0 && op.type&type)
|
|
|
|
break;
|
|
|
|
if (!step (dbg, 1)) {
|
2010-09-23 20:42:35 +02:00
|
|
|
eprintf ("r_debug_step: failed\n");
|
2010-05-24 12:07:54 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
n++;
|
2012-05-30 11:14:41 +02:00
|
|
|
}
|
2010-09-23 20:42:35 +02:00
|
|
|
} else eprintf ("Undefined pointer at dbg->anal\n");
|
2010-05-24 12:07:54 +02:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2010-02-28 22:58:21 +01:00
|
|
|
R_API int r_debug_continue_until(struct r_debug_t *dbg, ut64 addr) {
|
2010-05-24 12:07:54 +02:00
|
|
|
// TODO: use breakpoint+continue... more efficient
|
|
|
|
int n = 0;
|
|
|
|
ut64 pc = 0;
|
2011-05-20 20:42:25 +02:00
|
|
|
if (r_debug_is_dead (dbg))
|
|
|
|
return R_FALSE;
|
2010-05-24 12:07:54 +02:00
|
|
|
do {
|
2010-06-27 21:33:32 +02:00
|
|
|
if (pc !=0) r_debug_step (dbg, 1);
|
2010-05-24 12:07:54 +02:00
|
|
|
n++;
|
2011-05-20 20:42:25 +02:00
|
|
|
} while (pc != addr && !r_debug_is_dead (dbg));
|
2010-05-24 12:07:54 +02:00
|
|
|
return n;
|
2009-08-14 00:37:18 +00:00
|
|
|
//struct r_debug_bp_t *bp = r_debug_bp_add (dbg, addr);
|
|
|
|
//int ret = r_debug_continue(dbg);
|
|
|
|
/* TODO: check if the debugger stops at the right address */
|
|
|
|
//r_debug_bp_del(dbg, bp);
|
2010-05-24 12:07:54 +02:00
|
|
|
//return -1;
|
2009-02-05 22:08:46 +01:00
|
|
|
}
|
|
|
|
|
2010-09-20 14:02:45 +02:00
|
|
|
// XXX: this function uses 'oeax' which is linux-i386-specific
|
2010-02-28 22:58:21 +01:00
|
|
|
R_API int r_debug_continue_syscall(struct r_debug_t *dbg, int sc) {
|
2010-03-04 01:46:25 +01:00
|
|
|
int reg, ret = R_FALSE;
|
2011-05-20 20:42:25 +02:00
|
|
|
if (r_debug_is_dead (dbg))
|
|
|
|
return R_FALSE;
|
2010-05-24 12:07:54 +02:00
|
|
|
if (dbg && dbg->h) {
|
|
|
|
if (dbg->h->contsc) {
|
|
|
|
do {
|
2011-04-03 16:38:24 +02:00
|
|
|
ret = dbg->h->contsc (dbg, dbg->pid, sc);
|
2010-05-24 12:07:54 +02:00
|
|
|
if (!r_debug_reg_sync (dbg, R_REG_TYPE_GPR, R_FALSE)) {
|
|
|
|
eprintf ("--> eol\n");
|
2011-10-09 04:15:32 +02:00
|
|
|
sc = 0;
|
2010-05-24 12:07:54 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
reg = (int)r_debug_reg_get (dbg, "oeax"); // XXX
|
|
|
|
eprintf ("--> syscall %d\n", reg);
|
2010-09-23 20:42:35 +02:00
|
|
|
if (reg == 0LL)
|
2010-05-24 12:07:54 +02:00
|
|
|
break;
|
|
|
|
// TODO: must use r_core_cmd(as)..import code from rcore
|
|
|
|
} while (sc != 0 && sc != reg);
|
|
|
|
} else {
|
2010-05-24 17:51:51 +02:00
|
|
|
r_debug_continue_until_optype (dbg, R_ANAL_OP_TYPE_SWI, 0);
|
2010-03-04 01:46:25 +01:00
|
|
|
reg = (int)r_debug_reg_get (dbg, "oeax"); // XXX
|
|
|
|
eprintf ("--> syscall %d\n", reg);
|
2010-05-24 12:07:54 +02:00
|
|
|
}
|
2010-03-04 01:46:25 +01:00
|
|
|
}
|
2009-02-05 22:08:46 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|