1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
// Copyright 2018 Google Inc. All Rights Reserved.
// This file is available under the Apache license.
/*
Command mdot turns an mtail program AST into a graphviz graph on standard output.
To use, run it like (assuming your shell is in the same directory as this file)
go run github.com/jaqx0r/mtail/cmd/mdot --prog ../../examples/dhcpd.mtail | xdot -
or
go run github.com/jaqx0r/mtail/cmd/mdot --prog ../../examples/dhcpd.mtail --http_port 8080
to view the dot output visit http://localhost:8080
You'll need the graphviz `dot' command installed.
*/
package main
import (
"flag"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/golang/glog"
"github.com/jaqx0r/mtail/internal/mtail"
"github.com/jaqx0r/mtail/internal/runtime/compiler/ast"
"github.com/jaqx0r/mtail/internal/runtime/compiler/checker"
"github.com/jaqx0r/mtail/internal/runtime/compiler/parser"
)
var (
prog = flag.String("prog", "", "Name of the program source to parse.")
httpPort = flag.String("http_port", "", "Port number to run HTTP server on.")
)
type dotter struct {
w io.Writer
id int
parentID []int // id of the parent node
}
func (d *dotter) nextID() int {
d.id++
return d.id
}
func (d *dotter) emitNode(id int, node ast.Node) {
attrs := map[string]string{
"label": strings.Split(fmt.Sprintf("%T", node), ".")[1] + "\n",
"shape": "box",
"style": "filled",
"tooltip": node.Type().String(),
}
switch n := node.(type) {
case *ast.VarDecl, *ast.DecoDecl:
attrs["fillcolor"] = "lightgreen"
switch n := n.(type) {
case *ast.VarDecl:
attrs["label"] += fmt.Sprintf("%s %s", n.Kind, n.Name)
case *ast.DecoDecl:
attrs["label"] += n.Name
}
case *ast.IDTerm, *ast.CaprefTerm:
attrs["fillcolor"] = "pink"
attrs["shape"] = "ellipse"
switch n := n.(type) {
case *ast.IDTerm:
attrs["label"] += n.Name
case *ast.CaprefTerm:
attrs["label"] += fmt.Sprintf("$%s", n.Name)
}
case *ast.IntLit, *ast.FloatLit, *ast.PatternLit, *ast.StringLit:
attrs["fillcolor"] = "pink"
attrs["shape"] = "ellipse"
switch n := n.(type) {
case *ast.IntLit:
attrs["label"] += fmt.Sprintf("%d", n.I)
case *ast.FloatLit:
attrs["label"] += fmt.Sprintf("%g", n.F)
case *ast.PatternLit:
attrs["label"] += fmt.Sprintf("/%s/", n.Pattern)
case *ast.StringLit:
attrs["label"] += n.Text
}
case *ast.IndexedExpr, *ast.BinaryExpr, *ast.UnaryExpr, *ast.PatternExpr, *ast.BuiltinExpr:
attrs["fillcolor"] = "lightblue"
switch n := n.(type) {
case *ast.BinaryExpr:
attrs["label"] += parser.Kind(n.Op).String()
case *ast.UnaryExpr:
attrs["label"] += parser.Kind(n.Op).String()
case *ast.BuiltinExpr:
attrs["label"] += n.Name
}
}
pos := node.Pos()
if pos != nil {
attrs["xlabel"] = pos.String()
}
fmt.Fprintf(d.w, "n%d [", id)
for k, v := range attrs {
fmt.Fprintf(d.w, "%s=\"%s\" ", k, v)
}
fmt.Fprintf(d.w, "]\n")
}
func (d *dotter) emitLine(src, dst int) {
fmt.Fprintf(d.w, "n%d -> n%d\n", src, dst)
}
func (d *dotter) VisitBefore(node ast.Node) (ast.Visitor, ast.Node) {
id := d.nextID()
d.emitNode(id, node)
if len(d.parentID) > 0 {
parentID := d.parentID[len(d.parentID)-1]
d.emitLine(parentID, id)
}
d.parentID = append(d.parentID, id)
return d, node
}
func (d *dotter) VisitAfter(node ast.Node) ast.Node {
d.parentID = d.parentID[:len(d.parentID)-1]
return node
}
func makeDot(name string, w io.Writer) error {
f, err := os.Open(filepath.Clean(name))
if err != nil {
return err
}
n, err := parser.Parse(name, f)
if err != nil {
return err
}
n, err = checker.Check(n, 0, 0)
if err != nil {
return err
}
fmt.Fprintf(w, "digraph \"%s\" {\n", *prog)
dot := &dotter{w: w}
ast.Walk(dot, n)
fmt.Fprintf(w, "}\n")
return nil
}
func main() {
flag.Parse()
if *prog == "" {
glog.Exitf("No -prog given")
}
if *httpPort == "" {
glog.Exit(makeDot(*prog, os.Stdout))
}
http.HandleFunc("/",
func(w http.ResponseWriter, _ *http.Request) {
dot := exec.Command("dot", "-Tsvg")
in, err := dot.StdinPipe()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
out, err := dot.StdoutPipe()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = dot.Start()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = makeDot(*prog, in)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = in.Close()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Add("Content-type", "image/svg+xml")
w.WriteHeader(http.StatusOK)
_, err = io.Copy(w, out)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
err = dot.Wait()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
http.HandleFunc("/favicon.ico", mtail.FaviconHandler)
glog.Info(http.ListenAndServe(fmt.Sprintf(":%s", *httpPort), nil))
}
|