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