Rizin
unix-like reverse engineering framework and cli tools
emit_trace.c File Reference
#include <rz_egg.h>

Go to the source code of this file.

Macros

#define attsyntax   0
 
#define EMIT_NAME   emit_trace
 
#define RZ_ARCH   "trace"
 
#define RZ_SZ   8
 
#define RZ_SP   "sp"
 
#define RZ_BP   "bp"
 
#define RZ_AX   "a0"
 
#define RZ_GP    { "a0", "a1", "a2", "a3", "a4" }
 
#define RZ_TMP   "t0"
 
#define RZ_NGP   5
 

Functions

static void emit_init (RzEgg *egg)
 
static char * emit_syscall (RzEgg *egg, int num)
 
static void emit_frame (RzEgg *egg, int sz)
 
static void emit_frame_end (RzEgg *egg, int sz, int ctx)
 
static void emit_comment (RzEgg *egg, const char *fmt,...)
 
static void emit_equ (RzEgg *egg, const char *key, const char *value)
 
static void emit_syscall_args (RzEgg *egg, int nargs)
 
static void emit_set_string (RzEgg *egg, const char *dstvar, const char *str, int j)
 
static void emit_call (RzEgg *egg, const char *str, int atr)
 
static void emit_jmp (RzEgg *egg, const char *str, int atr)
 
static void emit_arg (RzEgg *egg, int xs, int num, const char *str)
 
static void emit_get_result (RzEgg *egg, const char *ocn)
 
static void emit_restore_stack (RzEgg *egg, int size)
 
static void emit_get_while_end (RzEgg *egg, char *str, const char *ctxpush, const char *label)
 
static void emit_while_end (RzEgg *egg, const char *labelback)
 
static void emit_get_var (RzEgg *egg, int type, char *out, int idx)
 
static void emit_trap (RzEgg *egg)
 
static void emit_load_ptr (RzEgg *egg, const char *dst)
 
static void emit_branch (RzEgg *egg, char *b, char *g, char *e, char *n, int sz, const char *dst)
 
static void emit_load (RzEgg *egg, const char *dst, int sz)
 
static void emit_mathop (RzEgg *egg, int ch, int vs, int type, const char *eq, const char *p)
 
static const char * emit_regs (RzEgg *egg, int idx)
 

Variables

static char * regs [] = RZ_GP
 
RzEggEmit EMIT_NAME
 

Macro Definition Documentation

◆ attsyntax

#define attsyntax   0

Definition at line 5 of file emit_trace.c.

◆ EMIT_NAME

#define EMIT_NAME   emit_trace

Definition at line 7 of file emit_trace.c.

◆ RZ_ARCH

#define RZ_ARCH   "trace"

Definition at line 8 of file emit_trace.c.

◆ RZ_AX

#define RZ_AX   "a0"

Definition at line 12 of file emit_trace.c.

◆ RZ_BP

#define RZ_BP   "bp"

Definition at line 11 of file emit_trace.c.

◆ RZ_GP

#define RZ_GP    { "a0", "a1", "a2", "a3", "a4" }

Definition at line 13 of file emit_trace.c.

◆ RZ_NGP

#define RZ_NGP   5

Definition at line 16 of file emit_trace.c.

◆ RZ_SP

#define RZ_SP   "sp"

Definition at line 10 of file emit_trace.c.

◆ RZ_SZ

#define RZ_SZ   8

Definition at line 9 of file emit_trace.c.

◆ RZ_TMP

#define RZ_TMP   "t0"

Definition at line 15 of file emit_trace.c.

Function Documentation

◆ emit_arg()

static void emit_arg ( RzEgg egg,
int  xs,
int  num,
const char *  str 
)
static

Definition at line 77 of file emit_trace.c.

