如何在VSCode中调试AWS SAM制作的应用程序?

时间:2019-12-28 15:42:31

标签: debugging go visual-studio-code aws-sam

摘要

我通过AWS SAM制作了Go应用程序。现在,我尝试在VSCode中调试该示例应用程序,但是失败了,所以我想知道如何正确调试它。

尝试

toggl-slack
├── Makefile                    
├── README.md     
├── dlv 
├── samconfig.toml               
├── hello-world                
│   ├── main.go                 
│   └── main_test.go            
└── template.yaml

我在控制台上输入以下命令进行调试。

cd toggl-slack
go get -u github.com/go-delve/delve/cmd/dlv
GOARCH=amd64 GOOS=linux go build -o ./dlv github.com/go-delve/delve/cmd/dlv
GOARCH=amd64 GOOS=linux go build -gcflags='-N -l' -o hello-world/hello-world ./hello-world
sam local start-api -d 5986 --debugger-path . --debug-args="-delveAPI=2"
curl http://127.0.0.1:3000/hello

结果,调试不起作用,控制台显示错误消息。

Mounting HelloWorldFunction at http://127.0.0.1:3000/hello [GET]
You can now browse to the above endpoints to invoke your functions. You do not need to restart/reload SAM CLI while working on your functions, changes will be reflected instantly/automatically. You only need to restart SAM CLI if you update your AWS SAM template
2019-12-28 21:23:17  * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)
Invoking hello-world (go1.x)

Fetching lambci/lambda:go1.x Docker container image......
Mounting /Users/jpskgc/toggl-slack/hello-world as /var/task:ro,delegated inside runtime container
Could not create config directory: mkdir /home/sbx_user1051: permission denied.API server listening at: [::]:5986
2019-12-28T12:23:46Z info layer=debugger launching process with args: [/var/task/hello-world]
2019-12-28T12:23:47Z warning layer=debugger reading debug_info: could not find abstract origin (0x13ed31) of inlined call at 0xfab50

代码

launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Connect to Lambda container",
      "type": "go",
      "request": "launch",
      "mode": "remote",
      "remotePath": "",
      "port": 5986,
      "host": "127.0.0.1",
      "program": "${workspaceRoot}",
      "env": {},
      "args": []
    }
  ]
}

其他代码与AWS SAM的默认示例应用程序相同。

完整的源代码在这里:
https://github.com/jpskgc/toggl-slack/tree/0db02109685ce89f17ed64fdaadd5261e5f61512

1 个答案:

答案 0 :(得分:2)

reading debug_info: could not find abstract origin行告诉我们,编译标志被加扰,并且调试信息实际上未包括在内。我们需要的是:

GOOS=linux GOARCH=amd64 go build -gcflags "all=-N -l" -o hello-world/hello-world ./hello-world

第二,VSCode应该告诉您这一点,但是在launch.json中,"request": "launch",现在是模式"request": "attach",的{​​{1}},给出:

remote

通过上述更改,您应该能够使用VSCode(或GoLand / Vim / CLI)连接到调试服务器。

最后,{ "version": "0.2.0", "configurations": [ { "name": "Connect to SAM local", "type": "go", "request": "attach", "mode": "remote", "remotePath": "", "port": 5986, "host": "127.0.0.1", "program": "${workspaceRoot}", "env": {}, "args": [] } ] } 行可以忽略。

我代表我的雇主Amazon捐款。我的贡献是根据MIT许可获得许可的。有关详细说明,请参见here