debuggers.hg

changeset 22047:0b6f82eaaea9

xl: make libxl_uuid2string internal to libxenlight

libxenlight exports a function libxl_uuid2string which is used
internally in several places but has one external caller in xl.
This means that libxl internal callers leak since they were not
expecting to have to free() the UUID since the per-api-call-gc-lifetime
patch.

Convert libxl_uuid2string to be an internal function which participates
in the callers garbage collection. Eliminate string_of_uuid() macro in
favour of "format" and "arguments" macros suitable for printf()-like
functions which are made part of the libxl API and fix-up xl callers to
use that to avoid code duplication and enhance readability.

Signed-off-by: Gianni Tedesco <gianni.tedesco@citrix.com>
Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
committer: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
author Gianni Tedesco <gianni.tedesco@citrix.com>
date Mon Aug 16 17:15:04 2010 +0100 (2010-08-16)
parents 4fc66044477d
children 6469a683b174
files tools/libxl/libxl.c tools/libxl/libxl.h tools/libxl/libxl_dom.c tools/libxl/libxl_internal.h tools/libxl/xl_cmdimpl.c
line diff
     1.1 --- a/tools/libxl/libxl.c	Mon Aug 16 17:14:10 2010 +0100
     1.2 +++ b/tools/libxl/libxl.c	Mon Aug 16 17:15:04 2010 +0100
     1.3 @@ -90,7 +90,7 @@ int libxl_domain_make(libxl_ctx *ctx, li
     1.4      xs_transaction_t t;
     1.5      xen_domain_handle_t handle;
     1.6  
     1.7 -    uuid_string = libxl_uuid2string(ctx, info->uuid);
     1.8 +    uuid_string = libxl_uuid2string(&gc, info->uuid);
     1.9      if (!uuid_string) {
    1.10          libxl_free_all(&gc);
    1.11          return ERROR_NOMEM;
    1.12 @@ -453,7 +453,7 @@ int libxl_domain_preserve(libxl_ctx *ctx
    1.13          return ERROR_NOMEM;
    1.14      }
    1.15  
    1.16 -    uuid_string = libxl_uuid2string(ctx, new_uuid);
    1.17 +    uuid_string = libxl_uuid2string(&gc, new_uuid);
    1.18      if (!uuid_string) {
    1.19          libxl_free_all(&gc);
    1.20          return ERROR_NOMEM;
    1.21 @@ -2764,7 +2764,7 @@ int libxl_set_memory_target(libxl_ctx *c
    1.22      if (rc != 1 || info.domain != domid)
    1.23          goto out;
    1.24      xcinfo2xlinfo(&info, &ptr);
    1.25 -    uuid = libxl_uuid2string(ctx, ptr.uuid);
    1.26 +    uuid = libxl_uuid2string(&gc, ptr.uuid);
    1.27      libxl_xs_write(&gc, XBT_NULL, libxl_sprintf(&gc, "/vm/%s/memory", uuid), "%"PRIu32, target_memkb / 1024);
    1.28  
    1.29      if (enforce || !domid)
     2.1 --- a/tools/libxl/libxl.h	Mon Aug 16 17:14:10 2010 +0100
     2.2 +++ b/tools/libxl/libxl.h	Mon Aug 16 17:15:04 2010 +0100
     2.3 @@ -132,6 +132,12 @@
     2.4  #include <sys/wait.h> /* for pid_t */
     2.5  
     2.6  typedef uint8_t libxl_uuid[16];
     2.7 +#define LIBXL_UUID_FMT "%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"
     2.8 +#define LIBXL_UUID_BYTES(uuid) uuid[0], uuid[1], uuid[2], uuid[3], \
     2.9 +            uuid[4], uuid[5], uuid[6], uuid[7], \
    2.10 +            uuid[8], uuid[9], uuid[10], uuid[11], \
    2.11 +            uuid[12], uuid[13], uuid[14], uuid[15] \
    2.12 +
    2.13  
    2.14  typedef uint8_t libxl_mac[6];
    2.15  
    2.16 @@ -477,7 +483,6 @@ int libxl_run_bootloader(libxl_ctx *ctx,
    2.17                           libxl_device_disk *disk,
    2.18                           uint32_t domid);
    2.19  
    2.20 -char *libxl_uuid2string(libxl_ctx *ctx, const libxl_uuid uuid);
    2.21    /* 0 means ERROR_ENOMEM, which we have logged */
    2.22  
    2.23  /* events handling */
     3.1 --- a/tools/libxl/libxl_dom.c	Mon Aug 16 17:14:10 2010 +0100
     3.2 +++ b/tools/libxl/libxl_dom.c	Mon Aug 16 17:15:04 2010 +0100
     3.3 @@ -442,19 +442,12 @@ int save_device_model(libxl_ctx *ctx, ui
     3.4      return 0;
     3.5  }
     3.6  
     3.7 -char *libxl_uuid2string(libxl_ctx *ctx, const libxl_uuid uuid)
     3.8 +char *libxl_uuid2string(libxl_gc *gc, const libxl_uuid uuid)
     3.9  {
    3.10 -    libxl_gc gc = LIBXL_INIT_GC(ctx);
    3.11 -    char *s = string_of_uuid(&gc, uuid);
    3.12 -    char *ret;
    3.13 -    if (!s) {
    3.14 -        XL_LOG(ctx, XL_LOG_ERROR, "cannot allocate for uuid");
    3.15 -        ret = NULL;
    3.16 -    }else{
    3.17 -        ret = strdup(s);
    3.18 -    }
    3.19 -    libxl_free_all(&gc);
    3.20 -    return ret;
    3.21 +    char *s = libxl_sprintf(gc, LIBXL_UUID_FMT, LIBXL_UUID_BYTES(uuid));
    3.22 +    if (!s)
    3.23 +        XL_LOG(libxl_gc_owner(gc), XL_LOG_ERROR, "cannot allocate for uuid");
    3.24 +    return s;
    3.25  }
    3.26  
    3.27  static const char *userdata_path(libxl_gc *gc, uint32_t domid,
    3.28 @@ -472,7 +465,7 @@ static const char *userdata_path(libxl_g
    3.29                       " for domain %"PRIu32, domid);
    3.30          return NULL;
    3.31      }
    3.32 -    uuid_string = string_of_uuid(gc, info.uuid);
    3.33 +    uuid_string = libxl_sprintf(gc, LIBXL_UUID_FMT, LIBXL_UUID_BYTES(info.uuid));
    3.34  
    3.35      path = libxl_sprintf(gc, "/var/lib/xen/"
    3.36                           "userdata-%s.%s.%s",
     4.1 --- a/tools/libxl/libxl_internal.h	Mon Aug 16 17:14:10 2010 +0100
     4.2 +++ b/tools/libxl/libxl_internal.h	Mon Aug 16 17:15:04 2010 +0100
     4.3 @@ -106,12 +106,6 @@ typedef struct {
     4.4  
     4.5  #define PRINTF_ATTRIBUTE(x, y) __attribute__((format(printf, x, y)))
     4.6  
     4.7 -#define UUID_FMT "%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"
     4.8 -#define string_of_uuid(ctx, u) \
     4.9 -    libxl_sprintf(ctx, UUID_FMT, \
    4.10 -                (u)[0], (u)[1], (u)[2], (u)[3], (u)[4], (u)[5], (u)[6], (u)[7], \
    4.11 -                (u)[8], (u)[9], (u)[10], (u)[11], (u)[12], (u)[13], (u)[14], (u)[15])
    4.12 -
    4.13  _hidden int xs_writev(struct xs_handle *xsh, xs_transaction_t t, char *dir, char *kvs[]);
    4.14  
    4.15  typedef struct {
    4.16 @@ -267,4 +261,6 @@ const char *libxl_blktap_devpath(libxl_g
    4.17                                   const char *disk,
    4.18                                   libxl_disk_phystype phystype);
    4.19  
    4.20 +_hidden char *libxl_uuid2string(libxl_gc *gc, const libxl_uuid uuid);
    4.21 +
    4.22  #endif
     5.1 --- a/tools/libxl/xl_cmdimpl.c	Mon Aug 16 17:14:10 2010 +0100
     5.2 +++ b/tools/libxl/xl_cmdimpl.c	Mon Aug 16 17:15:04 2010 +0100
     5.3 @@ -41,8 +41,6 @@
     5.4  #include "libxlutil.h"
     5.5  #include "xl.h"
     5.6  
     5.7 -#define UUID_FMT "%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"
     5.8 -
     5.9  #define CHK_ERRNO( call ) ({                                            \
    5.10          int chk_errno = (call);                                         \
    5.11          if (chk_errno < 0) {                                                \
    5.12 @@ -393,11 +391,7 @@ static void printf_info(int domid,
    5.13      printf("\t(oos %d)\n", c_info->oos);
    5.14      printf("\t(ssidref %d)\n", c_info->ssidref);
    5.15      printf("\t(name %s)\n", c_info->name);
    5.16 -    printf("\t(uuid " UUID_FMT ")\n",
    5.17 -           (c_info->uuid)[0], (c_info->uuid)[1], (c_info->uuid)[2], (c_info->uuid)[3],
    5.18 -           (c_info->uuid)[4], (c_info->uuid)[5], (c_info->uuid)[6], (c_info->uuid)[7],
    5.19 -           (c_info->uuid)[8], (c_info->uuid)[9], (c_info->uuid)[10], (c_info->uuid)[11],
    5.20 -           (c_info->uuid)[12], (c_info->uuid)[13], (c_info->uuid)[14], (c_info->uuid)[15]);
    5.21 +    printf("\t(uuid " LIBXL_UUID_FMT ")\n", LIBXL_UUID_BYTES(c_info->uuid));
    5.22      printf("\t(cpupool %s (%d))\n", c_info->poolname, c_info->poolid);
    5.23      if (c_info->xsdata)
    5.24          printf("\t(xsdata contains data)\n");
    5.25 @@ -2188,10 +2182,8 @@ void list_domains(int verbose, const lib
    5.26                  info[i].dying ? 'd' : '-',
    5.27                  ((float)info[i].cpu_time / 1e9));
    5.28          free(domname);
    5.29 -        if (verbose) {
    5.30 -            char *uuid = libxl_uuid2string(&ctx, info[i].uuid);
    5.31 -            printf(" %s", uuid);
    5.32 -        }
    5.33 +        if (verbose)
    5.34 +            printf(" " LIBXL_UUID_FMT, LIBXL_UUID_BYTES(info[i].uuid));
    5.35          putchar('\n');
    5.36      }
    5.37  }
    5.38 @@ -2211,11 +2203,7 @@ void list_vm(void)
    5.39      printf("UUID                                  ID    name\n");
    5.40      for (i = 0; i < nb_vm; i++) {
    5.41          domname = libxl_domid_to_name(&ctx, info[i].domid);
    5.42 -        printf(UUID_FMT "  %d    %-30s\n",
    5.43 -            info[i].uuid[0], info[i].uuid[1], info[i].uuid[2], info[i].uuid[3],
    5.44 -            info[i].uuid[4], info[i].uuid[5], info[i].uuid[6], info[i].uuid[7],
    5.45 -            info[i].uuid[8], info[i].uuid[9], info[i].uuid[10], info[i].uuid[11],
    5.46 -            info[i].uuid[12], info[i].uuid[13], info[i].uuid[14], info[i].uuid[15],
    5.47 +        printf(LIBXL_UUID_FMT "  %d    %-30s\n", LIBXL_UUID_BYTES(info[i].uuid),
    5.48              info[i].domid, domname);
    5.49          free(domname);
    5.50      }