77  {
78  // TODO: enhance output here
79  rz_egg_printf(egg, "arg.%d.%d=%s\n", xs, num, str);
80 }
RZ_API void rz_egg_printf(RzEgg *egg, const char *fmt,...)
Definition: egg.c:336
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc static sig const char static mode static oldfd struct tms static buf static getgid static geteuid const char static filename static arg static mask struct ustat static ubuf static getppid static setsid static egid sigset_t static set struct timeval struct timezone static tz fd_set fd_set fd_set struct timeval static timeout const char char static bufsiz const char static swapflags void static offset const char static length static mode static who const char struct statfs static buf unsigned unsigned num
Definition: sflib.h:126

References num, rz_egg_printf(), and cmd_descs_generate::str.

◆ emit_branch()

static void emit_branch ( RzEgg egg,
char *  b,
char *  g,
char *  e,
char *  n,
int  sz,
const char *  dst 
)
static

Definition at line 116 of file emit_trace.c.

116  {
117  // This function signature is bad
118  char *p, str[64];
119  char *arg = NULL;
120  char *op = "beq";
121  /* NOTE that jb/ja are inverted to fit cmp opcode */
122  if (b) {
123  *b = '\0';
124  op = e ? "bge" : "bgt";
125  arg = b + 1;
126  } else if (g) {
127  *g = '\0';
128  op = e ? "ble" : "blt";
129  arg = g + 1;
130  }
131  if (!arg) {
132  if (e) {
133  arg = e + 1;
134  op = "bne";
135  } else {
136  arg = "0";
137  op = n ? "bne" : "beq";
138  }
139  }
140 
141  if (*arg == '=') {
142  arg++; /* for <=, >=, ... */
143  }
144  p = rz_egg_mkvar(egg, str, arg, 0);
145  rz_egg_printf(egg, "%s (%s) => (%s)\n", op, p, dst);
146  free(p);
147 }
#define e(frag)
#define NULL
Definition: cris-opc.c:27
RZ_API char * rz_egg_mkvar(RzEgg *egg, char *out, const char *_str, int delta)
Definition: egg_lang.c:538
struct @667 g
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
void * p
Definition: libc.cpp:67
char * dst
Definition: lz4.h:724
int n
Definition: mipsasm.c:19
#define b(i)
Definition: sha256.c:42
Definition: dis.c:32

References b, dst, e, free(), g, n, NULL, p, rz_egg_mkvar(), rz_egg_printf(), and cmd_descs_generate::str.

◆ emit_call()

static void emit_call ( RzEgg egg,
const char *  str,
int  atr 
)
static

Definition at line 61 of file emit_trace.c.

61  {
62  if (atr) {
63  rz_egg_printf(egg, "call ([%s])\n", str);
64  } else {
65  rz_egg_printf(egg, "call (%s)\n", str);
66  }
67 }

References rz_egg_printf(), and cmd_descs_generate::str.

◆ emit_comment()

static void emit_comment ( RzEgg egg,
const char *  fmt,
  ... 
)
static

Definition at line 39 of file emit_trace.c.

39  {
40  va_list ap;
41  char buf[1024];
42  va_start(ap, fmt);
43  vsnprintf(buf, sizeof(buf), fmt, ap);
44  rz_egg_printf(egg, "# %s\n", buf);
45  va_end(ap);
46 }
voidpf void * buf
Definition: ioapi.h:138
vsnprintf
Definition: kernel.h:366

References rz_egg_printf(), and vsnprintf.

◆ emit_equ()

static void emit_equ ( RzEgg egg,
const char *  key,
const char *  value 
)
static

Definition at line 48 of file emit_trace.c.

48  {
49  rz_egg_printf(egg, "equ (%s, %s)\n", key, value);
50 }
static int value
Definition: cmd_api.c:93
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len key
Definition: sflib.h:118

References key, rz_egg_printf(), and value.

◆ emit_frame()

static void emit_frame ( RzEgg egg,
int  sz 
)
static

Definition at line 31 of file emit_trace.c.

31  {
32  rz_egg_printf(egg, "frame (%d)\n", sz);
33 }

References rz_egg_printf().

◆ emit_frame_end()

