Commit Graph

271 Commits

Author SHA1 Message Date
Tom Tromey
c64cba1b56 Introduce sourceReference handling in DAP
This changes the gdb DAP implementation to emit a real
sourceReference, rather than emitting 0.  Sources are tracked in some
maps in sources.py, and a new helper function is introduced to compute
the "Source" object that can be sent to the client.
2023-08-01 13:12:22 -06:00
Tom Tromey
7b4a5561e4 Don't supply DAP 'path' for non-file shared libraries
The DAP 'module' event may include a 'path' component.  I noticed that
this is supplied even when the module in question does not come from a
file.

This patch only emits this field when the objfile corresponds to a
real file.

No test case, because I wasn't sure how to write a portable one.
However, it's clear from gdb.log on Linux:

{"type": "event", "event": "module", "body": {"reason": "new", "module": {"id": "system-supplied DSO at 0x7ffff7fc4000", "name": "system-supplied DSO at 0x7ffff7fc4000"}}, "seq": 21}

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30676
2023-08-01 13:06:53 -06:00
Tom Tromey
e137699884 Implement ValueFormat for DAP
This patch implements ValueFormat for DAP.  Currently this only means
supporting "hex".

Note that StackFrameFormat is defined to have many more options, but
none are currently recognized.  It isn't entirely clear how these
should be handled.  I'll file a new gdb bug for this, and perhaps an
upstream DAP bug as well.

New in v2:
- I realized that the "hover" context was broken, and furthermore
  that we only had tests for "hover" failing, not for it succeeding.
  This version fixes the oversight and adds a test.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30469
2023-08-01 13:03:31 -06:00
Tom Tromey
2128d888b8 Respect supportsMemoryReferences in DAP
I noticed that the support for memoryReference in the "variables"
output is gated on the client "supportsMemoryReferences" capability.

This patch implements this and makes some other changes to the DAP
memory reference code:

* Remove the memoryReference special case from _SetResult.
  Upstream DAP fixed this oversight in response to
  https://github.com/microsoft/debug-adapter-protocol/issues/414

* Don't use the address of a variable as its memoryReference -- only
  emit this for pointer types.  There's no spec support for the
  previous approach.

* Use strip_typedefs to handle typedefs of pointers.
2023-08-01 13:00:57 -06:00
Tom Tromey
af93035b27 Add DAP support for C++ exceptions
This adds DAP support for the various C++ exception-catching
operations.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30682
2023-08-01 12:59:02 -06:00
Tom Tromey
21db866dc6 Implement DAP 'terminated' event
This implements the DAP 'terminated' event.  Vladimir Makaev noticed
that VSCode will not report the debug session as over unless this is
sent.

It's not completely clear when exactly this event ought to be sent.
Here I've done it when the inferior exits.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30681
2023-08-01 12:56:52 -06:00
Tom Tromey
8a8a5ccadd Do not send "new breakpoint" event when breakpoint is set
When the DAP client sets a breakpoint, gdb currently sends a "new
breakpoint" event.  However, Vladimir Makaev discovered that this
causes VSCode to think there are two breakpoints.

This patch changes gdb to suppress the event in this case.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30678
2023-08-01 12:54:52 -06:00
Tom Tromey
e982d96cd6 Move DAP breakpoint event code to breakpoint.py
A subsequent patch will add the ability to suppress breakpoint events
to DAP.  My first attempt at this ended up with recurse imports,
causing Python failures.  So, this patch moves all the DAP breakpoint
event code to breakpoint.py in preparation for the change.

I've renamed breakpoint_descriptor here as well, because it can now be
private to breakpoint.py.
2023-08-01 12:54:44 -06:00
Tom Tromey
65403bd0ed Full paths in DAP stackTrace responses
Vladimir Makaev noticed that, in some cases, a DAP stackTrace response
would include a relative path name for the "path" component.

This patch changes the frame decorator code to add a new DAP-specific
decorator, and changes the DAP entry point to frame filters to use it.
This decorator prefers the symtab's full name, and does not fall back
to the solib's name.

I'm not entirely happy with this patch, because if a user frame filter
uses FrameDecorator, it may still do the wrong thing.  It would be
better to have frame filters return symtab-like objects instead, or to
have a separate method to return the full path to the source file.

I also tend to think that the solib fallback behavior of
FrameDecorator is a mistake.  If this is ever needed, it seems to me
that it should be a separate method.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30665
2023-08-01 12:52:26 -06:00
Tom Tromey
f131a57908 Add "cwd" parameter to DAP launch request
This adds the "cwd" parameter to the DAP launch request.

This came up here:
    https://github.com/eclipse-cdt-cloud/cdt-gdb-adapter/issues/90
... and seemed like a good idea.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
2023-08-01 12:52:26 -06:00
Tom Tromey
f3337b1e94 Rename private member of FrameDecorator
In Python, a member name starting with "__" is specially handled to
make it "more private" to the class -- it isn't truly private, but it
is renamed to make it less likely to be reused by mistake.  This patch
ensures that this is done for the private method of FrameDecorator.
2023-08-01 12:52:26 -06:00
Simon Farre
a18b53a8f6 Add thread exited event
Reports a thread exit according to the DAP spec:
https://microsoft.github.io/debug-adapter-protocol/specification#Events_Thread

This patch requires the ThreadExitedEvent to be checked in,
in order to work. That patch is found here https://sourceware.org/pipermail/gdb-patches/2023-June/200071.html

Formatted correctly using black

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

Approved-By: Tom Tromey <tom@tromey.com>
2023-08-01 18:14:59 +02:00
Tom Tromey
30c01bb104 Restore previous sigmask in gdb.block_signals
Tom de Vries found a bug where, sometimes, a SIGCHLD would be
delivered to a non-main thread, wreaking havoc.

The problem is that gdb.block_signals after first blocking a set of
signals, then unblocked the same set rather than restoring the initial
situation.  This function being called from the DAP thread lead to
SIGCHLD being unblocked there.

This patch fixes the problem by restoring the previous set of signals
instead.

Tested-by: Tom de Vries <tdevries@suse.de>
Reviewed-By: Tom de Vries <tdevries@suse.de>
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30680
2023-07-31 06:35:32 -06:00
Tom Tromey
2f01a2b9ea Report supportsBreakpointLocationsRequest
While looking at the DAP spec, I noticed that the breakpointLocations
request is gated behind a capability.  This patch changes gdb to
report this capability.

I've also added a comment to explain the fact that arguments to
breakpointLocations are not optional, even though the spec says they
are.
2023-07-27 10:35:00 -06:00
Tom Tromey
338b21b088 Use 'name' in DAP start_thread function
The DAP start_thread helper function has a 'name' parameter that is
unused.  Apparently I forgot to hook it up to the thread constructor.
This patch fixes the oversight.
2023-07-23 15:08:40 -06:00
Tom Tromey
560c121c20 Export gdb.block_signals and create gdb.Thread
While working on an experiment, I realized that I needed the DAP
block_signals function.  I figured other developers may need it as
well, so this patch moves it from DAP to the gdb module and exports
it.

I also added a new subclass of threading.Thread that ensures that
signals are blocked in the new thread.

