Commit Graph

15695 Commits

Author SHA1 Message Date
Michał Górny c18784ba33 [lldb] [llgs] Implement the vKill packet
Implement the support for the vKill packet.  This is the modern packet
used by the GDB Remote Serial Protocol to kill one of the debugged
processes.  Unlike the `k` packet, it has well-defined semantics.

The `vKill` packet takes the PID of the process to kill, and always
replies with an `OK` reply (rather than the exit status, as LLGS does
for `k` packets at the moment).  Additionally, unlike the `k` packet
it does not cause the connection to be terminated once the last process
is killed — the client needs to close it explicitly.

Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.llvm.org/D127667
2022-06-24 17:20:23 +02:00
Michał Górny e8fe7e930a [lldb] [llgs] Make `k` kill all processes, and fix multiple exits
Modify the behavior of the `k` packet to kill all inferiors rather than
just the current one.  The specification leaves the exact behavior
of this packet up to the implementation but since vKill is specifically
meant to be used to kill a single process, it seems logical to use `k`
to provide the alternate function of killing all of them.

Move starting stdio forwarding from the "running" response
to the packet handlers that trigger the process to start.  This avoids
attempting to start it multiple times when multiple processes are killed
on Linux which implicitly causes LLGS to receive "started" events
for all of them.  This is probably also more correct as the ability
to send "O" packets is implied by the continue-like command being issued
(and therefore the client waiting for responses) rather than the start
notification.

Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.llvm.org/D127500
2022-06-24 17:20:23 +02:00
Jonas Devlieghere 6fa9120080
[lldb] Fix up Objective-C ISA pointers
Support stripping the PAC bits from Objective-C ISA pointers in the
Objective-C runtime plugin.
2022-06-23 14:16:29 -07:00
Jim Ingham a1f20da315 The help string for stop-on-shared-library-load was copied to stop-on-exec.
Fix it so it says it does what it does.
2022-06-23 13:54:50 -07:00
Jonas Devlieghere 70841b97eb
[lldb] Make thread safety the responsibility of the log handlers
Drop the thread-safe flag and make the locking strategy the
responsibility of the individual log handler.

Previously we got away with a non-thread safe mode because we were using
unbuffered streams that rely on the underlying syscalls/OS for
synchronization. With the introduction of log handlers, we can have
arbitrary logic involved in writing out the logs. With this patch the
log handlers can pick the most appropriate locking strategy for their
particular implementation.

Differential revision: https://reviews.llvm.org/D127922
2022-06-23 09:12:05 -07:00
Jonas Devlieghere 09dea54669
[lldb] Support a buffered logging mode
This patch adds a buffered logging mode to lldb. A buffer size can be
passed to `log enable` with the -b flag. If no buffer size is specified,
logging is unbuffered.

Differential revision: https://reviews.llvm.org/D127986
2022-06-23 09:12:01 -07:00
David Spickett 5e7ddb0ddf Revert "[LLDB] Handle DIE with DW_AT_low_pc and empty ranges"
This reverts commit 1beededc0e7d86d09cee972f0b9f0030a139cab4.

Due to failures on the Arm/AArch64 build bots:
https://lab.llvm.org/buildbot/#/builders/96/builds/25032
2022-06-23 10:33:05 +00:00
Jonas Devlieghere d95c406c20
[lldb] Fix off-by-one error in the AppleObjCRuntimeV2 utility function
Fix an off-by-one error in the utility function used to extract the
dynamic class info. This resulted in a buffer overflow in the inferior
which interrupted our utility function.

Differential revision: https://reviews.llvm.org/D128377
2022-06-22 13:57:24 -07:00
Jonas Devlieghere f0d87dfe30
[lldb] Return the correct utility function in AppleObjCRuntimeV2
A copy/paste error in GetClassInfoUtilityFunction resulted in the wrong
utility function being returned: copyRealizedClassList instead of
getRealizedClassList_trylock.

