github.com/shaardie/u-root@v4.0.1-0.20190127173353-f24a1c26aa2e+incompatible/cmds/strace/strace.go (about)

     1  // Copyright 2012-2018 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // strace is a simple multi-process tracer.
     6  // It starts the comand and lets the strace.Run() do all the work.
     7  //
     8  // Synopsis:
     9  //     strace <command> [args...]
    10  //
    11  // Description:
    12  //	trace a single process given a command name.
    13  package main
    14  
    15  import (
    16  	// Don't use spf13 flags. It will not allow commands like
    17  	// strace ls -l
    18  	// it tries to use the -l for strace instead of leaving it alone.
    19  	"flag"
    20  	"fmt"
    21  	"log"
    22  	"os"
    23  	"os/exec"
    24  
    25  	"github.com/u-root/u-root/pkg/strace"
    26  )
    27  
    28  var (
    29  	cmdUsage = "Usage: strace <command> [args...]"
    30  	debug    = flag.Bool("d", false, "enable debug printing")
    31  	tr       = make(chan *strace.TraceRecord)
    32  )
    33  
    34  func usage() {
    35  	log.Fatalf(cmdUsage)
    36  }
    37  
    38  func main() {
    39  
    40  	flag.Parse()
    41  
    42  	if *debug {
    43  		strace.Debug = log.Printf
    44  	}
    45  	a := flag.Args()
    46  	if len(a) < 1 {
    47  		usage()
    48  	}
    49  
    50  	c := exec.Command(a[0], a[1:]...)
    51  	c.Stdin, c.Stdout, c.Stderr = os.Stdin, os.Stdout, os.Stderr
    52  
    53  	t, err := strace.New()
    54  	if err != nil {
    55  		log.Fatal(err)
    56  	}
    57  
    58  	go t.RunTracerFromCmd(c)
    59  	for r := range t.Records {
    60  		if r.Err != nil {
    61  			fmt.Printf("Record shows error %v\n", r.Err)
    62  			continue
    63  		}
    64  		fmt.Printf("%s\n", r.Out)
    65  	}
    66  }