| 1 | #!/bin/bash |
| 2 | # |
| 3 | # Copyright (C) 2015 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com> |
| 4 | # |
| 5 | # SPDX-License-Identifier: LGPL-2.1-only |
| 6 | # |
| 7 | |
| 8 | TEST_DESC="LTTng-crash & shm testing" |
| 9 | |
| 10 | CURDIR=$(dirname $0)/ |
| 11 | TESTDIR=$CURDIR/../../../ |
| 12 | CRASH_BIN="lttng-crash" |
| 13 | |
| 14 | # Test app for ust event |
| 15 | TESTAPP_PATH="$TESTDIR/utils/testapp" |
| 16 | TESTAPP_NAME="gen-ust-events" |
| 17 | TESTAPP_BIN="$TESTAPP_PATH/$TESTAPP_NAME/$TESTAPP_NAME" |
| 18 | NR_USEC_WAIT=0 |
| 19 | NR_ITER=-1 |
| 20 | |
| 21 | # Temp file output |
| 22 | OUTPUT_DIR=$(mktemp -d -t tmp.test_crash_shm.XXXXXX) |
| 23 | |
| 24 | LAST_APP_PID= |
| 25 | |
| 26 | NUM_TESTS=76 |
| 27 | |
| 28 | source $TESTDIR/utils/utils.sh |
| 29 | |
| 30 | # Global declaration for simplification |
| 31 | LTTNG_CRASH=$TESTDIR/../src/bin/lttng-crash/$CRASH_BIN |
| 32 | |
| 33 | # MUST set TESTDIR before calling those functions |
| 34 | plan_tests $NUM_TESTS |
| 35 | |
| 36 | print_test_banner "$TEST_DESC" |
| 37 | |
| 38 | bail_out_if_no_babeltrace |
| 39 | |
| 40 | function start_test_app() |
| 41 | { |
| 42 | local tmp_file=$(mktemp -u -t "tmp.${FUNCNAME[0]}.XXXXXX") |
| 43 | |
| 44 | # Start application with a temporary file. |
| 45 | $TESTAPP_BIN -i $NR_ITER -w $NR_USEC_WAIT --sync-after-first-event $tmp_file & |
| 46 | ret=$? |
| 47 | LAST_APP_PID="${!}" |
| 48 | APPS_PID="${APPS_PID} ${!}" |
| 49 | ok $ret "Start application to trace" |
| 50 | |
| 51 | # Wait for the application file to appear indicating that at least one |
| 52 | # tracepoint has been fired. |
| 53 | while [ ! -f "$tmp_file" ]; do |
| 54 | sleep 0.5 |
| 55 | done |
| 56 | diag "Removing test app temporary file $tmp_file" |
| 57 | rm -rf $tmp_file |
| 58 | } |
| 59 | |
| 60 | function stop_test_apps() |
| 61 | { |
| 62 | diag "Stopping $TESTAPP_NAME" |
| 63 | for p in ${APPS_PID}; do |
| 64 | diag "Stopping $p" |
| 65 | kill ${p} 2>/dev/null |
| 66 | wait ${p} 2>/dev/null |
| 67 | diag "Stopped $p" |
| 68 | done |
| 69 | APPS_PID= |
| 70 | } |
| 71 | |
| 72 | function stop_test_app() |
| 73 | { |
| 74 | local pid="$1" |
| 75 | for p in ${pid}; do |
| 76 | diag "Stopping $p" |
| 77 | kill ${p} 2>/dev/null |
| 78 | wait ${p} 2>/dev/null |
| 79 | diag "Stopped $p" |
| 80 | done |
| 81 | } |
| 82 | |
| 83 | function verify_path_dont_exists() |
| 84 | { |
| 85 | local path=$1 |
| 86 | |
| 87 | while find $path -mindepth 1 -maxdepth 1 &>/dev/null ; do |
| 88 | sleep 2 |
| 89 | done |
| 90 | return 0 |
| 91 | } |
| 92 | |
| 93 | function test_shm_path_per_pid() |
| 94 | { |
| 95 | diag "Shm: ust per-pid test" |
| 96 | local session_name=shm_path_per_pid |
| 97 | local channel_name=channel_per_pid |
| 98 | local shm_path=$(mktemp -d -t "tmp.${FUNCNAME[0]}_shm.XXXXXX") |
| 99 | |
| 100 | # Build up |
| 101 | start_lttng_sessiond |
| 102 | create_lttng_session_ok $session_name $OUTPUT_DIR "--shm-path $shm_path" |
| 103 | enable_ust_lttng_channel_ok $session_name $channel_name "--buffers-pid" |
| 104 | |
| 105 | start_lttng_tracing_ok $session_name |
| 106 | |
| 107 | diag "Shm: clean state" |
| 108 | file_count=$(find $shm_path -mindepth 1 -maxdepth 1 | wc -l) |
| 109 | test $file_count -eq "0" |
| 110 | ok $? "No file created on set-up" |
| 111 | |
| 112 | # Look for per-pid folder structure |
| 113 | # Start first test app |
| 114 | diag "Shm: check folder creation and structure" |
| 115 | |
| 116 | start_test_app |
| 117 | first_app_pid=$LAST_APP_PID |
| 118 | shm_session_path=$(find $shm_path -mindepth 1 -maxdepth 1) |
| 119 | |
| 120 | file_count=$(echo "$shm_session_path"| wc -l) |
| 121 | test $file_count -eq "1" |
| 122 | ok $? "Path $shm_session_path created on application creation" |
| 123 | |
| 124 | first_pid_path=$(find $shm_session_path/ust/pid -mindepth 1 -maxdepth 1) |
| 125 | ok $? "Pid path exists: $first_pid_path" |
| 126 | |
| 127 | file_count=$(find $shm_session_path/ust/pid -mindepth 1 -maxdepth 1 | wc -l) |
| 128 | test $file_count -eq "1" |
| 129 | ok $? "Expect 1 pid registration folder got $file_count" |
| 130 | |
| 131 | # Check for buffer and metadata presence in ust/pid/appfolder |
| 132 | file_count=$(find $first_pid_path/ -mindepth 1 -maxdepth 1 | wc -l) |
| 133 | test $file_count -ne "0" |
| 134 | ok $? "Expect > 0 buffer and metadata files got $file_count" |
| 135 | |
| 136 | # Start second application pid |
| 137 | diag "Shm: check basic creation of second ust application" |
| 138 | |
| 139 | start_test_app |
| 140 | second_app_pid=$LAST_APP_PID |
| 141 | |
| 142 | file_count=$(find $shm_session_path/ust/pid -mindepth 1 -maxdepth 1 | wc -l) |
| 143 | test $file_count -eq "2" |
| 144 | ok $? "Expect 2 pid registration folders got $file_count" |
| 145 | |
| 146 | # Stop first test application and check for cleanup |
| 147 | stop_test_app "$first_app_pid" |
| 148 | APPS_PID="$second_app_pid" |
| 149 | verify_path_dont_exists "$first_pid_path" |
| 150 | ok $? "First pid cleanup" |
| 151 | |
| 152 | # Stop all applications and check for full cleanup |
| 153 | stop_test_apps |
| 154 | verify_path_dont_exists "$shm_session_path" |
| 155 | ok $? "Full cleanup" |
| 156 | |
| 157 | # Tear down |
| 158 | destroy_lttng_session_ok $session_name |
| 159 | stop_lttng_sessiond |
| 160 | rm -rf $shm_path |
| 161 | } |
| 162 | |
| 163 | function test_shm_path_per_uid() |
| 164 | { |
| 165 | diag "Shm: ust per-uid test" |
| 166 | local session_name=shm_path_per_uid |
| 167 | local channel_name=channel_per_uid |
| 168 | set -x |
| 169 | local shm_path=$(mktemp -d -t "tmp.${FUNCNAME[0]}_shm_path.XXXXXX") |
| 170 | set +x |
| 171 | |
| 172 | # Build up |
| 173 | start_lttng_sessiond |
| 174 | create_lttng_session_ok $session_name $OUTPUT_DIR "--shm-path $shm_path" |
| 175 | enable_ust_lttng_channel_ok $session_name $channel_name "--buffers-uid" |
| 176 | |
| 177 | start_lttng_tracing_ok $session_name |
| 178 | |
| 179 | diag "Shm: test clean state" |
| 180 | file_count=$(find $shm_path -mindepth 1 -maxdepth 1 | wc -l) |
| 181 | test $file_count -eq "0" |
| 182 | ok $? "No files created on set-up" |
| 183 | |
| 184 | # Look for per-pid folder structure |
| 185 | # Start first test app |
| 186 | diag "Shm: check folder creation and structure" |
| 187 | |
| 188 | start_test_app |
| 189 | shm_session_path=$(find $shm_path -mindepth 1 -maxdepth 1) |
| 190 | file_count=$(echo "$shm_session_path"| wc -l) |
| 191 | test $file_count -eq "1" |
| 192 | ok $? "Path $shm_session_path created on application creation" |
| 193 | |
| 194 | uid_path=$(find $shm_session_path/ust/uid -mindepth 1 -maxdepth 1) |
| 195 | ok $? "uid path exist found $uid_path" |
| 196 | |
| 197 | file_count=$(find $shm_session_path/ust/uid -mindepth 1 -maxdepth 1 | wc -l) |
| 198 | test $file_count -eq "1" |
| 199 | ok $? "Expect 1 uid registration folder got $file_count" |
| 200 | |
| 201 | # Stop all applications and check for uid presence |
| 202 | stop_test_apps |
| 203 | file_count=$(find $shm_session_path/ust/uid -mindepth 1 -maxdepth 1 | wc -l) |
| 204 | test $file_count -eq "1" |
| 205 | ok $? "Expect 1 uid registration folder got $file_count" |
| 206 | |
| 207 | # Test full cleanup |
| 208 | destroy_lttng_session_ok $session_name |
| 209 | verify_path_dont_exists "$shm_session_path" |
| 210 | ok $? "Full cleanup" |
| 211 | |
| 212 | stop_lttng_sessiond |
| 213 | rm -rf $shm_path |
| 214 | } |
| 215 | |
| 216 | function test_lttng_crash() |
| 217 | { |
| 218 | diag "Lttng-crash: basic recuperation" |
| 219 | local session_name=crash_test |
| 220 | local channel_name=channel_crash |
| 221 | local shm_path=$(mktemp -d -t "tmp.${FUNCNAME[0]}_shm_path.XXXXXX") |
| 222 | local shm_path_symlink=$(mktemp -d -t "tmp.${FUNCNAME[0]}_shm_path_symlink.XXXXXX") |
| 223 | local event_name="tp:tptest" |
| 224 | |
| 225 | # Create a session in snapshot mode to deactivate any use of consumerd |
| 226 | start_lttng_sessiond |
| 227 | create_lttng_session_ok $session_name $OUTPUT_DIR "--shm-path $shm_path --snapshot" |
| 228 | enable_ust_lttng_channel_ok $session_name $channel_name "--buffers-uid" |
| 229 | enable_ust_lttng_event_ok $session_name $event_name $channel_name |
| 230 | start_lttng_tracing_ok $session_name |
| 231 | |
| 232 | # Generate 10 events |
| 233 | $TESTAPP_BIN -i 10 -w 0 |
| 234 | stop_lttng_tracing_ok |
| 235 | |
| 236 | crash_recup_count=$($LTTNG_CRASH $shm_path | wc -l) |
| 237 | test $crash_recup_count -eq "10" |
| 238 | ok $? "Expect 10 recup event from buffers got $crash_recup_count" |
| 239 | |
| 240 | # Test with symlink |
| 241 | cp -rs $shm_path/. $shm_path_symlink |
| 242 | crash_recup_count=$($LTTNG_CRASH $shm_path_symlink | wc -l) |
| 243 | test $crash_recup_count -eq "10" |
| 244 | ok $? "Expect 10 recup event from symlink buffers got $crash_recup_count" |
| 245 | |
| 246 | # Tear down |
| 247 | destroy_lttng_session_ok $session_name |
| 248 | stop_lttng_sessiond |
| 249 | rm -rf $shm_path |
| 250 | rm -rf $shm_path_symlink |
| 251 | } |
| 252 | |
| 253 | function test_lttng_crash_extraction() |
| 254 | { |
| 255 | diag "Lttng-crash: extraction to path" |
| 256 | local session_name=crash_test |
| 257 | local channel_name=channel_crash |
| 258 | local shm_path=$(mktemp -d -t "tmp.${FUNCNAME[0]}_shm_path.XXXXXX") |
| 259 | local extraction_dir_path=$(mktemp -d -t "tmp.${FUNCNAME[0]}_extraction_dir_path.XXXXXX") |
| 260 | local extraction_path=$extraction_dir_path/extract |
| 261 | local event_name="tp:tptest" |
| 262 | |
| 263 | # Create a session in snapshot mode to deactivate any use of consumerd |
| 264 | start_lttng_sessiond |
| 265 | create_lttng_session_ok $session_name $OUTPUT_DIR "--shm-path $shm_path --snapshot" |
| 266 | enable_ust_lttng_channel_ok $session_name $channel_name "--buffers-uid" |
| 267 | enable_ust_lttng_event_ok $session_name $event_name $channel_name |
| 268 | |
| 269 | start_lttng_tracing_ok $session_name |
| 270 | # Generate 10 events |
| 271 | $TESTAPP_BIN -i 10 -w 0 |
| 272 | stop_lttng_tracing_ok |
| 273 | |
| 274 | $LTTNG_CRASH -x $extraction_path $shm_path |
| 275 | ok $? "Extraction of crashed buffers to path" |
| 276 | |
| 277 | # Test extracted trace |
| 278 | trace_match_only $event_name 10 $extraction_path |
| 279 | |
| 280 | # Tear down |
| 281 | destroy_lttng_session_ok $session_name |
| 282 | stop_lttng_sessiond |
| 283 | rm -rf $shm_path |
| 284 | rm -rf $extraction_dir_path |
| 285 | } |
| 286 | |
| 287 | function test_shm_path_per_pid_sigint() |
| 288 | { |
| 289 | diag "Shm: ust per-pid test sigint" |
| 290 | local session_name=shm_path_per_pid |
| 291 | local channel_name=channel_per_pid |
| 292 | local shm_path=$(mktemp -d -t "tmp.${FUNCNAME[0]}_shm_path.XXXXXX") |
| 293 | local num_files=0 |
| 294 | |
| 295 | # Build up |
| 296 | start_lttng_sessiond |
| 297 | create_lttng_session_ok $session_name $OUTPUT_DIR "--shm-path $shm_path" |
| 298 | enable_ust_lttng_channel_ok $session_name $channel_name "--buffers-pid" |
| 299 | |
| 300 | start_lttng_tracing_ok $session_name |
| 301 | |
| 302 | start_test_app |
| 303 | start_test_app |
| 304 | shm_session_path=$(find $shm_path -mindepth 1 -maxdepth 1) |
| 305 | |
| 306 | # Stop sessiond with sigint |
| 307 | stop_lttng_sessiond SIGINT |
| 308 | |
| 309 | # Looking for a full cleanup |
| 310 | verify_path_dont_exists "$shm_session_path" |
| 311 | ok $? "Full cleanup on sigint" |
| 312 | |
| 313 | # Tear down |
| 314 | stop_test_apps |
| 315 | rm -rf $shm_path |
| 316 | } |
| 317 | |
| 318 | function test_shm_path_per_uid_sigint() |
| 319 | { |
| 320 | diag "Shm: ust per-uid test sigint" |
| 321 | local session_name=shm_path_per_uid_sigint |
| 322 | local channel_name=channel_per_uid_sigint |
| 323 | local shm_path=$(mktemp -d -t "tmp.${FUNCNAME[0]}_shm_path.XXXXXX") |
| 324 | local ret=0 |
| 325 | |
| 326 | # Build up |
| 327 | start_lttng_sessiond |
| 328 | create_lttng_session_ok $session_name $OUTPUT_DIR "--shm-path $shm_path" |
| 329 | enable_ust_lttng_channel_ok $session_name $channel_name "--buffers-uid" |
| 330 | |
| 331 | start_lttng_tracing_ok $session_name |
| 332 | |
| 333 | start_test_app |
| 334 | start_test_app |
| 335 | shm_session_path=$(find $shm_path -mindepth 1 -maxdepth 1) |
| 336 | |
| 337 | # Test full cleanup on SIGINT |
| 338 | stop_lttng_sessiond SIGINT |
| 339 | |
| 340 | # Looking for a full cleanup |
| 341 | verify_path_dont_exists "$shm_session_path" |
| 342 | ok $? "Full cleanup on sigint" |
| 343 | |
| 344 | # Tear down |
| 345 | stop_test_apps |
| 346 | rm -rf $shm_path |
| 347 | } |
| 348 | |
| 349 | function test_lttng_crash_extraction_sigkill() |
| 350 | { |
| 351 | diag "Lttng-crash: extraction with sigkill" |
| 352 | local session_name=crash_test |
| 353 | local channel_name=channel_crash |
| 354 | local shm_path=$(mktemp -d -t "tmp.${FUNCNAME[0]}_shm_path.XXXXXX") |
| 355 | local extraction_dir_path=$(mktemp -d -t "tmp.${FUNCNAME[0]}_extraction_dir_path.XXXXXX") |
| 356 | local extraction_path=$extraction_dir_path/extract |
| 357 | local event_name="tp:tptest" |
| 358 | local ret=0 |
| 359 | |
| 360 | start_lttng_sessiond |
| 361 | create_lttng_session_ok $session_name $OUTPUT_DIR "--shm-path $shm_path" |
| 362 | enable_ust_lttng_channel_ok $session_name $channel_name "--buffers-uid" |
| 363 | enable_ust_lttng_event_ok $session_name $event_name $channel_name |
| 364 | start_lttng_tracing_ok $session_name |
| 365 | |
| 366 | # Generate 10 events |
| 367 | $TESTAPP_BIN -i 10 -w 0 |
| 368 | |
| 369 | sigstop_lttng_sessiond |
| 370 | sigstop_lttng_consumerd |
| 371 | |
| 372 | # Kill the consumers then sessiond with sigkill |
| 373 | stop_lttng_consumerd SIGKILL |
| 374 | stop_lttng_sessiond SIGKILL |
| 375 | |
| 376 | $LTTNG_CRASH -x $extraction_path $shm_path |
| 377 | ret=$? |
| 378 | ok $ret "Extraction of crashed buffers to path $extraction_path" |
| 379 | |
| 380 | # Test extracted trace |
| 381 | trace_match_only $event_name 10 $extraction_path |
| 382 | |
| 383 | # Tear down |
| 384 | stop_test_apps |
| 385 | rm -rf $shm_path |
| 386 | rm -rf $extraction_dir_path |
| 387 | } |
| 388 | |
| 389 | function interrupt_cleanup() |
| 390 | { |
| 391 | diag "*** Cleaning-up test ***" |
| 392 | stop_test_apps |
| 393 | full_cleanup |
| 394 | } |
| 395 | |
| 396 | TESTS=( |
| 397 | test_shm_path_per_pid |
| 398 | test_shm_path_per_uid |
| 399 | test_shm_path_per_pid_sigint |
| 400 | test_shm_path_per_uid_sigint |
| 401 | test_lttng_crash |
| 402 | test_lttng_crash_extraction |
| 403 | test_lttng_crash_extraction_sigkill |
| 404 | ) |
| 405 | |
| 406 | trap interrupt_cleanup SIGTERM SIGINT |
| 407 | |
| 408 | for fct_test in ${TESTS[@]}; |
| 409 | do |
| 410 | ${fct_test} |
| 411 | if [ $? -ne 0 ]; then |
| 412 | break; |
| 413 | fi |
| 414 | done |
| 415 | rm -rf $OUTPUT_DIR |
| 416 | |
| 417 | OUTPUT_DEST=/dev/null 2>&1 |