3238 Commits

Author SHA1 Message Date
Andrew Burgess
53d0889088 gdb/doc: add some notes on selecting suitable attribute names
In previous commits I've added Object.__dict__ support to gdb.Inferior
and gdb.InferiorThread, this is similar to the existing support for
gdb.Objfile and gdb.Progspace.

This commit extends the documentation to offer the user some guidance
on selecting good names for their custom attributes so they
can (hopefully) avoid conflicting with any future attributes that GDB
might add.

The rules I've proposed are:

  1. Don't start user attributes with a lower case letter, all the
  current GDB attributes start with a lower case letter, and I suspect
  all future attributes would also start with a lower case letter, and

  2. Don't start user attributes with a double underscore, this risks
  conflicting with Python built in attributes (e.g. __dict__) - though
  clearly the user would need to start and end with a double
  underscore, but it seemed easier just to say no double underscores.

I'm doing this as a separate commit as I've updated the docs for the
existing gdb.Objfile and gdb.Progspace so they all reference a single
paragraph on selecting attribute names.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Approved-By: Tom Tromey <tom@tromey.com>
2024-01-12 11:21:32 +00:00
Andrew Burgess
1d586eda5c gdb/python: Add gdb.InferiorThread.__dict__ attribute
The gdb.Objfile, gdb.Progspace, gdb.Type, and gdb.Inferior Python
types already have a __dict__ attribute, which allows users to create
user defined attributes within the objects.  This is useful if the
user wants to cache information within an object.

This commit adds the same functionality to the gdb.InferiorThread
type.

After this commit there is a new gdb.InferiorThread.__dict__
attribute, which is a dictionary.  A user can, for example, do this:

  (gdb) pi
  >>> t = gdb.selected_thread()
  >>> t._user_attribute = 123
  >>> t._user_attribute
  123
  >>>

There's a new test included.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Approved-By: Tom Tromey <tom@tromey.com>
2024-01-12 11:21:31 +00:00
Andrew Burgess
13cd9bceea gdb/python: Add gdb.Inferior.__dict__ attribute
The gdb.Objfile, gdb.Progspace, and gdb.Type Python types already have
a __dict__ attribute, which allows users to create user defined
attributes within the objects.  This is useful if the user wants to
cache information within an object.

This commit adds the same functionality to the gdb.Inferior type.

After this commit there is a new gdb.Inferior.__dict__ attribute,
which is a dictionary.  A user can, for example, do this:

  (gdb) pi
  >>> i = gdb.selected_inferior()
  >>> i._user_attribute = 123
  >>> i._user_attribute
  123
  >>>

There's a new test included.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Approved-By: Tom Tromey <tom@tromey.com>
2024-01-12 11:21:30 +00:00
Andrew Burgess
76118e1675 gdb/python: New InferiorThread.ptid_string attribute
This commit adds a new InferiorThread.ptid_string attribute.  This
read-only attribute contains the string returned by target_pid_to_str,
which actually converts a ptid (not pid) to a string.

This is the string that appears (at least in part) in the output of
'info threads' in the 'Target Id' column, but also in the thread
exited message that GDB prints.

Having access to this string from Python is useful for allowing
extensions identify threads in a similar way to how GDB core would
identify the thread.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Approved-By: Tom Tromey <tom@tromey.com>
2024-01-12 09:22:25 +00:00
Tom Tromey
9b595a2641 Add "maint set dwarf synchronous"
For testing, it's sometimes convenient to be able to request that
DWARF reading be done synchronously.  This patch adds a new "maint"
setting for this purpose.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
2024-01-08 18:40:21 -07:00
Guinevere Larsen
528b729be1 gdb/dwarf2: Add support for DW_LNS_set_epilogue_begin in line-table
This commit adds a mechanism for GDB to detect the linetable opcode
DW_LNS_set_epilogue_begin. This opcode is set by compilers to indicate
that a certain instruction marks the point where the frame is destroyed.

While the standard allows for multiple points marked with epilogue_begin
in the same function, for performance reasons, the function that
searches for the epilogue address will only find the last address that
sets this flag for a given block.

This commit also changes amd64_stack_frame_destroyed_p_1 to attempt to
use the epilogue begin directly, and only if an epilogue can't be found
will it attempt heuristics based on the current instruction.

Finally, this commit also changes the dwarf assembler to be able to emit
epilogue-begin instructions, to make it easier to test this patch

Approved-By: Tom Tromey <tom@tromey.com>
2024-01-02 10:21:37 +01:00
Tom Tromey
dfc4bd461b Add DAP log level parameter
This adds a new parameter to control the DAP logging level.  By
default, "expected" exceptions are not logged, but the parameter lets
the user change this when more logging is desired.

This also changes a couple of spots to avoid logging the stack trace
for a DAPException.

