github.com/newrelic/go-agent@v3.26.0+incompatible/internal/stack_frame_pre_1_7.go (about)

     1  // Copyright 2020 New Relic Corporation. All rights reserved.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  // +build !go1.7
     5  
     6  package internal
     7  
     8  import "runtime"
     9  
    10  func (st StackTrace) frames() []stacktraceFrame {
    11  	fs := make([]stacktraceFrame, len(st))
    12  	for idx, pc := range st {
    13  		fs[idx] = lookupFrame(pc)
    14  	}
    15  	return fs
    16  }
    17  
    18  func lookupFrame(pc uintptr) stacktraceFrame {
    19  	// The Golang runtime package documentation says "To look up the file
    20  	// and line number of the call itself, use pc[i]-1. As an exception to
    21  	// this rule, if pc[i-1] corresponds to the function runtime.sigpanic,
    22  	// then pc[i] is the program counter of a faulting instruction and
    23  	// should be used without any subtraction."
    24  	//
    25  	// TODO: Fully understand when this subtraction is necessary.
    26  	place := pc - 1
    27  	f := runtime.FuncForPC(place)
    28  	if nil == f {
    29  		return stacktraceFrame{}
    30  	}
    31  	file, line := f.FileLine(place)
    32  	return stacktraceFrame{
    33  		Name: f.Name(),
    34  		File: file,
    35  		Line: int64(line),
    36  	}
    37  }