Various improvements to dump-init.sh (#13177)
Blue committed
Jun 26, 2025 at 11:55 UTC
5e88d9acbf66cf7e01996e697fa7105d00ae6cd5
1 file changed
+54
-18
diagnostics/dump-init.sh
+54
-18
@@ -3,35 +3,71 @@
3
set -ue
4
5
if [ $(whoami) != "root" ]; then
6
- echo "This script must be run as root in the system distro (via wsl.exe -u root --system)"
6
+ echo "This script must be run as root in the debug shell (via wsl.exe -u root --debug-shell)"
7
exit 1
8
fi
9
10
-ts=$(date +%F_%H-%M-%S)
11
-target="/mnt/c/wsl-init-dump-$ts"
10
+dmesg
11
13
-mkdir -p "$target"
12
+# Try to install gdb
13
15
-tdnf install -y gdb
14
+if [ "${dump:-0}" == 1 ]; then
15
+ tdnf install -y gdb || true
16
+fi
17
+
18
+declare -a pids_to_dump
19
+
20
+for proc in /proc/[0-9]*; do
21
+ read -a stats < "$proc/stat" # Skip kernel threads to make the output easier to read
22
+ flags=${stats[8]}
23
+
24
+ if (( ("$flags" & 0x00200000) == 0x00200000 )); then
25
+ continue
26
+ fi
27
+
28
+ pid=$(basename "$proc")
29
17
-bash -c "cd $target && gcore -a \$(pgrep init)"
30
+ if [ "${dump:-0}" == 1 ]; then
31
+ pids_to_dump+=("$pid")
32
+ fi
33
+
34
+ parent=$(ps -o ppid= -p "$pid")
35
+
36
+ echo -e "\nProcess: $pid (parent: $parent) "
37
+ echo -en "cmd: "
38
+ cat "/proc/$pid/cmdline" || true
39
+ echo -e "\nstat: "
40
+ cat "/proc/$pid/stat" || true
41
19
-stack_log="$target/stacks.txt"
20
-fd_log="$target/fd.txt"
21
-for pid in $(pgrep init); do
22
- echo -e "\nProcess: $pid" >> "$stack_log"
23
- echo -e "\nProcess: $pid" >> "$fd_log"
42
for tid in $(ls "/proc/$pid/task" || true); do
25
- echo "tid: $tid" >> "$stack_log"
26
- cat "/proc/$pid/task/$tid/stack" >> "$stack_log" || true
43
+ echo -n "tid: $tid - "
44
+ cat "/proc/$pid/task/$tid/comm" || true
45
+ cat "/proc/$pid/task/$tid/stack" || true
46
done
47
29
- ls -la "/proc/$pid/fd" >> "$fd_log" || true
48
+ echo "fds: "
49
+ ls -la "/proc/$pid/fd" || true
50
done
51
52
+for pid in "${pids_to_dump[@]}" ; do
53
+ name=$(ps -p "$pid" -o comm=)
54
+ if [[ "$name" =~ ^(bash|login)$ ]]; then
55
+ echo "Skipping dump for process: $name"
56
+ continue
57
+ fi
58
+
59
+ echo "Dumping process: $name ($pid) "
60
+ if gcore -a -o core "$pid" ; then
61
+ if ! /wsl-capture-crash 0 "$name" "$pid" 0 < "core.$pid" ; then
62
+ echo "Failed to dump process $pid"
63
+ fi
64
+
65
+ rm "core.$pid"
66
+ fi
67
+done
68
33
-ss -lap --vsock > "/$target/sockets.txt"
34
-dmesg > "/$target/dmesg.txt"
35
-cat /proc/meminfo > "/$target/meminfo.txt"
69
+echo "hvsockets: "
70
+ss -lap --vsock
71
37
-echo "Logs and dumps written in $target"
72
+echo "meminfo: "
73
+cat /proc/meminfo