Commit Graph

660 Commits

Author SHA1 Message Date
Tom de Vries
7fd46e6b2a [gdb] Eliminate catch(...) in execute_fn_to_string
Remove duplicate code in execute_fn_to_string using SCOPE_EXIT.

Tested on aarch64-linux.

Approved-By: Tom Tromey <tom@tromey.com>
2024-09-24 13:50:19 +02:00
Andrew Burgess
88aad97c21 gdb: add overloads of gdb_abspath
Add two overloads of gdb_abspath, one which takes std::string and one
which takes gdb::unique_xmalloc_ptr<char>, then make use of these
overloads throughout GDB and gdbserver.

There should be no user visible changes after this commit.

Approved-By: Tom Tromey <tom@tromey.com>
2024-06-27 15:15:25 +01:00
Simon Marchi
05d9d66d92 gdb: remove unused includes in utils.h
Remove some includes reported as unused by clangd.  Add some includes in
other files that were previously relying on the transitive include.

Change-Id: Ibdd0a998b04d21362a20d0ca8e5267e21e2e133e
2024-05-30 22:43:52 -04:00
Tom Tromey
ac38857074 Remove unnecessary block from execute_fn_to_ui_file
I noticed that execute_fn_to_ui_file has an extra, unnecessary block.
This patch removes it.
2024-05-18 11:00:10 -06:00
Tom Tromey
f2e4bd45d9 Remove gdb_stdtargerr
This patch removes gdb_stdtargerr.  There doesn't seem to be a need
for this -- it is always the same as stdtarg, and (I believe) has been
for many years.

Approved-By: Andrew Burgess <aburgess@redhat.com>
2024-05-17 10:01:13 -06:00
Simon Marchi
5b9707eb87 gdb: remove gdbcmd.h
Most files including gdbcmd.h currently rely on it to access things
actually declared in cli/cli-cmds.h (setlist, showlist, etc).  To make
things easy, replace all includes of gdbcmd.h with includes of
cli/cli-cmds.h.  This might lead to some unused includes of
cli/cli-cmds.h, but it's harmless, and much faster than going through
the 170 or so files by hand.

Change-Id: I11f884d4d616c12c05f395c98bbc2892950fb00f
Approved-By: Tom Tromey <tom@tromey.com>
2024-04-25 12:59:02 -04:00
Simon Marchi
824dd26e97 gdb: move execute function declarations from gdbcmd.h to top.h
These functions are implemented in top.c, move their declarations to
top.h.

Change-Id: I8893ef91d955156a6530734fefe8002d78c3e5fc
Approved-By: Tom Tromey <tom@tromey.com>
2024-04-25 12:58:49 -04:00
Eli Zaretskii
6a2dbb742d Remove excess whitespace from doc strings of some commands
I've noticed that doc strings of some commands, like "set cwd"
and  "set inferior-tty", have some excess whitespace, which
makes them display with unexpected indentation, at least in a
Windows command prompt window.  This patch fixes that.

* gdb/linux-nat.c (_initialize_linux_nat):
* gdb/riscv-tdep.c (riscv_insn):
* gdb/top.c (quit_force):
* gdb/infcmd.c (_initialize_infcmd): Remove excess whitespace.
2024-04-16 19:13:39 +03:00
Simon Marchi
18d2988e5d gdb, gdbserver, gdbsupport: remove includes of early headers
Now that defs.h, server.h and common-defs.h are included via the
`-include` option, it is no longer necessary for source files to include
them.  Remove all the inclusions of these files I could find.  Update
the generation scripts where relevant.

Change-Id: Ia026cff269c1b7ae7386dd3619bc9bb6a5332837
Approved-By: Pedro Alves <pedro@palves.net>
2024-03-26 21:13:22 -04:00
Andrew Burgess
ec483c2344 gdb: move more completion setup into completer.c
Move more setup of the readline global state relating to tab
completion into completer.c out of top.c.

Lots of the readline setup is done in init_main (top.c).  This commit
moves those bits of initialisation that relate to completion, and
which are only set the one time, into completer.c.  This does mean
that readline initialisation is now done in multiple locations, some
in init_main (top.c) and some in completer.c, but I think this is OK.
The work done in init_main is the general readline setup.

