1

Golang と cgo を使用しています。C コードで が発生するassert()と、 を使用しているときに C コードのスタック トレースが表示されませんcgo

代わりに、アサートをキャッチした golang ランタイムのスタック トレースが表示されます。

これが私のCコードの例です

#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 {}
}

そして、ここに私のMakefileがあります

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

clean:
    rm -f *.o hello
4

2 に答える 2

2

の明らかなチェックに加えてulimit -c、 go プログラムを で実行しGOTRACEBACK=crashます。これにより、より多くの情報が出力され、プログラムを終了しSIGABRTてコア ダンプをトリガーできるようになります。

于 2016-02-05T21:40:00.143 に答える
0

実際には、これを go コードに追加する必要があります: signal.Ignore(syscall.SIGABRT)。これにより、クラッシュした C コードのスタック トレースを確認できます。

于 2016-02-05T22:12:11.013 に答える