-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
80 lines (66 loc) · 1.99 KB
/
main.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
package main
import (
"debug/elf"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/polarsignals/split-debug/pkg/elfutils"
"github.com/polarsignals/split-debug/pkg/elfwriter"
"github.com/polarsignals/split-debug/pkg/logger"
"github.com/alecthomas/kong"
"github.com/go-kit/log/level"
)
type flags struct {
LogLevel string `kong:"enum='error,warn,info,debug',help='Log level.',default='info'"`
Path string `kong:"required,arg,name='path',help='File path to the object file extract debug information from.',type:'path'"`
}
func main() {
flags := flags{}
_ = kong.Parse(&flags)
l := logger.NewLogger(flags.LogLevel, logger.LogFormatLogfmt, "")
if err := run(flags.Path); err != nil {
level.Error(l).Log("err", err)
os.Exit(1)
}
level.Info(l).Log("msg", "done!")
}
var isDwarf = func(s *elf.Section) bool {
return strings.HasPrefix(s.Name, ".debug_") ||
strings.HasPrefix(s.Name, ".zdebug_") ||
strings.HasPrefix(s.Name, "__debug_") // macos
}
var isSymbolTable = func(s *elf.Section) bool {
return s.Name == ".symtab" || s.Name == ".strtab" || s.Name == ".dynsymtab"
}
var isGoSymbolTable = func(s *elf.Section) bool {
return s.Name == ".gosymtab" || s.Name == ".gopclntab"
}
func run(path string) error {
elfFile, err := elfutils.Open(path)
if err != nil {
return fmt.Errorf("failed to open given field: %w", err)
}
defer elfFile.Close()
output, err := ioutil.TempFile(filepath.Dir(path), filepath.Base(path)+"-debuginfo.*")
if err != nil {
return fmt.Errorf("failed to create temp file: %w", err)
}
w, err := elfwriter.New(output, &elfFile.FileHeader)
if err != nil {
return fmt.Errorf("failed to initialize writer: %w", err)
}
for _, s := range elfFile.Sections {
if isDwarf(s) || isSymbolTable(s) || isGoSymbolTable(s) {
w.Sections = append(w.Sections, s)
}
}
if err := w.Write(); err != nil {
return fmt.Errorf("failed to write: %w", err)
}
if err := w.Close(); err != nil {
return fmt.Errorf("failed tom closer writer: %w", err)
}
return nil
}