jjb: Add env and os details printing to build jobs
[lttng-ci.git] / scripts / lttng-tools / hang_processes.sh
index d64fa9c1da3e5099932dabd15c2a4452c1952711..3e52a607a3ddb6a3396ff17d680abff3be9d8fba 100755 (executable)
@@ -1,4 +1,4 @@
-#!/bin/bash -exu
+#!/bin/bash
 #
 # Copyright (C) 2018 - Jonathan Rajotte-Julien <jonathan.rajotte-julien@efficios.com>
 #
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+set -exu
+
 PGREP=pgrep
 pids=""
-dependencies=""
 file_list=$(mktemp)
 ret=0
 
+# WORKSPACE is normally set by Jenkins.  Use the current directory otherwise,
+# like when testing this script manually.
+WORKSPACE=${WORKSPACE:-$PWD}
+
 lttng_processes="$("$PGREP" -l 'lttng|gen-ust-.+')" || true
-if [ ! -z "$lttng_processes" ]; then
+if [ -n "$lttng_processes" ]; then
 
     pids="$(cut -d ' ' -f 1 <<< "$lttng_processes" | tr '\n' ' ')"
-    comma_pids=$(tr ' ' ',' <<< "$pids")
     echo "The following LTTng processes were detected running on the system and will be aborted:"
     echo "$lttng_processes"
 
     # Stop the processes to make sure everything is frozen
     kill -SIGSTOP $pids
-
-    # Get dependencies for coredump analysis
-    # Use /proc/$PID/exe and ldd to get all shared libs necessary
-    array=(${pids})
-    # Add the /proc/ prefix using parameter expansion
-    array=("${array[@]/#/\/proc\/}")
-    # Add the /exe suffix using parameter expansion
-    array=("${array[@]/%/\/exe}")
-    dependencies=$(ldd "${array[@]}" | grep -v "not found")
-    dependencies=$(awk '/=>/{print$(NF-1)}' <<< "$dependencies" | sort | uniq)
-
     kill -SIGABRT $pids
     kill -SIGCONT $pids
     ret=1
 fi
 
-core_files=$(find "/tmp" -name "core\.[0-9]*" -type f 2>/dev/null) || true
-if [ ! -z "$core_files" ]; then
-    echo "$core_files" >> "$file_list"
-    echo "$dependencies" >> "$file_list"
+# Add the file passed as $1 to the list of files to collect.
+#
+# If that file is a symlink, follow it and collect the target, recursively.
+
+function collect_recursive
+{
+    file_to_collect=$1
 
-    # Make sure the coredump is finished using fuser
-    for core in $core_files; do
-        while fuser "$core"; do
-            sleep 1
-        done
+    if [ -f "$file_to_collect" ]; then
+        echo "$file_to_collect" >> "$file_list"
+
+        if [ -L "$file_to_collect" ]; then
+            collect_recursive "$(readlink "$file_to_collect")"
+        fi
+    fi
+}
+
+# For each core file...
+while read -r core_file; do
+    # Make sure the coredump is finished using fuser.
+    while fuser "$core_file"; do
+        sleep 1
+    done
+
+    # Collect everything in the core file that looks like a reference to a
+    # shared lib.
+    strings "$core_file" | grep '^/.*\.so.*' | while read -r str; do
+        collect_recursive "$str"
     done
 
-    tar cfzh "${WORKSPACE}/build/core.tar.gz" -T "$file_list"
-    rm -rf $core_files
+    echo "$core_file" >> $file_list
     ret=1
+done < <(find "/tmp" -maxdepth 1 -name "core\.[0-9]*" -type f 2>/dev/null)
+
+# If we recorded some files to collect, pack them up.
+if [ -s "$file_list" ]; then
+    mkdir -p "${WORKSPACE}/build"
+    tar cfzh "${WORKSPACE}/build/core.tar.gz" -T <(sort "$file_list" | uniq)
 fi
 
-rm -rf "$file_list"
+# Remove core file
+while read -r core_file; do
+       rm -rf "$core_file"
+done < <(find "/tmp" -maxdepth 1 -name "core\.[0-9]*" -type f 2>/dev/null)
+
+rm -f "$file_list"
 exit $ret
This page took 0.037857 seconds and 4 git commands to generate.