2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 # Saves the gdb index for a given binary and its shared library dependencies.
8 # This will run gdb index in parallel on a number of binaries using SIGUSR1
9 # as the communication mechanism to simulate a semaphore. Because of the
10 # nature of this technique, using "set -e" is very difficult. The SIGUSR1
11 # terminates a "wait" with an error which we need to interpret.
13 # When modifying this code, most of the real logic is in the index_one_file
14 # function. The rest is cleanup + sempahore plumbing.
16 # Cleanup temp directory and ensure all child jobs are dead-dead.
18 trap "" EXIT USR1
# Avoid reentrancy.
21 if [ -n "$jobs" ]; then
22 echo -n "Killing outstanding index jobs..."
28 if [ -f "$DIRECTORY" ]; then
29 echo -n "Removing temp directory $DIRECTORY..."
35 # Add index to one binary.
36 function index_one_file
{
38 local basename=$
(basename "$file")
39 local should_index
="${SHOULD_INDEX}"
41 local readelf_out
=$
(${TOOLCHAIN_PREFIX}readelf
-S "$file")
42 if [[ $readelf_out =~
"gdb_index" ]]; then
43 if [ "${REMOVE_INDEX}" = 1 ]; then
44 ${TOOLCHAIN_PREFIX}objcopy
--remove-section .gdb_index
"$file"
45 echo "Removed index from $basename."
47 echo "Skipped $basename -- already contains index."
52 if [ "${should_index}" = 1 ]; then
53 local start
=$
(date +"%s%N")
54 echo "Adding index to $basename..."
56 ${TOOLCHAIN_PREFIX}gdb
-batch "$file" -ex "save gdb-index $DIRECTORY" \
58 local index_file
="$DIRECTORY/$basename.gdb-index"
59 if [ -f "$index_file" ]; then
60 ${TOOLCHAIN_PREFIX}objcopy
--add-section .gdb_index
="$index_file" \
61 --set-section-flags .gdb_index
=readonly "$file" "$file"
62 local finish
=$
(date +"%s%N")
63 local elapsed
=$
(((finish
- start
)/1000000))
64 echo " ...$basename indexed. [${elapsed}ms]"
66 echo " ...$basename unindexable."
71 # Functions that when combined, concurrently index all files in FILES_TO_INDEX
72 # array. The global FILES_TO_INDEX is declared in the main body of the script.
73 function async_index
{
74 # Start a background subshell to run the index command.
77 kill -SIGUSR1 $$
# $$ resolves to the parent script.
78 exit 129 # See comment above wait loop at bottom.
84 if (( CUR_FILE_NUM
>= ${#FILES_TO_INDEX[@]} )); then
88 async_index
"${FILES_TO_INDEX[CUR_FILE_NUM]}"
89 ((CUR_FILE_NUM
+= 1)) || true
94 ### Main body of the script.
98 while getopts ":f:r" opt
; do
110 echo "Invalid option: -$OPTARG" >&2
115 if [[ ! $# == 1 ]]; then
116 echo "Usage: $0 [-f] [-r] path-to-binary"
117 echo " -f forces replacement of an existing index."
118 echo " -r removes the index section."
123 if [[ ! -f "$FILENAME" ]]; then
124 echo "Path $FILENAME does not exist."
128 # Ensure we cleanup on on exit.
131 # We're good to go! Create temp directory for index files.
132 DIRECTORY
=$
(mktemp
-d)
133 echo "Made temp directory $DIRECTORY."
135 # Create array with the filename and all shared libraries that
136 # have the same dirname. The dirname is a signal that these
137 # shared libraries were part of the same build as the binary.
138 declare -a FILES_TO_INDEX
=($FILENAME
139 $
(ldd
"$FILENAME" 2>/dev
/null \
140 |
grep $
(dirname "$FILENAME") \
141 |
sed "s/.*[ \t]\(.*\) (.*/\1/")
144 # Start concurrent indexing.
147 # 4 is an arbitrary default. When changing, remember we are likely IO bound
148 # so basing this off the number of cores is not sensible.
149 INDEX_TASKS
=${INDEX_TASKS:-4}
150 for ((i
=0;i
<${INDEX_TASKS};i
++)); do
154 # Do a wait loop. Bash waits that terminate due a trap have an exit
155 # code > 128. We also ensure that our subshell's "normal" exit occurs with
156 # an exit code > 128. This allows us to do consider a > 128 exit code as
157 # an indication that the loop should continue. Unfortunately, it also means
158 # we cannot use set -e since technically the "wait" is failing.
160 while (( $?
> 128 )); do