From patchwork Wed Jan 22 17:42:59 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 13947601 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.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 6849B218AB2 for ; Wed, 22 Jan 2025 17:43:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737567815; cv=none; b=lm6JWKgldqofM2Lubks6tdrL1fOsHv32VUWlkGhsYcpXIxOtUBefEDr2cculKIPsPnLtmj/QRVw2BVt9LfhKwmYyp3BYU6PKnwiZKm//qke23tIN52EehjfD0wJZoLsX8VuDLTHYCWScaqFl9QiBWsQ9JH3cHTiNHUGHIjIxt2U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737567815; c=relaxed/simple; bh=LqqJkW9wQZg/mqVTYlhoF7g7n6RPfhtbpaNpwaetZ7w=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=aiFRrYZm2ukTDgliR8Fh1Ztgp88Ntu6cnG/d3KGfh0qwkV4WK5bWMmJ/mosdRzH45CDyMzFjzUI3CSBJoeKMijlkl50BZGNPjea9TduSPMVDSOoNqF0bmiTy1qTO5V3tXSRCnrqbpqyJFfOq7C2XVrjYsp/02Ud0EvsPP94sKno= 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=RrS2I89D; arc=none smtp.client-ip=209.85.219.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="RrS2I89D" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e549de22484so18771146276.2 for ; Wed, 22 Jan 2025 09:43:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737567812; x=1738172612; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=GYbV6ojbffPu+gOoXIxYld5orW9nEz6CS6izeWV5Q/Q=; b=RrS2I89Dm6k2cgYuo8YSAJ+pnSRjwdUAqiMqlsj+GBTosltlGgnvR4KOrqQS3D5BgV 4aSdEssfzeH8rqaQO7XeOfbXyQY8SX56v9/bkvQDQZqRLfPvqUESoS4z/IwN9erN/P3G AT2duQNlMJveTmtJxTllDUaQp/vb7JRIICWlks/XxyBf34jq8WGjPe2iPKsbiQEQshQi T0W0Gl9+v88goMEgksxek2Zwb8qNjibk8N835NK6pJyPH1hgFfZF9eQ9kPebpUA4cBdV Wd7Barv4EYHDVEUcNtzGIvJFr8SQ/SIM9PWbyICwlTvTnUEzL8Z/8BYR7nWOU7cZ8vhZ mU4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737567812; x=1738172612; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=GYbV6ojbffPu+gOoXIxYld5orW9nEz6CS6izeWV5Q/Q=; b=lIC9XCEA+hRgZvgk9uQRg5WZ4ZRQvQ9QzHn5tCCW71z2mIMlajl1ZEWlwk9LqDBA2J 0Sf9zeooOG8U6XRJJEufgC2gi0ZMByjpYUX7EJBmtr+/5rs3l4ZKsJbvdfvone88Jd3/ E6O+xxx3zSzxzN0vfu6vcFzta9xwnMbuuH4JO33dua+H3Og4+DyplMNF4LJMw/vQGTYi XMxQSgtc7VSvkKGeQ4XFNCP9v8rRS7awkuDgFwWzx7z+f+bMmzWcJ93gQ0uBTxAa2EYq 3RmbaxCwfGL+aSQ/PK6jGSQMA1Mj0OE8UHwiZlq1GL9DmdGYoWgvxGGV0joDWctzzfvo ppRQ== X-Forwarded-Encrypted: i=1; AJvYcCUtc8kaFeDjukH12AkQFIZD7Jw2CjFEY6U+IzBUulNA0MmHOYETTwr1BVUpnZnF3QwLtds=@vger.kernel.org X-Gm-Message-State: AOJu0YyrYvOq0TK7MurdnhCPsq9N1G7kuvT7m5eLzVbICULNSHZRKLSv YPb9G55O3dpL8br2AYljRgWK4wAAVXafNtV4C3vUsWJBiGML60NQL1qCQdw82sE22jsu2qHbRag DCN1c7g== X-Google-Smtp-Source: AGHT+IHx8m+lePTVPMpD2Dvolez/WdPoS04cbuOyWXsPeMR4rZOgZW0kEpZDnFCabZwjTDzxVBQaMDMalK1i X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:807b:be79:d5c3:ee5c]) (user=irogers job=sendgmr) by 2002:a81:b241:0:b0:6ef:70fb:459b with SMTP id 00721157ae682-6f6eb511590mr457277b3.0.1737567812388; Wed, 22 Jan 2025 09:43:32 -0800 (PST) Date: Wed, 22 Jan 2025 09:42:59 -0800 In-Reply-To: <20250122174308.350350-1-irogers@google.com> Message-Id: <20250122174308.350350-10-irogers@google.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250122174308.350350-1-irogers@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Subject: [PATCH v3 09/18] perf dso: Move read_symbol from llvm/capstone to dso From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Aditya Gupta , "Steinar H. Gunderson" , Charlie Jenkins , Changbin Du , "Masami Hiramatsu (Google)" , James Clark , Kajol Jain , Athira Rajeev , Li Huafei , Dmitry Vyukov , Andi Kleen , Chaitanya S Prakash , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, llvm@lists.linux.dev, Song Liu , bpf@vger.kernel.org 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 | 64 +++++----------------------- tools/perf/util/dso.c | 67 +++++++++++++++++++++++++++++ tools/perf/util/dso.h | 4 ++ tools/perf/util/llvm.c | 87 +++++++------------------------------- 4 files changed, 97 insertions(+), 125 deletions(-) diff --git a/tools/perf/util/capstone.c b/tools/perf/util/capstone.c index 8d65c7a55a8b..f103988184c8 100644 --- a/tools/perf/util/capstone.c +++ b/tools/perf/util/capstone.c @@ -434,66 +434,23 @@ 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; -} - 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 len; u64 offset; int i, count, free_count; bool is_64bit = false; bool needs_cs_close = false; - u8 *buf = NULL; + /* 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]; @@ -503,7 +460,8 @@ int symbol__disassemble_capstone(const char *filename __maybe_unused, if (args->options->objdump_path) return -1; - buf = read_symbol(filename, map, sym, &len, &is_64bit); + buf = dso__read_symbol(dso, filename, map, sym, + &code_buf, &buf_len, &is_64bit); if (buf == NULL) return -1; @@ -532,7 +490,7 @@ int symbol__disassemble_capstone(const char *filename __maybe_unused, needs_cs_close = true; - free_count = count = perf_cs_disasm(handle, buf, len, start, len, &insn); + free_count = count = perf_cs_disasm(handle, buf, buf_len, start, buf_len, &insn); for (i = 0, offset = 0; i < count; i++) { int printed; @@ -556,7 +514,7 @@ int symbol__disassemble_capstone(const char *filename __maybe_unused, } /* It failed in the middle: probably due to unknown instructions */ - if (offset != len) { + if (offset != buf_len) { struct list_head *list = ¬es->src->source; /* Discard all lines and fallback to objdump */ @@ -575,7 +533,7 @@ int symbol__disassemble_capstone(const char *filename __maybe_unused, if (free_count > 0) perf_cs_free(insn, free_count); } - free(buf); + free(code_buf); return count < 0 ? count : 0; err: diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c index 5c6e85fdae0d..0285904ed26d 100644 --- a/tools/perf/util/dso.c +++ b/tools/perf/util/dso.c @@ -1664,3 +1664,70 @@ bool is_perf_pid_map_name(const char *dso_name) 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 = arg; + + if (start <= data->ip && data->ip < start + len) { + data->offset = 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 = map__rip_2objdump(map, sym->start); + u64 end = map__rip_2objdump(map, sym->end); + int fd, count; + u8 *buf = NULL; + size_t len; + struct find_file_offset_data data = { + .ip = start, + }; + + *out_buf = NULL; + *out_buf_len = 0; + *is_64bit = false; + + nsinfo__mountns_enter(dso__nsinfo(dso), &nsc); + fd = open(symfs_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; + + *out_buf = buf; + *out_buf_len = len; + return buf; + +err: + if (fd >= 0) + close(fd); + free(buf); + return NULL; +} diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h index bb8e8f444054..734e3a3d95f0 100644 --- a/tools/perf/util/dso.h +++ b/tools/perf/util/dso.h @@ -828,4 +828,8 @@ u64 dso__findnew_global_type(struct dso *dso, u64 addr, u64 offset); bool perf_pid_map_tid(const char *dso_name, int *tid); bool is_perf_pid_map_name(const char *dso_name); +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 f6a8943b7c9d..a0774373f0d6 100644 --- a/tools/perf/util/llvm.c +++ b/tools/perf/util/llvm.c @@ -296,71 +296,6 @@ void dso__free_a2l_llvm(struct dso *dso __maybe_unused) /* Nothing to free. */ } - -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; -} - -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; -} - /* * Whenever LLVM wants to resolve an address into a symbol, it calls this * callback. We don't ever actually _return_ anything (in particular, because @@ -397,8 +332,11 @@ int symbol__disassemble_llvm(const char *filename, struct symbol *sym, struct map *map = args->ms.map; struct dso *dso = map__dso(map); u64 start = map__rip_2objdump(map, sym->start); - u8 *buf; - u64 len; + /* Malloc-ed buffer containing instructions read from disk. */ + u8 *code_buf = NULL; + /* Pointer to code to be disassembled. */ + const u8 *buf; + u64 buf_len; u64 pc; bool is_64bit; char triplet[64]; @@ -418,7 +356,8 @@ int symbol__disassemble_llvm(const char *filename, struct symbol *sym, perf_LLVMInitializeAllTargetMCs(); perf_LLVMInitializeAllDisassemblers(); - buf = read_symbol(filename, map, sym, &len, &is_64bit); + buf = dso__read_symbol(dso, filename, map, sym, + &code_buf, &buf_len, &is_64bit); if (buf == NULL) return -1; @@ -466,14 +405,18 @@ int symbol__disassemble_llvm(const char *filename, struct symbol *sym, annotation_line__add(&dl->al, ¬es->src->source); pc = start; - for (u64 offset = 0; offset < len; ) { + for (u64 offset = 0; offset < buf_len; ) { unsigned int ins_len; storage.branch_addr = 0; storage.pcrel_load_addr = 0; - ins_len = perf_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 = perf_LLVMDisasmInstruction(disasm, (u8 *)(buf + offset), + buf_len - offset, pc, disasm_buf, sizeof(disasm_buf)); if (ins_len == 0) goto err; @@ -531,7 +474,7 @@ int symbol__disassemble_llvm(const char *filename, struct symbol *sym, err: perf_LLVMDisasmDispose(disasm); - free(buf); + free(code_buf); free(line_storage); return ret; }