]> git.saurik.com Git - apple/mdnsresponder.git/blob - Platforms/ADK/Thread/adk-mem-parse.py
mDNSResponder-1310.80.1.tar.gz
[apple/mdnsresponder.git] / Platforms / ADK / Thread / adk-mem-parse.py
1 #!/usr/bin/env python3
2 # Copyright (c) 2020 Apple Inc. All rights reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16
17 # This code parses an ADK log with MALLOC_DEBUG_LOGGING and eliminates matching alloc/free pairs.
18 # It then prints a list of all allocations that have not yet been freed, and the file and line
19 # number where they were allocated. This can be useful for detecting leaks, although not everything
20 # printed will be a leak: it could just be live data.
21
22 import sys
23 import re
24
25 allocations = {}
26
27 alloc_re = re.compile(r"^([0-9][0-9]*\.[0-9][0-9]*)\s+[A-Za-z][A-Za-z]*\s+(0x[0-9a-f][0-9a-f]*): (malloc|strdup|calloc)\((.*)\) at (.*)$")
28 free_re = re.compile(r"^([0-9][0-9]*\.[0-9][0-9]*)\s+[A-Za-z][A-Za-z]*\s+(0x[0-9a-f][0-9a-f]*): (free)\((.*)\) at (.*)$")
29
30 for line in sys.stdin:
31 line = line.strip()
32 matches = alloc_re.match(line)
33 if matches != None:
34 allocations[matches.group(2)] = matches
35 else:
36 matches = free_re.match(line)
37 if matches != None:
38 if matches.group(2) in allocations:
39 del allocations[matches.group(2)]
40 else:
41 print("mismatched free: ", line);
42
43 leaks = {}
44 for key, value in allocations.items():
45 if value.group(5) in leaks:
46 leaks[value.group(5)].append(value)
47 else:
48 leaks[value.group(5)] = [value]
49
50 for key in sorted(leaks.keys()):
51 print("\nPossible leaks at: ", key)
52 for match in leaks[key]:
53 print(" ", match.group(2), " ", match.group(3), " ", match.group(4))