Source file src/runtime/example_test.go

     1  // Copyright 2017 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 runtime_test
     6  
     7  import (
     8  	"fmt"
     9  	"runtime"
    10  	"strings"
    11  )
    12  
    13  func ExampleFrames() {
    14  	c := func() {
    15  		// Ask runtime.Callers for up to 10 PCs, including runtime.Callers itself.
    16  		pc := make([]uintptr, 10)
    17  		n := runtime.Callers(0, pc)
    18  		if n == 0 {
    19  			// No PCs available. This can happen if the first argument to
    20  			// runtime.Callers is large.
    21  			//
    22  			// Return now to avoid processing the zero Frame that would
    23  			// otherwise be returned by frames.Next below.
    24  			return
    25  		}
    26  
    27  		pc = pc[:n] // pass only valid pcs to runtime.CallersFrames
    28  		frames := runtime.CallersFrames(pc)
    29  
    30  		// Loop to get frames.
    31  		// A fixed number of PCs can expand to an indefinite number of Frames.
    32  		for {
    33  			frame, more := frames.Next()
    34  
    35  			// Process this frame.
    36  			//
    37  			// To keep this example's output stable
    38  			// even if there are changes in the testing package,
    39  			// stop unwinding when we leave package runtime.
    40  			if !strings.Contains(frame.File, "runtime/") {
    41  				break
    42  			}
    43  			fmt.Printf("- more:%v | %s\n", more, frame.Function)
    44  
    45  			// Check whether there are more frames to process after this one.
    46  			if !more {
    47  				break
    48  			}
    49  		}
    50  	}
    51  
    52  	b := func() { c() }
    53  	a := func() { b() }
    54  
    55  	a()
    56  	// Output:
    57  	// - more:true | runtime.Callers
    58  	// - more:true | runtime_test.ExampleFrames.func1
    59  	// - more:true | runtime_test.ExampleFrames.func2
    60  	// - more:true | runtime_test.ExampleFrames.func3
    61  	// - more:true | runtime_test.ExampleFrames
    62  }
    63  

View as plain text