Skip to contentSkip to conversation

Error reporting capabilities for libxc

PATCH Daniel P. Berrange 5 participants 7 messages Sep 22, 2006 – Sep 26, 2006 Arm/actions semi architecture

lore
Share & tools
lore
#0 lore raw

Currently APIs in the libxc library have a very limited means for reporting errors. They basically return -1 / NULL on error and set errno. Unfortunately since most of the errors which can occur in libxc are not related to system calls failing, the information available via errno is essentially useless. Indeed libxc simply sets errno to -EINVAL in most cases.

The irritating thing is that libxc *does* actually know exactly what went wrong in all these cases & if you compile with -DDEBUG will report this information on STDERR. This is not much use for applications using libxc since they can hardly trap & parse STDERR to find out what went wrong!

As an illustration of why better error handling is needed, consider the frequently encountered problem of trying to create a non-PAE guest on a PAE hypervisor:

  # xm create demo
  Using config file "demo".
  Error: (22, 'Invalid argument')

This makes it essentially impossible to debug any errors with domain creation, especially if -DDEBUG was not defined in your build of libxc

So, I've prototyped a simple error reporting mechanism for libxc. The idea is we first define an enum for the broad classes of errors which can occur. As proof of concept I've defined one generic error code, and a special one for invalid kernels

typedef enum {
XC_ERROR_NONE = 0,
XC_INTERNAL_ERROR = 1,
XC_INVALID_KERNEL = 2,
} xc_error_code;

Next, I created an internal function for setting the error code, and providing an associated descritive message

  void xc_set_error(int code, const char *fmt, ...);

And simply re-defined the existing ERROR & PERROR macros to use this function passing the generic XC_INTERNAL_ERROR

#define ERROR(_m, _a...) xc_set_error(XC_INTERNAL_ERROR, _m, ## _a)

#define PERROR(_m, _a...) xc_set_error(XC_INTERNAL_ERROR, "%s (%d = %s)", _m, errno, strerror(errno))

In the various places which deal with validating the guest kernel I changed calls to ERROR to call xc_set_error(XC_INVALID_KERNEL,...) instead. We can define further error codes over time to reduce use fo the genreic XC_INTERNAL_ERROR as desired.

In the public API defined a way to either fetch the last error, or register a callback for receving errors:

  int xc_get_last_error_code(void);
  const char *xc_get_last_error_message(void);
  void xc_clear_last_error(void);
  const char *xc_error_code_to_desc(int code);

  typedef void (*xc_error_handler)(int code, const char *msg);
  void xc_default_error_handler(int code, const char *msg);
  xc_error_handler xc_set_error_handler(xc_error_handler handler);

In the python binding I replaced all use of PyErr_SetFromErrno with a helper which fetches the info from xc_get_last_error_code/message.

Its important to note that in all this work I did not change the contract of *any* public method in the C library for libxc. The return codes are still the same, errno is still set in various places. If you compile with -DDEBUG a error handler callback is set to print to STDERR. So any existing users will continue to get exactly same behaviour as before. They do now have the ability to opt-in to getting full error details if they so desire.

The python bindings are also unchanged with the exception that the error thrown contains the xc_error_code & description instead of the errno value. Since the latter was essentially useless I don't think that's a huge problem, but we could probably figure out a way to maintain compat at the python level if absolutely required.

Any way, the upshot of all this work:

- Try running a 32-bit PV kernel on 64-bit host:

  # xm create error
  Using config file "error".
  Error: [2, 'Kernel ELF architecture 3 does not match Xen architecture 62']

- Try running a non-PAE kernel on a PAE host:

  # xm create demo
  Using config file "demo".
  Error: [2, 'Non PAE-kernel on PAE host.']

- Try using libgtk as your kernel:

  # xm create error
  Using config file "error".
  Error: [2, 'Kernel ELF type 3 does not match Xen type 2']

Any way there's some more cleanup to do & some error messages to improve, and switch more errors to use an explicit error code rather than the generic XC_INTERNAL_ERROR, but I thought I'd post the patch for review before going too much further.

