如何调试使用cgo调用的C代码?

huangapple go评论91阅读模式
英文:

How can I debug C code called with cgo?

问题

背景

我目前正在为这个编写一个Go接口。

作为第一步,我正在尝试通过Go包装器调用/test/test_pc.c中的测试。值得注意的是,当使用GCC编译时,这些测试是成功的。

重要的额外细节:值得一提的是,目前我将所有要运行的C代码放在import "C"之上。原因是relic_test.h没有提供函数原型,因此cgo无法通过C.foo()这样的调用识别函数。因此,我将所有的test_pc.c代码放在以下格式中:

package main
// #include<>
/*
void test1(){}
void test2(){}
.
.
void testN(){}
*/
import "C"
func main(){
  C.test1()
  C.testN()
}

问题

当我尝试从Go中运行它们时,执行会“卡住”(即代码正在执行,但它只是简单地不继续执行下一条指令)在一个特定的测试中。我如何找出代码为什么会卡住?我如何观察程序的执行流程?

我尝试过的事情

再次说明,如果我直接使用C(即使用GCC编译并运行它),代码是可以工作的。因此,GDB也可以工作。
我还尝试使用go tools cgo -debug-gcc,但这只会打印预处理指令。go tools cgo -gccgo没有输出任何内容。最后,Go调试器Delve无法调试C调用。

英文:

Background

I'm currently in the process of writing a Go interface for this library.

As a first step, I'm trying to run the tests in /test/test_pc.c by calling them through Go wrappers. It is important to note that the tests are successful when compiled by GCC.

Important Additional Details: It is worth mentioning that currently I have all the C code that I want to run above import &quot;C&quot;. The reason for this is that the relic_test.h doesn't provide function prototypes, thus cgo does not recognize the functions through calls like C.foo(). Therefore, I put all the test_pc.c code in the following format:

<!-- language: go -->

package main
// #include&lt;&gt;
/*
void test1(){}
void test2(){}
.
.
void testN(){}
*/
import &quot;C&quot;
func main(){
  C.test1()
  C.testN()
}

Problem

Whenn I try running them from go, the execution gets "stuck" (i.e. the code is in execution but it just simply doesn't go to the next instruction) in one particular test.
How can I find out why the code gets stuck? How can I observe the execution flow of the program?

Thing I've Tried

Again, if I try using C directly (i.e. if I compile it with GCC and run it), the code works. Thus, GDB will also work.
I've also tried using go tools cgo -debug-gcc, but this only prints preprocessing directives. go tools cgo -gccgo doesn't output anything. finally, the Go debugger, Delve, is not able to debug the C calls.

答案1

得分: 3

一些调查显示,你唯一真正的选择如下:

  1. GDB。Go程序应该可以很好地与GDB配合使用,但是GDB和cgo可能不兼容。我认为这在*NIX系统上应该可以工作,但Windows可能会有些问题。你可以尝试一下,看看会怎样?
  2. 大量使用printf或其他日志记录方法。

这两个选项都不是很好...

这份文档中有关于cgo调试的部分似乎表明它可以正常工作。

英文:

Some investigation shows your only real options are as follows:

  1. GDB. Go programs should work with GDB just fine, but GDB and cgo may not get along. I think this will work on *NIX systems, but Windows may or may not cooperate. Try it and see?
  2. Liberal application of good ol printf or other logging.

Neither of these options is very good...

This document has a section on cgo debugging seems to indicate that it works ok.

huangapple
  • 本文由 发表于 2017年7月26日 21:27:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/45328414.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定