aboutsummaryrefslogtreecommitdiff
path: root/tools/src/crashhandler/main.go
blob: 7b1717f20385cd189833663f918be7ecaef94d0b (plain)
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
package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"os"
	"strings"
)

type CrashDump struct {
	Name         string
	DumpPath     string
	MetadataPath string
}

func expandHomeDir(path string) (string, error) {
	home, err := os.UserHomeDir()
	if err != nil {
		return path, err
	}

	return strings.Replace(path, "~/", home, -1), nil
}

func dumps(path string) ([]CrashDump, error) {
	files, err := ioutil.ReadDir(path)
	if err != nil {
		return nil, err
	}

	var crashes []CrashDump

	for i, file := range files {
		if strings.HasSuffix(file.Name(), ".dmp") {
			if i+1 < len(files) && files[i+1].Name() == file.Name()+".txt" {
				crashes = append(crashes, CrashDump{Name: strings.TrimSuffix(file.Name(), ".dmp"),
					DumpPath: file.Name(), MetadataPath: files[i+1].Name()})
			} else {
				crashes = append(crashes, CrashDump{Name: strings.TrimSuffix(file.Name(), ".dmp"), DumpPath: file.Name()})
			}
		}
	}

	return crashes, nil
}

func main() {
	helpFlag := flag.Bool("help", false, "Show help information.")
	flag.StringVar(&dumpPath, "crashd", dumpPath, "Crash dump path")

	// create crash report flags
	crashedFlag := flag.String("c", "", "Create crash report at specified location and write any specified data into it")

	flag.Parse()
	dumpPath, _ = expandHomeDir(dumpPath)

	if *helpFlag {
		flag.PrintDefaults()
		return
	}

	if *crashedFlag != "" {
		fmt.Println("Creating crash dump report", *crashedFlag)
		contents := []byte("Additional information: " + strings.Join(flag.Args(), ""))
		ioutil.WriteFile(*crashedFlag+".txt", contents, 0644)
		return
	}

	fmt.Printf("  [%s]\n", dumpPath)
	c, err := dumps(dumpPath)
	if err != nil {
		panic(err)
	}
	for _, d := range c {
		fmt.Printf("\t- %s\n", d.Name)
	}
        fmt.Println("  To analyze a crashdump, use the minidump_stackwalk tool:")
        fmt.Println("minidump_stackwalk <minidump-file> [symbol-path]")

}