Differential revision: https://reviews.llvm.org/D128378
2022-06-22 13:57:23 -07:00
Jim Ingham bae10a6bbb Fix a bug with "process continue -b" when no breakpoints are
passed.  I was passing the empty list of breakponts to the
VerifyBreakpointList routine, but that treats empty as "choose
the default breakpoint" which we don't want here.
2022-06-22 12:18:07 -07:00
Walter Erquinigo efbfde0dd0 [trace] Add an option to dump instructions in json and to a file
In order to provide simple scripting support on top of instruction traces, a simple solution is to enhance the `dump instructions` command and allow printing in json and directly to a file. The format is verbose and not space efficient, but it's not supposed to be used for really large traces, in which case the TraceCursor API is the way to go.

- add a -j option for printing the dump in json
- add a -J option for pretty printing the json output
- add a -F option for specifying an output file
- add a -a option for dumping all the instructions available starting at the initial point configured with the other flags
- add tests for all cases
- refactored the instruction dumper and abstracted the actual "printing" logic. There are two writer implementations: CLI and JSON. This made the dumper itself much more readable and maintanable

sample output:

```
(lldb) thread trace dump instructions  -t -a --id 100 -J
[
  {
    "id": 100,
    "tsc": "43591204528448966"
    "loadAddress": "0x407a91",
    "module": "a.out",
    "symbol": "void std::deque<Foo, std::allocator<Foo>>::_M_push_back_aux<Foo>(Foo&&)",
    "mnemonic": "movq",
    "source": "/usr/include/c++/8/bits/deque.tcc",
    "line": 492,
    "column": 30
  },
  ...
```

Differential Revision: https://reviews.llvm.org/D128316
2022-06-22 11:14:22 -07:00
Alexander Yermolovich 130167ed1e [LLDB] Handle DIE with DW_AT_low_pc and empty ranges
The case comes out of how BOLT handles transformation of
DW_AT_low_pc/DW_AT_high_pc into DW_AT_low_pc/DW_AT_high_pc
with latter being 0.

Reviewed By: clayborg

Differential Revision: https://reviews.llvm.org/D127889
2022-06-22 10:54:25 -07:00
Jim Ingham 4298b1b8d1 Add a "-b" option to "process continue" to run to a set of breakpoints,
temporarily ignoring the others.

Differential Revision: https://reviews.llvm.org/D126513
2022-06-22 09:55:30 -07:00
Alvin Wong 3c867898c7 [lldb] Add setting to override PE/COFF ABI by module name
The setting `plugin.object-file.pe-coff.module-abi` is a string-to-enum
map that allows specifying an ABI to a module name. For example:

    ucrtbase.dll=msvc
    libstdc++-6.dll=gnu

This allows for debugging a process which mixes both modules built using
the MSVC ABI and modules built using the MinGW ABI.

Depends on D127048

Reviewed By: DavidSpickett

Differential Revision: https://reviews.llvm.org/D127234
2022-06-22 17:16:06 +03:00
Alvin Wong 4d12378395 [lldb][windows] Fix crash on getting nested exception
LLDB tries to follow `EXCEPTION_RECORD::ExceptionRecord` to follow the
nested exception chain. In practice this code just causes Access
Violation whenever there is a nested exception. Since there does not
appear to be any code in LLDB that is actually using the nested
exceptions, this change just removes the crashing code and adds a
comment for future reference.

Fixes https://github.com/mstorsjo/llvm-mingw/issues/292

Reviewed By: DavidSpickett

Differential Revision: https://reviews.llvm.org/D128201
2022-06-22 17:16:06 +03:00
Alvin Wong 2bae956057 [lldb] Resolve exe location for `target create`
This fixes an issue that, when you start lldb or use `target create`
with a program name which is on $PATH, or not specify the .exe suffix of
a program in the working directory on Windows, you get a confusing
error, for example:

    (lldb) target create notepad
    error: 'C:\WINDOWS\SYSTEM32\notepad.exe' doesn't contain any 'host'
    platform architectures: i686, x86_64, i386, i386

Fixes https://github.com/mstorsjo/llvm-mingw/issues/265

Reviewed By: DavidSpickett

