summaryrefslogtreecommitdiffstats
path: root/backtrace.py
blob: 57e907ed1f7d67370c806173d67435e3e01e09bc (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
'''
Support for parsing strings containing gdb backtraces, turning them into classes
'''
import re

class Frame(object):
    '''
    Class representing a stack frame of a thread within a backtrace
    '''
    def __init__(self, index, address, function, info):
        self.index = index
        self.address = address
        self.function = function
        self.info = info

class Thread(object):
    '''
    Class representing a thread within a backtrace
    '''
    def __init__(self, index, extra):
        self.index = index
        self.extra = extra
        self.frames = {}
        self.framelist = []

class Backtrace(object):
    '''
    Class representing a parsed backtrace
    '''
    def __init__(self, string, debug=False):
        '''
        Parse the given string (from gdb)
        '''
        self._string = string
        #if debug:
        #    print string
        self._crash_site = None
        self._thread = None
        self._threads = {}
        self._frame = None
        for line in string.splitlines():
            if debug:
                print 'GOT LINE: %r' % line
            m = re.match('^Thread ([0-9]+) \(Thread (.*)\):$', line)
            if m:
                if debug:
                    print 'THREAD START:', m.groups()
                self._thread = Thread(int(m.group(1)),
                                      m.group(2))
                self._threads[self._thread.index] = self._thread
                self._frame = None
                continue

            m = re.match('^#([0-9]+)\s+(?:0x([0-9a-f]+) in)? (\S+) (.*)$', line)
            if m:
                if debug:
                    print 'STACK FRAME:', m.groups()
                #print m.groups()
                if m.group(2):
                    address = int(m.group(2), 16)
                else:
                    address = None
                f = Frame(int(m.group(1)),
                          address,
                          m.group(3),
                          m.group(4))

                if self._thread is None:
                    if debug:
                        print 'GOT CRASH SITE'
                    self._crash_site = f
                    self._frame = None
                    continue
                else:
                    self._thread.frames[f.index] = f
                    self._thread.framelist.append(f)
                    self._frame = f
                    continue

            if line.startswith(' '):
                if self._frame:
                    self._frame.info += '\n' + line

        #pprint(self._threads[5].frames[6].__dict__)

    def get_crash_site(self):
        '''Get a (Thread, Frame) pair for where the crash happened (or None)'''
        debug = False
        if debug:
            print self._crash_site.__dict__
        for t in self._threads.values():
            if debug:
                print t
            if 0 in t.frames:
                if debug:
                    print t.frames[0].__dict__
                if t.frames[0].address == self._crash_site.address:
                    return (t, t.frames[0])