File: memdump2dot.py

package info (click to toggle)
python-pyutil 3.3.2-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 884 kB
  • sloc: python: 7,198; makefile: 6
file content (69 lines) | stat: -rwxr-xr-x 1,753 bytes parent folder | download
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
#!/usr/bin/env python
# -*- coding: utf-8-with-signature-unix; fill-column: 77 -*-
# -*- indent-tabs-mode: nil -*-

#  This file is part of pyutil; see README.rst for licensing terms.

import bindann
bindann.install_exception_handler()

import sys

inf = open(sys.argv[1], "r")

outf = open(sys.argv[1]+".dot", "w")
outf.write("digraph %s {\n" % sys.argv[1].replace(".",""))

def parse_netstring(l, i):
    try:
        j = l.find(':', i)
        if j == -1:
            return (None, len(l),)
        lenval = int(l[i:j])
        val = l[j+1:j+1+lenval]
        # skip the comma
        assert l[j+1+lenval] == ","
        return (val, j+1+lenval+1,)
    except Exception as le:
        le.args = tuple(le.args + (l, i,))
        raise

def parse_ref(l, i):
    (attrname, i,) = parse_netstring(l, i)
    j = l.find(",", i)
    assert j != -1
    objid = l[i:j]
    return (objid, attrname, j+1,)

def parse_memdump_line(l):
    result = []

    i = l.find('-')
    objid = l[:i]
    (objdesc, i,) = parse_netstring(l, i+1)

    result.append((objid, objdesc,))

    while i != -1 and i < len(l):
        (objid, attrname, i,) = parse_ref(l, i)
        result.append((objid, attrname,))

    return result

for l in inf:
    if l[-1] != "\n":
        raise "waht the HECK? %r" % l
    res = parse_memdump_line(l.strip())
    # declare the node
    outf.write("\"%s\" [label=\"%s\"];\n" % (res[0][0], res[0][1],))

    # declare all the edges
    for edge in res[1:]:
        if edge[1]:
            # a named edge
            outf.write("\"%s\" -> \"%s\" [style=bold, label=\"%s\"];\n" % (res[0][0], edge[0], edge[1],))
        else:
            # an anonymous edge
            outf.write("\"%s\" -> \"%s\";\n" % (res[0][0], edge[0]))

outf.write("}")