Commit Graph

62 Commits

Author SHA1 Message Date
Cassie Jones
f478b79af9 swift-inspect: Explicitly reference String.init(cString:)
This was briefly failing to build because of an ambiguous initializer
while making other changes, it's better to be explicit with method names
anyway.

rdar://127548384
2024-05-07 14:06:36 -07:00
Saleem Abdulrasool
18fa46021c Merge pull request #70103 from compnerd/remote
swift-inspect: clean up some of the code in `WindowsRemoteProcess`
2023-12-03 12:23:54 -08:00
Saleem Abdulrasool
7a6c6c7eee swift-inspect: clean up some of the code in WindowsRemoteProcess
Add some vertical whitespace to the code ejection process.  Alter the
logic to clean up the memory allocation first, ignoring the error as the
subsequent run will perform a new allocation and this will leak a fixed
amount of memory without interrupting the process or use of the tool.
No longer check the exit code of the thread as that is always guaranteed
to be 0 as the module unloading path does not report any error code in
the injected code (DLL).

Use the opportunity to do some simple renaming to improve the
readability and create an overload for avoiding unnecessary ceremony
around use of a shared constant.
2023-12-02 08:56:52 -08:00
Emre Celebi
6a2650371d [swift-inspect] Again: Fix integer type mismatch on watchOS build.
Redo Mike's changes which were reverted: Convert integers to to `swift_reflection_ptr_t` instead of `UInt64` since watchOS uses `UInt` as its pointer type.

rdar://118458631
2023-11-30 10:09:21 -08:00
Emre Celebi
0953b1d514 [swift-inspect] Deallocate symbolicator after inspecting a process
This change deallocates the symbolicator. Added CSRelease to symbolication
extensions.
2023-11-29 17:00:30 -08:00
Emre Celebi
2a3d3cb894 Merge pull request #69916 from w6sec/swift-inspect-json
[swift-inspect] Add JSON output option for dump-generic-metadata
2023-11-29 12:00:35 -08:00
Emre Celebi
4af03a89a2 [swift-inspect] Inspect all processes and add summary option
With this change swift-inspect can inspect all processes to see if metadata allocation iteration is enabled. We also added summary option that sorts metadata to popularity.
2023-11-29 07:19:04 -08:00
Emre Celebi
f0a6b02bb6 [swift-inspect] Change argument options to select between file or standard output
With this change we make --json flag to toggle between JSON and text output. If --output-file option is given, then the output stream is forwarded to the file specified.
2023-11-16 18:25:38 -08:00
Saleem Abdulrasool
d1459247d5 swift-inspect: clean up event creation on Windows
Create a Swiftier interface for `CreateEvent` from the Windows SDK and
use that to simplify the implementation of the remote process interface.
2023-11-16 12:27:01 -08:00
Emre Celebi
d9989d3e9f [swift-inspect] Add json output option for dump-generic-metadata
This change adds json output option to dump-generic-metadata for
swift-inspect.
2023-11-16 11:26:45 -08:00
Mike Ash
9477ed2626 [swift-inspect] Fix integer type mismatch on watchOS build.
Convert integers to to `swift_reflection_ptr_t` instead of `UInt64` since watchOS uses `UInt` as its pointer type.

rdar://118458631
2023-11-15 15:23:08 -05:00
YR Chen
be5a06555f Add support for SWIFTCI_USE_LOCAL_DEPS in swift-inspect
If the `SWIFTCI_USE_LOCAL_DEPS` environment variable is set, we're building in the Swift.org CI system alongside other projects in the Swift toolchain and we can depend on local versions of our dependencies instead of fetching them remotely.
2023-11-09 20:02:14 +08:00
Emre Celebi
b6f77a46be [swift-inspect] Add an option to print mangled names while dumping generic metadata. 2023-10-30 23:39:42 -07:00
Mike Ash
8689cf8a27 [swift-inspect] Compute ptrauth mask when building for plain ARM64.
This allows an ARM64 swift-inspect to correctly read from an ARM64e target process. We compute a ptrauth mask by stripping all bits above MACH_VM_MAX_ADDRESS. This strips more than we strictly need, but it works.
2023-10-23 15:53:49 -04:00
Mike Ash
7b32ffe601 [swift-inspect] Limit number of jobs printed on an actor.
If we encounter bad data in the target, we could end up trying to print an infinite list of jobs. Clamp it to 1,000 so we fail more gracefully.

