From b01de5038617bbfaef4194ea129a4ae1b667d766 Mon Sep 17 00:00:00 2001 From: David Hopper Date: Fri, 6 Apr 2018 16:21:42 +0800 Subject: [PATCH] vscode: support entering debug mode with F5 key in VSCode. --- .vscode/launch.json | 11 +++- .vscode/tasks.json | 21 +++++++ docker/scripts/dev_start_gdb_server.sh | 63 +++++++++++++++++++++ scripts/start_gdb_server.sh | 76 ++++++++++++++++++++++++++ 4 files changed, 169 insertions(+), 2 deletions(-) create mode 100755 docker/scripts/dev_start_gdb_server.sh create mode 100755 scripts/start_gdb_server.sh diff --git a/.vscode/launch.json b/.vscode/launch.json index 5988e90b90..6b60985a4f 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -5,10 +5,17 @@ "name": "C++ Launch", "type": "cppdbg", "request": "launch", - // you can change the "planning" to your module name. + // You can change the "planning" to your module name, but it should be + // same as in gdbserver of the docker container. "program": "${workspaceRoot}/bazel-bin/modules/planning/planning", - // you can change "localhost:1111" to another "IP:port" name. + // You can change "localhost:1111" to another "IP:port" name, but it + // should be same as in gdbserver of the docker container. "miDebuggerServerAddress": "localhost:1111", + // You can change the task to meet your demands in the + // ".vscode/tasks.json" file (search the label: + // "start the gdbserver in the ocker", but the module name and the port + // number should be consistent with this file. + "preLaunchTask": "start gdbserver", "args": [], "stopAtEntry": false, "cwd": "${workspaceRoot}", diff --git a/.vscode/tasks.json b/.vscode/tasks.json index af3d1adfef..e1f0f55ebd 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -92,6 +92,27 @@ "message": 5 } } + }, + { + "label": "start gdbserver", + "type": "shell", + // you can change the "planning" module name to another one and + // change the "1111" to another port number. + // Note: the module name and port number should be same as in + // the "launch.json" file. + "command": "bash docker/scripts/dev_start_gdb_server.sh planning 1111", + "isBackground": true, + "problemMatcher": { + "owner": "custom", + "pattern": { + "regexp": "__________" + }, + "background": { + "activeOnStart": true, + "beginsPattern": "^Listening on port$", + "endsPattern": "^$" + } + } } ] } \ No newline at end of file diff --git a/docker/scripts/dev_start_gdb_server.sh b/docker/scripts/dev_start_gdb_server.sh new file mode 100755 index 0000000000..0f672c9220 --- /dev/null +++ b/docker/scripts/dev_start_gdb_server.sh @@ -0,0 +1,63 @@ +#!/usr/bin/env bash + +############################################################################### +# Copyright 2017 The Apollo Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +############################################################################### + +function check_docker_open() { + docker ps --format "{{.Names}}" | grep apollo_dev 1>/dev/null 2>&1 + if [ $? != 0 ]; then + echo "The docker is not started, please start it first. " + exit 1 + fi +} + +function print_usage() { + RED='\033[0;31m' + BLUE='\033[0;34m' + BOLD='\033[1m' + NONE='\033[0m' + + echo -e "\n${RED}Usage${NONE}: + .${BOLD}/dev_debug_server.sh${NONE} MODULE_NAME PORT_NUMBER" + + echo -e "${RED}MODULE_NAME${NONE}: + ${BLUE}planning${NONE}: debug the planning module. + ${BLUE}control${NONE}: debug the control module. + ${BLUE}routing${NONE}: debug the routing module. + ..., and so on." + + echo -e "${RED}PORT_NUMBER${NONE}: + ${NONE}a port number, such as '1111'." +} + +if [ $# -lt 2 ];then + print_usage + exit 1 +fi + +check_docker_open + +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +cd "${DIR}/../.." +pwd + +xhost +local:root 1>/dev/null 2>&1 +#echo $@ +docker exec \ + -u $USER \ + -it apollo_dev \ + /bin/bash scripts/start_gdb_server.sh $@ +xhost -local:root 1>/dev/null 2>&1 diff --git a/scripts/start_gdb_server.sh b/scripts/start_gdb_server.sh new file mode 100755 index 0000000000..42f727e2ec --- /dev/null +++ b/scripts/start_gdb_server.sh @@ -0,0 +1,76 @@ +#!/usr/bin/env bash + +############################################################################### +# Copyright 2017 The Apollo Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +############################################################################### + +function print_usage() { + RED='\033[0;31m' + BLUE='\033[0;34m' + BOLD='\033[1m' + NONE='\033[0m' + + echo -e "\n${RED}Usage${NONE}: + .${BOLD}/start_gdb_server.sh${NONE} MODULE_NAME PORT_NUMBER" + + echo -e "${RED}MODULE_NAME${NONE}: + ${BLUE}planning${NONE}: debug the planning module. + ${BLUE}control${NONE}: debug the control module. + ${BLUE}routing${NONE}: debug the routing module. + ..., and so on." + + echo -e "${RED}PORT_NUMBER${NONE}: + ${NONE}a port number, such as '1111'." +} + +if [ $# -lt 2 ];then + print_usage + exit 1 +fi + +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +source "${DIR}/apollo_base.sh" + +MODULE_NAME=$1 +PORT_NUM=$2 +shift 2 + +# If there is a gdbserver process running, stop it first. +GDBSERVER_NUMS=$(pgrep -c -x "gdbserver") +if [ ${GDBSERVER_NUMS} -ne 0 ]; then + sudo pkill -f "gdbserver" +fi + +# Because the "grep ${MODULE_NAME}" always generates a process with the name of +# "${MODULE_NAME}", I added another grep to remove grep itself from the output. +# The following command got a wrong result and I can't find the reason. +#PROCESS_ID=$(ps -eo pid,command | grep "${MODULE_NAME}" | grep -v "grep" | awk '{print $1}') +# This one is OK. +PROCESS_ID=$(pgrep -o -x "${MODULE_NAME}") +#echo ${PROCESS_ID} + +# If the moudle is not started, start it first. +if [ -z ${PROCESS_ID} ]; then + #echo "The '${MODULE_NAME}' module is not started, please start it in the dreamview first. " + #exit 1 + + # run function from apollo_base.sh + # run command_name module_name + run ${MODULE_NAME} "$@" + + PROCESS_ID=$(pgrep -o -x "${MODULE_NAME}") +fi + +sudo gdbserver :${PORT_NUM} --attach ${PROCESS_ID} \ No newline at end of file -- GitLab