scripts/faddr2line

Source file repositories/reference/linux-study-clean/scripts/faddr2line

File Facts

System
Linux kernel
Corpus path
scripts/faddr2line
Extension
[no extension]
Size
11218 bytes
Lines
381
Domain
Support Tooling And Documentation
Bucket
scripts
Inferred role
Support Tooling And Documentation: scripts
Status
atlas-only

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

# addr2line output.  HACK ALERT: This assumes that start_kernel() is in
# init/main.c!  This only works for vmlinux.  Otherwise it falls back to
# printing the absolute path.
find_dir_prefix() {
	local start_kernel_addr=$(echo "${ELF_SYMS}" | sed 's/\[.*\]//' |
		${AWK} '$8 == "start_kernel" {printf "0x%s", $2}')
	[[ -z $start_kernel_addr ]] && return

	run_addr2line ${start_kernel_addr} ""
	[[ -z $ADDR2LINE_OUT ]] && return

	local file_line=${ADDR2LINE_OUT#* at }
	if [[ -z $file_line ]] || [[ $file_line = $ADDR2LINE_OUT ]]; then
		return
	fi
	local prefix=${file_line%init/main.c:*}
	if [[ -z $prefix ]] || [[ $prefix = $file_line ]]; then
		return
	fi

	DIR_PREFIX=$prefix
	return 0
}

run_readelf() {
	local objfile=$1
	local tmpfile
	tmpfile=$(mktemp)

	${READELF} --file-header --section-headers --symbols --wide "$objfile" > "$tmpfile"

	# This assumes that readelf first prints the file header, then the section headers, then the symbols.
	# Note: It seems that GNU readelf does not prefix section headers with the "There are X section headers"
	# line when multiple options are given, so let's also match with the "Section Headers:" line.
	ELF_FILEHEADER=$(sed -n '/There are [0-9]* section headers, starting at offset\|Section Headers:/q;p' "$tmpfile")
	ELF_SECHEADERS=$(sed -n '/There are [0-9]* section headers, starting at offset\|Section Headers:/,$p' "$tmpfile" | sed -n '/Symbol table .* contains [0-9]* entries:/q;p')
	ELF_SYMS=$(sed -n '/Symbol table .* contains [0-9]* entries:/,$p' "$tmpfile")

	rm -f -- "$tmpfile"
}

check_vmlinux() {
	# vmlinux uses absolute addresses in the section table rather than
	# section offsets.
	IS_VMLINUX=0
	local file_type=$(echo "${ELF_FILEHEADER}" |
		${AWK} '$1 == "Type:" { print $2; exit }')
	if [[ $file_type = "EXEC" ]] || [[ $file_type == "DYN" ]]; then
		IS_VMLINUX=1
	fi
}

init_addr2line() {
	local objfile=$1

	check_vmlinux

	ADDR2LINE_ARGS="--functions --pretty-print --inlines --addresses --exe=$objfile"
	if [[ $IS_VMLINUX = 1 ]]; then
		# If the executable file is vmlinux, we don't pass section names to
		# addr2line, so we can launch it now as a single long-running process.
		coproc ADDR2LINE_PROC (${ADDR2LINE} ${ADDR2LINE_ARGS})
	fi
}

run_addr2line() {
	local addr=$1
	local sec_name=$2

	if [[ $IS_VMLINUX = 1 ]]; then

Annotation

Implementation Notes