Xenomai  3.0.8
debug.h
1 /*
2  * Copyright (C) 2011 Philippe Gerum <rpm@xenomai.org>.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13 
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18 #ifndef _BOILERPLATE_DEBUG_H
19 #define _BOILERPLATE_DEBUG_H
20 
21 #include <stdint.h>
22 #include <stddef.h>
23 #include <xeno_config.h>
24 
25 #ifdef CONFIG_XENO_DEBUG
26 
27 #include <pthread.h>
28 #include <boilerplate/compiler.h>
29 
30 static inline int must_check(void)
31 {
32  return 1;
33 }
34 
35 struct error_frame {
36  int retval;
37  int lineno;
38  const char *fn;
39  const char *file;
40  struct error_frame *next;
41 };
42 
43 struct backtrace_data {
44  const char *name;
45  struct error_frame *inner;
46  pthread_mutex_t lock;
47  char eundef[16];
48 };
49 
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53 
54 void backtrace_init_context(struct backtrace_data *btd,
55  const char *name);
56 
57 void backtrace_destroy_context(struct backtrace_data *btd);
58 
59 void backtrace_dump(struct backtrace_data *btd);
60 
61 void backtrace_log(int retval, const char *fn,
62  const char *file, int lineno);
63 
64 void backtrace_check(void);
65 
66 void __debug(const char *name, const char *fmt, ...);
67 
68 char *__get_error_buf(size_t *sizep);
69 
70 void debug_init(void);
71 
72 #ifdef __cplusplus
73 }
74 #endif
75 
76 #define __bt(__exp) \
77  ({ \
78  typeof(__exp) __ret = (__exp); \
79  if (unlikely(__ret < 0)) \
80  backtrace_log((int)__ret, __FUNCTION__, \
81  __FILE__, __LINE__); \
82  __ret; \
83  })
84 
85 #define __bterrno(__exp) \
86  ({ \
87  typeof(__exp) __ret = (__exp); \
88  if (unlikely(__ret < 0)) \
89  backtrace_log(-errno, __FUNCTION__, \
90  __FILE__, __LINE__); \
91  __ret; \
92  })
93 
94 #else /* !CONFIG_XENO_DEBUG */
95 
96 static inline int must_check(void)
97 {
98  return 0;
99 }
100 
101 struct backtrace_data {
102 };
103 
104 #define __bt(__exp) (__exp)
105 
106 #define __bterrno(__exp) (__exp)
107 
108 #define backtrace_init_context(btd, name) \
109  do { (void)(btd); (void)(name); } while (0)
110 
111 #define backtrace_destroy_context(btd) \
112  do { (void)(btd); } while (0)
113 
114 #define backtrace_dump(btd) \
115  do { (void)(btd); } while (0)
116 
117 #define backtrace_check() \
118  do { } while (0)
119 /*
120  * XXX: We have no thread-private backtrace context in non-debug mode,
121  * so there is a potential race if multiple threads want to write to
122  * this buffer. This looks acceptable though, since this is primarily
123  * a debug feature, and the race won't damage the system anyway.
124  */
125 #define __get_error_buf(sizep) \
126  ({ \
127  static char __buf[16]; \
128  *(sizep) = sizeof(__buf); \
129  __buf; \
130  })
131 
132 #define debug_init() do { } while (0)
133 
134 #endif /* !CONFIG_XENO_DEBUG */
135 
136 static inline int bad_pointer(const void *ptr)
137 {
138  return ptr == NULL || ((intptr_t)ptr & (sizeof(intptr_t)-1)) != 0;
139 }
140 
141 #endif /* _BOILERPLATE_DEBUG_H */