mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Alexandre Chartre <alexandre.chartre@oracle.com>
To: linux-kernel@vger.kernel.org, mingo@kernel.org,
	jpoimboe@kernel.org, peterz@infradead.org,
	david.laight.linux@gmail.com
Cc: alexandre.chartre@oracle.com
Subject: [PATCH v5 15/30] objtool: Improve tracing of alternative instructions
Date: Wed, 19 Nov 2025 15:32:32 +0100	[thread overview]
Message-ID: <20251119143247.3944213-16-alexandre.chartre@oracle.com> (raw)
In-Reply-To: <20251119143247.3944213-1-alexandre.chartre@oracle.com>

When tracing function validation, improve the reporting of
alternative instruction by more clearly showing the different
alternatives beginning and end.

Signed-off-by: Alexandre Chartre <alexandre.chartre@oracle.com>
---
 tools/objtool/check.c                 | 18 +++-----
 tools/objtool/include/objtool/trace.h | 65 ++++++++++++++++++++++++++-
 tools/objtool/trace.c                 | 55 +++++++++++++++++++++++
 3 files changed, 125 insertions(+), 13 deletions(-)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 20630df83e85c..fb000923718dc 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -3616,7 +3616,7 @@ static bool skip_alt_group(struct instruction *insn)
 
 	/* ANNOTATE_IGNORE_ALTERNATIVE */
 	if (insn->alt_group->ignore) {
-		TRACE_INSN(insn, "alt group ignored");
+		TRACE_ALT(insn, "alt group ignored");
 		return true;
 	}
 
