Received: by 2002:ab2:6816:0:b0:1f9:5764:f03e with SMTP id t22csp2308420lqo; Mon, 20 May 2024 01:25:01 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXhpyDGYgDBrBhzddYt7w8JvHRPZUHfKf6y3ck7Uj8N5Xmi3A2isDj35Q//dzvOft5+P4u1k5pAxmg++1euLmpEbTlMdjDfBFto2bOo+w== X-Google-Smtp-Source: AGHT+IEKWWiyyLE2XwmfWcSHeX826B1cEWQKayzPw/XGqgwzauaw67ANqglUkTiLvgEmJ9F3A8V7 X-Received: by 2002:a17:906:6cf:b0:a5a:54e9:a9d7 with SMTP id a640c23a62f3a-a5a54e9aa25mr1472301566b.26.1716193501419; Mon, 20 May 2024 01:25:01 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716193501; cv=pass; d=google.com; s=arc-20160816; b=rw/WKiKhztab5+fh4qGwo0EieWTjLL1vab5x1Y+Q98gpd51zxeJ48Z0fkKVOpXbgQC fBIN6Lp43p6W/kT6San7BtzHVNgTG5GoJON37rwPljbVd8Obds9qS1fFaOLcH50xkYnv 49bZIQAAxhgHxLLgGtUaR0dACw+GFraMX2K/wHLFSjuR7NRABc3l3waKPc56oUBQt1UJ jCys7ySvq4JMHLsV9r4TDJGqfkgRjuEeN3o9QlJSbHXNnDTpzkqZPYOWggSnEy1BU2cn x2HhwZTZe31xQIXy1SbWMLKm6u81HxXds15GWV/m2h+8zadqkbk+kVCXKNBOQ3f/qvaW z1hQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=t3v7Wla8eK6FwuQ+NrrrAEqVqGJsnfekL6/dutIVE2c=; fh=NSRSulRInXPks7S1W0sMgVOO+mqt0HfJLP4svzPi+z4=; b=KP0y1eSiuJId4pOd144yGvSoOJCL1ndqY3Hq0n46Xm7wimDBqL38COFGuoUYHMwkU9 FPwyBZhtdch03wY7D3c+7b103dzWz/dbi8v18mEwm2tajIIFMQwfXOoktsLFpan/M/xQ Z8PWMk4GhrvOLEU54iQnX0whMYGyGUMwu0+l8IgkVY6Z3OGRukwe1BYhkIdYwUAs8/sT H5mPcRzV+p13t67CF7OIDg7DUecCliNBcpl9hzs9KK7BEfbsdfdXmFdtg3oey5KZS1yC P8uZf0YKaL1aJ9pu2HUDpHQi2WXqWG6M3tUsUE+oodQop0Gmxl28///qjeP4ATsPs67a gpcA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Cdq2P4lV; arc=pass (i=1 spf=pass spfdomain=flex--sesse.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-183491-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-183491-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id a640c23a62f3a-a5a17be663asi1361981866b.742.2024.05.20.01.25.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 May 2024 01:25:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-183491-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Cdq2P4lV; arc=pass (i=1 spf=pass spfdomain=flex--sesse.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-183491-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-183491-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 042701F214E4 for ; Mon, 20 May 2024 08:25:01 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 140E11CABA; Mon, 20 May 2024 08:24:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Cdq2P4lV" Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (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 A81581BC58 for ; Mon, 20 May 2024 08:24:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716193487; cv=none; b=Ge1gTOWQwXFnQTaz53VEB9I0nvD0Hc0AbeNGdZpX8P3r+4xw11QArJI9hCJzObyewweQ0MUASBIV0m+t5XPcb1iAET7V3YDOEZWbAJBbBwJhe2ygVuTzdw5qtSVQAQiJwkADJ8Ndg1HDS/9IrG2DKI8fxpVP9t7F9jub1+YIFAE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716193487; c=relaxed/simple; bh=eu6ojl58i6PaNBkZTsBa65l6mv2Xh7vqwl4uDCER8f0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ZY/3vvbNVedgAK0T1Xbx/ymctphP4811TTNYhImguWo4YJajQmcCldyu2fzsf123GAjqgv/7CeG8nbKquVKzVYNSDGyMKZ7+OuwTKHkOmCZr7NDvB33X5yn8RvFeVva1eIt+zxWvoA3h8OXOs/0g4mBw2YKW2f4suUcuvKfa3AQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--sesse.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Cdq2P4lV; arc=none smtp.client-ip=209.85.219.201 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--sesse.bounces.google.com Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-de59e612376so16476070276.3 for ; Mon, 20 May 2024 01:24:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1716193484; x=1716798284; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=t3v7Wla8eK6FwuQ+NrrrAEqVqGJsnfekL6/dutIVE2c=; b=Cdq2P4lV3wrjZd3pPhnElVcv5/4XxPbk7KV6McaGNpKP+23HPzya24h/Jq4pnmiome pAmnRi+AVC7Bk1qUZo8z46+DwrvU3hR6ZMo20g798K0xRyXap+GAGfduW/ToJ9yKcnY2 4dvVJh80YLE9hCZQfIeWhZCspIs24CBwTZc1Jf2nBAItUchDWFwQ/ZGX/8DJt4IC2Mdq XZtoB+VdYW1Bi55BfmE25RG5XWvMqTqsJoXU0NGpX0/UhP8A+MDC79b0pIaatUant1Zk HqEuYewUqCU4BZ07ga1y++ZfOveU5JAbCAu9FxyHqQJBOA0RW6rGTlj+8axnKsAbCX6Z 20nQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716193484; x=1716798284; h=cc: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=t3v7Wla8eK6FwuQ+NrrrAEqVqGJsnfekL6/dutIVE2c=; b=SGwT1negZI8QkIUHDAnmTuP1tZhY7JrB8X4wx6+Uj1p54mJijBFgZFF2hWRv6TDht9 5c+alCUd3CJ541TAwD5GxWrWIACaZMc0bgNoNUT12gFTFmTp1hAvjMK3RXPq8oC7UicT NyFBKQ2qAfzYY8kpupbsukBkohOeHgbST6AbjmuYB1FYIyqzm32060Lf5rEyGOM7vUiL kFvvW6nidJmjH0AIWNm5xk6KBBT21lN58Af5Nh+SEiAwiU8bBu5s4Z+DIVglSpQLDzzy Kf09jJRwcLJPCNS8Bxvt6ECAcVZdSAa6UG27wW7HzvMYagH528gIpyJTilmiRm1HJfWf /1ow== X-Forwarded-Encrypted: i=1; AJvYcCVf0GwhF3+GC91i8izwlfWQjdl6cNc4Wgno62kfMUyRl3DaFE6v4OMc7MWP6FquswEzY/F+uLFk5MUWTcXeU9DBz/oFk0Y/rFmvt3A5 X-Gm-Message-State: AOJu0YzEHhdRG+NHz4rlp8OGBNEVkWHahY04mh2gOTBnF+2x0A63Oqes T7gVJuj0e4GzopjdkvcV2+u0jk/wqkjyLwhirekFHwsNqOpjD0xY3demqQrTOfgkAiNcUSMZKQ= = X-Received: from sesse.osl.corp.google.com ([2a00:79e0:18:10:d897:cddc:22b9:d054]) (user=sesse job=sendgmr) by 2002:a05:6902:1082:b0:df4:a395:4cf2 with SMTP id 3f1490d57ef6-df4a395502fmr297617276.0.1716193484680; Mon, 20 May 2024 01:24:44 -0700 (PDT) Date: Mon, 20 May 2024 10:24:28 +0200 In-Reply-To: <20240520082429.320107-1-sesse@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240520082429.320107-1-sesse@google.com> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog Message-ID: <20240520082429.320107-2-sesse@google.com> Subject: [PATCH v3 2/3] perf annotate: split out read_symbol() From: "Steinar H. Gunderson" To: acme@kernel.org Cc: linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, irogers@google.com, "Steinar H. Gunderson" Content-Type: text/plain; charset="UTF-8" The Capstone disassembler code has a useful code snippet to read the bytes for a given code symbol into memory. Split it out into its own function, so that the LLVM disassembler can use it in the next patch. Signed-off-by: Steinar H. Gunderson --- tools/perf/util/disasm.c | 90 +++++++++++++++++++++++++--------------- 1 file changed, 56 insertions(+), 34 deletions(-) diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c index 72aec8f61b94..c0dbb955e61a 100644 --- a/tools/perf/util/disasm.c +++ b/tools/perf/util/disasm.c @@ -1396,6 +1396,53 @@ static int find_file_offset(u64 start, u64 len, u64 pgoff, void *arg) return 0; } +static u8 * +read_symbol(const char *filename, struct map *map, struct symbol *sym, + u64 *len, bool *is_64bit) +{ + struct dso *dso = map__dso(map); + struct nscookie nsc; + u64 start = map__rip_2objdump(map, sym->start); + u64 end = map__rip_2objdump(map, sym->end); + int fd, count; + u8 *buf = NULL; + struct find_file_offset_data data = { + .ip = start, + }; + + *is_64bit = false; + + nsinfo__mountns_enter(dso__nsinfo(dso), &nsc); + fd = open(filename, O_RDONLY); + nsinfo__mountns_exit(&nsc); + if (fd < 0) + return NULL; + + if (file__read_maps(fd, /*exe=*/true, find_file_offset, &data, + is_64bit) == 0) + goto err; + + *len = end - start; + buf = malloc(*len); + if (buf == NULL) + goto err; + + count = pread(fd, buf, *len, data.offset); + close(fd); + fd = -1; + + if ((u64)count != *len) + goto err; + + return buf; + +err: + if (fd >= 0) + close(fd); + free(buf); + return NULL; +} + static void print_capstone_detail(cs_insn *insn, char *buf, size_t len, struct annotate_args *args, u64 addr) { @@ -1458,19 +1505,13 @@ static int symbol__disassemble_capstone(char *filename, struct symbol *sym, { struct annotation *notes = symbol__annotation(sym); struct map *map = args->ms.map; - struct dso *dso = map__dso(map); - struct nscookie nsc; u64 start = map__rip_2objdump(map, sym->start); - u64 end = map__rip_2objdump(map, sym->end); - u64 len = end - start; + u64 len; u64 offset; - int i, fd, count; + int i, count; bool is_64bit = false; bool needs_cs_close = false; u8 *buf = NULL; - struct find_file_offset_data data = { - .ip = start, - }; csh handle; cs_insn *insn; char disasm_buf[512]; @@ -1479,31 +1520,9 @@ static int symbol__disassemble_capstone(char *filename, struct symbol *sym, if (args->options->objdump_path) return -1; - nsinfo__mountns_enter(dso__nsinfo(dso), &nsc); - fd = open(filename, O_RDONLY); - nsinfo__mountns_exit(&nsc); - if (fd < 0) - return -1; - - if (file__read_maps(fd, /*exe=*/true, find_file_offset, &data, - &is_64bit) == 0) - goto err; - - if (open_capstone_handle(args, is_64bit, &handle) < 0) - goto err; - - needs_cs_close = true; - - buf = malloc(len); + buf = read_symbol(filename, map, sym, &len, &is_64bit); if (buf == NULL) - goto err; - - count = pread(fd, buf, len, data.offset); - close(fd); - fd = -1; - - if ((u64)count != len) - goto err; + return -1; /* add the function address and name */ scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:", @@ -1521,6 +1540,11 @@ static int symbol__disassemble_capstone(char *filename, struct symbol *sym, annotation_line__add(&dl->al, ¬es->src->source); + if (open_capstone_handle(args, is_64bit, &handle) < 0) + goto err; + + needs_cs_close = true; + count = cs_disasm(handle, buf, len, start, len, &insn); for (i = 0, offset = 0; i < count; i++) { int printed; @@ -1565,8 +1589,6 @@ static int symbol__disassemble_capstone(char *filename, struct symbol *sym, return count < 0 ? count : 0; err: - if (fd >= 0) - close(fd); if (needs_cs_close) { struct disasm_line *tmp; -- 2.43.0