This patch also documents the existing DAP logging parameter.  I
forgot to document this before.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Reviewed-By: Kévin Le Gouguec <legouguec@adacore.com>
2023-12-22 09:57:48 -07:00
Tom Tromey
eb6476e2db Add 'program' to DAP 'attach' request
In many cases, it's not possible for gdb to discover the executable
when a DAP 'attach' request is used.  This patch lets the IDE supply
this information.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
2023-12-22 09:05:17 -07:00
Magne Hov
14a2449eee [gdb/tui] add SingleKey bindings for reverse execution commands
The bindings for the reverse execution commands are the same letters
as the forward execution command, but with the opposite case. This way
one can simply hold down the Shift modifier key or tap the Caps Lock key
to change the direction of execution.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Approved-By: Tom Tromey <tom@tromey.com>
2023-12-13 11:45:49 +00:00
Tom Tromey
1c79c8dad9 Implement DAP cancellation
This implements DAP cancellation.  A new object is introduced that
handles the details of cancellation.  While cancellation is inherently
racy, this code attempts to make it so that gdb doesn't inadvertently
cancel the wrong request.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30472
Approved-By: Eli Zaretskii <eliz@gnu.org>
Reviewed-By: Kévin Le Gouguec <legouguec@adacore.com>
2023-12-11 11:44:35 -07:00
Tom Tromey
3a12e74ece Introduce gdb.interrupt
DAP cancellation needs a way to interrupt whatever is happening on
gdb's main thread -- whether that is the inferior, a gdb CLI command,
or Python code.

This patch adds a new gdb.interrupt() function for this purpose.  It
simply sets the quit flag and lets gdb do the rest.

No tests in this patch -- instead this is tested via the DAP
cancellation tests.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Reviewed-By: Kévin Le Gouguec <legouguec@adacore.com>
2023-12-11 11:44:33 -07:00
Tom Tromey
e187e7c969 Emit stop reason details in Python stop events
This changes Python stop events to carry a "details" dictionary, that
holds any relevant information about the stop.  The details are
constructed using more or less the same procedure as is done for MI.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=13587
Reviewed-By: Eli Zaretskii <eliz@gnu.org>
2023-12-11 10:51:32 -07:00
Tom Tromey
01312843c8 Fix "not not" in Python documentation
I noticed a "not not" in the Python documentation where just "not" was
meant.  This patch fixes the error.
2023-12-10 15:26:46 -07:00
Tom de Vries
612f120d41 [gdb/tui] Show focus window in status line
The focused window is highlighted by using active-border-kind instead of
border-kind.

But if the focused window is the cmd window (which is an unboxed window), then
no highlighting is done, and it's not obvious from looking at the screen which
window has the focus.  Instead, you have to notice the absence of highlighting
on boxed windows, and then infer that the focus is on the unboxed window.

That approach stops working if there are multiple unboxed windows.

Likewise if highlighting is switched off by setting active-border-kind to the
same value as border-kind.

Make it more explicit which window has the focus by mentioning it in the status
window, like so:
...
native process 8282 (src) In: main                      L7    PC: 0x400525
...

Tested on x86_64-linux and ppc64le-linux.

Tested-By: Alexandra Petlanova Hajkova <ahajkova@redhat.com>
Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Approved-By: Tom Tromey <tom@tromey.com>
2023-12-08 23:02:31 +01:00
Andrew Burgess
8fd5a6058f gdbserver: allow for general 'monitor set debug COMPONENT VALUE' use
Building on the last commit, which added a general --debug=COMPONENT
option to the gdbserver command line, this commit updates the monitor
command to allow for general:

  (gdb) monitor set debug COMPONENT off|on

style commands.  Just like with the previous commit, the COMPONENT can
be any one of all, threads, remote, event-loop, and correspond to the
same set of global debug flags.

While on the command line it is possible to do:

  --debug=remote,event-loop,threads

the components have to be entered one at a time with the monitor
command.  I guess there's no reason why we couldn't allow component
grouping within the monitor command, but (to me) what I have here
seemed more in the spirit of GDB's existing 'set debug ...' commands.
If people want it then we can always add component grouping later.

Notice in the above that I use 'off' and 'on' instead of '0' and '1',
which is what the 'monitor set debug' command used to use.  The 0/1
can still be used, but I now advertise off/on in all the docs and help
text, again, this feels more inline with GDB's existing boolean
settings.

I have removed the two existing monitor commands:

  monitor set remote-debug 0|1
  monitor set event-loop-debug 0|1

These are replaced by:

  monitor set debug remote off|on
  monitor set debug event-loop off|on

respectively.

Approved-By: Tom Tromey <tom@tromey.com>
Reviewed-By: Eli Zaretskii <eliz@gnu.org>
2023-12-08 17:52:00 +00:00
Andrew Burgess
3bf9e166ca gdbserver: allow the --debug command line option to take a value
Currently, gdbserver has the following command line options related to
debugging output:

  --debug
  --remote-debug
  --event-loop-debug

This doesn't scale well.  If I want an extra debug component I need to
add another command line flag.

This commit changes --debug to take a list of components.

The currently supported components are: all, threads, remote, and
event-loop.  The 'threads' component represents the debug we currently
get from the --debug option.  And if --debug is used without a
component list then the threads component is assumed as the default.

