Skip to contentSkip to conversation

xl: Add subcommand 'xl dmesg'

PATCH Yu Zhiguo 3 participants 6 messages May 20, 2010 – May 25, 2010

lore
Share & tools
lore

Yu Zhiguo

author
#0 lore raw

Can be used to read and/or clear dmesg buffer.

tools/libxl/libxl.c +12 -0
diff -r 840f269d95fb -r ab11e3eab7cc tools/libxl/libxl.c
@@ -2827,6 +2827,18 @@
     return xc_send_debug_keys(ctx->xch, keys);
 }
 
+int libxl_readconsolering(struct libxl_ctx *ctx, char **pbuffer,
+                          unsigned int *pnr_chars, int clear,
+                          int incremental, uint32_t *pindex)
+{
+    int ret;
+
+    ret = xc_readconsolering(ctx->xch, pbuffer, pnr_chars, clear,
+                             incremental, pindex);
+
+    return ret;
+}
+
 uint32_t libxl_vm_get_start_time(struct libxl_ctx *ctx, uint32_t domid)
 {
     char *dompath = libxl_xs_get_dompath(ctx, domid);
tools/libxl/libxl.h +3 -0
diff -r 840f269d95fb -r ab11e3eab7cc tools/libxl/libxl.h
@@ -512,6 +512,9 @@
                        char *trigger_name, uint32_t vcpuid);
 int libxl_send_sysrq(struct libxl_ctx *ctx, uint32_t domid, char sysrq);
 int libxl_send_debug_keys(struct libxl_ctx *ctx, char *keys);
+int libxl_readconsolering(struct libxl_ctx *ctx, char **pbuffer,
+                          unsigned int *pnr_chars, int clear,
+                          int incremental, uint32_t *pindex);
 uint32_t libxl_vm_get_start_time(struct libxl_ctx *ctx, uint32_t domid);
 
 char *libxl_tmem_list(struct libxl_ctx *ctx, uint32_t domid, int use_long);
tools/libxl/xl_cmdimpl.c +60 -0

66 lines not on this page · raw message

tools/libxl/xl_cmdimpl.h +1 -0
diff -r 840f269d95fb -r ab11e3eab7cc tools/libxl/xl_cmdimpl.h
@@ -45,6 +45,7 @@
 int main_trigger(int argc, char **argv);
 int main_sysrq(int argc, char **argv);
 int main_debug_keys(int argc, char **argv);
+int main_dmesg(int argc, char **argv);
 int main_top(int argc, char **argv);
 int main_networkattach(int argc, char **argv);
 int main_networklist(int argc, char **argv);
tools/libxl/xl_cmdtable.c +6 -0
diff -r 840f269d95fb -r ab11e3eab7cc tools/libxl/xl_cmdtable.c
@@ -191,6 +191,12 @@
       "Send debug keys to Xen",
       "<Keys>",
     },
+    { "dmesg",
+      &main_dmesg,
+      "Read and/or clear dmesg buffer",
+      "[-c]",
+      "  -c                        Clear dmesg buffer as well as printing it",
+    },
     { "top",
       &main_top,
       "Monitor a host and the domains in real time",
#1 lore raw
Quoted 2 lines · Yu Zhiguo

Can be used to read and/or clear dmesg buffer.

Thanks for the patch!

The implementation is good, but I think we could probably offer an higher level API in libxenlight than just a wrapper around xc_readconsolering.
I think we should have a libxl_dmesg instead of libxl_readconsolering, and libxl_dmesg would print dmesg itself using the libxl logging functions.
The caller can setup the logging function so that dmesg is printed to stdout in this case.

Quoted 135 lines

diff -r 840f269d95fb -r ab11e3eab7cc tools/libxl/libxl.c

tools/libxl/libxl.c +12 -0
@@ -2827,6 +2827,18 @@
     return xc_send_debug_keys(ctx->xch, keys);
 }
 
+int libxl_readconsolering(struct libxl_ctx *ctx, char **pbuffer,
+                          unsigned int *pnr_chars, int clear,
+                          int incremental, uint32_t *pindex)
+{
+    int ret;
+
+    ret = xc_readconsolering(ctx->xch, pbuffer, pnr_chars, clear,
+                             incremental, pindex);
+
+    return ret;
+}
+
 uint32_t libxl_vm_get_start_time(struct libxl_ctx *ctx, uint32_t domid)
 {
     char *dompath = libxl_xs_get_dompath(ctx, domid);

diff -r 840f269d95fb -r ab11e3eab7cc tools/libxl/libxl.h

tools/libxl/libxl.h +3 -0
@@ -512,6 +512,9 @@
                        char *trigger_name, uint32_t vcpuid);
 int libxl_send_sysrq(struct libxl_ctx *ctx, uint32_t domid, char sysrq);
 int libxl_send_debug_keys(struct libxl_ctx *ctx, char *keys);
+int libxl_readconsolering(struct libxl_ctx *ctx, char **pbuffer,
+                          unsigned int *pnr_chars, int clear,
+                          int incremental, uint32_t *pindex);
 uint32_t libxl_vm_get_start_time(struct libxl_ctx *ctx, uint32_t domid);
 
 char *libxl_tmem_list(struct libxl_ctx *ctx, uint32_t domid, int use_long);

diff -r 840f269d95fb -r ab11e3eab7cc tools/libxl/xl_cmdimpl.c

tools/libxl/xl_cmdimpl.c +60 -0
@@ -3246,6 +3246,66 @@
     exit(0);
 }
 
+int main_dmesg(int argc, char **argv)
+{
+    unsigned int clear = 0, index = 0, incremental = 0;
+    unsigned int count = 16384 + 1, size = count;
+    char *str, *ptr;
+    int opt, ret;
+
+    while ((opt = getopt(argc, argv, "hc")) != -1) {
+        switch (opt) {
+        case 'c':
+            clear = 1;
+            break;
+        case 'h':
+            help("dmesg");
+            exit(0);
+        default:
+            fprintf(stderr, "option not supported\n");
+            break;
+        }
+    }
+
+    str = malloc(size);
+    memset(str, 0, size);
+    ret = libxl_readconsolering(&ctx, &str, &count, clear,
+                                incremental, &index);
+    if (ret < 0)
+        goto out;
+
+    while (!incremental && count == size) {
+        size += count - 1;
+        if (size < count)
+            break;
+
+        ptr = realloc(str, size);
+        if (!ptr)
+            break;
+
+        str = ptr + count;
+        count = size - count;
+        ret = libxl_readconsolering(&ctx, &str, &count, clear,
+                                    1, &index);
+        if (ret < 0) {
+            str = ptr;
+            break;
+        }
+
+        count += str - ptr;
+        str = ptr;
+    }
+
+out:
+    printf(str);
+    free(str);
+
+    if (ret)
+        exit(1);
+
+    exit(0);
+}
+
 int main_top(int argc, char **argv)
 {
     int opt;

diff -r 840f269d95fb -r ab11e3eab7cc tools/libxl/xl_cmdimpl.h

tools/libxl/xl_cmdimpl.h +1 -0
@@ -45,6 +45,7 @@
 int main_trigger(int argc, char **argv);
 int main_sysrq(int argc, char **argv);
 int main_debug_keys(int argc, char **argv);
+int main_dmesg(int argc, char **argv);
 int main_top(int argc, char **argv);
 int main_networkattach(int argc, char **argv);
 int main_networklist(int argc, char **argv);

diff -r 840f269d95fb -r ab11e3eab7cc tools/libxl/xl_cmdtable.c

tools/libxl/xl_cmdtable.c +6 -0
@@ -191,6 +191,12 @@
       "Send debug keys to Xen",
       "<Keys>",
     },
