perf tools: port UI from GTK2 to GTK4
cover perf tools: port UI from GTK2 to GTK4
GTK2 is long dead upstream and increasingly hard to keep building on current distros. This series ports perf's GTK-based report browser to GTK4 and fixes it up so it's actually loadable at runtime after the port.
Patch 1 does the mechanical port (build system, widget API changes). Patch 2 fixes a runtime issue found after the port that prevented the browser from loading.
---
Matt Turner (2):
tools: port perf ui from GTK 2 to GTK 4
perf tools: make the GTK4 report browser actually loadable at runtime
tools/build/Makefile.feature | 5 ++-
tools/build/feature/Makefile | 12 ++---
.../{test-gtk2-infobar.c => test-gtk4-infobar.c} | 4 +-
tools/build/feature/{test-gtk2.c => test-gtk4.c} | 4 +-
tools/perf/Documentation/perf-report.txt | 2 +-
tools/perf/Makefile | 2 +-
tools/perf/Makefile.config | 27 ++++++-----
tools/perf/Makefile.perf | 6 +--
tools/perf/builtin-annotate.c | 8 ++--
tools/perf/builtin-report.c | 8 ++--
tools/perf/scripts/install-build-deps.sh | 4 +-
tools/perf/tests/make | 4 +-
tools/perf/ui/gtk/annotate.c | 28 +++++-------
tools/perf/ui/gtk/browser.c | 52 +++++++++++++++++-----
tools/perf/ui/gtk/gtk.h | 2 +
tools/perf/ui/gtk/hists.c | 32 +++++--------
tools/perf/ui/gtk/progress.c | 28 +++++++-----
tools/perf/ui/gtk/setup.c | 4 +-
tools/perf/ui/gtk/util.c | 22 ++++++++-
tools/perf/ui/setup.c | 2 +-
tools/perf/util/annotate.c | 11 +++++
tools/perf/util/annotate.h | 12 +----
22 files changed, 166 insertions(+), 113 deletions(-)---
base-commit: 9f0346dcbea363787186c94ef94dd01aaa215afa
change-id: 20260906-perf-gtk2-555ca04bb652
Best regards,
Signature · 1 line
Matt Turner <[email protected]>
PATCH 1/2 tools: port perf ui from GTK 2 to GTK 4
Port straight to GTK 4 rather than the intermediate GTK 3, since GTK 4 is where new development happens and GTK 3 is now old itself.
GTK 4 removes several APIs perf's GTK UI relied on, rather than just renaming them, so this is a bigger change than a GTK 2 to GTK 3 port would have been. GtkContainer and gtk_container_add() are gone, so each container gets its own setter (gtk_window_set_child(), gtk_box_append(), gtk_scrolled_window_set_child()). GdkScreen is gone; monitor geometry, used to size the main window relative to the display it's shown on, now comes from GdkMonitor via GdkDisplay's monitor list, so we use the first monitor in that list instead of the monitor the (not yet realized) window happens to be on. gtk_window_set_position() and programmatic gtk_window_resize() are gone, so we call gtk_window_set_default_size() before the window is shown and leave placement to the window manager. gtk_main(), gtk_main_quit(), gtk_main_iteration() and gtk_events_pending() are gone; the nested main loop is replaced with a GMainLoop, quitting from the window's "close-request" signal instead of "delete_event". gtk_dialog_run() is gone too, so the error dialog runs its own GMainLoop, quitting on the dialog's "response" signal. Finally, gtk_widget_show_all()/gtk_widget_set_no_show_all() are gone since GTK 4 widgets are visible by default, gtk_widget_destroy() is replaced by gtk_window_destroy(), GTK_STOCK_OK is replaced by a plain button label string, and gtk_init(), gtk_init_check() and gtk_scrolled_window_new() drop their now-unused arguments.
gtk_ui_progress__finish() is also made to skip gtk_window_destroy() when no progress dialog was ever created (p->total small enough that update() never ran): a plain GtkWidget destroy tolerated that case silently, but gtk_window_destroy() on a NULL/non-window asserts.
While at it, fix two spots the GTK 2 to GTK 3 port had missed (builtin-annotate.c and ui/gtk/setup.c were still using HAVE_GTK2_SUPPORT and gtk_main_quit()), which would otherwise leave the GTK 4 build inconsistent.
Claude-Session: https://claude.ai/code/session_019moPUkPJjtTUWR8CBm1QdW
---
tools/build/Makefile.feature | 5 ++-
tools/build/feature/Makefile | 12 ++---
.../{test-gtk2-infobar.c => test-gtk4-infobar.c} | 4 +-
tools/build/feature/{test-gtk2.c => test-gtk4.c} | 4 +-
tools/perf/Documentation/perf-report.txt | 2 +-
tools/perf/Makefile | 2 +-
tools/perf/Makefile.config | 22 ++++-----
tools/perf/Makefile.perf | 6 +--
tools/perf/builtin-annotate.c | 8 ++--
tools/perf/builtin-report.c | 8 ++--
tools/perf/scripts/install-build-deps.sh | 4 +-
tools/perf/tests/make | 4 +-
tools/perf/ui/gtk/annotate.c | 28 +++++-------
tools/perf/ui/gtk/browser.c | 52 +++++++++++++++++-----
tools/perf/ui/gtk/gtk.h | 2 +
tools/perf/ui/gtk/hists.c | 32 +++++--------
tools/perf/ui/gtk/progress.c | 28 +++++++-----
tools/perf/ui/gtk/setup.c | 4 +-
tools/perf/ui/gtk/util.c | 22 ++++++++-
tools/perf/ui/setup.c | 2 +-
20 files changed, 148 insertions(+), 103 deletions(-)tools/build/Makefile.feature +3 -2
@@ -113,8 +113,8 @@ FEATURE_TESTS_EXTRA := \ compile-x32 \ cplus-demangle \ cxa-demangle \ - gtk2 \ - gtk2-infobar \ + gtk4 \ + gtk4-infobar \ hello \ babeltrace2-ctf-writer \ libcapstone \
@@ -143,6 +143,7 @@ endif FEATURE_DISPLAY ?= \ libdw \ glibc \ + gtk4 \ libelf \ libnuma \ numa_num_possible_cpus \
tools/build/feature/Makefile +6 -6
@@ -11,8 +11,8 @@ FILES= \ test-eventfd.bin \ test-fortify-source.bin \ test-glibc.bin \ - test-gtk2.bin \ - test-gtk2-infobar.bin \ + test-gtk4.bin \ + test-gtk4-infobar.bin \ test-hello.bin \ test-libbfd.bin \ test-libbfd-threadsafe.bin \
@@ -240,11 +240,11 @@ $(OUTPUT)test-libcpupower.bin: $(OUTPUT)test-libtracefs.bin: $(BUILD) $(shell $(PKG_CONFIG) --cflags libtracefs 2>/dev/null) -ltracefs -$(OUTPUT)test-gtk2.bin: - $(BUILD) $(shell $(PKG_CONFIG) --libs --cflags gtk+-2.0 2>/dev/null) -Wno-deprecated-declarations +$(OUTPUT)test-gtk4.bin: + $(BUILD) $(shell $(PKG_CONFIG) --libs --cflags gtk4 2>/dev/null) -$(OUTPUT)test-gtk2-infobar.bin: - $(BUILD) $(shell $(PKG_CONFIG) --libs --cflags gtk+-2.0 2>/dev/null) +$(OUTPUT)test-gtk4-infobar.bin: + $(BUILD) $(shell $(PKG_CONFIG) --libs --cflags gtk4 2>/dev/null) grep-libs = $(filter -l%,$(1)) strip-libs = $(filter-out -l%,$(1))
tools/build/feature/test-gtk4-infobar.c renamed from tools/build/feature/test-gtk2-infobar.c +2 -2
@@ -3,9 +3,9 @@#include <gtk/gtk.h> #pragma GCC diagnostic error "-Wstrict-prototypes" -int main (int argc,char *argv[]) +int main (void ) { -gtk_init (&argc, &argv); +gtk_init ();gtk_info_bar_new ();return 0 ;
tools/build/feature/test-gtk4.c renamed from tools/build/feature/test-gtk2.c +2 -2
@@ -3,9 +3,9 @@#include <gtk/gtk.h> #pragma GCC diagnostic error "-Wstrict-prototypes" -int main (int argc,char *argv[]) +int main (void ) { -gtk_init (&argc, &argv); +gtk_init ();return 0 ; }
tools/perf/Documentation/perf-report.txt +1 -1
@@ -351,7 +351,7 @@ OPTIONS requires a tty, if one is not present, as when piping to other commands, the stdio interface is used. ---gtk:: Use the GTK2 interface. +--gtk:: Use the GTK4 interface. -k:: --vmlinux=<file>::
tools/perf/Makefile +1 -1
@@ -106,7 +106,7 @@ clean: # make -C tools/perf -f tests/make # build-test: - @$(MAKE) SHUF=1 -f tests/make REUSE_FEATURES_DUMP=1 MK=Makefile SET_PARALLEL=1 --no-print-directory tarpkg make_static make_with_gtk2 out + @$(MAKE) SHUF=1 -f tests/make REUSE_FEATURES_DUMP=1 MK=Makefile SET_PARALLEL=1 --no-print-directory tarpkg make_static make_with_gtk4 out build-test-tarball: @$(MAKE) -f tests/make REUSE_FEATURES_DUMP=1 MK=Makefile SET_PARALLEL=1 --no-print-directory out
tools/perf/Makefile.config +11 -11
@@ -774,20 +774,20 @@ ifndef NO_SLANG endif endif -ifdef GTK2 - FLAGS_GTK2=$(CFLAGS) $(LDFLAGS) $(EXTLIBS) $(shell $(PKG_CONFIG) --libs --cflags gtk+-2.0 2>/dev/null) - $(call feature_check,gtk2) - ifneq ($(feature-gtk2), 1) - $(warning GTK2 not found, disables GTK2 support. Please install gtk2-devel or libgtk2.0-dev) - NO_GTK2 := 1 +ifdef GTK4 + FLAGS_GTK4=$(CFLAGS) $(LDFLAGS) $(EXTLIBS) $(shell $(PKG_CONFIG) --libs --cflags gtk4 2>/dev/null) + $(call feature_check,gtk4) + ifneq ($(feature-gtk4), 1) + $(warning GTK4 not found, disables GTK4 support. Please install gtk4-devel or libgtk-4-dev) + NO_GTK4 := 1 else - $(call feature_check,gtk2-infobar) - ifeq ($(feature-gtk2-infobar), 1) + $(call feature_check,gtk4-infobar) + ifeq ($(feature-gtk4-infobar), 1) GTK_CFLAGS := -DHAVE_GTK_INFO_BAR_SUPPORT endif - CFLAGS += -DHAVE_GTK2_SUPPORT - GTK_CFLAGS += $(shell $(PKG_CONFIG) --cflags gtk+-2.0 2>/dev/null) - GTK_LIBS := $(shell $(PKG_CONFIG) --libs gtk+-2.0 2>/dev/null) + CFLAGS += -DHAVE_GTK4_SUPPORT + GTK_CFLAGS += $(shell $(PKG_CONFIG) --cflags gtk4 2>/dev/null) + GTK_LIBS := $(shell $(PKG_CONFIG) --libs gtk4 2>/dev/null) EXTLIBS += -ldl endif endif
tools/perf/Makefile.perf +3 -3
@@ -49,7 +49,7 @@ include ../scripts/utilities.mak # # Define NO_SLANG if you do not want TUI support. # -# Define GTK2 if you want GTK+ GUI support. +# Define GTK4 if you want GTK+ GUI support. # # Define NO_DEMANGLE if you do not want C++ symbol demangling. #
@@ -473,7 +473,7 @@ ifneq ($(OUTPUT),) CFLAGS += -I$(OUTPUT) endif -ifdef GTK2 +ifdef GTK4 ALL_PROGRAMS += $(OUTPUT)libperf-gtk.so GTK_IN := $(OUTPUT)gtk-in.o endif
@@ -811,7 +811,7 @@ check: prepare ### Installation rules -ifdef GTK2 +ifdef GTK4 install-gtk: $(OUTPUT)libperf-gtk.so $(call QUIET_INSTALL, 'GTK UI') \ $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(libdir_SQ)'; \
tools/perf/builtin-annotate.c +4 -4
@@ -52,7 +52,7 @@ struct perf_annotate {bool use_tui;#endif bool use_stdio, use_stdio2; -#ifdef HAVE_GTK2_SUPPORT +#ifdef HAVE_GTK4_SUPPORT bool use_gtk;#endif bool skip_missing;
@@ -712,7 +712,7 @@ int cmd_annotate(int argc, const char **argv)OPT_BOOLEAN ('q' ,"quiet" , &quiet,"do now show any warnings or messages" ),OPT_BOOLEAN ('D' ,"dump-raw-trace" , &dump_trace,"dump raw trace in ASCII" ), -#ifdef HAVE_GTK2_SUPPORT +#ifdef HAVE_GTK4_SUPPORTOPT_BOOLEAN (0 ,"gtk" , &annotate.use_gtk,"Use the GTK interface" ), #endif #ifdef HAVE_SLANG_SUPPORT
@@ -828,7 +828,7 @@ int cmd_annotate(int argc, const char **argv)if (annotate_check_args () <0 )return -EINVAL ; -#ifdef HAVE_GTK2_SUPPORT +#ifdef HAVE_GTK4_SUPPORT if (symbol_conf.show_nr_samples && annotate.use_gtk) {pr_err ("--show-nr-samples is not available in --gtk mode at this time\n" );return ret;
@@ -898,7 +898,7 @@ int cmd_annotate(int argc, const char **argv) else if (annotate.use_tui) use_browser =1 ;#endif -#ifdef HAVE_GTK2_SUPPORT +#ifdef HAVE_GTK4_SUPPORT else if (annotate.use_gtk) use_browser =2 ;#endif
tools/perf/builtin-report.c +4 -4
@@ -82,7 +82,7 @@ struct report {#ifdef HAVE_SLANG_SUPPORT bool use_tui;#endif -#ifdef HAVE_GTK2_SUPPORT +#ifdef HAVE_GTK4_SUPPORT bool use_gtk;#endif bool use_stdio;
@@ -1359,8 +1359,8 @@ int cmd_report(int argc, const char **argv)#ifdef HAVE_SLANG_SUPPORT OPT_BOOLEAN(0, "tui", &report.use_tui, "Use the TUI interface"),#endif -#ifdef HAVE_GTK2_SUPPORT -OPT_BOOLEAN (0 ,"gtk" , &report.use_gtk,"Use the GTK2 interface" ), +#ifdef HAVE_GTK4_SUPPORT +OPT_BOOLEAN (0 ,"gtk" , &report.use_gtk,"Use the GTK4 interface" ), #endifOPT_BOOLEAN (0 ,"stdio" , &report.use_stdio, "Use the stdio interface"),
@@ -1710,7 +1710,7 @@ int cmd_report(int argc, const char **argv) else if (report.use_tui) use_browser =1 ;#endif -#ifdef HAVE_GTK2_SUPPORT +#ifdef HAVE_GTK4_SUPPORT else if (report.use_gtk) use_browser =2 ;#endif
tools/perf/scripts/install-build-deps.sh +2 -2
@@ -199,8 +199,8 @@ fedora_pkg_for() { # opt-in features, which a default build does not enable: the libbfd # disassembler family (libbfd, libbfd-threadsafe, libbfd-liberty, # disassembler-*, cplus-demangle), only linked on BUILD_NONDISTRO - # builds and deprecated in favor of capstone, GTK2, LIBPERL and - # LIBUNWIND support (ifdef GTK2 / ifdef LIBPERL / LIBUNWIND=1), + # builds and deprecated in favor of capstone, GTK4, LIBPERL and + # LIBUNWIND support (ifdef GTK4 / ifdef LIBPERL / LIBUNWIND=1), # and CoreSight (ifdef CORESIGHT), are deliberately not mapped. # libaio is not mapped either: its # test uses the POSIX AIO API (aio.h, aio_*, -lrt), provided by
tools/perf/tests/make +2 -2
@@ -95,7 +95,7 @@ make_no_babeltrace2 := NO_BABELTRACE2=1 make_with_coresight := CORESIGHT=1 make_no_sdt := NO_SDT=1 make_no_libpfm4 := NO_LIBPFM4=1 -make_with_gtk2 := GTK2=1 +make_with_gtk4 := GTK4=1 make_refcnt_check := EXTRA_CFLAGS="-DREFCNT_CHECKING=1" make_tags := tags make_cscope := cscope
@@ -318,7 +318,7 @@ $(run): $(call test,$@) && \ rm -rf $@ $$TMP_DEST || (cat $@ ; false) -make_with_gtk2: +make_with_gtk4: $(call clean) @TMP_DEST=$$(mktemp -d); \ cmd="cd $(PERF) && $(MAKE_F) $($@) $(PARALLEL_OPT) $(O_OPT) DESTDIR=$$TMP_DEST"; \
tools/perf/ui/gtk/annotate.c +12 -16
@@ -161,7 +161,7 @@ static int perf_gtk__annotate_symbol(GtkWidget *window, struct map_symbol *ms,gtk_list_store_set (store, &iter,ANN_COL__LINE , s,-1 ); } -gtk_container_add (GTK_CONTAINER (window), view); +gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (window), view);list_for_each_entry_safe (pos, n, ¬es->src->source, al.node) {list_del_init (&pos->al.node);
@@ -211,34 +211,31 @@ static int symbol__gtk_annotate(struct map_symbol *ms, struct evsel *evsel,signal (SIGQUIT , perf_gtk__signal);signal (SIGTERM , perf_gtk__signal); - window =gtk_window_new (GTK_WINDOW_TOPLEVEL ); + window =gtk_window_new ();gtk_window_set_title (GTK_WINDOW (window),"perf annotate" ); -g_signal_connect (window,"delete_event" , gtk_main_quit,NULL ); - pgctx =perf_gtk__activate_context (window);if (!pgctx)return -1 ; - vbox =gtk_vbox_new (FALSE,0 ); + vbox =gtk_box_new (GTK_ORIENTATION_VERTICAL ,0 ); notebook =gtk_notebook_new (); pgctx->notebook = notebook; -gtk_box_pack_start (GTK_BOX (vbox), notebook, TRUE, TRUE,0 ); +gtk_widget_set_vexpand (notebook, TRUE); +gtk_box_append (GTK_BOX (vbox), notebook); infobar =perf_gtk__setup_info_bar (); -if (infobar) { -gtk_box_pack_start (GTK_BOX (vbox), infobar, - FALSE, FALSE,0 ); - } +if (infobar) +gtk_box_append (GTK_BOX (vbox), infobar); statbar =perf_gtk__setup_statusbar (); -gtk_box_pack_start (GTK_BOX (vbox), statbar, FALSE, FALSE,0 ); +gtk_box_append (GTK_BOX (vbox), statbar); -gtk_container_add (GTK_CONTAINER (window), vbox); +gtk_window_set_child (GTK_WINDOW (window), vbox); } - scrolled_window =gtk_scrolled_window_new (NULL ,NULL ); + scrolled_window =gtk_scrolled_window_new (); tab_label =gtk_label_new (sym->name); gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
@@ -267,12 +264,11 @@ void perf_gtk__show_annotations(void)return ; window = pgctx->main_window; -gtk_widget_show_all (window);perf_gtk__resize_window (window); -gtk_window_set_position (GTK_WINDOW (window),GTK_WIN_POS_CENTER ); +gtk_widget_set_visible (window, TRUE); -gtk_main (); +perf_gtk__run_main_loop (window);perf_gtk__deactivate_context (&pgctx); }
tools/perf/ui/gtk/browser.c +42 -10
78 lines not on this page · raw message
tools/perf/ui/gtk/gtk.h +2 -0
@@ -40,6 +40,8 @@ void perf_gtk__init_hpp(void);void perf_gtk__signal (int sig);void perf_gtk__resize_window (GtkWidget *window); +void perf_gtk__run_main_loop (GtkWidget *window); +void perf_gtk__quit_main_loop (void );const char *perf_gtk__get_percent_color (double percent);GtkWidget *perf_gtk__setup_statusbar (void );
tools/perf/ui/gtk/hists.c +12 -20
@@ -395,11 +395,9 @@ static void perf_gtk__show_hists(GtkWidget *window, struct hists *hists, } } -gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (view), TRUE); -g_signal_connect (view,"row-activated" ,G_CALLBACK (on_row_activated),NULL ); -gtk_container_add (GTK_CONTAINER (window), view); +gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (window), view); } static void perf_gtk__add_hierarchy_entries(struct hists *hists,
@@ -583,11 +581,9 @@ static void perf_gtk__show_hierarchy(GtkWidget *window, struct hists *hists,perf_gtk__add_hierarchy_entries (hists, &hists->entries, store,NULL , &hpp, min_pcnt); -gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (view), TRUE); -g_signal_connect (view,"row-activated" ,G_CALLBACK (on_row_activated),NULL ); -gtk_container_add (GTK_CONTAINER (window), view); +gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (window), view); } int evlist__gtk_browse_hists(struct evlist *evlist, const char *help,
@@ -606,30 +602,29 @@ int evlist__gtk_browse_hists(struct evlist *evlist, const char *help,signal (SIGQUIT , perf_gtk__signal);signal (SIGTERM , perf_gtk__signal); - window =gtk_window_new (GTK_WINDOW_TOPLEVEL ); + window =gtk_window_new ();gtk_window_set_title (GTK_WINDOW (window),"perf report" ); -g_signal_connect (window,"delete_event" , gtk_main_quit,NULL ); - pgctx =perf_gtk__activate_context (window);if (!pgctx)return -1 ; - vbox =gtk_vbox_new (FALSE,0 ); + vbox =gtk_box_new (GTK_ORIENTATION_VERTICAL ,0 ); notebook =gtk_notebook_new (); -gtk_box_pack_start (GTK_BOX (vbox), notebook, TRUE, TRUE,0 ); +gtk_widget_set_vexpand (notebook, TRUE); +gtk_box_append (GTK_BOX (vbox), notebook); info_bar =perf_gtk__setup_info_bar ();if (info_bar) -gtk_box_pack_start (GTK_BOX (vbox), info_bar, FALSE, FALSE,0 ); +gtk_box_append (GTK_BOX (vbox), info_bar); statbar =perf_gtk__setup_statusbar (); -gtk_box_pack_start (GTK_BOX (vbox), statbar, FALSE, FALSE,0 ); +gtk_box_append (GTK_BOX (vbox), statbar); -gtk_container_add (GTK_CONTAINER (window), vbox); +gtk_window_set_child (GTK_WINDOW (window), vbox);evlist__for_each_entry (evlist, pos) {struct hists *hists =evsel__hists (pos);
@@ -649,7 +644,7 @@ int evlist__gtk_browse_hists(struct evlist *evlist, const char *help, } } - scrolled_window =gtk_scrolled_window_new (NULL ,NULL ); + scrolled_window =gtk_scrolled_window_new (); gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC,
@@ -665,15 +660,12 @@ int evlist__gtk_browse_hists(struct evlist *evlist, const char *help,gtk_notebook_append_page (GTK_NOTEBOOK (notebook), scrolled_window, tab_label); } -gtk_widget_show_all (window); -perf_gtk__resize_window (window); - -gtk_window_set_position (GTK_WINDOW (window),GTK_WIN_POS_CENTER ); +gtk_widget_set_visible (window, TRUE);ui_helpline__push (help); -gtk_main (); +perf_gtk__run_main_loop (window);perf_gtk__deactivate_context (&pgctx);
tools/perf/ui/gtk/progress.c +16 -12
@@ -13,37 +13,41 @@ static void gtk_ui_progress__update(struct ui_progress *p)char buf[1024 ];if (dialog ==NULL ) { -GtkWidget *vbox =gtk_vbox_new (TRUE,5 ); +GtkWidget *vbox =gtk_box_new (GTK_ORIENTATION_VERTICAL ,5 );GtkWidget *label =gtk_label_new (p->title); - dialog =gtk_window_new (GTK_WINDOW_TOPLEVEL ); + dialog =gtk_window_new (); progress =gtk_progress_bar_new (); -gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, FALSE,3 ); -gtk_box_pack_start (GTK_BOX (vbox), progress, TRUE, TRUE,3 ); +gtk_widget_set_vexpand (label, TRUE); +gtk_box_append (GTK_BOX (vbox), label); +gtk_widget_set_vexpand (progress, TRUE); +gtk_box_append (GTK_BOX (vbox), progress); -gtk_container_add (GTK_CONTAINER (dialog), vbox); +gtk_window_set_child (GTK_WINDOW (dialog), vbox);gtk_window_set_title (GTK_WINDOW (dialog),"perf" ); -gtk_window_resize (GTK_WINDOW (dialog),300 ,80 ); -gtk_window_set_position (GTK_WINDOW (dialog),GTK_WIN_POS_CENTER ); +gtk_window_set_default_size (GTK_WINDOW (dialog),300 ,80 ); -gtk_widget_show_all (dialog); +gtk_widget_set_visible (dialog, TRUE); }gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (progress), fraction);snprintf (buf,sizeof (buf),"%" PRIu64" / %" PRIu64, p->curr, p->total);gtk_progress_bar_set_text (GTK_PROGRESS_BAR (progress), buf); -/* we didn't call gtk_main yet, so do it manually */ -while (gtk_events_pending ()) -gtk_main_iteration (); +/* we didn't start a main loop yet, so pump events manually */ +while (g_main_context_pending (NULL )) +g_main_context_iteration (NULL , FALSE); }static void gtk_ui_progress__finish (void ) { +if (dialog ==NULL ) +return ; +/* this will also destroy all of its children */ -gtk_widget_destroy (dialog); +gtk_window_destroy (GTK_WINDOW (dialog)); dialog =NULL ; }
tools/perf/ui/gtk/setup.c +2 -2
@@ -12,7 +12,7 @@ int perf_gtk__init(void)gtk_ui_progress__init ();perf_gtk__init_hpp (); -return gtk_init_check (NULL ,NULL ) ?0 :-1 ; +return gtk_init_check () ?0 :-1 ; }void perf_gtk__exit (bool wait_for_ok__maybe_unused )
@@ -20,5 +20,5 @@ void perf_gtk__exit(bool wait_for_ok __maybe_unused)if (!perf_gtk__is_active_context (pgctx))return ;perf_error__unregister (&perf_gtk_eops); -gtk_main_quit (); +perf_gtk__quit_main_loop (); }
tools/perf/ui/gtk/util.c +20 -2
@@ -4,6 +4,7 @@#include <stdlib.h> #include <string.h> +#include <linux/compiler.h> #include <linux/zalloc.h> struct perf_gtk_context *pgctx;
@@ -28,10 +29,21 @@ int perf_gtk__deactivate_context(struct perf_gtk_context **ctx)return 0 ; } +static void perf_gtk__dialog_response (GtkDialog *dialog, +gint response_id__maybe_unused , +gpointer data) +{ +GMainLoop *loop = data; + +gtk_window_destroy (GTK_WINDOW (dialog)); +g_main_loop_quit (loop); +} +static int perf_gtk__error (const char *format,va_list args) {char *msg;GtkWidget *dialog; +GMainLoop *loop;if (!perf_gtk__is_active_context (pgctx) ||vasprintf (&msg, format, args) <0 ) {
@@ -46,9 +58,15 @@ static int perf_gtk__error(const char *format, va_list args)GTK_MESSAGE_ERROR ,GTK_BUTTONS_CLOSE , "<b>Error</b>\n\n%s", msg); -gtk_dialog_run (GTK_DIALOG (dialog)); -gtk_widget_destroy (dialog); + loop =g_main_loop_new (NULL , FALSE); +g_signal_connect (dialog,"response" , +G_CALLBACK (perf_gtk__dialog_response), loop); + +gtk_widget_set_visible (dialog, TRUE); +g_main_loop_run (loop); +g_main_loop_unref (loop); +free (msg);return 0 ; }
tools/perf/ui/setup.c +1 -1
@@ -14,7 +14,7 @@ int use_browser = -1;#define PERF_GTK_DSO "libperf-gtk.so" -#ifdef HAVE_GTK2_SUPPORT +#ifdef HAVE_GTK4_SUPPORT static int setup_gtk_browser (void ) {
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH 1/2] tools: port perf ui from GTK 2 to GTK 4
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
New issues:
- [High] The custom nested GMainLoop in perf_gtk__error() will hang indefinitely if the parent window is closed or a process signal is received while the error dialog is open.
- [Low] The code uses GTK APIs that have been removed in GTK4, causing build failures when GTK info bar support is enabled.
Pre-existing issues:
- [High] User-triggered Use-After-Free in the progress dialog if closed manually.
- [High] Reusing an exhausted va_list after vasprintf fails leads to Undefined Behavior and arbitrary memory reads. --
commit b3b8000cd456db5d6741b6a000f3706d6fd6d929
Author: Matt Turner <[email protected]>
tools: port perf ui from GTK 2 to GTK 4
Port straight to GTK 4 rather than the intermediate GTK 3, since GTK 4 is where new development happens and GTK 3 is now old itself.
GTK 4 removes several APIs perf's GTK UI relied on, rather than just renaming them, so this is a bigger change than a GTK 2 to GTK 3 port would have been.
[ ... ]
Quoted 4 lines
tools/perf/ui/gtk/browser.c +0
-0
[ ... ]
tools/perf/ui/gtk/browser.c GtkWidget *perf_gtk__setup_info_bar(void) · line 90
Quoted 11 lines
tools/perf/ui/gtk/browser.c +1
-1@@ -50,15 +82,15 @@ GtkWidget *perf_gtk__setup_info_bar(void)GtkWidget *content_area; info_bar =gtk_info_bar_new (); -gtk_widget_set_no_show_all (info_bar, TRUE); +gtk_widget_set_visible (info_bar, FALSE); label =gtk_label_new ("" );gtk_widget_show (label); content_area =gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
[Severity: Low]
Does compiling with GTK4 enabled fail here? While test-gtk4-infobar.c still finds gtk_info_bar_new(), APIs like gtk_widget_show(), gtk_info_bar_get_content_area(), and gtk_widget_hide() were removed in GTK4 and replaced, which might cause build failures.
Quoted 35 lines
tools/perf/ui/gtk/browser.c +2
-2-gtk_container_add (GTK_CONTAINER (content_area), label); +gtk_box_append (GTK_BOX (content_area), label); -gtk_info_bar_add_button (GTK_INFO_BAR (info_bar),GTK_STOCK_OK , +gtk_info_bar_add_button (GTK_INFO_BAR (info_bar),"_OK" ,GTK_RESPONSE_OK );g_signal_connect (info_bar,"response" ,G_CALLBACK (gtk_widget_hide),NULL );tools/perf/ui/gtk/progress.c +7
-5@@ -13,37 +13,41 @@ static void gtk_ui_progress__update(struct ui_progress *p)char buf[1024 ];if (dialog ==NULL ) { -GtkWidget *vbox =gtk_vbox_new (TRUE,5 ); +GtkWidget *vbox =gtk_box_new (GTK_ORIENTATION_VERTICAL ,5 );GtkWidget *label =gtk_label_new (p->title); - dialog =gtk_window_new (GTK_WINDOW_TOPLEVEL ); + dialog =gtk_window_new (); progress =gtk_progress_bar_new (); -gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, FALSE,3 ); -gtk_box_pack_start (GTK_BOX (vbox), progress, TRUE, TRUE,3 ); +gtk_widget_set_vexpand (label, TRUE); +gtk_box_append (GTK_BOX (vbox), label); +gtk_widget_set_vexpand (progress, TRUE); +gtk_box_append (GTK_BOX (vbox), progress); -gtk_container_add (GTK_CONTAINER (dialog), vbox); +gtk_window_set_child (GTK_WINDOW (dialog), vbox);
tools/perf/ui/gtk/progress.c static void gtk_ui_progress__update(struct ui_progress *p) · line 35
Quoted 10 lines
tools/perf/ui/gtk/progress.c +2
-3gtk_window_set_title (GTK_WINDOW (dialog),"perf" ); -gtk_window_resize (GTK_WINDOW (dialog),300 ,80 ); -gtk_window_set_position (GTK_WINDOW (dialog),GTK_WIN_POS_CENTER ); +gtk_window_set_default_size (GTK_WINDOW (dialog),300 ,80 ); -gtk_widget_show_all (dialog); +gtk_widget_set_visible (dialog, TRUE); }gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (progress), fraction);
[Severity: High]
This isn't a bug introduced by this patch, but can this cause a use-after-free if the user manually closes the progress dialog?
If the window manager destroys the dialog, the dialog and progress widget pointers aren't set to NULL. The next call to gtk_progress_bar_set_fraction() might dereference a freed widget pointer.
Quoted 4 lines
tools/perf/ui/gtk/util.c +0
-0
[ ... ]
tools/perf/ui/gtk/util.c static int perf_gtk__error(const char *format, va_list args)
Quoted 1 line
tools/perf/ui/gtk/util.c +0
-0@@ -46,9 +58,15 @@ static int perf_gtk__error(const char *format, va_list args)
[Severity: High]
This is a pre-existing issue, but could this lead to undefined behavior and out-of-bounds stack reads?
In the early fallback paths of perf_gtk__error() and similar functions, if vasprintf() fails it returns a negative value after potentially exhausting args. The fallback error path immediately calls vfprintf(stderr, format, args) without resetting the va_list via va_copy(), which could cause vfprintf() to read past the end of the provided arguments.
tools/perf/ui/gtk/util.c static int perf_gtk__error(const char *format, va_list args) · line 67
Quoted 12 lines
tools/perf/ui/gtk/util.c +6
-2GTK_MESSAGE_ERROR ,GTK_BUTTONS_CLOSE , "<b>Error</b>\n\n%s", msg); -gtk_dialog_run (GTK_DIALOG (dialog)); -gtk_widget_destroy (dialog); + loop =g_main_loop_new (NULL , FALSE); +g_signal_connect (dialog,"response" , +G_CALLBACK (perf_gtk__dialog_response), loop); + +gtk_widget_set_visible (dialog, TRUE); +g_main_loop_run (loop);
[Severity: High]
If the parent window is closed or a signal is received while the error dialog is open, will this nested main loop hang indefinitely?
Since the dialog is created with GTK_DIALOG_DESTROY_WITH_PARENT, destroying the parent will programmatically destroy the dialog without emitting the 'response' signal, leaving the loop blocked forever.
Quoted 5 lines
tools/perf/ui/gtk/util.c +2
-0+g_main_loop_unref (loop); +free (msg);return 0 ; }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH 2/2 perf tools: make the GTK4 report browser actually loadable at runtime
In reply to Matt Turnerperf report --gtk dlopen()s libperf-gtk.so and expects it to resolve symbols back against the running perf binary (callchain_param, symbol_conf, evsel__name, and friends all live in perf itself, not in the plugin). Two things stood in the way of that after the GTK 2 to GTK 4 port:
perf never passed -rdynamic, so none of its own symbols were in its dynamic symbol table for a dlopen()ed plugin to find at all. Add -rdynamic to LDFLAGS when GTK4 support is enabled.
annotated_source__hist_entry() was a static inline in annotate.h, so ui/gtk/annotate.c calling it pulled hashmap__find()'s expansion, hashmap_find(), directly into libperf-gtk.so as an undefined symbol. The only hashmap_find perf links against in the common case is libbpf's internal one (tools/lib/bpf/hashmap.c), which libbpf compiles with -fvisibility=hidden; a hidden symbol can never be exported to a dlopen()ed plugin no matter what LDFLAGS perf itself gets. Move annotated_source__hist_entry() out of the header and into annotate.c, as an ordinary exported function, so the plugin depends on it the same way it already depends on evsel__group_desc() and friends, instead of reaching for hashmap_find directly.
With both fixes, a default 'make GTK4=1' build (libbpf statically linked, as usual) can dlopen() libperf-gtk.so and open the report browser without needing NO_LIBBPF=1 or manual LDFLAGS. Verified with perf report --gtk against a real perf.data on an actual GTK4 desktop session.
Claude-Session: https://claude.ai/code/session_019moPUkPJjtTUWR8CBm1QdW
--- tools/perf/Makefile.config | 5 +++++ tools/perf/util/annotate.c | 11 +++++++++++ tools/perf/util/annotate.h | 12 ++---------- 3 files changed, 18 insertions(+), 10 deletions(-)
tools/perf/Makefile.config +5 -0
@@ -789,6 +789,11 @@ ifdef GTK4 GTK_CFLAGS += $(shell $(PKG_CONFIG) --cflags gtk4 2>/dev/null) GTK_LIBS := $(shell $(PKG_CONFIG) --libs gtk4 2>/dev/null) EXTLIBS += -ldl + # libperf-gtk.so is dlopen()ed at runtime and calls back into + # symbols defined in the perf binary itself (callchain_param, + # symbol_conf, evsel__name, ...): perf needs to export those + # dynamically for the plugin to resolve them. + LDFLAGS += -rdynamic endif endif
tools/perf/util/annotate.c +11 -0
@@ -145,6 +145,17 @@ static int annotated_source__alloc_histograms(struct annotated_source *src,return src->histograms ?0 :-1 ; } +struct sym_hist_entry * +annotated_source__hist_entry (struct annotated_source *src,const struct evsel *evsel,u64 offset) +{ +struct sym_hist_entry *entry; +long key = offset <<16 | evsel->core.idx; + +if (!hashmap__find (src->samples, key, &entry)) +return NULL ; +return entry; +} +void symbol__annotate_zero_histograms (struct symbol *sym) {struct annotation *notes =symbol__annotation (sym);
tools/perf/util/annotate.h +2 -10
@@ -406,16 +406,8 @@ static inline struct sym_hist *annotation__histogram(struct annotation *notes,return annotated_source__histogram (notes->src, evsel); } -static inline struct sym_hist_entry * -annotated_source__hist_entry (struct annotated_source *src,const struct evsel *evsel,u64 offset) -{ -struct sym_hist_entry *entry; -long key = offset <<16 | evsel->core.idx; - -if (!hashmap__find (src->samples, key, &entry)) -return NULL ; -return entry; -} +struct sym_hist_entry * +annotated_source__hist_entry (struct annotated_source *src,const struct evsel *evsel,u64 offset);static inline struct annotation *symbol__annotation (struct symbol *sym) {
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH 2/2] perf tools: make the GTK4 report browser actually loadable at runtime
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 22 lines
perf report --gtk dlopen()s libperf-gtk.so and expects it to resolve symbols back against the running perf binary (
callchain_param,symbol_conf,evsel__name, and friends all live in perf itself, not in the plugin). Two things stood in the way of that after the GTK 2 to GTK 4 port:perf never passed -rdynamic, so none of its own symbols were in its dynamic symbol table for a dlopen()ed plugin to find at all. Add -rdynamic to LDFLAGS when GTK4 support is enabled.
annotated_source__hist_entry()was a static inline inannotate.h, soui/gtk/annotate.ccalling it pulled hashmap__find()'s expansion,hashmap_find(), directly into libperf-gtk.so as an undefined symbol. The onlyhashmap_findperf links against in the common case is libbpf's internal one (tools/lib/bpf/hashmap.c), which libbpf compiles with -fvisibility=hidden; a hidden symbol can never be exported to a dlopen()ed plugin no matter what LDFLAGS perf itself gets. Moveannotated_source__hist_entry()out of the header and intoannotate.c, as an ordinary exported function, so the plugin depends on it the same way it already depends onevsel__group_desc()and friends, instead of reaching forhashmap_finddirectly.
Quoted 7 lines
With both fixes, a default 'make GTK4=1' build (libbpf statically linked, as usual) can
dlopen()libperf-gtk.so and open the report browser without needing NO_LIBBPF=1 or manual LDFLAGS. Verified with perf report --gtk against a real perf.data on an actual GTK4 desktop session.Signed-off-by Matt Turner <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2