github.com/haraldrudell/parl@v0.4.176/stack-parl.go (about)

     1  /*
     2  © 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  // Package pdebug provides a portable, parsed stack trace.
     7  package parl
     8  
     9  import (
    10  	"github.com/haraldrudell/parl/pruntime"
    11  )
    12  
    13  // - Go stack traces are created by [runtime.NewStack] and is a byte slice
    14  type stackParl struct{ pruntime.Stack }
    15  
    16  // [pdebug.Stack] implements [parl.Stack]
    17  var _ Stack = &stackParl{}
    18  
    19  // NewStack populates a Stack object with the current thread
    20  // and its stack using debug.Stack
    21  func newStack(skipFrames int) (stack Stack) {
    22  	if skipFrames < 0 {
    23  		skipFrames = 0
    24  	}
    25  	// count [pdebug.NewStack]
    26  	skipFrames++
    27  
    28  	// result of parsing to be returned
    29  	stack = &stackParl{Stack: pruntime.NewStack(skipFrames)}
    30  
    31  	return
    32  }
    33  
    34  // thread ID 1… for the thread requesting the stack trace
    35  //   - ThreadID is comparable and has IsValid and String methods
    36  //   - ThreadID is typically an incremented 64-bit integer with
    37  //     main thread having ID 1
    38  func (s *stackParl) ID() (threadID ThreadID) {
    39  	threadID = ThreadID(s.Stack.(*pruntime.StackR).ThreadID)
    40  	return
    41  }
    42  
    43  // a word indicating thread status, typically word “running”
    44  func (s *stackParl) Status() (status ThreadStatus) {
    45  	status = ThreadStatus(s.Stack.(*pruntime.StackR).Status)
    46  	return
    47  }
    48  
    49  // the code location of the go statement creating this thread
    50  //   - if IsMain is true, zero-value. Check with Creator().IsSet()
    51  //   - never nil
    52  func (s *stackParl) Creator() (creator *pruntime.CodeLocation, creatorID ThreadID, goRoutineRef string) {
    53  	var stackR = s.Stack.(*pruntime.StackR)
    54  	var c = stackR.Creator
    55  	creator = &c
    56  	creatorID = ThreadID(stackR.CreatorID)
    57  	goRoutineRef = stackR.GoroutineRef
    58  	return
    59  }