github.com/zaolin/u-root@v0.0.0-20200428085104-64aaafd46c6d/cmds/core/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  )
    32  
    33  func usage() {
    34  	log.Fatalf(cmdUsage)
    35  }
    36  
    37  func main() {
    38  
    39  	flag.Parse()
    40  
    41  	if *debug {
    42  		strace.Debug = log.Printf
    43  	}
    44  	a := flag.Args()
    45  	if len(a) < 1 {
    46  		usage()
    47  	}
    48  
    49  	c := exec.Command(a[0], a[1:]...)
    50  	c.Stdin, c.Stdout, c.Stderr = os.Stdin, os.Stdout, os.Stderr
    51  
    52  	t, err := strace.New()
    53  	if err != nil {
    54  		log.Fatal(err)
    55  	}
    56  
    57  	go t.RunTracerFromCmd(c)
    58  	for r := range t.Records {
    59  		if r.Err != nil {
    60  			fmt.Printf("Record shows error %v\n", r.Err)
    61  			continue
    62  		}
    63  		fmt.Printf("%s\n", r.Out)
    64  	}
    65  }