Currently the threads component actually includes a lot of output that
is not really threads related.  In the future I'd like to split this
up into some new, separate components.  But that is not part of this
commit, or even this series.

The special component 'all' does what you'd expect: enables debug
output from all supported components.

The component list is parsed left to write, and you can prefix a
component with '-' to disable that component, so I can write:

  target> gdbserver --debug=all,-event-loop

to get debug for all components except the event-loop component.

I've removed the existing --remote-debug and --event-loop-debug
command line options, these are equivalent to --debug=remote and
--debug=event-loop respectively, or --debug=remote,event-loop to
enable both components.

In this commit I've only update the command line options, in the next
commit I'll update the monitor commands to support a similar
interface.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Approved-By: Tom Tromey <tom@tromey.com>
2023-12-08 17:52:00 +00:00
Tom Tromey
a70364f6b4 Fix minor grammar error in gdb.texinfo
This fixes a small grammar issue in gdb.texinfo -- "additional" was
written where "additionally" is correct.
2023-12-06 12:32:57 -07:00
Ciaran Woodward
850f4ee546 [gdb/doc] Escape the '@' symbols in generated texinfo files.
'@' is a special symbol meaning 'command' in GNU texinfo.

If the GDBINIT or GDBINIT_DIR path during configuration
included an '@' character, the makeinfo command would fail,
as it interpreted the '@' in the path as a start of a command
when expanding the path in the docs.

This patch simply escapes any '@' characters in the path,
by replacing them with '@@'. This was already done for the
bugurl variable.

This was detected because the 'Jenkins' tool sometimes puts
an '@' in the workspace path.

Approved-By: Tom Tromey <tom@tromey.com>
2023-12-04 18:49:26 +00:00
Tom Tromey
f644dde103 Add link to Debugger Adapter Protocol node in documentation
I noticed that the interpreters node in the docs links to the DAP
protocol docs, but I thought the DAP node ought to as well.  This
patch adds a bit of introductory text there.

Approved-By: Eli Zaretskii <eliz@gnu.org>
2023-12-01 08:49:54 -07:00
Tom Tromey
5006ea556d Update gdb.Symbol.is_variable documentation
Kévin found a bug in an earlier version of this series that was based
on a misconception I had about Symbol.is_variable.  This patch fixes
the documentation to explain the method a bit better.

Approved-By: Eli Zaretskii <eliz@gnu.org>
2023-11-14 08:44:31 -07:00
Tom Tromey
4ead09a294 Add gdb.Frame.static_link method
This adds a new gdb.Frame.static_link method to the gdb Python layer.
This can be used to find the static link frame for a given frame.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
2023-11-14 08:44:30 -07:00
Andrew Burgess
8f6c452b5a gdb: implement missing debug handler hook for Python
This commit builds on the previous commit, and implements the
extension_language_ops::handle_missing_debuginfo function for Python.
This hook will give user supplied Python code a chance to help find
missing debug information.

The implementation of the new hook is pretty minimal within GDB's C++
code; most of the work is out-sourced to a Python implementation which
is modelled heavily on how GDB's Python frame unwinders are
implemented.

The following new commands are added as commands implemented in
Python, this is similar to how the Python unwinder commands are
implemented:

  info missing-debug-handlers
  enable missing-debug-handler LOCUS HANDLER
  disable missing-debug-handler LOCUS HANDLER

To make use of this extension hook a user will create missing debug
information handler objects, and registers these handlers with GDB.
When GDB encounters an objfile that is missing debug information, each
handler is called in turn until one is able to help.  Here is a
minimal handler that does nothing useful:

  import gdb
  import gdb.missing_debug

  class MyFirstHandler(gdb.missing_debug.MissingDebugHandler):
      def __init__(self):
          super().__init__("my_first_handler")

      def __call__(self, objfile):
          # This handler does nothing useful.
          return None

  gdb.missing_debug.register_handler(None, MyFirstHandler())

Returning None from the __call__ method tells GDB that this handler
was unable to find the missing debug information, and GDB should ask
any other registered handlers.

By extending the __call__ method it is possible for the Python
extension to locate the debug information for objfile and return a
value that tells GDB how to use the information that has been located.

Possible return values from a handler:

  - None: This means the handler couldn't help.  GDB will call other
          registered handlers to see if they can help instead.

  - False: The handler has done all it can, but the debug information
           for the objfile still couldn't be found.  GDB will not call
	   any other handlers, and will continue without the debug
	   information for objfile.

  - True: The handler has installed the debug information into a
          location where GDB would normally expect to find it.  GDB
	  should look again for the debug information.

  - A string: The handler can return a filename, which is the file
              containing the missing debug information.  GDB will load
	      this file.

When a handler returns True, GDB will look again for the debug
information, but only using the standard built-in build-id and
.gnu_debuglink based lookup strategies.  It is not possible for an
extension to trigger another debuginfod lookup; the assumption is that
the debuginfod server is remote, and out of the control of extensions
running within GDB.

Handlers can be registered globally, or per program space.  GDB checks
the handlers for the current program space first, and then all of the
global handles.  The first handler that returns a value that is not
None, has "handled" the objfile, at which point GDB continues.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Approved-By: Tom Tromey <tom@tromey.com>
2023-11-14 12:02:47 +00:00
Pedro Alves
b11df22aa7 Document remote clone events, and QThreadOptions packet
This commit documents in both manual and NEWS:

 - the new remote clone event stop reply,
 - the new QThreadOptions packet and its current defined options,
 - the associated "set/show remote thread-events-packet" command,
 - and the associated QThreadOptions qSupported feature.

Approved-By: Eli Zaretskii <eliz@gnu.org>
Change-Id: Ic1c8de1fefba95729bbd242969284265de42427e
2023-11-13 14:16:11 +00:00
Pedro Alves
7ac958f267 Don't resume new threads if scheduler-locking is in effect
If scheduler-locking is in effect, e.g., with "set scheduler-locking
on", and you step over a function that spawns a new thread, the new
thread is allowed to run free, at least until some event is hit, at
which point, whether the new thread is re-resumed depends on a number
of seemingly random factors.  E.g., if the target is all-stop, and the
parent thread hits a breakpoint, and GDB decides the breakpoint isn't
interesting to report to the user, then the parent thread is resumed,
but the new thread is left stopped.

I think that letting the new threads run with scheduler-locking
enabled is a defect.  This commit fixes that, making use of the new
clone events on Linux, and of target_thread_events() on targets where
new threads have no connection to the thread that spawned them.

Testcase and documentation changes included.

Approved-By: Eli Zaretskii <eliz@gnu.org>
Reviewed-By: Andrew Burgess <aburgess@redhat.com>
Change-Id: Ie12140138b37534b7fc1d904da34f0f174aa11ce
2023-11-13 14:16:11 +00:00
Andrew Burgess
0ae5b8fade Add "maint info linux-lwps" command
This adds a maintenance command that lets you list all the LWPs under
control of the linux-nat target.

For example:

 (gdb) maint info linux-lwps
 LWP Ptid        Thread ID
 560948.561047.0 None
 560948.560948.0 1.1

This shows that "560948.561047.0" LWP doesn't map to any thread_info
object, which is bogus.  We'll be using this in a testcase in a
following patch.

Co-Authored-By: Pedro Alves <pedro@palves.net>
Change-Id: Ic4e9e123385976e5cd054391990124b7a20fb3f5
2023-11-13 14:16:09 +00:00
Andrew Burgess
f3a8a979bb gdb: error if /r and /b are used with disassemble command
The disassembler gained a new /b flag in this commit:

  commit d4ce49b7ac077a9882d6a5e689e260300045ca88
  Date:   Tue Jun 21 20:23:35 2022 +0100

      gdb: disassembler opcode display formatting

The /b and /r flags result in the instruction opcodes displayed in
different formats, so it's not possible to have both at the same
time.  Currently the /b flag overrides the /r flag.

We have a similar situation with the /m and /s flags, but here, if the
user tries to use both flags then they will get an error.

I think the error is clearer, so in this commit I propose that we add
an error if /r and /b are both used.

Obviously this change breaks backwards compatibility.  I don't have a
compelling argument for why we should make the change beyond my
feeling that it was a mistake not to add this error from the start,
and that the new behaviour is better.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
2023-11-08 11:18:39 +00:00
Andrew Burgess
ef8cf9093d gdb/python: Add new gdb.Value.bytes attribute
Add a gdb.Value.bytes attribute.  This attribute contains the bytes of
the value (assuming the complete bytes of the value are available).

If the bytes of the gdb.Value are not available then accessing this
attribute raises an exception.

The bytes object returned from gdb.Value.bytes is cached within GDB so
that the same bytes object is returned each time.  The bytes object is
created on-demand though to reduce unnecessary work.

For some values we can of course obtain the same information by
reading inferior memory based on gdb.Value.address and
gdb.Value.type.sizeof, however, not every value is in memory, so we
don't always have an address.

The gdb.Value.bytes attribute will convert any value to a bytes
object, so long as the contents are available.  The value can be one
created purely in Python code, the value could be in a register,
or (of course) the value could be in memory.

The Value.bytes attribute can also be assigned too.  Assigning to this
attribute is similar to calling Value.assign, the value of the
underlying value is updated within the inferior.  The value assigned
to Value.bytes must be a buffer which contains exactly the correct
number of bytes (i.e. unlike value creation, we don't allow oversized
buffers).

To support this assignment like behaviour I've factored out the core
of valpy_assign.  I've also updated convert_buffer_and_type_to_value
so that it can (for my use case) check the exact buffer length.