I think making static what can be made static, and having it all in
one file, makes things easier to reason about.  So I'm OK with having
this split initialisation.

The only completion related thing which is still setup in top.c is
rl_completion_display_matches_hook.  I've left this where it is for
now as rl_completion_display_matches_hook is also updated in the tui
code, and the display hook functions are not in completer.c anyway, so
moving this initialisation to completer.c would not allow anything
else to be made static.

There should be no user visible changes after this commit.
2024-03-25 17:47:44 +00:00
Andrew Burgess
5792be9244 gdb: fix bug where quote characters would become nullptr
In gdb_completion_word_break_characters_throw, after calling
complete_line_internal, if the completion function chose to use a
custom word point then we set rl_completer_quote_characters to NULL.

However, nowhere do we set rl_completer_quote_characters back to its
default value, which is setup in init_main (top.c).

An example of something that uses a custom word point for its
completion is 'thread apply all ...'.

An example of something that relies on rl_completer_quote_characters
would be completion of a quoted filename that contains white space.

Consider this shell and GDB session.  The <TAB> markers indicate where
I've used tab to trigger completion:

  $ mkdir /tmp/aaa\ bbb
  $ touch /tmp/aaa\ bbb/xx\ 11
  $ touch /tmp/aaa\ bbb/xx\ 22
  $ gdb -q
  (gdb) file '/tmp/aaa bbb/xx<TAB><TAB>
  xx 11  xx 22
  (gdb) thread apply all hel<TAB>
  (gdb) thread apply all help
  (gdb) file '/tmp/aaa bbb/xx<TAB><TAB>

First I create a directory structure which uses white space within
file and directory names.  Then within GDB I use the 'file' command
and use a single quote to quote the filename.  When I tab complete GDB
correctly offers the two files within the directory '/tmp/aaa bbb/'.

This works because rl_completer_quote_characters contains the single
quote, and so readline knows that it is trying to complete the string
that starts after the single quote: /tmp/aaa bbb/xx

Next I invoke the completer for the 'thread apply all' command, to do
this I type 'thread apply all hel' and hit tab, this expands to the
one completion 'thread apply all help'.  We can run this command or
not, it doesn't matter (there are no threads, so we'll get no output).

Now I repeat the original 'file' completion.  This time though I don't
get offered any completions.

The reason is that the 'thread apply all' completer set
rl_completer_quote_characters to nullptr.  Now, when readline tries to
figure out the word to complete it doesn't see the single quote as the
start of a quoted word, so instead readline falls back to the word
break characters, and in this case spots the white space.  As a result
readline tries to complete the string 'bbb/xx' which obviously doesn't
have any completions.

By setting rl_completer_quote_characters each time completion is
invoked this problem is resolved and the second 'file' command
completes as expected.

I've extended gdb.base/filename-completion.exp to also test with
quoted filenames, and added a 'thread apply all' completion at the
start to expose this bug.

As setting of rl_completer_quote_characters is now all done in the
completer.c file the function get_gdb_completer_quote_characters()
could be made static.  However, as this function is only used one time
to initialise rl_completer_quote_characters, I've instead just deleted
get_gdb_completer_quote_characters() and used
gdb_completer_quote_characters directly.
2024-03-25 17:47:43 +00:00
Tom Tromey
ec471b627a Change finalize_values into a final cleanup
This removes finalize_values in favor of adding a new final cleanup.
This is safe now that extension languages are explicitly shut down.
2024-02-27 10:30:29 -07:00
Tom Tromey
beadf91284 Add extension_language_ops::shutdown
Right now, Python is shut down via a final cleanup.  However, it seems
to me that it is better for extension languages to be shut down
explicitly, after all the ordinary final cleanups are run.  The main
reason for this is that a subsequent patch adds another case like
finalize_values; and rather than add a series of workarounds for
Python shutdown, it seemed better to let these be done via final
cleanups, and then have Python shutdown itself be the special case.
2024-02-27 10:30:29 -07:00
Aditya Vidyadhar Kamath
49346fa794 Fix AIX build break.
A recent commit broke AIX build. The thread_local type defined functions
were being considered a weak symbol and hence while creating the binary these
symbols were not visible.

