简体   繁体   English

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

[英]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: 一些调查显示您唯一的实际选择如下:

  1. GDB. GDB。 Go programs should work with GDB just fine, but GDB and cgo may not get along. Go程序应该可以正常使用GDB,但是GDB和cgo可能不会相处。 I think this will work on *NIX systems, but Windows may or may not cooperate. 我认为这将适用于* NIX系统,但Windows可能会也可能不会合作。 Try it and see? 试试吧,看看?
  2. Liberal application of good ol printf or other logging. 自由应用好的ol 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.

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