Indentation fixes around the previous commit and beyond

This commit is contained in:
pancake 2016-05-15 10:40:57 +02:00
parent 29c2670039
commit 80ea7427bb
4 changed files with 149 additions and 122 deletions

View File

@ -91,15 +91,23 @@ R_API RBreakpointItem *r_bp_get_at(RBreakpoint *bp, ut64 addr) {
return NULL;
}
static inline bool inRange(RBreakpointItem *b, ut64 addr) {
return (addr >= b->addr && addr < (b->addr + b->size));
}
static inline bool matchProt(RBreakpointItem *b, int rwx) {
return (!rwx || (rwx && b->rwx));
}
R_API RBreakpointItem *r_bp_get_in(RBreakpoint *bp, ut64 addr, int rwx) {
RBreakpointItem *b;
RListIter *iter;
r_list_foreach (bp->bps, iter, b) {
// eprintf ("---ataddr--- 0x%08"PFMT64x" %d %d %x\n", b->addr, b->size, b->recoil, b->rwx);
// Check addr within range and provided rwx matches (or null)
if (addr >= b->addr && addr < (b->addr+b->size) && \
(!rwx || rwx&b->rwx))
if (inRange (b, addr) && matchProt (b, rwx)) {
return b;
}
}
return NULL;
}
@ -131,8 +139,9 @@ R_API int r_bp_stepy_continuation(RBreakpoint *bp) {
static RBreakpointItem *r_bp_add(RBreakpoint *bp, const ut8 *obytes, ut64 addr, int size, int hw, int rwx) {
int ret;
RBreakpointItem *b;
if (addr == UT64_MAX || size < 1)
if (addr == UT64_MAX || size < 1) {
return NULL;
}
if (r_bp_get_in (bp, addr, rwx)) {
eprintf ("Breakpoint already set at this address.\n");
return NULL;
@ -174,15 +183,16 @@ R_API int r_bp_add_fault(RBreakpoint *bp, ut64 addr, int size, int rwx) {
R_API RBreakpointItem* r_bp_add_sw(RBreakpoint *bp, ut64 addr, int size, int rwx) {
RBreakpointItem *item;
ut8 *bytes;
if (size < 1)
if (size < 1) {
size = 1;
bytes = calloc (1, size);
if (bytes == NULL)
}
if (!(bytes = calloc (1, size))) {
return NULL;
if (bp->iob.read_at)
}
memset (bytes, 0, size);
if (bp->iob.read_at) {
bp->iob.read_at (bp->iob.io, addr, bytes, size);
else
memset (bytes, 0, size);
}
item = r_bp_add (bp, bytes, addr, size, R_BP_TYPE_SW, rwx);
free (bytes);
return item;
@ -193,10 +203,11 @@ R_API RBreakpointItem* r_bp_add_hw(RBreakpoint *bp, ut64 addr, int size, int rwx
}
R_API int r_bp_del_all(RBreakpoint *bp) {
if (r_list_empty (bp->bps))
return false;
r_list_purge (bp->bps);
return true;
if (!r_list_empty (bp->bps)) {
r_list_purge (bp->bps);
return true;
}
return false;
}
R_API int r_bp_del(RBreakpoint *bp, ut64 addr) {
@ -301,14 +312,15 @@ R_API RBreakpointItem *r_bp_item_new (RBreakpoint *bp) {
for (j = i; j < bp->bps_idx_count; j++) {
bp->bps_idx[j] = NULL;
}
return_slot:
return_slot:
/* empty slot */
return (bp->bps_idx[i] = R_NEW0 (RBreakpointItem));
}
R_API RBreakpointItem *r_bp_get_index(RBreakpoint *bp, int idx) {
if (idx >= 0 && idx < bp->bps_idx_count)
if (idx >= 0 && idx < bp->bps_idx_count) {
return bp->bps_idx[idx];
}
return NULL;
}

View File

@ -1,10 +1,8 @@
/* radare - LGPL - Copyright 2009-2011 pancake<nopcode.org> */
/* radare - LGPL - Copyright 2009-2016 pancake */
#include <r_bp.h>
#include "../config.h"
// TODO: rename from r_debug_ ...
/**
* reflect all r_bp stuff in the process using dbg->bp_write or ->breakpoint
*/
@ -17,31 +15,38 @@ R_API int r_bp_restore(RBreakpoint *bp, int set) {
*
* except the specified breakpoint...
*/
R_API int r_bp_restore_except(RBreakpoint *bp, int set, ut64 addr) {
R_API bool r_bp_restore_except(RBreakpoint *bp, int set, ut64 addr) {
bool rc = false;
RListIter *iter;
RBreakpointItem *b;
r_list_foreach (bp->bps, iter, b) {
if (addr && b->addr == addr)
if (addr && b->addr == addr) {
continue;
if (bp->breakpoint && bp->breakpoint (b, set, bp->user))
}
if (bp->breakpoint && bp->breakpoint (b, set, bp->user)) {
continue;
}
/* write obytes from every breakpoint in r_bp if not handled by plugin */
if (set) {
//eprintf ("Setting bp at 0x%08"PFMT64x"\n", b->addr);
if (b->hw || !b->bbytes)
if (b->hw || !b->bbytes) {
eprintf ("hw breakpoints not yet supported\n");
else
} else {
bp->iob.write_at (bp->iob.io, b->addr, b->bbytes, b->size);
rc = true;
}
} else {
//eprintf ("Clearing bp at 0x%08"PFMT64x"\n", b->addr);
if (b->hw || !b->obytes)
if (b->hw || !b->obytes) {
eprintf ("hw breakpoints not yet supported\n");
else
} else {
bp->iob.write_at (bp->iob.io, b->addr, b->obytes, b->size);
rc = true;
}
}
}
return true;
return rc;
}
R_API int r_bp_recoil(RBreakpoint *bp, ut64 addr) {
@ -50,8 +55,9 @@ R_API int r_bp_recoil(RBreakpoint *bp, ut64 addr) {
//eprintf("HIT AT ADDR 0x%"PFMT64x"\n", addr);
//eprintf(" recoil = %d\n", b->recoil);
//eprintf(" size = %d\n", b->size);
if (!b->hw && b->addr == addr)
if (!b->hw && b->addr == addr) {
return b->recoil;
}
}
return 0;
}

View File

@ -1,16 +1,21 @@
/* radare - LGPL - Copyright 2009-2015 - pancake, TheLemonMan */
/* radare - LGPL - Copyright 2009-2016 - pancake, jduck, TheLemonMan */
#include <r_debug.h>
#include <signal.h>
#if __WINDOWS__
void w32_break_process(void *);
#endif
R_LIB_VERSION(r_debug);
// Size of the lookahead buffers used in r_debug functions
#define DBG_BUF_SIZE 512
R_API RDebugInfo *r_debug_info(RDebug *dbg, const char *arg) {
if (!dbg || !dbg->h || !dbg->h->info)
if (!dbg || !dbg->h || !dbg->h->info) {
return NULL;
}
return dbg->h->info (dbg, arg);
}
@ -25,8 +30,9 @@ R_API void r_debug_info_free (RDebugInfo *rdi) {
static int r_debug_recoil(RDebug *dbg) {
int recoil;
RRegItem *ri;
if (r_debug_is_dead (dbg))
if (r_debug_is_dead (dbg)) {
return false;
}
r_debug_reg_sync (dbg, R_REG_TYPE_GPR, false);
ri = r_reg_get (dbg->reg, dbg->reg->name[R_REG_NAME_PC], -1);
dbg->reason.bpi = NULL;
@ -37,7 +43,7 @@ static int r_debug_recoil(RDebug *dbg) {
if (recoil < 1)
recoil = 0; // XXX Hack :D
if (recoil) {
dbg->in_recoil = 1;
dbg->in_recoil = true;
dbg->reason.type = R_DEBUG_REASON_BREAKPOINT;
dbg->reason.bpi = r_bp_get_at (dbg->bp, addr-recoil);
dbg->reason.addr = addr - recoil;
@ -107,26 +113,27 @@ R_API void r_debug_tracenodes_reset (RDebug *dbg) {
}
R_API RDebug *r_debug_free(RDebug *dbg) {
if (!dbg) return NULL;
// TODO: free it correctly.. we must ensure this is an instance and not a reference..
r_bp_free (dbg->bp);
//r_reg_free(&dbg->reg);
r_list_free (dbg->snaps);
r_list_free (dbg->maps);
r_list_free (dbg->maps_user);
r_list_free (dbg->threads);
r_num_free (dbg->num);
sdb_free (dbg->sgnls);
r_tree_free (dbg->tree);
sdb_foreach (dbg->tracenodes, (SdbForeachCallback)free_tracenodes_entry, dbg);
sdb_free (dbg->tracenodes);
//r_debug_plugin_free();
free (dbg->btalgo);
r_debug_trace_free (dbg);
free (dbg->arch);
free (dbg->glob_libs);
free (dbg->glob_unlibs);
free (dbg);
if (dbg) {
// TODO: free it correctly.. we must ensure this is an instance and not a reference..
r_bp_free (dbg->bp);
//r_reg_free(&dbg->reg);
r_list_free (dbg->snaps);
r_list_free (dbg->maps);
r_list_free (dbg->maps_user);
r_list_free (dbg->threads);
r_num_free (dbg->num);
sdb_free (dbg->sgnls);
r_tree_free (dbg->tree);
sdb_foreach (dbg->tracenodes, (SdbForeachCallback)free_tracenodes_entry, dbg);
sdb_free (dbg->tracenodes);
//r_debug_plugin_free();
free (dbg->btalgo);
r_debug_trace_free (dbg);
free (dbg->arch);
free (dbg->glob_libs);
free (dbg->glob_unlibs);
free (dbg);
}
return NULL;
}
@ -144,8 +151,9 @@ R_API int r_debug_attach(RDebug *dbg, int pid) {
/* stop execution of child process */
R_API int r_debug_stop(RDebug *dbg) {
if (dbg && dbg->h && dbg->h->stop)
if (dbg && dbg->h && dbg->h->stop) {
return dbg->h->stop (dbg);
}
return false;
}
@ -218,9 +226,9 @@ R_API ut64 r_debug_execute(RDebug *dbg, const ut8 *buf, int len, int restore) {
/* execute code here */
dbg->iob.write_at (dbg->iob.io, rpc, buf, len);
//r_bp_add_sw (dbg->bp, rpc+len, 4, R_BP_PROT_EXEC);
//r_bp_add_sw (dbg->bp, rpc+len, 4, R_BP_PROT_EXEC);
r_debug_continue (dbg);
//r_bp_del (dbg->bp, rpc+len);
//r_bp_del (dbg->bp, rpc+len);
/* TODO: check if stopped in breakpoint or not */
r_bp_del (dbg->bp, rpc+len);
@ -263,8 +271,9 @@ R_API int r_debug_detach(RDebug *dbg, int pid) {
}
R_API int r_debug_select(RDebug *dbg, int pid, int tid) {
if (tid < 0)
if (tid < 0) {
tid = pid;
}
if (pid != -1 && tid != -1) {
if (pid != dbg->pid || tid != dbg->tid)
@ -364,24 +373,25 @@ R_API int r_debug_step_soft(RDebug *dbg) {
ut32 r32[2];
} sp_top;
if (r_debug_is_dead (dbg))
if (r_debug_is_dead (dbg)) {
return false;
}
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) {
if (dbg->iob.read_at (dbg->iob.io, pc, buf, sizeof (buf)) < 0)
return false;
} else {
if (!dbg->iob.read_at) {
return false;
}
if (!r_anal_op (dbg->anal, &op, pc, buf, sizeof (buf)))
if (dbg->iob.read_at (dbg->iob.io, pc, buf, sizeof (buf)) < 0) {
return false;
if (op.type == R_ANAL_OP_TYPE_ILL)
}
if (!r_anal_op (dbg->anal, &op, pc, buf, sizeof (buf))) {
return false;
}
if (op.type == R_ANAL_OP_TYPE_ILL) {
return false;
}
switch (op.type) {
case R_ANAL_OP_TYPE_RET:
@ -389,72 +399,72 @@ R_API int r_debug_step_soft(RDebug *dbg) {
next[0] = (dbg->bits == R_SYS_BITS_32) ? sp_top.r32[0] : sp_top.r64;
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++)
for (i = 0; i < br; i++) {
r_bp_add_sw (dbg->bp, next[i], dbg->bpsize, R_BP_PROT_EXEC);
}
ret = r_debug_continue (dbg);
for (i = 0; i < br; i++)
for (i = 0; i < br; i++) {
r_bp_del (dbg->bp, next[i]);
}
return ret;
}
R_API int r_debug_step_hard(RDebug *dbg) {
dbg->reason.type = R_DEBUG_REASON_STEP;
if (r_debug_is_dead (dbg))
if (r_debug_is_dead (dbg)) {
return false;
if (!dbg->h->step (dbg))
}
if (!dbg->h->step (dbg)) {
return false;
}
return r_debug_wait (dbg);
}
R_API int r_debug_step(RDebug *dbg, int steps) {
int i, ret;
if (!dbg || !dbg->h)
if (!dbg || !dbg->h) {
return false;
}
dbg->reason.type = R_DEBUG_REASON_STEP;
if (r_debug_is_dead (dbg)) {
return false;
}
if (steps < 1)
if (steps < 1) {
steps = 1;
}
for (i = 0; i < steps; i++) {
ret = dbg->swstep ?
r_debug_step_soft (dbg):
r_debug_step_hard (dbg);
ret = dbg->swstep
? r_debug_step_soft (dbg)
: r_debug_step_hard (dbg);
if (!ret) {
eprintf ("Stepping failed!\n");
return false;
} else {
dbg->steps++;
dbg->reason.type = R_DEBUG_REASON_STEP;
//dbg->reason.addr =
}
dbg->steps++;
dbg->reason.type = R_DEBUG_REASON_STEP;
}
return i;
@ -521,26 +531,27 @@ R_API int r_debug_step_over(RDebug *dbg, int steps) {
eprintf ("step over failed over rep\n");
return false;
}
} else r_debug_step (dbg, 1);
} else {
r_debug_step (dbg, 1);
}
}
return i;
}
#if __WINDOWS__
void w32_break_process (void *);
#endif
R_API int r_debug_continue_kill(RDebug *dbg, int sig) {
ut64 pc;
int retwait, ret = false;
if (!dbg)
if (!dbg) {
return false;
}
#if __WINDOWS__
r_cons_break(w32_break_process, dbg);
r_cons_break (w32_break_process, dbg);
#endif
repeat:
if (r_debug_is_dead (dbg))
if (r_debug_is_dead (dbg)) {
return false;
}
if (dbg->h && dbg->h->cont) {
if (dbg->in_recoil) {
/* if we are recoiling, we should not set the breakpoint that
@ -548,9 +559,9 @@ repeat:
*/
dbg->in_recoil = false;
r_bp_restore_except (dbg->bp, true, dbg->reason.addr);
}
else
} else {
r_bp_restore (dbg->bp, true); // set sw breakpoints
}
ret = dbg->h->cont (dbg, dbg->pid, dbg->tid, sig);
dbg->reason.signum = 0;
retwait = r_debug_wait (dbg);
@ -667,9 +678,9 @@ R_API int r_debug_continue_until_optype(RDebug *dbg, int type, int over) {
if (op.type == type)
break;
// Step over and repeat
ret = over ?
r_debug_step_over (dbg, 1) :
r_debug_step (dbg, 1);
ret = over
? r_debug_step_over (dbg, 1)
: r_debug_step (dbg, 1);
if (!ret) {
eprintf ("r_debug_step: failed\n");
@ -697,7 +708,6 @@ R_API int r_debug_continue_until(RDebug *dbg, ut64 addr) {
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;
@ -705,11 +715,10 @@ R_API int r_debug_continue_until(RDebug *dbg, ut64 addr) {
break;
r_debug_continue (dbg);
}
// Clean up if needed
if (!has_bp)
if (!has_bp) {
r_bp_del (dbg->bp, addr);
}
return true;
}
@ -745,7 +754,7 @@ static int show_syscall(RDebug *dbg, const char *sysreg) {
}
R_API int r_debug_continue_syscalls(RDebug *dbg, int *sc, int n_sc) {
int i, reg, ret = false;
int i, err, reg, ret = false;
if (!dbg || !dbg->h || r_debug_is_dead (dbg))
return false;
if (!dbg->h->contsc) {
@ -758,13 +767,10 @@ R_API int r_debug_continue_syscalls(RDebug *dbg, int *sc, int n_sc) {
eprintf ("--> cannot read registers\n");
return -1;
}
{
int err;
reg = (int)r_debug_reg_get_err (dbg, "SN", &err);
if (err) {
eprintf ("Cannot find 'sn' register for current arch-os.\n");
return -1;
}
reg = (int)r_debug_reg_get_err (dbg, "SN", &err);
if (err) {
eprintf ("Cannot find 'sn' register for current arch-os.\n");
return -1;
}
for (;;) {
if (r_cons_singleton()->breaked)
@ -786,9 +792,10 @@ R_API int r_debug_continue_syscalls(RDebug *dbg, int *sc, int n_sc) {
if (n_sc == 0) {
break;
}
for (i=0; i<n_sc; i++) {
if (sc[i] == reg)
for (i = 0; i < n_sc; i++) {
if (sc[i] == reg) {
return reg;
}
}
// TODO: must use r_core_cmd(as)..import code from rcore
}
@ -801,12 +808,9 @@ R_API int r_debug_continue_syscall(RDebug *dbg, int sc) {
// TODO: remove from here? this is code injection!
R_API int r_debug_syscall(RDebug *dbg, int num) {
bool ret = false;
bool ret = true;
if (dbg->h->contsc) {
ret = dbg->h->contsc (dbg, dbg->pid, num);
} else {
ret = true;
// TODO.check for num
}
eprintf ("TODO: show syscall information\n");
/* r2rc task? ala inject? */
@ -814,18 +818,19 @@ R_API int r_debug_syscall(RDebug *dbg, int num) {
}
R_API int r_debug_kill(RDebug *dbg, int pid, int tid, int sig) {
int ret = false;
if (r_debug_is_dead (dbg))
return false;
if (dbg->h && dbg->h->kill)
ret = dbg->h->kill (dbg, pid, tid, sig);
else eprintf ("Backend does not implements kill()\n");
return ret;
if (dbg->h && dbg->h->kill) {
return dbg->h->kill (dbg, pid, tid, sig);
}
eprintf ("Backend does not implements kill()\n");
return false;
}
R_API RList *r_debug_frames(RDebug *dbg, ut64 at) {
if (dbg && dbg->h && dbg->h->frames)
if (dbg && dbg->h && dbg->h->frames) {
return dbg->h->frames (dbg, at);
}
return NULL;
}
@ -854,24 +859,28 @@ R_API int r_debug_is_dead(RDebug *dbg) {
}
R_API int r_debug_map_protect(RDebug *dbg, ut64 addr, int size, int perms) {
if (dbg && dbg->h && dbg->h->map_protect)
if (dbg && dbg->h && dbg->h->map_protect) {
return dbg->h->map_protect (dbg, addr, size, perms);
}
return false;
}
R_API void r_debug_drx_list(RDebug *dbg) {
if (dbg && dbg->h && dbg->h->drx)
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)
if (dbg && dbg->h && dbg->h->drx) {
return dbg->h->drx (dbg, idx, addr, len, rwx, g);
}
return false;
}
R_API int r_debug_drx_unset(RDebug *dbg, int idx) {
if (dbg && dbg->h && dbg->h->drx)
if (dbg && dbg->h && dbg->h->drx) {
return dbg->h->drx (dbg, idx, 0, -1, 0, 0);
}
return false;
}

View File

@ -131,7 +131,7 @@ R_API int r_bp_add_fault(RBreakpoint *bp, ut64 addr, int size, int rwx);
R_API RBreakpointItem *r_bp_add_sw(RBreakpoint *bp, ut64 addr, int size, int rwx);
R_API RBreakpointItem *r_bp_add_hw(RBreakpoint *bp, ut64 addr, int size, int rwx);
R_API int r_bp_restore(RBreakpoint *bp, int set);
R_API int r_bp_restore_except(RBreakpoint *bp, int set, ut64 addr);
R_API bool r_bp_restore_except(RBreakpoint *bp, int set, ut64 addr);
R_API int r_bp_recoil(RBreakpoint *bp, ut64 addr);
/* traptrace */