mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "tip-bot2 for Josh Poimboeuf" <tip-bot2@linutronix.de>
To: linux-tip-commits@vger.kernel.org
Cc: Josh Poimboeuf <jpoimboe@kernel.org>,
	"Peter Zijlstra (Intel)" <peterz@infradead.org>,
	x86@kernel.org, linux-kernel@vger.kernel.org
Subject: [tip: objtool/core] objtool: Create backup on error and print args
Date: Mon, 17 Mar 2025 10:46:25 -0000	[thread overview]
Message-ID: <174220838508.14745.14480888937876114986.tip-bot2@tip-bot2> (raw)
In-Reply-To: <7571e30636359b3e173ce6e122419452bb31882f.1741975349.git.jpoimboe@kernel.org>

The following commit has been merged into the objtool/core branch of tip:

Commit-ID:     aa8b3e64fd397eddd6a627d148a964e4bc2ed9ab
Gitweb:        https://git.kernel.org/tip/aa8b3e64fd397eddd6a627d148a964e4bc2ed9ab
Author:        Josh Poimboeuf <jpoimboe@kernel.org>
AuthorDate:    Fri, 14 Mar 2025 12:29:10 -07:00
Committer:     Peter Zijlstra <peterz@infradead.org>
CommitterDate: Mon, 17 Mar 2025 11:36:02 +01:00

objtool: Create backup on error and print args

Recreating objtool errors can be a manual process.  Kbuild removes the
object, so it has to be compiled or linked again before running objtool.
Then the objtool args need to be reversed engineered.

Make that all easier by automatically making a backup of the object file
on error, and print a modified version of the args which can be used to
recreate.

Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lore.kernel.org/r/7571e30636359b3e173ce6e122419452bb31882f.1741975349.git.jpoimboe@kernel.org
---
 tools/objtool/builtin-check.c           | 68 ++++++++++++++++++++++--
 tools/objtool/include/objtool/builtin.h |  1 +-
 tools/objtool/objtool.c                 | 63 +----------------------
 3 files changed, 65 insertions(+), 67 deletions(-)

diff --git a/tools/objtool/builtin-check.c b/tools/objtool/builtin-check.c
index c201650..39ddca6 100644
--- a/tools/objtool/builtin-check.c
+++ b/tools/objtool/builtin-check.c
@@ -91,7 +91,6 @@ static const struct option check_options[] = {
 
 	OPT_GROUP("Options:"),
 	OPT_BOOLEAN(0,   "backtrace", &opts.backtrace, "unwind on error"),
-	OPT_BOOLEAN(0,   "backup", &opts.backup, "create .orig files before modification"),
 	OPT_BOOLEAN(0,   "dry-run", &opts.dryrun, "don't write modifications"),
 	OPT_BOOLEAN(0,   "link", &opts.link, "object is a linked object"),
 	OPT_BOOLEAN(0,   "module", &opts.module, "object is part of a kernel module"),
@@ -228,10 +227,39 @@ static int copy_file(const char *src, const char *dst)
 	return 0;
 }
 
+static char **save_argv(int argc, const char **argv)
+{
+	char **orig_argv;
+
+	orig_argv = calloc(argc, sizeof(char *));
+	if (!orig_argv) {
+		perror("calloc");
+		return NULL;
+	}
+
+	for (int i = 0; i < argc; i++) {
+		orig_argv[i] = strdup(argv[i]);
+		if (!orig_argv[i]) {
+			perror("strdup");
+			return NULL;
+		}
+	};
+
+	return orig_argv;
+}
+
+#define ORIG_SUFFIX ".orig"
+
 int objtool_run(int argc, const char **argv)
 {
 	struct objtool_file *file;
-	int ret;
+	char *backup = NULL;
+	char **orig_argv;
+	int ret = 0;
+
+	orig_argv = save_argv(argc, argv);
+	if (!orig_argv)
+		return 1;
 
 	cmd_parse_options(argc, argv, check_usage);
 
@@ -271,8 +299,42 @@ int objtool_run(int argc, const char **argv)
 	return 0;
 
 err:
-	if (opts.output)
+	if (opts.dryrun)
+		goto err_msg;
+
+	if (opts.output) {
 		unlink(opts.output);
+		goto err_msg;
+	}
+
+	/*
+	 * Make a backup before kbuild deletes the file so the error
+	 * can be recreated without recompiling or relinking.
+	 */
+	backup = malloc(strlen(objname) + strlen(ORIG_SUFFIX) + 1);
+	if (!backup) {
+		perror("malloc");
+		return 1;
+	}
+
+	strcpy(backup, objname);
+	strcat(backup, ORIG_SUFFIX);
+	if (copy_file(objname, backup))
+		return 1;
+
+err_msg:
+	fprintf(stderr, "%s", orig_argv[0]);
+
+	for (int i = 1; i < argc; i++) {
+		char *arg = orig_argv[i];
+
+		if (backup && !strcmp(arg, objname))
+			fprintf(stderr, " %s -o %s", backup, objname);
+		else
+			fprintf(stderr, " %s", arg);
+	}
+
+	fprintf(stderr, "\n");
 
 	return 1;
 }
