-
Notifications
You must be signed in to change notification settings - Fork 23
/
debug.go
82 lines (68 loc) · 1.63 KB
/
debug.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
// Parse the debug headers - extracts the GUID and PDB information (RSDS).
package pe
import (
"fmt"
"os"
"strings"
"github.com/davecgh/go-spew/spew"
)
var (
PE_DEBUG bool
)
func (self *IMAGE_NT_HEADERS) DebugDirectory(
rva_resolver *RVAResolver) (*IMAGE_DEBUG_DIRECTORY, error) {
dir := self.DataDirectory(IMAGE_DIRECTORY_ENTRY_DEBUG)
offset, err := rva_resolver.GetFileAddress(dir.VirtualAddress())
if err != nil {
return nil, err
}
return self.Profile.IMAGE_DEBUG_DIRECTORY(self.Reader, int64(offset)), nil
}
func (self *IMAGE_NT_HEADERS) RSDS(
rva_resolver *RVAResolver) (*CV_RSDS_HEADER, error) {
debug_directory, err := self.DebugDirectory(rva_resolver)
if err != nil {
return nil, err
}
file_address, err := rva_resolver.GetFileAddress(
debug_directory.AddressOfRawData())
if err != nil {
return nil, err
}
return self.Profile.CV_RSDS_HEADER(self.Reader, int64(
file_address)), nil
}
// A prefixed string contains a length followed by the UTF16 string.
func (self *PrefixedString) String() string {
return ParseUTF16String(
self.Reader,
self.Profile.Off_PrefixedString__Buffer+self.Offset,
int64(self.Length()*2))
}
func init() {
// os.Environ() seems very expensive in Go so we cache it.
for _, x := range os.Environ() {
if strings.HasPrefix(x, "PE_DEBUG=") {
PE_DEBUG = true
return
}
}
}
func DebugPrint(fmt_str string, v ...interface{}) {
if PE_DEBUG {
fmt.Printf(fmt_str, v...)
}
}
type DebugStringer interface {
DebugString() string
}
func Debug(arg interface{}) {
if PE_DEBUG {
d, ok := arg.(DebugStringer)
if ok {
fmt.Println(d.DebugString())
return
}
spew.Dump(arg)
}
}