/* * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com> * * Parts came from builtin-annotate.c, see those files for further * copyright notes. * * Released under the GPL v2. (and only v2, not any later version) */ #include <errno.h>#include <inttypes.h>#include "util.h"#include "ui/ui.h"#include "sort.h"#include "build-id.h"#include "color.h"#include "cache.h"#include "symbol.h"#include "debug.h"#include "annotate.h"#include "evsel.h"#include "block-range.h"#include "string2.h"#include "arch/common.h"#include <regex.h>#include <pthread.h>#include <linux/bitops.h>#include <linux/kernel.h>#include <sys/utsname.h> #include "sane_ctype.h" const char *disassembler_style;const char *objdump_path;static regex_t file_lineno; static struct ins_ops *ins__find(struct arch *arch, const char *name);static void ins__sort(struct arch *arch);static int disasm_line__parse(char *line, const char **namep, char **rawp); struct arch { const char *name; struct ins *instructions; size_t nr_instructions; size_t nr_instructions_allocated; struct ins_ops *(*associate_instruction_ops)(struct arch *arch, const char *name); bool sorted_instructions; bool initialized; void *priv; int (*init)(struct arch *arch); struct { char comment_char; char skip_functions_char; } objdump;}; static struct ins_ops call_ops;static struct ins_ops dec_ops;static struct ins_ops jump_ops;static struct ins_ops mov_ops;static struct ins_ops nop_ops;static struct ins_ops lock_ops;static struct ins_ops ret_ops; static int arch__grow_instructions(struct arch *arch){ struct ins *new_instructions; size_t new_nr_allocated; if (arch->nr_instructions_allocated == 0 && arch->instructions) goto grow_from_non_allocated_table; new_nr_allocated = arch->nr_instructions_allocated + 128; new_instructions = realloc(arch->instructions, new_nr_allocated * sizeof(struct ins)); if (new_instructions == NULL) return -1; out_update_instructions: arch->instructions = new_instructions; arch->nr_instructions_allocated = new_nr_allocated; return 0; grow_from_non_allocated_table: new_nr_allocated = arch->nr_instructions + 128; new_instructions = calloc(new_nr_allocated, sizeof(struct ins)); if (new_instructions == NULL) return -1; memcpy(new_instructions, arch->instructions, arch->nr_instructions); goto out_update_instructions;} static int arch__associate_ins_ops(struct arch* arch, const char *name, struct ins_ops *ops){ struct ins *ins; if (arch->nr_instructions == arch->nr_instructions_allocated && arch__grow_instructions(arch)) return -1; ins = &arch->instructions[arch->nr_instructions]; ins->name = strdup(name); if (!ins->name) return -1; ins->ops = ops; arch->nr_instructions++; ins__sort(arch); return 0;} #include "arch/arm/annotate/instructions.c"#include "arch/arm64/annotate/instructions.c"#include "arch/x86/annotate/instructions.c"#include "arch/powerpc/annotate/instructions.c"#include "arch/s390/annotate/instructions.c" static struct arch architectures[] = { { .name = "arm", .init = arm__annotate_init, }, { .name = "arm64", .init = arm64__annotate_init, }, { .name = "x86", .instructions = x86__instructions, .nr_instructions = ARRAY_SIZE(x86__instructions), .objdump = { .comment_char = '#', }, }, { .name = "powerpc", .init = powerpc__annotate_init, }, { .name = "s390", .init = s390__annotate_init, .objdump = { .comment_char = '#', }, },}; static void ins__delete(struct ins_operands *ops){ if (ops == NULL) return; zfree(&ops->source.raw); zfree(&ops->source.name); zfree(&ops->target.raw); zfree(&ops->target.name);} static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size, struct ins_operands *ops){ return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->raw);} int ins__scnprintf(struct ins *ins, char *bf, size_t size, struct ins_operands *ops){ if (ins->ops->scnprintf) return ins->ops->scnprintf(ins, bf, size, ops); return ins__raw_scnprintf(ins, bf, size, ops);} static int call__parse(struct arch *arch, struct ins_operands *ops, struct map *map){ char *endptr, *tok, *name; ops->target.addr = strtoull(ops->raw, &endptr, 16); name = strchr(endptr, '<'); if (name == NULL) goto indirect_call; name++; if (arch->objdump.skip_functions_char && strchr(name, arch->objdump.skip_functions_char)) return -1; tok = strchr(name, '>'); if (tok == NULL) return -1; *tok = '\0'; ops->target.name = strdup(name); *tok = '>'; return ops->target.name == NULL ? -1 : 0; indirect_call: tok = strchr(endptr, '*'); if (tok == NULL) { struct symbol *sym = map__find_symbol(map, map->map_ip(map, ops->target.addr)); if (sym != NULL) ops->target.name = strdup(sym->name); else ops->target.addr = 0; return 0; } ops->target.addr = strtoull(tok + 1, NULL, 16); return 0;} static int call__scnprintf(struct ins *ins, char *bf, size_t size, struct ins_operands *ops){ if (ops->target.name) return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->target.name); if (ops->target.addr == 0) return ins__raw_scnprintf(ins, bf, size, ops); return scnprintf(bf, size, "%-6.6s *%" PRIx64, ins->name, ops->target.addr);} static struct ins_ops call_ops = { .parse = call__parse, .scnprintf = call__scnprintf,}; bool ins__is_call(const struct ins *ins){ return ins->ops == &call_ops;} static int jump__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map *map __maybe_unused){ const char *s = strchr(ops->raw, '+'); const char *c = strchr(ops->raw, ','); /* * skip over possible up to 2 operands to get to address, e.g.: * tbnz w0, #26, ffff0000083cd190 <security_file_permission+0xd0> */ if (c++ != NULL) { ops->target.addr = strtoull(c, NULL, 16); if (!ops->target.addr) { c = strchr(c, ','); if (c++ != NULL) ops->target.addr = strtoull(c, NULL, 16); } } else { ops->target.addr = strtoull(ops->raw, NULL, 16); } if (s++ != NULL) { ops->target.offset = strtoull(s, NULL, 16); ops->target.offset_avail = true; } else { ops->target.offset_avail = false; } return 0;} static int jump__scnprintf(struct ins *ins, char *bf, size_t size, struct ins_operands *ops){ const char *c = strchr(ops->raw, ','); if (!ops->target.addr || ops->target.offset < 0) return ins__raw_scnprintf(ins, bf, size, ops); if (c != NULL) { const char *c2 = strchr(c + 1, ','); /* check for 3-op insn */ if (c2 != NULL) c = c2; c++; /* mirror arch objdump's space-after-comma style */ if (*c == ' ') c++; } return scnprintf(bf, size, "%-6.6s %.*s%" PRIx64, ins->name, c ? c - ops->raw : 0, ops->raw, ops->target.offset);} static struct ins_ops jump_ops = { .parse = jump__parse, .scnprintf = jump__scnprintf,}; bool ins__is_jump(const struct ins *ins){ return ins->ops == &jump_ops;} static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep){ char *endptr, *name, *t; if (strstr(raw, "(%rip)") == NULL) return 0; *addrp = strtoull(comment, &endptr, 16); name = strchr(endptr, '<'); if (name == NULL) return -1; name++; t = strchr(name, '>'); if (t == NULL) return 0; *t = '\0'; *namep = strdup(name); *t = '>'; return 0;} static int lock__parse(struct arch *arch, struct ins_operands *ops, struct map *map){ ops->locked.ops = zalloc(sizeof(*ops->locked.ops)); if (ops->locked.ops == NULL) return 0; if (disasm_line__parse(ops->raw, &ops->locked.ins.name, &ops->locked.ops->raw) < 0) goto out_free_ops; ops->locked.ins.ops = ins__find(arch, ops->locked.ins.name); if (ops->locked.ins.ops == NULL) goto out_free_ops; if (ops->locked.ins.ops->parse && ops->locked.ins.ops->parse(arch, ops->locked.ops, map) < 0) goto out_free_ops; return 0; out_free_ops: zfree(&ops->locked.ops); return 0;} static int lock__scnprintf(struct ins *ins, char *bf, size_t size, struct ins_operands *ops){ int printed; if (ops->locked.ins.ops == NULL) return ins__raw_scnprintf(ins, bf, size, ops); printed = scnprintf(bf, size, "%-6.6s ", ins->name); return printed + ins__scnprintf(&ops->locked.ins, bf + printed, size - printed, ops->locked.ops);} static void lock__delete(struct ins_operands *ops){ struct ins *ins = &ops->locked.ins; if (ins->ops && ins->ops->free) ins->ops->free(ops->locked.ops); else ins__delete(ops->locked.ops); zfree(&ops->locked.ops); zfree(&ops->target.raw); zfree(&ops->target.name);} static struct ins_ops lock_ops = { .free = lock__delete, .parse = lock__parse, .scnprintf = lock__scnprintf,}; static int mov__parse(struct arch *arch, struct ins_operands *ops, struct map *map __maybe_unused){ char *s = strchr(ops->raw, ','), *target, *comment, prev; if (s == NULL) return -1; *s = '\0'; ops->source.raw = strdup(ops->raw); *s = ','; if (ops->source.raw == NULL) return -1; target = ++s; comment = strchr(s, arch->objdump.comment_char); if (comment != NULL) s = comment - 1; else s = strchr(s, '\0') - 1; while (s > target && isspace(s[0])) --s; s++; prev = *s; *s = '\0'; ops->target.raw = strdup(target); *s = prev; if (ops->target.raw == NULL) goto out_free_source; if (comment == NULL) return 0; comment = ltrim(comment); comment__symbol(ops->source.raw, comment, &ops->source.addr, &ops->source.name); comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name); return 0; out_free_source: zfree(&ops->source.raw); return -1;} static int mov__scnprintf(struct ins *ins, char *bf, size_t size, struct ins_operands *ops){ return scnprintf(bf, size, "%-6.6s %s,%s", ins->name, ops->source.name ?: ops->source.raw, ops->target.name ?: ops->target.raw);} static struct ins_ops mov_ops = { .parse = mov__parse, .scnprintf = mov__scnprintf,}; static int dec__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map *map __maybe_unused){ char *target, *comment, *s, prev; target = s = ops->raw; while (s[0] != '\0' && !isspace(s[0])) ++s; prev = *s; *s = '\0'; ops->target.raw = strdup(target); *s = prev; if (ops->target.raw == NULL) return -1; comment = strchr(s, arch->objdump.comment_char); if (comment == NULL) return 0; comment = ltrim(comment); comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name); return 0;} static int dec__scnprintf(struct ins *ins, char *bf, size_t size, struct ins_operands *ops){ return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->target.name ?: ops->target.raw);} static struct ins_ops dec_ops = { .parse = dec__parse, .scnprintf = dec__scnprintf,}; static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size, struct ins_operands *ops __maybe_unused){ return scnprintf(bf, size, "%-6.6s", "nop");} static struct ins_ops nop_ops = { .scnprintf = nop__scnprintf,}; static struct ins_ops ret_ops = { .scnprintf = ins__raw_scnprintf,}; bool ins__is_ret(const struct ins *ins)Lines 1–500 of 1934. Load more
/* * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com> * * Parts came from builtin-annotate.c, see those files for further * copyright notes. * * Released under the GPL v2. (and only v2, not any later version) */ #include <errno.h> #include <inttypes.h> #include "util.h" #include "ui/ui.h" #include "sort.h" #include "build-id.h" #include "color.h" #include "cache.h" #include "symbol.h" #include "debug.h" #include "annotate.h" #include "evsel.h" #include "block-range.h" #include "string2.h" #include "arch/common.h" #include <regex.h> #include <pthread.h> #include <linux/bitops.h> #include <linux/kernel.h> #include <sys/utsname.h> #include "sane_ctype.h" const char *disassembler_style; const char *objdump_path; static regex_t file_lineno; static struct ins_ops *ins__find(struct arch *arch, const char *name); static void ins__sort(struct arch *arch); static int disasm_line__parse(char *line, const char **namep, char **rawp); struct arch { const char *name; struct ins *instructions; size_t nr_instructions; size_t nr_instructions_allocated; struct ins_ops *(*associate_instruction_ops)(struct arch *arch, const char *name); bool sorted_instructions; bool initialized; void *priv; int (*init)(struct arch *arch); struct { char comment_char; char skip_functions_char; } objdump; }; static struct ins_ops call_ops; static struct ins_ops dec_ops; static struct ins_ops jump_ops; static struct ins_ops mov_ops; static struct ins_ops nop_ops; static struct ins_ops lock_ops; static struct ins_ops ret_ops; static int arch__grow_instructions(struct arch *arch) { struct ins *new_instructions; size_t new_nr_allocated; if (arch->nr_instructions_allocated == 0 && arch->instructions) goto grow_from_non_allocated_table; new_nr_allocated = arch->nr_instructions_allocated + 128; new_instructions = realloc(arch->instructions, new_nr_allocated * sizeof(struct ins)); if (new_instructions == NULL) return -1; out_update_instructions: arch->instructions = new_instructions; arch->nr_instructions_allocated = new_nr_allocated; return 0; grow_from_non_allocated_table: new_nr_allocated = arch->nr_instructions + 128; new_instructions = calloc(new_nr_allocated, sizeof(struct ins)); if (new_instructions == NULL) return -1; memcpy(new_instructions, arch->instructions, arch->nr_instructions); goto out_update_instructions; } static int arch__associate_ins_ops(struct arch* arch, const char *name, struct ins_ops *ops) { struct ins *ins; if (arch->nr_instructions == arch->nr_instructions_allocated && arch__grow_instructions(arch)) return -1; ins = &arch->instructions[arch->nr_instructions]; ins->name = strdup(name); if (!ins->name) return -1; ins->ops = ops; arch->nr_instructions++; ins__sort(arch); return 0; } #include "arch/arm/annotate/instructions.c" #include "arch/arm64/annotate/instructions.c" #include "arch/x86/annotate/instructions.c" #include "arch/powerpc/annotate/instructions.c" #include "arch/s390/annotate/instructions.c" static struct arch architectures[] = { { .name = "arm", .init = arm__annotate_init, }, { .name = "arm64", .init = arm64__annotate_init, }, { .name = "x86", .instructions = x86__instructions, .nr_instructions = ARRAY_SIZE(x86__instructions), .objdump = { .comment_char = '#', }, }, { .name = "powerpc", .init = powerpc__annotate_init, }, { .name = "s390", .init = s390__annotate_init, .objdump = { .comment_char = '#', }, }, }; static void ins__delete(struct ins_operands *ops) { if (ops == NULL) return; zfree(&ops->source.raw); zfree(&ops->source.name); zfree(&ops->target.raw); zfree(&ops->target.name); } static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size, struct ins_operands *ops) { return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->raw); } int ins__scnprintf(struct ins *ins, char *bf, size_t size, struct ins_operands *ops) { if (ins->ops->scnprintf) return ins->ops->scnprintf(ins, bf, size, ops); return ins__raw_scnprintf(ins, bf, size, ops); } static int call__parse(struct arch *arch, struct ins_operands *ops, struct map *map) { char *endptr, *tok, *name; ops->target.addr = strtoull(ops->raw, &endptr, 16); name = strchr(endptr, '<'); if (name == NULL) goto indirect_call; name++; if (arch->objdump.skip_functions_char && strchr(name, arch->objdump.skip_functions_char)) return -1; tok = strchr(name, '>'); if (tok == NULL) return -1; *tok = '\0'; ops->target.name = strdup(name); *tok = '>'; return ops->target.name == NULL ? -1 : 0; indirect_call: tok = strchr(endptr, '*'); if (tok == NULL) { struct symbol *sym = map__find_symbol(map, map->map_ip(map, ops->target.addr)); if (sym != NULL) ops->target.name = strdup(sym->name); else ops->target.addr = 0; return 0; } ops->target.addr = strtoull(tok + 1, NULL, 16); return 0; } static int call__scnprintf(struct ins *ins, char *bf, size_t size, struct ins_operands *ops) { if (ops->target.name) return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->target.name); if (ops->target.addr == 0) return ins__raw_scnprintf(ins, bf, size, ops); return scnprintf(bf, size, "%-6.6s *%" PRIx64, ins->name, ops->target.addr); } static struct ins_ops call_ops = { .parse = call__parse, .scnprintf = call__scnprintf, }; bool ins__is_call(const struct ins *ins) { return ins->ops == &call_ops; } static int jump__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map *map __maybe_unused) { const char *s = strchr(ops->raw, '+'); const char *c = strchr(ops->raw, ','); /* * skip over possible up to 2 operands to get to address, e.g.: * tbnz w0, #26, ffff0000083cd190 <security_file_permission+0xd0> */ if (c++ != NULL) { ops->target.addr = strtoull(c, NULL, 16); if (!ops->target.addr) { c = strchr(c, ','); if (c++ != NULL) ops->target.addr = strtoull(c, NULL, 16); } } else { ops->target.addr = strtoull(ops->raw, NULL, 16); } if (s++ != NULL) { ops->target.offset = strtoull(s, NULL, 16); ops->target.offset_avail = true; } else { ops->target.offset_avail = false; } return 0; } static int jump__scnprintf(struct ins *ins, char *bf, size_t size, struct ins_operands *ops) { const char *c = strchr(ops->raw, ','); if (!ops->target.addr || ops->target.offset < 0) return ins__raw_scnprintf(ins, bf, size, ops); if (c != NULL) { const char *c2 = strchr(c + 1, ','); /* check for 3-op insn */ if (c2 != NULL) c = c2; c++; /* mirror arch objdump's space-after-comma style */ if (*c == ' ') c++; } return scnprintf(bf, size, "%-6.6s %.*s%" PRIx64, ins->name, c ? c - ops->raw : 0, ops->raw, ops->target.offset); } static struct ins_ops jump_ops = { .parse = jump__parse, .scnprintf = jump__scnprintf, }; bool ins__is_jump(const struct ins *ins) { return ins->ops == &jump_ops; } static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep) { char *endptr, *name, *t; if (strstr(raw, "(%rip)") == NULL) return 0; *addrp = strtoull(comment, &endptr, 16); name = strchr(endptr, '<'); if (name == NULL) return -1; name++; t = strchr(name, '>'); if (t == NULL) return 0; *t = '\0'; *namep = strdup(name); *t = '>'; return 0; } static int lock__parse(struct arch *arch, struct ins_operands *ops, struct map *map) { ops->locked.ops = zalloc(sizeof(*ops->locked.ops)); if (ops->locked.ops == NULL) return 0; if (disasm_line__parse(ops->raw, &ops->locked.ins.name, &ops->locked.ops->raw) < 0) goto out_free_ops; ops->locked.ins.ops = ins__find(arch, ops->locked.ins.name); if (ops->locked.ins.ops == NULL) goto out_free_ops; if (ops->locked.ins.ops->parse && ops->locked.ins.ops->parse(arch, ops->locked.ops, map) < 0) goto out_free_ops; return 0; out_free_ops: zfree(&ops->locked.ops); return 0; } static int lock__scnprintf(struct ins *ins, char *bf, size_t size, struct ins_operands *ops) { int printed; if (ops->locked.ins.ops == NULL) return ins__raw_scnprintf(ins, bf, size, ops); printed = scnprintf(bf, size, "%-6.6s ", ins->name); return printed + ins__scnprintf(&ops->locked.ins, bf + printed, size - printed, ops->locked.ops); } static void lock__delete(struct ins_operands *ops) { struct ins *ins = &ops->locked.ins; if (ins->ops && ins->ops->free) ins->ops->free(ops->locked.ops); else ins__delete(ops->locked.ops); zfree(&ops->locked.ops); zfree(&ops->target.raw); zfree(&ops->target.name); } static struct ins_ops lock_ops = { .free = lock__delete, .parse = lock__parse, .scnprintf = lock__scnprintf, }; static int mov__parse(struct arch *arch, struct ins_operands *ops, struct map *map __maybe_unused) { char *s = strchr(ops->raw, ','), *target, *comment, prev; if (s == NULL) return -1; *s = '\0'; ops->source.raw = strdup(ops->raw); *s = ','; if (ops->source.raw == NULL) return -1; target = ++s; comment = strchr(s, arch->objdump.comment_char); if (comment != NULL) s = comment - 1; else s = strchr(s, '\0') - 1; while (s > target && isspace(s[0])) --s; s++; prev = *s; *s = '\0'; ops->target.raw = strdup(target); *s = prev; if (ops->target.raw == NULL) goto out_free_source; if (comment == NULL) return 0; comment = ltrim(comment); comment__symbol(ops->source.raw, comment, &ops->source.addr, &ops->source.name); comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name); return 0; out_free_source: zfree(&ops->source.raw); return -1; } static int mov__scnprintf(struct ins *ins, char *bf, size_t size, struct ins_operands *ops) { return scnprintf(bf, size, "%-6.6s %s,%s", ins->name, ops->source.name ?: ops->source.raw, ops->target.name ?: ops->target.raw); } static struct ins_ops mov_ops = { .parse = mov__parse, .scnprintf = mov__scnprintf, }; static int dec__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map *map __maybe_unused) { char *target, *comment, *s, prev; target = s = ops->raw; while (s[0] != '\0' && !isspace(s[0])) ++s; prev = *s; *s = '\0'; ops->target.raw = strdup(target); *s = prev; if (ops->target.raw == NULL) return -1; comment = strchr(s, arch->objdump.comment_char); if (comment == NULL) return 0; comment = ltrim(comment); comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name); return 0; } static int dec__scnprintf(struct ins *ins, char *bf, size_t size, struct ins_operands *ops) { return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->target.name ?: ops->target.raw); } static struct ins_ops dec_ops = { .parse = dec__parse, .scnprintf = dec__scnprintf, }; static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size, struct ins_operands *ops __maybe_unused) { return scnprintf(bf, size, "%-6.6s", "nop"); } static struct ins_ops nop_ops = { .scnprintf = nop__scnprintf, }; static struct ins_ops ret_ops = { .scnprintf = ins__raw_scnprintf, }; bool ins__is_ret(const struct ins *ins)