[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-changelog] [xen-unstable] libelf: Tidy up logging and remove dependency on stdio.



# HG changeset patch
# User Keir Fraser <keir.fraser@xxxxxxxxxx>
# Date 1275035260 -3600
# Node ID 2a9bb324e645a72f1f3bd5b99f2b109b22cb6e1d
# Parent  ff10a1fdc83e1361563df2dd9c7679cc0f6cb912
libelf: Tidy up logging and remove dependency on stdio.

libelf now permits callers to specify logging callback functions,
rather than a FILE*.  libelf's non-Xen callers are all libxc users, so
the stdio dependency and the default logging callback function (which
calls vfprintf) is now in libxc.

Xen's use of libxc is unaffected in this patch.

Signed-off-by: Ian Jackson <Ian.Jackson@xxxxxxxxxxxxx>
---
 tools/libxc/xc_dom_elfloader.c      |   17 +++++++++++++++--
 tools/libxc/xenctrl.h               |    5 +++++
 tools/xcutils/readnotes.c           |    2 +-
 xen/common/libelf/libelf-loader.c   |   24 +++++++++++++++++++++---
 xen/common/libelf/libelf-private.h  |   15 ++++++++-------
 xen/common/libelf/libelf-relocate.c |    2 +-
 xen/include/xen/libelf.h            |   13 +++++++++++--
 7 files changed, 62 insertions(+), 16 deletions(-)

diff -r ff10a1fdc83e -r 2a9bb324e645 tools/libxc/xc_dom_elfloader.c
--- a/tools/libxc/xc_dom_elfloader.c    Fri May 28 09:26:51 2010 +0100
+++ b/tools/libxc/xc_dom_elfloader.c    Fri May 28 09:27:40 2010 +0100
@@ -9,12 +9,24 @@
  */
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdarg.h>
 #include <inttypes.h>
 
 #include "xg_private.h"
 #include "xc_dom.h"
 
 #define XEN_VER "xen-3.0"
+
+/* ------------------------------------------------------------------------ */
+
+static void log_callback(struct elf_binary *elf, void *caller_data,
+                         int iserr, const char *fmt, va_list al) {
+    vfprintf(caller_data,fmt,al);
+}
+
+void xc_elf_set_logfile(struct elf_binary *elf, FILE *f, int verbose) {
+    elf_set_log(elf, log_callback, f, verbose);
+}
 
 /* ------------------------------------------------------------------------ */
 
@@ -137,8 +149,9 @@ static int xc_dom_load_elf_symtab(struct
     }
     if ( elf_init(&syms, hdr + sizeof(int), size - sizeof(int)) )
         return -1;
+
     if ( xc_dom_logfile )
