Linux-libre 4.11.5-gnu
[librecmc/linux-libre.git] / drivers / staging / lustre / lnet / libcfs / linux / linux-debug.c
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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * libcfs/libcfs/linux/linux-debug.c
33  *
34  * Author: Phil Schwan <phil@clusterfs.com>
35  */
36
37 #include <linux/module.h>
38 #include <linux/kmod.h>
39 #include <linux/notifier.h>
40 #include <linux/kernel.h>
41 #include <linux/mm.h>
42 #include <linux/string.h>
43 #include <linux/stat.h>
44 #include <linux/errno.h>
45 #include <linux/unistd.h>
46 #include <linux/interrupt.h>
47 #include <linux/completion.h>
48 #include <linux/fs.h>
49 #include <linux/uaccess.h>
50 #include <linux/miscdevice.h>
51
52 # define DEBUG_SUBSYSTEM S_LNET
53
54 #include "../../../include/linux/libcfs/libcfs.h"
55
56 #include "../tracefile.h"
57
58 #include <linux/kallsyms.h>
59
60 char lnet_debug_log_upcall[1024] = "/usr/lib/lustre/lnet_debug_log_upcall";
61
62 /**
63  * Upcall function once a Lustre log has been dumped.
64  *
65  * \param file  path of the dumped log
66  */
67 void libcfs_run_debug_log_upcall(char *file)
68 {
69         char *argv[3];
70         int rc;
71         static const char * const envp[] = {
72                 "HOME=/",
73                 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
74                 NULL
75         };
76
77         argv[0] = lnet_debug_log_upcall;
78
79         LASSERTF(file, "called on a null filename\n");
80         argv[1] = file; /* only need to pass the path of the file */
81
82         argv[2] = NULL;
83
84         rc = call_usermodehelper(argv[0], argv, (char **)envp, 1);
85         if (rc < 0 && rc != -ENOENT) {
86                 CERROR("Error %d invoking LNET debug log upcall %s %s; check /sys/kernel/debug/lnet/debug_log_upcall\n",
87                        rc, argv[0], argv[1]);
88         } else {
89                 CDEBUG(D_HA, "Invoked LNET debug log upcall %s %s\n",
90                        argv[0], argv[1]);
91         }
92 }
93
94 /* coverity[+kill] */
95 void __noreturn lbug_with_loc(struct libcfs_debug_msg_data *msgdata)
96 {
97         libcfs_catastrophe = 1;
98         libcfs_debug_msg(msgdata, "LBUG\n");
99
100         if (in_interrupt()) {
101                 panic("LBUG in interrupt.\n");
102                 /* not reached */
103         }
104
105         dump_stack();
106         if (!libcfs_panic_on_lbug)
107                 libcfs_debug_dumplog();
108         if (libcfs_panic_on_lbug)
109                 panic("LBUG");
110         set_current_state(TASK_UNINTERRUPTIBLE);
111         while (1)
112                 schedule();
113 }
114 EXPORT_SYMBOL(lbug_with_loc);
115
116 static int panic_notifier(struct notifier_block *self, unsigned long unused1,
117                           void *unused2)
118 {
119         if (libcfs_panic_in_progress)
120                 return 0;
121
122         libcfs_panic_in_progress = 1;
123         mb();
124
125         return 0;
126 }
127
128 static struct notifier_block libcfs_panic_notifier = {
129         .notifier_call  = panic_notifier,
130         .next           = NULL,
131         .priority       = 10000,
132 };
133
134 void libcfs_register_panic_notifier(void)
135 {
136         atomic_notifier_chain_register(&panic_notifier_list,
137                                        &libcfs_panic_notifier);
138 }
139
140 void libcfs_unregister_panic_notifier(void)
141 {
142         atomic_notifier_chain_unregister(&panic_notifier_list,
143                                          &libcfs_panic_notifier);
144 }