blob: f2c262076f5d6a966388b72967186189aa4e35a9 [file] [log] [blame]
YUHANG TANG08da0c02016-10-14 20:47:29 +08001/* Capstone Disassembler Engine */
2/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013> */
3
4#include <stdio.h>
5#include <stdlib.h>
6
Nguyen Anh Quynh2897a802016-10-21 17:03:27 +08007#include <capstone/capstone.h>
YUHANG TANG08da0c02016-10-14 20:47:29 +08008
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +08009void print_string_hex(char *comment, unsigned char *str, size_t len);
YUHANG TANG08da0c02016-10-14 20:47:29 +080010
11void print_insn_detail_arm64(csh handle, cs_insn *ins)
12{
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +080013 cs_arm64 *arm64;
14 int i;
YUHANG TANGbe3f8672016-10-27 12:12:59 +080015 cs_regs regs_read, regs_write;
16 uint8_t regs_read_count, regs_write_count;
Nguyen Anh Quynh1af1e412018-02-14 15:02:05 +080017 uint8_t access;
YUHANG TANGbe3f8672016-10-27 12:12:59 +080018
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +080019 // detail can be NULL if SKIPDATA option is turned ON
20 if (ins->detail == NULL)
21 return;
YUHANG TANG9bc14c12016-10-28 15:32:50 +080022
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +080023 arm64 = &(ins->detail->arm64);
24 if (arm64->op_count)
25 printf("\top_count: %u\n", arm64->op_count);
YUHANG TANG9bc14c12016-10-28 15:32:50 +080026
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +080027 for (i = 0; i < arm64->op_count; i++) {
28 cs_arm64_op *op = &(arm64->operands[i]);
29 switch(op->type) {
30 default:
31 break;
32 case ARM64_OP_REG:
33 printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(handle, op->reg));
34 break;
35 case ARM64_OP_IMM:
36 printf("\t\toperands[%u].type: IMM = 0x%" PRIx64 "\n", i, op->imm);
37 break;
38 case ARM64_OP_FP:
YUHANG TANG08da0c02016-10-14 20:47:29 +080039#if defined(_KERNEL_MODE)
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +080040 // Issue #681: Windows kernel does not support formatting float point
41 printf("\t\toperands[%u].type: FP = <float_point_unsupported>\n", i);
YUHANG TANG08da0c02016-10-14 20:47:29 +080042#else
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +080043 printf("\t\toperands[%u].type: FP = %f\n", i, op->fp);
YUHANG TANG08da0c02016-10-14 20:47:29 +080044#endif
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +080045 break;
46 case ARM64_OP_MEM:
47 printf("\t\toperands[%u].type: MEM\n", i);
48 if (op->mem.base != ARM64_REG_INVALID)
49 printf("\t\t\toperands[%u].mem.base: REG = %s\n", i, cs_reg_name(handle, op->mem.base));
50 if (op->mem.index != ARM64_REG_INVALID)
51 printf("\t\t\toperands[%u].mem.index: REG = %s\n", i, cs_reg_name(handle, op->mem.index));
52 if (op->mem.disp != 0)
53 printf("\t\t\toperands[%u].mem.disp: 0x%x\n", i, op->mem.disp);
YUHANG TANG9bc14c12016-10-28 15:32:50 +080054
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +080055 break;
56 case ARM64_OP_CIMM:
57 printf("\t\toperands[%u].type: C-IMM = %u\n", i, (int)op->imm);
58 break;
59 case ARM64_OP_REG_MRS:
60 printf("\t\toperands[%u].type: REG_MRS = 0x%x\n", i, op->reg);
61 break;
62 case ARM64_OP_REG_MSR:
63 printf("\t\toperands[%u].type: REG_MSR = 0x%x\n", i, op->reg);
64 break;
65 case ARM64_OP_PSTATE:
66 printf("\t\toperands[%u].type: PSTATE = 0x%x\n", i, op->pstate);
67 break;
68 case ARM64_OP_SYS:
69 printf("\t\toperands[%u].type: SYS = 0x%x\n", i, op->sys);
70 break;
71 case ARM64_OP_PREFETCH:
72 printf("\t\toperands[%u].type: PREFETCH = 0x%x\n", i, op->prefetch);
73 break;
74 case ARM64_OP_BARRIER:
75 printf("\t\toperands[%u].type: BARRIER = 0x%x\n", i, op->barrier);
76 break;
77 }
YUHANG TANGbe3f8672016-10-27 12:12:59 +080078
Nguyen Anh Quynh1af1e412018-02-14 15:02:05 +080079 access = op->access;
YUHANG TANGbe3f8672016-10-27 12:12:59 +080080 switch(access) {
81 default:
82 break;
83 case CS_AC_READ:
84 printf("\t\toperands[%u].access: READ\n", i);
85 break;
86 case CS_AC_WRITE:
87 printf("\t\toperands[%u].access: WRITE\n", i);
88 break;
89 case CS_AC_READ | CS_AC_WRITE:
90 printf("\t\toperands[%u].access: READ | WRITE\n", i);
91 break;
92 }
93
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +080094 if (op->shift.type != ARM64_SFT_INVALID &&
YUHANG TANGbe3f8672016-10-27 12:12:59 +080095 op->shift.value)
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +080096 printf("\t\t\tShift: type = %u, value = %u\n",
YUHANG TANGbe3f8672016-10-27 12:12:59 +080097 op->shift.type, op->shift.value);
YUHANG TANG9bc14c12016-10-28 15:32:50 +080098
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +080099 if (op->ext != ARM64_EXT_INVALID)
100 printf("\t\t\tExt: %u\n", op->ext);
YUHANG TANG9bc14c12016-10-28 15:32:50 +0800101
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +0800102 if (op->vas != ARM64_VAS_INVALID)
103 printf("\t\t\tVector Arrangement Specifier: 0x%x\n", op->vas);
YUHANG TANG9bc14c12016-10-28 15:32:50 +0800104
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +0800105 if (op->vess != ARM64_VESS_INVALID)
106 printf("\t\t\tVector Element Size Specifier: %u\n", op->vess);
YUHANG TANG9bc14c12016-10-28 15:32:50 +0800107
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +0800108 if (op->vector_index != -1)
109 printf("\t\t\tVector Index: %u\n", op->vector_index);
110 }
YUHANG TANG9bc14c12016-10-28 15:32:50 +0800111
Nguyen Anh Quynh32238dc2016-10-21 16:42:47 +0800112 if (arm64->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 (arm64->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 (arm64->cc)
119 printf("\tCode-condition: %u\n", arm64->cc);
YUHANG TANG9bc14c12016-10-28 15:32:50 +0800120
YUHANG TANGbe3f8672016-10-27 12:12:59 +0800121 // Print out all registers accessed by this instruction (either implicit or explicit)
122 if (!cs_regs_access(handle, ins,
123 regs_read, &regs_read_count,
124 regs_write, &regs_write_count)) {
125 if (regs_read_count) {
126 printf("\tRegisters read:");
127 for(i = 0; i < regs_read_count; i++) {
128 printf(" %s", cs_reg_name(handle, regs_read[i]));
129 }
130 printf("\n");
131 }
132
133 if (regs_write_count) {
134 printf("\tRegisters modified:");
135 for(i = 0; i < regs_write_count; i++) {
136 printf(" %s", cs_reg_name(handle, regs_write[i]));
137 }
138 printf("\n");
139 }
140 }
YUHANG TANG08da0c02016-10-14 20:47:29 +0800141}