Tests: add regression test for Java JUL support
[lttng-tools.git] / tests / utils / utils.sh
1 #!/src/bin/bash
2 #
3 # Copyright (C) - 2012 David Goulet <dgoulet@efficios.com>
4 #
5 # This library is free software; you can redistribute it and/or modify it under
6 # the terms of the GNU Lesser General Public License as published by the Free
7 # Software Foundation; version 2.1 of the License.
8 #
9 # This library is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12 # details.
13 #
14 # You should have received a copy of the GNU Lesser General Public License
15 # along with this library; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
18 SESSIOND_BIN="lttng-sessiond"
19 RELAYD_BIN="lttng-relayd"
20 LTTNG_BIN="lttng"
21 BABELTRACE_BIN="babeltrace"
22
23 # Minimal kernel version supported for session daemon tests
24 KERNEL_MAJOR_VERSION=2
25 KERNEL_MINOR_VERSION=6
26 KERNEL_PATCHLEVEL_VERSION=27
27
28 source $TESTDIR/utils/tap/tap.sh
29
30 function print_ok ()
31 {
32 # Check if we are a terminal
33 if [ -t 1 ]; then
34 echo -e "\e[1;32mOK\e[0m"
35 else
36 echo -e "OK"
37 fi
38 }
39
40 function print_fail ()
41 {
42 # Check if we are a terminal
43 if [ -t 1 ]; then
44 echo -e "\e[1;31mFAIL\e[0m"
45 else
46 echo -e "FAIL"
47 fi
48 }
49
50 function print_test_banner ()
51 {
52 desc="$1"
53 diag "$desc"
54 }
55
56 function validate_kernel_version ()
57 {
58 kern_version=($(uname -r | awk -F. '{ printf("%d.%d.%d\n",$1,$2,$3); }' | tr '.' '\n'))
59 if [ ${kern_version[0]} -gt $KERNEL_MAJOR_VERSION ]; then
60 return 0
61 fi
62 if [ ${kern_version[1]} -gt $KERNEL_MINOR_VERSION ]; then
63 return 0
64 fi
65 if [ ${kern_version[2]} -ge $KERNEL_PATCHLEVEL_VERSION ]; then
66 return 0
67 fi
68 return 1
69 }
70
71 # Generate a random string
72 # $1 = number of characters; defaults to 16
73 # $2 = include special characters; 1 = yes, 0 = no; defaults to yes
74 function randstring()
75 {
76 [ "$2" == "0" ] && CHAR="[:alnum:]" || CHAR="[:graph:]"
77 cat /dev/urandom | tr -cd "$CHAR" | head -c ${1:-16}
78 echo
79 }
80
81 function lttng_enable_kernel_event
82 {
83 sess_name=$1
84 event_name=$2
85 channel_name=$3
86
87 if [ -z $event_name ]; then
88 # Enable all event if no event name specified
89 event_name="-a"
90 fi
91
92 if [ -z $channel_name ]; then
93 # default channel if none specified
94 chan=""
95 else
96 chan="-c $channel_name"
97 fi
98
99 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" $chan -s $sess_name -k >/dev/null 2>&1
100 ok $? "Enable kernel event $event_name for session $sess_name"
101 }
102
103 function start_lttng_relayd
104 {
105 local opt=$1
106
107 DIR=$(readlink -f $TESTDIR)
108
109 if [ -z $(pidof lt-$RELAYD_BIN) ]; then
110 $DIR/../src/bin/lttng-relayd/$RELAYD_BIN $opt >/dev/null 2>&1 &
111 #$DIR/../src/bin/lttng-relayd/$RELAYD_BIN $opt -vvv >>/tmp/relayd.log 2>&1 &
112 if [ $? -eq 1 ]; then
113 fail "Start lttng-relayd (opt: $opt)"
114 return 1
115 else
116 pass "Start lttng-relayd (opt: $opt)"
117 fi
118 else
119 pass "Start lttng-relayd (opt: $opt)"
120 fi
121 }
122
123 function stop_lttng_relayd
124 {
125 PID_RELAYD=`pidof lt-$RELAYD_BIN`
126
127 kill $PID_RELAYD >/dev/null 2>&1
128
129 if [ $? -eq 1 ]; then
130 fail "Kill lttng-relayd (pid: $PID_RELAYD)"
131 return 1
132 else
133 out=1
134 while [ -n "$out" ]; do
135 out=$(pidof lt-$RELAYD_BIN)
136 sleep 0.5
137 done
138 pass "Kill lttng-relayd (pid: $PID_RELAYD)"
139 return 0
140 fi
141 }
142
143 function start_lttng_sessiond()
144 {
145 if [ -n $TEST_NO_SESSIOND ] && [ "$TEST_NO_SESSIOND" == "1" ]; then
146 # Env variable requested no session daemon
147 return
148 fi
149
150 validate_kernel_version
151 if [ $? -ne 0 ]; then
152 fail "Start session daemon"
153 BAIL_OUT "*** Kernel too old for session daemon tests ***"
154 fi
155
156 DIR=$(readlink -f $TESTDIR)
157
158 if [ -z $(pidof lt-$SESSIOND_BIN) ]; then
159 $DIR/../src/bin/lttng-sessiond/$SESSIOND_BIN --daemonize --consumerd32-path="$DIR/../src/bin/lttng-consumerd/lttng-consumerd" --consumerd64-path="$DIR/../src/bin/lttng-consumerd/lttng-consumerd"
160 #$DIR/../src/bin/lttng-sessiond/$SESSIOND_BIN --consumerd32-path="$DIR/../src/bin/lttng-consumerd/lttng-consumerd" --consumerd64-path="$DIR/../src/bin/lttng-consumerd/lttng-consumerd" --verbose-consumer >>/tmp/sessiond.log 2>&1 &
161 status=$?
162 ok $status "Start session daemon"
163 fi
164 }
165
166 function stop_lttng_sessiond ()
167 {
168 if [ -n $TEST_NO_SESSIOND ] && [ "$TEST_NO_SESSIOND" == "1" ]; then
169 # Env variable requested no session daemon
170 return
171 fi
172
173 PID_SESSIOND=`pidof lt-$SESSIOND_BIN`
174
175 kill $PID_SESSIOND >/dev/null 2>&1
176
177 if [ $? -eq 1 ]; then
178 fail "Kill sessions daemon"
179 return 1
180 else
181 out=1
182 while [ -n "$out" ]; do
183 out=$(pidof lt-$SESSIOND_BIN)
184 sleep 0.5
185 done
186 pass "Kill session daemon"
187 fi
188 }
189
190 function create_lttng_session_no_output ()
191 {
192 sess_name=$1
193
194 $TESTDIR/../src/bin/lttng/$LTTNG_BIN create $sess_name --no-output >/dev/null 2>&1
195 ok $? "Create session $sess_name in no-output mode"
196 }
197
198 function create_lttng_session ()
199 {
200 sess_name=$1
201 trace_path=$2
202
203 $TESTDIR/../src/bin/lttng/$LTTNG_BIN create $sess_name -o $trace_path >/dev/null 2>&1
204 ok $? "Create session $sess_name in $trace_path"
205 }
206
207 function enable_ust_lttng_channel()
208 {
209 sess_name=$1
210 channel_name=$2
211
212 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-channel -u $channel_name -s $sess_name >/dev/null 2>&1
213 ok $? "Enable channel $channel_name for session $sess_name"
214 }
215
216 function disable_ust_lttng_channel()
217 {
218 sess_name=$1
219 channel_name=$2
220
221 $TESTDIR/../src/bin/lttng/$LTTNG_BIN disable-channel -u $channel_name -s $sess_name >/dev/null 2>&1
222 ok $? "Disable channel $channel_name for session $sess_name"
223 }
224
225 function enable_lttng_mmap_overwrite_kernel_channel()
226 {
227 sess_name=$1
228 channel_name=$2
229
230 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-channel -s $sess_name $channel_name -k --output mmap --overwrite >/dev/null 2>&1
231 ok $? "Enable channel $channel_name for session $sess_name"
232 }
233
234 function enable_lttng_mmap_overwrite_ust_channel()
235 {
236 sess_name=$1
237 channel_name=$2
238
239 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-channel -s $sess_name $channel_name -u --output mmap --overwrite >/dev/null 2>&1
240 ok $? "Enable channel $channel_name for session $sess_name"
241 }
242
243 function enable_ust_lttng_event ()
244 {
245 sess_name=$1
246 event_name="$2"
247 channel_name=$3
248
249 if [ -z $channel_name ]; then
250 # default channel if none specified
251 chan=""
252 else
253 chan="-c $channel_name"
254 fi
255
256 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" $chan -s $sess_name -u >/dev/null 2>&1
257 ok $? "Enable event $event_name for session $sess_name"
258 }
259
260 function enable_jul_lttng_event()
261 {
262 sess_name=$1
263 event_name="$2"
264 channel_name=$3
265
266 if [ -z $channel_name ]; then
267 # default channel if none specified
268 chan=""
269 else
270 chan="-c $channel_name"
271 fi
272
273 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" $chan -s $sess_name -j >/dev/null 2>&1
274 ok $? "Enable JUL event $event_name for session $sess_name"
275 }
276
277 function enable_ust_lttng_event_filter()
278 {
279 sess_name="$1"
280 event_name="$2"
281 filter="$3"
282
283 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" -s $sess_name -u --filter "$filter" >/dev/null 2>&1
284 ok $? "Enable event $event_name with filtering for session $sess_name"
285 }
286
287 function enable_ust_lttng_event_loglevel()
288 {
289 sess_name="$1"
290 event_name="$2"
291 loglevel="$3"
292
293 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" -s $sess_name -u --loglevel $loglevel >/dev/null 2>&1
294 ok $? "Enable event $event_name with loglevel $loglevel"
295 }
296
297 function enable_ust_lttng_event_loglevel_only()
298 {
299 sess_name="$1"
300 event_name="$2"
301 loglevel="$3"
302
303 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" -s $sess_name -u --loglevel-only $loglevel >/dev/null 2>&1
304 ok $? "Enable event $event_name with loglevel-only $loglevel"
305 }
306
307 function disable_ust_lttng_event ()
308 {
309 sess_name="$1"
310 event_name="$2"
311
312 $TESTDIR/../src/bin/lttng/$LTTNG_BIN disable-event "$event_name" -s $sess_name -u >/dev/null 2>&1
313 ok $? "Disable event $event_name for session $sess_name"
314 }
315
316 function start_lttng_tracing ()
317 {
318 sess_name=$1
319
320 $TESTDIR/../src/bin/lttng/$LTTNG_BIN start $sess_name >/dev/null 2>&1
321 ok $? "Start tracing for session $sess_name"
322 }
323
324 function stop_lttng_tracing ()
325 {
326 sess_name=$1
327
328 $TESTDIR/../src/bin/lttng/$LTTNG_BIN stop $sess_name >/dev/null 2>&1
329 ok $? "Stop lttng tracing for session $sess_name"
330 }
331
332 function destroy_lttng_session ()
333 {
334 sess_name=$1
335
336
337 $TESTDIR/../src/bin/lttng/$LTTNG_BIN destroy $sess_name >/dev/null 2>&1
338 ok $? "Destroy lttng session $sess_name"
339 }
340
341 function lttng_snapshot_add_output ()
342 {
343 sess_name=$1
344 trace_path=$2
345
346 $TESTDIR/../src/bin/lttng/$LTTNG_BIN snapshot add-output -s $sess_name file://$trace_path >/dev/null 2>&1
347 ok $? "Added snapshot output file://$trace_path"
348 }
349
350 function lttng_snapshot_del_output ()
351 {
352 local sess_name=$1
353 local id=$2
354
355 $TESTDIR/../src/bin/lttng/$LTTNG_BIN snapshot del-output -s $sess_name $id >/dev/null 2>&1
356 ok $? "Deleted snapshot output id $id"
357 }
358
359 function lttng_snapshot_record ()
360 {
361 sess_name=$1
362 trace_path=$2
363
364 $TESTDIR/../src/bin/lttng/$LTTNG_BIN snapshot record -s $sess_name >/dev/null 2>&1
365 ok $? "Snapshot recorded"
366 }
367
368 function trace_matches ()
369 {
370 event_name=$1
371 nr_iter=$2
372 trace_path=$3
373
374 which $BABELTRACE_BIN >/dev/null
375 skip $? -ne 0 "Babeltrace binary not found. Skipping trace matches"
376
377 count=$($BABELTRACE_BIN $trace_path | grep $event_name | wc -l)
378
379 if [ "$count" -ne "$nr_iter" ]; then
380 fail "Trace match"
381 diag "$count events found in trace"
382 else
383 pass "Trace match"
384 fi
385 }
386
387 function validate_trace
388 {
389 event_name=$1
390 trace_path=$2
391
392 which $BABELTRACE_BIN >/dev/null
393 if [ $? -ne 0 ]; then
394 skip 0 "Babeltrace binary not found. Skipping trace validation"
395 fi
396
397 OLDIFS=$IFS
398 IFS=","
399 for i in $event_name; do
400 traced=$($BABELTRACE_BIN $trace_path 2>/dev/null | grep $i | wc -l)
401 if [ "$traced" -ne 0 ]; then
402 pass "Validate trace for event $i, $traced events"
403 else
404 fail "Validate trace for event $i"
405 diag "Found $traced occurences of $i"
406 fi
407 done
408 ret=$?
409 IFS=$OLDIFS
410 return $ret
411 }
This page took 0.060939 seconds and 5 git commands to generate.