Rizin
unix-like reverse engineering framework and cli tools
bp_traptrace.c File Reference
#include <rz_bp.h>
#include <rz_list.h>

Go to the source code of this file.

Functions

RZ_API void rz_bp_traptrace_free (void *ptr)
 
RZ_API RzListrz_bp_traptrace_new (void)
 
RZ_API void rz_bp_traptrace_enable (RzBreakpoint *bp, int enable)
 
RZ_API void rz_bp_traptrace_reset (RzBreakpoint *bp, int hard)
 
RZ_API ut64 rz_bp_traptrace_next (RzBreakpoint *bp, ut64 addr)
 
RZ_API int rz_bp_traptrace_add (RzBreakpoint *bp, ut64 from, ut64 to)
 
RZ_API int rz_bp_traptrace_free_at (RzBreakpoint *bp, ut64 from)
 
RZ_API void rz_bp_traptrace_list (RzBreakpoint *bp)
 
RZ_API int rz_bp_traptrace_at (RzBreakpoint *bp, ut64 from, int len)
 

Function Documentation

◆ rz_bp_traptrace_add()

RZ_API int rz_bp_traptrace_add ( RzBreakpoint bp,
ut64  from,
ut64  to 
)

Definition at line 71 of file bp_traptrace.c.

71  {
72  RzBreakpointTrace *trace;
73  ut8 *buf, *trap, *bits;
74  ut64 len;
75  int bitlen;
76  /* cannot map addr 0 */
77  if (from == 0LL) {
78  return false;
79  }
80  if (from > to) {
81  return false;
82  }
83  len = to - from;
84  if (len >= ST32_MAX) {
85  return false;
86  }
87  buf = (ut8 *)malloc((int)len);
88  if (!buf) {
89  return false;
90  }
91  trap = (ut8 *)malloc((int)len + 4);
92  if (!trap) {
93  free(buf);
94  return false;
95  }
96  bitlen = (len >> 4) + 1;
97  bits = malloc(bitlen);
98  if (!bits) {
99  free(buf);
100  free(trap);
101  return false;
102  }
103  // TODO: check return value
104  bp->iob.read_at(bp->iob.io, from, buf, len);
105  memset(bits, 0x00, bitlen);
106  rz_bp_get_bytes(bp, from, trap, len);
107 
108  trace = RZ_NEW(RzBreakpointTrace);
109  if (!trace) {
110  free(buf);
111  free(trap);
112  free(bits);
113  return false;
114  }
115  trace->addr = from;
116  trace->addr_end = to;
117  trace->bits = bits;
118  trace->traps = trap;
119  trace->buffer = buf;
120  trace->length = len;
121  if (!rz_list_append(bp->traces, trace)) {
122  free(buf);
123  free(trap);
124  free(trace);
125  return false;
126  }
127  // read a memory, overwrite it as breakpointing area
128  // every time it is hitted, instruction is restored
129  return true;
130 }
size_t len
Definition: 6502dis.c:15
int bits(struct state *s, int need)
Definition: blast.c:72
RZ_API int rz_bp_get_bytes(RZ_NONNULL RzBreakpoint *bp, ut64 addr, RZ_NONNULL ut8 *buf, int len)
Definition: bp.c:61
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
return memset(p, 0, total)
RZ_API RZ_BORROW RzListIter * rz_list_append(RZ_NONNULL RzList *list, void *data)
Appends at the end of the list a new element.
Definition: list.c:288
void * malloc(size_t size)
Definition: malloc.c:123
#define RZ_NEW(x)
Definition: rz_types.h:285
#define ST32_MAX
Definition: rz_types_base.h:97
static struct sockaddr static addrlen static backlog const void static flags void struct sockaddr from
Definition: sfsocketcall.h:123
static struct sockaddr static addrlen static backlog const void static flags void struct sockaddr socklen_t static fromlen const void const struct sockaddr to
Definition: sfsocketcall.h:125
RzList * traces
Definition: rz_bp.h:86
RzIOBind iob
Definition: rz_bp.h:84
ut8 * bits
Definition: rz_bp.h:104
ut8 * traps
Definition: rz_bp.h:102
int length
Definition: rz_bp.h:105
ut64 addr_end
Definition: rz_bp.h:101
ut64 addr
Definition: rz_bp.h:100
ut8 * buffer
Definition: rz_bp.h:103
RzIOReadAt read_at
Definition: rz_io.h:240
RzIO * io
Definition: rz_io.h:232
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References rz_bp_trace_t::addr, rz_bp_trace_t::addr_end, rz_bp_trace_t::bits, bits(), rz_bp_trace_t::buffer, free(), from, rz_io_bind_t::io, rz_bp_t::iob, len, rz_bp_trace_t::length, malloc(), memset(), rz_io_bind_t::read_at, rz_bp_get_bytes(), rz_list_append(), RZ_NEW, ST32_MAX, to, rz_bp_t::traces, rz_bp_trace_t::traps, and ut64().

