1 /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013> */
2
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 #include <capstone/capstone.h>
7
8 void print_string_hex(const char *comment, unsigned char *str, size_t len);
9
get_eflag_name(uint64_t flag)10 static const char *get_eflag_name(uint64_t flag)
11 {
12 switch(flag) {
13 default:
14 return NULL;
15 case X86_EFLAGS_UNDEFINED_OF:
16 return "UNDEF_OF";
17 case X86_EFLAGS_UNDEFINED_SF:
18 return "UNDEF_SF";
19 case X86_EFLAGS_UNDEFINED_ZF:
20 return "UNDEF_ZF";
21 case X86_EFLAGS_MODIFY_AF:
22 return "MOD_AF";
23 case X86_EFLAGS_UNDEFINED_PF:
24 return "UNDEF_PF";
25 case X86_EFLAGS_MODIFY_CF:
26 return "MOD_CF";
27 case X86_EFLAGS_MODIFY_SF:
28 return "MOD_SF";
29 case X86_EFLAGS_MODIFY_ZF:
30 return "MOD_ZF";
31 case X86_EFLAGS_UNDEFINED_AF:
32 return "UNDEF_AF";
33 case X86_EFLAGS_MODIFY_PF:
34 return "MOD_PF";
35 case X86_EFLAGS_UNDEFINED_CF:
36 return "UNDEF_CF";
37 case X86_EFLAGS_MODIFY_OF:
38 return "MOD_OF";
39 case X86_EFLAGS_RESET_OF:
40 return "RESET_OF";
41 case X86_EFLAGS_RESET_CF:
42 return "RESET_CF";
43 case X86_EFLAGS_RESET_DF:
44 return "RESET_DF";
45 case X86_EFLAGS_RESET_IF:
46 return "RESET_IF";
47 case X86_EFLAGS_RESET_ZF:
48 return "RESET_ZF";
49 case X86_EFLAGS_TEST_OF:
50 return "TEST_OF";
51 case X86_EFLAGS_TEST_SF:
52 return "TEST_SF";
53 case X86_EFLAGS_TEST_ZF:
54 return "TEST_ZF";
55 case X86_EFLAGS_TEST_PF:
56 return "TEST_PF";
57 case X86_EFLAGS_TEST_CF:
58 return "TEST_CF";
59 case X86_EFLAGS_RESET_SF:
60 return "RESET_SF";
61 case X86_EFLAGS_RESET_AF:
62 return "RESET_AF";
63 case X86_EFLAGS_RESET_TF:
64 return "RESET_TF";
65 case X86_EFLAGS_RESET_NT:
66 return "RESET_NT";
67 case X86_EFLAGS_PRIOR_OF:
68 return "PRIOR_OF";
69 case X86_EFLAGS_PRIOR_SF:
70 return "PRIOR_SF";
71 case X86_EFLAGS_PRIOR_ZF:
72 return "PRIOR_ZF";
73 case X86_EFLAGS_PRIOR_AF:
74 return "PRIOR_AF";
75 case X86_EFLAGS_PRIOR_PF:
76 return "PRIOR_PF";
77 case X86_EFLAGS_PRIOR_CF:
78 return "PRIOR_CF";
79 case X86_EFLAGS_PRIOR_TF:
80 return "PRIOR_TF";
81 case X86_EFLAGS_PRIOR_IF:
82 return "PRIOR_IF";
83 case X86_EFLAGS_PRIOR_DF:
84 return "PRIOR_DF";
85 case X86_EFLAGS_TEST_NT:
86 return "TEST_NT";
87 case X86_EFLAGS_TEST_DF:
88 return "TEST_DF";
89 case X86_EFLAGS_RESET_PF:
90 return "RESET_PF";
91 case X86_EFLAGS_PRIOR_NT:
92 return "PRIOR_NT";
93 case X86_EFLAGS_MODIFY_TF:
94 return "MOD_TF";
95 case X86_EFLAGS_MODIFY_IF:
96 return "MOD_IF";
97 case X86_EFLAGS_MODIFY_DF:
98 return "MOD_DF";
99 case X86_EFLAGS_MODIFY_NT:
100 return "MOD_NT";
101 case X86_EFLAGS_MODIFY_RF:
102 return "MOD_RF";
103 case X86_EFLAGS_SET_CF:
104 return "SET_CF";
105 case X86_EFLAGS_SET_DF:
106 return "SET_DF";
107 case X86_EFLAGS_SET_IF:
108 return "SET_IF";
109 case X86_EFLAGS_SET_OF:
110 return "SET_OF";
111 case X86_EFLAGS_SET_SF:
112 return "SET_SF";
113 case X86_EFLAGS_SET_ZF:
114 return "SET_ZF";
115 case X86_EFLAGS_SET_AF:
116 return "SET_AF";
117 case X86_EFLAGS_SET_PF:
118 return "SET_PF";
119 case X86_EFLAGS_TEST_AF:
120 return "TEST_AF";
121 case X86_EFLAGS_TEST_TF:
122 return "TEST_TF";
123 case X86_EFLAGS_TEST_RF:
124 return "TEST_RF";
125 case X86_EFLAGS_RESET_0F:
126 return "RESET_0F";
127 case X86_EFLAGS_RESET_AC:
128 return "RESET_AC";
129 }
130 }
131
get_fpu_flag_name(uint64_t flag)132 static const char *get_fpu_flag_name(uint64_t flag)
133 {
134 switch (flag) {
135 default:
136 return NULL;
137 case X86_FPU_FLAGS_MODIFY_C0:
138 return "MOD_C0";
139 case X86_FPU_FLAGS_MODIFY_C1:
140 return "MOD_C1";
141 case X86_FPU_FLAGS_MODIFY_C2:
142 return "MOD_C2";
143 case X86_FPU_FLAGS_MODIFY_C3:
144 return "MOD_C3";
145 case X86_FPU_FLAGS_RESET_C0:
146 return "RESET_C0";
147 case X86_FPU_FLAGS_RESET_C1:
148 return "RESET_C1";
149 case X86_FPU_FLAGS_RESET_C2:
150 return "RESET_C2";
151 case X86_FPU_FLAGS_RESET_C3:
152 return "RESET_C3";
153 case X86_FPU_FLAGS_SET_C0:
154 return "SET_C0";
155 case X86_FPU_FLAGS_SET_C1:
156 return "SET_C1";
157 case X86_FPU_FLAGS_SET_C2:
158 return "SET_C2";
159 case X86_FPU_FLAGS_SET_C3:
160 return "SET_C3";
161 case X86_FPU_FLAGS_UNDEFINED_C0:
162 return "UNDEF_C0";
163 case X86_FPU_FLAGS_UNDEFINED_C1:
164 return "UNDEF_C1";
165 case X86_FPU_FLAGS_UNDEFINED_C2:
166 return "UNDEF_C2";
167 case X86_FPU_FLAGS_UNDEFINED_C3:
168 return "UNDEF_C3";
169 case X86_FPU_FLAGS_TEST_C0:
170 return "TEST_C0";
171 case X86_FPU_FLAGS_TEST_C1:
172 return "TEST_C1";
173 case X86_FPU_FLAGS_TEST_C2:
174 return "TEST_C2";
175 case X86_FPU_FLAGS_TEST_C3:
176 return "TEST_C3";
177 }
178 }
179
print_insn_detail_x86(csh ud,cs_mode mode,cs_insn * ins)180 void print_insn_detail_x86(csh ud, cs_mode mode, cs_insn *ins)
181 {
182 int count, i;
183 cs_x86 *x86;
184 cs_regs regs_read, regs_write;
185 uint8_t regs_read_count, regs_write_count;
186
187 // detail can be NULL on "data" instruction if SKIPDATA option is turned ON
188 if (ins->detail == NULL)
189 return;
190
191 x86 = &(ins->detail->x86);
192
193 print_string_hex("\tPrefix:", x86->prefix, 4);
194 print_string_hex("\tOpcode:", x86->opcode, 4);
195 printf("\trex: 0x%x\n", x86->rex);
196 printf("\taddr_size: %u\n", x86->addr_size);
197 printf("\tmodrm: 0x%x\n", x86->modrm);
198 printf("\tdisp: 0x%" PRIx64 "\n", x86->disp);
199
200 // SIB is not available in 16-bit mode
201 if ((mode & CS_MODE_16) == 0) {
202 printf("\tsib: 0x%x\n", x86->sib);
203 if (x86->sib_base != X86_REG_INVALID)
204 printf("\t\tsib_base: %s\n", cs_reg_name(ud, x86->sib_base));
205 if (x86->sib_index != X86_REG_INVALID)
206 printf("\t\tsib_index: %s\n", cs_reg_name(ud, x86->sib_index));
207 if (x86->sib_scale != 0)
208 printf("\t\tsib_scale: %d\n", x86->sib_scale);
209 }
210
211 // XOP code condition
212 if (x86->xop_cc != X86_XOP_CC_INVALID) {
213 printf("\txop_cc: %u\n", x86->xop_cc);
214 }
215
216 // SSE code condition
217 if (x86->sse_cc != X86_SSE_CC_INVALID) {
218 printf("\tsse_cc: %u\n", x86->sse_cc);
219 }
220
221 // AVX code condition
222 if (x86->avx_cc != X86_AVX_CC_INVALID) {
223 printf("\tavx_cc: %u\n", x86->avx_cc);
224 }
225
226 // AVX Suppress All Exception
227 if (x86->avx_sae) {
228 printf("\tavx_sae: %u\n", x86->avx_sae);
229 }
230
231 // AVX Rounding Mode
232 if (x86->avx_rm != X86_AVX_RM_INVALID) {
233 printf("\tavx_rm: %u\n", x86->avx_rm);
234 }
235
236 // Print out all immediate operands
237 count = cs_op_count(ud, ins, X86_OP_IMM);
238 if (count > 0) {
239 printf("\timm_count: %u\n", count);
240 for (i = 1; i < count + 1; i++) {
241 int index = cs_op_index(ud, ins, X86_OP_IMM, i);
242 printf("\t\timms[%u]: 0x%" PRIx64 "\n", i, x86->operands[index].imm);
243 }
244 }
245
246 if (x86->op_count)
247 printf("\top_count: %u\n", x86->op_count);
248
249 // Print out all operands
250 for (i = 0; i < x86->op_count; i++) {
251 cs_x86_op *op = &(x86->operands[i]);
252
253 switch((int)op->type) {
254 case X86_OP_REG:
255 printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(ud, op->reg));
256 break;
257 case X86_OP_IMM:
258 printf("\t\toperands[%u].type: IMM = 0x%" PRIx64 "\n", i, op->imm);
259 break;
260 case X86_OP_MEM:
261 printf("\t\toperands[%u].type: MEM\n", i);
262 if (op->mem.segment != X86_REG_INVALID)
263 printf("\t\t\toperands[%u].mem.segment: REG = %s\n", i, cs_reg_name(ud, op->mem.segment));
264 if (op->mem.base != X86_REG_INVALID)
265 printf("\t\t\toperands[%u].mem.base: REG = %s\n", i, cs_reg_name(ud, op->mem.base));
266 if (op->mem.index != X86_REG_INVALID)
267 printf("\t\t\toperands[%u].mem.index: REG = %s\n", i, cs_reg_name(ud, op->mem.index));
268 if (op->mem.scale != 1)
269 printf("\t\t\toperands[%u].mem.scale: %u\n", i, op->mem.scale);
270 if (op->mem.disp != 0)
271 printf("\t\t\toperands[%u].mem.disp: 0x%" PRIx64 "\n", i, op->mem.disp);
272 break;
273 default:
274 break;
275 }
276
277 // AVX broadcast type
278 if (op->avx_bcast != X86_AVX_BCAST_INVALID)
279 printf("\t\toperands[%u].avx_bcast: %u\n", i, op->avx_bcast);
280
281 // AVX zero opmask {z}
282 if (op->avx_zero_opmask != false)
283 printf("\t\toperands[%u].avx_zero_opmask: TRUE\n", i);
284
285 printf("\t\toperands[%u].size: %u\n", i, op->size);
286
287 switch(op->access) {
288 default:
289 break;
290 case CS_AC_READ:
291 printf("\t\toperands[%u].access: READ\n", i);
292 break;
293 case CS_AC_WRITE:
294 printf("\t\toperands[%u].access: WRITE\n", i);
295 break;
296 case CS_AC_READ | CS_AC_WRITE:
297 printf("\t\toperands[%u].access: READ | WRITE\n", i);
298 break;
299 }
300 }
301
302 // Print out all registers accessed by this instruction (either implicit or explicit)
303 if (!cs_regs_access(ud, ins,
304 regs_read, ®s_read_count,
305 regs_write, ®s_write_count)) {
306 if (regs_read_count) {
307 printf("\tRegisters read:");
308 for(i = 0; i < regs_read_count; i++) {
309 printf(" %s", cs_reg_name(ud, regs_read[i]));
310 }
311 printf("\n");
312 }
313
314 if (regs_write_count) {
315 printf("\tRegisters modified:");
316 for(i = 0; i < regs_write_count; i++) {
317 printf(" %s", cs_reg_name(ud, regs_write[i]));
318 }
319 printf("\n");
320 }
321 }
322
323 if (x86->eflags || x86->fpu_flags) {
324 for(i = 0; i < ins->detail->groups_count; i++) {
325 if (ins->detail->groups[i] == X86_GRP_FPU) {
326 printf("\tFPU_FLAGS:");
327 for(i = 0; i <= 63; i++)
328 if (x86->fpu_flags & ((uint64_t)1 << i)) {
329 printf(" %s", get_fpu_flag_name((uint64_t)1 << i));
330 }
331 printf("\n");
332 break;
333 }
334 }
335
336 if (i == ins->detail->groups_count) {
337 printf("\tEFLAGS:");
338 for(i = 0; i <= 63; i++)
339 if (x86->eflags & ((uint64_t)1 << i)) {
340 printf(" %s", get_eflag_name((uint64_t)1 << i));
341 }
342 printf("\n");
343 }
344 }
345 }
346