aboutsummaryrefslogtreecommitdiff
path: root/gdb/event-top.c
AgeCommit message (Collapse)AuthorFilesLines
2024-01-12Update copyright year range in header of all files managed by GDBAndrew Burgess1-1/+1
This commit is the result of the following actions: - Running gdb/copyright.py to update all of the copyright headers to include 2024, - Manually updating a few files the copyright.py script told me to update, these files had copyright headers embedded within the file, - Regenerating gdbsupport/Makefile.in to refresh it's copyright date, - Using grep to find other files that still mentioned 2023. If these files were updated last year from 2022 to 2023 then I've updated them this year to 2024. I'm sure I've probably missed some dates. Feel free to fix them up as you spot them.
2023-11-21gdb: Replace gdb::optional with std::optionalLancelot Six1-1/+1
Since GDB now requires C++17, we don't need the internally maintained gdb::optional implementation. This patch does the following replacing: - gdb::optional -> std::optional - gdb::in_place -> std::in_place - #include "gdbsupport/gdb_optional.h" -> #include <optional> This change has mostly been done automatically. One exception is gdbsupport/thread-pool.* which did not use the gdb:: prefix as it already lives in the gdb namespace. Change-Id: I19a92fa03e89637bab136c72e34fd351524f65e9 Approved-By: Tom Tromey <tom@tromey.com> Approved-By: Pedro Alves <pedro@palves.net>
2023-11-20gdb: move all bfd_cache_close_all calls in gdb_bfd.cAndrew Burgess1-3/+17
In the following commit I ran into a problem. The next commit aims to improve GDB's handling of the main executable being a file on a remote target (i.e. one with a 'target:' prefix). To do this I have replaced a system 'stat' call with a bfd_stat call. However, doing this caused a regression in gdb.base/attach.exp. The problem is that the bfd library caches open FILE* handles for bfd objects that it has accessed, which is great for short-lived, non interactive programs (e.g. the assembler, or objcopy, etc), however, for GDB this caching causes us a problem. If we open the main executable as a bfd then the bfd library will cache the open FILE*. If some time passes, maybe just sat at the GDB prompt, or with the inferior running, and then later we use bfd_stat to check if the underlying, on-disk file has changed, then the bfd library will actually use fstat on the underlying file descriptor. This is of course slightly different than using system stat on with the on-disk file name. If the on-disk file has changed then system stat will give results for the current on-disk file. But, if the bfd cache is still holding open the file descriptor for the original on-disk file (from before the change) then fstat will return a result based on the original file, and so show no change as having happened. This is a known problem in GDB, and so far this has been solved by scattering bfd_cache_close_all() calls throughout GDB. But, as I said, in the next commit I've made a change and run into a problem (gdb.base/attach.exp) where we are apparently missing a bfd_cache_close_all() call. Now I could solve this problem by adding a bfd_cache_close_all() call before the bfd_stat call that I plan to add in the next commit, that would for sure solve the problem, but feels a little crude. Better I think would be to track down where the bfd is being opened and add a corresponding bfd_cache_close_all() call elsewhere in GDB once we've finished doing whatever it is that caused us to open the bfd in the first place. This second solution felt like the better choice, so I tracked the problem down to elf_locate_base and fixed that. But that just exposed another problem in gdb_bfd_map_section which was also re-opening the bfd, so I fixed this (with another bfd_cache_close_all() call), and that exposed another issue in gdbarch_lookup_osabi... and at this point I wondered if I was approaching this problem the wrong way... .... And so, I wonder, is there a _better_ way to handle these bfd_cache_close_all() calls? I see two problems with the current approach: 1. It's fragile. Folk aren't always aware that they need to clear the bfd cache, and this feels like something that is easy to overlook in review. So adding new code to GDB can innocently touch a bfd, which populates the cache, which will then be a bug that can lie hidden until an on-disk file just happens to change at the wrong time ... and GDB fails to spot the change. Additionally, 2. It's in efficient. The caching is intended to stop the bfd library from continually having to re-open the on-disk file. If we have a function that touches a bfd then often that function is the obvious place to call bfd_cache_close_all. But if a single GDB command calls multiple functions, each of which touch the bfd, then we will end up opening and closing the same on-disk file multiple times. It feels like we would be better postponing the bfd_cache_close_all call until some later point, then we can benefit from the bfd cache. So, in this commit I propose a new approach. We now clear the bfd cache in two places: (a) Just before we display a GDB prompt. We display a prompt after completing a command, and GDB is about to enter an idle state waiting for further input from the user (or in async mode, for an inferior event). If while we are in this idle state the user changes the on-disk file(s) then we would like GDB to notice this the next time it leaves its idle state, e.g. the next time the user executes a command, or when an inferior event arrives, (b) When we resume the inferior. In synchronous mode, resuming the inferior is another time when GDB is blocked and sitting idle, but in this case we don't display a prompt. As with (a) above, when an inferior event arrives we want GDB to notice any changes to on-disk files. It turns out that there are existing observers for both of these cases (before_prompt and target_resumed respectively), so my initial thought was that I should attach to these observers in gdb_bfd.c, and in both cases call bfd_cache_close_all(). And this does indeed solve the gdb.base/attach.exp problem that I see with the following commit. However, I see a problem with this solution. Both of the observers I'm using are exposed through the Python API as events that a user can hook into. The user can potentially run any GDB command (using gdb.execute), so Python code might end up causing some bfds to be reopened, and inserted into the cache. To solve this one solution would be to add a bfd_cache_close_all() call into gdbpy_enter::~gdbpy_enter(). Unfortunately, there's no similar enter/exit object for Guile, though right now Guile doesn't offer the same event API, so maybe we could just ignore that problem... but this doesn't feel great. So instead, I think a better solution might be to not use observers for the bfd_cache_close_all() calls. Instead, I'll call bfd_cache_close_all() directly from core GDB after we've notified the before_prompt and target_resumed observers, this was we can be sure that the cache is cleared after the observers have run, and before GDB enters an idle state. This commit also removes all of the other bfd_cache_close_all() calls from GDB. My claim is that these are no longer needed. Approved-By: Tom Tromey <tom@tromey.com>
2023-11-06Remove EXTERN_C and related definesTom Tromey1-1/+1
common-defs.h has a few defines that I suspect were used during the transition to C++. These aren't needed any more, so remove them. Tested by rebuilding. Approved-By: Simon Marchi <simon.marchi@efficios.com> Approved-By: Andrew Burgess <aburgess@redhat.com>
2023-09-20Remove explanatory comments from includesTom Tromey1-2/+2
I noticed a comment by an include and remembered that I think these don't really provide much value -- sometimes they are just editorial, and sometimes they are obsolete. I think it's better to just remove them. Tested by rebuilding. Approved-By: Andrew Burgess <aburgess@redhat.com>
2023-09-07gdb: remove interp_supports_command_editingSimon Marchi1-2/+2
It is a trivial wrapper around the supports_command_editing method, remove it. Change-Id: I0fe3d7dc69601b3b89f82e055f7fe3d4af1becf7 Approved-By: Tom Tromey <tom@tromey.com>
2023-06-07[gdb/cli] Handle pending ^C after rl_callback_read_char for readline 7Tom de Vries1-1/+10
In commit faf01aee1d0 ("[gdb] Handle pending ^C after rl_callback_read_char") we handled a problem (described in detail in that commit) for readline >= 8 using public readline functions rl_pending_signal and rl_check_signals. For readline 7 (note that we require at least readline 7 so there's no need to worry about readline 6), there was no fix though, because rl_check_signals was not available. Fix this by instead using the private readline function _rl_signal_handler. There is precedent for using private readline variables and functions, but it's something we want to get rid of (PR build/10723). Nevertheless, I think we can allow this specific instance because it's not used when building against readline >= 8. [ In the meanwhile, a fix was committed in the devel branch of the readline repo, contained in commit 8d0c439 ("rollup of changes since readline-8.2"), first proposed here ( https://lists.gnu.org/archive/html/bug-readline/2022-10/msg00008.html ). ] Tested on x86_64-linux, against system readline 7.0 on openSUSE Leap 15.4. PR cli/27813 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=27813
2023-05-01gdb: move struct ui and related things to ui.{c,h}Simon Marchi1-77/+1
I'd like to move some things so they become methods on struct ui. But first, I think that struct ui and the related things are big enough to deserve their own file, instead of being scattered through top.{c,h} and event-top.c. Change-Id: I15594269ace61fd76ef80a7b58f51ff3ab6979bc
2023-02-27Introduce set_force_quit_flag and change type of sync_quit_force_runKevin Buettner1-3/+10
At the moment, handle_sigterm() in event-top.c does the following: sync_quit_force_run = 1; set_quit_flag (); This was used several more times in a later patch in this series, so I'm introducing (at Pedro's suggestion) a new function named 'set_force_quit_flag'. It simply sets sync_quit_force_run and also calls set_quit_flag(). I've revised the later patch to call set_force_quit_flag instead. I noticed that sync_quit_force_run is declared as an int but is being used as a bool, so I also changed its type to bool in this commit. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=26761 Approved-By: Pedro Alves <pedro@palves.net>
2023-02-27QUIT processing w/ explicit throw for gdb_exception_forced_quitKevin Buettner1-0/+2
This commit contains changes which have an explicit throw for gdb_exception_forced_quit, or, in a couple of cases for gdb_exception, but with a throw following a check to see if 'reason' is RETURN_FORCED_QUIT. Most of these are straightforward - it made sense to continue to allow an existing catch of gdb_exception to also catch gdb_exception_quit; in these cases, a catch/throw for gdb_exception_forced_quit was added. There are two cases, however, which deserve a more detailed explanation. 1) remote_fileio_request in gdb/remote-fileio.c: The try block calls do_remote_fileio_request which can (in turn) call one of the functions in remote_fio_func_map[]. Taking the first one, remote_fileio_func_open(), we have the following call path to maybe_quit(): remote_fileio_func_open(remote_target*, char*) -> target_read_memory(unsigned long, unsigned char*, long) -> target_read(target_ops*, target_object, char const*, unsigned char*, unsigned long, long) -> maybe_quit() Since there is a path to maybe_quit(), we must ensure that the catch block is not permitted to swallow a QUIT representing a SIGTERM. However, for this case, we must take care not to change the way that Ctrl-C / SIGINT is handled; we want to send a suitable EINTR reply to the remote target should that happen. That being the case, I added a catch/throw for gdb_exception_forced_quit. I also did a bit of rewriting here, adding a catch for gdb_exception_quit in favor of checking the 'reason' code in the catch block for gdb_exception. 2) mi_execute_command in gdb/mi/mi-main.c: The try block calls captured_mi_execute_command(); there exists a call path to maybe_quit(): captured_mi_execute_command(ui_out*, mi_parse*) -> mi_cmd_execute(mi_parse*) -> get_current_frame() -> get_prev_frame_always_1(frame_info*) -> frame_register_unwind_location(frame_info*, int, int*, lval_type*, unsigned long*, int*) -> frame_register_unwind(frame_info*, int, int*, int*, lval_type*, unsigned long*, int*, unsigned char*) -> value_entirely_available(value*) -> value_fetch_lazy(value*) -> value_fetch_lazy_memory(value*) -> read_value_memory(value*, long, int, unsigned long, unsigned char*, unsigned long) -> maybe_quit() That being the case, we can't allow the exception handler (catch block) to swallow a gdb_exception_quit for SIGTERM. However, it does seem reasonable to output the exception via the mi interface so that some suitable message regarding SIGTERM might be printed; therefore, I check the exception's 'reason' field for RETURN_FORCED_QUIT and do a throw for this case. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=26761 Tested-by: Tom de Vries <tdevries@suse.de> Approved-By: Pedro Alves <pedro@palves.net>
2023-02-24Don't use struct buffer in event-top.cTom Tromey1-14/+6
This changes event-top.c to use std::string rather than struct buffer. This isn't completely ideal, in that it requires a copy of the string to be made.
2023-01-27gdb/tui: disable tui mode when an assert triggersAndrew Burgess1-0/+8
When an assert triggers in tui mode the output is not great, the internal backtrace that is generated is printed directly to the file descriptor for gdb_stderr, and, as a result, does not currently format itself correctly - the output uses only '\n' at the end of each line, and so, when the terminal is in raw mode, the cursor does not return to the start of each line after the '\n'. This is mostly fixable, we could update bt-utils.c to use '\r\n' instead of just '\n', and this would fix most of the problems. The one we can't easily fix is if/when GDB is built to use execinfo instead of libbacktrace, in this case we use backtrace_symbols_fd to print the symbols, and this function only uses '\n' as the line terminator. Fixing this would require switching to backtrace_symbols, but that API uses malloc, which is something we're trying to avoid (this code is called when GDB hits an error, so ideally we don't want to rely on malloc). However, the execinfo code is only used when libbacktrace is not available (or the user specifically disables libbacktrace) so maybe we can ignore that problem... ... but there is another problem. When the backtrace is printed in raw mode, it is possible that the backtrace fills the screen. With the terminal in raw mode we don't have the ability to scroll back, which means we loose some of the backtrace, which isn't ideal. In this commit I propose that we should disable tui mode whenever we handle a fatal signal, or when we hit the internal error code path (e.g. when an assert triggers). With this done then we don't need to update the bt-utils.c code, and the execinfo version of the code (using backtrace_symbols_fd) works just fine. We also get the ability to scroll back to view the error message and all of the backtrace, assuming the users terminal supports scrolling back. The only downside I see with this change is if the tui_disable call itself causes an error for some reason, or, if we handle a single at a time when it is not safe to call tui_disable, in these cases the extra tui_disable call might cause GDB to loose the original error. However, I think (just from personal experience) that the above two issues are pretty rare and the benefits from this change far out weighs the possible drawbacks.
2023-01-01Update copyright year range in header of all files managed by GDBJoel Brobecker1-1/+1
This commit is the result of running the gdb/copyright.py script, which automated the update of the copyright year range for all source files managed by the GDB project to be updated to include year 2023.
2022-12-15gdb: remove static buffer in command_line_inputSimon Marchi1-59/+51
[I sent this earlier today, but I don't see it in the archives. Resending it through a different computer / SMTP.] The use of the static buffer in command_line_input is becoming problematic, as explained here [1]. In short, with this patch [2] that attempt to fix a post-hook bug, when running gdb.base/commands.exp, we hit a case where we read a "define" command line from a script file using command_command_line_input. The command line is stored in command_line_input's static buffer. Inside the define command's execution, we read the lines inside the define using command_line_input, which overwrites the define command, in command_line_input's static buffer. After the execution of the define command, execute_command does a command look up to see if a post-hook is registered. For that, it uses a now stale pointer that used to point to the define command, in the static buffer, causing a use-after-free. Note that the pointer in execute_command points to the dynamically-allocated buffer help by the static buffer in command_line_input, not to the static object itself, hence why we see a use-after-free. Fix that by removing the static buffer. I initially changed command_line_input and other related functions to return an std::string, which is the obvious but naive solution. The thing is that some callees don't need to return an allocated string, so this this an unnecessary pessimization. I changed it to passing in a reference to an std::string buffer, which the callee can use if it needs to return dynamically-allocated content. It fills the buffer and returns a pointers to the C string inside. The callees that don't need to return dynamically-allocated content simply don't use it. So, it started with modifying command_line_input as described above, all the other changes derive directly from that. One slightly shady thing is in handle_line_of_input, where we now pass a pointer to an std::string's internal buffer to readline's history_value function, which takes a `char *`. I'm pretty sure that this function does not modify the input string, because I was able to change it (with enough massaging) to take a `const char *`. A subtle change is that we now clear a UI's line buffer using a SCOPE_EXIT in command_line_handler, after executing the command. This was previously done by this line in handle_line_of_input: /* We have a complete command line now. Prepare for the next command, but leave ownership of memory to the buffer . */ cmd_line_buffer->used_size = 0; I think the new way is clearer. [1] https://inbox.sourceware.org/gdb-patches/becb8438-81ef-8ad8-cc42-fcbfaea8cddd@simark.ca/ [2] https://inbox.sourceware.org/gdb-patches/20221213112241.621889-1-jan.vrany@labware.com/ Change-Id: I8fc89b1c69870c7fc7ad9c1705724bd493596300 Reviewed-By: Tom Tromey <tom@tromey.com>
2022-12-14gdb: remove the pop_all_targets (and friends) global functionsAndrew Burgess1-2/+1
This commit removes the global functions pop_all_targets, pop_all_targets_above, and pop_all_targets_at_and_above, and makes them methods on the inferior class. As the pop_all_targets functions will unpush each target, which decrements the targets reference count, it is possible that the target might be closed. Right now, closing a target, in some cases, depends on the current inferior being set correctly, that is, to the inferior from which the target was popped. To facilitate this I have used switch_to_inferior_no_thread within the new methods. Previously it was the responsibility of the caller to ensure that the correct inferior was selected. In a couple of places (event-top.c and top.c) I have been able to remove a previous switch_to_inferior_no_thread call. In remote_unpush_target (remote.c) I have left the switch_to_inferior_no_thread call as it is required for the generic_mourn_inferior call.
2022-12-13Fix control-c handling on WindowsTom Tromey1-1/+1
As Hannes pointed out, the Windows target-async patches broke C-c handling there. Looking into this, I found a few oddities, fixed here. First, windows_nat_target::interrupt calls GenerateConsoleCtrlEvent. I think this event can be ignored by the inferior, so it's not a great way to interrupt. Instead, using DebugBreakProcess (or a more complicated thing for Wow64) seems better. Second, windows_nat_target did not implement the pass_ctrlc method. Implementing this lets us remove the special code to call SetConsoleCtrlHandler and instead integrate into gdb's approach to C-c handling. I believe that this should also fix the race that's described in the comment that's being removed. Initially, I thought a simpler version of this patch would work. However, I think what happens is that some other library (I'm not sure what) calls SetConsoleCtrlHandler while gdb is running, and this intercepts and handles C-c -- so that the gdb SIGINT handler is not called. C-break continues to work, presumably because whatever handler is installed ignores it. This patch works around this issue by ensuring that the gdb handler always comes first.
2022-10-19internal_error: remove need to pass __FILE__/__LINE__Pedro Alves1-1/+1
Currently, every internal_error call must be passed __FILE__/__LINE__ explicitly, like: internal_error (__FILE__, __LINE__, "foo %d", var); The need to pass in explicit __FILE__/__LINE__ is there probably because the function predates widespread and portable variadic macros availability. We can use variadic macros nowadays, and in fact, we already use them in several places, including the related gdb_assert_not_reached. So this patch renames the internal_error function to something else, and then reimplements internal_error as a variadic macro that expands __FILE__/__LINE__ itself. The result is that we now should call internal_error like so: internal_error ("foo %d", var); Likewise for internal_warning. The patch adjusts all calls sites. 99% of the adjustments were done with a perl/sed script. The non-mechanical changes are in gdbsupport/errors.h, gdbsupport/gdb_assert.h, and gdb/gdbarch.py. Approved-By: Simon Marchi <simon.marchi@efficios.com> Change-Id: Ia6f372c11550ca876829e8fd85048f4502bdcf06
2022-09-20Make stdin_event_handler staticTom Tromey1-1/+1
I noticed that stdin_event_handler is only used in event-top.c, so this patch changes it to be 'static'.
2022-09-16[gdb] Handle pending ^C after rl_callback_read_charTom de Vries1-0/+16
In completion tests in various test-cases, we've been running into these "clearing input line" timeouts: ... (gdb) $cmd^GPASS: gdb.gdb/unittest.exp: tab complete "$cmd" FAIL: gdb.gdb/unittest.exp: tab complete "$cmd" (clearing input line) (timeout) ... where $cmd == "maintenance selftest name_that_does_not_exist". AFAIU, the following scenario happens: - expect sends "$cmd\t" - gdb detects the stdin event, and calls rl_callback_read_char until it comes to handle \t - readline interprets the \t as completion, tries to complete, fails to do so, outputs a bell (^G) - expect sees the bell, and proceeds to send ^C - readline is still in the call to rl_callback_read_char, and stores the signal in _rl_caught_signal - readline returns from the call to rl_callback_read_char, without having handled _rl_caught_signal - gdb goes to wait for the next event - expect times out waiting for "Quit", the expected reaction for ^C Fix this by handling pending signals after each call to rl_callback_read_char. The fix is only available for readline 8.x, if --with-system-readline provides an older version, then the fix is disabled due to missing function rl_check_signals. Tested on x86_64-linux. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=27813
2022-08-31Fix interpreter-exec crashTom Tromey1-2/+4
PR mi/10347 points out that using interpreter-exec inside of a "define" command will crash gdb. The bug here is that gdb_setup_readline doesn't check for the case where instream==nullptr. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=10347
2022-08-31Remove a ui-related memory leakTom Tromey1-11/+0
gdb_setup_readline makes new streams and assigns to the various stream members of struct ui. However, these assignments cause the previous values to leak. As far as I can, this code is simply unnecessary and can be removed -- with the exception of the assignment to gdb_stdtarg, which is not initialized anywhere else.
2022-08-31Remove the "for moment" commentsTom Tromey1-2/+2
A few spots setting some gdb output stream variables have a "for moment" comment. These comments aren't useful and I think the moment has passed -- these are permanent now.
2022-08-31Remove some dead codeTom Tromey1-13/+0
This patch removes some dead code and an old FIXME. These no longer seem useful, even for documentation purposes.
2022-08-31Let ui::input_fd be -1Tom Tromey1-3/+5
This changes gdb so that, if ui::input_fd is set to -1, then it will not be registered with the event loop. This is useful for the DAP support code I wrote, but as it turns out to also be useful to Insight, it seems best to check it in separately.
2022-07-18Replace input_interactive_p with a methodTom Tromey1-2/+2
This replaces the global input_interactive_p function with a new method ui::input_interactive_p.
2022-07-18Remove ui_register_input_event_handlerTom Tromey1-10/+10
This patch removes ui_register_input_event_handler and ui_unregister_input_event_handler, replacing them with methods on 'ui'. It also changes gdb to use these methods everywhere, rather than sometimes reaching in to the ui to manage the file descriptor directly.
2022-04-24gdb: move setbuf calls out of gdb_readline_no_editing_callbackAndrew Burgess1-13/+0
After this commit: commit d08cbc5d3203118da5583296e49273cf82378042 Date: Wed Dec 22 12:57:44 2021 +0000 gdb: unbuffer all input streams when not using readline Issues were reported with some MS-Windows hosts, see the thread starting here: https://sourceware.org/pipermail/gdb-patches/2022-March/187004.html Filed in bugzilla as: PR mi/29002 The problem seems to be that calling setbuf on terminal file handles is not always acceptable, see this mail for more details: https://sourceware.org/pipermail/gdb-patches/2022-April/187310.html This commit does two things, first moving the setbuf calls out of gdb_readline_no_editing_callback so that we don't end up calling setbuf so often. Then, for MS-Windows hosts, we don't call setbuf for terminals, this appears to resolve the issues that have been reported. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=29002
2022-04-22gdb: handle bracketed-paste-mode and EOF correctlyAndrew Burgess1-12/+56
This commit replaces an earlier commit that worked around the issues reported in bug PR gdb/28833. The previous commit just implemented a work around in order to avoid the worst results of the bug, but was not a complete solution. The full solution was considered too risky to merge close to branching GDB 12. This improved fix has been applied after GDB 12 branched. See this thread for more details: https://sourceware.org/pipermail/gdb-patches/2022-March/186391.html This commit replaces this earlier commit: commit 74a159a420d4b466cc81061c16d444568e36740c Date: Fri Mar 11 14:44:03 2022 +0000 gdb: work around prompt corruption caused by bracketed-paste-mode Please read that commit for a full description of the bug, and why is occurs. In this commit I extend GDB to use readline's rl_deprep_term_function hook to call a new function gdb_rl_deprep_term_function. From this new function we can now print the 'quit' message, this replaces the old printing of 'quit' in command_line_handler. Of course, we only print 'quit' in gdb_rl_deprep_term_function when we are handling EOF, but thanks to the previous commit (to readline) we now know when this is. There are two aspects of this commit that are worth further discussion, the first is in the new gdb_rl_deprep_term_function function. In here I have used a scoped_restore_tmpl to disable the readline global variable rl_eof_found. The reason for this is that, in rl_deprep_terminal, readline will print an extra '\n' character before printing the escape sequence to leave bracketed paste mode. You might then think that in the gdb_rl_deprep_term_function function, we could simply print "quit" and rely on rl_deprep_terminal to print the trailing '\n'. However, rl_deprep_terminal only prints the '\n' when bracketed paste mode is on. If the user has turned this feature off, no '\n' is printed. This means that in gdb_rl_deprep_term_function we need to print "quit" when bracketed paste mode is on, and "quit\n" when bracketed paste mode is off. We could absolutely do that, no problem, but given we know how rl_deprep_terminal is implemented, it's easier (I think) to just temporarily clear rl_eof_found, this prevents the '\n' being printed from rl_deprep_terminal, and so in gdb_rl_deprep_term_function, we can now always print "quit\n" and this works for all cases. The second issue that should be discussed is backwards compatibility with older versions of readline. GDB can be built against the system readline, which might be older than the version contained within GDB's tree. If this is the case then the system readline might not contain the fixes needed to support correctly printing the 'quit' string. To handle this situation I have retained the existing code in command_line_handler for printing 'quit', however, this code is only used now if the version of readline we are using doesn't not include the required fixes. And so, if a user is using an older version of readline, and they have bracketed paste mode on, then they will see the 'quit' sting printed on the line below the prompt, like this: (gdb) quit I think this is the best we can do when someone builds GDB against an older version of readline. Using a newer version of readline, or the patched version of readline that is in-tree, will now give a result like this in all cases: (gdb) quit Which is what we want. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=28833
2022-04-22gdb: improved EOF handling when using readline 7Andrew Burgess1-1/+2
In this commit: commit a6b413d24ccc5d76179bab866834e11fd6fec294 Date: Fri Mar 11 14:44:03 2022 +0000 gdb: work around prompt corruption caused by bracketed-paste-mode a change was made to GDB to work around bug PR gdb/28833. The consequence of this work around is that, when bracketed paste mode is enabled in readline, and GDB is quit by sending EOF, then the output will look like this: (gdb) quit The ideal output, which is what we get when bracketed paste mode is off, is this: (gdb) quit The reason we need to make this change is explained in the original commit referenced above. What isn't mentioned in the above commit, is that the change that motivated this work around was only added in readline 8, older versions of readline don't require the change. In later commits in this series I will add a fix to GDB's in-tree copy of readline (this fix is back-ported from upstream readline), and then I will change GDB so that, when using the (patched) in-tree readline, we can have the ideal output in all cases. However, GDB can be built against the system readline. When this is done, and the system readline is version 8, then we will still have to use the work around (two line) style output. But, if GDB is built against the system readline, and the system readline is an older version 7 readline, then there's no reason why we can't have the ideal output, after all, readline 7 doesn't include the change that we need to work around. This commit changes GDB so that, when using readline 7 we get the ideal output in all cases. This change is trivial (a simple check against the readline version number) so I think this should be fine to include. For testing this commit, you need to configure GDB including the '--with-system-readline' flag, and build GDB on a system that uses readline 7, for example 'Ubuntu 18.04'. Then run the test 'gdb.base/eof-exit.exp', you should expect everything to PASS. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=28833
2022-03-29Unify gdb printf functionsTom Tromey1-3/+3
Now that filtered and unfiltered output can be treated identically, we can unify the printf family of functions. This is done under the name "gdb_printf". Most of this patch was written by script.
2022-03-29Unify gdb puts functionsTom Tromey1-2/+2
Now that filtered and unfiltered output can be treated identically, we can unify the puts family of functions. This is done under the name "gdb_puts". Most of this patch was written by script.
2022-03-29Change the pager to a ui_fileTom Tromey1-1/+2
This rewrites the output pager as a ui_file implementation. A new header is introduced to declare the pager class. The implementation remains in utils.c for the time being, because there are some static globals there that must be used by this code. (This could be cleaned up at some future date.) I went through all the text output in gdb to ensure that this change should be ok. There are a few cases: * Any existing call to printf_unfiltered is required to be avoid the pager. This is ensured directly in the implementation. * All remaining calls to the f*_unfiltered functions -- the ones that take an explicit ui_file -- either send to an unfiltered stream (e.g., gdb_stderr), which is obviously ok; or conditionally send to gdb_stdout I investigated all such calls by searching for: grep -e '\bf[a-z0-9_]*_unfiltered' *.[chyl] */*.[ch] | grep -v gdb_stdlog | grep -v gdb_stderr This yields a number of candidates to check. * The breakpoint _print_recreate family, and save_trace_state_variables. These are used for "save" commands and so are fine. * Things printing to a temporary stream. Obviously ok. * Disassembly selftests. * print_gdb_help - this is non-obvious, but ok because paging isn't yet enabled at this point during startup. * serial.c - doens't use gdb_stdout * The code in compile/. This is all printing to a file. * DWARF DIE dumping - doesn't reference gdb_stdout. * Calls to the _filtered form -- these are all clearly ok, because if they are using gdb_stdout, then filtering will still apply; and if not, then filtering never applied and still will not. Therefore, at this point, there is no longer any distinction between all the other _filtered and _unfiltered calls, and they can be unified. In this patch, take special note of the vfprintf_maybe_filtered and ui_file::vprintf change. This is one instance of the above idea, erasing the distinction between filtered and unfiltered -- in this part of the change, the "unfiltered_output" flag is never passe to cli_ui_out. Subsequent patches will go much further in this direction. Also note the can_emit_style_escape changes in ui-file.c. Checking against gdb_stdout or gdb_stderr was always a bit of a hack; and now it is no longer needed, because this is decision can be more fully delegated to the particular ui_file implementation. ui_file::can_page is removed, because this patch removed the only call to it. I think this is the main part of fixing PR cli/7234. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=7234
2022-03-29Only have one API for unfiltered outputTom Tromey1-5/+1
At the end of this series, the use of unfiltered output will be very restricted -- only places that definitely need it will use it. To this end, I thought it would be good to reduce the number of _unfiltered APIs that are exposed. This patch changes gdb so that only printf_unfiltered exists. (After this patch, the f* variants still exist as well, but those will be removed later.)
2022-03-28Switch gdb_stdlog to use timestamped_fileTom Tromey1-1/+1
Currently, timestamps for logging are done by looking for the use of gdb_stdlog in vfprintf_unfiltered. This seems potentially buggy, in that during logging or other redirects (like execute_fn_to_ui_file) we might have gdb_stdout==gdb_stdlog and so, conceivably, wind up with timestamps in a log when they were not desired. It seems better, instead, for timestamps to be a property of the ui_file itself. This patch changes gdb to use the new timestamped_file for gdb_stdlog where appropriate, and removes the special case from vfprintf_unfiltered. Note that this may somewhat change the output in some cases -- in particular, when going through execute_fn_to_ui_file (or the _string variant), timestamps won't be emitted. This could be fixed in those functions, but it wasn't clear to me whether this is really desirable. Note also that this changes the TUI to send gdb_stdlog to gdb_stderr. I imagine that the previous use of gdb_stdout here was inadvertent. (And in any case it probably doesn't matter.)
2022-03-16gdb: work around prompt corruption caused by bracketed-paste-modeAndrew Burgess1-1/+19
In this commit: commit b4f26d541aa7224b70d363932e816e6e1a857633 Date: Tue Mar 2 13:42:37 2021 -0700 Import GNU Readline 8.1 We imported readline 8.1 into GDB. As a consequence bug PR cli/28833 was reported. This bug spotted that, when the user terminated GDB by sending EOF (usually bound to Ctrl+d), the last prompt would become corrupted. Here's what happens, the user is sat at a prompt like this: (gdb) And then the user sends EOF (Ctrl+d), we now see this: quit) ... gdb terminates, and we return to the shell ... Notice the 'quit' was printed over the prompt. This problem is a result of readline 8.1 enabling bracketed paste mode by default. This problem is present in readline 8.0 too, but in that version of readline bracketed paste mode is off by default, so a user will not see the bug unless they specifically enable the feature. Bracketed paste mode is available in readline 7.0 too, but the bug is not present in this version of readline, see below for why. What causes this problem is how readline disables bracketed paste mode. Bracketed paste mode is a terminal feature that is enabled and disabled by readline emitting a specific escape sequence. The problem for GDB is that the escape sequence to disable bracketed paste mode includes a '\r' character at the end, see this thread for more details: https://lists.gnu.org/archive/html/bug-bash/2018-01/msg00097.html The change to add the '\r' character to the escape sequence used to disable bracketed paste mode was introduced between readline 7.0 and readline 8.0, this is why the bug would not occur when using older versions of readline (note: I don't know if its even possible to build GDB using readline 7.0. That really isn't important, I'm just documenting the history of this issue). So, the escape sequence to disable bracketed paste mode is emitted from the readline function rl_deprep_terminal, this is called after the user has entered a complete command and pressed return, or, if the user sends EOF. However, these two cases are slightly different. In the first case, when the user has entered a command and pressed return, the cursor will have moved to the next, empty, line, before readline emits the escape sequence to leave bracketed paste mode. The final '\r' character moves the cursor back to the beginning of this empty line, which is harmless. For the EOF case though, this is not what happens. Instead, the escape sequence to leave bracketed paste mode is emitted on the same line as the prompt. The final '\r' moves the cursor back to the start of the prompt line. This leaves us ready to override the prompt. It is worth noting, that this is not the intended behaviour of readline, in rl_deprep_terminal, readline should emit a '\n' character when EOF is seen. However, due to a bug in readline this does not happen (the _rl_eof_found flag is never set). This is the first readline bug that effects GDB. GDB prints the 'quit' message from command_line_handler (in event-top.c), this function is called (indirectly) from readline to process the complete command line, but also in the EOF case (in which case the command line is set to nullptr). As this is part of the callback to process a complete command, this is called after readline has disabled bracketed paste mode (by calling rl_deprep_terminal). And so, when bracketed paste mode is in use, rl_deprep_terminal leaves the cursor at the start of the prompt line (in the EOF case), and command_line_handler then prints 'quit', which overwrites the prompt. The solution to this problem is to print the 'quit' message earlier, before rl_deprep_terminal is called. This is easy to do by using the rl_deprep_term_function hook. It is this hook that usually calls rl_deprep_terminal, however, if we replace this with a new function, we can print the 'quit' string, and then call rl_deprep_terminal ourselves. This allows the 'quit' to be printed before rl_deprep_terminal is called. The problem here is that there is no way in rl_deprep_terminal to know if readline is processing EOF or not, and as a result, we don't know when we should print 'quit'. This is the second readline bug that effects GDB. Both of these readline issues are discussed in this thread: https://lists.gnu.org/archive/html/bug-readline/2022-02/msg00021.html The result of that thread was that readline was patched to address both of these issues. Now it should be easy to backport the readline fix to GDB's in tree copy of readline, and then change GDB to make use of these fixes to correctly print the 'quit' string. However, we are just about to branch GDB 12, and there is concern from some that changing readline this close to a new release is a risky idea, see this thread: https://sourceware.org/pipermail/gdb-patches/2022-March/186391.html So, this commit doesn't change readline at all. Instead, this commit is the smallest possible GDB change in order to avoid the prompt corruption. In this commit I change GDB to print the 'quit' string on the line after the prompt, but only when bracketed paste mode is on. This avoids the overwriting issue, the user sees this: (gdb) quit ... gdb terminates, and returns to the shell ... This isn't ideal, but is better than the existing behaviour. After GDB 12 has branched, we can backport the readline fix, and apply a real fix to GDB. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=28833
2022-02-16gdb: convert callback_handler_installed from int to boolAndrew Burgess1-3/+3
Simple int to bool conversion on callback_handler_installed in event-top.c. There should be no user visible changes after this commit.
2022-02-07gdb: unbuffer all input streams when not using readlineAndrew Burgess1-8/+11
This commit should fix PR gdb/28711. What's actually going on is pretty involved, and there's still a bit of the story that I don't understand completely, however, from my observed results, I think that the change I propose making here (or something very similar) is going to be needed. The original bug report involves using eclipse to drive gdb using mi commands. A separate tty is spun off in which to send gdb the mi commands, this tty is created using the new-ui command. The behaviour observed is that, given a particular set of mi commands being sent to gdb, we sometimes see an ESPIPE error from a lseek call, which ultimately results in gdb terminating. The problems all originate from gdb_readline_no_editing_callback in gdb/event-top.c, where we can (sometimes) perform calls to fgetc, and allow glibc to perform buffering on the FILE object being used. I say sometime, because, gdb_readline_no_editing_callback already includes a call to disable the glibc buffering, but this is only done if the input stream is not a tty. In our case the input stream is a tty, so the buffering is left in place. The first step to understanding why this problem occurs is to understand that eclipse sends multiple commands to gdb very quickly without waiting for and answer to each command, eclipse plans to collect all of the command results after sending all the commands to gdb. In fact, eclipse sends the commands to gdb that they appear to arrive in the gdb process as a single block of data. When reproducing this issue within the testsuite I find it necessary to send multiple commands using a single write call. The next bit of the story gets a little involved, and this is where my understanding is not complete. I can describe the behaviour that I observe, and (for me at least) I'm happy that what I'm seeing, if a little strange, is consistent. In order to fully understand what's going on I think I would likely need to dive into kernel code, which currently seems unnecessary given that I'm happy with the solution I'm proposing. The following description all relates to input from a tty in which I'm not using readline. I see the same problems either when using a new-ui tty, or with gdb's standard, non-readline, mi tty. Here's what I observe happening when I send multiple commands to gdb using a single write, if I send gdb this: command_1\ncommand_2\ncommand_3 then gdb's event loop will wake up (from its select) as it sees there is input available. We call into gdb_readline_no_editing_callback, where we call fgetc, glibc will do a single big read, and get back just: command_1\n that is, despite there being multiple lines of input available, I consistently get just a single line. From glibc a single character is returned from the fgetc call, and within gdb we accumulate characters, one at a time, into our own buffer. Eventually gdb sees the '\n' character, and dispatches the whole 'command_1' into gdb's command handler, which processes the command and prints the result. We then return to gdb_readline_no_editing_callback, which in turn returns to gdb's event loop where we re-enter the select. Inside the select we immediately see that there is more input waiting on the input stream, drop out of the select, and call back into gdb_readline_no_editing_callback. In this function we again call fgetc where glibc performs another big read. This time glibc gets: command_2\n that is, we once again get just a single line, despite there being a third line available. Just like the first command we copy the whole string, character by character into gdb's buffer, then handle the command. After handling the command we go to the event loop, enter, and then exit the select, and call back to the function gdb_readline_no_editing_callback. In gdb_readline_no_editing_callback we again call fgetc, this time glibc gets the string: command_3\n like before, we copy this to gdb's buffer and handle the command, then we return to the event loop. At this point the select blocks while we wait for more input to arrive. The important bit of this is that someone, somewhere is, it appears, taking care to split the incoming write into lines. My next experiment is to try something like: this_is_a_very_long_command\nshort_command\n However, I actually make 'this_is_a_very_long_command' very long, as in many hundreds of characters long. One way to do this is: echo xxxxxx.....xxxxx and just adding more and more 'x' characters as needed. What I'm aiming for is to have the first command be longer than glibc's internal read buffer, which, on my machine, is 1024 characters. However, for this discussion, lets imagine that glibc's buffer is just 8 characters (we can create just this situation by adding a suitable setbuf call into gdb_readline_no_editing_callback). Now, if I send gdb this data: abcdefghij\nkl\n The first read from glibc will get 'abcdefgh', that is a full 8 character buffer. Once gdb has copied these to its buffer we call fgetc again, and now glibc will get 'ij\n', that is, just like before, multiple lines are split at the '\n' character. The full command, which is now in gdb's buffer can be handled 'abcdefghij', after which we go (via the event loop) back to gdb_readline_no_editing_callback. Now we call fgetc, and glibc will get 'kl\n', which is then handled in the normal way. So far, so good. However, there is, apparently, one edge case where the above rules don't apply. If the '\n' character is the first character read from the kernel, then the incoming lines are not split up. So, given glibc's 8 character buffer, if I send gdb this: abcdefgh\nkl\n that is the first command is 8 characters plus a newline, then, on the first read (from within glibc) we get 'abcdefgh' in a single buffer. As there's no newline gdb calls fgetc again, and glibc does another large read, now we get: \nkl\n which doesn't follow the above pattern - the lines are not split into separate buffers! So, gdb reads the first character from glibc using fgetc, this is the newline. Now gdb has a complete command, and so the command is handled. We then return to the event loop and enter the select. The problem is that, as far as the kernel is concerned, there is no more input pending, it's all been read into glibc's buffer, and so the select doesn't return. The second command is basically stuck in glibc's buffer. If I send another command to gdb, or even just send an empty command (a lone newline) then the select returns, we call into gdb_readline_no_editing_callback, and now gdb sees the second command. OK, so the above is interesting, but it doesn't explain the ESPIPE error. Well, that's a slightly different, but related issue. The ESPIPE case will _only_ show up when using new-ui to create the separate tty for mi commands, and is a consequence of this commit: commit afe09f0b6311a4dd1a7e2dc6491550bb228734f8 Date: Thu Jul 18 17:20:04 2019 +0100 Fix for using named pipes on Windows Prior to this commit, the new-ui command would open the tty three times, once each for stdin, stderr, and stdout. After this commit we open the tty just once and reuse the FILE object for all three roles. Consider the problem case, where glibc has (unexpectedly) read the second command into its internal buffer. When we handle the first command we usually end up having to write something to the mi output stream. After the above commit the same FILE object represents both the input and output streams, so, when gdb tries to write to the FILE object, glibc spots that there is input pending within the input buffer, and so assumes that we have read ahead of where we should be in the input file. To correct for this glibc tries to do an lseek call to reposition the file offset of the output stream prior to writing to it. However, as the output stream is a tty, and seeking is not supported on a tty, this lseek call fails, this results in the ESPIPE, which ultimately causes gdb to terminate. So, now we understand why the ESPIPE triggers (which was what caused the gdb crash in the original bug report), and we also understand that sometime gdb will not handle the second command in a timely fashion (if the first command is just the wrong length). So, what to do about all this? We could revert the commit mentioned above (and implement its functionality another way). This would certainly resolve the ESPIPE issue, the buffered input would now only be on the input stream, the output stream would have no buffered input, and so glibc would never try to lseek, and so we'd never get the ESPIPE error. However, this only solves one of the two problems. We would still suffer from the problem where, if the first command is just the wrong length, the second command will not (immediately) get handled. The only solution I can see to this problem is to unbuffer the input stream. If glibc is not buffering the input, but instead, we read incoming data character by character from the kernel, then everything will be fine. As soon as we see the newline at the end of the first command we will handle the first command. As glibc will have no buffered input it will not be tempted to lseek, so no ESPIPE error. When we go have to the event loop there will be more data pending in the kernel, so the select will immediately return, and the second command will be processed. I'm tempted to suggest that we should move the unbuffering of the input stream out of gdb_readline_no_editing_callback and do it somewhere earlier, more like when we create the input streams. However, I've not done that in this commit for a couple of reasons: 1. By keeping the unbuffering in gdb_readline_no_editing_callback I'm making the smallest possible change that fixes the bug. Moving the unbuffering somewhere better can be done as a refactor later, if that 's felt to be important, 2. I don't think making repeated calls to unbuffer the input will have that much performance impact. We only make the unbuffer call once per call to gdb_readline_no_editing_callback, and, if the input stream is already unbuffered we'll return pretty quickly, so I don't see this as being massively costly, 3. Tom is currently doing lots of gdb stream management changes and I want to minimise the chances we'll conflict. So, this commit just changes gdb_readline_no_editing_callback to always unbuffer the input stream. The test for this issue sends two commands in a loop, with the first command growing bigger each time around the loop. I actually make the first command bigger by just adding whitespace to the front, as gdb still has to read the complete command (including whitespace) via glibc, so this is enough to trigger the bug. The original bug was reported when using a virtual machine, and in this situation we see this in the strace output: read(9, "70-var-info-path-expression var1.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1024) = 64 read(9, "\n71-var-info-path-expression var1.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n", 1024) = 67 I'm not completely sure what's going on here, but it appears that the kernel on the virtual machine is delivering the input to glibc slower than I see on my real hardware; glibc asks for 1024 bytes, but only gets 64 bytes the first time. In the second read we see the problem case, the first character is the newline, but then the entire second command is included. If I run this exact example on my real hardware then the first command would not be truncated at 64 bytes, instead, I'd expect to see the newline included in the first read, with the second command split into a second read. So, for testing, I check cases where the first command is just a few characters (starting at 8 character), all the way up to 2048 characters. Hopefully, this should mean we hit the problem case for most machine setups. The only last question relates to commit afe09f0b6311a4d that I mentioned earlier. That commit was intended to provide support for Microsoft named pipes: https://docs.microsoft.com/en-us/windows/win32/ipc/named-pipes I know next to nothing about this topic beyond a brief scan of the above link, but I think these windows named pipe are closer in behaviour to unix sockets than to unix named fifos. I am a little nervous that, after the above commit, we now use the same FILE for in, err, and out streams. In contrast, in a vanilla C program, I would expect different FILE objects for each stream. Still, I'm reluctant to revert the above commit (and provide the same functionality a different way) without a specific bug to point at, and, now that the streams are unbuffered, I expect a lot of the read and write calls are going straight to the kernel with minimal glibc involvement, so maybe it doesn't really matter. Anyway, I haven't touched the above patch, but it is something to keep in mind when working in this area. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=28711
2022-01-25Reduce explicit use of gdb_stdoutTom Tromey1-1/+1
In an earlier version of the pager rewrite series, it was important to audit unfiltered output calls to see which were truly necessary. This is no longer necessary, but it still seems like a decent cleanup to change calls to avoid explicitly passing gdb_stdout. That is, rather than using something like fprintf_unfiltered with gdb_stdout, the code ought to use plain printf_unfiltered instead. This patch makes this change. I went ahead and converted all the _filtered calls I could find, as well, for the same clarity.
2022-01-25Send some error output to gdb_stderrTom Tromey1-1/+1
This changes some code to send some error messages to gdb_stderr rather than gdb_stdout.
2022-01-01Automatic Copyright Year update after running gdb/copyright.pyJoel Brobecker1-1/+1
This commit brings all the changes made by running gdb/copyright.py as per GDB's Start of New Year Procedure. For the avoidance of doubt, all changes in this commits were performed by the script.
2021-10-03gdb: make string-like set show commands use std::string variableSimon Marchi1-7/+5
String-like settings (var_string, var_filename, var_optional_filename, var_string_noescape) currently take a pointer to a `char *` storage variable (typically global) that holds the setting's value. I'd like to "mordernize" this by changing them to use an std::string for storage. An obvious reason is that string operations on std::string are often easier to write than with C strings. And they avoid having to do any manual memory management. Another interesting reason is that, with `char *`, nullptr and an empty string often both have the same meaning of "no value". String settings are initially nullptr (unless initialized otherwise). But when doing "set foo" (where `foo` is a string setting), the setting now points to an empty string. For example, solib_search_path is nullptr at startup, but points to an empty string after doing "set solib-search-path". This leads to some code that needs to check for both to check for "no value". Or some code that converts back and forth between NULL and "" when getting or setting the value. I find this very error-prone, because it is very easy to forget one or the other. With std::string, we at least know that the variable is not "NULL". There is only one way of representing an empty string setting, that is with an empty string. I was wondering whether the distinction between NULL and "" would be important for some setting, but it doesn't seem so. If that ever happens, it would be more C++-y and self-descriptive to use optional<string> anyway. Actually, there's one spot where this distinction mattered, it's in init_history, for the test gdb.base/gdbinit-history.exp. init_history sets the history filename to the default ".gdb_history" if it sees that the setting was never set - if history_filename is nullptr. If history_filename is an empty string, it means the setting was explicitly cleared, so it leaves it as-is. With the change to std::string, this distinction doesn't exist anymore. This can be fixed by moving the code that chooses a good default value for history_filename to _initialize_top. This is ran before -ex commands are processed, so an -ex command can then clear that value if needed (what gdb.base/gdbinit-history.exp tests). Another small improvement, in my opinion is that we can now easily give string parameters initial values, by simply initializing the global variables, instead of xstrdup-ing it in the _initialize function. In Python and Guile, when registering a string-like parameter, we allocate (with new) an std::string that is owned by the param_smob (in Guile) and the parmpy_object (in Python) objects. This patch started by changing all relevant add_setshow_* commands to take an `std::string *` instead of a `char **` and fixing everything that failed to build. That includes of course all string setting variable and their uses. string_option_def now uses an std::string also, because there's a connection between options and settings (see add_setshow_cmds_for_options). The add_path function in source.c is really complex and twisted, I'd rather not try to change it to work on an std::string right now. Instead, I added an overload that copies the std:string to a `char *` and back. This means more copying, but this is not used in a hot path at all, so I think it is acceptable. Change-Id: I92c50a1bdd8307141cdbacb388248e4e4fc08c93 Co-authored-by: Lancelot SIX <lsix@lancelotsix.com>
2021-09-28gdb: use libbacktrace to create a better backtrace for fatal signalsAndrew Burgess1-40/+7
GDB recently gained the ability to print a backtrace when a fatal signal is encountered. This backtrace is produced using the backtrace and backtrace_symbols_fd API available in glibc. However, in order for this API to actually map addresses to symbol names it is required that the application (GDB) be compiled with -rdynamic, which GDB is not by default. As a result, the backtrace produced often looks like this: Fatal signal: Bus error ----- Backtrace ----- ./gdb/gdb[0x80ec00] ./gdb/gdb[0x80ed56] /lib64/libc.so.6(+0x3c6b0)[0x7fc2ce1936b0] /lib64/libc.so.6(__poll+0x4f)[0x7fc2ce24da5f] ./gdb/gdb[0x15495ba] ./gdb/gdb[0x15489b8] ./gdb/gdb[0x9b794d] ./gdb/gdb[0x9b7a6d] ./gdb/gdb[0x9b943b] ./gdb/gdb[0x9b94a1] ./gdb/gdb[0x4175dd] /lib64/libc.so.6(__libc_start_main+0xf3)[0x7fc2ce17e1a3] ./gdb/gdb[0x4174de] --------------------- This is OK if you have access to the exact same build of GDB, you can manually map the addresses back to symbols, however, it is next to useless if all you have is a backtrace copied into a bug report. GCC uses libbacktrace for printing a backtrace when it encounters an error. In recent commits I added this library into the binutils-gdb repository, and in this commit I allow this library to be used by GDB. Now (when GDB is compiled with debug information) the backtrace looks like this: ----- Backtrace ----- 0x80ee08 gdb_internal_backtrace ../../src/gdb/event-top.c:989 0x80ef0b handle_fatal_signal ../../src/gdb/event-top.c:1036 0x7f24539dd6af ??? 0x7f2453a97a5f ??? 0x154976f gdb_wait_for_event ../../src/gdbsupport/event-loop.cc:613 0x1548b6d _Z16gdb_do_one_eventv ../../src/gdbsupport/event-loop.cc:237 0x9b7b02 start_event_loop ../../src/gdb/main.c:421 0x9b7c22 captured_command_loop ../../src/gdb/main.c:481 0x9b95f0 captured_main ../../src/gdb/main.c:1353 0x9b9656 _Z8gdb_mainP18captured_main_args ../../src/gdb/main.c:1368 0x4175ec main ../../src/gdb/gdb.c:32 --------------------- Which seems much more useful. Use of libbacktrace is optional. If GDB is configured with --disable-libbacktrace then the libbacktrace directory will not be built, and GDB will not try to use this library. In this case GDB would try to use the old backtrace and backtrace_symbols_fd API. All of the functions related to writing the backtrace of GDB itself have been moved into the new files gdb/by-utils.{c,h}.
2021-08-11gdb: register SIGBUS, SIGFPE, and SIGABRT handlersAndrew Burgess1-1/+16
Register handlers for SIGBUS, SIGFPE, and SIGABRT. All of these signals are setup as fatal signals that will cause GDB to terminate. However, by passing these signals through the handle_fatal_signal function, a user can arrange to see a backtrace when GDB terminates (see maint set backtrace-on-fatal-signal). In normal use of GDB there should be no user visible changes after this commit. Only if GDB terminates with one of the above signals will GDB change slightly, potentially printing a backtrace before aborting. I've added new tests for SIGFPE, SIGBUS, and SIGABRT.
2021-08-11gdb: print backtrace on fatal SIGSEGVAndrew Burgess1-11/+129
This commit adds a new maintenance feature, the ability to print a (limited) backtrace if GDB dies due to a fatal signal. The backtrace is produced using the backtrace and backtrace_symbols_fd functions which are declared in the execinfo.h header, and both of which are async signal safe. A configure check has been added to check for these features, if they are not available then the new code is not compiled into GDB and the backtrace will not be printed. The motivation for this new feature is to aid in debugging GDB in situations where GDB has crashed at a users site, but the user is reluctant to share core files, possibly due to concerns about what might be in the memory image within the core file. Such a user might be happy to share a simple backtrace that was written to stderr. The production of the backtrace is on by default, but can switched off using the new commands: maintenance set backtrace-on-fatal-signal on|off maintenance show backtrace-on-fatal-signal Right now, I have hooked this feature in to GDB's existing handling of SIGSEGV only, but this will be extended to more signals in a later commit. One additional change I have made in this commit is that, when we decide GDB should terminate due to the fatal signal, we now raise the same fatal signal rather than raising SIGABRT. Currently, this is only effecting our handling of SIGSEGV. So, previously, if GDB hit a SEGV then we would terminate GDB with a SIGABRT. After this commit we will terminate GDB with a SIGSEGV. This feels like an improvement to me, we should still get a core dump, but in many shells, the user will see a more specific message once GDB exits, in bash for example "Segmentation fault" rather than "Aborted". Finally then, here is an example of the output a user would see if GDB should hit an internal SIGSEGV: Fatal signal: Segmentation fault ----- Backtrace ----- ./gdb/gdb[0x8078e6] ./gdb/gdb[0x807b20] /lib64/libpthread.so.0(+0x14b20)[0x7f6648c92b20] /lib64/libc.so.6(__poll+0x4f)[0x7f66484d3a5f] ./gdb/gdb[0x1540f4c] ./gdb/gdb[0x154034a] ./gdb/gdb[0x9b002d] ./gdb/gdb[0x9b014d] ./gdb/gdb[0x9b1aa6] ./gdb/gdb[0x9b1b0c] ./gdb/gdb[0x41756d] /lib64/libc.so.6(__libc_start_main+0xf3)[0x7f66484041a3] ./gdb/gdb[0x41746e] --------------------- A fatal error internal to GDB has been detected, further debugging is not possible. GDB will now terminate. This is a bug, please report it. For instructions, see: <https://www.gnu.org/software/gdb/bugs/>. Segmentation fault (core dumped) It is disappointing that backtrace_symbols_fd does not actually map the addresses back to symbols, this appears, in part, to be due to GDB not being built with -rdynamic as the manual page for backtrace_symbols_fd suggests, however, even when I do add -rdynamic to the build of GDB I only see symbols for some addresses. We could potentially look at alternative libraries to provide the backtrace (e.g. libunwind) however, the solution presented here, which is available as part of glibc is probably a good baseline from which we might improve things in future.
2021-08-11gdb: rename async_init_signals to gdb_init_signalsAndrew Burgess1-27/+14
The async_init_signals has, for some time, dealt with async and sync signals, so removing the async prefix makes sense I think. Additionally, as pointed out by Pedro: ..... The comments relating to SIGTRAP and SIGQUIT within this function are out of date. The comments for SIGTRAP talk about the signal disposition (SIG_IGN) being passed to the inferior, meaning the signal disposition being inherited by GDB's fork children. However, we now call restore_original_signals_state prior to forking, so the comment on SIGTRAP is redundant. The comments for SIGQUIT are similarly out of date, further, the comment on SIGQUIT talks about problems with BSD4.3 and vfork, however, we have not supported BSD4.3 for several years now. Given the above, it seems that changing the disposition of SIGTRAP is no longer needed, so I've deleted the signal() call for SIGTRAP. Finally, the header comment on the function now called gdb_init_signals was getting quite out of date, so I've updated it to (hopefully) better reflect reality. There should be no user visible change after this commit.
2021-08-11gdb: register signal handler after setting up event tokenAndrew Burgess1-3/+5
This commit fixes the smallest of small possible bug related to signal handling. If we look in async_init_signals we see code like this: signal (SIGQUIT, handle_sigquit); sigquit_token = create_async_signal_handler (async_do_nothing, NULL, "sigquit"); Then if we look in handle_sigquit we see code like this: mark_async_signal_handler (sigquit_token); signal (sig, handle_sigquit); Finally, in mark_async_signal_handler we have: async_handler_ptr->ready = 1; Where async_handler_ptr will be sigquit_token. What this means is that if a SIGQUIT arrive in async_init_signals after handle_sigquit has been registered, but before sigquit_token has been initialised, then GDB will most likely crash. The chance of this happening is tiny, but fixing this is trivial, just ensure we call create_async_signal_handler before calling signal, so lets do that. There are no tests for this. Trying to land a signal in the right spot is pretty hit and miss. I did try changing the current HEAD GDB like this: signal (SIGQUIT, handle_sigquit); raise (SIGQUIT); sigquit_token = create_async_signal_handler (async_do_nothing, NULL, "sigquit"); And confirmed that this did result in a crash, after my change I tried this: sigquit_token = create_async_signal_handler (async_do_nothing, NULL, "sigquit"); signal (SIGQUIT, handle_sigquit); raise (SIGQUIT); And GDB now starts up just fine. gdb/ChangeLog: * event-top.c (async_init_signals): For each signal, call signal only after calling create_async_signal_handler.
2021-08-11gdb: terminate upon receipt of SIGFPEAndrew Burgess1-24/+1
GDB's SIGFPE handling is broken, this is PR gdb/16505 and PR gdb/17891. We currently try to use an async event token to process SIGFPE. So, when a SIGFPE arrives the signal handler calls mark_async_signal_handler then returns, effectively ignoring the signal (for now). The intention is that later the event loop will see that the async token associated with SIGFPE has been marked and will call the async handler, which just throws an error. The problem is that SIGFPE is not safe to ignore. Ignoring a SIGFPE (unless it is generated artificially, e.g. by raise()) is undefined behaviour, after ignoring the signal on many targets we return to the instruction that caused the SIGFPE to be raised, which immediately causes another SIGFPE to be raised, we get stuck in an infinite loop. The behaviour is certainly true on x86-64. To view this behaviour I simply added some dummy code to GDB that performed an integer divide by zero, compiled this on x86-64 GNU/Linux, ran GDB and saw GDB hang. In this commit, I propose to remove all special handling of SIGFPE and instead just let GDB make use of the default SIGFPE action, that is, to terminate the process. The only user visible change here should be: - If a user sends a SIGFPE to GDB using something like kill, previously GDB would just print an error and remain alive, now GDB will terminate. This is inline with what happens if the user sends GDB a SIGSEGV from kill though, so I don't see this as an issue. - If a bug in GDB causes a real SIGFPE, previously the users GDB session would hang. Now the GDB session will terminate. Again, this is inline with what happens if GDB receives a SIGSEGV due to an internal bug. In bug gdb/16505 there is mention that it would be nice if GDB did more than just terminate when receiving a fatal signal. I haven't done that in this commit, but later commits will move in that direction. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=16505 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=17891
2021-04-22gdb/continuations: turn continuation functions into inferior methodsTankut Baris Aktemur1-1/+0
Turn continuations-related functions into methods of the inferior class. This is a refactoring. gdb/ChangeLog: 2021-04-22 Tankut Baris Aktemur <tankut.baris.aktemur@intel.com> * Makefile.in (COMMON_SFILES): Remove continuations.c. * inferior.c (inferior::add_continuation): New method, adapted from 'add_inferior_continuation'. (inferior::do_all_continuations): New method, adapted from 'do_all_inferior_continuations'. (inferior::~inferior): Clear the list of continuations directly. * inferior.h (class inferior) <continuations>: Rename into... <m_continuations>: ...this and make private. * continuations.c: Remove. * continuations.h: Remove. * event-top.c: Don't include "continuations.h". Update the users below. * inf-loop.c (inferior_event_handler) * infcmd.c (attach_command) (notice_new_inferior): Update.
2021-03-12Use RAII to set the per-thread SIGSEGV handlerChristian Biesinger1-2/+21
This avoids using a thread-local extern variable, which causes link errors on some platforms, notably Cygwin. But I think this is a better pattern even outside of working around linker bugs because it encapsulates direct access to the variable inside the class, instead of having a global extern variable. The cygwin link error is: cp-support.o: in function `gdb_demangle(char const*, int)': /home/Christian/binutils-gdb/obj/gdb/../../gdb/cp-support.c:1619:(.text+0x6472): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `TLS init function for thread_local_segv_handler' /home/Christian/binutils-gdb/obj/gdb/../../gdb/cp-support.c:1619:(.text+0x648b): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `TLS init function for thread_local_segv_handler' collect2: error: ld returned 1 exit status 2021-03-12 Christian Biesinger <cbiesinger@google.com> PR threads/27239 * cp-support.c: Use scoped_segv_handler_restore. * event-top.c (thread_local_segv_handler): Made static. (scoped_segv_handler_restore::scoped_segv_handler_restore): New function. (scoped_segv_handler_restore::~scoped_segv_handler_restore): New function. * event-top.h (class scoped_segv_handler_restore): New class. (thread_local_segv_handler): Removed.
2021-01-01Update copyright year range in all GDB filesJoel Brobecker1-1/+1
This commits the result of running gdb/copyright.py as per our Start of New Year procedure... gdb/ChangeLog Update copyright year range in copyright header of all GDB files.