rdar://113417637
2023-08-07 16:43:15 -04:00
Saleem Abdulrasool
72337efc3b build: adjust application of flags for LLVM issue (#67455)
The flags were meant to be applied to the C++ code not the Swift code.
Adjust the configuration to apply the flags properly.
2023-07-21 13:23:27 -07:00
Hiroshi Yamauchi
627175fe4c Fix a build issue with swift-inspect
Update the version of swift-argument-parser used by swift-inspect to
be compatible with the package description version.

Add compiler flags for a LLVM bug workaround.
2023-07-21 04:36:15 -07:00
Saleem Abdulrasool
bc9e90d318 swift-inspect: ensure that we eject any injected code
We would previously fail to eject the injected code on a failure.  This
would prevent a future introspection into the process as well as leave
the file open with an incremented retain count in the kernel space which
would prevent the file from being deleted.

In the future, when the application is able to treat the injected code
as a resource, this resource would be temporarily extracted, but would
no longer be possible to delete until a reboot (with a registration of
the deletion) due to the retained code.

Take the opportunity to rename some functions to take advantage of
labelled parameters and trailing function syntax.  This makes the code a
small amount easier to read.
2023-07-19 07:51:12 -07:00
Saleem Abdulrasool
255beb3d0e Merge pull request #67334 from compnerd/66973
swift-inspect: clean up incorrect casts introduced in #66973
2023-07-18 07:15:58 -07:00
Saleem Abdulrasool
c6fbff4c94 Merge pull request #67336 from compnerd/lambda-calculus
swift-inspect: correct some escaping binding for RemoteMirror
2023-07-18 07:15:35 -07:00
Saleem Abdulrasool
1b538f55ef swift-inspect: correct some escaping binding for RemoteMirror
Adjust the pointer handling for the callback and adjust the code to
ensure that we properly form memory references to the function for the
callback.
2023-07-17 10:58:18 -07:00
Saleem Abdulrasool
8794df84e9 swift-inspect: correct invalid memory usage introduced in #66973
Clean up the incorrect memory binding in swift-inspect introduced in
PR#66973.  This fixes the incorrect memory usage to mutate the `context`
parameter.
2023-07-17 10:16:26 -07:00
Saleem Abdulrasool
9fb9561f67 swift-inspect: clean up incorrect casts introduced in #66973
Clean up some casts that would cause warnings on Windows introduced in
PR #66973.  Rewrite some of the path computation for the DLL path to
avoid some unnecessary conversions.  Split out some of the merged guards
to allow for split diagnostic emissions.
2023-07-17 09:51:38 -07:00
Hiroshi Yamauchi
af890f3cbf Improve the dump-arrays performance on Windows
Use the HeapWalk API for heap iteration instead of the
Heap32First/Next API, which was known to be slow. Since HeapWalk only
works locally, it requires using a remote thread and a DLL.
2023-07-14 16:13:10 -07:00
Josh Soref
9e54b97e4f Spelling tools swift inspect (#58559) 2022-05-27 22:07:24 -07:00
Saleem Abdulrasool
ebf1cec6a6 swift-inspect: update instructions for Windows
Remove the reference to `DEVELOPER_DIR` and update the instructions to
actually be usable with a released snapshot (and official releases).
The development setup should mimic the actual layout further and is a
separate issue.  This requires a new snapshot with
apple/swift-installer-scripts#128 included.
2022-05-02 08:22:18 -07:00
Daniel Duan
3dfc40898c [NFC] Remove Python 2 imports from __future__ (#42086)
The `__future__` we relied on is now,  where the 3 specific things are
all included [since Python 3.0](https://docs.python.org/3/library/__future__.html):

* absolute_import
* print_function
* unicode_literals
* division

These import statements are no-ops and are no longer necessary.
2022-04-13 14:01:30 -07:00
Mike Ash
43df54caee [swift-inspect] Support forking corpses and self-inspection of the swift-inspect process on Darwin.
Support inspecting the swift-inspect process itself. `pidFromHint` skips the process it's in, so we manually check the requested name against `argv[0]` as a special case.

Add a `--fork-corpse` flag which uses `task_generate_corpse` on the target task before inspecting it. This allows the target to keep running while we inspect it, and also works around a bug when self-inspecting.
2022-03-22 16:30:16 -04:00
Mike Ash
ae2b5140ed [RemoteMirror][swift-inspect] Decode locks in priority-escalation concurrency runtime.
When possible, decode the DrainLock/ExecutionLock fields of tasks and actors in concurrency runtimes built with priority escalation, and show the corresponding thread info in swift-inspect output.

We weren't properly decoding actor flags previously, so fix that up as well and have Remote Mirror split them out into separate fields so clients don't have to. We were missing the Job Storage field from the definition of DefaultActorImpl in RuntimeInternals.h, fix that so we actually read the right data.

rdar://88598003
2022-03-18 15:41:16 -04:00
Mike Ash
9fd97d5b52 Merge pull request #41676 from mikeash/swift-inspect-concurrency-flags
[swift-inspect][RemoteMirror] Decode job/task/actor flags.
2022-03-16 10:21:40 -04:00
Mike Ash
5fd87a8ee3 [swift-inspect][RemoteMirror] Decode job/task/actor flags.
Have RemoteMirror internally decode these flags fields and return them as separate fields in the task/actor info. Handle the structures both with and without task escalation support.

Also show when a task is the current task on a thread in swift-inspect's task listing.

rdar://88598003
2022-03-14 16:20:05 -04:00
Mike Ash
4b85670ffc [swift-inspect] Fix some missing type conversions when targeting watchOS. 2022-03-04 13:56:12 -05:00
Mike Ash
bf4fbdc43a [Runtime] Tiny fix for swift-inspect, let -> var. 2022-03-02 17:00:00 -05:00
Mike Ash
282b8125c9 Merge pull request #41620 from mikeash/fix-swift-inspect-darwin
[swift-inspect] Fix the swift-inspect build on Darwin.
2022-03-02 16:34:04 -05:00
Mike Ash
e29434d4de [swift-inspect] Fix the swift-inspect build on Darwin.
We had a few typos and such from the recent reorganization.
2022-03-02 14:29:35 -05:00
Saleem Abdulrasool
18ec9bffc1 swift-inspect: enable dump-arrays on Windows
Hoist `iterateHeaps` into the `RemoteProcess` protocol, requiring an
implementation on all platforms.  If the platform is unable to implement
heap traversal, it would be possible to simply leave the callback
uncalled.

Be more careful about memory queries, we may receive invalid memory
addresses.
2022-02-25 14:55:18 -08:00
Saleem Abdulrasool
21edf9c322 swift-inspect: more typo/correctness fixes
This cleans up the DumpArray operation and fixes an unintentional,
undesirable change to the heap iteration on Darwin.
2022-02-24 11:04:20 -08:00
Saleem Abdulrasool
4a64b9f8f8 swift-inspect: add some documentation
This adds some documentation about swift-inspect and how to build it on
Windows, which requires additional flags to locate the SwiftRemoteMirror
library from the toolchain.
2022-02-24 08:34:50 -08:00
Saleem Abdulrasool
7f789ba319 swift-inspect: port to windows
This adds an initial port to Windows which allows inspection of the
processes.  It is not possible to port `dump-arrays` or
`dump-concurrency` due to the need to iterate the heap.  This still
allows for gaining some insight into the metadata and protocol caches.
2022-02-21 08:59:27 -08:00
Saleem Abdulrasool
f085f2b42f swift-inspect: fix all the typos/correctness issues
Fix the many typos and missing `)` instances.  Replace the inline array
removal with explicit duplication due to the behaviour of `#if`.  This
allows the tool to build after the changes for the refactoring.
2022-02-20 15:58:37 -08:00
Saleem Abdulrasool
eef3a8f60a swift-inspect: restructure the project for porting
This restructures and refactors the project to split up the
implementation into smaller fragments.  The majority of the operations
are portable: the ones which are not require iterating the heap
allocations which is not guaranteed to be a portable operation.
Additionally, the `Inspector` class is renamed into `RemoteProcess`
which is also converted to a protocol to allow adding in an
implementation for other targets.  The inspection operations are split
off into individual files to make it easier to follow.  Take the
opportunity to use `@main` for the entry point.
2022-02-18 12:58:11 -08:00
Mike Ash
f829167882 [swift-inspect] Fix errors when targeting 32-bit.
The C APIs are somewhat inconsistent about what kind of integers they use, so we need a bunch of conversions when targeting 32-bit.
2022-02-08 11:34:37 -05:00
Mike Ash
a82ea120a4 [RemoteMirror][swift-inspect] Add a command to inspect the state of the concurrency runtime.
Most of the new inspection logic is in Remote Mirror. New code in swift-inspect calls the new Remote Mirror functions and formats the resulting information for display.

Specific Remote Mirror changes:

* Add a call to check if a given metadata is an actor.
* Add calls to get information about actors and tasks.
* Add a `readObj` call to MemoryReader that combines the read and the cast, greatly simplifying code chasing pointers in the remote process.
* Add a generalized facility to the C shims that can allocate a temporary object that remains valid until at least the next call, which is used to return various temporary arrays from the new calls. Remove the existing `lastString` and `lastChunks` member variables in favor of this new facility.

Swift-inspect changes:

* Add a new dump-concurrency command.
* Add a new `ConcurrencyDumper.swift` file with the implementation. The dumper needs to do some additional work with the results from Remote Mirror to build up the task tree and this keeps it all organized.
* Extend `Inspector` to query the target's threads and fetch each thread's current task.

Concurrency runtime changes:

* Add `_swift_concurrency_debug` variables pointing to the various future adapters. Remote Mirror uses these to provide a better view of a tasks's resume pointer.

rdar://85231338
2022-02-04 09:28:32 -05:00
Mike Ash
9fa76e2f4f [swift-inspect] Add a command to dump information about allocated arrays in the target process. 2021-11-04 16:15:15 -04:00
Arnold Schwaighofer
3dfede7482 Remove fatalError calls 2021-10-28 09:18:00 -07:00
Arnold Schwaighofer
f0c864063e Print whether the metadata is a _ContiguousArrayStorage with a class element type 2021-10-28 07:59:44 -07:00
Arnold Schwaighofer
28b53dec31 Use the utility function to add the reflection info from loaded images 2021-10-28 07:58:53 -07:00
Arnold Schwaighofer
86c9407cba Add a utility function to Inspector to load the reflection metadata from
loaded images
2021-10-28 07:58:53 -07:00
Doug Gregor
9a8847dd4f [swift-inspect] Make sure to initialize backtrace flags.
This eliminates a compilation error with Swift 5.5 and the latest
argument parser library.
2021-08-31 09:56:50 -07:00
Mike Ash
ecd6d4ddec Add a new ConcurrentReadableHashMap type. Switch the protocol conformance cache
to use it.

ConcurrentReadableHashMap is lock-free for readers, with writers using a lock to
ensure mutual exclusion amongst each other. The intent is to eventually replace
all uses ConcurrentMap with ConcurrentReadableHashMap.

ConcurrentReadableHashMap provides for relatively quick lookups by using a hash
table. Rearders perform an atomic increment/decrement in order to inform writers
that there are active readers. The design attempts to minimize wasted memory by
storing the actual elements out-of-line, and having the table store indices into
a separate array of elements.

The protocol conformance cache now uses ConcurrentReadableHashMap, which
provides faster lookups and less memory use than the previous ConcurrentMap
implementation. The previous implementation caches
ProtocolConformanceDescriptors and extracts the WitnessTable after the cache
lookup. The new implementation directly caches the WitnessTable, removing an
extra step (potentially a quite slow one) from the fast path.

The previous implementation used a generational scheme to detect when negative
cache entries became obsolete due to new dynamic libraries being loaded, and
update them in place. The new implementation just clears the entire cache when
libraries are loaded, greatly simplifying the code and saving the memory needed
to track the current generation in each negative cache entry. This means we need
to re-cache all requested conformances after loading a dynamic library, but
loading libraries at runtime is rare and slow anyway.

rdar://problem/67268325
2020-08-20 13:05:30 -04:00