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