github.com/guyezi/gofrontend@v0.0.0-20200228202240-7a62a49e62c0/libgo/go/runtime/testdata/testprogcgo/traceback.go (about)

     1  // Copyright 2016 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build !gccgo
     6  
     7  package main
     8  
     9  // This program will crash.
    10  // We want the stack trace to include the C functions.
    11  // We use a fake traceback, and a symbolizer that dumps a string we recognize.
    12  
    13  /*
    14  #cgo CFLAGS: -g -O0
    15  
    16  #include <stdint.h>
    17  
    18  char *p;
    19  
    20  static int f3(void) {
    21  	*p = 0;
    22  	return 0;
    23  }
    24  
    25  static int f2(void) {
    26  	return f3();
    27  }
    28  
    29  static int f1(void) {
    30  	return f2();
    31  }
    32  
    33  struct cgoTracebackArg {
    34  	uintptr_t  context;
    35  	uintptr_t  sigContext;
    36  	uintptr_t* buf;
    37  	uintptr_t  max;
    38  };
    39  
    40  struct cgoSymbolizerArg {
    41  	uintptr_t   pc;
    42  	const char* file;
    43  	uintptr_t   lineno;
    44  	const char* func;
    45  	uintptr_t   entry;
    46  	uintptr_t   more;
    47  	uintptr_t   data;
    48  };
    49  
    50  void cgoTraceback(void* parg) {
    51  	struct cgoTracebackArg* arg = (struct cgoTracebackArg*)(parg);
    52  	arg->buf[0] = 1;
    53  	arg->buf[1] = 2;
    54  	arg->buf[2] = 3;
    55  	arg->buf[3] = 0;
    56  }
    57  
    58  void cgoSymbolizer(void* parg) {
    59  	struct cgoSymbolizerArg* arg = (struct cgoSymbolizerArg*)(parg);
    60  	if (arg->pc != arg->data + 1) {
    61  		arg->file = "unexpected data";
    62  	} else {
    63  		arg->file = "cgo symbolizer";
    64  	}
    65  	arg->lineno = arg->data + 1;
    66  	arg->data++;
    67  }
    68  */
    69  import "C"
    70  
    71  import (
    72  	"runtime"
    73  	"unsafe"
    74  )
    75  
    76  func init() {
    77  	register("CrashTraceback", CrashTraceback)
    78  }
    79  
    80  func CrashTraceback() {
    81  	runtime.SetCgoTraceback(0, unsafe.Pointer(C.cgoTraceback), nil, unsafe.Pointer(C.cgoSymbolizer))
    82  	C.f1()
    83  }