static void emit_frame_end ( RzEgg egg,
int  sz,
int  ctx 
)
static

Definition at line 35 of file emit_trace.c.

35  {
36  rz_egg_printf(egg, "frame_end (%d, %d)\n", sz, ctx);
37 }

References rz_egg_printf().

◆ emit_get_result()

static void emit_get_result ( RzEgg egg,
const char *  ocn 
)
static

Definition at line 82 of file emit_trace.c.

82  {
83  rz_egg_printf(egg, "get_result (%s)\n", ocn);
84 }

References rz_egg_printf().

◆ emit_get_var()

static void emit_get_var ( RzEgg egg,
int  type,
char *  out,
int  idx 
)
static

Definition at line 100 of file emit_trace.c.

100  {
101  switch (type) {
102  case 0: sprintf(out, "fp,$%d", -idx); break; /* variable */
103  case 1: sprintf(out, "sp,$%d", idx); break; /* argument */ // XXX: MUST BE r0, r1, r2, ..
104  }
105 }
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
sprintf
Definition: kernel.h:365
int type
Definition: mipsasm.c:17
int idx
Definition: setup.py:197

References setup::idx, out, sprintf, and type.

◆ emit_get_while_end()

static void emit_get_while_end ( RzEgg egg,
char *  str,
const char *  ctxpush,
const char *  label 
)
static

Definition at line 92 of file emit_trace.c.

92  {
93  rz_egg_printf(egg, "get_while_end (%s, %s, %s)\n", str, ctxpush, label);
94 }
Definition: dis.h:35

References rz_egg_printf(), and cmd_descs_generate::str.

◆ emit_init()

static void emit_init ( RzEgg egg)
static

Definition at line 21 of file emit_trace.c.

21  {
22  /* TODO */
23 }

◆ emit_jmp()

static void emit_jmp ( RzEgg egg,
const char *  str,
int  atr 
)
static

Definition at line 69 of file emit_trace.c.

69  {
70  if (atr) {
71  rz_egg_printf(egg, "goto ([%s])\n", str);
72  } else {
73  rz_egg_printf(egg, "goto (%s)\n", str);
74  }
75 }

References rz_egg_printf(), and cmd_descs_generate::str.

◆ emit_load()

static void emit_load ( RzEgg egg,
const char *  dst,
int  sz 
)
static

Definition at line 150 of file emit_trace.c.

150  {
151  rz_egg_printf(egg, "load (\"%s\", %c)\n", dst, sz);
152 }

References dst, and rz_egg_printf().

◆ emit_load_ptr()

static void emit_load_ptr ( RzEgg egg,
const char *  dst 
)
static

Definition at line 112 of file emit_trace.c.

112  {
113  rz_egg_printf(egg, "loadptr (%s)\n", dst);
114 }

References dst, and rz_egg_printf().

◆ emit_mathop()

static void emit_mathop ( RzEgg egg,
int  ch,
int  vs,
int  type,
const char *  eq,
const char *  p 
)
static

Definition at line 154 of file emit_trace.c.

154  {
155  char *op;
156  switch (ch) {
157  case '^': op = "eor"; break;
158  case '&': op = "and"; break;
159  case '|': op = "orr"; break;
160  case '-': op = "sub"; break;
161  case '+': op = "add"; break;
162  case '*': op = "mul"; break;
163  case '/': op = "div"; break;
164  default: op = "mov"; break;
165  }
166  if (!eq) {
167  eq = RZ_AX;
168  }
169  if (!p) {
170  p = RZ_AX;
171  }
172 #if 0
173  // TODO:
174  eprintf ("TYPE = %c\n", type);
175  eprintf (" %s%c %c%s, %s\n", op, vs, type, eq, p);
176  eprintf (" %s %s, [%s]\n", op, p, eq);
177 #endif
178  if (type == '*') {
179  rz_egg_printf(egg, "%s (%s, [%s])\n", op, p, eq);
180  } else {
181  rz_egg_printf(egg, "%s (%s, %s)\n", op, p, eq);
182  }
183 }
ut8 op
Definition: 6502dis.c:13
#define RZ_AX
Definition: emit_trace.c:12
#define eprintf(x, y...)
Definition: rlcc.c:7

