mirror of
https://github.com/go-delve/delve.git
synced 2025-10-30 02:07:58 +08:00
Refactor read/write memory code
This commit is contained in:
@ -43,7 +43,7 @@ func (bp *Breakpoint) Clear(thread *Thread) (*Breakpoint, error) {
|
|||||||
}
|
}
|
||||||
return bp, nil
|
return bp, nil
|
||||||
}
|
}
|
||||||
if _, err := writeMemory(thread, uintptr(bp.Addr), bp.OriginalData); err != nil {
|
if _, err := thread.writeMemory(uintptr(bp.Addr), bp.OriginalData); err != nil {
|
||||||
return nil, fmt.Errorf("could not clear breakpoint %s", err)
|
return nil, fmt.Errorf("could not clear breakpoint %s", err)
|
||||||
}
|
}
|
||||||
return bp, nil
|
return bp, nil
|
||||||
@ -126,8 +126,8 @@ func (dbp *Process) setBreakpoint(tid int, addr uint64, temp bool) (*Breakpoint,
|
|||||||
|
|
||||||
// Fall back to software breakpoint.
|
// Fall back to software breakpoint.
|
||||||
thread := dbp.Threads[tid]
|
thread := dbp.Threads[tid]
|
||||||
originalData := make([]byte, dbp.arch.BreakpointSize())
|
originalData, err := thread.readMemory(uintptr(addr), dbp.arch.BreakpointSize())
|
||||||
if _, err := readMemory(thread, uintptr(addr), originalData); err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err := dbp.writeSoftwareBreakpoint(thread, addr); err != nil {
|
if err := dbp.writeSoftwareBreakpoint(thread, addr); err != nil {
|
||||||
@ -140,7 +140,7 @@ func (dbp *Process) setBreakpoint(tid int, addr uint64, temp bool) (*Breakpoint,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (dbp *Process) writeSoftwareBreakpoint(thread *Thread, addr uint64) error {
|
func (dbp *Process) writeSoftwareBreakpoint(thread *Thread, addr uint64) error {
|
||||||
_, err := writeMemory(thread, uintptr(addr), dbp.arch.BreakpointInstruction())
|
_, err := thread.writeMemory(uintptr(addr), dbp.arch.BreakpointInstruction())
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -50,13 +50,7 @@ func getRegisters(p *Process, t *testing.T) Registers {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func dataAtAddr(thread *Thread, addr uint64) ([]byte, error) {
|
func dataAtAddr(thread *Thread, addr uint64) ([]byte, error) {
|
||||||
data := make([]byte, 1)
|
return thread.readMemory(uintptr(addr), 1)
|
||||||
_, err := readMemory(thread, uintptr(addr), data)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return data, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func assertNoError(err error, t *testing.T, s string) {
|
func assertNoError(err error, t *testing.T, s string) {
|
||||||
@ -413,9 +407,10 @@ func TestFindReturnAddress(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
addr := uint64(int64(regs.SP()) + ret)
|
addr := uint64(int64(regs.SP()) + ret)
|
||||||
data := make([]byte, 8)
|
data, err := p.CurrentThread.readMemory(uintptr(addr), 8)
|
||||||
|
if err != nil {
|
||||||
readMemory(p.CurrentThread, uintptr(addr), data)
|
t.Fatal(err)
|
||||||
|
}
|
||||||
addr = binary.LittleEndian.Uint64(data)
|
addr = binary.LittleEndian.Uint64(data)
|
||||||
|
|
||||||
_, l, _ := p.goSymTable.PCToLine(addr)
|
_, l, _ := p.goSymTable.PCToLine(addr)
|
||||||
|
|||||||
@ -46,7 +46,6 @@ func (n NullAddrError) Error() string {
|
|||||||
func (dbp *Process) stacktrace(pc, sp uint64, depth int) ([]Location, error) {
|
func (dbp *Process) stacktrace(pc, sp uint64, depth int) ([]Location, error) {
|
||||||
var (
|
var (
|
||||||
ret = pc
|
ret = pc
|
||||||
data = make([]byte, dbp.arch.PtrSize())
|
|
||||||
btoffset int64
|
btoffset int64
|
||||||
locations []Location
|
locations []Location
|
||||||
retaddr uintptr
|
retaddr uintptr
|
||||||
@ -63,7 +62,7 @@ func (dbp *Process) stacktrace(pc, sp uint64, depth int) ([]Location, error) {
|
|||||||
if retaddr == 0 {
|
if retaddr == 0 {
|
||||||
return nil, NullAddrError{}
|
return nil, NullAddrError{}
|
||||||
}
|
}
|
||||||
_, err = readMemory(dbp.CurrentThread, retaddr, data)
|
data, err := dbp.CurrentThread.readMemory(retaddr, dbp.arch.PtrSize())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@ -266,8 +266,7 @@ func (thread *Thread) GetG() (g *G, err error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
gaddrbs := make([]byte, 8)
|
gaddrbs, err := thread.readMemory(uintptr(regs.TLS()+thread.dbp.arch.GStructOffset()), thread.dbp.arch.PtrSize())
|
||||||
_, err = readMemory(thread, uintptr(regs.TLS()+thread.dbp.arch.GStructOffset()), gaddrbs)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@ -65,7 +65,7 @@ func (t *Thread) blocked() bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeMemory(thread *Thread, addr uintptr, data []byte) (int, error) {
|
func (thread *Thread) writeMemory(addr uintptr, data []byte) (int, error) {
|
||||||
if len(data) == 0 {
|
if len(data) == 0 {
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
@ -80,19 +80,20 @@ func writeMemory(thread *Thread, addr uintptr, data []byte) (int, error) {
|
|||||||
return len(data), nil
|
return len(data), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func readMemory(thread *Thread, addr uintptr, data []byte) (int, error) {
|
func (thread *Thread) readMemory(addr uintptr, size int) ([]byte, error) {
|
||||||
if len(data) == 0 {
|
if size == 0 {
|
||||||
return 0, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
var (
|
var (
|
||||||
vm_data = unsafe.Pointer(&data[0])
|
buf = make([]byte, size)
|
||||||
|
vm_data = unsafe.Pointer(&buf[0])
|
||||||
vm_addr = C.mach_vm_address_t(addr)
|
vm_addr = C.mach_vm_address_t(addr)
|
||||||
length = C.mach_msg_type_number_t(len(data))
|
length = C.mach_msg_type_number_t(size)
|
||||||
)
|
)
|
||||||
|
|
||||||
ret := C.read_memory(thread.dbp.os.task, vm_addr, vm_data, length)
|
ret := C.read_memory(thread.dbp.os.task, vm_addr, vm_data, length)
|
||||||
if ret < 0 {
|
if ret < 0 {
|
||||||
return 0, fmt.Errorf("could not read memory")
|
return nil, fmt.Errorf("could not read memory")
|
||||||
}
|
}
|
||||||
return len(data), nil
|
return buf, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -67,7 +67,7 @@ func (thread *Thread) restoreRegisters() (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeMemory(thread *Thread, addr uintptr, data []byte) (written int, err error) {
|
func (thread *Thread) writeMemory(addr uintptr, data []byte) (written int, err error) {
|
||||||
if len(data) == 0 {
|
if len(data) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -75,10 +75,11 @@ func writeMemory(thread *Thread, addr uintptr, data []byte) (written int, err er
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func readMemory(thread *Thread, addr uintptr, data []byte) (read int, err error) {
|
func (thread *Thread) readMemory(addr uintptr, size int) (data []byte, err error) {
|
||||||
if len(data) == 0 {
|
if size == 0 {
|
||||||
return
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
data = make([]byte, size)
|
||||||
thread.dbp.execPtraceFunc(func() { read, err = sys.PtracePeekData(thread.Id, addr, data) })
|
thread.dbp.execPtraceFunc(func() { read, err = sys.PtracePeekData(thread.Id, addr, data) })
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@ -810,18 +810,6 @@ func (thread *Thread) readFunctionPtr(addr uintptr) (string, error) {
|
|||||||
return fn.Name, nil
|
return fn.Name, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (thread *Thread) readMemory(addr uintptr, size int) ([]byte, error) {
|
|
||||||
if size == 0 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
buf := make([]byte, size)
|
|
||||||
_, err := readMemory(thread, addr, buf)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return buf, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fetches all variables of a specific type in the current function scope
|
// Fetches all variables of a specific type in the current function scope
|
||||||
func (thread *Thread) variablesByTag(tag dwarf.Tag) ([]*Variable, error) {
|
func (thread *Thread) variablesByTag(tag dwarf.Tag) ([]*Variable, error) {
|
||||||
pc, err := thread.PC()
|
pc, err := thread.PC()
|
||||||
|
|||||||
Reference in New Issue
Block a user