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 // Canonicalize function name and skip callers of this function 36 // for predictable example output. 37 // You probably don't need this in your own code. 38 function := strings.ReplaceAll(frame.Function, "main.main", "runtime_test.ExampleFrames") 39 fmt.Printf("- more:%v | %s\n", more, function) 40 if function == "runtime_test.ExampleFrames" { 41 break 42 } 43 44 // Check whether there are more frames to process after this one. 45 if !more { 46 break 47 } 48 } 49 } 50 51 b := func() { c() } 52 a := func() { b() } 53 54 a() 55 // Output: 56 // - more:true | runtime.Callers 57 // - more:true | runtime_test.ExampleFrames.func1 58 // - more:true | runtime_test.ExampleFrames.func2 59 // - more:true | runtime_test.ExampleFrames.func3 60 // - more:true | runtime_test.ExampleFrames 61 } 62