libroot/posix/stdio: Remove unused portions.
[haiku.git] / src / bin / leak_analyser.sh
blob2532e2af520e1e63ae739baefdca6d679ffbb15e
1 #!/bin/bash
3 if [ ! -f "$1" ]
4 then
5 cat << EOF
7 $(basename $0) <leaksFile> [<options>] [<excludePatterns>]
9 <leaksFile>
11 A file containing the allocations with stack traces from
12 the guarded heap output.
14 To generate such a file run a program with the following
15 environment variables prefixed and pipe the output to a file:
17 LD_PRELOAD=libroot_debug.so MALLOC_DEBUG=ges50 program > file
19 The number after the "s" is the stack trace depth. Note that
20 there is an implementation defined maximum.
22 --no-default-excludes
24 Do not exclude known statics and globals. By default a list of
25 excludes is used that removes known allocations that are never
26 freed by the system libraries.
28 --no-exclude-empty
30 Do not exclude allocations with no stack trace. By default
31 allocations without a stack trace are excluded. This should
32 only happen for very early allocations where the stack trace
33 setting has not yet been applied. The program should not be
34 able to generate such allocations.
36 <excludePatterns>
38 Exclude allocations that match a regular expression. The
39 expression is matched against the whole text block of the
40 allocation, so can match in the header line as well as any
41 stack trace lines. Note that the whole block is on a single
42 line and newlines have been replaced with the caret (^)
43 character.
45 Multiple exclude patterns can be specified as one argument each
46 and they will be ored to form the final expression.
48 EOF
50 exit 1
54 FILENAME="$1"
55 shift
58 DONE_PARSING_OPTIONS=
59 NO_DEFAULTS=
60 NO_EXCLUDE_EMPTY=
61 while [ -z "$DONE_PARSING_OPTIONS" ]
63 case "$1" in
64 --no-default-excludes)
65 NO_DEFAULTS=yes
66 shift
68 --no-exclude-empty)
69 NO_EXCLUDE_EMPTY=yes
70 shift
72 --*)
73 echo "unrecognized option \"$1\" ignored"
74 shift
77 DONE_PARSING_OPTIONS=yes
79 esac
80 done
83 function append_pattern {
84 if [ -z "$EXCLUDE_PATTERN" ]
85 then
86 EXCLUDE_PATTERN="$1"
87 else
88 EXCLUDE_PATTERN="$EXCLUDE_PATTERN|$1"
93 EXCLUDE_PATTERN=""
94 if [ -z "$NO_DEFAULTS" ]
95 then
96 declare -a DEFAULT_EXCLUDE_LIST=( \
97 "<libroot.so> initialize_before " \
98 "<libroot.so> __cxa_atexit " \
99 "<libroot.so> BPrivate::Libroot::LocaleBackend::LoadBackend" \
100 "<libbe.so> initialize_before " \
101 "<libbe.so> initialize_after " \
102 "<libbe.so> _control_input_server_" \
103 "<libbe.so> BApplication::_InitGUIContext" \
104 "<libbe.so> BApplication::_InitAppResources" \
105 "<libbe.so> BResources::LoadResource" \
106 "<libbe.so> BClipboard::_DownloadFromSystem" \
107 "<libbe.so> BToolTipManager::_InitSingleton" \
108 "<libbe.so> BPrivate::WidthBuffer::StringWidth" \
109 "<libtracker.so> _init " \
110 "<libtranslation.so> BTranslatorRoster::Default" \
111 "Translator> " \
112 "<libicu[^.]+.so.[0-9]+> icu(_[0-9]+)?::" \
115 for EXCLUDE in "${DEFAULT_EXCLUDE_LIST[@]}"
117 append_pattern "$EXCLUDE"
118 done
122 if [ -z "$NO_EXCLUDE_EMPTY" ]
123 then
124 append_pattern "^[^^]*\^$"
128 while [ $# -gt 0 ]
130 append_pattern "$1"
131 shift
132 done
135 ALLOCATIONS=$(cat "$FILENAME" | egrep "^allocation: |^ " | tr '\n' '^' \
136 | sed 's/\^a/~a/g' | tr '~' '\n' | sed 's/$/^/' | c++filt)
138 if [ ! -z "$EXCLUDE_PATTERN" ]
139 then
140 ALLOCATIONS=$(echo "$ALLOCATIONS" | egrep -v "$EXCLUDE_PATTERN")
143 if [ -z "$ALLOCATIONS" ]
144 then
145 COUNT=0
146 else
147 COUNT=$(echo "$ALLOCATIONS" | wc -l)
151 echo "$ALLOCATIONS^total leaks: $COUNT" | tr '^' '\n' | less