[CRIU] [PATCH 2/4] piegen: separates generated output from debug

Laurent Dufour ldufour at linux.vnet.ibm.com
Wed Jun 10 06:00:40 PDT 2015


Introduce a new -o argument to piegen to specify generate file name.
Send the debug stream to stdout and force it to /dev/null in the makefile
if V=1 is not specify.

Signed-off-by: Laurent Dufour <ldufour at linux.vnet.ibm.com>
---
 pie/Makefile        |  6 +++++-
 pie/piegen/main.c   | 27 ++++++++++++++++++++++-----
 pie/piegen/piegen.h | 10 ++++------
 3 files changed, 31 insertions(+), 12 deletions(-)

diff --git a/pie/Makefile b/pie/Makefile
index 2ad4bcfca827..efafa361dc7f 100644
--- a/pie/Makefile
+++ b/pie/Makefile
@@ -77,13 +77,17 @@ $(obj)/$(PIELDS): $(obj)/pie-reloc.lds.S.in
 endif
 endif
 
+ifeq ($(strip $(V)),)
+piegen_stdout = >/dev/null
+endif
+
 $(obj)/%.built-in.bin.o: $(obj)/%.built-in.o $(obj)/$(PIELDS)
 	$(E) "  GEN     " $@
 	$(Q) $(LD) $(ldflags-y) -T $(obj)/$(PIELDS) -o $@ $<
 
 $(obj)/%-blob.h: $(obj)/%.built-in.bin.o $(obj)/$(PIELDS) pie/piegen
 	$(E) "  GEN     " $@
-	$(Q) pie/piegen/piegen -f $< -p $(call target-name,$@)_blob_offset__ -s $(call target-name,$@)_blob > $@
+	$(Q) pie/piegen/piegen -f $< -p $(call target-name,$@)_blob_offset__ -s $(call target-name,$@)_blob -o $@ $(piegen_stdout)
 
 else
 
diff --git a/pie/piegen/main.c b/pie/piegen/main.c
index 406a0a89718c..d1e90f6192fc 100644
--- a/pie/piegen/main.c
+++ b/pie/piegen/main.c
@@ -25,6 +25,8 @@ piegen_opt_t opts = {
 	.nrgotpcrel_name	= "nr_gotpcrel",
 };
 
+FILE *fout;
+
 static int handle_elf(const piegen_opt_t *opts, void *mem, size_t size)
 {
 #if defined(CONFIG_X86_32) || defined(CONFIG_X86_64)
@@ -73,9 +75,10 @@ int main(int argc, char *argv[])
 	void *mem;
 	int fd;
 
-	static const char short_opts[] = "f:s:p:v:h";
+	static const char short_opts[] = "f:o:s:p:v:h";
 	static struct option long_opts[] = {
 		{ "file",	required_argument,	0, 'f' },
+		{ "output",	required_argument,	0, 'o' },
 		{ "stream",	required_argument,	0, 's' },
 		{ "sym-prefix",	required_argument,	0, 'p' },
 		{ "variable",	required_argument,	0, 'v' },
@@ -95,6 +98,9 @@ int main(int argc, char *argv[])
 		case 'f':
 			opts.input_filename = optarg;
 			break;
+		case 'o':
+			opts.output_filename = optarg;
+			break;
 		case 's':
 			opts.stream_name = optarg;
 			break;
@@ -121,17 +127,28 @@ int main(int argc, char *argv[])
 		goto err;
 	}
 
+	fout = fopen(opts.output_filename, "w");
+	if (fout == NULL) {
+		pr_perror("Can't open %s", opts.output_filename);
+		goto err;
+	}
+
 	mem = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FILE, fd, 0);
 	if (mem == MAP_FAILED) {
 		pr_perror("Can't mmap file %s", opts.input_filename);
 		goto err;
 	}
 
-	if (handle_elf(&opts, mem, st.st_size))
+	if (handle_elf(&opts, mem, st.st_size)) {
+		fclose(fout);
+		unlink(opts.output_filename);
 		goto err;
-	return 1;
+	}
+	fclose(fout);
+	printf("%s generated successfully.\n", opts.output_filename);
+	return 0;
 usage:
-	printf("Usage: %s -f filename\n", argv[0]);
+	fprintf(stderr, "Usage: %s -f filename\n", argv[0]);
 err:
-	return 0;
+	return 1;
 }
diff --git a/pie/piegen/piegen.h b/pie/piegen/piegen.h
index 5b0bc7917f07..02d431a93913 100644
--- a/pie/piegen/piegen.h
+++ b/pie/piegen/piegen.h
@@ -6,6 +6,7 @@
 
 typedef struct {
 	char		*input_filename;
+	char		*output_filename;
 	char		*stream_name;
 	char		*prefix_name;
 	char		*var_name;
@@ -13,6 +14,7 @@ typedef struct {
 } piegen_opt_t;
 
 extern piegen_opt_t opts;
+extern FILE *fout;
 
 #if defined(CONFIG_X86_32) || defined(CONFIG_X86_64)
 extern int handle_elf_x86_32(const piegen_opt_t *opts, void *mem, size_t size);
@@ -23,13 +25,9 @@ extern int handle_elf_x86_64(const piegen_opt_t *opts, void *mem, size_t size);
 extern int handle_elf_ppc64(const piegen_opt_t *opts, void *mem, size_t size);
 #endif
 
-#define pr_out(fmt, ...)	fprintf(stdout, fmt, ##__VA_ARGS__)
+#define pr_out(fmt, ...)	fprintf(fout, fmt, ##__VA_ARGS__)
 
-#if 0
-# define pr_debug(fmt, ...)	fprintf(stderr, fmt, ##__VA_ARGS__)
-#else
-# define pr_debug(fmt, ...)
-#endif
+#define pr_debug(fmt, ...)	fprintf(stdout, fmt, ##__VA_ARGS__)
 
 #define pr_err(fmt, ...)	fprintf(stderr, "Error (%s:%d): "fmt, __FILE__, __LINE__, ##__VA_ARGS__)
 #define pr_perror(fmt, ...)	fprintf(stderr, "Error (%s:%d): "fmt "%m\n", __FILE__, __LINE__, ##__VA_ARGS__)
-- 
1.9.1



More information about the CRIU mailing list