#!/bin/sh
#
# Written by Jerry James <loganjerry@gmail.com>
# Released under the terms of the CC0 license.
# See http://creativecommons.org/publicdomain/zero/1.0/.
#
# Produce the arguments to add-symbol-file for a kernel module to be debugged.

if [ $# != 1 -o ! -d /sys/module/$1/sections ]; then
  echo "Usage: modaddress <module_name>, where"
  echo "       module_name is the name of a loaded kernel module to inspect"
  exit 1
fi

moddir=/sys/module/$1/sections
kernelver=$(uname -r)
ko=$(find /lib/modules/$kernelver -name $1.ko)
if [ -z "$ko" ]; then
  ko=$(tr _ - <<< $1.ko | xargs find /lib/modules/$kernelver -name)
fi
textaddr=$(cat $moddir/.text)
out="add-symbol-file /usr/lib/debug$ko.debug $textaddr"
for section in $moddir/.*; do
  sect=$(basename $section)
  if [[ $sect != . && $sect != .. && $sect != .text && $sect != .strtab && \
	$sect != .symtab && ! $sect =~ ^\.note ]]; then
    addr=$(cat $section)
    out="$out -s $sect $addr"
  fi
done
echo $out
