From: Alexandre Chartre <alexandre.chartre@oracle.com>
To: linux-kernel@vger.kernel.org, mingo@kernel.org,
jpoimboe@kernel.org, peterz@infradead.org
Cc: alexandre.chartre@oracle.com
Subject: [RFC 10/13] objtool: Trace instruction state changes during function validation
Date: Fri, 6 Jun 2025 17:34:37 +0200 [thread overview]
Message-ID: <20250606153440.865808-11-alexandre.chartre@oracle.com> (raw)
In-Reply-To: <20250606153440.865808-1-alexandre.chartre@oracle.com>
During function validation, objtool maintains a per-instruction state,
in particular to track call frame information. When tracing validation,
print any instruction state changes.
Signed-off-by: Alexandre Chartre <alexandre.chartre@oracle.com>
---
tools/objtool/check.c | 116 +++++++++++++++++++++++++-
tools/objtool/disas.c | 27 ++++++
tools/objtool/include/objtool/check.h | 1 +
3 files changed, 143 insertions(+), 1 deletion(-)
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 40eaac4b5756..050d34930372 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -111,6 +111,111 @@ static void vtrace_insn(struct instruction *insn, const char *format, ...)
free((char *)addr_str);
}
+/*
+ * Macros to trace CFI state attributes changes.
+ */
+
+#define VTRACE_CFI_ATTR(attr, prev, next, fmt, ...) \
+ do { \
+ if ((prev)->attr != (next)->attr) \
+ VTRACE_PRINTF("%s=" fmt " ", #attr, __VA_ARGS__); \
+ } while (0)
+
+#define VTRACE_CFI_ATTR_BOOL(attr, prev, next) \
+ VTRACE_CFI_ATTR(attr, prev, next, \
+ "%s", (next)->attr ? "true" : "false")
+
+#define VTRACE_CFI_ATTR_NUM(attr, prev, next, fmt) \
+ VTRACE_CFI_ATTR(attr, prev, next, fmt, (next)->attr)
+
+/*
+ * Functions and macros to trace CFI registers changes.
+ */
+
+static void vtrace_cfi_register(const char *prefix, int reg, const char *fmt,
+ int base_prev, int offset_prev,
+ int base_next, int offset_next)
+{
+ const char *rname;
+
+ if (base_prev == base_next && offset_prev == offset_next)
+ return;
+
+ if (prefix)
+ VTRACE_PRINTF("%s:", prefix);
+
+ rname = register_name(reg);
+
+ if (base_next == CFI_UNDEFINED) {
+ VTRACE_PRINTF("%1$s=<undef> ", rname);
+ } else {
+ VTRACE_PRINTF(fmt, rname,
+ register_name(base_next), offset_next);
+ }
+}
+
+static void vtrace_cfi_reg_value(const char *prefix, int reg,
+ int base_prev, int offset_prev,
+ int base_next, int offset_next)
+{
+ vtrace_cfi_register(prefix, reg, "%1$s=%2$s%3$+d ",
+ base_prev, offset_prev, base_next, offset_next);
+}
+
+static void vtrace_cfi_reg_reference(const char *prefix, int reg,
+ int base_prev, int offset_prev,
+ int base_next, int offset_next)
+{
+ vtrace_cfi_register(prefix, reg, "%1$s=(%2$s%3$+d) ",
+ base_prev, offset_prev, base_next, offset_next);
+}
+
+#define VTRACE_CFI_REG_VAL(reg, prev, next) \
+ vtrace_cfi_reg_value(NULL, reg, prev.base, prev.offset, \
+ next.base, next.offset)
+
+#define VTRACE_CFI_REG_REF(reg, prev, next) \
+ vtrace_cfi_reg_reference(NULL, reg, prev.base, prev.offset, \
+ next.base, next.offset)
+
+static void vtrace_insn_state(struct instruction *insn,
+ struct insn_state *sprev,
+ struct insn_state *snext)
+{
+ struct cfi_state *cprev, *cnext;
+ int i;
+
+ if (memcmp(sprev, snext, sizeof(struct insn_state)) == 0)
+ return;
+
+ cprev = &sprev->cfi;
+ cnext = &snext->cfi;
+
+ vtrace_insn(insn, NULL);
+ VTRACE_PRINTF(" - state: ");
+
+ /* print registers changes */
+ VTRACE_CFI_REG_VAL(CFI_CFA, cprev->cfa, cnext->cfa);
+ for (i = 0; i < CFI_NUM_REGS; i++) {
+ VTRACE_CFI_REG_VAL(i, cprev->vals[i], cnext->vals[i]);
+ VTRACE_CFI_REG_REF(i, cprev->regs[i], cnext->regs[i]);
+ }
+
+ /* print attributes changes */
+ VTRACE_CFI_ATTR_NUM(stack_size, cprev, cnext, "%d");
+ VTRACE_CFI_ATTR_BOOL(drap, cprev, cnext);
+ if (cnext->drap) {
+ vtrace_cfi_reg_value("drap", cnext->drap_reg,
+ cprev->drap_reg, cprev->drap_offset,
+ cnext->drap_reg, cnext->drap_offset);
+ }
+ VTRACE_CFI_ATTR_BOOL(bp_scratch, cprev, cnext);
+ VTRACE_CFI_ATTR_NUM(instr, sprev, snext, "%d");
+ VTRACE_CFI_ATTR_NUM(uaccess_stack, sprev, snext, "%u");
+
+ VTRACE_PRINTF("\n");
+}
+
struct instruction *find_insn(struct objtool_file *file,
struct section *sec, unsigned long offset)
{
@@ -3698,6 +3803,7 @@ static int validate_insn(struct objtool_file *file, struct symbol *func,
struct instruction *prev_insn, struct instruction *next_insn,
bool *validate_nextp)
{
+ struct insn_state state_prev;
struct alternative *alt;
u8 visited;
int ret;
@@ -3814,7 +3920,15 @@ static int validate_insn(struct objtool_file *file, struct symbol *func,
if (skip_alt_group(insn))
return 0;
- if (handle_insn_ops(insn, next_insn, statep))
+ if (vtrace)
+ state_prev = *statep;
+
+ ret = handle_insn_ops(insn, next_insn, statep);
+
+ if (vtrace)
+ vtrace_insn_state(insn, &state_prev, statep);
+
+ if (ret)
return 1;
switch (insn->type) {
diff --git a/tools/objtool/disas.c b/tools/objtool/disas.c
index 1e198d5f9205..4326c608f925 100644
--- a/tools/objtool/disas.c
+++ b/tools/objtool/disas.c
@@ -29,6 +29,33 @@ struct disas_context {
((*(dinfo)->fprintf_func)((dinfo)->stream, __VA_ARGS__))
+#define REGISTER_NAME_MAXLEN 16
+
+/*
+ * Return the name of a register. Note that the same static buffer
+ * is returned if the name is dynamically generated.
+ */
+const char *register_name(unsigned int reg)
+{
+ static char rname_buffer[REGISTER_NAME_MAXLEN];
+
+ switch (reg) {
+ case CFI_UNDEFINED:
+ return "<undefined>";
+ case CFI_CFA:
+ return "cfa";
+ case CFI_SP_INDIRECT:
+ return "(sp)";
+ case CFI_BP_INDIRECT:
+ return "(bp)";
+ }
+
+ if (snprintf(rname_buffer, REGISTER_NAME_MAXLEN, "r%d", reg) == 1)
+ return NULL;
+
+ return (const char *)rname_buffer;
+}
+
static int dbuffer_init(struct dbuffer *dbuf, size_t size)
{
dbuf->used = 0;
diff --git a/tools/objtool/include/objtool/check.h b/tools/objtool/include/objtool/check.h
index 1b9b399578ea..137d20963921 100644
--- a/tools/objtool/include/objtool/check.h
+++ b/tools/objtool/include/objtool/check.h
@@ -152,5 +152,6 @@ int disas_info_init(struct disassemble_info *dinfo,
size_t disas_insn(struct disas_context *dctx, struct instruction *insn);
char *disas_result(struct disas_context *dctx);
const char *objtool_disas_insn(struct instruction *insn);
+const char *register_name(unsigned int reg);
#endif /* _CHECK_H */
--
2.43.5
next prev parent reply other threads:[~2025-06-06 15:35 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-06 15:34 [RFC 00/13] objtool: Function validation tracing Alexandre Chartre
2025-06-06 15:34 ` [RFC 01/13] objtool: Move disassembly functions to a separated file Alexandre Chartre
2025-06-06 15:34 ` [RFC 02/13] objtool: Create disassembly context Alexandre Chartre
2025-06-10 21:12 ` Josh Poimboeuf
2025-06-06 15:34 ` [RFC 03/13] objtool: Disassemble code with libopcodes instead of running objdump Alexandre Chartre
2025-06-10 21:22 ` Josh Poimboeuf
2025-06-11 12:23 ` Peter Zijlstra
2025-06-11 13:35 ` Alexandre Chartre
2025-06-11 19:25 ` Josh Poimboeuf
2025-06-06 15:34 ` [RFC 04/13] objtool: Print symbol during disassembly Alexandre Chartre
2025-06-10 21:55 ` Josh Poimboeuf
2025-06-06 15:34 ` [RFC 05/13] objtool: Store instruction disassembly result Alexandre Chartre
2025-06-10 22:40 ` Josh Poimboeuf
2025-06-06 15:34 ` [RFC 06/13] objtool: Disassemble instruction on warning or backtrace Alexandre Chartre
2025-06-06 15:34 ` [RFC 07/13] objtool: Extract code to validate instruction from the validate branch loop Alexandre Chartre
2025-06-10 23:31 ` Josh Poimboeuf
2025-06-06 15:34 ` [RFC 08/13] objtool: Record symbol name max length Alexandre Chartre
2025-06-06 15:34 ` [RFC 09/13] objtool: Add option to trace function validation Alexandre Chartre
2025-06-11 0:48 ` Josh Poimboeuf
2025-06-06 15:34 ` Alexandre Chartre [this message]
2025-06-11 1:23 ` [RFC 10/13] objtool: Trace instruction state changes during " Josh Poimboeuf
2025-06-06 15:34 ` [RFC 11/13] objtool: Improve register reporting " Alexandre Chartre
2025-06-06 15:34 ` [RFC 12/13] objtool: Improve tracing of alternative instructions Alexandre Chartre
2025-06-06 15:34 ` [RFC 13/13] objtool: Do not validate IBT for .return_sites and .call_sites Alexandre Chartre
2025-06-06 15:58 ` [RFC 00/13] objtool: Function validation tracing Josh Poimboeuf
2025-06-06 19:29 ` Alexandre Chartre
2025-06-09 18:31 ` Josh Poimboeuf
2025-06-10 7:07 ` Alexandre Chartre
2025-06-10 13:00 ` Alexandre Chartre
2025-06-10 21:05 ` Josh Poimboeuf
2025-06-11 6:00 ` Alexandre Chartre
2025-06-11 14:20 ` 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=20250606153440.865808-11-alexandre.chartre@oracle.com \
--to=alexandre.chartre@oracle.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®