Skip to contentSkip to conversation

tools lib traceevent: Export dynamic symbols used by traceevent plugins

PATCH Superseded He Kuang 4 participants 6 messages May 11, 2015 – May 12, 2015 perf events

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

He Kuang

author
#0 lore raw

PATCH 1/2 tools lib traceevent: Export dynamic symbols used by traceevent plugins

Traceevent plugins need dynamic symbols exported from libtraceevent.a, otherwise a dlopen error will occur during plugins loading.

This patch uses dynamic-list-file to export dynamic symbols which will be used in plugins to perf executable.

The problem is covered up if feature-libpython is enabled, because PYTHON_EMBED_LDOPTS contains '-Xlinker --export-dynamic' which adds all symbols to the dynamic symbol table. So we should reproduce the problem by setting NO_LIBPYTHON=1.

Before this patch:

(Prepare plugins)

  $ ls /root/.traceevent/plugins/
  plugin_sched_switch.so
  plugin_function.so
  ...
  $ perf record -e 'ftrace:function' ls
  $ perf script
    Warning: could not load plugin '/mnt/data/root/.traceevent/plugins/plugin_sched_switch.so'
    /root/.traceevent/plugins/plugin_sched_switch.so: undefined symbol: pevent_unregister_event_handler
    Warning: could not load plugin '/root/.traceevent/plugins/plugin_function.so'
    /root/.traceevent/plugins/plugin_function.so: undefined symbol: warning
    ...
           :1049  1049 [000]  9666.754487: ftrace:function:  ffffffff8118bc50 <-- ffffffff8118c5b3
           :1049  1049 [000]  9666.754487: ftrace:function:  ffffffff818e2440 <-- ffffffff8118bc75
           :1049  1049 [000]  9666.754487: ftrace:function:  ffffffff8106eee0 <-- ffffffff811212e2

After this patch:

  $ perf record -e 'ftrace:function' ls
  $ perf script
           :1049  1049 [000]  9666.754487: ftrace:function: __set_task_comm
           :1049  1049 [000]  9666.754487: ftrace:function:    _raw_spin_lock
           :1049  1049 [000]  9666.754487: ftrace:function: task_tgid_nr_ns
           ...
---
 tools/lib/traceevent/Makefile | 14 +++++++++++++-
 tools/perf/Makefile.perf      |  9 +++++++--
 2 files changed, 20 insertions(+), 3 deletions(-)
tools/lib/traceevent/Makefile +13 -1
index d410da3..2cbac13 100644
@@ -23,6 +23,7 @@ endef
 # Allow setting CC and AR, or setting CROSS_COMPILE as a prefix.
 $(call allow-override,CC,$(CROSS_COMPILE)gcc)
 $(call allow-override,AR,$(CROSS_COMPILE)ar)
+$(call allow-override,NM,$(CROSS_COMPILE)nm)
 
 EXT = -std=gnu99
 INSTALL = install
@@ -151,8 +152,9 @@ PLUGINS_IN := $(PLUGINS:.so=-in.o)
 
 TE_IN    := $(OUTPUT)libtraceevent-in.o
 LIB_FILE := $(addprefix $(OUTPUT),$(LIB_FILE))
+DYNAMIC_LIST_FILE := $(OUTPUT)libtraceevent-dynamic-list
 
-CMD_TARGETS = $(LIB_FILE) $(PLUGINS)
+CMD_TARGETS = $(LIB_FILE) $(PLUGINS) $(DYNAMIC_LIST_FILE)
 
 TARGETS = $(CMD_TARGETS)
 
@@ -169,6 +171,9 @@ $(OUTPUT)libtraceevent.so: $(TE_IN)
 $(OUTPUT)libtraceevent.a: $(TE_IN)
 	$(QUIET_LINK)$(RM) $@; $(AR) rcs $@ $^
 
+$(OUTPUT)libtraceevent-dynamic-list: $(PLUGINS)
+	$(QUIET_GEN)$(call do_generate_dynamic_list_file, $(PLUGINS), $@)
+
 plugins: $(PLUGINS)
 
 __plugin_obj = $(notdir $@)
@@ -238,6 +243,13 @@ define do_install_plugins
 	done
 endef
 
+define do_generate_dynamic_list_file
+	(echo '{';							\
+	$(NM) -u -D $1 | awk 'NF>1 {print "\t"$$2";"}' | sort -u;	\
+	echo '};';							\
+	) > $2
+endef
+
 install_lib: all_cmd install_plugins
 	$(call QUIET_INSTALL, $(LIB_FILE)) \
 		$(call do_install,$(LIB_FILE),$(bindir_SQ))
tools/perf/Makefile.perf +7 -2
index 03409cc..19c61e0 100644
@@ -172,6 +172,7 @@ endif
 
 LIBTRACEEVENT = $(TE_PATH)libtraceevent.a
 export LIBTRACEEVENT