-        elf_set_logfile(&syms, xc_dom_logfile, 1);
+        xc_elf_set_logfile(&syms, xc_dom_logfile, 1);
 
     symtab = dom->bsd_symtab_start + sizeof(int);
     maxaddr = elf_round_up(&syms, symtab + elf_size(&syms, syms.ehdr) +
@@ -231,7 +244,7 @@ static int xc_dom_parse_elf_kernel(struc
     dom->private_loader = elf;
     rc = elf_init(elf, dom->kernel_blob, dom->kernel_size);
     if ( xc_dom_logfile )
-        elf_set_logfile(elf, xc_dom_logfile, 1);
+        xc_elf_set_logfile(elf, xc_dom_logfile, 1);
     if ( rc != 0 )
     {
         xc_dom_panic(XC_INVALID_KERNEL, "%s: corrupted ELF image\n",
diff -r ff10a1fdc83e -r 2a9bb324e645 tools/libxc/xenctrl.h
--- a/tools/libxc/xenctrl.h     Fri May 28 09:26:51 2010 +0100
+++ b/tools/libxc/xenctrl.h     Fri May 28 09:27:40 2010 +0100
@@ -19,6 +19,7 @@
 
 #include <stddef.h>
 #include <stdint.h>
+#include <stdio.h>
 #include <xen/xen.h>
 #include <xen/domctl.h>
 #include <xen/physdev.h>
@@ -1474,4 +1475,8 @@ int xc_memshr_debug_gref(int xc_handle,
                          uint32_t domid,
                          grant_ref_t gref);
 
+struct elf_binary;
+void xc_elf_set_logfile(struct elf_binary *elf, FILE *f, int verbose);
+/* Useful for callers who also use libelf. */
+
 #endif /* XENCTRL_H */
diff -r ff10a1fdc83e -r 2a9bb324e645 tools/xcutils/readnotes.c
--- a/tools/xcutils/readnotes.c Fri May 28 09:26:51 2010 +0100
+++ b/tools/xcutils/readnotes.c Fri May 28 09:27:40 2010 +0100
@@ -170,7 +170,7 @@ int main(int argc, char **argv)
                fprintf(stderr, "File %s is not an ELF image\n", f);
                return 1;
        }
-       elf_set_logfile(&elf, stderr, 0);
+       xc_elf_set_logfile(&elf, stderr, 0);
 
        count = elf_phdr_count(&elf);
        for ( h=0; h < count; h++)
diff -r ff10a1fdc83e -r 2a9bb324e645 xen/common/libelf/libelf-loader.c
--- a/xen/common/libelf/libelf-loader.c Fri May 28 09:26:51 2010 +0100
+++ b/xen/common/libelf/libelf-loader.c Fri May 28 09:27:40 2010 +0100
@@ -1,6 +1,8 @@
 /*
  * parse and load elf binaries
  */
+
+#include <stdarg.h>
 
 #include "libelf-private.h"
 
@@ -72,9 +74,25 @@ int elf_init(struct elf_binary *elf, con
 }
 
 #ifndef __XEN__
-void elf_set_logfile(struct elf_binary *elf, FILE * log, int verbose)
-{
-    elf->log = log;
+void elf_call_log_callback(struct elf_binary *elf, int iserr,
+                           const char *fmt,...) {
+    va_list al;
+
+    if (!elf->log_callback)
+        return;
+    if (!(iserr || elf->verbose))
+        return;
+
+    va_start(al,fmt);
+    elf->log_callback(elf, elf->log_caller_data, iserr, fmt, al);
+    va_end(al);
+}
+    
+void elf_set_log(struct elf_binary *elf, elf_log_callback *log_callback,
+                 void *log_caller_data, int verbose)
+{
+    elf->log_callback = log_callback;
+    elf->log_caller_data = log_caller_data;
     elf->verbose = verbose;
 }
 #else
diff -r ff10a1fdc83e -r 2a9bb324e645 xen/common/libelf/libelf-private.h
--- a/xen/common/libelf/libelf-private.h        Fri May 28 09:26:51 2010 +0100
+++ b/xen/common/libelf/libelf-private.h        Fri May 28 09:27:40 2010 +0100
@@ -11,6 +11,8 @@
 #include <asm/byteorder.h>
 #include <public/elfnote.h>
 
+/* we would like to use elf->log_callback but we can't because
+ * there is no vprintk in Xen */
 #define elf_msg(elf, fmt, args ... ) \
    if (elf->verbose) printk(fmt, ## args )
 #define elf_err(elf, fmt, args ... ) \
@@ -54,13 +56,12 @@
 #include "xenctrl.h"
 #include "xc_private.h"
 
-#define elf_msg(elf, fmt, args ... ) \
-    if (elf->log && elf->verbose) fprintf(elf->log, fmt , ## args )
-#define elf_err(elf, fmt, args ... ) do {               \
-    if (elf->log)                                       \
-        fprintf(elf->log, fmt , ## args );              \
-    xc_set_error(XC_INVALID_KERNEL, fmt , ## args );    \
-} while (0)
+#define elf_msg(elf, fmt, args ... )                    \
+    elf_call_log_callback(elf, 0, fmt , ## args );
+#define elf_err(elf, fmt, args ... )                    \
+    elf_call_log_callback(elf, 1, fmt , ## args );
+
+void elf_call_log_callback(struct elf_binary*, int iserr, const char *fmt,...);
 
 #define safe_strcpy(d,s)                        \
 do { strncpy((d),(s),sizeof((d))-1);            \
diff -r ff10a1fdc83e -r 2a9bb324e645 xen/common/libelf/libelf-relocate.c
--- a/xen/common/libelf/libelf-relocate.c       Fri May 28 09:26:51 2010 +0100
+++ b/xen/common/libelf/libelf-relocate.c       Fri May 28 09:27:40 2010 +0100
@@ -289,7 +289,7 @@ static int elf_reloc_section(struct elf_
         value = elf_uval(elf, sym, st_value);
         value += r_addend;
 
-        if ( elf->log && (elf->verbose > 1) )
+        if ( elf->log_callback && (elf->verbose > 1) )
         {
             uint64_t st_name = elf_uval(elf, sym, st_name);
             const char *name = st_name ? elf->sym_strtab + st_name : "*NONE*";
diff -r ff10a1fdc83e -r 2a9bb324e645 xen/include/xen/libelf.h
--- a/xen/include/xen/libelf.h  Fri May 28 09:26:51 2010 +0100
+++ b/xen/include/xen/libelf.h  Fri May 28 09:27:40 2010 +0100
@@ -37,6 +37,13 @@
 #else
 #include <xen/elfnote.h>
 #include <xen/features.h>
+
+#include <stdarg.h>
+
+struct elf_binary;
+typedef void elf_log_callback(struct elf_binary*, void *caller_data,
+                              int iserr, const char *fmt, va_list al);
+
 #endif
 
 /* ------------------------------------------------------------------------ */
@@ -99,7 +106,8 @@ struct elf_binary {
 
 #ifndef __XEN__
     /* misc */
-    FILE *log;
+    elf_log_callback *log_callback;
+    void *log_caller_data;
 #endif
     int verbose;
 };
@@ -183,7 +191,8 @@ int elf_init(struct elf_binary *elf, con
 #ifdef __XEN__
 void elf_set_verbose(struct elf_binary *elf);
 #else
-void elf_set_logfile(struct elf_binary *elf, FILE * log, int verbose);
+void elf_set_log(struct elf_binary *elf, elf_log_callback*,
+                 void *log_caller_pointer, int verbose);
 #endif
 
 void elf_parse_binary(struct elf_binary *elf);

_______________________________________________
Xen-changelog mailing list
Xen-changelog@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-changelog


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.