Skip to content

Commit e254e0c

Browse files
committed
Merge tag 'perf-tools-fixes-for-v6.11-2024-07-30' of git://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools
Pull perf tools fixes from Namhyung Kim: "Some more build fixes and a random crash fix: - Fix cross-build by setting pkg-config env according to the arch - Fix static build for missing library dependencies - Fix Segfault when callchain has no symbols" * tag 'perf-tools-fixes-for-v6.11-2024-07-30' of git://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools: perf docs: Document cross compilation perf: build: Link lib 'zstd' for static build perf: build: Link lib 'lzma' for static build perf: build: Only link libebl.a for old libdw perf: build: Set Python configuration for cross compilation perf: build: Setup PKG_CONFIG_LIBDIR for cross compilation perf tool: fix dereferencing NULL al->maps
2 parents c91a7de + d27087c commit e254e0c

File tree

5 files changed

+117
-13
lines changed

5 files changed

+117
-13
lines changed

tools/build/feature/Makefile

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,30 @@ FILES= \
8282

8383
FILES := $(addprefix $(OUTPUT),$(FILES))
8484

85-
PKG_CONFIG ?= $(CROSS_COMPILE)pkg-config
85+
# Some distros provide the command $(CROSS_COMPILE)pkg-config for
86+
# searching packges installed with Multiarch. Use it for cross
87+
# compilation if it is existed.
88+
ifneq (, $(shell which $(CROSS_COMPILE)pkg-config))
89+
PKG_CONFIG ?= $(CROSS_COMPILE)pkg-config
90+
else
91+
PKG_CONFIG ?= pkg-config
92+
93+
# PKG_CONFIG_PATH or PKG_CONFIG_LIBDIR, alongside PKG_CONFIG_SYSROOT_DIR
94+
# for modified system root, are required for the cross compilation.
95+
# If these PKG_CONFIG environment variables are not set, Multiarch library
96+
# paths are used instead.
97+
ifdef CROSS_COMPILE
98+
ifeq ($(PKG_CONFIG_LIBDIR)$(PKG_CONFIG_PATH)$(PKG_CONFIG_SYSROOT_DIR),)
99+
CROSS_ARCH = $(shell $(CC) -dumpmachine)
100+
PKG_CONFIG_LIBDIR := /usr/local/$(CROSS_ARCH)/lib/pkgconfig/
101+
PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/local/lib/$(CROSS_ARCH)/pkgconfig/
102+
PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/lib/$(CROSS_ARCH)/pkgconfig/
103+
PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/local/share/pkgconfig/
104+
PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/share/pkgconfig/
105+
export PKG_CONFIG_LIBDIR
106+
endif
107+
endif
108+
endif
86109

87110
all: $(FILES)
88111

@@ -147,7 +170,17 @@ $(OUTPUT)test-libopencsd.bin:
147170

148171
DWARFLIBS := -ldw
149172
ifeq ($(findstring -static,${LDFLAGS}),-static)
150-
DWARFLIBS += -lelf -lebl -lz -llzma -lbz2
173+
DWARFLIBS += -lelf -lz -llzma -lbz2 -lzstd
174+
175+
LIBDW_VERSION := $(shell $(PKG_CONFIG) --modversion libdw)
176+
LIBDW_VERSION_1 := $(word 1, $(subst ., ,$(LIBDW_VERSION)))
177+
LIBDW_VERSION_2 := $(word 2, $(subst ., ,$(LIBDW_VERSION)))
178+
179+
# Elfutils merged libebl.a into libdw.a starting from version 0.177,
180+
# Link libebl.a only if libdw is older than this version.
181+
ifeq ($(shell test $(LIBDW_VERSION_2) -lt 177; echo $$?),0)
182+
DWARFLIBS += -lebl
183+
endif
151184
endif
152185

153186
$(OUTPUT)test-dwarf.bin:
@@ -178,27 +211,27 @@ $(OUTPUT)test-numa_num_possible_cpus.bin:
178211
$(BUILD) -lnuma
179212

180213
$(OUTPUT)test-libunwind.bin:
181-
$(BUILD) -lelf
214+
$(BUILD) -lelf -llzma
182215

183216
$(OUTPUT)test-libunwind-debug-frame.bin:
184-
$(BUILD) -lelf
217+
$(BUILD) -lelf -llzma
185218
$(OUTPUT)test-libunwind-x86.bin:
186-
$(BUILD) -lelf -lunwind-x86
219+
$(BUILD) -lelf -llzma -lunwind-x86
187220

188221
$(OUTPUT)test-libunwind-x86_64.bin:
189-
$(BUILD) -lelf -lunwind-x86_64
222+
$(BUILD) -lelf -llzma -lunwind-x86_64
190223

191224
$(OUTPUT)test-libunwind-arm.bin:
192-
$(BUILD) -lelf -lunwind-arm
225+
$(BUILD) -lelf -llzma -lunwind-arm
193226

194227
$(OUTPUT)test-libunwind-aarch64.bin:
195-
$(BUILD) -lelf -lunwind-aarch64
228+
$(BUILD) -lelf -llzma -lunwind-aarch64
196229

197230
$(OUTPUT)test-libunwind-debug-frame-arm.bin:
198-
$(BUILD) -lelf -lunwind-arm
231+
$(BUILD) -lelf -llzma -lunwind-arm
199232

200233
$(OUTPUT)test-libunwind-debug-frame-aarch64.bin:
201-
$(BUILD) -lelf -lunwind-aarch64
234+
$(BUILD) -lelf -llzma -lunwind-aarch64
202235

203236
$(OUTPUT)test-libaudit.bin:
204237
$(BUILD) -laudit

tools/perf/Documentation/Build.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,31 @@ supported by GCC. UBSan detects undefined behaviors of programs at runtime.
7171
$ UBSAN_OPTIONS=print_stacktrace=1 ./perf record -a
7272

7373
If UBSan detects any problem at runtime, it outputs a “runtime error:” message.
74+
75+
4) Cross compilation
76+
====================
77+
As Multiarch is commonly supported in Linux distributions, we can install
78+
libraries for multiple architectures on the same system and then cross-compile
79+
Linux perf. For example, Aarch64 libraries and toolchains can be installed on
80+
an x86_64 machine, allowing us to compile perf for an Aarch64 target.
81+
82+
Below is the command for building the perf with dynamic linking.
83+
84+
$ cd /path/to/Linux
85+
$ make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -C tools/perf
86+
87+
For static linking, the option `LDFLAGS="-static"` is required.
88+
89+
$ make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- \
90+
LDFLAGS="-static" -C tools/perf
91+
92+
In the embedded system world, a use case is to explicitly specify the package
93+
configuration paths for cross building:
94+
95+
$ PKG_CONFIG_SYSROOT_DIR="/path/to/cross/build/sysroot" \
96+
PKG_CONFIG_LIBDIR="/usr/lib/:/usr/local/lib" \
97+
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -C tools/perf
98+
99+
In this case, the variable PKG_CONFIG_SYSROOT_DIR can be used alongside the
100+
variable PKG_CONFIG_LIBDIR or PKG_CONFIG_PATH to prepend the sysroot path to
101+
the library paths for cross compilation.

tools/perf/Makefile.config

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,17 @@ ifdef LIBDW_DIR
152152
endif
153153
DWARFLIBS := -ldw
154154
ifeq ($(findstring -static,${LDFLAGS}),-static)
155-
DWARFLIBS += -lelf -lebl -ldl -lz -llzma -lbz2
155+
DWARFLIBS += -lelf -ldl -lz -llzma -lbz2 -lzstd
156+
157+
LIBDW_VERSION := $(shell $(PKG_CONFIG) --modversion libdw)
158+
LIBDW_VERSION_1 := $(word 1, $(subst ., ,$(LIBDW_VERSION)))
159+
LIBDW_VERSION_2 := $(word 2, $(subst ., ,$(LIBDW_VERSION)))
160+
161+
# Elfutils merged libebl.a into libdw.a starting from version 0.177,
162+
# Link libebl.a only if libdw is older than this version.
163+
ifeq ($(shell test $(LIBDW_VERSION_2) -lt 177; echo $$?),0)
164+
DWARFLIBS += -lebl
165+
endif
156166
endif
157167
FEATURE_CHECK_CFLAGS-libdw-dwarf-unwind := $(LIBDW_CFLAGS)
158168
FEATURE_CHECK_LDFLAGS-libdw-dwarf-unwind := $(LIBDW_LDFLAGS) $(DWARFLIBS)
@@ -296,6 +306,11 @@ endif
296306

