...
Run Format

Source file src/runtime/testdata/testprogcgo/traceback.go

Documentation: runtime/testdata/testprogcgo

     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(void) {
    19  	*p = 0;
    20  	return 0;
    21  }
    22  
    23  static int f2(void) {
    24  	return f3();
    25  }
    26  
    27  static int f1(void) {
    28  	return f2();
    29  }
    30  
    31  struct cgoTracebackArg {
    32  	uintptr_t  context;
    33  	uintptr_t  sigContext;
    34  	uintptr_t* buf;
    35  	uintptr_t  max;
    36  };
    37  
    38  struct cgoSymbolizerArg {
    39  	uintptr_t   pc;
    40  	const char* file;
    41  	uintptr_t   lineno;
    42  	const char* func;
    43  	uintptr_t   entry;
    44  	uintptr_t   more;
    45  	uintptr_t   data;
    46  };
    47  
    48  void cgoTraceback(void* parg) {
    49  	struct cgoTracebackArg* arg = (struct cgoTracebackArg*)(parg);
    50  	arg->buf[0] = 1;
    51  	arg->buf[1] = 2;
    52  	arg->buf[2] = 3;
    53  	arg->buf[3] = 0;
    54  }
    55  
    56  void cgoSymbolizer(void* parg) {
    57  	struct cgoSymbolizerArg* arg = (struct cgoSymbolizerArg*)(parg);
    58  	if (arg->pc != arg->data + 1) {
    59  		arg->file = "unexpected data";
    60  	} else {
    61  		arg->file = "cgo symbolizer";
    62  	}
    63  	arg->lineno = arg->data + 1;
    64  	arg->data++;
    65  }
    66  */
    67  import "C"
    68  
    69  import (
    70  	"runtime"
    71  	"unsafe"
    72  )
    73  
    74  func init() {
    75  	register("CrashTraceback", CrashTraceback)
    76  }
    77  
    78  func CrashTraceback() {
    79  	runtime.SetCgoTraceback(0, unsafe.Pointer(C.cgoTraceback), nil, unsafe.Pointer(C.cgoSymbolizer))
    80  	C.f1()
    81  }
    82  

View as plain text