Finally, this patch slightly rearranges the documentation so that
gdb-side threading issues and functions are all discussed in a single
node.
2023-07-23 14:33:44 -06:00
Tom Tromey
8a35f6b30a Implement DAP modules request
This implements the DAP "modules" request, and also arranges to add
the module ID to stack frames.
2023-07-21 12:05:30 -06:00
Tom Tromey
672c55ddcf Remove unused imports
I noticed an unused import in dap/evaluate.py; and also I found out
that my recent changes to use frame filters from DAP left some unused
imports in dap/bt.py.
2023-07-21 12:05:30 -06:00
Tom Tromey
812e7caf60 Add instruction bytes to DAP disassembly response
The DAP disassemble command lets the client return the underlying
bytes of the instruction in an implementation-defined format.  This
patch updates gdb to return this, and simply uses a hex string of the
bytes as the format.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
2023-07-21 09:30:12 -06:00
Tom Tromey
c38bda5104 Handle typedefs in no-op pretty printers
The no-ops pretty-printers that were introduced for DAP have a classic
gdb bug: they neglect to call check_typedef.  This will cause some
strange behavior; for example not showing the children of a variable
whose type is a typedef of a structure type.  This patch fixes the
oversight.
2023-07-10 13:17:31 -06:00
Tom Tromey
5b86f10883 Reimplement DAP stack traces using frame filters
This reimplements DAP stack traces using frame filters.  This slightly
simplifies the code, because frame filters and DAP were already doing
some similar work.  This also renames RegisterReference and
ScopeReference to make it clear that these are private (and so changes
don't have to worry about other files).

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30468
2023-07-10 13:17:30 -06:00
Tom Tromey
f921fe9318 Simplify FrameVars
FrameVars implements its own variant of Symbol.is_variable.  This
patch replaces this code.
2023-07-10 13:17:30 -06:00
Tom Tromey
4a1311ba0c Fix oversights in frame decorator code
The frame decorator "FrameVars" code misses a couple of cases,
discovered when working on related DAP changes.

First, fetch_frame_locals does not stop when reaching a function
boundary.  This means it would return locals from any enclosing
functions.

Second, fetch_frame_args assumes that all arguments are at the
outermost scope, but this doesn't seem to be required by gdb.
2023-07-10 13:17:30 -06:00
Tom Tromey
70ef91c5aa Add new interface to frame filter iteration
This patch adds a new function, frame_iterator, that wraps the
existing code to find and execute the frame filters.  However, unlike
execute_frame_filters, it will always return an iterator -- whereas
execute_frame_filters will return None if no frame filters apply.

Nothing uses this new function yet, but it will used by a subsequent
DAP patch.
2023-07-10 13:17:30 -06:00
Tom Tromey
7355e1a74d Fix execute_frame_filters doc string
When reading the doc string for execute_frame_filters, I wasn't sure
if the ranges were inclusive or exclusive.  This patch updates the doc
string to reflect my findings, and also fixes an existing typo.
2023-07-10 13:17:30 -06:00
Tom Tromey
de5dfbe91c Fix result of DAP setExpression
A co-worker, Andry, noticed that the DAP setExpression implementation
returned the wrong fields -- it used "result" rather than "value", and
included "memoryReference", which isn't in the spec (an odd oversight,
IMO).

This patch fixes the problems.
2023-07-07 13:56:52 -06:00
Tom Tromey
1d2ee87e60 Remove some Python 2 code
I found some Python 2 compatibility code in gdb's Python library.
There's no need for this any more, so this removes it.  There is still
a bit more of this remaining in __init__.py, but I haven't tried
removing that yet.

Reviewed-By: Bruno Larsen <blarsen@redhat.com>
2023-06-28 09:02:56 -06:00
Tom Tromey
d8a001f570 Implement DAP "hover" context
A DAP client can request that an expression be evaluated in "hover"
context, meaning that it should not cause side effects.  In gdb, this
can be implemented by temporarily setting a few "may-" parameters to
"off".

In order to make this work, I had to also change "may-write-registers"
so that it can be changed while the program is running.  I don't think
there was any reason for this prohibition in the first place.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30476
2023-06-22 09:46:24 -06:00
Tom Tromey
0aafd5d038 Implement DAP logging breakpoints
DAP allows a source breakpoint to specify a log message.  When this is
done, the breakpoint acts more like gdb's dprintf: it logs a message
but does not cause a stop.

