2011-03-24 11:03:23 +01:00
|
|
|
/* radare - LGPL - Copyright 2009-2011 pancake<nopcode.org> */
|
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>
|
|
|
|
|
|
|
|
/* 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);
|
2010-06-16 09:42:46 +02:00
|
|
|
eprintf ("Recoil at 0x%"PFMT64x" = %d\n", addr, recoil);
|
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);
|
|
|
|
r_debug_reg_sync (dbg, R_REG_TYPE_GPR, R_TRUE);
|
2011-05-11 20:08:19 +02:00
|
|
|
eprintf ("[BP Hit] Setting pc to 0x%"PFMT64x"\n", (addr-recoil));
|
2011-05-20 20:42:25 +02:00
|
|
|
return R_TRUE;
|
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;
|
2010-09-23 20:42:35 +02:00
|
|
|
dbg->swstep = 0;
|
2010-01-19 11:25:17 +01:00
|
|
|
dbg->newstate = 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 ();
|
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
|
|
|
}
|
|
|
|
|
2010-02-22 12:42:43 +01:00
|
|
|
R_API struct r_debug_t *r_debug_free(struct r_debug_t *dbg) {
|
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);
|
2010-05-26 18:25:35 +02:00
|
|
|
//r_debug_plugin_free();
|
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);
|
2010-01-21 02:38:52 +01:00
|
|
|
} else eprintf ("Cannot attach to this pid\n");
|
|
|
|
} else eprintf ("dbg->attach = NULL\n");
|
2009-02-05 22:08:46 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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-05-06 19:56:16 +02:00
|
|
|
dbg->arch = arch;
|
2011-05-06 00:59:10 +02:00
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
}
|
2011-04-04 18:33:27 +02:00
|
|
|
eprintf ("arch (%s) not supported by debug backend (%s)\n",
|
|
|
|
r_sys_arch_str (arch), dbg->h->name);
|
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
|
|
|
|
*/
|
2010-02-22 12:42:43 +01:00
|
|
|
R_API ut64 r_debug_execute(struct r_debug_t *dbg, ut8 *buf, int len) {
|
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);
|
|
|
|
risp = r_reg_get (dbg->reg, dbg->reg->name[R_REG_NAME_PC], R_REG_TYPE_GPR);
|
|
|
|
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);
|
|
|
|
if (backup == NULL)
|
|
|
|
return 0LL;
|
|
|
|
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);
|
|
|
|
r_debug_continue (dbg);
|
|
|
|
/* 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);
|
|
|
|
dbg->iob.write_at (dbg->iob.io, rsp, stackbackup, len);
|
|
|
|
|
|
|
|
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);
|
|
|
|
r_reg_set_bytes (dbg->reg, -1, orig, orig_sz);
|
|
|
|
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) {
|
2009-02-05 22:08:46 +01:00
|
|
|
dbg->pid = pid;
|
|
|
|
dbg->tid = tid;
|
2010-04-11 01:46:07 +02:00
|
|
|
eprintf ("r_debug_select: %d %d\n", pid, tid);
|
2009-02-05 22:08:46 +01:00
|
|
|
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;
|
2010-05-24 17:51:51 +02:00
|
|
|
ret = dbg->h->wait (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");
|
|
|
|
r_debug_select (dbg, -1, -1); //dbg->pid = -1;
|
|
|
|
}
|
|
|
|
//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);
|
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) {
|
|
|
|
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]);
|
2011-02-24 14:06:49 +01:00
|
|
|
int ret = r_anal_op (dbg->anal, &op, pc0, buf, sizeof (buf));
|
2010-11-11 03:13:44 +01:00
|
|
|
pc1 = pc0 + op.length;
|
|
|
|
// XXX: Does not works for 'ret'
|
|
|
|
pc2 = op.jump?op.jump:0;
|
|
|
|
|
|
|
|
r_bp_add_sw (dbg->bp, pc1, 4, R_BP_PROT_EXEC);
|
|
|
|
if (pc2) r_bp_add_sw (dbg->bp, pc2, 4, R_BP_PROT_EXEC);
|
|
|
|
r_debug_continue (dbg);
|
|
|
|
r_debug_wait (dbg);
|
|
|
|
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;
|
|
|
|
return r_debug_wait (dbg);
|
|
|
|
}
|
|
|
|
|
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) {
|
2010-03-12 18:46:11 +01:00
|
|
|
for (i=0;i<steps;i++) {
|
2010-11-11 03:13:44 +01:00
|
|
|
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;
|
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));
|
2010-05-24 17:51:51 +02:00
|
|
|
if (op.type & R_ANAL_OP_TYPE_CALL) {
|
|
|
|
ut64 bpaddr = pc + op.length;
|
|
|
|
r_bp_add_sw (dbg->bp, bpaddr, 1, R_BP_PROT_EXEC);
|
|
|
|
ret = r_debug_continue (dbg);
|
|
|
|
r_bp_del (dbg->bp, bpaddr);
|
|
|
|
} 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-11-24 23:19:17 +01:00
|
|
|
R_API int r_debug_kill_setup(RDebug *dbg, int sig, int action) {
|
|
|
|
// TODO: implement r_debug_kill_setup
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
|
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);
|
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)
|
2010-03-11 00:51:32 +01:00
|
|
|
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) {
|
2010-01-19 11:25:17 +01:00
|
|
|
return r_debug_continue_kill (dbg, -1);
|
|
|
|
}
|
|
|
|
|
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;
|
2010-05-24 12:07:54 +02:00
|
|
|
RAnalOp op;
|
|
|
|
ut8 buf[64];
|
|
|
|
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
|
|
|
if (dbg->anal) {
|
|
|
|
do {
|
2010-05-24 17:51:51 +02:00
|
|
|
if (over) ret = r_debug_step_over (dbg, 1);
|
|
|
|
else ret = r_debug_step (dbg, 1);
|
|
|
|
if (!ret) {
|
2010-09-23 20:42:35 +02:00
|
|
|
eprintf ("r_debug_step: failed\n");
|
2010-05-24 12:07:54 +02:00
|
|
|
break;
|
|
|
|
}
|
2010-05-24 17:51:51 +02:00
|
|
|
pc = r_debug_reg_get (dbg, dbg->reg->name[R_REG_NAME_PC]);
|
2010-05-24 12:07:54 +02:00
|
|
|
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));
|
2010-05-24 12:07:54 +02:00
|
|
|
n++;
|
2010-05-24 17:51:51 +02:00
|
|
|
} while (!(op.type&type));
|
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");
|
|
|
|
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!
|
2010-02-28 22:58:21 +01:00
|
|
|
R_API int r_debug_syscall(struct r_debug_t *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
|
|
|
}
|
|
|
|
|
2010-11-17 00:56:48 +01:00
|
|
|
R_API int r_debug_kill(struct r_debug_t *dbg, boolt thread, int sig) {
|
2010-02-28 22:58:21 +01:00
|
|
|
int ret = R_FALSE;
|
|
|
|
if (dbg->h && dbg->h->kill)
|
2010-11-17 00:56:48 +01:00
|
|
|
ret = dbg->h->kill (dbg, thread, 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
|
|
|
|
2010-03-02 11:18:49 +01:00
|
|
|
R_API RList *r_debug_frames (RDebug *dbg) {
|
|
|
|
if (dbg && dbg->h && dbg->h->frames)
|
|
|
|
return dbg->h->frames (dbg);
|
|
|
|
return NULL;
|
2010-02-02 11:09:52 +01:00
|
|
|
}
|
2010-11-18 11:41:17 +01:00
|
|
|
|
|
|
|
/* TODO: Implement fork and clone */
|
|
|
|
R_API int r_debug_fork (RDebug *dbg) {
|
|
|
|
//if (dbg && dbg->h && dbg->h->frames)
|
|
|
|
//return dbg->h->frames (dbg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API int r_debug_clone (RDebug *dbg) {
|
|
|
|
//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);
|
|
|
|
}
|