From mboxrd@z Thu Jan 1 00:00:00 1970 From: Yosuke Iwamatsu Subject: [PATCH] Log qemu-dm's message with date, time and pid Date: Thu, 26 Jun 2008 14:02:54 +0900 Message-ID: <486322FE.3070304@ab.jp.nec.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------030408080207070406050302" Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: xen-devel@lists.xensource.com Cc: Yuji Shimada List-Id: xen-devel@lists.xenproject.org This is a multi-part message in MIME format. --------------030408080207070406050302 Content-Type: text/plain; charset=ISO-2022-JP Content-Transfer-Encoding: 7bit The qemu-dm's log message doesn't have date, time, and pid. It is difficult to find out the message we want. This patch solves the problem without changing the codes which write messages. We developed a small program called ioemu-logger. qemu-dm creates a pipe and starts ioemu-logger. qemu-dm associates a stream with the pipe, and sets the stream to existing "logfile" variable. When qemu-dm writes a message to "logfile", ioemu-logger receive it through the pipe and outputs it with date, time and pid to the actual log file. Signed-off-by: Yuji Shimada ----------------------- Yosuke Iwamatsu NEC Corporation --------------030408080207070406050302 Content-Type: all/allfiles; name="ioemu-logger.patch" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="ioemu-logger.patch" Log qemu-dm's message with date, time and pid. Signed-off-by: Yuji Shimada diff -r 926a366ca82f tools/Makefile --- a/tools/Makefile Fri Jun 20 15:21:26 2008 +0100 +++ b/tools/Makefile Wed Jun 25 17:26:20 2008 +0900 @@ -23,6 +23,7 @@ SUBDIRS-y += libfsimage SUBDIRS-y += libfsimage SUBDIRS-$(LIBXENAPI_BINDINGS) += libxen SUBDIRS-$(CONFIG_IOEMU) += ioemu +SUBDIRS-$(CONFIG_IOEMU) += ioemu-logger # These don't cross-compile ifeq ($(XEN_COMPILE_ARCH),$(XEN_TARGET_ARCH)) @@ -61,4 +62,5 @@ subdir-all-ioemu subdir-install-ioemu: i subdir-clean-ioemu: $(MAKE) -C ioemu distclean + $(MAKE) -C ioemu-logger distclean diff -r 926a366ca82f tools/ioemu-logger/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tools/ioemu-logger/Makefile Wed Jun 25 17:26:20 2008 +0900 @@ -0,0 +1,21 @@ +XEN_ROOT=../.. +include $(XEN_ROOT)/tools/Rules.mk +-include $(XEN_ROOT)/tools/ioemu/config-host.mak + +TARGET = ioemu-logger + +.PHONY: all +all: $(TARGET) + +.PHONY: install +install: all + $(INSTALL_PROG) $(TARGET) $(DESTDIR)$(bindir)/$(TARGET) + +.PHONY: clean +clean: + rm -f $(TARGET) *.o + +.PHONY: distclean +distclean: clean + +$(TARGET): ioemu-logger.o diff -r 926a366ca82f tools/ioemu-logger/ioemu-logger.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tools/ioemu-logger/ioemu-logger.c Wed Jun 25 17:26:20 2008 +0900 @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2008, NEC Corporation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of version 2.1 of the GNU Lesser General Public + * License as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include +#include +#include +#include +#include + +#define LOGBUFSIZ 256 + +int main(int argc, char **argv) +{ + char buffer[LOGBUFSIZ]; + time_t current_time; + struct tm *t_st; + + while (fgets(buffer, LOGBUFSIZ, stdin) != NULL) + { + time(¤t_time); + t_st = localtime(¤t_time); + fprintf(stderr, "[%4d-%.2d-%.2d %.2d:%.2d:%.2d %4d] %s", + t_st->tm_year+1900, t_st->tm_mon+1, t_st->tm_mday, + t_st->tm_hour, t_st->tm_min, t_st->tm_sec, getppid(), buffer); + fflush(stderr); + } + return 0; +} diff -r 926a366ca82f tools/ioemu/cpu-all.h --- a/tools/ioemu/cpu-all.h Fri Jun 20 15:21:26 2008 +0100 +++ b/tools/ioemu/cpu-all.h Wed Jun 25 17:26:20 2008 +0900 @@ -822,7 +822,7 @@ extern CPULogItem cpu_log_items[]; extern CPULogItem cpu_log_items[]; void cpu_set_log(int log_flags); -void cpu_set_log_filename(const char *filename); +void start_logger(void); int cpu_str_to_log_mask(const char *str); /* IO ports API */ diff -r 926a366ca82f tools/ioemu/target-i386-dm/exec-dm.c --- a/tools/ioemu/target-i386-dm/exec-dm.c Fri Jun 20 15:21:26 2008 +0100 +++ b/tools/ioemu/target-i386-dm/exec-dm.c Wed Jun 25 17:26:20 2008 +0900 @@ -122,7 +122,9 @@ static int io_mem_nb = 1; static int io_mem_nb = 1; /* log support */ -FILE *logfile; +FILE *logfile = NULL; +int backup_stderr; +int log_pid; int loglevel; void cpu_exec_init(CPUState *env) @@ -152,12 +154,42 @@ void cpu_set_log(int log_flags) logfile = stderr; } -void cpu_set_log_filename(const char *filename) -{ - logfile = fopen(filename, "w"); - if (!logfile) { - perror(filename); - _exit(1); +void start_logger(void) +{ + int log_pipes[2]; + if (pipe(log_pipes) != 0) { + fprintf(stderr, "[Error] (ioemu) " + "Failed to create pipes.\n"); + exit(1); + } + log_pid = fork(); + if (log_pid == -1) + { + fprintf(stderr, "[Error] (ioemu) " + "Failed to fork ioemu-logger.\n"); + exit(1); + } else if (log_pid == 0) { + close(log_pipes[1]); + dup2(log_pipes[0], 0); + close(log_pipes[0]); +#ifdef __x86_64__ + (void)execl("/usr/lib64/xen/bin/ioemu-logger", + "ioemu-logger", (char *)0); +#else /* __i386__ */ + (void)execl("/usr/lib/xen/bin/ioemu-logger", + "ioemu-logger", (char *)0); +#endif + fprintf(stderr, "[Error] (ioemu) " + "Failed to exec ioemu-logger.\n"); + _exit(1); + } + close(log_pipes[0]); + logfile = fdopen(log_pipes[1], "w"); + if (!logfile) + { + fprintf(stderr, "[Error] (ioemu) " + "Failed to associate stream with pipe.\n"); + exit(1); } #if !defined(CONFIG_SOFTMMU) /* must avoid mmap() usage of glibc by setting a buffer "by hand" */ diff -r 926a366ca82f tools/ioemu/vl.c --- a/tools/ioemu/vl.c Fri Jun 20 15:21:26 2008 +0100 +++ b/tools/ioemu/vl.c Wed Jun 25 17:26:20 2008 +0900 @@ -139,6 +139,8 @@ /* Max number of PCI emulation */ #define MAX_PCI_EMULATION 32 + +#define MAX_RESTART_TIMES 3 const char *bios_dir = CONFIG_QEMU_SHAREDIR; void *ioport_opaque[MAX_IOPORTS]; @@ -214,6 +216,8 @@ int xc_handle; char domain_name[64] = "Xen-no-name"; extern int domid; + +int logger_status; PCI_EMULATION_INFO *PciEmulationInfoHead = NULL; @@ -7054,6 +7058,35 @@ int set_mm_mapping(int xc_handle, uint32 return 0; } +void sigchld_handle(int sid) +{ + static int restart_number = MAX_RESTART_TIMES; + + if (waitpid(log_pid, &logger_status, WNOHANG) == 0) + /* SIGCHLD hasn't come from ioemu-logger. */ + return; + + dup2(backup_stderr, 2); + if (logfile != NULL) + { + fclose(logfile); + logfile = NULL; + } + if (restart_number > 0) + { + fprintf(stderr, "[Warning] (ioemu) " + "Ioemu-logger is dead. Attempt to reboot ioemu-logger.\n"); + start_logger(); + } else { + fprintf(stderr, "[Error] (ioemu) " + "Failed to restart ioemu-logger %d times. " + "Log won't be recorded.\n", MAX_RESTART_TIMES); + logfile = fopen("/dev/null", "w"); + dup2(fileno(logfile), 1); + dup2(fileno(logfile), 2); + } + restart_number--; +} int main(int argc, char **argv) { @@ -7092,7 +7125,6 @@ int main(int argc, char **argv) struct rlimit rl; #endif sigset_t set; - char qemu_dm_logfilename[128]; const char *direct_pci = direct_pci_str; int nb_pci_emulation = 0; char pci_emulation_config_text[MAX_PCI_EMULATION][256]; @@ -7635,7 +7667,10 @@ int main(int argc, char **argv) } } - cpu_set_log(0); + signal(SIGCHLD, sigchld_handle); + signal(SIGPIPE, SIG_IGN); + backup_stderr = dup(2); + start_logger(); #ifndef NO_DAEMONIZE if (daemonize && !nographic && vnc_display == NULL && vncunused == 0) { @@ -8020,5 +8055,9 @@ int main(int argc, char **argv) main_loop(); quit_timers(); + signal(SIGCHLD, SIG_IGN); + fclose(logfile); + logfile = NULL; + waitpid(log_pid, &logger_status, 0); return 0; } diff -r 926a366ca82f tools/ioemu/vl.h --- a/tools/ioemu/vl.h Fri Jun 20 15:21:26 2008 +0100 +++ b/tools/ioemu/vl.h Wed Jun 25 17:26:21 2008 +0900 @@ -155,7 +155,8 @@ int set_mm_mapping(int xc_handle, uint32 unsigned int address_bits, unsigned long *extent_start); extern FILE *logfile; - +extern int log_pid; +extern int backup_stderr; #if (defined(__i386__) || defined(__x86_64__)) && !defined(QEMU_TOOL) #define MAPCACHE --------------030408080207070406050302 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel --------------030408080207070406050302--