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
Cc: alexandre.chartre@oracle.com
Subject: [RFC 05/13] objtool: Store instruction disassembly result
Date: Fri,  6 Jun 2025 17:34:32 +0200	[thread overview]
Message-ID: <20250606153440.865808-6-alexandre.chartre@oracle.com> (raw)
In-Reply-To: <20250606153440.865808-1-alexandre.chartre@oracle.com>

When disassembling an instruction store the result instead of directly
printing it.

Signed-off-by: Alexandre Chartre <alexandre.chartre@oracle.com>
---
 tools/objtool/disas.c | 161 ++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 155 insertions(+), 6 deletions(-)

diff --git a/tools/objtool/disas.c b/tools/objtool/disas.c
index 83fe2c018c4b..f86b9b04ef97 100644
--- a/tools/objtool/disas.c
+++ b/tools/objtool/disas.c
@@ -11,9 +11,16 @@
 #include <linux/string.h>
 #include <tools/dis-asm-compat.h>
 
+struct dbuffer {
+	char *addr;
+	size_t size;
+	size_t used;
+};
+
 struct disas_context {
 	struct objtool_file *file;
 	struct instruction *insn;
+	struct dbuffer result;
 	disassembler_ftype disassembler;
 	struct disassemble_info info;
 };
@@ -21,6 +28,129 @@ struct disas_context {
 #define DINFO_FPRINTF(dinfo, ...)	\
 	((*(dinfo)->fprintf_func)((dinfo)->stream, __VA_ARGS__))
 
+
+static int dbuffer_init(struct dbuffer *dbuf, size_t size)
+{
+	dbuf->used = 0;
+	dbuf->size = size;
+
+	if (!size) {
+		dbuf->addr = NULL;
+		return 0;
+	}
+
+	dbuf->addr = malloc(size);
+	if (!dbuf->addr)
+		return -1;
+
+	return 0;
+}
+
+static void dbuffer_fini(struct dbuffer *dbuf)
+{
+	free(dbuf->addr);
+	dbuf->size = 0;
+	dbuf->used = 0;
+}
+
+static void dbuffer_reset(struct dbuffer *dbuf)
+{
+	dbuf->used = 0;
+}
+
+static char *dbuffer_data(struct dbuffer *dbuf)
+{
+	return dbuf->addr;
+}
+
+static int dbuffer_expand(struct dbuffer *dbuf, size_t space)
+{
+	size_t size;
+	char *addr;
+
+	size = dbuf->size + space;
+	addr = realloc(dbuf->addr, size);
+	if (!addr)
+		return -1;
+
+	dbuf->addr = addr;
+	dbuf->size = size;
+
+	return 0;
+}
+
+static int dbuffer_vappendf_noexpand(struct dbuffer *dbuf, const char *fmt, va_list ap)
+{
+	int free, len;
+
+	free = dbuf->size - dbuf->used;
+
+	len = vsnprintf(dbuf->addr + dbuf->used, free, fmt, ap);
+
+	if (len < 0)
+		return -1;
+
+	if (len < free) {
+		dbuf->used += len;
+		return 0;
+	}
+
+	return (len - free) + 1;
+}
+
+static int dbuffer_vappendf(struct dbuffer *dbuf, const char *fmt, va_list ap)
+{
+	int space_needed, err;
+
+	space_needed = dbuffer_vappendf_noexpand(dbuf, fmt, ap);
+	if (space_needed <= 0)
+		return space_needed;
+
+	/*
+	 * The buffer is not large enough to store all data. Expand
+	 * the buffer and retry. The buffer is expanded with enough
+	 * space to store all data.
+	 */
+	err = dbuffer_expand(dbuf, space_needed * 2);
+	if (err) {
+		WARN("failed to expand buffer\n");
+		return -1;
+	}
+
+	return dbuffer_vappendf_noexpand(dbuf, fmt, ap);
+}
+
+static int disas_fprintf(void *stream, const char *fmt, ...)
+{
+	va_list arg;
+	int len;
+
+	va_start(arg, fmt);
+	len = dbuffer_vappendf(stream, fmt, arg);
+	va_end(arg);
+
+	return len == 0 ? 0 : -1;
+}
+
+/*
+ * For init_disassemble_info_compat().
+ */
+static int disas_fprintf_styled(void *stream,
+				enum disassembler_style style,
+				const char *fmt, ...)
+{
+	va_list arg;
+	int len;
+
+	(void)style;
+
+	va_start(arg, fmt);
+	len = dbuffer_vappendf(stream, fmt, arg);
+	va_end(arg);
+
+	return len == 0 ? 0 : -1;
+}
+
 static void disas_print_address(bfd_vma addr, struct disassemble_info *dinfo)
 {
 	struct disas_context *dctx = dinfo->application_data;
@@ -147,6 +277,7 @@ struct disas_context *disas_context_create(struct objtool_file *file)
 {
 	struct disas_context *dctx;
 	struct disassemble_info *dinfo;
+	struct dbuffer *dbuf;
 	int err;
 
 	dctx = malloc(sizeof(*dctx));
@@ -157,10 +288,16 @@ struct disas_context *disas_context_create(struct objtool_file *file)
 
 	dctx->file = file;
 	dinfo = &dctx->info;
+	dbuf = &dctx->result;
+
+	err = dbuffer_init(dbuf, 1024);
+	if (err) {
+		WARN("failed to initialize buffer\n");
+		return NULL;
+	}
 
-	init_disassemble_info_compat(dinfo, stdout,
-				     (fprintf_ftype)fprintf,
-				     fprintf_styled);
+	init_disassemble_info_compat(dinfo, dbuf,
+				     disas_fprintf, disas_fprintf_styled);
 
 	dinfo->read_memory_func = buffer_read_memory;
 	dinfo->print_address_func = disas_print_address;
@@ -204,9 +341,18 @@ struct disas_context *disas_context_create(struct objtool_file *file)
 
 void disas_context_destroy(struct disas_context *dctx)
 {
+	if (!dctx)
+		return;
+
+	dbuffer_fini(&dctx->result);
 	free(dctx);
 }
 
+static char *disas_result(struct disas_context *dctx)
+{
+	return dbuffer_data(&dctx->result);
+}
+
 /*
  * Disassemble a single instruction. Return the size of the instruction.
  */
@@ -216,6 +362,7 @@ static size_t disas_insn(struct disas_context *dctx,
 	disassembler_ftype disasm = dctx->disassembler;
 	struct disassemble_info *dinfo = &dctx->info;
 
+	dbuffer_reset(&dctx->result);
 	dctx->insn = insn;
 
 	/*
@@ -241,10 +388,12 @@ static void disas_func(struct disas_context *dctx, struct symbol *func)
 	sym_for_each_insn(dctx->file, func, insn) {
 
 		addr = insn->offset;
-		printf(" %6lx:  %s+0x%-6lx      ",
-		       addr, func->name, addr - func->offset);
 		size = disas_insn(dctx, insn);
-		printf("\n");
+
+		printf(" %6lx:  %s+0x%-6lx      %s\n",
+		       addr, func->name, addr - func->offset,
+		       disas_result(dctx));
+
 		if (size != insn->len)
 			WARN("inconsistent insn size (%ld and %d)\n", size, insn->len);
 	}
-- 
2.43.5


  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 ` Alexandre Chartre [this message]
2025-06-10 22:40   ` [RFC 05/13] objtool: Store instruction disassembly result 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 ` [RFC 10/13] objtool: Trace instruction state changes during " Alexandre Chartre
2025-06-11  1:23   ` 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-6-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®