pkg/proc: optimize code for supporting different arch in the future. (#1849)

This commit is contained in:
chainhelen
2020-02-10 19:32:50 -06:00
committed by GitHub
parent 0741d3e57f
commit bd279cb9da
2 changed files with 9 additions and 22 deletions

View File

@ -1,7 +1,5 @@
package proc package proc
import "encoding/binary"
type goroutineCache struct { type goroutineCache struct {
partialGCache map[int]*G partialGCache map[int]*G
allGCache []*G allGCache []*G
@ -29,20 +27,14 @@ func (gcache *goroutineCache) getRuntimeAllg(bi *BinaryInfo, mem MemoryReadWrite
if gcache.allglenAddr == 0 || gcache.allgentryAddr == 0 { if gcache.allglenAddr == 0 || gcache.allgentryAddr == 0 {
return 0, 0, ErrNoRuntimeAllG return 0, 0, ErrNoRuntimeAllG
} }
allglenBytes := make([]byte, 8) allglen, err := readUintRaw(mem, uintptr(gcache.allglenAddr), int64(bi.Arch.PtrSize()))
_, err := mem.ReadMemory(allglenBytes, uintptr(gcache.allglenAddr))
if err != nil { if err != nil {
return 0, 0, err return 0, 0, err
} }
allglen := binary.LittleEndian.Uint64(allglenBytes) allgptr, err := readUintRaw(mem, uintptr(gcache.allgentryAddr), int64(bi.Arch.PtrSize()))
faddr := make([]byte, bi.Arch.PtrSize())
_, err = mem.ReadMemory(faddr, uintptr(gcache.allgentryAddr))
if err != nil { if err != nil {
return 0, 0, err return 0, 0, err
} }
allgptr := binary.LittleEndian.Uint64(faddr)
return allgptr, allglen, nil return allgptr, allglen, nil
} }

View File

@ -524,12 +524,11 @@ func (v *Variable) parseG() (*G, error) {
_, deref := v.RealType.(*godwarf.PtrType) _, deref := v.RealType.(*godwarf.PtrType)
if deref { if deref {
gaddrbytes := make([]byte, v.bi.Arch.PtrSize()) var err error
_, err := mem.ReadMemory(gaddrbytes, uintptr(gaddr)) gaddr, err = readUintRaw(mem, uintptr(gaddr), int64(v.bi.Arch.PtrSize()))
if err != nil { if err != nil {
return nil, fmt.Errorf("error derefing *G %s", err) return nil, fmt.Errorf("error derefing *G %s", err)
} }
gaddr = binary.LittleEndian.Uint64(gaddrbytes)
} }
if gaddr == 0 { if gaddr == 0 {
id := 0 id := 0
@ -1061,26 +1060,23 @@ func readStringInfo(mem MemoryReadWriter, arch Arch, addr uintptr) (uintptr, int
mem = cacheMemory(mem, addr, arch.PtrSize()*2) mem = cacheMemory(mem, addr, arch.PtrSize()*2)
// read len // read len
val := make([]byte, arch.PtrSize()) strlen, err := readIntRaw(mem, addr+uintptr(arch.PtrSize()), int64(arch.PtrSize()))
_, err := mem.ReadMemory(val, addr+uintptr(arch.PtrSize()))
if err != nil { if err != nil {
return 0, 0, fmt.Errorf("could not read string len %s", err) return 0, 0, fmt.Errorf("could not read string len %s", err)
} }
strlen := int64(binary.LittleEndian.Uint64(val))
if strlen < 0 { if strlen < 0 {
return 0, 0, fmt.Errorf("invalid length: %d", strlen) return 0, 0, fmt.Errorf("invalid length: %d", strlen)
} }
// read addr // read addr
_, err = mem.ReadMemory(val, addr) val, err := readUintRaw(mem, addr, int64(arch.PtrSize()))
if err != nil { if err != nil {
return 0, 0, fmt.Errorf("could not read string pointer %s", err) return 0, 0, fmt.Errorf("could not read string pointer %s", err)
} }
addr = uintptr(binary.LittleEndian.Uint64(val)) addr = uintptr(val)
if addr == 0 { if addr == 0 {
return 0, 0, nil return 0, 0, nil
} }
return addr, strlen, nil return addr, strlen, nil
} }
@ -1457,14 +1453,13 @@ func (v *Variable) readFunctionPtr() {
return return
} }
val := make([]byte, v.bi.Arch.PtrSize()) val, err := readUintRaw(v.mem, uintptr(v.closureAddr), int64(v.bi.Arch.PtrSize()))
_, err := v.mem.ReadMemory(val, uintptr(v.closureAddr))
if err != nil { if err != nil {
v.Unreadable = err v.Unreadable = err
return return
} }
v.Base = uintptr(binary.LittleEndian.Uint64(val)) v.Base = uintptr(val)
fn := v.bi.PCToFunc(uint64(v.Base)) fn := v.bi.PCToFunc(uint64(v.Base))
if fn == nil { if fn == nil {
v.Unreadable = fmt.Errorf("could not find function for %#v", v.Base) v.Unreadable = fmt.Errorf("could not find function for %#v", v.Base)