+    { "dmesg",
+      &main_dmesg,
+      "Read and/or clear dmesg buffer",
+      "[-c]",
+      "  -c                        Clear dmesg buffer as well as printing it",
+    },
     { "top",
       &main_top,
       "Monitor a host and the domains in real time",
#2 lore raw

Stefano Stabellini writes ("[Xen-devel] Re: xl: Add subcommand 'xl dmesg'"):

Quoted 3 lines

The implementation is good, but I think we could probably offer an higher level API in libxenlight than just a wrapper around xc_readconsolering.

I agree that a higher-level API would be good.

Quoted 5 lines

I think we should have a libxl_dmesg instead of libxl_readconsolering, and libxl_dmesg would print dmesg itself using the libxl logging functions.
The caller can setup the logging function so that dmesg is printed to stdout in this case.

Are we sure that calling the logging function for every line in the ring is really what we want ? If nothing else this will repeat messages.

Ian.

#3 lore raw

Ian Jackson writes ("[Xen-devel] Re: xl: Add subcommand 'xl dmesg'"):

Quoted 3 lines

Are we sure that calling the logging function for every line in the ring is really what we want ? If nothing else this will repeat messages.

Having thought about this some more (and had a chat with Stefano) I think the right libxl interface is an iterator, something like this:

struct libxl_xen_console_reader*
libxl_xen_console_read_start(struct libxl_ctx*);

int
libxl_xen_console_read_line(struct libxl_ctx*,

                             struct libxl_xen_console_reader*,
                             char **line_r);
   /* return values:                                          *line_r
    *   1          success, whole line obtained from buffer    non-0
    *   0          no more lines available right now           0
    *   negative   error code ERROR_*                          0

* On success *line_r is updated to point to a nul-terminated
* string which is valid until the next call on the same console
* reader. The libxl caller may overwrite parts of the string
* if it wishes. */

void
libxl_xen_console_read_finish(struct libxl_ctx*,

                               struct libxl_xen_console_reader*);

"xl dmesg" then calls _start, loops calling line, and then calls _finish. Other libxl callers can plumb it into logging or poll for more output of whatever they like.

Ian.

author
#4 lore raw

Hi Ian,

Quoted 4 lines · Ian Jackson

Having thought about this some more (and had a chat with Stefano) I think the right libxl interface is an iterator, something like this:

Thanks for your explanation, I'll fix it later.

Regards
Yu Zhiguo

author
#5 lore raw

Hi Ian,

Quoted 5 lines · Yu Zhiguo
Quoted 3 lines · Ian Jackson

Having thought about this some more (and had a chat with Stefano) I think the right libxl interface is an iterator, something like this:

Fix it to an iterator now, how about this version.

-----------------------

Can be used to read and/or clear dmesg buffer.

tools/libxl/libxl.c +66 -0
diff -r 93410e5e4ad8 -r 2302edf6155c tools/libxl/libxl.c
@@ -2827,6 +2827,72 @@
     return xc_send_debug_keys(ctx->xch, keys);
 }
 
+struct libxl_xen_console_reader *
+    libxl_xen_console_read_start(struct libxl_ctx *ctx, int clear)
+{
+    struct libxl_xen_console_reader *cr;
+    unsigned int size = 16384;
+    char *buf = malloc(size);
+
+    if (!buf) {
+        XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "cannot malloc buffer for libxl_xen_console_reader,"
+            " size is %u", size);
+        return NULL;
+    }
+
+    cr = malloc(sizeof(struct libxl_xen_console_reader));
+    if (!cr) {
+        XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "cannot malloc libxl_xen_console_reader");
+        return NULL;
+    }
+
+    memset(cr, 0, sizeof(struct libxl_xen_console_reader));
+    cr->buffer = buf;
+    cr->size = size;
+    cr->count = size;
+    cr->clear = clear;
+    cr->incremental = 1;
+
+    return cr;
+}
+
+/* return values:                                          *line_r
+ *   1          success, whole line obtained from buffer    non-0
+ *   0          no more lines available right now           0
+ *   negative   error code ERROR_*                          0
+ * On success *line_r is updated to point to a nul-terminated
+ * string which is valid until the next call on the same console
+ * reader.  The libxl caller may overwrite parts of the string
+ * if it wishes. */
+int libxl_xen_console_read_line(struct libxl_ctx *ctx,
+                                struct libxl_xen_console_reader *cr,
+                                char **line_r)
+{
+    int ret;
+
+    memset(cr->buffer, 0, cr->size);
+    ret = xc_readconsolering(ctx->xch, &cr->buffer, &cr->count,
+                             cr->clear, cr->incremental, &cr->index);
+    if (!ret) {
+        if (cr->count) {
+            *line_r = cr->buffer;
+            ret = 1;
+        } else {
+            *line_r = NULL;
+            ret = 0;
+        }
+    }
+
+    return ret;
+}
+
+void libxl_xen_console_read_finish(struct libxl_ctx *ctx,
+                                   struct libxl_xen_console_reader *cr)
+{
+    free(cr->buffer);
+    free(cr);
+}
+
 uint32_t libxl_vm_get_start_time(struct libxl_ctx *ctx, uint32_t domid)
 {
     char *dompath = libxl_xs_get_dompath(ctx, domid);
tools/libxl/libxl.h +18 -0
diff -r 93410e5e4ad8 -r 2302edf6155c tools/libxl/libxl.h
@@ -512,6 +512,24 @@
                        char *trigger_name, uint32_t vcpuid);
 int libxl_send_sysrq(struct libxl_ctx *ctx, uint32_t domid, char sysrq);
 int libxl_send_debug_keys(struct libxl_ctx *ctx, char *keys);
