#!/bin/sh

# $Id: getsolibs,v 1.7 2006/01/21 22:45:36 rkufrin Exp $

############################################################################
#
#                      University of Illinois/NCSA
#                          Open Source License
# 
#          Copyright(C) 2005-2006, The Board of Trustees of the
#              University of Illinois. All rights reserved.
#
#                             Developed by:             
# 
#                        The PerfSuite Project
#            National Center for Supercomputing Applications
#              University of Illinois at Urbana-Champaign
# 
#                   http://perfsuite.ncsa.uiuc.edu/
# 
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the "Software"),
# to deal with the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense, 
# and/or sell copies of the Software, and to permit persons to whom the 
# Software is furnished to do so, subject to the following conditions:
# 
# + Redistributions of source code must retain the above copyright notice, 
#   this list of conditions and the following disclaimers.
# + Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimers in
#   the documentation and/or other materials provided with the distribution.
# + Neither the names of The PerfSuite Project, NCSA/University of Illinois
#   at Urbana-Champaign, nor the names of its contributors may be used to
#   endorse or promote products derived from this Software without specific
#   prior written permission.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
# CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
# DEALINGS WITH THE SOFTWARE.
############################################################################

# getsolibs: shell script to display shared libraries loaded by
#            a program.  Specific to output format of ld.so/LD_DEBUG.
#            LD_DEBUG_OUTPUT is ignored for setuid/setgid binaries,
#            so this script cannot be used on them.
#
# This script may be helpful when profiling programs that dynamically open
# libraries.  You can use this script to learn what libraries are opened
# and then set your LD_PRELOAD environment variable based on that information.
# Preloading these libraries will allow psrun/libpshwpc to include them in its
# load map, which is constructed on application startup.
#
# Author: Rick Kufrin
# Date:   2/5/2005
#
# This file is part of PerfSuite.

if test $# -eq 0; then
    echo "Usage: `basename $0` [-q] command [args ...]"
    echo "Error: please supply a command to run."
    exit 1
fi

QUIET=no
if test $1 = "-q"; then
    QUIET=yes
    shift
fi

DEBUGFILE=/tmp/debug.$$

trap "/bin/rm -f ${DEBUGFILE}*" 0

LD_DEBUG=files LD_DEBUG_OUTPUT=$DEBUGFILE $*

if test $? -eq 127; then
    exit 127
fi

if test $QUIET = "no"; then
    echo ""
    echo "== Shared libraries loaded by \"$1\" =="
    echo ""
    cat ${DEBUGFILE}* | awk '/calling init/ {print $4}' | sort | uniq
else
    cat ${DEBUGFILE}* | sort | uniq | awk '/calling init/ {printf "%s ", $4}'
    echo ""
fi

exit 0
