*/
/**
- * @file debug_ch.c
- * @brief Management of the debugging channels
+ * @file debug_ch.c
+ * @brief Management of the debugging channels
*
* @author
* @date
static HANDLE handle;
#endif
-static inline int interlocked_xchg_add( int *dest, int incr )
+static inline int interlocked_xchg_add(int *dest, int incr)
{
- int ret_val;
- __asm__ __volatile__( "lock; xaddl %0,(%1)"
- : "=r" (ret_val) : "r" (dest), "0" (incr) : "memory" );
- return ret_val;
+ int ret_val;
+ __asm__ __volatile__("lock; xaddl %0,(%1)"
+ : "=r" (ret_val) : "r" (dest), "0" (incr) : "memory");
+
+ return ret_val;
}
static const char * const debug_classes[] = {"fixme", "err", "warn", "trace", "info"};
static void debug_init(void);
-static int cmp_name( const void *p1, const void *p2 )
+static int cmp_name(const void *p1, const void *p2)
{
- const char *name = p1;
- const struct _debug_channel *chan = p2;
- return strcmp( name, chan->name );
+ const char *name = p1;
+ const struct _debug_channel *chan = p2;
+
+ return strcmp(name, chan->name);
}
/* get the flags to use for a given channel, possibly setting them too in case of lazy init */
-unsigned char _dbg_get_channel_flags( struct _debug_channel *channel )
+unsigned char _dbg_get_channel_flags(struct _debug_channel *channel)
{
- if (nb_debug_options == -1)
- debug_init();
-
- if(nb_debug_options){
-
- struct _debug_channel *opt;
-
- /* first check for multi channel */
- opt = bsearch( channel->multiname,
- debug_options,
- nb_debug_options,
- sizeof(debug_options[0]), cmp_name );
- if (opt)
- return opt->flags;
-
- opt = bsearch( channel->name,
- debug_options,
- nb_debug_options,
- sizeof(debug_options[0]), cmp_name );
- if (opt){
- return opt->flags;
- }
- }
-
- /* no option for this channel */
- if (channel->flags & (1 << __DBCL_INIT))
- channel->flags = default_flags;
-
- return default_flags;
+ if (nb_debug_options == -1) {
+ debug_init();
+ }
+
+ if (nb_debug_options) {
+ struct _debug_channel *opt;
+
+ /* first check for multi channel */
+ opt = bsearch(channel->multiname,
+ debug_options,
+ nb_debug_options,
+ sizeof(debug_options[0]), cmp_name);
+ if (opt) {
+ return opt->flags;
+ }
+
+ opt = bsearch(channel->name,
+ debug_options,
+ nb_debug_options,
+ sizeof(debug_options[0]), cmp_name);
+ if (opt) {
+ return opt->flags;
+ }
+ }
+
+ /* no option for this channel */
+ if (channel->flags & (1 << __DBCL_INIT)) {
+ channel->flags = default_flags;
+ }
+
+ return default_flags;
}
/* set the flags to use for a given channel; return 0 if the channel is not available to set */
-int _dbg_set_channel_flags( struct _debug_channel *channel,
- unsigned char set, unsigned char clear )
+int _dbg_set_channel_flags(struct _debug_channel *channel,
+ unsigned char set, unsigned char clear)
{
- if (nb_debug_options == -1)
- debug_init();
-
- if (nb_debug_options){
- struct _debug_channel *opt;
-
- /* first set for multi channel */
- opt = bsearch( channel->multiname,
- debug_options,
- nb_debug_options,
- sizeof(debug_options[0]), cmp_name );
- if (opt){
- opt->flags = (opt->flags & ~clear) | set;
- return 1;
- }
-
- opt = bsearch( channel->name,
- debug_options,
- nb_debug_options,
- sizeof(debug_options[0]), cmp_name );
- if (opt){
- opt->flags = (opt->flags & ~clear) | set;
- return 1;
- }
- }
- return 0;
+ if (nb_debug_options == -1) {
+ debug_init();
+ }
+
+ if (nb_debug_options) {
+ struct _debug_channel *opt;
+
+ /* first set for multi channel */
+ opt = bsearch(channel->multiname,
+ debug_options,
+ nb_debug_options,
+ sizeof(debug_options[0]), cmp_name);
+ if (opt) {
+ opt->flags = (opt->flags & ~clear) | set;
+ return 1;
+ }
+
+ opt = bsearch(channel->name,
+ debug_options,
+ nb_debug_options,
+ sizeof(debug_options[0]), cmp_name);
+ if (opt) {
+ opt->flags = (opt->flags & ~clear) | set;
+ return 1;
+ }
+ }
+
+ return 0;
}
/* add a new debug option at the end of the option list */
-static void add_option( const char *name, unsigned char set, unsigned char clear )
+static void add_option(const char *name, unsigned char set, unsigned char clear)
{
- int min = 0, max = nb_debug_options - 1, pos, res;
-
- if (!name[0]) /* "all" option */
- {
- default_flags = (default_flags & ~clear) | set;
- return;
- }
-
- if (strlen(name) >= sizeof(debug_options[0].name))
- return;
-
- while (min <= max)
- {
- pos = (min + max) / 2;
- res = strcmp( name, debug_options[pos].name );
- if (!res)
- {
- debug_options[pos].flags = (debug_options[pos].flags & ~clear) | set;
- return;
- }
- if (res < 0)
- max = pos - 1;
- else
- min = pos + 1;
- }
- if (nb_debug_options >= MAX_DEBUG_OPTIONS)
- return;
-
- pos = min;
- if (pos < nb_debug_options) {
- memmove( &debug_options[pos + 1],
- &debug_options[pos],
- (nb_debug_options - pos) * sizeof(debug_options[0]) );
- }
-
- strcpy( debug_options[pos].name, name );
- debug_options[pos].flags = (default_flags & ~clear) | set;
- nb_debug_options++;
+ int min = 0, max = nb_debug_options - 1, pos, res;
+
+ if (!name[0]) { /* "all" option */
+ default_flags = (default_flags & ~clear) | set;
+ return;
+ }
+
+ if (strlen(name) >= sizeof(debug_options[0].name)) {
+ return;
+ }
+
+ while (min <= max) {
+ pos = (min + max) / 2;
+ res = strcmp(name, debug_options[pos].name);
+ if (!res) {
+ debug_options[pos].flags = (debug_options[pos].flags & ~clear) | set;
+ return;
+ }
+
+ if (res < 0) {
+ max = pos - 1;
+ } else {
+ min = pos + 1;
+ }
+ }
+ if (nb_debug_options >= MAX_DEBUG_OPTIONS) {
+ return;
+ }
+
+ pos = min;
+ if (pos < nb_debug_options) {
+ memmove(&debug_options[pos + 1],
+ &debug_options[pos],
+ (nb_debug_options - pos) * sizeof(debug_options[0]));
+ }
+
+ strcpy(debug_options[pos].name, name);
+ debug_options[pos].flags = (default_flags & ~clear) | set;
+ nb_debug_options++;
}
/* parse a set of debugging option specifications and add them to the option list */
-static void parse_options( const char *str )
+static void parse_options(const char *str)
{
- char *opt, *next, *options;
- unsigned int i;
-
- if (!(options = strdup(str)))
- return;
- for (opt = options; opt; opt = next)
- {
- const char *p;
- unsigned char set = 0, clear = 0;
-
- if ((next = strchr( opt, ',' )))
- *next++ = 0;
-
- p = opt + strcspn( opt, "+-" );
- if (!p[0])
- p = opt; /* assume it's a debug channel name */
-
- if (p > opt)
- {
- for (i = 0; i < sizeof(debug_classes)/sizeof(debug_classes[0]); i++)
- {
- int len = strlen(debug_classes[i]);
- if (len != (p - opt))
- continue;
- if (!memcmp( opt, debug_classes[i], len )) /* found it */
- {
- if (*p == '+')
- set |= 1 << i;
- else
- clear |= 1 << i;
- break;
- }
- }
- if (i == sizeof(debug_classes)/sizeof(debug_classes[0])) /* bad class name, skip it */
- continue;
- }
- else
- {
- if (*p == '-')
- clear = ~0;
- else
- set = ~0;
- }
- if (*p == '+' || *p == '-')
- p++;
- if (!p[0])
- continue;
-
- if (!strcmp( p, "all" ))
- default_flags = (default_flags & ~clear) | set;
- else
- add_option( p, set, clear );
- }
- free( options );
+ char *opt, *next, *options;
+ unsigned int i;
+
+ if (!(options = strdup(str))) {
+ return;
+ }
+
+ for (opt = options; opt; opt = next) {
+ const char *p;
+ unsigned char set = 0, clear = 0;
+
+ if ((next = strchr( opt, ',' ))) {
+ *next++ = 0;
+ }
+
+ p = opt + strcspn( opt, "+-" );
+ if (!p[0]) {
+ p = opt; /* assume it's a debug channel name */
+ }
+
+ if (p > opt) {
+ for (i = 0; i < sizeof(debug_classes) / sizeof(debug_classes[0]); i++) {
+ int len = strlen(debug_classes[i]);
+ if (len != (p - opt)) {
+ continue;
+ }
+
+ if (!memcmp( opt, debug_classes[i], len)) { /* found it */
+ if (*p == '+') {
+ set |= 1 << i;
+ } else {
+ clear |= 1 << i;
+ }
+ break;
+ }
+ }
+
+ if (i == sizeof(debug_classes) / sizeof(debug_classes[0])) { /* bad class name, skip it */
+ continue;
+ }
+ } else {
+ if (*p == '-') {
+ clear = ~0;
+ } else {
+ set = ~0;
+ }
+ }
+
+ if (*p == '+' || *p == '-') {
+ p++;
+ }
+ if (!p[0]) {
+ continue;
+ }
+
+ if (!strcmp(p, "all")) {
+ default_flags = (default_flags & ~clear) | set;
+ } else {
+ add_option(p, set, clear);
+ }
+ }
+
+ free(options);
}
/* print the usage message */
static void debug_usage(void)
{
- static const char usage[] =
- "Syntax of the DEBUGCH variable:\n"
- " DEBUGCH=[class]+xxx,[class]-yyy,...\n\n"
- "Example: DEBUGCH=+all,warn-heap\n"
- " turns on all messages except warning heap messages\n"
- "Available message classes: err, warn, fixme, trace\n";
- const int ret = write( 2, usage, sizeof(usage) - 1 );
- assert(ret >= 0);
- exit(1);
+ static const char usage[] =
+ "Syntax of the DEBUGCH variable:\n"
+ " DEBUGCH=[class]+xxx,[class]-yyy,...\n\n"
+ "Example: DEBUGCH=+all,warn-heap\n"
+ " turns on all messages except warning heap messages\n"
+ "Available message classes: err, warn, fixme, trace\n";
+ const int ret = write(2, usage, sizeof(usage) - 1);
+
+ assert(ret >= 0);
+ exit(1);
}
/* initialize all options at startup */
static void debug_init(void)
{
- char *debug = NULL;
- FILE *fp = NULL;
- char *tmp = NULL;
+ char *debug = NULL;
+ FILE *fp = NULL;
+ char *tmp = NULL;
int open_flags;
int fd;
- if (nb_debug_options != -1)
- return; /* already initialized */
+ if (nb_debug_options != -1) {
+ return; /* already initialized */
+ }
- nb_debug_options = 0;
+ nb_debug_options = 0;
#if 0
- strcpy(debugchfile, get_etc_path());
- strcat(debugchfile, "/DEBUGCH");
+ strcpy(debugchfile, get_etc_path());
+ strcat(debugchfile, "/DEBUGCH");
#endif
if (0 == strlen(bin_path)) {
strcat(debugchfile, "DEBUGCH");
}
- fp= fopen(debugchfile, "r");
- if( fp == NULL){
- debug = getenv("DEBUGCH");
- }else{
- if ((tmp= (char *)malloc(1024 + 1)) == NULL){
- fclose(fp);
- return;
- }
-
- fseek(fp, 0, SEEK_SET);
- const char* str = fgets(tmp, 1024, fp);
- if (str) {
- tmp[strlen(tmp)-1] = 0;
- debug = tmp;
- }
-
- fclose(fp);
- }
-
- if( debug != NULL ){
- if (!strcmp( debug, "help" ))
- debug_usage();
- parse_options( debug );
- }
-
- if( tmp != NULL ){
- free(tmp);
- }
-
- open_flags = O_BINARY | O_RDWR | O_CREAT | O_TRUNC;
- fd = qemu_open(log_path, open_flags, 0666);
- if(fd < 0) {
+ fp = fopen(debugchfile, "r");
+ if (fp == NULL) {
+ debug = getenv("DEBUGCH");
+ } else {
+ if ((tmp = (char *)malloc(1024 + 1)) == NULL){
+ fclose(fp);
+ return;
+ }
+
+ fseek(fp, 0, SEEK_SET);
+ const char* str = fgets(tmp, 1024, fp);
+ if (str) {
+ tmp[strlen(tmp) - 1] = 0;
+ debug = tmp;
+ }
+
+ fclose(fp);
+ }
+
+ if (debug != NULL) {
+ if (!strcmp(debug, "help")) {
+ debug_usage();
+ }
+ parse_options(debug);
+ }
+
+ if (tmp != NULL) {
+ free(tmp);
+ }
+
+ open_flags = O_BINARY | O_RDWR | O_CREAT | O_TRUNC;
+ fd = qemu_open(log_path, open_flags, 0666);
+ if (fd < 0) {
fprintf(stderr, "Can't open logfile: %s\n", log_path);
- exit(1);
+ exit(1);
}
+
close(fd);
}
/* allocate some tmp string space */
/* FIXME: this is not 100% thread-safe */
-char *get_dbg_temp_buffer( size_t size )
+char *get_dbg_temp_buffer(size_t size)
{
- static char *list[32];
- static int pos;
- char *ret;
- int idx;
+ static char *list[32];
+ static int pos;
+ char *ret;
+ int idx;
- idx = interlocked_xchg_add( &pos, 1 ) % (sizeof(list)/sizeof(list[0]));
+ idx = interlocked_xchg_add(&pos, 1) % (sizeof(list) / sizeof(list[0]));
- if ((ret = realloc( list[idx], size )))
- list[idx] = ret;
+ if ((ret = realloc(list[idx], size))) {
+ list[idx] = ret;
+ }
- return ret;
+ return ret;
}
/* release unused part of the buffer */
-void release_dbg_temp_buffer( char *buffer, size_t size )
+void release_dbg_temp_buffer(char *buffer, size_t size)
{
- /* don't bother doing anything */
- (void)( buffer );
- (void)( size );
+ /* don't bother doing anything */
+ (void)(buffer);
+ (void)(size);
}
-static int dbg_vprintf( const char *format, va_list args )
+static int dbg_vprintf(const char *format, va_list args)
{
- char tmp[MSGSIZE_MAX] = { 0, };
- char txt[MSGSIZE_MAX] = { 0, };
+ char tmp[MSGSIZE_MAX] = { 0, };
+ char txt[MSGSIZE_MAX] = { 0, };
- FILE *fp;
- // lock
+ FILE *fp;
+ // lock
- int ret = vsnprintf( tmp, MSGSIZE_MAX, format, args );
+ int ret = vsnprintf(tmp, MSGSIZE_MAX, format, args);
- tmp[MSGSIZE_MAX - 2] = '\n';
- tmp[MSGSIZE_MAX - 1] = 0;
+ tmp[MSGSIZE_MAX - 2] = '\n';
+ tmp[MSGSIZE_MAX - 1] = 0;
- sprintf(txt, "%s", tmp);
+ sprintf(txt, "%s", tmp);
+
+ // unlock
+ if ((fp = fopen(log_path, "a+")) == NULL) {
+ fprintf(stdout, "Emulator can't open.\n"
+ "Please check if "
+ "this binary file is running on the right path.\n");
+ exit(1);
+ }
- // unlock
- if ((fp = fopen(log_path, "a+")) == NULL) {
- fprintf(stdout, "Emulator can't open.\n"
- "Please check if "
- "this binary file is running on the right path.\n");
- exit(1);
- }
+ fputs(txt, fp);
+ fclose(fp);
- fputs(txt, fp);
- fclose(fp);
- return ret;
+ return ret;
}
-int dbg_printf( const char *format, ... )
+int dbg_printf(const char *format, ...)
{
- int ret;
- va_list valist;
+ int ret;
+ va_list valist;
- va_start(valist, format);
- ret = dbg_vprintf( format, valist );
- va_end(valist);
+ va_start(valist, format);
+ ret = dbg_vprintf(format, valist);
+ va_end(valist);
- return ret;
+ return ret;
}
-int dbg_printf_nonewline( const char *format, ... )
+int dbg_printf_nonewline(const char *format, ...)
{
- int ret;
- va_list valist;
+ int ret;
+ va_list valist;
- va_start(valist, format);
- ret = dbg_vprintf( format, valist );
- va_end(valist);
+ va_start(valist, format);
+ ret = dbg_vprintf(format, valist);
+ va_end(valist);
- return ret;
+ return ret;
}
/* printf with temp buffer allocation */
-const char *dbg_sprintf( const char *format, ... )
+const char *dbg_sprintf(const char *format, ...)
{
- static const int max_size = 200;
- char *ret;
- int len;
- va_list valist;
-
- va_start(valist, format);
- ret = get_dbg_temp_buffer( max_size );
- len = vsnprintf( ret, max_size, format, valist );
- if (len == -1 || len >= max_size)
- ret[max_size-1] = 0;
- else
- release_dbg_temp_buffer( ret, len + 1 );
- va_end(valist);
- return ret;
+ static const int max_size = 200;
+ char *ret;
+ int len;
+ va_list valist;
+
+ va_start(valist, format);
+ ret = get_dbg_temp_buffer(max_size);
+ len = vsnprintf(ret, max_size, format, valist);
+
+ if (len == -1 || len >= max_size) {
+ ret[max_size-1] = 0;
+ } else {
+ release_dbg_temp_buffer(ret, len + 1);
+ }
+ va_end(valist);
+
+ return ret;
}
-int dbg_log( enum _debug_class cls, struct _debug_channel *channel,
- const char *format, ... )
+int dbg_log(enum _debug_class cls, struct _debug_channel *channel,
+ const char *format, ...)
{
int ret = 0;
int ret_write = 0;
qemu_timeval tv = { 0, 0 };
time_t ti;
- if (!(_dbg_get_channel_flags( channel ) & (1 << cls))) {
+ if (!(_dbg_get_channel_flags(channel) & (1 << cls))) {
return -1;
}
void assert_fail(char *exp, const char *file, int line)
{
- fprintf(stderr, "[%s][%d] Assert(%s) failed \n"
- , file, line, exp);
- fprintf(stdout, "[%s][%d] Assert(%s) failed \n"
- , file, line, exp);
- exit(0);
+ fprintf(stderr, "[%s][%d] Assert(%s) failed \n"
+ , file, line, exp);
+ fprintf(stdout, "[%s][%d] Assert(%s) failed \n"
+ , file, line, exp);
+ exit(0);
}
/* end of debug_ch.c */
/**
- * @file debug_ch.h
- * @brief Management of the debugging channels
+ * @file debug_ch.h
+ * @brief Management of the debugging channels
*
- * @author
+ * @author
* @date
- * @attention
+ * @attention
*/
#ifndef __DEBUGCHANNEL_H
enum _debug_class
{
- __DBCL_FIXME,
- __DBCL_ERR,
- __DBCL_WARN,
- __DBCL_TRACE,
- __DBCL_INFO,
+ __DBCL_FIXME,
+ __DBCL_ERR,
+ __DBCL_WARN,
+ __DBCL_TRACE,
+ __DBCL_INFO,
- __DBCL_INIT = 7 /* lazy init flag */
+ __DBCL_INIT = 7 /* lazy init flag */
};
struct _debug_channel
{
- unsigned char flags;
- char name[15];
- char multiname[15];
+ unsigned char flags;
+ char name[15];
+ char multiname[15];
};
#ifndef NO_DEBUG
#define __GET_DEBUGGING(dbcl,dbch) __GET_DEBUGGING##dbcl(dbch)
#define __IS_DEBUG_ON(dbcl,dbch) \
- (__GET_DEBUGGING##dbcl(dbch) && \
- (_dbg_get_channel_flags(dbch) & (1 << __DBCL##dbcl)))
+ (__GET_DEBUGGING##dbcl(dbch) && \
+ (_dbg_get_channel_flags(dbch) & (1 << __DBCL##dbcl)))
#define __DPRINTF(dbcl,dbch) \
- do{ if(__GET_DEBUGGING(dbcl,(dbch))){ \
- struct _debug_channel * const __dbch =(struct _debug_channel *)(dbch); \
- const enum _debug_class __dbcl = __DBCL##dbcl; \
- _DBG_LOG
+ do{ if(__GET_DEBUGGING(dbcl,(dbch))){ \
+ struct _debug_channel * const __dbch =(struct _debug_channel *)(dbch); \
+ const enum _debug_class __dbcl = __DBCL##dbcl; \
+ _DBG_LOG
#define _DBG_LOG(args...) \
- dbg_log(__dbcl, (struct _debug_channel *)(__dbch), args); } }while(0)
+ dbg_log(__dbcl, (struct _debug_channel *)(__dbch), args); } }while(0)
/*
#define __DPRINTF(dbcl,dbch) \
- (!__GET_DEBUGGING(dbcl,(dbch)) || \
- (dbg_log(__DBCL##dbcl,(dbch), "") == -1)) ? \
+ (!__GET_DEBUGGING(dbcl,(dbch)) || \
+ (dbg_log(__DBCL##dbcl,(dbch), "") == -1)) ? \
(void)0 : (void)dbg_printf
*/
-extern unsigned char _dbg_get_channel_flags( struct _debug_channel *channel );
-extern int _dbg_set_channel_flags( struct _debug_channel *channel,
- unsigned char set, unsigned char clear );
+extern unsigned char _dbg_get_channel_flags(struct _debug_channel *channel);
+extern int _dbg_set_channel_flags(struct _debug_channel *channel,
+ unsigned char set, unsigned char clear);
-extern const char *dbg_sprintf( const char *format, ... );
-extern int dbg_printf( const char *format, ... );
-extern int dbg_printf_nonewline( const char *format, ... );
-extern int dbg_log( enum _debug_class cls, struct _debug_channel *ch,
- const char *format, ... );
+extern const char *dbg_sprintf(const char *format, ...);
+extern int dbg_printf(const char *format, ...);
+extern int dbg_printf_nonewline(const char *format, ...);
+extern int dbg_log(enum _debug_class cls, struct _debug_channel *ch,
+ const char *format, ...);
-extern char *get_dbg_temp_buffer( size_t size );
-extern void release_dbg_temp_buffer( char *buffer, size_t size );
+extern char *get_dbg_temp_buffer(size_t size);
+extern void release_dbg_temp_buffer(char *buffer, size_t size);
#ifndef TRACE
#define TRACE __DPRINTF(_TRACE,_dbch___default)
#define INFO_ON(ch) __IS_DEBUG_ON(_INFO,&_dbch_##ch)
#define DECLARE_DEBUG_CHANNEL(ch) \
- static struct _debug_channel _dbch_##ch = { ~0, #ch, ""};
+ static struct _debug_channel _dbch_##ch = { ~0, #ch, ""};
#define DEFAULT_DEBUG_CHANNEL(ch) \
- static struct _debug_channel _dbch_##ch = { ~0, #ch, ""}; \
+ static struct _debug_channel _dbch_##ch = { ~0, #ch, ""}; \
static struct _debug_channel * const _dbch___default = &_dbch_##ch
#define MULTI_DEBUG_CHANNEL(ch, chm) \
- static struct _debug_channel _dbch_##ch = { ~0, #ch , #chm}; \
+ static struct _debug_channel _dbch_##ch = { ~0, #ch , #chm}; \
static struct _debug_channel * const _dbch___default = &_dbch_##ch
#define DPRINTF dbg_printf
void assert_fail(char *exp, const char *file, int line);
#define ASSERT(exp) if (exp) ; \
- else assert_fail( (char *)#exp, __FILE__, __LINE__ )
+ else assert_fail( (char *)#exp, __FILE__, __LINE__ )
#ifdef __cplusplus
}