#!/bin/sh
# Show the packages containing libraries the given process
# uses at run-time, either by dloading or direct linking.
# This file is part of maemo-debug-scripts.
#
# Copyright (C) 2006,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 "usage: $0 <PID>"
	echo "shows packages providing libs the given PID has dlopened or linked"
	exit 1
fi
if [ \! -d /proc/$1 ]; then
	echo "process $PID does not exist"
	exit 1
fi

dpkg -S $(\
  for i in $(\
    grep 'xp ' /proc/$1/maps|grep '/[a-z]'|\
    sed 's%^[^/]*\(/.*\)$%\1%'|uniq|grep -v ld-2.3.6.so\
    );
  do
    /lib/ld-2.3.6.so --list $i|cut -d'(' -f1|cut -d'>' -f2;
  done)\
|cut -d: -f1|sort -u|sed 's/^/- /'