The restrictions for when the Value.bytes can or cannot be written too
are exactly the same as for Value.assign.

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

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Approved-By: Tom Tromey <tom@tromey.com>
2023-10-26 18:27:17 +01:00
Jan Vrany
4825fd2d35 gdb/python: implement support for sending custom MI async notifications
This commit adds a new Python function, gdb.notify_mi, that can be used
to emit custom async notification to MI channel.  This can be used, among
other things, to implement notifications about events MI does not support,
such as remote connection closed or register change.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Approved-By: Andrew Burgess <aburgess@redhat.com>
2023-10-10 11:22:56 +01:00
Matheus Branco Borella
8b9c08edda [gdb/symtab] Add name_of_main and language_of_main to the DWARF index
This patch adds a new section to the DWARF index containing the name
and the language of the main function symbol, gathered from
`cooked_index::get_main`, if available. Currently, for lack of a better name,
this section is called the "shortcut table". The way this name is both saved and
applied upon an index being loaded in mirrors how it is done in
`cooked_index_functions`, more specifically, the full name of the main function
symbol is saved and `set_objfile_main_name` is used to apply it after it is
loaded.

The main use case for this patch is in improving startup times when dealing with
large binaries. Currently, when an index is used, GDB has to expand symtabs
until it finds out what the language of the main function symbol is. For some
large executables, this may take a considerable amount of time to complete,
slowing down startup. This patch bypasses that operation by having both the name
and language of the main function symbol be provided ahead of time by the index.

In my testing (a binary with about 1.8GB worth of DWARF data) this change brings
startup time down from about 34 seconds to about 1.5 seconds.

When testing the patch with target board cc-with-gdb-index, test-case
gdb.fortran/nested-funcs-2.exp starts failing, but this is due to a
pre-existing issue, filed as PR symtab/30946.

Tested on x86_64-linux, with target board unix and cc-with-gdb-index.

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

Approved-By: Tom de Vries <tdevries@suse.de>
2023-10-10 10:26:40 +02: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
Luis Machado
cacc7bd710 sme2: Document SME2 registers and features
Document changes introduced by gdb's SME2 support.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Reviewed-by: Thiago Jung Bauermann <thiago.bauermann@linaro.org>
2023-10-04 16:23:41 +01:00
Luis Machado
6762e153a9 sme: Document SME registers and features
Provide documentation for the SME feature and other information that
should be useful for users that need to debug a SME-capable target.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Reviewed-by: Thiago Jung Bauermann <thiago.bauermann@linaro.org>
2023-10-04 16:23:40 +01:00
Aaron Merey
4afdefc337 gdb: Add command 'maint set/show debuginfod download-sections'
This setting controls whether GDB may attempt to download ELF/DWARF
sections from debuginfod servers.

This setting is enabled by default if gdb is built with debuginfod
section download support (requires libdebuginfod 0.188).

Also update debuginfod command help text and gdb.texinfo with
information on section downloading and the new command.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Approved-By: Andrew Burgess <aburgess@redhat.com>
2023-10-02 14:04:59 -04:00
Andrew Burgess
59912fb2d2 gdb: add Python events for program space addition and removal
Initially I just wanted a Python event for when GDB removes a program
space, I'm writing a Python extension that caches information for each
program space, and need to know when I should discard entries for a
particular program space.

But, it seemed easy enough to also add an event for when GDB adds a
new program space, so I went ahead and added both new events.

Of course, we don't currently have an observable for program space
addition or removal, so I first needed to add these.  After that it's
pretty simple to add two new Python events and have these trigger.

The two new event registries are:

  events.new_progspace
  events.free_progspace

These emit NewProgspaceEvent and FreeProgspaceEvent objects
respectively, each of these new event types has a 'progspace'
attribute that contains the relevant gdb.Progspace object.

There's a couple of things to be mindful of.

First, it is not possible to catch the NewProgspaceEvent for the very
first program space, the one that is created when GDB first starts, as
this program space is created before any Python scripts are sourced.

In order to allow this event to be caught we would need to defer
creating the first program space, and as a consequence the first
inferior, until some later time.  But, existing scripts could easily
depend on there being an initial inferior, so I really don't think we
should change that -- and so, we end up with the consequence that we
can't catch the event for the first program space.

The second, I think minor, issue, is that GDB doesn't clean up its
program spaces upon exit -- or at least, they are not cleaned up
before Python is shut down.  As a result, any program spaces in use at
the time GDB exits don't generate a FreeProgspaceEvent.  I'm not
particularly worried about this for my use case, I'm using the event
to ensure that a cache doesn't hold stale entries within a single GDB
session.  It's also easy enough to add a Python at-exit callback which
can do any final cleanup if needed.

Finally, when testing, I did hit a slightly weird issue with some of
the remote boards (e.g. remote-stdio-gdbserver).  As a consequence of
this issue I see some output like this in the gdb.log:

  (gdb) PASS: gdb.python/py-progspace-events.exp: inferior 1
  step
  FreeProgspaceEvent: <gdb.Progspace object at 0x7fb7e1d19c10>
  warning: cannot close "target:/lib64/libm.so.6": Cannot execute this command while the target is running.
  Use the "interrupt" command to stop the target
  and then try again.
  warning: cannot close "target:/lib64/libc.so.6": Cannot execute this command while the target is running.
  Use the "interrupt" command to stop the target
  and then try again.
  warning: cannot close "target:/lib64/ld-linux-x86-64.so.2": Cannot execute this command while the target is running.
  Use the "interrupt" command to stop the target
  and then try again.
  do_parent_stuff () at py-progspace-events.c:41
  41        ++global_var;
  (gdb) PASS: gdb.python/py-progspace-events.exp: step

The 'FreeProgspaceEvent ...' line is expected, that's my test Python
extension logging the event.  What isn't expected are all the blocks
like:

  warning: cannot close "target:/lib64/libm.so.6": Cannot execute this command while the target is running.
  Use the "interrupt" command to stop the target
  and then try again.

It turns out that this has nothing to do with my changes, this is just
a consequence of reading files over the remote protocol.  The test
forks a child process which GDB stays attached too.  When the child
exits, GDB cleans up by calling prune_inferiors, which in turn can
result in GDB trying to close some files that are open because of the
inferior being deleted.

If the prune_inferiors call occurs when the remote target is
running (and in non-async mode) then GDB will try to send a fileio
packet while the remote target is waiting for a stop reply, and the
remote target will throw an error, see remote_target::putpkt_binary in
remote.c for details.

I'm going to look at fixing this, but, as I said, this is nothing to
do with this change, I just mention it because I ended up needing to
account for these warning messages in one of my tests, and it all
looks a bit weird.

Approved-By: Tom Tromey <tom@tromey.com>
Reviewed-By: Eli Zaretskii <eliz@gnu.org>
2023-10-02 17:06:40 +01:00
Tom Tromey
4ebfd53de0 Support the NO_COLOR environment variable
I ran across this site:

    https://no-color.org/

... which lobbies for tools to recognize the NO_COLOR environment
variable and disable any terminal styling when it is seen.

This patch implements this for gdb.

Regression tested on x86-64 Fedora 38.

Co-Authored-By: Andrew Burgess <aburgess@redhat.com>
Reviewed-by: Kevin Buettner <kevinb@redhat.com>
Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Approved-By: Andrew Burgess <aburgess@redhat.com>
2023-09-29 10:55:43 -06:00
Andrew Burgess
42f297ad36 gdb/python: make the executable_changed event available from Python
This commit makes the executable_changed observable available through
the Python API as an event.  There's nothing particularly interesting
going on here, it just follows the same pattern as many of the other
Python events we support.

The new event registry is called events.executable_changed, and this
emits an ExecutableChangedEvent object which has two attributes, a
gdb.Progspace called 'progspace', this is the program space in which
the executable changed, and a Boolean called 'reload', which is True
if the same executable changed on disk and has been reloaded, or is
False when a new executable has been loaded.

One interesting thing did come up during testing though, you'll notice
the test contains a setup_kfail call.  During testing I observed that
the executable_changed event would trigger twice when GDB restarted an
inferior.  However, the ExecutableChangedEvent object is identical for
both calls, so the wrong information is never sent out, we just see
one too many events.

I tracked this down to how the reload_symbols function (symfile.c)
takes care to also reload the executable, however, I've split fixing
this into a separate commit, so see the next commit for details.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Approved-By: Tom Tromey <tom@tromey.com>
2023-09-28 15:33:13 +01:00
Andrew Burgess
4e02aca0c5 gdb/python: new Progspace.executable_filename attribute
Add a new Progspace.executable_filename attribute that contains the
path to the executable for this program space, or None if no
executable is set.

The path within this attribute will be set by the "exec-file" and/or
"file" commands.

Accessing this attribute for an invalid program space will raise an
exception.

This new attribute is similar too, but not the same as the existing
gdb.Progspace.filename attribute.  If I could change the past, I'd
change the 'filename' attribute to 'symbol_filename', which is what it
actually represents.  The old attribute will be set by the
'symbol-file' command, while the new attribute is set by the
'exec-file' command.  Obviously the 'file' command sets both of these
attributes.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Approved-By: Tom Tromey <tom@tromey.com>
2023-09-28 15:33:13 +01:00
Andrew Burgess
5ce85461a1 gdb/python: new Progspace.symbol_file attribute
Add a new Progspace.symbol_file attribute.  This attribute holds the
gdb.Objfile object that corresponds to Progspace.filename, or None if
there is no main symbol file currently set.

Currently, to get this gdb.Objfile, a user would need to use
Progspace.objfiles, and then search for the objfile with a name that
matches Progspace.filename -- which should work just fine, but having
direct access seems a little nicer.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Approved-By: Tom Tromey <tom@tromey.com>
2023-09-28 15:33:13 +01:00
Andrew Burgess
ce65386db6 gdb/doc: extend the description for Progspace.filename
Extend the description for Progspace.filename in the documentation to
mention what the returned string is actually the filename
for (e.g. that it is the filename passed to the 'symbol-file' or
'file' command).

Also document that this attribute will be None if no symbol file is
currently loaded.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Approved-By: Tom Tromey <tom@tromey.com>
2023-09-28 15:33:13 +01:00
Tom Tromey
f35baff348 Add two new pretty-printer methods
This adds two new pretty-printer methods, to support random access to
children.  The methods are implemented for the no-op array printer,
and DAP is updated to use this.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
2023-09-26 09:29:14 -06:00
Tom Tromey
fb28257699 Introduce gdb.ValuePrinter
There was an earlier thread about adding new methods to
pretty-printers:

