scripts: handle kernel backtraces with bt-akaros.sh

Signed-off-by: Barret Rhoden <brho@cs.berkeley.edu>
diff --git a/scripts/bt-akaros.sh b/scripts/bt-akaros.sh
index 97ce8c9..d1b13b0 100755
--- a/scripts/bt-akaros.sh
+++ b/scripts/bt-akaros.sh
@@ -1,39 +1,58 @@
 #!/bin/bash
 # Barret Rhoden (brho@cs.berkeley.edu)
 #
-# Resolves functions from an Akaros user backtrace.
+# Resolves functions from Akaros backtraces, both user and kernel.
 # Pipe a backtrace (echo "huge-copy-paste" | ./thisfile.sh) to it.
 #
-# Be sure to set your environment paths for the SOLIBS and BIN, or use the
-# defaults, which require AKAROS_ROOT.
+# There are a few environment variables you may want to override, which control
+# the location of the binaries and libraries.  If you use AKAROS_ROOT, KFS, and
+# x86, just stick with the defaults.
 
 : ${SOLIBS_PREFIX:=$AKAROS_ROOT/kern/kfs/lib/}
 : ${SO_REGEX:=.*so$}
 : ${BIN_PREFIX:=$AKAROS_ROOT/kern/kfs/bin/}
+: ${KERNEL_BINARY:=$AKAROS_ROOT/obj/kern/akaros-kernel-64b}
 
 # takes the path to the binary and offset (offset in hex), prints name of the
 # function where the offset is in the binary.  basically a wrapper for
 # addr2line.
-function print_func()
-{
+print_user_func() {
 	addr2line -e $1 -fC $2 | xargs
 }
 
-while read line
-do
+kernel_line() {
+	line=$1
+
+	addr=`echo $line | cut -c 7-25`
+
+	echo -n $line " "
+	# sed cleans out build paths.  All kernel files have 'kern', unlike
+	# arbitrary user binaries.
+	addr2line -e $KERNEL_BINARY $addr | sed 's/^.*kern\//at kern\//'
+}
+
+user_line() {
+	line=$1
+
 	binary=`echo $line | cut -f 6 -d ' '`
 	lib_off=`echo $line | cut -f 9 -d ' '`
 	app_off=`echo $line | cut -f 3 -d ' '`
-	if [[ $binary == "" ]]
-	then
-		break
-	fi
+
 	echo -n $line " "
-	if [[ $binary =~ $SO_REGEX ]]
-	then
-		# could also do addr=$(print_func $lib $off)
-		print_func $SOLIBS_PREFIX/$binary $lib_off
+	if [[ $binary =~ $SO_REGEX ]]; then
+		# could also do addr=$(print_user_func $lib $off)
+		print_user_func $SOLIBS_PREFIX/$binary $lib_off
 	else
-		print_func $BIN_PREFIX/$binary $app_off
+		print_user_func $BIN_PREFIX/$binary $app_off
+	fi
+}
+
+while read line; do
+	fifth_char=`echo $line | cut -c 5`
+
+	if [[ $fifth_char == "[" ]]; then
+		kernel_line "$line"
+	elif [[ $fifth_char == "A" ]]; then
+		user_line "$line"
 	fi
 done