+LIBTRACEEVENT_DYNAMIC_LIST = $(TE_PATH)libtraceevent-dynamic-list
 
 LIBAPI = $(LIB_PATH)libapi.a
 export LIBAPI
@@ -278,8 +279,9 @@ build := -f $(srctree)/tools/build/Makefile.build dir=. obj
 $(PERF_IN): $(OUTPUT)PERF-VERSION-FILE $(OUTPUT)common-cmds.h FORCE
 	$(Q)$(MAKE) $(build)=perf
 
-$(OUTPUT)perf: $(PERFLIBS) $(PERF_IN)
-	$(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) $(PERF_IN) $(LIBS) -o $@
+LD_LIBTRACEEVENT_FLAGS += -Xlinker --dynamic-list=$(LIBTRACEEVENT_DYNAMIC_LIST)
+$(OUTPUT)perf: $(PERFLIBS) $(PERF_IN) $(LIBTRACEEVENT_DYNAMIC_LIST)
+	$(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) $(LD_LIBTRACEEVENT_FLAGS) $(PERF_IN) $(LIBS) -o $@
 
 $(GTK_IN): FORCE
 	$(Q)$(MAKE) $(build)=gtk
@@ -375,6 +377,9 @@ LIBTRACEEVENT_FLAGS += plugin_dir=$(plugindir_SQ)
 $(LIBTRACEEVENT): FORCE
 	$(Q)$(MAKE) -C $(TRACE_EVENT_DIR) $(LIBTRACEEVENT_FLAGS) O=$(OUTPUT) $(OUTPUT)libtraceevent.a plugins
 
+$(LIBTRACEEVENT_DYNAMIC_LIST): $(LIBTRACEEVENT)
+	$(Q)$(MAKE) -C $(TRACE_EVENT_DIR) $(LIBTRACEEVENT_FLAGS) O=$(OUTPUT) $(OUTPUT)libtraceevent-dynamic-list
+
 $(LIBTRACEEVENT)-clean:
 	$(call QUIET_CLEAN, libtraceevent)
 	$(Q)$(MAKE) -C $(TRACE_EVENT_DIR) O=$(OUTPUT) clean >/dev/null
Signature · 1 line
1.8.5.2
author
#1 lore raw

PATCH 2/2 tools lib traceevent: Ignore libtrace-dynamic-list file

The libtrace-dynamic-list file is used to export symbols used by traceevent plugins.

---
 tools/lib/traceevent/.gitignore | 1 +
 1 file changed, 1 insertion(+)
tools/lib/traceevent/.gitignore +1 -0
index 35f56be..3c60335 100644
@@ -1 +1,2 @@
 TRACEEVENT-CFLAGS
+libtraceevent-dynamic-list
Signature · 1 line
1.8.5.2
#2 lore raw
In reply to He Kuang
Quoted 10 lines · He Kuang

Traceevent plugins need dynamic symbols exported from libtraceevent.a, otherwise a dlopen error will occur during plugins loading.

This patch uses dynamic-list-file to export dynamic symbols which will be used in plugins to perf executable.

The problem is covered up if feature-libpython is enabled, because PYTHON_EMBED_LDOPTS contains '-Xlinker --export-dynamic' which adds all symbols to the dynamic symbol table. So we should reproduce the problem by setting NO_LIBPYTHON=1.

there's was another patch taking the shortcut:
http://marc.info/?l=linux-kernel&m=142885447432384&w=2

it seems it's not merged yet, but I like this one better

SNIP

Quoted 25 lines
tools/perf/Makefile.perf +4 -2
index 03409cc..19c61e0 100644
@@ -172,6 +172,7 @@ endif
 
 LIBTRACEEVENT = $(TE_PATH)libtraceevent.a
 export LIBTRACEEVENT
+LIBTRACEEVENT_DYNAMIC_LIST = $(TE_PATH)libtraceevent-dynamic-list
 
 LIBAPI = $(LIB_PATH)libapi.a
 export LIBAPI
@@ -278,8 +279,9 @@ build := -f $(srctree)/tools/build/Makefile.build dir=. obj
 $(PERF_IN): $(OUTPUT)PERF-VERSION-FILE $(OUTPUT)common-cmds.h FORCE
 	$(Q)$(MAKE) $(build)=perf
 
-$(OUTPUT)perf: $(PERFLIBS) $(PERF_IN)
-	$(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) $(PERF_IN) $(LIBS) -o $@
+LD_LIBTRACEEVENT_FLAGS += -Xlinker --dynamic-list=$(LIBTRACEEVENT_DYNAMIC_LIST)
+$(OUTPUT)perf: $(PERFLIBS) $(PERF_IN) $(LIBTRACEEVENT_DYNAMIC_LIST)
+	$(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) $(LD_LIBTRACEEVENT_FLAGS) $(PERF_IN) $(LIBS) -o $@
 
 $(GTK_IN): FORCE
 	$(Q)$(MAKE) $(build)=gtk