Differential Revision: https://reviews.llvm.org/D127436
2022-06-22 17:16:05 +03:00
Martin Storsjö 8a64dd5b06 [lldb] Fix reading i686-windows executables with GNU environment
25c8a061c5 / D127048 added an option
for setting the ABI to GNU.

When an object file is loaded, there's only minimal verification
done for the architecture spec set for it, if the object file only
provides one.

However, for i386 object files, the PECOFF object file plugin
provides two architectures, i386-pc-windows and i686-pc-windows.
This picks a totally different codepath in
TargetList::CreateTargetInternal, where it's treated as a fat
binary. This goes through more verifications to see if the
architectures provided by the object file matches what the
platform plugin supports.

The PlatformWindows() constructor explicitly adds the
"i386-pc-windows" and "i686-pc-windows" architectures (even when
running on other architectures), which allows this "fat binary
verification" to succeed for the i386 object files that provide
two architectures.

However, after that commit, if the object file is advertised with
the different environment (either when lldb is built in a mingw
environment, or if that setting is set), the fat binary validation
won't accept the file any longer.

Update ArchSpec::IsEqualTo with more logic for the Windows use
cases; mismatching vendors is not an issue (they don't have any
practical effect on Windows), and GNU and MSVC environments are
compatible to the point that PlatformWindows can handle object
files for both environments/ABIs.

As a separate path forward, one could also consider to stop returning
two architecture specs from ObjectFilePECOFF::GetModuleSpecifications
for i386 files.

Differential Revision: https://reviews.llvm.org/D128268
2022-06-22 17:16:05 +03:00
Martin Storsjö 9846a1f2d4 [lldb] Remove an outdated comment. NFC.
This comment became outdated in 053eb35651
(but was moved along); that commit moved the code and the comment
to a separate function, with a separate local variable
`num_of_bytes_read`. On error, the possibly garbage value is never
copied back to the caller's reference, thus the comment is no longer
relevant (and slightly confusing as is).

Differential Revision: https://reviews.llvm.org/D128226
2022-06-22 16:11:59 +03:00
Jonas Devlieghere c866f8544c
[lldb] Add a setting to specify the preferred dynamic class info extractor o
Add a setting to configure how LLDB parses dynamic Objective-C class
metadata. By default LLDB will choose the most appropriate method for
the target OS.

Differential revision: https://reviews.llvm.org/D128312
2022-06-21 18:51:39 -07:00
Jonas Devlieghere c08f61b45e
[lldb] Instantiate lazily named classes on macOS Ventura.
Recent revisions of the Objective-C runtime changed
objc_debug_class_getNameRaw() in a way that no longer triggers lazy
names to be instantiated. This has the unintended side-effect of making
generic bridged Swift classes, such as _SwiftDeferredNSDictionary<U,V>
to become invisible to the Objective-C runtime.

This patch detects this situation and forces the names to be
instantiated by calling class_getName() and discarding the result before
calling objc_debug_class_getNameRaw() again.

Many thanks to Mike Ash for outlining the solution and Adrian for
authoring the downstream patch.

rdar://95245318

Differential revision: https://reviews.llvm.org/D128306
2022-06-21 18:51:38 -07:00
Michał Górny 13eb5b3455 [lldb] [llgs] Add a test for detach-all packet
Add a test verifying that plain 'D' packet correctly detaches all
processes.

Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.llvm.org/D127291
2022-06-21 19:47:31 +02:00
Michał Górny d6b3de7256 [lldb] [llgs] Fix signo sent with fork/vfork/vforkdone events
Fix ThreadStopInfo struct to include the signal number for all events.
Since signo was not included in the details for fork, vfork
and vforkdone stops, the code incidentally referenced the wrong union
member, resulting in wrong signo being sent.

Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.llvm.org/D127193
2022-06-21 19:47:30 +02:00
Michał Górny 5b04eb23ae [lldb] [MainLoop] Support "pending callbacks", to be called once
Support adding a "pending callback" to the main loop, that will be
called once after all the pending events are processed.  This can be
e.g. to defer destroying the process instance until its exit is fully
processed, as suggested in D127500.

Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.llvm.org/D128253
2022-06-21 19:47:30 +02:00
Michał Górny bc04d24085 [lldb] [llgs] Implement non-stop style stop notification packets
Implement the support for %Stop asynchronous notification packet format
in LLGS.  This does not implement full support for non-stop mode for
threaded programs -- process plugins continue stopping all threads
on every event.  However, it will be used to implement asynchronous
events in multiprocess debugging.

