Bug 1829125 - Align the PHC area to the jemalloc chunk size r=glandium
[gecko.git] / tools / rb / find_leakers.py
blob9e9a37ac69a9f0c5c8b24b295c0da9e392848704
1 #!/usr/bin/python
3 # This Source Code Form is subject to the terms of the Mozilla Public
4 # License, v. 2.0. If a copy of the MPL was not distributed with this
5 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 # This script processes a `refcount' log, and finds out if any object leaked.
8 # It simply goes through the log, finds `AddRef' or `Ctor' lines, and then
9 # sees if they `Release' or `Dtor'. If not, it reports them as leaks.
10 # Please see README file in the same directory.
12 import sys
14 import six
17 def print_output(allocation, obj_to_class):
18 """Formats and prints output."""
19 items = []
20 for (
21 obj,
22 count,
23 ) in six.iteritems(allocation):
24 # Adding items to a list, so we can sort them.
25 items.append((obj, count))
26 # Sorting by count.
27 items.sort(key=lambda item: item[1])
29 for (
30 obj,
31 count,
32 ) in items:
33 print(
34 "{obj} ({count}) @ {class_name}".format(
35 obj=obj, count=count, class_name=obj_to_class[obj]
40 def process_log(log_lines):
41 """Process through the log lines, and print out the result.
43 @param log_lines: List of strings.
44 """
45 allocation = {}
46 class_count = {}
47 obj_to_class = {}
49 for log_line in log_lines:
50 if not log_line.startswith("<"):
51 continue
53 (class_name, obj, ignore, operation, count,) = log_line.strip("\r\n").split(
54 " "
55 )[:5]
57 # for AddRef/Release `count' is the refcount,
58 # for Ctor/Dtor it's the size.
60 if (operation == "AddRef" and count == "1") or operation == "Ctor":
61 # Examples:
62 # <nsStringBuffer> 0x01AFD3B8 1 AddRef 1
63 # <PStreamNotifyParent> 0x08880BD0 8 Ctor (20)
64 class_count[class_name] = class_count.setdefault(class_name, 0) + 1
65 allocation[obj] = class_count[class_name]
66 obj_to_class[obj] = class_name
68 elif (operation == "Release" and count == "0") or operation == "Dtor":
69 # Examples:
70 # <nsStringBuffer> 0x01AFD3B8 1 Release 0
71 # <PStreamNotifyParent> 0x08880BD0 8 Dtor (20)
72 if obj not in allocation:
73 print(
74 "An object was released that wasn't allocated!",
76 print(obj, "@", class_name)
77 else:
78 allocation.pop(obj)
79 obj_to_class.pop(obj)
81 # Printing out the result.
82 print_output(allocation, obj_to_class)
85 def print_usage():
86 print("")
87 print("Usage: find-leakers.py [log-file]")
88 print("")
89 print("If `log-file' provided, it will read that as the input log.")
90 print("Else, it will read the stdin as the input log.")
91 print("")
94 def main():
95 """Main method of the script."""
96 if len(sys.argv) == 1:
97 # Reading log from stdin.
98 process_log(sys.stdin.readlines())
99 elif len(sys.argv) == 2:
100 # Reading log from file.
101 with open(sys.argv[1], "r") as log_file:
102 log_lines = log_file.readlines()
103 process_log(log_lines)
104 else:
105 print("ERROR: Invalid number of arguments")
106 print_usage()
109 if __name__ == "__main__":
110 main()