1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Instruction binary disassembler based on capstone. 4 * 5 * Author(s): Changbin Du <changbin.du@huawei.com> 6 */ 7 #include <inttypes.h> 8 #include <string.h> 9 #include <stdbool.h> 10 #include "capstone.h" 11 #include "debug.h" 12 #include "sample.h" 13 #include "symbol.h" 14 #include "machine.h" 15 #include "thread.h" 16 #include "print_insn.h" 17 #include "dump-insn.h" 18 #include "map.h" 19 #include "dso.h" 20 21 size_t sample__fprintf_insn_raw(struct perf_sample *sample, FILE *fp) 22 { 23 int printed = 0; 24 25 for (int i = 0; i < sample->insn_len; i++) { 26 printed += fprintf(fp, "%02x", (unsigned char)sample->insn[i]); 27 if (sample->insn_len - i > 1) 28 printed += fprintf(fp, " "); 29 } 30 return printed; 31 } 32 33 static bool is64bitip(struct machine *machine, struct addr_location *al) 34 { 35 const struct dso *dso = al->map ? map__dso(al->map) : NULL; 36 37 if (dso) 38 return dso__is_64_bit(dso); 39 40 return machine__is(machine, "x86_64") || 41 machine__normalized_is(machine, "arm64") || 42 machine__normalized_is(machine, "s390"); 43 } 44 45 ssize_t fprintf_insn_asm(struct machine *machine, struct thread *thread, u8 cpumode, 46 bool is64bit, const uint8_t *code, size_t code_size, 47 uint64_t ip, int *lenp, int print_opts, FILE *fp) 48 { 49 return capstone__fprintf_insn_asm(machine, thread, cpumode, is64bit, code, code_size, 50 ip, lenp, print_opts, fp); 51 } 52 53 size_t sample__fprintf_insn_asm(struct perf_sample *sample, struct thread *thread, 54 struct machine *machine, FILE *fp, 55 struct addr_location *al) 56 { 57 bool is64bit = is64bitip(machine, al); 58 ssize_t printed; 59 60 printed = fprintf_insn_asm(machine, thread, sample->cpumode, is64bit, 61 (uint8_t *)sample->insn, sample->insn_len, 62 sample->ip, NULL, 0, fp); 63 if (printed < 0) 64 return sample__fprintf_insn_raw(sample, fp); 65 66 return printed; 67 } 68