Rizin
unix-like reverse engineering framework and cli tools
il_trace.c File Reference

new rizin IL trace implementation More...

#include <rz_util.h>
#include <rz_analysis.h>

Go to the source code of this file.

Functions

RZ_API RzILTraceInstruction * rz_analysis_il_trace_instruction_new (ut64 addr)
 
RZ_API void rz_analysis_il_trace_instruction_free (RzILTraceInstruction *instruction)
 
RZ_API bool rz_analysis_il_trace_add_mem (RzILTraceInstruction *trace, RzILTraceMemOp *mem)
 
RZ_API bool rz_analysis_il_trace_add_reg (RzILTraceInstruction *trace, RzILTraceRegOp *reg)
 
RZ_API RzILTraceMemOp * rz_analysis_il_get_mem_op_trace (RzILTraceInstruction *trace, ut64 addr, RzILTraceOpType op_type)
 
RZ_API RzILTraceRegOp * rz_analysis_il_get_reg_op_trace (RzILTraceInstruction *trace, const char *regname, RzILTraceOpType op_type)
 
RZ_API bool rz_analysis_il_mem_trace_contains (RzILTraceInstruction *trace, ut64 addr, RzILTraceOpType op_type)
 
RZ_API bool rz_analysis_il_reg_trace_contains (RzILTraceInstruction *trace, const char *regname, RzILTraceOpType op_type)
 

Detailed Description

new rizin IL trace implementation

provide operations to new IL trace structure to record the memory changes and register changes. TODO : Should be moved to librz/il after integrations with new IL : should move the prototypes and trace structure to new header, too prototypes in <rz_analysis.h> Used by : analysis_tp.c, debug/trace.c

Definition in file il_trace.c.

Function Documentation

◆ rz_analysis_il_get_mem_op_trace()

RZ_API RzILTraceMemOp* rz_analysis_il_get_mem_op_trace ( RzILTraceInstruction *  trace,
ut64  addr,
RzILTraceOpType  op_type 
)

Find the memory change in an instruction by given address

Parameters
traceRzILTraceInstruction *, instruction trace
addrut64, memory address
is_writebool, true if you want to find a write operation to address, else find a read operation
Returns
RzILTraceMemOp *, info of memory change

Definition at line 135 of file il_trace.c.

135  {
136  if (!trace) {
137  return NULL;
138  }
139 
140  RzPVector *mem_ops;
141  RzILTraceMemOp *mem_op;
142  switch (op_type) {
143  case RZ_IL_TRACE_OP_WRITE:
144  mem_ops = trace->write_mem_ops;
145  break;
146  case RZ_IL_TRACE_OP_READ:
147  mem_ops = trace->read_mem_ops;
148  break;
149  default:
151  return NULL;
152  }
153 
154  void **iter;
155  rz_pvector_foreach (mem_ops, iter) {
156  mem_op = *iter;
157  if (mem_op->addr == addr) {
158  return mem_op;
159  }
160  }
161 
162  return NULL;
163 }
#define NULL
Definition: cris-opc.c:27
#define rz_warn_if_reached()
Definition: rz_assert.h:29
#define rz_pvector_foreach(vec, it)
Definition: rz_vector.h:334
static int addr
Definition: z80asm.c:58

References addr, NULL, rz_pvector_foreach, and rz_warn_if_reached.

Referenced by rz_analysis_il_mem_trace_contains().

◆ rz_analysis_il_get_reg_op_trace()

RZ_API RzILTraceRegOp* rz_analysis_il_get_reg_op_trace ( RzILTraceInstruction *  trace,
const char *  regname,
RzILTraceOpType  op_type 
)

Find the register change in an instruction by register name

Parameters
traceRzILTraceInstruction *, instruction trace
regnameconst char *, name of register
is_writebool, true if you want to find a write operation to register, else find a read operation
Returns
RzILTraceRegOp *, info of register change

Definition at line 172 of file il_trace.c.

172  {
173  if (!(trace && regname)) {
174  return NULL;
175  }
176 
177  RzPVector *reg_ops;
178  RzILTraceRegOp *reg_op;
179  switch (op_type) {
180  case RZ_IL_TRACE_OP_WRITE:
181  reg_ops = trace->write_reg_ops;
182  break;
183  case RZ_IL_TRACE_OP_READ:
184  reg_ops = trace->read_reg_ops;
185  break;
186  default:
188  return NULL;
189  }
190 
191  void **iter;
192  rz_pvector_foreach (reg_ops, iter) {
193  reg_op = *iter;
194  if (strcmp(reg_op->reg_name, regname) == 0) {
195  return reg_op;
196  }
197  }
198 
199  return NULL;
200 }
static char * regname(int reg)
Definition: dis.c:71

