简体   繁体   English

如何在 Visual Studio Code 中使用 Cgo 调试 Go

[英]How to debug Go with Cgo in Visual Studio Code

I can set breakpoints and step through the Go code without issues in Visual Studio Code if there is no Cgo code.如果没有 Cgo 代码,我可以在 Visual Studio Code 中设置断点并单步执行 Go 代码而不会出现问题。 Once Cgo code is called in the Go code, breakpoints are basically ignored, although the application runs fine.一旦在 Go 代码中调用了 Cgo 代码,断点基本上被忽略了,尽管应用程序运行良好。

Here is the snippet:这是片段:

//hello.c
#include <stdio.h>

void Test() {
  printf("C: Hello world");
}

//hello.go
package main

// #cgo LDFLAGS: -Wl,--allow-multiple-definition
// #cgo CFLAGS: -Wall -std=c99 -O1 -g
/*
#include "hello.c"
*/
import "C"

import "fmt"

func main() {
    //Call to void function without params
    C.Test()
    fmt.Printf("Go: Hello world\n")
}

//launch.json
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch Package",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "program": "learning"
        }
    ]
}

Currently I am facing this exact bug.目前我正面临这个确切的错误。 If you time.Sleep at the beginning如果你有时间。睡一开始

func main() {

    time.Sleep(time.Hour) //now hover on your breakpoints
    
    //Call to void function without params
    C.Test()
    fmt.Printf("Go: Hello world\n")
}

you can see the breakpoints are hollow.你可以看到断点是空的。 Only information I can get is from the tooltip at that states我只能从该州的工具提示中获取信息

could not find the file c:\d\e\main.go

I can only reproduce this on windows.我只能在 Windows 上重现这个。 Might be because windows uses \\ for it's paths but c compiler or dlv-dap or go can't tell if it's a windows path.可能是因为 windows 使用 \\ 作为它的路径,但 c 编译器或 dlv-dap 或 go 无法判断它是否是 windows 路径。 Because in most programs \\ is an escape character.因为在大多数程序中 \\ 是一个转义字符。 Normally path separators are escaped using \\\\.通常路径分隔符使用 \\\\ 进行转义。 Windows currently supports both forward and backslashes except sometimes it doesn't work. Windows 目前支持正斜杠和反斜杠,但有时它不起作用。 To my knowledge cgo debugging is working normally with UNIX alike systems that uses / on file paths.据我所知,cgo 调试与在文件路径上使用 / 的类似 UNIX 的系统一起正常工作。 This is probably underlying bug that you can't solve by changing the terminal.这可能是您无法通过更改终端解决的潜在错误。

This might not be an option but you could try linux or macos.这可能不是一种选择,但您可以尝试使用 linux 或 macos。

This is currently a known bug .这是目前已知的错误 This may get fixed in go 1.7.4.这可能会在 go 1.7.4 中得到修复。 You can normally debug on Linux.您通常可以在 Linux 上进行调试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM