[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250619145659.1377970-15-alexandre.chartre@oracle.com>
Date: Thu, 19 Jun 2025 16:56:56 +0200
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: [RFC PATCH v2 14/17] 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 | 64 ++++++++++++++++++++++++---
tools/objtool/include/objtool/trace.h | 33 ++++++++++++++
2 files changed, 91 insertions(+), 6 deletions(-)
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 8a51c871e1dc..43c88a5fd58a 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -3517,7 +3517,7 @@ static bool skip_alt_group(struct instruction *insn)
/* ANNOTATE_IGNORE_ALTERNATIVE */
if (insn->alt_group && insn->alt_group->ignore) {
- TRACE_INSN(insn, "alt group ignored");
+ TRACE_ALT(insn, "alt group ignored");
return true;
}
@@ -3641,22 +3641,74 @@ static int validate_insn(struct objtool_file *file, struct symbol *func,
if (insn->alts) {
int i, num_alts;
+ /*
+ * Count the number of alternatives with an alt group.
+ *
+ * For a jump alternative, count is 0 and there is a single
+ * alternative (with no alt group).
+ *
+ * For a group alternative, count is at least 1. In addition
+ * there is an alternative that points to the code following
+ * the alternative.
+ */
num_alts = 0;
- for (alt = insn->alts; alt; alt = alt->next)
- num_alts++;
+ for (alt = insn->alts; alt; alt = alt->next) {
+ if (alt->insn->alt_group)
+ num_alts++;
+ }
i = 1;
for (alt = insn->alts; alt; alt = alt->next) {
- TRACE_INSN(insn, "alternative %d/%d", i, num_alts);
+ if (!num_alts) {
+ /*
+ * For a jump alternative, the non-default
+ * branch is validated first. So if the
+ * default instruction is a NOP then the
+ * branch is validated first (jump taken),
+ * otherwise the branch is not taken. Then
+ * the default alternative is validated.
+ */
+ if (insn->type == INSN_NOP)
+ TRACE_ALT_INFO(insn, "jump taken - begin");
+ else
+ TRACE_ALT_INFO(insn, "jump not taken - begin");
+ } else {
+ /*
+ * For a group alternative, the code after the
+ * alternative (alternative with no alt group)
+ * is validated first. Then each alternative
+ * is validated. Finally the default alternative
+ * is validated.
+ */
+ if (alt->insn->alt_group)
+ TRACE_ALT_INFO(insn, "alt %d/%d - begin", i, num_alts);
+ else
+ TRACE_ALT_INFO(insn, "after alternative - begin");
+ }
+
ret = validate_branch(file, func, alt->insn, *statep);
+
+ if (!num_alts) {
+ if (insn->type == INSN_NOP)
+ TRACE_ALT_INFO(insn, "jump taken - end");
+ else
+ TRACE_ALT_INFO(insn, "jump not taken - end");
+ } else {
+ if (alt->insn->alt_group)
+ TRACE_ALT_INFO_NOADDR(insn, "alt %d/%d - end", i, num_alts);
+ else
+ TRACE_ALT_INFO_NOADDR(insn, "after alternative - end");
+ }
+
if (ret) {
BT_INSN(insn, "(alt)");
return ret;
}
- i++;
+ if (alt->insn->alt_group)
+ i++;
}
- TRACE_INSN(insn, "alternative orig");
+ TRACE_ALT_INFO(insn, "default");
}
if (skip_alt_group(insn))
diff --git a/tools/objtool/include/objtool/trace.h b/tools/objtool/include/objtool/trace.h
index 5b8abdb9b09f..da3d41d6dedd 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,20 @@ extern int trace_depth;
trace_insn_state(insn, sprev, snext); \
})
+#define TRACE_ALT_FMT(fmt) "<alternative.%lx> " fmt
+
+#define TRACE_ALT(insn, fmt, ...) \
+ TRACE_INSN(insn, TRACE_ALT_FMT(fmt), \
+ (insn)->offset, ##__VA_ARGS__)
+
+#define TRACE_ALT_INFO(insn, fmt, ...) \
+ TRACE_ADDR(insn, TRACE_ALT_FMT(fmt), \
+ (insn)->offset, ##__VA_ARGS__)
+
+#define TRACE_ALT_INFO_NOADDR(insn, fmt, ...) \
+ TRACE_ADDR(NULL, TRACE_ALT_FMT(fmt), \
+ (insn)->offset, ##__VA_ARGS__)
+
static inline void trace_enable(void)
{
trace = true;
@@ -65,8 +94,12 @@ void trace_insn_state(struct instruction *insn, struct insn_state *sprev,
#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, ...)
static inline void trace_enable(void) {}
static inline void trace_disable(void) {}
--
2.43.5
Powered by blists - more mailing lists