History

scripts/faddr2line: Reduce number of readelf calls to three

Rather than calling readelf several times for each invocation of __faddr2line, call readelf only three times at the beginning, and save its result for future use.

Brian Johannesmeyer committed 2 years ago

Commit 39cf6501 parent 22a40d11 file · +17 −9Browse files

Unified · Split

scripts/faddr2line+17−9
@@ -87,7 +87,7 @@ command -v ${ADDR2LINE} >/dev/null 2>&1 || die "${ADDR2LINE} isn't installed" · 86 unmodified lines
8787 find_dir_prefix() {
8888 local objfile=$1
8989
90− local start_kernel_addr=$(${READELF} --symbols --wide $objfile | sed 's/\[.*\]//' |
90+ local start_kernel_addr=$(echo "${ELF_SYMS}" | sed 's/\[.*\]//' |
9191 ${AWK} '$8 == "start_kernel" {printf "0x%s", $2}')
9292 [[ -z $start_kernel_addr ]] && return
9393
@@ -103,6 +103,14 @@ find_dir_prefix() { · 9 unmodified lines
103103 return 0
104104 }
105105
106+run_readelf() {
107+ local objfile=$1
108+
109+ ELF_FILEHEADER=$(${READELF} --file-header $objfile)
110+ ELF_SECHEADERS=$(${READELF} --section-headers --wide $objfile)
111+ ELF_SYMS=$(${READELF} --symbols --wide $objfile)
112+}
113+
106114 __faddr2line() {
107115 local objfile=$1
108116 local func_addr=$2
@@ -125,7 +133,7 @@ __faddr2line() { · 16 unmodified lines
125133
126134 # vmlinux uses absolute addresses in the section table rather than
127135 # section offsets.
128− local file_type=$(${READELF} --file-header $objfile |
136+ local file_type=$(echo "${ELF_FILEHEADER}" |
129137 ${AWK} '$1 == "Type:" { print $2; exit }')
130138 if [[ $file_type = "EXEC" ]] || [[ $file_type == "DYN" ]]; then
131139 is_vmlinux=1
@@ -143,8 +151,7 @@ __faddr2line() { · 11 unmodified lines
143151 local sec_name
144152
145153 # Get the section size:
146− sec_size=$(${READELF} --section-headers --wide $objfile |
147− sed 's/\[ /\[/' |
154+ sec_size=$(echo "${ELF_SECHEADERS}" | sed 's/\[ /\[/' |
148155 ${AWK} -v sec=$sym_sec '$1 == "[" sec "]" { print "0x" $6; exit }')
149156
150157 if [[ -z $sec_size ]]; then
@@ -154,8 +161,7 @@ __faddr2line() { · 3 unmodified lines
154161 fi
155162
156163 # Get the section name:
157− sec_name=$(${READELF} --section-headers --wide $objfile |
158− sed 's/\[ /\[/' |
164+ sec_name=$(echo "${ELF_SECHEADERS}" | sed 's/\[ /\[/' |
159165 ${AWK} -v sec=$sym_sec '$1 == "[" sec "]" { print $2; exit }')
160166
161167 if [[ -z $sec_name ]]; then
@@ -197,7 +203,7 @@ __faddr2line() { · 35 unmodified lines
197203 found=2
198204 break
199205 fi
200− done < <(${READELF} --symbols --wide $objfile | sed 's/\[.*\]//' | ${AWK} -v sec=$sym_sec '$7 == sec' | sort --key=2)
206+ done < <(echo "${ELF_SYMS}" | sed 's/\[.*\]//' | ${AWK} -v sec=$sym_sec '$7 == sec' | sort --key=2)
201207
202208 if [[ $found = 0 ]]; then
203209 warn "can't find symbol: sym_name: $sym_name sym_sec: $sym_sec sym_addr: $sym_addr sym_elf_size: $sym_elf_size"
@@ -278,7 +284,7 @@ __faddr2line() { · 74 unmodified lines
278284
279285 DONE=1
280286
281− done < <(${READELF} --symbols --wide $objfile | sed 's/\[.*\]//' | ${AWK} -v fn=$sym_name '$8 == fn')
287+ done < <(echo "${ELF_SYMS}" | sed 's/\[.*\]//' | ${AWK} -v fn=$sym_name '$8 == fn')
282288 }
283289
284290 [[ $# -lt 2 ]] && usage
@@ -291,7 +297,9 @@ LIST=0 · 6 unmodified lines
291297 [[ ! -f $objfile ]] && die "can't find objfile $objfile"
292298 shift
293299
294−${READELF} --section-headers --wide $objfile | ${GREP} -q '\.debug_info' || die "CONFIG_DEBUG_INFO not enabled"
300+run_readelf $objfile
301+
302+echo "${ELF_SECHEADERS}" | ${GREP} -q '\.debug_info' || die "CONFIG_DEBUG_INFO not enabled"
295303
296304 DIR_PREFIX=supercalifragilisticexpialidocious
297305 find_dir_prefix $objfile