References NULL, regname(), rz_pvector_foreach, and rz_warn_if_reached.

Referenced by get_addr(), rz_analysis_il_reg_trace_contains(), and type_match().

◆ rz_analysis_il_mem_trace_contains()

RZ_API bool rz_analysis_il_mem_trace_contains ( RzILTraceInstruction *  trace,
ut64  addr,
RzILTraceOpType  op_type 
)

Check if instruction contains a read/write to given address

Parameters
traceRzILTraceInstruction *, instruction trace
addrut64, Address of memory
is_writebool, set true to find if it contains a write to address, else read
Returns
bool, true if contains, else return a false

Definition at line 209 of file il_trace.c.

209  {
210  return rz_analysis_il_get_mem_op_trace(trace, addr, op_type) ? true : false;
211 }
#define true
RZ_API RzILTraceMemOp * rz_analysis_il_get_mem_op_trace(RzILTraceInstruction *trace, ut64 addr, RzILTraceOpType op_type)
Definition: il_trace.c:135

References addr, rz_analysis_il_get_mem_op_trace(), and true.

Referenced by rz_analysis_il_trace_add_mem().

◆ rz_analysis_il_reg_trace_contains()

RZ_API bool rz_analysis_il_reg_trace_contains ( RzILTraceInstruction *  trace,
const char *  regname,
RzILTraceOpType  op_type 
)

Check if instruction contains a read/write to given register

Parameters
traceRzILTraceInstruction *, instruction trace
regnameconst char *, name of register
is_writebool, set true to find if it contains a write to the register, else read
Returns
bool, true if contains, else return a false

Definition at line 220 of file il_trace.c.

220  {
221  return rz_analysis_il_get_reg_op_trace(trace, regname, op_type) ? true : false;
222 }
RZ_API RzILTraceRegOp * rz_analysis_il_get_reg_op_trace(RzILTraceInstruction *trace, const char *regname, RzILTraceOpType op_type)
Definition: il_trace.c:172

References regname(), rz_analysis_il_get_reg_op_trace(), and true.

Referenced by rz_analysis_il_trace_add_reg(), and type_pos_hit().

◆ rz_analysis_il_trace_add_mem()

RZ_API bool rz_analysis_il_trace_add_mem ( RzILTraceInstruction *  trace,
RzILTraceMemOp *  mem 
)

add memory change to an instruction trace

Parameters
traceRzILTraceInstruction *, trace of instruction which triggers a memory change
memRzILTraceMemOp *, info of memory change
Returns
true if succeed

Definition at line 70 of file il_trace.c.

70  {
71  if (!(trace && mem)) {
72  return false;
73  }
74 
75  if (rz_analysis_il_mem_trace_contains(trace, mem->addr, mem->behavior)) {
76  return false;
77  }
78 
79  bool ret = false;
80  switch (mem->behavior) {
81  case RZ_IL_TRACE_OP_WRITE:
82  ret = !!rz_pvector_push(trace->write_mem_ops, mem);
83  trace->stats |= RZ_IL_TRACE_INS_HAS_MEM_W;
84  break;
85  case RZ_IL_TRACE_OP_READ:
86  ret = !!rz_pvector_push(trace->read_mem_ops, mem);
87  trace->stats |= RZ_IL_TRACE_INS_HAS_MEM_R;
88  break;
89  default:
91  break;
92  }
93  return ret;
94 }
RZ_API bool rz_analysis_il_mem_trace_contains(RzILTraceInstruction *trace, ut64 addr, RzILTraceOpType op_type)
Definition: il_trace.c:209
void * mem
Definition: libc.cpp:91
static void ** rz_pvector_push(RzPVector *vec, void *x)
Definition: rz_vector.h:300

References mem, rz_analysis_il_mem_trace_contains(), rz_pvector_push(), and rz_warn_if_reached.

Referenced by esil_add_mem_trace().

◆ rz_analysis_il_trace_add_reg()

RZ_API bool rz_analysis_il_trace_add_reg ( RzILTraceInstruction *  trace,
RzILTraceRegOp *  reg 
)