tools/perf/Makefile.perf LIBTRACEEVENT_FLAGS += plugin_dir=$(plugindir_SQ) · line 381

Quoted 5 lines
tools/perf/Makefile.perf +2 -0
@@ -375,6 +377,9 @@ LIBTRACEEVENT_FLAGS += plugin_dir=$(plugindir_SQ)
 $(LIBTRACEEVENT): FORCE
 	$(Q)$(MAKE) -C $(TRACE_EVENT_DIR) $(LIBTRACEEVENT_FLAGS) O=$(OUTPUT) $(OUTPUT)libtraceevent.a plugins
 
+$(LIBTRACEEVENT_DYNAMIC_LIST): $(LIBTRACEEVENT)
+	$(Q)$(MAKE) -C $(TRACE_EVENT_DIR) $(LIBTRACEEVENT_FLAGS) O=$(OUTPUT) $(OUTPUT)libtraceevent-dynamic-list

hum, do we need extra target in Makefile.perf, it could be rebuilt any time 'plugins:' target in lib/tracevent/Makefile is called

jirka

#3 lore raw

Em Mon, May 11, 2015 at 04:33:09PM +0200, Jiri Olsa escreveu:

Quoted 12 lines · He Kuang
Quoted 10 lines

Traceevent plugins need dynamic symbols exported from libtraceevent.a, otherwise a dlopen error will occur during plugins loading.

This patch uses dynamic-list-file to export dynamic symbols which will be used in plugins to perf executable.

The problem is covered up if feature-libpython is enabled, because PYTHON_EMBED_LDOPTS contains '-Xlinker --export-dynamic' which adds all symbols to the dynamic symbol table. So we should reproduce the problem by setting NO_LIBPYTHON=1.

Quoted 4 lines

there's was another patch taking the shortcut:
http://marc.info/?l=linux-kernel&m=142885447432384&w=2

it seems it's not merged yet, but I like this one better

Ok, please let me know when something that can be merged is ready,

- Arnaldo

Quoted 38 lines

SNIP

Quoted 25 lines
tools/perf/Makefile.perf +4 -2
index 03409cc..19c61e0 100644
@@ -172,6 +172,7 @@ endif
 
 LIBTRACEEVENT = $(TE_PATH)libtraceevent.a
 export LIBTRACEEVENT
+LIBTRACEEVENT_DYNAMIC_LIST = $(TE_PATH)libtraceevent-dynamic-list
 
 LIBAPI = $(LIB_PATH)libapi.a
 export LIBAPI
@@ -278,8 +279,9 @@ build := -f $(srctree)/tools/build/Makefile.build dir=. obj
 $(PERF_IN): $(OUTPUT)PERF-VERSION-FILE $(OUTPUT)common-cmds.h FORCE
 	$(Q)$(MAKE) $(build)=perf
 
-$(OUTPUT)perf: $(PERFLIBS) $(PERF_IN)
-	$(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) $(PERF_IN) $(LIBS) -o $@
+LD_LIBTRACEEVENT_FLAGS += -Xlinker --dynamic-list=$(LIBTRACEEVENT_DYNAMIC_LIST)
+$(OUTPUT)perf: $(PERFLIBS) $(PERF_IN) $(LIBTRACEEVENT_DYNAMIC_LIST)
+	$(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) $(LD_LIBTRACEEVENT_FLAGS) $(PERF_IN) $(LIBS) -o $@
 
 $(GTK_IN): FORCE
 	$(Q)$(MAKE) $(build)=gtk

tools/perf/Makefile.perf LIBTRACEEVENT_FLAGS += plugin_dir=$(plugindir_SQ) · line 381

Quoted 5 lines
tools/perf/Makefile.perf +2 -0
@@ -375,6 +377,9 @@ LIBTRACEEVENT_FLAGS += plugin_dir=$(plugindir_SQ)
 $(LIBTRACEEVENT): FORCE
 	$(Q)$(MAKE) -C $(TRACE_EVENT_DIR) $(LIBTRACEEVENT_FLAGS) O=$(OUTPUT) $(OUTPUT)libtraceevent.a plugins
 
+$(LIBTRACEEVENT_DYNAMIC_LIST): $(LIBTRACEEVENT)
+	$(Q)$(MAKE) -C $(TRACE_EVENT_DIR) $(LIBTRACEEVENT_FLAGS) O=$(OUTPUT) $(OUTPUT)libtraceevent-dynamic-list

hum, do we need extra target in Makefile.perf, it could be rebuilt any time 'plugins:' target in lib/tracevent/Makefile is called

