Skip to content

Commit e8500c4

Browse files
committed
Add hack/codesign/debugserver that wraps the LLDB debugserver to codesign the target executable.
# `hack/codesign/debugserver` This script wraps the LLDB `debugserver` to `codesign` the target executable. This is needed for [Delve](https://github.com/go-delve/delve) to work properly on macOS with the virtualization framework. ## How to use this script with Delve ### Use `DELVE_DEBUGSERVER_PATH` environment variable Override `debugserver` by setting the `DELVE_DEBUGSERVER_PATH` environment variable. Ref: [Environment variables - Using Delve](https://github.com/go-delve/delve/blob/master/Documentation/usage/README.md#environment-variables) e.g. in `.vscode/launch.json`: ```jsonc { "version": "0.2.0", "configurations": [ { "name": "hostagent for input instance", "type": "go", "request": "launch", "mode": "debug", // Use integratedTerminal to stop using ctrl+C "console": "integratedTerminal", "program": "${workspaceFolder}/cmd/limactl", "buildFlags": [ "-ldflags=-X github.com/lima-vm/lima/pkg/version.Version=2.0.0-alpha.0", ], "env": { "CGO_ENABLED": "1", "DELVE_DEBUGSERVER_PATH": "${workspaceFolder}/hack/codesign/debugserver", "LIMA_SSH_PORT_FORWARDER": "true", }, "cwd": "${userHome}/.lima/${input:targetInstance}", "args": [ "--debug", "hostagent", "--pidfile", "ha.pid", "--socket", "ha.sock", "--guestagent", "${workspaceFolder}/_output/share/lima/lima-guestagent.Linux-aarch64", "${input:targetInstance}" ], }, ], "inputs": [ { "id": "targetInstance", "type": "promptString", "description": "Input target instance parameter for `limactl` command", } ] } ``` ### Override `debugserver` in the PATH You can also override the `debugserver` in the PATH environment variable. e.g. in `.vscode/launch.json`: ```diff "env": { "CGO_ENABLED": "1", - "DELVE_DEBUGSERVER_PATH": "${workspaceFolder}/hack/codesign/debugserver", + "PATH": "${workspaceFolder}/hack/codesign:${env:PATH}", }, ``` Signed-off-by: Norio Nomura <[email protected]>
1 parent cc46912 commit e8500c4

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

hack/codesign/debugserver

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#!/bin/sh
2+
set -eu
3+
4+
# SPDX-FileCopyrightText: Copyright The Lima Authors
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
# # `hack/codesign/debugserver`
8+
#
9+
# This script wraps the LLDB `debugserver` to `codesign` the target executable.
10+
# This is needed for [Delve](https://github.com/go-delve/delve) to work properly
11+
# on macOS with the virtualization framework.
12+
#
13+
# ## How to use this script with Delve
14+
#
15+
# ### Use `DELVE_DEBUGSERVER_PATH` environment variable
16+
#
17+
# Override `debugserver` by setting the `DELVE_DEBUGSERVER_PATH` environment variable.
18+
# Ref: [Environment variables - Using Delve](https://github.com/go-delve/delve/blob/master/Documentation/usage/README.md#environment-variables)
19+
#
20+
# e.g. in `.vscode/launch.json`:
21+
# ```jsonc
22+
# {
23+
# "version": "0.2.0",
24+
# "configurations": [
25+
# {
26+
# "name": "hostagent for input instance",
27+
# "type": "go",
28+
# "request": "launch",
29+
# "mode": "debug",
30+
# // Use integratedTerminal to stop using ctrl+C
31+
# "console": "integratedTerminal",
32+
# "program": "${workspaceFolder}/cmd/limactl",
33+
# "buildFlags": [
34+
# "-ldflags=-X github.com/lima-vm/lima/pkg/version.Version=2.0.0-alpha.0",
35+
# ],
36+
# "env": {
37+
# "CGO_ENABLED": "1",
38+
# "DELVE_DEBUGSERVER_PATH": "${workspaceFolder}/hack/codesign/debugserver",
39+
# "LIMA_SSH_PORT_FORWARDER": "true",
40+
# },
41+
# "cwd": "${userHome}/.lima/${input:targetInstance}",
42+
# "args": [
43+
# "--debug",
44+
# "hostagent",
45+
# "--pidfile", "ha.pid",
46+
# "--socket", "ha.sock",
47+
# "--guestagent", "${workspaceFolder}/_output/share/lima/lima-guestagent.Linux-aarch64",
48+
# "${input:targetInstance}"
49+
# ],
50+
# },
51+
# ],
52+
# "inputs": [
53+
# {
54+
# "id": "targetInstance",
55+
# "type": "promptString",
56+
# "description": "Input target instance parameter for `limactl` command",
57+
# }
58+
# ]
59+
# }
60+
# ```
61+
#
62+
# ### Override `debugserver` in the PATH
63+
#
64+
# You can also override the `debugserver` in the PATH environment variable.
65+
#
66+
# e.g. in `.vscode/launch.json`:
67+
# ```diff
68+
# "env": {
69+
# "CGO_ENABLED": "1",
70+
# - "DELVE_DEBUGSERVER_PATH": "${workspaceFolder}/hack/codesign/debugserver",
71+
# + "PATH": "${workspaceFolder}/hack/codesign:${env:PATH}",
72+
# },
73+
# ```
74+
75+
script_dir="$(dirname "$0")"
76+
77+
# Codesign the target executable if vz.entitlements exists
78+
entitlements="${script_dir}/../../vz.entitlements"
79+
if [ -f "${entitlements}" ]; then
80+
prev_arg=
81+
for arg in "$@"; do
82+
# find the target executable in the args next to "--"
83+
if [ "${prev_arg}" = "--" ]; then
84+
codesign --entitlements "${entitlements}" --force -s - -v "${arg}" || {
85+
echo "error: codesign failed" >&2
86+
exit 1
87+
}
88+
break
89+
fi
90+
prev_arg="${arg}"
91+
done
92+
fi
93+
94+
# Simulate how Delve locates debugserver
95+
# https://github.com/go-delve/delve/blob/65a6830eb7f0b882e0c52df0ef9585945ed55470/pkg/proc/gdbserial/gdbserver.go#L103-L129
96+
# 1. PATH (in this script, excluding the directory of this script to avoid recursion)
97+
candidate_paths="$(echo "${PATH}" | tr ':' '\n' | grep --fixed-strings --invert-match --line-regexp "${script_dir}" | paste -sd: -)"
98+
# 2. CommandLineTools LLDB path
99+
CLT_path="/Library/Developer/CommandLineTools"
100+
candidate_paths="${candidate_paths}:${CLT_path}/Library/PrivateFrameworks/LLDB.framework/Versions/A/Resources/"
101+
# 3. Xcode LLDB path (if Xcode is installed)
102+
if developer_dir=$(xcode-select --print-path 2>/dev/null) && [ "${developer_dir}" != "${CLT_path}" ]; then
103+
# Xcode is installed
104+
candidate_paths="${candidate_paths}:${developer_dir}/../SharedFrameworks/LLDB.framework/Versions/A/Resources/"
105+
fi
106+
# Find debugserver in the candidate paths
107+
debugserver=$(PATH="${candidate_paths}" command -v debugserver) || {
108+
echo "error: debugserver not found" >&2
109+
exit 1
110+
}
111+
# Execute debugserver with the original arguments
112+
exec "${debugserver}" "$@"

0 commit comments

Comments
 (0)