| 2161 | } |
| 2162 | |
| 2163 | int disasm_capstone(uint8_t *data, uint32_t addr, string& result, string& err) |
| 2164 | { |
| 2165 | int rc = -1; |
| 2166 | static bool init = false; |
| 2167 | |
| 2168 | /* capstone vars */ |
| 2169 | static csh handle; |
| 2170 | cs_insn *insn = NULL; |
| 2171 | size_t count = 0; |
| 2172 | |
| 2173 | if (!init) { |
| 2174 | /* initialize capstone handle */ |
| 2175 | cs_mode mode = (cs_mode)(CS_MODE_LITTLE_ENDIAN); |
| 2176 | |
| 2177 | if(cs_open(CS_ARCH_PPC, mode, &handle) != CS_ERR_OK) { |
| 2178 | MYLOG("ERROR: cs_open()\n"); |
| 2179 | goto cleanup; |
| 2180 | } |
| 2181 | init = true; |
| 2182 | } |
| 2183 | |
| 2184 | count = cs_disasm(handle, data, |
| 2185 | /* code_size */ 4, |
| 2186 | /* address */ addr, |
| 2187 | /* instr count */ 1, |
| 2188 | /* result */ &insn |
| 2189 | ); |
| 2190 | |
| 2191 | if(count != 1) { |
| 2192 | cs_err e = cs_errno(handle); |
| 2193 | |
| 2194 | if(e == CS_ERR_OK) { |
| 2195 | result = "undefined"; |
| 2196 | } |
| 2197 | else { |
| 2198 | char msg[128]; |
| 2199 | snprintf( |
| 2200 | msg, sizeof(msg), "ERROR: cs_disasm() returned %zu cs_err=%d (%s)\n", count, e, cs_err_to_string(e)); |
| 2201 | err = msg; |
| 2202 | goto cleanup; |
| 2203 | } |
| 2204 | } |
| 2205 | else { |
| 2206 | result = insn->mnemonic; |
| 2207 | result += " "; |
| 2208 | result += insn->op_str; |
| 2209 | } |
| 2210 | |
| 2211 | rc = 0; |
| 2212 | cleanup: |
| 2213 | if(insn) cs_free(insn, count); |
| 2214 | return rc; |
| 2215 | } |
| 2216 | |
| 2217 | /*****************************************************************************/ |
| 2218 | /* instruction tokenizing */ |