@@ -3732,8 +3732,9 @@ static int validate_insn(struct objtool_file *file, struct symbol *func,
 			 struct instruction *prev_insn, struct instruction *next_insn,
 			 bool *dead_end)
 {
-	/* prev_state is not used if there is no disassembly support */
+	/* prev_state and alt_name are not used if there is no disassembly support */
 	struct insn_state prev_state __maybe_unused;
+	char *alt_name __maybe_unused = NULL;
 	struct alternative *alt;
 	u8 visited;
 	int ret;
@@ -3820,23 +3821,16 @@ static int validate_insn(struct objtool_file *file, struct symbol *func,
 		return 1;
 
 	if (insn->alts) {
-		int i, num_alts;
-
-		num_alts = 0;
-		for (alt = insn->alts; alt; alt = alt->next)
-			num_alts++;
-
-		i = 1;
 		for (alt = insn->alts; alt; alt = alt->next) {
-			TRACE_INSN(insn, "alternative %d/%d", i, num_alts);
+			TRACE_ALT_BEGIN(insn, alt, alt_name);
 			ret = validate_branch(file, func, alt->insn, *statep);
+			TRACE_ALT_END(insn, alt, alt_name);
 			if (ret) {
 				BT_INSN(insn, "(alt)");
 				return ret;
 			}
-			i++;
 		}
-		TRACE_INSN(insn, "alternative DEFAULT");
+		TRACE_ALT_INFO_NOADDR(insn, "/ ", "DEFAULT");
 	}
 
 	if (skip_alt_group(insn))
diff --git a/tools/objtool/include/objtool/trace.h b/tools/objtool/include/objtool/trace.h
index 33fe9c6acb4fd..70b574366797b 100644
--- a/tools/objtool/include/objtool/trace.h
+++ b/tools/objtool/include/objtool/trace.h
@@ -19,11 +19,26 @@ extern int trace_depth;
 		fprintf(stderr, fmt, ##__VA_ARGS__);		\
 })
 
+/*
+ * Print the instruction address and a message. The instruction
+ * itself is not printed.
+ */
+#define TRACE_ADDR(insn, fmt, ...)				\
+({								\
+	if (trace) {						\
+		disas_print_info(stderr, insn, trace_depth - 1, \
+				 fmt "\n", ##__VA_ARGS__);	\
+	}							\
+})
+
+/*
+ * Print the instruction address, the instruction and a message.
+ */
 #define TRACE_INSN(insn, fmt, ...)				\
 ({								\
 	if (trace) {						\
 		disas_print_insn(stderr, objtool_disas_ctx,	\
-				 insn, trace_depth - 1,	\
+				 insn, trace_depth - 1,		\
 				 fmt, ##__VA_ARGS__);		\
 		fprintf(stderr, "\n");				\
 		insn->trace = 1;				\
@@ -36,6 +51,37 @@ extern int trace_depth;
 		trace_insn_state(insn, sprev, snext);		\
 })
 
+#define TRACE_ALT_FMT(pfx, fmt) pfx "<%s.%lx> " fmt
+#define TRACE_ALT_ARG(insn) disas_alt_type_name(insn), (insn)->offset
+
+#define TRACE_ALT(insn, fmt, ...)				\
+	TRACE_INSN(insn, TRACE_ALT_FMT("", fmt),		\
+		   TRACE_ALT_ARG(insn), ##__VA_ARGS__)
+
+#define TRACE_ALT_INFO(insn, pfx, fmt, ...)			\
+	TRACE_ADDR(insn, TRACE_ALT_FMT(pfx, fmt),		\
+		   TRACE_ALT_ARG(insn), ##__VA_ARGS__)
+
+#define TRACE_ALT_INFO_NOADDR(insn, pfx, fmt, ...)		\
+	TRACE_ADDR(NULL, TRACE_ALT_FMT(pfx, fmt),		\
+		   TRACE_ALT_ARG(insn), ##__VA_ARGS__)
+
+#define TRACE_ALT_BEGIN(insn, alt, alt_name)			\
+({								\
+	if (trace) {						\
+		alt_name = disas_alt_name(alt);			\
+		trace_alt_begin(insn, alt, alt_name);		\
+	}							\
+})
+
+#define TRACE_ALT_END(insn, alt, alt_name)			\
+({								\
+	if (trace) {						\
+		trace_alt_end(insn, alt, alt_name);		\
+		free(alt_name);					\
+	}							\
+})
+
 static inline void trace_enable(void)
 {
 	trace = true;
@@ -61,17 +107,34 @@ static inline void trace_depth_dec(void)
 
 void trace_insn_state(struct instruction *insn, struct insn_state *sprev,
 		      struct insn_state *snext);
+void trace_alt_begin(struct instruction *orig_insn, struct alternative *alt,
+		     char *alt_name);
+void trace_alt_end(struct instruction *orig_insn, struct alternative *alt,
+		   char *alt_name);
 
 #else /* DISAS */
 
 #define TRACE(fmt, ...) ({})
+#define TRACE_ADDR(insn, fmt, ...) ({})
 #define TRACE_INSN(insn, fmt, ...) ({})
 #define TRACE_INSN_STATE(insn, sprev, snext) ({})
+#define TRACE_ALT(insn, fmt, ...) ({})
+#define TRACE_ALT_INFO(insn, fmt, ...) ({})
+#define TRACE_ALT_INFO_NOADDR(insn, fmt, ...) ({})
+#define TRACE_ALT_BEGIN(insn, alt, alt_name) ({})
+#define TRACE_ALT_END(insn, alt, alt_name) ({})
+
 
 static inline void trace_enable(void) {}
 static inline void trace_disable(void) {}
 static inline void trace_depth_inc(void) {}
 static inline void trace_depth_dec(void) {}
+static inline void trace_alt_begin(struct instruction *orig_insn,
+				   struct alternative *alt,
+				   char *alt_name) {};
+static inline void trace_alt_end(struct instruction *orig_insn,
+				 struct alternative *alt,
+				 char *alt_name) {};
 
 #endif
 
diff --git a/tools/objtool/trace.c b/tools/objtool/trace.c
index d70d47081e82d..5dec44dab781c 100644
--- a/tools/objtool/trace.c
+++ b/tools/objtool/trace.c
@@ -146,3 +146,58 @@ void trace_insn_state(struct instruction *insn, struct insn_state *sprev,
 
 	insn->trace = 1;
 }
+
+void trace_alt_begin(struct instruction *orig_insn, struct alternative *alt,
+		     char *alt_name)
+{
+	struct instruction *alt_insn;
+	char suffix[2];
+
+	alt_insn = alt->insn;
+
+	if (alt->type == ALT_TYPE_EX_TABLE) {
+		/*
+		 * When there is an exception table then the instruction
+		 * at the original location is executed but it can cause
+		 * an exception. In that case, the execution will be
+		 * redirected to the alternative instruction.
+		 *
+		 * The instruction at the original location can have
+		 * instruction alternatives, so we just print the location
+		 * of the instruction that can cause the exception and
+		 * not the instruction itself.
+		 */
+		TRACE_ALT_INFO_NOADDR(orig_insn, "/ ", "%s for instruction at 0x%lx <%s+0x%lx>",
+				      alt_name,
+				      orig_insn->offset, orig_insn->sym->name,
+				      orig_insn->offset - orig_insn->sym->offset);
+	} else {
+		TRACE_ALT_INFO_NOADDR(orig_insn, "/ ", "%s", alt_name);
+	}
+
+	if (alt->type == ALT_TYPE_JUMP_TABLE) {
+		/*
+		 * For a jump alternative, if the default instruction is
+		 * a NOP then it is replaced with the jmp instruction,
+		 * otherwise it is replaced with a NOP instruction.
+		 */
+		trace_depth++;
+		if (orig_insn->type == INSN_NOP) {
+			suffix[0] = (orig_insn->len == 5) ? 'q' : '\0';
+			TRACE_ADDR(orig_insn, "jmp%-3s %lx <%s+0x%lx>", suffix,
+				   alt_insn->offset, alt_insn->sym->name,
+				   alt_insn->offset - alt_insn->sym->offset);
+		} else {
+			TRACE_ADDR(orig_insn, "nop%d", orig_insn->len);
+			trace_depth--;
+		}
+	}
+}
+
+void trace_alt_end(struct instruction *orig_insn, struct alternative *alt,
+		   char *alt_name)
+{
+	if (alt->type == ALT_TYPE_JUMP_TABLE && orig_insn->type == INSN_NOP)
+		trace_depth--;
+	TRACE_ALT_INFO_NOADDR(orig_insn, "\\ ", "%s", alt_name);
+}
-- 
2.43.5


  parent reply	other threads:[~2025-11-19 14:34 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-19 14:32 [PATCH v5 00/30] objtool: Function validation tracing Alexandre Chartre
2025-11-19 14:32 ` [PATCH v5 01/30] objtool: Move disassembly functions to a separated file Alexandre Chartre
2025-11-19 14:32 ` [PATCH v5 02/30] objtool: Create disassembly context Alexandre Chartre
2025-11-19 14:32 ` [PATCH v5 03/30] objtool: Disassemble code with libopcodes instead of running objdump Alexandre Chartre
2025-11-19 14:32 ` [PATCH v5 04/30] tool build: Remove annoying newline in build output Alexandre Chartre
2025-11-19 14:32 ` [PATCH v5 05/30] objtool: Print symbol during disassembly Alexandre Chartre
2025-11-19 14:32 ` [PATCH v5 06/30] objtool: Store instruction disassembly result Alexandre Chartre
2025-11-19 14:32 ` [PATCH v5 07/30] objtool: Disassemble instruction on warning or backtrace Alexandre Chartre
2025-11-19 14:32 ` [PATCH v5 08/30] objtool: Extract code to validate instruction from the validate branch loop Alexandre Chartre
2025-11-19 14:32 ` [PATCH v5 09/30] objtool: Record symbol name max length Alexandre Chartre
2025-11-19 14:32 ` [PATCH v5 10/30] objtool: Add option to trace function validation Alexandre Chartre
2025-11-19 14:32 ` [PATCH v5 11/30] objtool: Trace instruction state changes during " Alexandre Chartre
2025-11-19 14:32 ` [PATCH v5 12/30] objtool: Improve register reporting " Alexandre Chartre
2025-11-19 14:32 ` [PATCH v5 13/30] objtool: Identify the different types of alternatives Alexandre Chartre
2025-11-19 14:32 ` [PATCH v5 14/30] objtool: Add functions to better name alternatives Alexandre Chartre
2025-11-19 14:32 ` Alexandre Chartre [this message]
2025-11-19 14:32 ` [PATCH v5 16/30] objtool: Do not validate IBT for .return_sites and .call_sites Alexandre Chartre
2025-11-19 14:32 ` [PATCH v5 17/30] objtool: Add the --disas=<function-pattern> action Alexandre Chartre
2025-11-19 14:32 ` [PATCH v5 18/30] objtool: Preserve alternatives order Alexandre Chartre
2025-11-19 14:32 ` [PATCH v5 19/30] objtool: Print headers for alternatives Alexandre Chartre
2025-11-19 14:32 ` [PATCH v5 20/30] objtool: Disassemble group alternatives Alexandre Chartre
2025-11-19 14:32 ` [PATCH v5 21/30] objtool: Print addresses with alternative instructions Alexandre Chartre
2025-11-19 14:32 ` [PATCH v5 22/30] objtool: Disassemble exception table alternatives Alexandre Chartre
2025-11-20 17:28   ` Josh Poimboeuf
2025-11-19 14:32 ` [PATCH v5 23/30] objtool: Disassemble jump " Alexandre Chartre
2025-11-19 14:32 ` [PATCH v5 24/30] objtool: Fix address references in alternatives Alexandre Chartre
2025-11-19 14:32 ` [PATCH v5 25/30] objtool: Provide access to feature and flags of group alternatives Alexandre Chartre
2025-11-19 14:32 ` [PATCH v5 26/30] objtool: Function to get the name of a CPU feature Alexandre Chartre
2025-11-19 14:32 ` [PATCH v5 27/30] objtool: Improve naming of group alternatives Alexandre Chartre
2025-11-19 14:32 ` [PATCH v5 28/30] objtool: Compact output for alternatives with one instruction Alexandre Chartre
2025-11-19 14:32 ` [PATCH v5 29/30] objtool: Add wide output for disassembly Alexandre Chartre
2025-11-19 14:32 ` [PATCH v5 30/30] objtool: Trim trailing NOPs in alternative Alexandre Chartre
2025-11-20 17:30 ` [PATCH v5 00/30] objtool: Function validation tracing Josh Poimboeuf
2025-11-20 17:37   ` Alexandre Chartre
2025-11-21 10:28   ` Peter Zijlstra

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251119143247.3944213-16-alexandre.chartre@oracle.com \
    --to=alexandre.chartre@oracle.com \
    --cc=david.laight.linux@gmail.com \
    --cc=jpoimboe@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®