Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 1 | #ifndef __CS_H__ |
| 2 | #define __CS_H__ |
| 3 | |
| 4 | /* Capstone Disassembler Engine */ |
| 5 | /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013> */ |
| 6 | |
| 7 | #ifdef __cplusplus |
| 8 | extern "C" { |
| 9 | #endif |
| 10 | |
| 11 | #include <stdint.h> |
| 12 | #include <stdbool.h> |
Nguyen Anh Quynh | f185180 | 2013-12-21 12:16:47 +0800 | [diff] [blame] | 13 | #include <stdlib.h> |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 14 | |
Nguyen Anh Quynh | bb64b0b | 2013-12-10 07:56:17 +0800 | [diff] [blame] | 15 | // Capstone API version |
| 16 | #define CS_API_MAJOR 1 |
Nguyen Anh Quynh | 39a42ed | 2013-12-22 10:40:58 +0800 | [diff] [blame] | 17 | #define CS_API_MINOR 1 |
Nguyen Anh Quynh | bb64b0b | 2013-12-10 07:56:17 +0800 | [diff] [blame] | 18 | |
Nguyen Anh Quynh | 2296d5e | 2013-12-22 21:01:17 +0800 | [diff] [blame] | 19 | // Macro to create combined version which can be compared to |
| 20 | // result of cs_version_ex() API. |
| 21 | #define CS_MAKE_VERSION(major, minor) ((major << 8) + minor) |
| 22 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 23 | // Handle using with all API |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 24 | typedef size_t csh; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 25 | |
| 26 | // Architecture type |
| 27 | typedef enum cs_arch { |
| 28 | CS_ARCH_ARM = 0, // ARM architecture (including Thumb, Thumb-2) |
| 29 | CS_ARCH_ARM64, // ARM-64, also called AArch64 |
| 30 | CS_ARCH_MIPS, // Mips architecture |
| 31 | CS_ARCH_X86, // X86 architecture (including x86 & x86-64) |
| 32 | CS_ARCH_MAX, |
Nguyen Anh Quynh | 39a42ed | 2013-12-22 10:40:58 +0800 | [diff] [blame] | 33 | CS_ARCH_ALL = 0xFFFF, |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 34 | } cs_arch; |
| 35 | |
| 36 | // Mode type |
| 37 | typedef enum cs_mode { |
| 38 | CS_MODE_LITTLE_ENDIAN = 0, // little endian mode (default mode) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 39 | CS_MODE_ARM = 0, // 32-bit ARM |
| 40 | CS_MODE_16 = 1 << 1, // 16-bit mode |
| 41 | CS_MODE_32 = 1 << 2, // 32-bit mode |
| 42 | CS_MODE_64 = 1 << 3, // 64-bit mode |
| 43 | CS_MODE_THUMB = 1 << 4, // ARM's Thumb mode, including Thumb-2 |
| 44 | CS_MODE_MICRO = 1 << 4, // MicroMips mode (MIPS architecture) |
| 45 | CS_MODE_N64 = 1 << 5, // Nintendo-64 mode (MIPS architecture) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 46 | |
| 47 | CS_MODE_BIG_ENDIAN = 1 << 31 // big endian mode |
| 48 | } cs_mode; |
| 49 | |
Nguyen Anh Quynh | 79976c1 | 2013-12-04 23:03:13 +0800 | [diff] [blame] | 50 | // Runtime option for the disassembled engine |
Nguyen Anh Quynh | b8ce68e | 2013-12-03 23:45:08 +0800 | [diff] [blame] | 51 | typedef enum cs_opt_type { |
Nguyen Anh Quynh | 4d70daf | 2013-12-05 09:50:50 +0800 | [diff] [blame] | 52 | CS_OPT_SYNTAX = 1, // Asssembly output syntax |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 53 | CS_OPT_DETAIL, // Break down instruction structure into details |
Nguyen Anh Quynh | 1bdb23a | 2013-12-20 00:04:26 +0800 | [diff] [blame] | 54 | CS_OPT_MODE, // Change engine's mode at run-time |
Nguyen Anh Quynh | b8ce68e | 2013-12-03 23:45:08 +0800 | [diff] [blame] | 55 | } cs_opt_type; |
| 56 | |
Nguyen Anh Quynh | 79976c1 | 2013-12-04 23:03:13 +0800 | [diff] [blame] | 57 | // Runtime option value (associated with option type above) |
Nguyen Anh Quynh | b8ce68e | 2013-12-03 23:45:08 +0800 | [diff] [blame] | 58 | typedef enum cs_opt_value { |
Nguyen Anh Quynh | 4994c58 | 2013-12-14 11:39:33 +0800 | [diff] [blame] | 59 | CS_OPT_OFF = 0, // Turn OFF an option (CS_OPT_DETAIL) |
| 60 | CS_OPT_SYNTAX_INTEL = 1, // X86 Intel asm syntax - default syntax on X86 (CS_OPT_SYNTAX). |
Nguyen Anh Quynh | c618db4 | 2013-12-04 00:05:04 +0800 | [diff] [blame] | 61 | CS_OPT_SYNTAX_ATT, // X86 ATT asm syntax (CS_OPT_SYNTAX) |
Nguyen Anh Quynh | 4994c58 | 2013-12-14 11:39:33 +0800 | [diff] [blame] | 62 | CS_OPT_ON, // Turn ON an option - default option for CS_OPT_DETAIL |
Nguyen Anh Quynh | b8ce68e | 2013-12-03 23:45:08 +0800 | [diff] [blame] | 63 | } cs_opt_value; |
Nguyen Anh Quynh | 01aba00 | 2013-12-03 21:00:09 +0800 | [diff] [blame] | 64 | |
| 65 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 66 | #include "arm.h" |
| 67 | #include "arm64.h" |
| 68 | #include "mips.h" |
| 69 | #include "x86.h" |
| 70 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame^] | 71 | // NOTE: All information in cs_detail is only available when CS_OPT_DETAIL = CS_OPT_ON |
| 72 | typedef struct cs_detail { |
| 73 | uint8_t regs_read[12]; // list of implicit registers read by this insn |
| 74 | uint8_t regs_read_count; // number of implicit registers read by this insn |
| 75 | |
| 76 | uint8_t regs_write[20]; // list of implicit registers modified by this insn |
| 77 | uint8_t regs_write_count; // number of implicit registers modified by this insn |
| 78 | |
| 79 | uint8_t groups[8]; // list of group this instruction belong to |
| 80 | uint8_t groups_count; // number of groups this insn belongs to |
| 81 | |
| 82 | // Architecture-specific instruction info |
| 83 | union { |
| 84 | cs_x86 x86; // X86 architecture, including 16-bit, 32-bit & 64-bit mode |
| 85 | cs_arm64 arm64; // ARM64 architecture (aka AArch64) |
| 86 | cs_arm arm; // ARM architecture (including Thumb/Thumb2) |
| 87 | cs_mips mips; // MIPS architecture |
| 88 | }; |
| 89 | } cs_detail; |
| 90 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 91 | // Detail information of disassembled instruction |
| 92 | typedef struct cs_insn { |
| 93 | // Instruction ID |
| 94 | // Find the instruction id from header file of corresponding architecture, |
| 95 | // such as arm.h for ARM, x86.h for X86, etc... |
Nguyen Anh Quynh | 46a5afd | 2013-12-14 11:52:06 +0800 | [diff] [blame] | 96 | // This information is available even when CS_OPT_DETAIL = CS_OPT_OFF |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 97 | unsigned int id; |
| 98 | |
Nguyen Anh Quynh | 79976c1 | 2013-12-04 23:03:13 +0800 | [diff] [blame] | 99 | // Address (EIP) of this instruction |
Nguyen Anh Quynh | 46a5afd | 2013-12-14 11:52:06 +0800 | [diff] [blame] | 100 | // This information is available even when CS_OPT_DETAIL = CS_OPT_OFF |
Nguyen Anh Quynh | f2a649e | 2013-12-03 12:21:01 +0800 | [diff] [blame] | 101 | uint64_t address; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 102 | |
Nguyen Anh Quynh | 79976c1 | 2013-12-04 23:03:13 +0800 | [diff] [blame] | 103 | // Size of this instruction |
Nguyen Anh Quynh | 46a5afd | 2013-12-14 11:52:06 +0800 | [diff] [blame] | 104 | // This information is available even when CS_OPT_DETAIL = CS_OPT_OFF |
Nguyen Anh Quynh | 8f13f3c | 2013-12-04 22:57:04 +0800 | [diff] [blame] | 105 | uint16_t size; |
Nguyen Anh Quynh | 79976c1 | 2013-12-04 23:03:13 +0800 | [diff] [blame] | 106 | // Machine bytes of this instruction, with number of bytes indicated by @size above |
Nguyen Anh Quynh | 46a5afd | 2013-12-14 11:52:06 +0800 | [diff] [blame] | 107 | // This information is available even when CS_OPT_DETAIL = CS_OPT_OFF |
pancake | f0e4eed | 2013-12-11 22:14:42 +0100 | [diff] [blame] | 108 | uint8_t bytes[16]; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 109 | |
| 110 | // Ascii text of instruction mnemonic |
Nguyen Anh Quynh | 46a5afd | 2013-12-14 11:52:06 +0800 | [diff] [blame] | 111 | // This information is available even when CS_OPT_DETAIL = CS_OPT_OFF |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 112 | char mnemonic[32]; |
| 113 | |
| 114 | // Ascii text of instruction operands |
Nguyen Anh Quynh | 46a5afd | 2013-12-14 11:52:06 +0800 | [diff] [blame] | 115 | // This information is available even when CS_OPT_DETAIL = CS_OPT_OFF |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 116 | char op_str[96]; |
| 117 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame^] | 118 | // Pointer to cs_detail. |
| 119 | // NOTE: detail pointer is only valid (not NULL) when CS_OP_DETAIL = CS_OPT_ON |
| 120 | // Otherwise, if CS_OPT_DETAIL = CS_OPT_OFF, @detail = NULL |
| 121 | cs_detail *detail; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 122 | } cs_insn; |
| 123 | |
Nguyen Anh Quynh | bb54603 | 2013-12-05 18:29:51 +0800 | [diff] [blame] | 124 | |
| 125 | // Calculate the offset of a disassembled instruction in its buffer, given its position |
| 126 | // in its array of disassembled insn |
Nguyen Anh Quynh | a236902 | 2013-12-05 20:21:09 +0800 | [diff] [blame] | 127 | // NOTE: this macro works with position (>=1), not index |
| 128 | #define CS_INSN_OFFSET(insns, post) (insns[post - 1].address - insns[0].address) |
Nguyen Anh Quynh | bb54603 | 2013-12-05 18:29:51 +0800 | [diff] [blame] | 129 | |
| 130 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 131 | // All type of errors encountered by Capstone API. |
| 132 | // These are values returned by cs_errno() |
| 133 | typedef enum cs_err { |
| 134 | CS_ERR_OK = 0, // No error: everything was fine |
Nguyen Anh Quynh | 041e25d | 2013-12-06 00:37:32 +0800 | [diff] [blame] | 135 | CS_ERR_MEM, // Out-Of-Memory error: cs_open(), cs_disasm_dyn() |
| 136 | CS_ERR_ARCH, // Unsupported architecture: cs_open() |
| 137 | CS_ERR_HANDLE, // Invalid handle: cs_op_count(), cs_op_index() |
| 138 | CS_ERR_CSH, // Invalid csh argument: cs_close(), cs_errno(), cs_option() |
| 139 | CS_ERR_MODE, // Invalid/unsupported mode: cs_open() |
| 140 | CS_ERR_OPTION, // Invalid/unsupported option: cs_option() |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame^] | 141 | CS_ERR_DETAIL, // Information is unavailable because detail option is OFF |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 142 | } cs_err; |
| 143 | |
Nguyen Anh Quynh | b880678 | 2013-12-22 15:20:07 +0800 | [diff] [blame] | 144 | /* |
| 145 | Retrieve API version in major and minor numbers. |
| 146 | |
| 147 | @major: major number of API version |
| 148 | @minor: minor number of API version |
| 149 | |
| 150 | For example, first API version would return 1 in @major, and 0 in @minor |
| 151 | */ |
| 152 | void cs_version(int *major, int *minor); |
| 153 | |
Nguyen Anh Quynh | 79976c1 | 2013-12-04 23:03:13 +0800 | [diff] [blame] | 154 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 155 | /* |
Nguyen Anh Quynh | b880678 | 2013-12-22 15:20:07 +0800 | [diff] [blame] | 156 | Return combined API version & major and minor version numbers. |
Nguyen Anh Quynh | 36df4bb | 2013-12-10 13:31:20 +0800 | [diff] [blame] | 157 | |
Nguyen Anh Quynh | b880678 | 2013-12-22 15:20:07 +0800 | [diff] [blame] | 158 | @major: major number of API version |
| 159 | @minor: minor number of API version |
Nguyen Anh Quynh | 36df4bb | 2013-12-10 13:31:20 +0800 | [diff] [blame] | 160 | |
Nguyen Anh Quynh | 39a42ed | 2013-12-22 10:40:58 +0800 | [diff] [blame] | 161 | @return hexical number encoding both major & minor versions, which is comparisonable. |
| 162 | |
| 163 | For example, second API version would return 1 in @major, and 1 in @minor |
| 164 | The return value would be 0x0101 |
Nguyen Anh Quynh | 0877747 | 2013-12-22 14:16:28 +0800 | [diff] [blame] | 165 | |
| 166 | NOTE: if you only care about returned value, but not major and minor values, |
| 167 | set both arguments to NULL. |
Nguyen Anh Quynh | 36df4bb | 2013-12-10 13:31:20 +0800 | [diff] [blame] | 168 | */ |
Nguyen Anh Quynh | b880678 | 2013-12-22 15:20:07 +0800 | [diff] [blame] | 169 | unsigned int cs_version_ex(int *major, int *minor); |
Nguyen Anh Quynh | 39a42ed | 2013-12-22 10:40:58 +0800 | [diff] [blame] | 170 | |
| 171 | |
| 172 | /* |
| 173 | Check if a particular arch is supported by this library. |
| 174 | |
| 175 | @arch: the architecture to be checked. |
| 176 | To verify if this library supports everything, use CS_ARCH_ALL |
| 177 | |
| 178 | @return True if this library supports the given arch. |
| 179 | */ |
| 180 | bool cs_support(cs_arch arch); |
Nguyen Anh Quynh | 36df4bb | 2013-12-10 13:31:20 +0800 | [diff] [blame] | 181 | |
| 182 | /* |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 183 | Initialize CS handle: this must be done before any usage of CS. |
| 184 | |
| 185 | @arch: architecture type (CS_ARCH_*) |
| 186 | @mode: hardware mode. This is combined of CS_MODE_* |
| 187 | @handle: pointer to handle, which will be updated at return time |
| 188 | |
| 189 | @return CS_ERR_OK on success, or other value on failure (refer to cs_err enum |
| 190 | for detailed error). |
| 191 | */ |
| 192 | cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle); |
| 193 | |
| 194 | /* |
| 195 | Close CS handle: MUST do to release the handle when it is not used anymore. |
| 196 | |
| 197 | @handle: handle returned by cs_open() |
| 198 | |
| 199 | @return CS_ERR_OK on success, or other value on failure (refer to cs_err enum |
| 200 | for detailed error). |
| 201 | */ |
| 202 | cs_err cs_close(csh handle); |
| 203 | |
| 204 | /* |
Nguyen Anh Quynh | 79976c1 | 2013-12-04 23:03:13 +0800 | [diff] [blame] | 205 | Set option for disassembling engine at runtime |
Nguyen Anh Quynh | 01aba00 | 2013-12-03 21:00:09 +0800 | [diff] [blame] | 206 | |
| 207 | @handle: handle returned by cs_open() |
Nguyen Anh Quynh | b8ce68e | 2013-12-03 23:45:08 +0800 | [diff] [blame] | 208 | @type: type of option to be set |
Nguyen Anh Quynh | 79976c1 | 2013-12-04 23:03:13 +0800 | [diff] [blame] | 209 | @value: option value corresponding with @type |
Nguyen Anh Quynh | 01aba00 | 2013-12-03 21:00:09 +0800 | [diff] [blame] | 210 | |
Nguyen Anh Quynh | 79976c1 | 2013-12-04 23:03:13 +0800 | [diff] [blame] | 211 | @return CS_ERR_OK on success, or other value on failure. |
| 212 | Refer to cs_err enum for detailed error. |
Nguyen Anh Quynh | 01aba00 | 2013-12-03 21:00:09 +0800 | [diff] [blame] | 213 | */ |
Nguyen Anh Quynh | da8adad | 2013-12-04 09:44:07 +0800 | [diff] [blame] | 214 | cs_err cs_option(csh handle, cs_opt_type type, size_t value); |
Nguyen Anh Quynh | 01aba00 | 2013-12-03 21:00:09 +0800 | [diff] [blame] | 215 | |
| 216 | /* |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 217 | Report the last error number when some API function fail. |
| 218 | Like glibc's errno, cs_errno might not retain its old value once accessed. |
| 219 | |
| 220 | @handle: handle returned by cs_open() |
| 221 | |
| 222 | @return: error code of cs_err enum type (CS_ERR_*, see above) |
| 223 | */ |
| 224 | cs_err cs_errno(csh handle); |
| 225 | |
| 226 | /* |
| 227 | Disasm the binary code in @buffer. |
| 228 | Disassembled instructions will be put into @insn |
| 229 | NOTE: this API requires the pre-allocated buffer in @insn |
| 230 | |
| 231 | @handle: handle returned by cs_open() |
| 232 | @code: buffer containing raw binary code to be disassembled |
| 233 | @code_size: size of above code |
Nguyen Anh Quynh | 612b5d2 | 2013-12-03 12:23:09 +0800 | [diff] [blame] | 234 | @address: address of the first insn in given raw code buffer |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 235 | @insn: array of insn filled in by this function |
| 236 | NOTE: @insn size must be at least @count to avoid buffer overflow |
| 237 | @count: number of instrutions to be disassembled, or 0 to get all of them |
| 238 | @return: the number of succesfully disassembled instructions, |
| 239 | or 0 if this function failed to disassemble the given code |
Nguyen Anh Quynh | 029df20 | 2013-12-03 11:36:54 +0800 | [diff] [blame] | 240 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame^] | 241 | NOTE: this API does not provide detail information, meaning @detail = NULL |
| 242 | |
Nguyen Anh Quynh | 029df20 | 2013-12-03 11:36:54 +0800 | [diff] [blame] | 243 | On failure, call cs_errno() for error code. |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 244 | */ |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 245 | size_t cs_disasm(csh handle, |
pancake | f0e4eed | 2013-12-11 22:14:42 +0100 | [diff] [blame] | 246 | const uint8_t *code, size_t code_size, |
Nguyen Anh Quynh | 612b5d2 | 2013-12-03 12:23:09 +0800 | [diff] [blame] | 247 | uint64_t address, |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 248 | size_t count, |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 249 | cs_insn *insn); |
| 250 | |
| 251 | /* |
| 252 | Dynamicly allocate memory to contain disasm insn |
| 253 | Disassembled instructions will be put into @*insn |
| 254 | |
| 255 | NOTE 1: this API will automatically determine memory needed to contain |
| 256 | output disassembled instructions in @insn. |
| 257 | NOTE 2: caller must free() the allocated memory itself to avoid memory leaking |
| 258 | |
| 259 | @handle: handle returned by cs_open() |
| 260 | @code: buffer containing raw binary code to be disassembled |
| 261 | @code_size: size of above code |
Nguyen Anh Quynh | 612b5d2 | 2013-12-03 12:23:09 +0800 | [diff] [blame] | 262 | @address: address of the first insn in given raw code buffer |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 263 | @insn: array of insn filled in by this function |
Nguyen Anh Quynh | 79976c1 | 2013-12-04 23:03:13 +0800 | [diff] [blame] | 264 | NOTE: @insn will be allocated by this function, and should be freed |
| 265 | with cs_free() API. |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 266 | @count: number of instrutions to be disassembled, or 0 to get all of them |
| 267 | @return: the number of succesfully disassembled instructions, |
| 268 | or 0 if this function failed to disassemble the given code |
Nguyen Anh Quynh | 029df20 | 2013-12-03 11:36:54 +0800 | [diff] [blame] | 269 | |
| 270 | On failure, call cs_errno() for error code. |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 271 | */ |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 272 | size_t cs_disasm_dyn(csh handle, |
pancake | f0e4eed | 2013-12-11 22:14:42 +0100 | [diff] [blame] | 273 | const uint8_t *code, size_t code_size, |
Nguyen Anh Quynh | 612b5d2 | 2013-12-03 12:23:09 +0800 | [diff] [blame] | 274 | uint64_t address, |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 275 | size_t count, |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 276 | cs_insn **insn); |
| 277 | |
| 278 | /* |
Nguyen Anh Quynh | 79976c1 | 2013-12-04 23:03:13 +0800 | [diff] [blame] | 279 | Free memory allocated in @insn by cs_disasm_dyn() |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 280 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame^] | 281 | @insn: pointer returned by @insn argument in cs_disasm_dyn() |
| 282 | @count: number of cs_insn structures returned by cs_disasm_dyn() |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 283 | */ |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame^] | 284 | void cs_free(cs_insn *insn, size_t count); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 285 | |
| 286 | /* |
| 287 | Return friendly name of regiser in a string |
| 288 | Find the instruction id from header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...) |
| 289 | |
Nguyen Anh Quynh | 3640f3c | 2013-12-01 00:26:27 +0800 | [diff] [blame] | 290 | @handle: handle returned by cs_open() |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 291 | @reg: register id |
| 292 | @return: string name of the register, or NULL if @reg_id is invalid. |
| 293 | */ |
pancake | f0e4eed | 2013-12-11 22:14:42 +0100 | [diff] [blame] | 294 | const char *cs_reg_name(csh handle, unsigned int reg_id); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 295 | |
| 296 | /* |
| 297 | Return friendly name of an instruction in a string |
| 298 | Find the instruction id from header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...) |
| 299 | |
Nguyen Anh Quynh | 3640f3c | 2013-12-01 00:26:27 +0800 | [diff] [blame] | 300 | @handle: handle returned by cs_open() |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 301 | @insn: instruction id |
| 302 | |
| 303 | @return: string name of the instruction, or NULL if @insn_id is invalid. |
| 304 | */ |
pancake | f0e4eed | 2013-12-11 22:14:42 +0100 | [diff] [blame] | 305 | const char *cs_insn_name(csh handle, unsigned int insn_id); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 306 | |
| 307 | /* |
| 308 | Check if a disassembled instruction belong to a particular group. |
| 309 | Find the group id from header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...) |
| 310 | Internally, this simply verifies if @group_id matches any member of insn->groups array. |
| 311 | |
| 312 | @handle: handle returned by cs_open() |
| 313 | @insn: disassembled instruction structure received from cs_disasm() or cs_disasm_dyn() |
| 314 | @group_id: group that you want to check if this instruction belong to. |
| 315 | |
| 316 | @return: true if this instruction indeed belongs to aboved group, or false otherwise. |
| 317 | */ |
| 318 | bool cs_insn_group(csh handle, cs_insn *insn, unsigned int group_id); |
| 319 | |
| 320 | /* |
| 321 | Check if a disassembled instruction IMPLICITLY used a particular register. |
| 322 | Find the register id from header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...) |
| 323 | Internally, this simply verifies if @reg_id matches any member of insn->regs_read array. |
| 324 | |
| 325 | @insn: disassembled instruction structure received from cs_disasm() or cs_disasm_dyn() |
| 326 | @reg_id: register that you want to check if this instruction used it. |
| 327 | |
| 328 | @return: true if this instruction indeed implicitly used aboved register, or false otherwise. |
| 329 | */ |
| 330 | bool cs_reg_read(csh handle, cs_insn *insn, unsigned int reg_id); |
| 331 | |
| 332 | /* |
| 333 | Check if a disassembled instruction IMPLICITLY modified a particular register. |
| 334 | Find the register id from header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...) |
| 335 | Internally, this simply verifies if @reg_id matches any member of insn->regs_write array. |
| 336 | |
| 337 | @insn: disassembled instruction structure received from cs_disasm() or cs_disasm_dyn() |
| 338 | @reg_id: register that you want to check if this instruction modified it. |
| 339 | |
| 340 | @return: true if this instruction indeed implicitly modified aboved register, or false otherwise. |
| 341 | */ |
| 342 | bool cs_reg_write(csh handle, cs_insn *insn, unsigned int reg_id); |
| 343 | |
| 344 | /* |
| 345 | Count the number of operands of a given type. |
| 346 | Find the operand type in header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...) |
| 347 | |
Nguyen Anh Quynh | 3640f3c | 2013-12-01 00:26:27 +0800 | [diff] [blame] | 348 | @handle: handle returned by cs_open() |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 349 | @insn: disassembled instruction structure received from cs_disasm() or cs_disasm_dyn() |
| 350 | @op_type: Operand type to be found. |
| 351 | |
| 352 | @return: number of operands of given type @op_type in instruction @insn, |
| 353 | or -1 on failure. |
| 354 | */ |
| 355 | int cs_op_count(csh handle, cs_insn *insn, unsigned int op_type); |
| 356 | |
| 357 | /* |
| 358 | Retrieve the position of operand of given type in arch.op_info[] array. |
| 359 | Later, the operand can be accessed using the returned position. |
| 360 | Find the operand type in header file of corresponding architecture (arm.h for ARM, x86.h for X86, ...) |
| 361 | |
Nguyen Anh Quynh | 3640f3c | 2013-12-01 00:26:27 +0800 | [diff] [blame] | 362 | @handle: handle returned by cs_open() |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 363 | @insn: disassembled instruction structure received from cs_disasm() or cs_disasm_dyn() |
| 364 | @op_type: Operand type to be found. |
| 365 | @position: position of the operand to be found. This must be in the range |
| 366 | [1, cs_op_count(handle, insn, op_type)] |
| 367 | |
| 368 | @return: index of operand of given type @op_type in arch.op_info[] array |
| 369 | in instruction @insn, or -1 on failure. |
| 370 | */ |
| 371 | int cs_op_index(csh handle, cs_insn *insn, unsigned int op_type, |
| 372 | unsigned int position); |
| 373 | |
| 374 | #ifdef __cplusplus |
| 375 | } |
| 376 | #endif |
| 377 | |
| 378 | #endif |