1

I am using Golang and cgo. When my C code raises an assert(), I am unable to see the stack trace of the C code when using cgo.

Instead, I see the stack trace of the golang runtime that caught the assert.

Here is an example of my C code

#include <stdio.h>
#include <pthread.h>
#include <assert.h>
#include <string.h>

void fn2(char *arg)
{
    int stackvar2 = 256;

    printf("Argument %s\n", arg);

    assert(1 == 2);
}

void fn1(int arg)
{
    int stackvar3 = 512;
    char var[256];

    strcpy(var, "deadbeef");

    fn2(var);
}


void *thread(void *arg)
{
    printf("Hello from the thread... going in for an assert\n");

    fn1(1092);

    return NULL;
}

void hello_world(char *str)
{
    pthread_t tid;

    printf("Hello World from C and here the str is from Go: %s\n", str);

    pthread_create(&tid, NULL, thread, NULL);

    sleep(100000);
}


Here is my Go code



package main

/*
extern void hello_world(char *str);
#cgo LDFLAGS: -L. -lhello
#cgo CFLAGS: -g3
*/
import "C"

import (
    _ "fmt"
)

func main() {
    str := "From Golang"

    cStr := C.CString(str)
    C.hello_world(cStr)

    select {}
}

And here is my Makefile

all:
    gcc -g3 -O0 -c hello.c
    ar cru libhello.a hello.o
    go build hello.go

clean:
    rm -f *.o hello
steve landiss
  • 1,833
  • 3
  • 19
  • 30

2 Answers2

2

Besides the obvious check of ulimit -c, run the go program with GOTRACEBACK=crash. This will print out more information, and allow the program to exit with SIGABRT to trigger a core dump.

JimB
  • 104,193
  • 13
  • 262
  • 255
  • Actually I need to add this to my go code: signal.Ignore(syscall.SIGABRT). This allows me to see the stack trace of the C code that crashed. – steve landiss Feb 05 '16 at 22:11
  • @stevelandiss: but doesn't `GOTRACEBACK=crash` let you do that without altering your code at all? – JimB Feb 05 '16 at 22:14
1

Actually I need to add this to my go code: signal.Ignore(syscall.SIGABRT). This allows me to see the stack trace of the C code that crashed.

steve landiss
  • 1,833
  • 3
  • 19
  • 30