Skip to content

[Tools] Create swift-inspect, a debugging tool for dumping Swift runtime data from another process #31468

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 36 commits into from
May 31, 2020

Conversation

mikeash
Copy link
Contributor

@mikeash mikeash commented May 1, 2020

Create swift-inspect. This is a tool which can inspect a Swift process and dump information about the Swift runtime in that process.

It currently supports inspecting two kinds of information:

  1. It can dump the protocol conformance cache.
  2. It can dump all metadata allocations, and print the name and size of the metadata allocated by the generic metadata cache.

It's meant to grow more functionality over time.

The design of the tool places all of the runtime-related smarts in Remote Mirror. swiftdt is then a small that connects the Remote Mirror functionality with remote process inspection. The remote process inspection parts are necessarily OS-specific. Currently they're only implemented for Apple OSes. Everything is cleanly separated out so that someone who knows the appropriate APIs for another OS should be able to add support for that OS without much difficulty.

Update: after much soul searching, we have renamed this from swiftdt to swift-inspect, which is more descriptive and better fits the naming conventions of other tools.

@gottesmm
Copy link
Contributor

gottesmm commented May 8, 2020

@mikeash what does dt stand for? Maybe it would make sense to expand out the name? My thought: swift_memory_dumping_tool?

@@ -191,8 +191,8 @@ void mangleIdentifier(Mangler &M, StringRef ident) {
// of the identifier - that's why we added the dummy-word).
// The first thing: we add the encoded sub-string length.
M.Buffer << (Repl.StringPos - Pos);
assert(!isDigit(ident[Pos]) &&
"first char of sub-string may not be a digit");
// assert(!isDigit(ident[Pos]) &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I'm fixing this up along with some other stuff. Not quite done with it yet!

@mikeash
Copy link
Contributor Author

mikeash commented May 8, 2020

@gottesmm It stands for Swift Debug Tool. There's some internal precedent with other blahdt tools that it's named after, but I'm not completely opposed to changing it.

@gottesmm
Copy link
Contributor

gottesmm commented May 8, 2020

@mikeash does it debug everything about a swift program? To me debug tool is very generic to the point of being confusing = /.

@MadCoder
Copy link
Contributor

MadCoder commented May 8, 2020

it stands for debug/dump tool

@gparker42
Copy link
Contributor

"swiftrtdt" for Swift runtime debug tool?

}

if let commandName = commandName {
print("error: \(executableName): unknown command \(commandName)", to: &Std.err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can these both be argFail() calls?

}

let commandName = argv.popFirst()
for command in commands {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check that argv is now empty here, or else allow multiple commands to be run in one execution.

@gottesmm
Copy link
Contributor

gottesmm commented May 8, 2020

@MadCoder I figured as much. I was just pointing out the ambiguity of the name.

@gottesmm
Copy link
Contributor

gottesmm commented May 8, 2020

@gparker42 I think swift runtime debug tool would be a better name. At least it is clear what you are debugging.

@@ -0,0 +1,14 @@
add_swift_target_executable(swiftdt
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the wrong way to do this. We do not want add_swift_target_executable to be used outside of the stdlib directory. This is in preparation for splitting the stdlib from the main host build.

If you need to be in swift's cmake build, I would suggest putting this tool into ./stdlib/tools.

Otherwise, if you can compile against a just built toolchain, I would create a build-script tool that builds against the just built toolchain.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I imagine that you can do the latter since you are just using remote mirrors.

}

func printUsage(args: inout ArraySlice<String>) {
print("Usage: \(executableName) <command>", to: &Std.err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

print("Usage: \(executableName) <pid | process name> <command>", to: &Std.err)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gottesmm helped me transform this into a proper package, so I went ahead and redid it to use ArgumentParser instead of rolling my own. Now we get all the right behavior for free!

@gottesmm
Copy link
Contributor

gottesmm commented May 8, 2020

@mikeash one cool thing is that if you make this a build-script tool, you can use the swift package manager and get access to the argument parser/other things.

@gottesmm
Copy link
Contributor

gottesmm commented May 8, 2020

*use the just built swiftpm with your project.

@gottesmm
Copy link
Contributor

gottesmm commented May 9, 2020

@mikeash #31667. I tested that locally. It works. If one wants to invoke the build_script_helper.py directly, one does it like this:

+./build_script_helper.py --toolchain $BUILD_DIR/toolchain-macosx-x86_64/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain \
                                          --build-path $PWD/build \
                                          --package-path $PWD

Or to do it using build-script normally:

./swift/utils/build-script --release-debuginfo --swiftdt --skip-build-benchmarks \
     --force-optimized-typechecker --swiftpm --llbuild   --install-swift \
     --install-swiftpm --install-llbuild

@mikeash mikeash force-pushed the swiftdt branch 2 times, most recently from d516251 to ca1add9 Compare May 15, 2020 20:24
@mikeash mikeash marked this pull request as ready for review May 15, 2020 20:25
@mikeash mikeash requested review from tbkka and airspeedswift May 15, 2020 20:25
@benrimmington

This comment has been minimized.

nameOrPid: String
) -> (Inspector, SwiftReflectionContextRef) {
guard let pid = pidFromHint(nameOrPid) else {
argFail("Cannot find pid/process \(nameOrPid)")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don’t mind making this a throwing method, you can use ValidationError here.

Suggested change
argFail("Cannot find pid/process \(nameOrPid)")
throw ValidationError("Cannot find pid/process \(nameOrPid)")

@mikeash mikeash changed the title [Tools] Create swiftdt, a debugging tool for dumping Swift runtime data from another process [Tools] Create swift-inspect, a debugging tool for dumping Swift runtime data from another process May 29, 2020
@mikeash
Copy link
Contributor Author

mikeash commented May 29, 2020

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 27df9e3

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 27df9e3

@mikeash
Copy link
Contributor Author

mikeash commented May 29, 2020

@swift-ci please test os x platform

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 23ebf78

@mikeash
Copy link
Contributor Author

mikeash commented May 29, 2020

@swift-ci please test os x platform

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 23ebf78

@airspeedswift
Copy link
Member

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 23ebf78

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 23ebf78

@mikeash
Copy link
Contributor Author

mikeash commented May 29, 2020

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - b3a5cb7

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - b3a5cb7

@mikeash
Copy link
Contributor Author

mikeash commented May 29, 2020

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - b3a5cb7

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - b3a5cb7

@mikeash
Copy link
Contributor Author

mikeash commented May 30, 2020

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 6c8fa93

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 6c8fa93

@mikeash
Copy link
Contributor Author

mikeash commented May 31, 2020

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 2a55b9d

@mikeash
Copy link
Contributor Author

mikeash commented May 31, 2020

@swift-ci please test Linux platform

Copy link
Member

@airspeedswift airspeedswift left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@mikeash mikeash merged commit ad0a8d3 into swiftlang:master May 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants