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