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