-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbg.go
311 lines (281 loc) · 7.17 KB
/
dbg.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/* Distributed under the MIT license. See the LICENSE file.
* Copyright (c) 2014--2016 Thomas Fogal */
// "dbg" is a collection of stuff which is not really needed. these functions
// are often thrown into a code path as an ad hoc way of verifying that things
// work the way we expect.
package main
import "bufio"
import "fmt"
import "io"
import "log"
import "os"
import "sort"
import "syscall"
import "./bfd"
import "./cfg"
import "github.com/tfogal/ptrace"
// follows RSP a couple times and prints it out; a crude stack trace.
func print_walkstack(inferior *ptrace.Tracee) {
regs, err := inferior.GetRegs()
if err != nil {
log.Fatalf(err.Error())
}
for i := uint64(0); i < 4; i++ {
deref, err := inferior.ReadWord(uintptr(regs.Rsp + (i * 8)))
if err != nil {
log.Fatalf(err.Error())
}
fmt.Printf("[go] rsp-%d (0x%0x): 0x%0x\n", i*8, regs.Rsp+(i*8), deref)
}
}
// reads symbols and prints out the address for the symbols we care about most.
func print_symbol_info(inferior *ptrace.Tracee) {
symbols, err := bfd.SymbolsProcess(inferior)
if err != nil {
log.Fatalf("could not read inferior's symbols: %v\n", err)
}
symmalloc := symbol("malloc", symbols)
if symmalloc == nil {
log.Fatalf("could not find malloc symbol")
}
n := 0
for _, v := range symbols {
if v.Name() == "malloc" {
n++
fmt.Printf("malloc @ 0x%0x\n", v.Address())
}
}
fmt.Printf("There are %d 'malloc's in the symbol table.\n", n)
symcalloc := symbol("calloc", symbols)
if symcalloc == nil {
log.Fatalf("could not find calloc symbol")
}
fmt.Printf("calloc is at: 0x%x\n", symcalloc.Address())
fmt.Printf("malloc is at: 0x%x\n", symmalloc.Address())
}
// attaches to the PID given by the first argument and prints out the symbols
// of that process.
func print_address_info(argv []string) {
var pid int
fmt.Sscanf(argv[0], "%d", &pid)
fmt.Printf("attaching to %d\n", pid)
inferior, err := ptrace.Attach(pid)
if err != nil {
log.Fatalf("could not start program: %v", err)
}
eatstatus(<-inferior.Events()) // 'process stopped' due to attach.
print_symbol_info(inferior)
inferior.Detach()
inferior.Close()
}
// count the number of nodes in a graph.
func nnodes(graph map[uintptr]*cfg.Node) uint {
seen := make(map[uintptr]bool)
nodes := uint(0)
for k, _ := range graph {
if seen[k] {
continue
}
seen[k] = true
nodes++
}
return nodes
}
// a function on a CFG node.
type gfunc func(node *cfg.Node)
// performs an inorder traversal of the given graph, executing 'f' on each node.
func inorder(graph map[uintptr]*cfg.Node, f gfunc) {
seen := make(map[uintptr]bool)
for k, v := range graph {
if seen[k] {
continue
}
inorder_helper(v, seen, f)
seen[k] = true
}
}
// inorder traversal of a graph, internal implementation. this exists so that
// users don't need to create the map that keeps track of which nodes have been
// visited.
func inorder_helper(node *cfg.Node, seen map[uintptr]bool, f gfunc) {
if seen[node.Addr] {
return
}
seen[node.Addr] = true
f(node)
for _, edge := range node.Edgelist {
inorder_helper(edge.To, seen, f)
}
}
// returns true iff this node has a child that points back to it.
func loop1d(node *cfg.Node) bool {
for _, edge := range node.Edgelist {
target := edge.To
for _, childedge := range target.Edgelist {
if childedge.To.Addr == node.Addr {
return true
}
}
}
return false
}
func max_depth(root *cfg.Node) uint {
seen := make(map[uintptr]bool)
return max_depth_helper(root, seen)
}
func max_depth_helper(node *cfg.Node, seen map[uintptr]bool) uint {
if seen[node.Addr] {
return 0
}
seen[node.Addr] = true
dpth := node.Depth()
for _, e := range node.Edgelist {
d := max_depth_helper(e.To, seen)
if d > dpth {
dpth = d
}
}
return dpth
}
// prints out a node in 'dot' notation.
func dotNode(node *cfg.Node, w io.Writer) {
fmt.Fprintf(w, "\t\"0x%08x\" [", node.Addr)
fmt.Fprintf(w, "label = \"")
if node.Name != "" {
fmt.Fprintf(w, "%s\\n", node.Name)
} else {
fmt.Fprintf(w, "0x%08x\\n", node.Addr)
}
dpth := node.Depth()
dpth_max := max_depth(node)
fmt.Fprintf(w, "Depth: %d", dpth)
if node.LoopHeader() {
fmt.Fprintf(w, "\\nLoop header")
}
depthcolor := uint8(lerp(dpth, 0, dpth_max, 0, 255))
fmt.Fprintf(w, "\", color=\"#0000%02x\"", depthcolor)
fmt.Fprintf(w, "];\n")
for _, edge := range node.Edgelist {
fmt.Fprintf(w, "\t\"0x%08x\" -> \"0x%08x\";\n", node.Addr, edge.To.Addr)
}
}
func findNodeByName(graph map[uintptr]*cfg.Node, name string) *cfg.Node {
seen := make(map[uintptr]bool)
for k, v := range graph {
node := find_helper(v, seen, name)
if node != nil && node.Name == name {
return node
}
seen[k] = true
}
return nil
}
func find_helper(node *cfg.Node, seen map[uintptr]bool, name string) *cfg.Node {
if seen[node.Addr] {
return nil
}
if node.Name == name {
return node
}
seen[node.Addr] = true
for _, edge := range node.Edgelist {
found := find_helper(edge.To, seen, name)
if found != nil && found.Name == name {
return found
}
}
return nil
}
// linear interpolate
func lerp(val uint, imin uint, imax uint, omin uint, omax uint) uint {
if !(imin <= imax) {
panic("input parameter range makes no sense.")
}
if !(omin < omax) {
panic("output parameter range makes no sense")
}
return uint(float64(omin) + float64(val-imin)*
float64(omax-omin)/float64(imax-imin))
}
func whereis(inferior *ptrace.Tracee) uintptr {
iptr, err := inferior.GetIPtr()
if err != nil {
log.Fatalf("get insn ptr failed: %v\n", err)
}
return iptr
}
func eatstatus(stat ptrace.Event) {
status := stat.(syscall.WaitStatus)
fmt.Printf("[go] eating ")
if status.Stopped() {
fmt.Println("'stop' event")
}
if status.Continued() {
fmt.Println("'continue' event")
}
if status.Signaled() {
fmt.Printf("'%v' signal\n", status.Signal())
}
if status.Exited() {
fmt.Printf("exited event.\n")
}
}
// prints out the contents of the given process' maps. used for debugging
// free_space_address.
func viewmaps(pid int) {
filename := fmt.Sprintf("/proc/%d/maps", pid)
maps, err := os.Open(filename)
if err != nil {
panic(err)
}
defer maps.Close()
scanner := bufio.NewScanner(maps)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
}
type mapaddr struct {
low uintptr
high uintptr
name string
}
func getmaps(pid int) []mapaddr {
filename := fmt.Sprintf("/proc/%d/maps", pid)
maps, err := os.Open(filename)
if err != nil {
panic(err)
}
defer maps.Close()
addrs := make([]mapaddr, 0)
scanner := bufio.NewScanner(maps)
var l, h uintptr
var name string
for scanner.Scan() {
var junk string
line := scanner.Text()
_, err := fmt.Sscanf(line, "%x-%x %s %s %s %s %s", &l, &h, &junk, &junk,
&junk, &junk, &name)
if err == io.EOF {
return addrs
}
if err != nil {
panic(err)
}
addrs = append(addrs, mapaddr{low: l, high: h})
}
return addrs
}
type MapList []mapaddr
func (m MapList) Len() int { return len(m) }
func (m MapList) Less(i int, j int) bool { return m[i].low < m[j].low }
func (m MapList) Swap(i int, j int) { m[i], m[j] = m[j], m[i] }
func addr_where(pid int, addr uintptr) string {
alist := getmaps(pid)
sort.Sort(MapList(alist))
for _, ma := range alist {
if ma.low <= addr && addr <= ma.high {
return ma.name
}
}
return "unknown..."
}