Referenced by cmd_debug_backtrace().

◆ rz_bp_traptrace_at()

RZ_API int rz_bp_traptrace_at ( RzBreakpoint bp,
ut64  from,
int  len 
)

Definition at line 161 of file bp_traptrace.c.

161  {
162  int delta;
163  RzListIter *iter;
164  RzBreakpointTrace *trace;
165  rz_list_foreach (bp->traces, iter, trace) {
166  // TODO: do we really need len?
167  if (from >= trace->addr && from + len <= trace->addr_end) {
168  delta = (int)(from - trace->addr);
169  if (RZ_BIT_CHK(trace->bits, delta)) {
170  if (trace->traps[delta] == 0x00) {
171  return false; // already traced..debugger should stop
172  }
173  }
174  RZ_BIT_SET(trace->bits, delta);
175  return true;
176  }
177  }
178  return false;
179 }
#define RZ_BIT_SET(x, y)
Definition: rz_types.h:311
#define RZ_BIT_CHK(x, y)
Definition: rz_types.h:316
static int
Definition: sfsocketcall.h:114
static st64 delta
Definition: vmenus.c:2425

References rz_bp_trace_t::addr, rz_bp_trace_t::bits, delta, from, int, RZ_BIT_CHK, RZ_BIT_SET, rz_bp_t::traces, and rz_bp_trace_t::traps.

Referenced by cmd_debug_backtrace().

◆ rz_bp_traptrace_enable()

RZ_API void rz_bp_traptrace_enable ( RzBreakpoint bp,
int  enable 
)

Definition at line 25 of file bp_traptrace.c.

25  {
27  RzBreakpointTrace *trace;
28  rz_list_foreach (bp->traces, iter, trace) {
29  ut8 *buf = (enable) ? trace->traps : trace->buffer;
30  bp->iob.write_at(bp->iob.io, trace->addr, buf, trace->length);
31  }
32 }
RzIOWriteAt write_at
Definition: rz_io.h:241

References rz_bp_trace_t::addr, rz_bp_trace_t::buffer, rz_io_bind_t::io, rz_bp_t::iob, rz_bp_trace_t::length, rz_bp_t::traces, rz_bp_trace_t::traps, and rz_io_bind_t::write_at.

Referenced by cmd_debug_backtrace().

◆ rz_bp_traptrace_free()

RZ_API void rz_bp_traptrace_free ( void *  ptr)

Definition at line 8 of file bp_traptrace.c.

8  {
9  RzBreakpointTrace *trace = ptr;
10  free(trace->buffer);
11  free(trace->traps);
12  free(trace->bits);
13  free(trace);
14 }

References rz_bp_trace_t::bits, rz_bp_trace_t::buffer, free(), and rz_bp_trace_t::traps.

Referenced by rz_bp_traptrace_free_at(), rz_bp_traptrace_new(), and rz_bp_traptrace_reset().

◆ rz_bp_traptrace_free_at()

RZ_API int rz_bp_traptrace_free_at ( RzBreakpoint bp,
ut64  from 
)

Definition at line 132 of file bp_traptrace.c.

132  {
133  int ret = false;
134  RzListIter *iter, *iter_tmp;
135  RzBreakpointTrace *trace;
136  rz_list_foreach_safe (bp->traces, iter, iter_tmp, trace) {
137  if (from >= trace->addr && from <= trace->addr_end) {
138  bp->iob.write_at(bp->iob.io, trace->addr,
139  trace->buffer, trace->length);
140  rz_bp_traptrace_free(trace);
141  rz_list_delete(bp->traces, iter);
142  ret = true;
143  }
144  }
145  return ret;
146 }
RZ_API void rz_bp_traptrace_free(void *ptr)
Definition: bp_traptrace.c:8
RZ_API void rz_list_delete(RZ_NONNULL RzList *list, RZ_NONNULL RzListIter *iter)
Removes an entry in the list by using the RzListIter pointer.
Definition: list.c:162

