bbdbg: Debug a target environment generated by bitbake

bbdbg does the legwork to give you a gdb instance tailored for a target
environment generated from a local bitbake build tree. This saves
the time otherwise spent by building and installing an SDK for the same.

Documentation:

```
3 15:52:42 andrew@mistburn:~/src/openbmc/openbmc-tools/bbdbg (bbdbg) $ ./bbdbg
./bbdbg: 26: 1: parameter not set
NAME
        bbdbg - debug applications in a target environment built by bitbake

SYNOPSIS
        bbdbg PATH FILE CORE PACKAGES

DESCRIPTION
        PATH is the path to the root of a bitbake build directory
        FILE is the absolute path to the binary of interest in the target environment
        CORE is an optional core file generated by FILE. Pass '-' for no core file
        PACKAGES will be used to populate a temporary rootfs for debugging FILE

EXAMPLE
        bbdbg ~/src/openbmc/openbmc/build/p10bmc \
                /usr/bin/nvmesensor - \
                dbus-sensors dbus-sensors-dbg
```

Example use:

```
3 15:50:40 andrew@mistburn:~/src/openbmc/openbmc-tools/bbdbg (bbdbg) $ ./bbdbg ~/src/openbmc/openbmc/build/p10bmc /usr/bin/nvmesensor obmcdump_3_1641877435/core.nvmesensor.0.db0e4de236a6448e9005e85a0dffb348.1554.1641877432000000 dbus-sensors dbus-sensors-dbg
+ bbdbg_opkg update
+ LD_LIBRARY_PATH=/home/andrew/src/openbmc/openbmc/build/p10bmc/tmp/sysroots-components/x86_64/libsolv-native/usr/lib /home/andrew/src/openbmc/openbmc/build/p10bmc/tmp/sysroots-components/x86_64/opkg-native/usr/bin/opkg -V0 -f /home/andrew/src/openbmc/openbmc/build/p10bmc/tmp/work/p10bmc-openbmc-linux-gnueabi/obmc-phosphor-image/1.0-r0/opkg.conf -o /tmp/bbdbg.375 update
+ bbdbg_opkg install dbus-sensors dbus-sensors-dbg
+ LD_LIBRARY_PATH=/home/andrew/src/openbmc/openbmc/build/p10bmc/tmp/sysroots-components/x86_64/libsolv-native/usr/lib /home/andrew/src/openbmc/openbmc/build/p10bmc/tmp/sysroots-components/x86_64/opkg-native/usr/bin/opkg -V0 -f /home/andrew/src/openbmc/openbmc/build/p10bmc/tmp/work/p10bmc-openbmc-linux-gnueabi/obmc-phosphor-image/1.0-r0/opkg.conf -o /tmp/bbdbg.375 install dbus-sensors dbus-sensors-dbg
+ [ - = obmcdump_3_1641877435/core.nvmesensor.0.db0e4de236a6448e9005e85a0dffb348.1554.1641877432000000 ]
+ echo obmcdump_3_1641877435/core.nvmesensor.0.db0e4de236a6448e9005e85a0dffb348.1554.1641877432000000
+ gdb-multiarch -q -iex set solib-absolute-prefix /tmp/bbdbg.375 -iex add-auto-load-safe-path /tmp/bbdbg.375 /tmp/bbdbg.375/usr/bin/nvmesensor obmcdump_3_1641877435/core.nvmesensor.0.db0e4de236a6448e9005e85a0dffb348.1554.1641877432000000
Reading symbols from /tmp/bbdbg.375/usr/bin/nvmesensor...
Reading symbols from /tmp/bbdbg.375/usr/bin/.debug/nvmesensor...
[New LWP 1554]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `/usr/bin/nvmesensor'.
Program terminated with signal SIGSEGV, Segmentation fault.
 #0  0x76b88f50 in epoll_wait (epfd=<optimised out>, events=0x7edf1478, maxevents=128, timeout=-1) at ../sysdeps/unix/sysv/linux/epoll_wait.c:30
