hs-test: add support for running vpp in gdb

Type: test

Signed-off-by: Filip Tehlar <ftehlar@cisco.com>
Change-Id: I6e03b88ca013cafd73f424ea63f706f105bebe6b
diff --git a/extras/hs-test/Makefile b/extras/hs-test/Makefile
index 268a518..29cbd0e 100644
--- a/extras/hs-test/Makefile
+++ b/extras/hs-test/Makefile
@@ -15,6 +15,10 @@
 TEST=all
 endif
 
+ifeq ($(DEBUG),)
+DEBUG=false
+endif
+
 list_tests = @(grep -r ') Test' *_test.go | cut -d '*' -f2 | cut -d '(' -f1 | \
 		tr -d ' ' | tr ')' '/' | sed 's/Suite//')
 
@@ -33,6 +37,7 @@
 	@echo " PERSIST=[true|false]     - whether clean up topology and dockers after test"
 	@echo " VERBOSE=[true|false]     - verbose output"
 	@echo " UNCONFIGURE=[true|false] - unconfigure selected test"
+	@echo " DEBUG=[true|false]       - attach VPP to GDB"
 	@echo " TEST=[test-name]         - specific test to run"
 	@echo
 	@echo "List of all tests:"
@@ -51,7 +56,7 @@
 .PHONY: test
 test: .deps.ok .build.vpp
 	@bash ./test --persist=$(PERSIST) --verbose=$(VERBOSE) \
-		--unconfigure=$(UNCONFIGURE) --test=$(TEST)
+		--unconfigure=$(UNCONFIGURE) --debug=$(DEBUG) --test=$(TEST)
 
 build-go:
 	go build ./tools/http_server
diff --git a/extras/hs-test/hst_suite.go b/extras/hs-test/hst_suite.go
index 8e52cc4..4e4b7d2 100644
--- a/extras/hs-test/hst_suite.go
+++ b/extras/hs-test/hst_suite.go
@@ -20,6 +20,7 @@
 var IsPersistent = flag.Bool("persist", false, "persists topology config")
 var IsVerbose = flag.Bool("verbose", false, "verbose test output")
 var IsUnconfiguring = flag.Bool("unconfigure", false, "remove topology")
+var IsVppDebug = flag.Bool("debug", false, "attach gdb to vpp")
 
 type HstSuite struct {
 	suite.Suite
diff --git a/extras/hs-test/test b/extras/hs-test/test
index f02c159..a886652 100755
--- a/extras/hs-test/test
+++ b/extras/hs-test/test
@@ -6,6 +6,7 @@
 single_test=0
 persist_set=0
 unconfigure_set=0
+debug_set=0
 
 for i in "$@"
 do
@@ -17,6 +18,13 @@
             persist_set=1
         fi
         ;;
+    --debug=*)
+        debug="${i#*=}"
+        if [ $debug = "true" ]; then
+            args="$args -debug"
+            debug_set=1
+        fi
+        ;;
     --verbose=*)
         verbose="${i#*=}"
         if [ $verbose = "true" ]; then
@@ -54,4 +62,9 @@
     exit 1
 fi
 
+if [ $single_test -eq 0 ] && [ $debug_set -eq 1 ]; then
+    echo "VPP debug flag is not supperted while running all tests!"
+    exit 1
+fi
+
 sudo -E go test -buildvcs=false -v $args
diff --git a/extras/hs-test/vppinstance.go b/extras/hs-test/vppinstance.go
index 4092d35..1c28ec9 100644
--- a/extras/hs-test/vppinstance.go
+++ b/extras/hs-test/vppinstance.go
@@ -3,8 +3,11 @@
 import (
 	"fmt"
 	"github.com/edwarnicke/exechelper"
+	"os"
 	"os/exec"
+	"os/signal"
 	"strings"
+	"syscall"
 	"time"
 
 	"go.fd.io/govpp"
@@ -113,8 +116,27 @@
 	startupFileName := vpp.getEtcDir() + "/startup.conf"
 	vpp.container.createFile(startupFileName, configContent)
 
-	// Start VPP
-	vpp.container.execServer("su -c \"vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
+	if *IsVppDebug {
+		sig := make(chan os.Signal, 1)
+		signal.Notify(sig, syscall.SIGINT)
+		cont := make(chan bool, 1)
+		go func() {
+			sig := <-sig
+			fmt.Println(sig)
+			cont <- true
+		}()
+
+		// Start VPP in GDB and wait for user to attach it
+		vpp.container.execServer("su -c \"gdb -ex run --args vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
+		fmt.Println("run following command in different terminal:")
+		fmt.Println("docker exec -it " + vpp.container.name + " gdb -ex \"attach $(docker exec " + vpp.container.name + " pidof gdb)\"")
+		fmt.Println("Afterwards press CTRL+C to continue")
+		<-cont
+		fmt.Println("continuing...")
+	} else {
+		// Start VPP
+		vpp.container.execServer("su -c \"vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
+	}
 
 	// Connect to VPP and store the connection
 	sockAddress := vpp.container.GetHostWorkDir() + defaultApiSocketFilePath