Skip to content

Instantly share code, notes, and snippets.

@necrophonic
Created June 26, 2020 09:36
Show Gist options
  • Save necrophonic/b95cc09b592288cf622391c001579167 to your computer and use it in GitHub Desktop.
Save necrophonic/b95cc09b592288cf622391c001579167 to your computer and use it in GitHub Desktop.
Simple Java thread dump parser
package main
import (
"bufio"
"context"
"flag"
"fmt"
"os"
"os/signal"
"regexp"
)
func main() {
filter := flag.String("filter", ".*", "Filter threads")
flag.Parse()
fmt.Println("Using filter:", *filter)
// Set up the signal handler to deal with user interrupts
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt)
// Set up the scanning of the STDIN
s := make(chan string, 1)
scanner := bufio.NewScanner(os.Stdin)
go func(s chan string) {
for scanner.Scan() {
s <- scanner.Text()
}
}(s)
// Regexes for parsing pertinent parts of the data stream
reFilter := regexp.MustCompile(*filter)
reThread := regexp.MustCompile(`"([^"]+)".* nid=\S+ ([^[]+)`)
for {
select {
case text := <-s:
if matches := reThread.FindStringSubmatch(text); len(matches) > 0 {
// Simply for now just output
if reFilter.MatchString(text) {
fmt.Printf("%-25s %s\n", matches[1], matches[2])
}
}
case <-ctx.Done():
return
case <-sigs:
// TODO
fmt.Println("Cleaning up and exiting!")
os.Exit(0)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment