]>
The Tcpdump Group git mirrors - libpcap/blob - testprogs/visopts.py
4 This program parses the output from pcap_compile() to visualize the CFG after
8 1. Enable optimizier debugging code when configure libpcap,
9 and build libpcap & the test programs
10 ./configure --enable-optimizer-dbg
13 2. Run filtertest to compile BPF expression and produce the CFG as a
14 DOT graph, save to output a.txt
15 testprogs/filtertest -g EN10MB host 192.168.1.1 > a.txt
16 3. Send a.txt to this program's standard input
17 cat a.txt | testprogs/visopts.py
18 (Graphviz must be installed)
19 4. Step 2&3 can be merged:
20 testprogs/filtertest -g EN10MB host 192.168.1.1 | testprogs/visopts.py
21 5. The standard output is something like this:
22 generated files under directory: /tmp/visopts-W9ekBw
23 the directory will be removed when this programs finished.
24 open this link: http://localhost:39062/expr1.html
25 6. Open the URL at the 3rd line in a browser.
28 1. The CFG is translated to SVG images, expr1.html embeds them as external
29 documents. If you open expr1.html as local file using file:// protocol, some
30 browsers will deny such requests so the web page will not work properly.
31 For Chrome, you can run it using the following command to avoid this:
32 chromium --disable-web-security
33 That's why this program starts a localhost HTTP server.
34 2. expr1.html uses jQuery from https://ajax.googleapis.com, so it needs Internet
43 html_template
= string
.Template("""
46 <title>BPF compiler optimization phases for $expr </title>
47 <style type="text/css">
49 /* half width container */
50 display: inline-block;
56 <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"/></script>
57 <!--script type="text/javascript" src="./jquery.min.js"/></script-->
58 <script type="text/javascript">
62 var logs = JSON.parse('$logs');
68 function gurl(index) {
76 return "./expr" + exprid + "_g" + s + ".svg"
79 function annotate_svgs() {
80 if (!leftsvg || !rightsvg) return;
82 $$.each([$$(leftsvg), $$(rightsvg)], function() {
83 $$(this).find("[id|='block'][opacity]").each(function() {
84 $$(this).removeAttr('opacity');
88 $$(leftsvg).find("[id|='block']").each(function() {
89 var has = $$(rightsvg).find("#" + this.id).length != 0;
90 if (!has) $$(this).attr("opacity", "0.4");
92 $$(this).click(function() {
93 var target = $$(rightsvg).find("#" + this.id);
94 var offset = $$("#rightsvgc").offset().top + target.position().top;
95 window.scrollTo(0, offset);
100 $$(rightsvg).find("[id|='block']").each(function() {
101 var has = $$(leftsvg).find("#" + this.id).length != 0;
102 if (!has) $$(this).attr("opacity", "0.4");
104 $$(this).click(function() {
105 var target = $$(leftsvg).find("#" + this.id);
106 var offset = $$("#leftsvgc").offset().top + target.position().top;
107 window.scrollTo(0, offset);
114 function init_svgroot(svg) {
115 svg.setAttribute("width", "100%");
116 svg.setAttribute("height", "100%");
118 function wait_leftsvg() {
120 var doc = document.getElementById("leftsvgc").getSVGDocument();
122 setTimeout(wait_leftsvg, 500);
125 leftsvg = doc.documentElement;
126 //console.log(leftsvg);
128 init_svgroot(leftsvg);
131 function wait_rightsvg() {
132 if (rightsvg) return;
133 var doc = document.getElementById("rightsvgc").getSVGDocument();
135 setTimeout(wait_rightsvg, 500);
138 rightsvg = doc.documentElement;
139 //console.log(rightsvg);
141 init_svgroot(rightsvg);
144 function load_left(index) {
145 var url = gurl(index);
146 var frag = "<embed id='leftsvgc' type='image/svg+xml' pluginspage='https://www.adobe.com/svg/viewer/install/' src='" + url + "'/>";
147 $$("#lsvg").html(frag);
148 $$("#lcomment").html(logs[index]);
149 $$("#lsvglink").attr("href", url);
153 function load_right(index) {
154 var url = gurl(index);
155 var frag = "<embed id='rightsvgc' type='image/svg+xml' pluginspage='https://www.adobe.com/svg/viewer/install/' src='" + url + "'/>";
156 $$("#rsvg").html(frag);
157 $$("#rcomment").html(logs[index]);
158 $$("#rsvglink").attr("href", url);
163 $$(document).ready(function() {
164 for (var i = 0; i < gcount; i++) {
165 var opt = "<option value='" + i + "'>loop" + i + " -- " + logs[i] + "</option>";
166 $$("#lselect").append(opt);
167 $$("#rselect").append(opt);
169 var on_selected = function() {
170 var index = parseInt($$(this).children("option:selected").val());
171 if (this.id == "lselect")
176 $$("#lselect").change(on_selected);
177 $$("#rselect").change(on_selected);
179 $$("#backward").click(function() {
180 var index = parseInt($$("#lselect option:selected").val());
181 if (index <= 0) return;
182 $$("#lselect").val(index - 1).change();
183 $$("#rselect").val(index).change();
185 $$("#forward").click(function() {
186 var index = parseInt($$("#rselect option:selected").val());
187 if (index >= gcount - 1) return;
188 $$("#lselect").val(index).change();
189 $$("#rselect").val(index + 1).change();
192 if (gcount >= 1) $$("#lselect").val(0).change();
193 if (gcount >= 2) $$("#rselect").val(1).change();
197 <body style="width: 96%">
200 <div style="text-align: center;">
201 <button id="backward" type="button"><<</button>
202
203 <button id="forward" type="button">>></button>
207 <div style="clear: both;">
209 <select id="lselect"></select>
210 <a id="lsvglink" target="_blank">open this svg in browser</a>
211 <p id="lcomment"></p>
214 <select id="rselect"></select>
215 <a id="rsvglink" target="_blank">open this svg in browser</a>
216 <p id="rcomment"></p>
220 <div style="clear: both;">
221 <div id="lsvg" class="hc lc"></div>
222 <div id="rsvg" class="hc rc"></div>
228 def write_html(expr
, gcount
, logs
):
229 logs
= map(lambda s
: s
.strip().replace("\n", "<br/>"), logs
)
232 html
= html_template
.safe_substitute(expr
=expr
.encode("string-escape"), gcount
=gcount
, logs
=json
.dumps(logs
).encode("string-escape"))
233 with
file("expr1.html", "wt") as f
:
236 def render_on_html(infile
):
245 if line
.startswith("machine codes for filter:"):
246 expr
= line
[len("machine codes for filter:"):].strip()
248 elif line
.startswith("digraph BPF {"):
253 if line
.startswith("}"):
260 p
= subprocess
.Popen(['dot', '-Tsvg'], stdin
=subprocess
.PIPE
, stdout
=subprocess
.PIPE
)
261 except OSError as ose
:
262 print "Failed to run 'dot':", ose
263 print "(Is Graphviz installed?)"
266 svg
= p
.communicate(dot
)[0]
267 with
file("expr1_g%03d.svg" % (gid
), "wt") as f
:
277 #unterminated dot graph for expression
280 # BPF parser encounter error(s)
282 write_html(expr
, gid
- 1, logs
)
286 import SimpleHTTPServer
289 class MySocketServer(SocketServer
.TCPServer
):
290 allow_reuse_address
= True
291 Handler
= SimpleHTTPServer
.SimpleHTTPRequestHandler
292 httpd
= MySocketServer(("localhost", 0), Handler
)
293 print "open this link: http://localhost:%d/expr1.html" % (httpd
.server_address
[1])
295 httpd
.serve_forever()
296 except KeyboardInterrupt as e
:
303 os
.chdir(tempfile
.mkdtemp(prefix
="visopts-"))
304 atexit
.register(shutil
.rmtree
, os
.getcwd())
305 print "generated files under directory: %s" % os
.getcwd()
306 print " the directory will be removed when this program has finished."
308 if not render_on_html(sys
.stdin
):
313 if __name__
== "__main__":
314 if '-h' in sys
.argv
or '--help' in sys
.argv
: