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 84d9887
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 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
31 changes: 31 additions & 0 deletions ETTrace/Symbolicator/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,37 @@ 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

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
}

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

0 comments on commit 84d9887

Please sign in to comment.