jirka

#4 lore raw
In reply to Jiri Olsa
Quoted 12 lines · Jiri Olsa
Quoted 10 lines · He Kuang

Traceevent plugins need dynamic symbols exported from libtraceevent.a, otherwise a dlopen error will occur during plugins loading.

This patch uses dynamic-list-file to export dynamic symbols which will be used in plugins to perf executable.

The problem is covered up if feature-libpython is enabled, because PYTHON_EMBED_LDOPTS contains '-Xlinker --export-dynamic' which adds all symbols to the dynamic symbol table. So we should reproduce the problem by setting NO_LIBPYTHON=1.

Quoted 5 lines

there's was another patch taking the shortcut:
http://marc.info/?l=linux-kernel&m=142885447432384&w=2

it seems it's not merged yet, but I like this one better

I don't care much as long as it get fixed -- either using the big hammer with -Wl,--export-dynamic or the fine grained approach using a symbol file.

Thanks,
Mathias

author
#5 lore raw
In reply to Jiri Olsa

Hi, jirka

Quoted 47 lines · Jiri Olsa
Quoted 10 lines · He Kuang

Traceevent plugins need dynamic symbols exported from libtraceevent.a, otherwise a dlopen error will occur during plugins loading.

This patch uses dynamic-list-file to export dynamic symbols which will be used in plugins to perf executable.

The problem is covered up if feature-libpython is enabled, because PYTHON_EMBED_LDOPTS contains '-Xlinker --export-dynamic' which adds all symbols to the dynamic symbol table. So we should reproduce the problem by setting NO_LIBPYTHON=1.

there's was another patch taking the shortcut:

   http://marc.info/?l=linux-kernel&m=142885447432384&w=2

it seems it's not merged yet, but I like this one better

SNIP

Quoted 28 lines
tools/perf/Makefile.perf +4 -2
index 03409cc..19c61e0 100644
@@ -172,6 +172,7 @@ endif
 
  LIBTRACEEVENT = $(TE_PATH)libtraceevent.a
  export LIBTRACEEVENT
+LIBTRACEEVENT_DYNAMIC_LIST = $(TE_PATH)libtraceevent-dynamic-list
 
  LIBAPI = $(LIB_PATH)libapi.a
  export LIBAPI
@@ -278,8 +279,9 @@ build := -f $(srctree)/tools/build/Makefile.build dir=. obj
  $(PERF_IN): $(OUTPUT)PERF-VERSION-FILE $(OUTPUT)common-cmds.h FORCE
  	$(Q)$(MAKE) $(build)=perf
 
-$(OUTPUT)perf: $(PERFLIBS) $(PERF_IN)
-	$(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) $(PERF_IN) $(LIBS) -o $@
+LD_LIBTRACEEVENT_FLAGS += -Xlinker --dynamic-list=$(LIBTRACEEVENT_DYNAMIC_LIST)
+$(OUTPUT)perf: $(PERFLIBS) $(PERF_IN) $(LIBTRACEEVENT_DYNAMIC_LIST)
+	$(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) $(LD_LIBTRACEEVENT_FLAGS) $(PERF_IN) $(LIBS) -o $@
 
  $(GTK_IN): FORCE
  	$(Q)$(MAKE) $(build)=gtk
@@ -375,6 +377,9 @@ LIBTRACEEVENT_FLAGS += plugin_dir=$(plugindir_SQ)
  $(LIBTRACEEVENT): FORCE
  	$(Q)$(MAKE) -C $(TRACE_EVENT_DIR) $(LIBTRACEEVENT_FLAGS) O=$(OUTPUT) $(OUTPUT)libtraceevent.a plugins
 
Quoted 6 lines

tools/perf/Makefile.perf LIBTRACEEVENT_FLAGS += plugin_dir=$(plugindir_SQ) · line 381

Quoted 2 lines
tools/perf/Makefile.perf +2 -0
+$(LIBTRACEEVENT_DYNAMIC_LIST): $(LIBTRACEEVENT)
+	$(Q)$(MAKE) -C $(TRACE_EVENT_DIR) $(LIBTRACEEVENT_FLAGS) O=$(OUTPUT) $(OUTPUT)libtraceevent-dynamic-list

hum, do we need extra target in Makefile.perf, it could be rebuilt any time 'plugins:' target in lib/tracevent/Makefile is called

jirka

Yes, the dynamic-list-file should be rebuilt when plugins changed, and not depends on $(LIBTRACEEVENT).

I sent a new version, please review that patch.

  $ touch ../lib/traceevent/plugin_function.c
  $ make
  CC       plugin_function.o
  LD       plugin_function-in.o
  LINK     plugin_function.so
  GEN      libtraceevent-dynamic-list
  LINK     perf

Thanks.