add register change to an instruction trace

Parameters
traceRzILTraceInstruction *, trace of instruction which triggers a register change
memRzILTraceRegOp *, info of register change
Returns
true if succeed

Definition at line 102 of file il_trace.c.

102  {
103  if (!(trace && reg)) {
104  return false;
105  }
106 
107  if (rz_analysis_il_reg_trace_contains(trace, reg->reg_name, reg->behavior)) {
108  return false;
109  }
110 
111  bool ret = false;
112  switch (reg->behavior) {
113  case RZ_IL_TRACE_OP_WRITE:
114  ret = !!rz_pvector_push(trace->write_reg_ops, reg);
115  trace->stats |= RZ_IL_TRACE_INS_HAS_REG_W;
116  break;
117  case RZ_IL_TRACE_OP_READ:
118  ret = !!rz_pvector_push(trace->read_reg_ops, reg);
119  trace->stats |= RZ_IL_TRACE_INS_HAS_REG_R;
120  break;
121  default:
123  break;
124  }
125  return ret;
126 }
RZ_API bool rz_analysis_il_reg_trace_contains(RzILTraceInstruction *trace, const char *regname, RzILTraceOpType op_type)
Definition: il_trace.c:220
#define reg(n)

References reg, rz_analysis_il_reg_trace_contains(), rz_pvector_push(), and rz_warn_if_reached.

Referenced by esil_add_reg_trace().

◆ rz_analysis_il_trace_instruction_free()

RZ_API void rz_analysis_il_trace_instruction_free ( RzILTraceInstruction *  instruction)

clean an IL trace

Parameters
instructionRzILTraceInstruction, trace to be cleaned

Definition at line 53 of file il_trace.c.

53  {
54  if (!instruction) {
55  return;
56  }
57  rz_pvector_free(instruction->write_reg_ops);
58  rz_pvector_free(instruction->read_reg_ops);
59  rz_pvector_free(instruction->write_mem_ops);
60  rz_pvector_free(instruction->read_mem_ops);
62 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_API void rz_pvector_free(RzPVector *vec)
Definition: vector.c:336

References free(), and rz_pvector_free().

Referenced by rz_analysis_esil_trace_new(), rz_analysis_il_trace_instruction_new(), rz_analysis_rzil_trace_new(), and rz_cmd_debug_traces_esil_delete_handler().

◆ rz_analysis_il_trace_instruction_new()

RZ_API RzILTraceInstruction* rz_analysis_il_trace_instruction_new ( ut64  addr)

create and init a trace structure for an instruction at address

Parameters
addrut64, address of instruction
Returns
RzILTraceInstruction, trace structure of an instruction

Definition at line 24 of file il_trace.c.

24  {
25  RzILTraceInstruction *instruction_trace = RZ_NEW0(RzILTraceInstruction);
26  if (!instruction_trace) {
27  RZ_LOG_ERROR("Cannot create instruction trace\n");
28  return NULL;
29  }
30 
31  instruction_trace->addr = addr;
32 
33  instruction_trace->read_mem_ops = rz_pvector_new((RzPVectorFree)free);
34  instruction_trace->read_reg_ops = rz_pvector_new((RzPVectorFree)free);
35  instruction_trace->write_mem_ops = rz_pvector_new((RzPVectorFree)free);
36  instruction_trace->write_reg_ops = rz_pvector_new((RzPVectorFree)free);
37 
38  if (!(instruction_trace->read_reg_ops &&
39  instruction_trace->read_mem_ops &&
40  instruction_trace->write_reg_ops &&
41  instruction_trace->write_mem_ops)) {
42  rz_analysis_il_trace_instruction_free(instruction_trace);
43  return NULL;
44  }
45 
46  return instruction_trace;
47 }
RZ_API void rz_analysis_il_trace_instruction_free(RzILTraceInstruction *instruction)
Definition: il_trace.c:53
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
#define RZ_NEW0(x)
Definition: rz_types.h:284
RZ_API RzPVector * rz_pvector_new(RzPVectorFree free)
Definition: vector.c:302
void(* RzPVectorFree)(void *e)
Definition: rz_vector.h:43

References addr, free(), NULL, rz_analysis_il_trace_instruction_free(), RZ_LOG_ERROR, RZ_NEW0, and rz_pvector_new().

Referenced by rz_analysis_esil_trace_op().