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.
17 def print_output(allocation
, obj_to_class
):
18 """Formats and prints output."""
23 ) in six
.iteritems(allocation
):
24 # Adding items to a list, so we can sort them.
25 items
.append((obj
, count
))
27 items
.sort(key
=lambda item
: item
[1])
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.
49 for log_line
in log_lines
:
50 if not log_line
.startswith("<"):
59 ) = log_line
.strip("\r\n").split(
63 # for AddRef/Release `count' is the refcount,
64 # for Ctor/Dtor it's the size.
66 if (operation
== "AddRef" and count
== "1") or operation
== "Ctor":
68 # <nsStringBuffer> 0x01AFD3B8 1 AddRef 1
69 # <PStreamNotifyParent> 0x08880BD0 8 Ctor (20)
70 class_count
[class_name
] = class_count
.setdefault(class_name
, 0) + 1
71 allocation
[obj
] = class_count
[class_name
]
72 obj_to_class
[obj
] = class_name
74 elif (operation
== "Release" and count
== "0") or operation
== "Dtor":
76 # <nsStringBuffer> 0x01AFD3B8 1 Release 0
77 # <PStreamNotifyParent> 0x08880BD0 8 Dtor (20)
78 if obj
not in allocation
:
80 "An object was released that wasn't allocated!",
82 print(obj
, "@", class_name
)
87 # Printing out the result.
88 print_output(allocation
, obj_to_class
)
93 print("Usage: find-leakers.py [log-file]")
95 print("If `log-file' provided, it will read that as the input log.")
96 print("Else, it will read the stdin as the input log.")
101 """Main method of the script."""
102 if len(sys
.argv
) == 1:
103 # Reading log from stdin.
104 process_log(sys
.stdin
.readlines())
105 elif len(sys
.argv
) == 2:
106 # Reading log from file.
107 with
open(sys
.argv
[1], "r") as log_file
:
108 log_lines
= log_file
.readlines()
109 process_log(log_lines
)
111 print("ERROR: Invalid number of arguments")
115 if __name__
== "__main__":