gdb-add-index 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #!/bin/bash
  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.
  5. #
  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.
  15. function usage_exit {
  16. echo "Usage: $0 [-f] [-r] [-n] <paths-to-binaries>..."
  17. echo " -f forces replacement of an existing index."
  18. echo " -r removes the index section."
  19. echo " -n don't extract the dependencies of each binary with lld."
  20. echo " e.g., $0 -n out/Debug/lib.unstripped/lib*"
  21. echo
  22. echo " Set TOOLCHAIN_PREFIX to use a non-default set of binutils."
  23. exit 1
  24. }
  25. # Cleanup temp directory and ensure all child jobs are dead-dead.
  26. function on_exit {
  27. trap "" EXIT USR1 # Avoid reentrancy.
  28. local jobs=$(jobs -p)
  29. if [ -n "$jobs" ]; then
  30. echo -n "Killing outstanding index jobs..."
  31. kill -KILL $(jobs -p)
  32. wait
  33. echo "done"
  34. fi
  35. if [ -d "$directory" ]; then
  36. echo -n "Removing temp directory $directory..."
  37. rm -rf "$directory"
  38. echo done
  39. fi
  40. }
  41. # Add index to one binary.
  42. function index_one_file {
  43. local file=$1
  44. local basename=$(basename "$file")
  45. local should_index_this_file="${should_index}"
  46. local readelf_out=$(${TOOLCHAIN_PREFIX}readelf -S "$file")
  47. if [[ $readelf_out =~ "gdb_index" ]]; then
  48. if $remove_index; then
  49. ${TOOLCHAIN_PREFIX}objcopy --remove-section .gdb_index "$file"
  50. echo "Removed index from $basename."
  51. else
  52. echo "Skipped $basename -- already contains index."
  53. should_index_this_file=false
  54. fi
  55. fi
  56. if $should_index_this_file; then
  57. local start=$(date +"%s%N")
  58. echo "Adding index to $basename..."
  59. ${TOOLCHAIN_PREFIX}gdb -batch "$file" -ex "save gdb-index $directory" \
  60. -ex "quit"
  61. local index_file="$directory/$basename.gdb-index"
  62. if [ -f "$index_file" ]; then
  63. ${TOOLCHAIN_PREFIX}objcopy --add-section .gdb_index="$index_file" \
  64. --set-section-flags .gdb_index=readonly "$file" "$file"
  65. local finish=$(date +"%s%N")
  66. local elapsed=$(((finish - start) / 1000000))
  67. echo " ...$basename indexed. [${elapsed}ms]"
  68. else
  69. echo " ...$basename unindexable."
  70. fi
  71. fi
  72. }
  73. # Functions that when combined, concurrently index all files in FILES_TO_INDEX
  74. # array. The global FILES_TO_INDEX is declared in the main body of the script.
  75. function async_index {
  76. # Start a background subshell to run the index command.
  77. {
  78. index_one_file $1
  79. kill -SIGUSR1 $$ # $$ resolves to the parent script.
  80. exit 129 # See comment above wait loop at bottom.
  81. } &
  82. }
  83. cur_file_num=0
  84. function index_next {
  85. if ((cur_file_num >= ${#files_to_index[@]})); then
  86. return
  87. fi
  88. async_index "${files_to_index[cur_file_num]}"
  89. ((cur_file_num += 1)) || true
  90. }
  91. ########
  92. ### Main body of the script.
  93. remove_index=false
  94. should_index=true
  95. should_index_deps=true
  96. files_to_index=()
  97. while (($# > 0)); do
  98. case "$1" in
  99. -h)
  100. usage_exit
  101. ;;
  102. -f)
  103. remove_index=true
  104. ;;
  105. -r)
  106. remove_index=true
  107. should_index=false
  108. ;;
  109. -n)
  110. should_index_deps=false
  111. ;;
  112. -*)
  113. echo "Invalid option: $1" >&2
  114. usage_exit
  115. ;;
  116. *)
  117. if [[ ! -f "$1" ]]; then
  118. echo "Path $1 does not exist."
  119. exit 1
  120. fi
  121. files_to_index+=("$1")
  122. ;;
  123. esac
  124. shift
  125. done
  126. if ((${#files_to_index[@]} == 0)); then
  127. usage_exit
  128. fi
  129. dependencies=()
  130. if $should_index_deps; then
  131. for file in "${files_to_index[@]}"; do
  132. # Append the shared library dependencies of this file that
  133. # have the same dirname. The dirname is a signal that these
  134. # shared libraries were part of the same build as the binary.
  135. dependencies+=( \
  136. $(ldd "$file" 2>/dev/null \
  137. | grep $(dirname "$file") \
  138. | sed "s/.*[ \t]\(.*\) (.*/\1/") \
  139. )
  140. done
  141. fi
  142. files_to_index+=("${dependencies[@]}")
  143. # Ensure we cleanup on on exit.
  144. trap on_exit EXIT INT
  145. # We're good to go! Create temp directory for index files.
  146. directory=$(mktemp -d)
  147. echo "Made temp directory $directory."
  148. # Start concurrent indexing.
  149. trap index_next USR1
  150. # 4 is an arbitrary default. When changing, remember we are likely IO bound
  151. # so basing this off the number of cores is not sensible.
  152. index_tasks=${INDEX_TASKS:-4}
  153. for ((i = 0; i < index_tasks; i++)); do
  154. index_next
  155. done
  156. # Do a wait loop. Bash waits that terminate due a trap have an exit
  157. # code > 128. We also ensure that our subshell's "normal" exit occurs with
  158. # an exit code > 128. This allows us to do consider a > 128 exit code as
  159. # an indication that the loop should continue. Unfortunately, it also means
  160. # we cannot use set -e since technically the "wait" is failing.
  161. wait
  162. while (($? > 128)); do
  163. wait
  164. done