Merge pull request #10 from amboar/netboot/buildroot

netboot: Minor fixes to accommodate rebooting from buildroot, failures in u-boot
diff --git a/amboar/obmc-scripts/tracing/README.md b/amboar/obmc-scripts/tracing/README.md
new file mode 100644
index 0000000..5ee628f
--- /dev/null
+++ b/amboar/obmc-scripts/tracing/README.md
@@ -0,0 +1,83 @@
+Description
+===========
+
+The trace script automates the process of enabling named sets of tracepoints
+and dumping the trace file over the SSH connection, then cleaning up after
+itself.
+
+Usage
+=====
+
+To run:
+
+```
+Usage: ./trace [USER@]HOST EVENTSET [EVENTSET...]
+
+Valid EVENTSETs: fsi, occ, sbefifo, timer, sched
+
+```
+
+To stop the trace output and disable the tracepoints, hit `Return`.
+
+For example, capturing the defined tracepoints for the `sched` event set:
+
+```
+$ ./trace root@my-bmc sched
++ set -eou pipefail
++ set -x
++ EVENT_fsi='fsi fsi_master_gpio'
++ EVENT_occ='occ hwmon_occ'
++ EVENT_sbefifo=sbefifo
++ EVENT_timer='timer/timer_start timer/timer_cancel timer/timer_expire_entry timer/timer_expire_exit'
++ EVENT_sched='sched/sched_switch sched/sched_wakeup sched/sched_wakeup_new sched/sched_waking'
++ trap on_exit EXIT
++ CAT_PID=1867
++ for elem in sched
++ eval 'trace=\${EVENT_${elem}}'
+++ trace='${EVENT_sched}'
+++ eval echo '${EVENT_sched}'
++++ echo sched/sched_switch sched/sched_wakeup sched/sched_wakeup_new sched/sched_waking
++ for event in '$(eval echo ${trace})'
++ echo 1
++ for event in '$(eval echo ${trace})'
++ echo 1
++ for event in '$(eval echo ${trace})'
++ echo 1
++ for event in '$(eval echo ${trace})'
++ echo 1
++ echo 1
++ read
++ cat /sys/kernel/debug/tracing/per_cpu/cpu0/trace_pipe
+              sh-1866  [000] d.h. 201821.030000: sched_waking: comm=rngd pid=987 prio=120 target_cpu=000
+              sh-1866  [000] dnh. 201821.030000: sched_wakeup: comm=rngd pid=987 prio=120 target_cpu=000
+              sh-1866  [000] dn.. 201821.030000: sched_waking: comm=ksoftirqd/0 pid=6 prio=120 target_cpu=000
+              sh-1866  [000] dn.. 201821.030000: sched_wakeup: comm=ksoftirqd/0 pid=6 prio=120 target_cpu=000
+              sh-1866  [000] d... 201821.030000: sched_switch: prev_comm=sh prev_pid=1866 prev_prio=120 prev_state=R ==> next_comm=rngd next_pid=987 next_prio=120
+            rngd-987   [000] d... 201821.030000: sched_switch: prev_comm=rngd prev_pid=987 prev_prio=120 prev_state=S ==> next_comm=ksoftirqd/0 next_pid=6 next_prio=120
+     ksoftirqd/0-6     [000] d... 201821.030000: sched_switch: prev_comm=ksoftirqd/0 prev_pid=6 prev_prio=120 prev_state=S ==> next_comm=sh next_pid=1866 next_prio=120
+              sh-1866  [000] d.h. 201821.030000: sched_waking: comm=phosphor-hwmon- pid=1188 prio=120 target_cpu=000
+              sh-1866  [000] dnh. 201821.030000: sched_wakeup: comm=phosphor-hwmon- pid=1188 prio=120 target_cpu=000
+              sh-1866  [000] d... 201821.030000: sched_switch: prev_comm=sh prev_pid=1866 prev_prio=120 prev_state=R ==> next_comm=phosphor-hwmon- next_pid=1188 next_prio=120
+ phosphor-hwmon--1188  [000] d... 201821.030000: sched_switch: prev_comm=phosphor-hwmon- prev_pid=1188 prev_prio=120 prev_state=D ==> next_comm=sh next_pid=1866 next_prio=120
+...
+<RETURN>
+...
++ kill 1867
++ on_exit
++ for elem in sched
++ eval 'trace=\${EVENT_${elem}}'
+++ trace='${EVENT_sched}'
+++ eval echo '${EVENT_sched}'
++++ echo sched/sched_switch sched/sched_wakeup sched/sched_wakeup_new sched/sched_waking
++ for event in '$(eval echo ${trace})'
++ echo 0
++ for event in '$(eval echo ${trace})'
++ echo 0
++ for event in '$(eval echo ${trace})'
++ echo 0
++ for event in '$(eval echo ${trace})'
++ echo 0
++ echo 0
++ on_exit
++ rm -f obmc-fsi-trace.Rz15GL
+```
diff --git a/amboar/obmc-scripts/tracing/trace b/amboar/obmc-scripts/tracing/trace
new file mode 100755
index 0000000..89840e1
--- /dev/null
+++ b/amboar/obmc-scripts/tracing/trace
@@ -0,0 +1,77 @@
+#!/bin/bash
+#
+# SPDX-License-Identifier: Apache-2.0
+# Copyright (C) 2018 IBM Corp.
+
+set -eou pipefail
+
+if [ $# -lt 2 ]
+then
+	echo Usage: $0 [USER@]HOST EVENTSET [EVENTSET...]
+	echo
+	echo Valid EVENTSETs: fsi, occ, sbefifo, timer, sched
+	exit 1
+fi
+
+TRACE_TARGET="$1"
+shift 1
+TRACE_EVENT_SET="$@"
+
+TRACESCRIPT_PATH="$(mktemp obmc-fsi-trace.XXXXXX)"
+TRACESCRIPT="$(basename "${TRACESCRIPT_PATH}")"
+
+on_exit() {
+	rm -f "${TRACESCRIPT_PATH}"
+}
+
+trap on_exit EXIT
+
+cat > "${TRACESCRIPT_PATH}" <<-EOF
+set -eou pipefail
+
+set -x
+
+# Look Ma! Associative arrays in ash!
+EVENT_fsi="fsi fsi_master_gpio"
+EVENT_occ="occ hwmon_occ"
+EVENT_sbefifo="sbefifo"
+EVENT_timer="timer/timer_start timer/timer_cancel timer/timer_expire_entry timer/timer_expire_exit"
+EVENT_sched="sched/sched_switch sched/sched_wakeup sched/sched_wakeup_new sched/sched_waking"
+
+on_exit() {
+for elem in ${TRACE_EVENT_SET}
+do
+	# Abuse eval(1) to extract the list of tracepoints to enable from our
+	# fake associative arrays
+	eval 'trace=\\\${EVENT_\${elem}}'
+	for event in \$(eval echo \${trace})
+	do
+		echo 0 > /sys/kernel/debug/tracing/events/\${event}/enable
+	done
+done
+	echo 0 > /sys/kernel/debug/tracing/tracing_on
+}
+
+trap on_exit EXIT
+
+cat /sys/kernel/debug/tracing/per_cpu/cpu0/trace_pipe &
+CAT_PID=\$!
+
+for elem in ${TRACE_EVENT_SET}
+do
+	# See comment in the disable path
+	eval 'trace=\\\${EVENT_\${elem}}'
+	for event in \$(eval echo \${trace})
+	do
+		echo 1 > /sys/kernel/debug/tracing/events/\${event}/enable
+	done
+done
+echo 1 > /sys/kernel/debug/tracing/tracing_on
+
+read
+
+kill \${CAT_PID}
+EOF
+
+scp "${TRACESCRIPT_PATH}" "${TRACE_TARGET}":/tmp/"${TRACESCRIPT}"
+ssh "${TRACE_TARGET}" "/bin/sh -x /tmp/'${TRACESCRIPT}'"
diff --git a/amboar/obmc-scripts/witherspoon-debug/README.md b/amboar/obmc-scripts/witherspoon-debug/README.md
new file mode 100644
index 0000000..3840aba
--- /dev/null
+++ b/amboar/obmc-scripts/witherspoon-debug/README.md
@@ -0,0 +1,33 @@
+Description
+===========
+
+The OpenBMC debugtools tarball clashes with the filesystem layout of
+witherspoon systems, and contains a number of other outstanding issues with
+respect to the tools it deploys. The debug script here documents and executes
+the workarounds necessary to make it functional.
+
+Usage
+-----
+```
+./debug [USER@]HOST TARBALL
+```
+
+For example:
+
+```
+$ ./debug root@my-witherspoon obmc-phosphor-debug-tarball-witherspoon.tar.xz
+```
+
+Notes
+-----
+
+The script will:
+
+1. Ensure `/usr/local` is a tmpfs mountpoint (may not be if system is
+   configured for Field Mode)
+2. Deploy the debugtools tarball to `/usr/local`
+3. Make `perf(1)` work by
+   1. Installing a fake `expand(1)` if necessary
+   2. Fixing the `objdump(1)` symlink
+   3. Disabling `perf(1)`'s buildid tracking (fills the RW filesystem)
+4. Make `latencytop(1)` work by deploying and symlinking libncurses{,w}.so
diff --git a/amboar/obmc-scripts/witherspoon-debug/debug b/amboar/obmc-scripts/witherspoon-debug/debug
new file mode 100755
index 0000000..42c7581
--- /dev/null
+++ b/amboar/obmc-scripts/witherspoon-debug/debug
@@ -0,0 +1,93 @@
+#!/bin/bash
+#
+# SPDX-License-Identifier: Apache-2.0
+# Copyright (C) 2018 IBM Corp.
+
+set -eou pipefail
+
+if [ $# -lt 2 ]
+then
+	echo Usage: $0 [user@][host] [tarball]
+	exit 1
+fi
+
+TARGET="$1"
+DEBUGTOOLS_PATH="$2"
+DEBUGTOOLS="$(basename "${DEBUGTOOLS_PATH}")"
+
+scp "${DEBUGTOOLS_PATH}" libncurses.so.5.9 libncursesw.so.5.9 "${TARGET}":/tmp/
+ssh "${TARGET}" <<-EOF
+set -eou pipefail
+
+set -x
+
+on_exit() {
+	rm -f /tmp/'${DEBUGTOOLS}'
+	rm -f /tmp/libncurses.so.5.9 /tmp/libncursesw.so.5.9
+}
+
+trap on_exit EXIT
+
+# Deal with field mode
+if ! mountpoint /usr/local
+then
+	systemctl unmask usr-local.mount
+	systemctl start usr-local.mount
+fi
+
+# Untar debug tools tarball into /usr/local
+pushd /usr/local
+tar -xvJf /tmp/"${DEBUGTOOLS}"
+popd
+
+# Work around bugs preventing perf from working
+# Tracking issue: https://github.com/openbmc/openbmc/issues/2880
+
+# Fake expand(1): https://github.com/openbmc/openbmc/issues/2879
+mkdir -p bin
+pushd /usr/local/bin
+# which expand isn't enough as expand might be a symlink to a busybox not built with CONFIG_EXPAND
+if ! echo "" | expand
+then
+	# Remove what is likely a symlink to busybox before trying to overwrite
+	# file because:
+	#
+	# 1) We get errors through trying to write to a read-only filesystem
+	# 2) If the filesystem were read-write, we would overwrite the busybox binary
+	[ -f expand ] && rm expand
+	# Fake out expand(1) in the cheapest way possible
+	echo -e "#!/bin/sh\ncat" > expand
+	chmod +x expand
+fi
+
+# Fix the broken objdump link: https://github.com/openbmc/openbmc/issues/2878
+ln -sf arm-openbmc-linux-gnueabi-objdump objdump
+popd
+
+# perf-config(1) is terrible and won't write your configuration unless the
+# configuration file exists
+touch \${HOME}/.perfconfig
+
+# Disable writing junk to \${HOME}, otherwise we fill up the RW volume and
+# cause systemic failures.
+LD_LIBRARY_PATH=/usr/local/lib /usr/local/bin/perf config --user buildid.dir=/dev/null
+
+# Deal with latencytop's missing shared object dependency on ncurses, tracked by
+# https://github.com/openbmc/openbmc/issues/2430
+pushd /usr/local/lib
+if [ -f /tmp/libncurses.so.5.9 -o -f /tmp/libncursesw.so.5.9 ]
+then
+	cp /tmp/libncurses.so.5.9 /tmp/libncursesw.so.5.9 .
+	ln -sf libncurses.so.5.9 libncurses.so.5
+	ln -sf libncurses.so.5.9 libncurses.so
+	ln -sf libncursesw.so.5.9 libncursesw.so.5
+	ln -sf libncursesw.so.5.9 libncursesw.so
+fi
+popd
+EOF
+
+echo
+echo Make sure to run the following in your shell:
+echo
+echo export LD_LIBRARY_PATH=/usr/local/lib PYTHONPATH=/usr/local/lib/python2.7
+echo
diff --git a/amboar/obmc-scripts/witherspoon-debug/libncurses.so.5.9 b/amboar/obmc-scripts/witherspoon-debug/libncurses.so.5.9
new file mode 100755
index 0000000..f0b6ec1
--- /dev/null
+++ b/amboar/obmc-scripts/witherspoon-debug/libncurses.so.5.9
Binary files differ
diff --git a/amboar/obmc-scripts/witherspoon-debug/libncursesw.so.5.9 b/amboar/obmc-scripts/witherspoon-debug/libncursesw.so.5.9
new file mode 100755
index 0000000..9388407
--- /dev/null
+++ b/amboar/obmc-scripts/witherspoon-debug/libncursesw.so.5.9
Binary files differ