This addition will allow us to cross-compile swift-inspect to Windows
ARM64. Enabling the Windows ARM64 build permits the toolchain to become
more similar across the architectures.
Various processes, such as launchd, are unsafe to inspect directly. Inspecting them pauses them and the system does not like it when they're paused. Forking a corpse avoids this, as the original process continues running while we inspect the forked corpse.
VMUProcInfo provides a shouldAnalyzeWithCorpse call which tells us whether we're inspecting one of those processes. When we are, then we force the forkCorpse option even when it's not specified.
Add shims to Symbolication+Extensions.swift to allow calls to VMUProcInfo and use it to force forkCorpse when appropriate.
rdar://124720793
After the C standard library headers are split out of Darwin, DumpConcurrency.swift no longer sees string.h. Explicitly import <string.h>'s new module when it's available.
rdar://127076885
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
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.
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
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.
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.
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.
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
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.
Clean up the incorrect memory binding in swift-inspect introduced in
PR#66973. This fixes the incorrect memory usage to mutate the `context`
parameter.
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.
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.
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.
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
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
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.
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.
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.
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.
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