297307
ifdef PYTHON_CONFIG
298308
PYTHON_EMBED_LDOPTS := $(shell $(PYTHON_CONFIG_SQ) $(PYTHON_CONFIG_LDFLAGS) 2>/dev/null)
309+
# Update the python flags for cross compilation
310+
ifdef CROSS_COMPILE
311+
PYTHON_NATIVE := $(shell echo $(PYTHON_EMBED_LDOPTS) | sed 's/\(-L.*\/\)\(.*-linux-gnu\).*/\2/')
312+
PYTHON_EMBED_LDOPTS := $(subst $(PYTHON_NATIVE),$(shell $(CC) -dumpmachine),$(PYTHON_EMBED_LDOPTS))
313+
endif
299314
PYTHON_EMBED_LDFLAGS := $(call strip-libs,$(PYTHON_EMBED_LDOPTS))
300315
PYTHON_EMBED_LIBADD := $(call grep-libs,$(PYTHON_EMBED_LDOPTS)) -lutil
301316
PYTHON_EMBED_CCOPTS := $(shell $(PYTHON_CONFIG_SQ) --includes 2>/dev/null)
@@ -897,6 +912,9 @@ else
897912
PYTHON_SETUPTOOLS_INSTALLED := $(shell $(PYTHON) -c 'import setuptools;' 2> /dev/null && echo "yes" || echo "no")
898913
ifeq ($(PYTHON_SETUPTOOLS_INSTALLED), yes)
899914
PYTHON_EXTENSION_SUFFIX := $(shell $(PYTHON) -c 'from importlib import machinery; print(machinery.EXTENSION_SUFFIXES[0])')
915+
ifdef CROSS_COMPILE
916+
PYTHON_EXTENSION_SUFFIX := $(subst $(PYTHON_NATIVE),$(shell $(CC) -dumpmachine),$(PYTHON_EXTENSION_SUFFIX))
917+
endif
900918
LANG_BINDINGS += $(obj-perf)python/perf$(PYTHON_EXTENSION_SUFFIX)
901919
else
902920
$(warning Missing python setuptools, the python binding won't be built, please install python3-setuptools or equivalent)

tools/perf/Makefile.perf

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,32 @@ HOSTLD ?= ld
193193
HOSTAR ?= ar
194194
CLANG ?= clang
195195

196-
PKG_CONFIG = $(CROSS_COMPILE)pkg-config
196+
# Some distros provide the command $(CROSS_COMPILE)pkg-config for
197+
# searching packges installed with Multiarch. Use it for cross
198+
# compilation if it is existed.
199+
ifneq (, $(shell which $(CROSS_COMPILE)pkg-config))
200+
PKG_CONFIG ?= $(CROSS_COMPILE)pkg-config
201+
else
202+
PKG_CONFIG ?= pkg-config
203+
204+
# PKG_CONFIG_PATH or PKG_CONFIG_LIBDIR, alongside PKG_CONFIG_SYSROOT_DIR
205+
# for modified system root, is required for the cross compilation.
206+
# If these PKG_CONFIG environment variables are not set, Multiarch library
207+
# paths are used instead.
208+
ifdef CROSS_COMPILE
209+
ifeq ($(PKG_CONFIG_LIBDIR)$(PKG_CONFIG_PATH)$(PKG_CONFIG_SYSROOT_DIR),)
210+
CROSS_ARCH = $(shell $(CC) -dumpmachine)
211+
PKG_CONFIG_LIBDIR := /usr/local/$(CROSS_ARCH)/lib/pkgconfig/
212+
PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/local/lib/$(CROSS_ARCH)/pkgconfig/
213+
PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/lib/$(CROSS_ARCH)/pkgconfig/
214+
PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/local/share/pkgconfig/
215+
PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/share/pkgconfig/
216+
export PKG_CONFIG_LIBDIR
217+
$(warning Missing PKG_CONFIG_LIBDIR, PKG_CONFIG_PATH and PKG_CONFIG_SYSROOT_DIR for cross compilation,)
218+
$(warning set PKG_CONFIG_LIBDIR for using Multiarch libs.)
219+
endif
220+
endif
221+
endif
197222

198223
RM = rm -f
199224
LN = ln -f

tools/perf/util/callchain.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1141,7 +1141,7 @@ int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *samp
11411141
int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *node,
11421142
bool hide_unresolved)
11431143
{
1144-
struct machine *machine = maps__machine(node->ms.maps);
1144+
struct machine *machine = node->ms.maps ? maps__machine(node->ms.maps) : NULL;
11451145

11461146
maps__put(al->maps);
11471147
al->maps = maps__get(node->ms.maps);

0 commit comments

Comments
 (0)