blob: 20afd08dfa0ff21d28d9b844f85a8e0577f724b7 [file] [log] [blame]
YUHANG TANG08da0c02016-10-14 20:47:29 +08001#include <stdio.h>
2#include <stdlib.h>
3
Nguyen Anh Quynh2897a802016-10-21 17:03:27 +08004#include <capstone/capstone.h>
YUHANG TANG08da0c02016-10-14 20:47:29 +08005
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +08006void print_string_hex(char *comment, unsigned char *str, size_t len);
YUHANG TANG08da0c02016-10-14 20:47:29 +08007
8void print_insn_detail_arm(csh handle, cs_insn *ins)
9{
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +080010 cs_arm *arm;
11 int i;
YUHANG TANGbe3f8672016-10-27 12:12:59 +080012 cs_regs regs_read, regs_write;
13 uint8_t regs_read_count, regs_write_count;
Nguyen Anh Quynhdf6f9cc2016-10-28 16:12:05 +080014
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +080015 // detail can be NULL on "data" instruction if SKIPDATA option is turned ON
16 if (ins->detail == NULL)
17 return;
YUHANG TANG9bc14c12016-10-28 15:32:50 +080018
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +080019 arm = &(ins->detail->arm);
YUHANG TANG9bc14c12016-10-28 15:32:50 +080020
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +080021 if (arm->op_count)
22 printf("\top_count: %u\n", arm->op_count);
YUHANG TANG9bc14c12016-10-28 15:32:50 +080023
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +080024 for (i = 0; i < arm->op_count; i++) {
25 cs_arm_op *op = &(arm->operands[i]);
26 switch((int)op->type) {
27 default:
28 break;
29 case ARM_OP_REG:
30 printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(handle, op->reg));
31 break;
32 case ARM_OP_IMM:
33 printf("\t\toperands[%u].type: IMM = 0x%x\n", i, op->imm);
34 break;
35 case ARM_OP_FP:
YUHANG TANG08da0c02016-10-14 20:47:29 +080036#if defined(_KERNEL_MODE)
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +080037 // Issue #681: Windows kernel does not support formatting float point
38 printf("\t\toperands[%u].type: FP = <float_point_unsupported>\n", i);
YUHANG TANG08da0c02016-10-14 20:47:29 +080039#else
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +080040 printf("\t\toperands[%u].type: FP = %f\n", i, op->fp);
YUHANG TANG08da0c02016-10-14 20:47:29 +080041#endif
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +080042 break;
43 case ARM_OP_MEM:
44 printf("\t\toperands[%u].type: MEM\n", i);
YUHANG TANGbe3f8672016-10-27 12:12:59 +080045 if (op->mem.base != ARM_REG_INVALID)
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +080046 printf("\t\t\toperands[%u].mem.base: REG = %s\n",
Nguyen Anh Quynhdf6f9cc2016-10-28 16:12:05 +080047 i, cs_reg_name(handle, op->mem.base));
YUHANG TANGbe3f8672016-10-27 12:12:59 +080048 if (op->mem.index != ARM_REG_INVALID)
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +080049 printf("\t\t\toperands[%u].mem.index: REG = %s\n",
Nguyen Anh Quynhdf6f9cc2016-10-28 16:12:05 +080050 i, cs_reg_name(handle, op->mem.index));
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +080051 if (op->mem.scale != 1)
52 printf("\t\t\toperands[%u].mem.scale: %u\n", i, op->mem.scale);
53 if (op->mem.disp != 0)
54 printf("\t\t\toperands[%u].mem.disp: 0x%x\n", i, op->mem.disp);
YUHANG TANGbe3f8672016-10-27 12:12:59 +080055 if (op->mem.lshift != 0)
56 printf("\t\t\toperands[%u].mem.lshift: 0x%x\n", i, op->mem.lshift);
Nguyen Anh Quynhdf6f9cc2016-10-28 16:12:05 +080057
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +080058 break;
59 case ARM_OP_PIMM:
60 printf("\t\toperands[%u].type: P-IMM = %u\n", i, op->imm);
61 break;
62 case ARM_OP_CIMM:
63 printf("\t\toperands[%u].type: C-IMM = %u\n", i, op->imm);
64 break;
65 case ARM_OP_SETEND:
66 printf("\t\toperands[%u].type: SETEND = %s\n", i, op->setend == ARM_SETEND_BE? "be" : "le");
67 break;
68 case ARM_OP_SYSREG:
69 printf("\t\toperands[%u].type: SYSREG = %u\n", i, op->reg);
70 break;
71 }
Nguyen Anh Quynhdf6f9cc2016-10-28 16:12:05 +080072
YUHANG TANGbe3f8672016-10-27 12:12:59 +080073 if (op->neon_lane != -1) {
74 printf("\t\toperands[%u].neon_lane = %u\n", i, op->neon_lane);
75 }
Nguyen Anh Quynhdf6f9cc2016-10-28 16:12:05 +080076
YUHANG TANGbe3f8672016-10-27 12:12:59 +080077 switch(op->access) {
78 default:
79 break;
80 case CS_AC_READ:
81 printf("\t\toperands[%u].access: READ\n", i);
82 break;
83 case CS_AC_WRITE:
84 printf("\t\toperands[%u].access: WRITE\n", i);
85 break;
86 case CS_AC_READ | CS_AC_WRITE:
87 printf("\t\toperands[%u].access: READ | WRITE\n", i);
88 break;
89 }
Nguyen Anh Quynhdf6f9cc2016-10-28 16:12:05 +080090
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +080091 if (op->shift.type != ARM_SFT_INVALID && op->shift.value) {
92 if (op->shift.type < ARM_SFT_ASR_REG)
93 // shift with constant value
94 printf("\t\t\tShift: %u = %u\n", op->shift.type, op->shift.value);
95 else
96 // shift with register
97 printf("\t\t\tShift: %u = %s\n", op->shift.type,
Nguyen Anh Quynhdf6f9cc2016-10-28 16:12:05 +080098 cs_reg_name(handle, op->shift.value));
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +080099 }
YUHANG TANG9bc14c12016-10-28 15:32:50 +0800100
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +0800101 if (op->vector_index != -1) {
102 printf("\t\toperands[%u].vector_index = %u\n", i, op->vector_index);
103 }
YUHANG TANG9bc14c12016-10-28 15:32:50 +0800104
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +0800105 if (op->subtracted)
106 printf("\t\tSubtracted: True\n");
107 }
YUHANG TANG9bc14c12016-10-28 15:32:50 +0800108
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +0800109 if (arm->cc != ARM_CC_AL && arm->cc != ARM_CC_INVALID)
110 printf("\tCode condition: %u\n", arm->cc);
YUHANG TANG9bc14c12016-10-28 15:32:50 +0800111
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +0800112 if (arm->update_flags)
113 printf("\tUpdate-flags: True\n");
YUHANG TANG9bc14c12016-10-28 15:32:50 +0800114
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +0800115 if (arm->writeback)
116 printf("\tWrite-back: True\n");
YUHANG TANG9bc14c12016-10-28 15:32:50 +0800117
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +0800118 if (arm->cps_mode)
119 printf("\tCPSI-mode: %u\n", arm->cps_mode);
YUHANG TANG9bc14c12016-10-28 15:32:50 +0800120
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +0800121 if (arm->cps_flag)
122 printf("\tCPSI-flag: %u\n", arm->cps_flag);
YUHANG TANG9bc14c12016-10-28 15:32:50 +0800123
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +0800124 if (arm->vector_data)
125 printf("\tVector-data: %u\n", arm->vector_data);
YUHANG TANG9bc14c12016-10-28 15:32:50 +0800126
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +0800127 if (arm->vector_size)
128 printf("\tVector-size: %u\n", arm->vector_size);
YUHANG TANG9bc14c12016-10-28 15:32:50 +0800129
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +0800130 if (arm->usermode)
131 printf("\tUser-mode: True\n");
YUHANG TANG9bc14c12016-10-28 15:32:50 +0800132
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +0800133 if (arm->mem_barrier)
134 printf("\tMemory-barrier: %u\n", arm->mem_barrier);
YUHANG TANG9bc14c12016-10-28 15:32:50 +0800135
YUHANG TANGbe3f8672016-10-27 12:12:59 +0800136 // Print out all registers accessed by this instruction (either implicit or explicit)
137 if (!cs_regs_access(handle, ins,
Nguyen Anh Quynhdf6f9cc2016-10-28 16:12:05 +0800138 regs_read, &regs_read_count,
139 regs_write, &regs_write_count)) {
YUHANG TANGbe3f8672016-10-27 12:12:59 +0800140 if (regs_read_count) {
141 printf("\tRegisters read:");
142 for(i = 0; i < regs_read_count; i++) {
143 printf(" %s", cs_reg_name(handle, regs_read[i]));
144 }
145 printf("\n");
146 }
Nguyen Anh Quynhdf6f9cc2016-10-28 16:12:05 +0800147
YUHANG TANGbe3f8672016-10-27 12:12:59 +0800148 if (regs_write_count) {
149 printf("\tRegisters modified:");
150 for(i = 0; i < regs_write_count; i++) {
151 printf(" %s", cs_reg_name(handle, regs_write[i]));
152 }
153 printf("\n");
154 }
155 }
YUHANG TANG08da0c02016-10-14 20:47:29 +0800156}