Whamcloud - gitweb
LU-1346 libcfs: cleanup macros in kp30.h
[fs/lustre-release.git] / libcfs / libcfs / posix / posix-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.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/posix/posix_debug.c
37  *
38  * Userspace debugging.
39  *
40  */
41
42 # define DEBUG_SUBSYSTEM S_LNET
43
44 #include <libcfs/libcfs.h>
45
46 static char debug_file_name[1024];
47 unsigned int libcfs_subsystem_debug = ~(S_LNET | S_LND);
48 unsigned int libcfs_debug = 0;
49
50 #ifdef HAVE_NETDB_H
51 #include <sys/utsname.h>
52 #endif /* HAVE_NETDB_H */
53 struct utsname *tmp_utsname;
54 static char source_nid[sizeof(tmp_utsname->nodename)];
55
56 static int source_pid;
57 int smp_processor_id = 1;
58 char libcfs_debug_file_path[1024];
59 FILE *debug_file_fd;
60
61 int portals_do_debug_dumplog(void *arg)
62 {
63         printf("Look in %s\n", debug_file_name);
64         return 0;
65 }
66
67
68 void portals_debug_print(void)
69 {
70         return;
71 }
72
73
74 void libcfs_debug_dumplog(void)
75 {
76         printf("Look in %s\n", debug_file_name);
77         return;
78 }
79
80 int libcfs_debug_init(unsigned long bufsize)
81 {
82         char *debug_mask = NULL;
83         char *debug_subsys = NULL;
84         char *debug_filename;
85
86         struct utsname myname;
87
88         if (uname(&myname) == 0)
89                 strcpy(source_nid, myname.nodename);
90         source_pid = getpid();
91
92         /* debug masks */
93         debug_mask = getenv("LIBLUSTRE_DEBUG_MASK");
94         if (debug_mask)
95                 libcfs_debug = (unsigned int) strtol(debug_mask, NULL, 0);
96
97         debug_subsys = getenv("LIBLUSTRE_DEBUG_SUBSYS");
98         if (debug_subsys)
99                 libcfs_subsystem_debug =
100                                 (unsigned int) strtol(debug_subsys, NULL, 0);
101
102         debug_filename = getenv("LIBLUSTRE_DEBUG_BASE");
103         if (debug_filename)
104                 strncpy(libcfs_debug_file_path, debug_filename,
105                         sizeof(libcfs_debug_file_path));
106
107         debug_filename = getenv("LIBLUSTRE_DEBUG_FILE");
108         if (debug_filename)
109                 strncpy(debug_file_name,debug_filename,sizeof(debug_file_name));
110
111         if (debug_file_name[0] == '\0' && libcfs_debug_file_path[0] != '\0')
112                 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
113                          "%s-%s-"CFS_TIME_T".log", libcfs_debug_file_path,
114                          source_nid, time(0));
115
116         if (strcmp(debug_file_name, "stdout") == 0 ||
117             strcmp(debug_file_name, "-") == 0) {
118                 debug_file_fd = stdout;
119         } else if (strcmp(debug_file_name, "stderr") == 0) {
120                 debug_file_fd = stderr;
121         } else if (debug_file_name[0] != '\0') {
122                 debug_file_fd = fopen(debug_file_name, "w");
123                 if (debug_file_fd == NULL)
124                         fprintf(stderr, "%s: unable to open '%s': %s\n",
125                                 source_nid, debug_file_name, strerror(errno));
126         }
127
128         if (debug_file_fd == NULL)
129                 debug_file_fd = stdout;
130
131         return 0;
132 }
133
134 int libcfs_debug_cleanup(void)
135 {
136         if (debug_file_fd != stdout && debug_file_fd != stderr)
137                 fclose(debug_file_fd);
138         return 0;
139 }
140
141 int libcfs_debug_clear_buffer(void)
142 {
143         return 0;
144 }
145
146 int libcfs_debug_mark_buffer(const char *text)
147 {
148
149         fprintf(debug_file_fd, "*******************************************************************************\n");
150         fprintf(debug_file_fd, "DEBUG MARKER: %s\n", text);
151         fprintf(debug_file_fd, "*******************************************************************************\n");
152
153         return 0;
154 }
155
156 int libcfs_debug_msg(struct libcfs_debug_msg_data *msgdata,
157                      const char *format, ...)
158 {
159         va_list args;
160         int     rc;
161
162         va_start(args, format);
163         rc = libcfs_debug_vmsg2(msgdata, format, args, NULL);
164         va_end(args);
165
166         return rc;
167 }
168
169 int
170 libcfs_debug_vmsg2(struct libcfs_debug_msg_data *msgdata,
171                    const char *format1, va_list args,
172                    const char *format2, ...)
173 {
174         struct timeval tv;
175         int            nob;
176         int            remain;
177         va_list        ap;
178         char           buf[PAGE_CACHE_SIZE]; /* size 4096 used for compatimble
179                                             * with linux, where message can`t
180                                             * be exceed PAGE_SIZE */
181         int            console = 0;
182         char *prefix = "Lustre";
183
184         if ((!console) && (!debug_file_fd)) {
185                 return 0;
186         }
187
188         if (msgdata->msg_mask & (D_EMERG | D_ERROR))
189                prefix = "LustreError";
190
191         nob = snprintf(buf, sizeof(buf), "%s: %u-%s:(%s:%d:%s()): ", prefix,
192                        source_pid, source_nid, msgdata->msg_file,
193                        msgdata->msg_line, msgdata->msg_fn);
194
195         remain = sizeof(buf) - nob;
196         if (format1) {
197                 nob += vsnprintf(&buf[nob], remain, format1, args);
198         }
199
200         remain = sizeof(buf) - nob;
201         if ((format2) && (remain > 0)) {
202                 va_start(ap, format2);
203                 nob += vsnprintf(&buf[nob], remain, format2, ap);
204                 va_end(ap);
205         }
206
207         if (debug_file_fd == NULL)
208                 return 0;
209
210         gettimeofday(&tv, NULL);
211
212         fprintf(debug_file_fd, CFS_TIME_T".%06lu:%u:%s:(%s:%d:%s()): %s",
213                 tv.tv_sec, tv.tv_usec, source_pid, source_nid,
214                 msgdata->msg_file, msgdata->msg_line, msgdata->msg_fn, buf);
215
216         return 0;
217 }
218
219 /*
220  * a helper function for RETURN(): the sole purpose is to save 8-16 bytes
221  * on the stack - function calling RETURN() doesn't need to allocate two
222  * additional 'rc' on the stack
223  */
224 long libcfs_log_return(struct libcfs_debug_msg_data *msgdata, long rc)
225 {
226         libcfs_debug_msg(msgdata, "Process leaving (rc=%lu : %ld : %lx)\n",
227                          rc, rc, rc);
228         return rc;
229 }
230
231 /*
232  * a helper function for GOTO(): the sole purpose is to save 8-16 bytes
233  * on the stack - function calling GOTO() doesn't need to allocate two
234  * additional 'rc' on the stack
235  */
236 void libcfs_log_goto(struct libcfs_debug_msg_data *msgdata, const char *l,
237                      long_ptr_t rc)
238 {
239         libcfs_debug_msg(msgdata, "Process leaving via %s (rc=" LPLU " : "
240                          LPLD " : " LPLX ")\n", l, (ulong_ptr_t) rc, rc, rc);
241 }