From bd279cb9da918b173923f8dbe84f837035106a26 Mon Sep 17 00:00:00 2001 From: chainhelen Date: Mon, 10 Feb 2020 19:32:50 -0600 Subject: [PATCH] pkg/proc: optimize code for supporting different arch in the future. (#1849) --- pkg/proc/goroutine_cache.go | 12 ++---------- pkg/proc/variables.go | 19 +++++++------------ 2 files changed, 9 insertions(+), 22 deletions(-) diff --git a/pkg/proc/goroutine_cache.go b/pkg/proc/goroutine_cache.go index e0517337..c4613a10 100644 --- a/pkg/proc/goroutine_cache.go +++ b/pkg/proc/goroutine_cache.go @@ -1,7 +1,5 @@ package proc -import "encoding/binary" - type goroutineCache struct { partialGCache map[int]*G allGCache []*G @@ -29,20 +27,14 @@ func (gcache *goroutineCache) getRuntimeAllg(bi *BinaryInfo, mem MemoryReadWrite if gcache.allglenAddr == 0 || gcache.allgentryAddr == 0 { return 0, 0, ErrNoRuntimeAllG } - allglenBytes := make([]byte, 8) - _, err := mem.ReadMemory(allglenBytes, uintptr(gcache.allglenAddr)) + allglen, err := readUintRaw(mem, uintptr(gcache.allglenAddr), int64(bi.Arch.PtrSize())) if err != nil { return 0, 0, err } - allglen := binary.LittleEndian.Uint64(allglenBytes) - - faddr := make([]byte, bi.Arch.PtrSize()) - _, err = mem.ReadMemory(faddr, uintptr(gcache.allgentryAddr)) + allgptr, err := readUintRaw(mem, uintptr(gcache.allgentryAddr), int64(bi.Arch.PtrSize())) if err != nil { return 0, 0, err } - allgptr := binary.LittleEndian.Uint64(faddr) - return allgptr, allglen, nil } diff --git a/pkg/proc/variables.go b/pkg/proc/variables.go index ee8468a1..772448ff 100644 --- a/pkg/proc/variables.go +++ b/pkg/proc/variables.go @@ -524,12 +524,11 @@ func (v *Variable) parseG() (*G, error) { _, deref := v.RealType.(*godwarf.PtrType) if deref { - gaddrbytes := make([]byte, v.bi.Arch.PtrSize()) - _, err := mem.ReadMemory(gaddrbytes, uintptr(gaddr)) + var err error + gaddr, err = readUintRaw(mem, uintptr(gaddr), int64(v.bi.Arch.PtrSize())) if err != nil { return nil, fmt.Errorf("error derefing *G %s", err) } - gaddr = binary.LittleEndian.Uint64(gaddrbytes) } if gaddr == 0 { id := 0 @@ -1061,26 +1060,23 @@ func readStringInfo(mem MemoryReadWriter, arch Arch, addr uintptr) (uintptr, int mem = cacheMemory(mem, addr, arch.PtrSize()*2) // read len - val := make([]byte, arch.PtrSize()) - _, err := mem.ReadMemory(val, addr+uintptr(arch.PtrSize())) + strlen, err := readIntRaw(mem, addr+uintptr(arch.PtrSize()), int64(arch.PtrSize())) if err != nil { return 0, 0, fmt.Errorf("could not read string len %s", err) } - strlen := int64(binary.LittleEndian.Uint64(val)) if strlen < 0 { return 0, 0, fmt.Errorf("invalid length: %d", strlen) } // read addr - _, err = mem.ReadMemory(val, addr) + val, err := readUintRaw(mem, addr, int64(arch.PtrSize())) if err != nil { return 0, 0, fmt.Errorf("could not read string pointer %s", err) } - addr = uintptr(binary.LittleEndian.Uint64(val)) + addr = uintptr(val) if addr == 0 { return 0, 0, nil } - return addr, strlen, nil } @@ -1457,14 +1453,13 @@ func (v *Variable) readFunctionPtr() { return } - val := make([]byte, v.bi.Arch.PtrSize()) - _, err := v.mem.ReadMemory(val, uintptr(v.closureAddr)) + val, err := readUintRaw(v.mem, uintptr(v.closureAddr), int64(v.bi.Arch.PtrSize())) if err != nil { v.Unreadable = err return } - v.Base = uintptr(binary.LittleEndian.Uint64(val)) + v.Base = uintptr(val) fn := v.bi.PCToFunc(uint64(v.Base)) if fn == nil { v.Unreadable = fmt.Errorf("could not find function for %#v", v.Base)