Skip to content

Commit 3973628

Browse files
authored
Merge pull request #2766 from wk8/wk8/debugging
Adding a `hack/debug` script
2 parents 8d8689d + 0a88f50 commit 3973628

4 files changed

Lines changed: 105 additions & 25 deletions

File tree

Makefile

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,8 @@
11
# Root directory of the project (absolute path).
22
ROOTDIR=$(dir $(abspath $(lastword $(MAKEFILE_LIST))))
33

4-
# Base path used to install.
5-
DESTDIR=/usr/local
6-
7-
# Used to populate version variable in main package.
8-
VERSION=$(shell git describe --match 'v[0-9]*' --dirty='.m' --always)
9-
104
PROJECT_ROOT=github.com/docker/swarmkit
115

12-
# Race detector is only supported on amd64.
13-
RACE := $(shell test $$(go env GOARCH) != "amd64" || (echo "-race"))
14-
15-
# Project packages.
16-
PACKAGES=$(shell go list ./... | grep -v /vendor/)
17-
INTEGRATION_PACKAGE=${PROJECT_ROOT}/integration
18-
19-
# Project binaries.
20-
COMMANDS=swarmd swarmctl swarm-bench swarm-rafttool protoc-gen-gogoswarm
21-
BINARIES=$(addprefix bin/,$(COMMANDS))
22-
23-
VNDR=$(shell which vndr || echo '')
24-
25-
GO_LDFLAGS=-ldflags "-X `go list ./version`.Version=$(VERSION)"
26-
276
SHELL := /bin/bash
287

298
# stop here. do we want to run everything inside of a container, or do we want

containerized.mk

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ IMAGE_NAME=docker/swarmkit
22
GOPATH=/go
33
DOCKER_IMAGE_DIR=${GOPATH}/src/${PROJECT_ROOT}
44

5+
DOCKER_SWARMKIT_DELVE_PORT ?= 2345
6+
57
# don't bother writing every single make target. just pass the call through to
68
# docker and make
79
# we prefer `%:` to `.DEFAULT` as the latter doesn't run phony deps
@@ -40,10 +42,16 @@ run: ensure_image_exists
4042
@ [ "$$DOCKER_SWARMKIT_DOCKER_RUN_CMD" ] || exit 1
4143
@ DOCKER_RUN_COMMAND="docker run -t -v swarmkit-cache:${GOPATH}" \
4244
&& if [ "$$DOCKER_SWARMKIT_USE_DOCKER_SYNC" ]; then \
43-
$(MAKE) ensure_sync_started && DOCKER_RUN_COMMAND="$$DOCKER_RUN_COMMAND -v swarmkit-sync:${DOCKER_IMAGE_DIR}"; \
45+
$(MAKE) ensure_sync_started && DOCKER_RUN_COMMAND+=" -v swarmkit-sync:${DOCKER_IMAGE_DIR}"; \
4446
else \
45-
DOCKER_RUN_COMMAND="$$DOCKER_RUN_COMMAND -v ${ROOTDIR}:${DOCKER_IMAGE_DIR}"; \
47+
DOCKER_RUN_COMMAND+=" -v ${ROOTDIR}:${DOCKER_IMAGE_DIR}"; \
48+
fi \
49+
&& if [ "$$DOCKER_SWARMKIT_USE_DELVE" ]; then \
50+
DOCKER_RUN_COMMAND="DOCKER_SWARMKIT_DELVE_PORT=${DOCKER_SWARMKIT_DELVE_PORT} $$DOCKER_RUN_COMMAND" ; \
51+
DOCKER_RUN_COMMAND+=" -p ${DOCKER_SWARMKIT_DELVE_PORT}:${DOCKER_SWARMKIT_DELVE_PORT} -e DOCKER_SWARMKIT_DELVE_PORT"; \
52+
`# see https://github.com/derekparker/delve/issues/515#issuecomment-214911481'` ; \
53+
DOCKER_RUN_COMMAND+=" --security-opt=seccomp:unconfined"; \
4654
fi \
47-
&& DOCKER_RUN_COMMAND="$$DOCKER_RUN_COMMAND $$DOCKER_SWARMKIT_DOCKER_RUN_FLAGS ${IMAGE_NAME} $$DOCKER_SWARMKIT_DOCKER_RUN_CMD" \
55+
&& DOCKER_RUN_COMMAND+=" $$DOCKER_SWARMKIT_DOCKER_RUN_FLAGS ${IMAGE_NAME} $$DOCKER_SWARMKIT_DOCKER_RUN_CMD" \
4856
&& echo $$DOCKER_RUN_COMMAND \
49-
&& $$DOCKER_RUN_COMMAND
57+
&& eval $$DOCKER_RUN_COMMAND

