Skip to content
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

Stop using shell #74

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions ETTrace/Symbolicator/Symbolicator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,7 @@ public class StackSymbolicator {
let strs = addrsArray.map { String($0 + addition, radix: 16) }
try! strs.joined(separator: "\n").write(toFile: addrsFile, atomically: true, encoding: .utf8)

let arch = try? safeShellWithOutput("/usr/bin/file \"\(binary)\"").contains("arm64e") ? "arm64e" : "arm64"
Copy link
Member Author

Choose a reason for hiding this comment

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

The arch flag doesn't seem to be needed as long as it isn't a universal binary, and this wasn't making an effort to pick the right arch from a universal binary anyways


try! strs.joined(separator: "\n").write(toFile: addrsFile, atomically: true, encoding: .utf8)
Copy link
Member Author

Choose a reason for hiding this comment

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

This file was already written on line 154


let symsStr = try? safeShellWithOutput("/usr/bin/atos -l \(String(addition, radix: 16)) -arch \(arch!) -o \"\(binary)\" -f \(addrsFile)")
let symsStr = try? processWithOutput("/usr/bin/atos", args: ["-l", String(addition, radix: 16), "-o", binary, "-f", addrsFile])

let syms = symsStr!.split(separator: "\n").enumerated().map { (idx, sym) -> (UInt64, String?) in
let trimmed = sym.trimmingCharacters(in: .whitespacesAndNewlines)
Expand Down
56 changes: 36 additions & 20 deletions ETTrace/Symbolicator/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,49 @@ func safeShell(_ command: String) throws {
task.waitUntilExit()
}

func processWithOutput(_ executable: String, args: [String]) throws -> String {
let task = Process()
let pipe = Pipe()

task.standardOutput = pipe
task.arguments = args
task.executableURL = URL(fileURLWithPath: executable)
task.standardInput = nil

return try runTask(task)
}

func safeShellWithOutput(_ command: String) throws -> String {
let task = Process()
let pipe = Pipe()


task.standardOutput = pipe
task.arguments = ["--login", "-c", command]
task.executableURL = URL(fileURLWithPath: "/bin/zsh")
task.standardInput = nil

let group = DispatchGroup()
group.enter()
var result = String()
pipe.fileHandleForReading.readabilityHandler = { fh in
let data = fh.availableData
if data.isEmpty { // EOF on the pipe
pipe.fileHandleForReading.readabilityHandler = nil
group.leave()
} else {
if let newString = String(data: data, encoding: .utf8) {
result.append(newString)
}
return try runTask(task)
}

private func runTask(_ task: Process) throws -> String {
let pipe = Pipe()
let group = DispatchGroup()
Copy link

Choose a reason for hiding this comment

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

nit: can you share the pipe processing logic with safeShellWithOutput()?

group.enter()
var result = String()
pipe.fileHandleForReading.readabilityHandler = { fh in
let data = fh.availableData
if data.isEmpty { // EOF on the pipe
pipe.fileHandleForReading.readabilityHandler = nil
group.leave()
} else {
if let newString = String(data: data, encoding: .utf8) {
result.append(newString)
}
}
}
}

try task.run()
task.waitUntilExit()
group.wait()
return result
try task.run()
task.waitUntilExit()
group.wait()

return result
}
Loading