Skip to content

Commit

Permalink
Send VPN system extension crashes to Sentry (#2002)
Browse files Browse the repository at this point in the history
Task/Issue URL: https://app.asana.com/0/0/1206227536353331/f
Tech Design URL:
CC:

Description:

This PR updates the crash report parser to read and send system extension crashes.
  • Loading branch information
samsymons authored Jan 8, 2024
1 parent e777097 commit 517bb99
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions DuckDuckGo/CrashReports/Model/CrashReportReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,32 @@ import Foundation
final class CrashReportReader {

static let displayName = Bundle.main.displayName
static let vpnExtensionDisplayName = "com.duckduckgo.macos.vpn.network-extension"

func getCrashReports(since lastCheckDate: Date) -> [CrashReport] {
let allPaths: [URL]
var allPaths: [URL]

do {
allPaths = try FileManager.default.contentsOfDirectory(at: FileManager.diagnosticReports,
includingPropertiesForKeys: nil,
options: [])
allPaths = try FileManager.default.contentsOfDirectory(at: FileManager.userDiagnosticReports, includingPropertiesForKeys: nil)
} catch {
assertionFailure("CrashReportReader: Can't read content of diagnostic reports \(error.localizedDescription)")
return []
}

return allPaths
.filter({ isCrashReportPath($0) &&
belongsToThisApp($0) &&
isFile(at: $0, newerThan: lastCheckDate) })
.compactMap(crashReport(from:))
#if NETP_SYSTEM_EXTENSION
do {
let systemPaths = try FileManager.default.contentsOfDirectory(at: FileManager.systemDiagnosticReports, includingPropertiesForKeys: nil)
allPaths.append(contentsOf: systemPaths)
} catch {
assertionFailure("Failed to read system crash reports: \(error)")
}
#endif

let filteredPaths = allPaths.filter({
isCrashReportPath($0) && belongsToThisApp($0) && isFile(at: $0, newerThan: lastCheckDate)
})

return filteredPaths.compactMap(crashReport(from:))
}

private func isCrashReportPath(_ path: URL) -> Bool {
Expand All @@ -46,7 +55,10 @@ final class CrashReportReader {
}

private func belongsToThisApp(_ path: URL) -> Bool {
return path.lastPathComponent.hasPrefix(Self.displayName ?? "DuckDuckGo")
let hasAppPrefix = path.lastPathComponent.hasPrefix(Self.displayName ?? "DuckDuckGo")
let hasVPNPrefix = path.lastPathComponent.hasPrefix(Self.vpnExtensionDisplayName)

return hasAppPrefix || hasVPNPrefix
}

private func isFile(at path: URL, newerThan lastCheckDate: Date) -> Bool {
Expand All @@ -70,12 +82,16 @@ final class CrashReportReader {

fileprivate extension FileManager {

static let diagnosticReports: URL = {
static let userDiagnosticReports: URL = {
let homeDirectoryURL = FileManager.default.homeDirectoryForCurrentUser
return homeDirectoryURL
.appendingPathComponent("Library/Logs/DiagnosticReports")
}()

static let systemDiagnosticReports: URL = {
return URL(fileURLWithPath: "/Library/Logs/DiagnosticReports")
}()

func fileCreationDate(url: URL) -> Date? {
let fileAttributes: [FileAttributeKey: Any] = (try? self.attributesOfItem(atPath: url.path)) ?? [:]
return fileAttributes[.creationDate] as? Date
Expand Down

0 comments on commit 517bb99

Please sign in to comment.