github.com/undoio/delve@v1.9.0/pkg/proc/threads.go (about)

     1  package proc
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/undoio/delve/pkg/dwarf/op"
     7  )
     8  
     9  // Thread represents a thread.
    10  type Thread interface {
    11  	Location() (*Location, error)
    12  	// Breakpoint will return the breakpoint that this thread is stopped at or
    13  	// nil if the thread is not stopped at any breakpoint.
    14  	Breakpoint() *BreakpointState
    15  	ThreadID() int
    16  
    17  	// Registers returns the CPU registers of this thread. The contents of the
    18  	// variable returned may or may not change to reflect the new CPU status
    19  	// when the thread is resumed or the registers are changed by calling
    20  	// SetPC/SetSP/etc.
    21  	// To insure that the the returned variable won't change call the Copy
    22  	// method of Registers.
    23  	Registers() (Registers, error)
    24  
    25  	// RestoreRegisters restores saved registers
    26  	RestoreRegisters(Registers) error
    27  	BinInfo() *BinaryInfo
    28  	// ProcessMemory returns the process memory.
    29  	ProcessMemory() MemoryReadWriter
    30  	StepInstruction() error
    31  	// SetCurrentBreakpoint updates the current breakpoint of this thread, if adjustPC is true also checks for breakpoints that were just hit (this should only be passed true after a thread resume)
    32  	SetCurrentBreakpoint(adjustPC bool) error
    33  	// SoftExc returns true if this thread received a software exception during the last resume.
    34  	SoftExc() bool
    35  	// Common returns the CommonThread structure for this thread
    36  	Common() *CommonThread
    37  
    38  	// SetReg changes the value of the specified register. A minimal
    39  	// implementation of this interface can support just setting the PC
    40  	// register.
    41  	SetReg(uint64, *op.DwarfRegister) error
    42  }
    43  
    44  // Location represents the location of a thread.
    45  // Holds information on the current instruction
    46  // address, the source file:line, and the function.
    47  type Location struct {
    48  	PC   uint64
    49  	File string
    50  	Line int
    51  	Fn   *Function
    52  }
    53  
    54  // CommonThread contains fields used by this package, common to all
    55  // implementations of the Thread interface.
    56  type CommonThread struct {
    57  	CallReturn   bool // returnValues are the return values of a call injection
    58  	returnValues []*Variable
    59  	g            *G // cached g for this thread
    60  }
    61  
    62  // ReturnValues reads the return values from the function executing on
    63  // this thread using the provided LoadConfig.
    64  func (t *CommonThread) ReturnValues(cfg LoadConfig) []*Variable {
    65  	loadValues(t.returnValues, cfg)
    66  	return t.returnValues
    67  }
    68  
    69  // topframe returns the two topmost frames of g, or thread if g is nil.
    70  func topframe(g *G, thread Thread) (Stackframe, Stackframe, error) {
    71  	var frames []Stackframe
    72  	var err error
    73  
    74  	if g == nil {
    75  		frames, err = ThreadStacktrace(thread, 1)
    76  	} else {
    77  		frames, err = g.Stacktrace(1, StacktraceReadDefers)
    78  	}
    79  	if err != nil {
    80  		return Stackframe{}, Stackframe{}, err
    81  	}
    82  	switch len(frames) {
    83  	case 0:
    84  		return Stackframe{}, Stackframe{}, errors.New("empty stack trace")
    85  	case 1:
    86  		return frames[0], Stackframe{}, nil
    87  	default:
    88  		return frames[0], frames[1], nil
    89  	}
    90  }
    91  
    92  func setPC(thread Thread, newPC uint64) error {
    93  	return thread.SetReg(thread.BinInfo().Arch.PCRegNum, op.DwarfRegisterFromUint64(newPC))
    94  }
    95  
    96  func setSP(thread Thread, newSP uint64) error {
    97  	return thread.SetReg(thread.BinInfo().Arch.SPRegNum, op.DwarfRegisterFromUint64(newSP))
    98  }
    99  
   100  func setClosureReg(thread Thread, newClosureReg uint64) error {
   101  	return thread.SetReg(thread.BinInfo().Arch.ContextRegNum, op.DwarfRegisterFromUint64(newClosureReg))
   102  }
   103  
   104  func setLR(thread Thread, newLR uint64) error {
   105  	return thread.SetReg(thread.BinInfo().Arch.LRRegNum, op.DwarfRegisterFromUint64(newLR))
   106  }