tools/perf/util/capstone.c
Source file repositories/reference/linux-study-clean/tools/perf/util/capstone.c
File Facts
- System
- Linux kernel
- Corpus path
tools/perf/util/capstone.c- Extension
.c- Size
- 13744 bytes
- Lines
- 572
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
capstone.hannotate.haddr_location.hdebug.hdisasm.hdso.hmachine.hmap.hnamespaces.hprint_insn.hsymbol.hthread.hdlfcn.herrno.hfcntl.hinttypes.hstring.hcapstone/capstone.h
Detected Declarations
struct find_file_offset_datafunction perf_cs_openfunction perf_cs_optionfunction perf_cs_disasmfunction perf_cs_freefunction perf_cs_closefunction capstone_initfunction print_insn_x86function capstone__fprintf_insn_asmfunction print_capstone_detailfunction find_file_offsetfunction symbol__disassemble_capstonefunction list_for_each_entry_safefunction symbol__disassemble_capstone_powerpc
Annotated Snippet
struct find_file_offset_data {
u64 ip;
u64 offset;
};
/* This will be called for each PHDR in an ELF binary */
static int find_file_offset(u64 start, u64 len, u64 pgoff, void *arg)
{
struct find_file_offset_data *data = arg;
if (start <= data->ip && data->ip < start + len) {
data->offset = pgoff + data->ip - start;
return 1;
}
return 0;
}
int symbol__disassemble_capstone(const char *filename __maybe_unused,
struct symbol *sym __maybe_unused,
struct annotate_args *args __maybe_unused)
{
struct annotation *notes = symbol__annotation(sym);
struct map *map = args->ms->map;
struct dso *dso = map__dso(map);
u64 start = map__rip_2objdump(map, sym->start);
u64 offset;
int i, count, free_count;
bool is_64bit = false;
bool needs_cs_close = false;
/* Malloc-ed buffer containing instructions read from disk. */
u8 *code_buf = NULL;
/* Pointer to code to be disassembled. */
const u8 *buf;
u64 buf_len;
csh handle;
struct cs_insn *insn = NULL;
char disasm_buf[512];
struct disasm_line *dl;
bool disassembler_style = false;
if (args->options->objdump_path)
return -1;
buf = dso__read_symbol(dso, filename, map, sym,
&code_buf, &buf_len, &is_64bit);
if (buf == NULL)
return errno;
/* add the function address and name */
scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:",
start, sym->name);
args->offset = -1;
args->line = disasm_buf;
args->line_nr = 0;
args->fileloc = NULL;
args->ms->sym = sym;
dl = disasm_line__new(args);
if (dl == NULL)
goto err;
annotation_line__add(&dl->al, ¬es->src->source);
if (!args->options->disassembler_style ||
!strcmp(args->options->disassembler_style, "att"))
disassembler_style = true;
if (capstone_init(maps__machine(thread__maps(args->ms->thread)), &handle, is_64bit,
disassembler_style) < 0)
goto err;
needs_cs_close = true;
free_count = count = perf_cs_disasm(handle, buf, buf_len, start, buf_len, &insn);
for (i = 0, offset = 0; i < count; i++) {
int printed;
printed = scnprintf(disasm_buf, sizeof(disasm_buf),
" %-7s %s",
insn[i].mnemonic, insn[i].op_str);
print_capstone_detail(&insn[i], disasm_buf + printed,
sizeof(disasm_buf) - printed, args,
start + offset);
args->offset = offset;
args->line = disasm_buf;
dl = disasm_line__new(args);
if (dl == NULL)
Annotation
- Immediate include surface: `capstone.h`, `annotate.h`, `addr_location.h`, `debug.h`, `disasm.h`, `dso.h`, `machine.h`, `map.h`.
- Detected declarations: `struct find_file_offset_data`, `function perf_cs_open`, `function perf_cs_option`, `function perf_cs_disasm`, `function perf_cs_free`, `function perf_cs_close`, `function capstone_init`, `function print_insn_x86`, `function capstone__fprintf_insn_asm`, `function print_capstone_detail`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.