From 0924d156ee50a58aea9b3e901184b5c8c2c0caaa Mon Sep 17 00:00:00 2001
From: Sungju Kwon <sungju.kwon@gmail.com>
Date: Wed, 3 Jul 2019 10:40:34 +1000
Subject: [PATCH] Allows to change the error output direction

Currently, the error() is always printing the output to the console
through 'stdout'. This does not follow redirection which is good when
you want to know error while redirecting commands output to a file.
However, there are situations that you want to hide error messages or
redirect it into somewhere else.

Using 'set stderr' command, it can be changed to three different
destination - default behaviour 'default', following redirection (fp),
or a custom file path.

        crash> set stderr
        stderr: default
        crash> sym 0x523 > /dev/null
        sym: invalid address: 0x523
        crash> set stderr fp
        stderr: fp
        crash> sym 0x523 > /dev/null
        crash> set stderr /tmp/err.log
        stderr: /tmp/err.log
        crash> sym 0x523 > /dev/null
        crash> set stderr default
        stderr: default
        crash> sym 0x523 > /dev/null
        sym: invalid address: 0x523
---
 defs.h  |  3 +++
 help.c  |  8 +++++++
 main.c  |  2 ++
 tools.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------
 4 files changed, 81 insertions(+), 11 deletions(-)

diff --git a/defs.h b/defs.h
index ceb6eb7..306bacc 100644
--- a/defs.h
+++ b/defs.h
@@ -553,6 +553,8 @@ struct program_context {
 	ulong scope;			/* optional text context address */
 	ulong nr_hash_queues;		/* hash queue head count */
 	char *(*read_vmcoreinfo)(const char *);
+        FILE *stderr;                   /* error() message direction */
+        char *stderr_path;              /* stderr path information */
 };
 
 #define READMEM  pc->readmem
