-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdecode_address.go
52 lines (49 loc) · 1.12 KB
/
decode_address.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright 2016 Platina Systems, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package elib
import (
"fmt"
"reflect"
"sort"
)
func DecodeAddress(x interface{}, address uint) (path []string, t reflect.Type) {
t = reflect.ValueOf(x).Type()
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
addr := uintptr(address)
for {
switch t.Kind() {
case reflect.Struct:
nf := t.NumField()
var nextType reflect.Type
sort.Search(nf, func(i int) (ok bool) {
f := t.Field(i)
lo, hi := f.Offset, f.Offset+f.Type.Size()
ok = addr <= lo
if found := addr >= lo && addr < hi; found {
dot := ""
if len(path) > 0 {
dot = "."
}
path = append(path, dot+f.Name)
nextType = f.Type
addr -= lo
}
return
})
if nextType == nil {
panic(fmt.Errorf("not found %s 0x%x %v 0x%x", t.Name(), addr, path, address))
}
t = nextType
case reflect.Array:
t = t.Elem()
i0, i1 := addr/t.Size(), addr%t.Size()
path = append(path, fmt.Sprintf("[%d]", i0))
addr = i1
default:
return
}
}
}