aboutsummaryrefslogtreecommitdiff
path: root/gdb/python
AgeCommit message (Collapse)AuthorFilesLines
2023-07-23Use 'name' in DAP start_thread functionTom Tromey1-1/+1
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-23Export gdb.block_signals and create gdb.ThreadTom Tromey2-24/+34
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-21Implement DAP modules requestTom Tromey4-1/+85
This implements the DAP "modules" request, and also arranges to add the module ID to stack frames.
2023-07-21Add Progspace.objfile_for_addressTom Tromey1-0/+27
This adds a new objfile_for_address method to gdb.Progspace. This makes it easy to find the objfile for a given address. There's a related PR; and while this change would have been sufficient for my original need, it's not clear to me whether I should close the bug. Nevertheless I think it makes sense to at least mention it here. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=19288 Reviewed-By: Eli Zaretskii <eliz@gnu.org>
2023-07-21Remove unused importsTom Tromey2-4/+0
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-21Fix typo in py-type.c docstringTom Tromey1-1/+1
I noticed that a doc string py-type.c says "an signed". This patch corrects it to "a signed".
2023-07-21Add instruction bytes to DAP disassembly responseTom Tromey1-1/+4
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-19Fix gdb.Inferior.read_memory without execution (PR dap/30644)Pedro Alves1-4/+4
Andrew reported that the previous change to gdb.Inferior.read_memory & friends introducing scoped_restore_current_inferior_for_memory broke gdb.dap/stop-at-main.exp. This is also reported as PR dap/30644. The root of the problem is that all the methods that now use scoped_restore_current_inferior_for_memory cause GDB to crash with a failed assert if they are run on an inferior that is not yet started. E.g.: (gdb) python i = gdb.selected_inferior () (gdb) python i.read_memory (4,4) gdb/thread.c:626: internal-error: any_thread_of_inferior: Assertion `inf->pid != 0' failed. This patch fixes the problem by removing scoped_restore_current_inferior_for_memory's ctor ptid parameter and the any_thread_of_inferior calls completely, and making scoped_restore_current_inferior_for_memory switch inferior_ptid to a pid ptid. I was a little worried that some port might be assuming inferior_ptid points at a thread in the xfer_partial memory access routines. We know that anything that supports forks must not assume that, due to how detach_breakpoints works. I looked at a number of xfer_partial implementations, and didn't see anything that is looking at inferior_ptid in a way that would misbehave. I'm thinking that we could go forward with this and just fix ports if they break. While on some ports like on AMD GPU we have thread-specific address spaces, and so when accessing memory for those address spaces, we must have the right thread context (via inferior_ptid) selected, in Inferior.read_memory, we only have the inferior to work with, so this API as is can't be used to access thread-specific address spaces. IOW, it can only be used to access the global address space that is visible to both the CPU and the GPUs. In proc-service.c:ps_xfer_memory, the other spot using scoped_restore_current_inferior_for_memory, we're always accessing per-inferior memory. If we end up using scoped_restore_current_inferior_for_memory later to set up the context to read memory from a specific thread, then we can add an alternative ctor that takes a thread_info pointer, and make inferior_ptid point to the thread, for example. New test added to gdb.python/py-inferior.exp, exercising Inferior.read_memory without execution. No regressions on native and extended-gdbserver x86_64 GNU/Linux. Reviewed-By: Tom Tromey <tom@tromey.com> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30644 Change-Id: I11309c5ddbbb51a4594cf63c21b3858bfd9aed19
2023-07-15gdb/tui: make tui_win_info::title privateAndrew Burgess1-1/+1
This commit builds on this earlier work: commit 9fe01a376b2fb096e4836e985ba316ce9dc02399 Date: Thu Jun 29 11:26:55 2023 -0600 Update TUI window title when changed and makes tui_win_info::title private, renaming to m_title at the same time. There's a new tui_win_info::title() member function to provide read-only access to the title. There should be no user visible changes after this commit. Approved-By: Tom Tromey <tom@tromey.com>
2023-07-14Use correct inferior in Inferior.read_memory et alTom Tromey1-7/+36
A user noticed that Inferior.read_memory and a few other Python APIs will always use the currently selected inferior, not the one passed to the call. This patch fixes the bug by arranging to switch to the inferior. I found this same issue in several APIs, so this fixes them all. I also added a few missing calls to INFPY_REQUIRE_VALID to these methods. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30615 Approved-By: Pedro Alves <pedro@palves.net>
2023-07-10Update TUI window title when changedTom Tromey1-1/+1
I wrote a TUI window in Python, and I noticed that setting its title did not result in a refresh, so the new title did not appear. This patch corrects this problem.
2023-07-10Handle typedefs in no-op pretty printersTom Tromey1-11/+12
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-10Reimplement DAP stack traces using frame filtersTom Tromey4-103/+79
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-10Simplify FrameVarsTom Tromey1-26/+1
FrameVars implements its own variant of Symbol.is_variable. This patch replaces this code.
2023-07-10Fix oversights in frame decorator codeTom Tromey1-4/+13
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-10Add new interface to frame filter iterationTom Tromey1-26/+59
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-10Fix execute_frame_filters doc stringTom Tromey1-7/+9
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-07Fix result of DAP setExpressionTom Tromey1-1/+13
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-04gdb: add __repr__() implementation to a few Python typesMatheus Branco Borella5-6/+160
Only a few types in the Python API currently have __repr__() implementations. This patch adds a few more of them. specifically: it adds __repr__() implementations to gdb.Symbol, gdb.Architecture, gdb.Block, gdb.Breakpoint, gdb.BreakpointLocation, and gdb.Type. This makes it easier to play around the GDB Python API in the Python interpreter session invoked with the 'pi' command in GDB, giving more easily accessible tipe information to users. An example of how this would look like: (gdb) pi >> gdb.lookup_type("char") <gdb.Type code=TYPE_CODE_INT name=char> >> gdb.lookup_global_symbol("main") <gdb.Symbol print_name=main> The gdb.Block.__repr__() method shows the first 5 symbols from the block, and then a message to show how many more were elided (if any).
2023-07-03Fix two Python calls that don't check for errorsTom Tromey1-2/+6
PyModule_AddObject steals a reference on success, but not on error, which is why we have gdb_pymodule_addobject. I found one spot still calling the former, which could in theory leak memory on failure. This patch fixes this. In the same function I found an unchecked call to PyDict_SetItemString. This patch fixes this as well. Approved-By: Andrew Burgess <aburgess@redhat.com>
2023-06-28Remove some Python 2 codeTom Tromey2-13/+3
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-22Implement DAP "hover" contextTom Tromey1-0/+13
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-22Implement DAP logging breakpointsTom Tromey1-3/+50
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-22Handle supportsVariablePaging in DAPTom Tromey1-1/+6
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-22Implement type checking for DAP breakpoint requestsTom Tromey1-46/+82
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-22Handle exceptions when creating DAP breakpointsTom Tromey1-33/+56
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-22Reuse breakpoints more frequently in DAPTom Tromey1-2/+4
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-22Fix type of DAP hitConditionTom Tromey1-4/+7
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-22gdb/DAP Few bug fixes & Evaluate Array Watch varsSimon Farre2-2/+2
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-20Use unique_xmalloc_ptr for mi_parse::commandTom Tromey1-1/+1
This changes mi_parse::command to be a unique_xmalloc_ptr and fixes up all the uses. This avoids some manual memory management. std::string is not used here due to how the Python API works -- this approach avoids an extra copy there. Reviewed-by: Keith Seitz <keiths@redhat.com>
2023-06-19Fixes f1a614dc8f015743e9fe7fe5f3f019303f8db718Simon Farre1-0/+2
Fixes failure reported by buildbot regarding ill-formatted Python code.
2023-06-19gdb/Python: Added ThreadExitedEventSimon Farre5-0/+33
v6: Fix comments. Fix copyright Remove unnecessary test suite stuff. save_var had to stay, as it mutates some test suite state that otherwise fails. v5: Did what Tom Tromey requested in v4; which can be found here: https://pi.simark.ca/gdb-patches/87pmjm0xar.fsf@tromey.com/ v4: Doc formatting fixed. v3: Eli: Updated docs & NEWS to reflect new changes. Added a reference from the .ptid attribute of the ThreadExitedEvent to the ptid attribute of InferiorThread. To do this, I've added an anchor to that attribute. Tom: Tom requested that I should probably just emit the thread object; I ran into two issues for this, which I could not resolve in this patch; 1 - The Thread Object (the python type) checks it's own validity by doing a comparison of it's `thread_info* thread` to nullptr. This means that any access of it's attributes may (probably, since we are in "async" land) throw Python exceptions because the thread has been removed from the thread object. Therefore I've decided in v3 of this patch to just emit most of the same fields that gdb.InferiorThread has, namely global_num, name, num and ptid (the 3-attribute tuple provided by gdb.InferiorThread.ptid). 2 - A python user can hold a global reference to an exiting thread. Thus in order to have a ThreadExit event that can provide attribute access reliably (both as a global reference, but also inside the thread exit handler, as we can never guarantee that it's executed _before_ the thread_info pointer is removed from the gdbpy thread object), the `thread_info *` thread pointer must not be null. However, this comes at the cost of gdb.InferiorThread believing it is "valid" - which means, that if a user holds takes a global reference to that exiting event thread object, they can some time later do `t.switch()` at which point GDB will 'explode' so to speak. v2: Fixed white space issues and NULL/nullptr stuff, as requested by Tom Tromey. v1: Currently no event is emitted for a thread exit. This adds this functionality by emitting a new gdb.ThreadExitedEvent. It currently provides four attributes: - global_num: The GDB assigned global thread number - num: the per-inferior thread number - name: name of the thread or none if not set - ptid: the PTID of the thread, a 3-attribute tuple, identical to InferiorThread.ptid attribute Added info to docs & the NEWS file as well. Added test to test suite. Fixed formatting. Feedback wanted and appreciated.
2023-06-19gdb/dap - Getting thread namesSimon Farre1-1/+7
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-12Remove f-strings from DAPTom Tromey4-4/+4
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-12Implement DAP conditional breakpointsTom Tromey1-16/+53
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-12Do not report totalFrames from DAP stackTrace requestTom Tromey1-7/+5
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-12Implement DAP breakpointLocations requestTom Tromey2-0/+46
This implements the DAP breakpointLocations request.
2023-06-12Add "stop at main" extension to DAP launch requestTom Tromey1-1/+12
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-12Add "target" parameter to DAP attach requestTom Tromey1-2/+8
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-12Handle DAP supportsVariableType capabilityTom Tromey3-1/+14
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-12Implement DAP setExpression requestTom Tromey1-1/+23
This implements the DAP setExpression request.
2023-06-12Add gdb.Value.assign methodTom Tromey1-0/+30
This adds an 'assign' method to gdb.Value. This allows for assignment without requiring the use of parse_and_eval. Reviewed-By: Eli Zaretskii <eliz@gnu.org>
2023-06-12Add type-checking to DAP requestsTom Tromey10-28/+161
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-12Use tuples for default arguments in DAPTom Tromey2-3/+3
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-12Fix a latent bug in DAP request decoratorTom Tromey1-2/+3
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-12Rename one DAP functionTom Tromey1-1/+1
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-12Add singleThread support to some DAP requestsTom Tromey1-12/+33
A few DAP requests support a "singleThread" parameter, which is somewhat similar to scheduler-locking. This patch implements support for this.
2023-06-12Implement DAP stepOut requestTom Tromey1-0/+6
This implements the DAP "stepOut" request.
2023-06-12Implement DAP attach requestTom Tromey1-1/+12
This implements the DAP "attach" request. Note that the copyright dates on the new test source file are not incorrect -- this was copied verbatim from another directory. Reviewed-By: Eli Zaretskii <eliz@gnu.org>
2023-06-12Implement DAP setExceptionBreakpoints requestTom Tromey2-7/+62
This implements the DAP setExceptionBreakpoints request for Ada. This is a somewhat minimal implementation, in that "exceptionOptions" are not implemented (or advertised) -- I wasn't completely sure how this feature is supposed to work. I haven't added C++ exception handling here, but it's easy to do if needed. This patch relies on the new MI command execution support to do its work.