blob: e756ceacc62a595a5796039da86291c63e06f919 [file] [log] [blame]
[email protected]d43a9fb2012-08-21 00:09:191#!/bin/bash
Avi Drissman73a09d12022-09-08 20:33:382# Copyright 2012 The Chromium Authors
[email protected]4cec25342012-06-28 23:37:313# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5#
[email protected]d43a9fb2012-08-21 00:09:196# Saves the gdb index for a given binary and its shared library dependencies.
[email protected]3e89a9d2013-08-15 18:03:037#
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.
12#
13# When modifying this code, most of the real logic is in the index_one_file
14# function. The rest is cleanup + sempahore plumbing.
[email protected]fdb9ecc2012-06-28 01:53:4515
watkf94079e2015-11-24 00:30:3316function usage_exit {
17 echo "Usage: $0 [-f] [-r] [-n] <paths-to-binaries>..."
18 echo " -f forces replacement of an existing index."
19 echo " -r removes the index section."
20 echo " -n don't extract the dependencies of each binary with lld."
21 echo " e.g., $0 -n out/Debug/lib.unstripped/lib*"
22 echo
23 echo " Set TOOLCHAIN_PREFIX to use a non-default set of binutils."
24 exit 1
25}
26
[email protected]3e89a9d2013-08-15 18:03:0327# Cleanup temp directory and ensure all child jobs are dead-dead.
28function on_exit {
29 trap "" EXIT USR1 # Avoid reentrancy.
30
31 local jobs=$(jobs -p)
32 if [ -n "$jobs" ]; then
33 echo -n "Killing outstanding index jobs..."
34 kill -KILL $(jobs -p)
35 wait
36 echo "done"
37 fi
38
watkbb802c682016-06-01 20:18:4439 if [ -d "$directory" ]; then
watkf94079e2015-11-24 00:30:3340 echo -n "Removing temp directory $directory..."
41 rm -rf "$directory"
[email protected]3e89a9d2013-08-15 18:03:0342 echo done
43 fi
44}
45
46# Add index to one binary.
47function index_one_file {
48 local file=$1
49 local basename=$(basename "$file")
watkf94079e2015-11-24 00:30:3350 local should_index_this_file="${should_index}"
[email protected]3e89a9d2013-08-15 18:03:0351
cleichnercdc89df2015-01-09 00:36:5052 local readelf_out=$(${TOOLCHAIN_PREFIX}readelf -S "$file")
[email protected]3e89a9d2013-08-15 18:03:0353 if [[ $readelf_out =~ "gdb_index" ]]; then
watkf94079e2015-11-24 00:30:3354 if $remove_index; then
cleichnercdc89df2015-01-09 00:36:5055 ${TOOLCHAIN_PREFIX}objcopy --remove-section .gdb_index "$file"
[email protected]6192e652014-06-14 08:50:2256 echo "Removed index from $basename."
57 else
58 echo "Skipped $basename -- already contains index."
watkf94079e2015-11-24 00:30:3359 should_index_this_file=false
[email protected]6192e652014-06-14 08:50:2260 fi
61 fi
62
watkf94079e2015-11-24 00:30:3363 if $should_index_this_file; then
[email protected]3e89a9d2013-08-15 18:03:0364 local start=$(date +"%s%N")
65 echo "Adding index to $basename..."
66
watkf94079e2015-11-24 00:30:3367 ${TOOLCHAIN_PREFIX}gdb -batch "$file" -ex "save gdb-index $directory" \
cleichnercdc89df2015-01-09 00:36:5068 -ex "quit"
watkf94079e2015-11-24 00:30:3369 local index_file="$directory/$basename.gdb-index"
[email protected]3e89a9d2013-08-15 18:03:0370 if [ -f "$index_file" ]; then
cleichnercdc89df2015-01-09 00:36:5071 ${TOOLCHAIN_PREFIX}objcopy --add-section .gdb_index="$index_file" \
[email protected]3e89a9d2013-08-15 18:03:0372 --set-section-flags .gdb_index=readonly "$file" "$file"
73 local finish=$(date +"%s%N")
watkf94079e2015-11-24 00:30:3374 local elapsed=$(((finish - start) / 1000000))
cleichnercdc89df2015-01-09 00:36:5075 echo " ...$basename indexed. [${elapsed}ms]"
[email protected]3e89a9d2013-08-15 18:03:0376 else
77 echo " ...$basename unindexable."
78 fi
79 fi
80}
81
82# Functions that when combined, concurrently index all files in FILES_TO_INDEX
83# array. The global FILES_TO_INDEX is declared in the main body of the script.
84function async_index {
85 # Start a background subshell to run the index command.
86 {
87 index_one_file $1
88 kill -SIGUSR1 $$ # $$ resolves to the parent script.
89 exit 129 # See comment above wait loop at bottom.
90 } &
91}
92
watkf94079e2015-11-24 00:30:3393cur_file_num=0
[email protected]3e89a9d2013-08-15 18:03:0394function index_next {
watkf94079e2015-11-24 00:30:3395 if ((cur_file_num >= ${#files_to_index[@]})); then
[email protected]3e89a9d2013-08-15 18:03:0396 return
97 fi
98
watkf94079e2015-11-24 00:30:3399 async_index "${files_to_index[cur_file_num]}"
100 ((cur_file_num += 1)) || true
[email protected]3e89a9d2013-08-15 18:03:03101}
102
[email protected]3e89a9d2013-08-15 18:03:03103########
104### Main body of the script.
[email protected]fdb9ecc2012-06-28 01:53:45105
watkf94079e2015-11-24 00:30:33106remove_index=false
107should_index=true
108should_index_deps=true
109files_to_index=()
110while (($# > 0)); do
111 case "$1" in
112 -h)
113 usage_exit
[email protected]6192e652014-06-14 08:50:22114 ;;
watkf94079e2015-11-24 00:30:33115 -f)
116 remove_index=true
117 ;;
118 -r)
119 remove_index=true
120 should_index=false
121 ;;
122 -n)
123 should_index_deps=false
124 ;;
125 -*)
126 echo "Invalid option: $1" >&2
127 usage_exit
[email protected]6192e652014-06-14 08:50:22128 ;;
129 *)
watkf94079e2015-11-24 00:30:33130 if [[ ! -f "$1" ]]; then
131 echo "Path $1 does not exist."
132 exit 1
133 fi
134 files_to_index+=("$1")
[email protected]6192e652014-06-14 08:50:22135 ;;
136 esac
watkf94079e2015-11-24 00:30:33137 shift
[email protected]6192e652014-06-14 08:50:22138done
139
watkf94079e2015-11-24 00:30:33140if ((${#files_to_index[@]} == 0)); then
141 usage_exit
[email protected]d43a9fb2012-08-21 00:09:19142fi
[email protected]fdb9ecc2012-06-28 01:53:45143
watkf94079e2015-11-24 00:30:33144dependencies=()
145if $should_index_deps; then
146 for file in "${files_to_index[@]}"; do
147 # Append the shared library dependencies of this file that
148 # have the same dirname. The dirname is a signal that these
149 # shared libraries were part of the same build as the binary.
150 dependencies+=( \
151 $(ldd "$file" 2>/dev/null \
152 | grep $(dirname "$file") \
153 | sed "s/.*[ \t]\(.*\) (.*/\1/") \
154 )
155 done
[email protected]d43a9fb2012-08-21 00:09:19156fi
watkf94079e2015-11-24 00:30:33157files_to_index+=("${dependencies[@]}")
[email protected]fdb9ecc2012-06-28 01:53:45158
[email protected]3e89a9d2013-08-15 18:03:03159# Ensure we cleanup on on exit.
watkf94079e2015-11-24 00:30:33160trap on_exit EXIT INT
[email protected]3e89a9d2013-08-15 18:03:03161
[email protected]d43a9fb2012-08-21 00:09:19162# We're good to go! Create temp directory for index files.
watkf94079e2015-11-24 00:30:33163directory=$(mktemp -d)
164echo "Made temp directory $directory."
[email protected]4cec25342012-06-28 23:37:31165
[email protected]3e89a9d2013-08-15 18:03:03166# Start concurrent indexing.
167trap index_next USR1
168
169# 4 is an arbitrary default. When changing, remember we are likely IO bound
170# so basing this off the number of cores is not sensible.
watkf94079e2015-11-24 00:30:33171index_tasks=${INDEX_TASKS:-4}
172for ((i = 0; i < index_tasks; i++)); do
[email protected]3e89a9d2013-08-15 18:03:03173 index_next
174done
175
176# Do a wait loop. Bash waits that terminate due a trap have an exit
177# code > 128. We also ensure that our subshell's "normal" exit occurs with
178# an exit code > 128. This allows us to do consider a > 128 exit code as
179# an indication that the loop should continue. Unfortunately, it also means
180# we cannot use set -e since technically the "wait" is failing.
181wait
watkf94079e2015-11-24 00:30:33182while (($? > 128)); do
[email protected]3e89a9d2013-08-15 18:03:03183 wait
[email protected]4cec25342012-06-28 23:37:31184done