-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnav.go
230 lines (202 loc) · 5.62 KB
/
nav.go
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
* Copyright (c) 2022 Red Hat, Inc.
* SPDX-License-Identifier: GPL-2.0-or-later
*/
package main
import (
"bytes"
"compress/gzip"
"encoding/base64"
"encoding/binary"
"errors"
"fmt"
"nav/config"
c "nav/constants"
"github.com/goccy/go-graphviz"
"os"
"strings"
)
const jsonOutputFMT string = "{\"graph\": \"%s\",\"graph_type\":\"%s\",\"symbols\": [%s]}"
var fmtDot = []string{
"",
"\"%s\"->\"%s\" \n",
"\\\"%s\\\"->\\\"%s\\\" \\\\\\n",
"\"%s\"->\"%s\" \n",
"\"%s\"->\"%s\" \n",
}
var fmtDotHeader = []string{
"",
"digraph G {\nrankdir=\"LR\"\n",
"digraph G {\\\\\\nrankdir=\"LR\"\\\\\\n",
"digraph G {\nrankdir=\"LR\"\n",
"digraph G {\nrankdir=\"LR\"\n",
}
var fmtDotNodeHighlightWSymb = "\"%[1]s\" [shape=record style=\"rounded,filled,bold\" fillcolor=yellow label=\"%[1]s|%[2]s\"]\n"
var fmtDotNodeHighlightWoSymb = "\"%[1]s\" [shape=record style=\"rounded,filled,bold\" fillcolor=yellow label=\"%[1]s\"]\n"
func opt2num(s string) int {
var opt = map[string]int{
"graphOnly": c.GraphOnly,
"jsonOutputPlain": c.JsonOutputPlain,
"jsonOutputB64": c.JsonOutputB64,
"jsonOutputGZB64": c.JsonOutputGZB64,
}
val, ok := opt[s]
if !ok {
return 0
}
return val
}
func decorateLine(l string, r string, adjm []adjM) string {
var res = " [label=\""
for _, item := range adjm {
if (item.l.subsys == l) && (item.r.subsys == r) {
tmp := fmt.Sprintf("%s([%s]%s),\\n", item.r.symbol, item.r.addressRef, item.r.sourceRef)
if !strings.Contains(res, tmp) {
res += fmt.Sprintf("%s([%s]%s),\\n", item.r.symbol, item.r.addressRef, item.r.sourceRef)
}
}
}
res += "\"]"
return res
}
func decorate(dotStr string, adjm []adjM) string {
var res string
dotBody := strings.Split(dotStr, "\n")
for i, line := range dotBody {
split := strings.Split(line, "->")
if len(split) == 2 {
res = res + dotBody[i] + decorateLine(strings.TrimSpace(strings.ReplaceAll(split[0], "\"", "")), strings.TrimSpace(strings.ReplaceAll(split[1], "\"", "")), adjm) + "\n"
}
}
return res
}
func do_graphviz(dot string, output_type c.OutIMode) error{
var buf bytes.Buffer
var format graphviz.Format
switch output_type {
case c.OPNG:
format=graphviz.PNG
case c.OJPG:
format=graphviz.JPG
case c.OSVG:
format=graphviz.SVG
default:
return errors.New("Unknown format")
}
graph, _ := graphviz.ParseBytes([]byte(dot))
g := graphviz.New()
defer func() {
if err := graph.Close(); err != nil {
panic(err)
}
g.Close()
}()
g.Render(graph, format, &buf)
binary.Write(os.Stdout, binary.LittleEndian, buf.Bytes())
return nil
}
func generateOutput(d Datasource, cfg *config.Config) (string, error) {
var graphOutput string
var jsonOutput string
var prod = map[string]int{}
var visited []int
var entryName string
var output string
var adjm []adjM
conf := cfg.ConfValues
start, err := d.sym2num(conf.Symbol, conf.DBInstance)
if err != nil {
fmt.Println("Symbol not found")
return "", err
}
graphOutput = fmtDotHeader[opt2num(conf.Type)]
entry, err := d.getEntryById(start, conf.DBInstance)
if err != nil {
return "", err
} else {
entryName = entry.symbol
}
startSubsys, _ := d.getSubsysFromSymbolName(entryName, conf.DBInstance)
if startSubsys == "" {
startSubsys = SUBSYS_UNDEF
}
if (conf.Mode == c.PrintTargeted) && len(conf.TargetSubsys) == 0 {
targSubsysTmp, err := d.getSubsysFromSymbolName(conf.Symbol, conf.DBInstance)
if err != nil {
panic(err)
}
conf.TargetSubsys = append(conf.TargetSubsys, targSubsysTmp)
}
navigate(d, start, node{startSubsys, entryName, "entry point", "0x0"}, conf.TargetSubsys, &visited, &adjm, prod, conf.DBInstance, conf.Mode, conf.ExcludedAfter, conf.ExcludedBefore, 0, conf.MaxDepth, fmtDot[opt2num(conf.Type)], &output)
if (conf.Mode == c.PrintSubsysWs) || (conf.Mode == c.PrintTargeted) {
output = decorate(output, adjm)
}
graphOutput += output
if conf.Mode == c.PrintTargeted {
for _, i := range conf.TargetSubsys {
if d.GetExploredSubsystemByName(conf.Symbol) == i {
graphOutput += fmt.Sprintf(fmtDotNodeHighlightWSymb, i, conf.Symbol)
} else {
graphOutput += fmt.Sprintf(fmtDotNodeHighlightWoSymb, i)
}
}
}
graphOutput += "}"
symbdata, err := d.symbSubsys(visited, conf.DBInstance)
if err != nil {
return "", err
}
switch opt2num(conf.Type) {
case c.GraphOnly:
jsonOutput = graphOutput
case c.JsonOutputPlain:
jsonOutput = fmt.Sprintf(jsonOutputFMT, graphOutput, conf.Type, symbdata)
case c.JsonOutputB64:
b64dot := base64.StdEncoding.EncodeToString([]byte(graphOutput))
jsonOutput = fmt.Sprintf(jsonOutputFMT, b64dot, conf.Type, symbdata)
case c.JsonOutputGZB64:
var b bytes.Buffer
gz := gzip.NewWriter(&b)
if _, err := gz.Write([]byte(graphOutput)); err != nil {
return "", errors.New("gzip failed")
}
if err := gz.Close(); err != nil {
return "", errors.New("gzip failed")
}
b64dot := base64.StdEncoding.EncodeToString(b.Bytes())
jsonOutput = fmt.Sprintf(jsonOutputFMT, b64dot, conf.Type, symbdata)
default:
return "", errors.New("unknown output mode")
}
return jsonOutput, nil
}
func main() {
conf, err := config.New()
if err != nil {
fmt.Println(err)
os.Exit(c.OSExitError)
}
if opt2num(conf.ConfValues.Type) == 0 {
fmt.Printf("Unknown mode %s\n", conf.ConfValues.Type)
os.Exit(-2)
}
t := connectToken{conf.ConfValues.DBDriver, conf.ConfValues.DBDSN}
d := &SqlDB{}
err = d.init(&t)
if err != nil {
panic(err)
}
output, err := generateOutput(d, conf)
if err != nil {
fmt.Println("Internal error", err)
os.Exit(-3)
}
if conf.ConfValues.Graphviz != c.OText {
err = do_graphviz(output, conf.ConfValues.Graphviz);
if err != nil {
fmt.Println(err.Error())
}
} else {
fmt.Println(output)
}
}