source: rtems/cpukit/libdl/rtl-alloc-check.py @ a50839ff

Last change on this file since a50839ff was a50839ff, checked in by Joel Sherrill <joel@…>, on 02/28/22 at 23:07:06

cpukit/libdl/rtl-alloc-check.py: Change to BSD-2 by hand

Updates #3053.

  • Property mode set to 100644
File size: 4.3 KB
Line 
1# SPDX-License-Identifier: BSD-2-Clause
2
3#
4# Check the allocations for libdl:
5#
6#  1. Turn on the allocation trace.
7#
8#  2. Load and unload object files.
9#
10#  3. Capture the trace output and feed to this tool
11
12# Copyright (c) 2019 Chris Johns <chrisj@rtems.org>.
13# All rights reserved.
14#
15# Redistribution and use in source and binary forms, with or without
16# modification, are permitted provided that the following conditions
17# are met:
18# 1. Redistributions of source code must retain the above copyright
19#    notice, this list of conditions and the following disclaimer.
20# 2. Redistributions in binary form must reproduce the above copyright
21#    notice, this list of conditions and the following disclaimer in the
22#    documentation and/or other materials provided with the distribution.
23#
24# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34# POSSIBILITY OF SUCH DAMAGE.
35
36from __future__ import print_function
37
38import argparse
39
40
41class libdl_trace(object):
42    def __init__(self, name):
43        self.name = name
44        self.trace = {'alloc': []}
45
46    def load(self):
47        with open(self.name, 'r') as f:
48            lc = 0
49            for line in f:
50                line = line[:-1]
51                lc += 1
52                if line.startswith('rtl: '):
53                    if line.startswith('rtl: alloc: '):
54                        self.trace['alloc'] += [(lc, line)]
55
56    def check_allocs(self):
57        allocs = {}
58        locks = 0
59        wr_enable = False
60        for lc, line in self.trace['alloc']:
61            ls = line.split(' ')
62            if len(ls) > 3:
63                if ls[2] == 'new:':
64                    addr = ls[4].split('=')[1]
65                    size = ls[5].split('=')[1]
66                    count = 0
67                    if addr in allocs:
68                        alc, alloced, asize, count = allocs[addr]
69                        if alloced:
70                            print(
71                                '%5d: already alloced: %5d: addr=%-9s size=%-9s count=%d'
72                                % (lc, alc, addr, asize, count))
73                    allocs[addr] = (lc, True, size, count + 1)
74                elif ls[2] == 'del:':
75                    addr = ls[4].split('=')[1]
76                    if addr != '0':
77                        if addr not in allocs:
78                            print('%5d: delete never alloced: addr=%s' %
79                                  (lc, addr))
80                        else:
81                            alc, alloced, size, count = allocs[addr]
82                            if not alloced:
83                                print(
84                                    '%5d: delete not alloced: %5d: addr=%-9s size=%-9s count=%d'
85                                    % (lc, alc, addr, size, count))
86                        allocs[addr] = (lc, False, size, count)
87        alloced_remaiing = 0
88        addresses = sorted(list(allocs.keys()))
89        for addr in addresses:
90            lc, alloced, size, count = allocs[addr]
91            if alloced:
92                print('%5d: never deleted: addr=%-9s size=%-9s count=%d' %
93                      (lc, addr, size, count))
94                alloced_remaiing += int(size)
95        if alloced_remaiing != 0:
96            print("Amount alloced: %d" % (alloced_remaiing))
97
98
99def run(args):
100    argsp = argparse.ArgumentParser(prog='rtl-alloc-check',
101                                    description='Audit libdl allocations')
102    argsp.add_argument('traces', help='libdl trace files', nargs='+')
103
104    opts = argsp.parse_args(args[1:])
105
106    for t in opts.traces:
107        trace = libdl_trace(t)
108        trace.load()
109        trace.check_allocs()
110
111
112if __name__ == "__main__":
113    import sys
114    run(sys.argv)
Note: See TracBrowser for help on using the repository browser.