-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
97 lines (88 loc) · 2.29 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"context"
"flag"
"fmt"
"io"
"log"
"os"
"syscall"
"time"
"github.com/google/uuid"
"github.com/noxiouz/gcoredumper/configuration/configurator"
_ "github.com/noxiouz/gcoredumper/configuration/configurator/localfile"
"github.com/noxiouz/gcoredumper/core"
"github.com/noxiouz/gcoredumper/report"
)
var (
initialPid = flag.Int64("P", 0, "initial PID")
nsPid = flag.Int64("p", 0, "ns PID")
initialTid = flag.Int64("I", 0, "initial TID")
nsTid = flag.Int64("i", 0, "ns TID")
executable = flag.String("E", "", "%E core_pattern")
signalNum = flag.Int("s", 0, "signal num %s")
dumpable = flag.Int("d", 0, "dumpable")
timestampInSec = flag.Int64("t", 0, "")
config = flag.String("cfg", "embed:null", "path to config")
)
func SetUpLogger(w io.Writer) {
log.SetOutput(w)
log.SetPrefix(fmt.Sprintf("%v: ", uuid.NewString()))
}
func main() {
flag.Parse()
reporter := report.New()
// TODO(noxiouz): make configurable
cfg, err := configurator.Open("embed", "")
if err != nil {
log.Fatalf("%v", err)
}
config, err := cfg.Get(context.Background())
if err != nil {
log.Fatalf("%v", err)
}
f, err := os.OpenFile(config.LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Println(err)
SetUpLogger(io.Discard)
} else {
SetUpLogger(f)
defer f.Close()
}
defer func() {
if r := recover(); r != nil {
log.Printf("Recovered in main %v", r)
}
}()
sink := &report.LogBasedReporter{Logger: log.Default()}
defer reporter.Report(sink)
log.Println("Start dump")
ctx := report.WithReport(context.Background(), reporter)
// TODO: add required arguments check
si := core.SystemInput{
// Pathname of Executable
Executable: *executable,
// TID in initial namespace
InitialTid: *initialTid,
// TID in process namespace
NsTid: *nsTid,
// PR_GET_DUMPABLE
PrGetDumpable: core.Dumpable(*dumpable),
// PID in intial namespace
InitialPid: *initialPid,
// PID in process namespace
NsPid: *nsPid,
// Time of dump
DumpTime: time.Unix(*timestampInSec, 0),
// Signal
Signal: syscall.Signal(*signalNum),
// Input
Stream: os.Stdin,
}
err = core.Run(ctx, si, config)
if err != nil {
log.Printf("Run returned an error %v", err)
reporter.AddError("core.run.error", err)
}
log.Println("Done")
}