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>
49 # define DEBUG_SUBSYSTEM S_PORTALS
51 #include <linux/kp30.h>
52 #include <linux/portals_compat25.h>
53 #include <linux/libcfs.h>
55 unsigned int portal_subsystem_debug = ~0 - (S_PORTALS | S_QSWNAL | S_SOCKNAL |
57 EXPORT_SYMBOL(portal_subsystem_debug);
59 unsigned int portal_debug = (D_WARNING | D_DLMTRACE | D_ERROR | D_EMERG | D_HA |
60 D_RPCTRACE | D_VFSTRACE);
61 EXPORT_SYMBOL(portal_debug);
63 unsigned int portal_cerror = 1;
64 EXPORT_SYMBOL(portal_cerror);
66 unsigned int portal_printk;
67 EXPORT_SYMBOL(portal_printk);
69 unsigned int portal_stack;
70 EXPORT_SYMBOL(portal_stack);
73 atomic_t portal_kmemory = ATOMIC_INIT(0);
74 EXPORT_SYMBOL(portal_kmemory);
77 #define DEBUG_OVERFLOW 1024
78 static char *debug_buf = NULL;
79 static unsigned long debug_size = 0;
80 static atomic_t debug_off_a = ATOMIC_INIT(0);
81 static int debug_wrapped;
82 static DECLARE_WAIT_QUEUE_HEAD(debug_ctlwq);
83 #define DAEMON_SND_SIZE (64 << 10)
86 * used by the daemon to keep track the offset into debug_buffer for the next
87 * write to the file. Usually, the daemon is to write out buffer
88 * from debug_daemon_next_write upto debug_off
90 * Reader - portals_debug_msg()
91 * Writer - portals_debug_daemon()
92 * portals_debug_daemon_start() during daemon init time
93 * portals_debug_daemon_continue() to reset to debug_off
94 * portals_debug_clear_buffer() reset to debug_off for clear
95 * Note that *_start(), *_continue() & *clear_buffer() should serialized;
97 static atomic_t debug_daemon_next_write;
100 * A debug_daemon can be in following states
101 * stopped - stopped state means there is no debug_daemon running.
102 * accordingly, it must be in paused state
103 * a daemon is in !stopped && !paused state after
104 * "lctl debug_daemon start" creates debug_daemon successfully
106 * Reader - portals_debug_daemon()
107 * portals_debug_set_daemon() routines
108 * Writer - portals_debug_set_daemon() routines
109 * portals_debug_daemon() on IO error
110 * paused - a debug_daemon state is changed from !paused into paused
111 * when "lctl debug_daemon paused" is issued
112 * "lctl debug_daemon continue" gets a daemon into !paused mode
113 * Reader - portals_debug_set_daemon() routines
114 * portals_debug_msg()
115 * Writer - portals_debug_set_daemon() on init
116 * portals_debug_daemon()
118 * Daemon state diagram.
120 * | <-- debug_daemon start
122 * (!stopped, !paused)
123 * | <-- debug_daemon pause
126 * | <-- debug_daemon continue
128 * (!stopped, !paused)
129 * | <-- debug_daemon stop
132 * Overlapped - this is a state when CDEBUG is too fast for the daemon to
133 * write out the debug_bufferr. That is, debug_off is to
134 * overlap debug_daemon_next_write;
135 * Reader - portals_debug_msg()
136 * Writer - portals_debug_msg()
140 * Description on Trace Daemon Synchronization
142 * Three categories of code are synchronizing between each other
143 * 1. lctl, portals_debug_set_daemon(), the user debug control code,
144 * as well as portals_debug_clear_buffer()
145 * 2. CDEBUG, portals_debug_msg(), the debug put messages routine
146 * 3. Daemon, portals_debug_daemon(), to write out debug log file
149 * Three different controls for synchronizations
151 * 1. debug_daemon_semaphore
152 * The usage of this semaphore is to serialize multiple lctl controls
153 * in manipulating debug daemon state. The semaphore serves as the
154 * gatekeeper to allow only one user control thread, at any giving time,
155 * to access debug daemon state and keeps the other user control requests
156 * in wait state until the current control request is serviced.
158 * 2. wait_queue_head_t lctl (paired with lctl_event flag)
159 * Lctl event is the event between portals_debug_set_daemon() and
160 * portals_debug_daemon(). Lctl is an indicator for portals_debug_daemon()
161 * to flush data out to file. portals_debug_daemon() is to use lctl event
162 * as signal channel to wakeup portals_debug_set_daemon() upon flush
166 * portals_debug_daemon() uses to wake up
167 * portals_debug_set_daemon(), pause and stop, routines
169 * portals_debug_set_daemon(), stop and pause operations,
170 * wait and sleep on the event
172 * 3. wait_queue_head_t daemon (paired with daemon_event flag)
173 * This is an event channel to wakeup portals_debug_daemon. Daemon
174 * wakes up to run whenever there is an event posted. Daemon handles
175 * 2 types of operations . 1. Writes data out to debug file, 2. Flushes
176 * file and terminates base on lctl event.
178 * Daemon is normally in a sleep state.
179 * Daemon is woken up through daemon event whenever CDEBUG is
180 * putting data over any 64K boundary.
181 * File flush and termination -
182 * On portals_debug_daemon_stop/pause() operations, lctl control
183 * is to wake up daemon through daemon event.
185 * We can't use sleep_on() and wake_up() to replace daemon event because
186 * portals_debug_daemon() must catch the wakeup operation posted by
187 * portals_debug_daemon_stop/pause(). Otherwise, stop and pause may
188 * stuck in lctl wait event.
191 * a. portals_debug_daemon_pause() and portals_debug_daemon_stop()
192 * uses the event to wake up portals_debug_daemon()
193 * b. portals_debug_msg() uses the event to wake up
194 * portals_debug_daemon() whenever the data output is acrossing
195 * a 64K bytes boundary.
197 * portals_debug_daemon() wakes up upon daemon event.
199 * Sequence for portals_debug_daemon_stop() operation
201 * _Portals_debug_daemon_stop()_ _Daemon_
202 * Wait_event(daemon) or running
204 * Wakeup_event (daemon)
206 * Set force_flush flag if lctlevnt
208 * Wakeup_event (lctl)
211 * Wakeup_event (daemon)
213 * Exit daemon loop if (Stopped)
214 * Wakeup_event (lctl)
216 * Return to user application
219 * _Portals_debug_msg()_ _Daemon_
220 * Wait_event(daemon) or running
221 * If (WriteStart<64K<WriteEnd)
222 * Wakeup_event(daemon)
226 struct debug_daemon_state {
227 unsigned long overlapped;
228 unsigned long stopped;
230 unsigned long lctl_event; /* event for lctl */
231 wait_queue_head_t lctl;
232 unsigned long daemon_event; /* event for daemon */
233 wait_queue_head_t daemon;
235 static struct debug_daemon_state debug_daemon_state;
236 static DECLARE_MUTEX(debug_daemon_semaphore);
238 static loff_t daemon_file_size_limit;
239 char debug_daemon_file_path[1024] = "";
241 spinlock_t portals_debug_lock = SPIN_LOCK_UNLOCKED;
242 char debug_file_path[1024] = "/tmp/lustre-log";
243 char debug_file_name[1024];
244 int handled_panic; /* to avoid recursive calls to notifiers */
245 char portals_upcall[1024] = "/usr/lib/lustre/portals_upcall";
248 int portals_do_debug_dumplog(void *arg)
254 unsigned long debug_off;
256 kportal_daemonize("");
259 journal_info = current->journal_info;
260 current->journal_info = NULL;
261 sprintf(debug_file_name, "%s.%ld", debug_file_path, CURRENT_SECONDS);
262 file = filp_open(debug_file_name, O_CREAT|O_EXCL|O_RDWR, 0644);
264 if (!file || IS_ERR(file)) {
265 CERROR("cannot open %s for dumping: %ld\n", debug_file_name,
267 GOTO(out, PTR_ERR(file));
269 printk(KERN_ALERT "LustreError: dumping log to %s ...\n",
273 debug_off = atomic_read(&debug_off_a);
277 rc = file->f_op->write(file, debug_buf + debug_off + 1,
278 debug_size-debug_off-1, &file->f_pos);
279 rc += file->f_op->write(file, debug_buf, debug_off + 1,
282 rc = file->f_op->write(file, debug_buf, debug_off,&file->f_pos);
284 printk("LustreError: wrote %d bytes\n", rc);
287 rc = file->f_op->fsync(file, file->f_dentry, 1);
289 CERROR("sync returns %d\n", rc);
292 current->journal_info = journal_info;
293 wake_up(&debug_ctlwq);
297 int portals_debug_daemon(void *arg)
302 unsigned long force_flush = 0;
303 unsigned long size, off, flags;
306 kportal_daemonize("ldebug_daemon");
308 journal_info = current->journal_info;
309 current->journal_info = NULL;
311 file = filp_open(debug_daemon_file_path,
312 O_CREAT|O_TRUNC|O_RDWR|O_LARGEFILE, 0644);
314 if (!file || IS_ERR(file)) {
315 CERROR("cannot open %s for logging", debug_daemon_file_path);
316 GOTO(out1, PTR_ERR(file));
318 printk(KERN_INFO "daemon dumping log to %s\n", debug_daemon_file_path);
320 debug_daemon_state.overlapped = 0;
321 debug_daemon_state.stopped = 0;
323 spin_lock_irqsave(&portals_debug_lock, flags);
324 off = atomic_read(&debug_off_a) + 1;
326 off = (off >= debug_size)? 0 : off;
329 atomic_set(&debug_daemon_next_write, off);
330 atomic_set(&debug_daemon_state.paused, 0);
331 spin_unlock_irqrestore(&portals_debug_lock, flags);
336 unsigned long ending;
337 unsigned long start, tail;
340 debug_daemon_state.daemon_event = 0;
342 ending = atomic_read(&debug_off_a);
343 start = atomic_read(&debug_daemon_next_write);
345 /* check if paused is imposed by lctl ? */
346 force_flush = !debug_daemon_state.lctl_event;
348 delta = ending - start;
349 tail = debug_size - start;
350 size = (delta >= 0) ? delta : tail;
351 while (size && (force_flush || (delta < 0) ||
352 (size >= DAEMON_SND_SIZE))) {
353 if (daemon_file_size_limit) {
354 int ssize = daemon_file_size_limit - file->f_pos;
359 rc = file->f_op->write(file, debug_buf+start,
362 printk(KERN_ALERT "LustreError: Debug_daemon "
363 "write error %d\n", rc);
367 delta = ending - start;
368 tail = debug_size - start;
374 size = (tail == 0) ? ending : tail;
375 if (daemon_file_size_limit == file->f_pos) {
376 // file wrapped around
380 atomic_set(&debug_daemon_next_write, start);
382 rc = file->f_op->fsync(file, file->f_dentry, 1);
384 printk(KERN_ALERT "LustreError: Debug_daemon "
385 "sync error %d\n", rc);
388 if (debug_daemon_state.stopped)
390 debug_daemon_state.lctl_event = 1;
391 wake_up(&debug_daemon_state.lctl);
393 wait_event(debug_daemon_state.daemon,
394 debug_daemon_state.daemon_event);
397 atomic_set(&debug_daemon_state.paused, 1);
398 debug_daemon_state.stopped = 1;
401 current->journal_info = journal_info;
403 debug_daemon_state.lctl_event = 1;
404 wake_up(&debug_daemon_state.lctl);
408 void portals_debug_print(void)
410 unsigned long dumplen = 64 * 1024;
411 char *start1, *start2;
413 unsigned long debug_off = atomic_read(&debug_off_a);
415 start1 = debug_buf + debug_off - dumplen;
416 if (start1 < debug_buf) {
417 start1 += debug_size;
418 end1 = debug_buf + debug_size - 1;
420 end2 = debug_buf + debug_off;
422 end1 = debug_buf + debug_off;
423 start2 = debug_buf + debug_off;
424 end2 = debug_buf + debug_off;
427 while (start1 < end1) {
428 int count = MIN(1024, end1 - start1);
429 printk("LustreError: %*s", count, start1);
432 while (start2 < end2) {
433 int count = MIN(1024, end2 - start2);
434 printk("LustreError: %*s", count, start2);
439 void portals_debug_dumplog(void)
442 DECLARE_WAITQUEUE(wait, current);
445 /* we're being careful to ensure that the kernel thread is
446 * able to set our state to running as it exits before we
447 * get to schedule() */
448 set_current_state(TASK_INTERRUPTIBLE);
449 add_wait_queue(&debug_ctlwq, &wait);
451 rc = kernel_thread(portals_do_debug_dumplog,
452 NULL, CLONE_VM | CLONE_FS | CLONE_FILES);
454 printk(KERN_ERR "LustreError: cannot start log dump thread: "
459 /* be sure to teardown if kernel_thread() failed */
460 remove_wait_queue(&debug_ctlwq, &wait);
461 set_current_state(TASK_RUNNING);
464 int portals_debug_daemon_start(char *file, unsigned int size)
468 if (!debug_daemon_state.stopped)
472 strncpy(debug_daemon_file_path, file, 1024);
474 init_waitqueue_head(&debug_daemon_state.lctl);
475 init_waitqueue_head(&debug_daemon_state.daemon);
477 daemon_file_size_limit = size << 20;
479 debug_daemon_state.lctl_event = 0;
480 rc = kernel_thread(portals_debug_daemon, NULL, 0);
482 printk(KERN_ERR "LustreError: cannot start debug daemon thread\n");
483 strncpy(debug_daemon_file_path, "\0", 1);
486 wait_event(debug_daemon_state.lctl, debug_daemon_state.lctl_event);
490 int portals_debug_daemon_pause(void)
492 if (atomic_read(&debug_daemon_state.paused))
495 atomic_set(&debug_daemon_state.paused, 1);
496 debug_daemon_state.lctl_event = 0;
497 debug_daemon_state.daemon_event = 1;
498 wake_up(&debug_daemon_state.daemon);
499 wait_event(debug_daemon_state.lctl, debug_daemon_state.lctl_event);
503 int portals_debug_daemon_continue(void)
505 if (!atomic_read(&debug_daemon_state.paused))
507 if (debug_daemon_state.stopped)
510 debug_daemon_state.overlapped = 0;
511 atomic_set(&debug_daemon_next_write, atomic_read(&debug_off_a));
512 atomic_set(&debug_daemon_state.paused, 0);
516 int portals_debug_daemon_stop(void)
518 if (debug_daemon_state.stopped)
521 if (!atomic_read(&debug_daemon_state.paused))
522 portals_debug_daemon_pause();
524 debug_daemon_state.lctl_event = 0;
525 debug_daemon_state.stopped = 1;
527 debug_daemon_state.daemon_event = 1;
528 wake_up(&debug_daemon_state.daemon);
529 wait_event(debug_daemon_state.lctl, debug_daemon_state.lctl_event);
531 debug_daemon_file_path[0] = '\0';
535 int portals_debug_set_daemon(unsigned int cmd, unsigned int length,
536 char *filename, unsigned int size)
540 down(&debug_daemon_semaphore);
542 case DEBUG_DAEMON_START:
543 if (length && (filename[length -1] != '\0')) {
544 CERROR("Invalid filename for debug_daemon\n");
548 rc = portals_debug_daemon_start(filename, size);
550 case DEBUG_DAEMON_STOP:
551 rc = portals_debug_daemon_stop();
553 case DEBUG_DAEMON_PAUSE:
554 rc = portals_debug_daemon_pause();
556 case DEBUG_DAEMON_CONTINUE:
557 rc = portals_debug_daemon_continue();
560 CERROR("unknown set_daemon cmd\n");
562 up(&debug_daemon_semaphore);
566 static int panic_dumplog(struct notifier_block *self, unsigned long unused1,
574 if (in_interrupt()) {
575 portals_debug_print();
579 while (current->lock_depth >= 0)
581 portals_debug_dumplog();
585 static struct notifier_block lustre_panic_notifier = {
586 notifier_call : panic_dumplog,
591 int portals_debug_init(unsigned long bufsize)
593 unsigned long debug_off = atomic_read(&debug_off_a);
594 if (debug_buf != NULL)
597 atomic_set(&debug_daemon_state.paused, 1);
598 debug_daemon_state.stopped = 1;
600 debug_buf = vmalloc(bufsize + DEBUG_OVERFLOW);
601 if (debug_buf == NULL)
603 memset(debug_buf, 0, bufsize + DEBUG_OVERFLOW);
606 //printk(KERN_INFO "Portals: allocated %lu byte debug buffer at %p.\n",
607 //bufsize, debug_buf);
608 atomic_set(&debug_off_a, debug_off);
609 notifier_chain_register(&panic_notifier_list, &lustre_panic_notifier);
610 debug_size = bufsize;
615 int portals_debug_cleanup(void)
617 notifier_chain_unregister(&panic_notifier_list, &lustre_panic_notifier);
618 if (debug_buf == NULL)
621 down(&debug_daemon_semaphore);
622 portals_debug_daemon_stop();
625 atomic_set(&debug_off_a, 0);
626 up(&debug_daemon_semaphore);
631 int portals_debug_clear_buffer(void)
636 if (debug_buf == NULL)
639 down(&debug_daemon_semaphore);
640 state = atomic_read(&debug_daemon_state.paused);
642 portals_debug_daemon_pause();
643 spin_lock_irqsave(&portals_debug_lock, flags);
644 atomic_set(&debug_off_a, 0);
646 atomic_set(&debug_daemon_next_write, 0);
647 debug_daemon_state.overlapped = 0;
648 spin_unlock_irqrestore(&portals_debug_lock, flags);
651 atomic_set(&debug_daemon_state.paused, 0);
652 up(&debug_daemon_semaphore);
657 /* Debug markers, although printed by S_PORTALS
658 * should not be be marked as such.
660 #undef DEBUG_SUBSYSTEM
661 #define DEBUG_SUBSYSTEM S_UNDEFINED
662 int portals_debug_mark_buffer(char *text)
664 if (debug_buf == NULL)
667 CDEBUG(D_TRACE,"***************************************************\n");
668 CWARN("DEBUG MARKER: %s\n", text);
669 CDEBUG(D_TRACE,"***************************************************\n");
673 #undef DEBUG_SUBSYSTEM
674 #define DEBUG_SUBSYSTEM S_PORTALS
676 /* this copies a snapshot of the debug buffer into an array of pages
677 * before doing the potentially blocking copy into userspace. it could
678 * be warning userspace if things wrap heavily while its off copying. */
679 __s32 portals_debug_copy_to_user(char *buf, unsigned long len)
682 unsigned long total, debug_off, i, off, copied;
686 struct list_head *pos, *n;
688 if (len < debug_size)
691 for (i = 0 ; i < debug_size; i += PAGE_SIZE) {
692 page = alloc_page(GFP_NOFS);
697 list_add(&PAGE_LIST(page), &my_pages);
700 spin_lock_irqsave(&portals_debug_lock, flags);
701 debug_off = atomic_read(&debug_off_a);
703 /* Sigh. If the buffer is empty, then skip to the end. */
704 if (debug_off == 0 && !debug_wrapped) {
705 spin_unlock_irqrestore(&portals_debug_lock, flags);
718 list_for_each(pos, &my_pages) {
719 unsigned long to_copy;
722 page = list_entry(pos, struct page, PAGE_LIST_ENTRY);
723 to_copy = min(total - off, PAGE_SIZE);
726 to_copy = min(debug_size - off, PAGE_SIZE);
729 addr = kmap_atomic(page, KM_USER0);
730 memcpy(addr, debug_buf + off, to_copy);
731 kunmap_atomic(addr, KM_USER0);
737 if (off >= debug_size) {
739 if (to_copy != PAGE_SIZE) {
740 to_copy = PAGE_SIZE - to_copy;
746 spin_unlock_irqrestore(&portals_debug_lock, flags);
749 list_for_each(pos, &my_pages) {
750 unsigned long to_copy;
751 page = list_entry(pos, struct page, PAGE_LIST_ENTRY);
753 to_copy = min(copied - off, PAGE_SIZE);
754 rc = copy_to_user(buf + off, kmap(page), to_copy);
767 list_for_each_safe(pos, n, &my_pages) {
768 page = list_entry(pos, struct page, PAGE_LIST_ENTRY);
769 list_del(&PAGE_LIST(page));
775 /* FIXME: I'm not very smart; someone smarter should make this better. */
777 portals_debug_msg(int subsys, int mask, char *file, const char *fn,
778 const int line, unsigned long stack, char *format, ...)
786 unsigned long base_offset;
787 unsigned long debug_off;
789 if (debug_buf == NULL) {
790 printk("LustreError: portals_debug_msg: debug_buf is NULL!\n");
794 spin_lock_irqsave(&portals_debug_lock, flags);
795 debug_off = atomic_read(&debug_off_a);
796 if (!atomic_read(&debug_daemon_state.paused)) {
797 unsigned long available;
799 long v = atomic_read(&debug_daemon_next_write);
801 delta = debug_off - v;
802 available = (delta>=0) ? debug_size-delta : -delta;
803 // Check if we still have enough debug buffer for CDEBUG
804 if (available < DAEMON_SND_SIZE) {
805 /* Drop CDEBUG packets until enough debug_buffer is
807 if (debug_daemon_state.overlapped)
809 /* If this is the first time, leave a marker in the
811 debug_daemon_state.overlapped = 1;
812 format = "DEBUG MARKER: Debug buffer overlapped\n";
813 printk(KERN_ERR "LustreError: debug daemon buffer "
815 } else /* More space just became available */
816 debug_daemon_state.overlapped = 0;
819 max_nob = debug_size - debug_off + DEBUG_OVERFLOW;
821 spin_unlock_irqrestore(&portals_debug_lock, flags);
822 printk("LustreError: logic error in portals_debug_msg: "
823 "< 0 bytes to write\n");
827 /* NB since we pass a non-zero sized buffer (at least) on the first
828 * print, we can be assured that by the end of all the snprinting,
829 * we _do_ have a terminated buffer, even if our message got truncated.
832 do_gettimeofday(&tv);
834 prefix_nob = snprintf(debug_buf + debug_off, max_nob,
835 "%06x:%06x:%d:%lu.%06lu:%lu:%d:",
836 subsys, mask, smp_processor_id(),
837 tv.tv_sec, tv.tv_usec, stack, current->pid);
838 max_nob -= prefix_nob;
840 if(*(format + strlen(format) - 1) != '\n')
841 printk(KERN_INFO "format at %s:%d:%s doesn't end in newline\n",
844 #if defined(__arch_um__) && (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,20))
845 msg_nob = snprintf(debug_buf + debug_off + prefix_nob, max_nob,
847 current->thread.extern_pid, file, line, fn);
848 #elif defined(__arch_um__) && (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
849 msg_nob = snprintf(debug_buf + debug_off + prefix_nob, max_nob,
851 current->thread.mode.tt.extern_pid, file, line, fn);
853 msg_nob = snprintf(debug_buf + debug_off + prefix_nob, max_nob,
855 current->pid, file, line, fn);
858 va_start(ap, format);
859 msg_nob += vsnprintf(debug_buf + debug_off + prefix_nob + msg_nob,
860 max_nob, format, ap);
864 /* Print to console, while msg is contiguous in debug_buf */
865 /* NB safely terminated see above */
866 if ((mask & D_EMERG) != 0)
867 printk(KERN_EMERG "LustreError: %s",
868 debug_buf + debug_off + prefix_nob);
869 else if ((mask & D_ERROR) != 0)
870 printk(KERN_ERR "LustreError: %s",
871 debug_buf + debug_off + prefix_nob);
872 else if ((mask & D_WARNING) != 0)
873 printk(KERN_WARNING "Lustre: %s",
874 debug_buf + debug_off + prefix_nob);
875 else if (portal_printk)
876 printk("<%d>Lustre: %s", portal_printk,
877 debug_buf+debug_off+prefix_nob);
878 base_offset = debug_off & 0xFFFF;
880 debug_off += prefix_nob + msg_nob;
881 if (debug_off > debug_size) {
882 memcpy(debug_buf, debug_buf + debug_size,
883 debug_off - debug_size + 1);
884 debug_off -= debug_size;
888 atomic_set(&debug_off_a, debug_off);
889 if (!atomic_read(&debug_daemon_state.paused) &&
890 ((base_offset+prefix_nob+msg_nob) >= DAEMON_SND_SIZE)) {
891 debug_daemon_state.daemon_event = 1;
892 wake_up(&debug_daemon_state.daemon);
895 spin_unlock_irqrestore(&portals_debug_lock, flags);
898 void portals_debug_set_level(unsigned int debug_level)
900 printk("Lustre: Setting portals debug level to %08x\n", debug_level);
901 portal_debug = debug_level;
904 void portals_run_upcall(char **argv)
910 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
914 argv[0] = portals_upcall;
916 while (argv[argc] != NULL)
921 rc = USERMODEHELPER(argv[0], argv, envp);
923 CERROR("Error %d invoking portals upcall %s %s%s%s%s%s%s%s%s; "
924 "check /proc/sys/portals/upcall\n",
925 rc, argv[0], argv[1],
926 argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
927 argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
928 argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
929 argc < 6 ? "" : ",...");
931 CERROR("Invoked portals upcall %s %s%s%s%s%s%s%s%s\n",
933 argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
934 argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
935 argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
936 argc < 6 ? "" : ",...");
940 void portals_run_lbug_upcall(char *file, const char *fn, const int line)
946 snprintf (buf, sizeof buf, "%d", line);
950 argv[3] = (char *)fn;
954 portals_run_upcall (argv);
957 char *portals_nid2str(int nal, ptl_nid_t nid, char *str)
960 /* XXX this could be a nal method of some sort, 'cept it's config
961 * dependent whether (say) socknal NIDs are actually IP addresses... */
966 snprintf(str, PTL_NALFMT_SIZE-1,
967 "%u:%d.%d.%d.%d", (__u32)(nid >> 32), HIPQUAD(nid));
973 snprintf(str, PTL_NALFMT_SIZE-1, LPD64, nid);
977 snprintf(str, PTL_NALFMT_SIZE-1, "(?%llx)", (long long)nid);
983 char stack_backtrace[LUSTRE_TRACE_SIZE];
984 spinlock_t stack_backtrace_lock = SPIN_LOCK_UNLOCKED;
986 #if defined(__arch_um__)
988 extern int is_kernel_text_address(unsigned long addr);
990 char *portals_debug_dumpstack(void)
993 return "dump stack\n";
996 #elif defined(__i386__)
998 extern int is_kernel_text_address(unsigned long addr);
999 extern int lookup_symbol(unsigned long address, char *buf, int buflen);
1001 char *portals_debug_dumpstack(void)
1003 unsigned long esp = current->thread.esp;
1004 unsigned long *stack = (unsigned long *)&esp;
1007 char *buf = stack_backtrace;
1009 static char buffer[512];
1012 /* User space on another CPU? */
1013 if ((esp ^ (unsigned long)current) & (PAGE_MASK<<1)){
1018 size = sprintf(pbuf, " Call Trace: ");
1020 while (((long) stack & (THREAD_SIZE-1)) != 0) {
1022 if (is_kernel_text_address(addr)) {
1023 rc = lookup_symbol(addr, buffer, 512);
1024 if (rc == -ENOSYS) {
1025 if (buf + LUSTRE_TRACE_SIZE <= pbuf + 12)
1027 size = sprintf(pbuf, "[<%08lx>] ", addr);
1029 if (buf + LUSTRE_TRACE_SIZE
1030 /* fix length + sizeof('\0') */
1031 <= pbuf + strlen(buffer) + 28 + 1)
1033 size = sprintf(pbuf, "([<%08lx>] %s (0x%p)) ",
1034 addr, buffer, stack-1);
1043 #else /* !__arch_um__ && !__i386__ */
1045 char *portals_debug_dumpstack(void)
1047 char *buf = stack_backtrace;
1052 #endif /* __arch_um__ */
1053 EXPORT_SYMBOL(stack_backtrace_lock);
1054 EXPORT_SYMBOL(portals_debug_dumpstack);
1055 #endif /* __KERNEL__ */
1057 EXPORT_SYMBOL(portals_debug_dumplog);
1058 EXPORT_SYMBOL(portals_debug_msg);
1059 EXPORT_SYMBOL(portals_debug_set_level);
1060 EXPORT_SYMBOL(portals_run_upcall);
1061 EXPORT_SYMBOL(portals_run_lbug_upcall);
1062 EXPORT_SYMBOL(portals_nid2str);