github.com/miolini/go@v0.0.0-20160405192216-fca68c8cb408/src/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  package main
     6  
     7  // This program will crash.
     8  // We want the stack trace to include the C functions.
     9  // We use a fake traceback, and a symbolizer that dumps a string we recognize.
    10  
    11  /*
    12  #cgo CFLAGS: -g -O0
    13  
    14  #include <stdint.h>
    15  
    16  char *p;
    17  
    18  static int f3() {
    19  	*p = 0;
    20  	return 0;
    21  }
    22  
    23  static int f2() {
    24  	return f3();
    25  }
    26  
    27  static int f1() {
    28  	return f2();
    29  }
    30  
    31  struct cgoTracebackArg {
    32  	uintptr_t  context;
    33  	uintptr_t* buf;
    34  	uintptr_t  max;
    35  };
    36  
    37  struct cgoSymbolizerArg {
    38  	uintptr_t   pc;
    39  	const char* file;
    40  	uintptr_t   lineno;
    41  	const char* func;
    42  	uintptr_t   entry;
    43  	uintptr_t   more;
    44  	uintptr_t   data;
    45  };
    46  
    47  void cgoTraceback(void* parg) {
    48  	struct cgoTracebackArg* arg = (struct cgoTracebackArg*)(parg);
    49  	arg->buf[0] = 1;
    50  	arg->buf[1] = 2;
    51  	arg->buf[2] = 3;
    52  	arg->buf[3] = 0;
    53  }
    54  
    55  void cgoSymbolizer(void* parg) {
    56  	struct cgoSymbolizerArg* arg = (struct cgoSymbolizerArg*)(parg);
    57  	if (arg->pc != arg->data + 1) {
    58  		arg->file = "unexpected data";
    59  	} else {
    60  		arg->file = "cgo symbolizer";
    61  	}
    62  	arg->lineno = arg->data + 1;
    63  	arg->data++;
    64  }
    65  */
    66  import "C"
    67  
    68  import (
    69  	"runtime"
    70  	"unsafe"
    71  )
    72  
    73  func init() {
    74  	register("CrashTraceback", CrashTraceback)
    75  }
    76  
    77  func CrashTraceback() {
    78  	runtime.SetCgoTraceback(0, unsafe.Pointer(C.cgoTraceback), nil, unsafe.Pointer(C.cgoSymbolizer))
    79  	C.f1()
    80  }