Commit Graph

111 Commits

Author SHA1 Message Date
Andrew Rogers
9e5f0ca9cd PR feedback: update copyright to 2024 2024-12-04 11:00:37 -08:00
Andrew Rogers
6dc7c3265d address some code review comments 2024-12-03 17:15:02 -08:00
Andrew Rogers
4906ed9c36 [linux] define _GNU_SOURCE for process_vm_readv 2024-12-03 15:53:05 -08:00
Andrew Rogers
59d39a1c7d [linux] exclude dump-array support 2024-12-03 14:47:16 -08:00
Andrew Rogers
2ee5d15886 [linux] swift-inspect support for Linux 2024-12-03 14:47:16 -08:00
Saleem Abdulrasool
22a014b484 swift-inspect: add initial CMake based build system
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.
2024-11-19 15:41:52 -08:00
Mike Ash
37dc7d5c66 [swift-inspect] Force forkCorpse if VMUProcInfo advises it.
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
2024-10-22 12:27:11 -04:00
Ian Anderson
578336c859 Build fix for when the Darwin module is split up
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
2024-08-07 15:46:02 -07:00
Alexander Cyon
4a2942bb4e Fix typos in: cmake, tools, utils, unittests, validation-test
Co-authored-by: Saleem Abdulrasool <compnerd@compnerd.org>
2024-07-12 02:34:00 +03:00
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
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
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
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
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