I looked into implement this using dprintf with the new %V printf
format.  However, my initial attempt at this did not work, because
when the inferior is continued, the dprintf output is captured by the
gdb.execute call.  Maybe this could be fixed by having all
inferior-continuation commands use the "&" form; the main benefit of
this would be that expressions are only parsed a single time.
2023-06-22 09:46:24 -06:00
Tom Tromey
59e75852dd Handle supportsVariablePaging in DAP
A bug report about the supportsVariablePaging capability in DAP
resulted in a clarification: when this capability is not present, DAP
implementations should ignore the paging parameters to the "variables"
request.  This patch implements this clarification.
2023-06-22 09:46:23 -06:00
Tom Tromey
5ad513ae62 Implement type checking for DAP breakpoint requests
I realized that with a small refactoring, it is possible to type-check
the parameters to the various DAP breakpoint requests.  This would
have caught the earlier bug involving hitCondition.
2023-06-22 09:46:23 -06:00
Tom Tromey
c1dad46f35 Handle exceptions when creating DAP breakpoints
When creating a DAP breakpoint, a failure should be returned by
setting "verified" to False.  gdb didn't properly implement this, and
there was a FIXME comment to this effect.  This patch fixes the
problem.
2023-06-22 09:46:23 -06:00
Tom Tromey
44fc43e5c8 Reuse breakpoints more frequently in DAP
The DAP breakpoint code tries to reuse a breakpoint when possible.
Currently it uses the condition and the hit condition (aka ignore
count) when making this determination.  However, these attributes are
just going to be reset anyway, so this patch changes the code to
exclude these from the reuse decision.
2023-06-22 09:46:23 -06:00
Tom Tromey
32594d975a Fix type of DAP hitCondition
DAP specifies a breakpoint's hitCondition as a string, meaning it is
an expression to be evaluated.  However, gdb implemented this as if it
were an integer instead.  This patch fixes this oversight.
2023-06-22 09:46:23 -06:00
Simon Farre
596c507f5d gdb/DAP Few bug fixes & Evaluate Array Watch vars
v2.

EvaluateResult does not need a name, just as what Tom pointed out in
previous review. It's only the *children* that need to be made sure that
their names are valid. An identifier for a variable, can't ever have an
integer as a name, anyhow (not as far as I am aware, no programming
languages allow for that).

Removed the f-strings and use str() instead as pointed out that
f-strings might not be supported fully.

v1.

This patch fixes a few bugs.

First of all, name of VariableReferences must always be of string type.
This patch makes sure that this is the case by formatting the name. If
(when) the name is an integer, this will cause clients to fail or throw
errors.

Fixes a bug in NoOpArrayPrinter that calculated children to be N, but
only ever retrieves N-1 children, which makes Python at some time later
(during fetch_children -> fetch_one_child(N) ) raise an exception (out
of list index) which makes the entire request go bad.

The result[self.result_name] also f-strings the printer.to_string()
value, because this can potentially be a LazyString (which is a Python
object, not a string) and is not serializable by json.dumps.

Approved-By: Tom Tromey <tom@tromey.com>
2023-06-22 17:42:27 +02:00
Simon Farre
ce65796b17 Fixes f1a614dc8f
Fixes failure reported by buildbot regarding ill-formatted Python code.
2023-06-19 17:06:05 +02:00
Simon Farre
f1a614dc8f gdb/dap - Getting thread names
Renamed thread_name according to convention (_ first)

When testing firefox tests, it is apparent that
_get_threads returns threads with name field = None.

I had initially thought that this was due to Firefox setting the names
using /proc/pid/task/tid/comm, by writing directly to the proc fs the
names, but apparently GDB seems to catch this, because I re-wrote
the basic-dap.exp/c to do this specifically and it saw the changes.

So I couldn't determine right now, what operation of name change that
GDB does not pick up, but with this patch, GDB will pick up the thread
names for an applications that set the name of a thread in ways that
aren't obvious.
2023-06-19 16:08:45 +02:00
Tom Tromey
a1ef65231b Remove f-strings from DAP
Kévin pointed out that gdb claims a minimum Python version of 3.2, but
the DAP code uses f-strings, which were added in 3.6.

