2 * This file is part of the SPL: Solaris Porting Layer.
4 * Copyright (c) 2008 Lawrence Livermore National Security, LLC.
5 * Produced at Lawrence Livermore National Laboratory
7 * Brian Behlendorf <behlendorf1@llnl.gov>,
8 * Herb Wartens <wartens2@llnl.gov>,
9 * Jim Garlick <garlick@llnl.gov>
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.
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
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.
34 #include <linux/sched.h> /* THREAD_SIZE */
35 #include <linux/proc_fs.h>
37 extern unsigned long spl_debug_mask
;
38 extern unsigned long spl_debug_subsys
;
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 #define S_CRED 0x00010000
58 #define D_TRACE 0x00000001
59 #define D_INFO 0x00000002
60 #define D_WARNING 0x00000004
61 #define D_ERROR 0x00000008
62 #define D_EMERG 0x00000010
63 #define D_CONSOLE 0x00000020
64 #define D_IOCTL 0x00000040
65 #define D_DPRINTF 0x00000080
66 #define D_OTHER 0x00000100
68 #define D_CANTMASK (D_ERROR | D_EMERG | D_WARNING | D_CONSOLE)
69 #define DEBUG_SUBSYSTEM S_UNDEFINED
72 void debug_fini(void);
73 int spl_debug_mask2str(char *str
, int size
, unsigned long mask
, int is_subsys
);
74 int spl_debug_str2mask(unsigned long *mask
, const char *str
, int is_subsys
);
76 extern unsigned long spl_debug_subsys
;
77 extern unsigned long spl_debug_mask
;
78 extern unsigned long spl_debug_printk
;
79 extern int spl_debug_mb
;
80 extern unsigned int spl_debug_binary
;
81 extern unsigned int spl_debug_catastrophe
;
82 extern unsigned int spl_debug_panic_on_bug
;
83 extern char spl_debug_file_path
[PATH_MAX
];
84 extern unsigned int spl_console_ratelimit
;
85 extern long spl_console_max_delay
;
86 extern long spl_console_min_delay
;
87 extern unsigned int spl_console_backoff
;
88 extern unsigned int spl_debug_stack
;
90 #define TCD_MAX_PAGES (5 << (20 - PAGE_SHIFT))
91 #define TCD_STOCK_PAGES (TCD_MAX_PAGES)
92 #define TRACE_CONSOLE_BUFFER_SIZE 1024
94 #define SPL_DEFAULT_MAX_DELAY (600 * HZ)
95 #define SPL_DEFAULT_MIN_DELAY ((HZ + 1) / 2)
96 #define SPL_DEFAULT_BACKOFF 2
98 #define DL_NOTHREAD 0x0001 /* Do not create a new thread */
99 #define DL_SINGLE_CPU 0x0002 /* Collect pages from this CPU */
101 typedef struct dumplog_priv
{
102 wait_queue_head_t dp_waitq
;
109 unsigned long cdls_next
;
112 } spl_debug_limit_state_t
;
114 /* Three trace data types */
122 union trace_data_union
{
123 struct trace_cpu_data
{
124 /* pages with trace records not yet processed by tracefiled */
125 struct list_head tcd_pages
;
126 /* number of pages on ->tcd_pages */
127 unsigned long tcd_cur_pages
;
128 /* Max number of pages allowed on ->tcd_pages */
129 unsigned long tcd_max_pages
;
132 * preallocated pages to write trace records into. Pages from
133 * ->tcd_stock_pages are moved to ->tcd_pages by spl_debug_msg().
135 * This list is necessary, because on some platforms it's
136 * impossible to perform efficient atomic page allocation in a
137 * non-blockable context.
139 * Such platforms fill ->tcd_stock_pages "on occasion", when
140 * tracing code is entered in blockable context.
142 * trace_get_tage_try() tries to get a page from
143 * ->tcd_stock_pages first and resorts to atomic page
144 * allocation only if this queue is empty. ->tcd_stock_pages
145 * is replenished when tracing code is entered in blocking
146 * context (darwin-tracefile.c:trace_get_tcd()). We try to
147 * maintain TCD_STOCK_PAGES (40 by default) pages in this
148 * queue. Atomic allocation is only required if more than
149 * TCD_STOCK_PAGES pagesful are consumed by trace records all
150 * emitted in non-blocking contexts. Which is quite unlikely.
152 struct list_head tcd_stock_pages
;
153 /* number of pages on ->tcd_stock_pages */
154 unsigned long tcd_cur_stock_pages
;
156 unsigned short tcd_shutting_down
;
157 unsigned short tcd_cpu
;
158 unsigned short tcd_type
;
159 /* The factors to share debug memory. */
160 unsigned short tcd_pages_factor
;
163 * This spinlock is needed to workaround the problem of
164 * set_cpus_allowed() being GPL-only. Since we cannot
165 * schedule a thread on a specific CPU when dumping the
166 * pages, we must use the spinlock for mutual exclusion.
169 unsigned long tcd_lock_flags
;
171 char __pad
[L1_CACHE_ALIGN(sizeof(struct trace_cpu_data
))];
174 extern union trace_data_union (*trace_data
[TCD_TYPE_MAX
])[NR_CPUS
];
176 #define tcd_for_each(tcd, i, j) \
177 for (i = 0; i < TCD_TYPE_MAX && trace_data[i]; i++) \
178 for (j = 0, ((tcd) = &(*trace_data[i])[j].tcd); \
179 j < num_possible_cpus(); j++, (tcd) = &(*trace_data[i])[j].tcd)
181 #define tcd_for_each_type_lock(tcd, i, cpu) \
182 for (i = 0; i < TCD_TYPE_MAX && trace_data[i] && \
183 (tcd = &(*trace_data[i])[cpu].tcd) && \
184 trace_lock_tcd(tcd); trace_unlock_tcd(tcd), i++)
187 struct page
* page
; /* page itself */
188 struct list_head linkage
; /* Used by lists in trace_data_union */
189 unsigned int used
; /* number of bytes used within this page */
190 unsigned short cpu
; /* cpu that owns this page */
191 unsigned short type
; /* type(context) of this page */
194 struct page_collection
{
195 struct list_head pc_pages
;
197 int pc_want_daemon_pages
;
200 #define SBUG() spl_debug_bug(__FILE__, __FUNCTION__, __LINE__, 0);
204 #define CDEBUG_STACK() (0)
205 #define CDEBUG_LIMIT(x, y, z, a...) ((void)0)
206 #define __CDEBUG_LIMIT(x, y, z, a...) ((void)0)
207 #define CDEBUG(mask, format, a...) ((void)0)
208 #define CWARN(fmt, a...) ((void)0)
209 #define CERROR(fmt, a...) ((void)0)
210 #define CEMERG(fmt, a...) ((void)0)
211 #define CONSOLE(mask, fmt, a...) ((void)0)
213 #define ENTRY ((void)0)
214 #define EXIT ((void)0)
215 #define RETURN(x) return (x)
216 #define GOTO(x, y) { ((void)(y)); goto x; }
218 #define __ASSERT(x) ((void)0)
219 #define __ASSERT_TAGE_INVARIANT(x) ((void)0)
220 #define ASSERT(x) ((void)0)
221 #define ASSERTF(x, y, z...) ((void)0)
223 #define VERIFY(cond) \
225 if (unlikely(!(cond))) { \
226 printk(KERN_ERR "VERIFY(" #cond ") failed\n"); \
231 #define VERIFY3_IMPL(LEFT, OP, RIGHT, TYPE, FMT, CAST) \
233 if (!((TYPE)(LEFT) OP (TYPE)(RIGHT))) { \
235 "VERIFY3(" #LEFT " " #OP " " #RIGHT ") " \
236 "failed (" FMT " " #OP " " FMT ")\n", \
237 CAST (LEFT), CAST (RIGHT)); \
242 #define VERIFY3S(x,y,z) VERIFY3_IMPL(x, y, z, int64_t, "%lld", (long long))
243 #define VERIFY3U(x,y,z) VERIFY3_IMPL(x, y, z, uint64_t, "%llu", \
244 (unsigned long long))
245 #define VERIFY3P(x,y,z) VERIFY3_IMPL(x, y, z, uintptr_t, "%p", (void *))
247 #define ASSERT3S(x,y,z) ((void)0)
248 #define ASSERT3U(x,y,z) ((void)0)
249 #define ASSERT3P(x,y,z) ((void)0)
254 #define CDEBUG_STACK() (THREAD_SIZE - \
255 ((unsigned long)__builtin_dwarf_cfa() & \
258 #define CDEBUG_STACK() (THREAD_SIZE - \
259 ((unsigned long)__builtin_frame_address(0) & \
261 # endif /* __ia64__ */
263 /* DL_SINGLE_CPU flag is passed to spl_debug_bug() because we are about
264 * to over run our stack and likely damage at least one other unknown
265 * thread stack. We must finish generating the needed debug info within
266 * this thread context because once we yeild the CPU its very likely
267 * the system will crash.
269 #define __CHECK_STACK(file, func, line) \
271 if (unlikely(CDEBUG_STACK() > spl_debug_stack)) { \
272 spl_debug_stack = CDEBUG_STACK(); \
274 if (unlikely(CDEBUG_STACK() > (4 * THREAD_SIZE) / 5)) { \
275 spl_debug_msg(NULL, D_TRACE, D_WARNING, \
276 file, func, line, "Error " \
277 "exceeded maximum safe stack " \
278 "size (%lu/%lu)\n", \
279 CDEBUG_STACK(), THREAD_SIZE); \
280 spl_debug_bug(file, func, line, DL_SINGLE_CPU); \
285 #define CHECK_STACK() __CHECK_STACK(__FILE__, __func__, __LINE__)
287 /* ASSERTION that is safe to use within the debug system */
288 #define __ASSERT(cond) \
290 if (unlikely(!(cond))) { \
291 printk(KERN_ERR "ASSERTION(" #cond ") failed\n"); \
296 #define __ASSERT_TAGE_INVARIANT(tage) \
298 __ASSERT(tage != NULL); \
299 __ASSERT(tage->page != NULL); \
300 __ASSERT(tage->used <= PAGE_SIZE); \
301 __ASSERT(page_count(tage->page) > 0); \
304 /* ASSERTION that will debug log used outside the debug sysytem */
305 #define ASSERT(cond) \
309 if (unlikely(!(cond))) { \
310 spl_debug_msg(NULL, DEBUG_SUBSYSTEM, D_EMERG, \
311 __FILE__, __FUNCTION__, __LINE__, \
312 "ASSERTION(" #cond ") failed\n"); \
317 #define ASSERTF(cond, fmt, a...) \
321 if (unlikely(!(cond))) { \
322 spl_debug_msg(NULL, DEBUG_SUBSYSTEM, D_EMERG, \
323 __FILE__, __FUNCTION__, __LINE__, \
324 "ASSERTION(" #cond ") failed: " fmt, \
330 #define VERIFY3_IMPL(LEFT, OP, RIGHT, TYPE, FMT, CAST) \
334 if (!((TYPE)(LEFT) OP (TYPE)(RIGHT))) { \
335 spl_debug_msg(NULL, DEBUG_SUBSYSTEM, D_EMERG, \
336 __FILE__, __FUNCTION__, __LINE__, \
337 "VERIFY3(" #LEFT " " #OP " " #RIGHT ") " \
338 "failed (" FMT " " #OP " " FMT ")\n", \
339 CAST (LEFT), CAST (RIGHT)); \
344 #define VERIFY3S(x,y,z) VERIFY3_IMPL(x, y, z, int64_t, "%lld", (long long))
345 #define VERIFY3U(x,y,z) VERIFY3_IMPL(x, y, z, uint64_t, "%llu", \
346 (unsigned long long))
347 #define VERIFY3P(x,y,z) VERIFY3_IMPL(x, y, z, uintptr_t, "%p", (void *))
349 #define ASSERT3S(x,y,z) VERIFY3S(x, y, z)
350 #define ASSERT3U(x,y,z) VERIFY3U(x, y, z)
351 #define ASSERT3P(x,y,z) VERIFY3P(x, y, z)
354 #define VERIFY(x) ASSERT(x)
356 #define __CDEBUG(cdls, subsys, mask, format, a...) \
360 if (((mask) & D_CANTMASK) != 0 || \
361 ((spl_debug_mask & (mask)) != 0 && \
362 (spl_debug_subsys & (subsys)) != 0)) \
363 spl_debug_msg(cdls, subsys, mask, \
364 __FILE__, __FUNCTION__, __LINE__, \
368 #define CDEBUG(mask, format, a...) \
369 __CDEBUG(NULL, DEBUG_SUBSYSTEM, mask, format, ## a)
371 #define __CDEBUG_LIMIT(subsys, mask, format, a...) \
373 static spl_debug_limit_state_t cdls; \
375 __CDEBUG(&cdls, subsys, mask, format, ## a); \
378 #define CDEBUG_LIMIT(mask, format, a...) \
379 __CDEBUG_LIMIT(DEBUG_SUBSYSTEM, mask, format, ## a)
381 #define CWARN(fmt, a...) CDEBUG_LIMIT(D_WARNING, fmt, ## a)
382 #define CERROR(fmt, a...) CDEBUG_LIMIT(D_ERROR, fmt, ## a)
383 #define CEMERG(fmt, a...) CDEBUG_LIMIT(D_EMERG, fmt, ## a)
384 #define CONSOLE(mask, fmt, a...) CDEBUG(D_CONSOLE | (mask), fmt, ## a)
386 #define GOTO(label, rc) \
388 long GOTO__ret = (long)(rc); \
389 CDEBUG(D_TRACE,"Process leaving via %s (rc=%lu : %ld : %lx)\n", \
390 #label, (unsigned long)GOTO__ret, (signed long)GOTO__ret,\
391 (signed long)GOTO__ret); \
397 typeof(rc) RETURN__ret = (rc); \
398 CDEBUG(D_TRACE, "Process leaving (rc=%lu : %ld : %lx)\n", \
399 (long)RETURN__ret, (long)RETURN__ret, (long)RETURN__ret);\
400 return RETURN__ret; \
403 #define __ENTRY(subsys) \
405 __CDEBUG(NULL, subsys, D_TRACE, "Process entered\n"); \
408 #define __EXIT(subsys) \
410 __CDEBUG(NULL, subsys, D_TRACE, "Process leaving\n"); \
413 #define ENTRY __ENTRY(DEBUG_SUBSYSTEM)
414 #define EXIT __EXIT(DEBUG_SUBSYSTEM)
417 #define spl_debug_msg(cdls, subsys, mask, file, fn, line, format, a...) \
418 spl_debug_vmsg(cdls, subsys, mask, file, fn, \
419 line, NULL, NULL, format, ##a)
421 extern int spl_debug_vmsg(spl_debug_limit_state_t
*cdls
, int subsys
, int mask
,
422 const char *file
, const char *fn
, const int line
,
423 const char *format1
, va_list args
, const char *format2
, ...);
425 extern unsigned long spl_debug_set_mask(unsigned long mask
);
426 extern unsigned long spl_debug_get_mask(void);
427 extern unsigned long spl_debug_set_subsys(unsigned long mask
);
428 extern unsigned long spl_debug_get_subsys(void);
429 extern int spl_debug_set_mb(int mb
);
430 extern int spl_debug_get_mb(void);
432 extern int spl_debug_dumplog(int flags
);
433 extern void spl_debug_dumpstack(struct task_struct
*tsk
);
434 extern void spl_debug_bug(char *file
, const char *func
, const int line
, int flags
);
436 extern int spl_debug_clear_buffer(void);
437 extern int spl_debug_mark_buffer(char *text
);
443 #endif /* SPL_DEBUG_H */