diff --git a/tools/objtool/include/objtool/builtin.h b/tools/objtool/include/objtool/builtin.h
index b18f114..0fafd0f 100644
--- a/tools/objtool/include/objtool/builtin.h
+++ b/tools/objtool/include/objtool/builtin.h
@@ -29,7 +29,6 @@ struct opts {
 
 	/* options: */
 	bool backtrace;
-	bool backup;
 	bool dryrun;
 	bool link;
 	bool mnop;
diff --git a/tools/objtool/objtool.c b/tools/objtool/objtool.c
index 53cd881..1c73fb6 100644
--- a/tools/objtool/objtool.c
+++ b/tools/objtool/objtool.c
@@ -20,64 +20,6 @@ bool help;
 
 static struct objtool_file file;
 
-static bool objtool_create_backup(const char *_objname)
-{
-	int len = strlen(_objname);
-	char *buf, *base, *name = malloc(len+6);
-	int s, d, l, t;
-
-	if (!name) {
-		perror("failed backup name malloc");
-		return false;
-	}
-
-	strcpy(name, _objname);
-	strcpy(name + len, ".orig");
-
-	d = open(name, O_CREAT|O_WRONLY|O_TRUNC, 0644);
-	if (d < 0) {
-		perror("failed to create backup file");
-		return false;
-	}
-
-	s = open(_objname, O_RDONLY);
-	if (s < 0) {
-		perror("failed to open orig file");
-		return false;
-	}
-
-	buf = malloc(4096);
-	if (!buf) {
-		perror("failed backup data malloc");
-		return false;
-	}
-
-	while ((l = read(s, buf, 4096)) > 0) {
-		base = buf;
-		do {
-			t = write(d, base, l);
-			if (t < 0) {
-				perror("failed backup write");
-				return false;
-			}
-			base += t;
-			l -= t;
-		} while (l);
-	}
-
-	if (l < 0) {
-		perror("failed backup read");
-		return false;
-	}
-
-	free(name);
-	free(buf);
-	close(d);
-	close(s);
-
-	return true;
-}
-
 struct objtool_file *objtool_open_read(const char *filename)
 {
 	if (file.elf) {
@@ -89,11 +31,6 @@ struct objtool_file *objtool_open_read(const char *filename)
 	if (!file.elf)
 		return NULL;
 
-	if (opts.backup && !objtool_create_backup(objname)) {
-		WARN("can't create backup file");
-		return NULL;
-	}
-
 	hash_init(file.insn_hash);
 	INIT_LIST_HEAD(&file.retpoline_call_list);
 	INIT_LIST_HEAD(&file.return_thunk_list);

  parent reply	other threads:[~2025-03-17 10:46 UTC|newest]

Thread overview: 68+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-14 19:28 [PATCH 00/13] Fail the build on objtool warnings Josh Poimboeuf
2025-03-14 19:28 ` [PATCH 01/13] x86/traps: Make exc_double_fault() consistently noreturn Josh Poimboeuf
2025-03-17  8:26   ` Brendan Jackman
2025-03-17 10:46   ` [tip: objtool/core] " tip-bot2 for Josh Poimboeuf
2025-03-17 10:47   ` [PATCH 01/13] " Miroslav Benes
2025-03-14 19:29 ` [PATCH 02/13] objtool: Fix error handling inconsistencies in check() Josh Poimboeuf
2025-03-17  8:29   ` Brendan Jackman
2025-03-17 10:46   ` [tip: objtool/core] " tip-bot2 for Josh Poimboeuf
2025-03-17 10:47   ` [PATCH 02/13] " Miroslav Benes
2025-03-14 19:29 ` [PATCH 03/13] objtool: Improve __noreturn annotation warning Josh Poimboeuf
2025-03-17  8:31   ` Brendan Jackman
2025-03-17 10:36     ` Miroslav Benes
2025-03-17 10:46   ` [tip: objtool/core] " tip-bot2 for Josh Poimboeuf
2025-03-14 19:29 ` [PATCH 04/13] objtool: Update documentation Josh Poimboeuf
2025-03-17  8:47   ` Brendan Jackman
2025-03-17 10:46   ` [tip: objtool/core] " tip-bot2 for Josh Poimboeuf
2025-03-17 10:46   ` [PATCH 04/13] " Miroslav Benes
2025-03-14 19:29 ` [PATCH 05/13] objtool: Increase per-function WARN_FUNC() rate limit Josh Poimboeuf
2025-03-17  9:15   ` Brendan Jackman
2025-03-17 12:29     ` Miroslav Benes
2025-03-18 10:59       ` Brendan Jackman
2025-03-17 10:46   ` [tip: objtool/core] " tip-bot2 for Josh Poimboeuf
2025-03-14 19:29 ` [PATCH 06/13] objtool: Remove --unret dependency on --rethunk Josh Poimboeuf
2025-03-14 19:38   ` Peter Zijlstra
2025-03-14 19:52     ` Josh Poimboeuf
2025-03-14 19:58       ` Peter Zijlstra
2025-03-17 12:33       ` Miroslav Benes
2025-03-17 10:46   ` [tip: objtool/core] " tip-bot2 for Josh Poimboeuf
2025-03-14 19:29 ` [PATCH 07/13] objtool: Consolidate option validation Josh Poimboeuf
2025-03-17  9:19   ` Brendan Jackman
2025-03-17 10:46   ` [tip: objtool/core] " tip-bot2 for Josh Poimboeuf
2025-03-17 12:35   ` [PATCH 07/13] " Miroslav Benes
2025-03-14 19:29 ` [PATCH 08/13] objtool: Upgrade "Linked object detected" warning to error Josh Poimboeuf
2025-03-17  9:21   ` Brendan Jackman
2025-03-17 10:46   ` [tip: objtool/core] " tip-bot2 for Josh Poimboeuf
2025-03-17 12:38   ` [PATCH 08/13] " Miroslav Benes
2025-03-14 19:29 ` [PATCH 09/13] objtool: Add --output option Josh Poimboeuf
2025-03-17  9:40   ` Brendan Jackman
2025-03-17 10:46   ` [tip: objtool/core] " tip-bot2 for Josh Poimboeuf
2025-03-17 12:48   ` [PATCH 09/13] " Miroslav Benes
2025-03-14 19:29 ` [PATCH 10/13] objtool: Add --Werror option Josh Poimboeuf
2025-03-17 10:46   ` [tip: objtool/core] " tip-bot2 for Josh Poimboeuf
2025-03-17 12:53   ` [PATCH 10/13] " Miroslav Benes
2025-03-14 19:29 ` [PATCH 11/13] objtool: Change "warning:" to "error:" for --Werror Josh Poimboeuf
2025-03-17  9:42   ` Brendan Jackman
2025-03-17 10:46   ` [tip: objtool/core] " tip-bot2 for Josh Poimboeuf
2025-03-17 13:04   ` [PATCH 11/13] " Miroslav Benes
2025-03-14 19:29 ` [PATCH 12/13] objtool: Create backup on error and print args Josh Poimboeuf
2025-03-17  9:53   ` Brendan Jackman
2025-03-17 10:46   ` tip-bot2 for Josh Poimboeuf [this message]
2025-03-17 14:09   ` Miroslav Benes
2025-03-14 19:29 ` [PATCH 13/13] objtool: Add CONFIG_OBJTOOL_WERROR Josh Poimboeuf
2025-03-16  0:41   ` Ingo Molnar
2025-03-16  1:49     ` Josh Poimboeuf
2025-03-16 11:56       ` Ingo Molnar
2025-03-18  4:55         ` Josh Poimboeuf
2025-03-17 10:46   ` [tip: objtool/core] " tip-bot2 for Josh Poimboeuf
2025-03-17 11:07   ` tip-bot2 for Josh Poimboeuf
2025-03-17 21:27     ` Ingo Molnar
2025-03-18  0:05       ` Josh Poimboeuf
2025-03-20 17:10         ` Steven Rostedt
2025-03-18 11:51       ` Ingo Molnar
2025-03-18 14:26         ` Josh Poimboeuf
2025-03-20  8:51         ` Ingo Molnar
2025-03-18  0:27     ` Josh Poimboeuf
2025-03-18  4:59       ` Josh Poimboeuf
2025-03-17 14:14   ` [PATCH 13/13] " Miroslav Benes
2025-03-23  7:50     ` Josh Poimboeuf

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=174220838508.14745.14480888937876114986.tip-bot2@tip-bot2 \
    --to=tip-bot2@linutronix.de \
    --cc=jpoimboe@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tip-commits@vger.kernel.org \
    --cc=peterz@infradead.org \
    --cc=x86@kernel.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®