References eprintf, eq, op, p, RZ_AX, rz_egg_printf(), and type.

◆ emit_regs()

static const char* emit_regs ( RzEgg egg,
int  idx 
)
static

Definition at line 185 of file emit_trace.c.

185  {
186  return regs[idx % RZ_NGP];
187 }
static char * regs[]
Definition: emit_trace.c:19
#define RZ_NGP
Definition: emit_trace.c:16

References setup::idx, regs, and RZ_NGP.

◆ emit_restore_stack()

static void emit_restore_stack ( RzEgg egg,
int  size 
)
static

Definition at line 86 of file emit_trace.c.

86  {
87  rz_egg_printf(egg, "restore_stack (%d)\n", size);
88  // XXX: must die.. or add emit_store_stack. not needed by ARM
89  // rz_egg_printf (egg, " add sp, %d\n", size);
90 }
voidpf void uLong size
Definition: ioapi.h:138

References rz_egg_printf().

◆ emit_set_string()

static void emit_set_string ( RzEgg egg,
const char *  dstvar,
const char *  str,
int  j 
)
static

Definition at line 56 of file emit_trace.c.

56  {
57  // what is j?
58  rz_egg_printf(egg, "set (\"%s\", \"%s\", %d)\n", dstvar, str, j);
59 }

References rz_egg_printf(), and cmd_descs_generate::str.

◆ emit_syscall()

static char* emit_syscall ( RzEgg egg,
int  num 
)
static

Definition at line 25 of file emit_trace.c.

25  {
26  char buf[32];
27  snprintf(buf, sizeof(buf), "syscall (%d)\n", num);
28  return strdup(buf);
29 }
snprintf
Definition: kernel.h:364
return strdup("=SP r13\n" "=LR r14\n" "=PC r15\n" "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN or0\n" "gpr lr .32 56 0\n" "gpr pc .32 60 0\n" "gpr cpsr .32 64 0 ____tfiae_________________qvczn\n" "gpr or0 .32 68 0\n" "gpr tf .1 64.5 0 thumb\n" "gpr ef .1 64.9 0 endian\n" "gpr jf .1 64.24 0 java\n" "gpr qf .1 64.27 0 sticky_overflow\n" "gpr vf .1 64.28 0 overflow\n" "gpr cf .1 64.29 0 carry\n" "gpr zf .1 64.30 0 zero\n" "gpr nf .1 64.31 0 negative\n" "gpr itc .4 64.10 0 if_then_count\n" "gpr gef .4 64.16 0 great_or_equal\n" "gpr r0 .32 0 0\n" "gpr r1 .32 4 0\n" "gpr r2 .32 8 0\n" "gpr r3 .32 12 0\n" "gpr r4 .32 16 0\n" "gpr r5 .32 20 0\n" "gpr r6 .32 24 0\n" "gpr r7 .32 28 0\n" "gpr r8 .32 32 0\n" "gpr r9 .32 36 0\n" "gpr r10 .32 40 0\n" "gpr r11 .32 44 0\n" "gpr r12 .32 48 0\n" "gpr r13 .32 52 0\n" "gpr r14 .32 56 0\n" "gpr r15 .32 60 0\n" "gpr r16 .32 64 0\n" "gpr r17 .32 68 0\n")

References num, snprintf, and strdup().

◆ emit_syscall_args()

static void emit_syscall_args ( RzEgg egg,
int  nargs 
)
static

Definition at line 52 of file emit_trace.c.

52  {
53  rz_egg_printf(egg, "syscall_args (%d)\n", nargs);
54 }

References cmd_descs_generate::nargs, and rz_egg_printf().

◆ emit_trap()

static void emit_trap ( RzEgg egg)
static