Regards,
Dan.

Signature · 4 lines
|=- Red Hat, Engineering, Emerging Technologies, Boston.  +1 978 392 2496 -=|
|=-           Perl modules: http://search.cpan.org/~danberr/              -=|
|=-               Projects: http://freshmeat.net/~danielpb/               -=|
|=-  GnuPG: 7D3B9505   F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505  -=| 
tools/libxc/xc_hvm_build.c +10 -6
diff -ru xen-unstable-11405.orig/tools/libxc/xc_hvm_build.c xen-unstable-11405.new/tools/libxc/xc_hvm_build.c
@@ -460,7 +460,6 @@
                      ctxt, domctl.u.getdomaininfo.shared_info_frame,
                      vcpus, pae, acpi, apic, store_evtchn, store_mfn) < 0)
     {
-        ERROR("Error constructing guest OS");
         goto error_out;
     }
 
@@ -529,26 +528,30 @@
 
     if ( !IS_ELF(*ehdr) )
     {
-        ERROR("Kernel image does not have an ELF header.");
+        xc_set_error(XC_INVALID_KERNEL,
+                     "Kernel image does not have an ELF header.");
         return -EINVAL;
     }
 
     if ( (ehdr->e_phoff + (ehdr->e_phnum * ehdr->e_phentsize)) > elfsize )
     {
-        ERROR("ELF program headers extend beyond end of image.");
+        xc_set_error(XC_INVALID_KERNEL,
+                     "ELF program headers extend beyond end of image.");
         return -EINVAL;
     }
 
     if ( (ehdr->e_shoff + (ehdr->e_shnum * ehdr->e_shentsize)) > elfsize )
     {
-        ERROR("ELF section headers extend beyond end of image.");
+        xc_set_error(XC_INVALID_KERNEL,
+                     "ELF section headers extend beyond end of image.");
         return -EINVAL;
     }
 
     /* Find the section-header strings table. */
     if ( ehdr->e_shstrndx == SHN_UNDEF )
     {
-        ERROR("ELF image has no section-header strings table (shstrtab).");
+        xc_set_error(XC_INVALID_KERNEL,
+                     "ELF image has no section-header strings table (shstrtab).");
         return -EINVAL;
     }
     shdr = (Elf32_Shdr *)(elfbase + ehdr->e_shoff +
@@ -570,7 +573,8 @@
          (ehdr->e_entry < kernstart) ||
          (ehdr->e_entry > kernend) )
     {
-        ERROR("Malformed ELF image.");
+        xc_set_error(XC_INVALID_KERNEL,
+                     "Malformed ELF image.");
         return -EINVAL;
     }
 
tools/libxc/xc_linux_build.c +6 -5
diff -ru xen-unstable-11405.orig/tools/libxc/xc_linux_build.c xen-unstable-11405.new/tools/libxc/xc_linux_build.c
@@ -121,7 +121,6 @@
     if ( probe_elf(image, image_size, load_funcs) &&
          probe_bin(image, image_size, load_funcs) )
     {
-        ERROR( "Unrecognized image format" );
         return -EINVAL;
     }
 
@@ -611,17 +610,20 @@
     xen_capabilities_info_t xen_caps = "";
 
     if (xc_version(xc_handle, XENVER_capabilities, &xen_caps) != 0) {
-        ERROR("Cannot determine host capabilities.");
+        xc_set_error(XC_INVALID_KERNEL,
+                     "Cannot determine host capabilities.");
         return 0;
     }
 
     if (strstr(xen_caps, "xen-3.0-x86_32p")) {
         if (dsi->pae_kernel == PAEKERN_no) {
-            ERROR("Non PAE-kernel on PAE host.");
+            xc_set_error(XC_INVALID_KERNEL,
+                         "Non PAE-kernel on PAE host.");
             return 0;
         }
     } else if (dsi->pae_kernel != PAEKERN_no) {
-        ERROR("PAE-kernel on non-PAE host.");
+        xc_set_error(XC_INVALID_KERNEL,
+                     "PAE-kernel on non-PAE host.");
         return 0;
     }
 
