direct-io.hg

view tools/libxc/xc_tbuf.c @ 12765:2dd4569e0640

[LIBXC] Add an error reporting API to the libxc library.

- An 'xc_error' struct is used to pass around error
details. Currently contains two members 'code' an enumeration of
error types, and 'message' a free text description of the specific
problem.

- The xc_get_last_error() method returns a const pointer to the
internal instance of this struct manged by libxc. By returning a
const pointer we can add extra members to the end of the struct at
any time without worrying about ABI of callers. This will let us
provide more fine-grained info if needed in the future.

- The xc_error instance is statically defined inside libxc and marked
__thread. This ensures that errors are recorded per-thread, and
that when dealing with errors we never need to call malloc - all
storage needed is statically allocated.

- The xc_clear_last_error() method resets any currently recorded
error details

- The xc_error_code_to_desc() method converts the integer error code
into a generic user facing messsage. eg "Invalid kernel". Together
with the 'message' field from xc_error, this provides the user
visible feedback. eg "Invalid kernel: Non PAE-kernel on PAE host."

- A callback can be registered with xc_set_error_handler to receive
notification whenever an error is recorded, rather than querying
for error details after the fact with xc_get_last_error

- If built with -DDEBUG set, a default error handler will be
registered which calls fprintf(stderr), thus maintaining current
behaviour of logging errors to stderr during developer builds.

- The python binding for libxc is updated to use xc_get_last_error
to pull out error details whenever appropriate, instead of
returning info based on 'errno'

- The xc_set_error method is private to libxc internals, and is used
for setting error details

- The ERROR and PERROR macros have been updated to call xc_set_error
automatically specifying XC_INTERNAL_ERROR as the error code. This
gives a generic error report for all current failure points

- Some uses of the ERROR macro have been replaced with explicit
calls to xc_set_error to enable finer grained error reporting. In
particular the code dealing with invalid kernel types uses this
to report about PAE/architecture/wordsize mismatches

The patch has been tested by calling xm create against a varietry of
config files defining invalid kernels of various kinds. It has also
been tested with libvirt talking to xend. In both cases the error
messages were propagated all the way back up the stack.

There is only one place where I need to do further work. The suspend
& restore APIs in Xend invoke external helper programs rather than
calling libxc directly. This means that error details are essentially
lost. Since there is already code in XenD which scans STDERR from
these programs I will investigate adapting this to extract actual
error messages from these helpers.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
author kfraser@localhost.localdomain
date Thu Dec 07 11:36:26 2006 +0000 (2006-12-07)
parents 58f28e00001e
children 1a45828882e9
line source
1 /******************************************************************************
2 * xc_tbuf.c
3 *
4 * API for manipulating and accessing trace buffer parameters
5 *
6 * Copyright (c) 2005, Rob Gardner
7 *
8 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
9 * Use is subject to license terms.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation, version 2 of the
14 * License.
15 */
17 #include "xc_private.h"
19 static int tbuf_enable(int xc_handle, int enable)
20 {
21 DECLARE_SYSCTL;
23 sysctl.cmd = XEN_SYSCTL_tbuf_op;
24 sysctl.interface_version = XEN_SYSCTL_INTERFACE_VERSION;
25 if (enable)
26 sysctl.u.tbuf_op.cmd = XEN_SYSCTL_TBUFOP_enable;
27 else
28 sysctl.u.tbuf_op.cmd = XEN_SYSCTL_TBUFOP_disable;
30 return xc_sysctl(xc_handle, &sysctl);
31 }
33 int xc_tbuf_set_size(int xc_handle, unsigned long size)
34 {
35 DECLARE_SYSCTL;
37 sysctl.cmd = XEN_SYSCTL_tbuf_op;
38 sysctl.interface_version = XEN_SYSCTL_INTERFACE_VERSION;
39 sysctl.u.tbuf_op.cmd = XEN_SYSCTL_TBUFOP_set_size;
40 sysctl.u.tbuf_op.size = size;
42 return xc_sysctl(xc_handle, &sysctl);
43 }
45 int xc_tbuf_get_size(int xc_handle, unsigned long *size)
46 {
47 int rc;
48 DECLARE_SYSCTL;
50 sysctl.cmd = XEN_SYSCTL_tbuf_op;
51 sysctl.interface_version = XEN_SYSCTL_INTERFACE_VERSION;
52 sysctl.u.tbuf_op.cmd = XEN_SYSCTL_TBUFOP_get_info;
54 rc = xc_sysctl(xc_handle, &sysctl);
55 if (rc == 0)
56 *size = sysctl.u.tbuf_op.size;
57 return rc;
58 }
60 int xc_tbuf_enable(int xc_handle, unsigned long pages, unsigned long *mfn,
61 unsigned long *size)
62 {
63 DECLARE_SYSCTL;
64 int rc;
66 /*
67 * Ignore errors (at least for now) as we get an error if size is already
68 * set (since trace buffers cannot be reallocated). If we really have no
69 * buffers at all then tbuf_enable() will fail, so this is safe.
70 */
71 (void)xc_tbuf_set_size(xc_handle, pages);
73 if ( tbuf_enable(xc_handle, 1) != 0 )
74 return -1;
76 sysctl.cmd = XEN_SYSCTL_tbuf_op;
77 sysctl.interface_version = XEN_SYSCTL_INTERFACE_VERSION;
78 sysctl.u.tbuf_op.cmd = XEN_SYSCTL_TBUFOP_get_info;
80 rc = xc_sysctl(xc_handle, &sysctl);
81 if ( rc == 0 )
82 {
83 *size = sysctl.u.tbuf_op.size;
84 *mfn = sysctl.u.tbuf_op.buffer_mfn;
85 }
87 return 0;
88 }
90 int xc_tbuf_disable(int xc_handle)
91 {
92 return tbuf_enable(xc_handle, 0);
93 }
95 int xc_tbuf_set_cpu_mask(int xc_handle, uint32_t mask)
96 {
97 DECLARE_SYSCTL;
98 int ret = -1;
100 sysctl.cmd = XEN_SYSCTL_tbuf_op;
101 sysctl.interface_version = XEN_SYSCTL_INTERFACE_VERSION;
102 sysctl.u.tbuf_op.cmd = XEN_SYSCTL_TBUFOP_set_cpu_mask;
104 set_xen_guest_handle(sysctl.u.tbuf_op.cpu_mask.bitmap, (uint8_t *)&mask);
105 sysctl.u.tbuf_op.cpu_mask.nr_cpus = sizeof(mask) * 8;
107 if ( lock_pages(&mask, sizeof(mask)) != 0 )
108 {
109 PERROR("Could not lock memory for Xen hypercall");
110 goto out;
111 }
113 ret = do_sysctl(xc_handle, &sysctl);
115 unlock_pages(&mask, sizeof(mask));
117 out:
118 return ret;
119 }
121 int xc_tbuf_set_evt_mask(int xc_handle, uint32_t mask)
122 {
123 DECLARE_SYSCTL;
125 sysctl.cmd = XEN_SYSCTL_tbuf_op;
126 sysctl.interface_version = XEN_SYSCTL_INTERFACE_VERSION;
127 sysctl.u.tbuf_op.cmd = XEN_SYSCTL_TBUFOP_set_evt_mask;
128 sysctl.u.tbuf_op.evt_mask = mask;
130 return do_sysctl(xc_handle, &sysctl);
131 }