Definition at line 107 of file emit_trace.c.

107  {
108  rz_egg_printf(egg, "trap\n");
109 }

References rz_egg_printf().

◆ emit_while_end()

static void emit_while_end ( RzEgg egg,
const char *  labelback 
)
static

Definition at line 96 of file emit_trace.c.

96  {
97  rz_egg_printf(egg, "while_end (%s)\n", labelback);
98 }

References rz_egg_printf().

Variable Documentation

◆ EMIT_NAME

RzEggEmit EMIT_NAME
Initial value:
= {
.retvar = "a0",
.arch = RZ_ARCH,
.size = RZ_SZ,
.jmp = emit_jmp,
.call = emit_call,
.init = emit_init,
.equ = emit_equ,
.regs = emit_regs,
.trap = emit_trap,
.frame = emit_frame,
.frame_end = emit_frame_end,
.comment = emit_comment,
.push_arg = emit_arg,
.restore_stack = emit_restore_stack,
.get_result = emit_get_result,
.syscall_args = emit_syscall_args,
.set_string = emit_set_string,
.get_var = emit_get_var,
.while_end = emit_while_end,
.get_while_end = emit_get_while_end,
.branch = emit_branch,
.load = emit_load,
.load_ptr = emit_load_ptr,
.mathop = emit_mathop,
.syscall = emit_syscall,
}
static void emit_comment(RzEgg *egg, const char *fmt,...)
Definition: emit_trace.c:39
static void emit_branch(RzEgg *egg, char *b, char *g, char *e, char *n, int sz, const char *dst)
Definition: emit_trace.c:116
static void emit_jmp(RzEgg *egg, const char *str, int atr)
Definition: emit_trace.c:69
static void emit_call(RzEgg *egg, const char *str, int atr)
Definition: emit_trace.c:61
#define RZ_ARCH
Definition: emit_trace.c:8
#define RZ_SZ
Definition: emit_trace.c:9
static void emit_init(RzEgg *egg)
Definition: emit_trace.c:21
static char * emit_syscall(RzEgg *egg, int num)
Definition: emit_trace.c:25
static void emit_frame_end(RzEgg *egg, int sz, int ctx)
Definition: emit_trace.c:35
static void emit_get_while_end(RzEgg *egg, char *str, const char *ctxpush, const char *label)
Definition: emit_trace.c:92
static void emit_trap(RzEgg *egg)
Definition: emit_trace.c:107
static void emit_restore_stack(RzEgg *egg, int size)
Definition: emit_trace.c:86
static void emit_while_end(RzEgg *egg, const char *labelback)
Definition: emit_trace.c:96
static void emit_get_var(RzEgg *egg, int type, char *out, int idx)
Definition: emit_trace.c:100
static void emit_load(RzEgg *egg, const char *dst, int sz)
Definition: emit_trace.c:150
static const char * emit_regs(RzEgg *egg, int idx)
Definition: emit_trace.c:185
static void emit_arg(RzEgg *egg, int xs, int num, const char *str)
Definition: emit_trace.c:77
static void emit_mathop(RzEgg *egg, int ch, int vs, int type, const char *eq, const char *p)
Definition: emit_trace.c:154
static void emit_syscall_args(RzEgg *egg, int nargs)
Definition: emit_trace.c:52
static void emit_get_result(RzEgg *egg, const char *ocn)
Definition: emit_trace.c:82
static void emit_set_string(RzEgg *egg, const char *dstvar, const char *str, int j)
Definition: emit_trace.c:56
static void emit_frame(RzEgg *egg, int sz)
Definition: emit_trace.c:31
static void emit_load_ptr(RzEgg *egg, const char *dst)
Definition: emit_trace.c:112
static void emit_equ(RzEgg *egg, const char *key, const char *value)
Definition: emit_trace.c:48

Definition at line 189 of file emit_trace.c.

◆ regs

char* regs[] = RZ_GP
static

Definition at line 19 of file emit_trace.c.

Referenced by emit_regs().