github.com/cnboonhan/delve@v0.0.0-20230908061759-363f2388c2fb/pkg/proc/interface.go (about)

     1  package proc
     2  
     3  import (
     4  	"sync"
     5  	"time"
     6  
     7  	"github.com/go-delve/delve/pkg/elfwriter"
     8  	"github.com/go-delve/delve/pkg/proc/internal/ebpf"
     9  )
    10  
    11  // ProcessGroup is a group of processes that are resumed at the same time.
    12  type ProcessGroup interface {
    13  	ContinueOnce(*ContinueOnceContext) (Thread, StopReason, error)
    14  	Detach(int, bool) error
    15  }
    16  
    17  // Process represents the target of the debugger. This
    18  // target could be a system process, core file, etc.
    19  //
    20  // Implementations of Process are not required to be thread safe and users
    21  // of Process should not assume they are.
    22  // There is one exception to this rule: it is safe to call RequestManualStop
    23  // concurrently with ContinueOnce.
    24  type Process interface {
    25  	BinInfo() *BinaryInfo
    26  	EntryPoint() (uint64, error)
    27  
    28  	FindThread(threadID int) (Thread, bool)
    29  	ThreadList() []Thread
    30  
    31  	Breakpoints() *BreakpointMap
    32  
    33  	// Memory returns a memory read/writer for this process's memory.
    34  	Memory() MemoryReadWriter
    35  }
    36  
    37  // ProcessInternal holds a set of methods that need to be implemented by a
    38  // Delve backend. Methods in the Process interface are safe to be called by
    39  // clients of the 'proc' library, while all other methods are only called
    40  // directly within 'proc'.
    41  type ProcessInternal interface {
    42  	Process
    43  	// Valid returns true if this Process can be used. When it returns false it
    44  	// also returns an error describing why the Process is invalid (either
    45  	// ErrProcessExited or ErrProcessDetached).
    46  	Valid() (bool, error)
    47  
    48  	// RequestManualStop attempts to stop all the process' threads.
    49  	RequestManualStop(cctx *ContinueOnceContext) error
    50  
    51  	WriteBreakpoint(*Breakpoint) error
    52  	EraseBreakpoint(*Breakpoint) error
    53  
    54  	SupportsBPF() bool
    55  	SetUProbe(string, int64, []ebpf.UProbeArgMap) error
    56  	GetBufferedTracepoints() []ebpf.RawUProbeParams
    57  
    58  	// DumpProcessNotes returns ELF core notes describing the process and its threads.
    59  	// Implementing this method is optional.
    60  	DumpProcessNotes(notes []elfwriter.Note, threadDone func()) (bool, []elfwriter.Note, error)
    61  	// MemoryMap returns the memory map of the target process. This method must be implemented if CanDump is true.
    62  	MemoryMap() ([]MemoryMapEntry, error)
    63  
    64  	// StartCallInjection notifies the backend that we are about to inject a function call.
    65  	StartCallInjection() (func(), error)
    66  
    67  	// FollowExec enables (or disables) follow exec mode
    68  	FollowExec(bool) error
    69  }
    70  
    71  // RecordingManipulation is an interface for manipulating process recordings.
    72  type RecordingManipulation interface {
    73  	// Recorded returns true if the current process is a recording and the path
    74  	// to the trace directory.
    75  	Recorded() (recorded bool, tracedir string)
    76  	// ChangeDirection changes execution direction.
    77  	ChangeDirection(Direction) error
    78  	// GetDirection returns the current direction of execution.
    79  	GetDirection() Direction
    80  	// When returns current recording position.
    81  	When() (string, error)
    82  	// Checkpoint sets a checkpoint at the current position.
    83  	Checkpoint(where string) (id int, err error)
    84  	// Checkpoints returns the list of currently set checkpoint.
    85  	Checkpoints() ([]Checkpoint, error)
    86  	// ClearCheckpoint removes a checkpoint.
    87  	ClearCheckpoint(id int) error
    88  }
    89  
    90  // RecordingManipulationInternal is an interface that a Delve backend can
    91  // implement if it is a recording.
    92  type RecordingManipulationInternal interface {
    93  	RecordingManipulation
    94  
    95  	// Restart restarts the recording from the specified position, or from the
    96  	// last checkpoint if pos == "".
    97  	// If pos starts with 'c' it's a checkpoint ID, otherwise it's an event
    98  	// number.
    99  	// Returns the new current thread after the restart has completed.
   100  	Restart(cctx *ContinueOnceContext, pos string) (Thread, error)
   101  }
   102  
   103  // Direction is the direction of execution for the target process.
   104  type Direction int8
   105  
   106  const (
   107  	// Forward direction executes the target normally.
   108  	Forward Direction = 0
   109  	// Backward direction executes the target in reverse.
   110  	Backward Direction = 1
   111  )
   112  
   113  // Checkpoint is a checkpoint
   114  type Checkpoint struct {
   115  	ID    int
   116  	When  string
   117  	Where string
   118  }
   119  
   120  // ContinueOnceContext is an object passed to ContinueOnce that the backend
   121  // can use to communicate with the target layer.
   122  type ContinueOnceContext struct {
   123  	ResumeChan chan<- struct{}
   124  	StopMu     sync.Mutex
   125  	// manualStopRequested is set if all the threads in the process were
   126  	// signalled to stop as a result of a Halt API call. Used to disambiguate
   127  	// why a thread is found to have stopped.
   128  	manualStopRequested bool
   129  }
   130  
   131  // CheckAndClearManualStopRequest will check for a manual
   132  // stop and then clear that state.
   133  func (cctx *ContinueOnceContext) CheckAndClearManualStopRequest() bool {
   134  	cctx.StopMu.Lock()
   135  	defer cctx.StopMu.Unlock()
   136  	msr := cctx.manualStopRequested
   137  	cctx.manualStopRequested = false
   138  	return msr
   139  }
   140  
   141  func (cctx *ContinueOnceContext) GetManualStopRequested() bool {
   142  	cctx.StopMu.Lock()
   143  	defer cctx.StopMu.Unlock()
   144  	return cctx.manualStopRequested
   145  }
   146  
   147  // WaitFor is passed to native.Attach and gdbserver.LLDBAttach to wait for a
   148  // process to start before attaching.
   149  type WaitFor struct {
   150  	Name               string
   151  	Interval, Duration time.Duration
   152  }