This patch is a fix for the same.
2024-01-31 17:30:25 -07:00
Andrew Burgess
1d506c26d9 Update copyright year range in header of all files managed by GDB
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.
2024-01-12 15:49:57 +00:00
Tom Tromey
54b815ddb4 Refactor complaint thread-safety approach
This patch changes the way complaint works in a background thread.
The new approach requires installing a complaint interceptor in each
worker, and then the resulting complaints are treated as one of the
results of the computation.  This change is needed for a subsequent
patch, where installing a complaint interceptor around a parallel-for
is no longer a viable approach.
2024-01-08 18:40:21 -07:00
Simon Marchi
4133662031 gdb: pass address_space to target dcache functions
A simple refactor to make the reference to current_program_space bubble
up one level.  No behavior changes expected.

Change-Id: I237cf2f45ae73c35bcb433ce40e3c03cef6b87e2
2023-11-17 20:03:05 +00:00
Thiago Jung Bauermann
740ce35025 gdb/configure.ac: Add option --with-additional-debug-dirs
If you want to install GDB in a custom prefix, have it look for debug info
in that prefix but also in the distro's default location (typically,
/usr/lib/debug) and run the GDB testsuite before doing "make install", you
have a bit of a problem:

Configuring GDB with '--prefix=$PREFIX' sets the GDB 'debug-file-directory'
parameter to $PREFIX/lib/debug.  Unfortunately this precludes GDB from
looking for distro-installed debug info in /usr/lib/debug.  For regular GDB
use you could set debug-file-directory to $PREFIX:/usr/lib/debug in
$PREFIX/etc/gdbinit so that GDB will look in both places, but if you want
to run the testsuite then that doesn't help because in that case GDB runs
with the '-nx' option.

There's the configure option '--with-separate-debug-dir' to set the default
value for 'debug-file-directory', but it accepts only one directory and not
a list.  I considered modifying it to accept a list, but it's not obvious
how to do that because its value is also used by BFD, as well as processed
for "relocatability".

I thought it was simpler to add a new option to specify a list of
additional directories that will be appended to the debug-file-directory
setting.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Approved-By: Tom Tromey <tom@tromey.com>
2023-10-05 22:58:11 -03:00
Tom Tromey
0128542673 Use string_file::release in some places
I found a few spots like:

    string_file f;
    std::string x = f.string ();

However, string_file::string returns a 'const std::string &'...  so it
seems to me that this must be copying the string (? I find it hard to
reason about this in C++).

This patch changes these spots to use release() instead, which moves
the string.

Reviewed-by: Keith Seitz <keiths@redhat.com>
Reviewed-by: Lancelot Six <lancelot.six@amd.com>
2023-09-26 06:56:27 -06:00
Tom de Vries
87c9b0289d [gdb/tui] Fix secondary prompt
With CLI, a session defining a command looks like:
...
(gdb) define foo
Type commands for definition of "foo".
End with a line saying just "end".
>bar
>end
(gdb)
...

With TUI however, we get the same secondary prompts, and type the same, but
are left with:
...
(gdb) define foo
Type commands for definition of "foo".
End with a line saying just "end".
(gdb)
...

Fix this by calling tui_inject_newline_into_command_window in
gdb_readline_wrapper_line, as is done in tui_command_line_handler.

Tested on x86_64-linux.

PR tui/30636
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30636
2023-07-26 13:31:53 +02:00
Tom de Vries
37d9880d65 [gdb] Mention --with/without-system-readline for --configuration
Simon reported that the new test-case gdb.tui/pr30056.exp fails with system
readline.

This is because the test-case requires a fix in readline that's present in our
in-repo copy of readline, but most likely not in any system readline yet.

Fix this by:
- mentioning --with-system-readline or --without-system-readline in the
  configuration string.
- adding a new proc with_system_readline that makes this information available
  in the testsuite.
- using this in test-case gdb.tui/pr30056.exp to declare it unsupported for
  --with-system-readline.

Tested on x86_64-linux.