https://sourceware.org/pipermail/gdb-patches/2023-June/200503.html

We've known about the need for printer extensibility for a while, but
have been hampered by backward-compatibilty concerns: gdb never
documented that printers might acquire new methods, and so existing
printers may have attribute name clashes.

To solve this problem, this patch adds a new pretty-printer tag class
that signals to gdb that the printer follows new extensibility rules.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30816
Reviewed-By: Eli Zaretskii <eliz@gnu.org>
2023-09-26 09:29:14 -06:00
Matthew "strager" Glazar
0f6a699478 gdb/tui: add 'set tui mouse-events off' to restore mouse selection
Rationale:
I use the mouse with my terminal to select and copy text. In gdb, I use
the mouse to select a function name to set a breakpoint, or a variable
name to print, for example.

When gdb is compiled with ncurses mouse support, gdb's TUI mode
intercepts mouse events. Left-clicking and dragging, which would
normally select text, seems to do nothing. This means I cannot select
text using my mouse anymore. This makes it harder to set breakpoints,
print variables, etc.

Solution:
I tried to fix this issue by editing the 'mousemask' call to only enable
buttons 4 and 5. However, this still caused my terminal (gnome-terminal)
to not allow text to be selected. The only way I could make it work is
by calling 'mousemask (0, NULL);'. But doing so disables the mouse code
entirely, which other people might want.

I therefore decided to make a setting in gdb called 'tui mouse-events'.
If enabled (the default), the behavior is as it is now: terminal mouse
events are given to gdb, disabling the terminal's default behavior.
If disabled (opt-in), the behavior is as it was before the year 2020:
terminal mouse events are not given to gdb, therefore the mouse can be
used to select and copy text.

Notes:
I am not attached to the setting name or its description. Feel free to
suggest better wording.

Testing:
I tested this change in gnome-terminal by performing the following steps
manually:

1. Run: gdb --args ./myprogram
2. Enable TUI: press ctrl-x ctrl-a
3. Click and drag text with the mouse. Observe no selection.
4. Input: set tui mouse-events off
5. Click and drag text with the mouse. Observe that selection works now.
6. Input: set tui mouse-events on.
7. Click and drag text with the mouse. Observe no selection.
2023-09-20 16:35:36 +01:00
Guinevere Larsen
12f567bcb6 gdb/cli: fixes to newly added "list ." command
After the series that added this command was pushed, Pedro mentioned
that the news description could easily be misinterpreted, as well as
some code and test improvements that should be made.

While fixing the test, I realized that code repetition wasn't
happening as it should, so I took care of that too.

Approved-By: Andrew Burgess <aburgess@redhat.com>
Reviewed-By: Eli Zaretskii <eliz@gnu.org>
2023-09-19 14:06:49 +02:00
Simon Marchi
607c90c738 gdb/amdgpu: add precise-memory support
The amd-dbgapi library exposes a setting called "memory precision" for
AMD GPUs [1].  Here's a copy of the description of the setting:

    The AMD GPU can overlap the execution of memory instructions with other
    instructions.  This can result in a wave stopping due to a memory violation
    or hardware data watchpoint hit with a program counter beyond the
    instruction that caused the wave to stop.

    Some architectures allow the hardware to be configured to always wait for
    memory operations to complete before continuing.  This will result in the
    wave stopping at the instruction immediately after the one that caused the
    stop event.  Enabling this mode can make execution of waves significantly
    slower.

Expose this option through a new "amdgpu precise-memory" setting.

The precise memory setting is per inferior.  The setting is transferred
from one inferior to another when using the clone-inferior command, or
when a new inferior is created following an exec or a fork.

It can be set before starting the inferior, in which case GDB will
attempt to apply what the user wants when attaching amd-dbgapi.  If the
user has requested to enable precise memory, but it can't be enabled
(not all hardware supports it), GDB prints a warning.

If precise memory is disabled, GDB prints a warning when hitting a
memory exception (translated into GDB_SIGNAL_SEGV or GDB_SIGNAL_BUS),
saying that the stop location may not be precise.

Note that the precise memory setting also affects memory watchpoint
reporting, but the watchpoint support for AMD GPUs hasn't been
upstreamed to GDB yet.  When we do upstream watchpoint support, GDB will
produce a similar warning message when stopping due to a watchpoint if
precise memory is disabled.

Add a handful of tests.  Add a util proc
"hip_devices_support_precise_memory", which indicates if all devices
used for testing support that feature.

[1] 687374258a/include/amd-dbgapi.h.in (L6300-L6317)

Change-Id: Ife1a99c0e960513da375ced8f8afaf8e47a61b3f
Approved-By: Lancelot Six <lancelot.six@amd.com>
2023-09-15 16:19:22 -04:00
Tom Tromey
14432bde07 Update Python signal-handling documentation
I noticed a typo in the "Basic Python" node, and when fixing it
realized that the paragraph could use a link to the block_signals
function.  This patch is the result.

