From b65a9abf8e81a5beb4033e810066eaf673613b11 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Mon, 13 Jan 2020 00:59:27 -0500 Subject: [PATCH] misc: Add two scripts to help debug things This adds two new helper scripts that are good for debugging mgmt. Just build and add `sigtee` to your ~/bin/ along with filter-golang-stack.py and mgmt_debug.sh and use the later to call mgmt as you would normally. For example, I might do: $ mgmt_debug.sh ./mgmt run --tmp-prefix lang examples/lang/hello0.mcl And if I kill it with ^\ then I'll get a filtered trace at the end in my $PAGER (which is assumed to be `less`) and this should make my life easier. As a cool bonus, this means we use bash, python, and golang all together! --- misc/mgmt_debug.sh | 31 +++++++++++++++++++++++ misc/sigtee.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100755 misc/mgmt_debug.sh create mode 100644 misc/sigtee.go diff --git a/misc/mgmt_debug.sh b/misc/mgmt_debug.sh new file mode 100755 index 00000000..41d96da9 --- /dev/null +++ b/misc/mgmt_debug.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +# This is a helper script to run mgmt and capture and filter stack traces. Any +# time mgmt crashes with a trace, it will first be filtered, and then displayed +# with `less`. + +f1=`mktemp /tmp/tmp.X'X'X` +f2=`mktemp /tmp/tmp.X'X'X` + +# run the program until it ends +# XXX: we need an --ignore-exit signal blocker too so we can ^\ +#$@ 2>&1 | tee --ignore-interrupts --ignore-exit "$f1" +$@ 2>&1 | sigtee "$f1" + +# clean up when we're done +function cleanup { + filter-golang-stack.py "$f1" > "$f2" + if [ $? -eq 0 ]; then + less "$f2" + fi + if [ "$1" = "--preserve" ]; then + echo "logged:" + echo "$f1" + echo "$f2" + else + rm "$f2" + rm "$f1" + fi + +} +trap cleanup EXIT diff --git a/misc/sigtee.go b/misc/sigtee.go new file mode 100644 index 00000000..9e49c397 --- /dev/null +++ b/misc/sigtee.go @@ -0,0 +1,63 @@ +// Signal tee +// Copyright (C) 2013-2020+ James Shubin and the project contributors +// Written by James Shubin and the project contributors +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +// This program works exactly like the `tee` program, if it was invoked with an +// implicit --ignore-interrupts argument, and if it also had an --ignore-quit +// argument. These are needed so that it doesn't exit prematurely when called as +// a receiving member of a shell pipeline. This is needed so that a ^C or ^\ can +// cause the sending process to shutdown and relay its data into the tee. Sadly, +// the venerable `tee` program can't currently ignore the QUIT signal. +package main + +import ( + "fmt" + "io" + "os" + "os/signal" + "syscall" +) + +func main() { + // TODO: Add better argv parsing and implement explicit flags for + // --ignore-interrupts argument, and --ignore-quit so that it's cleaner. + if len(os.Args) != 2 { + fmt.Fprintf(os.Stderr, "usage: | %s \n", os.Args[0]) + os.Exit(1) + return + } + filename := os.Args[1] + + // Make sure we ignore ^C and ^\ when run in a shell pipe. + signal.Ignore(os.Interrupt, syscall.SIGQUIT) // TODO: add os.Kill ? + + f, err := os.Create(filename) + if err != nil { + fmt.Fprintf(os.Stderr, "can't write to: %s\n", filename) + os.Exit(1) + return + } + defer f.Close() + + writer := io.MultiWriter(os.Stdout, f) // tee ! + + _, err = io.Copy(writer, os.Stdin) + if err != nil { + fmt.Fprintf(os.Stderr, "copy error: %w\n", err) + os.Exit(1) + return + } +}