From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B4EFE1F3FED for ; Thu, 5 Mar 2026 16:32:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772728365; cv=none; b=OwWOEjg7NmIIs1z3xMa+cgRJk+KcPDEW6WyLEgbk0J+AD6N1LIpCC+srQAlQ3R+y9zrxjRJAV5RtOTnCulEtlPyE+65+VmQ3+lmP0gW/U3u9gXeMiRxNMtpB0w4dw3E3VwikqI5h8g1x3AmwA0PQen5SR3F69hXd9g3UyUEFDsU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772728365; c=relaxed/simple; bh=Xuyhxy6btSgmluuz7Dxe5EvD2tJ7dz0nAXgE//8SZNE=; h=Date:From:To:Cc:Subject:Message-Id:In-Reply-To:References: Mime-Version:Content-Type; b=Hbvb1w2yUa2db3dJ/TFaVnLUE6NHCc2VAEd8Esl2dt80lcrIyYsWfGfThgI4uoiKOJfdKwZZHgmtHSV5M5+sMEI8FwkTwDBj5rrUA/Sx3BpfzMOmXOYmTo42Betz/vEehCu4m4rI1XugUg8quw65EHAb8eyBo9whapUReb7KzQw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=RoEMDfsw; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="RoEMDfsw" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 815AAC116C6; Thu, 5 Mar 2026 16:32:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1772728365; bh=Xuyhxy6btSgmluuz7Dxe5EvD2tJ7dz0nAXgE//8SZNE=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=RoEMDfswfCZAlmWHRr33QBqBP+yiASIszVwe5V9eiRGcdSCpNCWy6lJCkq0OxDCjz g0siVTLCX5Qusc+TJIhsK2afPxh/GSUzeCdPgc0ek+zkrSo5Q9xR6izXyMrGR4Mbvt 2dgHPd/dxv55TUFD+Fuv+QQ3lnxeKUEVwSazB50Jfs9OrBglPem48M/DuwUHp1GQ45 xwZMWg1vD3S8eXl1+vDdIF80n2J9jiuxq/5UILPEys4H+PcGUOxWP43CTk2EVbTeyq a/eoFZr1pbm6/1Csy0EJM5vx6wtgSTic+9SdB75lYRlFBgQThGEsvjyes7Fst3wxkh 3IdQgPFn7d3NA== Date: Fri, 6 Mar 2026 01:32:41 +0900 From: Masami Hiramatsu (Google) To: Sasha Levin Cc: Matthieu Baerts , Andrew Morton , Carlos Llamas , Luca Ceresoli , linux-kernel@vger.kernel.org Subject: Re: [PATCH] decode_stacktrace: Support heuristic caller address search Message-Id: <20260306013241.4b0c7502d8db152f27f434b1@kernel.org> In-Reply-To: References: <177268753893.3271988.11559821020066428486.stgit@mhiramat.tok.corp.google.com> X-Mailer: Sylpheed 3.8.0beta1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Thu, 5 Mar 2026 10:51:47 -0500 Sasha Levin wrote: > On Thu, 5 Mar 2026 14:12:19 +0900, Masami Hiramatsu (Google) wrote: > > Add -c option to search call address search to decode_stacktrace. > > This tries to decode line info backwards, starting from 1byte before > > the return address, and displays the first line info it founds as > > the caller address. > > If it tries up to 10bytes before (or the symbol address) and still > > can not find it, it gives up and decodes the return address. > > The commit message says "up to 10bytes" but the code passes $offset > (the function offset from the symbol) as the max iteration count to > search_call_site(). There's no 10-byte cap anywhere in the code? > $offset can easily be hundreds or thousands of bytes into a function. Ah, sorry. I forgot to set maximum :( > > > +search_call_site() { > > + # Instead of using the return address, use the nearest line info > > + # address before given address. > > + local return_addr=${2} > > + local max=${3} > > + local i > > + > > + for i in $(seq 1 ${max}); do > > + local expr=$((0x$return_addr-$i)) > > + local address=$(printf "%x\n" "$expr") > > + > > + local code=$(${ADDR2LINE} -i -e "${1}" "$address" 2>/dev/null) > > + local first=${code% *} > > + if [[ "$code" != "" && "$code" != ${UNKNOWN_LINE} && "${first#*:}" != "?" ]]; then > > To also address Matthieu's question about performance: I think this > whole iterative search could be replaced by simply subtracting 1 from > the return address before passing it to addr2line. > > DWARF line tables map address *ranges* to source lines, so any address > within the CALL instruction resolves to the correct source line. > return_addr-1 is guaranteed to land inside the CALL instruction (it's > the last byte of it), so a single addr2line call is sufficient. Ah, got it, OK. I also confirmed "addr-1" works. But if there is no lineinfo entry for the call instruction, shouldn't we check more instructions before the call? > > This is exactly what the kernel itself does in sprint_backtrace() > (kernel/kallsyms.c:570): it passes symbol_offset=-1 to > __sprint_symbol(), which does `address += symbol_offset` before > lookup. GDB, perf, and libunwind all use the same addr-1 trick for > the same reason. OK. > > That would make this both correct and free. > > > + if [[ "$code" != "" && "$code" != ${UNKNOWN_LINE} && "${first#*:}" != "?" ]]; then > > Minor: ${UNKNOWN_LINE} is "??:0" -- when unquoted on the RHS of != inside > [[ ]], the ? characters are interpreted as glob wildcards (each matching > any single character). It happens to work here because ? also matches '?' > itself, but it should be quoted as "${UNKNOWN_LINE}" for correctness. > Same issue on the other != ${UNKNOWN_LINE} below. Ah, OK. Let me fix it. Thanks, > > -- > Thanks, > Sasha -- Masami Hiramatsu (Google)