Source file src/runtime/testdata/testprog/setcgotraceback.go

     1  // Copyright 2025 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  import (
     8  	"fmt"
     9  	"internal/abi"
    10  	"runtime"
    11  	"unsafe"
    12  )
    13  
    14  func init() {
    15  	register("SetCgoTracebackNoCgo", SetCgoTracebackNoCgo)
    16  }
    17  
    18  func cgoTraceback() {
    19  	panic("unexpectedly reached cgo traceback function")
    20  }
    21  
    22  func cgoContext() {
    23  	panic("unexpectedly reached cgo context function")
    24  }
    25  
    26  func cgoSymbolizer() {
    27  	panic("unexpectedly reached cgo symbolizer function")
    28  }
    29  
    30  // SetCgoTraceback is a no-op in non-cgo binaries.
    31  func SetCgoTracebackNoCgo() {
    32  	traceback := unsafe.Pointer(abi.FuncPCABIInternal(cgoTraceback))
    33  	context := unsafe.Pointer(abi.FuncPCABIInternal(cgoContext))
    34  	symbolizer := unsafe.Pointer(abi.FuncPCABIInternal(cgoSymbolizer))
    35  	runtime.SetCgoTraceback(0, traceback, context, symbolizer)
    36  
    37  	// In a cgo binary, runtime.(*Frames).Next calls the cgo symbolizer for
    38  	// any non-Go frames. Pass in a bogus frame to verify that Next does
    39  	// not attempt to call the cgo symbolizer, which would crash in a
    40  	// non-cgo binary like this one.
    41  	frames := runtime.CallersFrames([]uintptr{0x12345678})
    42  	frames.Next()
    43  
    44  	fmt.Println("OK")
    45  }
    46  

View as plain text