1 // SPDX-License-Identifier: GPL-2.0 2 #include <errno.h> 3 #include <signal.h> 4 #include <stdbool.h> 5 #include <stdlib.h> 6 #include <linux/kernel.h> 7 #ifdef HAVE_BACKTRACE_SUPPORT 8 #include <execinfo.h> 9 #endif 10 11 #include "../../util/cache.h" 12 #include "../../util/debug.h" 13 #include "../../util/util.h" 14 #include "../browser.h" 15 #include "../helpline.h" 16 #include "../ui.h" 17 #include "../util.h" 18 #include "../libslang.h" 19 #include "../keysyms.h" 20 #include "tui.h" 21 22 static volatile int ui__need_resize; 23 24 extern struct perf_error_ops perf_tui_eops; 25 extern bool tui_helpline__set; 26 27 extern void hist_browser__init_hpp(void); 28 29 void ui__refresh_dimensions(bool force) 30 { 31 if (force || ui__need_resize) { 32 ui__need_resize = 0; 33 pthread_mutex_lock(&ui__lock); 34 SLtt_get_screen_size(); 35 SLsmg_reinit_smg(); 36 pthread_mutex_unlock(&ui__lock); 37 } 38 } 39 40 static void ui__sigwinch(int sig __maybe_unused) 41 { 42 ui__need_resize = 1; 43 } 44 45 static void ui__setup_sigwinch(void) 46 { 47 static bool done; 48 49 if (done) 50 return; 51 52 done = true; 53 pthread__unblock_sigwinch(); 54 signal(SIGWINCH, ui__sigwinch); 55 } 56 57 int ui__getch(int delay_secs) 58 { 59 struct timeval timeout, *ptimeout = delay_secs ? &timeout : NULL; 60 fd_set read_set; 61 int err, key; 62 63 ui__setup_sigwinch(); 64 65 FD_ZERO(&read_set); 66 FD_SET(0, &read_set); 67 68 if (delay_secs) { 69 timeout.tv_sec = delay_secs; 70 timeout.tv_usec = 0; 71 } 72 73 err = select(1, &read_set, NULL, NULL, ptimeout); 74 75 if (err == 0) 76 return K_TIMER; 77 78 if (err == -1) { 79 if (errno == EINTR) 80 return K_RESIZE; 81 return K_ERROR; 82 } 83 84 key = SLang_getkey(); 85 if (key != K_ESC) 86 return key; 87 88 FD_ZERO(&read_set); 89 FD_SET(0, &read_set); 90 timeout.tv_sec = 0; 91 timeout.tv_usec = 20; 92 err = select(1, &read_set, NULL, NULL, &timeout); 93 if (err == 0) 94 return K_ESC; 95 96 SLang_ungetkey(key); 97 return SLkp_getkey(); 98 } 99 100 #ifdef HAVE_BACKTRACE_SUPPORT 101 static void ui__signal_backtrace(int sig) 102 { 103 void *stackdump[32]; 104 size_t size; 105 106 ui__exit(false); 107 psignal(sig, "perf"); 108 109 printf("-------- backtrace --------\n"); 110 size = backtrace(stackdump, ARRAY_SIZE(stackdump)); 111 backtrace_symbols_fd(stackdump, size, STDOUT_FILENO); 112 113 exit(0); 114 } 115 #else 116 # define ui__signal_backtrace ui__signal 117 #endif 118 119 static void ui__signal(int sig) 120 { 121 ui__exit(false); 122 psignal(sig, "perf"); 123 exit(0); 124 } 125 126 int ui__init(void) 127 { 128 int err; 129 130 SLutf8_enable(-1); 131 SLtt_get_terminfo(); 132 SLtt_get_screen_size(); 133 134 err = SLsmg_init_smg(); 135 if (err < 0) 136 goto out; 137 err = SLang_init_tty(-1, 0, 0); 138 if (err < 0) 139 goto out; 140 141 err = SLkp_init(); 142 if (err < 0) { 143 pr_err("TUI initialization failed.\n"); 144 goto out; 145 } 146 147 SLkp_define_keysym((char *)"^(kB)", SL_KEY_UNTAB); 148 149 signal(SIGSEGV, ui__signal_backtrace); 150 signal(SIGFPE, ui__signal_backtrace); 151 signal(SIGINT, ui__signal); 152 signal(SIGQUIT, ui__signal); 153 signal(SIGTERM, ui__signal); 154 155 perf_error__register(&perf_tui_eops); 156 157 ui_helpline__init(); 158 ui_browser__init(); 159 tui_progress__init(); 160 161 hist_browser__init_hpp(); 162 out: 163 return err; 164 } 165 166 void ui__exit(bool wait_for_ok) 167 { 168 if (wait_for_ok && tui_helpline__set) 169 ui__question_window("Fatal Error", 170 ui_helpline__last_msg, 171 "Press any key...", 0); 172 173 SLtt_set_cursor_visibility(1); 174 SLsmg_refresh(); 175 SLsmg_reset_smg(); 176 SLang_reset_tty(); 177 178 perf_error__unregister(&perf_tui_eops); 179 } 180