summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--common/doc/Doxyfile2
-rw-r--r--common/std.h1
-rw-r--r--lib/Module2
-rw-r--r--lib/dbg.h160
-rw-r--r--lib/rnd.h4
-rw-r--r--lib/src/dbg.c52
-rw-r--r--lib/types.h2
-rw-r--r--mac/design/test/sacki/src/test_sacki.c10
8 files changed, 225 insertions, 8 deletions
diff --git a/common/doc/Doxyfile b/common/doc/Doxyfile
index 1b015b2401..ced497b421 100644
--- a/common/doc/Doxyfile
+++ b/common/doc/Doxyfile
@@ -1012,7 +1012,7 @@ INCLUDE_FILE_PATTERNS =
# undefined via #undef or recursively expanded use the := operator
# instead of the = operator.
-PREDEFINED = "DOXYGEN_ONLY=1"
+PREDEFINED = "DOXYGEN_ONLY=1", "__attribute__(x)="
# If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then
# this tag can be used to specify a list of macro names that should be expanded.
diff --git a/common/std.h b/common/std.h
index be57e0affb..5d5d1233bc 100644
--- a/common/std.h
+++ b/common/std.h
@@ -17,5 +17,6 @@
#include "lib/types.h"
#include "lib/utils.h"
+#include "lib/dbg.h"
#endif /* common_std_h */
diff --git a/lib/Module b/lib/Module
index d89609c3e1..dced2ea987 100644
--- a/lib/Module
+++ b/lib/Module
@@ -1 +1 @@
-SOURCES = mt19937ar.c
+SOURCES = mt19937ar.c dbg.c
diff --git a/lib/dbg.h b/lib/dbg.h
new file mode 100644
index 0000000000..ed5f2b9d93
--- /dev/null
+++ b/lib/dbg.h
@@ -0,0 +1,160 @@
+#ifndef dbg_h
+#define dbg_h
+/* Maria project {{{
+ *
+ * Copyright (C) 2007 Spidcom
+ *
+ * <<<Licence>>>
+ *
+ * }}} */
+/**
+ * \file dbg.h
+ * \brief Debug functions.
+ * \ingroup lib
+ */
+
+/** \todo take this from config. */
+#define DEBUG 1
+
+/**
+ * Check that the provided expression is true.
+ * \param expr the assumed truth
+ *
+ * \warning \a expr is not evaluated when asserts are disabled.
+ */
+#define dbg_assert(expr) \
+ dbg_assert_ (expr, #expr)
+
+/**
+ * Check that the provided expression is true and provide more information on
+ * failure.
+ * \param expr the assumed truth
+ * \param fmt printf-like format string
+ * \param rest printf-like arguments
+ *
+ * \warning \a expr is not evaluated when asserts are disabled.
+ */
+#define dbg_assert_print(expr, fmt, rest...) \
+ dbg_assert_print_ (expr, fmt, ## rest)
+
+/**
+ * Check that the provided expression is true and print additional errno
+ * information on failure.
+ * \param expr the assumed truth
+ *
+ * \warning \a expr is not evaluated when asserts are disabled.
+ */
+#define dbg_assert_perror(expr) \
+ dbg_assert_perror_ (expr, #expr)
+
+/**
+ * Check that the provided expression is true, evaluate the expression even
+ * when asserts are disabled.
+ * \param expr the assumed truth
+ *
+ * \warning \a expr is always evaluated.
+ */
+#define dbg_check(expr) \
+ dbg_check_ (expr, #expr)
+
+/**
+ * Check that the provided expression is true, evaluate the expression even
+ * when asserts are disabled and provide more information on failure.
+ * \param expr the assumed truth
+ * \param fmt printf-like format string
+ * \param rest printf-like arguments
+ *
+ * \warning \a expr is always evaluated.
+ */
+#define dbg_check_print(expr, fmt, rest...) \
+ dbg_check_print_ (expr, fmt, ## rest)
+
+/**
+ * Check that the provided expression is true and print additional errno
+ * information on failure, evaluate the expression even when asserts are
+ * disabled.
+ * \param expr the assumed truth
+ *
+ * \warning \a expr is always evaluated.
+ */
+#define dbg_check_perror(expr) \
+ dbg_check_perror_ (expr, #expr)
+
+/* Asserts definitions. */
+#if DEBUG
+# define dbg_assert_ dbg_assert__
+# define dbg_assert_print_ dbg_assert_print__
+# define dbg_assert_perror_ dbg_assert_perror__
+# define dbg_check_ dbg_assert__
+# define dbg_check_print_ dbg_assert_print__
+# define dbg_check_perror_ dbg_assert_perror__
+# define dbg_assert__(expr, exprs) \
+ ((void) ((expr) ? 0 : (dbg_assert_fail (exprs, __FILE__, __LINE__, \
+ __PRETTY_FUNCTION__), 0)))
+# define dbg_assert_print__(expr, fmt, rest...) \
+ ((void) ((expr) ? 0 : (dbg_assert_print_fail (fmt, __FILE__, __LINE__, \
+ __PRETTY_FUNCTION__, \
+ ##rest), 0)))
+# define dbg_assert_perror__(expr, exprs) \
+ ((void) ((expr) ? 0 : (dbg_assert_perror_fail (exprs, __FILE__, __LINE__, \
+ __PRETTY_FUNCTION__), 0)))
+#else
+# define dbg_assert_ dbg_void__
+# define dbg_assert_print_ dbg_void_print__
+# define dbg_assert_perror_ dbg_void__
+# define dbg_check_ dbg_eval__
+# define dbg_check_print_ dbg_eval_print__
+# define dbg_check_perror_ dbg_eval__
+# define dbg_void__(expr, exprs) ((void) 0)
+# define dbg_eval__(expr, exprs) ((void) (expr, 0))
+# define dbg_void_print__(expr, fmt, rest...) ((void) 0)
+# define dbg_eval_print__(expr, fmt, rest...) ((void) (expr, 0))
+#endif
+
+/**
+ * Called on assertion failure.
+ * \param assertion assertion text
+ * \param file source file name
+ * \param line source file line
+ * \param function source function name
+ */
+void
+dbg_assert_fail (const char *assertion, const char *file, uint line,
+ const char *function) __attribute__ ((__noreturn__));
+
+/**
+ * Called on assertion failure with more information on failure.
+ * \param file source file name
+ * \param line source file line
+ * \param function source function name
+ * \param fmt printf-like format string
+ */
+void
+dbg_assert_print_fail (const char *fmt, const char *file, uint line,
+ const char *function, ...)
+ __attribute__ ((__noreturn__, format (printf, 1, 5)));
+
+/**
+ * Called on errno assertion failure.
+ * \param assertion assertion text
+ * \param file source file name
+ * \param line source file line
+ * \param function source function name
+ *
+ * \todo implement
+ */
+void
+dbg_assert_perror_fail (const char *assertion, const char *file, uint line,
+ const char *function) __attribute__ ((__noreturn__));
+
+/**
+ * Stop the program with a fatal error.
+ * \param fmt printf-like format string
+ *
+ * \warning interface may change in near future.
+ */
+void
+dbg_fatal (const char *fmt, ...)
+ __attribute__ ((__noreturn__, format (printf, 1, 2)));
+
+#endif /* dbg_h */
diff --git a/lib/rnd.h b/lib/rnd.h
index 8beafd13f1..627b24d8fa 100644
--- a/lib/rnd.h
+++ b/lib/rnd.h
@@ -22,6 +22,10 @@ struct lib_rnd_t
};
typedef struct lib_rnd_t lib_rnd_t;
+/** Generate a integer which has a probability of \a prob to be greater than a
+ * random 32 bit integer. */
+#define LIB_RND_RATIO(prob) ((u32) ((1ull << 32) * (prob)))
+
void
lib_rnd_init (lib_rnd_t *ctx, u32 seed);
void
diff --git a/lib/src/dbg.c b/lib/src/dbg.c
new file mode 100644
index 0000000000..50c809b437
--- /dev/null
+++ b/lib/src/dbg.c
@@ -0,0 +1,52 @@
+/* Maria project {{{
+ *
+ * Copyright (C) 2007 Spidcom
+ *
+ * <<<Licence>>>
+ *
+ * }}} */
+/**
+ * \file dbg.c
+ * \brief Debug functions.
+ * \ingroup lib
+ */
+#include "common/std.h"
+
+#include <stdio.h>
+#include <stdarg.h>
+#include <stdlib.h>
+
+#define DBG_ASSERT_FMT "%s:%d: assertion failure in %s: "
+
+void
+dbg_assert_fail (const char *assertion, const char *file, uint line,
+ const char *function)
+{
+ dbg_fatal (DBG_ASSERT_FMT "%s\n", file, line, function,
+ assertion);
+}
+
+void
+dbg_assert_print_fail (const char *fmt, const char *file, uint line,
+ const char *function, ...)
+{
+ /** \todo this is evil, we could stack overflow and we could have not
+ * enough room in this buffer. */
+ char buf[2048];
+ va_list ap;
+ va_start (ap, function);
+ vsnprintf (buf, sizeof (buf), fmt, ap);
+ va_end (ap);
+ dbg_fatal (DBG_ASSERT_FMT "%s\n", file, line, function, buf);
+}
+
+void
+dbg_fatal (const char *fmt, ...)
+{
+ va_list ap;
+ va_start (ap, fmt);
+ vfprintf (stderr, fmt, ap);
+ va_end (ap);
+ abort ();
+}
+
diff --git a/lib/types.h b/lib/types.h
index 0066e761d6..db908b35f4 100644
--- a/lib/types.h
+++ b/lib/types.h
@@ -41,6 +41,6 @@ typedef unsigned short u16;
typedef signed short s16;
typedef unsigned char u8;
typedef signed char s8;
-/*@{*/
+/*@}*/
#endif /* lib_types_h */
diff --git a/mac/design/test/sacki/src/test_sacki.c b/mac/design/test/sacki/src/test_sacki.c
index 7008ee9ba9..ed723ae3f7 100644
--- a/mac/design/test/sacki/src/test_sacki.c
+++ b/mac/design/test/sacki/src/test_sacki.c
@@ -30,7 +30,7 @@ print_bin (u32 w, uint b)
{
for (; b; b--, w >>= 1)
putchar ((w & 1) ? '1' : '0');
- //assert (!w);
+ dbg_assert (!w);
}
#endif /* DEBUG_COMPRESS || DEBUG_UNCOMPRESS */
@@ -350,7 +350,7 @@ compress_sacki (u32 *bmp, uint bmpl, u32 *si, uint sis, uint sil)
}
sil -= codel;
/* Load words? */
- //assert (bmp_eat <= bmp0l || bmpl == 0);
+ dbg_assert (bmp_eat <= bmp0l || bmpl == 0);
bmp_eat = MIN (bmp_eat, bmp0l);
bmp0 >>= bmp_eat;
bmp0 |= bmp1 << (32 - bmp_eat);
@@ -407,7 +407,7 @@ test_compress (const char *in, const char *out, uint offset)
if (s[0] == ' ' && s[1] == ' ')
bmpp_fit = bmpp;
while (*s == ' ') s++;
- //assert (*s == '1' || *s == '0');
+ dbg_assert (*s == '1' || *s == '0');
if (*s == '1')
bmp[bmpp / 32] |= 1u << (bmpp % 32);
bmpp++;
@@ -415,7 +415,7 @@ test_compress (const char *in, const char *out, uint offset)
/* Fill sil. */
uint sil;
u32 si[3];
- //assert (offset < 72);
+ dbg_assert (offset < 72);
for (i = 0; i < COUNT (si); i++)
{
if (offset <= i * 32)
@@ -443,7 +443,7 @@ test_compress (const char *in, const char *out, uint offset)
for (i = offset, s = out; i < sil; i++)
{
while (*s == ' ') s++;
- //assert (*s == '1' || *s == '0' || *s == '\0');
+ dbg_assert (*s == '1' || *s == '0' || *s == '\0');
u32 b = si[i / 32] & (1u << (i % 32));
if (!*s || (*s == '1' && !b) || (*s == '0' && b))
{