References rz_bp_trace_t::addr, rz_bp_trace_t::buffer, from, rz_io_bind_t::io, rz_bp_t::iob, rz_bp_trace_t::length, rz_bp_traptrace_free(), rz_list_delete(), rz_bp_t::traces, and rz_io_bind_t::write_at.

◆ rz_bp_traptrace_list()

RZ_API void rz_bp_traptrace_list ( RzBreakpoint bp)

Definition at line 148 of file bp_traptrace.c.

148  {
149  int i;
150  RzListIter *iter;
151  RzBreakpointTrace *trace;
152  rz_list_foreach (bp->traces, iter, trace) {
153  for (i = 0; i < trace->bitlen; i++) {
154  if (RZ_BIT_CHK(trace->bits, i)) {
155  eprintf(" - 0x%08" PFMT64x "\n", trace->addr + (i << 4));
156  }
157  }
158  }
159 }
lzma_index ** i
Definition: index.h:629
#define eprintf(x, y...)
Definition: rlcc.c:7
#define PFMT64x
Definition: rz_types.h:393
int bitlen
Definition: rz_bp.h:106

References rz_bp_trace_t::addr, rz_bp_trace_t::bitlen, rz_bp_trace_t::bits, eprintf, i, PFMT64x, RZ_BIT_CHK, and rz_bp_t::traces.

Referenced by cmd_debug_backtrace().

◆ rz_bp_traptrace_new()

RZ_API RzList* rz_bp_traptrace_new ( void  )

Definition at line 16 of file bp_traptrace.c.

16  {
17  RzList *list = rz_list_new();
18  if (!list) {
19  return NULL;
20  }
21  list->free = &rz_bp_traptrace_free;
22  return list;
23 }
#define NULL
Definition: cris-opc.c:27
static void list(RzEgg *egg)
Definition: rz-gg.c:52
RZ_API RZ_OWN RzList * rz_list_new(void)
Returns a new initialized RzList pointer (free method is not initialized)
Definition: list.c:235

References list(), NULL, rz_bp_traptrace_free(), and rz_list_new().

Referenced by rz_bp_new().

◆ rz_bp_traptrace_next()

RZ_API ut64 rz_bp_traptrace_next ( RzBreakpoint bp,
ut64  addr 
)

Definition at line 54 of file bp_traptrace.c.

54  {
55  int i, delta;
57  RzBreakpointTrace *trace;
58  rz_list_foreach (bp->traces, iter, trace) {
59  if (addr >= trace->addr && addr <= trace->addr_end) {
60  delta = (int)(addr - trace->addr);
61  for (i = delta; i < trace->length; i++) {
62  if (RZ_BIT_CHK(trace->bits, i)) {
63  return addr + i;
64  }
65  }
66  }
67  }
68  return 0LL;
69 }
static int addr
Definition: z80asm.c:58

References addr, rz_bp_trace_t::addr, rz_bp_trace_t::bits, delta, i, int, rz_bp_trace_t::length, RZ_BIT_CHK, and rz_bp_t::traces.

◆ rz_bp_traptrace_reset()

RZ_API void rz_bp_traptrace_reset ( RzBreakpoint bp,
int  hard 
)

Definition at line 34 of file bp_traptrace.c.

34  {
36  RzBreakpointTrace *trace;
37  rz_list_foreach (bp->traces, iter, trace) {
38  if (hard) {
39  rz_bp_traptrace_free(trace);
40  // XXX: This segfaults
41  // rz_list_delete (bp->traces, rz_list_iter_cur (iter));
42  } else {
43  memset(trace->bits, 0x00, trace->bitlen);
44  }
45  }
46  if (hard) {
47  // XXX: traces not freed correctly (memleak)
48  bp->traces = rz_list_new();
50  }
51 }
RzListFree free
Definition: rz_list.h:21

References rz_bp_trace_t::bitlen, rz_bp_trace_t::bits, rz_list_t::free, memset(), rz_bp_traptrace_free(), rz_list_new(), and rz_bp_t::traces.

Referenced by cmd_debug_backtrace().