OLD | NEW |
1 #!/bin/bash | 1 #!/bin/bash |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 # | 5 # |
6 # Saves the gdb index for a given binary and its shared library dependencies. | 6 # Saves the gdb index for a given binary and its shared library dependencies. |
| 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. |
7 | 15 |
8 set -e | 16 # Cleanup temp directory and ensure all child jobs are dead-dead. |
| 17 function on_exit { |
| 18 trap "" EXIT USR1 # Avoid reentrancy. |
| 19 |
| 20 local jobs=$(jobs -p) |
| 21 if [ -n "$jobs" ]; then |
| 22 echo -n "Killing outstanding index jobs..." |
| 23 kill -KILL $(jobs -p) |
| 24 wait |
| 25 echo "done" |
| 26 fi |
| 27 |
| 28 if [ -f "$DIRECTORY" ]; then |
| 29 echo -n "Removing temp directory $DIRECTORY..." |
| 30 rm -rf $DIRECTORY |
| 31 echo done |
| 32 fi |
| 33 } |
| 34 |
| 35 # Add index to one binary. |
| 36 function index_one_file { |
| 37 local file=$1 |
| 38 local basename=$(basename "$file") |
| 39 |
| 40 local readelf_out=$(readelf -S "$file") |
| 41 if [[ $readelf_out =~ "gdb_index" ]]; then |
| 42 echo "Skipped $basename -- already contains index." |
| 43 else |
| 44 local start=$(date +"%s%N") |
| 45 echo "Adding index to $basename..." |
| 46 |
| 47 gdb -batch "$file" -ex "save gdb-index $DIRECTORY" -ex "quit" |
| 48 local index_file="$DIRECTORY/$basename.gdb-index" |
| 49 if [ -f "$index_file" ]; then |
| 50 objcopy --add-section .gdb_index="$index_file" \ |
| 51 --set-section-flags .gdb_index=readonly "$file" "$file" |
| 52 local finish=$(date +"%s%N") |
| 53 local elappsed=$(((finish - start)/1000000)) |
| 54 echo " ...$basename indexed. [${elappsed}ms]" |
| 55 else |
| 56 echo " ...$basename unindexable." |
| 57 fi |
| 58 fi |
| 59 } |
| 60 |
| 61 # Functions that when combined, concurrently index all files in FILES_TO_INDEX |
| 62 # array. The global FILES_TO_INDEX is declared in the main body of the script. |
| 63 function async_index { |
| 64 # Start a background subshell to run the index command. |
| 65 { |
| 66 index_one_file $1 |
| 67 kill -SIGUSR1 $$ # $$ resolves to the parent script. |
| 68 exit 129 # See comment above wait loop at bottom. |
| 69 } & |
| 70 } |
| 71 |
| 72 CUR_FILE_NUM=0 |
| 73 function index_next { |
| 74 if (( CUR_FILE_NUM >= ${#FILES_TO_INDEX[@]} )); then |
| 75 return |
| 76 fi |
| 77 |
| 78 async_index "${FILES_TO_INDEX[CUR_FILE_NUM]}" |
| 79 ((CUR_FILE_NUM += 1)) || true |
| 80 } |
| 81 |
| 82 |
| 83 ######## |
| 84 ### Main body of the script. |
9 | 85 |
10 if [[ ! $# == 1 ]]; then | 86 if [[ ! $# == 1 ]]; then |
11 echo "Usage: $0 path-to-binary" | 87 echo "Usage: $0 path-to-binary" |
12 exit 1 | 88 exit 1 |
13 fi | 89 fi |
14 | 90 |
15 FILENAME="$1" | 91 FILENAME="$1" |
16 if [[ ! -f "$FILENAME" ]]; then | 92 if [[ ! -f "$FILENAME" ]]; then |
17 echo "Path $FILENAME does not exist." | 93 echo "Path $FILENAME does not exist." |
18 exit 1 | 94 exit 1 |
19 fi | 95 fi |
20 | 96 |
| 97 # Ensure we cleanup on on exit. |
| 98 trap on_exit EXIT |
| 99 |
21 # We're good to go! Create temp directory for index files. | 100 # We're good to go! Create temp directory for index files. |
22 DIRECTORY=$(mktemp -d) | 101 DIRECTORY=$(mktemp -d) |
23 echo "Made temp directory $DIRECTORY." | 102 echo "Made temp directory $DIRECTORY." |
24 | 103 |
25 # Always remove directory on exit. | 104 # Create array with the filename and all shared libraries that |
26 trap "{ echo -n Removing temp directory $DIRECTORY...; | 105 # have the same dirname. The dirname is a signal that these |
27 rm -rf $DIRECTORY; echo done; }" EXIT | 106 # shared libraries were part of the same build as the binary. |
28 | 107 declare -a FILES_TO_INDEX=($FILENAME |
29 # Grab all the chromium shared library files. | 108 $(ldd "$FILENAME" 2>/dev/null \ |
30 so_files=$(ldd "$FILENAME" 2>/dev/null \ | |
31 | grep $(dirname "$FILENAME") \ | 109 | grep $(dirname "$FILENAME") \ |
32 | sed "s/.*[ \t]\(.*\) (.*/\1/") | 110 | sed "s/.*[ \t]\(.*\) (.*/\1/") |
| 111 ) |
33 | 112 |
34 # Add index to binary and the shared library dependencies. | 113 # Start concurrent indexing. |
35 for file in "$FILENAME" $so_files; do | 114 trap index_next USR1 |
36 basename=$(basename "$file") | 115 |
37 echo -n "Adding index to $basename..." | 116 # 4 is an arbitrary default. When changing, remember we are likely IO bound |
38 readelf_out=$(readelf -S "$file") | 117 # so basing this off the number of cores is not sensible. |
39 if [[ $readelf_out =~ "gdb_index" ]]; then | 118 INDEX_TASKS=${INDEX_TASKS:-4} |
40 echo "already contains index. Skipped." | 119 for ((i=0;i<${INDEX_TASKS};i++)); do |
41 else | 120 index_next |
42 gdb -batch "$file" -ex "save gdb-index $DIRECTORY" -ex "quit" | |
43 objcopy --add-section .gdb_index="$DIRECTORY"/$basename.gdb-index \ | |
44 --set-section-flags .gdb_index=readonly "$file" "$file" | |
45 echo "done." | |
46 fi | |
47 done | 121 done |
| 122 |
| 123 # Do a wait loop. Bash waits that terminate due a trap have an exit |
| 124 # code > 128. We also ensure that our subshell's "normal" exit occurs with |
| 125 # an exit code > 128. This allows us to do consider a > 128 exit code as |
| 126 # an indication that the loop should continue. Unfortunately, it also means |
| 127 # we cannot use set -e since technically the "wait" is failing. |
| 128 wait |
| 129 while (( $? > 128 )); do |
| 130 wait |
| 131 done |
OLD | NEW |