Skip to content

Commit d762fe5

Browse files
authored
Launch a service with waitForDebugger if specified (#1101)
This PR enables launching a service with `waitForDebugger` flag if the service label matches a given env variable `CONTAINER_DEBUG`.
1 parent 4f93e3e commit d762fe5

2 files changed

Lines changed: 54 additions & 1 deletion

File tree

BUILDING.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,42 @@ To revert to using the Containerization dependency from your `Package.swift`:
119119
bin/container system start
120120
```
121121

122+
## Debug XPC Helpers
123+
124+
Attach debugger to the XPC helpers using their launchd service labels:
125+
126+
1. Find launchd service labels:
127+
128+
```console
129+
% container system start
130+
% container run -d --name test debian:bookworm sleep infinity
131+
test
132+
% launchd list | grep container
133+
27068 0 com.apple.container.container-network-vmnet.default
134+
27072 0 com.apple.container.container-core-images
135+
26980 0 com.apple.container.apiserver
136+
27331 0 com.apple.container.container-runtime-linux.test
137+
```
138+
139+
2. Stop container and start again after setting the environment variable `CONTAINER_DEBUG_LAUNCHD_LABEL` to the label of service to attach debugger. Services whose label starts with the `CONTAINER_DEBUG_LAUNCHD_LABEL` will wait the debugger:
140+
141+
```console
142+
% export CONTAINER_DEBUG_LAUNCHD_LABEL=com.apple.container.container-runtime-linux.test
143+
% container system start # Only the service `com.apple.container.container-runtime-linux.test` waits debugger
144+
```
145+
146+
```console
147+
% export CONTAINER_DEBUG_LAUNCHD_LABEL=com.apple.container.container-runtime-linux
148+
% container system start # Every service starting with `com.apple.container.container-runtime-linux` waits debugger
149+
```
150+
151+
3. Run the command to launch the service, and attach debugger:
152+
153+
```console
154+
% container run -it --name test debian:bookworm
155+
⠧ [6/6] Starting container [0s] # It hangs as the service is waiting for debugger
156+
```
157+
122158
## Pre-commit hook
123159

124160
Run `make pre-commit` to install a pre-commit hook that ensures that your changes have correct formatting and license headers when you run `git commit`.

Sources/ContainerPlugin/LaunchPlist.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import Foundation
1919

2020
public struct LaunchPlist: Encodable {
21+
static let debugTarget = "CONTAINER_DEBUG_LAUNCHD_LABEL"
22+
2123
public enum Domain: String, Codable {
2224
case Aqua
2325
case Background
@@ -61,6 +63,21 @@ public struct LaunchPlist: Encodable {
6163
case waitForDebugger = "WaitForDebugger"
6264
}
6365

66+
static private func getWaitForDebugger(label: String, fromArg: Bool?) -> Bool? {
67+
if let fromArg {
68+
return fromArg
69+
}
70+
71+
let env = ProcessInfo.processInfo.environment
72+
if let debugTarget = env[Self.debugTarget],
73+
label == debugTarget || label.starts(with: debugTarget + ".")
74+
{
75+
return true
76+
}
77+
78+
return nil
79+
}
80+
6481
public init(
6582
label: String,
6683
arguments: [String],
@@ -93,7 +110,7 @@ public struct LaunchPlist: Encodable {
93110
self.disabled = disabled
94111
self.program = program
95112
self.keepAlive = keepAlive
96-
self.waitForDebugger = waitForDebugger
113+
self.waitForDebugger = Self.getWaitForDebugger(label: label, fromArg: waitForDebugger)
97114
if let services = machServices {
98115
var machServices: [String: Bool] = [:]
99116
for service in services {

0 commit comments

Comments
 (0)