delve icon indicating copy to clipboard operation
delve copied to clipboard

Better cgo support

Open aarzilli opened this issue 8 years ago • 4 comments
trafficstars

  • [ ] When the process stops inside a cgo call correctly identify the goroutine that did the cgo call
  • [ ] Stitch together cgo stack and go stack, give the user the impression that it's all one stack since the separation is an implementation detail of the go runtime
  • [ ] next, step and stepout should all work on C code, even if multiple threads are executing the same C function (i.e. TestNextConcurrent but for cgo).
  • [ ] optimized C code puts function arguments inside registers, support more complex location expressions
  • [ ] the same name can be used by multiple C functions in different files by using the 'static' modifier, we should disambiguate those

aarzilli avatar Jul 31 '17 15:07 aarzilli

What's the status of this issue? Can we see C stack when calling C functions from Go with cgo?

I had a try, it doesn't seem to work for me. Can anyone shed any light on this?

I wrote a simple c function with an assert, then try to call the function from go code.

dlv core ./testcgo /var/local/dumps/core.testcgo.1505

Type 'help' for list of commands. (dlv) gr 1 Switched from 0 to 1 (thread 1505) (dlv) bt 0 0x000000000045467d in runtime.usleep at /usr/local/go/src/runtime/sys_linux_amd64.s:131 1 0x0000000000452512 in runtime.asmcgocall at /usr/local/go/src/runtime/asm_amd64.s:620 2 0x000000000040428f in runtime.cgocall at /usr/local/go/src/runtime/cgocall.go:131 3 0x000000000048912d in main._Cfunc_greet at _cgo_gotypes.go:88 4 0x000000000048933d in main.main at ./main.go:21 5 0x000000000042b29c in runtime.main at /usr/local/go/src/runtime/proc.go:200 6 0x0000000000452dc1 in runtime.goexit at /usr/local/go/src/runtime/asm_amd64.s:1337 (dlv) frame 3

runtime.usleep() /usr/local/go/src/runtime/sys_linux_amd64.s:131 (PC: 0x45467d) Warning: debugging optimized function Frame 3: _cgo_gotypes.go:88 (PC: 48912d) (dlv) up runtime.usleep() /usr/local/go/src/runtime/sys_linux_amd64.s:131 (PC: 0x45467d) Warning: debugging optimized function Frame 4: ./main.go:21 (PC: 48933d) 16: year := C.int(2018) 17: 18: ptr := C.malloc(C.sizeof_char * 1024) 19: defer C.free(unsafe.Pointer(ptr)) 20: => 21: size := C.greet(name, year, (*C.char)(ptr)) 22: 23: b := C.GoBytes(ptr, size) 24: fmt.Println(string(b)) 25: } (dlv)

The best I can see is the main._Cfunc_greet, but I can't tell from the core dump why the application crashed.

GuoAlbert avatar Jul 12 '19 03:07 GuoAlbert

As far as I know this happens when optimizations are enabled.

aarzilli avatar Jul 15 '19 14:07 aarzilli

Thanks Alessandro! However, I've already disabled the optimization:

I used below CFLAGS in go file: // #cgo CFLAGS: -g -Wall -O0

I used below gcflags when building the test program. -gcflags "-N -l"

Software versions in my test:

  • Linux(Fedora): 4.18.19-100.fc27.x86_64
  • go: version go1.12.7
  • dlv: Version: 1.2.0 Build: 86ed5b66a11dc0dbd917217d1b0f79a43c61fdce

Would you let me know your Linux distribution and software versions in your test?

Thanks!

GuoAlbert avatar Jul 17 '19 09:07 GuoAlbert

@GuoAlbert: I used below gcflags when building the test program. -gcflags "-N -l"

Go 1.10 changed the semantics of these flags to only propagate the flags to the matching package. You need to use -gcflags "all=-N -l" (source).

briandealwis avatar Oct 06 '20 19:10 briandealwis