@@ -4955,6 +4957,7 @@ void exec_args_input_file(struct command_table_entry *, struct args_input_file *
 /*
  *  tools.c
  */
+FILE *set_stderr(char *target);
 int __error(int, char *, ...);
 #define error __error               /* avoid conflict with gdb error() */
 int console(char *, ...);
diff --git a/help.c b/help.c
index c21f006..5990922 100644
--- a/help.c
+++ b/help.c
@@ -1093,6 +1093,13 @@ char *help_set[] = {
 "         redzone  on | off     if on, CONFIG_SLUB object addresses displayed by",
 "                               the kmem command will point to the SLAB_RED_ZONE",
 "                               padding inserted at the beginning of the object.", 
+"stderr  default | fp | <path>  set the direction of error output. 'default' always",
+"                               print error on console as well as write into",
+"                               redirected path. 'fp' prints into the redirected",
+"                               path only if redirected to file or pipe. Otherwise,",
+"                               it prints on console just like 'default'.",
+"                               '<path>' is a file path which error message goes",
+"                               and it does not print on console.",
 " ",
 "  Internal variables may be set in four manners:\n",
 "    1. entering the set command in $HOME/.%src.",
@@ -1144,6 +1151,7 @@ char *help_set[] = {
 "             scope: (not set)",
 "           offline: show",
 "           redzone: on",
+"            stderr: default",
 " ",
 "  Show the current context:\n",
 "    %s> set",
diff --git a/main.c b/main.c
index 83ccd31..dbf673c 100644
--- a/main.c
+++ b/main.c
@@ -1085,6 +1085,7 @@ setup_environment(int argc, char **argv)
 	 *  to pipes or output files.
 	 */
 	fp = stdout;
+        set_stderr("default");
 
 	/*
 	 *  Start populating the program_context structure.  It's used so
@@ -1725,6 +1726,7 @@ dump_program_context(void)
 		pc->scope ? "" : "(not set)");
 	fprintf(fp, "   nr_hash_queues: %ld\n", pc->nr_hash_queues);
 	fprintf(fp, "  read_vmcoreinfo: %lx\n", (ulong)pc->read_vmcoreinfo);
+        fprintf(fp, "           stderr: %s\n", pc->stderr_path);
 }
 
 char *
diff --git a/tools.c b/tools.c
index 5c0e63e..077a4cf 100644
--- a/tools.c
+++ b/tools.c
@@ -38,6 +38,45 @@ static void print_value(struct req_entry *, unsigned int, ulong, unsigned int);
 static struct req_entry *fill_member_offsets(char *);
 static void dump_struct_members_fast(struct req_entry *, int, ulong);
 
+FILE *
+set_stderr(char *target)
+{
+        FILE *tmp_fp = NULL;
+        char *tmp_str = NULL;
+
+        if (pc->stderr_path != NULL && !strcmp(target, pc->stderr_path))
+                return pc->stderr;
+
+        tmp_str = malloc(strlen(target) + 1);
+        if (tmp_str == NULL)
+                return NULL;
+        strcpy(tmp_str, target);
+
+        if (!strcmp(target, "default")) {
+                tmp_fp = stdout;
+        } else if (!strcmp(target, "fp")) {
+                tmp_fp = fp;
+        } else {
+                tmp_fp = fopen(target, "a");
+                if (tmp_fp == NULL) {
+                        error(INFO, "invalid path: %s\n", target);
+                        free(tmp_str);
+                        return NULL;
+                }
+        }
+
+        if (pc->stderr != NULL &&
+            strcmp(pc->stderr_path, "default") && strcmp(pc->stderr_path, "fp"))
+                fclose(pc->stderr);
+
+        if (pc->stderr_path)
+                free(pc->stderr_path);
+        pc->stderr = tmp_fp;
+        pc->stderr_path = tmp_str;
+        return pc->stderr;
+}
+
+
 /*
  *  General purpose error reporting routine.  Type INFO prints the message
  *  and returns.  Type FATAL aborts the command in progress, and longjmps
@@ -58,6 +97,9 @@ __error(int type, char *fmt, ...)
         void *retaddr[NUMBER_STACKFRAMES] = { 0 };
 	va_list ap;
 
+        if (!strcmp(pc->stderr_path, "fp"))
+                pc->stderr = fp;
+
 	if (CRASHDEBUG(1) || (pc->flags & DROP_CORE)) {
 		SAVE_RETURN_ADDRESS(retaddr);
 		console("error() trace: %lx => %lx => %lx => %lx\n",
@@ -69,7 +111,7 @@ __error(int type, char *fmt, ...)
         va_end(ap);
 
 	if (!fmt && FATAL_ERROR(type)) {
-		fprintf(stdout, "\n");
+		fprintf(pc->stderr, "\n");
 		clean_exit(1);
 	}
 
@@ -95,25 +137,26 @@ __error(int type, char *fmt, ...)
 			buf);
 		fflush(pc->stdpipe);
 	} else { 
-		fprintf(stdout, "%s%s%s %s%s", 
+		fprintf(pc->stderr, "%s%s%s %s%s",
 			new_line || end_of_line ? "\n" : "",
 			type == WARNING ? "WARNING" : 
 			type == NOTE ? "NOTE" : 
 			type == CONT ? spacebuf : pc->curcmd,
 			type == CONT ? " " : ":",
 			buf, end_of_line ? "\n" : "");
-		fflush(stdout);
+		fflush(pc->stderr);
 	}
 
-        if ((fp != stdout) && (fp != pc->stdpipe) && (fp != pc->tmpfile)) {
+        if ((!strcmp(pc->stderr_path, "default")) &&
+            (fp != stdout) && (fp != pc->stdpipe) && (fp != pc->tmpfile)) {
                 fprintf(fp, "%s%s%s %s", new_line ? "\n" : "",
-			type == WARNING ? "WARNING" : 
-			type == NOTE ? "NOTE" : 
-			type == CONT ? spacebuf : pc->curcmd, 
-			type == CONT ? " " : ":",
-			buf);
-		fflush(fp);
-	}
+                        type == WARNING ? "WARNING" :
+                        type == NOTE ? "NOTE" :
+                        type == CONT ? spacebuf : pc->curcmd,
+                        type == CONT ? " " : ":",
+                        buf);
+                fflush(fp);
+        }
 
 	if ((pc->flags & DROP_CORE) && (type != NOTE)) {
 		dump_trace(retaddr);
@@ -2486,6 +2529,19 @@ cmd_set(void)
 			}
 			return;
 
+                } else if (STREQ(args[optind], "stderr")) {
+                        if (args[optind+1]) {
+                                optind++;
+                                if (set_stderr(args[optind]) == NULL)
+                                        return;
+                        }
+
+                        if (runtime) {
+                                fprintf(fp, "stderr: %s\n",
+                                        pc->stderr_path);
+                        }
+                        return;
+
 		} else if (XEN_HYPER_MODE()) {
 			error(FATAL, "invalid argument for the Xen hypervisor\n");
 		} else if (pc->flags & MINIMAL_MODE) {
@@ -2593,6 +2649,7 @@ show_options(void)
 		fprintf(fp, "(not set)\n");
 	fprintf(fp, "       offline: %s\n", pc->flags2 & OFFLINE_HIDE ? "hide" : "show");
 	fprintf(fp, "       redzone: %s\n", pc->flags2 & REDZONE ? "on" : "off");
+	fprintf(fp, "        stderr: %s\n", pc->stderr_path);
 }
 
 
-- 
1.8.3.1

