ajwong@chromium.org | d43a9fb | 2012-08-21 00:09:19 | [diff] [blame] | 1 | #!/bin/bash |
Avi Drissman | 73a09d1 | 2022-09-08 20:33:38 | [diff] [blame] | 2 | # Copyright 2012 The Chromium Authors |
ajwong@chromium.org | 4cec2534 | 2012-06-28 23:37:31 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | # |
ajwong@chromium.org | d43a9fb | 2012-08-21 00:09:19 | [diff] [blame] | 6 | # Saves the gdb index for a given binary and its shared library dependencies. |
ajwong@chromium.org | 3e89a9d | 2013-08-15 18:03:03 | [diff] [blame] | 7 | # |
| 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. |
ajwong@chromium.org | fdb9ecc | 2012-06-28 01:53:45 | [diff] [blame] | 15 | |
watk | f94079e | 2015-11-24 00:30:33 | [diff] [blame] | 16 | function 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 | exit1 |
| 25 | } |
| 26 | |
ajwong@chromium.org | 3e89a9d | 2013-08-15 18:03:03 | [diff] [blame] | 27 | # Cleanup temp directory and ensure all child jobs are dead-dead. |
| 28 | function 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 | |
watk | bb802c68 | 2016-06-01 20:18:44 | [diff] [blame] | 39 | if[-d"$directory"];then |
watk | f94079e | 2015-11-24 00:30:33 | [diff] [blame] | 40 | echo-n"Removing temp directory $directory..." |
| 41 | rm-rf"$directory" |
ajwong@chromium.org | 3e89a9d | 2013-08-15 18:03:03 | [diff] [blame] | 42 | echodone |
| 43 | fi |
| 44 | } |
| 45 | |
| 46 | # Add index to one binary. |
| 47 | function index_one_file{ |
| 48 | local file=$1 |
| 49 | local basename=$(basename"$file") |
watk | f94079e | 2015-11-24 00:30:33 | [diff] [blame] | 50 | local should_index_this_file="${should_index}" |
ajwong@chromium.org | 3e89a9d | 2013-08-15 18:03:03 | [diff] [blame] | 51 | |
cleichner | cdc89df | 2015-01-09 00:36:50 | [diff] [blame] | 52 | local readelf_out=$(${TOOLCHAIN_PREFIX}readelf-S"$file") |
ajwong@chromium.org | 3e89a9d | 2013-08-15 18:03:03 | [diff] [blame] | 53 | if[[ $readelf_out=~"gdb_index"]];then |
watk | f94079e | 2015-11-24 00:30:33 | [diff] [blame] | 54 | if $remove_index;then |
cleichner | cdc89df | 2015-01-09 00:36:50 | [diff] [blame] | 55 | ${TOOLCHAIN_PREFIX}objcopy--remove-section.gdb_index"$file" |
ajwong@chromium.org | 6192e65 | 2014-06-14 08:50:22 | [diff] [blame] | 56 | echo"Removed index from $basename." |
| 57 | else |
| 58 | echo"Skipped $basename -- already contains index." |
watk | f94079e | 2015-11-24 00:30:33 | [diff] [blame] | 59 | should_index_this_file=false |
ajwong@chromium.org | 6192e65 | 2014-06-14 08:50:22 | [diff] [blame] | 60 | fi |
| 61 | fi |
| 62 | |
watk | f94079e | 2015-11-24 00:30:33 | [diff] [blame] | 63 | if $should_index_this_file;then |
ajwong@chromium.org | 3e89a9d | 2013-08-15 18:03:03 | [diff] [blame] | 64 | local start=$(date+"%s%N") |
| 65 | echo"Adding index to $basename..." |
| 66 | |
watk | f94079e | 2015-11-24 00:30:33 | [diff] [blame] | 67 | ${TOOLCHAIN_PREFIX}gdb-batch"$file"-ex"save gdb-index $directory" \ |
cleichner | cdc89df | 2015-01-09 00:36:50 | [diff] [blame] | 68 | -ex"quit" |
watk | f94079e | 2015-11-24 00:30:33 | [diff] [blame] | 69 | local index_file="$directory/$basename.gdb-index" |
ajwong@chromium.org | 3e89a9d | 2013-08-15 18:03:03 | [diff] [blame] | 70 | if[-f"$index_file"];then |
cleichner | cdc89df | 2015-01-09 00:36:50 | [diff] [blame] | 71 | ${TOOLCHAIN_PREFIX}objcopy--add-section.gdb_index="$index_file" \ |
ajwong@chromium.org | 3e89a9d | 2013-08-15 18:03:03 | [diff] [blame] | 72 | --set-section-flags.gdb_index=readonly"$file""$file" |
| 73 | local finish=$(date+"%s%N") |
watk | f94079e | 2015-11-24 00:30:33 | [diff] [blame] | 74 | local elapsed=$(((finish- start)/1000000)) |
cleichner | cdc89df | 2015-01-09 00:36:50 | [diff] [blame] | 75 | echo" ...$basename indexed. [${elapsed}ms]" |
ajwong@chromium.org | 3e89a9d | 2013-08-15 18:03:03 | [diff] [blame] | 76 | 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. |
| 84 | function 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 | exit129# See comment above wait loop at bottom. |
| 90 | }& |
| 91 | } |
| 92 | |
watk | f94079e | 2015-11-24 00:30:33 | [diff] [blame] | 93 | cur_file_num=0 |
ajwong@chromium.org | 3e89a9d | 2013-08-15 18:03:03 | [diff] [blame] | 94 | function index_next{ |
watk | f94079e | 2015-11-24 00:30:33 | [diff] [blame] | 95 | if((cur_file_num>= ${#files_to_index[@]}));then |
ajwong@chromium.org | 3e89a9d | 2013-08-15 18:03:03 | [diff] [blame] | 96 | return |
| 97 | fi |
| 98 | |
watk | f94079e | 2015-11-24 00:30:33 | [diff] [blame] | 99 | async_index"${files_to_index[cur_file_num]}" |
| 100 | ((cur_file_num+=1))|| true |
ajwong@chromium.org | 3e89a9d | 2013-08-15 18:03:03 | [diff] [blame] | 101 | } |
| 102 | |
ajwong@chromium.org | 3e89a9d | 2013-08-15 18:03:03 | [diff] [blame] | 103 | ######## |
| 104 | ### Main body of the script. |
ajwong@chromium.org | fdb9ecc | 2012-06-28 01:53:45 | [diff] [blame] | 105 | |
watk | f94079e | 2015-11-24 00:30:33 | [diff] [blame] | 106 | remove_index=false |
| 107 | should_index=true |
| 108 | should_index_deps=true |
| 109 | files_to_index=() |
| 110 | while(($# > 0)); do |
| 111 | case"$1"in |
| 112 | -h) |
| 113 | usage_exit |
ajwong@chromium.org | 6192e65 | 2014-06-14 08:50:22 | [diff] [blame] | 114 | ;; |
watk | f94079e | 2015-11-24 00:30:33 | [diff] [blame] | 115 | -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 |
ajwong@chromium.org | 6192e65 | 2014-06-14 08:50:22 | [diff] [blame] | 128 | ;; |
| 129 | *) |
watk | f94079e | 2015-11-24 00:30:33 | [diff] [blame] | 130 | if[[!-f"$1"]];then |
| 131 | echo"Path $1 does not exist." |
| 132 | exit1 |
| 133 | fi |
| 134 | files_to_index+=("$1") |
ajwong@chromium.org | 6192e65 | 2014-06-14 08:50:22 | [diff] [blame] | 135 | ;; |
| 136 | esac |
watk | f94079e | 2015-11-24 00:30:33 | [diff] [blame] | 137 | shift |
ajwong@chromium.org | 6192e65 | 2014-06-14 08:50:22 | [diff] [blame] | 138 | done |
| 139 | |
watk | f94079e | 2015-11-24 00:30:33 | [diff] [blame] | 140 | if((${#files_to_index[@]}==0));then |
| 141 | usage_exit |
ajwong@chromium.org | d43a9fb | 2012-08-21 00:09:19 | [diff] [blame] | 142 | fi |
ajwong@chromium.org | fdb9ecc | 2012-06-28 01:53:45 | [diff] [blame] | 143 | |
watk | f94079e | 2015-11-24 00:30:33 | [diff] [blame] | 144 | dependencies=() |
| 145 | if $should_index_deps;then |
| 146 | for filein"${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 |
ajwong@chromium.org | d43a9fb | 2012-08-21 00:09:19 | [diff] [blame] | 156 | fi |
watk | f94079e | 2015-11-24 00:30:33 | [diff] [blame] | 157 | files_to_index+=("${dependencies[@]}") |
ajwong@chromium.org | fdb9ecc | 2012-06-28 01:53:45 | [diff] [blame] | 158 | |
ajwong@chromium.org | 3e89a9d | 2013-08-15 18:03:03 | [diff] [blame] | 159 | # Ensure we cleanup on on exit. |
watk | f94079e | 2015-11-24 00:30:33 | [diff] [blame] | 160 | trap on_exit EXIT INT |
ajwong@chromium.org | 3e89a9d | 2013-08-15 18:03:03 | [diff] [blame] | 161 | |
ajwong@chromium.org | d43a9fb | 2012-08-21 00:09:19 | [diff] [blame] | 162 | # We're good to go! Create temp directory for index files. |
watk | f94079e | 2015-11-24 00:30:33 | [diff] [blame] | 163 | directory=$(mktemp-d) |
| 164 | echo"Made temp directory $directory." |
ajwong@chromium.org | 4cec2534 | 2012-06-28 23:37:31 | [diff] [blame] | 165 | |
ajwong@chromium.org | 3e89a9d | 2013-08-15 18:03:03 | [diff] [blame] | 166 | # Start concurrent indexing. |
| 167 | trap 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. |
watk | f94079e | 2015-11-24 00:30:33 | [diff] [blame] | 171 | index_tasks=${INDEX_TASKS:-4} |
| 172 | for((i=0; i< index_tasks; i++));do |
ajwong@chromium.org | 3e89a9d | 2013-08-15 18:03:03 | [diff] [blame] | 173 | index_next |
| 174 | done |
| 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. |
| 181 | wait |
watk | f94079e | 2015-11-24 00:30:33 | [diff] [blame] | 182 | while(($?>128));do |
ajwong@chromium.org | 3e89a9d | 2013-08-15 18:03:03 | [diff] [blame] | 183 | wait |
ajwong@chromium.org | 4cec2534 | 2012-06-28 23:37:31 | [diff] [blame] | 184 | done |