[英]How can I debug C code called with cgo?
Background 背景
I'm currently in the process of writing a Go interface for this library . 我目前正在为这个库编写Go接口。
As a first step, I'm trying to run the tests in /test/test_pc.c
by calling them through Go wrappers. 作为第一步,我试图通过Go包装器调用它们来在
/test/test_pc.c
运行测试。 It is important to note that the tests are successful when compiled by GCC. 值得注意的是,GCC编译时测试成功。
Important Additional Details : It is worth mentioning that currently I have all the C code that I want to run above import "C"
. 重要的附加细节 :值得一提的是,目前我想要在
import "C"
之上运行所有C代码。 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()
. 原因是
relic_test.h
不提供函数原型,因此cgo不能通过像C.foo()
类的调用来识别函数。 Therefore, I put all the test_pc.c
code in the following format: 因此,我将所有
test_pc.c
代码放在以下格式中:
package main
// #include<>
/*
void test1(){}
void test2(){}
.
.
void testN(){}
*/
import "C"
func main(){
C.test1()
C.testN()
}
Problem 问题
Whenn I try running them from go, the execution gets "stuck" (ie the code is in execution but it just simply doesn't go to the next instruction) in one particular test. 当我尝试从go运行它们时,执行在一个特定的测试中被“卡住”(即代码正在执行但它只是不会转到下一条指令)。 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 (ie if I compile it with GCC and run it), the code works. 再次,如果我尝试直接使用C(即如果我使用GCC编译并运行它),则代码可以正常工作。 Thus, GDB will also work.
因此,GDB也将起作用。 I've also tried using
go tools cgo -debug-gcc
, but this only prints preprocessing directives. 我也尝试使用
go tools cgo -debug-gcc
,但这只打印预处理指令。 go tools cgo -gccgo
doesn't output anything. go tools cgo -gccgo
不输出任何东西。 finally, the Go debugger, Delve, is not able to debug the C calls. 最后,Go调试器Delve无法调试C调用。
Some investigation shows your only real options are as follows: 一些调查显示您唯一的实际选择如下:
printf
or other logging. printf
或其他日志记录。 Neither of these options is very good... 这些选项都不是很好......
This document has a section on cgo debugging seems to indicate that it works ok. 这个文件有一个关于cgo调试的部分似乎表明它工作正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.