This patch removes the uses of f-strings from the DAP code.  I can't
test an older version of Python, but I did confirm that this still
works with the version I have.
2023-06-12 12:24:07 -06:00
Tom Tromey
d294a0fc26 Implement DAP conditional breakpoints
I realized that I had only implemented DAP breakpoint conditions for
exception breakpoints, and not other kinds of breakpoints.  This patch
corrects the oversight.
2023-06-12 12:10:15 -06:00
Tom Tromey
7cb909c409 Do not report totalFrames from DAP stackTrace request
Currently, gdb will unwind the entire stack in response to the
stackTrace request.  I had erroneously thought that the totalFrames
attribute was required in the response.  However, the spec says:

    If omitted or if `totalFrames` is larger than the available
    frames, a client is expected to request frames until a request
    returns less frames than requested (which indicates the end of the
    stack).

This patch removes this from the response in order to improve
performance when the stack trace is very long.
2023-06-12 12:10:15 -06:00
Tom Tromey
3c453cfb19 Implement DAP breakpointLocations request
This implements the DAP breakpointLocations request.
2023-06-12 12:10:15 -06:00
Tom Tromey
ad9cdfbcfd Add "stop at main" extension to DAP launch request
Co-workers who work on a program that uses DAP asked for the ability
to have gdb stop at the main subprogram when launching.  This patch
implements this extension.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
2023-06-12 12:10:10 -06:00
Tom Tromey
67efac36f1 Add "target" parameter to DAP attach request
This adds a new "target" to the DAP attach request.  This is passed to
"target remote".  I thought "attach" made the most sense for this,
because in some sense gdb is attaching to a running process.  It's
worth noting that all DAP "attach" parameters are defined by the
implementation.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
2023-06-12 12:09:56 -06:00
Tom Tromey
8115dffa1e Handle DAP supportsVariableType capability
A DAP client can report the supportsVariableType capability in the
initialize request.  In this case, gdb can include the type of a
variable or expression in various results.
2023-06-12 12:09:46 -06:00
Tom Tromey
c2a0d767db Implement DAP setExpression request
This implements the DAP setExpression request.
2023-06-12 12:09:45 -06:00
Tom Tromey
510586589e Add type-checking to DAP requests
It occurred to me recently that gdb's DAP implementation should
probably check the types of objects coming from the client.  This
patch implements this idea by reusing Python's existing type
annotations, and supplying a decorator that verifies these at runtime.

Python doesn't make it very easy to do runtime type-checking, so the
core of the checker is written by hand.  I haven't tried to make a
fully generic runtime type checker.  Instead, this only checks the
subset that is needed by DAP.  For example, only keyword-only
functions are handled.

Furthermore, in a few spots, it wasn't convenient to spell out the
type that is accepted.  I've added a couple of comments to this effect
in breakpoint.py.

I've tried to make this code compatible with older versions of Python,
but I've only been able to try it with 3.9 and 3.10.
2023-06-12 12:09:28 -06:00
Tom Tromey
5e20b4cd17 Use tuples for default arguments in DAP
My co-worker Kévin taught me that using a mutable object as a default
argument in Python is somewhat dangerous, because the object is
created a single time (when the function is defined), and so if it is
mutated in the body of the function, the changes will stick around.

This patch changes the cases like this in DAP to use () rather than []
as the default.  This patch is merely preventative, as no bugs like
this are in the code.
2023-06-12 12:09:28 -06:00
Tom Tromey
5c7cdc95aa Fix a latent bug in DAP request decorator
The 'request' decorator is intended to also ensure that the request
function runs in the DAP thread.  However, the unwrapped function is
installed in the global request map, so the wrapped version is never
called.  This patch fixes the bug.
2023-06-12 12:09:28 -06:00
Tom Tromey
8c8701f9cf Rename one DAP function
When I first started implementing DAP, I had some vague plan of having
the implementation functions use the same name as the request.  I
abandoned this idea, but one vestige remained.  This patch renames the
one remaining function to be gdb-ish.
2023-06-12 12:09:28 -06:00
Tom Tromey
fc2d72afc0 Add singleThread support to some DAP requests
A few DAP requests support a "singleThread" parameter, which is
somewhat similar to scheduler-locking.  This patch implements support
for this.
2023-06-12 12:09:28 -06:00