The non-stop protocol is enabled using QNonStop packet.  When it is
enabled, the server uses notification protocol instead of regular stop
replies.  Since all threads are always stopped, notifications are always
generated for all active threads and copied into stop notification
queue.

If the queue was empty, the initial asynchronous %Stop notification
is sent to the client immediately.  The client needs to (eventually)
acknowledge the notification by sending the vStopped packet, in which
case it is popped from the queue and the stop reason for the next thread
is reported.  This continues until notification queue is empty again,
in which case an OK reply is sent.

Asychronous notifications are also used for vAttach results and program
exits.  The `?` packet uses a hybrid approach -- it returns the first
stop reason synchronously, and exposes the stop reasons for remaining
threads via vStopped queue.

The change includes a test case for a program generating a segfault
on 3 threads.  The server is expected to generate a stop notification
for the segfaulting thread, along with the notifications for the other
running threads (with "no stop reason").  This verifies that the stop
reasons are correctly reported for all threads, and that notification
queue works.

Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.llvm.org/D125575
2022-06-21 19:04:20 +02:00
Emre Kultursay 6a85b9d163 Support expressions in the context of a reference
...type variable by dereferencing the variable before
evaluating the expression.

Reviewed By: labath

Differential Revision: https://reviews.llvm.org/D128126
2022-06-21 14:30:07 +02:00
Kazu Hirata ed8fceaa09 Don't use Optional::getValue (NFC) 2022-06-20 23:35:53 -07:00
Kazu Hirata 0916d96d12 Don't use Optional::hasValue (NFC) 2022-06-20 20:17:57 -07:00
Kazu Hirata 064a08cd95 Don't use Optional::hasValue (NFC) 2022-06-20 20:05:16 -07:00
Martin Storsjö c9fc4336d4 [lldb] Fix building with GCC 7 2022-06-21 00:19:09 +03:00
Jakob Johnson 50f9367960 Add LoadTraceFromFile to SBDebugger and SBTrace
Add trace load functionality to SBDebugger via the `LoadTraceFromFile` method.
Update intelpt test case class to have `testTraceLoad` method so we can take advantage of
the testApiAndSB decorator to test both the CLI and SB without duplicating code.

Differential Revision: https://reviews.llvm.org/D128107
2022-06-20 11:54:47 -07:00
Kazu Hirata ad7ce1e769 Don't use Optional::hasValue (NFC) 2022-06-20 11:49:10 -07:00
Kazu Hirata 5413bf1bac Don't use Optional::hasValue (NFC) 2022-06-20 11:33:56 -07:00
Michał Górny f8c6de8dbb [lldb] [llgs] Refactor SendStopReasonForState for multiprocess
Refactor GDBRemoteCommunicationServerLLGS::SendStopReasonForState()
to accept process as an argument rather than hardcoding
m_current_process, in order to make it work correctly for multiprocess
scenarios.

Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.llvm.org/D127497
2022-06-20 19:42:21 +02:00
Michał Górny af93f123b9 [lldb] [llgs] Refactor SendStopReplyPacketForThread for multiprocess
Refactor SendStopReplyPacketForThread() to accept process instance
as a parameter rather than use m_current_process.  This future-proofs
it for multiprocess support.

Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.llvm.org/D127289
2022-06-20 19:42:21 +02:00
Michał Górny ac570fbb85 [lldb] [llgs] Include process ID in stop responses
Include the process identifier in the `T` stop responses when
multiprocess extension is enabled (i.e. prepend it to the thread
identifier).  Use the exposed identifier to simplify the fork-and-follow
tests.

The LLDB client accounts for the possible PID since the multiprocess
extension support was added in b601c67192.

Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.llvm.org/D127192
2022-06-20 13:37:23 +02:00
Michał Górny e4d6ed58a8 [lldb] [llgs] Include process id in W/X stop reasons
Include the process identifier in W/X stop reasons when multiprocess
extensions are enabled.

