From nobody Fri Dec 19 06:33:53 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 43ADE221544 for ; Thu, 17 Apr 2025 23:07:57 +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=1744931278; cv=none; b=e4hccWgfRL5kACNNpp4gLYn2TuRXNYWgFSL42VGa8UGJkZqr9crogZINPk6lLdVVgf/f/qG9kv1Z8L8nmY9w0DpBFyeuTUmq26gMEb4BsCEEmlsyMhiPdec1x/gs5V1oHXK0cWxgBzjN9jlmAuv6EVfkvtZejD85tu7jRzysgKc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744931278; c=relaxed/simple; bh=DUhmjAhpzI4glFlJFVyHh6Dy0isdHfkiubMes37ZCtA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=SbC5l0TH+aT9yBPtKTRRZ5p9Txgr1h6RKsoXR8WXKghFmVOP5Ms375i4UB6SUjNiw+w6S1zuHeJ1UkPG5W+CReNM2KkbASx9EmFhxwx/Cr9+f28M8l3g+kt2gfJXU5iqgXi6IjrR0XeR4DbbbWrWDaT+Uh+gXCzy88rwMl6QknI= 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=Tatps3Ue; 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="Tatps3Ue" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2264e5b2b7cso11878865ad.2 for ; Thu, 17 Apr 2025 16:07:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1744931276; x=1745536076; 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=qnICyJ2RGS0Y400AqNRs1KksXFmr5hh/vv3vYVQnBHQ=; b=Tatps3UeOf3aVg+3xmw+Ju+O8rdbwyVWbCD32mkYDDwu7ibuc/j4wotSFOrASJSFQZ eaNO+oRAD82RTlqH+Cxqe8w6QJCd8WJ/0uVVOhG2PCTrqbygl4immiLzgtzlTVvaFmP/ +ET3q0RiWqjkj03aJ+uS0Rcory8jkt3EbXsrbzb+GjsSUoNDUfea8CGncx29InN3wJnN Wxj8GTNYkUG2/79z7HSHIprwC8NhBySAk1SS5rq3xO3b/5eWtHq42dSz3k/G8dcW6fTP 64H4J0GBlewWDWE0+nBSik8eDIQK+4b/JUlE9AiSZUY2wa/wI3XrmcRKrx43UtauIjTO o7Nw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744931276; x=1745536076; 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=qnICyJ2RGS0Y400AqNRs1KksXFmr5hh/vv3vYVQnBHQ=; b=beuRqYoReeEBw6FSPfBbJP198eEln9UvaiYXbqTf7F3iA317iU65fOCOEWwNJ22Ljx XD7KuNoQ4oxb03UD/5KkSbGQQh2xm42YYY/1mrUqGZJpG9QFcUQQ2CBl/5k5bF88sKzO /WfMfGIms3kyVJda3Hm7wmmJnFuIvAwGBY9LXE1lPbxY/JDcBiFFgiuTHKNWFfmwdA6+ u91MhGz7rXkhmjU9edQBj++dh5erNL5dmReTrm6hV7OcXF6y/AJ45bvW0ahWckYhpzmd LbPSSyycf7viUBNZYcih2Z5quEm3zPNz9Mgu0iDCnYJ5AHVksmYbOjvgeQoY01PhCxue XL6w== X-Forwarded-Encrypted: i=1; AJvYcCWlUPegljkURC+OCJoi4qhe4VSfm7W9XQ729e4sTvPvG2Vkd8rfc5q4beTnVD9MxKL9z65vPB1EFLkagnU=@vger.kernel.org X-Gm-Message-State: AOJu0Yxw1hmjgBhDdgXW7BNONAW8Giqn+zLYpjy7A1u1jcueLavC31ud k2769BWnnkRTw2wtmboPx3FevQaYD24WXZ9dtyDR9fhrnqYL/AMUoAp/1ECuqx3ZhqUnLzZYMhb lqlHLMg== X-Google-Smtp-Source: AGHT+IEwa/KfQTqntEV82qp0baymPwWs4os7wo5Uctj1zdlTuJFxSq6S5ewEmLz+IORlKXcqOKBHo7BSNgld X-Received: from plkj8.prod.google.com ([2002:a17:902:6908:b0:220:d79f:a9bd]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:ecd1:b0:226:5dbf:373f with SMTP id d9443c01a7336-22c53379f8amr10249835ad.10.1744931276358; Thu, 17 Apr 2025 16:07:56 -0700 (PDT) Date: Thu, 17 Apr 2025 16:07:22 -0700 In-Reply-To: <20250417230740.86048-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: <20250417230740.86048-1-irogers@google.com> X-Mailer: git-send-email 2.49.0.805.g082f7c87e0-goog Message-ID: <20250417230740.86048-2-irogers@google.com> Subject: [PATCH v4 01/19] perf build: Remove libtracefs configuration 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" libtracefs isn't used by perf but not having it installed causes build warnings. Given the library isn't used, there is no need for the configuration or warnings so remove. Signed-off-by: Ian Rogers --- tools/perf/Makefile.config | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/tools/perf/Makefile.config b/tools/perf/Makefile.config index 9f08a6e96b35..f31b240cd23e 100644 --- a/tools/perf/Makefile.config +++ b/tools/perf/Makefile.config @@ -1176,20 +1176,6 @@ ifneq ($(NO_LIBTRACEEVENT),1) else $(error ERROR: libtraceevent is missing. Please install libtraceevent-= dev/libtraceevent-devel and/or set LIBTRACEEVENT_DIR or build with NO_LIBTR= ACEEVENT=3D1) endif - - ifeq ($(feature-libtracefs), 1) - CFLAGS +=3D $(shell $(PKG_CONFIG) --cflags libtracefs) - LDFLAGS +=3D $(shell $(PKG_CONFIG) --libs-only-L libtracefs) - EXTLIBS +=3D $(shell $(PKG_CONFIG) --libs-only-l libtracefs) - LIBTRACEFS_VERSION :=3D $(shell $(PKG_CONFIG) --modversion libtracefs)= .0.0 - LIBTRACEFS_VERSION_1 :=3D $(word 1, $(subst ., ,$(LIBTRACEFS_VERSION))) - LIBTRACEFS_VERSION_2 :=3D $(word 2, $(subst ., ,$(LIBTRACEFS_VERSION))) - LIBTRACEFS_VERSION_3 :=3D $(word 3, $(subst ., ,$(LIBTRACEFS_VERSION))) - LIBTRACEFS_VERSION_CPP :=3D $(shell expr $(LIBTRACEFS_VERSION_1) \* 25= 5 \* 255 + $(LIBTRACEFS_VERSION_2) \* 255 + $(LIBTRACEFS_VERSION_3)) - CFLAGS +=3D -DLIBTRACEFS_VERSION=3D$(LIBTRACEFS_VERSION_CPP) - else - $(warning libtracefs is missing. Please install libtracefs-dev/libtrac= efs-devel) - endif endif =20 # Among the variables below, these: --=20 2.49.0.805.g082f7c87e0-goog From nobody Fri Dec 19 06:33:53 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 4103421D3EE for ; Thu, 17 Apr 2025 23:07:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744931280; cv=none; b=LEIDkBrapcL0+aipQ3texqKndygIaMIaWKAzkNpL9i+GwgacZ7u8gZ5QtcR2/zSdfhvE7TwMqRvmjT18vPSTr16bIUN9zLxv84BOq0tj1qKOQc3YEZ1TeADQMgHv4aFHeNETZtTlI9+uq6KbMKqeNM0otyDDptq+j0ocxtQvQks= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744931280; c=relaxed/simple; bh=mJQVFLCm9ty84JCDlLg5++0OwAqRrW5FDC64ClqBhO8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=Z7RCAXmGvqsY1VpEkn9ePo/0DChXgrC3ts1MOakwV7H4lhSJXOKelxlE1L+xGA+MywF7geWeGbu3t7WlpnzlL6Ue9h9lgdA7IkoSqH4dUb8pmIpS52KLTH6oj6Vsnql7Fdi/I0TxdVZ3G7XJiAlq9gmVITjQ9U5ky0ZZcaYpXKI= 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=qBrxRa1+; arc=none smtp.client-ip=209.85.216.73 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="qBrxRa1+" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2ff605a7a43so1883965a91.3 for ; Thu, 17 Apr 2025 16:07:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1744931278; x=1745536078; 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=PaI5u9A1ZK2E6Bx2qH0e5jW/HFuv3lmW4guRFbw3gqw=; b=qBrxRa1+ec9Ws5tbhZ5R2CKAnzVXDEeFJP3B6jGs+HI0iYWUEGJozfy8MqMkuCvm30 LNOTriykQ+VIw+5RpRWZDNTburKjCrilgYLM8o1Cgf55zOp5SyuMohATP88D+1p8oDY3 QoIBPiNyELe1l8s/+mhLwro4zdxuSWo7HF7Vz15o+PX1Wht69PY/8bv6M4xZsXGwsosd LoEWRbXP2/PfBrbSu25tyUi+8FJtzqeS1s8hoELNJI7j7NDim2EhUR6+czjC6v+ESEQW eU7wd5tC5nO+D3X9EI0BMQZPkCYxyet01tI6vxVFm38q1WM+3B0EU5ANIYGnHlEWywqt 8yow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744931278; x=1745536078; 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=PaI5u9A1ZK2E6Bx2qH0e5jW/HFuv3lmW4guRFbw3gqw=; b=gym7aJnWY3CgGDXxzOy+I7M3IxqrbaY0BEjkPuW0UTYMZZxvkbWuBlaQAApaENq1rS LYCLoJpd+caxmGEwflgLH2O5tiHFFAr+PhHyMJPr6ZhZ7yPgaZatUwWmB9jW+1wUJm7r HfQfMf0Or/VHYpHjqGQHrGX2LJwgVFTtVTV1oSwqCFbMMMwOAcDMkUcyeR9BpIsa81Js FfPAovyiIiI3avhWbWLvZWUypNtNDwmxA7U9JBPdz7nt7W70INn0nZt94iwuNkrASECq VllwGg7tU8+tmQklggN0I13vryKu9oteVdQTkV/8Ukemv/7YeEAXm3o6z14RaZLXu5/A I2UQ== X-Forwarded-Encrypted: i=1; AJvYcCWJUd8j/jeL2qRhiSVBu/Aa5S24CAVfkMHEw55ailEgos3U5stX5RK8kGRcbABjdKGNv5fYUgYdIW8Tkvc=@vger.kernel.org X-Gm-Message-State: AOJu0YzqKd6Gts54/7cJ0DY1hSBsbMjExUBFEA/3IqW8FEzctlCAOFu8 gGzfM624fqkhgcYKlybLw5o4dRsvwVnmTeMrgZYIh5jYdM+ir8ALw33eMDIT33huONEfh27ecjD rqD5kAg== X-Google-Smtp-Source: AGHT+IF4TiTN8mIjFeWYyjcvH+QxpKzHnUuvBCtLoCycCssQ0z2iMetyKW3xvnPKYvVbL7H/8N/Pe+HdGEeN X-Received: from pjbsf15.prod.google.com ([2002:a17:90b:51cf:b0:2fa:1fac:2695]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3a4b:b0:2fe:b774:3ec8 with SMTP id 98e67ed59e1d1-3087bcc7f59mr819081a91.23.1744931278619; Thu, 17 Apr 2025 16:07:58 -0700 (PDT) Date: Thu, 17 Apr 2025 16:07:23 -0700 In-Reply-To: <20250417230740.86048-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: <20250417230740.86048-1-irogers@google.com> X-Mailer: git-send-email 2.49.0.805.g082f7c87e0-goog Message-ID: <20250417230740.86048-3-irogers@google.com> Subject: [PATCH v4 02/19] perf map: Constify objdump offset/address conversion APIs 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Make the map argument const as the conversion act won't modify the map and this allows other callers to use a const struct map. Signed-off-by: Ian Rogers --- tools/perf/util/map.c | 19 +++++++++++++++---- tools/perf/util/map.h | 6 +++--- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/tools/perf/util/map.c b/tools/perf/util/map.c index d729438b7d65..a22a423792d7 100644 --- a/tools/perf/util/map.c +++ b/tools/perf/util/map.c @@ -514,6 +514,8 @@ void srccode_state_free(struct srccode_state *state) state->line =3D 0; } =20 +static const struct kmap *__map__const_kmap(const struct map *map); + /** * map__rip_2objdump - convert symbol start address to objdump address. * @map: memory map @@ -525,9 +527,9 @@ void srccode_state_free(struct srccode_state *state) * * Return: Address suitable for passing to "objdump --start-address=3D" */ -u64 map__rip_2objdump(struct map *map, u64 rip) +u64 map__rip_2objdump(const struct map *map, u64 rip) { - struct kmap *kmap =3D __map__kmap(map); + const struct kmap *kmap =3D __map__const_kmap(map); const struct dso *dso =3D map__dso(map); =20 /* @@ -570,7 +572,7 @@ u64 map__rip_2objdump(struct map *map, u64 rip) * * Return: Memory address. */ -u64 map__objdump_2mem(struct map *map, u64 ip) +u64 map__objdump_2mem(const struct map *map, u64 ip) { const struct dso *dso =3D map__dso(map); =20 @@ -587,7 +589,7 @@ u64 map__objdump_2mem(struct map *map, u64 ip) } =20 /* convert objdump address to relative address. (To be removed) */ -u64 map__objdump_2rip(struct map *map, u64 ip) +u64 map__objdump_2rip(const struct map *map, u64 ip) { const struct dso *dso =3D map__dso(map); =20 @@ -619,6 +621,15 @@ struct kmap *__map__kmap(struct map *map) return (struct kmap *)(&RC_CHK_ACCESS(map)[1]); } =20 +static const struct kmap *__map__const_kmap(const struct map *map) +{ + const struct dso *dso =3D map__dso(map); + + if (!dso || !dso__kernel(dso)) + return NULL; + return (struct kmap *)(&RC_CHK_ACCESS(map)[1]); +} + struct kmap *map__kmap(struct map *map) { struct kmap *kmap =3D __map__kmap(map); diff --git a/tools/perf/util/map.h b/tools/perf/util/map.h index 4262f5a143be..768501eec70e 100644 --- a/tools/perf/util/map.h +++ b/tools/perf/util/map.h @@ -133,13 +133,13 @@ static inline u64 map__unmap_ip(const struct map *map= , u64 ip_or_rip) } =20 /* rip/ip <-> addr suitable for passing to `objdump --start-address=3D` */ -u64 map__rip_2objdump(struct map *map, u64 rip); +u64 map__rip_2objdump(const struct map *map, u64 rip); =20 /* objdump address -> memory address */ -u64 map__objdump_2mem(struct map *map, u64 ip); +u64 map__objdump_2mem(const struct map *map, u64 ip); =20 /* objdump address -> rip */ -u64 map__objdump_2rip(struct map *map, u64 ip); +u64 map__objdump_2rip(const struct map *map, u64 ip); =20 struct symbol; struct thread; --=20 2.49.0.805.g082f7c87e0-goog From nobody Fri Dec 19 06:33:53 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 63BB12222D5 for ; Thu, 17 Apr 2025 23:08:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744931283; cv=none; b=qRan3Awzgm+rTvIt8MXav6nUgqtjTXA57rKW8giVKVQKspLdixD/3e0DH4CTDKWfKcBViNt8o/1TTOcSWKBCvbWoyUmJJCTSOGCXdjh+Cqahj/1lJzqDSWpD1VUplJau+ofraPbCkt+I7qykuIHO4d6G+g32n5RIvKCeYsDIpmc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744931283; c=relaxed/simple; bh=qJWghb4bMpGAmyDF2Bh8OBncl7SvpLysGonZafLFusk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=jDy5uRuwvVXulwh1zpkDLNMkHo3cZgPdcEiFFiASQCg6HA4B6PVl0e50AoxgKx9gxtER6/39D+APzUdqwp7acWipf/0QJXFAgx+XpYDPULLF7Kl/bZ/hAzQt9ZylGXdzq9KLk1Zzybh+n7t+ioVqRJ0msuEzHQ/ec9coyFABTnQ= 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=ChfDokau; arc=none smtp.client-ip=209.85.216.74 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="ChfDokau" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ff52e1c56fso1725061a91.2 for ; Thu, 17 Apr 2025 16:08:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1744931281; x=1745536081; 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=3i/8O3trr6QjT++RHRc8mQu7JHKaB3UMffwT2gzf59Q=; b=ChfDokauEhuvtmnRpLPyAmWXagYRp2OLnzD7Mg5MX8Z25VteMt9qFD575LThfUOElm JyGM0Xld9M+BDOCGtQ8LAAMIsLjgpuqFHnOPa9hjbX2SNiBQ8LuF7pWNkL3JMmzB5Fd1 ikXTWxCIQcCVumOtUTFG6Ulnsrag1yWeY8b9B5qfWnco+ex28RDRCW/eCS0JAGqhi1q0 09NkJEyU8b1vk4axcpCi5BoIiVOrKwwc/RuWMiqgw7lM/4VVABROU5AlNCMGLPvBf4il //Vnltu6Z2j58FlZiQKPAOt1B0HkKXXIjuT52kuw/7v+J+ytUHqIyvB8ilbuAM6QkYLr myPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744931281; x=1745536081; 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=3i/8O3trr6QjT++RHRc8mQu7JHKaB3UMffwT2gzf59Q=; b=jeCDFga4040ghqwn+SQ8YblUEou0SH44JjV2/yNKD1Ll8fOEarYQcw6E4+10UUJy3t U0RSdTz3I1xL5gPDe57N0GsxtigiH0x7ifVRc4ubYNM81qUs2riRoOKU+qrQwjml20LD IsxKWTACb8G0FWFdktDOos6Jc6j/MMwCfETw9i/K7FGsi6LE+6H4NF7MvJSUnDuZNgCz ATL98duJFEKvmOe2UuuWkDKCQmU+AC/5UkPrco+vu7+/VxLSzRxyFcYhpN4L7ElvVdhD iMh05FNU9wj9+QWgsKUTaIp0OQTldE6JUGOVDcyQ5zbnH4G79Vj/rUDLZTLTpT1Dj/JV LVtA== X-Forwarded-Encrypted: i=1; AJvYcCVwPdnHwaMDq8Im8ebCfJYA4y0oGrj4eiL5kyW3jS/8Mysc+GPG0XP3Bekegb6fo8InnTbG/iYIFQ0xSeg=@vger.kernel.org X-Gm-Message-State: AOJu0YwwjoJeUrqKEhfEKGYDUT1zcdhdedbACvWA1qDwp1kdqtSDVZh3 lgfb7A05aiuTok4BWFHjwcXomdSwWrk4sPjPdfenTybUejHdtS8oeq0eAIRhbkD0KAy0fWkmOD1 B4A2NSA== X-Google-Smtp-Source: AGHT+IG0ACjh5v1BdYeFIcD3V5KQ4zlkmsCPXOqqSoWYQXF5vuFOVCPays425Fktw8AfQPdBjC0LnFZYxo9l X-Received: from pjv6.prod.google.com ([2002:a17:90b:5646:b0:2fa:15aa:4d2b]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2e03:b0:2fc:c262:ef4b with SMTP id 98e67ed59e1d1-3087bb6f117mr1141740a91.18.1744931280745; Thu, 17 Apr 2025 16:08:00 -0700 (PDT) Date: Thu, 17 Apr 2025 16:07:24 -0700 In-Reply-To: <20250417230740.86048-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: <20250417230740.86048-1-irogers@google.com> X-Mailer: git-send-email 2.49.0.805.g082f7c87e0-goog Message-ID: <20250417230740.86048-4-irogers@google.com> Subject: [PATCH v4 03/19] perf capstone: Move capstone functionality into its own file 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Capstone disassembly support was split between disasm.c and print_insn.c. Move support out of these files into capstone.[ch] and remove include capstone/capstone.h from those files. As disassembly routines can fail, make failure the only option without HAVE_LIBCAPSTONE_SUPPORT. For simplicity's sake, duplicate the read_symbol utility function. The intent with moving capstone support into a single file is that dynamic support, using dlopen for libcapstone, can be added in later patches. This can potentially always succeed or fail, so relying on ifdefs isn't sufficient. Using dlopen is a useful option to minimize the perf tools dependencies and potentially size. Signed-off-by: Ian Rogers --- tools/perf/builtin-script.c | 2 - tools/perf/util/Build | 1 + tools/perf/util/capstone.c | 536 +++++++++++++++++++++++++++++++++++ tools/perf/util/capstone.h | 24 ++ tools/perf/util/disasm.c | 356 +---------------------- tools/perf/util/print_insn.c | 117 +------- 6 files changed, 569 insertions(+), 467 deletions(-) create mode 100644 tools/perf/util/capstone.c create mode 100644 tools/perf/util/capstone.h diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c index 9b16df881af8..9ae89349947a 100644 --- a/tools/perf/builtin-script.c +++ b/tools/perf/builtin-script.c @@ -1222,7 +1222,6 @@ static int any_dump_insn(struct evsel *evsel __maybe_= unused, u8 *inbuf, int inlen, int *lenp, FILE *fp) { -#ifdef HAVE_LIBCAPSTONE_SUPPORT if (PRINT_FIELD(BRSTACKDISASM)) { int printed =3D fprintf_insn_asm(x->machine, x->thread, x->cpumode, x->i= s64bit, (uint8_t *)inbuf, inlen, ip, lenp, @@ -1231,7 +1230,6 @@ static int any_dump_insn(struct evsel *evsel __maybe_= unused, if (printed > 0) return printed; } -#endif return fprintf(fp, "%s", dump_insn(x, ip, inbuf, inlen, lenp)); } =20 diff --git a/tools/perf/util/Build b/tools/perf/util/Build index 946bce6628f3..1300b42cd748 100644 --- a/tools/perf/util/Build +++ b/tools/perf/util/Build @@ -8,6 +8,7 @@ perf-util-y +=3D block-info.o perf-util-y +=3D block-range.o perf-util-y +=3D build-id.o perf-util-y +=3D cacheline.o +perf-util-y +=3D capstone.o perf-util-y +=3D config.o perf-util-y +=3D copyfile.o perf-util-y +=3D ctype.o diff --git a/tools/perf/util/capstone.c b/tools/perf/util/capstone.c new file mode 100644 index 000000000000..c0a6d94ebc18 --- /dev/null +++ b/tools/perf/util/capstone.c @@ -0,0 +1,536 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "capstone.h" +#include "annotate.h" +#include "addr_location.h" +#include "debug.h" +#include "disasm.h" +#include "dso.h" +#include "machine.h" +#include "map.h" +#include "namespaces.h" +#include "print_insn.h" +#include "symbol.h" +#include "thread.h" +#include +#include + +#ifdef HAVE_LIBCAPSTONE_SUPPORT +#include +#endif + +#ifdef HAVE_LIBCAPSTONE_SUPPORT +static int capstone_init(struct machine *machine, csh *cs_handle, bool is6= 4, + bool disassembler_style) +{ + cs_arch arch; + cs_mode mode; + + if (machine__is(machine, "x86_64") && is64) { + arch =3D CS_ARCH_X86; + mode =3D CS_MODE_64; + } else if (machine__normalized_is(machine, "x86")) { + arch =3D CS_ARCH_X86; + mode =3D CS_MODE_32; + } else if (machine__normalized_is(machine, "arm64")) { + arch =3D CS_ARCH_ARM64; + mode =3D CS_MODE_ARM; + } else if (machine__normalized_is(machine, "arm")) { + arch =3D CS_ARCH_ARM; + mode =3D CS_MODE_ARM + CS_MODE_V8; + } else if (machine__normalized_is(machine, "s390")) { + arch =3D CS_ARCH_SYSZ; + mode =3D CS_MODE_BIG_ENDIAN; + } else { + return -1; + } + + if (cs_open(arch, mode, cs_handle) !=3D CS_ERR_OK) { + pr_warning_once("cs_open failed\n"); + return -1; + } + + if (machine__normalized_is(machine, "x86")) { + /* + * In case of using capstone_init while symbol__disassemble + * setting CS_OPT_SYNTAX_ATT depends if disassembler_style opts + * is set via annotation args + */ + if (disassembler_style) + cs_option(*cs_handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_ATT); + /* + * Resolving address operands to symbols is implemented + * on x86 by investigating instruction details. + */ + cs_option(*cs_handle, CS_OPT_DETAIL, CS_OPT_ON); + } + + return 0; +} +#endif + +#ifdef HAVE_LIBCAPSTONE_SUPPORT +static size_t print_insn_x86(struct thread *thread, u8 cpumode, cs_insn *i= nsn, + int print_opts, FILE *fp) +{ + struct addr_location al; + size_t printed =3D 0; + + if (insn->detail && insn->detail->x86.op_count =3D=3D 1) { + cs_x86_op *op =3D &insn->detail->x86.operands[0]; + + addr_location__init(&al); + if (op->type =3D=3D X86_OP_IMM && + thread__find_symbol(thread, cpumode, op->imm, &al)) { + printed +=3D fprintf(fp, "%s ", insn[0].mnemonic); + printed +=3D symbol__fprintf_symname_offs(al.sym, &al, fp); + if (print_opts & PRINT_INSN_IMM_HEX) + printed +=3D fprintf(fp, " [%#" PRIx64 "]", op->imm); + addr_location__exit(&al); + return printed; + } + addr_location__exit(&al); + } + + printed +=3D fprintf(fp, "%s %s", insn[0].mnemonic, insn[0].op_str); + return printed; +} +#endif + + +ssize_t capstone__fprintf_insn_asm(struct machine *machine __maybe_unused, + struct thread *thread __maybe_unused, + u8 cpumode __maybe_unused, bool is64bit __maybe_unused, + const uint8_t *code __maybe_unused, + size_t code_size __maybe_unused, + uint64_t ip __maybe_unused, int *lenp __maybe_unused, + int print_opts __maybe_unused, FILE *fp __maybe_unused) +{ +#ifdef HAVE_LIBCAPSTONE_SUPPORT + size_t printed; + cs_insn *insn; + csh cs_handle; + size_t count; + int ret; + + /* TODO: Try to initiate capstone only once but need a proper place. */ + ret =3D capstone_init(machine, &cs_handle, is64bit, true); + if (ret < 0) + return ret; + + count =3D cs_disasm(cs_handle, code, code_size, ip, 1, &insn); + if (count > 0) { + if (machine__normalized_is(machine, "x86")) + printed =3D print_insn_x86(thread, cpumode, &insn[0], print_opts, fp); + else + printed =3D fprintf(fp, "%s %s", insn[0].mnemonic, insn[0].op_str); + if (lenp) + *lenp =3D insn->size; + cs_free(insn, count); + } else { + printed =3D -1; + } + + cs_close(&cs_handle); + return printed; +#else + return -1; +#endif +} + +#ifdef HAVE_LIBCAPSTONE_SUPPORT +static int open_capstone_handle(struct annotate_args *args, bool is_64bit,= csh *handle) +{ + struct annotation_options *opt =3D args->options; + cs_mode mode =3D is_64bit ? CS_MODE_64 : CS_MODE_32; + + /* TODO: support more architectures */ + if (!arch__is(args->arch, "x86")) + return -1; + + if (cs_open(CS_ARCH_X86, mode, handle) !=3D CS_ERR_OK) + return -1; + + if (!opt->disassembler_style || + !strcmp(opt->disassembler_style, "att")) + cs_option(*handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_ATT); + + /* + * Resolving address operands to symbols is implemented + * on x86 by investigating instruction details. + */ + cs_option(*handle, CS_OPT_DETAIL, CS_OPT_ON); + + return 0; +} +#endif + +#ifdef HAVE_LIBCAPSTONE_SUPPORT +static void print_capstone_detail(cs_insn *insn, char *buf, size_t len, + struct annotate_args *args, u64 addr) +{ + int i; + struct map *map =3D args->ms.map; + struct symbol *sym; + + /* TODO: support more architectures */ + if (!arch__is(args->arch, "x86")) + return; + + if (insn->detail =3D=3D NULL) + return; + + for (i =3D 0; i < insn->detail->x86.op_count; i++) { + cs_x86_op *op =3D &insn->detail->x86.operands[i]; + u64 orig_addr; + + if (op->type !=3D X86_OP_MEM) + continue; + + /* only print RIP-based global symbols for now */ + if (op->mem.base !=3D X86_REG_RIP) + continue; + + /* get the target address */ + orig_addr =3D addr + insn->size + op->mem.disp; + addr =3D map__objdump_2mem(map, orig_addr); + + if (dso__kernel(map__dso(map))) { + /* + * The kernel maps can be splitted into sections, + * let's find the map first and the search the symbol. + */ + map =3D maps__find(map__kmaps(map), addr); + if (map =3D=3D NULL) + continue; + } + + /* convert it to map-relative address for search */ + addr =3D map__map_ip(map, addr); + + sym =3D map__find_symbol(map, addr); + if (sym =3D=3D NULL) + continue; + + if (addr =3D=3D sym->start) { + scnprintf(buf, len, "\t# %"PRIx64" <%s>", + orig_addr, sym->name); + } else { + scnprintf(buf, len, "\t# %"PRIx64" <%s+%#"PRIx64">", + orig_addr, sym->name, addr - sym->start); + } + break; + } +} +#endif + +#ifdef HAVE_LIBCAPSTONE_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; +} +#endif + +#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) +{ +#ifdef HAVE_LIBCAPSTONE_SUPPORT + struct annotation *notes =3D symbol__annotation(sym); + struct map *map =3D args->ms.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; + csh handle; + cs_insn *insn =3D NULL; + char disasm_buf[512]; + struct disasm_line *dl; + + if (args->options->objdump_path) + return -1; + + buf =3D read_symbol(filename, map, sym, &len, &is_64bit); + if (buf =3D=3D NULL) + return -1; + + /* add the function address and name */ + scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:", + start, sym->name); + + args->offset =3D -1; + args->line =3D disasm_buf; + args->line_nr =3D 0; + args->fileloc =3D NULL; + args->ms.sym =3D sym; + + dl =3D disasm_line__new(args); + if (dl =3D=3D NULL) + goto err; + + annotation_line__add(&dl->al, ¬es->src->source); + + if (open_capstone_handle(args, is_64bit, &handle) < 0) + goto err; + + needs_cs_close =3D true; + + free_count =3D count =3D cs_disasm(handle, buf, len, start, len, &insn); + for (i =3D 0, offset =3D 0; i < count; i++) { + int printed; + + printed =3D 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 =3D offset; + args->line =3D disasm_buf; + + dl =3D disasm_line__new(args); + if (dl =3D=3D NULL) + goto err; + + annotation_line__add(&dl->al, ¬es->src->source); + + offset +=3D insn[i].size; + } + + /* It failed in the middle: probably due to unknown instructions */ + if (offset !=3D len) { + struct list_head *list =3D ¬es->src->source; + + /* Discard all lines and fallback to objdump */ + while (!list_empty(list)) { + dl =3D list_first_entry(list, struct disasm_line, al.node); + + list_del_init(&dl->al.node); + disasm_line__free(dl); + } + count =3D -1; + } + +out: + if (needs_cs_close) { + cs_close(&handle); + if (free_count > 0) + cs_free(insn, free_count); + } + free(buf); + return count < 0 ? count : 0; + +err: + if (needs_cs_close) { + struct disasm_line *tmp; + + /* + * It probably failed in the middle of the above loop. + * Release any resources it might add. + */ + list_for_each_entry_safe(dl, tmp, ¬es->src->source, al.node) { + list_del(&dl->al.node); + disasm_line__free(dl); + } + } + count =3D -1; + goto out; +#else + return -1; +#endif +} + +int symbol__disassemble_capstone_powerpc(const char *filename __maybe_unus= ed, + struct symbol *sym __maybe_unused, + struct annotate_args *args __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); + struct nscookie nsc; + u64 start =3D map__rip_2objdump(map, sym->start); + u64 end =3D map__rip_2objdump(map, sym->end); + u64 len =3D end - start; + u64 offset; + int i, fd, count; + bool is_64bit =3D false; + bool needs_cs_close =3D false; + u8 *buf =3D NULL; + struct find_file_offset_data data =3D { + .ip =3D start, + }; + csh handle; + char disasm_buf[512]; + struct disasm_line *dl; + u32 *line; + bool disassembler_style =3D false; + + if (args->options->objdump_path) + return -1; + + nsinfo__mountns_enter(dso__nsinfo(dso), &nsc); + fd =3D open(filename, O_RDONLY); + nsinfo__mountns_exit(&nsc); + if (fd < 0) + return -1; + + if (file__read_maps(fd, /*exe=3D*/true, find_file_offset, &data, + &is_64bit) =3D=3D 0) + goto err; + + if (!args->options->disassembler_style || + !strcmp(args->options->disassembler_style, "att")) + disassembler_style =3D true; + + if (capstone_init(maps__machine(args->ms.maps), &handle, is_64bit, disass= embler_style) < 0) + goto err; + + needs_cs_close =3D true; + + 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; + + line =3D (u32 *)buf; + + /* add the function address and name */ + scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:", + start, sym->name); + + args->offset =3D -1; + args->line =3D disasm_buf; + args->line_nr =3D 0; + args->fileloc =3D NULL; + args->ms.sym =3D sym; + + dl =3D disasm_line__new(args); + if (dl =3D=3D NULL) + goto err; + + annotation_line__add(&dl->al, ¬es->src->source); + + /* + * TODO: enable disassm for powerpc + * count =3D cs_disasm(handle, buf, len, start, len, &insn); + * + * For now, only binary code is saved in disassembled line + * to be used in "type" and "typeoff" sort keys. Each raw code + * is 32 bit instruction. So use "len/4" to get the number of + * entries. + */ + count =3D len/4; + + for (i =3D 0, offset =3D 0; i < count; i++) { + args->offset =3D offset; + sprintf(args->line, "%x", line[i]); + + dl =3D disasm_line__new(args); + if (dl =3D=3D NULL) + break; + + annotation_line__add(&dl->al, ¬es->src->source); + + offset +=3D 4; + } + + /* It failed in the middle */ + if (offset !=3D len) { + struct list_head *list =3D ¬es->src->source; + + /* Discard all lines and fallback to objdump */ + while (!list_empty(list)) { + dl =3D list_first_entry(list, struct disasm_line, al.node); + + list_del_init(&dl->al.node); + disasm_line__free(dl); + } + count =3D -1; + } + +out: + if (needs_cs_close) + cs_close(&handle); + free(buf); + return count < 0 ? count : 0; + +err: + if (fd >=3D 0) + close(fd); + count =3D -1; + goto out; +#else + return -1; +#endif +} diff --git a/tools/perf/util/capstone.h b/tools/perf/util/capstone.h new file mode 100644 index 000000000000..0f030ea034b6 --- /dev/null +++ b/tools/perf/util/capstone.h @@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __PERF_CAPSTONE_H +#define __PERF_CAPSTONE_H + +#include +#include +#include +#include +#include + +struct annotate_args; +struct machine; +struct symbol; +struct thread; + +ssize_t capstone__fprintf_insn_asm(struct machine *machine, struct thread = *thread, u8 cpumode, + bool is64bit, const uint8_t *code, size_t code_size, + uint64_t ip, int *lenp, int print_opts, FILE *fp); +int symbol__disassemble_capstone(const char *filename, struct symbol *sym, + struct annotate_args *args); +int symbol__disassemble_capstone_powerpc(const char *filename, struct symb= ol *sym, + struct annotate_args *args); + +#endif /* __PERF_CAPSTONE_H */ diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c index 8f0eb56c6fc6..85401fc04b2e 100644 --- a/tools/perf/util/disasm.c +++ b/tools/perf/util/disasm.c @@ -14,6 +14,7 @@ #include "annotate.h" #include "annotate-data.h" #include "build-id.h" +#include "capstone.h" #include "debug.h" #include "disasm.h" #include "disasm_bpf.h" @@ -1330,39 +1331,7 @@ static int dso__disassemble_filename(struct dso *dso= , char *filename, size_t fil return 0; } =20 -#ifdef HAVE_LIBCAPSTONE_SUPPORT -#include - -int capstone_init(struct machine *machine, csh *cs_handle, bool is64, bool= disassembler_style); - -static int open_capstone_handle(struct annotate_args *args, bool is_64bit, - csh *handle) -{ - struct annotation_options *opt =3D args->options; - cs_mode mode =3D is_64bit ? CS_MODE_64 : CS_MODE_32; - - /* TODO: support more architectures */ - if (!arch__is(args->arch, "x86")) - return -1; - - if (cs_open(CS_ARCH_X86, mode, handle) !=3D CS_ERR_OK) - return -1; - - if (!opt->disassembler_style || - !strcmp(opt->disassembler_style, "att")) - cs_option(*handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_ATT); - - /* - * Resolving address operands to symbols is implemented - * on x86 by investigating instruction details. - */ - cs_option(*handle, CS_OPT_DETAIL, CS_OPT_ON); - - return 0; -} -#endif - -#if defined(HAVE_LIBCAPSTONE_SUPPORT) || defined(HAVE_LIBLLVM_SUPPORT) +#if defined(HAVE_LIBLLVM_SUPPORT) struct find_file_offset_data { u64 ip; u64 offset; @@ -1428,322 +1397,6 @@ read_symbol(const char *filename, struct map *map, = struct symbol *sym, } #endif =20 -#if !defined(HAVE_LIBCAPSTONE_SUPPORT) || !defined(HAVE_LIBLLVM_SUPPORT) -static void symbol__disassembler_missing(const char *disassembler, const c= har *filename, - struct symbol *sym) -{ - pr_debug("The %s disassembler isn't linked in for %s in %s\n", - disassembler, sym->name, filename); -} -#endif - -#ifdef HAVE_LIBCAPSTONE_SUPPORT -static void print_capstone_detail(cs_insn *insn, char *buf, size_t len, - struct annotate_args *args, u64 addr) -{ - int i; - struct map *map =3D args->ms.map; - struct symbol *sym; - - /* TODO: support more architectures */ - if (!arch__is(args->arch, "x86")) - return; - - if (insn->detail =3D=3D NULL) - return; - - for (i =3D 0; i < insn->detail->x86.op_count; i++) { - cs_x86_op *op =3D &insn->detail->x86.operands[i]; - u64 orig_addr; - - if (op->type !=3D X86_OP_MEM) - continue; - - /* only print RIP-based global symbols for now */ - if (op->mem.base !=3D X86_REG_RIP) - continue; - - /* get the target address */ - orig_addr =3D addr + insn->size + op->mem.disp; - addr =3D map__objdump_2mem(map, orig_addr); - - if (dso__kernel(map__dso(map))) { - /* - * The kernel maps can be splitted into sections, - * let's find the map first and the search the symbol. - */ - map =3D maps__find(map__kmaps(map), addr); - if (map =3D=3D NULL) - continue; - } - - /* convert it to map-relative address for search */ - addr =3D map__map_ip(map, addr); - - sym =3D map__find_symbol(map, addr); - if (sym =3D=3D NULL) - continue; - - if (addr =3D=3D sym->start) { - scnprintf(buf, len, "\t# %"PRIx64" <%s>", - orig_addr, sym->name); - } else { - scnprintf(buf, len, "\t# %"PRIx64" <%s+%#"PRIx64">", - orig_addr, sym->name, addr - sym->start); - } - break; - } -} - -static int symbol__disassemble_capstone_powerpc(char *filename, struct sym= bol *sym, - struct annotate_args *args) -{ - struct annotation *notes =3D symbol__annotation(sym); - struct map *map =3D args->ms.map; - 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); - u64 len =3D end - start; - u64 offset; - int i, fd, count; - bool is_64bit =3D false; - bool needs_cs_close =3D false; - u8 *buf =3D NULL; - struct find_file_offset_data data =3D { - .ip =3D start, - }; - csh handle; - char disasm_buf[512]; - struct disasm_line *dl; - u32 *line; - bool disassembler_style =3D false; - - if (args->options->objdump_path) - return -1; - - nsinfo__mountns_enter(dso__nsinfo(dso), &nsc); - fd =3D open(filename, O_RDONLY); - nsinfo__mountns_exit(&nsc); - if (fd < 0) - return -1; - - if (file__read_maps(fd, /*exe=3D*/true, find_file_offset, &data, - &is_64bit) =3D=3D 0) - goto err; - - if (!args->options->disassembler_style || - !strcmp(args->options->disassembler_style, "att")) - disassembler_style =3D true; - - if (capstone_init(maps__machine(args->ms.maps), &handle, is_64bit, disass= embler_style) < 0) - goto err; - - needs_cs_close =3D true; - - 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; - - line =3D (u32 *)buf; - - /* add the function address and name */ - scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:", - start, sym->name); - - args->offset =3D -1; - args->line =3D disasm_buf; - args->line_nr =3D 0; - args->fileloc =3D NULL; - args->ms.sym =3D sym; - - dl =3D disasm_line__new(args); - if (dl =3D=3D NULL) - goto err; - - annotation_line__add(&dl->al, ¬es->src->source); - - /* - * TODO: enable disassm for powerpc - * count =3D cs_disasm(handle, buf, len, start, len, &insn); - * - * For now, only binary code is saved in disassembled line - * to be used in "type" and "typeoff" sort keys. Each raw code - * is 32 bit instruction. So use "len/4" to get the number of - * entries. - */ - count =3D len/4; - - for (i =3D 0, offset =3D 0; i < count; i++) { - args->offset =3D offset; - sprintf(args->line, "%x", line[i]); - - dl =3D disasm_line__new(args); - if (dl =3D=3D NULL) - break; - - annotation_line__add(&dl->al, ¬es->src->source); - - offset +=3D 4; - } - - /* It failed in the middle */ - if (offset !=3D len) { - struct list_head *list =3D ¬es->src->source; - - /* Discard all lines and fallback to objdump */ - while (!list_empty(list)) { - dl =3D list_first_entry(list, struct disasm_line, al.node); - - list_del_init(&dl->al.node); - disasm_line__free(dl); - } - count =3D -1; - } - -out: - if (needs_cs_close) - cs_close(&handle); - free(buf); - return count < 0 ? count : 0; - -err: - if (fd >=3D 0) - close(fd); - count =3D -1; - goto out; -} - -static int symbol__disassemble_capstone(char *filename, struct symbol *sym, - struct annotate_args *args) -{ - struct annotation *notes =3D symbol__annotation(sym); - struct map *map =3D args->ms.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; - csh handle; - cs_insn *insn =3D NULL; - char disasm_buf[512]; - struct disasm_line *dl; - - if (args->options->objdump_path) - return -1; - - buf =3D read_symbol(filename, map, sym, &len, &is_64bit); - if (buf =3D=3D NULL) - return -1; - - /* add the function address and name */ - scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:", - start, sym->name); - - args->offset =3D -1; - args->line =3D disasm_buf; - args->line_nr =3D 0; - args->fileloc =3D NULL; - args->ms.sym =3D sym; - - dl =3D disasm_line__new(args); - if (dl =3D=3D NULL) - goto err; - - annotation_line__add(&dl->al, ¬es->src->source); - - if (open_capstone_handle(args, is_64bit, &handle) < 0) - goto err; - - needs_cs_close =3D true; - - free_count =3D count =3D cs_disasm(handle, buf, len, start, len, &insn); - for (i =3D 0, offset =3D 0; i < count; i++) { - int printed; - - printed =3D 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 =3D offset; - args->line =3D disasm_buf; - - dl =3D disasm_line__new(args); - if (dl =3D=3D NULL) - goto err; - - annotation_line__add(&dl->al, ¬es->src->source); - - offset +=3D insn[i].size; - } - - /* It failed in the middle: probably due to unknown instructions */ - if (offset !=3D len) { - struct list_head *list =3D ¬es->src->source; - - /* Discard all lines and fallback to objdump */ - while (!list_empty(list)) { - dl =3D list_first_entry(list, struct disasm_line, al.node); - - list_del_init(&dl->al.node); - disasm_line__free(dl); - } - count =3D -1; - } - -out: - if (needs_cs_close) { - cs_close(&handle); - if (free_count > 0) - cs_free(insn, free_count); - } - free(buf); - return count < 0 ? count : 0; - -err: - if (needs_cs_close) { - struct disasm_line *tmp; - - /* - * It probably failed in the middle of the above loop. - * Release any resources it might add. - */ - list_for_each_entry_safe(dl, tmp, ¬es->src->source, al.node) { - list_del(&dl->al.node); - disasm_line__free(dl); - } - } - count =3D -1; - goto out; -} -#else // HAVE_LIBCAPSTONE_SUPPORT -static int symbol__disassemble_capstone(char *filename, struct symbol *sym, - struct annotate_args *args __maybe_unused) -{ - symbol__disassembler_missing("capstone", filename, sym); - return -1; -} - -static int symbol__disassemble_capstone_powerpc(char *filename, struct sym= bol *sym, - struct annotate_args *args __maybe_unused) -{ - symbol__disassembler_missing("capstone powerpc", filename, sym); - return -1; -} -#endif // HAVE_LIBCAPSTONE_SUPPORT - static int symbol__disassemble_raw(char *filename, struct symbol *sym, struct annotate_args *args) { @@ -2011,10 +1664,11 @@ static int symbol__disassemble_llvm(char *filename,= struct symbol *sym, return ret; } #else // HAVE_LIBLLVM_SUPPORT -static int symbol__disassemble_llvm(char *filename, struct symbol *sym, +static int symbol__disassemble_llvm(const char *filename, struct symbol *s= ym, struct annotate_args *args __maybe_unused) { - symbol__disassembler_missing("LLVM", filename, sym); + pr_debug("The LLVM disassembler isn't linked in for %s in %s\n", + sym->name, filename); return -1; } #endif // HAVE_LIBLLVM_SUPPORT diff --git a/tools/perf/util/print_insn.c b/tools/perf/util/print_insn.c index a33a7726422d..02e6fbb8ca04 100644 --- a/tools/perf/util/print_insn.c +++ b/tools/perf/util/print_insn.c @@ -7,6 +7,7 @@ #include #include #include +#include "capstone.h" #include "debug.h" #include "sample.h" #include "symbol.h" @@ -29,84 +30,6 @@ size_t sample__fprintf_insn_raw(struct perf_sample *samp= le, FILE *fp) return printed; } =20 -#ifdef HAVE_LIBCAPSTONE_SUPPORT -#include - -int capstone_init(struct machine *machine, csh *cs_handle, bool is64, bool= disassembler_style); - -int capstone_init(struct machine *machine, csh *cs_handle, bool is64, bool= disassembler_style) -{ - cs_arch arch; - cs_mode mode; - - if (machine__is(machine, "x86_64") && is64) { - arch =3D CS_ARCH_X86; - mode =3D CS_MODE_64; - } else if (machine__normalized_is(machine, "x86")) { - arch =3D CS_ARCH_X86; - mode =3D CS_MODE_32; - } else if (machine__normalized_is(machine, "arm64")) { - arch =3D CS_ARCH_ARM64; - mode =3D CS_MODE_ARM; - } else if (machine__normalized_is(machine, "arm")) { - arch =3D CS_ARCH_ARM; - mode =3D CS_MODE_ARM + CS_MODE_V8; - } else if (machine__normalized_is(machine, "s390")) { - arch =3D CS_ARCH_SYSZ; - mode =3D CS_MODE_BIG_ENDIAN; - } else { - return -1; - } - - if (cs_open(arch, mode, cs_handle) !=3D CS_ERR_OK) { - pr_warning_once("cs_open failed\n"); - return -1; - } - - if (machine__normalized_is(machine, "x86")) { - /* - * In case of using capstone_init while symbol__disassemble - * setting CS_OPT_SYNTAX_ATT depends if disassembler_style opts - * is set via annotation args - */ - if (disassembler_style) - cs_option(*cs_handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_ATT); - /* - * Resolving address operands to symbols is implemented - * on x86 by investigating instruction details. - */ - cs_option(*cs_handle, CS_OPT_DETAIL, CS_OPT_ON); - } - - return 0; -} - -static size_t print_insn_x86(struct thread *thread, u8 cpumode, cs_insn *i= nsn, - int print_opts, FILE *fp) -{ - struct addr_location al; - size_t printed =3D 0; - - if (insn->detail && insn->detail->x86.op_count =3D=3D 1) { - cs_x86_op *op =3D &insn->detail->x86.operands[0]; - - addr_location__init(&al); - if (op->type =3D=3D X86_OP_IMM && - thread__find_symbol(thread, cpumode, op->imm, &al)) { - printed +=3D fprintf(fp, "%s ", insn[0].mnemonic); - printed +=3D symbol__fprintf_symname_offs(al.sym, &al, fp); - if (print_opts & PRINT_INSN_IMM_HEX) - printed +=3D fprintf(fp, " [%#" PRIx64 "]", op->imm); - addr_location__exit(&al); - return printed; - } - addr_location__exit(&al); - } - - printed +=3D fprintf(fp, "%s %s", insn[0].mnemonic, insn[0].op_str); - return printed; -} - static bool is64bitip(struct machine *machine, struct addr_location *al) { const struct dso *dso =3D al->map ? map__dso(al->map) : NULL; @@ -123,32 +46,8 @@ ssize_t fprintf_insn_asm(struct machine *machine, struc= t thread *thread, u8 cpum bool is64bit, const uint8_t *code, size_t code_size, uint64_t ip, int *lenp, int print_opts, FILE *fp) { - size_t printed; - cs_insn *insn; - csh cs_handle; - size_t count; - int ret; - - /* TODO: Try to initiate capstone only once but need a proper place. */ - ret =3D capstone_init(machine, &cs_handle, is64bit, true); - if (ret < 0) - return ret; - - count =3D cs_disasm(cs_handle, code, code_size, ip, 1, &insn); - if (count > 0) { - if (machine__normalized_is(machine, "x86")) - printed =3D print_insn_x86(thread, cpumode, &insn[0], print_opts, fp); - else - printed =3D fprintf(fp, "%s %s", insn[0].mnemonic, insn[0].op_str); - if (lenp) - *lenp =3D insn->size; - cs_free(insn, count); - } else { - printed =3D -1; - } - - cs_close(&cs_handle); - return printed; + return capstone__fprintf_insn_asm(machine, thread, cpumode, is64bit, code= , code_size, + ip, lenp, print_opts, fp); } =20 size_t sample__fprintf_insn_asm(struct perf_sample *sample, struct thread = *thread, @@ -166,13 +65,3 @@ size_t sample__fprintf_insn_asm(struct perf_sample *sam= ple, struct thread *threa =20 return printed; } -#else -size_t sample__fprintf_insn_asm(struct perf_sample *sample __maybe_unused, - struct thread *thread __maybe_unused, - struct machine *machine __maybe_unused, - FILE *fp __maybe_unused, - struct addr_location *al __maybe_unused) -{ - return 0; -} -#endif --=20 2.49.0.805.g082f7c87e0-goog From nobody Fri Dec 19 06:33:53 2025 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.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 7774021C9F8 for ; Thu, 17 Apr 2025 23:08:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744931287; cv=none; b=j8TdLnrQrFMQMOBAdsmZxr5sq3k45eg5MhzyQLPL41m1FlCtskc7ZJllFBQLUoK6FlN4gO7XW+9mwn1iCByTygQLPEe9Ig14ip0YTsiU8oxctpwX9SSQI9VB9st/K7q2rV1pXn/KkFHjPT/ZrrnNOifOkGGUESfsj0cWFYweFTg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744931287; c=relaxed/simple; bh=t+ntL80qL11+3EBK9O44jcINqqGlvCxoSAZ55KLGctM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=mbDmzcgj/0eysW2eOthczp4XHmm30ZlOY67gLTFANodQ4+Dsm8PPJONwjmKygl8KI/Nm18mbS1vEQjHWEs2pIFT1QHv2w+mBmr0AFij3oC22/zOOkLHn9RnNxiifS/4U1KcBtvXSewyx/uWszxJ+KABLOC+Eb2Eq9soh91ROVKc= 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=1axL6Yur; arc=none smtp.client-ip=209.85.214.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--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="1axL6Yur" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-229170fbe74so12680915ad.2 for ; Thu, 17 Apr 2025 16:08:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1744931283; x=1745536083; 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=0E7hS9KDjP6uZ+cmfu2yXjA1yQ1cg3V+TX7Cp4lgD00=; b=1axL6Yur1qyxYGUcPir40wL8IRVs0Cps1W0PFXhHESV3tfRdbh9qE1KEZWbr8lifJy vf1kidwbOJQPdofCm75MJxrjCgDaCqwn/xWnGVm7KxxKbwbXv+f/mm+AZ0zT8XD0j7JZ KtppSt54HNCOEDCgQZlPQIWYp5W1zrEXyXdXVHLYKVtTX2uwckqdwxk6Be7T3uvHTLsQ avbBn+qKx18N9MhHe8Qn3JwL64hcE8Z5NxnECtwpl4d0kMgkIvxkGUIk6Bmo3gcdw760 OmJUAkNQMI0J+/WQEFG82oic+M4ydN1+lDp12KcSPio424+M80vOwjV4Av85ph0zMDS5 T+hw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744931283; x=1745536083; 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=0E7hS9KDjP6uZ+cmfu2yXjA1yQ1cg3V+TX7Cp4lgD00=; b=G5psYlR4z49dat+yzFI+j2gPaYTsAE4SPqSpXc7Ph8x9idBFauATjH8gcy/l8oHQxQ u+CmGFh5KbqjnDcrhCmELY6B2hDElbj0TzM782mL7A2X9SSfpXuwUrS6O9gCIdhO5SZ7 R5eJxIm+OOKMbEZXsLIGdAzkpuh7sPFJsKXRRabxsRIsLpRcuhlkxhgsZ4Lu8bBags4v NZ4EI8vduWuJ+1H6LQDJ98uWuzo0xSX+7zMMVr2fwwD10q9LQDPhv5/6wyBY56zDM1RW vzd8vnXPDuJJFySCit67uj065nwli3IBrJ/YApojnDqdv/wZ4toY6ljYdgX1MQ7bZKS6 wtQw== X-Forwarded-Encrypted: i=1; AJvYcCVWfjaYkVuAFTin7GW1QkcGxYWL6UC59N61z6d+ifa+nWnR/h6JylUI0SwVAP52mRs7UmIjhtrO7zZ4rRQ=@vger.kernel.org X-Gm-Message-State: AOJu0YxchFOGK6CpoLE1/nNLZmTyrSgncNkMLkE0pF/q9JphHm/Hl759 pDhwIDuuIo6FtHuUybdXfFvlQaP55Ws+Zv2qZimaMYZFXPqSrMST4IP/4f+uLqexEDR/KhslI3w 5NyyFQw== X-Google-Smtp-Source: AGHT+IG3bWnMv5YUd8oeAlUYk6S6NArRHk1ZCDSWQSMFRvkxVgVW3EXKKPeuxKYykAnGQLs3gYl4iJ2e7ibg X-Received: from plfp5.prod.google.com ([2002:a17:902:e745:b0:226:453a:f2db]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:fc50:b0:224:23ab:b88b with SMTP id d9443c01a7336-22c535734c2mr8550965ad.8.1744931282970; Thu, 17 Apr 2025 16:08:02 -0700 (PDT) Date: Thu, 17 Apr 2025 16:07:25 -0700 In-Reply-To: <20250417230740.86048-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: <20250417230740.86048-1-irogers@google.com> X-Mailer: git-send-email 2.49.0.805.g082f7c87e0-goog Message-ID: <20250417230740.86048-5-irogers@google.com> Subject: [PATCH v4 04/19] perf llvm: Move llvm functionality into its own file 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" LLVM disassembly support was in disasm.c and addr2line support in srcline.c. Move support out of these files into llvm.[ch] and remove LLVM includes from those files. As disassembl routines can fail, make failure the only option without HAVE_LIBLLVM_SUPPORT. For simplicity's sake, duplicate the read_symbol utility function. The intent with moving LLVM support into a single file is that dynamic support, using dlopen for libllvm, can be added in later patches. This can potentially always succeed or fail, so relying on ifdefs isn't sufficient. Using dlopen is a useful option to minimize the perf tools dependencies and potentially size. Signed-off-by: Ian Rogers --- tools/perf/util/Build | 1 + tools/perf/util/disasm.c | 260 +----------------------------- tools/perf/util/disasm.h | 2 + tools/perf/util/llvm.c | 326 ++++++++++++++++++++++++++++++++++++++ tools/perf/util/llvm.h | 24 +++ tools/perf/util/srcline.c | 65 ++------ tools/perf/util/srcline.h | 6 + 7 files changed, 373 insertions(+), 311 deletions(-) create mode 100644 tools/perf/util/llvm.c create mode 100644 tools/perf/util/llvm.h diff --git a/tools/perf/util/Build b/tools/perf/util/Build index 1300b42cd748..caea11286fc9 100644 --- a/tools/perf/util/Build +++ b/tools/perf/util/Build @@ -26,6 +26,7 @@ perf-util-y +=3D evswitch.o perf-util-y +=3D find_bit.o perf-util-y +=3D get_current_dir_name.o perf-util-y +=3D levenshtein.o +perf-util-y +=3D llvm.o perf-util-y +=3D mmap.o perf-util-y +=3D memswap.o perf-util-y +=3D parse-events.o diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c index 85401fc04b2e..73343ae5fc96 100644 --- a/tools/perf/util/disasm.c +++ b/tools/perf/util/disasm.c @@ -22,6 +22,7 @@ #include "dwarf-regs.h" #include "env.h" #include "evsel.h" +#include "llvm.h" #include "map.h" #include "maps.h" #include "namespaces.h" @@ -50,7 +51,6 @@ static int call__scnprintf(struct ins *ins, char *bf, siz= e_t size, static void ins__sort(struct arch *arch); static int disasm_line__parse(char *line, const char **namep, char **rawp); static int disasm_line__parse_powerpc(struct disasm_line *dl, struct annot= ate_args *args); -static char *expand_tabs(char *line, char **storage, size_t *storage_len); =20 static __attribute__((constructor)) void symbol__init_regexpr(void) { @@ -1331,72 +1331,6 @@ static int dso__disassemble_filename(struct dso *dso= , char *filename, size_t fil return 0; } =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 - static int symbol__disassemble_raw(char *filename, struct symbol *sym, struct annotate_args *args) { @@ -1483,202 +1417,12 @@ static int symbol__disassemble_raw(char *filename,= struct symbol *sym, goto out; } =20 -#ifdef HAVE_LIBLLVM_SUPPORT -#include -#include -#include "util/llvm-c-helpers.h" - -struct symbol_lookup_storage { - u64 branch_addr; - u64 pcrel_load_addr; -}; - -/* - * 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 - * it puts quotation marks around what we return), but we use this as a hi= nt - * that there is a branch or PC-relative address in the expression that we - * should add some textual annotation for after the instruction. The caller - * will use this information to add the actual annotation. - */ -static const char * -symbol_lookup_callback(void *disinfo, uint64_t value, - uint64_t *ref_type, - uint64_t address __maybe_unused, - const char **ref __maybe_unused) -{ - struct symbol_lookup_storage *storage =3D disinfo; - - if (*ref_type =3D=3D LLVMDisassembler_ReferenceType_In_Branch) - storage->branch_addr =3D value; - else if (*ref_type =3D=3D LLVMDisassembler_ReferenceType_In_PCrel_Load) - storage->pcrel_load_addr =3D value; - *ref_type =3D LLVMDisassembler_ReferenceType_InOut_None; - return NULL; -} - -static int symbol__disassemble_llvm(char *filename, struct symbol *sym, - struct annotate_args *args) -{ - 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); - u8 *buf; - u64 len; - u64 pc; - bool is_64bit; - char triplet[64]; - char disasm_buf[2048]; - size_t disasm_len; - struct disasm_line *dl; - LLVMDisasmContextRef disasm =3D NULL; - struct symbol_lookup_storage storage; - char *line_storage =3D NULL; - size_t line_storage_len =3D 0; - int ret =3D -1; - - if (args->options->objdump_path) - return -1; - - LLVMInitializeAllTargetInfos(); - LLVMInitializeAllTargetMCs(); - LLVMInitializeAllDisassemblers(); - - buf =3D read_symbol(filename, map, sym, &len, &is_64bit); - if (buf =3D=3D NULL) - return -1; - - if (arch__is(args->arch, "x86")) { - if (is_64bit) - scnprintf(triplet, sizeof(triplet), "x86_64-pc-linux"); - else - scnprintf(triplet, sizeof(triplet), "i686-pc-linux"); - } else { - scnprintf(triplet, sizeof(triplet), "%s-linux-gnu", - args->arch->name); - } - - disasm =3D LLVMCreateDisasm(triplet, &storage, 0, NULL, - symbol_lookup_callback); - if (disasm =3D=3D NULL) - goto err; - - if (args->options->disassembler_style && - !strcmp(args->options->disassembler_style, "intel")) - LLVMSetDisasmOptions(disasm, - LLVMDisassembler_Option_AsmPrinterVariant); - - /* - * This needs to be set after AsmPrinterVariant, due to a bug in LLVM; - * setting AsmPrinterVariant makes a new instruction printer, making it - * forget about the PrintImmHex flag (which is applied before if both - * are given to the same call). - */ - LLVMSetDisasmOptions(disasm, LLVMDisassembler_Option_PrintImmHex); - - /* add the function address and name */ - scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:", - start, sym->name); - - args->offset =3D -1; - args->line =3D disasm_buf; - args->line_nr =3D 0; - args->fileloc =3D NULL; - args->ms.sym =3D sym; - - dl =3D disasm_line__new(args); - if (dl =3D=3D NULL) - goto err; - - annotation_line__add(&dl->al, ¬es->src->source); - - pc =3D start; - for (u64 offset =3D 0; offset < len; ) { - unsigned int ins_len; - - storage.branch_addr =3D 0; - storage.pcrel_load_addr =3D 0; - - ins_len =3D LLVMDisasmInstruction(disasm, buf + offset, - len - offset, pc, - disasm_buf, sizeof(disasm_buf)); - if (ins_len =3D=3D 0) - goto err; - disasm_len =3D strlen(disasm_buf); - - if (storage.branch_addr !=3D 0) { - char *name =3D llvm_name_for_code(dso, filename, - storage.branch_addr); - if (name !=3D NULL) { - disasm_len +=3D scnprintf(disasm_buf + disasm_len, - sizeof(disasm_buf) - - disasm_len, - " <%s>", name); - free(name); - } - } - if (storage.pcrel_load_addr !=3D 0) { - char *name =3D llvm_name_for_data(dso, filename, - storage.pcrel_load_addr); - disasm_len +=3D scnprintf(disasm_buf + disasm_len, - sizeof(disasm_buf) - disasm_len, - " # %#"PRIx64, - storage.pcrel_load_addr); - if (name) { - disasm_len +=3D scnprintf(disasm_buf + disasm_len, - sizeof(disasm_buf) - - disasm_len, - " <%s>", name); - free(name); - } - } - - args->offset =3D offset; - args->line =3D expand_tabs(disasm_buf, &line_storage, - &line_storage_len); - args->line_nr =3D 0; - args->fileloc =3D NULL; - args->ms.sym =3D sym; - - llvm_addr2line(filename, pc, &args->fileloc, - (unsigned int *)&args->line_nr, false, NULL); - - dl =3D disasm_line__new(args); - if (dl =3D=3D NULL) - goto err; - - annotation_line__add(&dl->al, ¬es->src->source); - - free(args->fileloc); - pc +=3D ins_len; - offset +=3D ins_len; - } - - ret =3D 0; - -err: - LLVMDisasmDispose(disasm); - free(buf); - free(line_storage); - return ret; -} -#else // HAVE_LIBLLVM_SUPPORT -static int symbol__disassemble_llvm(const char *filename, struct symbol *s= ym, - struct annotate_args *args __maybe_unused) -{ - pr_debug("The LLVM disassembler isn't linked in for %s in %s\n", - sym->name, filename); - return -1; -} -#endif // HAVE_LIBLLVM_SUPPORT - /* * Possibly create a new version of line with tabs expanded. Returns the * existing or new line, storage is updated if a new line is allocated. If * allocation fails then NULL is returned. */ -static char *expand_tabs(char *line, char **storage, size_t *storage_len) +char *expand_tabs(char *line, char **storage, size_t *storage_len) { size_t i, src, dst, len, new_storage_len, num_tabs; char *new_line; diff --git a/tools/perf/util/disasm.h b/tools/perf/util/disasm.h index c135db2416b5..2cb4e1a6bd30 100644 --- a/tools/perf/util/disasm.h +++ b/tools/perf/util/disasm.h @@ -128,4 +128,6 @@ int disasm_line__scnprintf(struct disasm_line *dl, char= *bf, size_t size, =20 int symbol__disassemble(struct symbol *sym, struct annotate_args *args); =20 +char *expand_tabs(char *line, char **storage, size_t *storage_len); + #endif /* __PERF_UTIL_DISASM_H */ diff --git a/tools/perf/util/llvm.c b/tools/perf/util/llvm.c new file mode 100644 index 000000000000..ddc737194692 --- /dev/null +++ b/tools/perf/util/llvm.c @@ -0,0 +1,326 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "llvm.h" +#include "annotate.h" +#include "debug.h" +#include "dso.h" +#include "map.h" +#include "namespaces.h" +#include "srcline.h" +#include "symbol.h" +#include +#include +#include + +#ifdef HAVE_LIBLLVM_SUPPORT +#include "llvm-c-helpers.h" +#include +#include +#endif + +#ifdef HAVE_LIBLLVM_SUPPORT +static void free_llvm_inline_frames(struct llvm_a2l_frame *inline_frames, + int num_frames) +{ + if (inline_frames !=3D NULL) { + for (int i =3D 0; i < num_frames; ++i) { + zfree(&inline_frames[i].filename); + zfree(&inline_frames[i].funcname); + } + zfree(&inline_frames); + } +} +#endif + +int llvm__addr2line(const char *dso_name __maybe_unused, u64 addr __maybe_= unused, + char **file __maybe_unused, unsigned int *line __maybe_unused, + struct dso *dso __maybe_unused, bool unwind_inlines __maybe_unused, + struct inline_node *node __maybe_unused, struct symbol *sym __maybe= _unused) +{ +#ifdef HAVE_LIBLLVM_SUPPORT + struct llvm_a2l_frame *inline_frames =3D NULL; + int num_frames =3D llvm_addr2line(dso_name, addr, file, line, + node && unwind_inlines, &inline_frames); + + if (num_frames =3D=3D 0 || !inline_frames) { + /* Error, or we didn't want inlines. */ + return num_frames; + } + + for (int i =3D 0; i < num_frames; ++i) { + struct symbol *inline_sym =3D + new_inline_sym(dso, sym, inline_frames[i].funcname); + char *srcline =3D NULL; + + if (inline_frames[i].filename) { + srcline =3D + srcline_from_fileline(inline_frames[i].filename, + inline_frames[i].line); + } + if (inline_list__append(inline_sym, srcline, node) !=3D 0) { + free_llvm_inline_frames(inline_frames, num_frames); + return 0; + } + } + free_llvm_inline_frames(inline_frames, num_frames); + + return num_frames; +#else + return -1; +#endif +} + +void dso__free_a2l_llvm(struct dso *dso __maybe_unused) +{ + /* Nothing to free. */ +} + + +#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 + * it puts quotation marks around what we return), but we use this as a hi= nt + * that there is a branch or PC-relative address in the expression that we + * should add some textual annotation for after the instruction. The caller + * will use this information to add the actual annotation. + */ +#ifdef HAVE_LIBLLVM_SUPPORT +struct symbol_lookup_storage { + u64 branch_addr; + u64 pcrel_load_addr; +}; + +static const char * +symbol_lookup_callback(void *disinfo, uint64_t value, + uint64_t *ref_type, + uint64_t address __maybe_unused, + const char **ref __maybe_unused) +{ + struct symbol_lookup_storage *storage =3D disinfo; + + if (*ref_type =3D=3D LLVMDisassembler_ReferenceType_In_Branch) + storage->branch_addr =3D value; + else if (*ref_type =3D=3D LLVMDisassembler_ReferenceType_In_PCrel_Load) + storage->pcrel_load_addr =3D value; + *ref_type =3D LLVMDisassembler_ReferenceType_InOut_None; + return NULL; +} +#endif + +int symbol__disassemble_llvm(const char *filename, struct symbol *sym, + struct annotate_args *args __maybe_unused) +{ +#ifdef HAVE_LIBLLVM_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); + u8 *buf; + u64 len; + u64 pc; + bool is_64bit; + char triplet[64]; + char disasm_buf[2048]; + size_t disasm_len; + struct disasm_line *dl; + LLVMDisasmContextRef disasm =3D NULL; + struct symbol_lookup_storage storage; + char *line_storage =3D NULL; + size_t line_storage_len =3D 0; + int ret =3D -1; + + if (args->options->objdump_path) + return -1; + + LLVMInitializeAllTargetInfos(); + LLVMInitializeAllTargetMCs(); + LLVMInitializeAllDisassemblers(); + + buf =3D read_symbol(filename, map, sym, &len, &is_64bit); + if (buf =3D=3D NULL) + return -1; + + if (arch__is(args->arch, "x86")) { + if (is_64bit) + scnprintf(triplet, sizeof(triplet), "x86_64-pc-linux"); + else + scnprintf(triplet, sizeof(triplet), "i686-pc-linux"); + } else { + scnprintf(triplet, sizeof(triplet), "%s-linux-gnu", + args->arch->name); + } + + disasm =3D LLVMCreateDisasm(triplet, &storage, 0, NULL, + symbol_lookup_callback); + if (disasm =3D=3D NULL) + goto err; + + if (args->options->disassembler_style && + !strcmp(args->options->disassembler_style, "intel")) + LLVMSetDisasmOptions(disasm, + LLVMDisassembler_Option_AsmPrinterVariant); + + /* + * This needs to be set after AsmPrinterVariant, due to a bug in LLVM; + * setting AsmPrinterVariant makes a new instruction printer, making it + * forget about the PrintImmHex flag (which is applied before if both + * are given to the same call). + */ + LLVMSetDisasmOptions(disasm, LLVMDisassembler_Option_PrintImmHex); + + /* add the function address and name */ + scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:", + start, sym->name); + + args->offset =3D -1; + args->line =3D disasm_buf; + args->line_nr =3D 0; + args->fileloc =3D NULL; + args->ms.sym =3D sym; + + dl =3D disasm_line__new(args); + if (dl =3D=3D NULL) + goto err; + + annotation_line__add(&dl->al, ¬es->src->source); + + pc =3D start; + for (u64 offset =3D 0; offset < len; ) { + unsigned int ins_len; + + storage.branch_addr =3D 0; + storage.pcrel_load_addr =3D 0; + + ins_len =3D LLVMDisasmInstruction(disasm, buf + offset, + len - offset, pc, + disasm_buf, sizeof(disasm_buf)); + if (ins_len =3D=3D 0) + goto err; + disasm_len =3D strlen(disasm_buf); + + if (storage.branch_addr !=3D 0) { + char *name =3D llvm_name_for_code(dso, filename, + storage.branch_addr); + if (name !=3D NULL) { + disasm_len +=3D scnprintf(disasm_buf + disasm_len, + sizeof(disasm_buf) - + disasm_len, + " <%s>", name); + free(name); + } + } + if (storage.pcrel_load_addr !=3D 0) { + char *name =3D llvm_name_for_data(dso, filename, + storage.pcrel_load_addr); + disasm_len +=3D scnprintf(disasm_buf + disasm_len, + sizeof(disasm_buf) - disasm_len, + " # %#"PRIx64, + storage.pcrel_load_addr); + if (name) { + disasm_len +=3D scnprintf(disasm_buf + disasm_len, + sizeof(disasm_buf) - + disasm_len, + " <%s>", name); + free(name); + } + } + + args->offset =3D offset; + args->line =3D expand_tabs(disasm_buf, &line_storage, + &line_storage_len); + args->line_nr =3D 0; + args->fileloc =3D NULL; + args->ms.sym =3D sym; + + llvm_addr2line(filename, pc, &args->fileloc, + (unsigned int *)&args->line_nr, false, NULL); + + dl =3D disasm_line__new(args); + if (dl =3D=3D NULL) + goto err; + + annotation_line__add(&dl->al, ¬es->src->source); + + free(args->fileloc); + pc +=3D ins_len; + offset +=3D ins_len; + } + + ret =3D 0; + +err: + LLVMDisasmDispose(disasm); + free(buf); + free(line_storage); + return ret; +#else // HAVE_LIBLLVM_SUPPORT + pr_debug("The LLVM disassembler isn't linked in for %s in %s\n", + sym->name, filename); + return -1; +#endif +} diff --git a/tools/perf/util/llvm.h b/tools/perf/util/llvm.h new file mode 100644 index 000000000000..8aa19bb6b068 --- /dev/null +++ b/tools/perf/util/llvm.h @@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __PERF_LLVM_H +#define __PERF_LLVM_H + +#include +#include + +struct annotate_args; +struct dso; +struct inline_node; +struct symbol; + +int llvm__addr2line(const char *dso_name, u64 addr, + char **file, unsigned int *line, struct dso *dso, + bool unwind_inlines, struct inline_node *node, + struct symbol *sym); + + +void dso__free_a2l_llvm(struct dso *dso); + +int symbol__disassemble_llvm(const char *filename, struct symbol *sym, + struct annotate_args *args); + +#endif /* __PERF_LLVM_H */ diff --git a/tools/perf/util/srcline.c b/tools/perf/util/srcline.c index f32d0d4f4bc9..26fd55455efd 100644 --- a/tools/perf/util/srcline.c +++ b/tools/perf/util/srcline.c @@ -17,9 +17,7 @@ #include "util/debug.h" #include "util/callchain.h" #include "util/symbol_conf.h" -#ifdef HAVE_LIBLLVM_SUPPORT -#include "util/llvm-c-helpers.h" -#endif +#include "llvm.h" #include "srcline.h" #include "string2.h" #include "symbol.h" @@ -49,8 +47,7 @@ static const char *srcline_dso_name(struct dso *dso) return dso_name; } =20 -static int inline_list__append(struct symbol *symbol, char *srcline, - struct inline_node *node) +int inline_list__append(struct symbol *symbol, char *srcline, struct inlin= e_node *node) { struct inline_list *ilist; =20 @@ -77,7 +74,7 @@ static const char *gnu_basename(const char *path) return base ? base + 1 : path; } =20 -static char *srcline_from_fileline(const char *file, unsigned int line) +char *srcline_from_fileline(const char *file, unsigned int line) { char *srcline; =20 @@ -93,9 +90,9 @@ static char *srcline_from_fileline(const char *file, unsi= gned int line) return srcline; } =20 -static struct symbol *new_inline_sym(struct dso *dso, - struct symbol *base_sym, - const char *funcname) +struct symbol *new_inline_sym(struct dso *dso, + struct symbol *base_sym, + const char *funcname) { struct symbol *inline_sym; char *demangled =3D NULL; @@ -135,58 +132,20 @@ static struct symbol *new_inline_sym(struct dso *dso, #define MAX_INLINE_NEST 1024 =20 #ifdef HAVE_LIBLLVM_SUPPORT - -static void free_llvm_inline_frames(struct llvm_a2l_frame *inline_frames, - int num_frames) -{ - if (inline_frames !=3D NULL) { - for (int i =3D 0; i < num_frames; ++i) { - zfree(&inline_frames[i].filename); - zfree(&inline_frames[i].funcname); - } - zfree(&inline_frames); - } -} +#include "llvm.h" =20 static int addr2line(const char *dso_name, u64 addr, char **file, unsigned int *line, struct dso *dso, - bool unwind_inlines, struct inline_node *node, - struct symbol *sym) + bool unwind_inlines, struct inline_node *node, + struct symbol *sym) { - struct llvm_a2l_frame *inline_frames =3D NULL; - int num_frames =3D llvm_addr2line(dso_name, addr, file, line, - node && unwind_inlines, &inline_frames); - - if (num_frames =3D=3D 0 || !inline_frames) { - /* Error, or we didn't want inlines. */ - return num_frames; - } - - for (int i =3D 0; i < num_frames; ++i) { - struct symbol *inline_sym =3D - new_inline_sym(dso, sym, inline_frames[i].funcname); - char *srcline =3D NULL; - - if (inline_frames[i].filename) { - srcline =3D - srcline_from_fileline(inline_frames[i].filename, - inline_frames[i].line); - } - if (inline_list__append(inline_sym, srcline, node) !=3D 0) { - free_llvm_inline_frames(inline_frames, num_frames); - return 0; - } - } - free_llvm_inline_frames(inline_frames, num_frames); - - return num_frames; + return llvm__addr2line(dso_name, addr, file, line, dso, unwind_inlines, n= ode, sym); } =20 -void dso__free_a2l(struct dso *dso __maybe_unused) +void dso__free_a2l(struct dso *dso) { - /* Nothing to free. */ + dso__free_a2l_llvm(dso); } - #elif defined(HAVE_LIBBFD_SUPPORT) =20 /* diff --git a/tools/perf/util/srcline.h b/tools/perf/util/srcline.h index 75010d39ea28..80c20169e250 100644 --- a/tools/perf/util/srcline.h +++ b/tools/perf/util/srcline.h @@ -55,4 +55,10 @@ struct inline_node *inlines__tree_find(struct rb_root_ca= ched *tree, u64 addr); /* delete all nodes within the tree of inline_node s */ void inlines__tree_delete(struct rb_root_cached *tree); =20 +int inline_list__append(struct symbol *symbol, char *srcline, struct inlin= e_node *node); +char *srcline_from_fileline(const char *file, unsigned int line); +struct symbol *new_inline_sym(struct dso *dso, + struct symbol *base_sym, + const char *funcname); + #endif /* PERF_SRCLINE_H */ --=20 2.49.0.805.g082f7c87e0-goog From nobody Fri Dec 19 06:33:53 2025 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.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 D21062236F3 for ; Thu, 17 Apr 2025 23:08:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744931287; cv=none; b=iXsPrsVzgl/+Z/npddIBli/DbPeyi6iQaWOzM56om/oSrDg2V4B8qmEacEruTgG8JQ8WuKFIZ50uNI4778htZ6omsh3dwK2pKgIZXYoQfXytMFyRxC78gH1pAAdwRTTH+S/x387ZfpkMwu4zwNfZQILNg1CHXi967ApGVAsMImc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744931287; c=relaxed/simple; bh=LRLZdT5hOWA0SXwX+weES0oI2ZWCXSQ23XqvPbFD5aY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=rD5RFfCugqoGtFU9jUNgCVy4b8uRZCUxiD5BiobsnqzoL8FVFBjgxL4HK7O6vNZD4tIvTPne25lAuwapgV7sdWaYv1r3ANHnXbQE/pYFu4fHaFgbNU8JXfNMucf3Wi2ZYCo+YB8WSBdJavR76FNBpbFDmPQOqlSRMcLdKqh1xJU= 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=RvtyyBGc; arc=none smtp.client-ip=209.85.214.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--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="RvtyyBGc" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-225ab228a37so12138125ad.2 for ; Thu, 17 Apr 2025 16:08:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1744931285; x=1745536085; 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=C+v/iA3QAz14KzOUNaxFJb9QhGqk7bNH0V+P+kCM29I=; b=RvtyyBGcYB9JfwXtI52aNH7Xuq0llXIi4DgzFPp7sylcC+vrWjB+FnXLRe0rKJNaM8 qYFZWXsK4qycA54s04DeKrmTOSxbPZ03AYIaGZ8SyTYK6x/X/hTQISEihpo6dJLrkxJT McTsCEEval9miCnqz6FwZfR4VgJ6jX9GiCfDfg3F2jifmppeLhc3RtuMFR3eM0nHoiti sHrXcI5Jj+UMevq523ZrvhoHPLf14FUnBwQ3YGdq6tW1IRtRp4JPT55zkFNKJPS16jfp u92eT012Y2YYpxNfFs4q/tyFPu59+k0iyJzkq+V8z7Qf7Oi3OPRivlprq+2Y9CkhkUVs 5veA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744931285; x=1745536085; 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=C+v/iA3QAz14KzOUNaxFJb9QhGqk7bNH0V+P+kCM29I=; b=knl0GxH4PMEt4CB9z8gPHfZbucBd1qf+2f9pDzZsxDto1cgRZMzi8p9gmLIEb5+DtC jDedKHCwI04h9PmHy1FIIPQxQMdBClKUE0afq8/XLlGD9HWswjdxwH2wWzUhLkTwJOtW ALut8+GZcsDbJHNwnOUxUHy8EhAvTXytZWUWLivNhfatRk2I5MgQBKVCHXtLta9LODS+ gqvObPprq2sJA4SBE6ZR6w2V99rrcTBruJ+KuDQ8dsgaaessBhAWx3QwyILPCF2I6vfk RAokqh+bDu7SJCduJMlPAs6GdvHA36HTTUmJrijRXZiCzUImyJefJr/JIu2kD5nZ/M76 WsDg== X-Forwarded-Encrypted: i=1; AJvYcCXGEdbC3OsHqiG0pbYP4Mup12l4D1r4872sGy7Oe9TkBAtoSp9+o5J1g0bpIDNdctkZ82biENcEewy/F4Q=@vger.kernel.org X-Gm-Message-State: AOJu0YxNpAxv57AZrSzGC+/hj/q42hIgwGHkmj/EPJfVudr/2h8srhYa P+X7rSjtrcFXxrPgigi3geGiYW7DkQ5Juwei4qp8dvLKWKT8lRrbxoi8cGIJ+2wUty82v70hBXo SBmpsKw== X-Google-Smtp-Source: AGHT+IG7ZoF5x6T+kA85kaMATkQC66Ptfdqe25x2XYwjUy5ZUI7EMa3SwBgwuXxfCajqgxBSSEbaedI1jfQj X-Received: from plrp11.prod.google.com ([2002:a17:902:b08b:b0:223:432c:56d4]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:e80f:b0:223:fb3a:8647 with SMTP id d9443c01a7336-22c53607c39mr9603665ad.41.1744931285231; Thu, 17 Apr 2025 16:08:05 -0700 (PDT) Date: Thu, 17 Apr 2025 16:07:26 -0700 In-Reply-To: <20250417230740.86048-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: <20250417230740.86048-1-irogers@google.com> X-Mailer: git-send-email 2.49.0.805.g082f7c87e0-goog Message-ID: <20250417230740.86048-6-irogers@google.com> Subject: [PATCH v4 05/19] perf capstone: Remove open_capstone_handle 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" open_capstone_handle is similar to capstone_init and used only by symbol__disassemble_capstone. symbol__disassemble_capstone_powerpc already uses capstone_init, transition symbol__disassemble_capstone and eliminate open_capstone_handle. Signed-off-by: Ian Rogers --- tools/perf/util/capstone.c | 34 ++++++---------------------------- 1 file changed, 6 insertions(+), 28 deletions(-) diff --git a/tools/perf/util/capstone.c b/tools/perf/util/capstone.c index c0a6d94ebc18..c9845e4d8781 100644 --- a/tools/perf/util/capstone.c +++ b/tools/perf/util/capstone.c @@ -137,33 +137,6 @@ ssize_t capstone__fprintf_insn_asm(struct machine *mac= hine __maybe_unused, #endif } =20 -#ifdef HAVE_LIBCAPSTONE_SUPPORT -static int open_capstone_handle(struct annotate_args *args, bool is_64bit,= csh *handle) -{ - struct annotation_options *opt =3D args->options; - cs_mode mode =3D is_64bit ? CS_MODE_64 : CS_MODE_32; - - /* TODO: support more architectures */ - if (!arch__is(args->arch, "x86")) - return -1; - - if (cs_open(CS_ARCH_X86, mode, handle) !=3D CS_ERR_OK) - return -1; - - if (!opt->disassembler_style || - !strcmp(opt->disassembler_style, "att")) - cs_option(*handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_ATT); - - /* - * Resolving address operands to symbols is implemented - * on x86 by investigating instruction details. - */ - cs_option(*handle, CS_OPT_DETAIL, CS_OPT_ON); - - return 0; -} -#endif - #ifdef HAVE_LIBCAPSTONE_SUPPORT static void print_capstone_detail(cs_insn *insn, char *buf, size_t len, struct annotate_args *args, u64 addr) @@ -309,6 +282,7 @@ int symbol__disassemble_capstone(const char *filename _= _maybe_unused, cs_insn *insn =3D NULL; char disasm_buf[512]; struct disasm_line *dl; + bool disassembler_style =3D false; =20 if (args->options->objdump_path) return -1; @@ -333,7 +307,11 @@ int symbol__disassemble_capstone(const char *filename = __maybe_unused, =20 annotation_line__add(&dl->al, ¬es->src->source); =20 - if (open_capstone_handle(args, is_64bit, &handle) < 0) + if (!args->options->disassembler_style || + !strcmp(args->options->disassembler_style, "att")) + disassembler_style =3D true; + + if (capstone_init(maps__machine(args->ms.maps), &handle, is_64bit, disass= embler_style) < 0) goto err; =20 needs_cs_close =3D true; --=20 2.49.0.805.g082f7c87e0-goog From nobody Fri Dec 19 06:33:53 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 787F422371C for ; Thu, 17 Apr 2025 23:08:07 +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=1744931289; cv=none; b=I+zU1uXwms7OayaEYUHS3xl6sD1raQAng8gcOlKd5wyklML24Tzb3iYa9FPYSRwg6UNJeQH63+tJrmm23jzNknapcZNfTbKqjsbFUyrM1i3mQFWgBWXPM6gYp4PGK5PQTp2gkNqUb/7qXzdt+g2pkZFU+yi1GmIcN8B39udAcrE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744931289; c=relaxed/simple; bh=EMQfvbols4hJj92Bs0k+5Eg4kdx0vzL8MhtqLEbwqic=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=s3NbCJ+I2IwqTIXcq5r6J73/w4iOHS8giNr8yVdxbAf/qIm1YuU3Kcvdjo0rwYH+ark7zShHW+x0ntPKc8UFnqkLAAT58WrdX9cqa7FrqVA0sZv/PiYbkH3Hoy4eaiJ3Q7zON3rYVBHbJkW/JUSgohvT8olk3S/gSpG3HflBwFQ= 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=jNltjK+x; 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="jNltjK+x" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-22647ff3cf5so12029715ad.0 for ; Thu, 17 Apr 2025 16:08:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1744931287; x=1745536087; 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=hQcz1UF+kjInwfqSqtylZUr+LdC1+I0bC5jm3+HCs7A=; b=jNltjK+x/E7jli/aogRxP5m4kZnPWsEcTa7voRrpIl9Cu4wbbhjIiwEfx79lHLUSFC 3a/Vq0AnX92TKqg7xK1VzqGocxQM0gsIjFi3ld6/cID4/GIP4XLLlwBdhc1oRmhaZWdB c4R0cmx3XNCNCt3J6c4CL8QjHC4W7NTq5KKL5ZGdH3AqQG0Wd5SAINDKN5x8jYXgThey 6Vw+oLVVUphC/Bn7//v1NsPQ3UxZ0U/eral1tYGvC3VrUBpUpB1co4irvEveWd6PdiUZ 5qFKI4On0YXnOIwAmZGjqJsanqVqgORQ+0cnUmoIhBvPt/gE7oixQyYQdRHZY7w7Nx8V NVxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744931287; x=1745536087; 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=hQcz1UF+kjInwfqSqtylZUr+LdC1+I0bC5jm3+HCs7A=; b=MOXMUuJOmuaMEoFVPj4Ou9+raksZPQF0SmNKQswDkPGZq37jSyRia2+s3A+Udb5psx l3w4P70Q8zb2hGU0IDbDGRQd/gC1JVePaO1IrT8svy83GLREEewEQxolsBnhZ1dRIq+K isfoNnlTjQd8vPpbwYD9GwDbgbwqMuPNR4tLZlrx1j5eEN5OihgSifgUBycn9jjvlT6Z DSFA8rEY8oIi6VRjaIrE/b5DqzEJlDNpTuCr6XK633rk4Uf5l2m4G8LsuEjaW5oM93II ayNvy+1q+rTach4Zd12vLph+70azKqTAvLpnyT2x+EQHw3CU8sIN4jEjjQFq99mGIfz8 /GvQ== X-Forwarded-Encrypted: i=1; AJvYcCVUeq+OwjvdmsI7yT1pvcvSRp8fkufaZkPCDuvUDV57+2C/Q0kk0yQWQJm7bKOCmAMVqXSTLRxLDgV7wkM=@vger.kernel.org X-Gm-Message-State: AOJu0YzFaiucXFiz+XxSMIDqAAaWXNGrUrJMvUDGV5++8BvgOGOZmpRj gvh8Uzdi3EQb/u43Y2jHRH9NaqWCjG2y6D+irL65yFkK1MhOXPmQ7T5KWJiSSfQl6LU9GugpMJ4 66VN2BA== X-Google-Smtp-Source: AGHT+IGtnA2Ty6vk//bBoP/R4p/u1iRvOkcPhnAT2jHDyEJ07a3dJikdE/OFpcusNXm2u0OvB/OPz3UU/NP2 X-Received: from plcy20.prod.google.com ([2002:a17:903:114:b0:220:bf5f:1984]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:ce01:b0:223:3bf6:7e6a with SMTP id d9443c01a7336-22c53581231mr11802915ad.12.1744931286983; Thu, 17 Apr 2025 16:08:06 -0700 (PDT) Date: Thu, 17 Apr 2025 16:07:27 -0700 In-Reply-To: <20250417230740.86048-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: <20250417230740.86048-1-irogers@google.com> X-Mailer: git-send-email 2.49.0.805.g082f7c87e0-goog Message-ID: <20250417230740.86048-7-irogers@google.com> Subject: [PATCH v4 06/19] perf capstone: Support for dlopen-ing libcapstone.so 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" If perf wasn't built against libcapstone, no HAVE_LIBCAPSTONE_SUPPORT, support dlopen-ing libcapstone.so and then calling the necessary functions by looking them up using dlsym. Reverse engineer the types in the API using pahole, adding only what's used in the perf code or necessary for the sake of struct size and alignment. Signed-off-by: Ian Rogers --- tools/perf/util/capstone.c | 287 ++++++++++++++++++++++++++++++++----- 1 file changed, 248 insertions(+), 39 deletions(-) diff --git a/tools/perf/util/capstone.c b/tools/perf/util/capstone.c index c9845e4d8781..8d65c7a55a8b 100644 --- a/tools/perf/util/capstone.c +++ b/tools/perf/util/capstone.c @@ -11,19 +11,249 @@ #include "print_insn.h" #include "symbol.h" #include "thread.h" +#include #include +#include #include =20 #ifdef HAVE_LIBCAPSTONE_SUPPORT #include +#else +typedef size_t csh; +enum cs_arch { + CS_ARCH_ARM =3D 0, + CS_ARCH_ARM64 =3D 1, + CS_ARCH_X86 =3D 3, + CS_ARCH_SYSZ =3D 6, +}; +enum cs_mode { + CS_MODE_ARM =3D 0, + CS_MODE_32 =3D 1 << 2, + CS_MODE_64 =3D 1 << 3, + CS_MODE_V8 =3D 1 << 6, + CS_MODE_BIG_ENDIAN =3D 1 << 31, +}; +enum cs_opt_type { + CS_OPT_SYNTAX =3D 1, + CS_OPT_DETAIL =3D 2, +}; +enum cs_opt_value { + CS_OPT_SYNTAX_ATT =3D 2, + CS_OPT_ON =3D 3, +}; +enum cs_err { + CS_ERR_OK =3D 0, + CS_ERR_HANDLE =3D 3, +}; +enum x86_op_type { + X86_OP_IMM =3D 2, + X86_OP_MEM =3D 3, +}; +enum x86_reg { + X86_REG_RIP =3D 41, +}; +typedef int32_t x86_avx_bcast; +struct x86_op_mem { + enum x86_reg segment; + enum x86_reg base; + enum x86_reg index; + int scale; + int64_t disp; +}; + +struct cs_x86_op { + enum x86_op_type type; + union { + enum x86_reg reg; + int64_t imm; + struct x86_op_mem mem; + }; + uint8_t size; + uint8_t access; + x86_avx_bcast avx_bcast; + bool avx_zero_opmask; +}; +struct cs_x86_encoding { + uint8_t modrm_offset; + uint8_t disp_offset; + uint8_t disp_size; + uint8_t imm_offset; + uint8_t imm_size; +}; +typedef int32_t x86_xop_cc; +typedef int32_t x86_sse_cc; +typedef int32_t x86_avx_cc; +typedef int32_t x86_avx_rm; +struct cs_x86 { + uint8_t prefix[4]; + uint8_t opcode[4]; + uint8_t rex; + uint8_t addr_size; + uint8_t modrm; + uint8_t sib; + int64_t disp; + enum x86_reg sib_index; + int8_t sib_scale; + enum x86_reg sib_base; + x86_xop_cc xop_cc; + x86_sse_cc sse_cc; + x86_avx_cc avx_cc; + bool avx_sae; + x86_avx_rm avx_rm; + union { + uint64_t eflags; + uint64_t fpu_flags; + }; + uint8_t op_count; + struct cs_x86_op operands[8]; + struct cs_x86_encoding encoding; +}; +struct cs_detail { + uint16_t regs_read[12]; + uint8_t regs_read_count; + uint16_t regs_write[20]; + uint8_t regs_write_count; + uint8_t groups[8]; + uint8_t groups_count; + + union { + struct cs_x86 x86; + }; +}; +struct cs_insn { + unsigned int id; + uint64_t address; + uint16_t size; + uint8_t bytes[16]; + char mnemonic[32]; + char op_str[160]; + struct cs_detail *detail; +}; +#endif + +#ifndef HAVE_LIBCAPSTONE_SUPPORT +static void *perf_cs_dll_handle(void) +{ + static bool dll_handle_init; + static void *dll_handle; + + if (!dll_handle_init) { + dll_handle_init =3D true; + dll_handle =3D dlopen("libcapstone.so", RTLD_LAZY); + if (!dll_handle) + pr_debug("dlopen failed for libcapstone.so\n"); + } + return dll_handle; +} +#endif + +static enum cs_err perf_cs_open(enum cs_arch arch, enum cs_mode mode, csh = *handle) +{ +#ifdef HAVE_LIBCAPSTONE_SUPPORT + return cs_open(arch, mode, handle); +#else + static bool fn_init; + static enum cs_err (*fn)(enum cs_arch arch, enum cs_mode mode, csh *handl= e); + + if (!fn_init) { + fn =3D dlsym(perf_cs_dll_handle(), "cs_open"); + if (!fn) + pr_debug("dlsym failed for cs_open\n"); + fn_init =3D true; + } + if (!fn) + return CS_ERR_HANDLE; + return fn(arch, mode, handle); +#endif +} + +static enum cs_err perf_cs_option(csh handle, enum cs_opt_type type, size_= t value) +{ +#ifdef HAVE_LIBCAPSTONE_SUPPORT + return cs_option(handle, type, value); +#else + static bool fn_init; + static enum cs_err (*fn)(csh handle, enum cs_opt_type type, size_t value); + + if (!fn_init) { + fn =3D dlsym(perf_cs_dll_handle(), "cs_option"); + if (!fn) + pr_debug("dlsym failed for cs_option\n"); + fn_init =3D true; + } + if (!fn) + return CS_ERR_HANDLE; + return fn(handle, type, value); +#endif +} + +static size_t perf_cs_disasm(csh handle, const uint8_t *code, size_t code_= size, + uint64_t address, size_t count, struct cs_insn **insn) +{ +#ifdef HAVE_LIBCAPSTONE_SUPPORT + return cs_disasm(handle, code, code_size, address, count, insn); +#else + static bool fn_init; + static enum cs_err (*fn)(csh handle, const uint8_t *code, size_t code_siz= e, + uint64_t address, size_t count, struct cs_insn **insn); + + if (!fn_init) { + fn =3D dlsym(perf_cs_dll_handle(), "cs_disasm"); + if (!fn) + pr_debug("dlsym failed for cs_disasm\n"); + fn_init =3D true; + } + if (!fn) + return CS_ERR_HANDLE; + return fn(handle, code, code_size, address, count, insn); #endif +} =20 +static void perf_cs_free(struct cs_insn *insn, size_t count) +{ #ifdef HAVE_LIBCAPSTONE_SUPPORT + cs_free(insn, count); +#else + static bool fn_init; + static void (*fn)(struct cs_insn *insn, size_t count); + + if (!fn_init) { + fn =3D dlsym(perf_cs_dll_handle(), "cs_free"); + if (!fn) + pr_debug("dlsym failed for cs_free\n"); + fn_init =3D true; + } + if (!fn) + return; + fn(insn, count); +#endif +} + +static enum cs_err perf_cs_close(csh *handle) +{ +#ifdef HAVE_LIBCAPSTONE_SUPPORT + return cs_close(handle); +#else + static bool fn_init; + static enum cs_err (*fn)(csh *handle); + + if (!fn_init) { + fn =3D dlsym(perf_cs_dll_handle(), "cs_close"); + if (!fn) + pr_debug("dlsym failed for cs_close\n"); + fn_init =3D true; + } + if (!fn) + return CS_ERR_HANDLE; + return fn(handle); +#endif +} + static int capstone_init(struct machine *machine, csh *cs_handle, bool is6= 4, bool disassembler_style) { - cs_arch arch; - cs_mode mode; + enum cs_arch arch; + enum cs_mode mode; =20 if (machine__is(machine, "x86_64") && is64) { arch =3D CS_ARCH_X86; @@ -44,7 +274,7 @@ static int capstone_init(struct machine *machine, csh *c= s_handle, bool is64, return -1; } =20 - if (cs_open(arch, mode, cs_handle) !=3D CS_ERR_OK) { + if (perf_cs_open(arch, mode, cs_handle) !=3D CS_ERR_OK) { pr_warning_once("cs_open failed\n"); return -1; } @@ -56,27 +286,25 @@ static int capstone_init(struct machine *machine, csh = *cs_handle, bool is64, * is set via annotation args */ if (disassembler_style) - cs_option(*cs_handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_ATT); + perf_cs_option(*cs_handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_ATT); /* * Resolving address operands to symbols is implemented * on x86 by investigating instruction details. */ - cs_option(*cs_handle, CS_OPT_DETAIL, CS_OPT_ON); + perf_cs_option(*cs_handle, CS_OPT_DETAIL, CS_OPT_ON); } =20 return 0; } -#endif =20 -#ifdef HAVE_LIBCAPSTONE_SUPPORT -static size_t print_insn_x86(struct thread *thread, u8 cpumode, cs_insn *i= nsn, +static size_t print_insn_x86(struct thread *thread, u8 cpumode, struct cs_= insn *insn, int print_opts, FILE *fp) { struct addr_location al; size_t printed =3D 0; =20 if (insn->detail && insn->detail->x86.op_count =3D=3D 1) { - cs_x86_op *op =3D &insn->detail->x86.operands[0]; + struct cs_x86_op *op =3D &insn->detail->x86.operands[0]; =20 addr_location__init(&al); if (op->type =3D=3D X86_OP_IMM && @@ -94,7 +322,6 @@ static size_t print_insn_x86(struct thread *thread, u8 c= pumode, cs_insn *insn, printed +=3D fprintf(fp, "%s %s", insn[0].mnemonic, insn[0].op_str); return printed; } -#endif =20 =20 ssize_t capstone__fprintf_insn_asm(struct machine *machine __maybe_unused, @@ -105,9 +332,8 @@ ssize_t capstone__fprintf_insn_asm(struct machine *mach= ine __maybe_unused, uint64_t ip __maybe_unused, int *lenp __maybe_unused, int print_opts __maybe_unused, FILE *fp __maybe_unused) { -#ifdef HAVE_LIBCAPSTONE_SUPPORT size_t printed; - cs_insn *insn; + struct cs_insn *insn; csh cs_handle; size_t count; int ret; @@ -117,7 +343,7 @@ ssize_t capstone__fprintf_insn_asm(struct machine *mach= ine __maybe_unused, if (ret < 0) return ret; =20 - count =3D cs_disasm(cs_handle, code, code_size, ip, 1, &insn); + count =3D perf_cs_disasm(cs_handle, code, code_size, ip, 1, &insn); if (count > 0) { if (machine__normalized_is(machine, "x86")) printed =3D print_insn_x86(thread, cpumode, &insn[0], print_opts, fp); @@ -125,20 +351,16 @@ ssize_t capstone__fprintf_insn_asm(struct machine *ma= chine __maybe_unused, printed =3D fprintf(fp, "%s %s", insn[0].mnemonic, insn[0].op_str); if (lenp) *lenp =3D insn->size; - cs_free(insn, count); + perf_cs_free(insn, count); } else { printed =3D -1; } =20 - cs_close(&cs_handle); + perf_cs_close(&cs_handle); return printed; -#else - return -1; -#endif } =20 -#ifdef HAVE_LIBCAPSTONE_SUPPORT -static void print_capstone_detail(cs_insn *insn, char *buf, size_t len, +static void print_capstone_detail(struct cs_insn *insn, char *buf, size_t = len, struct annotate_args *args, u64 addr) { int i; @@ -153,7 +375,7 @@ static void print_capstone_detail(cs_insn *insn, char *= buf, size_t len, return; =20 for (i =3D 0; i < insn->detail->x86.op_count; i++) { - cs_x86_op *op =3D &insn->detail->x86.operands[i]; + struct cs_x86_op *op =3D &insn->detail->x86.operands[i]; u64 orig_addr; =20 if (op->type !=3D X86_OP_MEM) @@ -194,9 +416,7 @@ static void print_capstone_detail(cs_insn *insn, char *= buf, size_t len, break; } } -#endif =20 -#ifdef HAVE_LIBCAPSTONE_SUPPORT struct find_file_offset_data { u64 ip; u64 offset; @@ -213,9 +433,7 @@ static int find_file_offset(u64 start, u64 len, u64 pgo= ff, void *arg) } return 0; } -#endif =20 -#ifdef HAVE_LIBCAPSTONE_SUPPORT static u8 * read_symbol(const char *filename, struct map *map, struct symbol *sym, u64 *len, bool *is_64bit) @@ -262,13 +480,11 @@ read_symbol(const char *filename, struct map *map, st= ruct symbol *sym, free(buf); return NULL; } -#endif =20 int symbol__disassemble_capstone(const char *filename __maybe_unused, struct symbol *sym __maybe_unused, struct annotate_args *args __maybe_unused) { -#ifdef HAVE_LIBCAPSTONE_SUPPORT struct annotation *notes =3D symbol__annotation(sym); struct map *map =3D args->ms.map; u64 start =3D map__rip_2objdump(map, sym->start); @@ -279,7 +495,7 @@ int symbol__disassemble_capstone(const char *filename _= _maybe_unused, bool needs_cs_close =3D false; u8 *buf =3D NULL; csh handle; - cs_insn *insn =3D NULL; + struct cs_insn *insn =3D NULL; char disasm_buf[512]; struct disasm_line *dl; bool disassembler_style =3D false; @@ -316,7 +532,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 perf_cs_disasm(handle, buf, len, start, len, &in= sn); for (i =3D 0, offset =3D 0; i < count; i++) { int printed; =20 @@ -355,9 +571,9 @@ int symbol__disassemble_capstone(const char *filename _= _maybe_unused, =20 out: if (needs_cs_close) { - cs_close(&handle); + perf_cs_close(&handle); if (free_count > 0) - cs_free(insn, free_count); + perf_cs_free(insn, free_count); } free(buf); return count < 0 ? count : 0; @@ -377,16 +593,12 @@ int symbol__disassemble_capstone(const char *filename= __maybe_unused, } count =3D -1; goto out; -#else - return -1; -#endif } =20 int symbol__disassemble_capstone_powerpc(const char *filename __maybe_unus= ed, struct symbol *sym __maybe_unused, struct annotate_args *args __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); @@ -499,7 +711,7 @@ int symbol__disassemble_capstone_powerpc(const char *fi= lename __maybe_unused, =20 out: if (needs_cs_close) - cs_close(&handle); + perf_cs_close(&handle); free(buf); return count < 0 ? count : 0; =20 @@ -508,7 +720,4 @@ int symbol__disassemble_capstone_powerpc(const char *fi= lename __maybe_unused, close(fd); count =3D -1; goto out; -#else - return -1; -#endif } --=20 2.49.0.805.g082f7c87e0-goog From nobody Fri Dec 19 06:33:53 2025 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.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 EBD07221737 for ; Thu, 17 Apr 2025 23:08:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744931293; cv=none; b=jlIVQj9eApvdeCNqFQTkpRHaw8cHf7g4BlErvQQM+gLW9T3xipJiUzbecw4yTw/ZtuRPl9B74f79cUZt3K8GnFcUdgeam+IIB24sBSUWVYr+LO/64jNdRjzLyXjbCsKesjJq9gyDXpk6KEJpGVXHFTsoOJUeP7QuuP44jRRLrYw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744931293; c=relaxed/simple; bh=2kA9gnE8rD7CPW+2ophufjj6uIzIXHADwFTtacAu1UE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=jxQxN/UT/Tm3s2ZDxC+H1w+Dpk2hae2YRn4oRyZk+ccJXOKmlnjWkVBJ/Pq+eh2vjijPehx2/imEITt2V7YtqGcQqdL8F0SXtNldSvnzGOFU7EEROYtgiOtYMpQWgD9u1JGasgev+g8vW9RXlpDmu/sNZoERPuI6fx1bTS3rc1s= 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=gvZdGYvA; arc=none smtp.client-ip=209.85.210.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="gvZdGYvA" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-736c1ea954fso717658b3a.3 for ; Thu, 17 Apr 2025 16:08:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1744931289; x=1745536089; 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=m0XtkP993D7IB3S/clw8lKOGY7fGAXAkx2d+Kur+inA=; b=gvZdGYvAQdnCGGMRchRZueQf7vMZRiWaVIOeXMst6giix1WNde3qd6P7gx7rtgG2ZZ l3ykx2dpDczXnoitKqfRy9F2IsZQAgCez5VbQdB1RlqbVnAHQgMMBozSQ2Gv0aSJpsGx T+v1ETjbKo3HD+WsUHAsEzuW3pWzeg4nADNPdZzUEM5/kmN3UHClg0qaAID8iaZr3KfT lV+7PMVL2KH44R4IbdsLZLQB9Ln6NpzcaFwC7tsGsmd4YgVR0oYJKAOWaniok15UNAHc /2nHEAzyB27cDVO1DOLYaV6C7KcR0dMdjeIV5XFw3cntMa+d060f5Y1g2kLcdv6sVHfB mBwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744931289; x=1745536089; 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=m0XtkP993D7IB3S/clw8lKOGY7fGAXAkx2d+Kur+inA=; b=cIwa/dyHaEJxa8UyBSNR+zfxOMDszfJ4x4wW/wWDSpmNrzgtkrNL8BVswttNLjTyoU QubnbcDWvS34FtwqdYPMx+cMRlVR5JMNR0k38BjaIzgy9J3QMJR6ZNtZcGfEd2jP6QGp joHPwhSyzpKgO4PHwkJtkyQgXaUpQ/C8Ld/uMwycQ7BIBAw/9A5xQ6pFz6HZ9fequy/H jtHS7fg0E8bdteEWhgLXB5pvGWw2Eu8BKF+lE0RudWcOVkz1AkyqZISYAX709xTRiPHs 4VUHDTKNEvEteK6kJysj8zfvXhoSvM/fSR9YD7JolLzx27ISzWmK/KKwPchmpPi4s1DP 5V+w== X-Forwarded-Encrypted: i=1; AJvYcCXs2px5yAn7OX9rQq8ZXU37t1ufYP8/4+rilJOvfLAYFOAe92k4MP4Xi+I6SjVWHAx+gya6a8lYq9vsQGU=@vger.kernel.org X-Gm-Message-State: AOJu0Yx83lfgpFwHnDQ/yL9DjCfGoi7995uf/fBSbmtf/gK3S3mLEcVW 9sqkbovydxxaQthL6nTIziq96ev11+Z6ajcSqqLvAEdpBdCXx8/eCm2GsmO0yWB5XkK1kqqz/e0 e3I9t5w== X-Google-Smtp-Source: AGHT+IEV7GQEnd2xc0CefomOH245hN7sWi4UHNjRdbF5iVweTUbUaurYmmbmQAgnAsGofBJoIelsv9htP2tr X-Received: from pfnp22.prod.google.com ([2002:aa7:8616:0:b0:736:39fa:2251]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:1145:b0:736:53bc:f1ab with SMTP id d2e1a72fcca58-73dc14d3225mr722095b3a.12.1744931289161; Thu, 17 Apr 2025 16:08:09 -0700 (PDT) Date: Thu, 17 Apr 2025 16:07:28 -0700 In-Reply-To: <20250417230740.86048-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: <20250417230740.86048-1-irogers@google.com> X-Mailer: git-send-email 2.49.0.805.g082f7c87e0-goog Message-ID: <20250417230740.86048-8-irogers@google.com> Subject: [PATCH v4 07/19] perf llvm: Support for dlopen-ing libLLVM.so 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" If perf wasn't built against libLLVM, no HAVE_LIBLLVM_SUPPORT, support dlopen-ing libLLVM.so and then calling the necessary functions by looking them up using dlsym. As the C++ code in llvm-c-helpers used for addr2line is problematic to call using dlsym, build that C++ code against libLLVM.so as a separate shared object, and support dynamic loading of it. This build option is enabled with LIBLLVM_DYNAMIC=3D1 Signed-off-by: Ian Rogers --- tools/perf/Makefile.config | 13 ++ tools/perf/Makefile.perf | 23 ++- tools/perf/tests/make | 2 + tools/perf/util/Build | 2 +- tools/perf/util/llvm-c-helpers.cpp | 113 +++++++++++- tools/perf/util/llvm.c | 271 +++++++++++++++++++++++++---- 6 files changed, 386 insertions(+), 38 deletions(-) diff --git a/tools/perf/Makefile.config b/tools/perf/Makefile.config index f31b240cd23e..d6d8c6b970e3 100644 --- a/tools/perf/Makefile.config +++ b/tools/perf/Makefile.config @@ -968,6 +968,19 @@ ifndef NO_LIBLLVM NO_LIBLLVM :=3D 1 endif endif +ifdef LIBLLVM_DYNAMIC + ifndef NO_LIBLLVM + $(error LIBLLVM_DYNAMIC should be used with NO_LIBLLVM) + endif + $(call feature_check,llvm-perf) + ifneq ($(feature-llvm-perf), 1) + $(warning LIBLLVM_DYNAMIC requires libLLVM.so which wasn't feature det= ected) + endif + CFLAGS +=3D -DHAVE_LIBLLVM_DYNAMIC + CFLAGS +=3D $(shell $(LLVM_CONFIG) --cflags) + CXXFLAGS +=3D -DHAVE_LIBLLVM_DYNAMIC + CXXFLAGS +=3D $(shell $(LLVM_CONFIG) --cxxflags) +endif =20 ifndef NO_DEMANGLE $(call feature_check,cxa-demangle) diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf index 979d4691221a..6833dda27ffb 100644 --- a/tools/perf/Makefile.perf +++ b/tools/perf/Makefile.perf @@ -424,6 +424,12 @@ ifndef NO_JVMTI PROGRAMS +=3D $(OUTPUT)$(LIBJVMTI) endif =20 +LIBPERF_LLVM =3D libperf-llvm.so + +ifdef LIBLLVM_DYNAMIC +PROGRAMS +=3D $(OUTPUT)$(LIBPERF_LLVM) +endif + DLFILTERS :=3D dlfilter-test-api-v0.so dlfilter-test-api-v2.so dlfilter-sh= ow-cycles.so DLFILTERS :=3D $(patsubst %,$(OUTPUT)dlfilters/%,$(DLFILTERS)) =20 @@ -996,6 +1002,16 @@ $(LIBSYMBOL)-clean: $(call QUIET_CLEAN, libsymbol) $(Q)$(RM) -r -- $(LIBSYMBOL_OUTPUT) =20 +ifdef LIBLLVM_DYNAMIC +LIBPERF_LLVM_CXXFLAGS :=3D $(call filter-out,-DHAVE_LIBLLVM_DYNAMIC,$(CXXF= LAGS)) -DHAVE_LIBLLVM_SUPPORT +LIBPERF_LLVM_LIBS =3D -L$(shell $(LLVM_CONFIG) --libdir) $(LIBLLVM) -lstdc= ++ + +$(OUTPUT)$(LIBPERF_LLVM): util/llvm-c-helpers.cpp + $(QUIET_LINK)$(CXX) $(LIBPERF_LLVM_CXXFLAGS) $(LIBPERF_LLVM_LIBS) -shared= -o $@ $< + +$(OUTPUT)perf: $(OUTPUT)$(LIBPERF_LLVM) +endif + help: @echo 'Perf make targets:' @echo ' doc - make *all* documentation (see below)' @@ -1097,6 +1113,11 @@ ifndef NO_JVMTI $(call QUIET_INSTALL, $(LIBJVMTI)) \ $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(libdir_SQ)'; \ $(INSTALL) $(OUTPUT)$(LIBJVMTI) '$(DESTDIR_SQ)$(libdir_SQ)'; +endif +ifdef LIBLLVM_DYNAMIC + $(call QUIET_INSTALL, $(LIBPERF_LLVM)) \ + $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(libdir_SQ)'; \ + $(INSTALL) $(OUTPUT)$(LIBPERF_LLVM) '$(DESTDIR_SQ)$(libdir_SQ)'; endif $(call QUIET_INSTALL, libexec) \ $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)' @@ -1278,7 +1299,7 @@ clean:: $(LIBAPI)-clean $(LIBBPF)-clean $(LIBSUBCMD)-= clean $(LIBSYMBOL)-clean $( -name '\.*.cmd' -delete -o -name '\.*.d' -delete -o -name '*.shellcheck_= log' -delete $(Q)$(RM) $(OUTPUT).config-detected $(call QUIET_CLEAN, core-progs) $(RM) $(ALL_PROGRAMS) perf perf-read-vdso= 32 \ - perf-read-vdsox32 $(OUTPUT)$(LIBJVMTI).so + perf-read-vdsox32 $(OUTPUT)$(LIBJVMTI) $(OUTPUT)$(LIBPERF_LLVM) $(call QUIET_CLEAN, core-gen) $(RM) *.spec *.pyc *.pyo */*.pyc */*.pyo= \ $(OUTPUT)common-cmds.h TAGS tags cscope* $(OUTPUT)PERF-VERSION-FILE \ $(OUTPUT)FEATURE-DUMP $(OUTPUT)util/*-bison* $(OUTPUT)util/*-flex* \ diff --git a/tools/perf/tests/make b/tools/perf/tests/make index 0ee94caf9ec1..44d76eacce49 100644 --- a/tools/perf/tests/make +++ b/tools/perf/tests/make @@ -93,6 +93,7 @@ make_libbpf_dynamic :=3D LIBBPF_DYNAMIC=3D1 make_no_libbpf_DEBUG :=3D NO_LIBBPF=3D1 DEBUG=3D1 make_no_libcrypto :=3D NO_LIBCRYPTO=3D1 make_no_libllvm :=3D NO_LIBLLVM=3D1 +make_libllvm_dynamic :=3D NO_LIBLLVM=3D1 LIBLLVM_DYNAMIC=3D1 make_with_babeltrace:=3D LIBBABELTRACE=3D1 make_with_coresight :=3D CORESIGHT=3D1 make_no_sdt :=3D NO_SDT=3D1 @@ -162,6 +163,7 @@ run +=3D make_no_libbpf run +=3D make_no_libbpf_DEBUG run +=3D make_no_libcrypto run +=3D make_no_libllvm +run +=3D make_libllvm_dynamic run +=3D make_no_sdt run +=3D make_no_syscall_tbl run +=3D make_with_babeltrace diff --git a/tools/perf/util/Build b/tools/perf/util/Build index caea11286fc9..861e8ed4ac49 100644 --- a/tools/perf/util/Build +++ b/tools/perf/util/Build @@ -27,6 +27,7 @@ perf-util-y +=3D find_bit.o perf-util-y +=3D get_current_dir_name.o perf-util-y +=3D levenshtein.o perf-util-y +=3D llvm.o +perf-util-y +=3D llvm-c-helpers.o perf-util-y +=3D mmap.o perf-util-y +=3D memswap.o perf-util-y +=3D parse-events.o @@ -240,7 +241,6 @@ perf-util-$(CONFIG_CXX_DEMANGLE) +=3D demangle-cxx.o perf-util-y +=3D demangle-ocaml.o perf-util-y +=3D demangle-java.o perf-util-y +=3D demangle-rust.o -perf-util-$(CONFIG_LIBLLVM) +=3D llvm-c-helpers.o =20 ifdef CONFIG_JITDUMP perf-util-$(CONFIG_LIBELF) +=3D jitdump.o diff --git a/tools/perf/util/llvm-c-helpers.cpp b/tools/perf/util/llvm-c-he= lpers.cpp index 004081bd12c9..5a6f76e6b705 100644 --- a/tools/perf/util/llvm-c-helpers.cpp +++ b/tools/perf/util/llvm-c-helpers.cpp @@ -5,17 +5,23 @@ * macros (e.g. noinline) that conflict with compiler builtins used * by LLVM. */ +#ifdef HAVE_LIBLLVM_SUPPORT #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-parameter" /* Needed for LLVM <= =3D 15 */ #include #include #pragma GCC diagnostic pop +#endif =20 +#if !defined(HAVE_LIBLLVM_SUPPORT) || defined(HAVE_LIBLLVM_DYNAMIC) +#include +#endif #include #include #include #include extern "C" { +#include "debug.h" #include } #include "llvm-c-helpers.h" @@ -23,14 +29,33 @@ extern "C" { extern "C" char *dso__demangle_sym(struct dso *dso, int kmodule, const char *elf_name= ); =20 +#ifdef HAVE_LIBLLVM_SUPPORT using namespace llvm; using llvm::symbolize::LLVMSymbolizer; +#endif + +#if !defined(HAVE_LIBLLVM_SUPPORT) && defined(HAVE_LIBLLVM_DYNAMIC) +static void *perf_llvm_c_helpers_dll_handle(void) +{ + static bool dll_handle_init; + static void *dll_handle; + + if (!dll_handle_init) { + dll_handle_init =3D true; + dll_handle =3D dlopen("libperf-llvm.so", RTLD_LAZY); + if (!dll_handle) + pr_debug("dlopen failed for libperf-llvm.so\n"); + } + return dll_handle; +} +#endif =20 /* * Allocate a static LLVMSymbolizer, which will live to the end of the pro= gram. * Unlike the bfd paths, LLVMSymbolizer has its own cache, so we do not ne= ed * to store anything in the dso struct. */ +#if defined(HAVE_LIBLLVM_SUPPORT) && !defined(HAVE_LIBLLVM_DYNAMIC) static LLVMSymbolizer *get_symbolizer() { static LLVMSymbolizer *instance =3D nullptr; @@ -49,8 +74,10 @@ static LLVMSymbolizer *get_symbolizer() } return instance; } +#endif =20 /* Returns 0 on error, 1 on success. */ +#if defined(HAVE_LIBLLVM_SUPPORT) && !defined(HAVE_LIBLLVM_DYNAMIC) static int extract_file_and_line(const DILineInfo &line_info, char **file, unsigned int *line) { @@ -69,13 +96,15 @@ static int extract_file_and_line(const DILineInfo &line= _info, char **file, *line =3D line_info.Line; return 1; } +#endif =20 extern "C" -int llvm_addr2line(const char *dso_name, u64 addr, - char **file, unsigned int *line, - bool unwind_inlines, - llvm_a2l_frame **inline_frames) +int llvm_addr2line(const char *dso_name __maybe_unused, u64 addr __maybe_u= nused, + char **file __maybe_unused, unsigned int *line __maybe_unused, + bool unwind_inlines __maybe_unused, + llvm_a2l_frame **inline_frames __maybe_unused) { +#if defined(HAVE_LIBLLVM_SUPPORT) && !defined(HAVE_LIBLLVM_DYNAMIC) LLVMSymbolizer *symbolizer =3D get_symbolizer(); object::SectionedAddress sectioned_addr =3D { addr, @@ -135,8 +164,33 @@ int llvm_addr2line(const char *dso_name, u64 addr, return 0; return extract_file_and_line(*res_or_err, file, line); } +#elif defined(HAVE_LIBLLVM_DYNAMIC) + static bool fn_init; + static int (*fn)(const char *dso_name, u64 addr, + char **file, unsigned int *line, + bool unwind_inlines, + llvm_a2l_frame **inline_frames); + + if (!fn_init) { + void * handle =3D perf_llvm_c_helpers_dll_handle(); + + if (!handle) + return 0; + + fn =3D reinterpret_cast(dlsym(handle, "llvm_addr2line")); + if (!fn) + pr_debug("dlsym failed for llvm_addr2line\n"); + fn_init =3D true; + } + if (!fn) + return 0; + return fn(dso_name, addr, file, line, unwind_inlines, inline_frames); +#else + return 0; +#endif } =20 +#if defined(HAVE_LIBLLVM_SUPPORT) && !defined(HAVE_LIBLLVM_DYNAMIC) static char * make_symbol_relative_string(struct dso *dso, const char *sym_name, u64 addr, u64 base_addr) @@ -158,10 +212,13 @@ make_symbol_relative_string(struct dso *dso, const ch= ar *sym_name, return strdup(sym_name); } } +#endif =20 extern "C" -char *llvm_name_for_code(struct dso *dso, const char *dso_name, u64 addr) +char *llvm_name_for_code(struct dso *dso __maybe_unused, const char *dso_n= ame __maybe_unused, + u64 addr __maybe_unused) { +#if defined(HAVE_LIBLLVM_SUPPORT) && !defined(HAVE_LIBLLVM_DYNAMIC) LLVMSymbolizer *symbolizer =3D get_symbolizer(); object::SectionedAddress sectioned_addr =3D { addr, @@ -175,11 +232,34 @@ char *llvm_name_for_code(struct dso *dso, const char = *dso_name, u64 addr) return make_symbol_relative_string( dso, res_or_err->FunctionName.c_str(), addr, res_or_err->StartAddress ? *res_or_err->StartAddress : 0); +#elif defined(HAVE_LIBLLVM_DYNAMIC) + static bool fn_init; + static char *(*fn)(struct dso *dso, const char *dso_name, u64 addr); + + if (!fn_init) { + void * handle =3D perf_llvm_c_helpers_dll_handle(); + + if (!handle) + return NULL; + + fn =3D reinterpret_cast(dlsym(handle, "llvm_name_for_code"= )); + if (!fn) + pr_debug("dlsym failed for llvm_name_for_code\n"); + fn_init =3D true; + } + if (!fn) + return NULL; + return fn(dso, dso_name, addr); +#else + return 0; +#endif } =20 extern "C" -char *llvm_name_for_data(struct dso *dso, const char *dso_name, u64 addr) +char *llvm_name_for_data(struct dso *dso __maybe_unused, const char *dso_n= ame __maybe_unused, + u64 addr __maybe_unused) { +#if defined(HAVE_LIBLLVM_SUPPORT) && !defined(HAVE_LIBLLVM_DYNAMIC) LLVMSymbolizer *symbolizer =3D get_symbolizer(); object::SectionedAddress sectioned_addr =3D { addr, @@ -193,4 +273,25 @@ char *llvm_name_for_data(struct dso *dso, const char *= dso_name, u64 addr) return make_symbol_relative_string( dso, res_or_err->Name.c_str(), addr, res_or_err->Start); +#elif defined(HAVE_LIBLLVM_DYNAMIC) + static bool fn_init; + static char *(*fn)(struct dso *dso, const char *dso_name, u64 addr); + + if (!fn_init) { + void * handle =3D perf_llvm_c_helpers_dll_handle(); + + if (!handle) + return NULL; + + fn =3D reinterpret_cast(dlsym(handle, "llvm_name_for_data"= )); + if (!fn) + pr_debug("dlsym failed for llvm_name_for_data\n"); + fn_init =3D true; + } + if (!fn) + return NULL; + return fn(dso, dso_name, addr); +#else + return 0; +#endif } diff --git a/tools/perf/util/llvm.c b/tools/perf/util/llvm.c index ddc737194692..f6a8943b7c9d 100644 --- a/tools/perf/util/llvm.c +++ b/tools/perf/util/llvm.c @@ -1,5 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 #include "llvm.h" +#include "llvm-c-helpers.h" #include "annotate.h" #include "debug.h" #include "dso.h" @@ -7,17 +8,243 @@ #include "namespaces.h" #include "srcline.h" #include "symbol.h" +#include #include +#include #include #include =20 -#ifdef HAVE_LIBLLVM_SUPPORT -#include "llvm-c-helpers.h" +#if defined(HAVE_LIBLLVM_SUPPORT) && !defined(HAVE_LIBLLVM_DYNAMIC) #include #include +#else +typedef void *LLVMDisasmContextRef; +typedef int (*LLVMOpInfoCallback)(void *dis_info, uint64_t pc, uint64_t of= fset, + uint64_t op_size, uint64_t inst_size, + int tag_type, void *tag_buf); +typedef const char *(*LLVMSymbolLookupCallback)(void *dis_info, + uint64_t reference_value, + uint64_t *reference_type, + uint64_t reference_pc, + const char **reference_name); +#define LLVMDisassembler_ReferenceType_InOut_None 0 +#define LLVMDisassembler_ReferenceType_In_Branch 1 +#define LLVMDisassembler_ReferenceType_In_PCrel_Load 2 +#define LLVMDisassembler_Option_PrintImmHex 2 +#define LLVMDisassembler_Option_AsmPrinterVariant 4 +const char *llvm_targets[] =3D { + "AMDGPU", + "ARM", + "AVR", + "BPF", + "Hexagon", + "Lanai", + "LoongArch", + "Mips", + "MSP430", + "NVPTX", + "PowerPC", + "RISCV", + "Sparc", + "SystemZ", + "VE", + "WebAssembly", + "X86", + "XCore", + "M68k", + "Xtensa", +}; +#endif + +#if !defined(HAVE_LIBLLVM_SUPPORT) || defined(HAVE_LIBLLVM_DYNAMIC) +static void *perf_llvm_dll_handle(void) +{ + static bool dll_handle_init; + static void *dll_handle; + + if (!dll_handle_init) { + dll_handle_init =3D true; + dll_handle =3D dlopen("libLLVM.so", RTLD_LAZY); + if (!dll_handle) + pr_debug("dlopen failed for libLLVM.so\n"); + } + return dll_handle; +} +#endif + +#if !defined(HAVE_LIBLLVM_SUPPORT) || defined(HAVE_LIBLLVM_DYNAMIC) +static void *perf_llvm_dll_fun(const char *fmt, const char *target) +{ + char buf[128]; + void *fn; + + snprintf(buf, sizeof(buf), fmt, target); + fn =3D dlsym(perf_llvm_dll_handle(), buf); + if (!fn) + pr_debug("dlsym failed for %s\n", buf); + + return fn; +} +#endif + +static void perf_LLVMInitializeAllTargetInfos(void) +{ +#if defined(HAVE_LIBLLVM_SUPPORT) && !defined(HAVE_LIBLLVM_DYNAMIC) + LLVMInitializeAllTargetInfos(); +#else + /* LLVMInitializeAllTargetInfos is a header file function not available a= s a symbol. */ + static bool done_init; + + if (done_init) + return; + + for (size_t i =3D 0; i < ARRAY_SIZE(llvm_targets); i++) { + void (*fn)(void) =3D perf_llvm_dll_fun("LLVMInitialize%sTargetInfo", + llvm_targets[i]); + + if (!fn) + continue; + fn(); + } + done_init =3D true; +#endif +} + +static void perf_LLVMInitializeAllTargetMCs(void) +{ +#if defined(HAVE_LIBLLVM_SUPPORT) && !defined(HAVE_LIBLLVM_DYNAMIC) + LLVMInitializeAllTargetMCs(); +#else + /* LLVMInitializeAllTargetMCs is a header file function not available as = a symbol. */ + static bool done_init; + + if (done_init) + return; + + for (size_t i =3D 0; i < ARRAY_SIZE(llvm_targets); i++) { + void (*fn)(void) =3D perf_llvm_dll_fun("LLVMInitialize%sTargetMC", + llvm_targets[i]); + + if (!fn) + continue; + fn(); + } + done_init =3D true; +#endif +} + +static void perf_LLVMInitializeAllDisassemblers(void) +{ +#if defined(HAVE_LIBLLVM_SUPPORT) && !defined(HAVE_LIBLLVM_DYNAMIC) + LLVMInitializeAllDisassemblers(); +#else + /* LLVMInitializeAllDisassemblers is a header file function not available= as a symbol. */ + static bool done_init; + + if (done_init) + return; + + for (size_t i =3D 0; i < ARRAY_SIZE(llvm_targets); i++) { + void (*fn)(void) =3D perf_llvm_dll_fun("LLVMInitialize%sDisassembler", + llvm_targets[i]); + + if (!fn) + continue; + fn(); + } + done_init =3D true; +#endif +} + +static LLVMDisasmContextRef perf_LLVMCreateDisasm(const char *triple_name,= void *dis_info, + int tag_type, LLVMOpInfoCallback get_op_info, + LLVMSymbolLookupCallback symbol_lookup) +{ +#if defined(HAVE_LIBLLVM_SUPPORT) && !defined(HAVE_LIBLLVM_DYNAMIC) + return LLVMCreateDisasm(triple_name, dis_info, tag_type, get_op_info, sym= bol_lookup); +#else + static bool fn_init; + static LLVMDisasmContextRef (*fn)(const char *triple_name, void *dis_info, + int tag_type, LLVMOpInfoCallback get_op_info, + LLVMSymbolLookupCallback symbol_lookup); + + if (!fn_init) { + fn =3D dlsym(perf_llvm_dll_handle(), "LLVMCreateDisasm"); + if (!fn) + pr_debug("dlsym failed for LLVMCreateDisasm\n"); + fn_init =3D true; + } + if (!fn) + return NULL; + return fn(triple_name, dis_info, tag_type, get_op_info, symbol_lookup); +#endif +} + +static int perf_LLVMSetDisasmOptions(LLVMDisasmContextRef context, uint64_= t options) +{ +#if defined(HAVE_LIBLLVM_SUPPORT) && !defined(HAVE_LIBLLVM_DYNAMIC) + return LLVMSetDisasmOptions(context, options); +#else + static bool fn_init; + static int (*fn)(LLVMDisasmContextRef context, uint64_t options); + + if (!fn_init) { + fn =3D dlsym(perf_llvm_dll_handle(), "LLVMSetDisasmOptions"); + if (!fn) + pr_debug("dlsym failed for LLVMSetDisasmOptions\n"); + fn_init =3D true; + } + if (!fn) + return 0; + return fn(context, options); +#endif +} + +static size_t perf_LLVMDisasmInstruction(LLVMDisasmContextRef context, uin= t8_t *bytes, + uint64_t bytes_size, uint64_t pc, + char *out_string, size_t out_string_size) +{ +#if defined(HAVE_LIBLLVM_SUPPORT) && !defined(HAVE_LIBLLVM_DYNAMIC) + return LLVMDisasmInstruction(context, bytes, bytes_size, pc, out_string, = out_string_size); +#else + static bool fn_init; + static int (*fn)(LLVMDisasmContextRef context, uint8_t *bytes, + uint64_t bytes_size, uint64_t pc, + char *out_string, size_t out_string_size); + + if (!fn_init) { + fn =3D dlsym(perf_llvm_dll_handle(), "LLVMDisasmInstruction"); + if (!fn) + pr_debug("dlsym failed for LLVMDisasmInstruction\n"); + fn_init =3D true; + } + if (!fn) + return 0; + return fn(context, bytes, bytes_size, pc, out_string, out_string_size); +#endif +} + +static void perf_LLVMDisasmDispose(LLVMDisasmContextRef context) +{ +#if defined(HAVE_LIBLLVM_SUPPORT) && !defined(HAVE_LIBLLVM_DYNAMIC) + LLVMDisasmDispose(context); +#else + static bool fn_init; + static int (*fn)(LLVMDisasmContextRef context); + + if (!fn_init) { + fn =3D dlsym(perf_llvm_dll_handle(), "LLVMDisasmDispose"); + if (!fn) + pr_debug("dlsym failed for LLVMDisasmDispose\n"); + fn_init =3D true; + } + if (!fn) + return; + fn(context); #endif +} + =20 -#ifdef HAVE_LIBLLVM_SUPPORT static void free_llvm_inline_frames(struct llvm_a2l_frame *inline_frames, int num_frames) { @@ -29,14 +256,12 @@ static void free_llvm_inline_frames(struct llvm_a2l_fr= ame *inline_frames, zfree(&inline_frames); } } -#endif =20 int llvm__addr2line(const char *dso_name __maybe_unused, u64 addr __maybe_= unused, char **file __maybe_unused, unsigned int *line __maybe_unused, struct dso *dso __maybe_unused, bool unwind_inlines __maybe_unused, struct inline_node *node __maybe_unused, struct symbol *sym __maybe= _unused) { -#ifdef HAVE_LIBLLVM_SUPPORT struct llvm_a2l_frame *inline_frames =3D NULL; int num_frames =3D llvm_addr2line(dso_name, addr, file, line, node && unwind_inlines, &inline_frames); @@ -64,9 +289,6 @@ int llvm__addr2line(const char *dso_name __maybe_unused,= u64 addr __maybe_unused free_llvm_inline_frames(inline_frames, num_frames); =20 return num_frames; -#else - return -1; -#endif } =20 void dso__free_a2l_llvm(struct dso *dso __maybe_unused) @@ -75,7 +297,6 @@ void dso__free_a2l_llvm(struct dso *dso __maybe_unused) } =20 =20 -#if defined(HAVE_LIBLLVM_SUPPORT) struct find_file_offset_data { u64 ip; u64 offset; @@ -139,7 +360,6 @@ read_symbol(const char *filename, struct map *map, stru= ct symbol *sym, free(buf); return NULL; } -#endif =20 /* * Whenever LLVM wants to resolve an address into a symbol, it calls this @@ -149,7 +369,6 @@ read_symbol(const char *filename, struct map *map, stru= ct symbol *sym, * should add some textual annotation for after the instruction. The caller * will use this information to add the actual annotation. */ -#ifdef HAVE_LIBLLVM_SUPPORT struct symbol_lookup_storage { u64 branch_addr; u64 pcrel_load_addr; @@ -170,12 +389,10 @@ symbol_lookup_callback(void *disinfo, uint64_t value, *ref_type =3D LLVMDisassembler_ReferenceType_InOut_None; return NULL; } -#endif =20 int symbol__disassemble_llvm(const char *filename, struct symbol *sym, struct annotate_args *args __maybe_unused) { -#ifdef HAVE_LIBLLVM_SUPPORT struct annotation *notes =3D symbol__annotation(sym); struct map *map =3D args->ms.map; struct dso *dso =3D map__dso(map); @@ -197,9 +414,9 @@ int symbol__disassemble_llvm(const char *filename, stru= ct symbol *sym, if (args->options->objdump_path) return -1; =20 - LLVMInitializeAllTargetInfos(); - LLVMInitializeAllTargetMCs(); - LLVMInitializeAllDisassemblers(); + perf_LLVMInitializeAllTargetInfos(); + perf_LLVMInitializeAllTargetMCs(); + perf_LLVMInitializeAllDisassemblers(); =20 buf =3D read_symbol(filename, map, sym, &len, &is_64bit); if (buf =3D=3D NULL) @@ -215,15 +432,14 @@ int symbol__disassemble_llvm(const char *filename, st= ruct symbol *sym, args->arch->name); } =20 - disasm =3D LLVMCreateDisasm(triplet, &storage, 0, NULL, - symbol_lookup_callback); + disasm =3D perf_LLVMCreateDisasm(triplet, &storage, 0, NULL, + symbol_lookup_callback); if (disasm =3D=3D NULL) goto err; =20 if (args->options->disassembler_style && !strcmp(args->options->disassembler_style, "intel")) - LLVMSetDisasmOptions(disasm, - LLVMDisassembler_Option_AsmPrinterVariant); + perf_LLVMSetDisasmOptions(disasm, LLVMDisassembler_Option_AsmPrinterVari= ant); =20 /* * This needs to be set after AsmPrinterVariant, due to a bug in LLVM; @@ -231,7 +447,7 @@ int symbol__disassemble_llvm(const char *filename, stru= ct symbol *sym, * forget about the PrintImmHex flag (which is applied before if both * are given to the same call). */ - LLVMSetDisasmOptions(disasm, LLVMDisassembler_Option_PrintImmHex); + perf_LLVMSetDisasmOptions(disasm, LLVMDisassembler_Option_PrintImmHex); =20 /* add the function address and name */ scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:", @@ -256,9 +472,9 @@ int symbol__disassemble_llvm(const char *filename, stru= ct symbol *sym, storage.branch_addr =3D 0; storage.pcrel_load_addr =3D 0; =20 - ins_len =3D LLVMDisasmInstruction(disasm, buf + offset, - len - offset, pc, - disasm_buf, sizeof(disasm_buf)); + ins_len =3D perf_LLVMDisasmInstruction(disasm, buf + offset, + len - offset, pc, + disasm_buf, sizeof(disasm_buf)); if (ins_len =3D=3D 0) goto err; disasm_len =3D strlen(disasm_buf); @@ -314,13 +530,8 @@ int symbol__disassemble_llvm(const char *filename, str= uct symbol *sym, ret =3D 0; =20 err: - LLVMDisasmDispose(disasm); + perf_LLVMDisasmDispose(disasm); free(buf); free(line_storage); return ret; -#else // HAVE_LIBLLVM_SUPPORT - pr_debug("The LLVM disassembler isn't linked in for %s in %s\n", - sym->name, filename); - return -1; -#endif } --=20 2.49.0.805.g082f7c87e0-goog From nobody Fri Dec 19 06:33:53 2025 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.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 F1D72225402 for ; Thu, 17 Apr 2025 23:08:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744931294; cv=none; b=gASnLReiu2UbYRaCaJLb/aUZ/U/M5WuuMBs1+8jcElQ8gZqamfehoG3QphyMUSXerwXOkokyXFgHc9Hdh8e0dc8pjKB21eSjkuVUl4oP9g2wJ659oHQf4xxTLtrlRmUZjjBaGW8lArRB6j/1Ja5vy5aVbLQUoRN2MXFrgzH1Ndo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744931294; c=relaxed/simple; bh=vo0fATWMVyyAZc0aPQcFuWHRfsLIbRMrgnl393mZOuY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=YcuroOJtshk4DfevGnUUK12h39jXPY2JtetpIL8Zq3RhkWm1TK4RlvQ/M5lX6DVaA3/WRc+VTv4Sx9n2qUiQlrhnGGGqGxMJvExoy2eFmvoLhNkEucQv9GRcb7dUNRoUN/UYXNjFlFbvXpWVl9wuY8O7MoE1BbA9KGU52KkQ4/Q= 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=usBHnLUB; arc=none smtp.client-ip=209.85.214.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--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="usBHnLUB" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-22647ff3cf5so12029955ad.0 for ; Thu, 17 Apr 2025 16:08:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1744931291; x=1745536091; 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=dOWuhbgsGhCOfYjY+PJ0Yf970aP5bSbLzlrRNECM4Bs=; b=usBHnLUBrWTJ8yjuWfvOO0v5CrlxV/C3ynD6XYXH/vlUHEKhcXVkAuADsIBOtTySmW 2kgSLvCrtLO+Q9Feott/QOSlHOcnlbvvqHrRufFaS/EXGtP70PSSN1NbqX92cOyvpJhC JIpEBFkWGTcXZN4DK9XsqHY/dkDS9odEClKcj721MQCCwIBl3XhRUGp0+IvMDePLBnR+ shrmaZU6xA+YKfV4M1nzcUOglsDiJ2pQSLuPgLtpvD5idLK2qM45rwpUW/CRpeNyCckj D1st6J7kyJOEW/NeMHvb068f0Vrm28FcHZkgDEJSfmGpXadHsIZb5JdqP+JrNO+0uGc/ XifQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744931291; x=1745536091; 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=dOWuhbgsGhCOfYjY+PJ0Yf970aP5bSbLzlrRNECM4Bs=; b=jaieFDkWTCmI2rr0V90goKlz3sxkZ63hI1VFUtfx9FoZ4PkD0kqtsfqt+124bqebiw WtZZQbuB+uWuGemKxb+vbWqV5oUGuczZhPNiVQ7slVEHlEpN2n6qttK6z7TdDQRfQzzP 0pgyMEsYKdDXEVx4vj6ZTCscW3phyLps6ekGG9BHQliF/oMslXo5dbEU9qqJRtYiIGf1 ONkRXI0mqaaZg8+36oNvjHesvGWrhT/sWAXkW02vn7RGQliyh6hiCG+qbEHuzh64iXRj Eg1kO7KDtV2YaK9+Vi0Z06mAZDisjSd9Lzfg7FP6TSyZrWZRUOHciMot57stTJztiU92 g2fg== X-Forwarded-Encrypted: i=1; AJvYcCXB6bl6emyu5wNaRB1cN3CYJcGUH3VjYGnnXrdZj3ssvsJLn88oJ/nspSEDb+TDrMcdtHoxbh+VDfLiCKs=@vger.kernel.org X-Gm-Message-State: AOJu0Yz3vchthIx7BInK+DSoJ9M1i70cEBszs+b5FlDrUCFeUyM3zNCM TqDU9yJX7GIFS0HWtqmy87GWh56kDkOZoTAlA9leVyAWBG55gbxj4NMZZ58em1vZk5tNdmNDE+a ZmItN5w== X-Google-Smtp-Source: AGHT+IHoEc6jQbq8HuRmqmXiqrWdB4nYrkkSkauIGXEi8gW5l81eNAz3y+nJ3Ah8XzFLGgTQsEEewT4YwJxl X-Received: from pljs6.prod.google.com ([2002:a17:903:3ba6:b0:216:25a2:2ecf]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:3d0d:b0:224:c76:5e56 with SMTP id d9443c01a7336-22c535acd99mr8848435ad.27.1744931291201; Thu, 17 Apr 2025 16:08:11 -0700 (PDT) Date: Thu, 17 Apr 2025 16:07:29 -0700 In-Reply-To: <20250417230740.86048-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: <20250417230740.86048-1-irogers@google.com> X-Mailer: git-send-email 2.49.0.805.g082f7c87e0-goog Message-ID: <20250417230740.86048-9-irogers@google.com> Subject: [PATCH v4 08/19] perf llvm: Mangle libperf-llvm.so function names 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" For a function like llvm_addr2line having the libperf-llvm.so exported symbol named llvm_addr2line meant that the perf llvm_addr2line could sometimes erroneously be returned. This led to infinite recursion and eventual stack overflow. To avoid this conflict add a new BUILDING_PERF_LLVMSO when libperf-llvm.so is being built and use it to alter the behavior of MANGLE_PERF_LLVM_API, a macro that prefixes the name when libperf-llvm.so is being built. The prefixed named avoids the name collision. Signed-off-by: Ian Rogers --- tools/perf/Makefile.perf | 3 ++- tools/perf/util/llvm-c-helpers.cpp | 29 ++++++++++++++++++----------- tools/perf/util/llvm-c-helpers.h | 24 ++++++++++++++++-------- 3 files changed, 36 insertions(+), 20 deletions(-) diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf index 6833dda27ffb..fc65537d5821 100644 --- a/tools/perf/Makefile.perf +++ b/tools/perf/Makefile.perf @@ -1003,7 +1003,8 @@ $(LIBSYMBOL)-clean: $(Q)$(RM) -r -- $(LIBSYMBOL_OUTPUT) =20 ifdef LIBLLVM_DYNAMIC -LIBPERF_LLVM_CXXFLAGS :=3D $(call filter-out,-DHAVE_LIBLLVM_DYNAMIC,$(CXXF= LAGS)) -DHAVE_LIBLLVM_SUPPORT +LIBPERF_LLVM_CXXFLAGS :=3D $(call filter-out,-DHAVE_LIBLLVM_DYNAMIC,$(CXXF= LAGS)) +LIBPERF_LLVM_CXXFLAGS +=3D -DHAVE_LIBLLVM_SUPPORT -DBUILDING_PERF_LLVMSO LIBPERF_LLVM_LIBS =3D -L$(shell $(LLVM_CONFIG) --libdir) $(LIBLLVM) -lstdc= ++ =20 $(OUTPUT)$(LIBPERF_LLVM): util/llvm-c-helpers.cpp diff --git a/tools/perf/util/llvm-c-helpers.cpp b/tools/perf/util/llvm-c-he= lpers.cpp index 5a6f76e6b705..8cea380be5c2 100644 --- a/tools/perf/util/llvm-c-helpers.cpp +++ b/tools/perf/util/llvm-c-helpers.cpp @@ -99,10 +99,12 @@ static int extract_file_and_line(const DILineInfo &line= _info, char **file, #endif =20 extern "C" -int llvm_addr2line(const char *dso_name __maybe_unused, u64 addr __maybe_u= nused, - char **file __maybe_unused, unsigned int *line __maybe_unused, - bool unwind_inlines __maybe_unused, - llvm_a2l_frame **inline_frames __maybe_unused) +int MANGLE_PERF_LLVM_API(llvm_addr2line)(const char *dso_name __maybe_unus= ed, + u64 addr __maybe_unused, + char **file __maybe_unused, + unsigned int *line __maybe_unused, + bool unwind_inlines __maybe_unuse= d, + llvm_a2l_frame **inline_frames __= maybe_unused) { #if defined(HAVE_LIBLLVM_SUPPORT) && !defined(HAVE_LIBLLVM_DYNAMIC) LLVMSymbolizer *symbolizer =3D get_symbolizer(); @@ -177,7 +179,8 @@ int llvm_addr2line(const char *dso_name __maybe_unused,= u64 addr __maybe_unused, if (!handle) return 0; =20 - fn =3D reinterpret_cast(dlsym(handle, "llvm_addr2line")); + fn =3D reinterpret_cast( + dlsym(handle, MANGLE_PERF_LLVM_API_STR(llvm_addr2line))); if (!fn) pr_debug("dlsym failed for llvm_addr2line\n"); fn_init =3D true; @@ -215,8 +218,9 @@ make_symbol_relative_string(struct dso *dso, const char= *sym_name, #endif =20 extern "C" -char *llvm_name_for_code(struct dso *dso __maybe_unused, const char *dso_n= ame __maybe_unused, - u64 addr __maybe_unused) +char *MANGLE_PERF_LLVM_API(llvm_name_for_code)(struct dso *dso __maybe_unu= sed, + const char *dso_name __maybe_unused, + u64 addr __maybe_unused) { #if defined(HAVE_LIBLLVM_SUPPORT) && !defined(HAVE_LIBLLVM_DYNAMIC) LLVMSymbolizer *symbolizer =3D get_symbolizer(); @@ -242,7 +246,8 @@ char *llvm_name_for_code(struct dso *dso __maybe_unused= , const char *dso_name __ if (!handle) return NULL; =20 - fn =3D reinterpret_cast(dlsym(handle, "llvm_name_for_code"= )); + fn =3D reinterpret_cast( + dlsym(handle, MANGLE_PERF_LLVM_API_STR(llvm_name_for_code))); if (!fn) pr_debug("dlsym failed for llvm_name_for_code\n"); fn_init =3D true; @@ -256,8 +261,9 @@ char *llvm_name_for_code(struct dso *dso __maybe_unused= , const char *dso_name __ } =20 extern "C" -char *llvm_name_for_data(struct dso *dso __maybe_unused, const char *dso_n= ame __maybe_unused, - u64 addr __maybe_unused) +char *MANGLE_PERF_LLVM_API(llvm_name_for_data)(struct dso *dso __maybe_unu= sed, + const char *dso_name __maybe_unused, + u64 addr __maybe_unused) { #if defined(HAVE_LIBLLVM_SUPPORT) && !defined(HAVE_LIBLLVM_DYNAMIC) LLVMSymbolizer *symbolizer =3D get_symbolizer(); @@ -283,7 +289,8 @@ char *llvm_name_for_data(struct dso *dso __maybe_unused= , const char *dso_name __ if (!handle) return NULL; =20 - fn =3D reinterpret_cast(dlsym(handle, "llvm_name_for_data"= )); + fn =3D reinterpret_cast( + dlsym(handle, MANGLE_PERF_LLVM_API_STR(llvm_name_for_data))); if (!fn) pr_debug("dlsym failed for llvm_name_for_data\n"); fn_init =3D true; diff --git a/tools/perf/util/llvm-c-helpers.h b/tools/perf/util/llvm-c-help= ers.h index d2b99637a28a..cfcfd540cdae 100644 --- a/tools/perf/util/llvm-c-helpers.h +++ b/tools/perf/util/llvm-c-helpers.h @@ -13,6 +13,14 @@ extern "C" { #endif =20 +/* Support name mangling so that libperf_llvm.so's names don't match those= in perf. */ +#ifdef BUILDING_PERF_LLVMSO +#define MANGLE_PERF_LLVM_API(x) PERF_LLVM_SO_ ## x +#else +#define MANGLE_PERF_LLVM_API(x) x +#endif +#define MANGLE_PERF_LLVM_API_STR(x) "PERF_LLVM_SO_" #x + struct dso; =20 struct llvm_a2l_frame { @@ -37,12 +45,12 @@ struct llvm_a2l_frame { * a newly allocated array with that length. The caller is then responsible * for freeing both the strings and the array itself. */ -int llvm_addr2line(const char* dso_name, - u64 addr, - char** file, - unsigned int* line, - bool unwind_inlines, - struct llvm_a2l_frame** inline_frames); +int MANGLE_PERF_LLVM_API(llvm_addr2line)(const char *dso_name, + u64 addr, + char **file, + unsigned int *line, + bool unwind_inlines, + struct llvm_a2l_frame **inline_frames); =20 /* * Simple symbolizers for addresses; will convert something like @@ -50,8 +58,8 @@ int llvm_addr2line(const char* dso_name, * * The returned value must be freed by the caller, with free(). */ -char *llvm_name_for_code(struct dso *dso, const char *dso_name, u64 addr); -char *llvm_name_for_data(struct dso *dso, const char *dso_name, u64 addr); +char *MANGLE_PERF_LLVM_API(llvm_name_for_code)(struct dso *dso, const char= *dso_name, u64 addr); +char *MANGLE_PERF_LLVM_API(llvm_name_for_data)(struct dso *dso, const char= *dso_name, u64 addr); =20 #ifdef __cplusplus } --=20 2.49.0.805.g082f7c87e0-goog From nobody Fri Dec 19 06:33:53 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 4576F226188 for ; Thu, 17 Apr 2025 23:08:14 +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=1744931296; cv=none; b=g30Dg3zY31bE5DuZHMmwxH4KGK4aWuO6JW2ik25w99qLplwj2Q99RggP4M39MORJzZMAo3j1AECgDbHb/A59NXMNPkenO+8DgJzDwVAp7NlVEmloEg0orqiGg5DyLICnNy6YVw+OvhOrwb9wr7O0tseut0WiVll9vTY3KQWZ1nA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744931296; c=relaxed/simple; bh=Dop39pNTWiPxwnxSmZLSOwLih9pfECvq0vx0SaSe0F0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=i/qPKVgp1ZjSGsbIi+MzAC8OHycVp349sbIiLVLoT6P/tFdl8c4QraAwkVgrAVgqNrxbcgwzBnkvvs3FekgUE94KSC+Et7KafS+1wJfWjZBZmQ1cEfugGoU78rC5DdbDUwloKLiysoxYcLveB9pQgikcrjyv67/4iP4xQdRnooA= 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=o/EreJLY; 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="o/EreJLY" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2241e7e3addso12544735ad.1 for ; Thu, 17 Apr 2025 16:08:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1744931294; x=1745536094; 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=CGY+E3gxE4zS5dXOjOobKOoTErjVF4eK9OtL9zfg1Kg=; b=o/EreJLYw4Q5ZKsa5TIYtnm3C/d2E2/j1A4alFIs0ltNOq1DKF4GjcyEax/+07LLLO XHf4zCUCbC59fMs8hAUWvWR7PKVEj6HSCVUVRusCRPh7FPg8ssvnnl0qAanPI4tFIopt x95QJ8xQ2f/Lldvu4snC6xLvvDb7x3YTuJ8jY1eOb1eb351Uv9To/I+66/z2uQu2CPjP uJHq8oPhePo9ciphmcsdeUAyLWpscuKsPrLTR2BwgG9eAwROhTh71GZl39wiujRYpOUy R7xnJV5T/0VV5fHIyynlAuShqDGNUDg3ulaopkmg68PntbInN55L7l/oSJYAh5pthYDd cAnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744931294; x=1745536094; 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=CGY+E3gxE4zS5dXOjOobKOoTErjVF4eK9OtL9zfg1Kg=; b=tsMK+Rrwojkbx7MPQpERYGvt5AqtMLBMteNT0LhK1noRyOfV4atrfk/iX/6MppCvTU SXBR7+MuAr018J2XLEM1FcOKH+U6KkMerdESJ4fL7AlvuPghj/bGMqSVK2wfogcJsG33 l+vFq6hQ77APYWXcKH2vw+5FkRpi7oj4xdE88gnKMhx7kh2cSbbSQ7i81A3N0NIeCHBm KzI8hgrk63aWfv2M0hrDhHcBhoMWfsdGfq1cMabT+TY/5bQVf+J94oHTJurmoMfZjP7h l8RPppId5MhEg3G5FHwDpMlL0XX69U1qMe+EIuapPeQk+rCU/xYqSp330d4Fbxz4/PJP HA8Q== X-Forwarded-Encrypted: i=1; AJvYcCXrQ/5ZJwL1TAQfVxuBq/h1xWqxxZT+ljJyTsQoJJLVlSRXRpIa4ENwbQiNQdL9BfGYiAZD5+FdLUHaD1M=@vger.kernel.org X-Gm-Message-State: AOJu0Yxo+RykWBSty7afF8gaDyZaQclWxhx4paekQNXA7TK8q+ZreJ+0 DWWrq/w+vOHrCLtBjM4uPp0uL7P8A78lMJvxSFSbXX9QCZiaBBwvaPAS3lbtW8K2F3UrKL3ftFk B0oyxEg== X-Google-Smtp-Source: AGHT+IFulWl/JXP/uDZuGK+zA95FAU0s8izVzXe26Ii6nCX+oaQOaC629ZxyNDj9U+sVXbgW1pM08jdMi8r5 X-Received: from plo21.prod.google.com ([2002:a17:902:ee55:b0:221:8568:bfe3]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:ec90:b0:229:1717:8826 with SMTP id d9443c01a7336-22c535b4a1cmr10834225ad.28.1744931293301; Thu, 17 Apr 2025 16:08:13 -0700 (PDT) Date: Thu, 17 Apr 2025 16:07:30 -0700 In-Reply-To: <20250417230740.86048-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: <20250417230740.86048-1-irogers@google.com> X-Mailer: git-send-email 2.49.0.805.g082f7c87e0-goog Message-ID: <20250417230740.86048-10-irogers@google.com> Subject: [PATCH v4 09/19] 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 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 | 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 p= goff, void *arg) return 0; } =20 -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; -} - int symbol__disassemble_capstone(const char *filename __maybe_unused, struct symbol *sym __maybe_unused, struct annotate_args *args __maybe_unused) { 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; struct cs_insn *insn =3D 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; =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 @@ -532,7 +490,7 @@ int symbol__disassemble_capstone(const char *filename _= _maybe_unused, =20 needs_cs_close =3D true; =20 - free_count =3D count =3D perf_cs_disasm(handle, buf, len, start, len, &in= sn); + free_count =3D count =3D perf_cs_disasm(handle, buf, buf_len, start, buf_= len, &insn); for (i =3D 0, offset =3D 0; i < count; i++) { int printed; =20 @@ -556,7 +514,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 */ @@ -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; =20 err: diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c index 8619b6eea62d..9b1e3fb3b2b9 100644 --- a/tools/perf/util/dso.c +++ b/tools/perf/util/dso.c @@ -1776,3 +1776,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 c87564471f9b..6230eea7a93a 100644 --- a/tools/perf/util/dso.h +++ b/tools/perf/util/dso.h @@ -896,4 +896,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); =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 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. */ } =20 - -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; -} - /* * 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 @@ -397,8 +332,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 triplet[64]; @@ -418,7 +356,8 @@ int symbol__disassemble_llvm(const char *filename, stru= ct symbol *sym, perf_LLVMInitializeAllTargetMCs(); perf_LLVMInitializeAllDisassemblers(); =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 @@ -466,14 +405,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 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 =3D perf_LLVMDisasmInstruction(disasm, (u8 *)(buf + offset), + buf_len - offset, pc, disasm_buf, sizeof(disasm_buf)); if (ins_len =3D=3D 0) goto err; @@ -531,7 +474,7 @@ int symbol__disassemble_llvm(const char *filename, stru= ct symbol *sym, =20 err: perf_LLVMDisasmDispose(disasm); - free(buf); + free(code_buf); free(line_storage); return ret; } --=20 2.49.0.805.g082f7c87e0-goog From nobody Fri Dec 19 06:33:53 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 44287227E8A for ; Thu, 17 Apr 2025 23:08:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744931297; cv=none; b=Jp29/XpV/LNmGhgnhGlFd97iZskW99D3uQ7/FxlByBu8NAmsAvRh0R6LW0pnDCRcqTd92PILG7fyw7ptDsM9BNucapqQV4/LwrCbp2H8Cxc/D/ynDmXNX8f2JxNK8kv4e7PZUuR96bS0rNj5NipXs9iN3coMklvkcWB/41kj8x4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744931297; c=relaxed/simple; bh=b778ItJAkgSzsZeREtScM1hNY+Ffpp+8QlSXkzwLw4c=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=Y9WVmLeDPRJE3JDhOXJ1q90iAYvrAzg3Uql1hoeOgKijuWvKU5mQtbuu0EeJckRxhOhsyWpE3AqWpSEvAgVZk+rfyFcbck8wdMvG5mYf12z4RC0wQ4egJswLpqr1qUCHnzwlxqZZsx4JNNjPLRSCocds6ooxNt0fcITacS6sJXM= 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=E4kzD/DN; arc=none smtp.client-ip=209.85.216.74 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="E4kzD/DN" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-30364fc706fso1163225a91.3 for ; Thu, 17 Apr 2025 16:08:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1744931295; x=1745536095; 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=+ATo1kG4YAa83tuUgq+g3A09N22DQKM10QzKAR+cwLE=; b=E4kzD/DNqkIjviIW1fK7J00Fhkm9186RBel8UyU5KHhXMfhsxy7Nr+dy06uxI3hK8N x5SSDlBpRl1aURvjQVAYdqlwLDeq7FLdMTWHjM0rkz7FxjFQvZb0dOblb9abkrEqQsdV SidAPD/XEephenBZ975xI85mK8+RNcdYKLyBD1doE7l9zchDoq0cE27LgJsL3g+5bCBg Jhd/H4sDACrjKFLvoCj7IqVxfW7ZaZS50fsGripkltSoGZqa3nUZbu6+x7q/h5Nk8EE9 QWXJ9vvKKAf+iXIaOKcavfFSeifevU8J9PRgg1wPzS+7F0bvsf6EXSR53ySVAYlZcE+X zoPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744931295; x=1745536095; 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=+ATo1kG4YAa83tuUgq+g3A09N22DQKM10QzKAR+cwLE=; b=Mi8kXULa+Chg1dCS8yjIXGIk1dXm7pGk/ECBFqwq3gXa80e0Xy6RUrPOZeHv2MHZMj N25HyXo/asC58cYkpuQKgI5bgDZ1B1UBvekkjIkHEVr/9H4hTMge0I2CH5QQRMGlQ4Hg 7DU1EJzGpiNft/oYemo3b5jnQV7EQpLcKxwzOyg5ibOt/+K5cHNOSzhN+aZBfu01tbFc 3t8hEciymnvHM3m/yXrjkNUtrpUDiZsV8Wt9Zf1p7XeaLTsdZ0s9MccstZKqL2Jc8g42 G52Zf/KcA3MvdrG70Na+s3jWDQnoLgyXn2nmktlYLWNhLApEHca75KQlWulDy7rvd2Jc yMng== X-Forwarded-Encrypted: i=1; AJvYcCV+QpW/aV4+KrbOfuJ0CmZH6P0LHngYbzQoW6TRJNg3t1K6f/Uj9R7ix6zLpDpCHQbhIiUaRM6/8Qb1IGg=@vger.kernel.org X-Gm-Message-State: AOJu0YxSj4K/wJgwowPWRk8rv6wtg76whkmTyhkJ3RhhOLJcU7EeP+Zx v3a4TkGL0lcLVRd6inEiGFntFaTSbRJg+EG5+B2TyTq2x0hinNHJvTZLw5Ben1NAsy/U92XhQyF PNYc+Ew== X-Google-Smtp-Source: AGHT+IHH8UVkv/kmT/umdpA7zcimjqNLVkE4OP4Qq3nssLU4bZSOTO9d/zSoNOwy9xWN8TRKnJBaf5gC9JuN X-Received: from pjbsw12.prod.google.com ([2002:a17:90b:2c8c:b0:2f9:dc36:b11]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:56cd:b0:2ff:4bac:6fa2 with SMTP id 98e67ed59e1d1-3087bb62a73mr1192181a91.16.1744931295544; Thu, 17 Apr 2025 16:08:15 -0700 (PDT) Date: Thu, 17 Apr 2025 16:07:31 -0700 In-Reply-To: <20250417230740.86048-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: <20250417230740.86048-1-irogers@google.com> X-Mailer: git-send-email 2.49.0.805.g082f7c87e0-goog Message-ID: <20250417230740.86048-11-irogers@google.com> Subject: [PATCH v4 10/19] 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 73343ae5fc96..175d9d3b5c97 100644 --- a/tools/perf/util/disasm.c +++ b/tools/perf/util/disasm.c @@ -1501,6 +1501,12 @@ static int symbol__disassemble_objdump(const char *f= ilename, struct symbol *sym, struct child_process objdump_process; int err; =20 + if (dso__binary_type(dso) =3D=3D DSO_BINARY_TYPE__BPF_PROG_INFO) + return symbol__disassemble_bpf(sym, args); + + if (dso__binary_type(dso) =3D=3D DSO_BINARY_TYPE__BPF_IMAGE) + return symbol__disassemble_bpf_image(sym, args); + err =3D asprintf(&command, "%s %s%s --start-address=3D0x%016" PRIx64 " --stop-address=3D0x%016" PRIx64 @@ -1635,11 +1641,7 @@ int symbol__disassemble(struct symbol *sym, struct a= nnotate_args *args) =20 pr_debug("annotating [%p] %30s : [%p] %30s\n", dso, dso__long_name(dso), = sym, sym->name); =20 - if (dso__binary_type(dso) =3D=3D DSO_BINARY_TYPE__BPF_PROG_INFO) { - return symbol__disassemble_bpf(sym, args); - } else if (dso__binary_type(dso) =3D=3D DSO_BINARY_TYPE__BPF_IMAGE) { - return symbol__disassemble_bpf_image(sym, args); - } else if (dso__binary_type(dso) =3D=3D DSO_BINARY_TYPE__NOT_FOUND) { + if (dso__binary_type(dso) =3D=3D DSO_BINARY_TYPE__NOT_FOUND) { return SYMBOL_ANNOTATE_ERRNO__COULDNT_DETERMINE_FILE_TYPE; } else if (dso__is_kcore(dso)) { kce.addr =3D map__rip_2objdump(map, sym->start); diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c index 9b1e3fb3b2b9..d08e2a1f49c1 100644 --- a/tools/perf/util/dso.c +++ b/tools/perf/util/dso.c @@ -1798,48 +1798,69 @@ const u8 *dso__read_symbol(struct dso *dso, const c= har *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, - }; + const u8 *buf; + size_t len =3D end - start; =20 *out_buf =3D NULL; *out_buf_len =3D 0; *is_64bit =3D false; =20 - nsinfo__mountns_enter(dso__nsinfo(dso), &nsc); - fd =3D open(symfs_filename, O_RDONLY); - nsinfo__mountns_exit(&nsc); - if (fd < 0) + if (dso__binary_type(dso) =3D=3D DSO_BINARY_TYPE__BPF_IMAGE) { + pr_debug("No BPF image disassembly support\n"); return NULL; + } else if (dso__binary_type(dso) =3D=3D DSO_BINARY_TYPE__BPF_PROG_INFO) { +#ifdef HAVE_LIBBPF_SUPPORT + struct bpf_prog_info_node *info_node; + struct perf_bpil *info_linear; + + *is_64bit =3D sizeof(void *) =3D=3D sizeof(u64); + info_node =3D perf_env__find_bpf_prog_info(dso__bpf_prog(dso)->env, + dso__bpf_prog(dso)->id); + if (!info_node) { + errno =3D SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF; + return NULL; + } + info_linear =3D info_node->info_linear; + buf =3D (const u8 *)(uintptr_t)(info_linear->info.jited_prog_insns); + assert(len <=3D 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 =3D { + .ip =3D start, + }; + u8 *code_buf =3D NULL; =20 - 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; + nsinfo__mountns_enter(dso__nsinfo(dso), &nsc); + fd =3D open(symfs_filename, O_RDONLY); + nsinfo__mountns_exit(&nsc); + if (fd < 0) + return NULL; =20 - *out_buf =3D buf; + if (file__read_maps(fd, /*exe=3D*/true, find_file_offset, &data, is_64bi= t) =3D=3D 0) { + close(fd); + return NULL; + } + buf =3D code_buf =3D malloc(len); + if (buf =3D=3D NULL) { + close(fd); + return NULL; + } + count =3D pread(fd, code_buf, len, data.offset); + close(fd); + if ((u64)count !=3D len) { + free(code_buf); + return NULL; + } + *out_buf =3D code_buf; + } *out_buf_len =3D len; return buf; - -err: - if (fd >=3D 0) - close(fd); - free(buf); - return NULL; } --=20 2.49.0.805.g082f7c87e0-goog From nobody Fri Dec 19 06:33:53 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 30F4522A7FC for ; Thu, 17 Apr 2025 23:08:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744931300; cv=none; b=sKtMSMOV0DQrby15k23+XwfiVZHuuFM4drrHtSXuokx5x4IzSjCeKRFyfHy5Q9cjyBkfdU/CQlYBmg6bOU1dRyaXRzNQxVmj2NLqVnESCtQbOYCTY6vleOUy4jGUfU7ILe2tdFMPITz0nhIvwQWDFvyNAw9jobwboDl95BZTTD4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744931300; c=relaxed/simple; bh=Erynk8+/uixEXt7BeOJQFcm6Q561j11l35I5blMdZvo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=PcPUEFGSOB/kNA4N7lpGG0GJsdE+tSdnp73pbYr5wZVby5HGA14NvIs2z6ue66ChEglDNvF+8zDN2pAoh0GLBeUNUGMtRi7xUU26J7oTvork/KmG8s0Sf1d+9HZTjZA2vBXiqc/f5rRCifkAUMCy0jltSi51azgOJzZ5BWDqumA= 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=bf8vRXlF; arc=none smtp.client-ip=209.85.216.73 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="bf8vRXlF" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-3032ea03448so1294646a91.2 for ; Thu, 17 Apr 2025 16:08:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1744931297; x=1745536097; 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=IB6ehE4CIrRVO0upk/cDtkYMX2P2CRQHtVQu5oompks=; b=bf8vRXlFL73TDTj5aSdIV6q6yBtWW/K7VosyXCUI/tmOkZB8cOw+MNdpJ9ig0/NPRg uB61rd58BSOdrkLwVNnP+4LlhK1wra8PLzPN0rPTAQ3zMEGzgI5/qUodvPeuqKELHJvk J2cfRrOkxyp0VbXia5tvQGa7ewRTSLROg3Pkm1UVTuA+JfMsYTn0TZ8pQJSTjWMEixGC csI4tQJxzP75rEaoZydrLUsdMB4+0GvpALUPZdHlx6eA4xJb7cuk0by/QBSVgE9/hRoH R/Xzoa7I39jtyBCS2hTWr5D4iO6pd/jiulJt2iDuru4o7uX3u30NXJ8xXJDmuARY0zOZ saNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744931297; x=1745536097; 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=IB6ehE4CIrRVO0upk/cDtkYMX2P2CRQHtVQu5oompks=; b=Bqd0ACNvW4HVPiVGoq5HbiTrib/E7O0OsAhC8XXzWB2LHvwnNgZRL/X1UxkeLS6h00 t/ZLl2DKaCC5nJUJ1Pqb9NnjRDv93jh/9MIbJyEIibnIJzt09cFLIO0tI9Lq5CCRsgLf 38h1bUBrCbftx2h4vxMWKLnldNFkKT9iV+Na2q9jPAJ6f4iLD+W+7QjY8MK9PPa1YxbC aVyBvYHXn2LHvMCj16WPZjsSJmde06+19SJ5ijuSHXG7fGfnZdBxTu9wpan7LSaLhQ06 uGvkrhYTdhSoYiOnWncpbWBXAyJ0VfT7Nd7LsRykFugXTCMt+hWv8jc9HpYcq3ze1nns d3EQ== X-Forwarded-Encrypted: i=1; AJvYcCVbSNionkBMaEzu/spxm37XWh5LEpmucysiCrFmWRI09Rq6IJDxtixsC9JzZEfDItxgk+ktQx1kbW1VaVs=@vger.kernel.org X-Gm-Message-State: AOJu0YyH5tGnU0hUUKK2nKUeIzuoaE/L667Pyt3XqXPQ+3Lqcuy5f1OO mV/5i91BZJvMoxAHcPRVsjoKfQoLPGAP392vJD8cnHSxIdis8s1n82djTBs+QfWDKodHT/Gh738 R7l59pg== X-Google-Smtp-Source: AGHT+IFSvMMY0LC4wHuomXbZ/DawwgteI1xq56I/oeBQmnsf6EEf9nYOa/akkIXnOi8k2ogjeqQdR6Ooifxj X-Received: from pjbee3.prod.google.com ([2002:a17:90a:fc43:b0:308:6685:55e6]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:380b:b0:2f6:d266:f45e with SMTP id 98e67ed59e1d1-3087bb3e794mr1100495a91.2.1744931297373; Thu, 17 Apr 2025 16:08:17 -0700 (PDT) Date: Thu, 17 Apr 2025 16:07:32 -0700 In-Reply-To: <20250417230740.86048-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: <20250417230740.86048-1-irogers@google.com> X-Mailer: git-send-email 2.49.0.805.g082f7c87e0-goog Message-ID: <20250417230740.86048-12-irogers@google.com> Subject: [PATCH v4 11/19] perf llvm: Disassemble cleanup 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move the 3 LLVM initialization routines to be called in a single init_llvm function that has its own bool to avoid repeated initialization. Reduce the scope of triplet and avoid copying strings for x86. Signed-off-by: Ian Rogers --- tools/perf/util/llvm.c | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/tools/perf/util/llvm.c b/tools/perf/util/llvm.c index a0774373f0d6..a28f130c8951 100644 --- a/tools/perf/util/llvm.c +++ b/tools/perf/util/llvm.c @@ -244,6 +244,17 @@ static void perf_LLVMDisasmDispose(LLVMDisasmContextRe= f context) #endif } =20 +static void init_llvm(void) +{ + static bool init; + + if (!init) { + perf_LLVMInitializeAllTargetInfos(); + perf_LLVMInitializeAllTargetMCs(); + perf_LLVMInitializeAllDisassemblers(); + init =3D true; + } +} =20 static void free_llvm_inline_frames(struct llvm_a2l_frame *inline_frames, int num_frames) @@ -339,7 +350,6 @@ int symbol__disassemble_llvm(const char *filename, stru= ct symbol *sym, u64 buf_len; u64 pc; bool is_64bit; - char triplet[64]; char disasm_buf[2048]; size_t disasm_len; struct disasm_line *dl; @@ -352,27 +362,25 @@ int symbol__disassemble_llvm(const char *filename, st= ruct symbol *sym, if (args->options->objdump_path) return -1; =20 - perf_LLVMInitializeAllTargetInfos(); - perf_LLVMInitializeAllTargetMCs(); - perf_LLVMInitializeAllDisassemblers(); - buf =3D dso__read_symbol(dso, filename, map, sym, &code_buf, &buf_len, &is_64bit); if (buf =3D=3D NULL) return -1; =20 + init_llvm(); if (arch__is(args->arch, "x86")) { - if (is_64bit) - scnprintf(triplet, sizeof(triplet), "x86_64-pc-linux"); - else - scnprintf(triplet, sizeof(triplet), "i686-pc-linux"); + const char *triplet =3D is_64bit ? "x86_64-pc-linux" : "i686-pc-linux"; + + disasm =3D perf_LLVMCreateDisasm(triplet, &storage, /*tag_type=3D*/0, + /*get_op_info=3D*/NULL, symbol_lookup_callback); } else { + char triplet[64]; + scnprintf(triplet, sizeof(triplet), "%s-linux-gnu", args->arch->name); + disasm =3D perf_LLVMCreateDisasm(triplet, &storage, /*tag_type=3D*/0, + /*get_op_info=3D*/NULL, symbol_lookup_callback); } - - disasm =3D perf_LLVMCreateDisasm(triplet, &storage, 0, NULL, - symbol_lookup_callback); if (disasm =3D=3D NULL) goto err; =20 --=20 2.49.0.805.g082f7c87e0-goog From nobody Fri Dec 19 06:33:53 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 3332722AE4E for ; Thu, 17 Apr 2025 23:08:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744931302; cv=none; b=dbXkQf+TWkUJ20b70w3lux3y9Yh/3JPSdLBJouqf8THjZwQmPWW4eWFsn2zYxmMYMllcHvcyY0H0/hrOuDTR8wqTFD7vn2v7GSI8mZk1YXvH8QkX5ZfwKk/nLOtFDNcI4n3wpQ91FOsRVGpFHX8QW/FP8o1nJ0vkZQPNVZh/WFA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744931302; c=relaxed/simple; bh=r/o/FTdC44ei+b0RvMj4m3YZsDqShIjlMhUtWnKQGP0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=ABAXEC2IOnzr8j6I7HapbHzBfGT3LzngMJS1VTQlqAJv87KTWgus/9Z1xLS5yxeoWDIAvp/v4mc1O4ou9rGQCpDz2S69f4PfKhzgIlpJgp6fhsI2WN+YDHegrDeTdFXsiFj38PZNDYyZAjJzwG+xAhteB8NY3INOFXaPOgFMNts= 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=j1r67Gc1; arc=none smtp.client-ip=209.85.216.74 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="j1r67Gc1" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-3086107d023so1287667a91.2 for ; Thu, 17 Apr 2025 16:08:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1744931299; x=1745536099; 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=EHDpsk8SNrUR44AfS89J23CqH741UDdp/AyDnz+ulKM=; b=j1r67Gc1Jzt+ERQ5lTOFPuYRllOZJgSLfD97cO7RV6tCEqj8ZFoQhHYdmqSB8GvaU6 SawuumxS1mlhaYIRowIrRNjsRuRCceNmGP9CXQ/+cDDq5O8kqOSqzd5/eUJOrwl8C445 4drKxsGvlenA4I1TV+DoEWEelmqafEZiSG3n7763ZNAPt4kNcLPyLaPADe//jIQlVj6o VLFSW2LfVfq75mdyvE9nxgDO8r5VAXGKMMtDD1P4eUcxXXRCA/BP+TKc8+Rwnhzm1WfS Ut62JPwQifTd4HHE8EdEX0t9ZGM0jXqDqwHltgg2CAMSuReJDfuQoJmVxykYbTUJZxvS Ag6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744931299; x=1745536099; 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=EHDpsk8SNrUR44AfS89J23CqH741UDdp/AyDnz+ulKM=; b=PvA783diDSrugnSgRtQPwU9RnNJfKzkCwqjEQ4uyYw5p4UY9izDKIuAn7pdOcMwMri xBfEWaeQI+0K0uVDqtlGIKDJW5828RbwSA6mNJfdEL2gp/4kUeotnKi/JuKSqkzxPNYS /LPfrldBkdmrIHKqsADGWBGXzeA6G8t7bIC6vIr+X1xCX5MnEfSTKuZPeFVGEHAI1DfL ZnasawUHzjebwVmWgiDCCPhMYuICB3PU0jZ3zwHOFxzHJ976VU3ccuSECuDGHpA2pV// HkUiiklGw+zmuEakkkwOuaFYxmo4/i4uSFdhUnqUwvqnC68K9si0SSsDzw94II9JMA7J x3kg== X-Forwarded-Encrypted: i=1; AJvYcCVVo5lAZZ3PvYwlX7tlHHJm5jsmo0Y6PefeVnth/0OBTy61W+YsYSvYrwq2DjF+21u+oayBPhavNkUXEWI=@vger.kernel.org X-Gm-Message-State: AOJu0YyBo8eilWDnGXc11HECrYsLTe6GMgn95rV+xqeDFD9rApvxU+ho GvhkoL8S3VAxxjwoNWczumIBAq0ku0lbpJFxAQ0Y3iy6uJSyau9G/NS4xnL1okGMSuKAPYM2tpd mLTYqvQ== X-Google-Smtp-Source: AGHT+IFYwuA7D18/EJ4yrdKqcla6KAmqMKe4gcSykzoeeKn1WlVOiGHm924Np2e3rbawCT909KQILgiK9qYN X-Received: from pjx16.prod.google.com ([2002:a17:90b:5690:b0:2ff:852c:ceb8]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3b8d:b0:2ee:aed2:c15c with SMTP id 98e67ed59e1d1-3087bbaeb98mr890482a91.28.1744931299447; Thu, 17 Apr 2025 16:08:19 -0700 (PDT) Date: Thu, 17 Apr 2025 16:07:33 -0700 In-Reply-To: <20250417230740.86048-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: <20250417230740.86048-1-irogers@google.com> X-Mailer: git-send-email 2.49.0.805.g082f7c87e0-goog Message-ID: <20250417230740.86048-13-irogers@google.com> Subject: [PATCH v4 12/19] perf dso: Clean up read_symbol error handling 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Ensure errno is set and return to caller for error handling. Unusually for perf the value isn't negated as expected by symbol__strerror_disassemble. Signed-off-by: Ian Rogers --- tools/perf/util/capstone.c | 3 ++- tools/perf/util/dso.c | 14 ++++++++++++-- tools/perf/util/llvm.c | 3 ++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/tools/perf/util/capstone.c b/tools/perf/util/capstone.c index f103988184c8..032548828925 100644 --- a/tools/perf/util/capstone.c +++ b/tools/perf/util/capstone.c @@ -12,6 +12,7 @@ #include "symbol.h" #include "thread.h" #include +#include #include #include #include @@ -463,7 +464,7 @@ int symbol__disassemble_capstone(const char *filename _= _maybe_unused, buf =3D dso__read_symbol(dso, filename, map, sym, &code_buf, &buf_len, &is_64bit); if (buf =3D=3D NULL) - return -1; + return errno; =20 /* add the function address and name */ scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:", diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c index d08e2a1f49c1..243fc33cc449 100644 --- a/tools/perf/util/dso.c +++ b/tools/perf/util/dso.c @@ -1809,6 +1809,7 @@ const u8 *dso__read_symbol(struct dso *dso, const cha= r *symfs_filename, =20 if (dso__binary_type(dso) =3D=3D DSO_BINARY_TYPE__BPF_IMAGE) { pr_debug("No BPF image disassembly support\n"); + errno =3D EOPNOTSUPP; return NULL; } else if (dso__binary_type(dso) =3D=3D DSO_BINARY_TYPE__BPF_PROG_INFO) { #ifdef HAVE_LIBBPF_SUPPORT @@ -1827,6 +1828,7 @@ const u8 *dso__read_symbol(struct dso *dso, const cha= r *symfs_filename, assert(len <=3D info_linear->info.jited_prog_len); #else pr_debug("No BPF program disassembly support\n"); + errno =3D EOPNOTSUPP; return NULL; #endif } else { @@ -1837,26 +1839,34 @@ const u8 *dso__read_symbol(struct dso *dso, const c= har *symfs_filename, .ip =3D start, }; u8 *code_buf =3D NULL; + int saved_errno; =20 nsinfo__mountns_enter(dso__nsinfo(dso), &nsc); fd =3D open(symfs_filename, O_RDONLY); + saved_errno =3D errno; nsinfo__mountns_exit(&nsc); - if (fd < 0) + if (fd < 0) { + errno =3D saved_errno; return NULL; + } =20 - if (file__read_maps(fd, /*exe=3D*/true, find_file_offset, &data, is_64bi= t) =3D=3D 0) { + if (file__read_maps(fd, /*exe=3D*/true, find_file_offset, &data, is_64bi= t) <=3D 0) { close(fd); + errno =3D ENOENT; return NULL; } buf =3D code_buf =3D malloc(len); if (buf =3D=3D NULL) { close(fd); + errno =3D ENOMEM; return NULL; } count =3D pread(fd, code_buf, len, data.offset); + saved_errno =3D errno; close(fd); if ((u64)count !=3D len) { free(code_buf); + errno =3D saved_errno; return NULL; } *out_buf =3D code_buf; diff --git a/tools/perf/util/llvm.c b/tools/perf/util/llvm.c index a28f130c8951..1607364ee736 100644 --- a/tools/perf/util/llvm.c +++ b/tools/perf/util/llvm.c @@ -9,6 +9,7 @@ #include "srcline.h" #include "symbol.h" #include +#include #include #include #include @@ -365,7 +366,7 @@ int symbol__disassemble_llvm(const char *filename, stru= ct symbol *sym, buf =3D dso__read_symbol(dso, filename, map, sym, &code_buf, &buf_len, &is_64bit); if (buf =3D=3D NULL) - return -1; + return errno; =20 init_llvm(); if (arch__is(args->arch, "x86")) { --=20 2.49.0.805.g082f7c87e0-goog From nobody Fri Dec 19 06:33:53 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 0F18222C331 for ; Thu, 17 Apr 2025 23:08:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744931305; cv=none; b=ooGLz3PucL5+mt9rn8gzd4nQVIS7QEmWvGeW2jJxURXKLfFVFWmGspqZNbpMi6dyqT9ckyks723ZuCJ1zH+GpV1NuOPtgoMd0MpgRH4XQePKqDFjum9cu4zUh01hp9bc8fTzj+0Avn940dTgDS0lDbonvb/yAYaxhKQ+TjyEwKE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744931305; c=relaxed/simple; bh=25x/AujeW5nDLYelvwj6drHPTa3E+2OuB8Le1DM9/Xw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=HRcHPWFcyFY62SHws9IbmOiGmjx1/qXkn6AIt2ldUc0qrZxnrhwVEVNMNxd1TBb/TalmOD0eAoSiS1EJVAUnUVpmxB1PfBmTk/OGtdvngkivcMyrU+rlhUutjosQoFcFnYuFkDhZiAWqFK/MDuxG6vb100ZfloFUpAdjSbulTPo= 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=QHNr6ZAs; arc=none smtp.client-ip=209.85.216.74 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="QHNr6ZAs" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ff7f9a0b9bso1168475a91.0 for ; Thu, 17 Apr 2025 16:08:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1744931301; x=1745536101; 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=AmummREPKKhonQ8xUxtdoXmFzxg2XSGtVqwMCd14/PM=; b=QHNr6ZAs1Fnuu2HPJDNz9NkD2ZR3Dp6cgG4fyiNdem6QCDyx/MILrNTEtB/veK5awK xFXcwrOGK2JIcxs0z8PWT8iGSu2JkH89bnGtuN6I3IQG+WufLD8rytqI5oHyQG/rcQK1 TNb6q9DoL4U7t3JdgJuUh5dpj7R2CuZcpIYv0oc19Q4wG9pBVD7sPNoRpsCEK6RSuvMP m0qMafAaIY7CaZbUCCzyCKSzuh6LnbmEBIuJDxOe6lFPfZlVTU+kouBsvG1A7Eag9ETh V6PNjfjhB/Ho1+2toxP7Nsmt8fh6AP6t9LITFCwzXtTGaFiltdG/LsirM4Lk/dYGHBoe 8H0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744931301; x=1745536101; 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=AmummREPKKhonQ8xUxtdoXmFzxg2XSGtVqwMCd14/PM=; b=PAr2m0t65I3dajfSmrdlXUQQFQkNQM5lrqsK0QSzKCQwYOGQOIYuvk2ViWt5M+I9fU dgEKLzv1R793DkFJj+5w1b39ltc9ReJIfJbAtg26Dq7ky1rNHDI+xQDTp/XCnIlP8+Dc FNArKniN6Dh35eyNKtsUz/CaEcI8Uvxlos9/inV0hftFVjqcwxf46eAoWcxZjE8PY4As v1ES72YsPiBxYHANNzL95v9C+275n5PMwtmhLSoYmfKp0hs3BCDIKB9CfOyCzNByhSHW CCvXRvjHRFbWrrKNrnrqH/KmgqfGQGWRCMrJLHQaAAErU0cs3NODe+q+4u0Apcxk5XPI X+RQ== X-Forwarded-Encrypted: i=1; AJvYcCWBsVQMIasGraTIhYux/s1k6Qr91hKe6tcMMB8mj3UtfSPdEgr2ttdUxKayZ3GMqnHMdr4tKgG6jDZg4FU=@vger.kernel.org X-Gm-Message-State: AOJu0YxmWR95JaevkJFb9UZN/0lUVd8DQDQBqN89QMIPGMoboDvt1DLD Hcl2kytnwIDbBIMa1gIEsbXVK6Vvt7a11YrD4/E+m8jNO6hYabfG8UboqwtrnAiWsbnzIkdc8V6 IhxqSog== X-Google-Smtp-Source: AGHT+IGqB1WwW6If7efO0pd8CKx7TlogkznST2W0aXTmZ1gPbunuJU9lifedmeYF4zBLvHIC9jurUMdP42p1 X-Received: from pjv12.prod.google.com ([2002:a17:90b:564c:b0:2f4:465d:5c61]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:1fc8:b0:2ff:6ac2:c5a6 with SMTP id 98e67ed59e1d1-3087bbb6df5mr907017a91.31.1744931301488; Thu, 17 Apr 2025 16:08:21 -0700 (PDT) Date: Thu, 17 Apr 2025 16:07:34 -0700 In-Reply-To: <20250417230740.86048-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: <20250417230740.86048-1-irogers@google.com> X-Mailer: git-send-email 2.49.0.805.g082f7c87e0-goog Message-ID: <20250417230740.86048-14-irogers@google.com> Subject: [PATCH v4 13/19] perf build: Remove libbfd support 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" libbfd is license incompatible with perf and building requires the BUILD_NONDISTRO=3D1 build flag. Remove the code to simplify the code base. Signed-off-by: Ian Rogers --- tools/perf/Documentation/perf-check.txt | 1 - tools/perf/Makefile.config | 38 +--- tools/perf/builtin-check.c | 1 - tools/perf/tests/Build | 1 - tools/perf/tests/builtin-test.c | 1 - tools/perf/tests/pe-file-parsing.c | 101 ---------- tools/perf/tests/tests.h | 1 - tools/perf/util/demangle-cxx.cpp | 13 +- tools/perf/util/disasm_bpf.c | 166 ---------------- tools/perf/util/srcline.c | 243 +----------------------- tools/perf/util/symbol-elf.c | 86 +-------- tools/perf/util/symbol.c | 135 ------------- tools/perf/util/symbol.h | 4 - 13 files changed, 7 insertions(+), 784 deletions(-) delete mode 100644 tools/perf/tests/pe-file-parsing.c diff --git a/tools/perf/Documentation/perf-check.txt b/tools/perf/Documenta= tion/perf-check.txt index a764a4629220..f6ec81ad87b2 100644 --- a/tools/perf/Documentation/perf-check.txt +++ b/tools/perf/Documentation/perf-check.txt @@ -51,7 +51,6 @@ feature:: dwarf_getlocations / HAVE_LIBDW_SUPPORT dwarf-unwind / HAVE_DWARF_UNWIND_SUPPORT auxtrace / HAVE_AUXTRACE_SUPPORT - libbfd / HAVE_LIBBFD_SUPPORT libcapstone / HAVE_LIBCAPSTONE_SUPPORT libcrypto / HAVE_LIBCRYPTO_SUPPORT libdw-dwarf-unwind / HAVE_LIBDW_SUPPORT diff --git a/tools/perf/Makefile.config b/tools/perf/Makefile.config index d6d8c6b970e3..870451929771 100644 --- a/tools/perf/Makefile.config +++ b/tools/perf/Makefile.config @@ -916,42 +916,6 @@ ifneq ($(NO_JEVENTS),1) endif endif =20 -ifdef BUILD_NONDISTRO - ifeq ($(feature-libbfd), 1) - EXTLIBS +=3D -lbfd -lopcodes - else - # we are on a system that requires -liberty and (maybe) -lz - # to link against -lbfd; test each case individually here - - # call all detections now so we get correct - # status in VF output - $(call feature_check,libbfd-liberty) - $(call feature_check,libbfd-liberty-z) - - ifeq ($(feature-libbfd-liberty), 1) - EXTLIBS +=3D -lbfd -lopcodes -liberty - FEATURE_CHECK_LDFLAGS-disassembler-four-args +=3D -liberty -ldl - FEATURE_CHECK_LDFLAGS-disassembler-init-styled +=3D -liberty -ldl - else - ifeq ($(feature-libbfd-liberty-z), 1) - EXTLIBS +=3D -lbfd -lopcodes -liberty -lz - FEATURE_CHECK_LDFLAGS-disassembler-four-args +=3D -liberty -lz -ldl - FEATURE_CHECK_LDFLAGS-disassembler-init-styled +=3D -liberty -lz -= ldl - endif - endif - $(call feature_check,disassembler-four-args) - $(call feature_check,disassembler-init-styled) - endif - - CFLAGS +=3D -DHAVE_LIBBFD_SUPPORT - CXXFLAGS +=3D -DHAVE_LIBBFD_SUPPORT - ifeq ($(feature-libbfd-buildid), 1) - CFLAGS +=3D -DHAVE_LIBBFD_BUILDID_SUPPORT - else - $(warning Old version of libbfd/binutils things like PE executable pro= filing will not be available) - endif -endif - ifndef NO_LIBLLVM $(call feature_check,llvm-perf) ifeq ($(feature-llvm-perf), 1) @@ -1336,6 +1300,6 @@ endif =20 # re-generate FEATURE-DUMP as we may have called feature_check, found out # extra libraries to add to LDFLAGS of some other test and then redo those -# tests, see the block about libbfd, disassembler-four-args, for instance. +# tests, see the block about disassembler-four-args, for instance. $(shell rm -f $(FEATURE_DUMP_FILENAME)) $(foreach feat,$(FEATURE_TESTS),$(shell echo "$(call feature_assign,$(feat= ))" >> $(FEATURE_DUMP_FILENAME))) diff --git a/tools/perf/builtin-check.c b/tools/perf/builtin-check.c index 9a509cb3bb9a..6724dc45b609 100644 --- a/tools/perf/builtin-check.c +++ b/tools/perf/builtin-check.c @@ -42,7 +42,6 @@ struct feature_status supported_features[] =3D { FEATURE_STATUS("dwarf_getlocations", HAVE_LIBDW_SUPPORT), FEATURE_STATUS("dwarf-unwind", HAVE_DWARF_UNWIND_SUPPORT), FEATURE_STATUS("auxtrace", HAVE_AUXTRACE_SUPPORT), - FEATURE_STATUS_TIP("libbfd", HAVE_LIBBFD_SUPPORT, "Deprecated, license in= compatibility, use BUILD_NONDISTRO=3D1 and install binutils-dev[el]"), FEATURE_STATUS("libcapstone", HAVE_LIBCAPSTONE_SUPPORT), FEATURE_STATUS("libcrypto", HAVE_LIBCRYPTO_SUPPORT), FEATURE_STATUS("libdw-dwarf-unwind", HAVE_LIBDW_SUPPORT), diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build index 934f32090553..1ded1776aebc 100644 --- a/tools/perf/tests/Build +++ b/tools/perf/tests/Build @@ -58,7 +58,6 @@ perf-test-y +=3D demangle-java-test.o perf-test-y +=3D demangle-ocaml-test.o perf-test-y +=3D pfm.o perf-test-y +=3D parse-metric.o -perf-test-y +=3D pe-file-parsing.o perf-test-y +=3D expand-cgroup.o perf-test-y +=3D perf-time-to-tsc.o perf-test-y +=3D dlfilter-test.o diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-tes= t.c index 14d30a5053be..e77bf446e821 100644 --- a/tools/perf/tests/builtin-test.c +++ b/tools/perf/tests/builtin-test.c @@ -127,7 +127,6 @@ static struct test_suite *generic_tests[] =3D { &suite__demangle_java, &suite__demangle_ocaml, &suite__parse_metric, - &suite__pe_file_parsing, &suite__expand_cgroup_events, &suite__perf_time_to_tsc, &suite__dlfilter, diff --git a/tools/perf/tests/pe-file-parsing.c b/tools/perf/tests/pe-file-= parsing.c deleted file mode 100644 index fff58b220c07..000000000000 --- a/tools/perf/tests/pe-file-parsing.c +++ /dev/null @@ -1,101 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "debug.h" -#include "util/build-id.h" -#include "util/symbol.h" -#include "util/dso.h" - -#include "tests.h" - -#ifdef HAVE_LIBBFD_SUPPORT - -static int run_dir(const char *d) -{ - char filename[PATH_MAX]; - char debugfile[PATH_MAX]; - struct build_id bid; - char debuglink[PATH_MAX]; - char expect_build_id[] =3D { - 0x5a, 0x0f, 0xd8, 0x82, 0xb5, 0x30, 0x84, 0x22, - 0x4b, 0xa4, 0x7b, 0x62, 0x4c, 0x55, 0xa4, 0x69, - }; - char expect_debuglink[PATH_MAX] =3D "pe-file.exe.debug"; - struct dso *dso; - struct symbol *sym; - int ret; - size_t idx; - - scnprintf(filename, PATH_MAX, "%s/pe-file.exe", d); - ret =3D filename__read_build_id(filename, &bid); - TEST_ASSERT_VAL("Failed to read build_id", - ret =3D=3D sizeof(expect_build_id)); - TEST_ASSERT_VAL("Wrong build_id", !memcmp(bid.data, expect_build_id, - sizeof(expect_build_id))); - - ret =3D filename__read_debuglink(filename, debuglink, PATH_MAX); - TEST_ASSERT_VAL("Failed to read debuglink", ret =3D=3D 0); - TEST_ASSERT_VAL("Wrong debuglink", - !strcmp(debuglink, expect_debuglink)); - - scnprintf(debugfile, PATH_MAX, "%s/%s", d, debuglink); - ret =3D filename__read_build_id(debugfile, &bid); - TEST_ASSERT_VAL("Failed to read debug file build_id", - ret =3D=3D sizeof(expect_build_id)); - TEST_ASSERT_VAL("Wrong build_id", !memcmp(bid.data, expect_build_id, - sizeof(expect_build_id))); - - dso =3D dso__new(filename); - TEST_ASSERT_VAL("Failed to get dso", dso); - - ret =3D dso__load_bfd_symbols(dso, debugfile); - TEST_ASSERT_VAL("Failed to load symbols", ret =3D=3D 0); - - dso__sort_by_name(dso); - sym =3D dso__find_symbol_by_name(dso, "main", &idx); - TEST_ASSERT_VAL("Failed to find main", sym); - dso__delete(dso); - - return TEST_OK; -} - -static int test__pe_file_parsing(struct test_suite *test __maybe_unused, - int subtest __maybe_unused) -{ - struct stat st; - char path_dir[PATH_MAX]; - - /* First try development tree tests. */ - if (!lstat("./tests", &st)) - return run_dir("./tests"); - - /* Then installed path. */ - snprintf(path_dir, PATH_MAX, "%s/tests", get_argv_exec_path()); - - if (!lstat(path_dir, &st)) - return run_dir(path_dir); - - return TEST_SKIP; -} - -#else - -static int test__pe_file_parsing(struct test_suite *test __maybe_unused, - int subtest __maybe_unused) -{ - return TEST_SKIP; -} - -#endif - -DEFINE_SUITE("PE file support", pe_file_parsing); diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h index 8aea344536b8..751c8489059a 100644 --- a/tools/perf/tests/tests.h +++ b/tools/perf/tests/tests.h @@ -159,7 +159,6 @@ DECLARE_SUITE(demangle_java); DECLARE_SUITE(demangle_ocaml); DECLARE_SUITE(pfm); DECLARE_SUITE(parse_metric); -DECLARE_SUITE(pe_file_parsing); DECLARE_SUITE(expand_cgroup_events); DECLARE_SUITE(perf_time_to_tsc); DECLARE_SUITE(dlfilter); diff --git a/tools/perf/util/demangle-cxx.cpp b/tools/perf/util/demangle-cx= x.cpp index 85b706641837..bd657eb37efc 100644 --- a/tools/perf/util/demangle-cxx.cpp +++ b/tools/perf/util/demangle-cxx.cpp @@ -4,16 +4,11 @@ #include #include =20 -#ifdef HAVE_LIBBFD_SUPPORT -#define PACKAGE 'perf' -#include -#endif - #ifdef HAVE_CXA_DEMANGLE_SUPPORT #include #endif =20 -#if defined(HAVE_LIBBFD_SUPPORT) || defined(HAVE_CPLUS_DEMANGLE_SUPPORT) +#if defined(HAVE_CPLUS_DEMANGLE_SUPPORT) #ifndef DMGL_PARAMS #define DMGL_PARAMS (1 << 0) /* Include function args */ #define DMGL_ANSI (1 << 1) /* Include const, volatile, etc */ @@ -29,11 +24,7 @@ extern "C" char *cxx_demangle_sym(const char *str, bool params __maybe_unused, bool modifiers __maybe_unused) { -#ifdef HAVE_LIBBFD_SUPPORT - int flags =3D (params ? DMGL_PARAMS : 0) | (modifiers ? DMGL_ANSI = : 0); - - return bfd_demangle(NULL, str, flags); -#elif defined(HAVE_CPLUS_DEMANGLE_SUPPORT) +#if defined(HAVE_CPLUS_DEMANGLE_SUPPORT) int flags =3D (params ? DMGL_PARAMS : 0) | (modifiers ? DMGL_ANSI = : 0); =20 return cplus_demangle(str, flags); diff --git a/tools/perf/util/disasm_bpf.c b/tools/perf/util/disasm_bpf.c index 1fee71c79b62..a891a0b909a7 100644 --- a/tools/perf/util/disasm_bpf.c +++ b/tools/perf/util/disasm_bpf.c @@ -6,176 +6,10 @@ #include #include =20 -#if defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBBPF_SUPPORT) -#define PACKAGE "perf" -#include -#include -#include -#include -#include -#include -#include -#include - -#include "util/bpf-event.h" -#include "util/bpf-utils.h" -#include "util/debug.h" -#include "util/dso.h" -#include "util/map.h" -#include "util/env.h" -#include "util/util.h" - -int symbol__disassemble_bpf(struct symbol *sym, struct annotate_args *args) -{ - struct annotation *notes =3D symbol__annotation(sym); - struct bpf_prog_linfo *prog_linfo =3D NULL; - struct bpf_prog_info_node *info_node; - int len =3D sym->end - sym->start; - disassembler_ftype disassemble; - struct map *map =3D args->ms.map; - struct perf_bpil *info_linear; - struct disassemble_info info; - struct dso *dso =3D map__dso(map); - int pc =3D 0, count, sub_id; - struct btf *btf =3D NULL; - char tpath[PATH_MAX]; - size_t buf_size; - int nr_skip =3D 0; - char *buf; - bfd *bfdf; - int ret; - FILE *s; - - if (dso__binary_type(dso) !=3D DSO_BINARY_TYPE__BPF_PROG_INFO) - return SYMBOL_ANNOTATE_ERRNO__BPF_INVALID_FILE; - - pr_debug("%s: handling sym %s addr %" PRIx64 " len %" PRIx64 "\n", __func= __, - sym->name, sym->start, sym->end - sym->start); - - memset(tpath, 0, sizeof(tpath)); - perf_exe(tpath, sizeof(tpath)); - - bfdf =3D bfd_openr(tpath, NULL); - if (bfdf =3D=3D NULL) - abort(); - - if (!bfd_check_format(bfdf, bfd_object)) - abort(); - - s =3D open_memstream(&buf, &buf_size); - if (!s) { - ret =3D errno; - goto out; - } - init_disassemble_info_compat(&info, s, - (fprintf_ftype) fprintf, - fprintf_styled); - info.arch =3D bfd_get_arch(bfdf); - info.mach =3D bfd_get_mach(bfdf); - - info_node =3D perf_env__find_bpf_prog_info(dso__bpf_prog(dso)->env, - dso__bpf_prog(dso)->id); - if (!info_node) { - ret =3D SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF; - goto out; - } - info_linear =3D info_node->info_linear; - sub_id =3D dso__bpf_prog(dso)->sub_id; - - info.buffer =3D (void *)(uintptr_t)(info_linear->info.jited_prog_insns); - info.buffer_length =3D info_linear->info.jited_prog_len; - - if (info_linear->info.nr_line_info) - prog_linfo =3D bpf_prog_linfo__new(&info_linear->info); - - if (info_linear->info.btf_id) { - struct btf_node *node; - - node =3D perf_env__find_btf(dso__bpf_prog(dso)->env, - info_linear->info.btf_id); - if (node) - btf =3D btf__new((__u8 *)(node->data), - node->data_size); - } - - disassemble_init_for_target(&info); - -#ifdef DISASM_FOUR_ARGS_SIGNATURE - disassemble =3D disassembler(info.arch, - bfd_big_endian(bfdf), - info.mach, - bfdf); -#else - disassemble =3D disassembler(bfdf); -#endif - if (disassemble =3D=3D NULL) - abort(); - - fflush(s); - do { - const struct bpf_line_info *linfo =3D NULL; - struct disasm_line *dl; - size_t prev_buf_size; - const char *srcline; - u64 addr; - - addr =3D pc + ((u64 *)(uintptr_t)(info_linear->info.jited_ksyms))[sub_id= ]; - count =3D disassemble(pc, &info); - - if (prog_linfo) - linfo =3D bpf_prog_linfo__lfind_addr_func(prog_linfo, - addr, sub_id, - nr_skip); - - if (linfo && btf) { - srcline =3D btf__name_by_offset(btf, linfo->line_off); - nr_skip++; - } else - srcline =3D NULL; - - fprintf(s, "\n"); - prev_buf_size =3D buf_size; - fflush(s); - - if (!annotate_opts.hide_src_code && srcline) { - args->offset =3D -1; - args->line =3D strdup(srcline); - args->line_nr =3D 0; - args->fileloc =3D NULL; - args->ms.sym =3D sym; - dl =3D disasm_line__new(args); - if (dl) { - annotation_line__add(&dl->al, - ¬es->src->source); - } - } - - args->offset =3D pc; - args->line =3D buf + prev_buf_size; - args->line_nr =3D 0; - args->fileloc =3D NULL; - args->ms.sym =3D sym; - dl =3D disasm_line__new(args); - if (dl) - annotation_line__add(&dl->al, ¬es->src->source); - - pc +=3D count; - } while (count > 0 && pc < len); - - ret =3D 0; -out: - free(prog_linfo); - btf__free(btf); - fclose(s); - bfd_close(bfdf); - return ret; -} -#else // defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBBPF_SUPPORT) int symbol__disassemble_bpf(struct symbol *sym __maybe_unused, struct anno= tate_args *args __maybe_unused) { return SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF; } -#endif // defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBBPF_SUPPORT) =20 int symbol__disassemble_bpf_image(struct symbol *sym, struct annotate_args= *args) { diff --git a/tools/perf/util/srcline.c b/tools/perf/util/srcline.c index 26fd55455efd..797e78826508 100644 --- a/tools/perf/util/srcline.c +++ b/tools/perf/util/srcline.c @@ -146,247 +146,8 @@ void dso__free_a2l(struct dso *dso) { dso__free_a2l_llvm(dso); } -#elif defined(HAVE_LIBBFD_SUPPORT) =20 -/* - * Implement addr2line using libbfd. - */ -#define PACKAGE "perf" -#include - -struct a2l_data { - const char *input; - u64 addr; - - bool found; - const char *filename; - const char *funcname; - unsigned line; - - bfd *abfd; - asymbol **syms; -}; - -static int bfd_error(const char *string) -{ - const char *errmsg; - - errmsg =3D bfd_errmsg(bfd_get_error()); - fflush(stdout); - - if (string) - pr_debug("%s: %s\n", string, errmsg); - else - pr_debug("%s\n", errmsg); - - return -1; -} - -static int slurp_symtab(bfd *abfd, struct a2l_data *a2l) -{ - long storage; - long symcount; - asymbol **syms; - bfd_boolean dynamic =3D FALSE; - - if ((bfd_get_file_flags(abfd) & HAS_SYMS) =3D=3D 0) - return bfd_error(bfd_get_filename(abfd)); - - storage =3D bfd_get_symtab_upper_bound(abfd); - if (storage =3D=3D 0L) { - storage =3D bfd_get_dynamic_symtab_upper_bound(abfd); - dynamic =3D TRUE; - } - if (storage < 0L) - return bfd_error(bfd_get_filename(abfd)); - - syms =3D malloc(storage); - if (dynamic) - symcount =3D bfd_canonicalize_dynamic_symtab(abfd, syms); - else - symcount =3D bfd_canonicalize_symtab(abfd, syms); - - if (symcount < 0) { - free(syms); - return bfd_error(bfd_get_filename(abfd)); - } - - a2l->syms =3D syms; - return 0; -} - -static void find_address_in_section(bfd *abfd, asection *section, void *da= ta) -{ - bfd_vma pc, vma; - bfd_size_type size; - struct a2l_data *a2l =3D data; - flagword flags; - - if (a2l->found) - return; - -#ifdef bfd_get_section_flags - flags =3D bfd_get_section_flags(abfd, section); -#else - flags =3D bfd_section_flags(section); -#endif - if ((flags & SEC_ALLOC) =3D=3D 0) - return; - - pc =3D a2l->addr; -#ifdef bfd_get_section_vma - vma =3D bfd_get_section_vma(abfd, section); -#else - vma =3D bfd_section_vma(section); -#endif -#ifdef bfd_get_section_size - size =3D bfd_get_section_size(section); -#else - size =3D bfd_section_size(section); -#endif - - if (pc < vma || pc >=3D vma + size) - return; - - a2l->found =3D bfd_find_nearest_line(abfd, section, a2l->syms, pc - vma, - &a2l->filename, &a2l->funcname, - &a2l->line); - - if (a2l->filename && !strlen(a2l->filename)) - a2l->filename =3D NULL; -} - -static struct a2l_data *addr2line_init(const char *path) -{ - bfd *abfd; - struct a2l_data *a2l =3D NULL; - - abfd =3D bfd_openr(path, NULL); - if (abfd =3D=3D NULL) - return NULL; - - if (!bfd_check_format(abfd, bfd_object)) - goto out; - - a2l =3D zalloc(sizeof(*a2l)); - if (a2l =3D=3D NULL) - goto out; - - a2l->abfd =3D abfd; - a2l->input =3D strdup(path); - if (a2l->input =3D=3D NULL) - goto out; - - if (slurp_symtab(abfd, a2l)) - goto out; - - return a2l; - -out: - if (a2l) { - zfree((char **)&a2l->input); - free(a2l); - } - bfd_close(abfd); - return NULL; -} - -static void addr2line_cleanup(struct a2l_data *a2l) -{ - if (a2l->abfd) - bfd_close(a2l->abfd); - zfree((char **)&a2l->input); - zfree(&a2l->syms); - free(a2l); -} - -static int inline_list__append_dso_a2l(struct dso *dso, - struct inline_node *node, - struct symbol *sym) -{ - struct a2l_data *a2l =3D dso__a2l(dso); - struct symbol *inline_sym =3D new_inline_sym(dso, sym, a2l->funcname); - char *srcline =3D NULL; - - if (a2l->filename) - srcline =3D srcline_from_fileline(a2l->filename, a2l->line); - - return inline_list__append(inline_sym, srcline, node); -} - -static int addr2line(const char *dso_name, u64 addr, - char **file, unsigned int *line, struct dso *dso, - bool unwind_inlines, struct inline_node *node, - struct symbol *sym) -{ - int ret =3D 0; - struct a2l_data *a2l =3D dso__a2l(dso); - - if (!a2l) { - a2l =3D addr2line_init(dso_name); - dso__set_a2l(dso, a2l); - } - - if (a2l =3D=3D NULL) { - if (!symbol_conf.disable_add2line_warn) - pr_warning("addr2line_init failed for %s\n", dso_name); - return 0; - } - - a2l->addr =3D addr; - a2l->found =3D false; - - bfd_map_over_sections(a2l->abfd, find_address_in_section, a2l); - - if (!a2l->found) - return 0; - - if (unwind_inlines) { - int cnt =3D 0; - - if (node && inline_list__append_dso_a2l(dso, node, sym)) - return 0; - - while (bfd_find_inliner_info(a2l->abfd, &a2l->filename, - &a2l->funcname, &a2l->line) && - cnt++ < MAX_INLINE_NEST) { - - if (a2l->filename && !strlen(a2l->filename)) - a2l->filename =3D NULL; - - if (node !=3D NULL) { - if (inline_list__append_dso_a2l(dso, node, sym)) - return 0; - // found at least one inline frame - ret =3D 1; - } - } - } - - if (file) { - *file =3D a2l->filename ? strdup(a2l->filename) : NULL; - ret =3D *file ? 1 : 0; - } - - if (line) - *line =3D a2l->line; - - return ret; -} - -void dso__free_a2l(struct dso *dso) -{ - struct a2l_data *a2l =3D dso__a2l(dso); - - if (!a2l) - return; - - addr2line_cleanup(a2l); - - dso__set_a2l(dso, NULL); -} - -#else /* HAVE_LIBBFD_SUPPORT */ +#else /* HAVE_LIBLLVM_SUPPORT */ =20 static int filename_split(char *filename, unsigned int *line_nr) { @@ -805,7 +566,7 @@ void dso__free_a2l(struct dso *dso) dso__set_a2l(dso, NULL); } =20 -#endif /* HAVE_LIBBFD_SUPPORT */ +#endif /* HAVE_LIBLLVM_SUPPORT */ =20 static struct inline_node *addr2inlines(const char *dso_name, u64 addr, struct dso *dso, struct symbol *sym) diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c index fbf6d0f73af9..856819bbba59 100644 --- a/tools/perf/util/symbol-elf.c +++ b/tools/perf/util/symbol-elf.c @@ -28,12 +28,7 @@ #include #include =20 -#ifdef HAVE_LIBBFD_SUPPORT -#define PACKAGE 'perf' -#include -#endif - -#if defined(HAVE_LIBBFD_SUPPORT) || defined(HAVE_CPLUS_DEMANGLE_SUPPORT) +#if defined(HAVE_CPLUS_DEMANGLE_SUPPORT) #ifndef DMGL_PARAMS #define DMGL_PARAMS (1 << 0) /* Include function args */ #define DMGL_ANSI (1 << 1) /* Include const, volatile, etc */ @@ -292,11 +287,7 @@ static bool want_demangle(bool is_kernel_sym) char *cxx_demangle_sym(const char *str __maybe_unused, bool params __maybe= _unused, bool modifiers __maybe_unused) { -#ifdef HAVE_LIBBFD_SUPPORT - int flags =3D (params ? DMGL_PARAMS : 0) | (modifiers ? DMGL_ANSI : 0); - - return bfd_demangle(NULL, str, flags); -#elif defined(HAVE_CPLUS_DEMANGLE_SUPPORT) +#if defined(HAVE_CPLUS_DEMANGLE_SUPPORT) int flags =3D (params ? DMGL_PARAMS : 0) | (modifiers ? DMGL_ANSI : 0); =20 return cplus_demangle(str, flags); @@ -936,37 +927,6 @@ static int elf_read_build_id(Elf *elf, void *bf, size_= t size) return err; } =20 -#ifdef HAVE_LIBBFD_BUILDID_SUPPORT - -static int read_build_id(const char *filename, struct build_id *bid) -{ - size_t size =3D sizeof(bid->data); - int err =3D -1; - bfd *abfd; - - abfd =3D bfd_openr(filename, NULL); - if (!abfd) - return -1; - - if (!bfd_check_format(abfd, bfd_object)) { - pr_debug2("%s: cannot read %s bfd file.\n", __func__, filename); - goto out_close; - } - - if (!abfd->build_id || abfd->build_id->size > size) - goto out_close; - - memcpy(bid->data, abfd->build_id->data, abfd->build_id->size); - memset(bid->data + abfd->build_id->size, 0, size - abfd->build_id->size); - err =3D bid->size =3D abfd->build_id->size; - -out_close: - bfd_close(abfd); - return err; -} - -#else // HAVE_LIBBFD_BUILDID_SUPPORT - static int read_build_id(const char *filename, struct build_id *bid) { size_t size =3D sizeof(bid->data); @@ -997,8 +957,6 @@ static int read_build_id(const char *filename, struct b= uild_id *bid) return err; } =20 -#endif // HAVE_LIBBFD_BUILDID_SUPPORT - int filename__read_build_id(const char *filename, struct build_id *bid) { struct kmod_path m =3D { .name =3D NULL, }; @@ -1082,44 +1040,6 @@ int sysfs__read_build_id(const char *filename, struc= t build_id *bid) return err; } =20 -#ifdef HAVE_LIBBFD_SUPPORT - -int filename__read_debuglink(const char *filename, char *debuglink, - size_t size) -{ - int err =3D -1; - asection *section; - bfd *abfd; - - abfd =3D bfd_openr(filename, NULL); - if (!abfd) - return -1; - - if (!bfd_check_format(abfd, bfd_object)) { - pr_debug2("%s: cannot read %s bfd file.\n", __func__, filename); - goto out_close; - } - - section =3D bfd_get_section_by_name(abfd, ".gnu_debuglink"); - if (!section) - goto out_close; - - if (section->size > size) - goto out_close; - - if (!bfd_get_section_contents(abfd, section, debuglink, 0, - section->size)) - goto out_close; - - err =3D 0; - -out_close: - bfd_close(abfd); - return err; -} - -#else - int filename__read_debuglink(const char *filename, char *debuglink, size_t size) { @@ -1172,8 +1092,6 @@ int filename__read_debuglink(const char *filename, ch= ar *debuglink, return err; } =20 -#endif - bool symsrc__possibly_runtime(struct symsrc *ss) { return ss->dynsym || ss->opdsec; diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index 11540219481b..dacddfa2cab2 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -1575,137 +1575,6 @@ static int dso__load_perf_map(const char *map_path,= struct dso *dso) return -1; } =20 -#ifdef HAVE_LIBBFD_SUPPORT -#define PACKAGE 'perf' -#include - -static int bfd_symbols__cmpvalue(const void *a, const void *b) -{ - const asymbol *as =3D *(const asymbol **)a, *bs =3D *(const asymbol **)b; - - if (bfd_asymbol_value(as) !=3D bfd_asymbol_value(bs)) - return bfd_asymbol_value(as) - bfd_asymbol_value(bs); - - return bfd_asymbol_name(as)[0] - bfd_asymbol_name(bs)[0]; -} - -static int bfd2elf_binding(asymbol *symbol) -{ - if (symbol->flags & BSF_WEAK) - return STB_WEAK; - if (symbol->flags & BSF_GLOBAL) - return STB_GLOBAL; - if (symbol->flags & BSF_LOCAL) - return STB_LOCAL; - return -1; -} - -int dso__load_bfd_symbols(struct dso *dso, const char *debugfile) -{ - int err =3D -1; - long symbols_size, symbols_count, i; - asection *section; - asymbol **symbols, *sym; - struct symbol *symbol; - bfd *abfd; - u64 start, len; - - abfd =3D bfd_openr(debugfile, NULL); - if (!abfd) - return -1; - - if (!bfd_check_format(abfd, bfd_object)) { - pr_debug2("%s: cannot read %s bfd file.\n", __func__, - dso__long_name(dso)); - goto out_close; - } - - if (bfd_get_flavour(abfd) =3D=3D bfd_target_elf_flavour) - goto out_close; - - symbols_size =3D bfd_get_symtab_upper_bound(abfd); - if (symbols_size =3D=3D 0) { - bfd_close(abfd); - return 0; - } - - if (symbols_size < 0) - goto out_close; - - symbols =3D malloc(symbols_size); - if (!symbols) - goto out_close; - - symbols_count =3D bfd_canonicalize_symtab(abfd, symbols); - if (symbols_count < 0) - goto out_free; - - section =3D bfd_get_section_by_name(abfd, ".text"); - if (section) { - for (i =3D 0; i < symbols_count; ++i) { - if (!strcmp(bfd_asymbol_name(symbols[i]), "__ImageBase") || - !strcmp(bfd_asymbol_name(symbols[i]), "__image_base__")) - break; - } - if (i < symbols_count) { - /* PE symbols can only have 4 bytes, so use .text high bits */ - u64 text_offset =3D (section->vma - (u32)section->vma) - + (u32)bfd_asymbol_value(symbols[i]); - dso__set_text_offset(dso, text_offset); - dso__set_text_end(dso, (section->vma - text_offset) + section->size); - } else { - dso__set_text_offset(dso, section->vma - section->filepos); - dso__set_text_end(dso, section->filepos + section->size); - } - } - - qsort(symbols, symbols_count, sizeof(asymbol *), bfd_symbols__cmpvalue); - -#ifdef bfd_get_section -#define bfd_asymbol_section bfd_get_section -#endif - for (i =3D 0; i < symbols_count; ++i) { - sym =3D symbols[i]; - section =3D bfd_asymbol_section(sym); - if (bfd2elf_binding(sym) < 0) - continue; - - while (i + 1 < symbols_count && - bfd_asymbol_section(symbols[i + 1]) =3D=3D section && - bfd2elf_binding(symbols[i + 1]) < 0) - i++; - - if (i + 1 < symbols_count && - bfd_asymbol_section(symbols[i + 1]) =3D=3D section) - len =3D symbols[i + 1]->value - sym->value; - else - len =3D section->size - sym->value; - - start =3D bfd_asymbol_value(sym) - dso__text_offset(dso); - symbol =3D symbol__new(start, len, bfd2elf_binding(sym), STT_FUNC, - bfd_asymbol_name(sym)); - if (!symbol) - goto out_free; - - symbols__insert(dso__symbols(dso), symbol); - } -#ifdef bfd_get_section -#undef bfd_asymbol_section -#endif - - symbols__fixup_end(dso__symbols(dso), false); - symbols__fixup_duplicate(dso__symbols(dso)); - dso__set_adjust_symbols(dso, true); - - err =3D 0; -out_free: - free(symbols); -out_close: - bfd_close(abfd); - return err; -} -#endif - static bool dso__is_compatible_symtab_type(struct dso *dso, bool kmod, enum dso_binary_type type) { @@ -1908,10 +1777,6 @@ int dso__load(struct dso *dso, struct map *map) } } =20 -#ifdef HAVE_LIBBFD_SUPPORT - if (is_reg) - bfdrc =3D dso__load_bfd_symbols(dso, name); -#endif if (is_reg && bfdrc < 0) sirc =3D symsrc__init(ss, dso, name, symtab_type); =20 diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h index 3fb5d146d9b1..508fd559a8a1 100644 --- a/tools/perf/util/symbol.h +++ b/tools/perf/util/symbol.h @@ -174,10 +174,6 @@ int symbol__config_symfs(const struct option *opt __ma= ybe_unused, =20 struct symsrc; =20 -#ifdef HAVE_LIBBFD_SUPPORT -int dso__load_bfd_symbols(struct dso *dso, const char *debugfile); -#endif - int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss, struct symsrc *runtime_ss, int kmodule); int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss); --=20 2.49.0.805.g082f7c87e0-goog From nobody Fri Dec 19 06:33:53 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 3E13622B8C0 for ; Thu, 17 Apr 2025 23:08:23 +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=1744931305; cv=none; b=FPq1ijIwG4BasmuRtrx8+Q7Q4j+5/WkDKYrwGQVURhuRrjXpsZNpFpF9e2vBMAbdE5uyG0TeTZZ3ov1sFgz8N+QGZRe/SYc5/yG3YXQhPXA5+A6EPNCGxxQ/v/JUYCQY7CAdouHgGZvp+ItYixrca4sGdJKQXiGdEKhDqtm0oO8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744931305; c=relaxed/simple; bh=WlrESCObcpLxlvArnGyAQPVRYZZxf1CeQ6rfvuHIhQI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=ColbxKD8J8h15z4J/xZlAqce6AodA0p3WnmriEvOELh1VU02faBOOJBCjtydWjWKdSLkTj1lDjjE+LKU4tnxsGL2XVIyF4dDVTS0+CHsTG/r4ZEkW/KX7E+31cW1aA7YAMzZHxyX82zc8QVzeN56Voz9mMuJppAehe0qNhih3d8= 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=CkQ3pIAD; 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="CkQ3pIAD" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-225429696a9so17499215ad.1 for ; Thu, 17 Apr 2025 16:08:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1744931303; x=1745536103; 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=T5IiGvkesbLwU2LRHglyxF2fGjepXBF/Pg+PTt606Ww=; b=CkQ3pIADn3jWQu7qkm9wU0o1PKJ0c0ztlV0p6bspOGprrFb/04SWyl2xIzPLzdZFyd 2nm6w9v/kovw7ePwrdlLWY32fFFabqtNY2HV07WXWVKwlcKhlrEd7HsUtuq0cOMnvWVP lDUd3Q9ECOSkfQqfwxCRzS4zb5cvJvKm78eBeTvAwYgtOMMbJju7EBcOkKp70pDTe3Do /z2LZaKRv/ANFeINMm7Dynr3FMTwOJxIKz58Ds5dGuJm5CQH0Ht1N1arYfZQHyDOjLNy l3OtZCweTwZ4bsTc3Hlqwt+ZPd6PqNx72VTssgN7f1WvKQGIiUVP/KY6FAuo+aC0dx45 1yRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744931303; x=1745536103; 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=T5IiGvkesbLwU2LRHglyxF2fGjepXBF/Pg+PTt606Ww=; b=dkwE8iFGsm77m/dHeVle5iEf0e61cL5bTSw4fuvMX5jhqgS1Rj4WRyitcxIS8b0NwV hUYFhMUB6MQU4abhsVskEjx8KZFs4Mkoz6znxLCC1uFQRgOnN8a8VA9s700ATCnoGBkx XseHiSXRkJ1oWpl2Ov0bYmKnWPCo4URZSaxdeItAboFfdWDbXmenwn9CsT1J+584RjHu QGichS7r4CAFo9qv/t8kFhEHzXxSOAk2H2Gc9kFqbghosWmmtXDhe8EI2O2TB+rFcgUl QySHreaFhvS4w9oDHKZbwnFQVgximv5MdcPjk/NfdkJyv05K7zbcdS+gmX1nVqFpzpLP OB1A== X-Forwarded-Encrypted: i=1; AJvYcCUznqwGXkOr2oLx1czUKfB530yXS4hYBqmd+w5xDlf2ObSCBtbnOM+udETXzEk1ubqaSxp1M4TflPCPI2M=@vger.kernel.org X-Gm-Message-State: AOJu0YxmeRKNR5Ea64m9VEPiEPvTRf/YaIfuZAe1IlAYc5/9IHbv7iuf 8H7IjkrtuLNQGb+Wq1xEMHGu+4ZKdGGszzx74qjEf7vQmRRtIp51qjpgAiWoi194GfK/1bdAaEj at5aFYg== X-Google-Smtp-Source: AGHT+IG5F2LOaTgg/mX1XvwpFVLktCGd2m3MhUHdYc74oU2szNJdxzhC36EWYALgHq+zFbkNF9kxs7APIa5a X-Received: from plhn17.prod.google.com ([2002:a17:903:1111:b0:21f:3ef1:c029]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:1a6b:b0:223:f408:c3e2 with SMTP id d9443c01a7336-22c5357f39cmr10467445ad.14.1744931303485; Thu, 17 Apr 2025 16:08:23 -0700 (PDT) Date: Thu, 17 Apr 2025 16:07:35 -0700 In-Reply-To: <20250417230740.86048-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: <20250417230740.86048-1-irogers@google.com> X-Mailer: git-send-email 2.49.0.805.g082f7c87e0-goog Message-ID: <20250417230740.86048-15-irogers@google.com> Subject: [PATCH v4 14/19] perf build: Remove libiberty support 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" libiberty is license incompatible with perf and building requires the BUILD_NONDISTRO=3D1 build flag. libiberty is used for HAVE_CPLUS_DEMANGLE_SUPPORT. Remove the code to simplify the code base as it can't be distributed. Remove the BUILD_NONDISTRO build flag and test as they no longer enable/disable support. Signed-off-by: Ian Rogers --- tools/perf/Makefile.config | 12 ------------ tools/perf/Makefile.perf | 11 ++++------- tools/perf/tests/make | 2 -- tools/perf/util/demangle-cxx.cpp | 13 +------------ tools/perf/util/symbol-elf.c | 13 ------------- 5 files changed, 5 insertions(+), 46 deletions(-) diff --git a/tools/perf/Makefile.config b/tools/perf/Makefile.config index 870451929771..3ed047ffb4f5 100644 --- a/tools/perf/Makefile.config +++ b/tools/perf/Makefile.config @@ -954,18 +954,6 @@ ifndef NO_DEMANGLE CXXFLAGS +=3D -DHAVE_CXA_DEMANGLE_SUPPORT $(call detected,CONFIG_CXX_DEMANGLE) endif - ifdef BUILD_NONDISTRO - ifeq ($(filter -liberty,$(EXTLIBS)),) - $(call feature_check,cplus-demangle) - ifeq ($(feature-cplus-demangle), 1) - EXTLIBS +=3D -liberty - endif - endif - ifneq ($(filter -liberty,$(EXTLIBS)),) - CFLAGS +=3D -DHAVE_CPLUS_DEMANGLE_SUPPORT - CXXFLAGS +=3D -DHAVE_CPLUS_DEMANGLE_SUPPORT - endif - endif endif =20 ifndef NO_LZMA diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf index fc65537d5821..58982d8d1014 100644 --- a/tools/perf/Makefile.perf +++ b/tools/perf/Makefile.perf @@ -123,9 +123,6 @@ include ../scripts/utilities.mak # # Set BUILD_BPF_SKEL to 0 to override BUILD_BPF_SKEL and not build BPF ske= letons # -# Define BUILD_NONDISTRO to enable building an linking against libbfd and -# libiberty distribution license incompatible libraries. -# # Define EXTRA_TESTS to enable building extra tests useful mainly to perf # developers, such as: # x86 instruction decoder - new instructions test @@ -298,10 +295,10 @@ LIBSYMBOL_DIR =3D $(srctree)/tools/lib/symbol/ LIBPERF_DIR =3D $(srctree)/tools/lib/perf/ DOC_DIR =3D $(srctree)/tools/perf/Documentation/ =20 -# Set FEATURE_TESTS to 'all' so all possible feature checkers are executed. -# Without this setting the output feature dump file misses some features, = for -# example, liberty. Select all checkers so we won't get an incomplete feat= ure -# dump file. +# Set FEATURE_TESTS to 'all' so all possible feature checkers are +# executed. Without this setting the output feature dump file misses +# some features. Select all checkers so we won't get an incomplete +# feature dump file. ifeq ($(config),1) ifdef MAKECMDGOALS ifeq ($(filter feature-dump,$(MAKECMDGOALS)),feature-dump) diff --git a/tools/perf/tests/make b/tools/perf/tests/make index 44d76eacce49..4a1ed20bff7e 100644 --- a/tools/perf/tests/make +++ b/tools/perf/tests/make @@ -68,7 +68,6 @@ python_perf_so :=3D $(shell $(MAKE) python_perf_target|gr= ep "Target is:"|awk '{pri make_clean_all :=3D clean all make_python_perf_so :=3D $(python_perf_so) make_debug :=3D DEBUG=3D1 -make_nondistro :=3D BUILD_NONDISTRO=3D1 make_extra_tests :=3D EXTRA_TESTS=3D1 make_jevents_all :=3D JEVENTS_ARCH=3Dall make_no_bpf_skel :=3D BUILD_BPF_SKEL=3D0 @@ -139,7 +138,6 @@ MAKE_F :=3D $(MAKE) -f $(MK) endif run +=3D make_python_perf_so run +=3D make_debug -run +=3D make_nondistro run +=3D make_extra_tests run +=3D make_jevents_all run +=3D make_no_bpf_skel diff --git a/tools/perf/util/demangle-cxx.cpp b/tools/perf/util/demangle-cx= x.cpp index bd657eb37efc..36801ea327a6 100644 --- a/tools/perf/util/demangle-cxx.cpp +++ b/tools/perf/util/demangle-cxx.cpp @@ -8,13 +8,6 @@ #include #endif =20 -#if defined(HAVE_CPLUS_DEMANGLE_SUPPORT) -#ifndef DMGL_PARAMS -#define DMGL_PARAMS (1 << 0) /* Include function args */ -#define DMGL_ANSI (1 << 1) /* Include const, volatile, etc */ -#endif -#endif - /* * Demangle C++ function signature * @@ -24,11 +17,7 @@ extern "C" char *cxx_demangle_sym(const char *str, bool params __maybe_unused, bool modifiers __maybe_unused) { -#if defined(HAVE_CPLUS_DEMANGLE_SUPPORT) - int flags =3D (params ? DMGL_PARAMS : 0) | (modifiers ? DMGL_ANSI = : 0); - - return cplus_demangle(str, flags); -#elif defined(HAVE_CXA_DEMANGLE_SUPPORT) +#if defined(HAVE_CXA_DEMANGLE_SUPPORT) char *output; int status; =20 diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c index 856819bbba59..6e0cf3d2aec5 100644 --- a/tools/perf/util/symbol-elf.c +++ b/tools/perf/util/symbol-elf.c @@ -28,13 +28,6 @@ #include #include =20 -#if defined(HAVE_CPLUS_DEMANGLE_SUPPORT) -#ifndef DMGL_PARAMS -#define DMGL_PARAMS (1 << 0) /* Include function args */ -#define DMGL_ANSI (1 << 1) /* Include const, volatile, etc */ -#endif -#endif - #ifndef EM_AARCH64 #define EM_AARCH64 183 /* ARM 64 bit */ #endif @@ -287,13 +280,7 @@ static bool want_demangle(bool is_kernel_sym) char *cxx_demangle_sym(const char *str __maybe_unused, bool params __maybe= _unused, bool modifiers __maybe_unused) { -#if defined(HAVE_CPLUS_DEMANGLE_SUPPORT) - int flags =3D (params ? DMGL_PARAMS : 0) | (modifiers ? DMGL_ANSI : 0); - - return cplus_demangle(str, flags); -#else return NULL; -#endif } #endif /* !HAVE_CXA_DEMANGLE_SUPPORT */ =20 --=20 2.49.0.805.g082f7c87e0-goog From nobody Fri Dec 19 06:33:53 2025 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.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 2B58122E3E6 for ; Thu, 17 Apr 2025 23:08:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744931307; cv=none; b=sL+bQMC4J/EfcG+K5bQpdHQSUupsgvgm6zzAA7ZSxjmuwtV3mF+8Pb2FBQf56ZBAEhLaGLAJmMnbda6NPvsaz5xa4TDYKRolO2j7da4c0PibGZreWfLolFMBcM7eE96M7rV2bpgwjyxspe/77nXnazGN2Z2U3Ly7wXCits8ONME= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744931307; c=relaxed/simple; bh=foSoET0IF18/f5J0V+ZIUsKXop/FvoFoqTKnlWhCxVc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=XVj7lsq/iQpJvzIBI1tGYAXFdCbCI2QEbnE42jr/t0ckwArmf9hVUxWGEMuv27nfgyQGPdhF+LsSUpYXZ4NURYyvR5eYUjH3FFvBav8lxW5X+StTP3ro+1SlU00zKTNeJiuA4ae0IkHiBlJVdIzXl4Q8IBSpHNuBCs1egSxEJyM= 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=lg337xSK; arc=none smtp.client-ip=209.85.214.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--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="lg337xSK" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2262051205aso10073495ad.3 for ; Thu, 17 Apr 2025 16:08:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1744931306; x=1745536106; 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=Y8ztzt+tF3nTRYCrcDqKNMajzSGAzjCJLiKGSSFxPrQ=; b=lg337xSK5nvTDG2CvM+95trPXVKmEFO8unl/6Yo1b50uYKtDUWywDiQD7MWqYj6+81 r7nDO6RFRB6VOVcL7i41RYYxRudizFuoB7ZtpPA+Mt4hAUzMKDlaVCrMoDlrKqf3Q2xd zPdsNrV4c21g+z00IgB2RypGXwjc07XDyniJgZx18TyN5gU22AKHKXY0qrgbrrBge+Ny 3Mw0zRv/HFid1oulzNk+5jSJZAyF/kGzCdq1mS9h8zmLsdp8pQZFepofHHhjeYswMPLu aHx0RmSt7WdvpFL8Ik+Y6VIQtEXNumKHjcudAuOTtEJkUW/1VvdZEox/AT2mddX8iUzT +3qQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744931306; x=1745536106; 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=Y8ztzt+tF3nTRYCrcDqKNMajzSGAzjCJLiKGSSFxPrQ=; b=pC2cCawK/7jOv//++I+hfZWRIV2ob4p6Ji4Fml7uq7ijJx1y0VOsYLmhSZcATnUcyg d92UjYWnA6smee+ZbCIANUUz3zT3RpPT3lgimv5c+3OPlXkdcXlEtIdztsZNydMHhVmg mrZ3kIX1CclmdpU7wtIDOk8/Ls4ZI+lXshH97IGvzLm52gA3i5Hw4fJJ5WKmF9vEp0Nr Ch6qoQkcNxD+LuuHokC6VWXF7NzTm0y3PpJDiaUUmY8WZKW/1A+KkhFRmjbkr2D8dVFa MmEfLITe3HJO70f1IKQcRue47Ge8pIWx8U6WgyWbpZz5l2w720oCwroD7MlKeTVTHoj1 AjwA== X-Forwarded-Encrypted: i=1; AJvYcCVqgEUEomrJkUS4iUZc7QgipU/HTFKxsG7r6y1IlQSNd2pI0nduxyJKmzb2S+MUJ2jT4fsh7IyAzIrtAu8=@vger.kernel.org X-Gm-Message-State: AOJu0Yzaw3Bdba3qiNKXNPWdNdJluPXnv/4U41bzpWM1ttToE/v6GHHc 3f+e5ZxkFwlKZmbrPevJUVpvozrbZoUvPmk+lTaueXpe4OILB3WrsqiPdE1WXJ5/kqiAhQfK8Hk B+j1M/w== X-Google-Smtp-Source: AGHT+IEWSA4fDbg0ibRFGPSZKCMBCSJeYgY5UAGASEYAWSOWOvfZ2A76/t8FmAXO8wa38rFjn6xU2QPybjdA X-Received: from pfod7.prod.google.com ([2002:aa7:8687:0:b0:736:38af:afeb]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:1744:b0:224:249f:9723 with SMTP id d9443c01a7336-22c536204c8mr9101135ad.51.1744931305689; Thu, 17 Apr 2025 16:08:25 -0700 (PDT) Date: Thu, 17 Apr 2025 16:07:36 -0700 In-Reply-To: <20250417230740.86048-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: <20250417230740.86048-1-irogers@google.com> X-Mailer: git-send-email 2.49.0.805.g082f7c87e0-goog Message-ID: <20250417230740.86048-16-irogers@google.com> Subject: [PATCH v4 15/19] perf build: Remove unused defines 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" DISASM_FOUR_ARGS_SIGNATURE and DISASM_INIT_STYLED were used with libbfd support. Remove now that libbfd support is removed. Signed-off-by: Ian Rogers --- tools/perf/Makefile.config | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/tools/perf/Makefile.config b/tools/perf/Makefile.config index 3ed047ffb4f5..bb4d31b1e1df 100644 --- a/tools/perf/Makefile.config +++ b/tools/perf/Makefile.config @@ -320,9 +320,6 @@ FEATURE_CHECK_LDFLAGS-libpython :=3D $(PYTHON_EMBED_LDO= PTS) =20 FEATURE_CHECK_LDFLAGS-libaio =3D -lrt =20 -FEATURE_CHECK_LDFLAGS-disassembler-four-args =3D -lbfd -lopcodes -ldl -FEATURE_CHECK_LDFLAGS-disassembler-init-styled =3D -lbfd -lopcodes -ldl - CORE_CFLAGS +=3D -fno-omit-frame-pointer CORE_CFLAGS +=3D -Wall CORE_CFLAGS +=3D -Wextra @@ -349,7 +346,7 @@ endif =20 ifeq ($(FEATURES_DUMP),) # We will display at the end of this Makefile.config, using $(call feature= _display_entries) -# As we may retry some feature detection here, see the disassembler-four-a= rgs case, for instance +# As we may retry some feature detection here. FEATURE_DISPLAY_DEFERRED :=3D 1 include $(srctree)/tools/build/Makefile.feature else @@ -1006,14 +1003,6 @@ ifdef HAVE_KVM_STAT_SUPPORT CFLAGS +=3D -DHAVE_KVM_STAT_SUPPORT endif =20 -ifeq ($(feature-disassembler-four-args), 1) - CFLAGS +=3D -DDISASM_FOUR_ARGS_SIGNATURE -endif - -ifeq ($(feature-disassembler-init-styled), 1) - CFLAGS +=3D -DDISASM_INIT_STYLED -endif - ifeq (${IS_64_BIT}, 1) ifndef NO_PERF_READ_VDSO32 $(call feature_check,compile-32) @@ -1288,6 +1277,6 @@ endif =20 # re-generate FEATURE-DUMP as we may have called feature_check, found out # extra libraries to add to LDFLAGS of some other test and then redo those -# tests, see the block about disassembler-four-args, for instance. +# tests. $(shell rm -f $(FEATURE_DUMP_FILENAME)) $(foreach feat,$(FEATURE_TESTS),$(shell echo "$(call feature_assign,$(feat= ))" >> $(FEATURE_DUMP_FILENAME))) --=20 2.49.0.805.g082f7c87e0-goog From nobody Fri Dec 19 06:33:53 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 6FA632512C9 for ; Thu, 17 Apr 2025 23:08:28 +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=1744931310; cv=none; b=aGejZ2KAqspWZXz8VW55OXyjUTyjgbRoHiWUR6PlqfxFnWSfxs6V8gU1hznVhvXhXeApyyNZz8kWCMOQ426oWSO1wOchRDaZDBOS9atrwClIy2fFomHpFlCd//DtIyK6rN6j4suWWp+1N4aD6zZgaJ9KptMmoHMDN+t8EFN/Jik= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744931310; c=relaxed/simple; bh=dVqTwm6Z5kJPYlgxaTtj66E44vbniBTbV8kGzDOLkSk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=kvBftINaZXBUK412YitsaZ5ujmugsRbjzEf5hqDUhZbtl2sW34ezIP19OFVJ5LqbWF7RLeNYnPAUkZjLMExW2wtvgLouQq9N5cI/v+KUD0sKSTGNBpi8/Z0iI9vhHGxNwsFKoifm+zGj4pYt5tJV8cFZ3tW5o54MiSo9Gp8hmQs= 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=DsvI37f8; 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="DsvI37f8" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2255ae39f8fso22268035ad.0 for ; Thu, 17 Apr 2025 16:08:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1744931308; x=1745536108; 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=RY1r15pwpCxEu/4KBHv5AIK/eUkuBGzABOfCshTThNM=; b=DsvI37f8sUTX7c704DqOwYYW4c6e8G5CTi3vUnaYskkunDwfiOLfLURtIc8WQepQgV DYqQ4ZiNOWUlhZ8H8uShfPBT6DWH4F3Wm1lNeQmZwteh0cSC9nbxdRjo1R7nfoq1XBGP uv5irtYlKc15In1F7V9gDkWADI7bwT+u4E1HbOZr23epyxdyoM6Bi0qDHV+8XU/XH/ZN 4u6laLAg/yWvJq1JnDLieuGEEP1JQQNnUAMS22ss5WJyo2AakYP7TfZG5j2CQFJZC8QA MpI3qraKYr9fKqStkRf8ig9y7gynZM/n+vWX4sa2m33flgi5wByJkqvXmkZsfu8K8W5c pbXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744931308; x=1745536108; 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=RY1r15pwpCxEu/4KBHv5AIK/eUkuBGzABOfCshTThNM=; b=wCYJswNTIotKKUjzGYK3idapyBUJeeY4k/4HqpmFWt8jHSEPWIPzdVGOvk5qXN+Goq mYzr/TicIDXaIUPmsKW11H1JCGjfpxl1q25T6KWhKpyCBF3KlrkN+eokZmt/KtXZUQA/ iUUenWF69uAgPi7AEveukJy2JHV1NEo4NlgCfb9sZm9EGlascJm+biNHu0apRc90twMY aBHluLLtsKkIM5tjsugTWZluAuQ0j8woq9sU+rhwhFdAe9RWab/joY5Mar4vYAYHBCXp ybeIdo2A0SPdXFTindkE6DltHrbOMwFKGJ0Bsuo3XAJrMcOJeNuRLa9YEs5K3/pcBviz PIBA== X-Forwarded-Encrypted: i=1; AJvYcCVi2DUgBfyszmWWWsnWslw93bqnPDJl0dtVlCfLMwaQJqPLnizrRhAcDx1KHKjROPLX19mEV+LZTvVy4HI=@vger.kernel.org X-Gm-Message-State: AOJu0YxSB0PY/cQfdNWqRMwv3mH9V8DxcTaevZg9OGawJrc/xqj6mH2h cZNnAPl9i7Tx393dJGxVSnIbNWYKejhLzB6aqdPD+bsWtXwNGNoaHhqLBnCT9dNkSHnIKTHcBLu 9YXmpag== X-Google-Smtp-Source: AGHT+IF1KynYQjk+qiDtYBRZqtrA+sXYk/Qlh4FVBuUH3NSYZdwIzrbF7KXRZDSOIoNHB1eQbEczvfeazT6E X-Received: from plsa23.prod.google.com ([2002:a17:902:b597:b0:227:e456:d436]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:f78a:b0:220:cfb7:56eb with SMTP id d9443c01a7336-22c53f36a0fmr7932675ad.26.1744931307861; Thu, 17 Apr 2025 16:08:27 -0700 (PDT) Date: Thu, 17 Apr 2025 16:07:37 -0700 In-Reply-To: <20250417230740.86048-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: <20250417230740.86048-1-irogers@google.com> X-Mailer: git-send-email 2.49.0.805.g082f7c87e0-goog Message-ID: <20250417230740.86048-17-irogers@google.com> Subject: [PATCH v4 16/19] perf disasm: Remove disasm_bpf 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" BPF disassembly was handled in here by libbfd. The LLVM and capstone disassemblers now support BPF JIT disassembly. As libbfd support was removed the functions here no longer did anything remove them and associated error values. Signed-off-by: Ian Rogers --- tools/perf/util/Build | 1 - tools/perf/util/annotate.h | 1 - tools/perf/util/disasm.c | 12 +++--------- tools/perf/util/disasm_bpf.c | 29 ----------------------------- tools/perf/util/disasm_bpf.h | 12 ------------ 5 files changed, 3 insertions(+), 52 deletions(-) delete mode 100644 tools/perf/util/disasm_bpf.c delete mode 100644 tools/perf/util/disasm_bpf.h diff --git a/tools/perf/util/Build b/tools/perf/util/Build index 861e8ed4ac49..cc22cc8a5aac 100644 --- a/tools/perf/util/Build +++ b/tools/perf/util/Build @@ -14,7 +14,6 @@ perf-util-y +=3D copyfile.o perf-util-y +=3D ctype.o perf-util-y +=3D db-export.o perf-util-y +=3D disasm.o -perf-util-y +=3D disasm_bpf.o perf-util-y +=3D env.o perf-util-y +=3D event.o perf-util-y +=3D evlist.o diff --git a/tools/perf/util/annotate.h b/tools/perf/util/annotate.h index bbb89b32f398..8b14a90a3a0c 100644 --- a/tools/perf/util/annotate.h +++ b/tools/perf/util/annotate.h @@ -446,7 +446,6 @@ enum symbol_disassemble_errno { __SYMBOL_ANNOTATE_ERRNO__START =3D -10000, =20 SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX =3D __SYMBOL_ANNOTATE_ERRNO__START, - SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF, SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_CPUID_PARSING, SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_REGEXP, SYMBOL_ANNOTATE_ERRNO__BPF_INVALID_FILE, diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c index 175d9d3b5c97..683eb6aa7b53 100644 --- a/tools/perf/util/disasm.c +++ b/tools/perf/util/disasm.c @@ -17,7 +17,6 @@ #include "capstone.h" #include "debug.h" #include "disasm.h" -#include "disasm_bpf.h" #include "dso.h" #include "dwarf-regs.h" #include "env.h" @@ -1231,9 +1230,6 @@ int symbol__strerror_disassemble(struct map_symbol *m= s, int errnum, char *buf, s " --vmlinux vmlinux\n", build_id_msg ?: ""); } break; - case SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF: - scnprintf(buf, buflen, "Please link with binutils's libopcode to enable = BPF annotation"); - break; case SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_REGEXP: scnprintf(buf, buflen, "Problems with arch specific instruction name reg= ular expressions."); break; @@ -1501,11 +1497,9 @@ static int symbol__disassemble_objdump(const char *f= ilename, struct symbol *sym, struct child_process objdump_process; int err; =20 - if (dso__binary_type(dso) =3D=3D DSO_BINARY_TYPE__BPF_PROG_INFO) - return symbol__disassemble_bpf(sym, args); - - if (dso__binary_type(dso) =3D=3D DSO_BINARY_TYPE__BPF_IMAGE) - return symbol__disassemble_bpf_image(sym, args); + if (dso__binary_type(dso) =3D=3D DSO_BINARY_TYPE__BPF_PROG_INFO || + dso__binary_type(dso) =3D=3D DSO_BINARY_TYPE__BPF_IMAGE) + return -1; =20 err =3D asprintf(&command, "%s %s%s --start-address=3D0x%016" PRIx64 diff --git a/tools/perf/util/disasm_bpf.c b/tools/perf/util/disasm_bpf.c deleted file mode 100644 index a891a0b909a7..000000000000 --- a/tools/perf/util/disasm_bpf.c +++ /dev/null @@ -1,29 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0-only - -#include "util/annotate.h" -#include "util/disasm_bpf.h" -#include "util/symbol.h" -#include -#include - -int symbol__disassemble_bpf(struct symbol *sym __maybe_unused, struct anno= tate_args *args __maybe_unused) -{ - return SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF; -} - -int symbol__disassemble_bpf_image(struct symbol *sym, struct annotate_args= *args) -{ - struct annotation *notes =3D symbol__annotation(sym); - struct disasm_line *dl; - - args->offset =3D -1; - args->line =3D strdup("to be implemented"); - args->line_nr =3D 0; - args->fileloc =3D NULL; - dl =3D disasm_line__new(args); - if (dl) - annotation_line__add(&dl->al, ¬es->src->source); - - zfree(&args->line); - return 0; -} diff --git a/tools/perf/util/disasm_bpf.h b/tools/perf/util/disasm_bpf.h deleted file mode 100644 index 2ecb19545388..000000000000 --- a/tools/perf/util/disasm_bpf.h +++ /dev/null @@ -1,12 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0-only - -#ifndef __PERF_DISASM_BPF_H -#define __PERF_DISASM_BPF_H - -struct symbol; -struct annotate_args; - -int symbol__disassemble_bpf(struct symbol *sym, struct annotate_args *args= ); -int symbol__disassemble_bpf_image(struct symbol *sym, struct annotate_args= *args); - -#endif /* __PERF_DISASM_BPF_H */ --=20 2.49.0.805.g082f7c87e0-goog From nobody Fri Dec 19 06:33:53 2025 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.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 76740253B7B for ; Thu, 17 Apr 2025 23:08:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744931312; cv=none; b=Pi9u3HUtdK4TTG/QhislP0h/elx/2Wxi01p3JzBLUodY///hsUiJwbmHcExltO/eCk+A2lmgQ7Y83CurhAz1pdXhy51+/F/0x7jG3VVyi+7s5rhRbkF5BJNrVoB7EUK21csFO5Dmu6KiTVEnrkj4VaO9um0NsxoQ9PzPoxuy4hs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744931312; c=relaxed/simple; bh=4z4gW1zRYCCwysIUyAe3e8VCBdOYyrgGoIXqH8yEfYU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=Oa8p0zmdlThb5uPuE6mKL6hhQuyd2raPh9Bc7Itm40f/NXCETdHzjh0Fs0F1qFCOxT0uOlMleFuRqZc2ku+jQC92sGJ8pE9lfTC9wLSALWrRb8xaowLf1hyU9VUAHKZy/bxx5HMhv0kLnh4uuY6xqmrbkhVcqNWwpdRQpa1LpZI= 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=1oazZ2/a; arc=none smtp.client-ip=209.85.214.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--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="1oazZ2/a" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-225107fbdc7so12734645ad.0 for ; Thu, 17 Apr 2025 16:08:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1744931310; x=1745536110; 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=DtBD4ZMv9kOd8HctqbtqQiXkO3Y0PBpeScsbHocvr8Q=; b=1oazZ2/a1fAxXCagIPUMxygIfb7jHq4TRGeOmDDqTyYfF+CZC0QgJd6AE4OsvwBHoG 7QSuitWUfWm3erM/Z0RTCCTAw+XvqTXLfl8miQaB2LmIjIxPaLjYZaoblf1niASED8+J T+A5u711s4uPTLnCi/AU3NSL2pZGuDY/yxUn1mvXvUI5M7/BHRxdYNo0HLJUXBsUT1J7 bnG1/41whRkpWUvD6bvynjjL6KVGNKe+cwwl5dEr+gfnqcJetovfL6PWWbCvPiguJkbY 8jg2NtC4jxvOmgnMKJQtWflgS7kOIof8LjiPzVP3KbR0zh0wt8oXlJYaTKm1hHJd9mSV ZKVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744931310; x=1745536110; 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=DtBD4ZMv9kOd8HctqbtqQiXkO3Y0PBpeScsbHocvr8Q=; b=xNa1Xyjfle1SUjvnDSTsQ5G0qyqOKSPMIKPKuVkTrjgUcmk/+7rEWqP08BKItQMW6w Dx6oqS6pR7iV5XbKVYVLtaWqFRZ9/ETE4rMZbi2zhbjlBHYoQWQX6b5nRhwnFFjYFu2W AdxufZIy/tqlCkrnv+5MA1G7pS0hGO6nuU3HQ3Zo2mma/Hgr0jdxOX3Crl5w6FgUTuD0 kZAyAFjQuNuQmAnxRtHzqhWsehFHHZLhIRFg6bR8tdYm/+b6yFlJVnI4fqYMePTvEkYI Dm55T97bNj6vtAPzZSg6NyyFcwAdHQe4T37zopovYNqRm9FtCfd9lo1mvL7Nn7Xxx13g TO8Q== X-Forwarded-Encrypted: i=1; AJvYcCWrkKM/HBKCWXL/9TCl+l5OHs0SZmG7Jrnx0jZeGmXRDbNAUbkrglTTQY88Jk4H4BsR1VEwuKsW5dv+2fU=@vger.kernel.org X-Gm-Message-State: AOJu0Yz6i1dNmeQSolv7NcM+3RzThi6Utt9V5+ilhEctkhDJsyL7b8uO PdPt+jpEcQhWfdfvTABAYpZ+nTKx8PFQ5ecva9c1aa3bdYuSnrw4yjSa6Zmd9EFObiI8G3iDGfQ tU8i25Q== X-Google-Smtp-Source: AGHT+IHMtsZutPRyIyx14Pp7e7dCE8WR3NROHmlpPvZEjnfl9ohpt846ZUWL5TFfQkuwyyo4WIfiJ8YKJMLR X-Received: from plrf20.prod.google.com ([2002:a17:902:ab94:b0:223:6930:304e]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:1b66:b0:220:ec62:7dc8 with SMTP id d9443c01a7336-22c535738f3mr11016455ad.2.1744931310034; Thu, 17 Apr 2025 16:08:30 -0700 (PDT) Date: Thu, 17 Apr 2025 16:07:38 -0700 In-Reply-To: <20250417230740.86048-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: <20250417230740.86048-1-irogers@google.com> X-Mailer: git-send-email 2.49.0.805.g082f7c87e0-goog Message-ID: <20250417230740.86048-18-irogers@google.com> Subject: [PATCH v4 17/19] perf disasm: Make ins__scnprintf and ins__is_nop static 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Reduce the scope of ins__scnprintf and ins__is_nop that aren't used outside of disasm.c. Signed-off-by: Ian Rogers --- tools/perf/util/disasm.c | 6 +++--- tools/perf/util/disasm.h | 3 --- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c index 683eb6aa7b53..72e7f1312fb4 100644 --- a/tools/perf/util/disasm.c +++ b/tools/perf/util/disasm.c @@ -246,8 +246,8 @@ static int ins__raw_scnprintf(struct ins *ins, char *bf= , size_t size, return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->raw); } =20 -int ins__scnprintf(struct ins *ins, char *bf, size_t size, - struct ins_operands *ops, int max_ins_name) +static int ins__scnprintf(struct ins *ins, char *bf, size_t size, + struct ins_operands *ops, int max_ins_name) { if (ins->ops->scnprintf) return ins->ops->scnprintf(ins, bf, size, ops, max_ins_name); @@ -824,7 +824,7 @@ static struct ins_ops ret_ops =3D { .scnprintf =3D ins__raw_scnprintf, }; =20 -bool ins__is_nop(const struct ins *ins) +static bool ins__is_nop(const struct ins *ins) { return ins->ops =3D=3D &nop_ops; } diff --git a/tools/perf/util/disasm.h b/tools/perf/util/disasm.h index 2cb4e1a6bd30..09c86f540f7f 100644 --- a/tools/perf/util/disasm.h +++ b/tools/perf/util/disasm.h @@ -110,13 +110,10 @@ struct arch *arch__find(const char *name); bool arch__is(struct arch *arch, const char *name); =20 struct ins_ops *ins__find(struct arch *arch, const char *name, struct disa= sm_line *dl); -int ins__scnprintf(struct ins *ins, char *bf, size_t size, - struct ins_operands *ops, int max_ins_name); =20 bool ins__is_call(const struct ins *ins); bool ins__is_jump(const struct ins *ins); bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2); -bool ins__is_nop(const struct ins *ins); bool ins__is_ret(const struct ins *ins); bool ins__is_lock(const struct ins *ins); =20 --=20 2.49.0.805.g082f7c87e0-goog From nobody Fri Dec 19 06:33:53 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 C9A2A255E22 for ; Thu, 17 Apr 2025 23:08:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744931315; cv=none; b=SnLOuOV8JbLZYdWS5d1tzxEQfYYGXjuHIn7iu6O2otV0L6FI40x0hn9bh428NQ0kqYgB9OMaJtaVlGarnu9gPrWay4vY9yirxTvgydLAob8b1+HUb8YE+tYyH6X1RRsfwW2KJWZYFjRBeSzPSAYUvcJLQgzBCS0dBB9VOKfoXcU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744931315; c=relaxed/simple; bh=8k3FlCgHrdPmlJ/YavxA29IKK19rcLviTv4/LJaSO4M=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=eF/VCGO+fKvkxJunxAfTiwbmMM28e7dtvgERyeKxrHbb59AJMCVSHt8Jr/KLdr3d/P8vj6PLw7uE27/MfHs/vAQKQnG+HchKQYPed0fcTMhLBW4rQCxpgW6+1ON46bNdub7+LjMiRVHQzOUHo6e+eqvryQQAeDu76Bya+I1a+JY= 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=LEI3eN+X; arc=none smtp.client-ip=209.85.216.74 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="LEI3eN+X" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ff6aaa18e8so1133543a91.1 for ; Thu, 17 Apr 2025 16:08:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1744931312; x=1745536112; 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=4pxBzMoilXQ6no+1qGD8wYfrT1xzrnLTyS3x6ZunoUk=; b=LEI3eN+XfzFqm+n8xib7LcTB6PG0dElGGDr6bNtV+VCugOmMKL0x2Ui//RU8l7YaTL iJTOQpddRBM69jbYoJ2cJ9UsH6bdHR9VyNm/cntUtmn/iS1Ymz2nlax/VSvLDUorjP1j 1Dn9eZbI+O/YQoIdtU8hf3cjeKR7wZ/bdNOw6IFa0MbD6U3shRBOHbkN764SUFoA9Oeg tSpZlQpIP3eGmGn7XfxS2fybok9pr5odQNiG1mhzcROvgcrlTD6i7Sp6Tzl0Jy3Hz7Vh 77/rOCJVsl0aD8r7xZsZq35IUA+zEHMX6N+vYWWjpe9LnaM/7GJdt6ksQ14O1dSyd5JM aQ/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744931312; x=1745536112; 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=4pxBzMoilXQ6no+1qGD8wYfrT1xzrnLTyS3x6ZunoUk=; b=O0gS17i2z5U8AsluAQTWU3mJpBYvxX/Zf/SeuHgRUEvfty4ycytbr4SeFT2XODRpU1 /hL4K6BrIpYRPwkVu6511cS58pOse0ZP7J/+mYaFX8nb2BheW/rpqkNTBCJIasOX97/Y 8qA7UJEjrOgJOQTC6ZKFH6WJ4UFcJhc+tx+lLsR9I3YFihDwQBIoLHMRq8Fi0F6jgHTR Q6WPkp91S0+Fq34218TOj/wSqiOxd1rWuNe2gcncbMbI+tooxH7AsGFNxgbNmtaDvoov vrQdKYPbXZnflSHEVTadeMJiUPRiSjNQbja+SBpysGRfZsnIkirWAvhw1GrOD+u9mMAS 7d/Q== X-Forwarded-Encrypted: i=1; AJvYcCUufpBpU4pTfLA049E5LJnt9z4SZiTtqAFHAZwC1tpva0jG5b8FrWHSGzWsSMa1Z1Us8pvaTbf8CzPR6no=@vger.kernel.org X-Gm-Message-State: AOJu0Yyp3Aw5hExGbzvM7FTghLnhbxv/Nn7NsrRQcA251SAXrQb++SL3 QfD+3yBXt8rq2ERl5bs2oqFsdWe8uKVveTyJKSnz5WstAfUwnKBu2wqMGzgGAgftT4/+X1vbIw2 hwOwiUg== X-Google-Smtp-Source: AGHT+IEv3LCsICt0MA6uxA+Z6AcZZVxzRG8YMdz6HJQgt73yF2wk3abRzWSedqZd7n1OSBcvN2na0r6juK8x X-Received: from pjbsz11.prod.google.com ([2002:a17:90b:2d4b:b0:2f9:c349:2f84]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:fc46:b0:2fe:b907:3b05 with SMTP id 98e67ed59e1d1-3087bbb6abcmr1002744a91.29.1744931312357; Thu, 17 Apr 2025 16:08:32 -0700 (PDT) Date: Thu, 17 Apr 2025 16:07:39 -0700 In-Reply-To: <20250417230740.86048-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: <20250417230740.86048-1-irogers@google.com> X-Mailer: git-send-email 2.49.0.805.g082f7c87e0-goog Message-ID: <20250417230740.86048-19-irogers@google.com> Subject: [PATCH v4 18/19] perf srcline: Fallback between addr2line implementations 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Factor the addr2line function implementation into separate source files (addr2line.[ch]) and rename the addr2line function cmd__addr2line. In srcline replace the ifdef-ed addr2line implementations with one that first tries the llvm__addr2line implementation and on failure uses cmd__addr2line. If HAVE_LIBLLVM_SUPPORT is enabled the llvm__addr2line will execute against the libLLVM.so it is linked against. If HAVE_LIBLLVM_DYNAMIC is enabled then libperf-llvm.so (that links against libLLVM.so) will be dlopened. If the dlopen succeeds then the behavior should match HAVE_LIBLLVM_SUPPORT. On failure cmd__addr2line is used. The dlopen is only tried once. If HAVE_LIBLLVM_DYNAMIC isn't enabled then llvm__addr2line immediately fails and cmd__addr2line is used. Clean up the dso__free_a2l logic, which is only needed in the non-LLVM version and moved to addr2line.c. Signed-off-by: Ian Rogers --- tools/perf/util/Build | 1 + tools/perf/util/addr2line.c | 439 ++++++++++++++++++++++++++++++++ tools/perf/util/addr2line.h | 20 ++ tools/perf/util/config.c | 2 +- tools/perf/util/llvm.c | 5 - tools/perf/util/llvm.h | 3 - tools/perf/util/srcline.c | 482 ++---------------------------------- tools/perf/util/srcline.h | 1 - 8 files changed, 484 insertions(+), 469 deletions(-) create mode 100644 tools/perf/util/addr2line.c create mode 100644 tools/perf/util/addr2line.h diff --git a/tools/perf/util/Build b/tools/perf/util/Build index cc22cc8a5aac..197c40543c6d 100644 --- a/tools/perf/util/Build +++ b/tools/perf/util/Build @@ -2,6 +2,7 @@ include $(srctree)/tools/scripts/Makefile.include include $(srctree)/tools/scripts/utilities.mak =20 perf-util-y +=3D arm64-frame-pointer-unwind-support.o +perf-util-y +=3D addr2line.o perf-util-y +=3D addr_location.o perf-util-y +=3D annotate.o perf-util-y +=3D block-info.o diff --git a/tools/perf/util/addr2line.c b/tools/perf/util/addr2line.c new file mode 100644 index 000000000000..2f6445c183aa --- /dev/null +++ b/tools/perf/util/addr2line.c @@ -0,0 +1,439 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "addr2line.h" +#include "debug.h" +#include "dso.h" +#include "string2.h" +#include "srcline.h" +#include "symbol.h" +#include "symbol_conf.h" + +#include +#include +#include + +#include +#include +#include +#include + +#define MAX_INLINE_NEST 1024 + +/* If addr2line doesn't return data for 1 second then timeout. */ +int addr2line_timeout_ms =3D 1 * 1000; + +static int filename_split(char *filename, unsigned int *line_nr) +{ + char *sep; + + sep =3D strchr(filename, '\n'); + if (sep) + *sep =3D '\0'; + + if (!strcmp(filename, "??:0")) + return 0; + + sep =3D strchr(filename, ':'); + if (sep) { + *sep++ =3D '\0'; + *line_nr =3D strtoul(sep, NULL, 0); + return 1; + } + pr_debug("addr2line missing ':' in filename split\n"); + return 0; +} + +static void addr2line_subprocess_cleanup(struct child_process *a2l) +{ + if (a2l->pid !=3D -1) { + kill(a2l->pid, SIGKILL); + finish_command(a2l); /* ignore result, we don't care */ + a2l->pid =3D -1; + close(a2l->in); + close(a2l->out); + } + + free(a2l); +} + +static struct child_process *addr2line_subprocess_init(const char *addr2li= ne_path, + const char *binary_path) +{ + const char *argv[] =3D { + addr2line_path ?: "addr2line", + "-e", binary_path, + "-a", "-i", "-f", NULL + }; + struct child_process *a2l =3D zalloc(sizeof(*a2l)); + int start_command_status =3D 0; + + if (a2l =3D=3D NULL) { + pr_err("Failed to allocate memory for addr2line"); + return NULL; + } + + a2l->pid =3D -1; + a2l->in =3D -1; + a2l->out =3D -1; + a2l->no_stderr =3D 1; + + a2l->argv =3D argv; + start_command_status =3D start_command(a2l); + a2l->argv =3D NULL; /* it's not used after start_command; avoid dangling = pointers */ + + if (start_command_status !=3D 0) { + pr_warning("could not start addr2line (%s) for %s: start_command return = code %d\n", + addr2line_path, binary_path, start_command_status); + addr2line_subprocess_cleanup(a2l); + return NULL; + } + + return a2l; +} + +enum a2l_style { + BROKEN, + GNU_BINUTILS, + LLVM, +}; + +static enum a2l_style addr2line_configure(struct child_process *a2l, const= char *dso_name) +{ + static bool cached; + static enum a2l_style style; + + if (!cached) { + char buf[128]; + struct io io; + int ch; + int lines; + + if (write(a2l->in, ",\n", 2) !=3D 2) + return BROKEN; + + io__init(&io, a2l->out, buf, sizeof(buf)); + ch =3D io__get_char(&io); + if (ch =3D=3D ',') { + style =3D LLVM; + cached =3D true; + lines =3D 1; + pr_debug("Detected LLVM addr2line style\n"); + } else if (ch =3D=3D '0') { + style =3D GNU_BINUTILS; + cached =3D true; + lines =3D 3; + pr_debug("Detected binutils addr2line style\n"); + } else { + if (!symbol_conf.disable_add2line_warn) { + char *output =3D NULL; + size_t output_len; + + io__getline(&io, &output, &output_len); + pr_warning("%s %s: addr2line configuration failed\n", + __func__, dso_name); + pr_warning("\t%c%s", ch, output); + } + pr_debug("Unknown/broken addr2line style\n"); + return BROKEN; + } + while (lines) { + ch =3D io__get_char(&io); + if (ch <=3D 0) + break; + if (ch =3D=3D '\n') + lines--; + } + /* Ignore SIGPIPE in the event addr2line exits. */ + signal(SIGPIPE, SIG_IGN); + } + return style; +} + +static int read_addr2line_record(struct io *io, + enum a2l_style style, + const char *dso_name, + u64 addr, + bool first, + char **function, + char **filename, + unsigned int *line_nr) +{ + /* + * Returns: + * -1 =3D=3D> error + * 0 =3D=3D> sentinel (or other ill-formed) record read + * 1 =3D=3D> a genuine record read + */ + char *line =3D NULL; + size_t line_len =3D 0; + unsigned int dummy_line_nr =3D 0; + int ret =3D -1; + + if (function !=3D NULL) + zfree(function); + + if (filename !=3D NULL) + zfree(filename); + + if (line_nr !=3D NULL) + *line_nr =3D 0; + + /* + * Read the first line. Without an error this will be: + * - for the first line an address like 0x1234, + * - the binutils sentinel 0x0000000000000000, + * - the llvm-addr2line the sentinel ',' character, + * - the function name line for an inlined function. + */ + if (io__getline(io, &line, &line_len) < 0 || !line_len) + goto error; + + pr_debug("%s %s: addr2line read address for sentinel: %s", __func__, dso_= name, line); + if (style =3D=3D LLVM && line_len =3D=3D 2 && line[0] =3D=3D ',') { + /* Found the llvm-addr2line sentinel character. */ + zfree(&line); + return 0; + } else if (style =3D=3D GNU_BINUTILS && (!first || addr !=3D 0)) { + int zero_count =3D 0, non_zero_count =3D 0; + /* + * Check for binutils sentinel ignoring it for the case the + * requested address is 0. + */ + + /* A given address should always start 0x. */ + if (line_len >=3D 2 || line[0] !=3D '0' || line[1] !=3D 'x') { + for (size_t i =3D 2; i < line_len; i++) { + if (line[i] =3D=3D '0') + zero_count++; + else if (line[i] !=3D '\n') + non_zero_count++; + } + if (!non_zero_count) { + int ch; + + if (first && !zero_count) { + /* Line was erroneous just '0x'. */ + goto error; + } + /* + * Line was 0x0..0, the sentinel for binutils. Remove + * the function and filename lines. + */ + zfree(&line); + do { + ch =3D io__get_char(io); + } while (ch > 0 && ch !=3D '\n'); + do { + ch =3D io__get_char(io); + } while (ch > 0 && ch !=3D '\n'); + return 0; + } + } + } + /* Read the second function name line (if inline data then this is the fi= rst line). */ + if (first && (io__getline(io, &line, &line_len) < 0 || !line_len)) + goto error; + + pr_debug("%s %s: addr2line read line: %s", __func__, dso_name, line); + if (function !=3D NULL) + *function =3D strdup(strim(line)); + + zfree(&line); + line_len =3D 0; + + /* Read the third filename and line number line. */ + if (io__getline(io, &line, &line_len) < 0 || !line_len) + goto error; + + pr_debug("%s %s: addr2line filename:number : %s", __func__, dso_name, lin= e); + if (filename_split(line, line_nr =3D=3D NULL ? &dummy_line_nr : line_nr) = =3D=3D 0 && + style =3D=3D GNU_BINUTILS) { + ret =3D 0; + goto error; + } + + if (filename !=3D NULL) + *filename =3D strdup(line); + + zfree(&line); + line_len =3D 0; + + return 1; + +error: + free(line); + if (function !=3D NULL) + zfree(function); + if (filename !=3D NULL) + zfree(filename); + return ret; +} + +static int inline_list__append_record(struct dso *dso, + struct inline_node *node, + struct symbol *sym, + const char *function, + const char *filename, + unsigned int line_nr) +{ + struct symbol *inline_sym =3D new_inline_sym(dso, sym, function); + + return inline_list__append(inline_sym, srcline_from_fileline(filename, li= ne_nr), node); +} + +int cmd__addr2line(const char *dso_name, u64 addr, + char **file, unsigned int *line_nr, + struct dso *dso, + bool unwind_inlines, + struct inline_node *node, + struct symbol *sym __maybe_unused) +{ + struct child_process *a2l =3D dso__a2l(dso); + char *record_function =3D NULL; + char *record_filename =3D NULL; + unsigned int record_line_nr =3D 0; + int record_status =3D -1; + int ret =3D 0; + size_t inline_count =3D 0; + int len; + char buf[128]; + ssize_t written; + struct io io =3D { .eof =3D false }; + enum a2l_style a2l_style; + + if (!a2l) { + if (!filename__has_section(dso_name, ".debug_line")) + goto out; + + dso__set_a2l(dso, + addr2line_subprocess_init(symbol_conf.addr2line_path, dso_name)); + a2l =3D dso__a2l(dso); + } + + if (a2l =3D=3D NULL) { + if (!symbol_conf.disable_add2line_warn) + pr_warning("%s %s: addr2line_subprocess_init failed\n", __func__, dso_n= ame); + goto out; + } + a2l_style =3D addr2line_configure(a2l, dso_name); + if (a2l_style =3D=3D BROKEN) + goto out; + + /* + * Send our request and then *deliberately* send something that can't be + * interpreted as a valid address to ask addr2line about (namely, + * ","). This causes addr2line to first write out the answer to our + * request, in an unbounded/unknown number of records, and then to write + * out the lines "0x0...0", "??" and "??:0", for GNU binutils, or "," + * for llvm-addr2line, so that we can detect when it has finished giving + * us anything useful. + */ + len =3D snprintf(buf, sizeof(buf), "%016"PRIx64"\n,\n", addr); + written =3D len > 0 ? write(a2l->in, buf, len) : -1; + if (written !=3D len) { + if (!symbol_conf.disable_add2line_warn) + pr_warning("%s %s: could not send request\n", __func__, dso_name); + goto out; + } + io__init(&io, a2l->out, buf, sizeof(buf)); + io.timeout_ms =3D addr2line_timeout_ms; + switch (read_addr2line_record(&io, a2l_style, dso_name, addr, /*first=3D*= /true, + &record_function, &record_filename, &record_line_nr)) { + case -1: + if (!symbol_conf.disable_add2line_warn) + pr_warning("%s %s: could not read first record\n", __func__, dso_name); + goto out; + case 0: + /* + * The first record was invalid, so return failure, but first + * read another record, since we sent a sentinel ',' for the + * sake of detected the last inlined function. Treat this as the + * first of a record as the ',' generates a new start with GNU + * binutils, also force a non-zero address as we're no longer + * reading that record. + */ + switch (read_addr2line_record(&io, a2l_style, dso_name, + /*addr=3D*/1, /*first=3D*/true, + NULL, NULL, NULL)) { + case -1: + if (!symbol_conf.disable_add2line_warn) + pr_warning("%s %s: could not read sentinel record\n", + __func__, dso_name); + break; + case 0: + /* The sentinel as expected. */ + break; + default: + if (!symbol_conf.disable_add2line_warn) + pr_warning("%s %s: unexpected record instead of sentinel", + __func__, dso_name); + break; + } + goto out; + default: + /* First record as expected. */ + break; + } + + if (file) { + *file =3D strdup(record_filename); + ret =3D 1; + } + if (line_nr) + *line_nr =3D record_line_nr; + + if (unwind_inlines) { + if (node && inline_list__append_record(dso, node, sym, + record_function, + record_filename, + record_line_nr)) { + ret =3D 0; + goto out; + } + } + + /* + * We have to read the records even if we don't care about the inline + * info. This isn't the first record and force the address to non-zero + * as we're reading records beyond the first. + */ + while ((record_status =3D read_addr2line_record(&io, + a2l_style, + dso_name, + /*addr=3D*/1, + /*first=3D*/false, + &record_function, + &record_filename, + &record_line_nr)) =3D=3D 1) { + if (unwind_inlines && node && inline_count++ < MAX_INLINE_NEST) { + if (inline_list__append_record(dso, node, sym, + record_function, + record_filename, + record_line_nr)) { + ret =3D 0; + goto out; + } + ret =3D 1; /* found at least one inline frame */ + } + } + +out: + free(record_function); + free(record_filename); + if (io.eof) { + dso__set_a2l(dso, NULL); + addr2line_subprocess_cleanup(a2l); + } + return ret; +} + +void dso__free_a2l(struct dso *dso) +{ + struct child_process *a2l =3D dso__a2l(dso); + + if (!a2l) + return; + + addr2line_subprocess_cleanup(a2l); + + dso__set_a2l(dso, NULL); +} diff --git a/tools/perf/util/addr2line.h b/tools/perf/util/addr2line.h new file mode 100644 index 000000000000..d35a47ba8dab --- /dev/null +++ b/tools/perf/util/addr2line.h @@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __PERF_ADDR2LINE_H +#define __PERF_ADDR2LINE_H + +#include + +struct dso; +struct inline_node; +struct symbol; + +extern int addr2line_timeout_ms; + +int cmd__addr2line(const char *dso_name, u64 addr, + char **file, unsigned int *line_nr, + struct dso *dso, + bool unwind_inlines, + struct inline_node *node, + struct symbol *sym); + +#endif /* __PERF_ADDR2LINE_H */ diff --git a/tools/perf/util/config.c b/tools/perf/util/config.c index ae72b66b6ded..6f914620c6ff 100644 --- a/tools/perf/util/config.c +++ b/tools/perf/util/config.c @@ -19,7 +19,7 @@ #include "util/hist.h" /* perf_hist_config */ #include "util/stat.h" /* perf_stat__set_big_num */ #include "util/evsel.h" /* evsel__hw_names, evsel__use_bpf_counters */ -#include "util/srcline.h" /* addr2line_timeout_ms */ +#include "util/addr2line.h" /* addr2line_timeout_ms */ #include "build-id.h" #include "debug.h" #include "config.h" diff --git a/tools/perf/util/llvm.c b/tools/perf/util/llvm.c index 1607364ee736..cacb510c6814 100644 --- a/tools/perf/util/llvm.c +++ b/tools/perf/util/llvm.c @@ -303,11 +303,6 @@ int llvm__addr2line(const char *dso_name __maybe_unuse= d, u64 addr __maybe_unused return num_frames; } =20 -void dso__free_a2l_llvm(struct dso *dso __maybe_unused) -{ - /* Nothing to free. */ -} - /* * 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 diff --git a/tools/perf/util/llvm.h b/tools/perf/util/llvm.h index 8aa19bb6b068..57f6bafb24bb 100644 --- a/tools/perf/util/llvm.h +++ b/tools/perf/util/llvm.h @@ -15,9 +15,6 @@ int llvm__addr2line(const char *dso_name, u64 addr, bool unwind_inlines, struct inline_node *node, struct symbol *sym); =20 - -void dso__free_a2l_llvm(struct dso *dso); - int symbol__disassemble_llvm(const char *filename, struct symbol *sym, struct annotate_args *args); =20 diff --git a/tools/perf/util/srcline.c b/tools/perf/util/srcline.c index 797e78826508..a5d8e994d9ea 100644 --- a/tools/perf/util/srcline.c +++ b/tools/perf/util/srcline.c @@ -1,30 +1,14 @@ // SPDX-License-Identifier: GPL-2.0 -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include - -#include "util/dso.h" -#include "util/debug.h" -#include "util/callchain.h" -#include "util/symbol_conf.h" -#include "llvm.h" #include "srcline.h" -#include "string2.h" +#include "addr2line.h" +#include "dso.h" +#include "callchain.h" +#include "llvm.h" #include "symbol.h" -#include "subcmd/run-command.h" =20 -/* If addr2line doesn't return data for 1 second then timeout. */ -int addr2line_timeout_ms =3D 1 * 1000; +#include +#include + bool srcline_full_filename; =20 char *srcline__unknown =3D (char *)"??:0"; @@ -129,445 +113,22 @@ struct symbol *new_inline_sym(struct dso *dso, return inline_sym; } =20 -#define MAX_INLINE_NEST 1024 - -#ifdef HAVE_LIBLLVM_SUPPORT -#include "llvm.h" - static int addr2line(const char *dso_name, u64 addr, - char **file, unsigned int *line, struct dso *dso, - bool unwind_inlines, struct inline_node *node, - struct symbol *sym) -{ - return llvm__addr2line(dso_name, addr, file, line, dso, unwind_inlines, n= ode, sym); -} - -void dso__free_a2l(struct dso *dso) -{ - dso__free_a2l_llvm(dso); -} - -#else /* HAVE_LIBLLVM_SUPPORT */ - -static int filename_split(char *filename, unsigned int *line_nr) -{ - char *sep; - - sep =3D strchr(filename, '\n'); - if (sep) - *sep =3D '\0'; - - if (!strcmp(filename, "??:0")) - return 0; - - sep =3D strchr(filename, ':'); - if (sep) { - *sep++ =3D '\0'; - *line_nr =3D strtoul(sep, NULL, 0); - return 1; - } - pr_debug("addr2line missing ':' in filename split\n"); - return 0; -} - -static void addr2line_subprocess_cleanup(struct child_process *a2l) + char **file, unsigned int *line_nr, + struct dso *dso, + bool unwind_inlines, + struct inline_node *node, + struct symbol *sym) { - if (a2l->pid !=3D -1) { - kill(a2l->pid, SIGKILL); - finish_command(a2l); /* ignore result, we don't care */ - a2l->pid =3D -1; - close(a2l->in); - close(a2l->out); - } + int ret; =20 - free(a2l); -} + ret =3D llvm__addr2line(dso_name, addr, file, line_nr, dso, unwind_inline= s, node, sym); + if (ret > 0) + return ret; =20 -static struct child_process *addr2line_subprocess_init(const char *addr2li= ne_path, - const char *binary_path) -{ - const char *argv[] =3D { - addr2line_path ?: "addr2line", - "-e", binary_path, - "-a", "-i", "-f", NULL - }; - struct child_process *a2l =3D zalloc(sizeof(*a2l)); - int start_command_status =3D 0; - - if (a2l =3D=3D NULL) { - pr_err("Failed to allocate memory for addr2line"); - return NULL; - } - - a2l->pid =3D -1; - a2l->in =3D -1; - a2l->out =3D -1; - a2l->no_stderr =3D 1; - - a2l->argv =3D argv; - start_command_status =3D start_command(a2l); - a2l->argv =3D NULL; /* it's not used after start_command; avoid dangling = pointers */ - - if (start_command_status !=3D 0) { - pr_warning("could not start addr2line (%s) for %s: start_command return = code %d\n", - addr2line_path, binary_path, start_command_status); - addr2line_subprocess_cleanup(a2l); - return NULL; - } - - return a2l; -} - -enum a2l_style { - BROKEN, - GNU_BINUTILS, - LLVM, -}; - -static enum a2l_style addr2line_configure(struct child_process *a2l, const= char *dso_name) -{ - static bool cached; - static enum a2l_style style; - - if (!cached) { - char buf[128]; - struct io io; - int ch; - int lines; - - if (write(a2l->in, ",\n", 2) !=3D 2) - return BROKEN; - - io__init(&io, a2l->out, buf, sizeof(buf)); - ch =3D io__get_char(&io); - if (ch =3D=3D ',') { - style =3D LLVM; - cached =3D true; - lines =3D 1; - pr_debug("Detected LLVM addr2line style\n"); - } else if (ch =3D=3D '0') { - style =3D GNU_BINUTILS; - cached =3D true; - lines =3D 3; - pr_debug("Detected binutils addr2line style\n"); - } else { - if (!symbol_conf.disable_add2line_warn) { - char *output =3D NULL; - size_t output_len; - - io__getline(&io, &output, &output_len); - pr_warning("%s %s: addr2line configuration failed\n", - __func__, dso_name); - pr_warning("\t%c%s", ch, output); - } - pr_debug("Unknown/broken addr2line style\n"); - return BROKEN; - } - while (lines) { - ch =3D io__get_char(&io); - if (ch <=3D 0) - break; - if (ch =3D=3D '\n') - lines--; - } - /* Ignore SIGPIPE in the event addr2line exits. */ - signal(SIGPIPE, SIG_IGN); - } - return style; + return cmd__addr2line(dso_name, addr, file, line_nr, dso, unwind_inlines,= node, sym); } =20 -static int read_addr2line_record(struct io *io, - enum a2l_style style, - const char *dso_name, - u64 addr, - bool first, - char **function, - char **filename, - unsigned int *line_nr) -{ - /* - * Returns: - * -1 =3D=3D> error - * 0 =3D=3D> sentinel (or other ill-formed) record read - * 1 =3D=3D> a genuine record read - */ - char *line =3D NULL; - size_t line_len =3D 0; - unsigned int dummy_line_nr =3D 0; - int ret =3D -1; - - if (function !=3D NULL) - zfree(function); - - if (filename !=3D NULL) - zfree(filename); - - if (line_nr !=3D NULL) - *line_nr =3D 0; - - /* - * Read the first line. Without an error this will be: - * - for the first line an address like 0x1234, - * - the binutils sentinel 0x0000000000000000, - * - the llvm-addr2line the sentinel ',' character, - * - the function name line for an inlined function. - */ - if (io__getline(io, &line, &line_len) < 0 || !line_len) - goto error; - - pr_debug("%s %s: addr2line read address for sentinel: %s", __func__, dso_= name, line); - if (style =3D=3D LLVM && line_len =3D=3D 2 && line[0] =3D=3D ',') { - /* Found the llvm-addr2line sentinel character. */ - zfree(&line); - return 0; - } else if (style =3D=3D GNU_BINUTILS && (!first || addr !=3D 0)) { - int zero_count =3D 0, non_zero_count =3D 0; - /* - * Check for binutils sentinel ignoring it for the case the - * requested address is 0. - */ - - /* A given address should always start 0x. */ - if (line_len >=3D 2 || line[0] !=3D '0' || line[1] !=3D 'x') { - for (size_t i =3D 2; i < line_len; i++) { - if (line[i] =3D=3D '0') - zero_count++; - else if (line[i] !=3D '\n') - non_zero_count++; - } - if (!non_zero_count) { - int ch; - - if (first && !zero_count) { - /* Line was erroneous just '0x'. */ - goto error; - } - /* - * Line was 0x0..0, the sentinel for binutils. Remove - * the function and filename lines. - */ - zfree(&line); - do { - ch =3D io__get_char(io); - } while (ch > 0 && ch !=3D '\n'); - do { - ch =3D io__get_char(io); - } while (ch > 0 && ch !=3D '\n'); - return 0; - } - } - } - /* Read the second function name line (if inline data then this is the fi= rst line). */ - if (first && (io__getline(io, &line, &line_len) < 0 || !line_len)) - goto error; - - pr_debug("%s %s: addr2line read line: %s", __func__, dso_name, line); - if (function !=3D NULL) - *function =3D strdup(strim(line)); - - zfree(&line); - line_len =3D 0; - - /* Read the third filename and line number line. */ - if (io__getline(io, &line, &line_len) < 0 || !line_len) - goto error; - - pr_debug("%s %s: addr2line filename:number : %s", __func__, dso_name, lin= e); - if (filename_split(line, line_nr =3D=3D NULL ? &dummy_line_nr : line_nr) = =3D=3D 0 && - style =3D=3D GNU_BINUTILS) { - ret =3D 0; - goto error; - } - - if (filename !=3D NULL) - *filename =3D strdup(line); - - zfree(&line); - line_len =3D 0; - - return 1; - -error: - free(line); - if (function !=3D NULL) - zfree(function); - if (filename !=3D NULL) - zfree(filename); - return ret; -} - -static int inline_list__append_record(struct dso *dso, - struct inline_node *node, - struct symbol *sym, - const char *function, - const char *filename, - unsigned int line_nr) -{ - struct symbol *inline_sym =3D new_inline_sym(dso, sym, function); - - return inline_list__append(inline_sym, srcline_from_fileline(filename, li= ne_nr), node); -} - -static int addr2line(const char *dso_name, u64 addr, - char **file, unsigned int *line_nr, - struct dso *dso, - bool unwind_inlines, - struct inline_node *node, - struct symbol *sym __maybe_unused) -{ - struct child_process *a2l =3D dso__a2l(dso); - char *record_function =3D NULL; - char *record_filename =3D NULL; - unsigned int record_line_nr =3D 0; - int record_status =3D -1; - int ret =3D 0; - size_t inline_count =3D 0; - int len; - char buf[128]; - ssize_t written; - struct io io =3D { .eof =3D false }; - enum a2l_style a2l_style; - - if (!a2l) { - if (!filename__has_section(dso_name, ".debug_line")) - goto out; - - dso__set_a2l(dso, - addr2line_subprocess_init(symbol_conf.addr2line_path, dso_name)); - a2l =3D dso__a2l(dso); - } - - if (a2l =3D=3D NULL) { - if (!symbol_conf.disable_add2line_warn) - pr_warning("%s %s: addr2line_subprocess_init failed\n", __func__, dso_n= ame); - goto out; - } - a2l_style =3D addr2line_configure(a2l, dso_name); - if (a2l_style =3D=3D BROKEN) - goto out; - - /* - * Send our request and then *deliberately* send something that can't be - * interpreted as a valid address to ask addr2line about (namely, - * ","). This causes addr2line to first write out the answer to our - * request, in an unbounded/unknown number of records, and then to write - * out the lines "0x0...0", "??" and "??:0", for GNU binutils, or "," - * for llvm-addr2line, so that we can detect when it has finished giving - * us anything useful. - */ - len =3D snprintf(buf, sizeof(buf), "%016"PRIx64"\n,\n", addr); - written =3D len > 0 ? write(a2l->in, buf, len) : -1; - if (written !=3D len) { - if (!symbol_conf.disable_add2line_warn) - pr_warning("%s %s: could not send request\n", __func__, dso_name); - goto out; - } - io__init(&io, a2l->out, buf, sizeof(buf)); - io.timeout_ms =3D addr2line_timeout_ms; - switch (read_addr2line_record(&io, a2l_style, dso_name, addr, /*first=3D*= /true, - &record_function, &record_filename, &record_line_nr)) { - case -1: - if (!symbol_conf.disable_add2line_warn) - pr_warning("%s %s: could not read first record\n", __func__, dso_name); - goto out; - case 0: - /* - * The first record was invalid, so return failure, but first - * read another record, since we sent a sentinel ',' for the - * sake of detected the last inlined function. Treat this as the - * first of a record as the ',' generates a new start with GNU - * binutils, also force a non-zero address as we're no longer - * reading that record. - */ - switch (read_addr2line_record(&io, a2l_style, dso_name, - /*addr=3D*/1, /*first=3D*/true, - NULL, NULL, NULL)) { - case -1: - if (!symbol_conf.disable_add2line_warn) - pr_warning("%s %s: could not read sentinel record\n", - __func__, dso_name); - break; - case 0: - /* The sentinel as expected. */ - break; - default: - if (!symbol_conf.disable_add2line_warn) - pr_warning("%s %s: unexpected record instead of sentinel", - __func__, dso_name); - break; - } - goto out; - default: - /* First record as expected. */ - break; - } - - if (file) { - *file =3D strdup(record_filename); - ret =3D 1; - } - if (line_nr) - *line_nr =3D record_line_nr; - - if (unwind_inlines) { - if (node && inline_list__append_record(dso, node, sym, - record_function, - record_filename, - record_line_nr)) { - ret =3D 0; - goto out; - } - } - - /* - * We have to read the records even if we don't care about the inline - * info. This isn't the first record and force the address to non-zero - * as we're reading records beyond the first. - */ - while ((record_status =3D read_addr2line_record(&io, - a2l_style, - dso_name, - /*addr=3D*/1, - /*first=3D*/false, - &record_function, - &record_filename, - &record_line_nr)) =3D=3D 1) { - if (unwind_inlines && node && inline_count++ < MAX_INLINE_NEST) { - if (inline_list__append_record(dso, node, sym, - record_function, - record_filename, - record_line_nr)) { - ret =3D 0; - goto out; - } - ret =3D 1; /* found at least one inline frame */ - } - } - -out: - free(record_function); - free(record_filename); - if (io.eof) { - dso__set_a2l(dso, NULL); - addr2line_subprocess_cleanup(a2l); - } - return ret; -} - -void dso__free_a2l(struct dso *dso) -{ - struct child_process *a2l =3D dso__a2l(dso); - - if (!a2l) - return; - - addr2line_subprocess_cleanup(a2l); - - dso__set_a2l(dso, NULL); -} - -#endif /* HAVE_LIBLLVM_SUPPORT */ - static struct inline_node *addr2inlines(const char *dso_name, u64 addr, struct dso *dso, struct symbol *sym) { @@ -582,7 +143,9 @@ static struct inline_node *addr2inlines(const char *dso= _name, u64 addr, INIT_LIST_HEAD(&node->val); node->addr =3D addr; =20 - addr2line(dso_name, addr, NULL, NULL, dso, true, node, sym); + addr2line(dso_name, addr, /*file=3D*/NULL, /*line_nr=3D*/NULL, dso, + /*unwind_inlines=3D*/true, node, sym); + return node; } =20 @@ -609,7 +172,7 @@ char *__get_srcline(struct dso *dso, u64 addr, struct s= ymbol *sym, goto out_err; =20 if (!addr2line(dso_name, addr, &file, &line, dso, - unwind_inlines, NULL, sym)) + unwind_inlines, /*node=3D*/NULL, sym)) goto out_err; =20 srcline =3D srcline_from_fileline(file, line); @@ -655,7 +218,8 @@ char *get_srcline_split(struct dso *dso, u64 addr, unsi= gned *line) if (dso_name =3D=3D NULL) goto out_err; =20 - if (!addr2line(dso_name, addr, &file, line, dso, true, NULL, NULL)) + if (!addr2line(dso_name, addr, &file, line, dso, /*unwind_inlines=3D*/tru= e, + /*node=3D*/NULL, /*sym=3D*/NULL)) goto out_err; =20 dso__set_a2l_fails(dso, 0); diff --git a/tools/perf/util/srcline.h b/tools/perf/util/srcline.h index 80c20169e250..ce03b90dea1d 100644 --- a/tools/perf/util/srcline.h +++ b/tools/perf/util/srcline.h @@ -9,7 +9,6 @@ struct dso; struct symbol; =20 -extern int addr2line_timeout_ms; extern bool srcline_full_filename; char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym, bool show_sym, bool show_addr, u64 ip); --=20 2.49.0.805.g082f7c87e0-goog From nobody Fri Dec 19 06:33:53 2025 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.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 31305222564 for ; Thu, 17 Apr 2025 23:08:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744931316; cv=none; b=uA0hPBAxGLHpdeWf/KmDJTpFK/OdCfTYVUy8nCJ1r+DxTUIS31hIcZBN9ixXdYqVqDBlgDnwXihO4UPrqjhqXj+zoZwYV4M07CMUtxKYc2IsMTHyFlgT+FnnXI3QC021EOPn6xRQogcujDkFDspMsXjMdv0EvKAKTiud/MrFPBA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744931316; c=relaxed/simple; bh=BMIYhQe06B1m/m1VG1ijiI+0phHw0fLo69Q/i9qHTls=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=EHQyA0y+dRyccXsvdFwWf6mM4T6YN4ZYMRuyGf5/2ADVVZD6RrBh6yYhMCi1I9s0+k+XdIzwfLGioCk7XS58hDxQR5mEXoO9dk3RY6gEbuv5eT14ZXKvJyJQnZs12NfQDffBfDI7eaq9VunNZNkLSRO2kCQC87g3cGvujkJ1EaU= 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=ssB2vQJy; arc=none smtp.client-ip=209.85.215.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="ssB2vQJy" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-b06b29fee16so1323517a12.0 for ; Thu, 17 Apr 2025 16:08:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1744931314; x=1745536114; 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=OWFtkFZGIqy5t/lwfSWZdekY1UZ0W7H50S3h5IILNr0=; b=ssB2vQJybKGPyWCD1t5Y+aDP5S7146o8l6tysGdfMLs9s0zRCjBh41+tNHGXTkc8JA sqXk0S8GLNpS6rQmKBflWXV3h4FGGxhoP/khDjHqKrS5UjBTxp7g2u0hVDQsiPLxlgnZ Ev6aMU/ZTo6eYyMIxB1dUaT+y2yb9y3hhPtTKBqjxv3yT2ZCIC/jwp/P1XrfP5p3sX++ aHC3JxvUGxI1RUZm6oPvyqTouuc1y+8qInjnvmp2gRDrhWImgFniLyvLGR7+VoRGXwW5 EtVUNk0ccSJXVSG00EjgcJojpcVNfHfFysqDVcDsYkZOCKj33sFvyGzyjyolJRqWyd8s D4yQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744931314; x=1745536114; 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=OWFtkFZGIqy5t/lwfSWZdekY1UZ0W7H50S3h5IILNr0=; b=wnaYewX2dO+G77/xi5UVOKp25hAL6MvJMY8YccjYDanPhyTkPBGx6g3g/3kFw8AK4p nM05e/N7X9mo9atxewhAvCxA1ueVOnajOQYl1/674fcxTxscFJpEVuj2g/V5pz62ugd5 T3N5VkOHL6HfPNXJawV/HHYVKsPo0IHs8ZX+mDChzafDWJTZ5tf3D5xqOsoa+y+yR6tY JlUIp04G7o5j5QREdMYlzo1QMjaHtV0llgJJVhrYVptaQl3qstSKsbihUtKVmpBgjaiQ sO6GJKN+AGw7MzBo4m71wNvaVDbJr727+fFIfhMfPT+JeGFpu/FwxXjbCxVLUQfe+am8 ufEg== X-Forwarded-Encrypted: i=1; AJvYcCXVUGdrS/9UXCuaAgleZeuqXxzuQtmJEknwfAb5EAKCwtu4Pxa4F8DdFNXPUbxR21R0wUeXSIpuPQQuSp0=@vger.kernel.org X-Gm-Message-State: AOJu0YzflLXUzaIV0ohvAebMP49TlQDZ9AsRbVqnqdA7NHTpZTZuKFuQ AQxIzJI7/JSz7Il189QCrPNSY6Fe477bmmbgImswxZTlfKD+MtE86AS/cikebdoX0cjOW7Job5j 3MPCk/w== X-Google-Smtp-Source: AGHT+IEViYHLkFOWXw+5kwQsGXZZ/Zsx+Y2Lhd4vhaB6w7nKZkxVtdfZ8HyaZrzD+J1xMdknmkgOW1xEQGPK X-Received: from plbaz2.prod.google.com ([2002:a17:902:a582:b0:220:eaf6:fcbf]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:d4cd:b0:21f:35fd:1b6c with SMTP id d9443c01a7336-22c5364235fmr8482205ad.45.1744931314420; Thu, 17 Apr 2025 16:08:34 -0700 (PDT) Date: Thu, 17 Apr 2025 16:07:40 -0700 In-Reply-To: <20250417230740.86048-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: <20250417230740.86048-1-irogers@google.com> X-Mailer: git-send-email 2.49.0.805.g082f7c87e0-goog Message-ID: <20250417230740.86048-20-irogers@google.com> Subject: [PATCH v4 19/19] perf disasm: Remove unused evsel from annotate_args 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Set in symbol__annotate but never used. Signed-off-by: Ian Rogers --- tools/perf/util/annotate.c | 1 - tools/perf/util/disasm.h | 1 - 2 files changed, 2 deletions(-) diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index 264a212b47df..a2a2849a5b79 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -1013,7 +1013,6 @@ int symbol__annotate(struct map_symbol *ms, struct ev= sel *evsel, struct symbol *sym =3D ms->sym; struct annotation *notes =3D symbol__annotation(sym); struct annotate_args args =3D { - .evsel =3D evsel, .options =3D &annotate_opts, }; struct arch *arch =3D NULL; diff --git a/tools/perf/util/disasm.h b/tools/perf/util/disasm.h index 09c86f540f7f..d2cb555e4a3b 100644 --- a/tools/perf/util/disasm.h +++ b/tools/perf/util/disasm.h @@ -98,7 +98,6 @@ struct ins_ops { struct annotate_args { struct arch *arch; struct map_symbol ms; - struct evsel *evsel; struct annotation_options *options; s64 offset; char *line; --=20 2.49.0.805.g082f7c87e0-goog