Commit Graph

1790 Commits

Author SHA1 Message Date
Jonas Devlieghere
49277253f0 [lldb] Use LLVM's helper for Unicode conversion (NFC) (#112582)
The codecvt header has been deprecated in C++17. Use LLVM's unicode
helpers to convert between UTF-8 and UTF-16.
2024-10-30 09:31:32 -07:00
Wanyi
efc6d33be9 [lldb] Fix write only file action to truncate the file (#112657)
When `FileAction` opens file with write access, it doesn't clear the
file nor append to the end of the file if it already exists. Instead, it
writes from cursor index 0.

For example, by using the settings `target.output-path` and
`target.error-path`, lldb will redirect process stdout/stderr to files.
It then calls this function to write to the files which the above
symptoms appear.

## Test
- Added unit test checking the file flags
- Added 2 api tests checking
  - File content overwritten if the file path already exists
- Stdout and stderr redirection to the same file doesn't change its
behavior
2024-10-29 14:22:51 -04:00
Pavel Labath
98b419ca76 [lldb] Don't exit the main loop when in runs out of things to listen on (#112565)
This behavior made sense in the beginning as the class was completely
single threaded, so if the source count ever reached zero, there was no
way to add new ones. In https://reviews.llvm.org/D131160, the class
gained the ability to add events (callbacks) from other threads, which
means that is no longer the case (and indeed, one possible use case for
this class -- acting as a sort of arbiter for multiple threads wanting
to run code while making sure it runs serially -- has this class sit in
an empty Run call most of the time). I'm not aware of us having a use
for such a thing right now, but one of my tests in another patch turned
into something similar by accident.

Another problem with the current approach is that, in a
distributed/dynamic setup (multiple things using the main loop without a
clear coordinator), one can never be sure whether unregistering a
specific event will terminate the loop (it depends on whether there are
other listeners). We had this problem in lldb-platform.cpp, where we had
to add an additional layer of synchronization to avoid premature
termination. We can remove this if we can rely on the loop terminating
only when we tell it to.
2024-10-17 17:29:38 +02:00
Jonas Devlieghere
4897fc44a9 [lldb] Narrow scope of -Wno-deprecated-declarations (NFC) (#112276)
Currently all of LLDB is being compiled with
-Wno-deprecated-declarations. That's not desirable, especially as part
of the LLVM monorepo, as we miss deprecation warnings from LLVM and
clang.

According to the git history, this was first introduced to suppress
warnings related to auto_ptr. Since then, other things have been
deprecated and gone unnoticed. This patch limits the flag to Host.mm
which uses a handful of LSApplication headers that have no replacement.

rdar://112040718
2024-10-17 08:22:56 -07:00
Jonas Devlieghere
8c7f80f775 [lldb] Disable warning about codecvt_utf8 deprecation (NFC) (#112446)
Disable -Wdeprecated-declarations for codecvt_utf8 in Editline. This is
in preparation for #112276 which narrows the scope of
-Wno-deprecated-declarations for building LLDB.
2024-10-17 08:19:58 -07:00
Jonas Devlieghere
cc13d4fb4a [lldb] Make the system log a NOOP on non-Darwin platforms
As discussed in #111911, we have consensus that as it stands, the system
log is only meaningful on Darwin and that by default it should be a NOOP
on other platforms.
2024-10-15 16:13:42 -07:00
Jonas Devlieghere
3dedcab6d9 [lldb] Make the system log a NOOP on Windows (#112052)
Windows doesn't have a built-in system log. Previously we got away with
writing to stdout and stderr because it was used only sporadically. As
we're trying to make the system log more useful on the other platforms,
the increased use become a concern. Make it a NOOP until someone figures
out a reasonable alternative.
2024-10-14 15:02:02 +01:00
Jonas Devlieghere
c77b107461 [lldb] Introduce an always-on system log category/channel (#108495)
Add an "always on" log category and channel. Unlike other, existing log
channels, it is not exposed to users. The channel is meant to be used
sparsely and deliberately for logging high-value information to the
system log.

We have a similar concept in the downstream Swift fork and this has
proven to be extremely valuable. This is especially true on macOS where
system log messages are automatically captured as part of a sysdiagnose.
2024-10-10 08:47:30 -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

( 65b13610a5 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
Pavel Labath
ebbc9ed2d6 [lldb] Add a MainLoop version of DomainSocket::Accept (#108188)
To go along with the existing TCPSocket implementation.
2024-09-13 12:56:52 +02: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
Adrian Prantl
1e98aa4730 [lldb] Convert ConnectionGenericFileWindows.cpp to new Status API (NFC) 2024-09-05 15:09:25 -07:00
Adrian Prantl
f00c946c2d [lldb] Convert MainLoopWindows.cpp to new Status API (NFC) 2024-09-05 14:28:28 -07:00
Adrian Prantl
3836d4accc [lldb] Convert ConnectionGenericFileWindows.cpp to new Status API (NFC) 2024-09-05 14:28:04 -07:00
Adrian Prantl
b798f4bd50 [lldb] Make deep copies of Status explicit (NFC) (#107170) 2024-09-05 12:44:13 -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
Dmitry Vasilyev
deeafeab81 [lldb][NFC] Move few static helpers to the class Socket (#106640)
Fixed a typo in Socket::SetOption().
2024-09-04 13:38:55 +04:00
Pavel Labath
3d5e1ec650 [lldb] Add a callback version of TCPSocket::Accept (#106955)
The existing function already used the MainLoop class, which allows one
to wait on multiple events at once. It needed to do this in order to
wait for v4 and v6 connections simultaneously. However, since it was
creating its own instance of MainLoop, this meant that it was impossible
to multiplex these sockets with anything else.

This patch simply adds a version of this function which uses an
externally provided main loop instance, which allows the caller to add
any events it deems necessary. The previous function becomes a very thin
wrapper over the new one.
2024-09-03 13:24:39 +02:00
Pavel Labath
4353530a6f [lldb/windows] Reset MainLoop events after handling them (#107061)
This prevents the callback function from being called in a busy loop.
Discovered by @slydiman on #106955.
2024-09-03 13:23:07 +02:00
Adrian Prantl
d22bee143f [lldb] Update PipeWindows.cpp to new Status API 2024-08-27 15:52:04 -07:00
Adrian Prantl
de687eac24 [lldb] Update Host/windows to new Status API 2024-08-27 15:43:12 -07:00
Adrian Prantl
d48b0f8db8 [lldb] Update File to new Status API 2024-08-27 15:25:32 -07:00
Adrian Prantl
b24ffa6002 [lldb] Update ProcessLauncherWinows to new Status API 2024-08-27 12:34:54 -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
Alex Langford
384d69fcbb [lldb] Cleanup dyld_process_t after constructing SharedCacheInfo (#106157)
Without calling `dyld_process_dispose`, LLDB will leak the memory
associated with the `dyld_process_t`.

rdar://134738265
2024-08-27 09:59:17 -07:00
Dmitry Vasilyev
06ccd32bb7 [lldb][NFC] Moved the SharedSocket class to Socket.* (#104787)
This is the prerequisite for #104238.
2024-08-26 18:26:46 +04:00
Dmitry Vasilyev
899a3df024 [lldb][NFC] Moved FindSchemeByProtocol() from Acceptor to Socket (#104439)
This is the prerequisite for #104238.
2024-08-16 13:46:45 +04:00
Daniel Wedzicha
b4dc986938 [LLDB][OSX] Removed semi colon generating a warning during build (#104398)
Singular warning I noticed when compiling lldb.

Co-authored-by: Daniel <d.wedzicha@efg.gg>
2024-08-15 00:23:40 -04:00
Walter Erquinigo
cce2271eab [LLDB][OSX] Add a fallback support exe directory (#103458)
LLDB on OSX is looking at a `bin` directory sibling to the `lib` one
that contains liblldb for its supporting executables. This works well
for CMake, however, for other build systems like bazel, it's not that
easy to have that build structure, for which it's much easier to also
use the `lib` directory as a fallback under the absence of `bin`.
This shouldn't break anything, but instead should make it a bit easier
for LLDB to work with different build systems and folder structures.
2024-08-14 12:41:19 -04:00
Dmitry Vasilyev
ddb9869dd2 [lldb] Fixed PipeWindows bugs; added Pipe::WriteWithTimeout() (#101383)
Added also the test for async read/write.
2024-08-05 22:11:24 +04:00
David Spickett
f48c16631d [lldb][Linux] Parse, but don't store "comm" from /proc/stat file (#100387)
As reported in https://github.com/llvm/llvm-project/issues/89710, the %s
code used for `comm` could and probably does, overflow the buffer.
Likely we haven't seen it cause problems because the following data is
overwritten right afterwards.

Also scanf isn't a great choice here as this `comm` can include many
characters that might trip up %s.

We don't actually use `comm`, so parse but don't store it so we're not
overflowing anything.
2024-07-25 09:15:03 +01:00
Jonas Devlieghere
34e67ffe19 [lldb] Drop LOG_CONS from openlog call.
Don't pass `LOG_CONS` to the openlog call.

> Write directly to the system console if there is an error while
> sending to the system logger.

This seemed like a good idea at the time, but it turns out to be
extremely annoying when this happens and LLDB is overwhelmed by log
messages in the console.

rdar://132243490
2024-07-23 11:29:12 -07:00
Dmitry Vasilyev
f6eb89cdd0 [lldb][Windows] Fixed Host::Kill() (#99721)
HostProcessWindows::Terminate() correctly uses m_process which type is
process_t (HANDLE) to call ::TerminateProcess(). But Host::Kill() uses a
cast from pid, which is wrong.

This patch fixes #51793
2024-07-21 15:59:41 +04:00
Fred Grim
8f74725731 [lldb] Adds additional fields to ProcessInfo (#91544)
To implement SaveCore for elf binaries we need to populate some
additional fields in the prpsinfo struct. Those fields are the nice
value of the process whose core is to be taken as well as a boolean flag
indicating whether or not that process is a zombie. This commit adds
those as well as tests to ensure that the values are consistent with
expectations
2024-06-14 18:30:23 -07:00
Jonas Devlieghere
c6c08eee37 [lldb] Remove setupterm workaround on macOS (#93714)
Remove setupterm workaround on macOS which caused an issues after the
removal of the terminfo dependency. There's a comment that explains why
the workaround is present, but neither Jim nor I were able to reproduce
the issue by setting TERM to vt100.
2024-05-29 14:21:06 -07:00
Adrian Prantl
af31883341 Add a createError variant without error code (NFC) (#93209)
For the significant amount of call sites that want to create an
incontrovertible error, such a wrapper function creates a significant
readability improvement and lowers the cost of entry to add error
handling in more places.
2024-05-23 14:22:07 -07:00
Keith Smiley
f918c056f0 [lldb] Allow env override for LLDB_ARGDUMPER_PATH (#91688)
This mirrors the LLDB_DEBUGSERVER_PATH environment variable and allows
you to have lldb-argdumper in a non-standard location and still use it
at runtime.
2024-05-14 13:43:04 -07:00
Jonas Devlieghere
528f5ba7af [lldb] Create a single Severity enum in lldb-enumerations (#90917)
We have 3 different enums all expressing severity (info, warning,
error). Remove all uses with a new Severity enum in lldb-enumerations.h.
2024-05-03 09:25:38 -07:00
Jonas Devlieghere
a7e9e3eb8b [lldb] Add a log level to Host::SystemLog (#90904)
Add the ability to specify a log level to Host::SystemLog.
2024-05-02 16:07:11 -07:00
Danial Klimkin
29dda26c65 Fix lock guads in PipePosix.cpp (#90572)
Guard object destroyed immediately after creation without naming.
2024-04-30 10:06:28 +02:00
Fred Grim
4681079040 adds additional information to the ProcessInfo object for elf processes (#88995)
This adds some additional bits into a ProcessInfo structure that will be
of use in filling structs in an elf core file. This is a demand for
implementing process save-core
2024-04-18 09:46:58 -07:00
Jonas Devlieghere
b76fd1eddb [lldb] Avoid deadlock by unlocking before invoking callbacks (#86888)
Avoid deadlocks in the Alarm class by releasing the lock before invoking
callbacks. This deadlock manifested itself in the ProgressManager:

1. On the main thread, the ProgressManager acquires its lock in
ProgressManager::Decrement and calls Alarm::Create.
  2. On the main thread, the Alarm acquires its lock in Alarm::Create.
3. On the alarm thread, the Alarm acquires its lock after waiting on the
condition variable and calls ProgressManager::Expire.
4. On the alarm thread, the ProgressManager acquires its lock in
ProgressManager::Expire.

Note how the two threads are acquiring the locks in different orders.
Deadlocks can be avoided by always acquiring locks in the same order,
but since the two mutexes here are private implementation details,
belong to different classes, that's not straightforward. Luckily, we
don't need to have the Alarm mutex locked when invoking the callbacks.
That exactly how this patch solves the issue.
2024-03-27 16:37:00 -07:00
Jie Fu
ba97dc8c7a [lldb] Fix -Wctad-maybe-unsupported in Alarm.cpp (NFC)
llvm-project/lldb/source/Host/common/Alarm.cpp:37:5:
error: 'lock_guard' may not intend to support class template argument deduction [-Werror,-Wctad-maybe-unsupported]
    std::lock_guard alarm_guard(m_alarm_mutex);
    ^
2024-03-16 06:52:12 +08:00
Jonas Devlieghere
f01a32f5c5 [lldb] Add an Alarm class for coalescing progress reports (#85329)
The commit introduces a new, generic, Alarm class. The class lets you to
schedule functions (callbacks) that will execute after a predefined
timeout. Once scheduled, you can cancel and reset a callback, given the
timeout hasn't expired yet.

The alarm class worker thread that sleeps until the next timeout
expires. When the thread wakes up, it checks for all the callbacks that
have expired and calls them in order. Because the callback is called
from the worker thread, the only guarantee is that a callback is called
no sooner than the timeout. A long running callback could potentially
block the worker threads and delay other callbacks from getting called.

I intentionally kept the implementation as simple as possible while
addressing the needs for the use case of coalescing progress events as
discussed in [1]. If we want to rely on this somewhere else, we can
reassess whether we need to address this class' limitations.

[1] https://discourse.llvm.org/t/rfc-improve-lldb-progress-reporting/75717/
2024-03-15 09:35:38 -07:00
karzan
501bc101c0 [lldb] Save the edited line before clearing it in Editline::PrintAsync (#84154)
If the `m_editor_status` is `EditorStatus::Editing`, PrintAsync clears
the currently edited line. In some situations, the edited line is not
saved. After the stream flushes, PrintAsync tries to display the unsaved
line, causing the loss of the edited line.

The issue arose while I was debugging REPRLRun in
[Fuzzilli](https://github.com/googleprojectzero/fuzzilli). I started
LLDB and attempted to set a breakpoint in libreprl-posix.c. I entered
`breakpoint set -f lib` and used the "tab" key for command completion.
After completion, the edited line was flushed, leaving a blank line.
2024-03-11 10:07:12 -07:00
Jonas Devlieghere
1b812f9cd6 [lldb] Log to system log instead of stderr from Host::SystemLog (#83366)
Currently, calls to Host::SystemLog print to stderr on all host
platforms except Darwin. This severely limits its value on the command
line, where we don't want to overload the user with log messages. Switch
to using the syslog function on POSIX systems to send messages to the
system logger instead of stdout.

On Darwin systems this sends the log message to os_log, which matches
what we do today. Nevertheless I kept the current implementation that
uses os_log directly as it gives us more freedom.

I'm not sure if there's an equivalent on Windows, so I kept the existing
behavior of logging to stderr.
2024-03-05 10:56:01 -08:00
Walter Erquinigo
cd344a4c20 [LLDB] Fix completion of space-only lines in the REPL on Linux (#83203)
https://github.com/modularml/mojo/issues/1796 discovered that if you try
to complete a space-only line in the REPL on Linux, LLDB crashes. I
suspect that editline doesn't behave the same way on linux and on
darwin, because I can't replicate this on darwin.

Adding a boundary check in the completion code prevents the crash from
happening.
2024-02-28 11:43:36 -05:00
Jonas Devlieghere
705fcd4e0a Revert "[lldb] Expand background symbol lookup" (#81182)
Reverts llvm/llvm-project#80890
2024-02-08 11:50:53 -08:00
Jonas Devlieghere
74fc16aaaa [lldb] Expand background symbol download (#80890)
LLDB has a setting (symbols.enable-background-lookup) that calls
dsymForUUID on a background thread for images as they appear in the
current backtrace. Originally, the laziness of only looking up symbols
for images in the backtrace only existed to bring the number of
dsymForUUID calls down to a manageable number.

Users have requesting the same functionality but blocking. This gives
them the same user experience as enabling dsymForUUID globally, but
without the massive upfront cost of having to download all the images,
the majority of which they'll likely not need.

This patch renames the setting to have a more generic name
(symbols.auto-download) and changes its values from a boolean to an
enum. Users can now specify "off", "background" and "foreground". The
default remains "off" although I'll probably change that in the near
future.
2024-02-08 11:24:07 -08:00
Jonas Devlieghere
3b6a8f823b [lldb] Upstream xros support in lldb (#78389)
Upstream support for debugging xros applications through LLDB.
2024-01-17 09:47:08 -08:00