Reported-By: Simon Marchi <simon.marchi@efficios.com>
Approved-By: Simon Marchi <simon.marchi@efficios.com>
2023-05-30 17:49:31 +02:00
Jan Vrany
b69378ced6 gdb: fix post-hook execution for remote targets
Commit b5661ff2 ("gdb: fix possible use-after-free when
executing commands") attempted to fix possible use-after-free
in case command redefines itself.

Commit 37e5833d ("gdb: fix command lookup in execute_command ()")
updated the previous fix to handle subcommands as well by using the
original command string to lookup the command again after its execution.

This fixed the test in gdb.base/define.exp but it turned out that it
does not work (at least) for "target remote" and "target extended-remote".

The problem is that the command buffer P passed to execute_command ()
gets overwritten in dont_repeat () while executing "target remote"
command itself:

	#0  dont_repeat () at top.c:822
	#1  0x000055555730982a in target_preopen (from_tty=1) at target.c:2483
	#2  0x000055555711e911 in remote_target::open_1 (name=0x55555881c7fe ":1234", from_tty=1, extended_p=0)
	    at remote.c:5946
	#3  0x000055555711d577 in remote_target::open (name=0x55555881c7fe ":1234", from_tty=1) at remote.c:5272
	#4  0x00005555573062f2 in open_target (args=0x55555881c7fe ":1234", from_tty=1, command=0x5555589d0490)
	    at target.c:853
	#5  0x0000555556ad22fa in cmd_func (cmd=0x5555589d0490, args=0x55555881c7fe ":1234", from_tty=1)
	    at cli/cli-decode.c:2737
	#6  0x00005555573487fd in execute_command (p=0x55555881c802 "4", from_tty=1) at top.c:688

Therefore the second call to lookup_cmd () at line 697 fails to find
command because the original command string is gone.

This commit addresses this particular problem by creating a *copy* of
original command string for the sole purpose of using it after command
execution to lookup the command again. It may not be the most efficient
way but it's safer given that command buffer is shared and overwritten
in hard-to-foresee situations.

Tested on x86_64-linux.

PR 30249
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30249

Approved-By: Tom Tromey <tom@tromey.com>
2023-05-19 13:20:04 +01:00
Simon Marchi
13d03262f2 gdb: move struct ui and related things to ui.{c,h}
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-05-01 15:40:54 -04:00
Kevin Buettner
3b431a3c90 PR gdb/30219: Clear sync_quit_force_run in quit_force
PR 30219 shows an internal error due to a "Bad switch" in
print_exception() in gdb/exceptions.c.  The switch in question
contains cases for RETURN_QUIT and RETURN_ERROR, but is missing a case
for the recently added RETURN_FORCED_QUIT.  This commit adds that case.

Making the above change allows the errant test case to pass, but does
not fix the underlying problem, which I'll describe shortly.  Even
though the addition of a case for RETURN_FORCED_QUIT isn't the actual
fix, I still think it's important to add this case so that other
situations which lead to print_exeption() being called won't generate
that "Bad switch" internal error.

In order to understand the underlying problem, please examine
this portion of the backtrace from the bug report:

0x5576e4ff5780 print_exception
        /home/smarchi/src/binutils-gdb/gdb/exceptions.c:100
0x5576e4ff5930 exception_print(ui_file*, gdb_exception const&)
        /home/smarchi/src/binutils-gdb/gdb/exceptions.c:110
0x5576e6a896dd quit_force(int*, int)
        /home/smarchi/src/binutils-gdb/gdb/top.c:1849

The real problem is in quit_force; here's the try/catch which
eventually leads to the internal error:

  /* Get out of tfind mode, and kill or detach all inferiors.  */
  try
    {
      disconnect_tracing ();
      for (inferior *inf : all_inferiors ())
	kill_or_detach (inf, from_tty);
    }
  catch (const gdb_exception &ex)
    {
      exception_print (gdb_stderr, ex);
    }

While running the calls in the try-block, a QUIT check is being
performed.  This check finds that sync_quit_force_run is (still) set,
causing a gdb_exception_forced_quit to be thrown.  The exception
gdb_exception_forced_quit is derived from gdb_exception, causing
exception_print to be called.  As shown by the backtrace,
print_exception is then called, leading to the internal error.

The actual fix, also implemented by this commit, is to clear
sync_quit_force_run along with the quit flag.  This will allow the
various cleanup code, called by quit_force, to run without triggering
a gdb_exception_forced_quit.  (Though, if another SIGTERM is sent to
the gdb process, these flags will be set again and a QUIT check in the
cleanup code will detect it and throw the exception.)

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30219
Approved-By: Simon Marchi <simon.marchi@efficios.com>
2023-03-30 14:59:01 -07:00
Simon Marchi
287de65625 gdb, gdbserver, gdbsupport: fix whitespace issues
Replace spaces with tabs in a bunch of places.

Change-Id: If0f87180f1d13028dc178e5a8af7882a067868b0
2023-03-09 16:32:00 -05:00
Tom Tromey
1293ecd838 Don't use struct buffer in top.c
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-24 11:52:48 -07:00
Philippe Blain
4c9066e322 gdb: add --with-curses to --configuration output
'gdb --configuration' does not mention if GDB was built with curses.
Since b5075fb68d (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-21 09:36:34 -05:00
Lancelot SIX
08d8af48e4 gdb: 'show config' shows --with[out]-amd-dbgapi
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-02-13 09:42:13 +00:00
Tom Tromey
b5075fb68d Rename to allow_tui_tests
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-13 13:18:58 -07:00
Joel Brobecker
e1ca55341c Update copyright year in help message of gdb, gdbserver, gdbreplay
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.
2023-01-01 17:01:15 +04:00
Tom Tromey
a60535c39b Fix "set debug timestamp"
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-28 08:55:40 -07:00
Andrew Pinski
f7cb9bba3d Fix compiling of top.c
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-21 17:32:40 +00:00
Andrew Pinski
9911806278 Use toplevel configure for GMP and MPFR for gdb
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-21 16:49:23 +00:00
Jan Vrany
37e5833da5 gdb: fix command lookup in execute_command ()
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-19 11:24:36 +00:00
Simon Marchi
f8631e5e04 gdb: remove static buffer in command_line_input
[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-15 21:49:29 -05:00
Andrew Burgess
c8181f706f gdb: remove the pop_all_targets (and friends) global functions
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-14 13:57:22 +00:00
Jan Vrany
b5661ff24f gdb: fix possible use-after-free when executing commands
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-12-12 13:16:14 +00:00
Tom Tromey
bd2b40ac12 Change GDB to use frame_info_ptr
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-10-10 11:57:10 +02:00
Tom Tromey
d9f9581186 Use member initialization in 'struct ui'
This changes 'struct ui' to use member initialization.  This is
simpler to understand.
2022-08-31 11:03:40 -06:00
Tom Tromey
992aeed80b Use ui_out_redirect_pop in more places
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-31 11:03:39 -06:00
Tom Tromey
55a6603404 Free ui::line_buffer
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-31 11:03:39 -06:00
Tom Tromey
37163dcf1a Remove two initialization functions
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-08-19 08:23:20 -06:00
Tom Tromey
4a570176b4 Change target_ops::async to accept bool
This changes the parameter of target_ops::async from int to bool.
Regression tested on x86-64 Fedora 34.
2022-07-22 11:06:51 -06:00
Tom Tromey
efd3baf0dc Replace input_interactive_p with a method
This replaces the global input_interactive_p function with a new
method ui::input_interactive_p.
2022-07-18 08:49:55 -06:00
Andrew Burgess
ac16b09d7e gdb: move setbuf calls out of gdb_readline_no_editing_callback
After this commit:

  commit d08cbc5d32
  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-24 08:39:19 -07:00
Andrew Burgess
91395d97d9 gdb: handle bracketed-paste-mode and EOF correctly
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-22 18:46:05 +01:00
Simon Marchi
7ab2607f97 gdbsupport: make gdb_abspath return an std::string
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-18 15:48:03 -04:00
Simon Marchi
6e348286d8 gdb: fix gdb_print -> gdb_printf typo
This caused a build failure with !CXX_STD_THREAD.

Change-Id: I30f0c89c43a76f85c0db34809192644fa64a9d18
2022-04-03 09:54:58 -04:00
Tom Tromey
8839e3f3b0 Style URLs in GDB output
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-31 18:01:38 -06:00
Simon Marchi
29d210012a gdb: fix use of fprintf_filtered in top.c
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 12:52:16 -04:00