blob: f7842f89e19fcb3d99f0def2d429188568731e3c [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{
47 void *m = malloc(size);
48 FAILIF(NULL == m, "malloc(%d) failed!\n", size);
49 return m;
50}
51
52static inline void *CALLOC(unsigned int num_entries, unsigned int entry_size)
53{
54 void *m = calloc(num_entries, entry_size);
55 FAILIF(NULL == m, "calloc(%d, %d) failed!\n", num_entries, entry_size);
56 return m;
57}
58
59static inline void *REALLOC(void *ptr, unsigned int size)
60{
61 void *m = realloc(ptr, size);
62 FAILIF(NULL == m, "realloc(%p, %d) failed!\n", ptr, size);
63 return m;
64}
65
66static inline void FREE(void *ptr)
67{
68 free(ptr);
69}
70
71static inline void FREEIF(void *ptr)
72{
73 if (ptr) FREE(ptr);
74}
75
76#define PRINT(x...) do { \
77 extern int quiet_flag; \
78 if(likely(!quiet_flag)) \
79 fprintf(stdout, ##x); \
80} while(0)
81
82#define ERROR(x...) fprintf(stderr, ##x)
83
84#define INFO(x...) do { \
85 extern int verbose_flag; \
86 if(unlikely(verbose_flag)) \
87 fprintf(stdout, ##x); \
88} while(0)
89
90/* Prints a hex and ASCII dump of the selected buffer to the selected stream. */
91int dump_hex_buffer(FILE *s, void *b, size_t l, size_t elsize);
92
93#endif/*DEBUG_H*/