2012-08-21 04:09:19 +04:00
|
|
|
#!/bin/bash
|
2012-06-29 03:37:31 +04:00
|
|
|
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
|
|
|
# Use of this source code is governed by a BSD-style license that can be
|
|
|
|
# found in the LICENSE file.
|
|
|
|
#
|
2012-08-21 04:09:19 +04:00
|
|
|
# Saves the gdb index for a given binary and its shared library dependencies.
|
2013-08-15 22:03:03 +04:00
|
|
|
#
|
|
|
|
# This will run gdb index in parallel on a number of binaries using SIGUSR1
|
|
|
|
# as the communication mechanism to simulate a semaphore. Because of the
|
|
|
|
# nature of this technique, using "set -e" is very difficult. The SIGUSR1
|
|
|
|
# terminates a "wait" with an error which we need to interpret.
|
|
|
|
#
|
|
|
|
# When modifying this code, most of the real logic is in the index_one_file
|
|
|
|
# function. The rest is cleanup + sempahore plumbing.
|
|
|
|
|
2015-11-24 03:30:33 +03:00
|
|
|
function usage_exit {
|
|
|
|
echo "Usage: $0 [-f] [-r] [-n] <paths-to-binaries>..."
|
|
|
|
echo " -f forces replacement of an existing index."
|
|
|
|
echo " -r removes the index section."
|
|
|
|
echo " -n don't extract the dependencies of each binary with lld."
|
|
|
|
echo " e.g., $0 -n out/Debug/lib.unstripped/lib*"
|
|
|
|
echo
|
|
|
|
echo " Set TOOLCHAIN_PREFIX to use a non-default set of binutils."
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2013-08-15 22:03:03 +04:00
|
|
|
# Cleanup temp directory and ensure all child jobs are dead-dead.
|
|
|
|
function on_exit {
|
|
|
|
trap "" EXIT USR1 # Avoid reentrancy.
|
|
|
|
|
|
|
|
local jobs=$(jobs -p)
|
|
|
|
if [ -n "$jobs" ]; then
|
|
|
|
echo -n "Killing outstanding index jobs..."
|
|
|
|
kill -KILL $(jobs -p)
|
|
|
|
wait
|
|
|
|
echo "done"
|
|
|
|
fi
|
|
|
|
|
2015-11-24 03:30:33 +03:00
|
|
|
if [ -f "$directory" ]; then
|
|
|
|
echo -n "Removing temp directory $directory..."
|
|
|
|
rm -rf "$directory"
|
2013-08-15 22:03:03 +04:00
|
|
|
echo done
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Add index to one binary.
|
|
|
|
function index_one_file {
|
|
|
|
local file=$1
|
|
|
|
local basename=$(basename "$file")
|
2015-11-24 03:30:33 +03:00
|
|
|
local should_index_this_file="${should_index}"
|
2013-08-15 22:03:03 +04:00
|
|
|
|
2015-01-09 03:36:50 +03:00
|
|
|
local readelf_out=$(${TOOLCHAIN_PREFIX}readelf -S "$file")
|
2013-08-15 22:03:03 +04:00
|
|
|
if [[ $readelf_out =~ "gdb_index" ]]; then
|
2015-11-24 03:30:33 +03:00
|
|
|
if $remove_index; then
|
2015-01-09 03:36:50 +03:00
|
|
|
${TOOLCHAIN_PREFIX}objcopy --remove-section .gdb_index "$file"
|
2014-06-14 12:50:22 +04:00
|
|
|
echo "Removed index from $basename."
|
|
|
|
else
|
|
|
|
echo "Skipped $basename -- already contains index."
|
2015-11-24 03:30:33 +03:00
|
|
|
should_index_this_file=false
|
2014-06-14 12:50:22 +04:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2015-11-24 03:30:33 +03:00
|
|
|
if $should_index_this_file; then
|
2013-08-15 22:03:03 +04:00
|
|
|
local start=$(date +"%s%N")
|
|
|
|
echo "Adding index to $basename..."
|
|
|
|
|
2015-11-24 03:30:33 +03:00
|
|
|
${TOOLCHAIN_PREFIX}gdb -batch "$file" -ex "save gdb-index $directory" \
|
2015-01-09 03:36:50 +03:00
|
|
|
-ex "quit"
|
2015-11-24 03:30:33 +03:00
|
|
|
local index_file="$directory/$basename.gdb-index"
|
2013-08-15 22:03:03 +04:00
|
|
|
if [ -f "$index_file" ]; then
|
2015-01-09 03:36:50 +03:00
|
|
|
${TOOLCHAIN_PREFIX}objcopy --add-section .gdb_index="$index_file" \
|
2013-08-15 22:03:03 +04:00
|
|
|
--set-section-flags .gdb_index=readonly "$file" "$file"
|
|
|
|
local finish=$(date +"%s%N")
|
2015-11-24 03:30:33 +03:00
|
|
|
local elapsed=$(((finish - start) / 1000000))
|
2015-01-09 03:36:50 +03:00
|
|
|
echo " ...$basename indexed. [${elapsed}ms]"
|
2013-08-15 22:03:03 +04:00
|
|
|
else
|
|
|
|
echo " ...$basename unindexable."
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
2012-08-21 04:09:19 +04:00
|
|
|
|
2013-08-15 22:03:03 +04:00
|
|
|
# Functions that when combined, concurrently index all files in FILES_TO_INDEX
|
|
|
|
# array. The global FILES_TO_INDEX is declared in the main body of the script.
|
|
|
|
function async_index {
|
|
|
|
# Start a background subshell to run the index command.
|
|
|
|
{
|
|
|
|
index_one_file $1
|
|
|
|
kill -SIGUSR1 $$ # $$ resolves to the parent script.
|
|
|
|
exit 129 # See comment above wait loop at bottom.
|
|
|
|
} &
|
|
|
|
}
|
|
|
|
|
2015-11-24 03:30:33 +03:00
|
|
|
cur_file_num=0
|
2013-08-15 22:03:03 +04:00
|
|
|
function index_next {
|
2015-11-24 03:30:33 +03:00
|
|
|
if ((cur_file_num >= ${#files_to_index[@]})); then
|
2013-08-15 22:03:03 +04:00
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
2015-11-24 03:30:33 +03:00
|
|
|
async_index "${files_to_index[cur_file_num]}"
|
|
|
|
((cur_file_num += 1)) || true
|
2013-08-15 22:03:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
########
|
|
|
|
### Main body of the script.
|
2012-08-21 04:09:19 +04:00
|
|
|
|
2015-11-24 03:30:33 +03:00
|
|
|
remove_index=false
|
|
|
|
should_index=true
|
|
|
|
should_index_deps=true
|
|
|
|
files_to_index=()
|
|
|
|
while (($# > 0)); do
|
|
|
|
case "$1" in
|
|
|
|
-h)
|
|
|
|
usage_exit
|
|
|
|
;;
|
|
|
|
-f)
|
|
|
|
remove_index=true
|
2014-06-14 12:50:22 +04:00
|
|
|
;;
|
2015-11-24 03:30:33 +03:00
|
|
|
-r)
|
|
|
|
remove_index=true
|
|
|
|
should_index=false
|
|
|
|
;;
|
|
|
|
-n)
|
|
|
|
should_index_deps=false
|
|
|
|
;;
|
|
|
|
-*)
|
|
|
|
echo "Invalid option: $1" >&2
|
|
|
|
usage_exit
|
2014-06-14 12:50:22 +04:00
|
|
|
;;
|
|
|
|
*)
|
2015-11-24 03:30:33 +03:00
|
|
|
if [[ ! -f "$1" ]]; then
|
|
|
|
echo "Path $1 does not exist."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
files_to_index+=("$1")
|
2014-06-14 12:50:22 +04:00
|
|
|
;;
|
|
|
|
esac
|
2015-11-24 03:30:33 +03:00
|
|
|
shift
|
2014-06-14 12:50:22 +04:00
|
|
|
done
|
|
|
|
|
2015-11-24 03:30:33 +03:00
|
|
|
if ((${#files_to_index[@]} == 0)); then
|
|
|
|
usage_exit
|
2012-08-21 04:09:19 +04:00
|
|
|
fi
|
|
|
|
|
2015-11-24 03:30:33 +03:00
|
|
|
dependencies=()
|
|
|
|
if $should_index_deps; then
|
|
|
|
for file in "${files_to_index[@]}"; do
|
|
|
|
# Append the shared library dependencies of this file that
|
|
|
|
# have the same dirname. The dirname is a signal that these
|
|
|
|
# shared libraries were part of the same build as the binary.
|
|
|
|
dependencies+=( \
|
|
|
|
$(ldd "$file" 2>/dev/null \
|
|
|
|
| grep $(dirname "$file") \
|
|
|
|
| sed "s/.*[ \t]\(.*\) (.*/\1/") \
|
|
|
|
)
|
|
|
|
done
|
2012-08-21 04:09:19 +04:00
|
|
|
fi
|
2015-11-24 03:30:33 +03:00
|
|
|
files_to_index+=("${dependencies[@]}")
|
2012-08-21 04:09:19 +04:00
|
|
|
|
2013-08-15 22:03:03 +04:00
|
|
|
# Ensure we cleanup on on exit.
|
2015-11-24 03:30:33 +03:00
|
|
|
trap on_exit EXIT INT
|
2013-08-15 22:03:03 +04:00
|
|
|
|
2012-08-21 04:09:19 +04:00
|
|
|
# We're good to go! Create temp directory for index files.
|
2015-11-24 03:30:33 +03:00
|
|
|
directory=$(mktemp -d)
|
|
|
|
echo "Made temp directory $directory."
|
2012-08-21 04:09:19 +04:00
|
|
|
|
2013-08-15 22:03:03 +04:00
|
|
|
# Start concurrent indexing.
|
|
|
|
trap index_next USR1
|
|
|
|
|
|
|
|
# 4 is an arbitrary default. When changing, remember we are likely IO bound
|
|
|
|
# so basing this off the number of cores is not sensible.
|
2015-11-24 03:30:33 +03:00
|
|
|
index_tasks=${INDEX_TASKS:-4}
|
|
|
|
for ((i = 0; i < index_tasks; i++)); do
|
2013-08-15 22:03:03 +04:00
|
|
|
index_next
|
|
|
|
done
|
|
|
|
|
|
|
|
# Do a wait loop. Bash waits that terminate due a trap have an exit
|
|
|
|
# code > 128. We also ensure that our subshell's "normal" exit occurs with
|
|
|
|
# an exit code > 128. This allows us to do consider a > 128 exit code as
|
|
|
|
# an indication that the loop should continue. Unfortunately, it also means
|
|
|
|
# we cannot use set -e since technically the "wait" is failing.
|
|
|
|
wait
|
2015-11-24 03:30:33 +03:00
|
|
|
while (($? > 128)); do
|
2013-08-15 22:03:03 +04:00
|
|
|
wait
|
2012-06-29 03:37:31 +04:00
|
|
|
done
|