From d91a01dc868871e48b944bc4398fddb14573f329 Mon Sep 17 00:00:00 2001 From: Laurent Goderre Date: Tue, 26 Nov 2024 14:22:04 -0500 Subject: [PATCH] Add support for delve debugging in the shell Signed-off-by: Laurent Goderre --- Dockerfile | 3 ++- hack/run | 34 ++++++++++++++++++++++++++++++++++ hack/shell | 16 +++++++++++++--- 3 files changed, 49 insertions(+), 4 deletions(-) create mode 100755 hack/run diff --git a/Dockerfile b/Dockerfile index 3e674530280a..442819082c52 100644 --- a/Dockerfile +++ b/Dockerfile @@ -356,7 +356,7 @@ ENV BUILDKIT_SETUP_CGROUPV2_ROOT=1 ENTRYPOINT ["buildkitd"] FROM buildkit-linux AS buildkit-linux-debug -COPY --link --from=dlv /usr/bin/dlv /usr/bin/dlv +COPY --link --from=dlv /out/dlv /usr/bin/dlv COPY --link --chmod=755 </dev/null; then + echo >&2 'error: binary-daemon or dynbinary-daemon must be run before run' + false +fi + +COMMAND="$(command -v $1)" + +if [ -n "$DELVE_PORT" ]; then + delve_listen_port="${DELVE_PORT##*:}" +fi + +command=("$COMMAND") + +if [ -n "$DELVE_PORT" ]; then + command=( + dlv + --listen="0.0.0.0:$delve_listen_port" + --headless=true + --log + --api-version=2 + --only-same-user=false + --check-go-version=false + --accept-multiclient + exec "${command[@]}" -- + ) +fi + +set -x +# shellcheck disable=SC2086 +exec "${command[@]}" diff --git a/hack/shell b/hack/shell index 15ce006cbc21..1ba98592ca70 100755 --- a/hack/shell +++ b/hack/shell @@ -9,16 +9,26 @@ function clean() { iidfile=$(mktemp -t docker-iidfile.XXXXXXXXXX) DOCKER_BUILDKIT=1 docker build --iidfile $iidfile --build-arg BUILDKIT_DEBUG --target dev-env . +BUILDKIT_REGISTRY_MIRROR_DIR="/root/.cache/registry" + +ENV="-e DELVE_PORT -e BUILDKIT_REGISTRY_MIRROR_DIR" + trap clean EXIT SSH= if [ -n "$MOUNT_SSH_AUTH_SOCK" ]; then SSH="-v $SSH_AUTH_SOCK:$SSH_AUTH_SOCK -e SSH_AUTH_SOCK" fi -volumes= +volumes="-v /tmp" if [ -n "$MOUNT_BUILDKIT_SOURCE" ]; then - volumes="-v $(pwd):/src" + volumes="$volumes -v $(pwd):/src" fi +if [ -n "$DELVE_PORT" ]; then + DELVE_PORT_FORWARD="-p $DELVE_PORT" +fi + +DOCKER_FLAGS="-it --rm --privileged --name=buildkit-dev $SSH $volumes $ENV $DELVE_PORT_FORWARD" + set -x -docker run $SSH $volumes -it --privileged -v /tmp -e BUILDKIT_REGISTRY_MIRROR_DIR=/root/.cache/registry --rm $(cat $iidfile) ash +docker run $DOCKER_FLAGS $(cat $iidfile) ash