2014-03-11 05:27:13 -05:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Ondrej Oprala <ooprala@redhat.com>
|
2014-11-03 05:51:17 -06:00
|
|
|
* Copyright (C) 2014 Karel Zak <kzak@redhat.com>
|
2014-03-11 05:27:13 -05:00
|
|
|
*
|
|
|
|
* This file may be distributed under the terms of the
|
|
|
|
* GNU Lesser General Public License.
|
|
|
|
*/
|
|
|
|
#ifndef UTIL_LINUX_DEBUG_H
|
|
|
|
#define UTIL_LINUX_DEBUG_H
|
|
|
|
|
2014-11-03 05:51:17 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
* util-linux debug macros
|
|
|
|
*
|
|
|
|
* The debug stuff is based on <name>_debug_mask that controls what outputs is
|
|
|
|
* expected. The mask is usually initialized by <NAME>_DEBUG= env.variable
|
|
|
|
*
|
|
|
|
* After successful initialization the flag <PREFIX>_DEBUG_INIT is always set
|
|
|
|
* to the mask (this flag is required). The <PREFIX> is usually library API
|
|
|
|
* prefix (e.g. MNT_) or program name (e.g. CFDISK_)
|
|
|
|
*
|
|
|
|
* In the code is possible to use
|
|
|
|
*
|
|
|
|
* DBG(FOO, ul_debug("this is output for foo"));
|
|
|
|
*
|
|
|
|
* where for the FOO has to be defined <PREFIX>_DEBUG_FOO.
|
|
|
|
*
|
|
|
|
* It's possible to initialize the mask by comma delimited strings with
|
|
|
|
* subsystem names (e.g. "LIBMOUNT_DEBUG=options,tab"). In this case is
|
|
|
|
* necessary to define mask names array. This functionality is optional.
|
|
|
|
*
|
2016-05-29 16:11:53 -05:00
|
|
|
* It's strongly recommended to use UL_* macros to define/declare/use
|
2014-11-03 05:51:17 -06:00
|
|
|
* the debug stuff.
|
|
|
|
*
|
|
|
|
* See disk-utils/cfdisk.c: cfdisk_init_debug() for programs debug
|
|
|
|
* or libmount/src/init.c: mnt_init_debug() for library debug
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-03-11 05:27:13 -05:00
|
|
|
#include <stdarg.h>
|
2014-07-31 06:23:07 -05:00
|
|
|
#include <string.h>
|
2014-03-11 05:27:13 -05:00
|
|
|
|
2014-11-03 05:51:17 -06:00
|
|
|
struct ul_debug_maskname {
|
|
|
|
const char *name;
|
|
|
|
int mask;
|
|
|
|
const char *help;
|
|
|
|
};
|
|
|
|
#define UL_DEBUG_EMPTY_MASKNAMES {{ NULL, 0, NULL }}
|
|
|
|
#define UL_DEBUG_DEFINE_MASKNAMES(m) static const struct ul_debug_maskname m ## _masknames[]
|
|
|
|
#define UL_DEBUG_MASKNAMES(m) m ## _masknames
|
2014-08-13 07:19:21 -05:00
|
|
|
|
2014-03-12 09:40:40 -05:00
|
|
|
#define UL_DEBUG_DEFINE_MASK(m) int m ## _debug_mask
|
|
|
|
#define UL_DEBUG_DECLARE_MASK(m) extern UL_DEBUG_DEFINE_MASK(m)
|
|
|
|
|
|
|
|
/* p - flag prefix, m - flag postfix */
|
|
|
|
#define UL_DEBUG_DEFINE_FLAG(p, m) p ## m
|
|
|
|
|
|
|
|
/* l - library name, p - flag prefix, m - flag postfix, x - function */
|
2014-03-21 06:04:12 -05:00
|
|
|
#define __UL_DBG(l, p, m, x) \
|
2014-03-21 05:21:56 -05:00
|
|
|
do { \
|
|
|
|
if ((p ## m) & l ## _debug_mask) { \
|
|
|
|
fprintf(stderr, "%d: %s: %8s: ", getpid(), # l, # m); \
|
|
|
|
x; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2014-03-11 05:27:13 -05:00
|
|
|
|
2014-03-21 06:04:12 -05:00
|
|
|
#define __UL_DBG_CALL(l, p, m, x) \
|
|
|
|
do { \
|
|
|
|
if ((p ## m) & l ## _debug_mask) { \
|
|
|
|
x; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define __UL_DBG_FLUSH(l, p) \
|
|
|
|
do { \
|
|
|
|
if (l ## _debug_mask && \
|
|
|
|
l ## _debug_mask != p ## INIT) { \
|
|
|
|
fflush(stderr); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2014-03-21 05:21:56 -05:00
|
|
|
#define __UL_INIT_DEBUG(lib, pref, mask, env) \
|
|
|
|
do { \
|
|
|
|
if (lib ## _debug_mask & pref ## INIT) \
|
|
|
|
; \
|
|
|
|
else if (!mask) { \
|
|
|
|
char *str = getenv(# env); \
|
|
|
|
if (str) \
|
2014-11-03 05:51:17 -06:00
|
|
|
lib ## _debug_mask = ul_debug_parse_envmask(lib ## _masknames, str); \
|
2014-03-21 05:21:56 -05:00
|
|
|
} else \
|
|
|
|
lib ## _debug_mask = mask; \
|
|
|
|
lib ## _debug_mask |= pref ## INIT; \
|
|
|
|
} while (0)
|
2014-03-11 05:27:13 -05:00
|
|
|
|
2014-07-31 06:23:07 -05:00
|
|
|
|
2014-03-11 05:27:13 -05:00
|
|
|
static inline void __attribute__ ((__format__ (__printf__, 1, 2)))
|
|
|
|
ul_debug(const char *mesg, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, mesg);
|
|
|
|
vfprintf(stderr, mesg, ap);
|
|
|
|
va_end(ap);
|
|
|
|
fputc('\n', stderr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void __attribute__ ((__format__ (__printf__, 2, 3)))
|
2016-09-21 01:22:35 -05:00
|
|
|
ul_debugobj(const void *handler, const char *mesg, ...)
|
2014-03-11 05:27:13 -05:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
if (handler)
|
|
|
|
fprintf(stderr, "[%p]: ", handler);
|
|
|
|
va_start(ap, mesg);
|
|
|
|
vfprintf(stderr, mesg, ap);
|
|
|
|
va_end(ap);
|
|
|
|
fputc('\n', stderr);
|
|
|
|
}
|
|
|
|
|
2014-11-03 05:51:17 -06:00
|
|
|
static inline int ul_debug_parse_envmask(
|
|
|
|
const struct ul_debug_maskname flagnames[],
|
|
|
|
const char *mask)
|
2014-07-31 06:23:07 -05:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
char *ptr;
|
|
|
|
|
|
|
|
/* let's check for a numeric mask first */
|
|
|
|
res = strtoul(mask, &ptr, 0);
|
|
|
|
|
|
|
|
/* perhaps it's a comma-separated string? */
|
2014-11-03 05:51:17 -06:00
|
|
|
if (ptr && *ptr && flagnames && flagnames[0].name) {
|
2014-07-31 06:23:07 -05:00
|
|
|
char *msbuf, *ms, *name;
|
|
|
|
res = 0;
|
|
|
|
|
|
|
|
ms = msbuf = strdup(mask);
|
|
|
|
if (!ms)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
while ((name = strtok_r(ms, ",", &ptr))) {
|
2014-11-03 05:51:17 -06:00
|
|
|
const struct ul_debug_maskname *d;
|
2014-07-31 06:23:07 -05:00
|
|
|
ms = ptr;
|
|
|
|
|
2014-11-03 05:51:17 -06:00
|
|
|
for (d = flagnames; d && d->name; d++) {
|
|
|
|
if (strcmp(name, d->name) == 0) {
|
|
|
|
res |= d->mask;
|
2014-07-31 06:23:07 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* nothing else we can do by OR-ing the mask */
|
|
|
|
if (res == 0xffff)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free(msbuf);
|
2014-11-03 05:51:17 -06:00
|
|
|
} else if (ptr && strcmp(ptr, "all") == 0)
|
|
|
|
res = 0xffff;
|
|
|
|
|
2014-07-31 06:23:07 -05:00
|
|
|
return res;
|
|
|
|
}
|
2014-11-03 05:51:17 -06:00
|
|
|
|
|
|
|
static inline void ul_debug_print_masks(
|
|
|
|
const char *env,
|
|
|
|
const struct ul_debug_maskname flagnames[])
|
|
|
|
{
|
|
|
|
const struct ul_debug_maskname *d;
|
|
|
|
|
|
|
|
if (!flagnames)
|
|
|
|
return;
|
|
|
|
|
|
|
|
fprintf(stderr, "Available \"%s=<name>[,...]|<mask>\" debug masks:\n",
|
|
|
|
env);
|
|
|
|
for (d = flagnames; d && d->name; d++) {
|
|
|
|
if (!d->help)
|
|
|
|
continue;
|
|
|
|
fprintf(stderr, " %-8s [0x%04x] : %s\n",
|
|
|
|
d->name, d->mask, d->help);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-11 05:27:13 -05:00
|
|
|
#endif /* UTIL_LINUX_DEBUG_H */
|