#!/bin/sh
# Checks whether given debug symbol files package is correctly done.
# This file is part of maemo-debug-scripts.
#
# Copyright (C) 2007 by Nokia Corporation
#
# Contact: Eero Tamminen <eero.tamminen@nokia.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License 
# version 2 as published by the Free Software Foundation. 
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301 USA

if [ $# -ne 1 ]; then
	echo
	echo "usage: ${0##*/} <package name>"
	echo
	echo "Gets debug symbol package for given package and checks that"
	echo "each binary and library has corresponding debug symbol file"
	echo "and there are no problems with the debug symbols."
	echo
	echo "Requires either 'readelf' or 'objdump' (from binutils)."
	echo
	exit 1
fi
pkg=$1
if [ "${pkg%-dbg}" != "$pkg" ]; then
	pkg=${pkg%-dbg}
fi
# check tools
if [ \! -z $(which readelf) ]; then
	sections="readelf -S"
elif [ \! -z $(which objdump) ]; then
	sections="objdump -h"
else
	echo
	echo "ERROR: both 'readelf' and 'objdump' are missing!"
	exit 1
fi
if [ -f /targets/links/scratchbox.config ]; then
	apt_get="fakeroot apt-get"
else
	# no fakeroot on target
	apt_get="apt-get --allow-unauthenticated"
fi
$apt_get install $pkg
if [ $? -ne 0 ]; then
	echo
	echo "ERROR: unknown package '$pkg'"
	exit 1
fi
$apt_get install $pkg-dbg
if [ $? -ne 0 ]; then
	echo "ERROR: package doesn't have a corresponding debug package:"
	echo "  $pkg-dbg"
	exit 1
fi
# check for binaries and libraries
echo "Checking package contents..."
checked=""
skipped=""
missing=""
oldstyle=""
havecode=""
nodebug=""
for i in $(dpkg -L $pkg); do
	if [ -d $i ] || [ -h $i ] || [ \! -f $i ]; then
		# skipping dirs, symlinks and non-files
		continue
	fi
	# not executable, not so, or script
	if [ \! -x $i -a -z "$(echo $i|grep \\.so)" ] || [ "x$(dd  bs=1 count=1 if=$i 2> /dev/null)" == "x#" ]; then
		skipped="$skipped $i"
		continue
	else
		checked="$checked $i"
	fi
	if [ -f /usr/lib/debug/${i##*/} ]; then
		oldstyle="$oldstyle $i"
		continue
	fi
	dbg=/usr/lib/debug/$i
	if [ \! -f $dbg ]; then
		missing="$missing $i"
		continue
	fi
	if [ -z "$($sections $dbg|grep .text|grep NOBITS)" ]; then
		havecode="$havecode $dbg"
	fi
	if [ -z "$($sections $dbg|grep .debug_frame|grep PROGBITS)" ]; then
		nodebug="$nodebug $dbg"
	fi
done

if [ \! -z "$skipped" ]; then
	echo
	echo "Skipped following files as they don't need debug symbols:"
	for i in $skipped; do
		echo "- $i"
	done
fi
if [ \! -z "$checked" ]; then
	echo
	echo "...and checked following files:"
	for i in $checked; do
		echo "- $i"
	done
fi

if [ \! -z "$missing" ]; then
	echo
	echo "ERROR: debug symbol files are missing for following files:"
	for i in $missing; do
		echo "- $i"
	done
	echo "Most likely reason for this is incorrect dh_strip use in debian/rules."
	errors=1
fi
if [ \! -z "$oldstyle" ]; then
	echo
	echo "ERROR: files which have old style debug symbols:"
	for i in $oldstyle; do
		echo "- $i"
	done
	echo "Most likely reason for this is incorrect dh_strip use in debian/rules."
	errors=1
fi
if [ \! -z "$havecode" ]; then
	echo
	echo "ERROR: debug symbol files which contain code (.text section with PROGBITS):"
	for i in $havecode; do
		echo "- $i"
	done
	echo "Most likely reason for this is incorrect dh_strip use in debian/rules file."
	errors=1
fi
if [ \! -z "$nodebug" ]; then
	echo
	echo "ERROR: .debug_frame section needed for backtraces is missing from:"
	for i in $nodebug; do
		echo "- $i"
	done
	echo "Most likely reason for this is missing -g compile option."
	errors=1
fi
echo
if [ $errors ]; then
	echo "ERROR! There are errors in debug symbol files."
else
	echo "DONE! No issues found."
fi
