forked from TrenchBoot/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
strace.go
66 lines (55 loc) · 1.27 KB
/
strace.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
// Copyright 2012-2018 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// strace is a simple multi-process tracer.
// It starts the comand and lets the strace.Run() do all the work.
//
// Synopsis:
// strace <command> [args...]
//
// Description:
// trace a single process given a command name.
package main
import (
// Don't use spf13 flags. It will not allow commands like
// strace ls -l
// it tries to use the -l for strace instead of leaving it alone.
"flag"
"fmt"
"log"
"os"
"os/exec"
"github.com/u-root/u-root/pkg/strace"
)
var (
cmdUsage = "Usage: strace <command> [args...]"
debug = flag.Bool("d", false, "enable debug printing")
tr = make(chan *strace.TraceRecord)
)
func usage() {
log.Fatalf(cmdUsage)
}
func main() {
flag.Parse()
if *debug {
strace.Debug = log.Printf
}
a := flag.Args()
if len(a) < 1 {
usage()
}
c := exec.Command(a[0], a[1:]...)
c.Stdin, c.Stdout, c.Stderr = os.Stdin, os.Stdout, os.Stderr
t, err := strace.New()
if err != nil {
log.Fatal(err)
}
go t.RunTracerFromCmd(c)
for r := range t.Records {
if r.Err != nil {
fmt.Printf("Record shows error %v\n", r.Err)
continue
}
fmt.Printf("%s\n", r.Out)
}
}