2010-03-12 18:11:43 +00:00
|
|
|
/* radare - LGPL - Copyright 2008-2010 pancake<nopcode.org> */
|
|
|
|
|
|
|
|
#include <r_debug.h>
|
|
|
|
|
2010-06-03 22:56:44 +00:00
|
|
|
R_API RDebugTrace *r_debug_trace_new () {
|
|
|
|
RDebugTrace *t = R_NEW (RDebugTrace);
|
2010-06-04 21:47:35 +00:00
|
|
|
t->tag = 1; // UT32_MAX;
|
2010-06-13 09:59:17 +00:00
|
|
|
t->addresses = NULL;
|
2010-06-03 22:56:44 +00:00
|
|
|
t->enabled = R_FALSE;
|
|
|
|
t->traces = r_list_new ();
|
|
|
|
t->traces->free = free;
|
|
|
|
return t;
|
2010-03-12 18:11:43 +00:00
|
|
|
}
|
|
|
|
|
2010-06-03 22:56:44 +00:00
|
|
|
R_API void r_debug_trace_free (RDebug *dbg) {
|
|
|
|
if (dbg->trace == NULL)
|
|
|
|
return;
|
|
|
|
r_list_destroy (dbg->trace->traces);
|
|
|
|
free (dbg->trace);
|
|
|
|
dbg->trace = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: added overlap/mask support here... wtf?
|
|
|
|
// TODO: think about tagged traces
|
|
|
|
R_API int r_debug_trace_tag (RDebug *dbg, int tag) {
|
|
|
|
//if (tag>0 && tag<31) core->dbg->trace->tag = 1<<(sz-1);
|
|
|
|
return (dbg->trace->tag = (tag>0)? tag: UT32_MAX);
|
2010-03-12 18:11:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
R_API int r_debug_trace_pc (RDebug *dbg) {
|
2010-06-04 21:47:35 +00:00
|
|
|
ut8 buf[32];
|
2010-03-12 18:11:43 +00:00
|
|
|
RRegisterItem *ri;
|
|
|
|
r_debug_reg_sync (dbg, R_REG_TYPE_GPR, R_FALSE);
|
2010-06-04 21:47:35 +00:00
|
|
|
if ((ri = r_reg_get (dbg->reg, dbg->reg->name[R_REG_NAME_PC], -1))) {
|
2010-03-12 18:11:43 +00:00
|
|
|
ut64 addr = r_reg_get_value (dbg->reg, ri);
|
2010-06-04 21:47:35 +00:00
|
|
|
if (dbg->iob.read_at (dbg->iob.io, addr, buf, sizeof (buf))>0) {
|
|
|
|
RAnalOp aop;
|
|
|
|
if (r_anal_aop (dbg->anal, &aop, addr, buf, sizeof (buf))>0) {
|
|
|
|
r_debug_trace_add (dbg, addr, aop.length);
|
|
|
|
return R_TRUE;
|
|
|
|
} else eprintf ("trace_pc: cannot get opcode size at 0x%"PFMT64x"\n", addr);
|
|
|
|
} else eprintf ("trace_pc: cannot read memory at 0x%"PFMT64x"\n", addr);
|
|
|
|
} else eprintf ("trace_pc: cannot get program counter\n");
|
2010-03-12 18:11:43 +00:00
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
|
2010-06-13 09:59:17 +00:00
|
|
|
R_API void r_debug_trace_at(RDebug *dbg, const char *str) {
|
|
|
|
// TODO: parse offsets and so use ut64 instead of strstr()
|
|
|
|
free (dbg->trace->addresses);
|
|
|
|
dbg->trace->addresses = (str&&*str)? strdup (str): NULL;
|
|
|
|
}
|
|
|
|
|
2010-06-04 21:47:35 +00:00
|
|
|
R_API RDebugTracepoint *r_debug_trace_get (RDebug *dbg, ut64 addr) {
|
2010-03-12 18:11:43 +00:00
|
|
|
/* TODO: handle opcode size .. warn when jumping in the middle of instructions */
|
2010-06-04 21:47:35 +00:00
|
|
|
int tag = dbg->trace->tag;
|
2010-06-03 22:56:44 +00:00
|
|
|
RListIter *iter = r_list_iterator (dbg->trace->traces);
|
2010-03-12 18:11:43 +00:00
|
|
|
while (r_list_iter_next (iter)) {
|
2010-06-03 22:56:44 +00:00
|
|
|
RDebugTracepoint *trace = (RDebugTracepoint *)r_list_iter_get (iter);
|
|
|
|
if (tag != 0 && !(dbg->trace->tag & (1<<tag)))
|
2010-03-12 18:11:43 +00:00
|
|
|
continue;
|
|
|
|
if (trace->addr == addr)
|
|
|
|
return trace;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-06-04 21:47:35 +00:00
|
|
|
R_API void r_debug_trace_list (RDebug *dbg, int mode) {
|
|
|
|
int tag = dbg->trace->tag;
|
2010-06-03 22:56:44 +00:00
|
|
|
RListIter *iter = r_list_iterator (dbg->trace->traces);
|
2010-03-12 18:11:43 +00:00
|
|
|
while (r_list_iter_next (iter)) {
|
2010-06-03 22:56:44 +00:00
|
|
|
RDebugTracepoint *trace = r_list_iter_get (iter);
|
2010-06-04 21:47:35 +00:00
|
|
|
if (!trace->tag || (tag & trace->tag)) {
|
|
|
|
if (mode == 1)
|
|
|
|
dbg->printf ("at+ 0x%"PFMT64x" %d\n", trace->addr, trace->times);
|
|
|
|
else dbg->printf ("0x%08"PFMT64x" size=%d count=%d times=%d tag=%d\n",
|
|
|
|
trace->addr, trace->size, trace->count, trace->times, trace->tag);
|
|
|
|
}
|
2010-03-12 18:11:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-13 09:59:17 +00:00
|
|
|
// XXX: find better name, make it public?
|
|
|
|
static int r_debug_trace_is_traceable(RDebug *dbg, ut64 addr) {
|
|
|
|
int ret = R_TRUE;
|
|
|
|
char addr_str[32];
|
|
|
|
if (dbg->trace->addresses) {
|
|
|
|
snprintf (addr_str, sizeof (addr_str), "0x%08"PFMT64x, addr);
|
|
|
|
ret = strstr (dbg->trace->addresses, addr_str);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-03-12 18:11:43 +00:00
|
|
|
/* sort insert, or separated sort function ? */
|
|
|
|
/* TODO: detect if inner opcode */
|
2010-06-04 21:47:35 +00:00
|
|
|
R_API RDebugTracepoint *r_debug_trace_add (RDebug *dbg, ut64 addr, int size) {
|
2010-06-13 09:59:17 +00:00
|
|
|
RDebugTracepoint *tp;
|
2010-06-04 21:47:35 +00:00
|
|
|
int tag = dbg->trace->tag;
|
2010-06-13 09:59:17 +00:00
|
|
|
if (!r_debug_trace_is_traceable (dbg, addr))
|
|
|
|
return NULL;
|
|
|
|
tp = r_debug_trace_get (dbg, addr);
|
2010-06-03 22:56:44 +00:00
|
|
|
if (!tp) {
|
|
|
|
tp = R_NEW (RDebugTracepoint);
|
2010-06-04 21:47:35 +00:00
|
|
|
memset (tp, 0, sizeof (RDebugTracepoint));
|
2010-06-03 22:56:44 +00:00
|
|
|
tp->stamp = r_sys_now ();
|
|
|
|
tp->addr = addr;
|
|
|
|
tp->tags = tag;
|
|
|
|
tp->size = size;
|
2010-06-04 21:47:35 +00:00
|
|
|
tp->count = dbg->trace->count++;
|
2010-06-17 00:22:50 +00:00
|
|
|
tp->times = 1;
|
2010-06-03 22:56:44 +00:00
|
|
|
r_list_append (dbg->trace->traces, tp);
|
2010-06-04 21:47:35 +00:00
|
|
|
} else tp->times++;
|
2010-06-03 22:56:44 +00:00
|
|
|
return tp;
|
2010-03-12 18:11:43 +00:00
|
|
|
}
|