xref: /openbmc/linux/tools/perf/ui/setup.c (revision cd8ef949)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <dlfcn.h>
3 #include <signal.h>
4 #include <unistd.h>
5 
6 #include <subcmd/pager.h>
7 #include "../util/debug.h"
8 #include "../util/hist.h"
9 #include "ui.h"
10 
11 struct mutex ui__lock;
12 void *perf_gtk_handle;
13 int use_browser = -1;
14 
15 #define PERF_GTK_DSO "libperf-gtk.so"
16 
17 #ifdef HAVE_GTK2_SUPPORT
18 
setup_gtk_browser(void)19 static int setup_gtk_browser(void)
20 {
21 	int (*perf_ui_init)(void);
22 
23 	if (perf_gtk_handle)
24 		return 0;
25 
26 	perf_gtk_handle = dlopen(PERF_GTK_DSO, RTLD_LAZY);
27 	if (perf_gtk_handle == NULL) {
28 		char buf[PATH_MAX];
29 		scnprintf(buf, sizeof(buf), "%s/%s", LIBDIR, PERF_GTK_DSO);
30 		perf_gtk_handle = dlopen(buf, RTLD_LAZY);
31 	}
32 	if (perf_gtk_handle == NULL)
33 		return -1;
34 
35 	perf_ui_init = dlsym(perf_gtk_handle, "perf_gtk__init");
36 	if (perf_ui_init == NULL)
37 		goto out_close;
38 
39 	if (perf_ui_init() == 0)
40 		return 0;
41 
42 out_close:
43 	dlclose(perf_gtk_handle);
44 	return -1;
45 }
46 
exit_gtk_browser(bool wait_for_ok)47 static void exit_gtk_browser(bool wait_for_ok)
48 {
49 	void (*perf_ui_exit)(bool);
50 
51 	if (perf_gtk_handle == NULL)
52 		return;
53 
54 	perf_ui_exit = dlsym(perf_gtk_handle, "perf_gtk__exit");
55 	if (perf_ui_exit == NULL)
56 		goto out_close;
57 
58 	perf_ui_exit(wait_for_ok);
59 
60 out_close:
61 	dlclose(perf_gtk_handle);
62 
63 	perf_gtk_handle = NULL;
64 }
65 #else
setup_gtk_browser(void)66 static inline int setup_gtk_browser(void) { return -1; }
exit_gtk_browser(bool wait_for_ok __maybe_unused)67 static inline void exit_gtk_browser(bool wait_for_ok __maybe_unused) {}
68 #endif
69 
stdio__config_color(const struct option * opt __maybe_unused,const char * mode,int unset __maybe_unused)70 int stdio__config_color(const struct option *opt __maybe_unused,
71 			const char *mode, int unset __maybe_unused)
72 {
73 	perf_use_color_default = perf_config_colorbool("color.ui", mode, -1);
74 	return 0;
75 }
76 
setup_browser(bool fallback_to_pager)77 void setup_browser(bool fallback_to_pager)
78 {
79 	mutex_init(&ui__lock);
80 	if (use_browser < 2 && (!isatty(1) || dump_trace))
81 		use_browser = 0;
82 
83 	/* default to TUI */
84 	if (use_browser < 0)
85 		use_browser = 1;
86 
87 	switch (use_browser) {
88 	case 2:
89 		if (setup_gtk_browser() == 0)
90 			break;
91 		printf("GTK browser requested but could not find %s\n",
92 		       PERF_GTK_DSO);
93 		sleep(1);
94 		use_browser = 1;
95 		/* fall through */
96 	case 1:
97 		if (ui__init() == 0)
98 			break;
99 		/* fall through */
100 	default:
101 		use_browser = 0;
102 		if (fallback_to_pager)
103 			setup_pager();
104 		break;
105 	}
106 }
107 
exit_browser(bool wait_for_ok)108 void exit_browser(bool wait_for_ok)
109 {
110 	switch (use_browser) {
111 	case 2:
112 		exit_gtk_browser(wait_for_ok);
113 		break;
114 
115 	case 1:
116 		ui__exit(wait_for_ok);
117 		break;
118 
119 	default:
120 		break;
121 	}
122 	mutex_destroy(&ui__lock);
123 }
124 
pthread__block_sigwinch(void)125 void pthread__block_sigwinch(void)
126 {
127 	sigset_t set;
128 
129 	sigemptyset(&set);
130 	sigaddset(&set, SIGWINCH);
131 	pthread_sigmask(SIG_BLOCK, &set, NULL);
132 }
133 
pthread__unblock_sigwinch(void)134 void pthread__unblock_sigwinch(void)
135 {
136 	sigset_t set;
137 
138 	sigemptyset(&set);
139 	sigaddset(&set, SIGWINCH);
140 	pthread_sigmask(SIG_UNBLOCK, &set, NULL);
141 }
142