blob: 39968984bd4f66507570d7dbce7c991c9441b667 [file] [log] [blame]
The Android Open Source Project88b60792009-03-03 19:28:42 -08001#ifndef DEBUG_H
2#define DEBUG_H
3
4#include <stdlib.h>
5#include <stdio.h>
6#include <common.h>
7
8#ifdef DEBUG
9
10 #define FAILIF(cond, msg...) do { \
11 if (unlikely(cond)) { \
12 fprintf(stderr, "%s(%d): ", __FILE__, __LINE__); \
13 fprintf(stderr, ##msg); \
14 exit(1); \
15 } \
16} while(0)
17
18/* Debug enabled */
19 #define ASSERT(x) do { \
20 if (unlikely(!(x))) { \
21 fprintf(stderr, \
22 "ASSERTION FAILURE %s:%d: [%s]\n", \
23 __FILE__, __LINE__, #x); \
24 exit(1); \
25 } \
26} while(0)
27
28#else
29
30 #define FAILIF(cond, msg...) do { \
31 if (unlikely(cond)) { \
32 fprintf(stderr, ##msg); \
33 exit(1); \
34 } \
35} while(0)
36
37/* No debug */
38 #define ASSERT(x) do { } while(0)
39
40#endif/* DEBUG */
41
42#define FAILIF_LIBELF(cond, function) \
43 FAILIF(cond, "%s(): %s\n", #function, elf_errmsg(elf_errno()));
44
45static inline void *MALLOC(unsigned int size) {
46 void *m = malloc(size);
47 FAILIF(NULL == m, "malloc(%d) failed!\n", size);
48 return m;
49}
50
51static inline void *CALLOC(unsigned int num_entries, unsigned int entry_size) {
52 void *m = calloc(num_entries, entry_size);
53 FAILIF(NULL == m, "calloc(%d, %d) failed!\n", num_entries, entry_size);
54 return m;
55}
56
57static inline void *REALLOC(void *ptr, unsigned int size) {
58 void *m = realloc(ptr, size);
59 FAILIF(NULL == m, "realloc(%p, %d) failed!\n", ptr, size);
60 return m;
61}
62
63static inline void FREE(void *ptr) {
64 free(ptr);
65}
66
67static inline void FREEIF(void *ptr) {
68 if (ptr) FREE(ptr);
69}
70
71#define PRINT(x...) do { \
72 extern int quiet_flag; \
73 if(likely(!quiet_flag)) \
74 fprintf(stdout, ##x); \
75} while(0)
76
77#define ERROR PRINT
78
79#define INFO(x...) do { \
80 extern int verbose_flag; \
81 if(unlikely(verbose_flag)) \
82 fprintf(stdout, ##x); \
83} while(0)
84
85/* Prints a hex and ASCII dump of the selected buffer to the selected stream. */
86int dump_hex_buffer(FILE *s, void *b, size_t l, size_t elsize);
87
88#endif/*DEBUG_H*/