aboutsummaryrefslogtreecommitdiff
path: root/gdb/top.c
AgeCommit message (Collapse)AuthorFilesLines
2023-03-09gdb, gdbserver, gdbsupport: fix whitespace issuesSimon Marchi1-1/+1
Replace spaces with tabs in a bunch of places. Change-Id: If0f87180f1d13028dc178e5a8af7882a067868b0
2023-02-24Don't use struct buffer in top.cTom Tromey1-14/+8
This changes top.c to use std::string rather than struct buffer. Like the event-top.c change, this is not completely ideal in that it requires a copy of the string.
2023-02-21gdb: add --with-curses to --configuration outputPhilippe Blain1-0/+10
'gdb --configuration' does not mention if GDB was built with curses. Since b5075fb68d4 (Rename to allow_tui_tests, 2023-01-08) it does show --enable-tui (or --disable-tui), but one might want to know if GDB was built with curses independently of the availability of the TUI. Since configure.ac uses AC_SEARCH_LIBS to check for the curses library, we do not get an automatically defined HAVE_LIBCURSES symbol in config.in. We do have symbols defined by AC_CHECK_HEADERS (HAVE_CURSES_H, etc.) but it would be cumbersome to use those in print_gdb_configuration because we would have to check for all 6 symbols corresponding the 6 headers listed. This would also increase the maintenance burden if support for other variations of curses are added. Instead, define 'HAVE_LIBCURSES' ourselves by adding an 'action-if-found' argument to AC_SEARCH_LIBS, and use it in print_gdb_configuration. While at it, remove the condition on 'ac_cv_search_waddstr' and set 'curses_found' directly in 'action-if-found'. Change-Id: Id90e3d73990e169cee51bcc3e1d52072cfacd5b8 Approved-By: Simon Marchi <simon.marchi@efficios.com>
2023-02-13gdb: 'show config' shows --with[out]-amd-dbgapiLancelot SIX1-0/+10
Ensure that the "show configuration" command and the "--configuration" command line switch shows if GDB was built with the AMDGPU support or not. This will be used in a later patch in this series. Approved-By: Simon Marchi <simon.marchi@efficios.com>
2023-01-13Rename to allow_tui_testsTom Tromey1-0/+10
This changes skip_tui_tests to invert the sense, and renames it to allow_tui_tests. It also rewrites this function to use the output of "gdb --configuration", and it adds a note about the state of the TUI to that output.
2023-01-01Update copyright year in help message of gdb, gdbserver, gdbreplayJoel Brobecker1-2/+2
This commit updates the copyright year displayed by gdb, gdbserver and gdbreplay's help message from 2022 to 2023, as per our Start of New Year procedure. The corresponding source files' copyright header are also updated accordingly.
2022-12-28Fix "set debug timestamp"Tom Tromey1-1/+1
PR cli/29945 points out that "set debug timestamp 1" stopped working -- this is a regression due to commit b8043d27 ("Remove a ui-related memory leak"). This patch fixes the bug and adds a regression test. I think this should probably be backported to the gdb 13 branch. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=29945
2022-12-21Fix compiling of top.cAndrew Pinski1-1/+0
When I moved my last patch forward, somehow I missed removing the #endif for the HAVE_LIBMPFR case. Committed as obvious after a quick build. gdb/ChangeLog: * top.c: Remove the extra #endif which was missed.
2022-12-21Use toplevel configure for GMP and MPFR for gdbAndrew Pinski1-8/+0
This patch uses the toplevel configure parts for GMP/MPFR for gdb. The only thing is that gdb now requires MPFR for building. Before it was a recommended but not required library. Also this allows building of GMP and MPFR with the toplevel directory just like how it is done for GCC. We now error out in the toplevel configure of the version of GMP and MPFR that is wrong. OK after GDB 13 branches? Build gdb 3 ways: with GMP and MPFR in the toplevel (static library used at that point for both) With only MPFR in the toplevel (GMP distro library used and MPFR built from source) With neither GMP and MPFR in the toplevel (distro libraries used) Changes from v1: * Updated gdb/README and gdb/doc/gdb.texinfo. * Regenerated using unmodified autoconf-2.69 Thanks, Andrew Pinski ChangeLog: * Makefile.def: Add configure-gdb dependencies on all-gmp and all-mpfr. * configure.ac: Split out MPC checking from MPFR. Require GMP and MPFR if the gdb directory exist. * Makefile.in: Regenerate. * configure: Regenerate. gdb/ChangeLog: PR bug/28500 * configure.ac: Remove AC_LIB_HAVE_LINKFLAGS for gmp and mpfr. Use GMPLIBS and GMPINC which is provided by the toplevel configure. * Makefile.in (LIBGMP, LIBMPFR): Remove. (GMPLIBS, GMPINC): Add definition. (INTERNAL_CFLAGS_BASE): Add GMPINC. (CLIBS): Exchange LIBMPFR and LIBGMP for GMPLIBS. * target-float.c: Make the code conditional on HAVE_LIBMPFR unconditional. * top.c: Remove code checking HAVE_LIBMPFR. * configure: Regenerate. * config.in: Regenerate. * README: Update GMP/MPFR section of the config options. * doc/gdb.texinfo: Likewise. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=28500
2022-12-19gdb: fix command lookup in execute_command ()Jan Vrany1-6/+2
Commit b5661ff2 ("gdb: fix possible use-after-free when executing commands") used lookup_cmd_exact () to lookup command again after its execution to avoid possible use-after-free error. However this change broke test gdb.base/define.exp which defines a post-hook for subcommand ("target testsuite"). In this case, lookup_cmd_exact () returned NULL because there's no command 'testsuite' in top-level commands. This commit fixes this case by looking up the command again using the original command line via lookup_cmd (). Approved-By: Simon Marchi <simon.marchi@efficios.com>
2022-12-15gdb: remove static buffer in command_line_inputSimon Marchi1-26/+13
[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-12gdb: fix possible use-after-free when executing commandsJan Vrany1-2/+12
In principle, `execute_command()` does following: struct cmd_list_element *c; c = lookup_cmd ( ... ); ... /* If this command has been pre-hooked, run the hook first. */ execute_cmd_pre_hook (c); ... /* ...execute the command `c` ...*/ ... execute_cmd_post_hook (c); This may lead into use-after-free error. Imagine the command being executed is a user-defined Python command that redefines itself. In that case, struct `cmd_list_element` pointed to by `c` is deallocated during its execution so it is no longer valid when post hook is executed. To fix this case, this commit looks up the command once again after it is executed to get pointer to (possibly newly allocated) `cmd_list_element`.
2022-10-10Change GDB to use frame_info_ptrTom Tromey1-1/+1
This changes GDB to use frame_info_ptr instead of frame_info * The substitution was done with multiple sequential `sed` commands: sed 's/^struct frame_info;/class frame_info_ptr;/' sed 's/struct frame_info \*/frame_info_ptr /g' - which left some issues in a few files, that were manually fixed. sed 's/\<frame_info \*/frame_info_ptr /g' sed 's/frame_info_ptr $/frame_info_ptr/g' - used to remove whitespace problems. The changed files were then manually checked and some 'sed' changes undone, some constructors and some gets were added, according to what made sense, and what Tromey originally did Co-Authored-By: Bruno Larsen <blarsen@redhat.com> Approved-by: Tom Tomey <tom@tromey.com>
2022-08-31Use member initialization in 'struct ui'Tom Tromey1-11/+2
This changes 'struct ui' to use member initialization. This is simpler to understand.
2022-08-31Use ui_out_redirect_pop in more placesTom Tromey1-2/+1
This changes ui_out_redirect_pop to also perform the redirection, and then updates several sites to use this, rather than explicit redirects.
2022-08-31Free ui::line_bufferTom Tromey1-0/+2
A ui initializes its line_buffer, but never calls buffer_free on it. This patch fixes the oversight. I found this by inspection.
2022-08-19Remove two initialization functionsTom Tromey1-16/+7
I noticed a couple of initialization functions that aren't really needed, and that currently require explicit calls in gdb_init. This patch removes these functions, simplifying gdb a little. Regression tested on x86-64 Fedora 34.
2022-07-22Change target_ops::async to accept boolTom Tromey1-2/+2
This changes the parameter of target_ops::async from int to bool. Regression tested on x86-64 Fedora 34.
2022-07-18Replace input_interactive_p with a methodTom Tromey1-9/+9
This replaces the global input_interactive_p function with a new method ui::input_interactive_p.
2022-04-24gdb: move setbuf calls out of gdb_readline_no_editing_callbackAndrew Burgess1-0/+39
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-0/+1
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-18gdbsupport: make gdb_abspath return an std::stringSimon Marchi1-8/+2
I'm trying to switch these functions to use std::string instead of char arrays, as much as possible. Some callers benefit from it (can avoid doing a copy of the result), while others suffer (have to make one more copy). Change-Id: Iced49b8ee2f189744c5072a3b217aab5af17a993
2022-04-03gdb: fix gdb_print -> gdb_printf typoSimon Marchi1-1/+1
This caused a build failure with !CXX_STD_THREAD. Change-Id: I30f0c89c43a76f85c0db34809192644fa64a9d18
2022-03-31Style URLs in GDB outputTom Tromey1-4/+10
I noticed that GDB will display URLs in a few spots. This changes them to be styled. Originally I thought I'd introduce a new "url" style, but there aren't many places to use this, so I just reused filename styling instead. This patch also changes the debuginfod URL list to be printed one URL per line. I think this is probably a bit easier to read.
2022-03-31gdb: fix use of fprintf_filtered in top.cSimon Marchi1-2/+2
A race condition in how patches were pushed causes this build failure: CXX top.o /home/simark/src/binutils-gdb/gdb/top.c: In function ‘void print_gdb_configuration(ui_file*)’: /home/simark/src/binutils-gdb/gdb/top.c:1622:3: error: ‘fprintf_filtered’ was not declared in this scope; did you mean ‘printf_unfiltered’? 1622 | fprintf_filtered (stream, _("\ | ^~~~~~~~~~~~~~~~ fprintf_filtered has been removed, gdb_printf must be used now. Fix this. Change-Id: I6a172ba0d53dab2e7cc43ed0ed2696c82925245b
2022-03-31* gdb/top.c (print_gdb_configuration): Announce --enable-threading.Eli Zaretskii1-0/+10
This includes the reporting of --enable/disable-threading as part of the GDB configuration description.
2022-03-30Consolidate definition of current_directoryTom Tromey1-4/+0
I noticed that both gdbserver and gdb define current_directory. However, as it is referenced by gdbsupport, it seemed better to define it there as well. This patch also moves the declaration to pathstuff.h. Tested by rebuilding.
2022-03-29Remove unnecessary calls to wrap_here and gdb_flushTom Tromey1-1/+0
Various spots in gdb currently know about the wrap buffer, and so are careful to call wrap_here to be certain that all output has been flushed. Now that the pager is just an ordinary stream, this isn't needed, and a simple call to gdb_flush is enough. Similarly, there are places where gdb prints to gdb_stderr, but first flushes gdb_stdout. stderr_file already flushes gdb_stdout, so these aren't needed.
2022-03-29Unify gdb printf functionsTom Tromey1-104/+104
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-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-6/+2
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-14Replace deprecated_target_wait_hook by observersPatrick Monnerat1-7/+0
Commit b60cea7 (Make target_wait options use enum flags) broke deprecated_target_wait_hook usage: there's a commit comment telling this hook has not been converted. Rather than trying to mend it, this patch replaces the hook by two target_wait observers: target_pre_wait (ptid_t ptid) target_post_wait (ptid_t event_ptid) Upon target_wait entry, target_pre_wait is notified with the ptid passed to target_wait. Upon exit, target_post_wait is notified with the event ptid returned by target_wait. Should an exception occur, event_ptid is null_ptid. This change benefits to Insight (out-of-tree): there's no real use of the late hook in gdb itself.
2022-01-26Always call the wrap_here methodTom Tromey1-1/+1
This changes all existing calls to wrap_here to call the method on the appropriate ui_file instead. The choice of ui_file is determined by context.
2022-01-26Convert wrap_here to use integer parameterTom Tromey1-1/+1
I think it only really makes sense to call wrap_here with an argument consisting solely of spaces. Given this, it seemed better to me that the argument be an int, rather than a string. This patch is the result. Much of it was written by a script.
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-05Use filtered output in ordinary commandsTom Tromey1-2/+2
Many otherwise ordinary commands choose to use unfiltered output rather than filtered. I don't think there's any reason for this, so this changes many such commands to use filtered output instead. Note that complete_command is not touched due to a comment there explaining why unfiltered output is believed to be used.
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.
2022-01-01Update Copyright Year in gdb, gdbserver and gdbreplay version outputJoel Brobecker1-1/+1
This commit changes the copyright year printed by gdb, gdbserver and gdbreplay when printing the tool's version.
2021-10-09[gdb] Make execute_command_to_string return string on throwTom de Vries1-7/+30
The pattern for using execute_command_to_string is: ... std::string output; output = execute_fn_to_string (fn, term_out); ... This results in a problem when using it in a try/catch: ... try { output = execute_fn_to_string (fn, term_out) } catch (const gdb_exception &e) { /* Use output. */ } ... If an expection was thrown during execute_fn_to_string, then the output remains unassigned, while it could be worthwhile to known what output was generated by gdb before the expection was thrown. Fix this by returning the string using a parameter instead: ... execute_fn_to_string (output, fn, term_out) ... Also add a variant without string parameter, to support places where the function is used while ignoring the result: ... execute_fn_to_string (fn, term_out) ... Tested on x86_64-linux.
2021-10-05gdb/python: add a new gdb_exiting eventAndrew Burgess1-2/+4
Add a new event, gdb.events.gdb_exiting, which is called once GDB decides it is going to exit. This event is not triggered in the case that GDB performs a hard abort, for example, when handling an internal error and the user decides to quit the debug session, or if GDB hits an unexpected, fatal, signal. This event is triggered if the user just types 'quit' at the command prompt, or if GDB is run with '-batch' and has processed all of the required commands. The new event type is gdb.GdbExitingEvent, and it has a single attribute exit_code, which is the value that GDB is about to exit with. The event is triggered before GDB starts dismantling any of its own internal state, so, my expectation is that most Python calls should work just fine at this point. When considering this functionality I wondered about using the 'atexit' Python module. However, this is triggered when the Python environment is shut down, which is done from a final cleanup. At this point we don't know for sure what other GDB state has already been cleaned up.
2021-10-03gdb: make string-like set show commands use std::string variableSimon Marchi1-59/+53
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-30gdbsupport: make gdb_open_cloexec return scoped_fdSimon Marchi1-5/+3
Make gdb_open_cloexec return a scoped_fd, to encourage using automatic management of the file descriptor closing. Except in the most trivial cases, I changed the callers to just release the fd, which retains their existing behavior. That will allow the transition to using scoped_fd more to go gradually, one caller at a time. Change-Id: Ife022b403f96e71d5ebb4f1056ef6251b30fe554
2021-09-09[gdb/testsuite] Reimplement gdb.gdb/complaints.exp as unittestTom de Vries1-8/+23
When building gdb with "-Wall -O2 -g -flto=auto", I run into: ... (gdb) call clear_complaints()^M No symbol "clear_complaints" in current context.^M (gdb) FAIL: gdb.gdb/complaints.exp: clear complaints ... The problem is that lto has optimized away the clear_complaints function and consequently the selftest doesn't work. Fix this by reimplementing the selftest as a unit test. Factor out two new functions: - void execute_fn_to_ui_file (struct ui_file *file, std::function<void(void)> fn); - std::string execute_fn_to_string (std::function<void(void)> fn, bool term_out); and use the latter to capture the complaints output. Tested on x86_64-linux.
2021-08-11gdb: rename async_init_signals to gdb_init_signalsAndrew Burgess1-1/+1
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-05-27gdb: make add_setshow commands return set_show_commandsSimon Marchi1-1/+1
Some add_set_show commands return a single cmd_list_element, the one for the "set" command. A subsequent patch will need to access the show command's cmd_list_element as well. Change these functions to return a new structure type that holds both pointers. I initially only modified add_setshow_boolean_cmd (the one I needed), but I think it's better to change the whole chain to keep everything in sync. gdb/ChangeLog: * command.h (set_show_commands): New. (add_setshow_enum_cmd, add_setshow_auto_boolean_cmd, add_setshow_boolean_cmd, add_setshow_filename_cmd, add_setshow_string_cmd, add_setshow_string_noescape_cmd, add_setshow_optional_filename_cmd, add_setshow_integer_cmd, add_setshow_uinteger_cmd, add_setshow_zinteger_cmd, add_setshow_zuinteger_cmd, add_setshow_zuinteger_unlimited_cmd): Return set_show_commands. Adjust callers. * cli/cli-decode.c (add_setshow_cmd_full): Return set_show_commands, remove result parameters, adjust callers. Change-Id: I17492b01b76002d09effc84830f9c6db26f1db7a
2021-05-17gdb: add cmd_list_element::is_command_class_helpSimon Marchi1-1/+1
Same idea as the previous patches, but for whether a command is a "command class help" command. I think this one is particularly useful, because it's not obvious when reading code what "c->func == NULL" means. Remove the cmd_func_p function, which does kind of the same thing as cmd_list_element::is_command_class_help (except it doesn't give a clue about the semantic of a NULL func value). gdb/ChangeLog: * cli/cli-decode.h (cmd_list_element) <is_command_class_help>: New, use it. * command.h (cmd_func_p): Remove. * cli/cli-decode.c (cmd_func_p): Remove. Change-Id: I521a3e1896dc93a5babe1493d18f5eb071e1b3b7
2021-05-17gdb: add cmd_list_element::is_prefixSimon Marchi1-1/+1
Same idea as the previous patch, but for prefix instead of alias. gdb/ChangeLog: * cli/cli-decode.h (cmd_list_element) <is_prefix>: New, use it. Change-Id: I76a9d2e82fc8d7429904424674d99ce6f9880e2b
2021-05-17gdb: rename cmd_list_element::prefixlist to subcommandsSimon Marchi1-2/+2
While browsing this code, I found the name "prefixlist" really confusing. I kept reading it as "list of prefixes". Which it isn't: it's a list of sub-commands, for a prefix command. I think that renaming it to "subcommands" would make things clearer. gdb/ChangeLog: * Rename "prefixlist" parameters to "subcommands" throughout. * cli/cli-decode.h (cmd_list_element) <prefixlist>: Rename to... <subcommands>: ... this. * cli/cli-decode.c (lookup_cmd_for_prefixlist): Rename to... (lookup_cmd_with_subcommands): ... this. Change-Id: I150da10d03052c2420aa5b0dee41f422e2a97928
2021-05-12gdb: generate the prefix name for prefix commands on demandMarco Barisione1-3/+7
Previously, the prefixname field of struct cmd_list_element was manually set for prefix commands. This seems verbose and error prone as it required every single call to functions adding prefix commands to specify the prefix name while the same information can be easily generated. Historically, this was not possible as the prefix field was null for many commands, but this was fixed in commit 3f4d92ebdf7f848b5ccc9e8d8e8514c64fde1183 by Philippe Waroquiers, so we can rely on the prefix field being set when generating the prefix name. This commit also fixes a use after free in this scenario: * A command gets created via Python (using the gdb.Command class). The prefix name member is dynamically allocated. * An alias to the new command is created. The alias's prefixname is set to point to the prefixname for the original command with a direct assignment. * A new command with the same name as the Python command is created. * The object for the original Python command gets freed and its prefixname gets freed as well. * The alias is updated to point to the new command, but its prefixname is not updated so it keeps pointing to the freed one. gdb/ChangeLog: * command.h (add_prefix_cmd): Remove the prefixname argument as it can now be generated automatically. Update all callers. (add_basic_prefix_cmd): Ditto. (add_show_prefix_cmd): Ditto. (add_prefix_cmd_suppress_notification): Ditto. (add_abbrev_prefix_cmd): Ditto. * cli/cli-decode.c (add_prefix_cmd): Ditto. (add_basic_prefix_cmd): Ditto. (add_show_prefix_cmd): Ditto. (add_prefix_cmd_suppress_notification): Ditto. (add_prefix_cmd_suppress_notification): Ditto. (add_abbrev_prefix_cmd): Ditto. * cli/cli-decode.h (struct cmd_list_element): Replace the prefixname member variable with a method which generates the prefix name at runtime. Update all code reading the prefix name to use the method, and remove all code setting it. * python/py-cmd.c (cmdpy_destroyer): Remove code to free the prefixname member as it's now a method. (cmdpy_function): Determine if the command is a prefix by looking at prefixlist, not prefixname.