+
+struct libxl_xen_console_reader {
+    char *buffer;
+    unsigned int size;
+    unsigned int count;
+    unsigned int clear;
+    unsigned int incremental;
+    unsigned int index;
+};
+
+struct libxl_xen_console_reader *
+    libxl_xen_console_read_start(struct libxl_ctx *ctx, int clear);
+int libxl_xen_console_read_line(struct libxl_ctx *ctx,
+                                struct libxl_xen_console_reader *cr,
+                                char **line_r);
+void libxl_xen_console_read_finish(struct libxl_ctx *ctx,
+                                   struct libxl_xen_console_reader *cr);
+
 uint32_t libxl_vm_get_start_time(struct libxl_ctx *ctx, uint32_t domid);
 
 char *libxl_tmem_list(struct libxl_ctx *ctx, uint32_t domid, int use_long);
tools/libxl/xl_cmdimpl.c +38 -0
diff -r 93410e5e4ad8 -r 2302edf6155c tools/libxl/xl_cmdimpl.c
@@ -3246,6 +3246,44 @@
     exit(0);
 }
 
+int main_dmesg(int argc, char **argv)
+{
+    unsigned int clear = 0;
+    struct libxl_xen_console_reader *cr;
+    char *line;
+    int opt, ret = 1;
+
+    while ((opt = getopt(argc, argv, "hc")) != -1) {
+        switch (opt) {
+        case 'c':
+            clear = 1;
+            break;
+        case 'h':
+            help("dmesg");
+            exit(0);
+        default:
+            fprintf(stderr, "option not supported\n");
+            break;
+        }
+    }
+
+    cr = libxl_xen_console_read_start(&ctx, clear);
+    if (!cr)
+        goto finish;
+
+    while (1) {
+        ret = libxl_xen_console_read_line(&ctx, cr, &line);
+        if (ret > 0)
+            printf(line);
+        else
+            break;
+    }
+
+finish:
+    libxl_xen_console_read_finish(&ctx, cr);
+    exit(ret);
+}
+
 int main_top(int argc, char **argv)
 {
     int opt;
tools/libxl/xl_cmdimpl.h +1 -0
diff -r 93410e5e4ad8 -r 2302edf6155c tools/libxl/xl_cmdimpl.h
@@ -45,6 +45,7 @@
 int main_trigger(int argc, char **argv);
 int main_sysrq(int argc, char **argv);
 int main_debug_keys(int argc, char **argv);
+int main_dmesg(int argc, char **argv);
 int main_top(int argc, char **argv);
 int main_networkattach(int argc, char **argv);
 int main_networklist(int argc, char **argv);
tools/libxl/xl_cmdtable.c +6 -0
diff -r 93410e5e4ad8 -r 2302edf6155c tools/libxl/xl_cmdtable.c
@@ -191,6 +191,12 @@
       "Send debug keys to Xen",
       "<Keys>",
     },
+    { "dmesg",
+      &main_dmesg,
+      "Read and/or clear dmesg buffer",
+      "[-c]",
+      "  -c                        Clear dmesg buffer as well as printing it",
+    },
     { "top",
       &main_top,
       "Monitor a host and the domains in real time",