1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * libcfs/libcfs/posix/posix_debug.c
38 * Userspace debugging.
42 # define DEBUG_SUBSYSTEM S_LNET
44 #include <libcfs/libcfs.h>
46 static char debug_file_name[1024];
48 #ifdef HAVE_CATAMOUNT_DATA_H
49 #include <catamount/data.h>
50 #include <catamount/lputs.h>
52 static char source_nid[16];
53 /* 0 indicates no messages to console, 1 is errors, > 1 is all debug messages */
54 static int toconsole = 1;
55 unsigned int libcfs_console_ratelimit = 1;
56 cfs_duration_t libcfs_console_max_delay;
57 cfs_duration_t libcfs_console_min_delay;
58 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
59 #else /* !HAVE_CATAMOUNT_DATA_H */
61 #include <sys/utsname.h>
62 #endif /* HAVE_NETDB_H */
63 struct utsname *tmp_utsname;
64 static char source_nid[sizeof(tmp_utsname->nodename)];
65 #endif /* HAVE_CATAMOUNT_DATA_H */
67 static int source_pid;
68 int smp_processor_id = 1;
69 char debug_file_path[1024];
72 int portals_do_debug_dumplog(void *arg)
74 printf("Look in %s\n", debug_file_name);
79 void portals_debug_print(void)
85 void libcfs_debug_dumplog(void)
87 printf("Look in %s\n", debug_file_name);
91 int libcfs_debug_init(unsigned long bufsize)
93 char *debug_mask = NULL;
94 char *debug_subsys = NULL;
97 #ifdef HAVE_CATAMOUNT_DATA_H
98 char *debug_console = NULL;
99 char *debug_ratelimit = NULL;
100 char *debug_max_delay = NULL;
101 char *debug_min_delay = NULL;
102 char *debug_backoff = NULL;
104 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
105 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
107 snprintf(source_nid, sizeof(source_nid) - 1, "%u", _my_pnid);
108 source_pid = _my_pid;
110 debug_console = getenv("LIBLUSTRE_DEBUG_CONSOLE");
111 if (debug_console != NULL) {
112 toconsole = strtoul(debug_console, NULL, 0);
113 CDEBUG(D_INFO, "set liblustre toconsole to %u\n", toconsole);
115 debug_ratelimit = getenv("LIBLUSTRE_DEBUG_CONSOLE_RATELIMIT");
116 if (debug_ratelimit != NULL) {
117 libcfs_console_ratelimit = strtoul(debug_ratelimit, NULL, 0);
118 CDEBUG(D_INFO, "set liblustre console ratelimit to %u\n",
119 libcfs_console_ratelimit);
121 debug_max_delay = getenv("LIBLUSTRE_DEBUG_CONSOLE_MAX_DELAY");
122 if (debug_max_delay != NULL)
123 libcfs_console_max_delay =
124 cfs_time_seconds(strtoul(debug_max_delay, NULL, 0));
125 debug_min_delay = getenv("LIBLUSTRE_DEBUG_CONSOLE_MIN_DELAY");
126 if (debug_min_delay != NULL)
127 libcfs_console_min_delay =
128 cfs_time_seconds(strtoul(debug_min_delay, NULL, 0));
129 if (debug_min_delay || debug_max_delay) {
130 if (!libcfs_console_max_delay || !libcfs_console_min_delay ||
131 libcfs_console_max_delay < libcfs_console_min_delay) {
132 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
133 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
134 CDEBUG(D_INFO, "LIBLUSTRE_DEBUG_CONSOLE_MAX_DELAY "
135 "should be greater than "
136 "LIBLUSTRE_DEBUG_CONSOLE_MIN_DELAY "
137 "and both parameters should be non-null"
138 ": restore default values\n");
140 CDEBUG(D_INFO, "set liblustre console max delay to %lus"
141 " and min delay to %lus\n",
143 cfs_duration_sec(libcfs_console_max_delay),
145 cfs_duration_sec(libcfs_console_min_delay));
148 debug_backoff = getenv("LIBLUSTRE_DEBUG_CONSOLE_BACKOFF");
149 if (debug_backoff != NULL) {
150 libcfs_console_backoff = strtoul(debug_backoff, NULL, 0);
151 if (libcfs_console_backoff <= 0) {
152 libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
153 CDEBUG(D_INFO, "LIBLUSTRE_DEBUG_CONSOLE_BACKOFF <= 0: "
154 "restore default value\n");
156 CDEBUG(D_INFO, "set liblustre console backoff to %u\n",
157 libcfs_console_backoff);
161 struct utsname myname;
163 if (uname(&myname) == 0)
164 strcpy(source_nid, myname.nodename);
165 source_pid = getpid();
168 debug_mask = getenv("LIBLUSTRE_DEBUG_MASK");
170 libcfs_debug = (unsigned int) strtol(debug_mask, NULL, 0);
172 debug_subsys = getenv("LIBLUSTRE_DEBUG_SUBSYS");
174 libcfs_subsystem_debug =
175 (unsigned int) strtol(debug_subsys, NULL, 0);
177 debug_filename = getenv("LIBLUSTRE_DEBUG_BASE");
179 strncpy(debug_file_path,debug_filename,sizeof(debug_file_path));
181 debug_filename = getenv("LIBLUSTRE_DEBUG_FILE");
183 strncpy(debug_file_name,debug_filename,sizeof(debug_file_name));
185 if (debug_file_name[0] == '\0' && debug_file_path[0] != '\0')
186 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
187 "%s-%s-"CFS_TIME_T".log", debug_file_path, source_nid, time(0));
189 if (strcmp(debug_file_name, "stdout") == 0 ||
190 strcmp(debug_file_name, "-") == 0) {
191 debug_file_fd = stdout;
192 } else if (strcmp(debug_file_name, "stderr") == 0) {
193 debug_file_fd = stderr;
194 } else if (debug_file_name[0] != '\0') {
195 debug_file_fd = fopen(debug_file_name, "w");
196 if (debug_file_fd == NULL)
197 fprintf(stderr, "%s: unable to open '%s': %s\n",
198 source_nid, debug_file_name, strerror(errno));
201 if (debug_file_fd == NULL)
202 debug_file_fd = stdout;
207 int libcfs_debug_cleanup(void)
209 if (debug_file_fd != stdout && debug_file_fd != stderr)
210 fclose(debug_file_fd);
214 int libcfs_debug_clear_buffer(void)
219 int libcfs_debug_mark_buffer(char *text)
222 fprintf(debug_file_fd, "*******************************************************************************\n");
223 fprintf(debug_file_fd, "DEBUG MARKER: %s\n", text);
224 fprintf(debug_file_fd, "*******************************************************************************\n");
229 #ifdef HAVE_CATAMOUNT_DATA_H
230 #define CATAMOUNT_MAXLINE (256-4)
231 void catamount_printline(char *buf, size_t size)
238 pos += CATAMOUNT_MAXLINE;
239 prsize -= CATAMOUNT_MAXLINE;
245 libcfs_debug_vmsg2(cfs_debug_limit_state_t *cdls,
246 int subsys, int mask,
247 const char *file, const char *fn, const int line,
248 const char *format1, va_list args,
249 const char *format2, ...)
255 char buf[CFS_PAGE_SIZE]; /* size 4096 used for compatimble
256 * with linux, where message can`t
257 * be exceed PAGE_SIZE */
259 char *prefix = "Lustre";
261 #ifdef HAVE_CATAMOUNT_DATA_H
262 /* toconsole == 0 - all messages to debug_file_fd
263 * toconsole == 1 - warnings to console, all to debug_file_fd
264 * toconsole > 1 - all debug to console */
265 if (((mask & libcfs_printk) && toconsole == 1) || toconsole > 1)
269 if ((!console) && (!debug_file_fd)) {
273 if (mask & (D_EMERG | D_ERROR))
274 prefix = "LustreError";
276 nob = snprintf(buf, sizeof(buf), "%s: %u-%s:(%s:%d:%s()): ", prefix,
277 source_pid, source_nid, file, line, fn);
279 remain = sizeof(buf) - nob;
281 nob += vsnprintf(&buf[nob], remain, format1, args);
284 remain = sizeof(buf) - nob;
285 if ((format2) && (remain > 0)) {
286 va_start(ap, format2);
287 nob += vsnprintf(&buf[nob], remain, format2, ap);
291 #ifdef HAVE_CATAMOUNT_DATA_H
293 /* check rate limit for console */
295 if (libcfs_console_ratelimit &&
296 cdls->cdls_next != 0 && /* not first time ever */
297 !cfs_time_after(cfs_time_current(), cdls->cdls_next)) {
299 /* skipping a console message */
304 if (cfs_time_after(cfs_time_current(), cdls->cdls_next +
305 libcfs_console_max_delay +
306 cfs_time_seconds(10))) {
307 /* last timeout was a long time ago */
308 cdls->cdls_delay /= libcfs_console_backoff * 4;
310 cdls->cdls_delay *= libcfs_console_backoff;
312 if (cdls->cdls_delay <
313 libcfs_console_min_delay)
315 libcfs_console_min_delay;
316 else if (cdls->cdls_delay >
317 libcfs_console_max_delay)
319 libcfs_console_max_delay;
322 /* ensure cdls_next is never zero after it's been seen */
323 cdls->cdls_next = (cfs_time_current() + cdls->cdls_delay) | 1;
326 if (cdls != NULL && cdls->cdls_count != 0) {
329 nob = snprintf(buf2, sizeof(buf2),
330 "Skipped %d previous similar message%s\n",
331 cdls->cdls_count, (cdls->cdls_count > 1) ? "s" : "");
333 catamount_printline(buf2, nob);
334 cdls->cdls_count = 0;
337 catamount_printline(buf, nob);
340 /* return on toconsole > 1, as we don't want the user getting
341 * spammed by the debug data */
345 if (debug_file_fd == NULL)
348 gettimeofday(&tv, NULL);
350 fprintf(debug_file_fd, CFS_TIME_T".%06lu:%u:%s:(%s:%d:%s()): %s",
351 tv.tv_sec, tv.tv_usec, source_pid, source_nid,
352 file, line, fn, buf);
358 libcfs_assertion_failed(const char *expr, const char *file, const char *func,
361 libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line,
362 "ASSERTION(%s) failed\n", expr);