]> git.proxmox.com Git - mirror_spl-debian.git/blob - include/sys/debug.h
Remove stray ` from macro
[mirror_spl-debian.git] / include / sys / debug.h
1 /*
2 * This file is part of the SPL: Solaris Porting Layer.
3 *
4 * Copyright (c) 2008 Lawrence Livermore National Security, LLC.
5 * Produced at Lawrence Livermore National Laboratory
6 * Written by:
7 * Brian Behlendorf <behlendorf1@llnl.gov>,
8 * Herb Wartens <wartens2@llnl.gov>,
9 * Jim Garlick <garlick@llnl.gov>
10 * UCRL-CODE-235197
11 *
12 * This is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 */
26
27 #ifndef _SPL_DEBUG_H
28 #define _SPL_DEBUG_H
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 #include <linux/sched.h> /* THREAD_SIZE */
35 #include <linux/proc_fs.h>
36
37 extern unsigned long spl_debug_mask;
38 extern unsigned long spl_debug_subsys;
39
40 #define S_UNDEFINED 0x00000001
41 #define S_ATOMIC 0x00000002
42 #define S_KOBJ 0x00000004
43 #define S_VNODE 0x00000008
44 #define S_TIME 0x00000010
45 #define S_RWLOCK 0x00000020
46 #define S_THREAD 0x00000040
47 #define S_CONDVAR 0x00000080
48 #define S_MUTEX 0x00000100
49 #define S_RNG 0x00000200
50 #define S_TASKQ 0x00000400
51 #define S_KMEM 0x00000800
52 #define S_DEBUG 0x00001000
53 #define S_GENERIC 0x00002000
54 #define S_PROC 0x00004000
55 #define S_MODULE 0x00008000
56
57 #define D_TRACE 0x00000001
58 #define D_INFO 0x00000002
59 #define D_WARNING 0x00000004
60 #define D_ERROR 0x00000008
61 #define D_EMERG 0x00000010
62 #define D_CONSOLE 0x00000020
63 #define D_IOCTL 0x00000040
64 #define D_DPRINTF 0x00000080
65 #define D_OTHER 0x00000100
66
67 #define D_CANTMASK (D_ERROR | D_EMERG | D_WARNING | D_CONSOLE)
68 #define DEBUG_SUBSYSTEM S_UNDEFINED
69
70 int debug_init(void);
71 void debug_fini(void);
72 int spl_debug_mask2str(char *str, int size, unsigned long mask, int is_subsys);
73 int spl_debug_str2mask(unsigned long *mask, const char *str, int is_subsys);
74
75 extern unsigned long spl_debug_subsys;
76 extern unsigned long spl_debug_mask;
77 extern unsigned long spl_debug_printk;
78 extern int spl_debug_mb;
79 extern unsigned int spl_debug_binary;
80 extern unsigned int spl_debug_catastrophe;
81 extern unsigned int spl_debug_panic_on_bug;
82 extern char spl_debug_file_path[PATH_MAX];
83 extern unsigned int spl_console_ratelimit;
84 extern long spl_console_max_delay;
85 extern long spl_console_min_delay;
86 extern unsigned int spl_console_backoff;
87 extern unsigned int spl_debug_stack;
88
89 #define TCD_MAX_PAGES (5 << (20 - PAGE_SHIFT))
90 #define TCD_STOCK_PAGES (TCD_MAX_PAGES)
91 #define TRACE_CONSOLE_BUFFER_SIZE 1024
92
93 #define SPL_DEFAULT_MAX_DELAY (600 * HZ)
94 #define SPL_DEFAULT_MIN_DELAY ((HZ + 1) / 2)
95 #define SPL_DEFAULT_BACKOFF 2
96
97 #define DL_NOTHREAD 0x0001 /* Do not create a new thread */
98 #define DL_SINGLE_CPU 0x0002 /* Collect pages from this CPU */
99
100 typedef struct dumplog_priv {
101 wait_queue_head_t dp_waitq;
102 pid_t dp_pid;
103 int dp_flags;
104 atomic_t dp_done;
105 } dumplog_priv_t;
106
107 typedef struct {
108 unsigned long cdls_next;
109 int cdls_count;
110 long cdls_delay;
111 } spl_debug_limit_state_t;
112
113 /* Three trace data types */
114 typedef enum {
115 TCD_TYPE_PROC,
116 TCD_TYPE_SOFTIRQ,
117 TCD_TYPE_IRQ,
118 TCD_TYPE_MAX
119 } tcd_type_t;
120
121 union trace_data_union {
122 struct trace_cpu_data {
123 /* pages with trace records not yet processed by tracefiled */
124 struct list_head tcd_pages;
125 /* number of pages on ->tcd_pages */
126 unsigned long tcd_cur_pages;
127 /* Max number of pages allowed on ->tcd_pages */
128 unsigned long tcd_max_pages;
129
130 /*
131 * preallocated pages to write trace records into. Pages from
132 * ->tcd_stock_pages are moved to ->tcd_pages by spl_debug_msg().
133 *
134 * This list is necessary, because on some platforms it's
135 * impossible to perform efficient atomic page allocation in a
136 * non-blockable context.
137 *
138 * Such platforms fill ->tcd_stock_pages "on occasion", when
139 * tracing code is entered in blockable context.
140 *
141 * trace_get_tage_try() tries to get a page from
142 * ->tcd_stock_pages first and resorts to atomic page
143 * allocation only if this queue is empty. ->tcd_stock_pages
144 * is replenished when tracing code is entered in blocking
145 * context (darwin-tracefile.c:trace_get_tcd()). We try to
146 * maintain TCD_STOCK_PAGES (40 by default) pages in this
147 * queue. Atomic allocation is only required if more than
148 * TCD_STOCK_PAGES pagesful are consumed by trace records all
149 * emitted in non-blocking contexts. Which is quite unlikely.
150 */
151 struct list_head tcd_stock_pages;
152 /* number of pages on ->tcd_stock_pages */
153 unsigned long tcd_cur_stock_pages;
154
155 unsigned short tcd_shutting_down;
156 unsigned short tcd_cpu;
157 unsigned short tcd_type;
158 /* The factors to share debug memory. */
159 unsigned short tcd_pages_factor;
160
161 /*
162 * This spinlock is needed to workaround the problem of
163 * set_cpus_allowed() being GPL-only. Since we cannot
164 * schedule a thread on a specific CPU when dumping the
165 * pages, we must use the spinlock for mutual exclusion.
166 */
167 spinlock_t tcd_lock;
168 unsigned long tcd_lock_flags;
169 } tcd;
170 char __pad[L1_CACHE_ALIGN(sizeof(struct trace_cpu_data))];
171 };
172
173 extern union trace_data_union (*trace_data[TCD_TYPE_MAX])[NR_CPUS];
174
175 #define tcd_for_each(tcd, i, j) \
176 for (i = 0; trace_data[i] != NULL; i++) \
177 for (j = 0, ((tcd) = &(*trace_data[i])[j].tcd); \
178 j < num_possible_cpus(); j++, (tcd) = &(*trace_data[i])[j].tcd)
179
180 #define tcd_for_each_type_lock(tcd, i, cpu) \
181 for (i = 0; trace_data[i] && \
182 (tcd = &(*trace_data[i])[cpu].tcd) && \
183 trace_lock_tcd(tcd); trace_unlock_tcd(tcd), i++)
184
185 struct trace_page {
186 struct page * page; /* page itself */
187 struct list_head linkage; /* Used by lists in trace_data_union */
188 unsigned int used; /* number of bytes used within this page */
189 unsigned short cpu; /* cpu that owns this page */
190 unsigned short type; /* type(context) of this page */
191 };
192
193 struct page_collection {
194 struct list_head pc_pages;
195 spinlock_t pc_lock;
196 int pc_want_daemon_pages;
197 };
198
199 #define SBUG() spl_debug_bug(__FILE__, __FUNCTION__, __LINE__, 0);
200
201 #ifdef NDEBUG
202
203 #define CDEBUG_STACK() (0)
204 #define CDEBUG_LIMIT(x, y, z, a...) ((void)0)
205 #define __CDEBUG_LIMIT(x, y, z, a...) ((void)0)
206 #define CDEBUG(mask, format, a...) ((void)0)
207 #define CWARN(fmt, a...) ((void)0)
208 #define CERROR(fmt, a...) ((void)0)
209 #define CEMERG(fmt, a...) ((void)0)
210 #define CONSOLE(mask, fmt, a...) ((void)0)
211
212 #define ENTRY ((void)0)
213 #define EXIT ((void)0)
214 #define RETURN(x) return (x)
215 #define GOTO(x, y) { ((void)(y)); goto x; }
216
217 #define __ASSERT(x) ((void)0)
218 #define __ASSERT_TAGE_INVARIANT(x) ((void)0)
219 #define ASSERT(x) ((void)0)
220 #define ASSERTF(x, y, z...) ((void)0)
221 #define VERIFY(cond) \
222 do { \
223 if (unlikely(!(cond))) { \
224 printk(KERN_ERR "VERIFY(" #cond ") failed\n"); \
225 SBUG(); \
226 } \
227 } while (0)
228
229 #define VERIFY3_IMPL(LEFT, OP, RIGHT, TYPE, FMT, CAST) \
230 do { \
231 if (!((TYPE)(LEFT) OP (TYPE)(RIGHT))) { \
232 printk(KERN_ERR \
233 "VERIFY3(" #LEFT " " #OP " " #RIGHT ") " \
234 "failed (" FMT " " #OP " " FMT ")\n", \
235 CAST (LEFT), CAST (RIGHT)); \
236 SBUG(); \
237 } \
238 } while (0)
239
240 #define VERIFY3S(x,y,z) VERIFY3_IMPL(x, y, z, int64_t, "%lld", (long long))
241 #define VERIFY3U(x,y,z) VERIFY3_IMPL(x, y, z, uint64_t, "%llu", \
242 (unsigned long long))
243 #define VERIFY3P(x,y,z) VERIFY3_IMPL(x, y, z, uintptr_t, "%p", (void *))
244
245 #define ASSERT3S(x,y,z) ((void)0)
246 #define ASSERT3U(x,y,z) ((void)0)
247 #define ASSERT3P(x,y,z) ((void)0)
248
249 #else /* NDEBUG */
250
251 #ifdef __ia64__
252 #define CDEBUG_STACK() (THREAD_SIZE - \
253 ((unsigned long)__builtin_dwarf_cfa() & \
254 (THREAD_SIZE - 1)))
255 #else
256 #define CDEBUG_STACK() (THREAD_SIZE - \
257 ((unsigned long)__builtin_frame_address(0) & \
258 (THREAD_SIZE - 1)))
259 # endif /* __ia64__ */
260
261 /* DL_SINGLE_CPU flag is passed to spl_debug_bug() because we are about
262 * to over run our stack and likely damage at least one other unknown
263 * thread stack. We must finish generating the needed debug info within
264 * this thread context because once we yeild the CPU its very likely
265 * the system will crash.
266 */
267 #define __CHECK_STACK(file, func, line) \
268 do { \
269 if (unlikely(CDEBUG_STACK() > spl_debug_stack)) { \
270 spl_debug_stack = CDEBUG_STACK(); \
271 \
272 if (unlikely(CDEBUG_STACK() > (4 * THREAD_SIZE) / 5)) { \
273 spl_debug_msg(NULL, D_TRACE, D_WARNING, \
274 file, func, line, "Error " \
275 "exceeded maximum safe stack " \
276 "size (%lu/%lu)\n", \
277 CDEBUG_STACK(), THREAD_SIZE); \
278 spl_debug_bug(file, func, line, DL_SINGLE_CPU); \
279 } \
280 } \
281 } while (0)
282
283 #define CHECK_STACK() __CHECK_STACK(__FILE__, __func__, __LINE__)
284
285 /* ASSERTION that is safe to use within the debug system */
286 #define __ASSERT(cond) \
287 do { \
288 if (unlikely(!(cond))) { \
289 printk(KERN_ERR "ASSERTION(" #cond ") failed\n"); \
290 BUG(); \
291 } \
292 } while (0)
293
294 #define __ASSERT_TAGE_INVARIANT(tage) \
295 do { \
296 __ASSERT(tage != NULL); \
297 __ASSERT(tage->page != NULL); \
298 __ASSERT(tage->used <= PAGE_SIZE); \
299 __ASSERT(page_count(tage->page) > 0); \
300 } while(0)
301
302 /* ASSERTION that will debug log used outside the debug sysytem */
303 #define ASSERT(cond) \
304 do { \
305 CHECK_STACK(); \
306 \
307 if (unlikely(!(cond))) { \
308 spl_debug_msg(NULL, DEBUG_SUBSYSTEM, D_EMERG, \
309 __FILE__, __FUNCTION__, __LINE__, \
310 "ASSERTION(" #cond ") failed\n"); \
311 SBUG(); \
312 } \
313 } while (0)
314
315 #define ASSERTF(cond, fmt, a...) \
316 do { \
317 CHECK_STACK(); \
318 \
319 if (unlikely(!(cond))) { \
320 spl_debug_msg(NULL, DEBUG_SUBSYSTEM, D_EMERG, \
321 __FILE__, __FUNCTION__, __LINE__, \
322 "ASSERTION(" #cond ") failed: " fmt, \
323 ## a); \
324 SBUG(); \
325 } \
326 } while (0)
327
328 #define VERIFY3_IMPL(LEFT, OP, RIGHT, TYPE, FMT, CAST) \
329 do { \
330 CHECK_STACK(); \
331 \
332 if (!((TYPE)(LEFT) OP (TYPE)(RIGHT))) { \
333 spl_debug_msg(NULL, DEBUG_SUBSYSTEM, D_EMERG, \
334 __FILE__, __FUNCTION__, __LINE__, \
335 "VERIFY3(" #LEFT " " #OP " " #RIGHT ") " \
336 "failed (" FMT " " #OP " " FMT ")\n", \
337 CAST (LEFT), CAST (RIGHT)); \
338 SBUG(); \
339 } \
340 } while (0)
341
342 #define VERIFY3S(x,y,z) VERIFY3_IMPL(x, y, z, int64_t, "%lld", (long long))
343 #define VERIFY3U(x,y,z) VERIFY3_IMPL(x, y, z, uint64_t, "%llu", \
344 (unsigned long long))
345 #define VERIFY3P(x,y,z) VERIFY3_IMPL(x, y, z, uintptr_t, "%p", (void *))
346
347 #define ASSERT3S(x,y,z) VERIFY3S(x, y, z)
348 #define ASSERT3U(x,y,z) VERIFY3U(x, y, z)
349 #define ASSERT3P(x,y,z) VERIFY3P(x, y, z)
350
351 #define VERIFY(x) ASSERT(x)
352
353 #define __CDEBUG(cdls, subsys, mask, format, a...) \
354 do { \
355 CHECK_STACK(); \
356 \
357 if (((mask) & D_CANTMASK) != 0 || \
358 ((spl_debug_mask & (mask)) != 0 && \
359 (spl_debug_subsys & (subsys)) != 0)) \
360 spl_debug_msg(cdls, subsys, mask, \
361 __FILE__, __FUNCTION__, __LINE__, \
362 format, ## a); \
363 } while (0)
364
365 #define CDEBUG(mask, format, a...) \
366 __CDEBUG(NULL, DEBUG_SUBSYSTEM, mask, format, ## a)
367
368 #define __CDEBUG_LIMIT(subsys, mask, format, a...) \
369 do { \
370 static spl_debug_limit_state_t cdls; \
371 \
372 __CDEBUG(&cdls, subsys, mask, format, ## a); \
373 } while (0)
374
375 #define CDEBUG_LIMIT(mask, format, a...) \
376 __CDEBUG_LIMIT(DEBUG_SUBSYSTEM, mask, format, ## a)
377
378 #define CWARN(fmt, a...) CDEBUG_LIMIT(D_WARNING, fmt, ## a)
379 #define CERROR(fmt, a...) CDEBUG_LIMIT(D_ERROR, fmt, ## a)
380 #define CEMERG(fmt, a...) CDEBUG_LIMIT(D_EMERG, fmt, ## a)
381 #define CONSOLE(mask, fmt, a...) CDEBUG(D_CONSOLE | (mask), fmt, ## a)
382
383 #define GOTO(label, rc) \
384 do { \
385 long GOTO__ret = (long)(rc); \
386 CDEBUG(D_TRACE,"Process leaving via %s (rc=%lu : %ld : %lx)\n", \
387 #label, (unsigned long)GOTO__ret, (signed long)GOTO__ret,\
388 (signed long)GOTO__ret); \
389 goto label; \
390 } while (0)
391
392 #define RETURN(rc) \
393 do { \
394 typeof(rc) RETURN__ret = (rc); \
395 CDEBUG(D_TRACE, "Process leaving (rc=%lu : %ld : %lx)\n", \
396 (long)RETURN__ret, (long)RETURN__ret, (long)RETURN__ret);\
397 return RETURN__ret; \
398 } while (0)
399
400 #define __ENTRY(subsys) \
401 do { \
402 __CDEBUG(NULL, subsys, D_TRACE, "Process entered\n"); \
403 } while (0)
404
405 #define __EXIT(subsys) \
406 do { \
407 __CDEBUG(NULL, subsys, D_TRACE, "Process leaving\n"); \
408 } while(0)
409
410 #define ENTRY __ENTRY(DEBUG_SUBSYSTEM)
411 #define EXIT __EXIT(DEBUG_SUBSYSTEM)
412 #endif /* NDEBUG */
413
414 #define spl_debug_msg(cdls, subsys, mask, file, fn, line, format, a...) \
415 spl_debug_vmsg(cdls, subsys, mask, file, fn, \
416 line, NULL, NULL, format, ##a)
417
418 extern int spl_debug_vmsg(spl_debug_limit_state_t *cdls, int subsys, int mask,
419 const char *file, const char *fn, const int line,
420 const char *format1, va_list args, const char *format2, ...);
421
422 extern unsigned long spl_debug_set_mask(unsigned long mask);
423 extern unsigned long spl_debug_get_mask(void);
424 extern unsigned long spl_debug_set_subsys(unsigned long mask);
425 extern unsigned long spl_debug_get_subsys(void);
426 extern int spl_debug_set_mb(int mb);
427 extern int spl_debug_get_mb(void);
428
429 extern int spl_debug_dumplog(int flags);
430 extern void spl_debug_dumpstack(struct task_struct *tsk);
431 extern void spl_debug_bug(char *file, const char *func, const int line, int flags);
432
433 extern int spl_debug_clear_buffer(void);
434 extern int spl_debug_mark_buffer(char *text);
435
436 #ifdef __cplusplus
437 }
438 #endif
439
440 #endif /* SPL_DEBUG_H */