Add translations for various sub-directories
[binutils-gdb.git] / gdb / event-top.c
blob1fe37841935655808efe72afdd01a8565eda20cd
1 /* Top level stuff for GDB, the GNU debugger.
3 Copyright (C) 1999-2024 Free Software Foundation, Inc.
5 Written by Elena Zannoni <ezannoni@cygnus.com> of Cygnus Solutions.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "exceptions.h"
23 #include "gdbsupport/job-control.h"
24 #include "run-on-main-thread.h"
25 #include "top.h"
26 #include "ui.h"
27 #include "inferior.h"
28 #include "infrun.h"
29 #include "target.h"
30 #include "terminal.h"
31 #include "gdbsupport/event-loop.h"
32 #include "event-top.h"
33 #include "interps.h"
34 #include <signal.h>
35 #include "cli/cli-script.h"
36 #include "main.h"
37 #include "gdbthread.h"
38 #include "observable.h"
39 #include "cli/cli-cmds.h"
40 #include "annotate.h"
41 #include "maint.h"
42 #include "ser-event.h"
43 #include "gdbsupport/gdb_select.h"
44 #include "gdbsupport/gdb-sigmask.h"
45 #include "async-event.h"
46 #include "bt-utils.h"
47 #include "pager.h"
49 /* readline include files. */
50 #include "readline/readline.h"
51 #include "readline/history.h"
53 #ifdef TUI
54 #include "tui/tui.h"
55 #endif
57 /* readline defines this. */
58 #undef savestring
60 static std::string top_level_prompt ();
62 /* Signal handlers. */
63 #ifdef SIGQUIT
64 static void handle_sigquit (int sig);
65 #endif
66 #ifdef SIGHUP
67 static void handle_sighup (int sig);
68 #endif
70 /* Functions to be invoked by the event loop in response to
71 signals. */
72 #if defined (SIGQUIT) || defined (SIGHUP)
73 static void async_do_nothing (gdb_client_data);
74 #endif
75 #ifdef SIGHUP
76 static void async_disconnect (gdb_client_data);
77 #endif
78 #ifdef SIGTSTP
79 static void async_sigtstp_handler (gdb_client_data);
80 #endif
81 static void async_sigterm_handler (gdb_client_data arg);
83 /* Instead of invoking (and waiting for) readline to read the command
84 line and pass it back for processing, we use readline's alternate
85 interface, via callback functions, so that the event loop can react
86 to other event sources while we wait for input. */
88 /* Important variables for the event loop. */
90 /* This is used to determine if GDB is using the readline library or
91 its own simplified form of readline. It is used by the asynchronous
92 form of the set editing command.
93 ezannoni: as of 1999-04-29 I expect that this
94 variable will not be used after gdb is changed to use the event
95 loop as default engine, and event-top.c is merged into top.c. */
96 bool set_editing_cmd_var;
98 /* This is used to display the notification of the completion of an
99 asynchronous execution command. */
100 bool exec_done_display_p = false;
102 /* Used by the stdin event handler to compensate for missed stdin events.
103 Setting this to a non-zero value inside an stdin callback makes the callback
104 run again. */
105 int call_stdin_event_handler_again_p;
107 /* When true GDB will produce a minimal backtrace when a fatal signal is
108 reached (within GDB code). */
109 static bool bt_on_fatal_signal = GDB_PRINT_INTERNAL_BACKTRACE_INIT_ON;
111 /* Implement 'maintenance show backtrace-on-fatal-signal'. */
113 static void
114 show_bt_on_fatal_signal (struct ui_file *file, int from_tty,
115 struct cmd_list_element *cmd, const char *value)
117 gdb_printf (file, _("Backtrace on a fatal signal is %s.\n"), value);
120 /* Signal handling variables. */
121 /* Each of these is a pointer to a function that the event loop will
122 invoke if the corresponding signal has received. The real signal
123 handlers mark these functions as ready to be executed and the event
124 loop, in a later iteration, calls them. See the function
125 invoke_async_signal_handler. */
126 static struct async_signal_handler *sigint_token;
127 #ifdef SIGHUP
128 static struct async_signal_handler *sighup_token;
129 #endif
130 #ifdef SIGQUIT
131 static struct async_signal_handler *sigquit_token;
132 #endif
133 #ifdef SIGTSTP
134 static struct async_signal_handler *sigtstp_token;
135 #endif
136 static struct async_signal_handler *async_sigterm_token;
138 /* This hook is called by gdb_rl_callback_read_char_wrapper after each
139 character is processed. */
140 void (*after_char_processing_hook) (void);
142 #if RL_VERSION_MAJOR == 7
143 extern "C" void _rl_signal_handler (int);
144 #endif
146 /* Wrapper function for calling into the readline library. This takes
147 care of a couple things:
149 - The event loop expects the callback function to have a parameter,
150 while readline expects none.
152 - Propagation of GDB exceptions/errors thrown from INPUT_HANDLER
153 across readline requires special handling.
155 On the exceptions issue:
157 DWARF-based unwinding cannot cross code built without -fexceptions.
158 Any exception that tries to propagate through such code will fail
159 and the result is a call to std::terminate. While some ABIs, such
160 as x86-64, require all code to be built with exception tables,
161 others don't.
163 This is a problem when GDB calls some non-EH-aware C library code,
164 that calls into GDB again through a callback, and that GDB callback
165 code throws a C++ exception. Turns out this is exactly what
166 happens with GDB's readline callback.
168 In such cases, we must catch and save any C++ exception that might
169 be thrown from the GDB callback before returning to the
170 non-EH-aware code. When the non-EH-aware function itself returns
171 back to GDB, we then rethrow the original C++ exception.
173 In the readline case however, the right thing to do is to longjmp
174 out of the callback, rather than do a normal return -- there's no
175 way for the callback to return to readline an indication that an
176 error happened, so a normal return would have rl_callback_read_char
177 potentially continue processing further input, redisplay the
178 prompt, etc. Instead of raw setjmp/longjmp however, we use our
179 sjlj-based TRY/CATCH mechanism, which knows to handle multiple
180 levels of active setjmp/longjmp frames, needed in order to handle
181 the readline callback recursing, as happens with e.g., secondary
182 prompts / queries, through gdb_readline_wrapper. This must be
183 noexcept in order to avoid problems with mixing sjlj and
184 (sjlj-based) C++ exceptions. */
186 static struct gdb_exception
187 gdb_rl_callback_read_char_wrapper_sjlj ()
189 struct gdb_exception gdb_expt;
191 /* C++ exceptions can't normally be thrown across readline (unless
192 it is built with -fexceptions, but it won't by default on many
193 ABIs). So we instead wrap the readline call with a sjlj-based
194 TRY/CATCH, and rethrow the GDB exception once back in GDB. */
195 TRY_SJLJ
197 rl_callback_read_char ();
198 #if RL_VERSION_MAJOR >= 8
199 /* It can happen that readline (while in rl_callback_read_char)
200 received a signal, but didn't handle it yet. Make sure it's handled
201 now. If we don't do that we run into two related problems:
202 - we have to wait for another event triggering
203 rl_callback_read_char before the signal is handled
204 - there's no guarantee that the signal will be processed before the
205 event. */
206 while (rl_pending_signal () != 0)
207 /* Do this in a while loop, in case rl_check_signals also leaves a
208 pending signal. I'm not sure if that's possible, but it seems
209 better to handle the scenario than to assert. */
210 rl_check_signals ();
211 #elif RL_VERSION_MAJOR == 7
212 /* Unfortunately, rl_check_signals is not available. Use private
213 function _rl_signal_handler instead. */
215 while (rl_pending_signal () != 0)
216 _rl_signal_handler (rl_pending_signal ());
217 #else
218 #error "Readline major version >= 7 expected"
219 #endif
220 if (after_char_processing_hook)
221 (*after_char_processing_hook) ();
223 CATCH_SJLJ (ex, RETURN_MASK_ALL)
225 gdb_expt = std::move (ex);
227 END_CATCH_SJLJ
229 return gdb_expt;
232 /* Wrapper around gdb_rl_callback_read_char_wrapper_sjlj to ensure
233 noexcept. */
235 static struct gdb_exception
236 gdb_rl_callback_read_char_wrapper_noexcept () noexcept
240 return gdb_rl_callback_read_char_wrapper_sjlj ();
242 catch (gdb_exception &ex)
244 return std::move (ex);
248 static void
249 gdb_rl_callback_read_char_wrapper (gdb_client_data client_data)
251 struct gdb_exception gdb_expt
252 = gdb_rl_callback_read_char_wrapper_noexcept ();
254 /* Rethrow using the normal EH mechanism. */
255 if (gdb_expt.reason < 0)
256 throw_exception (std::move (gdb_expt));
259 /* GDB's readline callback handler. Calls the current INPUT_HANDLER,
260 and propagates GDB exceptions/errors thrown from INPUT_HANDLER back
261 across readline. See gdb_rl_callback_read_char_wrapper. This must
262 be noexcept in order to avoid problems with mixing sjlj and
263 (sjlj-based) C++ exceptions. */
265 static void
266 gdb_rl_callback_handler (char *rl) noexcept
268 /* This is static to avoid undefined behavior when calling longjmp
269 -- gdb_exception has a destructor with side effects. */
270 static struct gdb_exception gdb_rl_expt;
271 struct ui *ui = current_ui;
273 /* In bracketed paste mode, pasting a complete line can result in a
274 literal newline appearing at the end of LINE. However, we never
275 want this in gdb. */
276 if (rl != nullptr)
278 size_t len = strlen (rl);
279 while (len > 0 && (rl[len - 1] == '\r' || rl[len - 1] == '\n'))
280 --len;
281 rl[len] = '\0';
286 /* Ensure the exception is reset on each call. */
287 gdb_rl_expt = {};
288 ui->input_handler (gdb::unique_xmalloc_ptr<char> (rl));
290 catch (gdb_exception &ex)
292 gdb_rl_expt = std::move (ex);
295 /* If we caught a GDB exception, longjmp out of the readline
296 callback. There's no other way for the callback to signal to
297 readline that an error happened. A normal return would have
298 readline potentially continue processing further input, redisplay
299 the prompt, etc. (This is what GDB historically did when it was
300 a C program.) Note that since we're long jumping, local variable
301 dtors are NOT run automatically. */
302 if (gdb_rl_expt.reason < 0)
303 throw_exception_sjlj (gdb_rl_expt);
306 /* Change the function to be invoked every time there is a character
307 ready on stdin. This is used when the user sets the editing off,
308 therefore bypassing readline, and letting gdb handle the input
309 itself, via gdb_readline_no_editing_callback. Also it is used in
310 the opposite case in which the user sets editing on again, by
311 restoring readline handling of the input.
313 NOTE: this operates on input_fd, not instream. If we are reading
314 commands from a file, instream will point to the file. However, we
315 always read commands from a file with editing off. This means that
316 the 'set editing on/off' will have effect only on the interactive
317 session. */
319 void
320 change_line_handler (int editing)
322 struct ui *ui = current_ui;
324 /* We can only have one instance of readline, so we only allow
325 editing on the main UI. */
326 if (ui != main_ui)
327 return;
329 /* Don't try enabling editing if the interpreter doesn't support it
330 (e.g., MI). */
331 if (!top_level_interpreter ()->supports_command_editing ()
332 || !command_interp ()->supports_command_editing ())
333 return;
335 if (editing)
337 gdb_assert (ui == main_ui);
339 /* Turn on editing by using readline. */
340 ui->call_readline = gdb_rl_callback_read_char_wrapper;
342 else
344 /* Turn off editing by using gdb_readline_no_editing_callback. */
345 if (ui->command_editing)
346 gdb_rl_callback_handler_remove ();
347 ui->call_readline = gdb_readline_no_editing_callback;
349 ui->command_editing = editing;
352 /* The functions below are wrappers for rl_callback_handler_remove and
353 rl_callback_handler_install that keep track of whether the callback
354 handler is installed in readline. This is necessary because after
355 handling a target event of a background execution command, we may
356 need to reinstall the callback handler if it was removed due to a
357 secondary prompt. See gdb_readline_wrapper_line. We don't
358 unconditionally install the handler for every target event because
359 that also clears the line buffer, thus installing it while the user
360 is typing would lose input. */
362 /* Whether we've registered a callback handler with readline. */
363 static bool callback_handler_installed;
365 /* See event-top.h, and above. */
367 void
368 gdb_rl_callback_handler_remove (void)
370 gdb_assert (current_ui == main_ui);
372 rl_callback_handler_remove ();
373 callback_handler_installed = false;
376 /* See event-top.h, and above. Note this wrapper doesn't have an
377 actual callback parameter because we always install
378 INPUT_HANDLER. */
380 void
381 gdb_rl_callback_handler_install (const char *prompt)
383 gdb_assert (current_ui == main_ui);
385 /* Calling rl_callback_handler_install resets readline's input
386 buffer. Calling this when we were already processing input
387 therefore loses input. */
388 gdb_assert (!callback_handler_installed);
390 #ifdef RL_STATE_EOF
391 /* Some versions of readline contain a bug where the rl_eof_found flag
392 would not be reset back to 0 in rl_initialize, despite the
393 RL_STATE_EOF flag being cleared in this function.
395 The consequence of this mistake is that readline will appear to get
396 stuck in the EOF state, and will emit an extra '\n' character each
397 time an input line is completed.
399 Work around this by clearing the EOF state now ourselves. */
400 if (RL_ISSTATE (RL_STATE_EOF))
402 RL_UNSETSTATE (RL_STATE_EOF);
403 rl_eof_found = 0;
405 #endif /* RL_STATE_EOF */
407 rl_callback_handler_install (prompt, gdb_rl_callback_handler);
408 callback_handler_installed = true;
411 /* See event-top.h, and above. */
413 void
414 gdb_rl_callback_handler_reinstall (void)
416 gdb_assert (current_ui == main_ui);
418 if (!callback_handler_installed)
420 /* Passing NULL as prompt argument tells readline to not display
421 a prompt. */
422 gdb_rl_callback_handler_install (NULL);
426 /* Displays the prompt. If the argument NEW_PROMPT is NULL, the
427 prompt that is displayed is the current top level prompt.
428 Otherwise, it displays whatever NEW_PROMPT is as a local/secondary
429 prompt.
431 This is used after each gdb command has completed, and in the
432 following cases:
434 1. When the user enters a command line which is ended by '\'
435 indicating that the command will continue on the next line. In
436 that case the prompt that is displayed is the empty string.
438 2. When the user is entering 'commands' for a breakpoint, or
439 actions for a tracepoint. In this case the prompt will be '>'
441 3. On prompting for pagination. */
443 void
444 display_gdb_prompt (const char *new_prompt)
446 std::string actual_gdb_prompt;
448 annotate_display_prompt ();
450 /* Reset the nesting depth used when trace-commands is set. */
451 reset_command_nest_depth ();
453 /* Do not call the python hook on an explicit prompt change as
454 passed to this function, as this forms a secondary/local prompt,
455 IE, displayed but not set. */
456 if (! new_prompt)
458 struct ui *ui = current_ui;
460 if (ui->prompt_state == PROMPTED)
461 internal_error (_("double prompt"));
462 else if (ui->prompt_state == PROMPT_BLOCKED)
464 /* This is to trick readline into not trying to display the
465 prompt. Even though we display the prompt using this
466 function, readline still tries to do its own display if
467 we don't call rl_callback_handler_install and
468 rl_callback_handler_remove (which readline detects
469 because a global variable is not set). If readline did
470 that, it could mess up gdb signal handlers for SIGINT.
471 Readline assumes that between calls to rl_set_signals and
472 rl_clear_signals gdb doesn't do anything with the signal
473 handlers. Well, that's not the case, because when the
474 target executes we change the SIGINT signal handler. If
475 we allowed readline to display the prompt, the signal
476 handler change would happen exactly between the calls to
477 the above two functions. Calling
478 rl_callback_handler_remove(), does the job. */
480 if (current_ui->command_editing)
481 gdb_rl_callback_handler_remove ();
482 return;
484 else if (ui->prompt_state == PROMPT_NEEDED)
486 /* Display the top level prompt. */
487 actual_gdb_prompt = top_level_prompt ();
488 ui->prompt_state = PROMPTED;
491 else
492 actual_gdb_prompt = new_prompt;
494 if (current_ui->command_editing)
496 gdb_rl_callback_handler_remove ();
497 gdb_rl_callback_handler_install (actual_gdb_prompt.c_str ());
499 /* new_prompt at this point can be the top of the stack or the one
500 passed in. It can't be NULL. */
501 else
503 /* Don't use a _filtered function here. It causes the assumed
504 character position to be off, since the newline we read from
505 the user is not accounted for. */
506 printf_unfiltered ("%s", actual_gdb_prompt.c_str ());
507 gdb_flush (gdb_stdout);
511 /* Notify the 'before_prompt' observer, and run any additional actions
512 that must be done before we display the prompt. */
513 static void
514 notify_before_prompt (const char *prompt)
516 /* Give observers a chance of changing the prompt. E.g., the python
517 `gdb.prompt_hook' is installed as an observer. */
518 gdb::observers::before_prompt.notify (prompt);
520 /* As we are about to display the prompt, and so GDB might be sitting
521 idle for some time, close all the cached BFDs. This ensures that
522 when we next start running a user command all BFDs will be reopened
523 as needed, and as a result, we will see any on-disk changes. */
524 bfd_cache_close_all ();
527 /* Return the top level prompt, as specified by "set prompt", possibly
528 overridden by the python gdb.prompt_hook hook, and then composed
529 with the prompt prefix and suffix (annotations). */
531 static std::string
532 top_level_prompt (void)
534 notify_before_prompt (get_prompt ().c_str ());
536 const std::string &prompt = get_prompt ();
538 if (annotation_level >= 2)
540 /* Prefix needs to have new line at end. */
541 const char prefix[] = "\n\032\032pre-prompt\n";
543 /* Suffix needs to have a new line at end and \032 \032 at
544 beginning. */
545 const char suffix[] = "\n\032\032prompt\n";
547 return std::string (prefix) + prompt.c_str () + suffix;
550 return prompt;
553 /* Get a reference to the current UI's line buffer. This is used to
554 construct a whole line of input from partial input. */
556 static std::string &
557 get_command_line_buffer (void)
559 return current_ui->line_buffer;
562 /* Re-enable stdin after the end of an execution command in
563 synchronous mode, or after an error from the target, and we aborted
564 the exec operation. */
566 void
567 async_enable_stdin (void)
569 struct ui *ui = current_ui;
571 if (ui->prompt_state == PROMPT_BLOCKED
572 && !ui->keep_prompt_blocked)
574 target_terminal::ours ();
575 ui->register_file_handler ();
576 ui->prompt_state = PROMPT_NEEDED;
580 /* Disable reads from stdin (the console) marking the command as
581 synchronous. */
583 void
584 async_disable_stdin (void)
586 struct ui *ui = current_ui;
588 ui->prompt_state = PROMPT_BLOCKED;
589 ui->unregister_file_handler ();
593 /* Handle a gdb command line. This function is called when
594 handle_line_of_input has concatenated one or more input lines into
595 a whole command. */
597 void
598 command_handler (const char *command)
600 struct ui *ui = current_ui;
601 const char *c;
603 if (ui->instream == ui->stdin_stream)
604 reinitialize_more_filter ();
606 scoped_command_stats stat_reporter (true);
608 /* Do not execute commented lines. */
609 for (c = command; *c == ' ' || *c == '\t'; c++)
611 if (c[0] != '#')
613 execute_command (command, ui->instream == ui->stdin_stream);
615 /* Do any commands attached to breakpoint we stopped at. */
616 bpstat_do_actions ();
620 /* Append RL, an input line returned by readline or one of its emulations, to
621 CMD_LINE_BUFFER. Return true if we have a whole command line ready to be
622 processed by the command interpreter or false if the command line isn't
623 complete yet (input line ends in a backslash). */
625 static bool
626 command_line_append_input_line (std::string &cmd_line_buffer, const char *rl)
628 size_t len = strlen (rl);
630 if (len > 0 && rl[len - 1] == '\\')
632 /* Don't copy the backslash and wait for more. */
633 cmd_line_buffer.append (rl, len - 1);
634 return false;
636 else
638 /* Copy whole line including terminating null, and we're
639 done. */
640 cmd_line_buffer.append (rl, len + 1);
641 return true;
645 /* Handle a line of input coming from readline.
647 If the read line ends with a continuation character (backslash), return
648 nullptr. Otherwise, return a pointer to the command line, indicating a whole
649 command line is ready to be executed.
651 The returned pointer may or may not point to CMD_LINE_BUFFER's internal
652 buffer.
654 Return EOF on end of file.
656 If REPEAT, handle command repetitions:
658 - If the input command line is NOT empty, the command returned is
659 saved using save_command_line () so that it can be repeated later.
661 - OTOH, if the input command line IS empty, return the saved
662 command instead of the empty input line.
665 const char *
666 handle_line_of_input (std::string &cmd_line_buffer,
667 const char *rl, int repeat,
668 const char *annotation_suffix)
670 struct ui *ui = current_ui;
671 int from_tty = ui->instream == ui->stdin_stream;
673 if (rl == NULL)
674 return (char *) EOF;
676 bool complete = command_line_append_input_line (cmd_line_buffer, rl);
677 if (!complete)
678 return NULL;
680 if (from_tty && annotation_level > 1)
681 printf_unfiltered (("\n\032\032post-%s\n"), annotation_suffix);
683 #define SERVER_COMMAND_PREFIX "server "
684 server_command = startswith (cmd_line_buffer.c_str (), SERVER_COMMAND_PREFIX);
685 if (server_command)
687 /* Note that we don't call `save_command_line'. Between this
688 and the check in dont_repeat, this insures that repeating
689 will still do the right thing. */
690 return cmd_line_buffer.c_str () + strlen (SERVER_COMMAND_PREFIX);
693 /* Do history expansion if that is wished. */
694 if (history_expansion_p && from_tty && current_ui->input_interactive_p ())
696 char *cmd_expansion;
697 int expanded;
699 /* Note: here, we pass a pointer to the std::string's internal buffer as
700 a `char *`. At the time of writing, readline's history_expand does
701 not modify the passed-in string. Ideally, readline should be modified
702 to make that parameter `const char *`. */
703 expanded = history_expand (&cmd_line_buffer[0], &cmd_expansion);
704 gdb::unique_xmalloc_ptr<char> history_value (cmd_expansion);
705 if (expanded)
707 /* Print the changes. */
708 printf_unfiltered ("%s\n", history_value.get ());
710 /* If there was an error, call this function again. */
711 if (expanded < 0)
712 return cmd_line_buffer.c_str ();
714 cmd_line_buffer = history_value.get ();
718 /* If we just got an empty line, and that is supposed to repeat the
719 previous command, return the previously saved command. */
720 const char *p1;
721 for (p1 = cmd_line_buffer.c_str (); *p1 == ' ' || *p1 == '\t'; p1++)
723 if (repeat && *p1 == '\0')
724 return get_saved_command_line ();
726 /* Add command to history if appropriate. Note: lines consisting
727 solely of comments are also added to the command history. This
728 is useful when you type a command, and then realize you don't
729 want to execute it quite yet. You can comment out the command
730 and then later fetch it from the value history and remove the
731 '#'. The kill ring is probably better, but some people are in
732 the habit of commenting things out. */
733 if (cmd_line_buffer[0] != '\0' && from_tty && current_ui->input_interactive_p ())
734 gdb_add_history (cmd_line_buffer.c_str ());
736 /* Save into global buffer if appropriate. */
737 if (repeat)
739 save_command_line (cmd_line_buffer.c_str ());
741 /* It is important that we return a pointer to the saved command line
742 here, for the `cmd_start == saved_command_line` check in
743 execute_command to work. */
744 return get_saved_command_line ();
747 return cmd_line_buffer.c_str ();
750 /* See event-top.h. */
752 void
753 gdb_rl_deprep_term_function (void)
755 #ifdef RL_STATE_EOF
756 std::optional<scoped_restore_tmpl<int>> restore_eof_found;
758 if (RL_ISSTATE (RL_STATE_EOF))
760 printf_unfiltered ("quit\n");
761 restore_eof_found.emplace (&rl_eof_found, 0);
764 #endif /* RL_STATE_EOF */
766 rl_deprep_terminal ();
769 /* Handle a complete line of input. This is called by the callback
770 mechanism within the readline library. Deal with incomplete
771 commands as well, by saving the partial input in a global
772 buffer.
774 NOTE: This is the asynchronous version of the command_line_input
775 function. */
777 void
778 command_line_handler (gdb::unique_xmalloc_ptr<char> &&rl)
780 std::string &line_buffer = get_command_line_buffer ();
781 struct ui *ui = current_ui;
783 const char *cmd = handle_line_of_input (line_buffer, rl.get (), 1, "prompt");
784 if (cmd == (char *) EOF)
786 /* stdin closed. The connection with the terminal is gone.
787 This happens at the end of a testsuite run, after Expect has
788 hung up but GDB is still alive. In such a case, we just quit
789 gdb killing the inferior program too. This also happens if the
790 user sends EOF, which is usually bound to ctrl+d. */
792 #ifndef RL_STATE_EOF
793 /* When readline is using bracketed paste mode, then, when eof is
794 received, readline will emit the control sequence to leave
795 bracketed paste mode.
797 This control sequence ends with \r, which means that the "quit" we
798 are about to print will overwrite the prompt on this line.
800 The solution to this problem is to actually print the "quit"
801 message from gdb_rl_deprep_term_function (see above), however, we
802 can only do that if we can know, in that function, when eof was
803 received.
805 Unfortunately, with older versions of readline, it is not possible
806 in the gdb_rl_deprep_term_function to know if eof was received or
807 not, and, as GDB can be built against the system readline, which
808 could be older than the readline in GDB's repository, then we
809 can't be sure that we can work around this prompt corruption in
810 the gdb_rl_deprep_term_function function.
812 If we get here, RL_STATE_EOF is not defined. This indicates that
813 we are using an older readline, and couldn't print the quit
814 message in gdb_rl_deprep_term_function. So, what we do here is
815 check to see if bracketed paste mode is on or not. If it's on
816 then we print a \n and then the quit, this means the user will
817 see:
819 (gdb)
820 quit
822 Rather than the usual:
824 (gdb) quit
826 Which we will get with a newer readline, but this really is the
827 best we can do with older versions of readline. */
828 const char *value = rl_variable_value ("enable-bracketed-paste");
829 if (value != nullptr && strcmp (value, "on") == 0
830 && ((rl_readline_version >> 8) & 0xff) > 0x07)
831 printf_unfiltered ("\n");
832 printf_unfiltered ("quit\n");
833 #endif
835 execute_command ("quit", 1);
837 else if (cmd == NULL)
839 /* We don't have a full line yet. Print an empty prompt. */
840 display_gdb_prompt ("");
842 else
844 ui->prompt_state = PROMPT_NEEDED;
846 /* Ensure the UI's line buffer is empty for the next command. */
847 SCOPE_EXIT { line_buffer.clear (); };
849 command_handler (cmd);
851 if (ui->prompt_state != PROMPTED)
852 display_gdb_prompt (0);
856 /* Does reading of input from terminal w/o the editing features
857 provided by the readline library. Calls the line input handler
858 once we have a whole input line. */
860 void
861 gdb_readline_no_editing_callback (gdb_client_data client_data)
863 int c;
864 std::string line_buffer;
865 struct ui *ui = current_ui;
867 FILE *stream = ui->instream != nullptr ? ui->instream : ui->stdin_stream;
868 gdb_assert (stream != nullptr);
870 /* We still need the while loop here, even though it would seem
871 obvious to invoke gdb_readline_no_editing_callback at every
872 character entered. If not using the readline library, the
873 terminal is in cooked mode, which sends the characters all at
874 once. Poll will notice that the input fd has changed state only
875 after enter is pressed. At this point we still need to fetch all
876 the chars entered. */
878 while (1)
880 /* Read from stdin if we are executing a user defined command.
881 This is the right thing for prompt_for_continue, at least. */
882 c = fgetc (stream);
884 if (c == EOF)
886 if (!line_buffer.empty ())
888 /* The last line does not end with a newline. Return it, and
889 if we are called again fgetc will still return EOF and
890 we'll return NULL then. */
891 break;
893 ui->input_handler (NULL);
894 return;
897 if (c == '\n')
899 if (!line_buffer.empty () && line_buffer.back () == '\r')
900 line_buffer.pop_back ();
901 break;
904 line_buffer += c;
907 ui->input_handler (make_unique_xstrdup (line_buffer.c_str ()));
911 /* Attempt to unblock signal SIG, return true if the signal was unblocked,
912 otherwise, return false. */
914 static bool
915 unblock_signal (int sig)
917 #if HAVE_SIGPROCMASK
918 sigset_t sigset;
919 sigemptyset (&sigset);
920 sigaddset (&sigset, sig);
921 gdb_sigmask (SIG_UNBLOCK, &sigset, 0);
922 return true;
923 #endif
925 return false;
928 /* Signal safe language specific strings. */
930 #ifdef GDB_PRINT_INTERNAL_BACKTRACE
931 static const char *str_fatal_signal;
932 static const char *str_sigsegv;
933 #ifdef SIGFPE
934 static const char *str_sigfpe;
935 #endif
936 #ifdef SIGBUS
937 static const char *str_sigbus;
938 #endif
939 #ifdef SIGABRT
940 static const char *str_sigabrt;
941 #endif
942 static const char *str_unknown_signal;
943 static const char *str_fatal_error_detected_gdb_will_now_terminate;
944 static const char *str_this_is_a_bug;
945 static const char *str_for_instructions_see;
947 /* Initialize language specific strings. */
949 static void
950 init_str_handle_fatal_signal ()
952 str_fatal_signal = _("Fatal signal: ");
953 str_sigsegv = strsignal (SIGSEGV);
954 #ifdef SIGFPE
955 str_sigfpe = strsignal (SIGFPE);
956 #endif
957 #ifdef SIGBUS
958 str_sigbus = strsignal (SIGBUS);
959 #endif
960 #ifdef SIGABRT
961 str_sigabrt = strsignal (SIGABRT);
962 #endif
963 str_unknown_signal = _("Unknown signal");
964 str_fatal_error_detected_gdb_will_now_terminate =
965 _("A fatal error internal to GDB has been detected, "
966 "further\ndebugging is not possible. GDB will now "
967 "terminate.\n\n");
968 str_this_is_a_bug = _("This is a bug, please report it.");
969 str_for_instructions_see = _(" For instructions, see:\n");
971 #endif
973 /* Called to handle fatal signals. SIG is the signal number. */
975 [[noreturn]] static void
976 handle_fatal_signal (int sig)
978 #ifdef TUI
979 tui_disable ();
980 #endif
982 #ifdef GDB_PRINT_INTERNAL_BACKTRACE
983 const auto sig_write = [] (const char *msg) -> void
985 gdb_stderr->write_async_safe (msg, strlen (msg));
988 if (bt_on_fatal_signal)
990 sig_write ("\n\n");
991 sig_write (str_fatal_signal);
992 switch (sig)
994 case SIGSEGV:
995 sig_write (str_sigsegv);
996 break;
997 #ifdef SIGFPE
998 case SIGFPE:
999 sig_write (str_sigfpe);
1000 break;
1001 #endif
1002 #ifdef SIGBUS
1003 case SIGBUS:
1004 sig_write (str_sigbus);
1005 break;
1006 #endif
1007 #ifdef SIGABRT
1008 case SIGABRT:
1009 sig_write (str_sigabrt);
1010 break;
1011 #endif
1012 default:
1013 sig_write (str_unknown_signal);
1014 break;
1016 sig_write ("\n");
1018 gdb_internal_backtrace ();
1020 sig_write (str_fatal_error_detected_gdb_will_now_terminate);
1021 sig_write (str_this_is_a_bug);
1022 if (REPORT_BUGS_TO[0] != '\0')
1024 sig_write (str_for_instructions_see);
1025 sig_write (REPORT_BUGS_TO);
1026 sig_write (".");
1028 sig_write ("\n\n");
1030 gdb_stderr->flush ();
1032 #endif
1034 /* If possible arrange for SIG to have its default behavior (which
1035 should be to terminate the current process), unblock SIG, and reraise
1036 the signal. This ensures GDB terminates with the expected signal. */
1037 if (signal (sig, SIG_DFL) != SIG_ERR
1038 && unblock_signal (sig))
1039 raise (sig);
1041 /* The above failed, so try to use SIGABRT to terminate GDB. */
1042 #ifdef SIGABRT
1043 signal (SIGABRT, SIG_DFL);
1044 #endif
1045 abort (); /* ARI: abort */
1048 /* The SIGSEGV handler for this thread, or NULL if there is none. GDB
1049 always installs a global SIGSEGV handler, and then lets threads
1050 indicate their interest in handling the signal by setting this
1051 thread-local variable.
1053 This is a static variable instead of extern because on various platforms
1054 (notably Cygwin) extern thread_local variables cause link errors. So
1055 instead, we have scoped_segv_handler_restore, which also makes it impossible
1056 to accidentally forget to restore it to the original value. */
1058 static thread_local void (*thread_local_segv_handler) (int);
1060 static void handle_sigsegv (int sig);
1062 /* Install the SIGSEGV handler. */
1063 static void
1064 install_handle_sigsegv ()
1066 #if defined (HAVE_SIGACTION)
1067 struct sigaction sa;
1068 sa.sa_handler = handle_sigsegv;
1069 sigemptyset (&sa.sa_mask);
1070 #ifdef HAVE_SIGALTSTACK
1071 sa.sa_flags = SA_ONSTACK;
1072 #else
1073 sa.sa_flags = 0;
1074 #endif
1075 sigaction (SIGSEGV, &sa, nullptr);
1076 #else
1077 signal (SIGSEGV, handle_sigsegv);
1078 #endif
1081 /* Handler for SIGSEGV. */
1083 static void
1084 handle_sigsegv (int sig)
1086 install_handle_sigsegv ();
1088 if (thread_local_segv_handler == nullptr)
1089 handle_fatal_signal (sig);
1090 thread_local_segv_handler (sig);
1095 /* The serial event associated with the QUIT flag. set_quit_flag sets
1096 this, and check_quit_flag clears it. Used by interruptible_select
1097 to be able to do interruptible I/O with no race with the SIGINT
1098 handler. */
1099 static struct serial_event *quit_serial_event;
1101 /* Initialization of signal handlers and tokens. There are a number of
1102 different strategies for handling different signals here.
1104 For SIGINT, SIGTERM, SIGQUIT, SIGHUP, SIGTSTP, there is a function
1105 handle_sig* for each of these signals. These functions are the actual
1106 signal handlers associated to the signals via calls to signal(). The
1107 only job for these functions is to enqueue the appropriate
1108 event/procedure with the event loop. The event loop will take care of
1109 invoking the queued procedures to perform the usual tasks associated
1110 with the reception of the signal.
1112 For SIGSEGV the handle_sig* function does all the work for handling this
1113 signal.
1115 For SIGFPE, SIGBUS, and SIGABRT, these signals will all cause GDB to
1116 terminate immediately. */
1117 void
1118 gdb_init_signals (void)
1120 initialize_async_signal_handlers ();
1122 quit_serial_event = make_serial_event ();
1124 sigint_token =
1125 create_async_signal_handler (async_request_quit, NULL, "sigint");
1126 install_sigint_handler (handle_sigint);
1128 async_sigterm_token
1129 = create_async_signal_handler (async_sigterm_handler, NULL, "sigterm");
1130 signal (SIGTERM, handle_sigterm);
1132 #ifdef SIGQUIT
1133 sigquit_token =
1134 create_async_signal_handler (async_do_nothing, NULL, "sigquit");
1135 signal (SIGQUIT, handle_sigquit);
1136 #endif
1138 #ifdef SIGHUP
1139 if (signal (SIGHUP, handle_sighup) != SIG_IGN)
1140 sighup_token =
1141 create_async_signal_handler (async_disconnect, NULL, "sighup");
1142 else
1143 sighup_token =
1144 create_async_signal_handler (async_do_nothing, NULL, "sighup");
1145 #endif
1147 #ifdef SIGTSTP
1148 sigtstp_token =
1149 create_async_signal_handler (async_sigtstp_handler, NULL, "sigtstp");
1150 #endif
1152 #ifdef GDB_PRINT_INTERNAL_BACKTRACE
1153 init_str_handle_fatal_signal ();
1154 #endif
1156 #ifdef SIGFPE
1157 signal (SIGFPE, handle_fatal_signal);
1158 #endif
1160 #ifdef SIGBUS
1161 signal (SIGBUS, handle_fatal_signal);
1162 #endif
1164 #ifdef SIGABRT
1165 signal (SIGABRT, handle_fatal_signal);
1166 #endif
1168 install_handle_sigsegv ();
1171 /* See event-top.h. */
1173 void
1174 quit (void)
1176 if (sync_quit_force_run)
1178 sync_quit_force_run = false;
1179 throw_forced_quit ("SIGTERM");
1182 #ifdef __MSDOS__
1183 /* No steenking SIGINT will ever be coming our way when the
1184 program is resumed. Don't lie. */
1185 throw_quit ("Quit");
1186 #else
1187 if (job_control
1188 /* If there is no terminal switching for this target, then we can't
1189 possibly get screwed by the lack of job control. */
1190 || !target_supports_terminal_ours ())
1191 throw_quit ("Quit");
1192 else
1193 throw_quit ("Quit (expect signal SIGINT when the program is resumed)");
1194 #endif
1197 /* See event-top.h. */
1199 void
1200 maybe_quit ()
1202 if (!is_main_thread ())
1203 return;
1205 if (sync_quit_force_run)
1206 quit ();
1208 quit_handler ();
1211 /* See event-top.h. */
1213 void
1214 quit_serial_event_set ()
1216 serial_event_set (quit_serial_event);
1219 /* See event-top.h. */
1221 void
1222 quit_serial_event_clear (void)
1224 serial_event_clear (quit_serial_event);
1227 /* Return the selectable file descriptor of the serial event
1228 associated with the quit flag. */
1230 static int
1231 quit_serial_event_fd (void)
1233 return serial_event_fd (quit_serial_event);
1236 /* See defs.h. */
1238 void
1239 default_quit_handler (void)
1241 if (check_quit_flag ())
1243 if (target_terminal::is_ours ())
1244 quit ();
1245 else
1246 target_pass_ctrlc ();
1250 /* See defs.h. */
1251 quit_handler_ftype *quit_handler = default_quit_handler;
1253 /* Handle a SIGINT. */
1255 void
1256 handle_sigint (int sig)
1258 signal (sig, handle_sigint);
1260 /* We could be running in a loop reading in symfiles or something so
1261 it may be quite a while before we get back to the event loop. So
1262 set quit_flag to true here. Then if QUIT is called before we get to
1263 the event loop, we will unwind as expected. */
1264 set_quit_flag ();
1266 /* In case nothing calls QUIT before the event loop is reached, the
1267 event loop handles it. */
1268 mark_async_signal_handler (sigint_token);
1271 /* Copy file descriptors smaller than N from SRC to DST and return DST.
1272 Portable version of FD_COPY. */
1274 static fd_set *
1275 fd_copy (fd_set *dst, const fd_set *src, int n)
1277 FD_ZERO (dst);
1278 for (int i = 0; i < n; ++i)
1279 if (FD_ISSET (i, const_cast<fd_set *>(src)))
1280 FD_SET (i, dst);
1282 return dst;
1285 /* Copy SRC to DST and return DST. */
1287 static struct timeval *
1288 timeval_copy (struct timeval *dst, const struct timeval *src)
1290 *dst = *src;
1291 return dst;
1294 /* Version of select that can be used in a loop, since unlike select it keeps
1295 requested and returned values separate. */
1297 static int
1298 gdb_select (int n,
1299 const fd_set *req_readfds, fd_set *ret_readfds,
1300 const fd_set *req_writefds, fd_set *ret_writefds,
1301 const fd_set *req_exceptfds, fd_set *ret_exceptfds,
1302 const struct timeval *req_timeout, struct timeval *ret_timeout)
1304 ret_readfds
1305 = (req_readfds == nullptr
1306 ? nullptr
1307 : fd_copy (ret_readfds, req_readfds, n));
1308 ret_writefds
1309 = (req_writefds == nullptr
1310 ? nullptr
1311 : fd_copy (ret_writefds, req_writefds, n));
1312 ret_exceptfds
1313 = (req_exceptfds == nullptr
1314 ? nullptr
1315 : fd_copy (ret_exceptfds, req_exceptfds, n));
1317 ret_timeout
1318 = (req_timeout == nullptr
1319 ? nullptr
1320 : timeval_copy (ret_timeout, req_timeout));
1322 return gdb_select (n, ret_readfds, ret_writefds, ret_exceptfds, ret_timeout);
1325 /* See gdb_select.h. */
1328 interruptible_select (int n,
1329 fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
1330 struct timeval *timeout)
1332 fd_set my_readfds;
1333 int fd;
1334 int res;
1336 if (readfds == NULL)
1338 readfds = &my_readfds;
1339 FD_ZERO (&my_readfds);
1342 fd = quit_serial_event_fd ();
1343 FD_SET (fd, readfds);
1344 if (n <= fd)
1345 n = fd + 1;
1347 bool tsan_forced_timeout = false;
1348 #if defined (__SANITIZE_THREAD__)
1349 struct timeval tv;
1350 if (timeout == nullptr)
1352 /* A nullptr timeout means select is blocking, and ThreadSanitizer has
1353 a bug that it considers select non-blocking, and consequently when
1354 intercepting select it will not call signal handlers for pending
1355 signals, and gdb will hang in select waiting for those signal
1356 handlers to be called.
1358 Filed here ( https://github.com/google/sanitizers/issues/1813 ).
1360 Work around this by:
1361 - forcing a small timeout, and
1362 - upon timeout calling a function that ThreadSanitizer does consider
1363 blocking: usleep, forcing signal handlers to be called for pending
1364 signals. */
1365 tv.tv_sec = 0;
1366 tv.tv_usec = 1000;
1367 timeout = &tv;
1368 tsan_forced_timeout = true;
1370 #endif
1373 fd_set ret_readfds, ret_writefds, ret_exceptfds;
1374 struct timeval ret_timeout;
1376 while (true)
1378 res = gdb_select (n,
1379 readfds, &ret_readfds,
1380 writefds, &ret_writefds,
1381 exceptfds, &ret_exceptfds,
1382 timeout, &ret_timeout);
1384 if (res == -1 && errno == EINTR)
1385 continue;
1387 if (tsan_forced_timeout && res == 0)
1389 usleep (0);
1390 continue;
1393 break;
1396 if (readfds != nullptr)
1397 fd_copy (readfds, &ret_readfds, n);
1398 if (writefds != nullptr)
1399 fd_copy (writefds, &ret_writefds, n);
1400 if (exceptfds != nullptr)
1401 fd_copy (exceptfds, &ret_exceptfds, n);
1402 if (timeout)
1403 timeval_copy (timeout, &ret_timeout);
1406 if (res == 1 && FD_ISSET (fd, readfds))
1408 errno = EINTR;
1409 return -1;
1411 return res;
1414 /* Handle GDB exit upon receiving SIGTERM if target_can_async_p (). */
1416 static void
1417 async_sigterm_handler (gdb_client_data arg)
1419 quit_force (NULL, 0);
1422 /* See defs.h. */
1423 volatile bool sync_quit_force_run;
1425 /* See defs.h. */
1426 void
1427 set_force_quit_flag ()
1429 sync_quit_force_run = true;
1430 set_quit_flag ();
1433 /* Quit GDB if SIGTERM is received.
1434 GDB would quit anyway, but this way it will clean up properly. */
1435 void
1436 handle_sigterm (int sig)
1438 signal (sig, handle_sigterm);
1440 set_force_quit_flag ();
1442 mark_async_signal_handler (async_sigterm_token);
1445 /* Do the quit. All the checks have been done by the caller. */
1446 void
1447 async_request_quit (gdb_client_data arg)
1449 /* If the quit_flag has gotten reset back to false by the time we get
1450 back here, that means that an exception was thrown to unwind the
1451 current command before we got back to the event loop. So there
1452 is no reason to call quit again here. */
1453 QUIT;
1456 #ifdef SIGQUIT
1457 /* Tell the event loop what to do if SIGQUIT is received.
1458 See event-signal.c. */
1459 static void
1460 handle_sigquit (int sig)
1462 mark_async_signal_handler (sigquit_token);
1463 signal (sig, handle_sigquit);
1465 #endif
1467 #if defined (SIGQUIT) || defined (SIGHUP)
1468 /* Called by the event loop in response to a SIGQUIT or an
1469 ignored SIGHUP. */
1470 static void
1471 async_do_nothing (gdb_client_data arg)
1473 /* Empty function body. */
1475 #endif
1477 #ifdef SIGHUP
1478 /* Tell the event loop what to do if SIGHUP is received.
1479 See event-signal.c. */
1480 static void
1481 handle_sighup (int sig)
1483 scoped_restore restore_errno = make_scoped_restore (&errno);
1484 mark_async_signal_handler (sighup_token);
1485 signal (sig, handle_sighup);
1488 /* Called by the event loop to process a SIGHUP. */
1489 static void
1490 async_disconnect (gdb_client_data arg)
1495 quit_cover ();
1498 catch (const gdb_exception &exception)
1500 gdb_puts ("Could not kill the program being debugged",
1501 gdb_stderr);
1502 exception_print (gdb_stderr, exception);
1503 if (exception.reason == RETURN_FORCED_QUIT)
1504 throw;
1507 for (inferior *inf : all_inferiors ())
1511 inf->pop_all_targets ();
1513 catch (const gdb_exception &exception)
1518 signal (SIGHUP, SIG_DFL); /*FIXME: ??????????? */
1519 raise (SIGHUP);
1521 #endif
1523 #ifdef SIGTSTP
1524 void
1525 handle_sigtstp (int sig)
1527 mark_async_signal_handler (sigtstp_token);
1528 signal (sig, handle_sigtstp);
1531 static void
1532 async_sigtstp_handler (gdb_client_data arg)
1534 const std::string &prompt = get_prompt ();
1536 signal (SIGTSTP, SIG_DFL);
1537 unblock_signal (SIGTSTP);
1538 raise (SIGTSTP);
1539 signal (SIGTSTP, handle_sigtstp);
1540 printf_unfiltered ("%s", prompt.c_str ());
1541 gdb_flush (gdb_stdout);
1543 /* Forget about any previous command -- null line now will do
1544 nothing. */
1545 dont_repeat ();
1547 #endif /* SIGTSTP */
1551 /* Set things up for readline to be invoked via the alternate
1552 interface, i.e. via a callback function
1553 (gdb_rl_callback_read_char), and hook up instream to the event
1554 loop. */
1556 void
1557 gdb_setup_readline (int editing)
1559 struct ui *ui = current_ui;
1561 /* If the input stream is connected to a terminal, turn on editing.
1562 However, that is only allowed on the main UI, as we can only have
1563 one instance of readline. Also, INSTREAM might be nullptr when
1564 executing a user-defined command. */
1565 if (ui->instream != nullptr && ISATTY (ui->instream)
1566 && editing && ui == main_ui)
1568 /* Tell gdb that we will be using the readline library. This
1569 could be overwritten by a command in .gdbinit like 'set
1570 editing on' or 'off'. */
1571 ui->command_editing = 1;
1573 /* When a character is detected on instream by select or poll,
1574 readline will be invoked via this callback function. */
1575 ui->call_readline = gdb_rl_callback_read_char_wrapper;
1577 /* Tell readline to use the same input stream that gdb uses. */
1578 rl_instream = ui->instream;
1580 else
1582 ui->command_editing = 0;
1583 ui->call_readline = gdb_readline_no_editing_callback;
1586 /* Now create the event source for this UI's input file descriptor.
1587 Another source is going to be the target program (inferior), but
1588 that must be registered only when it actually exists (I.e. after
1589 we say 'run' or after we connect to a remote target. */
1590 ui->register_file_handler ();
1593 /* Disable command input through the standard CLI channels. Used in
1594 the suspend proc for interpreters that use the standard gdb readline
1595 interface, like the cli & the mi. */
1597 void
1598 gdb_disable_readline (void)
1600 struct ui *ui = current_ui;
1602 if (ui->command_editing)
1603 gdb_rl_callback_handler_remove ();
1604 ui->unregister_file_handler ();
1607 scoped_segv_handler_restore::scoped_segv_handler_restore (segv_handler_t new_handler)
1609 m_old_handler = thread_local_segv_handler;
1610 thread_local_segv_handler = new_handler;
1613 scoped_segv_handler_restore::~scoped_segv_handler_restore()
1615 thread_local_segv_handler = m_old_handler;
1618 static const char debug_event_loop_off[] = "off";
1619 static const char debug_event_loop_all_except_ui[] = "all-except-ui";
1620 static const char debug_event_loop_all[] = "all";
1622 static const char *debug_event_loop_enum[] = {
1623 debug_event_loop_off,
1624 debug_event_loop_all_except_ui,
1625 debug_event_loop_all,
1626 nullptr
1629 static const char *debug_event_loop_value = debug_event_loop_off;
1631 static void
1632 set_debug_event_loop_command (const char *args, int from_tty,
1633 cmd_list_element *c)
1635 if (debug_event_loop_value == debug_event_loop_off)
1636 debug_event_loop = debug_event_loop_kind::OFF;
1637 else if (debug_event_loop_value == debug_event_loop_all_except_ui)
1638 debug_event_loop = debug_event_loop_kind::ALL_EXCEPT_UI;
1639 else if (debug_event_loop_value == debug_event_loop_all)
1640 debug_event_loop = debug_event_loop_kind::ALL;
1641 else
1642 gdb_assert_not_reached ("Invalid debug event look kind value.");
1645 static void
1646 show_debug_event_loop_command (struct ui_file *file, int from_tty,
1647 struct cmd_list_element *cmd, const char *value)
1649 gdb_printf (file, _("Event loop debugging is %s.\n"), value);
1652 void _initialize_event_top ();
1653 void
1654 _initialize_event_top ()
1656 add_setshow_enum_cmd ("event-loop", class_maintenance,
1657 debug_event_loop_enum,
1658 &debug_event_loop_value,
1659 _("Set event-loop debugging."),
1660 _("Show event-loop debugging."),
1661 _("\
1662 Control whether to show event loop-related debug messages."),
1663 set_debug_event_loop_command,
1664 show_debug_event_loop_command,
1665 &setdebuglist, &showdebuglist);
1667 add_setshow_boolean_cmd ("backtrace-on-fatal-signal", class_maintenance,
1668 &bt_on_fatal_signal, _("\
1669 Set whether to produce a backtrace if GDB receives a fatal signal."), _("\
1670 Show whether GDB will produce a backtrace if it receives a fatal signal."), _("\
1671 Use \"on\" to enable, \"off\" to disable.\n\
1672 If enabled, GDB will produce a minimal backtrace if it encounters a fatal\n\
1673 signal from within GDB itself. This is a mechanism to help diagnose\n\
1674 crashes within GDB, not a mechanism for debugging inferiors."),
1675 gdb_internal_backtrace_set_cmd,
1676 show_bt_on_fatal_signal,
1677 &maintenance_set_cmdlist,
1678 &maintenance_show_cmdlist);