15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#!/bin/sh
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# Copyright (c) 2007, Google Inc.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# All rights reserved.
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# 
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# Redistribution and use in source and binary forms, with or without
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# modification, are permitted provided that the following conditions are
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# met:
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# 
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#     * Redistributions of source code must retain the above copyright
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# notice, this list of conditions and the following disclaimer.
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#     * Redistributions in binary form must reproduce the above
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# copyright notice, this list of conditions and the following disclaimer
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# in the documentation and/or other materials provided with the
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# distribution.
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#     * Neither the name of Google Inc. nor the names of its
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# contributors may be used to endorse or promote products derived from
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# this software without specific prior written permission.
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# 
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# ---
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# Author: Craig Silverstein
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# maybe_threads.cc was written to allow LD_PRELOAD=libtcmalloc.so to
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# work even on binaries that were not linked with pthreads.  This
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# unittest tests that, by running low_level_alloc_unittest with an
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# LD_PRELOAD.  (low_level_alloc_unittest was chosen because it doesn't
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# link in tcmalloc.)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# We assume all the .so files are in the same directory as both
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# addressmap_unittest and profiler1_unittest.  The reason we need
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# profiler1_unittest is because it's instrumented to show the directory
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# it's "really" in when run without any args.  In practice this will either
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# be BINDIR, or, when using libtool, BINDIR/.lib.
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# We expect BINDIR to be set in the environment.
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# If not, we set them to some reasonable values.
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BINDIR="${BINDIR:-.}"
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)if [ "x$1" = "x-h" -o "x$1" = "x--help" ]; then
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  echo "USAGE: $0 [unittest dir]"
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  echo "       By default, unittest_dir=$BINDIR"
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  exit 1
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)fi
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)UNITTEST_DIR=${1:-$BINDIR}
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# Figure out the "real" unittest directory.  Also holds the .so files.
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)UNITTEST_DIR=`$UNITTEST_DIR/low_level_alloc_unittest --help 2>&1 \
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              | awk '{print $2; exit;}' \
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              | xargs dirname`
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# Figure out where libtcmalloc lives.   It should be in UNITTEST_DIR,
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# but with libtool it might be in a subdir.
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)if [ -r "$UNITTEST_DIR/libtcmalloc_minimal.so" ]; then
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LIB_PATH="$UNITTEST_DIR/libtcmalloc_minimal.so"
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)elif [ -r "$UNITTEST_DIR/.libs/libtcmalloc_minimal.so" ]; then
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LIB_PATH="$UNITTEST_DIR/.libs/libtcmalloc_minimal.so"
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)elif [ -r "$UNITTEST_DIR/libtcmalloc_minimal.dylib" ]; then   # for os x
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LIB_PATH="$UNITTEST_DIR/libtcmalloc_minimal.dylib"
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)elif [ -r "$UNITTEST_DIR/.libs/libtcmalloc_minimal.dylib" ]; then
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LIB_PATH="$UNITTEST_DIR/.libs/libtcmalloc_minimal.dylib"
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)else
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  echo "Cannot run $0: cannot find libtcmalloc_minimal.so"
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  exit 2
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)fi
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)LD_PRELOAD="$LIB_PATH" $UNITTEST_DIR/low_level_alloc_unittest
80