gitlab.com/apertussolutions/u-root@v7.0.0+incompatible/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  // +build linux,amd64
     6  
     7  // strace is a simple multi-process syscall & signal tracer.
     8  //
     9  // Synopsis:
    10  //     strace <command> [args...]
    11  //
    12  // Description:
    13  //	trace a single process given a command name.
    14  package main
    15  
    16  import (
    17  	// Don't use spf13 flags. It will not allow commands like
    18  	// strace ls -l
    19  	// it tries to use the -l for strace instead of leaving it alone.
    20  	"flag"
    21  	"log"
    22  	"os"
    23  	"os/exec"
    24  
    25  	"github.com/u-root/u-root/pkg/strace"
    26  )
    27  
    28  const (
    29  	cmdUsage = "Usage: strace [-o <outputfile>] <command> [args...]"
    30  )
    31  
    32  func usage() {
    33  	log.Fatalf(cmdUsage)
    34  }
    35  
    36  func main() {
    37  	o := flag.String("o", "", "write output to file (if empty, stdout)")
    38  	flag.Parse()
    39  
    40  	a := flag.Args()
    41  	if len(a) < 1 {
    42  		usage()
    43  	}
    44  
    45  	c := exec.Command(a[0], a[1:]...)
    46  	c.Stdin, c.Stdout, c.Stderr = os.Stdin, os.Stdout, os.Stderr
    47  
    48  	out := os.Stdout
    49  	if len(*o) > 0 {
    50  		f, err := os.Create(*o)
    51  		if err != nil {
    52  			log.Fatalf("creating output file: %s", err)
    53  		}
    54  		defer f.Close()
    55  		out = f
    56  	}
    57  	if err := strace.Strace(c, out); err != nil {
    58  		log.Printf("strace exited: %v", err)
    59  	}
    60  }