From nobody Fri Dec 19 09:43:17 2025 Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A43B4322522 for ; Tue, 9 Dec 2025 12:14:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765282444; cv=none; b=dLa7l6+OOz7e03JbGsWiew0rT/Orl7PwBAUgerfqIZSah3pN2R29d1ODaAnp6Oi95x6NmJaQjHBUUu+suofxQdej/jBuGuSXZ9Qh83SeXtsGUUfjTYIAhQjyC73trvaPCTD7gp2aJV0J2vFsGR54lEJMmGSQ0CbJAzalTausL3Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765282444; c=relaxed/simple; bh=x1EZDXwpBtHEAJwiUMWKxwGt4FVIKlZiGWUwN6Sc5P8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=CHsCkJ28yoiJimkf0p2F6KEEwvZ3gfN/rF/MoXLwEQRP2Hbbak4PpOalCE67m4Oss+XjbVQQbDSQWdeunTd4A8Lfuqk2e2iRMwAb7p9uY/wmLq0Mk1jW9d78wHH2eBZ7sIxKzDMt4HQd22YVODf5BGtH4MiEbma9chj2Xjlpa3Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=OA069yn6; arc=none smtp.client-ip=209.85.210.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="OA069yn6" Received: by mail-pf1-f173.google.com with SMTP id d2e1a72fcca58-7b8eff36e3bso8570273b3a.2 for ; Tue, 09 Dec 2025 04:14:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1765282441; x=1765887241; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=xd9WY+m6+WX7Pur4QkVKIOZEntT/jaxLPiAk2vWokIQ=; b=OA069yn6MSRr+Og7FXrjNdhBMlN8ehPlTfUcpYhHzdlNbW4TC6pMg8DQW3fBLqUB3v T5iMLWsrqex3PD2Yqhl492WL2c9I+yk/yum3gzieNd35DK0l/0gzaE+esg/UGE1uJPY7 XpUp/YwdanA+tc11yV0m9/ZhKmy9HlaaXp1qMb6EvzZvK1Kne0lybESVlOltYntBvMWr GnMF3HyiG7eQj8xrmYvn+1u1wbT7yyeiH4uqLSy6HM9YbRE7y2e/le1rkvDaR7r8F42T +zAkG5A1VVmc3RyB46FD/StNaOsmjTWAkEuIl7Nu1oSg0rpBBCiZkgzEb0SYTpV5F+ls Goyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765282441; x=1765887241; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=xd9WY+m6+WX7Pur4QkVKIOZEntT/jaxLPiAk2vWokIQ=; b=EOck5jxYLfP727qGmdBv9YRMxdDgW37hK879DlmYSZpiKlHrpejr5ptaJ0XtvrbgoI Ple55+TWOCKhtQD8vQ/TKOnB/Juv4iPR7z7PqX34IaBMVUvySN5rsu9yGwL8fx4pw0zB OsUo+JKqkJAHWMQXQw7QaSJyHZ9HpQ92rP2pt6GlaCJ4Y0btsr7i/UaJbJn+XIwNWfiQ HEv9kOUDeBNg3KtAY+bCc0E7STfKI6Hnq9gAVJWZ7GgaVyVkcSQ+mpyO4ko++dL+D7W/ I1ye7RbLvtX0guZg4TlRcNTjA6YQRqgwpHDdRNHrPOZlO5Whk7tzftda8HSjqHd0gmmM dWyg== X-Forwarded-Encrypted: i=1; AJvYcCUE5Z04yNH72uxo9DckX6eHOr6o/IXB+LElz2wCBuYOBtFAZp3nIKvrtryswAnfTNfLj5o9EiHOBE2oCxo=@vger.kernel.org X-Gm-Message-State: AOJu0Yxr72HenEusxi970CBC/mTJJXjddu4iB4Z4sb19HPz9ea7HZihQ bxEBpQil6MmIMOcn60AZK7JGASaKDUpbjopT6s1xnEG8WSEZLT782b0r X-Gm-Gg: ASbGncvLeCIX26DyFIz9cmba0EF43CXcxLFCxXtRzp43/lWpUSrRx9JTb33DASi7IjX /fNDeehESGe4oMrU64+tauidhtU/SWGdo+auf0m7gtHo4pUvRap2GRcuHMnNtbNGi45PN/WCEhh xjvLkQYW24ApYc45rMGcOHPCX5+FwjLvS59rnBWXJInkDfLjtxaoZ4CuF3X6s1NACkxSriKPuSo HgZlkl1RUECDVzTcEKWsx9HO9Uc7z4IjtlAfCPZor8YSDc5np45SYqcqG4XTGoNBzjm45r7btUP 2Qw6LFr6WbWCHr+3+smOpAkaKbLbn0Gf9+aymioOEnoV7euwRLtPH281DjkBEi3Am5REwZc2Tcv uBeqY16r+d/qqk8BL9gH1aYM2TcEjM97TOxsPLuApIul2ZcPmvWamSw+OphRnaHHdrUXh9iy18b WWkLxICLVwxMWll9Rm7JUuzjQzCZfa51apuIcPVQ== X-Google-Smtp-Source: AGHT+IHnL8h4iPh68HebIIjK5biqwtKYROqay2GVZXGuZd32fD9IKVY9z2xFvcNNo8rKYOxEx25URQ== X-Received: by 2002:a05:6a21:7781:b0:366:2476:db4a with SMTP id adf61e73a8af0-36624775150mr8292105637.59.1765282440824; Tue, 09 Dec 2025 04:14:00 -0800 (PST) Received: from pengdl-pc.mioffice.cn ([43.224.245.249]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-c0724e9888esm4776a12.14.2025.12.09.04.13.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Dec 2025 04:13:59 -0800 (PST) From: Donglin Peng To: rostedt@goodmis.org Cc: mhiramat@kernel.org, linux-trace-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, pengdonglin , Xiaoqin Zhang Subject: [PATCH v3 1/2] fgraph: Enhance funcgraph-retval with BTF-based type-aware output Date: Tue, 9 Dec 2025 20:13:48 +0800 Message-Id: <20251209121349.525641-2-dolinux.peng@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20251209121349.525641-1-dolinux.peng@gmail.com> References: <20251209121349.525641-1-dolinux.peng@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: pengdonglin The current funcgraph-retval implementation suffers from two accuracy issues: 1. Void-returning functions still print a return value, creating misleading noise in the trace output. 2. For functions returning narrower types (e.g., char, short), the displayed value can be incorrect because high bits of the register may contain undefined data. This patch addresses both problems by leveraging BTF to obtain the exact return type of each traced kernel function. The key changes are: 1. Void function filtering: Functions with void return type no longer display any return value in the trace output, eliminating unnecessary clutter. 2. Type-aware value formatting: The return value is now properly truncated to match the actual width of the return type before being displayed. Additionally, the value is formatted according to its type for better human readability. Here is an output comparison: Before: # perf ftrace -G vfs_read --graph-opts retval ... 1) | touch_atime() { 1) | atime_needs_update() { 1) 0.069 us | make_vfsuid(); /* ret=3D0x0 */ 1) 0.067 us | make_vfsgid(); /* ret=3D0x0 */ 1) | current_time() { 1) 0.197 us | ktime_get_coarse_real_ts64_mg(); /* ret=3D0x187= f886aec3ed6f5 */ 1) 0.352 us | } /* current_time ret=3D0x69380753 */ 1) 0.792 us | } /* atime_needs_update ret=3D0x0 */ 1) 0.937 us | } /* touch_atime ret=3D0x0 */ After: # perf ftrace -G vfs_read --graph-opts retval ... 2) | touch_atime() { 2) | atime_needs_update() { 2) 0.070 us | make_vfsuid(); /* ret=3D0x0 */ 2) 0.070 us | make_vfsgid(); /* ret=3D0x0 */ 2) | current_time() { 2) 0.162 us | ktime_get_coarse_real_ts64_mg(); 2) 0.312 us | } /* current_time ret=3D0x69380649(trunc) */ 2) 0.753 us | } /* atime_needs_update ret=3Dfalse */ 2) 0.899 us | } /* touch_atime */ Cc: Steven Rostedt (Google) Cc: Masami Hiramatsu Cc: Xiaoqin Zhang Signed-off-by: pengdonglin --- kernel/trace/trace_functions_graph.c | 124 ++++++++++++++++++++++++--- 1 file changed, 111 insertions(+), 13 deletions(-) diff --git a/kernel/trace/trace_functions_graph.c b/kernel/trace/trace_func= tions_graph.c index 17c75cf2348e..46b66b1cfc16 100644 --- a/kernel/trace/trace_functions_graph.c +++ b/kernel/trace/trace_functions_graph.c @@ -15,6 +15,7 @@ =20 #include "trace.h" #include "trace_output.h" +#include "trace_btf.h" =20 /* When set, irq functions might be ignored */ static int ftrace_graph_skip_irqs; @@ -120,6 +121,13 @@ enum { FLAGS_FILL_END =3D 3 << TRACE_GRAPH_PRINT_FILL_SHIFT, }; =20 +enum { + RETVAL_FMT_HEX =3D BIT(0), + RETVAL_FMT_DEC =3D BIT(1), + RETVAL_FMT_BOOL =3D BIT(2), + RETVAL_FMT_TRUNC =3D BIT(3), +}; + static void print_graph_duration(struct trace_array *tr, unsigned long long duration, struct trace_seq *s, u32 flags); @@ -865,6 +873,73 @@ static void print_graph_retaddr(struct trace_seq *s, s= truct fgraph_retaddr_ent_e =20 #if defined(CONFIG_FUNCTION_GRAPH_RETVAL) || defined(CONFIG_FUNCTION_GRAPH= _RETADDR) =20 +static void trim_retval(unsigned long func, unsigned long *retval, bool *p= rint_retval, + int *fmt) +{ + const struct btf_type *t; + char name[KSYM_NAME_LEN]; + struct btf *btf; + u32 v, msb; + int kind; + + if (!IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) + return; + + if (lookup_symbol_name(func, name)) + return; + + t =3D btf_find_func_proto(name, &btf); + if (IS_ERR_OR_NULL(t)) + return; + + t =3D btf_type_skip_modifiers(btf, t->type, NULL); + kind =3D t ? BTF_INFO_KIND(t->info) : BTF_KIND_UNKN; + switch (kind) { + case BTF_KIND_UNKN: + *print_retval =3D false; + break; + case BTF_KIND_STRUCT: + case BTF_KIND_UNION: + case BTF_KIND_ENUM: + case BTF_KIND_ENUM64: + if (kind =3D=3D BTF_KIND_STRUCT || kind =3D=3D BTF_KIND_UNION) + *fmt =3D RETVAL_FMT_HEX; + else + *fmt =3D RETVAL_FMT_DEC; + + if (t->size > sizeof(unsigned long)) { + *fmt |=3D RETVAL_FMT_TRUNC; + } else { + msb =3D BITS_PER_BYTE * t->size - 1; + *retval &=3D GENMASK(msb, 0); + } + break; + case BTF_KIND_INT: + v =3D *(u32 *)(t + 1); + if (BTF_INT_ENCODING(v) =3D=3D BTF_INT_BOOL) { + *fmt =3D RETVAL_FMT_BOOL; + msb =3D 0; + } else { + if (BTF_INT_ENCODING(v) =3D=3D BTF_INT_SIGNED) + *fmt =3D RETVAL_FMT_DEC; + else + *fmt =3D RETVAL_FMT_HEX; + + if (t->size > sizeof(unsigned long)) { + *fmt |=3D RETVAL_FMT_TRUNC; + msb =3D BITS_PER_LONG - 1; + } else { + msb =3D BTF_INT_BITS(v) - 1; + } + } + *retval &=3D GENMASK(msb, 0); + break; + default: + *fmt =3D RETVAL_FMT_HEX; + break; + } +} + static void print_graph_retval(struct trace_seq *s, struct ftrace_graph_en= t_entry *entry, struct ftrace_graph_ret *graph_ret, void *func, u32 opt_flags, u32 trace_flags, int args_size) @@ -873,7 +948,7 @@ static void print_graph_retval(struct trace_seq *s, str= uct ftrace_graph_ent_entr unsigned long retval =3D 0; bool print_retaddr =3D false; bool print_retval =3D false; - bool hex_format =3D !!(opt_flags & TRACE_GRAPH_PRINT_RETVAL_HEX); + int retval_fmt =3D 0; =20 #ifdef CONFIG_FUNCTION_GRAPH_RETVAL retval =3D graph_ret->retval; @@ -884,17 +959,35 @@ static void print_graph_retval(struct trace_seq *s, s= truct ftrace_graph_ent_entr print_retaddr =3D !!(opt_flags & TRACE_GRAPH_PRINT_RETADDR); #endif =20 - if (print_retval && retval && !hex_format) { - /* Check if the return value matches the negative format */ - if (IS_ENABLED(CONFIG_64BIT) && (retval & BIT(31)) && - (((u64)retval) >> 32) =3D=3D 0) { - err_code =3D sign_extend64(retval, 31); - } else { - err_code =3D retval; + if (print_retval) { + int fmt =3D RETVAL_FMT_HEX; + + trim_retval((unsigned long)func, &retval, &print_retval, &fmt); + if (print_retval) { + if (opt_flags & TRACE_GRAPH_PRINT_RETVAL_HEX) + retval_fmt =3D RETVAL_FMT_HEX; + + if (retval && retval_fmt !=3D RETVAL_FMT_HEX) { + /* Check if the return value matches the negative format */ + if (IS_ENABLED(CONFIG_64BIT) && (retval & BIT(31)) && + (((u64)retval) >> 32) =3D=3D 0) { + err_code =3D sign_extend64(retval, 31); + } else { + err_code =3D retval; + } + + if (!IS_ERR_VALUE(err_code)) + err_code =3D 0; + } + + if (retval_fmt =3D=3D RETVAL_FMT_HEX) { + retval_fmt |=3D (fmt & RETVAL_FMT_TRUNC); + } else { + if (err_code && fmt & RETVAL_FMT_HEX) + fmt =3D (fmt & ~RETVAL_FMT_HEX) | RETVAL_FMT_DEC; + retval_fmt =3D fmt; + } } - - if (!IS_ERR_VALUE(err_code)) - err_code =3D 0; } =20 if (entry) { @@ -921,10 +1014,15 @@ static void print_graph_retval(struct trace_seq *s, = struct ftrace_graph_ent_entr trace_flags, false); =20 if (print_retval) { - if (hex_format || (err_code =3D=3D 0)) + if (retval_fmt & RETVAL_FMT_HEX) trace_seq_printf(s, " ret=3D0x%lx", retval); + else if (retval_fmt & RETVAL_FMT_BOOL) + trace_seq_printf(s, " ret=3D%s", retval ? "true" : "false"); else - trace_seq_printf(s, " ret=3D%ld", err_code); + trace_seq_printf(s, " ret=3D%ld", err_code ?: retval); + + if (retval_fmt & RETVAL_FMT_TRUNC) + trace_seq_printf(s, "(trunc)"); } =20 if (!entry || print_retval || print_retaddr) --=20 2.34.1 From nobody Fri Dec 19 09:43:17 2025 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A506232145B for ; Tue, 9 Dec 2025 12:14:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765282446; cv=none; b=CsUEvLAhvd3Z7S0BfTpA0QeCkMCupK6t3M6mtQxOpO8IgLCGxS71zmrdE2c+339lEyG2spQYAJK704Gw3zVouKNWIViuVXRkSAlIJ8zCDysh1yP7xtYA1PlLg+0RbRR8bOh3Mbi61C+eF2dq6psntBmQ5TahCNO5n15GBO/8tJ0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765282446; c=relaxed/simple; bh=BWyN4D8XB3EHVKIANdR3jOmuIyDp9zYjYnorcbrtitk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=bliIil8StJlS72N0vTlEg0Kw9rqAYUAhFcJC8BO8ZCdH+1I40RkjxT8lzY178/5NqHw34Np9D1+UtAqDvyvquoDqq8cHguVptDhVavuoib0Nzyw/Rpp5cgoModCseGyCEJQSzHAdvH/ouHgIF9Cz/WT0TIEGcgwgUwxTGhqt/y8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=hRyJQyJ1; arc=none smtp.client-ip=209.85.210.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="hRyJQyJ1" Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-7e1651ae0d5so4176584b3a.1 for ; Tue, 09 Dec 2025 04:14:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1765282444; x=1765887244; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=T7QTKaMq3p7fYjtMRSt1RZ9qB0q4/IC5ZQr51AGxCMI=; b=hRyJQyJ17mmkinprebALjEOLEmY/AJLILYAbB2HCw/bi5PhiUKP14HfFmaEBMLX+j3 6tZOMJm+0jF7+z9MNR/6st8slLCOhLsqqm7RFfodLF1CxGY23uQx76YnuBHt8DpI2aZR h/8ISaNFhvNNBY1DnDi5rjZASeptiTzxiFV535QOF1BWznZWObqVGgLfdkOs7NtnEi7j COMJvWSLy4/pCapEN8Ikgeo1OCZoOFWjU/QaAfx4uIFYNxOn/hw8Gj45yBfIVvl/aKM0 f7mlFrfXBu0awXVwwRsqQT2PDWA5/9x/zuL8K2x/TuN115pNDK1P+YxHTqyqW/55HwDF UGuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765282444; x=1765887244; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=T7QTKaMq3p7fYjtMRSt1RZ9qB0q4/IC5ZQr51AGxCMI=; b=J9eOhmZp/Pjf8nXRivcAmtpaSBSoJbkEVguFezYU6jAJCq/kBjDAzLFAEOQANIVXAE aVd7+fcLQKCXhipeohi3nSstXNJlI85xLlaL1D8uVfDzYve25Xn4+3FXaL1uZViFhx1/ HXEWRlH4ckJtFMXWheCqKj+Oxw2BaiU3gXQ9y8TjjWqH1UxurMoEwmB+CEYDiIW12XJG CMBkWjxvkr8waQyn6rqq8KC+f7g3hT+ETFxvCVbPr56n+fETqiWIuFS+2ZdJsVpPqscY A1UKa3O5rXBjBp5qyachO++/Zpnf4LE7hjkeH8DMtGkjoNH1DNx8WvCoC80e9SiOlkTB qPqg== X-Forwarded-Encrypted: i=1; AJvYcCW5WkyMLfSF/zfyMU0X/pulKlOAtJ/2uQ1g5oJ7eSyxhq35IoL4CO3E/hQrJAFydFbbbrN3uDyZ+bAvayI=@vger.kernel.org X-Gm-Message-State: AOJu0YzcPnSEC0X6HKvbJLK/R0xhuCxsE07WfyXhnm1L3PDN7pyChHVr BUaUu8cLyPCZG768xuzHWKhj5/R8xItUhJdiHiuuSn0GqOTl2lfl/E6bJtSD5Ui53DY= X-Gm-Gg: ASbGncskDgc1rwYvpqdDkuFUHsoH+H+F9B3UfCwhEPt4ldIIOBen9lC+jANTvfSKjrk KoryYzsd0cNhzLnsjt6fos+4s28S9XxOj2Wb2VHg9V4E3xXVjx2W0F9TprQ5rdWVyo4fRCA9nqB OAhSHzuIQZpEbtA810akF1f2BbwCA6L87ye0hiqRMbv/VkXUOlcCwK07FgASKdAHDsnR1oRAc/h SYOhevNhYXYbS/bWkW4Awe/WIcdv1As4YCivTaxlzEczst9w3SNBvH/f+6rMmxe9e/ainlM0X4U 2HBRCPfLGcsRcGcgO8aaV1FXIy8IE24XUKdoWqBUoVDis052xuZaY3+gZJCt+WxkdiGvEAHnIQb 3hd7AVSqSq+mQEk3wCVvGYEoWNUuhMV7IXYpffax47n99R26S+qiFdUsL4qYMfLqPAqYxOgeP4i Jg6LXSaBed/qXwMfIPiN6XVjw9gs0= X-Google-Smtp-Source: AGHT+IGX+H47LvTOZvJY2p32Lu/R40fOKg5MRKHIVtyz/ONyDGGZ7YAR8vn/V+zevzmjFEIZ6YxBfg== X-Received: by 2002:a05:6a20:4310:b0:35e:6c3:c8de with SMTP id adf61e73a8af0-36617e8d07bmr11175660637.34.1765282443851; Tue, 09 Dec 2025 04:14:03 -0800 (PST) Received: from pengdl-pc.mioffice.cn ([43.224.245.249]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-c0724e9888esm4776a12.14.2025.12.09.04.14.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Dec 2025 04:14:02 -0800 (PST) From: Donglin Peng To: rostedt@goodmis.org Cc: mhiramat@kernel.org, linux-trace-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, pengdonglin , Xiaoqin Zhang Subject: [PATCH v3 2/2] tracing: Update funcgraph-retval documentation Date: Tue, 9 Dec 2025 20:13:49 +0800 Message-Id: <20251209121349.525641-3-dolinux.peng@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20251209121349.525641-1-dolinux.peng@gmail.com> References: <20251209121349.525641-1-dolinux.peng@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: pengdonglin The existing documentation for funcgraph-retval is outdated and partially incorrect, as it describes limitations that have now been resolved. Recent changes (e.g., using BTF to obtain function return types) have addressed key issues: 1. Return values are now printed only for non-void functions. 2. Values are trimmed to the correct width of the return type, avoiding garbage data from high bits. Cc: Steven Rostedt (Google) Cc: Masami Hiramatsu Cc: Xiaoqin Zhang Signed-off-by: pengdonglin --- Documentation/trace/ftrace.rst | 78 ++++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 33 deletions(-) diff --git a/Documentation/trace/ftrace.rst b/Documentation/trace/ftrace.rst index d1f313a5f4ad..03c8c433c803 100644 --- a/Documentation/trace/ftrace.rst +++ b/Documentation/trace/ftrace.rst @@ -1454,6 +1454,10 @@ Options for function_graph tracer: printed in hexadecimal format. By default, this option is off. =20 + funcgraph-retaddr + When set, the return address will always be printed. + By default, this option is off. + sleep-time When running function graph tracer, to include the time a task schedules out in its function. @@ -2800,7 +2804,7 @@ It is default disabled. 0) 2.861 us | } /* putname() */ =20 The return value of each traced function can be displayed after -an equal sign "=3D". When encountering system call failures, it +an equal sign "ret =3D". When encountering system call failures, it can be very helpful to quickly locate the function that first returns an error code. =20 @@ -2810,16 +2814,16 @@ returns an error code. Example with funcgraph-retval:: =20 1) | cgroup_migrate() { - 1) 0.651 us | cgroup_migrate_add_task(); /* =3D 0xffff93fcfd= 346c00 */ + 1) 0.651 us | cgroup_migrate_add_task(); /* ret=3D0xffff93fc= fd346c00 */ 1) | cgroup_migrate_execute() { 1) | cpu_cgroup_can_attach() { 1) | cgroup_taskset_first() { - 1) 0.732 us | cgroup_taskset_next(); /* =3D 0xffff93fc= 8fb20000 */ - 1) 1.232 us | } /* cgroup_taskset_first =3D 0xffff93fc8f= b20000 */ - 1) 0.380 us | sched_rt_can_attach(); /* =3D 0x0 */ - 1) 2.335 us | } /* cpu_cgroup_can_attach =3D -22 */ - 1) 4.369 us | } /* cgroup_migrate_execute =3D -22 */ - 1) 7.143 us | } /* cgroup_migrate =3D -22 */ + 1) 0.732 us | cgroup_taskset_next(); /* ret=3D0xffff93= fc8fb20000 */ + 1) 1.232 us | } /* cgroup_taskset_first ret=3D0xffff93fc= 8fb20000 */ + 1) 0.380 us | sched_rt_can_attach(); /* ret=3D0x0 */ + 1) 2.335 us | } /* cpu_cgroup_can_attach ret=3D-22 */ + 1) 4.369 us | } /* cgroup_migrate_execute ret=3D-22 */ + 1) 7.143 us | } /* cgroup_migrate ret=3D-22 */ =20 The above example shows that the function cpu_cgroup_can_attach returned the error code -22 firstly, then we can read the code @@ -2836,37 +2840,41 @@ printed in hexadecimal format. Example with funcgraph-retval-hex:: =20 1) | cgroup_migrate() { - 1) 0.651 us | cgroup_migrate_add_task(); /* =3D 0xffff93fc= fd346c00 */ + 1) 0.651 us | cgroup_migrate_add_task(); /* ret=3D0xffff93= fcfd346c00 */ 1) | cgroup_migrate_execute() { 1) | cpu_cgroup_can_attach() { 1) | cgroup_taskset_first() { - 1) 0.732 us | cgroup_taskset_next(); /* =3D 0xffff93= fc8fb20000 */ - 1) 1.232 us | } /* cgroup_taskset_first =3D 0xffff93fc= 8fb20000 */ - 1) 0.380 us | sched_rt_can_attach(); /* =3D 0x0 */ - 1) 2.335 us | } /* cpu_cgroup_can_attach =3D 0xffffffea = */ - 1) 4.369 us | } /* cgroup_migrate_execute =3D 0xffffffea */ + 1) 0.732 us | cgroup_taskset_next(); /* ret=3D0xffff= 93fc8fb20000 */ + 1) 1.232 us | } /* cgroup_taskset_first ret=3D0xffff93= fc8fb20000 */ + 1) 0.380 us | sched_rt_can_attach(); /* ret=3D0x0 */ + 1) 2.335 us | } /* cpu_cgroup_can_attach ret=3D0xffffffe= a */ + 1) 4.369 us | } /* cgroup_migrate_execute ret=3D0xffffffea= */ 1) 7.143 us | } /* cgroup_migrate =3D 0xffffffea */ =20 -At present, there are some limitations when using the funcgraph-retval -option, and these limitations will be eliminated in the future: +Note that there are some limitations when using the funcgraph-retval +option: + +- If CONFIG_DEBUG_INFO_BTF is disabled (n), a return value is printed even= for + functions with a void return type. When CONFIG_DEBUG_INFO_BTF is enabled= (y), + the return value is printed only for non-void functions. =20 -- Even if the function return type is void, a return value will still - be printed, and you can just ignore it. +- If a return value occupies multiple registers, only the value in the fir= st + register is recorded and printed. For example, on the x86 architecture, a + 64-bit return value is stored across eax (lower 32 bits) and edx (upper = 32 bits), + but only the contents of eax are captured. If CONFIG_DEBUG_INFO_BTF is e= nabled, + the suffix "(trunc)" is appended to the printed value to indicate that t= he + output may be truncated because high-order register contents are omitted. =20 -- Even if return values are stored in multiple registers, only the - value contained in the first register will be recorded and printed. - To illustrate, in the x86 architecture, eax and edx are used to store - a 64-bit return value, with the lower 32 bits saved in eax and the - upper 32 bits saved in edx. However, only the value stored in eax - will be recorded and printed. +- Under certain procedure-call standards (e.g., arm64's AAPCS64), when the= return + type is smaller than a general-purpose register (GPR), the caller is res= ponsible + for narrowing the value; the upper bits of the register may contain unde= fined data. + For instance, when a u8 is returned in 64-bit GPR, bits [63:8] can hold = arbitrary + values, especially when larger types are truncated (explicitly or implic= itly). It + is therefore advisable to inspect the code in such cases. If CONFIG_DEBU= G_INFO_BTF + is enabled (y), the return value is automatically trimmed to the width o= f the return + type. =20 -- In certain procedure call standards, such as arm64's AAPCS64, when a - type is smaller than a GPR, it is the responsibility of the consumer - to perform the narrowing, and the upper bits may contain UNKNOWN values. - Therefore, it is advisable to check the code for such cases. For instanc= e, - when using a u8 in a 64-bit GPR, bits [63:8] may contain arbitrary value= s, - especially when larger types are truncated, whether explicitly or implic= itly. - Here are some specific cases to illustrate this point: + The following examples illustrate the behavior: =20 **Case One**: =20 @@ -2885,7 +2893,9 @@ option, and these limitations will be eliminated in t= he future: RET =20 If you pass 0x123456789abcdef to this function and want to narrow it, - it may be recorded as 0x123456789abcdef instead of 0xef. + it may be recorded as 0x123456789abcdef instead of 0xef. When + CONFIG_DEBUG_INFO_BTF is enabled, the value will be correctly truncated + to 0xef based on the size constraints of the u8 type. =20 **Case Two**: =20 @@ -2910,7 +2920,9 @@ option, and these limitations will be eliminated in t= he future: RET =20 When passing 0x2_0000_0000 to it, the return value may be recorded as - 0x2_0000_0000 instead of 0. + 0x2_0000_0000 instead of 0. When CONFIG_DEBUG_INFO_BTF is enabled, the + value will be correctly truncated to 0 based on the size constraints of + the int type. =20 You can put some comments on specific functions by using trace_printk() For example, if you want to put a comment inside --=20 2.34.1