summaryrefslogtreecommitdiff
path: root/contrib/scripts/refcounter.py
blob: 1f4b375170d147ceeee02c0b6bb6f60b1002216a (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
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
231
232
233
234
235
236
237
#!/usr/bin/env python
"""Process a ref debug log

 This file will process a log file created by enabling
 the refdebug config option in asterisk.conf.

 See http://www.asterisk.org for more information about
 the Asterisk project. Please do not directly contact
 any of the maintainers of this project for assistance;
 the project provides a web site, mailing lists and IRC
 channels for your use.

 This program is free software, distributed under the terms of
 the GNU General Public License Version 2. See the LICENSE file
 at the top of the source tree.

 Copyright (C) 2014, Digium, Inc.
 Matt Jordan <mjordan@digium.com>
"""

import sys
import os

from optparse import OptionParser


def parse_line(line):
    """Parse out a line into its constituent parts.

    Keyword Arguments:
    line The line from a ref debug log to parse out

    Returns:
    A dictionary containing the options, or None
    """
    tokens = line.strip().split(',', 7)
    if len(tokens) < 8:
        print "ERROR: ref debug line '%s' contains fewer tokens than " \
              "expected: %d" % (line.strip(), len(tokens))
        return None

    processed_line = {'addr': tokens[0],
                      'delta': tokens[1],
                      'thread_id': tokens[2],
                      'file': tokens[3],
                      'line': tokens[4],
                      'function': tokens[5],
                      'state': tokens[6],
                      'tag': tokens[7],
                      }
    return processed_line


def process_file(options):
    """The routine that kicks off processing a ref file

    Keyword Arguments:
    filename The full path to the file to process

    Returns:
    A tuple containing:
        - A list of objects whose lifetimes were completed
            (i.e., finished objects)
        - A list of objects referenced after destruction
            (i.e., invalid objects)
        - A list of objects whose lifetimes were not completed
            (i.e., leaked objects)
        - A list of objects whose lifetimes are skewed
            (i.e., Object history starting with an unusual ref count)
    """

    finished_objects = []
    invalid_objects = []
    leaked_objects = []
    skewed_objects = []
    current_objects = {}
    filename = options.filepath

    with open(filename, 'r') as ref_file:
        for line in ref_file:
            parsed_line = parse_line(line)
            if not parsed_line:
                continue

            invalid = False
            obj = parsed_line['addr']

            if obj not in current_objects:
                current_objects[obj] = {'log': [], 'curcount': 1}
                if 'constructor' in parsed_line['state']:
                    # This is the normal expected case
                    pass
                elif 'invalid' in parsed_line['state']:
                    invalid = True
                    current_objects[obj]['curcount'] = 0
                    if options.invalid:
                        invalid_objects.append((obj, current_objects[obj]))
                elif 'destructor' in parsed_line['state']:
                    current_objects[obj]['curcount'] = 0
                    if options.skewed:
                        skewed_objects.append((obj, current_objects[obj]))
                else:
                    current_objects[obj]['curcount'] = int(
                        parsed_line['state'])
                    if options.skewed:
                        skewed_objects.append((obj, current_objects[obj]))
            else:
                current_objects[obj]['curcount'] += int(parsed_line['delta'])

            current_objects[obj]['log'].append(
                "[%s] %s:%s %s: %s %s - [%s]" % (
                    parsed_line['thread_id'],
                    parsed_line['file'],
                    parsed_line['line'],
                    parsed_line['function'],
                    parsed_line['delta'],
                    parsed_line['tag'],
                    parsed_line['state']))

            # It is possible for curcount to go below zero if someone
            # unrefs an object by two or more when there aren't that
            # many refs remaining.  This condition abnormally finishes
            # the object.
            if current_objects[obj]['curcount'] <= 0:
                if current_objects[obj]['curcount'] < 0:
                    current_objects[obj]['log'].append(
                        "[%s] %s:%s %s: %s %s - [%s]" % (
                            parsed_line['thread_id'],
                            parsed_line['file'],
                            parsed_line['line'],
                            parsed_line['function'],
                            "+0",
                            "Object abnormally finalized",
                            "**implied destructor**"))
                    # Highlight the abnormally finished object in the
                    # invalid section as well as reporting it in the normal
                    # finished section.
                    if options.invalid:
                        invalid_objects.append((obj, current_objects[obj]))
                if not invalid and options.normal:
                    finished_objects.append((obj, current_objects[obj]))
                del current_objects[obj]

    if options.leaks:
        for key, lines in current_objects.iteritems():
            leaked_objects.append((key, lines))
    return (finished_objects, invalid_objects, leaked_objects, skewed_objects)


def print_objects(objects, prefix=""):
    """Prints out the objects that were processed

    Keyword Arguments:
    objects A list of objects to print
    prefix  A prefix to print that specifies something about
            this object
    """

    print "======== %s Objects ========" % prefix
    print "\n"
    for obj in objects:
        print "==== %s Object %s history ====" % (prefix, obj[0])
        for line in obj[1]['log']:
            print line
        print "\n"


def main(argv=None):
    """Main entry point for the script"""

    ret_code = 0

    if argv is None:
        argv = sys.argv

    parser = OptionParser()

    parser.add_option("-f", "--file", action="store", type="string",
                      dest="filepath", default="/var/log/asterisk/refs",
                      help="The full path to the refs file to process")
    parser.add_option("-i", "--suppress-invalid", action="store_false",
                      dest="invalid", default=True,
                      help="If specified, don't output invalid object "
                           "references")
    parser.add_option("-l", "--suppress-leaks", action="store_false",
                      dest="leaks", default=True,
                      help="If specified, don't output leaked objects")
    parser.add_option("-n", "--suppress-normal", action="store_false",
                      dest="normal", default=True,
                      help="If specified, don't output objects with a "
                           "complete lifetime")
    parser.add_option("-s", "--suppress-skewed", action="store_false",
                      dest="skewed", default=True,
                      help="If specified, don't output objects with a "
                           "skewed lifetime")

    (options, args) = parser.parse_args(argv)

    if not options.invalid and not options.leaks and not options.normal \
            and not options.skewed:
        print >>sys.stderr, "All options disabled"
        return -1

    if not os.path.isfile(options.filepath):
        print >>sys.stderr, "File not found: %s" % options.filepath
        return -1

    try:
        (finished_objects,
         invalid_objects,
         leaked_objects,
         skewed_objects) = process_file(options)

        if options.invalid and len(invalid_objects):
            print_objects(invalid_objects, "Invalid Referenced")
            ret_code |= 4

        if options.leaks and len(leaked_objects):
            print_objects(leaked_objects, "Leaked")
            ret_code |= 1

        if options.skewed and len(skewed_objects):
            print_objects(skewed_objects, "Skewed")
            ret_code |= 2

        if options.normal:
            print_objects(finished_objects, "Finalized")

    except (KeyboardInterrupt, SystemExit, IOError):
        print >>sys.stderr, "File processing cancelled"
        return -1

    return ret_code


if __name__ == "__main__":
    sys.exit(main(sys.argv))