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!
This commit is contained in:
James Shubin
2020-01-13 00:59:27 -05:00
parent fec94aa53a
commit b65a9abf8e
2 changed files with 94 additions and 0 deletions

31
misc/mgmt_debug.sh Executable file
View File

@@ -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

63
misc/sigtee.go Normal file
View File

@@ -0,0 +1,63 @@
// Signal tee
// Copyright (C) 2013-2020+ James Shubin and the project contributors
// Written by James Shubin <james@shubin.ca> 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 <http://www.gnu.org/licenses/>.
// 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: <STDIN> | %s <FILE>\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
}
}