GDB无法调试包含cgo代码的Go程序。

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

GDB can't debug the go program within cgo code

问题

#example files

src/test.go

package main
import (
  . "clib"
)
func main() {
  a := "123";
  b := "456";
  c := "789";
  println(a,b,c);
  Output("ABC");
}

src/clib/clib.h

#ifndef CLIB
void output(char* str);
#endif

src/clib/clib.c

#include "clib.h"
#include <stdio.h>
void output(char* str)
{
	printf("%s\n", str);
}

src/clib/clib.go

package clib
/*
#cgo CFLAGS:-g
#include "clib.h"
*/
import "C"
func Output(s string) {
  p := C.CString(s);
  C.output(p);
}

#exec code

go build -gcflags "-N -l" test.go
gdb ./test
b 10
r
info locals  // <- every variable's value is wrong!

Who can help me solve this problem, thank you very much.

My Environment:

  • ubuntu 11.04 i386
  • gdb 7.6
  • go 1.1
英文:

#example files

src/test.go

package main
import (
  . &quot;clib&quot;
)
func main() {
  a := &quot;123&quot;;
  b := &quot;456&quot;;
  c := &quot;789&quot;;
  println(a,b,c);
  Output(&quot;ABC&quot;);
}

src/clib/clib.h

#ifndef CLIB
void output(char* str);
#endif

src/clib/clib.c

#include &quot;clib.h&quot;
#include &lt;stdio.h&gt;
void output(char* str)
{
	printf(&quot;%s\n&quot;, str);
}

src/clib/clib.go

package clib
/*
#cgo CFLAGS:-g
#include &quot;clib.h&quot;
*/
import &quot;C&quot;
func Output(s string) {
  p := C.CString(s);
  C.output(p);
}

#exec code

go build -gcflags &quot;-N -l&quot; test.go
gdb ./test
b 10
r
info locals  // &lt;- every variable&#39;s value is wrong!

Who can help me solve this problem, thank you very much.

My Environment:

  • ubuntu 11.04 i386
  • gdb 7.6
  • go 1.1

答案1

得分: 4

目前有一个关于这个问题的未解决bug:https://code.google.com/p/go/issues/detail?id=5221

在1.0版本中,使用gdb调试cgo是可行的,但在1.1版本中目前存在问题。正在进行修复。

英文:

There is currently an open bug regarding this: https://code.google.com/p/go/issues/detail?id=5221

Debugging cgo with gdb worked in 1.0 but is currently broken in 1.1. It's being worked on.

huangapple
  • 本文由 发表于 2013年6月17日 21:09:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/17148279.html
匿名

发表评论

匿名网友

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

确定