Commit f94079e6 authored by watk's avatar watk Committed by Commit bot

gdb-add-index: accept a list of files to index

Previously gdb-add-index took a single binary argument to index. It uses
ldd to get its dependencies and index those too. Now it accepts more than
one file, along with a -n option to not attempt to extract dependencies.
This is useful for Android development where there's no single
binary to index. Now you can do gdb-add-index -n out/Debug/lib.unstripped/*

This CL also cleans up the style a bit for consistency.

BUG=

Review URL: https://codereview.chromium.org/1471523002

Cr-Commit-Position: refs/heads/master@{#361227}
parent eb605d8b
...@@ -13,6 +13,17 @@ ...@@ -13,6 +13,17 @@
# When modifying this code, most of the real logic is in the index_one_file # When modifying this code, most of the real logic is in the index_one_file
# function. The rest is cleanup + sempahore plumbing. # function. The rest is cleanup + sempahore plumbing.
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
}
# Cleanup temp directory and ensure all child jobs are dead-dead. # Cleanup temp directory and ensure all child jobs are dead-dead.
function on_exit { function on_exit {
trap "" EXIT USR1 # Avoid reentrancy. trap "" EXIT USR1 # Avoid reentrancy.
...@@ -25,9 +36,9 @@ function on_exit { ...@@ -25,9 +36,9 @@ function on_exit {
echo "done" echo "done"
fi fi
if [ -f "$DIRECTORY" ]; then if [ -f "$directory" ]; then
echo -n "Removing temp directory $DIRECTORY..." echo -n "Removing temp directory $directory..."
rm -rf $DIRECTORY rm -rf "$directory"
echo done echo done
fi fi
} }
...@@ -36,31 +47,31 @@ function on_exit { ...@@ -36,31 +47,31 @@ function on_exit {
function index_one_file { function index_one_file {
local file=$1 local file=$1
local basename=$(basename "$file") local basename=$(basename "$file")
local should_index="${SHOULD_INDEX}" local should_index_this_file="${should_index}"
local readelf_out=$(${TOOLCHAIN_PREFIX}readelf -S "$file") local readelf_out=$(${TOOLCHAIN_PREFIX}readelf -S "$file")
if [[ $readelf_out =~ "gdb_index" ]]; then if [[ $readelf_out =~ "gdb_index" ]]; then
if [ "${REMOVE_INDEX}" = 1 ]; then if $remove_index; then
${TOOLCHAIN_PREFIX}objcopy --remove-section .gdb_index "$file" ${TOOLCHAIN_PREFIX}objcopy --remove-section .gdb_index "$file"
echo "Removed index from $basename." echo "Removed index from $basename."
else else
echo "Skipped $basename -- already contains index." echo "Skipped $basename -- already contains index."
should_index=0 should_index_this_file=false
fi fi
fi fi
if [ "${should_index}" = 1 ]; then if $should_index_this_file; then
local start=$(date +"%s%N") local start=$(date +"%s%N")
echo "Adding index to $basename..." echo "Adding index to $basename..."
${TOOLCHAIN_PREFIX}gdb -batch "$file" -ex "save gdb-index $DIRECTORY" \ ${TOOLCHAIN_PREFIX}gdb -batch "$file" -ex "save gdb-index $directory" \
-ex "quit" -ex "quit"
local index_file="$DIRECTORY/$basename.gdb-index" local index_file="$directory/$basename.gdb-index"
if [ -f "$index_file" ]; then if [ -f "$index_file" ]; then
${TOOLCHAIN_PREFIX}objcopy --add-section .gdb_index="$index_file" \ ${TOOLCHAIN_PREFIX}objcopy --add-section .gdb_index="$index_file" \
--set-section-flags .gdb_index=readonly "$file" "$file" --set-section-flags .gdb_index=readonly "$file" "$file"
local finish=$(date +"%s%N") local finish=$(date +"%s%N")
local elapsed=$(((finish - start)/1000000)) local elapsed=$(((finish - start) / 1000000))
echo " ...$basename indexed. [${elapsed}ms]" echo " ...$basename indexed. [${elapsed}ms]"
else else
echo " ...$basename unindexable." echo " ...$basename unindexable."
...@@ -79,75 +90,86 @@ function async_index { ...@@ -79,75 +90,86 @@ function async_index {
} & } &
} }
CUR_FILE_NUM=0 cur_file_num=0
function index_next { function index_next {
if (( CUR_FILE_NUM >= ${#FILES_TO_INDEX[@]} )); then if ((cur_file_num >= ${#files_to_index[@]})); then
return return
fi fi
async_index "${FILES_TO_INDEX[CUR_FILE_NUM]}" async_index "${files_to_index[cur_file_num]}"
((CUR_FILE_NUM += 1)) || true ((cur_file_num += 1)) || true
} }
######## ########
### Main body of the script. ### Main body of the script.
REMOVE_INDEX=0 remove_index=false
SHOULD_INDEX=1 should_index=true
while getopts ":f:r" opt; do should_index_deps=true
case $opt in files_to_index=()
f) while (($# > 0)); do
REMOVE_INDEX=1 case "$1" in
shift -h)
usage_exit
;;
-f)
remove_index=true
;; ;;
r) -r)
REMOVE_INDEX=1 remove_index=true
SHOULD_INDEX=0 should_index=false
shift ;;
-n)
should_index_deps=false
;;
-*)
echo "Invalid option: $1" >&2
usage_exit
;; ;;
*) *)
echo "Invalid option: -$OPTARG" >&2 if [[ ! -f "$1" ]]; then
echo "Path $1 does not exist."
exit 1
fi
files_to_index+=("$1")
;; ;;
esac esac
shift
done done
if [[ ! $# == 1 ]]; then if ((${#files_to_index[@]} == 0)); then
echo "Usage: $0 [-f] [-r] path-to-binary" usage_exit
echo " -f forces replacement of an existing index."
echo " -r removes the index section."
exit 1
fi fi
FILENAME="$1" dependencies=()
if [[ ! -f "$FILENAME" ]]; then if $should_index_deps; then
echo "Path $FILENAME does not exist." for file in "${files_to_index[@]}"; do
exit 1 # 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
fi fi
files_to_index+=("${dependencies[@]}")
# Ensure we cleanup on on exit. # Ensure we cleanup on on exit.
trap on_exit EXIT trap on_exit EXIT INT
# We're good to go! Create temp directory for index files. # We're good to go! Create temp directory for index files.
DIRECTORY=$(mktemp -d) directory=$(mktemp -d)
echo "Made temp directory $DIRECTORY." echo "Made temp directory $directory."
# Create array with the filename and all shared libraries that
# have the same dirname. The dirname is a signal that these
# shared libraries were part of the same build as the binary.
declare -a FILES_TO_INDEX=($FILENAME
$(ldd "$FILENAME" 2>/dev/null \
| grep $(dirname "$FILENAME") \
| sed "s/.*[ \t]\(.*\) (.*/\1/")
)
# Start concurrent indexing. # Start concurrent indexing.
trap index_next USR1 trap index_next USR1
# 4 is an arbitrary default. When changing, remember we are likely IO bound # 4 is an arbitrary default. When changing, remember we are likely IO bound
# so basing this off the number of cores is not sensible. # so basing this off the number of cores is not sensible.
INDEX_TASKS=${INDEX_TASKS:-4} index_tasks=${INDEX_TASKS:-4}
for ((i=0;i<${INDEX_TASKS};i++)); do for ((i = 0; i < index_tasks; i++)); do
index_next index_next
done done
...@@ -157,6 +179,6 @@ done ...@@ -157,6 +179,6 @@ done
# an indication that the loop should continue. Unfortunately, it also means # an indication that the loop should continue. Unfortunately, it also means
# we cannot use set -e since technically the "wait" is failing. # we cannot use set -e since technically the "wait" is failing.
wait wait
while (( $? > 128 )); do while (($? > 128)); do
wait wait
done done
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment