• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * libcfs/libcfs/linux/linux-debug.c
37  *
38  * Author: Phil Schwan <phil@clusterfs.com>
39  */
40 
41 #include <linux/module.h>
42 #include <linux/kmod.h>
43 #include <linux/notifier.h>
44 #include <linux/kernel.h>
45 #include <linux/mm.h>
46 #include <linux/string.h>
47 #include <linux/stat.h>
48 #include <linux/errno.h>
49 #include <linux/unistd.h>
50 #include <linux/interrupt.h>
51 #include <linux/completion.h>
52 #include <linux/fs.h>
53 #include <asm/uaccess.h>
54 #include <linux/miscdevice.h>
55 
56 # define DEBUG_SUBSYSTEM S_LNET
57 
58 #include "../../../include/linux/libcfs/libcfs.h"
59 
60 #include "../tracefile.h"
61 
62 #include <linux/kallsyms.h>
63 
64 char lnet_upcall[1024] = "/usr/lib/lustre/lnet_upcall";
65 char lnet_debug_log_upcall[1024] = "/usr/lib/lustre/lnet_debug_log_upcall";
66 
67 /**
68  * Upcall function once a Lustre log has been dumped.
69  *
70  * \param file  path of the dumped log
71  */
libcfs_run_debug_log_upcall(char * file)72 void libcfs_run_debug_log_upcall(char *file)
73 {
74 	char *argv[3];
75 	int   rc;
76 	char *envp[] = {
77 		"HOME=/",
78 		"PATH=/sbin:/bin:/usr/sbin:/usr/bin",
79 		NULL};
80 
81 	argv[0] = lnet_debug_log_upcall;
82 
83 	LASSERTF(file != NULL, "called on a null filename\n");
84 	argv[1] = file; /* only need to pass the path of the file */
85 
86 	argv[2] = NULL;
87 
88 	rc = call_usermodehelper(argv[0], argv, envp, 1);
89 	if (rc < 0 && rc != -ENOENT) {
90 		CERROR("Error %d invoking LNET debug log upcall %s %s; "
91 		       "check /proc/sys/lnet/debug_log_upcall\n",
92 		       rc, argv[0], argv[1]);
93 	} else {
94 		CDEBUG(D_HA, "Invoked LNET debug log upcall %s %s\n",
95 		       argv[0], argv[1]);
96 	}
97 }
98 
libcfs_run_upcall(char ** argv)99 void libcfs_run_upcall(char **argv)
100 {
101 	int   rc;
102 	int   argc;
103 	char *envp[] = {
104 		"HOME=/",
105 		"PATH=/sbin:/bin:/usr/sbin:/usr/bin",
106 		NULL};
107 
108 	argv[0] = lnet_upcall;
109 	argc = 1;
110 	while (argv[argc] != NULL)
111 		argc++;
112 
113 	LASSERT(argc >= 2);
114 
115 	rc = call_usermodehelper(argv[0], argv, envp, 1);
116 	if (rc < 0 && rc != -ENOENT) {
117 		CERROR("Error %d invoking LNET upcall %s %s%s%s%s%s%s%s%s; "
118 		       "check /proc/sys/lnet/upcall\n",
119 		       rc, argv[0], argv[1],
120 		       argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
121 		       argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
122 		       argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
123 		       argc < 6 ? "" : ",...");
124 	} else {
125 		CDEBUG(D_HA, "Invoked LNET upcall %s %s%s%s%s%s%s%s%s\n",
126 		       argv[0], argv[1],
127 		       argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
128 		       argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
129 		       argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
130 		       argc < 6 ? "" : ",...");
131 	}
132 }
133 
libcfs_run_lbug_upcall(struct libcfs_debug_msg_data * msgdata)134 void libcfs_run_lbug_upcall(struct libcfs_debug_msg_data *msgdata)
135 {
136 	char *argv[6];
137 	char buf[32];
138 
139 	snprintf(buf, sizeof(buf), "%d", msgdata->msg_line);
140 
141 	argv[1] = "LBUG";
142 	argv[2] = (char *)msgdata->msg_file;
143 	argv[3] = (char *)msgdata->msg_fn;
144 	argv[4] = buf;
145 	argv[5] = NULL;
146 
147 	libcfs_run_upcall (argv);
148 }
149 
150 /* coverity[+kill] */
lbug_with_loc(struct libcfs_debug_msg_data * msgdata)151 void lbug_with_loc(struct libcfs_debug_msg_data *msgdata)
152 {
153 	libcfs_catastrophe = 1;
154 	libcfs_debug_msg(msgdata, "LBUG\n");
155 
156 	if (in_interrupt()) {
157 		panic("LBUG in interrupt.\n");
158 		/* not reached */
159 	}
160 
161 	dump_stack();
162 	if (!libcfs_panic_on_lbug)
163 		libcfs_debug_dumplog();
164 	libcfs_run_lbug_upcall(msgdata);
165 	if (libcfs_panic_on_lbug)
166 		panic("LBUG");
167 	set_task_state(current, TASK_UNINTERRUPTIBLE);
168 	while (1)
169 		schedule();
170 }
171 
panic_notifier(struct notifier_block * self,unsigned long unused1,void * unused2)172 static int panic_notifier(struct notifier_block *self, unsigned long unused1,
173 			 void *unused2)
174 {
175 	if (libcfs_panic_in_progress)
176 		return 0;
177 
178 	libcfs_panic_in_progress = 1;
179 	mb();
180 
181 	return 0;
182 }
183 
184 static struct notifier_block libcfs_panic_notifier = {
185 	.notifier_call	= panic_notifier,
186 	.next		= NULL,
187 	.priority	= 10000,
188 };
189 
libcfs_register_panic_notifier(void)190 void libcfs_register_panic_notifier(void)
191 {
192 	atomic_notifier_chain_register(&panic_notifier_list, &libcfs_panic_notifier);
193 }
194 
libcfs_unregister_panic_notifier(void)195 void libcfs_unregister_panic_notifier(void)
196 {
197 	atomic_notifier_chain_unregister(&panic_notifier_list, &libcfs_panic_notifier);
198 }
199 
200 EXPORT_SYMBOL(libcfs_run_upcall);
201 EXPORT_SYMBOL(libcfs_run_lbug_upcall);
202 EXPORT_SYMBOL(lbug_with_loc);
203