Fix: set full path in .gitignore
[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
24 # Minimal kernel version supported for session daemon tests
25 KERNEL_MAJOR_VERSION=2
26 KERNEL_MINOR_VERSION=6
27 KERNEL_PATCHLEVEL_VERSION=27
28
29 source $TESTDIR/utils/tap/tap.sh
30
31 function print_ok ()
32 {
33 # Check if we are a terminal
34 if [ -t 1 ]; then
35 echo -e "\e[1;32mOK\e[0m"
36 else
37 echo -e "OK"
38 fi
39 }
40
41 function print_fail ()
42 {
43 # Check if we are a terminal
44 if [ -t 1 ]; then
45 echo -e "\e[1;31mFAIL\e[0m"
46 else
47 echo -e "FAIL"
48 fi
49 }
50
51 function print_test_banner ()
52 {
53 desc="$1"
54 diag "$desc"
55 }
56
57 function validate_kernel_version ()
58 {
59 kern_version=($(uname -r | awk -F. '{ printf("%d.%d.%d\n",$1,$2,$3); }' | tr '.' '\n'))
60 if [ ${kern_version[0]} -gt $KERNEL_MAJOR_VERSION ]; then
61 return 0
62 fi
63 if [ ${kern_version[1]} -gt $KERNEL_MINOR_VERSION ]; then
64 return 0
65 fi
66 if [ ${kern_version[2]} -ge $KERNEL_PATCHLEVEL_VERSION ]; then
67 return 0
68 fi
69 return 1
70 }
71
72 # Generate a random string
73 # $1 = number of characters; defaults to 16
74 # $2 = include special characters; 1 = yes, 0 = no; defaults to yes
75 function randstring()
76 {
77 [ "$2" == "0" ] && CHAR="[:alnum:]" || CHAR="[:graph:]"
78 cat /dev/urandom | tr -cd "$CHAR" | head -c ${1:-16}
79 echo
80 }
81
82 function lttng_enable_kernel_event
83 {
84 sess_name=$1
85 event_name=$2
86 channel_name=$3
87
88 if [ -z $event_name ]; then
89 # Enable all event if no event name specified
90 event_name="-a"
91 fi
92
93 if [ -z $channel_name ]; then
94 # default channel if none specified
95 chan=""
96 else
97 chan="-c $channel_name"
98 fi
99
100 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" $chan -s $sess_name -k >/dev/null 2>&1
101 ok $? "Enable kernel event $event_name for session $sess_name"
102 }
103
104 function start_lttng_relayd
105 {
106 local opt=$1
107
108 DIR=$(readlink -f $TESTDIR)
109
110 if [ -z $(pidof lt-$RELAYD_BIN) ]; then
111 $DIR/../src/bin/lttng-relayd/$RELAYD_BIN -b $opt >/dev/null 2>&1
112 #$DIR/../src/bin/lttng-relayd/$RELAYD_BIN $opt -vvv >>/tmp/relayd.log 2>&1 &
113 if [ $? -eq 1 ]; then
114 fail "Start lttng-relayd (opt: $opt)"
115 return 1
116 else
117 pass "Start lttng-relayd (opt: $opt)"
118 fi
119 else
120 pass "Start lttng-relayd (opt: $opt)"
121 fi
122 }
123
124 function stop_lttng_relayd_nocheck
125 {
126 PID_RELAYD=`pidof lt-$RELAYD_BIN`
127
128 diag "Killing lttng-relayd (pid: $PID_RELAYD)"
129 kill $PID_RELAYD >/dev/null 2>&1
130 retval=$?
131
132 if [ $retval -eq 1 ]; then
133 out=1
134 while [ -n "$out" ]; do
135 out=$(pidof lt-$RELAYD_BIN)
136 sleep 0.5
137 done
138 fi
139 return $retval
140 }
141
142 function stop_lttng_relayd
143 {
144 stop_lttng_relayd_nocheck
145
146 if [ $? -eq 1 ]; then
147 fail "Killed lttng-relayd (pid: $PID_RELAYD)"
148 return 1
149 else
150 pass "Killed lttng-relayd (pid: $PID_RELAYD)"
151 return 0
152 fi
153 }
154
155 function start_lttng_sessiond()
156 {
157 if [ -n $TEST_NO_SESSIOND ] && [ "$TEST_NO_SESSIOND" == "1" ]; then
158 # Env variable requested no session daemon
159 return
160 fi
161
162 validate_kernel_version
163 if [ $? -ne 0 ]; then
164 fail "Start session daemon"
165 BAIL_OUT "*** Kernel too old for session daemon tests ***"
166 fi
167
168 DIR=$(readlink -f $TESTDIR)
169
170 if [ -z $(pidof lt-$SESSIOND_BIN) ]; then
171 $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"
172 #$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 &
173 status=$?
174 ok $status "Start session daemon"
175 fi
176 }
177
178 function stop_lttng_sessiond ()
179 {
180 if [ -n $TEST_NO_SESSIOND ] && [ "$TEST_NO_SESSIOND" == "1" ]; then
181 # Env variable requested no session daemon
182 return
183 fi
184
185 PID_SESSIOND=`pidof lt-$SESSIOND_BIN`
186
187 kill $PID_SESSIOND >/dev/null 2>&1
188
189 if [ $? -eq 1 ]; then
190 fail "Kill sessions daemon"
191 return 1
192 else
193 out=1
194 while [ -n "$out" ]; do
195 out=$(pidof lt-$SESSIOND_BIN)
196 sleep 0.5
197 done
198 out=1
199 while [ -n "$out" ]; do
200 out=$(pidof $CONSUMERD_BIN)
201 sleep 0.5
202 done
203 pass "Kill session daemon"
204 fi
205 }
206
207 function create_lttng_session_no_output ()
208 {
209 sess_name=$1
210
211 $TESTDIR/../src/bin/lttng/$LTTNG_BIN create $sess_name --no-output >/dev/null 2>&1
212 ok $? "Create session $sess_name in no-output mode"
213 }
214
215 function create_lttng_session ()
216 {
217 sess_name=$1
218 trace_path=$2
219
220 $TESTDIR/../src/bin/lttng/$LTTNG_BIN create $sess_name -o $trace_path >/dev/null 2>&1
221 ok $? "Create session $sess_name in $trace_path"
222 }
223
224 function enable_ust_lttng_channel()
225 {
226 sess_name=$1
227 channel_name=$2
228
229 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-channel -u $channel_name -s $sess_name >/dev/null 2>&1
230 ok $? "Enable channel $channel_name for session $sess_name"
231 }
232
233 function disable_ust_lttng_channel()
234 {
235 sess_name=$1
236 channel_name=$2
237
238 $TESTDIR/../src/bin/lttng/$LTTNG_BIN disable-channel -u $channel_name -s $sess_name >/dev/null 2>&1
239 ok $? "Disable channel $channel_name for session $sess_name"
240 }
241
242 function enable_lttng_mmap_overwrite_kernel_channel()
243 {
244 sess_name=$1
245 channel_name=$2
246
247 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-channel -s $sess_name $channel_name -k --output mmap --overwrite >/dev/null 2>&1
248 ok $? "Enable channel $channel_name for session $sess_name"
249 }
250
251 function enable_lttng_mmap_overwrite_ust_channel()
252 {
253 sess_name=$1
254 channel_name=$2
255
256 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-channel -s $sess_name $channel_name -u --output mmap --overwrite >/dev/null 2>&1
257 ok $? "Enable channel $channel_name for session $sess_name"
258 }
259
260 function enable_ust_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 -u >/dev/null 2>&1
274 ok $? "Enable event $event_name for session $sess_name"
275 }
276
277 function enable_jul_lttng_event()
278 {
279 sess_name=$1
280 event_name="$2"
281 channel_name=$3
282
283 if [ -z $channel_name ]; then
284 # default channel if none specified
285 chan=""
286 else
287 chan="-c $channel_name"
288 fi
289
290 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" $chan -s $sess_name -j >/dev/null 2>&1
291 ok $? "Enable JUL event $event_name for session $sess_name"
292 }
293
294 function enable_jul_lttng_event_loglevel()
295 {
296 sess_name=$1
297 event_name="$2"
298 loglevel=$3
299 channel_name=$4
300
301 if [ -z $channel_name ]; then
302 # default channel if none specified
303 chan=""
304 else
305 chan="-c $channel_name"
306 fi
307
308 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event --loglevel $loglevel "$event_name" $chan -s $sess_name -j >/dev/null 2>&1
309 ok $? "Enable JUL event $event_name for session $sess_name with loglevel $loglevel"
310 }
311
312 function enable_ust_lttng_event_filter()
313 {
314 sess_name="$1"
315 event_name="$2"
316 filter="$3"
317
318 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" -s $sess_name -u --filter "$filter" >/dev/null 2>&1
319 ok $? "Enable event $event_name with filtering for session $sess_name"
320 }
321
322 function enable_ust_lttng_event_loglevel()
323 {
324 sess_name="$1"
325 event_name="$2"
326 loglevel="$3"
327
328 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" -s $sess_name -u --loglevel $loglevel >/dev/null 2>&1
329 ok $? "Enable event $event_name with loglevel $loglevel"
330 }
331
332 function enable_ust_lttng_event_loglevel_only()
333 {
334 sess_name="$1"
335 event_name="$2"
336 loglevel="$3"
337
338 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" -s $sess_name -u --loglevel-only $loglevel >/dev/null 2>&1
339 ok $? "Enable event $event_name with loglevel-only $loglevel"
340 }
341
342 function disable_ust_lttng_event ()
343 {
344 sess_name="$1"
345 event_name="$2"
346
347 $TESTDIR/../src/bin/lttng/$LTTNG_BIN disable-event "$event_name" -s $sess_name -u >/dev/null 2>&1
348 ok $? "Disable event $event_name for session $sess_name"
349 }
350
351 function start_lttng_tracing ()
352 {
353 sess_name=$1
354
355 $TESTDIR/../src/bin/lttng/$LTTNG_BIN start $sess_name >/dev/null 2>&1
356 ok $? "Start tracing for session $sess_name"
357 }
358
359 function stop_lttng_tracing ()
360 {
361 sess_name=$1
362
363 $TESTDIR/../src/bin/lttng/$LTTNG_BIN stop $sess_name >/dev/null 2>&1
364 ok $? "Stop lttng tracing for session $sess_name"
365 }
366
367 function destroy_lttng_session ()
368 {
369 sess_name=$1
370
371
372 $TESTDIR/../src/bin/lttng/$LTTNG_BIN destroy $sess_name >/dev/null 2>&1
373 ok $? "Destroy lttng session $sess_name"
374 }
375
376 function lttng_snapshot_add_output ()
377 {
378 sess_name=$1
379 trace_path=$2
380
381 $TESTDIR/../src/bin/lttng/$LTTNG_BIN snapshot add-output -s $sess_name file://$trace_path >/dev/null 2>&1
382 ok $? "Added snapshot output file://$trace_path"
383 }
384
385 function lttng_snapshot_del_output ()
386 {
387 local sess_name=$1
388 local id=$2
389
390 $TESTDIR/../src/bin/lttng/$LTTNG_BIN snapshot del-output -s $sess_name $id >/dev/null 2>&1
391 ok $? "Deleted snapshot output id $id"
392 }
393
394 function lttng_snapshot_record ()
395 {
396 sess_name=$1
397 trace_path=$2
398
399 $TESTDIR/../src/bin/lttng/$LTTNG_BIN snapshot record -s $sess_name >/dev/null 2>&1
400 ok $? "Snapshot recorded"
401 }
402
403 function trace_matches ()
404 {
405 event_name=$1
406 nr_iter=$2
407 trace_path=$3
408
409 which $BABELTRACE_BIN >/dev/null
410 skip $? -ne 0 "Babeltrace binary not found. Skipping trace matches"
411
412 count=$($BABELTRACE_BIN $trace_path | grep $event_name | wc -l)
413
414 if [ "$count" -ne "$nr_iter" ]; then
415 fail "Trace match"
416 diag "$count events found in trace"
417 else
418 pass "Trace match"
419 fi
420 }
421
422 function validate_trace
423 {
424 event_name=$1
425 trace_path=$2
426
427 which $BABELTRACE_BIN >/dev/null
428 if [ $? -ne 0 ]; then
429 skip 0 "Babeltrace binary not found. Skipping trace validation"
430 fi
431
432 OLDIFS=$IFS
433 IFS=","
434 for i in $event_name; do
435 traced=$($BABELTRACE_BIN $trace_path 2>/dev/null | grep $i | wc -l)
436 if [ "$traced" -ne 0 ]; then
437 pass "Validate trace for event $i, $traced events"
438 else
439 fail "Validate trace for event $i"
440 diag "Found $traced occurences of $i"
441 fi
442 done
443 ret=$?
444 IFS=$OLDIFS
445 return $ret
446 }
This page took 0.049065 seconds and 6 git commands to generate.