Movatterモバイル変換


[0]ホーム

URL:


Google Git
Sign in
chromium /chromium /src /refs/heads/main /. /build /gdb-add-index
blob: e756ceacc62a595a5796039da86291c63e06f919 [file] [log] [blame]
ajwong@chromium.orgd43a9fb2012-08-21 00:09:19[diff] [blame]1#!/bin/bash
Avi Drissman73a09d12022-09-08 20:33:38[diff] [blame]2# Copyright 2012 The Chromium Authors
ajwong@chromium.org4cec25342012-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.orgd43a9fb2012-08-21 00:09:19[diff] [blame]6# Saves the gdb index for a given binary and its shared library dependencies.
ajwong@chromium.org3e89a9d2013-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.orgfdb9ecc2012-06-28 01:53:45[diff] [blame]15
watkf94079e2015-11-24 00:30:33[diff] [blame]16function 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.org3e89a9d2013-08-15 18:03:03[diff] [blame]27# Cleanup temp directory and ensure all child jobs are dead-dead.
28function on_exit{
29 trap"" EXIT USR1# Avoid reentrancy.
30
31local jobs=$(jobs-p)
32if[-n"$jobs"];then
33 echo-n"Killing outstanding index jobs..."
34 kill-KILL $(jobs-p)
35 wait
36 echo"done"
37fi
38
watkbb802c682016-06-01 20:18:44[diff] [blame]39if[-d"$directory"];then
watkf94079e2015-11-24 00:30:33[diff] [blame]40 echo-n"Removing temp directory $directory..."
41 rm-rf"$directory"
ajwong@chromium.org3e89a9d2013-08-15 18:03:03[diff] [blame]42 echodone
43fi
44}
45
46# Add index to one binary.
47function index_one_file{
48local file=$1
49local basename=$(basename"$file")
watkf94079e2015-11-24 00:30:33[diff] [blame]50local should_index_this_file="${should_index}"
ajwong@chromium.org3e89a9d2013-08-15 18:03:03[diff] [blame]51
cleichnercdc89df2015-01-09 00:36:50[diff] [blame]52local readelf_out=$(${TOOLCHAIN_PREFIX}readelf-S"$file")
ajwong@chromium.org3e89a9d2013-08-15 18:03:03[diff] [blame]53if[[ $readelf_out=~"gdb_index"]];then
watkf94079e2015-11-24 00:30:33[diff] [blame]54if $remove_index;then
cleichnercdc89df2015-01-09 00:36:50[diff] [blame]55 ${TOOLCHAIN_PREFIX}objcopy--remove-section.gdb_index"$file"
ajwong@chromium.org6192e652014-06-14 08:50:22[diff] [blame]56 echo"Removed index from $basename."
57else
58 echo"Skipped $basename -- already contains index."
watkf94079e2015-11-24 00:30:33[diff] [blame]59 should_index_this_file=false
ajwong@chromium.org6192e652014-06-14 08:50:22[diff] [blame]60fi
61fi
62
watkf94079e2015-11-24 00:30:33[diff] [blame]63if $should_index_this_file;then
ajwong@chromium.org3e89a9d2013-08-15 18:03:03[diff] [blame]64local start=$(date+"%s%N")
65 echo"Adding index to $basename..."
66
watkf94079e2015-11-24 00:30:33[diff] [blame]67 ${TOOLCHAIN_PREFIX}gdb-batch"$file"-ex"save gdb-index $directory" \
cleichnercdc89df2015-01-09 00:36:50[diff] [blame]68-ex"quit"
watkf94079e2015-11-24 00:30:33[diff] [blame]69local index_file="$directory/$basename.gdb-index"
ajwong@chromium.org3e89a9d2013-08-15 18:03:03[diff] [blame]70if[-f"$index_file"];then
cleichnercdc89df2015-01-09 00:36:50[diff] [blame]71 ${TOOLCHAIN_PREFIX}objcopy--add-section.gdb_index="$index_file" \
ajwong@chromium.org3e89a9d2013-08-15 18:03:03[diff] [blame]72--set-section-flags.gdb_index=readonly"$file""$file"
73local finish=$(date+"%s%N")
watkf94079e2015-11-24 00:30:33[diff] [blame]74local elapsed=$(((finish- start)/1000000))
cleichnercdc89df2015-01-09 00:36:50[diff] [blame]75 echo" ...$basename indexed. [${elapsed}ms]"
ajwong@chromium.org3e89a9d2013-08-15 18:03:03[diff] [blame]76else
77 echo" ...$basename unindexable."
78fi
79fi
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 exit129# See comment above wait loop at bottom.
90}&
91}
92
watkf94079e2015-11-24 00:30:33[diff] [blame]93cur_file_num=0
ajwong@chromium.org3e89a9d2013-08-15 18:03:03[diff] [blame]94function index_next{
watkf94079e2015-11-24 00:30:33[diff] [blame]95if((cur_file_num>= ${#files_to_index[@]}));then
ajwong@chromium.org3e89a9d2013-08-15 18:03:03[diff] [blame]96return
97fi
98
watkf94079e2015-11-24 00:30:33[diff] [blame]99 async_index"${files_to_index[cur_file_num]}"
100((cur_file_num+=1))|| true
ajwong@chromium.org3e89a9d2013-08-15 18:03:03[diff] [blame]101}
102
ajwong@chromium.org3e89a9d2013-08-15 18:03:03[diff] [blame]103########
104### Main body of the script.
ajwong@chromium.orgfdb9ecc2012-06-28 01:53:45[diff] [blame]105
watkf94079e2015-11-24 00:30:33[diff] [blame]106remove_index=false
107should_index=true
108should_index_deps=true
109files_to_index=()
110while(($# > 0)); do
111case"$1"in
112-h)
113 usage_exit
ajwong@chromium.org6192e652014-06-14 08:50:22[diff] [blame]114;;
watkf94079e2015-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.org6192e652014-06-14 08:50:22[diff] [blame]128;;
129*)
watkf94079e2015-11-24 00:30:33[diff] [blame]130if[[!-f"$1"]];then
131 echo"Path $1 does not exist."
132 exit1
133fi
134 files_to_index+=("$1")
ajwong@chromium.org6192e652014-06-14 08:50:22[diff] [blame]135;;
136esac
watkf94079e2015-11-24 00:30:33[diff] [blame]137 shift
ajwong@chromium.org6192e652014-06-14 08:50:22[diff] [blame]138done
139
watkf94079e2015-11-24 00:30:33[diff] [blame]140if((${#files_to_index[@]}==0));then
141 usage_exit
ajwong@chromium.orgd43a9fb2012-08-21 00:09:19[diff] [blame]142fi
ajwong@chromium.orgfdb9ecc2012-06-28 01:53:45[diff] [blame]143
watkf94079e2015-11-24 00:30:33[diff] [blame]144dependencies=()
145if $should_index_deps;then
146for 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)
155done
ajwong@chromium.orgd43a9fb2012-08-21 00:09:19[diff] [blame]156fi
watkf94079e2015-11-24 00:30:33[diff] [blame]157files_to_index+=("${dependencies[@]}")
ajwong@chromium.orgfdb9ecc2012-06-28 01:53:45[diff] [blame]158
ajwong@chromium.org3e89a9d2013-08-15 18:03:03[diff] [blame]159# Ensure we cleanup on on exit.
watkf94079e2015-11-24 00:30:33[diff] [blame]160trap on_exit EXIT INT
ajwong@chromium.org3e89a9d2013-08-15 18:03:03[diff] [blame]161
ajwong@chromium.orgd43a9fb2012-08-21 00:09:19[diff] [blame]162# We're good to go! Create temp directory for index files.
watkf94079e2015-11-24 00:30:33[diff] [blame]163directory=$(mktemp-d)
164echo"Made temp directory $directory."
ajwong@chromium.org4cec25342012-06-28 23:37:31[diff] [blame]165
ajwong@chromium.org3e89a9d2013-08-15 18:03:03[diff] [blame]166# 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:33[diff] [blame]171index_tasks=${INDEX_TASKS:-4}
172for((i=0; i< index_tasks; i++));do
ajwong@chromium.org3e89a9d2013-08-15 18:03:03[diff] [blame]173 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:33[diff] [blame]182while(($?>128));do
ajwong@chromium.org3e89a9d2013-08-15 18:03:03[diff] [blame]183 wait
ajwong@chromium.org4cec25342012-06-28 23:37:31[diff] [blame]184done

[8]ページ先頭

©2009-2025 Movatter.jp