| 1 | #!/bin/bash |
| 2 | # |
| 3 | # SPDX-FileCopyrightText: 2018 Jonathan Rajotte-Julien <jonathan.rajotte-julien@efficios.com> |
| 4 | # |
| 5 | # SPDX-License-Identifier: GPL-2.0-or-later |
| 6 | |
| 7 | set -exu |
| 8 | |
| 9 | # Add the file passed as $1 to the list of files to collect. |
| 10 | # |
| 11 | # If that file is a symlink, follow it and collect the target, recursively. |
| 12 | function collect_recursive |
| 13 | { |
| 14 | file_to_collect=$1 |
| 15 | |
| 16 | if [ -f "$file_to_collect" ]; then |
| 17 | echo "$file_to_collect" >> "$file_list" |
| 18 | |
| 19 | if [ -L "$file_to_collect" ]; then |
| 20 | collect_recursive "$(readlink "$file_to_collect")" |
| 21 | fi |
| 22 | fi |
| 23 | } |
| 24 | |
| 25 | pids=() |
| 26 | file_list=$(mktemp -t "postbuild_file_list.XXXXXX") |
| 27 | ret=0 |
| 28 | |
| 29 | # WORKSPACE is normally set by Jenkins. Use the current directory otherwise, |
| 30 | # like when testing this script manually. |
| 31 | WORKSPACE=${WORKSPACE:-$PWD} |
| 32 | |
| 33 | lttng_processes="$(pgrep -l 'lttng|gen-ust-.+')" || true |
| 34 | if [ -n "$lttng_processes" ]; then |
| 35 | |
| 36 | echo "The following LTTng processes were detected running on the system and will be aborted:" |
| 37 | echo "$lttng_processes" |
| 38 | |
| 39 | # Build the pids array |
| 40 | while read -r pid; do |
| 41 | pids+=("$pid") |
| 42 | done < <(cut -d ' ' -f 1 <<< "$lttng_processes") |
| 43 | |
| 44 | # Abort the leftover processes to generate core files |
| 45 | kill -SIGSTOP "${pids[@]}" |
| 46 | kill -SIGABRT "${pids[@]}" |
| 47 | kill -SIGCONT "${pids[@]}" |
| 48 | |
| 49 | # Exit with failure when leftover processes are found |
| 50 | ret=1 |
| 51 | fi |
| 52 | |
| 53 | # For each core file... |
| 54 | while read -r core_file; do |
| 55 | sleep_count=0 |
| 56 | |
| 57 | # Make sure the coredump is finished using fuser |
| 58 | while fuser "$core_file"; do |
| 59 | sleep 1 |
| 60 | sleep_count+=1 |
| 61 | |
| 62 | # Skip the core file if it takes more than 30 seconds |
| 63 | if [ "$sleep_count" -ge 30 ]; then |
| 64 | continue |
| 65 | fi |
| 66 | done |
| 67 | |
| 68 | # Print a full backtrace of all threads |
| 69 | if command -v gdb >/dev/null 2>&1; then |
| 70 | gdb -nh -c "$core_file" -ex "thread apply all bt full" -ex q |
| 71 | fi |
| 72 | |
| 73 | # Collect everything in the core file that looks like a reference to a |
| 74 | # shared lib |
| 75 | strings "$core_file" | grep '^/.*\.so.*' | while read -r str; do |
| 76 | collect_recursive "$str" |
| 77 | done |
| 78 | |
| 79 | echo "$core_file" >> "$file_list" |
| 80 | |
| 81 | # Exit with failure when core files are found |
| 82 | ret=1 |
| 83 | done < <(find "/tmp" -maxdepth 1 -name "core\.[0-9]*" -type f 2>/dev/null) |
| 84 | |
| 85 | # If we recorded some files to collect, pack them up. |
| 86 | if [ -s "$file_list" ]; then |
| 87 | mkdir -p "${WORKSPACE}/build" |
| 88 | tar cJfh "${WORKSPACE}/build/core.tar.xz" -T <(sort "$file_list" | uniq) |
| 89 | fi |
| 90 | |
| 91 | # Remove core files |
| 92 | find "/tmp" -maxdepth 1 -name "core\.[0-9]*" -type f -delete || true |
| 93 | |
| 94 | rm -f "$file_list" |
| 95 | |
| 96 | exit $ret |