@@ -1167,7 +1169,6 @@
                      console_evtchn, console_mfn,
                      features_bitmap) < 0 )
     {
-        ERROR("Error constructing guest OS");
         goto error_out;
     }
 
tools/libxc/xc_linux.c +76 -0

82 lines not on this page · raw message

tools/libxc/xc_load_elf.c +50 -19

136 lines not on this page · raw message

tools/libxc/xc_private.h +7 -19
diff -ru xen-unstable-11405.orig/tools/libxc/xc_private.h xen-unstable-11405.new/tools/libxc/xc_private.h
@@ -56,25 +56,13 @@
 #define PPRINTF(_f, _a...)
 #endif
 
-#define ERR(_f, _a...) do {                     \
-    DPRINTF(_f ": %d\n" , ## _a, errno);        \
-    fflush(stderr); }                           \
-while (0)
-
-#define ERROR(_m, _a...)                        \
-do {                                            \
-    int __saved_errno = errno;                  \
-    DPRINTF("ERROR: " _m "\n" , ## _a );        \
-    errno = __saved_errno;                      \
-} while (0)
-
-#define PERROR(_m, _a...)                               \
-do {                                                    \
-    int __saved_errno = errno;                          \
-    DPRINTF("ERROR: " _m " (%d = %s)\n" , ## _a ,       \
-            __saved_errno, strerror(__saved_errno));    \
-    errno = __saved_errno;                              \
-} while (0)
+void xc_set_error(int code, const char *fmt, ...);
+
+#define ERR(_f, _a...) ERROR(_f, ## _a)
+
+#define ERROR(_m, _a...) xc_set_error(XC_INTERNAL_ERROR, _m, ## _a)
+
+#define PERROR(_m, _a...) xc_set_error(XC_INTERNAL_ERROR, "%s (%d = %s)", _m, errno, strerror(errno))
 
 static inline void safe_munlock(const void *addr, size_t len)
 {
tools/libxc/xenctrl.h +39 -0
diff -ru xen-unstable-11405.orig/tools/libxc/xenctrl.h xen-unstable-11405.new/tools/libxc/xenctrl.h
@@ -646,4 +646,43 @@
  */
 int xc_evtchn_unmask(int xce_handle, evtchn_port_t port);
 
+
+typedef enum {
+  XC_ERROR_NONE = 0,
+  XC_INTERNAL_ERROR = 1,
+  XC_INVALID_KERNEL = 2,
+} xc_error_code;
+
+/*
+ * Return the code of the last error to occurr
+ */
+int xc_get_last_error_code(void);
+
+/*
+ * Return the message associated with the last error to occur
+ */
+const char *xc_get_last_error_message(void);
+
+/*
+ * Clear the last error
+ */
+void xc_clear_last_error(void);
+
+typedef void (*xc_error_handler)(int code, const char *msg);
+
+/*
+ * The default error handler which prints to stderr
+ */
+void xc_default_error_handler(int code, const char *msg);
+
+/*
+ * Convert an error code into a text description
+ */
+const char *xc_error_code_to_desc(int code);
+
+/*
+ * Registers a callback to handle errors
+ */
+xc_error_handler xc_set_error_handler(xc_error_handler handler);
+
 #endif
tools/python/xen/lowlevel/xc/xc.c +51 -38

287 lines not on this page · raw message

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#1 lore raw

Hi,

Quoted 2 lines · Daniel P. Berrange

So, I've prototyped a simple error reporting mechanism for libxc. The idea is we first define an enum for the broad classes of errors which can occur.

...

Quoted 4 lines

Any way, the upshot of all this work:

  # xm create error
  Using config file "error".
  Error: [2, 'Kernel ELF architecture 3 does not match Xen architecture 62']

IMHO this is sorely needed. Any comments from XenSource people?

--Stephen

#2 lore raw
Quoted 10 lines · Stephen C. Tweedie
Quoted 2 lines

So, I've prototyped a simple error reporting mechanism for libxc. The idea is we first define an enum for the broad classes of errors which can occur.

...

Quoted 4 lines

Any way, the upshot of all this work:

  # xm create error
  Using config file "error".
  Error: [2, 'Kernel ELF architecture 3 does not match Xen architecture 62']

IMHO this is sorely needed. Any comments from XenSource people?

I'd agree something like this is necessary in the 3.0.4 timeframe. I'll let one of the guys more acquainted with xend comment in detail. There's also the question of how this will integrate with the proposed 'XenAPI'.

-- Keir

#3 lore raw
Quoted 14 lines · Keir Fraser
Quoted 9 lines · Stephen C. Tweedie
Quoted 2 lines

So, I've prototyped a simple error reporting mechanism for libxc. The idea is we first define an enum for the broad classes of errors which can occur.

...

Quoted 4 lines

Any way, the upshot of all this work:

  # xm create error
  Using config file "error".
  Error: [2, 'Kernel ELF architecture 3 does not match Xen architecture 62']
Quoted 5 lines
Quoted 1 line

IMHO this is sorely needed. Any comments from XenSource people?

I'd agree something like this is necessary in the 3.0.4 timeframe. I'll let one of the guys more acquainted with xend comment in detail. There's also the question of how this will integrate with the proposed 'XenAPI'.

I don't anticipate any problems integrating with XenAPI - we've previously discussed the need to enumerate a set of error codes for XenAPI operations and the need to also pass back descriptive string of the problem so I should think its a good fit

Regards,
Dan.

Signature · 4 lines
|=- Red Hat, Engineering, Emerging Technologies, Boston.  +1 978 392 2496 -=|
|=-           Perl modules: http://search.cpan.org/~danberr/              -=|
|=-               Projects: http://freshmeat.net/~danielpb/               -=|
|=-  GnuPG: 7D3B9505   F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505  -=| 
#4 lore raw
In reply to Daniel P. Berrange
Quoted 15 lines · Daniel P. Berrange

Currently APIs in the libxc library have a very limited means for reporting errors. They basically return -1 / NULL on error and set errno. Unfortunately since most of the errors which can occur in libxc are not related to system calls failing, the information available via errno is essentially useless. Indeed libxc simply sets errno to -EINVAL in most cases.

The irritating thing is that libxc *does* actually know exactly what went wrong in all these cases & if you compile with -DDEBUG will report this information on STDERR. This is not much use for applications using libxc since they can hardly trap & parse STDERR to find out what went wrong!

As an illustration of why better error handling is needed, consider the frequently encountered problem of trying to create a non-PAE guest on a PAE hypervisor:

Quoted 6 lines
  # xm create demo
  Using config file "demo".
  Error: (22, 'Invalid argument')

This makes it essentially impossible to debug any errors with domain creation, especially if -DDEBUG was not defined in your build of libxc

I agree; this is a serious problem that I suspect we've all run into at least once.

Quoted 31 lines

So, I've prototyped a simple error reporting mechanism for libxc. The idea is we first define an enum for the broad classes of errors which can occur. As proof of concept I've defined one generic error code, and a special one for invalid kernels

typedef enum {
XC_ERROR_NONE = 0,
XC_INTERNAL_ERROR = 1,
XC_INVALID_KERNEL = 2,
} xc_error_code;

Next, I created an internal function for setting the error code, and providing an associated descritive message

  void xc_set_error(int code, const char *fmt, ...);

And simply re-defined the existing ERROR & PERROR macros to use this function passing the generic XC_INTERNAL_ERROR

#define ERROR(_m, _a...) xc_set_error(XC_INTERNAL_ERROR, _m, ## _a)

#define PERROR(_m, _a...) xc_set_error(XC_INTERNAL_ERROR, "%s (%d = %s)", _m, errno, strerror(errno))

In the various places which deal with validating the guest kernel I changed calls to ERROR to call xc_set_error(XC_INVALID_KERNEL,...) instead. We can define further error codes over time to reduce use fo the genreic XC_INTERNAL_ERROR as desired.

In the public API defined a way to either fetch the last error, or register a callback for receving errors:

Quoted 8 lines
  int xc_get_last_error_code(void);
  const char *xc_get_last_error_message(void);
  void xc_clear_last_error(void);
  const char *xc_error_code_to_desc(int code);

  typedef void (*xc_error_handler)(int code, const char *msg);
  void xc_default_error_handler(int code, const char *msg);
  xc_error_handler xc_set_error_handler(xc_error_handler handler);

Isn't the whole idea of "get_last_error" racy by design? I guess errno has the same problem, but I'm not quite clear how it's solved there (a magic array of errnos that is somehow sized to include all threads?)

Quoted 4 lines

Any way there's some more cleanup to do & some error messages to improve, and switch more errors to use an explicit error code rather than the generic XC_INTERNAL_ERROR, but I thought I'd post the patch for review before going too much further.

Thanks for sending this out! It's really helpful to see the design early.

Signature · 2 lines
Hollis Blanchard
IBM Linux Technology Center
#5 lore raw
Quoted 37 lines · Hollis Blanchard
Quoted 36 lines · Daniel P. Berrange

So, I've prototyped a simple error reporting mechanism for libxc. The idea is we first define an enum for the broad classes of errors which can occur. As proof of concept I've defined one generic error code, and a special one for invalid kernels

typedef enum {
XC_ERROR_NONE = 0,
XC_INTERNAL_ERROR = 1,
XC_INVALID_KERNEL = 2,
} xc_error_code;

Next, I created an internal function for setting the error code, and providing an associated descritive message

  void xc_set_error(int code, const char *fmt, ...);

And simply re-defined the existing ERROR & PERROR macros to use this function passing the generic XC_INTERNAL_ERROR

#define ERROR(_m, _a...) xc_set_error(XC_INTERNAL_ERROR, _m, ## _a)

#define PERROR(_m, _a...) xc_set_error(XC_INTERNAL_ERROR, "%s (%d = %s)", _m, errno, strerror(errno))

In the various places which deal with validating the guest kernel I changed calls to ERROR to call xc_set_error(XC_INVALID_KERNEL,...) instead. We can define further error codes over time to reduce use fo the genreic XC_INTERNAL_ERROR as desired.

In the public API defined a way to either fetch the last error, or register a callback for receving errors:

  int xc_get_last_error_code(void);
  const char *xc_get_last_error_message(void);
  void xc_clear_last_error(void);
  const char *xc_error_code_to_desc(int code);
Quoted 7 lines
Quoted 3 lines
  typedef void (*xc_error_handler)(int code, const char *msg);
  void xc_default_error_handler(int code, const char *msg);
  xc_error_handler xc_set_error_handler(xc_error_handler handler);

Isn't the whole idea of "get_last_error" racy by design? I guess errno has the same problem, but I'm not quite clear how it's solved there (a magic array of errnos that is somehow sized to include all threads?)

Yeah, the libc errno is kept in thread local storage, so we'll have to do the same thing here to be thread-safe. The async callback function is of course always thread safe, but a little more inconvient for the caller to use so I wanted to allow the direct access too.

Regards,
Dan.

Signature · 4 lines
|=- Red Hat, Engineering, Emerging Technologies, Boston.  +1 978 392 2496 -=|
|=-           Perl modules: http://search.cpan.org/~danberr/              -=|
|=-               Projects: http://freshmeat.net/~danielpb/               -=|
|=-  GnuPG: 7D3B9505   F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505  -=| 
#6 lore raw
In reply to Hollis Blanchard
Quoted 3 lines · Hollis Blanchard

Isn't the whole idea of "get_last_error" racy by design? I guess errno has the same problem, but I'm not quite clear how it's solved there (a magic array of errnos that is somehow sized to include all threads?)

#define errno (*__errno_location())

I think recent glibc's stash each thread's errno in the thread's local storage rather than using a global array, but haven't checked.

Cheers,
Muli