Debug EzPC in vscode

在使用EzPC的FSS模块写代码的时候经常会遇到需要调试的情况,使用print太麻烦了,这里介绍如何使用vscode的调试功能,使用gdb进行调试。

首先需要将FSS的编译模式设置为debug

1
2
3
cd FSS/build
cmake -DCMAKE_INSTALL_PREFIX=./install -DCMAKE_BUILD_TYPE=Debug ../
make install

cmake在编译FSS库的时候,生成的编译的命令有"-g"的标志。

对于要执行的主程序,如果要加断点的话,编译的时候也要加上debug选项。这里使用一个脚本来直接编译c++主程序文件。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/bin/bash

set -e
testname=$1

cd $testname
# . config.sh

if [ -z ${HELP} ]; then
	FILENAME="$1"
	if [ -z ${FILENAME} ]; then
		echo "fssc: fatal error: no input files"
		exit 1
	fi

	# finds location of fssc dir
	SOURCE="${BASH_SOURCE[0]}"
	while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
		DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
		SOURCE="$(readlink "$SOURCE")"
		[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
	done
	currentDirPath="$( cd -- "$( dirname -- "$SOURCE" )" &> /dev/null && pwd )"
	
	###		Next find the filename, extension and thus the new file names
	fullFilePath=$(dirname "$FILENAME")
	baseFileName=$(basename -- "$FILENAME")
	extension="${baseFileName##*.}"
	actualFileName="${baseFileName%.*}"
	newFileNamePrefix="${fullFilePath}/${actualFileName}"
	newFileName2="${newFileNamePrefix}.${extension}"
	outfile="${newFileNamePrefix}.cpp"
fi

g++ -g -pthread -std=c++17 "-I${currentDirPath}/../../build/install/include" "main.cpp" "${currentDirPath}/../../build/install/lib/libfss.a" -o "main.out"

如果只需要调试fsslib,可以直接用EzPC自己的“fssc”将ezpc文件转换成c++文件并且编译成main.out

然后需要把main.out设置为vscode debug的入口,例如对于我的runcmp_and_select,在${workspaceFolder}/.vscode/launch.json中设置如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) 启动",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/tests/runcmp_and_select/main.out",
            "args": ["file=1", "r=1"],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}/tests/runcmp_and_select",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "将反汇编风格设置为 Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ]
        }

    ]
}

然后在FSS库的源码中需要调试的地方加上断点,在vscode的运行和调试里,选择“(gdb)启动”,就可以开始调试了。可以在“调试控制台”中看到gdb的输出,可以在“监视”里查看变量的值。