summaryrefslogtreecommitdiffstats
path: root/gdb-bt-reformat
blob: 6bb31cb899920afef02d9b15c1ad2cd2401c3a75 (plain)
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
#!/usr/bin/env python

__author__ = "Jan Pokorny <jpokorny at redhat dot com>"
__license__ = "GPLv2"

from sys import stdin, stderr
import re
import textwrap

DEF_IDENT = '[A-Za-z_][A-Za-z0-9_]*'
DEF_UNKNOWN = '[?]{2}'
DEF_HEX = '0x[0-9A-Fa-f]+'
DEF_NUM = '[+-]?(?:[0-9]+(?:[.][0-9]*)?|[.][0-9]+)(?:[eE][0-9]+)?'
DEF_MSG = '[<][ A-Za-z0-9_]+[>]'
DEF_STR = '["][ A-Za-z0-9_]*["]'
DEF_PATH = '[A-Za-z0-9._/-]+'
RE_BT = re.compile(
    '(?P<init>^\#[0-9]+\s+)'
    '(?:(?P<hex>%(DEF_HEX)s) in )?'
    '(?P<where>%(DEF_IDENT)s(?:(?: |::)%(DEF_IDENT)s)*|(?P<unknown>%(DEF_UNKNOWN)s))'
    ' (?P<args>[(](?:(?<=[( ])%(DEF_IDENT)s=(?:%(DEF_IDENT)s|%(DEF_MSG)s|%(DEF_HEX)s|%(DEF_NUM)s|%(DEF_STR)s)(?:, )?)*[)])'
    '(?(unknown)| at (?P<path>%(DEF_PATH)s):(?P<line>[1-9][0-9]*))'
    % locals())


class BtLineError(Exception):
    pass


class BtLine(object):
    def __init__(self, line, space=None):
        self._space = space
        self._line = line
        self._found = RE_BT.search(self._line)
        if self._found:
            for k, v in self._found.groupdict().iteritems():
                #print "\t{0}: {1}".format(k, v)
                setattr(self, k, v)
        else:
            raise BtLineError

    def __str__(self):
        if self._found:
            space = '\n' + self.get_space()
            args = textwrap.wrap(self.args, subsequent_indent=self._space)
            res = self.init + self.where
            res += (args[0] == '()' and '()' or space + '\n'.join(args))
            if not self.unknown:
                res += space + 'at ' + self.path + ':' + self.line
            return res

    def get_space(self):
            if not self._space:
                self._space = ' ' * len(self.init)
            return self._space


def main():
    space = None
    line = '__dummy__'
    while line:
        line = stdin.readline()
        try:
            btline = BtLine(line, space)
        except BtLineError:
            space = None
            #print >>stderr, "bad line:", line
            print line
        else:
            print str(btline) + '\n'
            space = space or btline.get_space()


if __name__ == '__main__':
    main()