| 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 # | 7 # |
| 8 # This will run gdb index in parallel on a number of binaries using SIGUSR1 | 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 | 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 | 10 # nature of this technique, using "set -e" is very difficult. The SIGUSR1 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 echo -n "Removing temp directory $DIRECTORY..." | 29 echo -n "Removing temp directory $DIRECTORY..." |
| 30 rm -rf $DIRECTORY | 30 rm -rf $DIRECTORY |
| 31 echo done | 31 echo done |
| 32 fi | 32 fi |
| 33 } | 33 } |
| 34 | 34 |
| 35 # Add index to one binary. | 35 # Add index to one binary. |
| 36 function index_one_file { | 36 function index_one_file { |
| 37 local file=$1 | 37 local file=$1 |
| 38 local basename=$(basename "$file") | 38 local basename=$(basename "$file") |
| 39 local should_index="${SHOULD_INDEX}" |
| 39 | 40 |
| 40 local readelf_out=$(readelf -S "$file") | 41 local readelf_out=$(readelf -S "$file") |
| 41 if [[ $readelf_out =~ "gdb_index" ]]; then | 42 if [[ $readelf_out =~ "gdb_index" ]]; then |
| 42 echo "Skipped $basename -- already contains index." | 43 if [ "${REMOVE_INDEX}" = 1 ]; then |
| 43 else | 44 objcopy --remove-section .gdb_index "$file" |
| 45 echo "Removed index from $basename." |
| 46 else |
| 47 echo "Skipped $basename -- already contains index." |
| 48 should_index=0 |
| 49 fi |
| 50 fi |
| 51 |
| 52 if [ "${should_index}" = 1 ]; then |
| 44 local start=$(date +"%s%N") | 53 local start=$(date +"%s%N") |
| 45 echo "Adding index to $basename..." | 54 echo "Adding index to $basename..." |
| 46 | 55 |
| 47 gdb -batch "$file" -ex "save gdb-index $DIRECTORY" -ex "quit" | 56 gdb -batch "$file" -ex "save gdb-index $DIRECTORY" -ex "quit" |
| 48 local index_file="$DIRECTORY/$basename.gdb-index" | 57 local index_file="$DIRECTORY/$basename.gdb-index" |
| 49 if [ -f "$index_file" ]; then | 58 if [ -f "$index_file" ]; then |
| 50 objcopy --add-section .gdb_index="$index_file" \ | 59 objcopy --add-section .gdb_index="$index_file" \ |
| 51 --set-section-flags .gdb_index=readonly "$file" "$file" | 60 --set-section-flags .gdb_index=readonly "$file" "$file" |
| 52 local finish=$(date +"%s%N") | 61 local finish=$(date +"%s%N") |
| 53 local elappsed=$(((finish - start)/1000000)) | 62 local elappsed=$(((finish - start)/1000000)) |
| (...skipping 22 matching lines...) Expand all Loading... |
| 76 fi | 85 fi |
| 77 | 86 |
| 78 async_index "${FILES_TO_INDEX[CUR_FILE_NUM]}" | 87 async_index "${FILES_TO_INDEX[CUR_FILE_NUM]}" |
| 79 ((CUR_FILE_NUM += 1)) || true | 88 ((CUR_FILE_NUM += 1)) || true |
| 80 } | 89 } |
| 81 | 90 |
| 82 | 91 |
| 83 ######## | 92 ######## |
| 84 ### Main body of the script. | 93 ### Main body of the script. |
| 85 | 94 |
| 95 REMOVE_INDEX=0 |
| 96 SHOULD_INDEX=1 |
| 97 while getopts ":f:r" opt; do |
| 98 case $opt in |
| 99 f) |
| 100 REMOVE_INDEX=1 |
| 101 shift |
| 102 ;; |
| 103 r) |
| 104 REMOVE_INDEX=1 |
| 105 SHOULD_INDEX=0 |
| 106 shift |
| 107 ;; |
| 108 *) |
| 109 echo "Invalid option: -$OPTARG" >&2 |
| 110 ;; |
| 111 esac |
| 112 done |
| 113 |
| 86 if [[ ! $# == 1 ]]; then | 114 if [[ ! $# == 1 ]]; then |
| 87 echo "Usage: $0 path-to-binary" | 115 echo "Usage: $0 [-f] [-r] path-to-binary" |
| 116 echo " -f forces replacement of an existing index." |
| 117 echo " -r removes the index section." |
| 88 exit 1 | 118 exit 1 |
| 89 fi | 119 fi |
| 90 | 120 |
| 91 FILENAME="$1" | 121 FILENAME="$1" |
| 92 if [[ ! -f "$FILENAME" ]]; then | 122 if [[ ! -f "$FILENAME" ]]; then |
| 93 echo "Path $FILENAME does not exist." | 123 echo "Path $FILENAME does not exist." |
| 94 exit 1 | 124 exit 1 |
| 95 fi | 125 fi |
| 96 | 126 |
| 97 # Ensure we cleanup on on exit. | 127 # Ensure we cleanup on on exit. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 122 | 152 |
| 123 # Do a wait loop. Bash waits that terminate due a trap have an exit | 153 # 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 | 154 # 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 | 155 # 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 | 156 # an indication that the loop should continue. Unfortunately, it also means |
| 127 # we cannot use set -e since technically the "wait" is failing. | 157 # we cannot use set -e since technically the "wait" is failing. |
| 128 wait | 158 wait |
| 129 while (( $? > 128 )); do | 159 while (( $? > 128 )); do |
| 130 wait | 160 wait |
| 131 done | 161 done |
| OLD | NEW |