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>.
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://github.com/behlendorf/spl/>.
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.
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
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 \*****************************************************************************/
28 #include <linux/sched.h> /* THREAD_SIZE */
29 #include <linux/proc_fs.h>
31 extern unsigned long spl_debug_mask
;
32 extern unsigned long spl_debug_subsys
;
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
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
62 #define D_CANTMASK (D_ERROR | D_EMERG | D_WARNING | D_CONSOLE)
63 #define DEBUG_SUBSYSTEM S_UNDEFINED
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
);
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
;
84 #define TCD_MAX_PAGES (5 << (20 - PAGE_SHIFT))
85 #define TCD_STOCK_PAGES (TCD_MAX_PAGES)
86 #define TRACE_CONSOLE_BUFFER_SIZE 1024
88 #define SPL_DEFAULT_MAX_DELAY (600 * HZ)
89 #define SPL_DEFAULT_MIN_DELAY ((HZ + 1) / 2)
90 #define SPL_DEFAULT_BACKOFF 2
92 #define DL_NOTHREAD 0x0001 /* Do not create a new thread */
93 #define DL_SINGLE_CPU 0x0002 /* Collect pages from this CPU */
95 typedef struct dumplog_priv
{
96 wait_queue_head_t dp_waitq
;
103 unsigned long cdls_next
;
106 } spl_debug_limit_state_t
;
108 /* Three trace data types */
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
;
126 * preallocated pages to write trace records into. Pages from
127 * ->tcd_stock_pages are moved to ->tcd_pages by spl_debug_msg().
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.
133 * Such platforms fill ->tcd_stock_pages "on occasion", when
134 * tracing code is entered in blockable context.
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.
146 struct list_head tcd_stock_pages
;
147 /* number of pages on ->tcd_stock_pages */
148 unsigned long tcd_cur_stock_pages
;
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
;
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.
163 unsigned long tcd_lock_flags
;
165 char __pad
[L1_CACHE_ALIGN(sizeof(struct trace_cpu_data
))];
168 extern union trace_data_union (*trace_data
[TCD_TYPE_MAX
])[NR_CPUS
];
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)
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++)
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 */
188 struct page_collection
{
189 struct list_head pc_pages
;
191 int pc_want_daemon_pages
;
194 #define SBUG() spl_debug_bug(__FILE__, __FUNCTION__, __LINE__, 0);
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)
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; }
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)
217 #define VERIFY(cond) \
219 if (unlikely(!(cond))) { \
220 printk(KERN_ERR "VERIFY(" #cond ") failed\n"); \
225 #define VERIFY3_IMPL(LEFT, OP, RIGHT, TYPE, FMT, CAST) \
227 if (!((TYPE)(LEFT) OP (TYPE)(RIGHT))) { \
229 "VERIFY3(" #LEFT " " #OP " " #RIGHT ") " \
230 "failed (" FMT " " #OP " " FMT ")\n", \
231 CAST (LEFT), CAST (RIGHT)); \
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 *))
241 #define ASSERT3S(x,y,z) ((void)0)
242 #define ASSERT3U(x,y,z) ((void)0)
243 #define ASSERT3P(x,y,z) ((void)0)
248 #define CDEBUG_STACK() (THREAD_SIZE - \
249 ((unsigned long)__builtin_dwarf_cfa() & \
252 #define CDEBUG_STACK() (THREAD_SIZE - \
253 ((unsigned long)__builtin_frame_address(0) & \
255 # endif /* __ia64__ */
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.
263 #define __CHECK_STACK(file, func, line) \
265 if (unlikely(CDEBUG_STACK() > spl_debug_stack)) { \
266 spl_debug_stack = CDEBUG_STACK(); \
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); \
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().
285 * #define CHECK_STACK() __CHECK_STACK(__FILE__, __func__, __LINE__)
287 #define CHECK_STACK() ((void)0)
289 /* ASSERTION that is safe to use within the debug system */
290 #define __ASSERT(cond) \
292 if (unlikely(!(cond))) { \
293 printk(KERN_ERR "ASSERTION(" #cond ") failed\n"); \
298 #define __ASSERT_TAGE_INVARIANT(tage) \
300 __ASSERT(tage != NULL); \
301 __ASSERT(tage->page != NULL); \
302 __ASSERT(tage->used <= PAGE_SIZE); \
303 __ASSERT(page_count(tage->page) > 0); \
306 /* ASSERTION that will debug log used outside the debug sysytem */
307 #define ASSERT(cond) \
311 if (unlikely(!(cond))) { \
312 spl_debug_msg(NULL, DEBUG_SUBSYSTEM, D_EMERG, \
313 __FILE__, __FUNCTION__, __LINE__, \
314 "ASSERTION(" #cond ") failed\n"); \
319 #define ASSERTF(cond, fmt, a...) \
323 if (unlikely(!(cond))) { \
324 spl_debug_msg(NULL, DEBUG_SUBSYSTEM, D_EMERG, \
325 __FILE__, __FUNCTION__, __LINE__, \
326 "ASSERTION(" #cond ") failed: " fmt, \
332 #define VERIFY3_IMPL(LEFT, OP, RIGHT, TYPE, FMT, CAST) \
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)); \
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 *))
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)
356 #define VERIFY(x) ASSERT(x)
358 #define __CDEBUG(cdls, subsys, mask, format, a...) \
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__, \
370 #define CDEBUG(mask, format, a...) \
371 __CDEBUG(NULL, DEBUG_SUBSYSTEM, mask, format, ## a)
373 #define __CDEBUG_LIMIT(subsys, mask, format, a...) \
375 static spl_debug_limit_state_t cdls; \
377 __CDEBUG(&cdls, subsys, mask, format, ## a); \
380 #define CDEBUG_LIMIT(mask, format, a...) \
381 __CDEBUG_LIMIT(DEBUG_SUBSYSTEM, mask, format, ## a)
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)
388 #define GOTO(label, rc) \
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); \
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; \
405 #define __ENTRY(subsys) \
407 __CDEBUG(NULL, subsys, D_TRACE, "Process entered\n"); \
410 #define __EXIT(subsys) \
412 __CDEBUG(NULL, subsys, D_TRACE, "Process leaving\n"); \
415 #define ENTRY __ENTRY(DEBUG_SUBSYSTEM)
416 #define EXIT __EXIT(DEBUG_SUBSYSTEM)
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)
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
, ...);
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);
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
);
438 extern int spl_debug_clear_buffer(void);
439 extern int spl_debug_mark_buffer(char *text
);
441 #endif /* SPL_DEBUG_H */