• 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 <linux/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; check /proc/sys/lnet/debug_log_upcall\n",
91 		       rc, argv[0], argv[1]);
92 	} else {
93 		CDEBUG(D_HA, "Invoked LNET debug log upcall %s %s\n",
94 		       argv[0], argv[1]);
95 	}
96 }
97 
libcfs_run_upcall(char ** argv)98 void libcfs_run_upcall(char **argv)
99 {
100 	int   rc;
101 	int   argc;
102 	char *envp[] = {
103 		"HOME=/",
104 		"PATH=/sbin:/bin:/usr/sbin:/usr/bin",
105 		NULL};
106 
107 	argv[0] = lnet_upcall;
108 	argc = 1;
109 	while (argv[argc] != NULL)
110 		argc++;
111 
112 	LASSERT(argc >= 2);
113 
114 	rc = call_usermodehelper(argv[0], argv, envp, 1);
115 	if (rc < 0 && rc != -ENOENT) {
116 		CERROR("Error %d invoking LNET upcall %s %s%s%s%s%s%s%s%s; check /proc/sys/lnet/upcall\n",
117 		       rc, argv[0], argv[1],
118 		       argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
119 		       argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
120 		       argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
121 		       argc < 6 ? "" : ",...");
122 	} else {
123 		CDEBUG(D_HA, "Invoked LNET upcall %s %s%s%s%s%s%s%s%s\n",
124 		       argv[0], argv[1],
125 		       argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
126 		       argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
127 		       argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
128 		       argc < 6 ? "" : ",...");
129 	}
130 }
131 
libcfs_run_lbug_upcall(struct libcfs_debug_msg_data * msgdata)132 void libcfs_run_lbug_upcall(struct libcfs_debug_msg_data *msgdata)
133 {
134 	char *argv[6];
135 	char buf[32];
136 
137 	snprintf(buf, sizeof(buf), "%d", msgdata->msg_line);
138 
139 	argv[1] = "LBUG";
140 	argv[2] = (char *)msgdata->msg_file;
141 	argv[3] = (char *)msgdata->msg_fn;
142 	argv[4] = buf;
143 	argv[5] = NULL;
144 
145 	libcfs_run_upcall (argv);
146 }
147 
148 /* coverity[+kill] */
lbug_with_loc(struct libcfs_debug_msg_data * msgdata)149 void __noreturn lbug_with_loc(struct libcfs_debug_msg_data *msgdata)
150 {
151 	libcfs_catastrophe = 1;
152 	libcfs_debug_msg(msgdata, "LBUG\n");
153 
154 	if (in_interrupt()) {
155 		panic("LBUG in interrupt.\n");
156 		/* not reached */
157 	}
158 
159 	dump_stack();
160 	if (!libcfs_panic_on_lbug)
161 		libcfs_debug_dumplog();
162 	libcfs_run_lbug_upcall(msgdata);
163 	if (libcfs_panic_on_lbug)
164 		panic("LBUG");
165 	set_task_state(current, TASK_UNINTERRUPTIBLE);
166 	while (1)
167 		schedule();
168 }
169 
panic_notifier(struct notifier_block * self,unsigned long unused1,void * unused2)170 static int panic_notifier(struct notifier_block *self, unsigned long unused1,
171 			 void *unused2)
172 {
173 	if (libcfs_panic_in_progress)
174 		return 0;
175 
176 	libcfs_panic_in_progress = 1;
177 	mb();
178 
179 	return 0;
180 }
181 
182 static struct notifier_block libcfs_panic_notifier = {
183 	.notifier_call	= panic_notifier,
184 	.next		= NULL,
185 	.priority	= 10000,
186 };
187 
libcfs_register_panic_notifier(void)188 void libcfs_register_panic_notifier(void)
189 {
190 	atomic_notifier_chain_register(&panic_notifier_list, &libcfs_panic_notifier);
191 }
192 
libcfs_unregister_panic_notifier(void)193 void libcfs_unregister_panic_notifier(void)
194 {
195 	atomic_notifier_chain_unregister(&panic_notifier_list, &libcfs_panic_notifier);
196 }
197 
198 EXPORT_SYMBOL(libcfs_run_upcall);
199 EXPORT_SYMBOL(libcfs_run_lbug_upcall);
200 EXPORT_SYMBOL(lbug_with_loc);
201