1 /* radare - LGPL - Copyright 2013-2017 pancake */
2 // r2 -Desil ls
3 
4 #include <r_asm.h>
5 #include <r_debug.h>
6 
7 #if 0
8 static int is_io_esil(RDebug *dbg) {
9 	RIODesc *d = dbg->iob.io->desc;
10 	if (d && d->plugin && d->plugin->name)
11 		if (!strcmp ("esil", d->plugin->name))
12 			return true;
13 	return false;
14 }
15 #endif
16 
__esil_step_over(RDebug * dbg)17 static int __esil_step_over(RDebug *dbg) {
18 	eprintf ("TODO: ESIL STEP OVER\n");
19 	return true;
20 }
21 
__esil_step(RDebug * dbg)22 static int __esil_step(RDebug *dbg) {
23 	int oplen;
24 	ut8 buf[64];
25 	ut64 pc = 0LL; // getreg("pc")
26 	RAnalOp op = {0};
27 
28 	r_debug_reg_sync(dbg, R_REG_TYPE_GPR, false);
29 	pc = r_debug_reg_get (dbg, "PC");
30 	eprintf ("PC = 0x%" PFMT64x "\n", pc);
31 /// XXX. hack to trick vaddr issue
32 //pc = 0x100001478;
33 	//memset (buf, 0, sizeof (buf));
34 	dbg->iob.read_at (dbg->iob.io, pc, buf, 64);
35 	eprintf ("READ 0x%08"PFMT64x" %02x %02x %02x\n", pc, buf[0], buf[1], buf[2]);
36 	oplen = r_anal_op (dbg->anal, &op, pc, buf, sizeof (buf), R_ANAL_OP_MASK_ESIL);
37 	if (oplen > 0) {
38 		if (*R_STRBUF_SAFEGET (&op.esil)) {
39 			eprintf ("ESIL: %s\n", R_STRBUF_SAFEGET (&op.esil));
40 			r_anal_esil_parse (dbg->anal->esil, R_STRBUF_SAFEGET (&op.esil));
41 		}
42 	}
43 	r_anal_op_fini (&op);
44 	eprintf ("TODO: ESIL STEP\n");
45 	return true;
46 }
47 
__esil_init(RDebug * dbg)48 static int __esil_init(RDebug *dbg) {
49 	dbg->tid = dbg->pid = 1;
50 	// aeim
51 	// aei
52 	return true;
53 }
54 
__esil_continue(RDebug * dbg,int pid,int tid,int sig)55 static int __esil_continue(RDebug *dbg, int pid, int tid, int sig) {
56 	eprintf ("TODO continue\n");
57 	return true;
58 }
59 
__esil_continue_syscall(RDebug * dbg,int pid,int num)60 static int __esil_continue_syscall(RDebug *dbg, int pid, int num) {
61 	eprintf ("TODO: esil continue until syscall\n");
62 	return true;
63 }
64 
__esil_wait(RDebug * dbg,int pid)65 static int __esil_wait(RDebug *dbg, int pid) {
66 	/* do nothing */
67 	return true;
68 }
69 
__esil_attach(RDebug * dbg,int pid)70 static int __esil_attach(RDebug *dbg, int pid) {
71 	eprintf ("OK attach\n");
72 	return true;
73 #if 0
74 	if (!is_io_esil (dbg))
75 		return false;
76 #endif
77 #if 0
78 	RIOBdescbg *o;
79 	o = dbg->iob.io->desc->data;
80 eprintf ("base = %llx\n", o->bfvm->base);
81 eprintf ("screen = %llx\n", o->bfvm->screen);
82 eprintf ("input = %llx\n", o->bfvm->input);
83 #endif
84 	return true;
85 }
86 
__esil_detach(RDebug * dbg,int pid)87 static int __esil_detach(RDebug *dbg, int pid) {
88 	// reset vm?
89 	return true;
90 }
91 
__esil_reg_profile(RDebug * dbg)92 static char *__esil_reg_profile(RDebug *dbg) {
93 	if (!strcmp (dbg->arch, "bf")) {
94 		return strdup (
95 			"=PC	pc\n"
96 			"=SP	esp\n"
97 			"=BP	ptr\n"
98 			"=A0	rax\n"
99 			"gpr	rax	.32	0	0\n"
100 			"gpr	pc	.32	0	0\n"
101 			"gpr	ptr	.32	4	0\n"
102 			"gpr	esp	.32	8	0\n"
103 			"gpr	scr	.32	12	0\n"
104 			"gpr	scri	.32	16	0\n"
105 			"gpr	inp	.32	20	0\n"
106 			"gpr	inpi	.32	24	0\n"
107 			"gpr	mem	.32	28	0\n"
108 			"gpr	memi	.32	32	0\n"
109 		      );
110 	}
111 	return r_anal_get_reg_profile (dbg->anal);
112 }
113 
__esil_breakpoint(RBreakpoint * bp,RBreakpointItem * b,bool set)114 static int __esil_breakpoint (RBreakpoint *bp, RBreakpointItem *b, bool set) {
115 	//r_io_system (dbg->iob.io, "db");
116 	return false;
117 }
118 
__esil_kill(RDebug * dbg,int pid,int tid,int sig)119 static bool __esil_kill(RDebug *dbg, int pid, int tid, int sig) {
120 	// TODO: ESIL reset
121 	return true;
122 }
123 
__esil_stop(RDebug * dbg)124 static int __esil_stop(RDebug *dbg) {
125 	eprintf ("ESIL: stop\n");
126 	return true;
127 }
128 
__reg_read(RDebug * dbg,int type,ut8 * buf,int size)129 static int __reg_read (RDebug *dbg, int type, ut8 *buf, int size) {
130 	int sz;
131 	/* do nothing */
132 	ut8 *bytes = r_reg_get_bytes (dbg->reg, type, &sz);
133 	memcpy (buf, bytes, R_MIN (size, sz));
134 	free (bytes);
135 	return size;
136 }
137 
138 RDebugPlugin r_debug_plugin_esil = {
139 	.name = "esil",
140 	.license = "LGPL3",
141 	.arch = "any", // TODO: exception!
142 	.bits = R_SYS_BITS_32 | R_SYS_BITS_64,
143 	.init = __esil_init,
144 	.step = __esil_step,
145 	.step_over = __esil_step_over,
146 	.cont = __esil_continue,
147 	.contsc = __esil_continue_syscall,
148 	.attach = &__esil_attach,
149 	.detach = &__esil_detach,
150 	.wait = &__esil_wait,
151 	.stop = __esil_stop,
152 	.kill = __esil_kill,
153 	.breakpoint = __esil_breakpoint,
154 	.reg_profile = __esil_reg_profile,
155 	.reg_read = __reg_read,
156 };
157 
158 #ifndef R2_PLUGIN_INCORE
159 R_API RLibStruct radare_plugin = {
160 	.type = R_LIB_TYPE_DBG,
161 	.data = &r_debug_plugin_esil,
162 	.version = R2_VERSION
163 };
164 #endif
165