3748 Commits

Author SHA1 Message Date
Jacob Lalonde
e9c8f75d45
[LLDB][Minidump] Have Minidumps save off and properly read TLS data (#109477)
This patch adds the support to `Process.cpp` to automatically save off
TLS sections, either via loading the memory region for the module, or
via reading `fs_base` via generic register. Then when Minidumps are
loaded, we now specify we want the dynamic loader to be the `POSIXDYLD`
so we can leverage the same TLS accessor code as `ProcessELFCore`. Being
able to access TLS Data is an important step for LLDB generated
minidumps to have feature parity with ELF Core dumps.
2024-10-10 15:59:51 -07:00
Augusto Noronha
2ff4c25b7e Revert "[lldb] Implement basic support for reverse-continue (#99736)"
This reverts commit d5e1de6da96c1ab3b8cae68447e8ed3696a7006e.
2024-10-10 15:05:58 -07:00
Augusto Noronha
f02252e1fd Revert "[lldb] SetErrorStringWithFormatv -> FromErrorStringWithFormatv (NFC)"
This reverts commit b77fdf5799be6b29869f2f7969851709e03938ba.
2024-10-10 15:05:58 -07:00
Jonas Devlieghere
b77fdf5799
[lldb] SetErrorStringWithFormatv -> FromErrorStringWithFormatv (NFC) 2024-10-10 13:23:06 -07:00
Robert O'Callahan
d5e1de6da9
[lldb] Implement basic support for reverse-continue (#99736)
This commit only adds support for the
`SBProcess::ReverseContinue()` API. A user-accessible command for this
will follow in a later commit.

This feature depends on a gdbserver implementation (e.g. `rr`) providing
support for the `bc` and `bs` packets. `lldb-server` does not support
those packets, and there is no plan to change that. So, for testing
purposes, `lldbreverse.py` wraps `lldb-server` with a Python
implementation of *very limited* record-and-replay functionality for use
by *tests only*.

The majority of this PR is test infrastructure (about 700 of the 950
lines added).
2024-10-10 13:01:47 -07:00
Dmitry Vasilyev
2e89312419
[lldb] Removed gdbserver ports map from lldb-server (#104238)
Listen to gdbserver-port, accept the connection and run `lldb-server gdbserver --fd` on all platforms.
Parameters --min-gdbserver-port and --max-gdbserver-port are deprecated now.

This is the part 2 of #101283.

Fixes #97537.
2024-10-03 21:00:47 +04:00
Youngsuk Kim
f35719ff67
[lldb] Don't flush llvm::raw_string_ostream (NFC) (#110128)
Don't call raw_string_ostream::flush(), which is essentially a no-op. As
specified in the docs, raw_string_ostream is always unbuffered. (
65b13610a5226b84889b923bae884ba395ad084d for further reference )
2024-09-26 12:29:14 -04:00
David Spickett
3eaaf7c4d0 [lldb][AArch64] Fix crash loading core files on 32 bit systems
https://github.com/llvm/llvm-project/pull/109934 added FPMR which
uses a bit in hwcaps greater than 31. So it marked the 1 with UL
which is fine on 64 bit systems but for 32 bit UL is 4 bytes.

Use ULL so we aren't invoking undefined behaviour.
2024-09-26 12:06:23 +00:00
David Spickett
0e24611f57
[lldb][AArch64] Add register fields for the fpmr register (#109934)
The FP8 formats have a "_" in the name so that they are:
1. Easier to read.
2. Possible to use in register expressions if/when they are supported.

Some other bits do have defined meanings but they are not simple to
name. Better that folks read the manual for those.

See this page for the full details:

https://developer.arm.com/documentation/ddi0601/2024-06/AArch64-Registers/FPMR--Floating-point-Mode-Register
2024-09-26 11:44:01 +01:00
David Spickett
5ee2deac0c
[lldb][AArch64][Linux] Add Floating Point Mode Register (#106695)
Otherwise known as FEAT_FPMR. This register controls the behaviour of
floating point operations.

https://developer.arm.com/documentation/ddi0601/2024-06/AArch64-Registers/FPMR--Floating-point-Mode-Register

As the current floating point register contexts are fixed size, this has
been placed in a new set. Linux kernel patches have landed already, so
you can cross check with those.

To simplify testing we're not going to do any floating point operations,
just read and write from the program and debugger to make sure each sees
the other's values correctly.
2024-09-25 10:27:57 +01:00
dlav-sc
c93e29439b
[lldb] fix vFile:open, vFile:unlink error codes (#106950)
This patch makes gdb-server sends only GDB RSP supported error codes
during vFile:open and vFile:unlink handling.
2024-09-25 10:13:40 +01:00
David Spickett
497759e872
[lldb][AArch64] Create Neon subregs when XML only includes SVE (#108365)
Fixes #107864

QEMU decided that when SVE is enabled it will only tell us about SVE
registers in the XML, and not include Neon registers. On the grounds
that the Neon V registers can be read from the bottom 128 bits of a SVE
Z register (SVE's vector length is always >= 128 bits).

To support this we create sub-registers just as we do for S and D
registers of the V registers. Except this time we use part of the Z
registers.

This change also updates our fallback for registers with unknown types
that are > 128 bit. This is detailed in
https://github.com/llvm/llvm-project/issues/87471, though that covers
more than this change fixes.

We'll now treat any register of unknown type that is >= 128 bit as a
vector of bytes. So that the user gets to see something
even if the order might be wrong.

And until lldb supports vector and union types for registers, this is
also the only way we can get a value to apply the sub-reg to, to make
the V registers.
2024-09-24 12:40:42 +01:00
Jason Molenda
6e6d5eae76
[lldb] Don't invalid register context after setting thread pc's (#109499)
Some gdb remote serial protocol stubs will send the thread IDs and PCs
for all threads in a process in the stop-reply packet. lldb often needs
to know the pc values for all threads while at a private stop, and that
results in <n-1> read-register packets for <n> threads, and can be a big
performance problem when this is a hot code path.

GDBRemoteRegisterContext tracks the StopID of when its values were set,
and when the thread's StopID has incremented, it marks all values it has
as Invalid, and knows to refetch them.

We have a code path that resulted in setting the PCs for all the
threads, and then `ProcessGDBRemote::CalculateThreadStopInfo` *forcing*
an invalidation of all the register contexts, forcing us to re-read the
pc values for all threads except the one that stopped.

There are times when it is valid to force an invalidation of the
regsiter cache - for instance, if the layout of the registers has
changed because the processor state is different, or we've sent a
write-all-registers packet to the inferior and we want to make sure we
stay in sync with the inferior. But there was no reason for this method
to be forcing the register context to be invalid.

I added a test when running on Darwin systems, where debugserver always
sends the thread IDs and PCs, which turns on packet logging. The test
runs against an inferior which has 4 threads; it steps over a dlopen()
call, steps in to a user function with debug info, steps-over and
steps-in across source lines with multiple function calls, and then
examines the packet log and flags it as an error if lldb asked for the
pc value of any thread at any point in the debug session.

For this program and the operations we're doing, with debugserver that
provides thread IDs and PCs, we should never ask for the value of a pc
register.

rdar://136247381
2024-09-23 12:13:48 -07:00
Kelvin Lee
85220a0c65
[lldb][FreeBSD] Fix a typo in NativeProcessFreeBSD::MonitorSIGTRAP() (#109643)
Apparently a typo is causing compile error, added by https://github.com/llvm/llvm-project/pull/108504.
2024-09-23 11:44:58 +01:00
Pavel Labath
1f8a3286e0 [lldb] Only send "posix" error codes through the gdb-remote protocol
The other side has no way of telling which namespace do these codes
belong to, so mashing them all together is not very helpful.

I'm mainly doing this to simplify some code in a pending patch
<https://github.com/llvm/llvm-project/pull/106774/files#r1752628604>,
and I've picked the posix error category semi-randomly. If we wanted to
be serious about assigning meaning to these error codes, we should
create a special error category for "gdb errors".
2024-09-19 10:07:13 -07:00
Adrian Prantl
6dcde731eb Revert "[lldb] Only send "posix" error codes through the gdb-remote protocol"
This reverts commit a7c174502aef45b2d33291129cce10c085fef944.
2024-09-18 17:28:10 -07:00
Pavel Labath
a7c174502a [lldb] Only send "posix" error codes through the gdb-remote protocol
The other side has no way of telling which namespace do these codes
belong to, so mashing them all together is not very helpful.

I'm mainly doing this to simplify some code in a pending patch
<https://github.com/llvm/llvm-project/pull/106774/files#r1752628604>,
and I've picked the posix error category semi-randomly. If we wanted to
be serious about assigning meaning to these error codes, we should
create a special error category for "gdb errors".
2024-09-18 14:53:42 -07:00
Youngsuk Kim
d7796855b8
[lldb] Nits on uses of llvm::raw_string_ostream (NFC) (#108745)
As specified in the docs,
1) raw_string_ostream is always unbuffered and
2) the underlying buffer may be used directly

( 65b13610a5226b84889b923bae884ba395ad084d for further reference )

* Don't call raw_string_ostream::flush(), which is essentially a no-op.
* Avoid unneeded calls to raw_string_ostream::str(), to avoid excess
indirection.
2024-09-16 00:26:51 -04:00
Jason Molenda
65a4d11b1e
[lldb] Set the stop reason when receiving swbreak/hwbreak (#108518)
xusheng added support for swbreak/hwbreak a month ago, and no special
support was needed in ProcessGDBRemote when they're received because
lldb already marks a thread as having hit a breakpoint when it stops at
a breakpoint site. However, with changes I am working on, we need to
know the real stop reason a thread stopped or the breakpoint hit will
not be recognized.

This is similar to how lldb processes the "watch/rwatch/awatch" keys in
a thread stop packet -- we set the `reason` to `watchpoint`, and these
set it to `breakpoint` so we set the stop reason correctly later in
these methods.
2024-09-13 09:04:28 -07:00
Jason Molenda
213c59ddd2
[lldb] Add pc check for thread-step-by-bp algorithms (#108504)
lldb-server built with NativeProcessLinux.cpp and
NativeProcessFreeBSD.cpp can use breakpoints to implement instruction
stepping on cores where there is no native instruction-step primitive.
Currently these set a breakpoint, continue, and if we hit the breakpoint
with the original thread, set the stop reason to be "trace".

I am wrapping up a change to lldb's breakpoint algorithm where I change
its current behavior of

"if a thread stops at a breakpoint site, we set
the thread's stop reason to breakpoint-hit, even if the breakpoint
hasn't been executed" +
"when resuming any thread at a breakpoint site, instruction-step past
the breakpoint before resuming"

to a behavior of

"when a thread executes a breakpoint, set the stop reason to
breakpoint-hit" +
"when a thread has hit a breakpoint, when the thread resumes, we
silently step past the breakpoint and then resume the thread".

For these lldb-server targets doing breakpoint stepping, this means that
if we are sitting on a breakpoint that has not yet executed, and
instruction-step the thread, we will execute the breakpoint instruction
at $pc (instead of $next-pc where it meant to go), and stop again -- at
the same pc value. Then we will rewrite the stop reason to 'trace'. The
higher level logic will see that we haven't hit the breakpoint
instruction again, so it will try to instruction step again, hitting the
breakpoint again forever.

To fix this, I'm checking that the thread matches the one we are
instruction-stepping-by-breakpoint AND that we've stopped at the
breakpoint address we are stepping to. Only in that case will the stop
reason be rewritten to "trace" hiding the implementation detail that the
step was done by breakpoints.
2024-09-13 09:02:31 -07:00
Jacob Lalonde
492683527e
[LLDB][Minidump] Support minidumps where there are multiple exception streams (#97470)
Currently, LLDB assumes all minidumps will have unique sections. This is
intuitive because almost all of the minidump sections are themselves
lists. Exceptions including Signals are unique in that they are all
individual sections with their own directory.

This means LLDB fails to load minidumps with multiple exceptions due to
them not being unique. This behavior is erroneous and this PR introduces
support for an arbitrary number of exception streams. Additionally, stop
info was calculated only for a single thread before, and now we properly
support mapping exceptions to threads.

~~This PR is starting in DRAFT because implementing testing is still
required.~~
2024-09-09 21:07:12 -07:00
Daniil Fukalov
345cc47ba7
[NFC] Add explicit #include llvm-config.h where its macros are used, lldb part. (#107603)
(this is lldb part)

Without these explicit includes, removing other headers, who implicitly
include llvm-config.h, may have non-trivial side effects. For example,
`clangd` may report even `llvm-config.h` as "no used" in case it defines
a macro, that is explicitly used with #ifdef. It is actually amplified
with different build configs which use different set of macros.
2024-09-09 12:44:03 +02:00
Dmitry Vasilyev
5d2b337875
[lldb][NFC] Used shared_fd_t (#107553)
Replaced `int connection_fd = -1` with `shared_fd_t connection_fd =
SharedSocket::kInvalidFD`.

This is prerequisite for #104238.
2024-09-06 16:03:11 +04:00
Dmitry Vasilyev
b11a70392c [lldb] Fixed a typo in #107388 2024-09-06 14:42:28 +04:00
Christian Sigg
d4e320e6f4
[lldb][NFC] Fix -Wparentheses warning.
Fix `using the result of an assignment as a condition without parentheses` warning.
2024-09-06 11:55:24 +02:00
Dmitry Vasilyev
725fab987d
[lldb][NFC] Separated GDBRemoteCommunication::GetDebugserverPath() (#107388)
This is the prerequisite for #104238.
2024-09-06 12:38:55 +04:00
Adrian Prantl
11084c5c49 [lldb] Convert DebuggerThread.cpp to new Status API (NFC) 2024-09-05 18:00:53 -07:00
Adrian Prantl
e0a93d3505 [lldb] Convert ProcessWindows.cpp to new Status API (NFC) 2024-09-05 16:12:17 -07:00
Adrian Prantl
67fb8d15c9 [lldb] Convert ProcessDebugger.cpp to new Status API (NFC) 2024-09-05 16:12:17 -07:00
Adrian Prantl
3b426a8951 [lldb] Convert NativeProcessLinux to new Status API (NFC) 2024-09-05 12:53:08 -07:00
Adrian Prantl
7b760894f2 [lldb] Convert NativeProcessLinux to new Status API (NFC) 2024-09-05 12:49:16 -07:00
Adrian Prantl
a0dd90eb7d
[lldb] Make conversions from llvm::Error explicit with Status::FromEr… (#107163)
…ror() [NFC]
2024-09-05 12:19:31 -07:00
Jacob Lalonde
2ed510dc97
[LLDB][Minidump] Extend the minidump x86_64 registers to include fs_base and gs_base (#106767)
A follow up to #106473 Minidump wasn't collecting fs or gs_base. This
patch extends the x86_64 register context and gated reading it behind an
lldb specific flag. Additionally these registers are explicitly checked
in the tests.
2024-09-05 09:38:45 -07:00
Alexey Merzlyakov
660e34fd38
[lldb][RISCV] Support optionally disabled FPR for riscv64 (#104547)
The PR adds the support optionally enabled/disabled FP-registers to LLDB
`RegisterInfoPOSIX_riscv64`. This situation might take place for RISC-V
builds having no FP-registers, like RV64IMAC or RV64IMACV.

To aim this, patch adds `opt_regsets` flags mechanism. It re-works
RegisterInfo class to work with flexibly allocated (depending on
`opt_regsets` flag) `m_register_sets` and `m_register_infos` vectors
instead of statically defined structures. The registration of regsets is
being arranged by `m_per_regset_regnum_range` map.

The patch flows are spread to `NativeRegisterContextLinux_riscv64` and
`RegisterContextCorePOSIX_riscv64` classes, that were tested on:
 - x86_64 host working with coredumps
- RV64GC and RV64IMAC targets working with coredumps and natively in
run-time with binaries

`EmulateInstructionRISCV` is out of scope of this patch, and its
behavior did not change, using maximum set of registers.

According testcase built for RV64IMAC (no-FPR) was added to
`TestLinuxCore.py`.
2024-09-04 15:31:59 +01:00
Felipe de Azevedo Piovezan
1b0a802493
[GDBRemote] Handle 'heap' memory region info type (#105883)
This should cause the memory region info "is stack" field to be set to
"no".
2024-09-04 07:02:55 -07:00
Pavel Labath
04ed12ca3f
[lldb] Support partial memory reads on windows (#106981)
ReadProcessMemory will not perform the read if part of the memory is
unreadable (and even though the API has a `number_of_bytes_read`
argument). To make this work, I explicitly inspect the memory region
being read and only read the accessible part.
2024-09-03 10:48:40 +02:00
Pavel Labath
181cc75ea8
[lldb/linux] Make truncated reads work (#106532)
Previously, we were returning an error if we couldn't read the whole
region. This doesn't matter most of the time, because lldb caches memory
reads, and in that process it aligns them to cache line boundaries. As
(LLDB) cache lines are smaller than pages, the reads are unlikely to
cross page boundaries.

Nonetheless, this can cause a problem for large reads (which bypass the
cache), where we're unable to read anything even if just a single byte
of the memory is unreadable. This patch fixes the lldb-server to do
that, and also changes the linux implementation, to reuse any partial
results it got from the process_vm_readv call (to avoid having to
re-read everything again using ptrace, only to find that it stopped at
the same place).

This matches debugserver behavior. It is also consistent with the gdb
remote protocol documentation, but -- notably -- not with actual
gdbserver behavior (which returns errors instead of partial results). We
filed a
[clarification
bug](https://sourceware.org/bugzilla/show_bug.cgi?id=24751) several
years ago. Though we did not really reach a conclusion there, I think
this is the most logical behavior.

The associated test does not currently pass on windows, because the
windows memory read APIs don't support partial reads (I have a WIP patch
to work around that).
2024-09-02 14:44:18 +02:00
Igor Kudrin
ce7c828e08
[lldb][AArch64] Do not crash if NT_ARM_TLS is missing (#106478)
[D156118](https://reviews.llvm.org/D156118) states that this note is
always present, but it is better to check it explicitly, as otherwise
`lldb` may crash when trying to read registers.
2024-08-30 09:00:35 +01:00
Adrian Prantl
6ae657b08d [lldb] Adapt Plugins/Process/Windows to new Status API 2024-08-27 17:02:49 -07:00
Adrian Prantl
47667ee23d [lldb] Update NativeProcess*BSD to new Status API 2024-08-27 15:25:32 -07:00
Adrian Prantl
f2f78b284b [lldb] Update NativeThreadLinux to new Status API 2024-08-27 14:52:56 -07:00
Adrian Prantl
67eb72725d [lldb] Update NativeProcessLinux to new Status API 2024-08-27 14:48:16 -07:00
Adrian Prantl
0642cd768b
[lldb] Turn lldb_private::Status into a value type. (#106163)
This patch removes all of the Set.* methods from Status.

This cleanup is part of a series of patches that make it harder use the
anti-pattern of keeping a long-lives Status object around and updating
it while dropping any errors it contains on the floor.

This patch is largely NFC, the more interesting next steps this enables
is to:
1. remove Status.Clear()
2. assert that Status::operator=() never overwrites an error
3. remove Status::operator=()

Note that step (2) will bring 90% of the benefits for users, and step
(3) will dramatically clean up the error handling code in various
places. In the end my goal is to convert all APIs that are of the form

`    ResultTy DoFoo(Status& error)
`
to

`    llvm::Expected<ResultTy> DoFoo()
`
How to read this patch?

The interesting changes are in Status.h and Status.cpp, all other
changes are mostly

` perl -pi -e 's/\.SetErrorString/ = Status::FromErrorString/g' $(git
grep -l SetErrorString lldb/source)
`
plus the occasional manual cleanup.
2024-08-27 10:59:31 -07:00
Felipe de Azevedo Piovezan
8b4147d14c
[GDBRemote] Fix processing of comma-separated memory region entries (#105873)
The existing algorithm was performing the following comparisons for an
`aaa,bbb,ccc,ddd`:

aaa\0bbb,ccc,ddd == "stack"
aaa\0bbb\0ccc,ddd == "stack"
aaa\0bbb\0ccc\0ddd == "stack"

Which wouldn't work. This commit just dispatches to a known algorithm
implementation.
2024-08-23 13:09:31 -07:00
Jonas Devlieghere
9f418057dc
[lldb] Pick the correct architecutre when target and core file disagree (#105576)
In f9f3316, Adrian fixed an issue where LLDB wouldn't update the
target's architecture when the process reported a different triple that
only differed in its sub-architecture.

This unintentionally regressed core file debugging when the core file
reports the base architecture (e.g. armv7) while the main binary knows
the correct CPU subtype (e.g. armv7em). After the aforementioned change,
we update the target architecture from armv7em to armv7. Fix the issue
by trusting the target architecture over the ProcessMachCore process.

rdar://133834304
2024-08-22 08:29:48 -07:00
Jacob Lalonde
6cb14599ad
[LLDB][Minidump] Fix ProcessMinidump::GetMemoryRegions to include 64b regions when /proc/pid maps are missing. (#101086)
This PR is in response to a bug my coworker @mbucko discovered where on
MacOS Minidumps were being created where the 64b memory regions were
readable, but were not being listed in
`SBProcess.GetMemoryRegionList()`. This went unnoticed in #95312 due to
all the linux testing including /proc/pid maps. On MacOS generated dumps
(or any dump without access to /proc/pid) we would fail to properly map
Memory Regions due to there being two independent methods for 32b and
64b mapping.

In this PR I addressed this minor bug and merged the methods, but in
order to add test coverage required additions to `obj2yaml` and
`yaml2obj` which make up the bulk of this patch.

Lastly, there are some non-required changes such as the addition of the
`Memory64ListHeader` type, to make writing/reading the header section of
the Memory64List easier.
2024-08-21 10:25:23 -07:00
Dhruv Srivastava
b804516dc5
[lldb][AIX] 1. Avoid namespace collision on other platforms (#104679)
This PR is in reference to porting LLDB on AIX.

Link to discussions on llvm discourse and github:
1.  https://discourse.llvm.org/t/port-lldb-to-ibm-aix/80640
2.  #101657 

The complete changes for porting are present in this draft PR:
https://github.com/llvm/llvm-project/pull/102601

The changes on this PR are intended to avoid namespace collision for
certain typedefs between lldb and other platforms:
1. tid_t --> lldb::tid_t
2. offset_t --> lldb::offset_t
2024-08-20 10:19:32 +01:00
Fred Grim
a434cac523
[lldb] Add Populate Methods for ELFLinuxPrPsInfo and ELFLinuxPrStatus (#104109)
To create elf core files there are multiple notes in the core file that
contain these structs as the note. These populate methods take a Process
and produce fully specified structures that can be used to fill these
note sections. The pr also adds tests to ensure these structs are
correctly populated.
2024-08-16 08:53:15 -07:00
Igor Kudrin
494eec0255
[lldb][NFCI] Simplify ProcessElfCore::GetAuxvData() (#102263)
There is no need to clone the content and set extraction properties
because `m_auxv` is already in the required form.
2024-08-13 10:55:28 -07:00
xusheng
5dbec8c6ce
[lldb] Claim to support swbreak and hwbreak packets when debugging a gdbremote (#102873)
This fixes https://github.com/llvm/llvm-project/issues/56125 and
https://github.com/vadimcn/codelldb/issues/666, as well as the
downstream issue in our binary ninja debugger:
https://github.com/Vector35/debugger/issues/535

Basically, lldb does not claim to support the `swbreak` packet so the
gdbserver would not use it. As a result, the gdbserver always sends the
unmodified program counter value which, on systems like x86, causes the
program counter to be off-by-one (or otherwise wrong). For reference,
the lldb-server always sends the modified program counter value so it
works perfectly with lldb.

https://sourceware.org/gdb/current/onlinedocs/gdb.html/Stop-Reply-Packets.html#swbreak-stop-reason

No new code is added to add support `swbreak`, since the way lldb works
already expects the remote to have adjusted the program counter. The
change just lets the gdbserver know that lldb supports it, so that it
will send the adjusted program counter.

To test this PR, you can use lldb to connect to a gdbserver running on
e.g., Ubuntu 22.04, and see the program counter is off-by-one without
the patch. With the patch, things work as expected
2024-08-13 15:28:35 +01:00