/
frameinstr.go
103 lines (92 loc) · 2.19 KB
/
frameinstr.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"bytes"
"encoding/binary"
"fmt"
"github.com/go-delve/delve/pkg/dwarf/op"
"github.com/go-delve/delve/pkg/dwarf/util"
)
//go:generate go run scripts/gen-frame-opcodes.go ./frame_opcodes.txt ./frame_opcodes.go
func PrettyPrintFrameInstr(instrs []uint8, lowpc uint64) string {
in := bytes.NewBuffer(instrs)
out := bytes.NewBuffer([]byte{})
fmt.Fprintf(out, "\t")
loc := lowpc
for {
opcode, err := in.ReadByte()
if err != nil {
break
}
high2 := (opcode & 0xc0) >> 6
low6 := opcode & 0x3f
name, found := frameOpcodeHigh2[high2]
if found {
opcode = opcode & 0xc0
fmt.Fprintf(out, "%s %#x ", name, low6)
if name == "DW_CFA_advance_loc" {
loc += uint64(low6)
fmt.Fprintf(out, "to %#x ", loc)
}
} else {
name, found = frameOpcodeLow6[low6]
if found {
fmt.Fprintf(out, "%s ", name)
}
}
if !found {
fmt.Fprintf(out, "\n\t")
continue
}
for _, arg := range frameOpcodeArgs[opcode] {
switch arg {
case 's':
n, _ := util.DecodeSLEB128(in)
fmt.Fprintf(out, "%#x ", n)
case 'u':
n, _ := util.DecodeULEB128(in)
fmt.Fprintf(out, "%#x ", n)
case '1':
var x uint8
binary.Read(in, binary.LittleEndian, &x)
fmt.Fprintf(out, "%#x ", x)
if name == "DW_CFA_advance_loc1" {
loc += uint64(x)
fmt.Fprintf(out, "to %#x ", loc)
}
case '2':
var x uint16
binary.Read(in, binary.LittleEndian, &x)
fmt.Fprintf(out, "%#x ", x)
if name == "DW_CFA_advance_loc2" {
loc += uint64(x)
fmt.Fprintf(out, "to %#x ", loc)
}
case '4':
var x uint32
binary.Read(in, binary.LittleEndian, &x)
fmt.Fprintf(out, "%#x ", x)
if name == "DW_CFA_advance_loc4" {
loc += uint64(x)
fmt.Fprintf(out, "to %#x ", loc)
}
case '8':
var x uint64
binary.Read(in, binary.LittleEndian, &x)
fmt.Fprintf(out, "%#x ", x)
if name == "DW_CFA_set_loc" {
//TODO: set loc
fmt.Fprintf(out, "to %#x ", loc)
}
case 'B':
sz, _ := util.DecodeULEB128(in)
data := make([]byte, sz)
sz2, _ := in.Read(data)
data = data[:sz2]
op.PrettyPrint(out, data)
fmt.Fprintf(out, "")
}
}
fmt.Fprintf(out, "\n\t")
}
return out.String()
}