30      ../sysdeps/unix/sysv/linux/epoll_wait.c: No such file or directory.
(gdb) bt
 #0  0x76b88f50 in epoll_wait (epfd=<optimised out>, events=0x7edf1478, maxevents=128, timeout=-1) at ../sysdeps/unix/sysv/linux/epoll_wait.c:30
 #1  0x0044adf4 in boost::asio::detail::epoll_reactor::run (ops=..., usec=<optimised out>, this=<optimised out>) at /usr/include/boost/asio/detail/impl/epoll_reactor.ipp:501
 #2  boost::asio::detail::scheduler::do_run_one (ec=..., this_thread=..., lock=<synthetic pointer>..., this=0x17b1ea8) at /usr/include/boost/asio/detail/impl/scheduler.ipp:470
 #3  boost::asio::detail::scheduler::run (ec=..., this=0x17b1ea8) at /usr/include/boost/asio/detail/impl/scheduler.ipp:204
 #4  boost::asio::io_context::run (this=0x7edf1234, this=0x7edf1234) at /usr/include/boost/asio/impl/io_context.ipp:63
 #5  main () at ../../../../../../workspace/sources/dbus-sensors/src/NVMeSensorMain.cpp:274
(gdb) quit
+ set +x
3 15:51:10 andrew@mistburn:~/src/openbmc/openbmc-tools/bbdbg (bbdbg) $
```

Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
Change-Id: I82f5872742253351c7ac8d25b479ac8194e07afd
diff --git a/bbdbg/bbdbg b/bbdbg/bbdbg
new file mode 100755
index 0000000..601ed74
--- /dev/null
+++ b/bbdbg/bbdbg
@@ -0,0 +1,54 @@
+#!/bin/sh
+
+set -eu
+
+bbdbg_help() {
+  /bin/echo -e "NAME"
+  /bin/echo -e "\tbbdbg - debug applications in a target environment built by bitbake"
+  /bin/echo -e
+  /bin/echo -e "SYNOPSIS"
+  /bin/echo -e "\tbbdbg PATH FILE CORE PACKAGES"
+  /bin/echo -e
+  /bin/echo -e "DESCRIPTION"
+  /bin/echo -e "\tPATH is the path to the root of a bitbake build directory"
+  /bin/echo -e "\tFILE is the absolute path to the binary of interest in the target environment"
+  /bin/echo -e "\tCORE is an optional core file generated by FILE. Pass '-' for no core file"
+  /bin/echo -e "\tPACKAGES will be used to populate a temporary rootfs for debugging FILE"
+  /bin/echo -e
+  /bin/echo -e "EXAMPLE"
+  /bin/echo -e "\tbbdbg ~/src/openbmc/openbmc/build/p10bmc \\"
+  /bin/echo -e "\t\t/usr/bin/nvmesensor - \\"
+  /bin/echo -e "\t\tdbus-sensors dbus-sensors-dbg"
+}
+
+trap bbdbg_help EXIT
+
+BBDBG_PATH=$1; shift
+BBDBG_FILE=$1; shift
+BBDBG_CORE=$1; shift
+BBDBG_PKGS=$@
+
+BBDBG_ROOT=$(mktemp -t --directory bbdbg.XXX)
+BBDBG_LIBS=${BBDBG_PATH}/tmp/sysroots-components/$(uname -m)/libsolv-native/usr/lib
+BBDBG_OPKG=${BBDBG_PATH}/tmp/sysroots-components/$(uname -m)/opkg-native/usr/bin/opkg
+BBDBG_CONF=$(find ${BBDBG_PATH}/tmp/work/*/obmc-phosphor-image -name opkg.conf -exec grep -lE ^src \{\} \;)
+
+bbdbg_cleanup() {
+  rm -rf "$BBDBG_ROOT"
+}
+
+trap bbdbg_cleanup EXIT
+
+bbdbg_opkg() {
+  LD_LIBRARY_PATH=${BBDBG_LIBS} $BBDBG_OPKG -V0 -f $BBDBG_CONF -o $BBDBG_ROOT $@
+}
+
+set -x
+bbdbg_opkg update
+bbdbg_opkg install $BBDBG_PKGS
+gdb-multiarch -q \
+  -iex "set solib-absolute-prefix $BBDBG_ROOT" \
+  -iex "add-auto-load-safe-path $BBDBG_ROOT" \
+  ${BBDBG_ROOT}${BBDBG_FILE} \
+  $([ '-' = "${BBDBG_CORE}" ] || echo ${BBDBG_CORE})
+set +x