[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20251113164917.2563486-15-alexandre.chartre@oracle.com>
Date: Thu, 13 Nov 2025 17:49:03 +0100
From: Alexandre Chartre <alexandre.chartre@...cle.com>
To: linux-kernel@...r.kernel.org, mingo@...nel.org, jpoimboe@...nel.org,
peterz@...radead.org
Cc: alexandre.chartre@...cle.com
Subject: [PATCH v4 14/28] objtool: Improve tracing of alternative instructions
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@...cle.com>
---
tools/objtool/check.c | 18 +++-----
tools/objtool/disas.c | 34 +++++++++++++++
tools/objtool/include/objtool/disas.h | 6 +++
tools/objtool/include/objtool/trace.h | 61 +++++++++++++++++++++++++++
tools/objtool/trace.c | 55 ++++++++++++++++++++++++
5 files changed, 162 insertions(+), 12 deletions(-)
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 93268b7c015e3..9321486e6265e 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -3527,7 +3527,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;
}
@@ -3561,8 +3561,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;
@@ -3649,24 +3650,17 @@ 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 orig");
+ TRACE_ALT_INFO_NOADDR(insn, "/ ", "DEFAULT");
}
if (skip_alt_group(insn))
diff --git a/tools/objtool/disas.c b/tools/objtool/disas.c
index 181e4ce975d36..058e2053c31a7 100644
--- a/tools/objtool/disas.c
+++ b/tools/objtool/disas.c
@@ -3,6 +3,8 @@
* Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@...hat.com>
*/
+#define _GNU_SOURCE
+
#include <objtool/arch.h>
#include <objtool/check.h>
#include <objtool/disas.h>
@@ -427,6 +429,38 @@ size_t disas_insn(struct disas_context *dctx, struct instruction *insn)
return disasm(insn->offset, &dctx->info);
}
+/*
+ * Provide a name for an alternative.
+ */
+char *disas_alt_name(struct alternative *alt)
+{
+ char *str = NULL;
+
+ switch (alt->type) {
+
+ case ALT_TYPE_EX_TABLE:
+ str = strdup("EXCEPTION");
+ break;
+
+ case ALT_TYPE_JUMP_TABLE:
+ str = strdup("JUMP");
+ break;
+
+ case ALT_TYPE_INSTRUCTIONS:
+ /*
+ * This is a non-default group alternative. Create a unique
+ * name using the offset of the first original and alternative
+ * instructions.
+ */
+ asprintf(&str, "ALTERNATIVE %lx.%lx",
+ alt->insn->alt_group->orig_group->first_insn->offset,
+ alt->insn->alt_group->first_insn->offset);
+ break;
+ }
+
+ return str;
+}
+
/*
* Disassemble a function.
*/
diff --git a/tools/objtool/include/objtool/disas.h b/tools/objtool/include/objtool/disas.h
index 5db75d06f2197..f1bef2a6003e8 100644
--- a/tools/objtool/include/objtool/disas.h
+++ b/tools/objtool/include/objtool/disas.h
@@ -6,6 +6,7 @@
#ifndef _DISAS_H
#define _DISAS_H
+struct alternative;
struct disas_context;
struct disassemble_info;
@@ -24,6 +25,7 @@ void disas_print_info(FILE *stream, struct instruction *insn, int depth,
void disas_print_insn(FILE *stream, struct disas_context *dctx,
struct instruction *insn, int depth,
const char *format, ...);
+char *disas_alt_name(struct alternative *alt);
#else /* DISAS */
@@ -61,6 +63,10 @@ static inline void disas_print_info(FILE *stream, struct instruction *insn,
static inline void disas_print_insn(FILE *stream, struct disas_context *dctx,
struct instruction *insn, int depth,
const char *format, ...) {}
+static inline char *disas_alt_name(struct alternative *alt)
+{
+ return NULL;
+}
#endif /* DISAS */
diff --git a/tools/objtool/include/objtool/trace.h b/tools/objtool/include/objtool/trace.h
index 5b8abdb9b09fb..9a608523b6b08 100644
--- a/tools/objtool/include/objtool/trace.h
+++ b/tools/objtool/include/objtool/trace.h
@@ -19,6 +19,21 @@ 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) { \
@@ -36,6 +51,36 @@ extern int trace_depth;
trace_insn_state(insn, sprev, snext); \
})
+#define TRACE_ALT_FMT(pfx, fmt) pfx "<alternative.%lx> " fmt
+
+#define TRACE_ALT(insn, fmt, ...) \
+ TRACE_INSN(insn, TRACE_ALT_FMT("", fmt), \
+ (insn)->offset, ##__VA_ARGS__)
+
+#define TRACE_ALT_INFO(insn, pfx, fmt, ...) \
+ TRACE_ADDR(insn, TRACE_ALT_FMT(pfx, fmt), \
+ (insn)->offset, ##__VA_ARGS__)
+
+#define TRACE_ALT_INFO_NOADDR(insn, pfx, fmt, ...) \
+ TRACE_ADDR(NULL, TRACE_ALT_FMT(pfx, fmt), \
+ (insn)->offset, ##__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 +106,33 @@ 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 ef9250d4646bb..1c3d961b5123a 100644
--- a/tools/objtool/trace.c
+++ b/tools/objtool/trace.c
@@ -147,3 +147,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 end", alt_name);
+}
--
2.43.5
Powered by blists - more mailing lists