The LLDB client does not support process identifiers there at the moment
but it parses packets in such a way that their presence does not cause
any problems.

Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.llvm.org/D127191
2022-06-20 13:37:23 +02:00
Kazu Hirata aa88161b37 [lldb] Use value_or instead of getValueOr (NFC) 2022-06-19 09:12:01 -07:00
Jonas Devlieghere 91688716ba
[lldb] Fix modernize-use-equals-default warnings (NFC)
Fix modernize-use-equals-default warnings. Because this check is listed
in LLDB's top level .clang-tidy configuration, the check is enabled by
default and the resulting warnings show up in my editor.

I've audited the modified lines. This is not a blind change.
2022-06-17 15:08:02 -07:00
Jonas Devlieghere 9916633997
[lldb] Fix modernize-use-override warnings (NFC)
Fix modernize-use-override warnings. Because this check is listed in
LLDB's top level .clang-tidy configuration, the check is enabled by
default and the resulting warnings show up in my editor.

I've audited the modified lines. This is not a blind change.
2022-06-17 15:08:02 -07:00
Adrian Prantl f000de8760 [LLDB][ExpressionParser] Fix indices inside format-strings passed to LLDB_LOG
llvm::formatv expects the parameter indexes to start with 0.
Unfortunately it doesn't detect out-of-bounds accesses in the format
string at compile-time, of which we had several inside ClangExpressionDeclMap.

This patch fixes these out-of-bounds format accesses.

Example output

Before

ClangExpressionDeclMap::FindExternalVisibleDecls for '$__lldb_class' in a
'TranslationUnit'
  CEDM::FEVD Searching the root namespace
  CEDM::FEVD Adding type for $__lldb_class: 1

After

ClangExpressionDeclMap::FindExternalVisibleDecls for '$__lldb_class' in
a 'TranslationUnit'
  CEDM::FEVD Searching the root namespace
  CEDM::FEVD Adding type for $__lldb_class: class (lambda)

Patch by Michael Buch!

Differential Revision: https://reviews.llvm.org/D128063
2022-06-17 09:26:01 -07:00
Jonas Devlieghere de74756571
[lldb] Remove LogHandler::Create functions (NFC)
Remove the LogHandler::Create functions. Except for the StreamHandler
they were just forwarding their arguments to std::make_shared.
2022-06-16 21:04:08 -07:00
Jonas Devlieghere 6ac608b3d8
[lldb] Add RotatingLogHandler
Add a log handler that maintains a circular buffer with a fixed size.

Differential revision: https://reviews.llvm.org/D127937
2022-06-16 15:17:40 -07:00
Jonas Devlieghere 6ff49af33d
[lldb] Introduce the concept of a log handler (NFC)
This patch introduces the concept of a log handlers. Log handlers allow
customizing the way log output is emitted. The StreamCallback class
tried to do something conceptually similar. The benefit of the log
handler interface is that you don't need to conform to llvm's
raw_ostream interface.

Differential revision: https://reviews.llvm.org/D127922
2022-06-16 13:34:28 -07:00
Jim Ingham f22db1fabf Fix StopInfoBreakpoint::ShouldNotify when a callback deletes the site we hit.
When we hit a breakpoint site all of whose owners are internal, we don't
broadcast that event to the public event queue.  However, we were checking
whether that was true in the ShouldNotify method, which gets run after the
breakpoint callbacks get run.  If the breakpoint callback deletes the site
we just hit, we no longer have the information to make that determination.

This patch just gathers the "was all internal" fact when the StopInfoBreakpoint
gets made, which happens before anyone has a chance to delete the site, and then
uses that cached value.

This bug was causing a couple of tests (including TestStopAtEntry.py) to fail
when using new the macOS Ventura dyld support.

Differential Revision: https://reviews.llvm.org/D127997
2022-06-16 11:54:11 -07:00
Walter Erquinigo ea37cd52d1 [trace][intelpt] Support system-wide tracing [22] - Some final touches
Having a member variable TraceIntelPT * makes it look as if it was
optional. I'm using instead a weak_ptr to indicate that it's not
optional and the object is under the ownership of TraceIntelPT.

