From e8d690aeb544dfa230e10d1d419a5a5cf594f869 Mon Sep 17 00:00:00 2001 From: "Daniel P. Berrange" Date: Sat, 19 Jan 2008 18:36:01 +0000 Subject: [PATCH] Enable debug by default, but only if LIBVIRT_DEBUG=1. Use generic macro for debug output --- ChangeLog | 9 +++++ configure.in | 3 +- src/internal.h | 13 +++++++ src/libvirt.c | 83 ++++++++++++++++++++----------------------- src/remote_internal.c | 83 +++++++++++++++---------------------------- src/xen_unified.c | 12 +++---- src/xend_internal.c | 11 +++--- 7 files changed, 102 insertions(+), 112 deletions(-) diff --git a/ChangeLog b/ChangeLog index 81ecbd3550..a2e9b5f42a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +Sat Jan 19 13:32:22 EST 2008 Daniel P. Berrange + + * src/configure.in: enable debug by default. print status of + compiler warning flags in configure summary + * src/internal.h: Provide a generic VIR_DEBUG macro for logging + * src/libvirt.c, src/remote_internal.c, src/xen_unified.c, + src/xend_internal.c: Use generic VIR_DEBUG macro for logging. + Enable debug when env var LIBVIRT_DEBUG=1 + Tue Jan 15 16:25:57 CET Jim Meyering * docs/examples/examples.xml: Regenerate, now that *.c file names diff --git a/configure.in b/configure.in index 0fefb437db..06c6b641e4 100644 --- a/configure.in +++ b/configure.in @@ -147,7 +147,7 @@ AC_SUBST(STATIC_BINARIES) dnl --enable-debug=(yes|no) AC_ARG_ENABLE(debug, AC_HELP_STRING([--enable-debug=no/yes], - [enable debugging output]),[],[enable_debug=no]) + [enable debugging output]),[],[enable_debug=yes]) if test x"$enable_debug" = x"yes"; then AC_DEFINE(ENABLE_DEBUG, [], [whether debugging is enabled]) fi @@ -725,5 +725,6 @@ AC_MSG_NOTICE([]) AC_MSG_NOTICE([Miscellaneous]) AC_MSG_NOTICE([]) AC_MSG_NOTICE([ Debug: $enable_debug]) +AC_MSG_NOTICE([ Warnings: $enable_compile_warnings]) AC_MSG_NOTICE([ Readline: $lv_use_readline]) AC_MSG_NOTICE([]) diff --git a/src/internal.h b/src/internal.h index 50842cb1e7..b30b874126 100644 --- a/src/internal.h +++ b/src/internal.h @@ -54,6 +54,19 @@ extern "C" { #define STRNEQLEN(a,b,n) (strncmp((a),(b),(n)) != 0) #define STRCASENEQLEN(a,b,n) (strncasecmp((a),(b),(n)) != 0) + +/* If configured with --enable-debug=yes then library calls + * are printed to stderr for debugging. + */ +#ifdef ENABLE_DEBUG +extern int debugFlag; +#define VIR_DEBUG(category, fmt,...) \ + do { if (debugFlag) fprintf (stderr, "DEBUG: %s: %s (" fmt ")\n", category, __func__, __VA_ARGS__); } while (0) +#else +#define VIR_DEBUG(category, fmt,...) + do { } while (0) +#endif /* !ENABLE_DEBUG */ + /* C99 uses __func__. __FUNCTION__ is legacy. */ #ifndef __GNUC__ #define __FUNCTION__ __func__ diff --git a/src/libvirt.c b/src/libvirt.c index ca38c54754..3a5cb7ba50 100644 --- a/src/libvirt.c +++ b/src/libvirt.c @@ -55,18 +55,12 @@ static virStateDriverPtr virStateDriverTab[MAX_DRIVERS]; static int virStateDriverTabCount = 0; static int initialized = 0; -/* If configured with --enable-debug=yes then library calls - * are printed to stderr for debugging. - */ +#define DEBUG(fmt,...) VIR_DEBUG(__FILE__, fmt, __VA_ARGS__) +#define DEBUG0(msg) VIR_DEBUG(__FILE__, "%s", msg) + #ifdef ENABLE_DEBUG -#define DEBUG(fs,...) \ - fprintf (stderr, "libvirt: %s (" fs ")\n", __func__, __VA_ARGS__) -#define DEBUG0 \ - fprintf (stderr, "libvirt: %s ()\n", __func__) -#else -#define DEBUG0 -#define DEBUG(fs,...) -#endif /* !ENABLE_DEBUG */ +int debugFlag = 0; +#endif static int virConnectAuthCallbackDefault(virConnectCredentialPtr cred, unsigned int ncred, @@ -179,11 +173,21 @@ winsock_init (void) int virInitialize(void) { - DEBUG0; +#ifdef ENABLE_DEBUG + char *debugEnv; +#endif if (initialized) return(0); initialized = 1; +#ifdef ENABLE_DEBUG + debugEnv = getenv("LIBVIRT_DEBUG"); + if (debugEnv && *debugEnv && *debugEnv != '0') + debugFlag = 1; +#endif + + DEBUG0("register drivers"); + #if HAVE_WINSOCK2_H if (winsock_init () == -1) return -1; #endif @@ -542,20 +546,17 @@ do_open (const char *name, goto failed; } -#ifdef ENABLE_DEBUG - fprintf (stderr, - "libvirt: do_open: name \"%s\" to URI components:\n" - " scheme %s\n" - " opaque %s\n" - " authority %s\n" - " server %s\n" - " user %s\n" - " port %d\n" - " path %s\n", - name, - uri->scheme, uri->opaque, uri->authority, uri->server, - uri->user, uri->port, uri->path); -#endif + DEBUG("name \"%s\" to URI components:\n" + " scheme %s\n" + " opaque %s\n" + " authority %s\n" + " server %s\n" + " user %s\n" + " port %d\n" + " path %s\n", + name, + uri->scheme, uri->opaque, uri->authority, uri->server, + uri->user, uri->port, uri->path); ret->name = strdup (name); if (!ret->name) { @@ -564,18 +565,14 @@ do_open (const char *name, } for (i = 0; i < virDriverTabCount; i++) { -#ifdef ENABLE_DEBUG - fprintf (stderr, "libvirt: do_open: trying driver %d (%s) ...\n", - i, virDriverTab[i]->name); -#endif + DEBUG("trying driver %d (%s) ...", + i, virDriverTab[i]->name); res = virDriverTab[i]->open (ret, uri, auth, flags); -#ifdef ENABLE_DEBUG - fprintf (stderr, "libvirt: do_open: driver %d %s returned %s\n", - i, virDriverTab[i]->name, - res == VIR_DRV_OPEN_SUCCESS ? "SUCCESS" : - (res == VIR_DRV_OPEN_DECLINED ? "DECLINED" : - (res == VIR_DRV_OPEN_ERROR ? "ERROR" : "unknown status"))); -#endif + DEBUG("driver %d %s returned %s", + i, virDriverTab[i]->name, + res == VIR_DRV_OPEN_SUCCESS ? "SUCCESS" : + (res == VIR_DRV_OPEN_DECLINED ? "DECLINED" : + (res == VIR_DRV_OPEN_ERROR ? "ERROR" : "unknown status"))); if (res == VIR_DRV_OPEN_ERROR) goto failed; else if (res == VIR_DRV_OPEN_SUCCESS) { ret->driver = virDriverTab[i]; @@ -591,13 +588,11 @@ do_open (const char *name, for (i = 0; i < virNetworkDriverTabCount; i++) { res = virNetworkDriverTab[i]->open (ret, uri, auth, flags); -#ifdef ENABLE_DEBUG - fprintf (stderr, "libvirt: do_open: network driver %d %s returned %s\n", - i, virNetworkDriverTab[i]->name, - res == VIR_DRV_OPEN_SUCCESS ? "SUCCESS" : - (res == VIR_DRV_OPEN_DECLINED ? "DECLINED" : - (res == VIR_DRV_OPEN_ERROR ? "ERROR" : "unknown status"))); -#endif + DEBUG("network driver %d %s returned %s", + i, virNetworkDriverTab[i]->name, + res == VIR_DRV_OPEN_SUCCESS ? "SUCCESS" : + (res == VIR_DRV_OPEN_DECLINED ? "DECLINED" : + (res == VIR_DRV_OPEN_ERROR ? "ERROR" : "unknown status"))); if (res == VIR_DRV_OPEN_ERROR) { if (STREQ(virNetworkDriverTab[i]->name, "remote")) { virLibConnWarning (NULL, VIR_WAR_NO_NETWORK, diff --git a/src/remote_internal.c b/src/remote_internal.c index bf887118f0..860b4d8363 100644 --- a/src/remote_internal.c +++ b/src/remote_internal.c @@ -72,6 +72,9 @@ #include "remote_internal.h" #include "remote_protocol.h" +#define DEBUG(fmt,...) VIR_DEBUG(__FILE__, fmt,__VA_ARGS__) +#define DEBUG0(msg) VIR_DEBUG(__FILE__, "%s", msg) + /* Per-connection private data. */ #define MAGIC 999 /* private_data->magic if OK */ #define DEAD 998 /* private_data->magic if dead/closed */ @@ -159,22 +162,6 @@ remoteStartup(void) return 0; } -#if HAVE_SASL || HAVE_POLKIT -static void -remoteDebug(struct private_data *priv, const char *msg,...) -{ - va_list args; - if (priv->debugLog == NULL) - return; - - va_start(args, msg); - vfprintf(priv->debugLog, msg, args); - va_end(args); - fprintf(priv->debugLog, "\n"); -} -#endif /* HAVE_SASL */ - - /** * remoteFindServerPath: * @@ -426,14 +413,9 @@ doRemoteOpen (virConnectPtr conn, priv->debugLog = stdout; else priv->debugLog = stderr; - } -#ifdef ENABLE_DEBUG - else - fprintf (stderr, - "remoteOpen: " - "passing through variable '%s' ('%s') to remote end\n", - var->name, var->value); -#endif + } else + DEBUG("passing through variable '%s' ('%s') to remote end", + var->name, var->value); } #ifdef HAVE_XMLURI_QUERY_RAW @@ -478,9 +460,7 @@ doRemoteOpen (virConnectPtr conn, } assert (name); -#ifdef ENABLE_DEBUG - fprintf (stderr, "remoteOpen: proceeding with name = %s\n", name); -#endif + DEBUG("proceeding with name = %s", name); /* Connect to the remote service. */ switch (transport) { @@ -910,9 +890,7 @@ initialise_gnutls (virConnectPtr conn) return -1; /* Set the trusted CA cert. */ -#ifdef ENABLE_DEBUG - fprintf (stderr, "loading CA file %s\n", LIBVIRT_CACERT); -#endif + DEBUG("loading CA file %s", LIBVIRT_CACERT); err = gnutls_certificate_set_x509_trust_file (x509_cred, LIBVIRT_CACERT, GNUTLS_X509_FMT_PEM); @@ -922,10 +900,8 @@ initialise_gnutls (virConnectPtr conn) } /* Set the client certificate and private key. */ -#ifdef ENABLE_DEBUG - fprintf (stderr, "loading client cert and key from files %s and %s\n", - LIBVIRT_CLIENTCERT, LIBVIRT_CLIENTKEY); -#endif + DEBUG("loading client cert and key from files %s and %s", + LIBVIRT_CLIENTCERT, LIBVIRT_CLIENTKEY); err = gnutls_certificate_set_x509_key_file (x509_cred, LIBVIRT_CLIENTCERT, @@ -1000,10 +976,9 @@ negotiate_gnutls_on_connection (virConnectPtr conn, /* Verify certificate. */ if (verify_certificate (conn, priv, session) == -1) { - fprintf (stderr, - "remote_internal: failed to verify peer's certificate\n"); - if (!no_verify) return NULL; - } + DEBUG0("failed to verify peer's certificate"); + if (!no_verify) return NULL; + } /* At this point, the server is verifying _our_ certificate, IP address, * etc. If we make the grade, it will send us a '\1' byte. @@ -3013,7 +2988,7 @@ remoteAuthSASL (virConnectPtr conn, struct private_data *priv, int in_open, int ret = -1; const char *mechlist; - remoteDebug(priv, "Client initialize SASL authentication"); + DEBUG0("Client initialize SASL authentication"); /* Sets up the SASL library as a whole */ err = sasl_client_init(NULL); if (err != SASL_OK) { @@ -3085,7 +3060,7 @@ remoteAuthSASL (virConnectPtr conn, struct private_data *priv, int in_open, } ssf *= 8; /* key size is bytes, sasl wants bits */ - remoteDebug(priv, "Setting external SSF %d", ssf); + DEBUG("Setting external SSF %d", ssf); err = sasl_setprop(saslconn, SASL_SSF_EXTERNAL, &ssf); if (err != SASL_OK) { __virRaiseError (in_open ? NULL : conn, NULL, NULL, VIR_FROM_REMOTE, @@ -3135,7 +3110,7 @@ remoteAuthSASL (virConnectPtr conn, struct private_data *priv, int in_open, } restart: /* Start the auth negotiation on the client end first */ - remoteDebug(priv, "Client start negotiation mechlist '%s'", mechlist); + DEBUG("Client start negotiation mechlist '%s'", mechlist); err = sasl_client_start(saslconn, mechlist, &interact, @@ -3195,7 +3170,7 @@ remoteAuthSASL (virConnectPtr conn, struct private_data *priv, int in_open, sargs.data.data_val = (char*)clientout; sargs.data.data_len = clientoutlen; sargs.mech = (char*)mech; - remoteDebug(priv, "Server start negotiation with mech %s. Data %d bytes %p", mech, clientoutlen, clientout); + DEBUG("Server start negotiation with mech %s. Data %d bytes %p", mech, clientoutlen, clientout); /* Now send the initial auth data to the server */ memset (&sret, 0, sizeof sret); @@ -3208,8 +3183,8 @@ remoteAuthSASL (virConnectPtr conn, struct private_data *priv, int in_open, /* NB, distinction of NULL vs "" is *critical* in SASL */ serverin = sret.nil ? NULL : sret.data.data_val; serverinlen = sret.data.data_len; - remoteDebug(priv, "Client step result complete: %d. Data %d bytes %p", - complete, serverinlen, serverin); + DEBUG("Client step result complete: %d. Data %d bytes %p", + complete, serverinlen, serverin); /* Loop-the-loop... * Even if the server has completed, the client must *always* do at least one step @@ -3262,7 +3237,7 @@ remoteAuthSASL (virConnectPtr conn, struct private_data *priv, int in_open, free(serverin); serverin = NULL; } - remoteDebug(priv, "Client step result %d. Data %d bytes %p", err, clientoutlen, clientout); + DEBUG("Client step result %d. Data %d bytes %p", err, clientoutlen, clientout); /* Previous server call showed completion & we're now locally complete too */ if (complete && err == SASL_OK) @@ -3274,7 +3249,7 @@ remoteAuthSASL (virConnectPtr conn, struct private_data *priv, int in_open, pargs.nil = clientout ? 0 : 1; pargs.data.data_val = (char*)clientout; pargs.data.data_len = clientoutlen; - remoteDebug(priv, "Server step with %d bytes %p", clientoutlen, clientout); + DEBUG("Server step with %d bytes %p", clientoutlen, clientout); memset (&pret, 0, sizeof pret); if (call (conn, priv, in_open, REMOTE_PROC_AUTH_SASL_STEP, @@ -3287,8 +3262,8 @@ remoteAuthSASL (virConnectPtr conn, struct private_data *priv, int in_open, serverin = pret.nil ? NULL : pret.data.data_val; serverinlen = pret.data.data_len; - remoteDebug(priv, "Client step result complete: %d. Data %d bytes %p", - complete, serverinlen, serverin); + DEBUG("Client step result complete: %d. Data %d bytes %p", + complete, serverinlen, serverin); /* This server call shows complete, and earlier client step was OK */ if (complete && err == SASL_OK) { @@ -3308,7 +3283,7 @@ remoteAuthSASL (virConnectPtr conn, struct private_data *priv, int in_open, goto cleanup; } ssf = *(const int *)val; - remoteDebug(priv, "SASL SSF value %d", ssf); + DEBUG("SASL SSF value %d", ssf); if (ssf < 56) { /* 56 == DES level, good for Kerberos */ __virRaiseError (in_open ? NULL : conn, NULL, NULL, VIR_FROM_REMOTE, VIR_ERR_AUTH_FAILED, VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0, @@ -3317,7 +3292,7 @@ remoteAuthSASL (virConnectPtr conn, struct private_data *priv, int in_open, } } - remoteDebug(priv, "SASL authentication complete"); + DEBUG0("SASL authentication complete"); priv->saslconn = saslconn; ret = 0; @@ -3352,7 +3327,7 @@ remoteAuthPolkit (virConnectPtr conn, struct private_data *priv, int in_open, NULL, 0, }; - remoteDebug(priv, "Client initialize PolicyKit authentication"); + DEBUG0("Client initialize PolicyKit authentication"); if (auth && auth->cb) { /* Check if the neccessary credential type for PolicyKit is supported */ @@ -3370,10 +3345,10 @@ remoteAuthPolkit (virConnectPtr conn, struct private_data *priv, int in_open, return -1; } } else { - remoteDebug(priv, "Client auth callback does not support PolicyKit"); + DEBUG0("Client auth callback does not support PolicyKit"); } } else { - remoteDebug(priv, "No auth callback provided"); + DEBUG0("No auth callback provided"); } memset (&ret, 0, sizeof ret); @@ -3383,7 +3358,7 @@ remoteAuthPolkit (virConnectPtr conn, struct private_data *priv, int in_open, return -1; /* virError already set by call */ } - remoteDebug(priv, "PolicyKit authentication complete"); + DEBUG0("PolicyKit authentication complete"); return 0; } #endif /* HAVE_POLKIT */ diff --git a/src/xen_unified.c b/src/xen_unified.c index 99f99ce565..187b7f5a01 100644 --- a/src/xen_unified.c +++ b/src/xen_unified.c @@ -40,6 +40,8 @@ #include "xm_internal.h" #include "xml.h" +#define DEBUG(fmt,...) VIR_DEBUG(__FILE__, fmt,__VA_ARGS__) + static int xenUnifiedNodeGetInfo (virConnectPtr conn, virNodeInfoPtr info); static int @@ -271,15 +273,11 @@ xenUnifiedOpen (virConnectPtr conn, xmlURIPtr uri, virConnectAuthPtr auth, int f continue; if (drivers[i]->open) { -#ifdef ENABLE_DEBUG - fprintf (stderr, "libvirt: xenUnifiedOpen: trying Xen sub-driver %d\n", i); -#endif + DEBUG("trying Xen sub-driver %d", i); if (drivers[i]->open (conn, uri, auth, flags) == VIR_DRV_OPEN_SUCCESS) priv->opened[i] = 1; -#ifdef ENABLE_DEBUG - fprintf (stderr, "libvirt: xenUnifiedOpen: Xen sub-driver %d open %s\n", - i, priv->opened[i] ? "ok" : "failed"); -#endif + DEBUG("Xen sub-driver %d open %s\n", + i, priv->opened[i] ? "ok" : "failed"); } /* If as root, then all drivers must succeed. diff --git a/src/xend_internal.c b/src/xend_internal.c index 8e4c3d5ed9..bff625bf39 100644 --- a/src/xend_internal.c +++ b/src/xend_internal.c @@ -48,6 +48,9 @@ /* required for cpumap_t */ #include +#define DEBUG(fmt,...) VIR_DEBUG(__FILE__, fmt,__VA_ARGS__) +#define DEBUG0(msg) VIR_DEBUG(__FILE__, "%s", msg) + #ifndef PROXY static int xenDaemonListDomains(virConnectPtr conn, int *ids, int maxids); static int xenDaemonNumOfDomains(virConnectPtr conn); @@ -3410,9 +3413,7 @@ xenDaemonDomainMigratePerform (virDomainPtr domain, } } -#ifdef ENABLE_DEBUG - fprintf (stderr, "hostname = %s, port = %s\n", hostname, port); -#endif + DEBUG("hostname = %s, port = %s", hostname, port); /* Make the call. */ ret = xend_op (domain->conn, domain->name, @@ -3424,9 +3425,7 @@ xenDaemonDomainMigratePerform (virDomainPtr domain, NULL); free (hostname); -#ifdef ENABLE_DEBUG - fprintf (stderr, "migration done\n"); -#endif + DEBUG0("migration done"); return ret; }