1 | #!/bin/bash |
---|
2 | |
---|
3 | BASEDIR=$ARBHOME/UNIT_TESTER/valgrind |
---|
4 | LOGGING_BASE=$BASEDIR/arb_valgrind_logging_ |
---|
5 | LOGGED_BASE=$BASEDIR/arb_valgrind_logged_ |
---|
6 | |
---|
7 | CALL() { |
---|
8 | local LOGGING=$LOGGING_BASE$$.log |
---|
9 | local LOGGED=$LOGGED_BASE$$.log |
---|
10 | local EXE=$1 |
---|
11 | shift |
---|
12 | arb_valgrind -e -L $LOGGED.stdout -E $LOGGED.stderr $EXE "$@" > $LOGGING |
---|
13 | local EXITCODE=$? |
---|
14 | sleep 2 |
---|
15 | cp $LOGGING $LOGGED |
---|
16 | rm $LOGGING |
---|
17 | cat $LOGGED.stdout |
---|
18 | rm $LOGGED.stdout |
---|
19 | cat $LOGGED.stderr 1>&2 |
---|
20 | rm $LOGGED.stderr |
---|
21 | return $EXITCODE |
---|
22 | } |
---|
23 | |
---|
24 | rm_if() { |
---|
25 | ( test -f $1 && rm $1 ) || true |
---|
26 | } |
---|
27 | |
---|
28 | have_logging() { |
---|
29 | ls -1 $LOGGING_BASE*.log > /dev/null 2>&1 |
---|
30 | if [ "$?" = "0" ]; then |
---|
31 | echo "1" |
---|
32 | else |
---|
33 | echo "0" |
---|
34 | fi |
---|
35 | } |
---|
36 | have_logged() { |
---|
37 | ls -1 $LOGGED_BASE*.log > /dev/null 2>&1 |
---|
38 | if [ "$?" = "0" ]; then |
---|
39 | echo "1" |
---|
40 | else |
---|
41 | echo "0" |
---|
42 | fi |
---|
43 | } |
---|
44 | |
---|
45 | count_logging() { |
---|
46 | if [ `have_logging` == 1 ]; then |
---|
47 | echo $LOGGING_BASE*.log | wc -w |
---|
48 | else |
---|
49 | echo "0" |
---|
50 | fi |
---|
51 | } |
---|
52 | count_logged() { |
---|
53 | if [ `have_logged` == 1 ]; then |
---|
54 | echo $LOGGED_BASE*.log | wc -w |
---|
55 | else |
---|
56 | echo "0" |
---|
57 | fi |
---|
58 | } |
---|
59 | |
---|
60 | remove_flags() { |
---|
61 | rm_if $BASEDIR/flag.valgrind |
---|
62 | rm_if $BASEDIR/flag.valgrind.leaks |
---|
63 | rm_if $BASEDIR/flag.valgrind.reachable |
---|
64 | rm_if $BASEDIR/flag.valgrind.callseen |
---|
65 | } |
---|
66 | |
---|
67 | INIT() { |
---|
68 | if [ `have_logging` == 1 ]; then |
---|
69 | rm $LOGGING_BASE*.log |
---|
70 | fi |
---|
71 | |
---|
72 | if [ `have_logged` == 1 ]; then |
---|
73 | rm $LOGGED_BASE*.log |
---|
74 | fi |
---|
75 | |
---|
76 | shift |
---|
77 | local VALGRIND=$1 |
---|
78 | local CHECK_LEAKS=$2 |
---|
79 | |
---|
80 | remove_flags |
---|
81 | if [ "$VALGRIND" == "E" ]; then |
---|
82 | touch $BASEDIR/flag.valgrind |
---|
83 | if [ "$CHECK_LEAKS" == "1" ]; then |
---|
84 | touch $BASEDIR/flag.valgrind.leaks |
---|
85 | fi |
---|
86 | if [ "$CHECK_LEAKS" == "2" ]; then |
---|
87 | touch $BASEDIR/flag.valgrind.leaks |
---|
88 | touch $BASEDIR/flag.valgrind.reachable |
---|
89 | fi |
---|
90 | fi |
---|
91 | |
---|
92 | # ls -al $BASEDIR |
---|
93 | } |
---|
94 | |
---|
95 | wait_all_valgrinds_complete() { |
---|
96 | local WAIT= |
---|
97 | local COUNT=`count_logging` |
---|
98 | if [ "$COUNT" != "0" ]; then |
---|
99 | while [ "$COUNT" != "0" ]; do |
---|
100 | echo "arb_valgrind_logged sees $COUNT pending logs - waiting until they complete" |
---|
101 | sleep 1 |
---|
102 | COUNT=`count_logging` |
---|
103 | WAIT=x$WAIT |
---|
104 | if [ "$WAIT" == "xxxxxxxxxxxxxxx" ]; then # wait 15 seconds |
---|
105 | echo "Giving up" |
---|
106 | false |
---|
107 | return |
---|
108 | fi |
---|
109 | done |
---|
110 | echo "all logs completed (i.e. all async valgrinds terminated)" |
---|
111 | fi |
---|
112 | } |
---|
113 | |
---|
114 | dump_and_unlink() { |
---|
115 | echo "---------- [ valgrind $1 ]" |
---|
116 | cat $1 |
---|
117 | rm $1 |
---|
118 | echo "---------- [ end $1 ]" |
---|
119 | } |
---|
120 | |
---|
121 | dump_all() { |
---|
122 | while [ ! -z "$1" ]; do |
---|
123 | dump_and_unlink $1 |
---|
124 | shift |
---|
125 | done |
---|
126 | } |
---|
127 | |
---|
128 | dump_all_logged() { |
---|
129 | local COUNT=`count_logged` |
---|
130 | if [ "$COUNT" == "0" ]; then |
---|
131 | echo "Found no logs to dump" |
---|
132 | else |
---|
133 | echo "Dumping $COUNT logs:" |
---|
134 | dump_all $LOGGED_BASE*.log |
---|
135 | fi |
---|
136 | } |
---|
137 | |
---|
138 | WAIT() { |
---|
139 | wait_all_valgrinds_complete |
---|
140 | dump_all_logged |
---|
141 | remove_flags |
---|
142 | } |
---|
143 | |
---|
144 | usage() { |
---|
145 | echo "Usage: arb_valgrind_logged INIT" |
---|
146 | echo " delete any leftover logs" |
---|
147 | echo "" |
---|
148 | echo "Usage: arb_valgrind_logged CALL arb_valgrind-args" |
---|
149 | echo " Calls arb_valgrind asynchronously and redirects output into temp. logfile." |
---|
150 | echo "" |
---|
151 | echo "Usage: arb_valgrind_logged WAIT" |
---|
152 | echo " wait until all async calls terminate (=until all logs complete)" |
---|
153 | echo " then dump all logs (synchronously)" |
---|
154 | } |
---|
155 | |
---|
156 | if [ "$1" == "WAIT" ]; then |
---|
157 | WAIT |
---|
158 | else |
---|
159 | if [ "$1" == "INIT" ]; then |
---|
160 | INIT $@ |
---|
161 | else |
---|
162 | if [ "$1" == "CALL" ]; then |
---|
163 | shift |
---|
164 | CALL "$@" |
---|
165 | else |
---|
166 | usage |
---|
167 | false |
---|
168 | fi |
---|
169 | fi |
---|
170 | fi |
---|