Bug 1697394 [wpt PR 27904] - CSP: Add WPT for javascript URL inheritance, a=testonly
[gecko.git] / tools / rb / find_leakers.py
blob0359d6fefb5b842c14be087d0f295a764be84e12
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 from __future__ import absolute_import, print_function
14 import sys
16 import six
19 def print_output(allocation, obj_to_class):
20 """Formats and prints output."""
21 items = []
22 for (
23 obj,
24 count,
25 ) in six.iteritems(allocation):
26 # Adding items to a list, so we can sort them.
27 items.append((obj, count))
28 # Sorting by count.
29 items.sort(key=lambda item: item[1])
31 for (
32 obj,
33 count,
34 ) in items:
35 print(
36 "{obj} ({count}) @ {class_name}".format(
37 obj=obj, count=count, class_name=obj_to_class[obj]
42 def process_log(log_lines):
43 """Process through the log lines, and print out the result.
45 @param log_lines: List of strings.
46 """
47 allocation = {}
48 class_count = {}
49 obj_to_class = {}
51 for log_line in log_lines:
52 if not log_line.startswith("<"):
53 continue
55 (class_name, obj, ignore, operation, count,) = log_line.strip("\r\n").split(
56 " "
57 )[:5]
59 # for AddRef/Release `count' is the refcount,
60 # for Ctor/Dtor it's the size.
62 if (operation == "AddRef" and count == "1") or operation == "Ctor":
63 # Examples:
64 # <nsStringBuffer> 0x01AFD3B8 1 AddRef 1
65 # <PStreamNotifyParent> 0x08880BD0 8 Ctor (20)
66 class_count[class_name] = class_count.setdefault(class_name, 0) + 1
67 allocation[obj] = class_count[class_name]
68 obj_to_class[obj] = class_name
70 elif (operation == "Release" and count == "0") or operation == "Dtor":
71 # Examples:
72 # <nsStringBuffer> 0x01AFD3B8 1 Release 0
73 # <PStreamNotifyParent> 0x08880BD0 8 Dtor (20)
74 if obj not in allocation:
75 print(
76 "An object was released that wasn't allocated!",
78 print(obj, "@", class_name)
79 else:
80 allocation.pop(obj)
81 obj_to_class.pop(obj)
83 # Printing out the result.
84 print_output(allocation, obj_to_class)
87 def print_usage():
88 print("")
89 print("Usage: find-leakers.py [log-file]")
90 print("")
91 print("If `log-file' provided, it will read that as the input log.")
92 print("Else, it will read the stdin as the input log.")
93 print("")
96 def main():
97 """Main method of the script."""
98 if len(sys.argv) == 1:
99 # Reading log from stdin.
100 process_log(sys.stdin.readlines())
101 elif len(sys.argv) == 2:
102 # Reading log from file.
103 with open(sys.argv[1], "r") as log_file:
104 log_lines = log_file.readlines()
105 process_log(log_lines)
106 else:
107 print("ERROR: Invalid number of arguments")
108 print_usage()
111 if __name__ == "__main__":
112 main()