Skip to content
This repository was archived by the owner on Nov 17, 2025. It is now read-only.

Commit 0beb202

Browse files
committed
Add detailed logging to debug Claude process argument parsing
- Log process arguments length and content with null bytes escaped - Improve argument parsing to handle KERN_PROCARGS2 format - Add fallback parsing if initial UTF-8 conversion fails This will help diagnose why process arguments appear empty in logs.
1 parent ccaf748 commit 0beb202

1 file changed

Lines changed: 20 additions & 2 deletions

File tree

Features/Monitoring/Domain/Services/ClaudeProcessDetector.swift

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,12 @@ final class ClaudeProcessDetector: Loggable, @unchecked Sendable {
7575
continue
7676
}
7777

78+
logger.debug("Process args length for PID \(pid): \(processArgs.count)")
79+
logger.debug("Process args for PID \(pid): \(String(processArgs.prefix(500).replacingOccurrences(of: "\0", with: "\\0")))")
80+
7881
// Check if this Node process is running Claude
7982
guard isClaudeProcess(arguments: processArgs) else {
8083
logger.debug("Node process PID \(pid) is not Claude")
81-
logger.debug("Process args for PID \(pid): \(String(processArgs.prefix(200)))")
8284
continue
8385
}
8486

@@ -168,8 +170,24 @@ final class ClaudeProcessDetector: Loggable, @unchecked Sendable {
168170
return nil
169171
}
170172

173+
// KERN_PROCARGS2 format: [argc: int32_t][executable path][args...]
174+
// Skip the first 4 bytes (argc) to get to the actual arguments
175+
guard argsMax > 4 else { return nil }
176+
171177
let argsData = Data(bytes: argsPtr, count: argsMax)
172-
return String(data: argsData, encoding: .utf8)
178+
179+
// Try to create string from the data, handling null bytes
180+
if let fullString = String(data: argsData, encoding: .utf8) {
181+
return fullString
182+
}
183+
184+
// If that fails, try to extract just the executable path
185+
let execData = argsData.dropFirst(4) // Skip argc
186+
if let execString = String(data: execData, encoding: .utf8) {
187+
return execString
188+
}
189+
190+
return nil
173191
}
174192

175193
private func getWorkingDirectory(pid: pid_t) -> String? {

0 commit comments

Comments
 (0)