Besides that, I've simplified the Perf aux and data buffers copying by
using vector.insert.

I'm also renaming Lookup2 to Lookup. The 2 in the name is confusing.

Differential Revision: https://reviews.llvm.org/D127881
2022-06-16 11:42:22 -07:00
Walter Erquinigo 9f45f23d86 [trace][intelpt] Support system-wide tracing [21] - Support long numbers in JSON
llvm's JSON parser supports 64 bit integers, but other tools like the
ones written in JS don't support numbers that big, so we need to
represent these possibly big numbers as a string. This diff uses that to
represent addresses and tsc zero. The former is printed in hex for and
the latter in decimal string form. The schema was updated mentioning
that.

Besides that, I fixed some remaining issues and now all test pass. Before I wasn't running all tests because for some reason my computer reverted perf_paranoid to 1.

Differential Revision: https://reviews.llvm.org/D127819
2022-06-16 11:42:22 -07:00
Walter Erquinigo 6a5355e8a1 [trace][intelpt] Support system-wide tracing [20] - Rename some fields in the schema
As discusses offline with @jj10305, we are updating some naming used throughout the code, specially in the json schema

- traceBuffer -> iptTrace
- core -> cpu

Differential Revision: https://reviews.llvm.org/D127817
2022-06-16 11:42:22 -07:00
Walter Erquinigo 67c2405145 [trace][intelpt] Support system-wide tracing [19] - Some other minor improvements
This addresses the issues in diffs [13], [14] and [16]

- Add better documentation
- Fix some castings by making them safer
- Simplify CorrelateContextSwitchesAndIntelPtTraces
- Rename some functions

Differential Revision: https://reviews.llvm.org/D127804
2022-06-16 11:42:21 -07:00
Walter Erquinigo 561a61fb26 [trace][intelpt] Support system-wide tracing [18] - some more improvements
This applies the changes requested for diff 12.

- use DenseMap<ConstString, _> instead of std::unordered_map<ConstString, _>, which is more idiomatic and possibly performant.
- deduplicate some code in Trace.cpp by using helper functions for fetching in maps
- stop using size and offset when fetching binary data, because we in fact read the entire buffers all the time. If we ever need streaming, we can implement it then. Now, the size is used only to check that we are getting the correct amount of data. This is useful because in some cases determining the size doesn't involve fetching the actual data.
- added back the x86_64 macro to the perf tests
- added more documentation
- simplified some file handling
- fixed some comments

Differential Revision: https://reviews.llvm.org/D127752
2022-06-16 11:42:21 -07:00
Walter Erquinigo 03cc58ff2a [trace][intelpt] Support system-wide tracing [17] - Some improvements
This improves several things and addresses comments up to the diff [11] in this stack.

- Simplify many functions to receive less parameters that they can identify easily
- Create Storage classes for Trace and TraceIntelPT that can make it easier to reason about what can change with live process refreshes and what cannot.
- Don't cache the perf zero conversion numbers in lldb-server to make sure we get the most up-to-date numbers.
- Move the thread identifaction from context switches to the bundle parser, to leave TraceIntelPT simpler. This also makes sure that the constructor of TraceIntelPT is invoked when the entire data has been checked to be correct.
- Normalize all bundle paths before the Processes, Threads and Modules are created, so that they can assume that all paths are correct and absolute
- Fix some issues in the tests. Now they all pass.
- return the specific instance when constructing PerThread and MultiCore processor tracers.
- Properly implement IntelPTMultiCoreTrace::TraceStart.
- Improve some comments.
- Use the typedef ContextSwitchTrace more often for clarity.
- Move CreateContextSwitchTracePerfEvent to Perf.h as a utility function.
- Synchronize better the state of the context switch and the intel pt
perf events.
- Use a booblean instead of an enum for the PerfEvent state.

Differential Revision: https://reviews.llvm.org/D127456
2022-06-16 11:23:02 -07:00