Skip to content

Commit

Permalink
Stop using shell
Browse files Browse the repository at this point in the history
  • Loading branch information
noahsmartin committed Mar 1, 2024
1 parent 87d669a commit 22283fb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 25 deletions.
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"

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

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()
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
}

0 comments on commit 22283fb

Please sign in to comment.