1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002 Cluster File Systems, Inc.
5 * Author: Phil Schwan <phil@clusterfs.com>
7 * This file is part of Lustre, http://www.lustre.org.
9 * Lustre is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * Lustre is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Lustre; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 # define EXPORT_SYMTAB
27 #include <linux/config.h>
28 #include <linux/module.h>
29 #include <linux/kmod.h>
30 #include <linux/notifier.h>
31 #include <linux/kernel.h>
33 #include <linux/string.h>
34 #include <linux/stat.h>
35 #include <linux/errno.h>
36 #include <linux/smp_lock.h>
37 #include <linux/unistd.h>
38 #include <linux/interrupt.h>
39 #include <asm/system.h>
40 #include <asm/uaccess.h>
41 #include <linux/completion.h>
44 #include <linux/stat.h>
45 #include <asm/uaccess.h>
46 #include <asm/segment.h>
47 #include <linux/miscdevice.h>
48 #include <linux/version.h>
50 # define DEBUG_SUBSYSTEM S_PORTALS
52 #include <linux/kp30.h>
53 #include <linux/portals_compat25.h>
54 #include <linux/libcfs.h>
56 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
57 #include <linux/kallsyms.h>
60 unsigned int portal_subsystem_debug = ~0 - (S_PORTALS | S_QSWNAL | S_SOCKNAL |
62 EXPORT_SYMBOL(portal_subsystem_debug);
64 unsigned int portal_debug = (D_WARNING | D_DLMTRACE | D_ERROR | D_EMERG | D_HA |
65 D_RPCTRACE | D_VFSTRACE | 0xffffffff);
66 EXPORT_SYMBOL(portal_debug);
68 unsigned int portal_cerror = 1;
69 EXPORT_SYMBOL(portal_cerror);
71 unsigned int portal_printk;
72 EXPORT_SYMBOL(portal_printk);
74 unsigned int portal_stack;
75 EXPORT_SYMBOL(portal_stack);
78 atomic_t portal_kmemory = ATOMIC_INIT(0);
79 EXPORT_SYMBOL(portal_kmemory);
82 #define DEBUG_OVERFLOW 1024
83 static char *debug_buf = NULL;
84 static unsigned long debug_size = 0;
85 static atomic_t debug_off_a = ATOMIC_INIT(0);
86 static int debug_wrapped;
87 static DECLARE_WAIT_QUEUE_HEAD(debug_ctlwq);
88 #define DAEMON_SND_SIZE (64 << 10)
91 * used by the daemon to keep track the offset into debug_buffer for the next
92 * write to the file. Usually, the daemon is to write out buffer
93 * from debug_daemon_next_write upto debug_off
95 * Reader - portals_debug_msg()
96 * Writer - portals_debug_daemon()
97 * portals_debug_daemon_start() during daemon init time
98 * portals_debug_daemon_continue() to reset to debug_off
99 * portals_debug_clear_buffer() reset to debug_off for clear
100 * Note that *_start(), *_continue() & *clear_buffer() should serialized;
102 static atomic_t debug_daemon_next_write;
105 * A debug_daemon can be in following states
106 * stopped - stopped state means there is no debug_daemon running.
107 * accordingly, it must be in paused state
108 * a daemon is in !stopped && !paused state after
109 * "lctl debug_daemon start" creates debug_daemon successfully
111 * Reader - portals_debug_daemon()
112 * portals_debug_set_daemon() routines
113 * Writer - portals_debug_set_daemon() routines
114 * portals_debug_daemon() on IO error
115 * paused - a debug_daemon state is changed from !paused into paused
116 * when "lctl debug_daemon paused" is issued
117 * "lctl debug_daemon continue" gets a daemon into !paused mode
118 * Reader - portals_debug_set_daemon() routines
119 * portals_debug_msg()
120 * Writer - portals_debug_set_daemon() on init
121 * portals_debug_daemon()
123 * Daemon state diagram.
125 * | <-- debug_daemon start
127 * (!stopped, !paused)
128 * | <-- debug_daemon pause
131 * | <-- debug_daemon continue
133 * (!stopped, !paused)
134 * | <-- debug_daemon stop
137 * Overlapped - this is a state when CDEBUG is too fast for the daemon to
138 * write out the debug_bufferr. That is, debug_off is to
139 * overlap debug_daemon_next_write;
140 * Reader - portals_debug_msg()
141 * Writer - portals_debug_msg()
145 * Description on Trace Daemon Synchronization
147 * Three categories of code are synchronizing between each other
148 * 1. lctl, portals_debug_set_daemon(), the user debug control code,
149 * as well as portals_debug_clear_buffer()
150 * 2. CDEBUG, portals_debug_msg(), the debug put messages routine
151 * 3. Daemon, portals_debug_daemon(), to write out debug log file
154 * Three different controls for synchronizations
156 * 1. debug_daemon_semaphore
157 * The usage of this semaphore is to serialize multiple lctl controls
158 * in manipulating debug daemon state. The semaphore serves as the
159 * gatekeeper to allow only one user control thread, at any giving time,
160 * to access debug daemon state and keeps the other user control requests
161 * in wait state until the current control request is serviced.
163 * 2. wait_queue_head_t lctl (paired with lctl_event flag)
164 * Lctl event is the event between portals_debug_set_daemon() and
165 * portals_debug_daemon(). Lctl is an indicator for portals_debug_daemon()
166 * to flush data out to file. portals_debug_daemon() is to use lctl event
167 * as signal channel to wakeup portals_debug_set_daemon() upon flush
171 * portals_debug_daemon() uses to wake up
172 * portals_debug_set_daemon(), pause and stop, routines
174 * portals_debug_set_daemon(), stop and pause operations,
175 * wait and sleep on the event
177 * 3. wait_queue_head_t daemon (paired with daemon_event flag)
178 * This is an event channel to wakeup portals_debug_daemon. Daemon
179 * wakes up to run whenever there is an event posted. Daemon handles
180 * 2 types of operations . 1. Writes data out to debug file, 2. Flushes
181 * file and terminates base on lctl event.
183 * Daemon is normally in a sleep state.
184 * Daemon is woken up through daemon event whenever CDEBUG is
185 * putting data over any 64K boundary.
186 * File flush and termination -
187 * On portals_debug_daemon_stop/pause() operations, lctl control
188 * is to wake up daemon through daemon event.
190 * We can't use sleep_on() and wake_up() to replace daemon event because
191 * portals_debug_daemon() must catch the wakeup operation posted by
192 * portals_debug_daemon_stop/pause(). Otherwise, stop and pause may
193 * stuck in lctl wait event.
196 * a. portals_debug_daemon_pause() and portals_debug_daemon_stop()
197 * uses the event to wake up portals_debug_daemon()
198 * b. portals_debug_msg() uses the event to wake up
199 * portals_debug_daemon() whenever the data output is acrossing
200 * a 64K bytes boundary.
202 * portals_debug_daemon() wakes up upon daemon event.
204 * Sequence for portals_debug_daemon_stop() operation
206 * _Portals_debug_daemon_stop()_ _Daemon_
207 * Wait_event(daemon) or running
209 * Wakeup_event (daemon)
211 * Set force_flush flag if lctlevnt
213 * Wakeup_event (lctl)
216 * Wakeup_event (daemon)
218 * Exit daemon loop if (Stopped)
219 * Wakeup_event (lctl)
221 * Return to user application
224 * _Portals_debug_msg()_ _Daemon_
225 * Wait_event(daemon) or running
226 * If (WriteStart<64K<WriteEnd)
227 * Wakeup_event(daemon)
231 struct debug_daemon_state {
232 unsigned long overlapped;
233 unsigned long stopped;
235 unsigned long lctl_event; /* event for lctl */
236 wait_queue_head_t lctl;
237 unsigned long daemon_event; /* event for daemon */
238 wait_queue_head_t daemon;
240 static struct debug_daemon_state debug_daemon_state;
241 static DECLARE_MUTEX(debug_daemon_semaphore);
243 static loff_t daemon_file_size_limit;
244 char debug_daemon_file_path[1024] = "";
246 spinlock_t portals_debug_lock = SPIN_LOCK_UNLOCKED;
247 char debug_file_path[1024] = "/tmp/lustre-log";
248 char debug_file_name[1024];
249 int handled_panic; /* to avoid recursive calls to notifiers */
250 char portals_upcall[1024] = "/usr/lib/lustre/portals_upcall";
253 int portals_do_debug_dumplog(void *arg)
259 unsigned long debug_off;
261 kportal_daemonize("");
264 journal_info = current->journal_info;
265 current->journal_info = NULL;
266 sprintf(debug_file_name, "%s.%ld", debug_file_path, CURRENT_SECONDS);
267 file = filp_open(debug_file_name, O_CREAT|O_EXCL|O_RDWR, 0644);
269 if (!file || IS_ERR(file)) {
270 CERROR("cannot open %s for dumping: %ld\n", debug_file_name,
272 GOTO(out, PTR_ERR(file));
274 printk(KERN_ALERT "LustreError: dumping log to %s ...\n",
278 debug_off = atomic_read(&debug_off_a);
282 rc = file->f_op->write(file, debug_buf + debug_off + 1,
283 debug_size-debug_off-1, &file->f_pos);
284 rc += file->f_op->write(file, debug_buf, debug_off + 1,
287 rc = file->f_op->write(file, debug_buf, debug_off,&file->f_pos);
289 printk("LustreError: wrote %d bytes\n", rc);
292 rc = file->f_op->fsync(file, file->f_dentry, 1);
294 CERROR("sync returns %d\n", rc);
297 current->journal_info = journal_info;
298 wake_up(&debug_ctlwq);
302 int portals_debug_daemon(void *arg)
307 unsigned long force_flush = 0;
308 unsigned long size, off, flags;
311 kportal_daemonize("ldebug_daemon");
313 journal_info = current->journal_info;
314 current->journal_info = NULL;
316 file = filp_open(debug_daemon_file_path,
317 O_CREAT|O_TRUNC|O_RDWR|O_LARGEFILE, 0644);
319 if (!file || IS_ERR(file)) {
320 CERROR("cannot open %s for logging", debug_daemon_file_path);
321 GOTO(out1, PTR_ERR(file));
323 printk(KERN_INFO "daemon dumping log to %s\n", debug_daemon_file_path);
325 debug_daemon_state.overlapped = 0;
326 debug_daemon_state.stopped = 0;
328 spin_lock_irqsave(&portals_debug_lock, flags);
329 off = atomic_read(&debug_off_a) + 1;
331 off = (off >= debug_size)? 0 : off;
334 atomic_set(&debug_daemon_next_write, off);
335 atomic_set(&debug_daemon_state.paused, 0);
336 spin_unlock_irqrestore(&portals_debug_lock, flags);
341 unsigned long ending;
342 unsigned long start, tail;
345 debug_daemon_state.daemon_event = 0;
347 ending = atomic_read(&debug_off_a);
348 start = atomic_read(&debug_daemon_next_write);
350 /* check if paused is imposed by lctl ? */
351 force_flush = !debug_daemon_state.lctl_event;
353 delta = ending - start;
354 tail = debug_size - start;
355 size = (delta >= 0) ? delta : tail;
356 while (size && (force_flush || (delta < 0) ||
357 (size >= DAEMON_SND_SIZE))) {
358 if (daemon_file_size_limit) {
359 int ssize = daemon_file_size_limit - file->f_pos;
364 rc = file->f_op->write(file, debug_buf+start,
367 printk(KERN_ALERT "LustreError: Debug_daemon "
368 "write error %d\n", rc);
372 delta = ending - start;
373 tail = debug_size - start;
379 size = (tail == 0) ? ending : tail;
380 if (daemon_file_size_limit == file->f_pos) {
381 // file wrapped around
385 atomic_set(&debug_daemon_next_write, start);
387 rc = file->f_op->fsync(file, file->f_dentry, 1);
389 printk(KERN_ALERT "LustreError: Debug_daemon "
390 "sync error %d\n", rc);
393 if (debug_daemon_state.stopped)
395 debug_daemon_state.lctl_event = 1;
396 wake_up(&debug_daemon_state.lctl);
398 wait_event(debug_daemon_state.daemon,
399 debug_daemon_state.daemon_event);
402 atomic_set(&debug_daemon_state.paused, 1);
403 debug_daemon_state.stopped = 1;
406 current->journal_info = journal_info;
408 debug_daemon_state.lctl_event = 1;
409 wake_up(&debug_daemon_state.lctl);
413 void portals_debug_print(void)
415 unsigned long dumplen = 64 * 1024;
416 char *start1, *start2;
418 unsigned long debug_off = atomic_read(&debug_off_a);
420 start1 = debug_buf + debug_off - dumplen;
421 if (start1 < debug_buf) {
422 start1 += debug_size;
423 end1 = debug_buf + debug_size - 1;
425 end2 = debug_buf + debug_off;
427 end1 = debug_buf + debug_off;
428 start2 = debug_buf + debug_off;
429 end2 = debug_buf + debug_off;
432 while (start1 < end1) {
433 int count = MIN(1024, end1 - start1);
434 printk("LustreError: %*s", count, start1);
437 while (start2 < end2) {
438 int count = MIN(1024, end2 - start2);
439 printk("LustreError: %*s", count, start2);
444 void portals_debug_dumplog(void)
447 DECLARE_WAITQUEUE(wait, current);
450 /* we're being careful to ensure that the kernel thread is
451 * able to set our state to running as it exits before we
452 * get to schedule() */
453 set_current_state(TASK_INTERRUPTIBLE);
454 add_wait_queue(&debug_ctlwq, &wait);
456 rc = kernel_thread(portals_do_debug_dumplog,
457 NULL, CLONE_VM | CLONE_FS | CLONE_FILES);
459 printk(KERN_ERR "LustreError: cannot start log dump thread: "
464 /* be sure to teardown if kernel_thread() failed */
465 remove_wait_queue(&debug_ctlwq, &wait);
466 set_current_state(TASK_RUNNING);
469 int portals_debug_daemon_start(char *file, unsigned int size)
473 if (!debug_daemon_state.stopped)
477 strncpy(debug_daemon_file_path, file, 1024);
479 init_waitqueue_head(&debug_daemon_state.lctl);
480 init_waitqueue_head(&debug_daemon_state.daemon);
482 daemon_file_size_limit = size << 20;
484 debug_daemon_state.lctl_event = 0;
485 rc = kernel_thread(portals_debug_daemon, NULL, 0);
487 printk(KERN_ERR "LustreError: cannot start debug daemon thread\n");
488 strncpy(debug_daemon_file_path, "\0", 1);
491 wait_event(debug_daemon_state.lctl, debug_daemon_state.lctl_event);
495 int portals_debug_daemon_pause(void)
497 if (atomic_read(&debug_daemon_state.paused))
500 atomic_set(&debug_daemon_state.paused, 1);
501 debug_daemon_state.lctl_event = 0;
502 debug_daemon_state.daemon_event = 1;
503 wake_up(&debug_daemon_state.daemon);
504 wait_event(debug_daemon_state.lctl, debug_daemon_state.lctl_event);
508 int portals_debug_daemon_continue(void)
510 if (!atomic_read(&debug_daemon_state.paused))
512 if (debug_daemon_state.stopped)
515 debug_daemon_state.overlapped = 0;
516 atomic_set(&debug_daemon_next_write, atomic_read(&debug_off_a));
517 atomic_set(&debug_daemon_state.paused, 0);
521 int portals_debug_daemon_stop(void)
523 if (debug_daemon_state.stopped)
526 if (!atomic_read(&debug_daemon_state.paused))
527 portals_debug_daemon_pause();
529 debug_daemon_state.lctl_event = 0;
530 debug_daemon_state.stopped = 1;
532 debug_daemon_state.daemon_event = 1;
533 wake_up(&debug_daemon_state.daemon);
534 wait_event(debug_daemon_state.lctl, debug_daemon_state.lctl_event);
536 debug_daemon_file_path[0] = '\0';
540 int portals_debug_set_daemon(unsigned int cmd, unsigned int length,
541 char *filename, unsigned int size)
545 down(&debug_daemon_semaphore);
547 case DEBUG_DAEMON_START:
548 if (length && (filename[length -1] != '\0')) {
549 CERROR("Invalid filename for debug_daemon\n");
553 rc = portals_debug_daemon_start(filename, size);
555 case DEBUG_DAEMON_STOP:
556 rc = portals_debug_daemon_stop();
558 case DEBUG_DAEMON_PAUSE:
559 rc = portals_debug_daemon_pause();
561 case DEBUG_DAEMON_CONTINUE:
562 rc = portals_debug_daemon_continue();
565 CERROR("unknown set_daemon cmd\n");
567 up(&debug_daemon_semaphore);
571 static int panic_dumplog(struct notifier_block *self, unsigned long unused1,
579 if (in_interrupt()) {
580 portals_debug_print();
584 while (current->lock_depth >= 0)
586 portals_debug_dumplog();
590 static struct notifier_block lustre_panic_notifier = {
591 notifier_call : panic_dumplog,
596 int portals_debug_init(unsigned long bufsize)
598 unsigned long debug_off = atomic_read(&debug_off_a);
599 if (debug_buf != NULL)
602 atomic_set(&debug_daemon_state.paused, 1);
603 debug_daemon_state.stopped = 1;
605 debug_buf = vmalloc(bufsize + DEBUG_OVERFLOW);
606 if (debug_buf == NULL)
608 memset(debug_buf, 0, bufsize + DEBUG_OVERFLOW);
611 //printk(KERN_INFO "Portals: allocated %lu byte debug buffer at %p.\n",
612 //bufsize, debug_buf);
613 atomic_set(&debug_off_a, debug_off);
614 notifier_chain_register(&panic_notifier_list, &lustre_panic_notifier);
615 debug_size = bufsize;
620 int portals_debug_cleanup(void)
622 notifier_chain_unregister(&panic_notifier_list, &lustre_panic_notifier);
623 if (debug_buf == NULL)
626 down(&debug_daemon_semaphore);
627 portals_debug_daemon_stop();
630 atomic_set(&debug_off_a, 0);
631 up(&debug_daemon_semaphore);
636 int portals_debug_clear_buffer(void)
641 if (debug_buf == NULL)
644 down(&debug_daemon_semaphore);
645 state = atomic_read(&debug_daemon_state.paused);
647 portals_debug_daemon_pause();
648 spin_lock_irqsave(&portals_debug_lock, flags);
649 atomic_set(&debug_off_a, 0);
651 atomic_set(&debug_daemon_next_write, 0);
652 debug_daemon_state.overlapped = 0;
653 spin_unlock_irqrestore(&portals_debug_lock, flags);
656 atomic_set(&debug_daemon_state.paused, 0);
657 up(&debug_daemon_semaphore);
662 /* Debug markers, although printed by S_PORTALS
663 * should not be be marked as such.
665 #undef DEBUG_SUBSYSTEM
666 #define DEBUG_SUBSYSTEM S_UNDEFINED
667 int portals_debug_mark_buffer(char *text)
669 if (debug_buf == NULL)
672 CDEBUG(D_TRACE,"***************************************************\n");
673 CWARN("DEBUG MARKER: %s\n", text);
674 CDEBUG(D_TRACE,"***************************************************\n");
678 #undef DEBUG_SUBSYSTEM
679 #define DEBUG_SUBSYSTEM S_PORTALS
681 /* this copies a snapshot of the debug buffer into an array of pages
682 * before doing the potentially blocking copy into userspace. it could
683 * be warning userspace if things wrap heavily while its off copying. */
684 __s32 portals_debug_copy_to_user(char *buf, unsigned long len)
687 unsigned long total, debug_off, i, off, copied;
691 struct list_head *pos, *n;
693 if (len < debug_size)
696 for (i = 0 ; i < debug_size; i += PAGE_SIZE) {
697 page = alloc_page(GFP_NOFS);
702 list_add(&PAGE_LIST(page), &my_pages);
705 spin_lock_irqsave(&portals_debug_lock, flags);
706 debug_off = atomic_read(&debug_off_a);
708 /* Sigh. If the buffer is empty, then skip to the end. */
709 if (debug_off == 0 && !debug_wrapped) {
710 spin_unlock_irqrestore(&portals_debug_lock, flags);
723 list_for_each(pos, &my_pages) {
724 unsigned long to_copy;
727 page = list_entry(pos, struct page, PAGE_LIST_ENTRY);
728 to_copy = min(total - off, PAGE_SIZE);
731 to_copy = min(debug_size - off, PAGE_SIZE);
734 addr = kmap_atomic(page, KM_USER0);
735 memcpy(addr, debug_buf + off, to_copy);
736 kunmap_atomic(addr, KM_USER0);
742 if (off >= debug_size) {
744 if (to_copy != PAGE_SIZE) {
745 to_copy = PAGE_SIZE - to_copy;
751 spin_unlock_irqrestore(&portals_debug_lock, flags);
754 list_for_each(pos, &my_pages) {
755 unsigned long to_copy;
756 page = list_entry(pos, struct page, PAGE_LIST_ENTRY);
758 to_copy = min(copied - off, PAGE_SIZE);
759 rc = copy_to_user(buf + off, kmap(page), to_copy);
772 list_for_each_safe(pos, n, &my_pages) {
773 page = list_entry(pos, struct page, PAGE_LIST_ENTRY);
774 list_del(&PAGE_LIST(page));
780 /* FIXME: I'm not very smart; someone smarter should make this better. */
782 portals_debug_msg(int subsys, int mask, char *file, const char *fn,
783 const int line, unsigned long stack, char *format, ...)
791 unsigned long base_offset;
792 unsigned long debug_off;
794 if (debug_buf == NULL) {
795 printk("LustreError: portals_debug_msg: debug_buf is NULL!\n");
799 spin_lock_irqsave(&portals_debug_lock, flags);
800 debug_off = atomic_read(&debug_off_a);
801 if (!atomic_read(&debug_daemon_state.paused)) {
802 unsigned long available;
804 long v = atomic_read(&debug_daemon_next_write);
806 delta = debug_off - v;
807 available = (delta>=0) ? debug_size-delta : -delta;
808 // Check if we still have enough debug buffer for CDEBUG
809 if (available < DAEMON_SND_SIZE) {
810 /* Drop CDEBUG packets until enough debug_buffer is
812 if (debug_daemon_state.overlapped)
814 /* If this is the first time, leave a marker in the
816 debug_daemon_state.overlapped = 1;
817 format = "DEBUG MARKER: Debug buffer overlapped\n";
818 printk(KERN_ERR "LustreError: debug daemon buffer "
820 } else /* More space just became available */
821 debug_daemon_state.overlapped = 0;
824 max_nob = debug_size - debug_off + DEBUG_OVERFLOW;
826 spin_unlock_irqrestore(&portals_debug_lock, flags);
827 printk("LustreError: logic error in portals_debug_msg: "
828 "< 0 bytes to write\n");
832 /* NB since we pass a non-zero sized buffer (at least) on the first
833 * print, we can be assured that by the end of all the snprinting,
834 * we _do_ have a terminated buffer, even if our message got truncated.
837 do_gettimeofday(&tv);
839 prefix_nob = snprintf(debug_buf + debug_off, max_nob,
840 "%06x:%06x:%d:%lu.%06lu:%lu:%d:",
841 subsys, mask, smp_processor_id(),
842 tv.tv_sec, tv.tv_usec, stack, current->pid);
843 max_nob -= prefix_nob;
845 if(*(format + strlen(format) - 1) != '\n')
846 printk(KERN_INFO "format at %s:%d:%s doesn't end in newline\n",
849 #if defined(__arch_um__) && (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,20))
850 msg_nob = snprintf(debug_buf + debug_off + prefix_nob, max_nob,
852 current->thread.extern_pid, file, line, fn);
853 #elif defined(__arch_um__) && (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
854 msg_nob = snprintf(debug_buf + debug_off + prefix_nob, max_nob,
856 current->thread.mode.tt.extern_pid, file, line, fn);
858 msg_nob = snprintf(debug_buf + debug_off + prefix_nob, max_nob,
860 current->pid, file, line, fn);
863 va_start(ap, format);
864 msg_nob += vsnprintf(debug_buf + debug_off + prefix_nob + msg_nob,
865 max_nob, format, ap);
869 /* Print to console, while msg is contiguous in debug_buf */
870 /* NB safely terminated see above */
871 if ((mask & D_EMERG) != 0)
872 printk(KERN_EMERG "LustreError: %s",
873 debug_buf + debug_off + prefix_nob);
874 else if ((mask & D_ERROR) != 0)
875 printk(KERN_ERR "LustreError: %s",
876 debug_buf + debug_off + prefix_nob);
877 else if ((mask & D_WARNING) != 0)
878 printk(KERN_WARNING "Lustre: %s",
879 debug_buf + debug_off + prefix_nob);
880 else if (portal_printk)
881 printk("<%d>Lustre: %s", portal_printk,
882 debug_buf+debug_off+prefix_nob);
883 base_offset = debug_off & 0xFFFF;
885 debug_off += prefix_nob + msg_nob;
886 if (debug_off > debug_size) {
887 memcpy(debug_buf, debug_buf + debug_size,
888 debug_off - debug_size + 1);
889 debug_off -= debug_size;
893 atomic_set(&debug_off_a, debug_off);
894 if (!atomic_read(&debug_daemon_state.paused) &&
895 ((base_offset+prefix_nob+msg_nob) >= DAEMON_SND_SIZE)) {
896 debug_daemon_state.daemon_event = 1;
897 wake_up(&debug_daemon_state.daemon);
900 spin_unlock_irqrestore(&portals_debug_lock, flags);
903 void portals_debug_set_level(unsigned int debug_level)
905 printk("Lustre: Setting portals debug level to %08x\n", debug_level);
906 portal_debug = debug_level;
909 void portals_run_upcall(char **argv)
915 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
919 argv[0] = portals_upcall;
921 while (argv[argc] != NULL)
926 rc = USERMODEHELPER(argv[0], argv, envp);
928 CERROR("Error %d invoking portals upcall %s %s%s%s%s%s%s%s%s; "
929 "check /proc/sys/portals/upcall\n",
930 rc, argv[0], argv[1],
931 argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
932 argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
933 argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
934 argc < 6 ? "" : ",...");
936 CERROR("Invoked portals upcall %s %s%s%s%s%s%s%s%s\n",
938 argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
939 argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
940 argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
941 argc < 6 ? "" : ",...");
945 void portals_run_lbug_upcall(char *file, const char *fn, const int line)
951 snprintf (buf, sizeof buf, "%d", line);
955 argv[3] = (char *)fn;
959 portals_run_upcall (argv);
962 char *portals_nid2str(int nal, ptl_nid_t nid, char *str)
965 /* XXX this could be a nal method of some sort, 'cept it's config
966 * dependent whether (say) socknal NIDs are actually IP addresses... */
971 snprintf(str, PTL_NALFMT_SIZE - 1, "%u:%u.%u.%u.%u",
972 (__u32)(nid >> 32), HIPQUAD(nid));
977 snprintf(str, PTL_NALFMT_SIZE - 1, "%u:%u",
978 (__u32)(nid >> 32), (__u32)nid);
982 snprintf(str, PTL_NALFMT_SIZE - 1, "?%d? %llx",
983 nal, (long long)nid);
990 char stack_backtrace[LUSTRE_TRACE_SIZE];
991 spinlock_t stack_backtrace_lock = SPIN_LOCK_UNLOCKED;
993 #if defined(__arch_um__)
995 char *portals_debug_dumpstack(void)
998 return "dump stack\n";
1001 #elif defined(__i386__)
1003 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1004 extern int lookup_symbol(unsigned long address, char *buf, int buflen);
1005 const char *kallsyms_lookup(unsigned long addr,
1006 unsigned long *symbolsize,
1007 unsigned long *offset,
1008 char **modname, char *namebuf)
1010 int rc = lookup_symbol(addr, namebuf, 128);
1017 char *portals_debug_dumpstack(void)
1019 unsigned long esp = current->thread.esp, addr;
1020 unsigned long *stack = (unsigned long *)&esp;
1021 char *buf = stack_backtrace, *pbuf = buf;
1024 /* User space on another CPU? */
1025 if ((esp ^ (unsigned long)current) & (PAGE_MASK << 1)){
1030 size = sprintf(pbuf, " Call Trace: ");
1032 while (((long) stack & (THREAD_SIZE - 1)) != 0) {
1034 if (kernel_text_address(addr)) {
1035 const char *sym_name;
1036 char *modname, buffer[128];
1037 unsigned long junk, offset;
1039 sym_name = kallsyms_lookup(addr, &junk, &offset,
1041 if (sym_name == NULL) {
1042 if (buf + LUSTRE_TRACE_SIZE <= pbuf + 12)
1044 size = sprintf(pbuf, "[<%08lx>] ", addr);
1046 if (buf + LUSTRE_TRACE_SIZE
1047 /* fix length + sizeof('\0') */
1048 <= pbuf + strlen(buffer) + 28 + 1)
1050 size = sprintf(pbuf, "([<%08lx>] %s (0x%p)) ",
1051 addr, buffer, stack - 1);
1060 #else /* !__arch_um__ && !__i386__ */
1062 char *portals_debug_dumpstack(void)
1064 char *buf = stack_backtrace;
1069 #endif /* __arch_um__ */
1070 EXPORT_SYMBOL(stack_backtrace_lock);
1071 EXPORT_SYMBOL(portals_debug_dumpstack);
1072 #endif /* __KERNEL__ */
1074 EXPORT_SYMBOL(portals_debug_dumplog);
1075 EXPORT_SYMBOL(portals_debug_msg);
1076 EXPORT_SYMBOL(portals_debug_set_level);
1077 EXPORT_SYMBOL(portals_run_upcall);
1078 EXPORT_SYMBOL(portals_run_lbug_upcall);
1079 EXPORT_SYMBOL(portals_nid2str);