Approved-By: Eli Zaretskii <eliz@gnu.org>
2023-09-11 10:41:19 -06:00
Simon Marchi
15db2284f2 gdb/doc: describe x87 registers
While investigating this [1], I initially had no idea what register
"fioff" stood for, making it difficult to map it to something in the
Intel or AMD manuals.  Similarly, I can imaging someone familiar with
x87 to want to print the "x87 last instruction address", and have no
clue that GDB makes it available as register "fioff".  The names of the
x87 state fields don't seem to be standardized, they even change between
sections of the Intel manual (between the FSAVE, FXSAVE and XSAVE area
descriptions).

Add some details to the doc to help one map GDB register names to x87
state fields.

[1] https://inbox.sourceware.org/gdb-patches/20230908022722.430741-1-simon.marchi@efficios.com/T/#u

Change-Id: I0ea1eb648358e62da4aa87eea3515ee8a09f2762
Approved-By: Eli Zaretskii <eliz@gnu.org>
Approved-By: Pedro Alves <pedro@palves.net>
2023-09-08 13:14:41 -04:00
Simon Marchi
3c83779e12 gdb/doc: rename "x86 Architecture-specific Issues" section to "x86"
I'm looking to add some x86-specific information to the doc, but I find
the naming of this section odd.  It doesn't really talk about issues, it
just gives generally useful information.  Also, the sections about other
architectures don't mention "issues", just the architecture name.

Also, at least in the HTML version of the doc, the name is inconsistent
between the main table of content, where it appears as "x86
Architecture-specific Issues", and the sub-table of contents of the
"Architectures" section, where it appears as "i386".

Rename the section to just "x86".

Change-Id: I0a119ff1ab5e7b83c9afa3c3977eb085e88f52ca
Approved-By: Eli Zaretskii <eliz@gnu.org>
2023-09-08 13:13:33 -04:00
Tom Tromey
59668c9d8c Add new Python APIs to support DAP value display
gdb's language code may know how to display values specially.  For
example, the Rust code understands that &str is a string-like type, or
Ada knows how to handle unconstrained arrays.  This knowledge is
exposed via val-print, and via varobj -- but currently not via DAP.

This patch adds some support code to let DAP also handle these cases,
though in a somewhat more generic way.

Type.is_array_like and Value.to_array are added to make Python aware
of the cases where gdb knows that a structure type is really
"array-like".

Type.is_string_like is added to make Python aware of cases where gdb's
language code knows that a type is string-like.

Unlike Value.string, these cases are handled by the type's language,
rather than the current language.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
2023-09-05 11:10:15 -06:00
Andrew Burgess
b080fe54fb gdb: add inferior-specific breakpoints
This commit extends the breakpoint mechanism to allow for inferior
specific breakpoints (but not watchpoints in this commit).

As GDB gains better support for multiple connections, and so for
running multiple (possibly unrelated) inferiors, then it is not hard
to imagine that a user might wish to create breakpoints that apply to
any thread in a single inferior.  To achieve this currently, the user
would need to create a condition possibly making use of the $_inferior
convenience variable, which, though functional, isn't the most user
friendly.

This commit adds a new 'inferior' keyword that allows for the creation
of inferior specific breakpoints.

Inferior specific breakpoints are automatically deleted when the
associated inferior is removed from GDB, this is similar to how
thread-specific breakpoints are deleted when the associated thread is
deleted.

Watchpoints are already per-program-space, which in most cases mean
watchpoints are already inferior specific.  There is a small window
where inferior-specific watchpoints might make sense, which is after a
vfork, when two processes are sharing the same address space.
However, I'm leaving that as an exercise for another day.  For now,
attempting to use the inferior keyword with a watchpoint will give an
error, like this:

  (gdb) watch a8 inferior 1
  Cannot use 'inferior' keyword with watchpoints

A final note on the implementation: currently, inferior specific
breakpoints, like thread-specific breakpoints, are inserted into every
inferior, GDB then checks once the inferior stops if we are in the
correct thread or inferior, and resumes automatically if we stopped in
the wrong thread/inferior.

An obvious optimisation here is to only insert breakpoint locations
into the specific program space (which mostly means inferior) that
contains either the inferior or thread we are interested in.  This
would reduce the number times GDB has to stop and then resume again in
a multi-inferior setup.

I have a series on the mailing list[1] that implements this
optimisation for thread-specific breakpoints.  Once this series has
landed I'll update that series to also handle inferior specific
breakpoints in the same way.  For now, inferior specific breakpoints
are just slightly less optimal, but this is no different to
thread-specific breakpoints in a multi-inferior debug session, so I
don't see this as a huge problem.

[1] https://inbox.sourceware.org/gdb-patches/cover.1685479504.git.aburgess@redhat.com/
2023-08-17 16:42:39 +01:00
Tom Tromey
f8b12b8e10 Fix Python documentation for range type fields
GDB's Python documentation claims that range types have two fields,
but this is not true, and attempts to access them hit this error:

      "Type is not a structure, union, enum, or function type."

This patch fixes the documentation.
2023-08-11 12:22:27 -06:00