gitlab.com/Raven-IO/raven-delve@v1.22.4/pkg/proc/native/threads_windows_amd64.go (about)

     1  package native
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"unsafe"
     7  
     8  	"gitlab.com/Raven-IO/raven-delve/pkg/proc"
     9  	"gitlab.com/Raven-IO/raven-delve/pkg/proc/amd64util"
    10  	"gitlab.com/Raven-IO/raven-delve/pkg/proc/winutil"
    11  )
    12  
    13  func newContext() *winutil.AMD64CONTEXT {
    14  	return winutil.NewAMD64CONTEXT()
    15  }
    16  
    17  func registers(t *nativeThread) (proc.Registers, error) {
    18  	context := newContext()
    19  
    20  	context.SetFlags(_CONTEXT_ALL)
    21  	err := t.getContext(context)
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  
    26  	var threadInfo _THREAD_BASIC_INFORMATION
    27  	status := _NtQueryInformationThread(t.os.hThread, _ThreadBasicInformation, uintptr(unsafe.Pointer(&threadInfo)), uint32(unsafe.Sizeof(threadInfo)), nil)
    28  	if !_NT_SUCCESS(status) {
    29  		return nil, fmt.Errorf("NtQueryInformationThread failed: it returns 0x%x", status)
    30  	}
    31  
    32  	return winutil.NewAMD64Registers(context, uint64(threadInfo.TebBaseAddress)), nil
    33  }
    34  
    35  func (t *nativeThread) setContext(context *winutil.AMD64CONTEXT) error {
    36  	return _SetThreadContext(t.os.hThread, context)
    37  }
    38  
    39  func (t *nativeThread) getContext(context *winutil.AMD64CONTEXT) error {
    40  	return _GetThreadContext(t.os.hThread, context)
    41  }
    42  
    43  func (t *nativeThread) restoreRegisters(savedRegs proc.Registers) error {
    44  	return t.setContext(savedRegs.(*winutil.AMD64Registers).Context)
    45  }
    46  
    47  func (t *nativeThread) withDebugRegisters(f func(*amd64util.DebugRegisters) error) error {
    48  	if !enableHardwareBreakpoints {
    49  		return errors.New("hardware breakpoints not supported")
    50  	}
    51  
    52  	context := winutil.NewAMD64CONTEXT()
    53  	context.ContextFlags = _CONTEXT_DEBUG_REGISTERS
    54  
    55  	err := t.getContext(context)
    56  	if err != nil {
    57  		return err
    58  	}
    59  
    60  	drs := amd64util.NewDebugRegisters(&context.Dr0, &context.Dr1, &context.Dr2, &context.Dr3, &context.Dr6, &context.Dr7)
    61  
    62  	err = f(drs)
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	if drs.Dirty {
    68  		return t.setContext(context)
    69  	}
    70  
    71  	return nil
    72  }