dp518158 2017-07-26 13:27
浏览 280

如何调试用cgo调用的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 "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(). Therefore, I put all the test_pc.c code in the following format:

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" (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条回答 默认 最新

  • douzhi1924 2017-07-26 16:30
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题