From nobody Wed Dec 17 21:14:03 2025 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BFA472BF005 for ; Sun, 5 Oct 2025 21:22:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759699349; cv=none; b=cl14KtESyUvA5t3hCX6egvKfihG9W5IzvnGut2yzg716yVVGZFG7+iPR25jEnhUUndJ+UxRjQkoN/vfW9SRe9n5cGEbmNk9jwE0UHUKyNOR1XY11gqW64yRMnwWxQ956Z5LpgzsUKWCD8lQZgj6sqQP1wcIf3CsQahgLcjGLlCo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759699349; c=relaxed/simple; bh=1AcAVTJaNRek7hoOhiMLQ+rsKXs+qNDcn0xrSWd0d40=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=TNAz/sTstjBeGBQ8PNj97ek4E9PVoTYwmWXImpYmfY74M0TV6EdsABhoHhFmRoY6UyHuVJ+8LSmDKhJ+wOzdDq0XQkLSjbxR+gRUam3TLyAU6/xIKzuf917e491yj3+/TC8pW9i2Vt8ZnG743ewaAFiZR/Qf5Glkl9bbodUI7Pg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=qUQ4nYFO; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="qUQ4nYFO" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-27ee41e062cso47670865ad.1 for ; Sun, 05 Oct 2025 14:22:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1759699347; x=1760304147; darn=vger.kernel.org; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=O+l/WGm8eAK1INkc4qzoRNUwkFMn5H1vWu1UxQ6sXUY=; b=qUQ4nYFOHzyK1yvED+gc3RiW+rftl9MjMRFHs3zNlizjPkxIT+f517HR1dQGpSfYr+ jVAVMRp3Z+9GLS8AwZCCLYCvDRw3uxhDW8dSzzsu2EPefby7Mj/qBrGjlDYQA1Fbfrt8 +H3GlpE/DAM4/j7W0w/P6+GnBGgNw2AB0GO1EdtgmxU/4j+25VahQvNaCuhnZbD/2AYl 319ysLY2eeMXsJaB/ZFGKsKOuKT5fEAwoGTNTHCwEuk6EvRDgMr7o8OUxSJ2L80trHzI hiDyfLQ+cWz0TphYN7Ym47/Pbb6lJMdKnaR/CVQQik49okWgXrrp6EQJTrZivAodGgOn Nr2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1759699347; x=1760304147; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=O+l/WGm8eAK1INkc4qzoRNUwkFMn5H1vWu1UxQ6sXUY=; b=CfDjThpJjJ5FTH317ce1BQwXLO0w2d4J84BDylR7BS8q1w4NHQK5RglCe4sGFcWr1h +BjXsMY8HVAICNTZc5a3XkjBUH+xHvZ/Yk2DqyJa3ohszvqzxXyG6RaAt+OpHdS19zT/ SExhwm1rrBz6sWiIOFdGqZo3rhZuPCAaMaLMxYDQ7iCuHBN/7vs/N407ONORJwBQofrr ThnTQDTHUucsXnvzVLgp5fhWW8ggnxLpuExbV/w8sf+N3emqOjRyus6EHF5wxn6N2ow7 sajf7UMNXsfb2lGok2cb2V+NUtWZmY1RbllVYMAvIEe7F8uIb6mVaPyRHM0alPDxN9iE 7E/A== X-Forwarded-Encrypted: i=1; AJvYcCUl6FTtQRYyqXrhVRq/ngGDeXjRsQV/MdURbY//ACqlkJLIBx0GwqsYRqWr8whPjUHsrJ4F3Uj/g1StsVw=@vger.kernel.org X-Gm-Message-State: AOJu0YwRDFED7C0U0TNjzMYouNFeBTShKFitbuvni8KQaIIQaYg5Tj8J UNELbTNOKC7DBa3WFdbiieENeByS6Kcg9hjmUcKQTfmfM2JyHk2vNs83ESX7UW0tBGpu1ivFqvL /eAwnm/ihFQ== X-Google-Smtp-Source: AGHT+IFu3ifLGHl9t/SXSR8xeghMcMlScLbRJvVQc0sMzH1px7fbKrot0jtLZYXkZgynu4Xl7VItdjoJR39p X-Received: from pldb2.prod.google.com ([2002:a17:902:ed02:b0:269:6a61:3d1a]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:2c7:b0:269:96ee:17d3 with SMTP id d9443c01a7336-28e9a6fd7c4mr106405305ad.51.1759699347119; Sun, 05 Oct 2025 14:22:27 -0700 (PDT) Date: Sun, 5 Oct 2025 14:22:04 -0700 In-Reply-To: <20251005212212.2892175-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20251005212212.2892175-1-irogers@google.com> X-Mailer: git-send-email 2.51.0.618.g983fd99d29-goog Message-ID: <20251005212212.2892175-4-irogers@google.com> Subject: [PATCH v7 03/11] perf dso: Move read_symbol from llvm/capstone to dso From: Ian Rogers To: Arnaldo Carvalho de Melo , Peter Zijlstra , Ingo Molnar , Namhyung Kim , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Charlie Jenkins , Eric Biggers , "Masami Hiramatsu (Google)" , James Clark , Collin Funk , "Dr. David Alan Gilbert" , Li Huafei , Athira Rajeev , Stephen Brennan , Dmitry Vyukov , Alexandre Ghiti , Haibo Xu , Andi Kleen , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org, llvm@lists.linux.dev, Song Liu Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move the read_symbol function to dso.h, make the return type const and add a mutable out_buf out parameter. In future changes this will allow a code pointer to be returned without necessary allocating memory. Signed-off-by: Ian Rogers --- tools/perf/util/capstone.c | 66 +++++----------------------- tools/perf/util/dso.c | 67 +++++++++++++++++++++++++++++ tools/perf/util/dso.h | 4 ++ tools/perf/util/llvm.c | 88 +++++++------------------------------- 4 files changed, 97 insertions(+), 128 deletions(-) diff --git a/tools/perf/util/capstone.c b/tools/perf/util/capstone.c index 01e47d5c8e3e..c23df911e91c 100644 --- a/tools/perf/util/capstone.c +++ b/tools/perf/util/capstone.c @@ -215,55 +215,6 @@ static int find_file_offset(u64 start, u64 len, u64 pg= off, void *arg) } #endif =20 -#ifdef HAVE_LIBCAPSTONE_SUPPORT -static u8 * -read_symbol(const char *filename, struct map *map, struct symbol *sym, - u64 *len, bool *is_64bit) -{ - struct dso *dso =3D map__dso(map); - struct nscookie nsc; - u64 start =3D map__rip_2objdump(map, sym->start); - u64 end =3D map__rip_2objdump(map, sym->end); - int fd, count; - u8 *buf =3D NULL; - struct find_file_offset_data data =3D { - .ip =3D start, - }; - - *is_64bit =3D false; - - nsinfo__mountns_enter(dso__nsinfo(dso), &nsc); - fd =3D open(filename, O_RDONLY); - nsinfo__mountns_exit(&nsc); - if (fd < 0) - return NULL; - - if (file__read_maps(fd, /*exe=3D*/true, find_file_offset, &data, - is_64bit) =3D=3D 0) - goto err; - - *len =3D end - start; - buf =3D malloc(*len); - if (buf =3D=3D NULL) - goto err; - - count =3D pread(fd, buf, *len, data.offset); - close(fd); - fd =3D -1; - - if ((u64)count !=3D *len) - goto err; - - return buf; - -err: - if (fd >=3D 0) - close(fd); - free(buf); - return NULL; -} -#endif - int symbol__disassemble_capstone(const char *filename __maybe_unused, struct symbol *sym __maybe_unused, struct annotate_args *args __maybe_unused) @@ -271,13 +222,17 @@ int symbol__disassemble_capstone(const char *filename= __maybe_unused, #ifdef HAVE_LIBCAPSTONE_SUPPORT struct annotation *notes =3D symbol__annotation(sym); struct map *map =3D args->ms.map; + struct dso *dso =3D map__dso(map); u64 start =3D map__rip_2objdump(map, sym->start); - u64 len; u64 offset; int i, count, free_count; bool is_64bit =3D false; bool needs_cs_close =3D false; - u8 *buf =3D NULL; + /* Malloc-ed buffer containing instructions read from disk. */ + u8 *code_buf =3D NULL; + /* Pointer to code to be disassembled. */ + const u8 *buf; + u64 buf_len; csh handle; cs_insn *insn =3D NULL; char disasm_buf[512]; @@ -287,7 +242,8 @@ int symbol__disassemble_capstone(const char *filename _= _maybe_unused, if (args->options->objdump_path) return -1; =20 - buf =3D read_symbol(filename, map, sym, &len, &is_64bit); + buf =3D dso__read_symbol(dso, filename, map, sym, + &code_buf, &buf_len, &is_64bit); if (buf =3D=3D NULL) return -1; =20 @@ -316,7 +272,7 @@ int symbol__disassemble_capstone(const char *filename _= _maybe_unused, =20 needs_cs_close =3D true; =20 - free_count =3D count =3D cs_disasm(handle, buf, len, start, len, &insn); + free_count =3D count =3D cs_disasm(handle, buf, buf_len, start, buf_len, = &insn); for (i =3D 0, offset =3D 0; i < count; i++) { int printed; =20 @@ -340,7 +296,7 @@ int symbol__disassemble_capstone(const char *filename _= _maybe_unused, } =20 /* It failed in the middle: probably due to unknown instructions */ - if (offset !=3D len) { + if (offset !=3D buf_len) { struct list_head *list =3D ¬es->src->source; =20 /* Discard all lines and fallback to objdump */ @@ -359,7 +315,7 @@ int symbol__disassemble_capstone(const char *filename _= _maybe_unused, if (free_count > 0) cs_free(insn, free_count); } - free(buf); + free(code_buf); return count < 0 ? count : 0; =20 err: diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c index 282e3af85d5a..87d075942de6 100644 --- a/tools/perf/util/dso.c +++ b/tools/perf/util/dso.c @@ -1798,3 +1798,70 @@ bool is_perf_pid_map_name(const char *dso_name) =20 return perf_pid_map_tid(dso_name, &tid); } + +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 =3D arg; + + if (start <=3D data->ip && data->ip < start + len) { + data->offset =3D pgoff + data->ip - start; + return 1; + } + return 0; +} + +const u8 *dso__read_symbol(struct dso *dso, const char *symfs_filename, + const struct map *map, const struct symbol *sym, + u8 **out_buf, u64 *out_buf_len, bool *is_64bit) +{ + struct nscookie nsc; + u64 start =3D map__rip_2objdump(map, sym->start); + u64 end =3D map__rip_2objdump(map, sym->end); + int fd, count; + u8 *buf =3D NULL; + size_t len; + struct find_file_offset_data data =3D { + .ip =3D start, + }; + + *out_buf =3D NULL; + *out_buf_len =3D 0; + *is_64bit =3D false; + + nsinfo__mountns_enter(dso__nsinfo(dso), &nsc); + fd =3D open(symfs_filename, O_RDONLY); + nsinfo__mountns_exit(&nsc); + if (fd < 0) + return NULL; + + if (file__read_maps(fd, /*exe=3D*/true, find_file_offset, &data, is_64bit= ) =3D=3D 0) + goto err; + + len =3D end - start; + buf =3D malloc(len); + if (buf =3D=3D NULL) + goto err; + + count =3D pread(fd, buf, len, data.offset); + close(fd); + fd =3D -1; + + if ((u64)count !=3D len) + goto err; + + *out_buf =3D buf; + *out_buf_len =3D len; + return buf; + +err: + if (fd >=3D 0) + close(fd); + free(buf); + return NULL; +} diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h index fd8e95de77f7..f8ccb9816b89 100644 --- a/tools/perf/util/dso.h +++ b/tools/perf/util/dso.h @@ -924,4 +924,8 @@ static inline struct debuginfo *dso__debuginfo(struct d= so *dso) return debuginfo__new(dso__long_name(dso)); } =20 +const u8 *dso__read_symbol(struct dso *dso, const char *symfs_filename, + const struct map *map, const struct symbol *sym, + u8 **out_buf, u64 *out_buf_len, bool *is_64bit); + #endif /* __PERF_DSO */ diff --git a/tools/perf/util/llvm.c b/tools/perf/util/llvm.c index 2356778955fe..0369f3adcdb6 100644 --- a/tools/perf/util/llvm.c +++ b/tools/perf/util/llvm.c @@ -86,72 +86,6 @@ static void init_llvm(void) } } =20 -#if defined(HAVE_LIBLLVM_SUPPORT) -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 =3D arg; - - if (start <=3D data->ip && data->ip < start + len) { - data->offset =3D pgoff + data->ip - start; - return 1; - } - return 0; -} - -static u8 * -read_symbol(const char *filename, struct map *map, struct symbol *sym, - u64 *len, bool *is_64bit) -{ - struct dso *dso =3D map__dso(map); - struct nscookie nsc; - u64 start =3D map__rip_2objdump(map, sym->start); - u64 end =3D map__rip_2objdump(map, sym->end); - int fd, count; - u8 *buf =3D NULL; - struct find_file_offset_data data =3D { - .ip =3D start, - }; - - *is_64bit =3D false; - - nsinfo__mountns_enter(dso__nsinfo(dso), &nsc); - fd =3D open(filename, O_RDONLY); - nsinfo__mountns_exit(&nsc); - if (fd < 0) - return NULL; - - if (file__read_maps(fd, /*exe=3D*/true, find_file_offset, &data, - is_64bit) =3D=3D 0) - goto err; - - *len =3D end - start; - buf =3D malloc(*len); - if (buf =3D=3D NULL) - goto err; - - count =3D pread(fd, buf, *len, data.offset); - close(fd); - fd =3D -1; - - if ((u64)count !=3D *len) - goto err; - - return buf; - -err: - if (fd >=3D 0) - close(fd); - free(buf); - return NULL; -} -#endif - /* * Whenever LLVM wants to resolve an address into a symbol, it calls this * callback. We don't ever actually _return_ anything (in particular, beca= use @@ -191,8 +125,11 @@ int symbol__disassemble_llvm(const char *filename, str= uct symbol *sym, struct map *map =3D args->ms.map; struct dso *dso =3D map__dso(map); u64 start =3D map__rip_2objdump(map, sym->start); - u8 *buf; - u64 len; + /* Malloc-ed buffer containing instructions read from disk. */ + u8 *code_buf =3D NULL; + /* Pointer to code to be disassembled. */ + const u8 *buf; + u64 buf_len; u64 pc; bool is_64bit; char disasm_buf[2048]; @@ -207,7 +144,8 @@ int symbol__disassemble_llvm(const char *filename, stru= ct symbol *sym, if (args->options->objdump_path) return -1; =20 - buf =3D read_symbol(filename, map, sym, &len, &is_64bit); + buf =3D dso__read_symbol(dso, filename, map, sym, + &code_buf, &buf_len, &is_64bit); if (buf =3D=3D NULL) return -1; =20 @@ -259,14 +197,18 @@ int symbol__disassemble_llvm(const char *filename, st= ruct symbol *sym, annotation_line__add(&dl->al, ¬es->src->source); =20 pc =3D start; - for (u64 offset =3D 0; offset < len; ) { + for (u64 offset =3D 0; offset < buf_len; ) { unsigned int ins_len; =20 storage.branch_addr =3D 0; storage.pcrel_load_addr =3D 0; =20 - ins_len =3D LLVMDisasmInstruction(disasm, buf + offset, - len - offset, pc, + /* + * LLVM's API has the code be disassembled as non-const, cast + * here as we may be disassembling from mapped read-only memory. + */ + ins_len =3D LLVMDisasmInstruction(disasm, (u8 *)(buf + offset), + buf_len - offset, pc, disasm_buf, sizeof(disasm_buf)); if (ins_len =3D=3D 0) goto err; @@ -324,7 +266,7 @@ int symbol__disassemble_llvm(const char *filename, stru= ct symbol *sym, =20 err: LLVMDisasmDispose(disasm); - free(buf); + free(code_buf); free(line_storage); return ret; #else // HAVE_LIBLLVM_SUPPORT --=20 2.51.0.618.g983fd99d29-goog