1 #include <stdio.h>
2 #include <stdlib.h>
3 
4 #include <capstone/capstone.h>
5 
6 void print_string_hex(char *comment, unsigned char *str, size_t len);
7 
print_insn_detail_arm(csh handle,cs_insn * ins)8 void print_insn_detail_arm(csh handle, cs_insn *ins)
9 {
10 	cs_arm *arm;
11 	int i;
12 	cs_regs regs_read, regs_write;
13 	uint8_t regs_read_count, regs_write_count;
14 
15 	// detail can be NULL on "data" instruction if SKIPDATA option is turned ON
16 	if (ins->detail == NULL)
17 		return;
18 
19 	arm = &(ins->detail->arm);
20 
21 	if (arm->op_count)
22 		printf("\top_count: %u\n", arm->op_count);
23 
24 	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:
36 #if defined(_KERNEL_MODE)
37 				// Issue #681: Windows kernel does not support formatting float point
38 				printf("\t\toperands[%u].type: FP = <float_point_unsupported>\n", i);
39 #else
40 				printf("\t\toperands[%u].type: FP = %f\n", i, op->fp);
41 #endif
42 				break;
43 			case ARM_OP_MEM:
44 				printf("\t\toperands[%u].type: MEM\n", i);
45 				if (op->mem.base != ARM_REG_INVALID)
46 					printf("\t\t\toperands[%u].mem.base: REG = %s\n",
47 							i, cs_reg_name(handle, op->mem.base));
48 				if (op->mem.index != ARM_REG_INVALID)
49 					printf("\t\t\toperands[%u].mem.index: REG = %s\n",
50 							i, cs_reg_name(handle, op->mem.index));
51 				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);
55 				if (op->mem.lshift != 0)
56 					printf("\t\t\toperands[%u].mem.lshift: 0x%x\n", i, op->mem.lshift);
57 
58 				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 		}
72 
73 		if (op->neon_lane != -1) {
74 			printf("\t\toperands[%u].neon_lane = %u\n", i, op->neon_lane);
75 		}
76 
77 		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 		}
90 
91 		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,
98 						cs_reg_name(handle, op->shift.value));
99 		}
100 
101 		if (op->vector_index != -1) {
102 			printf("\t\toperands[%u].vector_index = %u\n", i, op->vector_index);
103 		}
104 
105 		if (op->subtracted)
106 			printf("\t\tSubtracted: True\n");
107 	}
108 
109 	if (arm->cc != ARM_CC_AL && arm->cc != ARM_CC_INVALID)
110 		printf("\tCode condition: %u\n", arm->cc);
111 
112 	if (arm->update_flags)
113 		printf("\tUpdate-flags: True\n");
114 
115 	if (arm->writeback)
116 		printf("\tWrite-back: True\n");
117 
118 	if (arm->cps_mode)
119 		printf("\tCPSI-mode: %u\n", arm->cps_mode);
120 
121 	if (arm->cps_flag)
122 		printf("\tCPSI-flag: %u\n", arm->cps_flag);
123 
124 	if (arm->vector_data)
125 		printf("\tVector-data: %u\n", arm->vector_data);
126 
127 	if (arm->vector_size)
128 		printf("\tVector-size: %u\n", arm->vector_size);
129 
130 	if (arm->usermode)
131 		printf("\tUser-mode: True\n");
132 
133 	if (arm->mem_barrier)
134 		printf("\tMemory-barrier: %u\n", arm->mem_barrier);
135 
136 	// Print out all registers accessed by this instruction (either implicit or explicit)
137 	if (!cs_regs_access(handle, ins,
138 				regs_read, &regs_read_count,
139 				regs_write, &regs_write_count)) {
140 		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 		}
147 
148 		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 	}
156 }
157