direct.mk

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
# Base path used to install.
2+
DESTDIR=/usr/local
3+
4+
# Used to populate version variable in main package.
5+
VERSION=$(shell git describe --match 'v[0-9]*' --dirty='.m' --always)
6+
7+
# Race detector is only supported on amd64.
8+
RACE := $(shell test $$(go env GOARCH) != "amd64" || (echo "-race"))
9+
10+
# Project packages.
11+
PACKAGES=$(shell go list ./... | grep -v /vendor/)
12+
INTEGRATION_PACKAGE=${PROJECT_ROOT}/integration
13+
14+
# Project binaries.
15+
COMMANDS=swarmd swarmctl swarm-bench swarm-rafttool protoc-gen-gogoswarm
16+
BINARIES=$(addprefix bin/,$(COMMANDS))
17+
18+
VNDR=$(shell which vndr || echo '')
19+
20+
GO_LDFLAGS=-ldflags "-X `go list ./version`.Version=$(VERSION)"
21+
22+
123
.DEFAULT_GOAL = all
224
.PHONY: all
325
all: check binaries test integration-tests ## run check, build the binaries and run the tests

hack/debug

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/bin/bash
2+
3+
## Installs delve, then runs it as a headless server
4+
## Keeps running until killed
5+
## Also adds some goodies: delve servers ignore interrupts, which is annoying...
6+
## and also once a debugging session is done, the server just hangs there, which
7+
## is equally annoying.
8+
## This script takes care of both these things
9+
10+
SUBSHELL_PID=
11+
DLV_PID_FILE=
12+
RUNNING=true
13+
14+
main() {
15+
[ "$1" ] || usage
16+
17+
ensure_delve_installed || exit $?
18+
19+
local PORT="$DOCKER_SWARMKIT_DELVE_PORT"
20+
[ "$PORT" ] || PORT=2345
21+
22+
local DLV_CMD="dlv test --accept-multiclient --headless --listen=:$PORT --api-version=2 --log $@"
23+
echo $DLV_CMD
24+
25+
trap handle_interrupt INT
26+
27+
DLV_PID_FILE=$(mktemp /tmp/dlv.XXXXXX.pid)
28+
local DLV_OUTPUT_FILE=$(mktemp /tmp/dlv.XXXXXX.out)
29+
30+
# the weird regex is because we keep the output colored
31+
local HALTING_REGEX='^\e\[37mDEBU\e\[0m\[[0-9]+\] halting\s+\e\[37mlayer\e\[0m=debugger'
32+
while $RUNNING; do
33+
# using `script` to keep colored output, and `exec` to get the PID from the
34+
# subshell
35+
script --flush --quiet "$DLV_OUTPUT_FILE" --command 'printf $$ > '"$DLV_PID_FILE && exec $DLV_CMD" &
36+
SUBSHELL_PID=$!
37+
38+
# wait for either the subshell to die, or for the "halting" line to appear
39+
# in the output
40+
tail --follow --pid="$SUBSHELL_PID" --sleep-interval=0.1 "$DLV_OUTPUT_FILE" 2> /dev/null | grep --perl-regex --line-buffered "$HALTING_REGEX" | ( read && kill_delve )
41+
42+
wait "$SUBSHELL_PID"
43+
done
44+
45+
rm -f "$DLV_PID_FILE" "$DLV_OUTPUT_FILE"
46+
}
47+
48+
handle_interrupt() {
49+
RUNNING=false
50+
kill_delve
51+
}
52+
53+
kill_delve() {
54+
if [ -r "$DLV_PID_FILE" ]; then
55+
local DLV_PID=$(cat "$DLV_PID_FILE")
56+
[ "$DLV_PID" ] && kill "$DLV_PID" &> /dev/null
57+
fi
58+
59+
[ "$SUBSHELL_PID" ] && kill $SUBSHELL_PID &> /dev/null
60+
}
61+
62+
ensure_delve_installed() {
63+
which dlv &> /dev/null || go get -u github.com/derekparker/delve/cmd/dlv
64+
}
65+
66+
usage() {
67+
echo "Usage: $0 name/of/go/package [additional dlv test options]"
68+
exit 1
69+
}
70+
71+
main "$@"

0 commit comments

Comments
 (0)