From patchwork Wed Jan 22 06:23:25 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 13946898 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 660D31B0F35 for ; Wed, 22 Jan 2025 06:25:11 +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=1737527113; cv=none; b=U8+OYDIsuds3lFluFKSNXadg2RQYd9K4v6WJ1fXWM65iaBDO1/LTv0Om4zE6uUP8jcAOnh9IPHO9KdlVC5JGykYa+MiT7Zm44q/bNONfe5DmyX3XSSch92/isEe23zPQ1PmhG+74BJyBgNgP+6heNMW3ae6rIj6IavhIRhmlTeg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737527113; c=relaxed/simple; bh=63nBtpeMpe7xv4aEwrBhb9npTX5s1rkxRdlgAXmvoMc=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=tCKS56CKEZLkRW5i2UFFvtTamaWYtEvD0gY7Y/R3PmI+uSA6dTuxqQC+GtxN93s9wqj835UWk4XVdpRIoXLc0qfuYqZ9FR9mk+WK+vFogauoaiclX5T5vRAY139TlxS35eb6trRQ86WVymyeWOgxI6ubQCjpiNNvXr7UPOsLt84= 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=llUaRN7g; 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="llUaRN7g" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e579836ee17so12674262276.2 for ; Tue, 21 Jan 2025 22:25:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737527110; x=1738131910; 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=wLotHYn7qhYJCMkThRcrq9/AzDWfjF/CYgpDO7eIVEI=; b=llUaRN7gBolxFomTia5TxhwOGEKEtBtoIDt5c8ccPPDWHyYb9+lZHyiiEqvzLXlHxy AOPhX1+EgnnIc/vqVCymXRogQ2Y7Ut2wAZKdaxxAJe2CshECvTevjNn5k1It8G9KIugf QKfpKS3G6Uj3vZ0woIAMyFya283lrvwVIYTZ6LnyPVFU+SEDeFNRc0bZZcioGncMdLyp JKLOCEsBW/ReqXITUYcPIOHB/p/OqdmJHlrRuuL8AcPYKsrs4cfqNU/y01T8jfzbJRqc NWV47idbRTLz+F4YmdBkZxwaq4AOwH5wKFl5EmQxyS0f20wxLm+KzuHLn/dJBgjXZcoo Bz6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737527110; x=1738131910; 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=wLotHYn7qhYJCMkThRcrq9/AzDWfjF/CYgpDO7eIVEI=; b=j0zMSchET/f5vSevKPXj4pdxBN69p6hv1YOViXunOPQ4kDYZZuRnUSEgY6hZ7mxGBh zECXxEZCT9R3CJmRE62LoWGihAy3sJM/GDhkwDloGYVjjHCoN1sb+5YD8srVTD3yWSBS ZnLfICTwW+pQM3ZBcKgJ6HY57CwSDR6GS82xVRSg9OmoCn/W4rmVY5nP0xdfaIsV2b0D FE29jUOgFagw7cg/0HlV2GPmjj76VAiAG/MiZtcTxftNh/OCEAgX6iyM/vLzTp3Sq9ra gYAm8BiP7XvYd61aVq4N5MUHVkMVGKH8t9hnfieCJBq3fcLRhri81oIP5bSy0ufiXvHY y8zg== X-Forwarded-Encrypted: i=1; AJvYcCWWDYvQgZJ4FtQYzF8cvoI1nm78WG02eTsScn2DjCdp6AdZxcZzXIBtGk7KGexuZnyZtLA=@vger.kernel.org X-Gm-Message-State: AOJu0YwuWWb3v3mQC6HWtc8rOqdorADmJxv/sqgERvY6v1QlJ5xjZKST IjZx2i7XmJJtVZ0uIQ4EFf0Xf6pO0gikhytxX7ANge0PMsXg7R9OZI8sA1LPKhbwsOv9+sygJLj 6Stj7Jw== X-Google-Smtp-Source: AGHT+IEqK8ZkONJZBAyEEerDbvvaV8PkDpFyO4x4ko4onzv5SasjqsaaN+Db8+thwFx1lPNpBvA0psxpee3q X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:807b:be79:d5c3:ee5c]) (user=irogers job=sendgmr) by 2002:a5b:341:0:b0:e48:9cab:d051 with SMTP id 3f1490d57ef6-e57b13e4fcamr41710276.9.1737527110404; Tue, 21 Jan 2025 22:25:10 -0800 (PST) Date: Tue, 21 Jan 2025 22:23:25 -0800 In-Reply-To: <20250122062332.577009-1-irogers@google.com> Message-Id: <20250122062332.577009-11-irogers@google.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250122062332.577009-1-irogers@google.com> X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Subject: [PATCH v2 10/17] perf dso: Support BPF programs in dso__read_symbol 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 Set the buffer to the code in the BPF linear info. This enables BPF JIT code disassembly by LLVM and capstone. Move the disassmble_bpf calls to disassemble_objdump so that they are only called after falling back to the objdump option. Signed-off-by: Ian Rogers --- tools/perf/util/disasm.c | 12 +++--- tools/perf/util/dso.c | 85 +++++++++++++++++++++++++--------------- 2 files changed, 60 insertions(+), 37 deletions(-) diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c index a9cc588a3006..99b9c21e02b0 100644 --- a/tools/perf/util/disasm.c +++ b/tools/perf/util/disasm.c @@ -1500,6 +1500,12 @@ static int symbol__disassemble_objdump(const char *filename, struct symbol *sym, struct child_process objdump_process; int err; + if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_PROG_INFO) + return symbol__disassemble_bpf(sym, args); + + if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_IMAGE) + return symbol__disassemble_bpf_image(sym, args); + err = asprintf(&command, "%s %s%s --start-address=0x%016" PRIx64 " --stop-address=0x%016" PRIx64 @@ -1681,11 +1687,7 @@ int symbol__disassemble(struct symbol *sym, struct annotate_args *args) pr_debug("annotating [%p] %30s : [%p] %30s\n", dso, dso__long_name(dso), sym, sym->name); - if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_PROG_INFO) { - return symbol__disassemble_bpf(sym, args); - } else if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_IMAGE) { - return symbol__disassemble_bpf_image(sym, args); - } else if (dso__binary_type(dso) == DSO_BINARY_TYPE__NOT_FOUND) { + if (dso__binary_type(dso) == DSO_BINARY_TYPE__NOT_FOUND) { return SYMBOL_ANNOTATE_ERRNO__COULDNT_DETERMINE_FILE_TYPE; } else if (dso__is_kcore(dso)) { kce.addr = map__rip_2objdump(map, sym->start); diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c index 0285904ed26d..a90799bed230 100644 --- a/tools/perf/util/dso.c +++ b/tools/perf/util/dso.c @@ -1686,48 +1686,69 @@ 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, - }; + const u8 *buf; + size_t len = end - 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) + if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_IMAGE) { + pr_debug("No BPF image disassembly support\n"); return NULL; + } else if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_PROG_INFO) { +#ifdef HAVE_LIBBPF_SUPPORT + struct bpf_prog_info_node *info_node; + struct perf_bpil *info_linear; + + *is_64bit = sizeof(void *) == sizeof(u64); + info_node = perf_env__find_bpf_prog_info(dso__bpf_prog(dso)->env, + dso__bpf_prog(dso)->id); + if (!info_node) { + errno = SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF; + return NULL; + } + info_linear = info_node->info_linear; + buf = (const u8 *)(uintptr_t)(info_linear->info.jited_prog_insns); + assert(len <= info_linear->info.jited_prog_len); +#else + pr_debug("No BPF program disassembly support\n"); + return NULL; +#endif + } else { + struct nscookie nsc; + int fd; + ssize_t count; + struct find_file_offset_data data = { + .ip = start, + }; + u8 *code_buf = 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; + nsinfo__mountns_enter(dso__nsinfo(dso), &nsc); + fd = open(symfs_filename, O_RDONLY); + nsinfo__mountns_exit(&nsc); + if (fd < 0) + return NULL; - *out_buf = buf; + if (file__read_maps(fd, /*exe=*/true, find_file_offset, &data, is_64bit) == 0) { + close(fd); + return NULL; + } + buf = code_buf = malloc(len); + if (buf == NULL) { + close(fd); + return NULL; + } + count = pread(fd, code_buf, len, data.offset); + close(fd); + if ((u64)count != len) { + free(code_buf); + return NULL; + } + *out_buf = code_buf; + } *out_buf_len = len; return buf; - -err: - if (fd >= 0) - close(fd); - free(buf); - return NULL; }