3f287d21fe67a124ee3eb3671895cae6e2965946
[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 CONSUMERD_BIN="lttng-consumerd"
20 RELAYD_BIN="lttng-relayd"
21 LTTNG_BIN="lttng"
22 BABELTRACE_BIN="babeltrace"
23 OUTPUT_DEST=/dev/null 2>&1
24
25 # Minimal kernel version supported for session daemon tests
26 KERNEL_MAJOR_VERSION=2
27 KERNEL_MINOR_VERSION=6
28 KERNEL_PATCHLEVEL_VERSION=27
29
30 source $TESTDIR/utils/tap/tap.sh
31
32 function print_ok ()
33 {
34 # Check if we are a terminal
35 if [ -t 1 ]; then
36 echo -e "\e[1;32mOK\e[0m"
37 else
38 echo -e "OK"
39 fi
40 }
41
42 function print_fail ()
43 {
44 # Check if we are a terminal
45 if [ -t 1 ]; then
46 echo -e "\e[1;31mFAIL\e[0m"
47 else
48 echo -e "FAIL"
49 fi
50 }
51
52 function print_test_banner ()
53 {
54 local desc="$1"
55 diag "$desc"
56 }
57
58 function validate_kernel_version ()
59 {
60 local kern_version=($(uname -r | awk -F. '{ printf("%d.%d.%d\n",$1,$2,$3); }' | tr '.' '\n'))
61 if [ ${kern_version[0]} -gt $KERNEL_MAJOR_VERSION ]; then
62 return 0
63 fi
64 if [ ${kern_version[1]} -gt $KERNEL_MINOR_VERSION ]; then
65 return 0
66 fi
67 if [ ${kern_version[2]} -ge $KERNEL_PATCHLEVEL_VERSION ]; then
68 return 0
69 fi
70 return 1
71 }
72
73 # Generate a random string
74 # $1 = number of characters; defaults to 16
75 # $2 = include special characters; 1 = yes, 0 = no; defaults to yes
76 function randstring()
77 {
78 [ "$2" == "0" ] && CHAR="[:alnum:]" || CHAR="[:graph:]"
79 cat /dev/urandom | tr -cd "$CHAR" | head -c ${1:-16}
80 echo
81 }
82
83 function lttng_enable_kernel_event
84 {
85 local sess_name=$1
86 local event_name=$2
87 local channel_name=$3
88
89 if [ -z $event_name ]; then
90 # Enable all event if no event name specified
91 event_name="-a"
92 fi
93
94 if [ -z $channel_name ]; then
95 # default channel if none specified
96 chan=""
97 else
98 chan="-c $channel_name"
99 fi
100
101 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" $chan -s $sess_name -k >$OUTPUT_DEST
102 ok $? "Enable kernel event $event_name for session $sess_name"
103 }
104
105 function start_lttng_relayd
106 {
107 local opt=$1
108
109 DIR=$(readlink -f $TESTDIR)
110
111 if [ -z $(pidof lt-$RELAYD_BIN) ]; then
112 $DIR/../src/bin/lttng-relayd/$RELAYD_BIN -b $opt >$OUTPUT_DEST
113 #$DIR/../src/bin/lttng-relayd/$RELAYD_BIN $opt -vvv >>/tmp/relayd.log 2>&1 &
114 if [ $? -eq 1 ]; then
115 fail "Start lttng-relayd (opt: $opt)"
116 return 1
117 else
118 pass "Start lttng-relayd (opt: $opt)"
119 fi
120 else
121 pass "Start lttng-relayd (opt: $opt)"
122 fi
123 }
124
125 function stop_lttng_relayd_nocheck
126 {
127 PID_RELAYD=`pidof lt-$RELAYD_BIN`
128
129 diag "Killing lttng-relayd (pid: $PID_RELAYD)"
130 kill $PID_RELAYD >$OUTPUT_DEST
131 retval=$?
132
133 if [ $retval -eq 1 ]; then
134 out=1
135 while [ -n "$out" ]; do
136 out=$(pidof lt-$RELAYD_BIN)
137 sleep 0.5
138 done
139 fi
140 return $retval
141 }
142
143 function stop_lttng_relayd
144 {
145 stop_lttng_relayd_nocheck
146
147 if [ $? -eq 1 ]; then
148 fail "Killed lttng-relayd (pid: $PID_RELAYD)"
149 return 1
150 else
151 pass "Killed lttng-relayd (pid: $PID_RELAYD)"
152 return 0
153 fi
154 }
155
156 function start_lttng_sessiond()
157 {
158 if [ -n $TEST_NO_SESSIOND ] && [ "$TEST_NO_SESSIOND" == "1" ]; then
159 # Env variable requested no session daemon
160 return
161 fi
162
163 validate_kernel_version
164 if [ $? -ne 0 ]; then
165 fail "Start session daemon"
166 BAIL_OUT "*** Kernel too old for session daemon tests ***"
167 fi
168
169 DIR=$(readlink -f $TESTDIR)
170 : ${LTTNG_SESSION_CONFIG_XSD_PATH=${DIR}/../src/common/config/}
171 export LTTNG_SESSION_CONFIG_XSD_PATH
172
173 if [ -z $(pidof lt-$SESSIOND_BIN) ]; then
174 $DIR/../src/bin/lttng-sessiond/$SESSIOND_BIN --background --consumerd32-path="$DIR/../src/bin/lttng-consumerd/lttng-consumerd" --consumerd64-path="$DIR/../src/bin/lttng-consumerd/lttng-consumerd"
175 #$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 &
176 status=$?
177 ok $status "Start session daemon"
178 fi
179 }
180
181 function stop_lttng_sessiond ()
182 {
183 if [ -n $TEST_NO_SESSIOND ] && [ "$TEST_NO_SESSIOND" == "1" ]; then
184 # Env variable requested no session daemon
185 return
186 fi
187
188 PID_SESSIOND=`pidof lt-$SESSIOND_BIN`
189
190 kill $PID_SESSIOND >$OUTPUT_DEST
191
192 if [ $? -eq 1 ]; then
193 fail "Kill sessions daemon"
194 return 1
195 else
196 out=1
197 while [ -n "$out" ]; do
198 out=$(pidof lt-$SESSIOND_BIN)
199 sleep 0.5
200 done
201 out=1
202 while [ -n "$out" ]; do
203 out=$(pidof $CONSUMERD_BIN)
204 sleep 0.5
205 done
206 pass "Kill session daemon"
207 fi
208 }
209
210 function list_lttng_with_opts ()
211 {
212 local opts=$1
213 $TESTDIR/../src/bin/lttng/$LTTNG_BIN list $opts >$OUTPUT_DEST
214 ok $? "Lttng-tool list command with option $opts"
215 }
216
217 function create_lttng_session_no_output ()
218 {
219 local sess_name=$1
220
221 $TESTDIR/../src/bin/lttng/$LTTNG_BIN create $sess_name --no-output >$OUTPUT_DEST
222 ok $? "Create session $sess_name in no-output mode"
223 }
224
225 function create_lttng_session ()
226 {
227 local sess_name=$1
228 local trace_path=$2
229 local expected_to_fail=$3
230
231 $TESTDIR/../src/bin/lttng/$LTTNG_BIN create $sess_name -o $trace_path > $OUTPUT_DEST
232 ret=$?
233 if [[ $expected_to_fail ]]; then
234 test "$ret" -ne "0"
235 ok $? "Expected fail on session creation $sess_name in $trace_path"
236 else
237 ok $ret "Create session $sess_name in $trace_path"
238 fi
239 }
240
241 function enable_ust_lttng_channel()
242 {
243 local sess_name=$1
244 local channel_name=$2
245 local expect_fail=$3
246
247 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-channel -u $channel_name -s $sess_name >$OUTPUT_DEST
248 ret=$?
249 if [[ $expect_fail ]]; then
250 test "$ret" -ne "0"
251 ok $? "Expected fail on ust channel creation $channel_name in $sess_name"
252 else
253 ok $ret "Enable channel $channel_name for session $sess_name"
254 fi
255 }
256
257 function disable_ust_lttng_channel()
258 {
259 local sess_name=$1
260 local channel_name=$2
261
262 $TESTDIR/../src/bin/lttng/$LTTNG_BIN disable-channel -u $channel_name -s $sess_name >$OUTPUT_DEST
263 ok $? "Disable channel $channel_name for session $sess_name"
264 }
265
266 function enable_lttng_mmap_overwrite_kernel_channel()
267 {
268 local sess_name=$1
269 local channel_name=$2
270
271 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-channel -s $sess_name $channel_name -k --output mmap --overwrite >$OUTPUT_DEST
272 ok $? "Enable channel $channel_name for session $sess_name"
273 }
274
275 function enable_lttng_mmap_overwrite_ust_channel()
276 {
277 local sess_name=$1
278 local channel_name=$2
279
280 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-channel -s $sess_name $channel_name -u --output mmap --overwrite >$OUTPUT_DEST
281 ok $? "Enable channel $channel_name for session $sess_name"
282 }
283
284 function enable_ust_lttng_event ()
285 {
286 local sess_name=$1
287 local event_name="$2"
288 local channel_name=$3
289 local expected_to_fail=$4
290
291 if [ -z $channel_name ]; then
292 # default channel if none specified
293 chan=""
294 else
295 chan="-c $channel_name"
296 fi
297
298 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" $chan -s $sess_name -u >$OUTPUT_DEST
299 ret=$?
300 if [[ $expected_to_fail ]]; then
301 test $ret -ne "0"
302 ok $? "Enable ust event $event_name for session $session_name on channel $channel_name failed as expected"
303 else
304 ok $ret "Enable event $event_name for session $sess_name"
305 fi
306 }
307
308 function enable_jul_lttng_event()
309 {
310 sess_name=$1
311 event_name="$2"
312 channel_name=$3
313
314 if [ -z $channel_name ]; then
315 # default channel if none specified
316 chan=""
317 else
318 chan="-c $channel_name"
319 fi
320
321 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" $chan -s $sess_name -j >$OUTPUT_DEST
322 ok $? "Enable JUL event $event_name for session $sess_name"
323 }
324
325 function enable_jul_lttng_event_loglevel()
326 {
327 local sess_name=$1
328 local event_name="$2"
329 local loglevel=$3
330 local channel_name=$4
331
332 if [ -z $channel_name ]; then
333 # default channel if none specified
334 chan=""
335 else
336 chan="-c $channel_name"
337 fi
338
339 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event --loglevel $loglevel "$event_name" $chan -s $sess_name -j >$OUTPUT_DEST
340 ok $? "Enable JUL event $event_name for session $sess_name with loglevel $loglevel"
341 }
342
343 function enable_ust_lttng_event_filter()
344 {
345 local sess_name="$1"
346 local event_name="$2"
347 local filter="$3"
348
349 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" -s $sess_name -u --filter "$filter" >$OUTPUT_DEST
350 ok $? "Enable event $event_name with filtering for session $sess_name"
351 }
352
353 function enable_ust_lttng_event_loglevel()
354 {
355 local sess_name="$1"
356 local event_name="$2"
357 local loglevel="$3"
358
359 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" -s $sess_name -u --loglevel $loglevel >$OUTPUT_DEST
360 ok $? "Enable event $event_name with loglevel $loglevel"
361 }
362
363 function enable_ust_lttng_event_loglevel_only()
364 {
365 local sess_name="$1"
366 local event_name="$2"
367 local loglevel="$3"
368
369 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" -s $sess_name -u --loglevel-only $loglevel >$OUTPUT_DEST
370 ok $? "Enable event $event_name with loglevel-only $loglevel"
371 }
372
373 function disable_ust_lttng_event ()
374 {
375 local sess_name="$1"
376 local event_name="$2"
377 local channel_name="$3"
378
379 if [ -z $channel_name ]; then
380 # default channel if none specified
381 chan=""
382 else
383 chan="-c $channel_name"
384 fi
385
386 $TESTDIR/../src/bin/lttng/$LTTNG_BIN disable-event "$event_name" -s $sess_name $chan -u >$OUTPUT_DEST
387 ok $? "Disable event $event_name for session $sess_name"
388 }
389
390 function disable_jul_lttng_event ()
391 {
392 local sess_name="$1"
393 local event_name="$2"
394
395 $TESTDIR/../src/bin/lttng/$LTTNG_BIN disable-event "$event_name" -s $sess_name -j >/dev/null 2>&1
396 ok $? "Disable JUL event $event_name for session $sess_name"
397 }
398
399 function start_lttng_tracing ()
400 {
401 local sess_name=$1
402 local expected_to_fail=$2
403
404 $TESTDIR/../src/bin/lttng/$LTTNG_BIN start $sess_name >$OUTPUT_DEST
405 ret=$?
406 if [[ $expected_to_fail ]]; then
407 test "$ret" -ne "0"
408 ok $? "Expected fail on start tracing for session: $sess_name"
409 else
410 ok $ret "Start tracing for session $sess_name"
411 fi
412 }
413
414 function stop_lttng_tracing ()
415 {
416 local sess_name=$1
417 local expected_to_fail=$2
418
419 $TESTDIR/../src/bin/lttng/$LTTNG_BIN stop $sess_name >$OUTPUT_DEST
420 ret=$?
421 if [[ $expected_to_fail ]]; then
422 test "$ret" -ne "0"
423 ok $? "Expected fail on stop tracing for session: $sess_name"
424 else
425 ok $ret "Stop lttng tracing for session $sess_name"
426 fi
427 }
428
429 function destroy_lttng_session ()
430 {
431 local sess_name=$1
432 local expected_to_fail=$2
433
434 $TESTDIR/../src/bin/lttng/$LTTNG_BIN destroy $sess_name >$OUTPUT_DEST
435 ret=$?
436 if [[ $expected_to_fail ]]; then
437 test "$ret" -ne "0"
438 ok $? "Expected fail on session deletion $sess_name"
439 else
440 ok $ret "Destroy session $sess_name"
441 fi
442 }
443
444 function destroy_lttng_sessions ()
445 {
446 $TESTDIR/../src/bin/lttng/$LTTNG_BIN destroy --all >$OUTPUT_DEST
447 ok $? "Destroy all lttng sessions"
448 }
449
450 function lttng_snapshot_add_output ()
451 {
452 local sess_name=$1
453 local trace_path=$2
454 local expected_to_fail=$3
455
456 $TESTDIR/../src/bin/lttng/$LTTNG_BIN snapshot add-output -s $sess_name file://$trace_path >$OUTPUT_DEST
457 ret=$?
458 if [[ $expected_to_fail ]]; then
459 test "$ret" -ne "0"
460 ok $? "Failed to add a snapshot output file://$trace_path as expected"
461 else
462 ok $ret "Added snapshot output file://$trace_path"
463 fi
464 }
465
466 function lttng_snapshot_del_output ()
467 {
468 local sess_name=$1
469 local id=$2
470 local expected_to_fail=$3
471
472 $TESTDIR/../src/bin/lttng/$LTTNG_BIN snapshot del-output -s $sess_name $id >$OUTPUT_DEST
473 ret=$?
474 if [[ $expected_to_fail ]]; then
475 test "$ret" -ne "0"
476 ok $? "Expect fail on deletion of snapshot output id $id"
477 else
478 ok $ret "Deleted snapshot output id $id"
479 fi
480 }
481
482 function lttng_snapshot_record ()
483 {
484 local sess_name=$1
485 local trace_path=$2
486
487 $TESTDIR/../src/bin/lttng/$LTTNG_BIN snapshot record -s $sess_name >$OUTPUT_DEST
488 ok $? "Snapshot recorded"
489 }
490
491 function lttng_snapshot_list ()
492 {
493 local sess_name=$1
494 $TESTDIR/../src/bin/lttng/$LTTNG_BIN snapshot list-output -s $sess_name >$OUTPUT_DEST
495 ok $? "Snapshot list"
496 }
497
498 function lttng_save()
499 {
500 local sess_name=$1
501 local opts=$2
502
503 $TESTDIR/../src/bin/lttng/$LTTNG_BIN save $sess_name $opts >$OUTPUT_DEST
504 ok $? "Session successfully saved"
505 }
506
507 function lttng_load()
508 {
509 local opts=$1
510
511 $TESTDIR/../src/bin/lttng/$LTTNG_BIN load $opts >$OUTPUT_DEST
512 ok $? "Load command successful"
513 }
514
515 function trace_matches ()
516 {
517 local event_name=$1
518 local nr_iter=$2
519 local trace_path=$3
520
521 which $BABELTRACE_BIN >/dev/null
522 skip $? -ne 0 "Babeltrace binary not found. Skipping trace matches"
523
524 local count=$($BABELTRACE_BIN $trace_path | grep $event_name | wc -l)
525
526 if [ "$count" -ne "$nr_iter" ]; then
527 fail "Trace match"
528 diag "$count events found in trace"
529 else
530 pass "Trace match"
531 fi
532 }
533
534 function validate_trace
535 {
536 local event_name=$1
537 local trace_path=$2
538
539 which $BABELTRACE_BIN >/dev/null
540 if [ $? -ne 0 ]; then
541 skip 0 "Babeltrace binary not found. Skipping trace validation"
542 fi
543
544 OLDIFS=$IFS
545 IFS=","
546 for i in $event_name; do
547 traced=$($BABELTRACE_BIN $trace_path 2>/dev/null | grep $i | wc -l)
548 if [ "$traced" -ne 0 ]; then
549 pass "Validate trace for event $i, $traced events"
550 else
551 fail "Validate trace for event $i"
552 diag "Found $traced occurences of $i"
553 fi
554 done
555 ret=$?
556 IFS=$OLDIFS
557 return $ret
558 }
This page took 0.06206 seconds and 3 git commands to generate.