summaryrefslogtreecommitdiffhomepage
path: root/tools/nogo/dump/dump.go
blob: f06567e0fd876ec086c51152ab27a4274abdd8b2 (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
// Copyright 2019 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package dump contains data dump tools.
//
// The interface used by the package corresponds to the tool generated by the
// nogo_dump_tool rule.
//
// This package is separate in order to avoid a dependency cycle.
package dump

import (
	"flag"
	"fmt"
	"io"
	"os"
	"os/exec"
)

var (
	// Binary is the binary under analysis.
	//
	// See Reader, below.
	binary = flag.String("binary", "", "binary under analysis")

	// Reader is the input stream.
	//
	// This may be set instead of Binary.
	Reader io.Reader

	// Tool is the tool used to dump a binary.
	tool = flag.String("dump_tool", "", "tool used to dump a binary")
)

// Command returns a command that will emit the dumped object on stdout.
//
// You must call Wait on the resulting command.
func Command() (*exec.Cmd, io.Reader, error) {
	var (
		args  []string
		stdin io.Reader
	)
	if *binary != "" {
		args = append(args, *binary)
		*binary = "" // Clear.
	} else if Reader != nil {
		stdin = Reader
		Reader = nil // Clear.
	} else {
		// We have no input stream or binary.
		return nil, nil, fmt.Errorf("no binary or reader provided!")
	}

	// Construct our command.
	cmd := exec.Command(*tool, args...)
	cmd.Stdin = stdin
	cmd.Stderr = os.Stderr
	out, err := cmd.StdoutPipe()
	if err != nil {
		return nil, nil, err
	}
	if err := cmd.Start(); err != nil {
		return nil, nil, err
	}

	return cmd, out, err
}