github.com/searKing/golang/go@v1.2.74/runtime/example_test.go (about)

     1  // Copyright 2020 The searKing Author. 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 runtime_test
     6  
     7  import (
     8  	"fmt"
     9  	"unsafe"
    10  
    11  	"github.com/searKing/golang/go/runtime"
    12  )
    13  
    14  func ExampleGetCaller() {
    15  	caller := runtime.GetCaller(1)
    16  	fmt.Print(caller)
    17  
    18  	// Output:
    19  	// github.com/searKing/golang/go/runtime_test.ExampleGetCaller
    20  }
    21  
    22  func ExampleGetShortCallerFuncFileLine() {
    23  	caller, file, line := runtime.GetShortCallerFuncFileLine(1)
    24  	fmt.Printf("%s() %s:%d", caller, file, line)
    25  
    26  	// Output:
    27  	// ExampleGetShortCallerFuncFileLine() run_example.go:63
    28  }
    29  
    30  //go:nosplit
    31  func cf() (got, expect uintptr) {
    32  	var spv = 99
    33  	var sp *int
    34  	sp = (*int)(unsafe.Pointer(runtime.GetEIP(unsafe.Sizeof(uintptr(0)))))
    35  	//sp = (*int)(unsafe.Pointer(runtime.GetEIP(0)))
    36  	*sp = 0xFFFFFFF
    37  
    38  	return uintptr(unsafe.Pointer(sp)), uintptr(unsafe.Pointer(&spv))
    39  }
    40  func ExampleGetCallFrame() {
    41  	got, expect := cf()
    42  	if got != expect {
    43  		fmt.Printf("got = %#x\n", got)
    44  		fmt.Printf("expect = %#x\n", expect)
    45  	}
    46  	fmt.Printf("%t", got == expect)
    47  
    48  	// Output:
    49  	// true
    50  
    51  }