#!/bin/sh
# Writes boot diagnostics to /boot (separate fs) and /var/log, repeatedly for a few minutes.
for n in 1 2 3 4 5 6 7 8; do
  OUT=/tmp/boot-debug.txt
  {
    echo "=== dump $n  date=$(date)  uptime=$(cut -d' ' -f1 /proc/uptime)"
    echo "--- mounts"; grep -E " / | /boot " /proc/mounts
    echo "--- lsmod"; lsmod | head -70
    echo "--- platform devs (usb/mt6360)"; ls /sys/bus/platform/devices | grep -iE "usb|mt6360|charger" | tr "\n" " "; echo
    echo "--- deferred"; cat /sys/kernel/debug/devices_deferred 2>&1 | head -20
    echo "--- modules-load"; journalctl -u systemd-modules-load -b --no-pager 2>&1 | tail -15
    echo "--- dmesg mt6360/charger/deferred"; dmesg | grep -iE "mt6360|charger|deferred|wmt|wlan|btif|hci" | tail -40
    echo "--- ip link/addr"; ip -br link; ip -br addr
    echo "--- gadget"; ls /sys/kernel/config/usb_gadget/ 2>&1; echo "UDC=[$(cat /sys/kernel/config/usb_gadget/g1/UDC 2>&1)]"; ls /sys/class/udc 2>&1
    echo "--- failed units"; systemctl --failed --no-pager 2>&1
    echo "--- usb-gadget.service"; journalctl -u usb-gadget -b --no-pager 2>&1 | tail -40
    echo "--- warnings (journal)"; journalctl -b -p warning --no-pager 2>&1 | tail -100
    echo "--- dmesg usb"; dmesg | grep -iE "usb|gadget|musb|udc|ncm|u_ether|tcpc|typec" | tail -60
    echo "--- dmesg tail"; dmesg | tail -50
  } > $OUT 2>&1
  cp $OUT /boot/boot-debug.txt 2>/dev/null; cp $OUT /var/log/boot-debug.txt 2>/dev/null; sync
  sleep 30
done
