Whamcloud - gitweb
Revert "b=23913 fix "ASSERTION(!cfs_list_empty(&dquot->dq_hash)) failed""
[fs/lustre-release.git] / lnet / utils / debug.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
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.
11  *
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).
17  *
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
21  *
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
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lnet/utils/debug.c
37  * Some day I'll split all of this functionality into a cfs_debug module
38  * of its own. That day is not today.
39  */
40
41 #define __USE_FILE_OFFSET64
42 #ifndef _GNU_SOURCE
43 #define _GNU_SOURCE
44 #endif
45
46 #include <stdio.h>
47 #ifdef HAVE_NETDB_H
48 #include <netdb.h>
49 #endif
50 #include <stdlib.h>
51 #include <string.h>
52 #ifdef HAVE_SYS_IOCTL_H
53 #include <sys/ioctl.h>
54 #endif
55 #ifndef _IOWR
56 #include "ioctl.h"
57 #endif
58 #include <fcntl.h>
59 #include <errno.h>
60 #include <unistd.h>
61 #include <assert.h>
62
63 #include <sys/types.h>
64 #include <sys/socket.h>
65 #include <sys/stat.h>
66 #include <sys/mman.h>
67 #include <sys/utsname.h>
68
69 #include <lnet/api-support.h>
70 #include <lnet/lnetctl.h>
71 #include <libcfs/portals_utils.h>
72 #include "parser.h"
73
74 #include <time.h>
75
76 static char rawbuf[8192];
77 static char *buf = rawbuf;
78 static int max = 8192;
79 /*static int g_pfd = -1;*/
80 static int subsystem_mask = ~0;
81 static int debug_mask = ~0;
82
83 #define MAX_MARK_SIZE 256
84
85 static const char *libcfs_debug_subsystems[] =
86         {"undefined", "mdc", "mds", "osc",
87          "ost", "class", "log", "llite",
88          "rpc", "mgmt", "lnet", "lnd",
89          "pinger", "filter", "", "echo",
90          "ldlm", "lov", "lquota", "",
91          "", "", "", "lmv",
92          "", "sec", "gss", "",
93          "mgc", "mgs", "fid", "fld", NULL};
94 static const char *libcfs_debug_masks[] =
95         {"trace", "inode", "super", "ext2",
96          "malloc", "cache", "info", "ioctl",
97          "neterror", "net", "warning", "buffs",
98          "other", "dentry", "nettrace", "page",
99          "dlmtrace", "error", "emerg", "ha",
100          "rpctrace", "vfstrace", "reada", "mmap",
101          "config", "console", "quota", "sec", NULL};
102
103 struct debug_daemon_cmd {
104         char *cmd;
105         unsigned int cmdv;
106 };
107
108 static const struct debug_daemon_cmd libcfs_debug_daemon_cmd[] = {
109         {"start", DEBUG_DAEMON_START},
110         {"stop", DEBUG_DAEMON_STOP},
111         {0, 0}
112 };
113
114 #ifdef __linux__
115
116 #define DAEMON_CTL_NAME         "/proc/sys/lnet/daemon_file"
117 #define SUBSYS_DEBUG_CTL_NAME   "/proc/sys/lnet/subsystem_debug"
118 #define DEBUG_CTL_NAME          "/proc/sys/lnet/debug"
119 #define DUMP_KERNEL_CTL_NAME    "/proc/sys/lnet/dump_kernel"
120
121 static int
122 dbg_open_ctlhandle(const char *str)
123 {
124         int fd;
125         fd = open(str, O_WRONLY);
126         if (fd < 0) {
127                 fprintf(stderr, "open %s failed: %s\n", str,
128                         strerror(errno));
129                 return -1;
130         }
131         return fd;
132 }
133
134 static void
135 dbg_close_ctlhandle(int fd)
136 {
137         close(fd);
138 }
139
140 static int
141 dbg_write_cmd(int fd, char *str, int len)
142 {
143         int    rc  = write(fd, str, len);
144
145         return (rc == len ? 0 : 1);
146 }
147
148 #elif defined(__DARWIN__)
149
150 #define DAEMON_CTL_NAME         "lnet.trace_daemon"
151 #define SUBSYS_DEBUG_CTL_NAME   "lnet.subsystem_debug"
152 #define DEBUG_CTL_NAME          "lnet.debug"
153 #define DUMP_KERNEL_CTL_NAME    "lnet.trace_dumpkernel"
154
155 static char     sysctl_name[128];
156 static int
157 dbg_open_ctlhandle(const char *str)
158 {
159
160         if (strlen(str)+1 > 128) {
161                 fprintf(stderr, "sysctl name is too long: %s.\n", str);
162                 return -1;
163         }
164         strcpy(sysctl_name, str);
165
166         return 0;
167 }
168
169 static void
170 dbg_close_ctlhandle(int fd)
171 {
172         sysctl_name[0] = '\0';
173         return;
174 }
175
176 static int
177 dbg_write_cmd(int fd, char *str, int len)
178 {
179         int     rc;
180
181         rc = sysctlbyname(sysctl_name, NULL, NULL, str, len+1);
182         if (rc != 0) {
183                 fprintf(stderr, "sysctl %s with cmd (%s) error: %d\n",
184                         sysctl_name, str, errno);
185         }
186         return (rc == 0 ? 0: 1);
187 }
188
189 #else
190 #error - Unknown sysctl convention.
191 #endif
192
193 static int do_debug_mask(char *name, int enable)
194 {
195         int found = 0, i;
196
197         for (i = 0; libcfs_debug_subsystems[i] != NULL; i++) {
198                 if (strcasecmp(name, libcfs_debug_subsystems[i]) == 0 ||
199                     strcasecmp(name, "all_subs") == 0) {
200                         printf("%s output from subsystem \"%s\"\n",
201                                 enable ? "Enabling" : "Disabling",
202                                 libcfs_debug_subsystems[i]);
203                         if (enable)
204                                 subsystem_mask |= (1 << i);
205                         else
206                                 subsystem_mask &= ~(1 << i);
207                         found = 1;
208                 }
209         }
210         for (i = 0; libcfs_debug_masks[i] != NULL; i++) {
211                 if (strcasecmp(name, libcfs_debug_masks[i]) == 0 ||
212                     strcasecmp(name, "all_types") == 0) {
213                         printf("%s output of type \"%s\"\n",
214                                 enable ? "Enabling" : "Disabling",
215                                 libcfs_debug_masks[i]);
216                         if (enable)
217                                 debug_mask |= (1 << i);
218                         else
219                                 debug_mask &= ~(1 << i);
220                         found = 1;
221                 }
222         }
223
224         return found;
225 }
226
227 int dbg_initialize(int argc, char **argv)
228 {
229         return 0;
230 }
231
232 int jt_dbg_filter(int argc, char **argv)
233 {
234         int   i;
235
236         if (argc < 2) {
237                 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
238                         argv[0]);
239                 return 0;
240         }
241
242         for (i = 1; i < argc; i++)
243                 if (!do_debug_mask(argv[i], 0))
244                         fprintf(stderr, "Unknown subsystem or debug type: %s\n",
245                                 argv[i]);
246         return 0;
247 }
248
249 int jt_dbg_show(int argc, char **argv)
250 {
251         int    i;
252
253         if (argc < 2) {
254                 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
255                         argv[0]);
256                 return 0;
257         }
258
259         for (i = 1; i < argc; i++)
260                 if (!do_debug_mask(argv[i], 1))
261                         fprintf(stderr, "Unknown subsystem or debug type: %s\n",
262                                 argv[i]);
263
264         return 0;
265 }
266
267 static int applymask(char* procpath, int value)
268 {
269         int rc;
270         char buf[64];
271         int len = snprintf(buf, 64, "%d", value);
272
273         int fd = dbg_open_ctlhandle(procpath);
274         if (fd == -1) {
275                 fprintf(stderr, "Unable to open %s: %s\n",
276                         procpath, strerror(errno));
277                 return fd;
278         }
279         rc = dbg_write_cmd(fd, buf, len+1);
280         if (rc != 0) {
281                 fprintf(stderr, "Write to %s failed: %s\n",
282                         procpath, strerror(errno));
283         }
284
285         dbg_close_ctlhandle(fd);
286
287         return rc;
288 }
289
290 static void applymask_all(unsigned int subs_mask, unsigned int debug_mask)
291 {
292         if (!dump_filename) {
293                 applymask(SUBSYS_DEBUG_CTL_NAME, subs_mask);
294                 applymask(DEBUG_CTL_NAME, debug_mask);
295         } else {
296                 struct libcfs_debug_ioctl_data data;
297
298                 data.hdr.ioc_len = sizeof(data);
299                 data.hdr.ioc_version = 0;
300                 data.subs = subs_mask;
301                 data.debug = debug_mask;
302
303                 dump(OBD_DEV_ID, LIBCFS_IOC_DEBUG_MASK, &data);
304         }
305         printf("Applied subsystem_debug=%d, debug=%d to /proc/sys/lnet\n",
306                subs_mask, debug_mask);
307 }
308
309 int jt_dbg_list(int argc, char **argv)
310 {
311         int i;
312
313         if (argc != 2) {
314                 fprintf(stderr, "usage: %s <subs || types>\n", argv[0]);
315                 return 0;
316         }
317
318         if (strcasecmp(argv[1], "subs") == 0) {
319                 printf("Subsystems: all_subs");
320                 for (i = 0; libcfs_debug_subsystems[i] != NULL; i++)
321                         if (libcfs_debug_subsystems[i][0])
322                                 printf(", %s", libcfs_debug_subsystems[i]);
323                 printf("\n");
324         } else if (strcasecmp(argv[1], "types") == 0) {
325                 printf("Types: all_types");
326                 for (i = 0; libcfs_debug_masks[i] != NULL; i++)
327                         printf(", %s", libcfs_debug_masks[i]);
328                 printf("\n");
329         } else if (strcasecmp(argv[1], "applymasks") == 0) {
330                 applymask_all(subsystem_mask, debug_mask);
331         }
332         return 0;
333 }
334
335 /* all strings nul-terminated; only the struct and hdr need to be freed */
336 struct dbg_line {
337         struct ptldebug_header *hdr;
338         char *file;
339         char *fn;
340         char *text;
341 };
342
343 static int cmp_rec(const void *p1, const void *p2)
344 {
345         struct dbg_line *d1 = *(struct dbg_line **)p1;
346         struct dbg_line *d2 = *(struct dbg_line **)p2;
347
348         if (d1->hdr->ph_sec < d2->hdr->ph_sec)
349                 return -1;
350         if (d1->hdr->ph_sec == d2->hdr->ph_sec &&
351             d1->hdr->ph_usec < d2->hdr->ph_usec)
352                 return -1;
353         if (d1->hdr->ph_sec == d2->hdr->ph_sec &&
354             d1->hdr->ph_usec == d2->hdr->ph_usec)
355                 return 0;
356         return 1;
357 }
358
359 static void print_rec(struct dbg_line ***linevp, int used, int fdout)
360 {
361         struct dbg_line **linev = *linevp;
362         int i;
363
364         qsort(linev, used, sizeof(struct dbg_line *), cmp_rec);
365         for (i = 0; i < used; i++) {
366                 struct dbg_line *line = linev[i];
367                 struct ptldebug_header *hdr = line->hdr;
368                 char out[4097];
369                 char *buf = out;
370                 int bytes;
371                 ssize_t bytes_written;
372
373                 bytes = sprintf(out, "%08x:%08x:%u:%u.%06llu:%u:%u:%u:(%s:%u:%s()) %s",
374                                 hdr->ph_subsys, hdr->ph_mask, hdr->ph_cpu_id,
375                                 hdr->ph_sec, (unsigned long long)hdr->ph_usec,
376                                 hdr->ph_stack, hdr->ph_pid, hdr->ph_extern_pid,
377                                 line->file, hdr->ph_line_num, line->fn, line->text);
378                 while (bytes > 0) {
379                         bytes_written = write(fdout, buf, bytes);
380                         if (bytes_written <= 0)
381                                 break;
382                         bytes -= bytes_written;
383                         buf += bytes_written;
384                 }
385                 free(line->hdr);
386                 free(line);
387         }
388         free(linev);
389         *linevp = NULL;
390 }
391
392 static int add_rec(struct dbg_line *line, struct dbg_line ***linevp, int *lenp,
393                    int used)
394 {
395         struct dbg_line **linev = *linevp;
396
397         if (used == *lenp) {
398                 int nlen = *lenp + 4096;
399                 int nsize = nlen * sizeof(struct dbg_line *);
400
401                 linev = realloc(*linevp, nsize);
402                 if (!linev)
403                         return -ENOMEM;
404
405                 *linevp = linev;
406                 *lenp = nlen;
407         }
408         linev[used] = line;
409
410         return 0;
411 }
412   
413 static void dump_hdr(unsigned long long offset, struct ptldebug_header *hdr)
414 {
415         fprintf(stderr, "badly-formed record at offset = %llu\n", offset);
416         fprintf(stderr, "  len = %u\n", hdr->ph_len);
417         fprintf(stderr, "  flags = %x\n", hdr->ph_flags);
418         fprintf(stderr, "  subsystem = %x\n", hdr->ph_subsys);
419         fprintf(stderr, "  mask = %x\n", hdr->ph_mask);
420         fprintf(stderr, "  cpu_id = %u\n", hdr->ph_cpu_id);
421         fprintf(stderr, "  seconds = %u\n", hdr->ph_sec);
422         fprintf(stderr, "  microseconds = %lu\n", (long)hdr->ph_usec);
423         fprintf(stderr, "  stack = %u\n", hdr->ph_stack);
424         fprintf(stderr, "  pid = %u\n", hdr->ph_pid);
425         fprintf(stderr, "  host pid = %u\n", hdr->ph_extern_pid);
426         fprintf(stderr, "  line number = %u\n", hdr->ph_line_num);
427 }
428  
429 #define HDR_SIZE sizeof(*hdr)
430  
431 static int parse_buffer(int fdin, int fdout)
432 {
433         struct dbg_line *line;
434         struct ptldebug_header *hdr;
435         char buf[4097], *ptr;
436         unsigned long dropped = 0, kept = 0, bad = 0;
437         struct dbg_line **linev = NULL;
438         int linev_len = 0;
439         int rc;
440  
441         hdr = (void *)buf;
442   
443         while (1) {
444                 int first_bad = 1;
445                 int count;
446  
447                 count = HDR_SIZE;
448                 ptr = buf;
449         readhdr:
450                 rc = read(fdin, ptr, count);
451                 if (rc <= 0)
452                          goto print;
453   
454                 ptr += rc;
455                 count -= rc;
456                 if (count > 0)
457                         goto readhdr;
458                 
459                 if (hdr->ph_len > 4094 ||       /* is this header bogus? */
460                     hdr->ph_cpu_id > 65536 ||
461                     hdr->ph_stack > 65536 ||
462                     hdr->ph_sec < (1 << 30) ||
463                     hdr->ph_usec > 1000000000 ||
464                     hdr->ph_line_num > 65536) {
465                         if (first_bad)
466                                 dump_hdr(lseek(fdin, 0, SEEK_CUR), hdr);
467                         bad += first_bad;
468                         first_bad = 0;
469  
470                         /* try to restart on next line */
471                         while (count < HDR_SIZE && buf[count] != '\n')
472                                 count++;
473                         if (buf[count] == '\n')
474                                 count++; /* move past '\n' */
475                         if (HDR_SIZE - count > 0) {
476                                 int left = HDR_SIZE - count;
477
478                                 memmove(buf, buf + count, left);
479                                 ptr = buf + left;
480
481                                 goto readhdr;
482                         }
483
484                         continue;
485                 }
486   
487                 if (hdr->ph_len == 0)
488                         continue;
489
490                 count = hdr->ph_len - HDR_SIZE;
491         readmore:
492                 rc = read(fdin, ptr, count);
493                 if (rc <= 0)
494                         break;
495   
496                 ptr += rc;
497                 count -= rc;
498                 if (count > 0)
499                         goto readmore;
500  
501                 first_bad = 1;
502
503                 if ((hdr->ph_subsys && !(subsystem_mask & hdr->ph_subsys)) ||
504                     (hdr->ph_mask && !(debug_mask & hdr->ph_mask))) {
505                         dropped++;
506                         continue;
507                 }
508   
509         retry_alloc:
510                 line = malloc(sizeof(*line));
511                 if (line == NULL) {
512                          if (linev) {
513                                 fprintf(stderr, "error: line malloc(%u): "
514                                         "printing accumulated records\n",
515                                         (unsigned int)sizeof(*line));
516                                 print_rec(&linev, kept, fdout);
517
518                                 goto retry_alloc;
519                         }
520                         fprintf(stderr, "error: line malloc(%u): exiting\n",
521                                 (unsigned int)sizeof(*line));
522                         break;
523                 }
524
525                 line->hdr = malloc(hdr->ph_len + 1);
526                 if (line->hdr == NULL) {
527                         free(line);
528                         if (linev) {
529                                 fprintf(stderr, "error: hdr malloc(%u): "
530                                         "printing accumulated records\n",
531                                         hdr->ph_len + 1);
532                                 print_rec(&linev, kept, fdout);
533  
534                                 goto retry_alloc;
535                         }
536                         fprintf(stderr, "error: hdr malloc(%u): exiting\n",
537                                         hdr->ph_len + 1);
538                         break;
539                 }
540   
541                 ptr = (void *)line->hdr;
542                 memcpy(line->hdr, buf, hdr->ph_len);
543                 ptr[hdr->ph_len] = '\0';
544
545                 ptr += sizeof(*hdr);
546                 line->file = ptr;
547                 ptr += strlen(line->file) + 1;
548                 line->fn = ptr;
549                 ptr += strlen(line->fn) + 1;
550                 line->text = ptr;
551  
552         retry_add:
553                 if (add_rec(line, &linev, &linev_len, kept) < 0) {
554                         if (linev) {
555                                 fprintf(stderr, "error: add_rec[%u] failed; "
556                                         "print accumulated records\n",
557                                         linev_len);
558                                 print_rec(&linev, kept, fdout);
559
560                                 goto retry_add;
561                         }
562                         fprintf(stderr, "error: add_rec[0] failed; exiting\n");
563                         break;
564                 }
565                 kept++;
566         }
567
568 print:
569         if (linev)
570                 print_rec(&linev, kept, fdout);
571
572         printf("Debug log: %lu lines, %lu kept, %lu dropped, %lu bad.\n",
573                 dropped + kept + bad, kept, dropped, bad);
574   
575         return 0;
576 }
577
578 int jt_dbg_debug_kernel(int argc, char **argv)
579 {
580         char filename[4096];
581         struct stat st;
582         int raw = 0;
583         int save_errno;
584         int fdin;
585         int fdout;
586         int rc;
587
588         if (argc > 3) {
589                 fprintf(stderr, "usage: %s [file] [raw]\n", argv[0]);
590                 return 0;
591         }
592
593         if (argc > 2) {
594                 raw = atoi(argv[2]);
595         } else if (argc > 1 && (argv[1][0] == '0' || argv[1][0] == '1')) {
596                 raw = atoi(argv[1]);
597                 argc--;
598         }
599
600         /* If we are dumping raw (which means no conversion step to ASCII)
601          * then dump directly to any supplied filename, otherwise this is
602          * just a temp file and we dump to the real file at convert time. */
603         if (argc > 1 && raw)
604                 strcpy(filename, argv[1]);
605         else
606                 sprintf(filename, "/tmp/lustre-log."CFS_TIME_T".%u",
607                         time(NULL),getpid());
608
609         if (stat(filename, &st) == 0 && S_ISREG(st.st_mode))
610                 unlink(filename);
611
612         fdin = dbg_open_ctlhandle(DUMP_KERNEL_CTL_NAME);
613         if (fdin < 0) {
614                 fprintf(stderr, "open(dump_kernel) failed: %s\n",
615                         strerror(errno));
616                 return 1;
617         }
618
619         rc = dbg_write_cmd(fdin, filename, strlen(filename));
620         save_errno = errno;
621         dbg_close_ctlhandle(fdin);
622         if (rc != 0) {
623                 fprintf(stderr, "write(%s) failed: %s\n", filename,
624                         strerror(save_errno));
625                 return 1;
626         }
627
628         if (raw)
629                 return 0;
630
631         fdin = open(filename, O_RDONLY);
632         if (fdin < 0) {
633                 if (errno == ENOENT) /* no dump file created */
634                         return 0;
635  
636                 fprintf(stderr, "fopen(%s) failed: %s\n", filename,
637                         strerror(errno));
638                 return 1;
639         }
640         if (argc > 1) {
641                 fdout = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC,
642                              S_IRUSR | S_IWUSR);
643                 if (fdout < 0) {
644                         fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
645                                 strerror(errno));
646                         close(fdin);
647                         return 1;
648                 }
649         } else {
650                 fdout = fileno(stdout);
651         }
652
653         rc = parse_buffer(fdin, fdout);
654         close(fdin);
655         if (argc > 1)
656                 close(fdout);
657         if (rc) {
658                 fprintf(stderr, "parse_buffer failed; leaving tmp file %s "
659                         "behind.\n", filename);
660         } else {
661                 rc = unlink(filename);
662                 if (rc)
663                         fprintf(stderr, "dumped successfully, but couldn't "
664                                 "unlink tmp file %s: %s\n", filename,
665                                 strerror(errno));
666         }
667
668         return rc;
669 }
670
671 int jt_dbg_debug_file(int argc, char **argv)
672 {
673         int    fdin;
674         int    fdout;
675         int    rc;
676
677         if (argc > 3 || argc < 2) {
678                 fprintf(stderr, "usage: %s <input> [output]\n", argv[0]);
679                 return 0;
680         }
681
682         fdin = open(argv[1], O_RDONLY | O_LARGEFILE);
683         if (fdin < 0) {
684                 fprintf(stderr, "open(%s) failed: %s\n", argv[1],
685                         strerror(errno));
686                 return 1;
687         }
688         if (argc > 2) {
689                 fdout = open(argv[2],
690                              O_CREAT | O_TRUNC | O_WRONLY | O_LARGEFILE,
691                              0600);
692                 if (fdout < 0) {
693                         fprintf(stderr, "open(%s) failed: %s\n", argv[2],
694                                 strerror(errno));
695                         close(fdin);
696                         return 1;
697                 }
698         } else {
699                 fdout = fileno(stdout);
700         }
701
702         rc = parse_buffer(fdin, fdout);
703
704         close(fdin);
705         if (fdout != fileno(stdout))
706                 close(fdout);
707
708         return rc;
709 }
710
711 const char debug_daemon_usage[] = "usage: %s {start file [MB]|stop}\n";
712
713 int jt_dbg_debug_daemon(int argc, char **argv)
714 {
715         int  rc;
716         int  fd;
717
718         if (argc <= 1) {
719                 fprintf(stderr, debug_daemon_usage, argv[0]);
720                 return 1;
721         }
722
723         fd = dbg_open_ctlhandle(DAEMON_CTL_NAME);
724         if (fd < 0)
725                 return -1;
726
727         rc = -1;
728         if (strcasecmp(argv[1], "start") == 0) {
729              if (argc < 3 || argc > 4 ||
730                     (argc == 4 && strlen(argv[3]) > 5)) {
731                         fprintf(stderr, debug_daemon_usage, argv[0]);
732                         goto out;
733                 }
734                 if (argc == 4) {
735                         char       buf[12];
736                         const long min_size = 10;
737                         const long max_size = 20480;
738                         long       size;
739                         char      *end;
740
741                         size = strtoul(argv[3], &end, 0);
742                         if (size < min_size ||
743                             size > max_size ||
744                             *end != 0) {
745                                 fprintf(stderr, "size %s invalid, must be in "
746                                         "the range %ld-%ld MB\n", argv[3],
747                                         min_size, max_size);
748                                 goto out;
749                         }
750                         snprintf(buf, sizeof(buf), "size=%ld", size);
751                         rc = dbg_write_cmd(fd, buf, strlen(buf));
752
753                         if (rc != 0) {
754                                 fprintf(stderr, "set %s failed: %s\n",
755                                         buf, strerror(errno));
756                                 goto out;
757                         }
758                 }
759
760                 rc = dbg_write_cmd(fd, argv[2], strlen(argv[2]));
761                 if (rc != 0) {
762                         fprintf(stderr, "start debug_daemon on %s failed: %s\n",
763                                 argv[2], strerror(errno));
764                         goto out;
765                 }
766                 rc = 0;
767                 goto out;
768         }
769         if (strcasecmp(argv[1], "stop") == 0) {
770                 rc = dbg_write_cmd(fd, "stop", 4);
771                 if (rc != 0) {
772                         fprintf(stderr, "stopping debug_daemon failed: %s\n",
773                                 strerror(errno));
774                         goto out;
775                 }
776
777                 rc = 0;
778                 goto out;
779         }
780
781         fprintf(stderr, debug_daemon_usage, argv[0]);
782         rc = -1;
783 out:
784         dbg_close_ctlhandle(fd);
785         return rc;
786 }
787
788 int jt_dbg_clear_debug_buf(int argc, char **argv)
789 {
790         int rc;
791         struct libcfs_ioctl_data data;
792
793         if (argc != 1) {
794                 fprintf(stderr, "usage: %s\n", argv[0]);
795                 return 0;
796         }
797
798         memset(&data, 0, sizeof(data));
799         if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
800                 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
801                 return -1;
802         }
803
804         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLEAR_DEBUG, buf);
805         if (rc) {
806                 fprintf(stderr, "IOC_LIBCFS_CLEAR_DEBUG failed: %s\n",
807                         strerror(errno));
808                 return -1;
809         }
810         return 0;
811 }
812
813 int jt_dbg_mark_debug_buf(int argc, char **argv)
814 {
815         static char scratch[MAX_MARK_SIZE] = { '\0' };
816         int rc, max_size = MAX_MARK_SIZE-1;
817         struct libcfs_ioctl_data data = { 0 };
818         char *text;
819         time_t now = time(NULL);
820
821         if (argc > 1) {
822                 int count;
823                 text = scratch;
824                 strncpy(text, argv[1], max_size);
825                 max_size-=strlen(argv[1]);
826                 for (count = 2; (count < argc) && (max_size > 0); count++){
827                         strncat(text, " ", max_size);
828                         max_size -= 1;
829                         strncat(text, argv[count], max_size);
830                         max_size -= strlen(argv[count]);
831                 }
832         } else {
833                 text = ctime(&now);
834         }
835
836         data.ioc_inllen1 = strlen(text) + 1;
837         data.ioc_inlbuf1 = text;
838         if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
839                 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
840                 return -1;
841         }
842
843         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_MARK_DEBUG, buf);
844         if (rc) {
845                 fprintf(stderr, "IOC_LIBCFS_MARK_DEBUG failed: %s\n",
846                         strerror(errno));
847                 return -1;
848         }
849         return 0;
850 }
851
852 static struct mod_paths {
853         char *name, *path;
854 } mod_paths[] = {
855         {"libcfs", "lnet/libcfs"},
856         {"lnet", "lnet/lnet"},
857         {"kciblnd", "lnet/klnds/ciblnd"},
858         {"kgmlnd", "lnet/klnds/gmlnd"},
859         {"kmxlnd", "lnet/klnds/mxlnd"},
860         {"kiiblnd", "lnet/klnds/iiblnd"},
861         {"ko2iblnd", "lnet/klnds/o2iblnd"},
862         {"kopeniblnd", "lnet/klnds/openiblnd"},
863         {"kptllnd", "lnet/klnds/ptllnd"},
864         {"kqswlnd", "lnet/klnds/qswlnd"},
865         {"kralnd", "lnet/klnds/ralnd"},
866         {"ksocklnd", "lnet/klnds/socklnd"},
867         {"ktdilnd", "lnet/klnds/tdilnd"},
868         {"kviblnd", "lnet/klnds/viblnd"},
869         {"lvfs", "lustre/lvfs"},
870         {"obdclass", "lustre/obdclass"},
871         {"llog_test", "lustre/obdclass"},
872         {"ptlrpc_gss", "lustre/ptlrpc/gss"},
873         {"ptlrpc", "lustre/ptlrpc"},
874         {"gks", "lustre/sec/gks"},
875         {"gkc", "lustre/sec/gks"},
876         {"ost", "lustre/ost"},
877         {"osc", "lustre/osc"},
878         {"mds", "lustre/mds"},
879         {"mdc", "lustre/mdc"},
880         {"llite", "lustre/llite"},
881         {"lustre", "lustre/llite"},
882         {"llite_lloop", "lustre/llite"},
883         {"ldiskfs", "ldiskfs/ldiskfs"},
884         {"smfs", "lustre/smfs"},
885         {"obdecho", "lustre/obdecho"},
886         {"ldlm", "lustre/ldlm"},
887         {"obdfilter", "lustre/obdfilter"},
888         {"lov", "lustre/lov"},
889         {"lmv", "lustre/lmv"},
890         {"fsfilt_ext3", "lustre/lvfs"},
891         {"fsfilt_reiserfs", "lustre/lvfs"},
892         {"fsfilt_smfs", "lustre/lvfs"},
893         {"fsfilt_ldiskfs", "lustre/lvfs"},
894         {"mds_ext3", "lustre/mds"},
895         {"cobd", "lustre/cobd"},
896         {"cmobd", "lustre/cmobd"},
897         {"lquota", "lustre/quota"},
898         {"mgs", "lustre/mgs"},
899         {"mgc", "lustre/mgc"},
900         {"mdt", "lustre/mdt"},
901         {"mdd", "lustre/mdd"},
902         {"osd", "lustre/osd"},
903         {"cmm", "lustre/cmm"},
904         {"fid", "lustre/fid"},
905         {"fld", "lustre/fld"},
906         {NULL, NULL}
907 };
908
909 static int jt_dbg_modules_2_4(int argc, char **argv)
910 {
911 #ifdef HAVE_LINUX_VERSION_H
912 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
913         struct mod_paths *mp;
914         char *path = "";
915         char *kernel = "linux";
916
917         if (argc >= 2)
918                 path = argv[1];
919         if (argc == 3)
920                 kernel = argv[2];
921         if (argc > 3) {
922                 printf("%s [path] [kernel]\n", argv[0]);
923                 return 0;
924         }
925
926         for (mp = mod_paths; mp->name != NULL; mp++) {
927                 struct module_info info;
928                 int rc;
929                 size_t crap;
930                 int query_module(const char *name, int which, void *buf,
931                                  size_t bufsize, size_t *ret);
932
933                 rc = query_module(mp->name, QM_INFO, &info, sizeof(info),
934                                   &crap);
935                 if (rc < 0) {
936                         if (errno != ENOENT)
937                                 printf("query_module(%s) failed: %s\n",
938                                        mp->name, strerror(errno));
939                 } else {
940                         printf("add-symbol-file %s%s%s/%s.o 0x%0lx\n", path,
941                                path[0] ? "/" : "", mp->path, mp->name,
942                                info.addr + sizeof(struct module));
943                 }
944         }
945
946         return 0;
947 #endif // Headers are 2.6-only
948 #endif // !HAVE_LINUX_VERSION_H
949         return -EINVAL;
950 }
951
952 static int jt_dbg_modules_2_5(int argc, char **argv)
953 {
954         struct mod_paths *mp;
955         char *path = "";
956         char *kernel = "linux";
957         const char *proc = "/proc/modules";
958         char modname[128], buf[4096];
959         long modaddr;
960         FILE *file;
961
962         if (argc >= 2)
963                 path = argv[1];
964         if (argc == 3)
965                 kernel = argv[2];
966         if (argc > 3) {
967                 printf("%s [path] [kernel]\n", argv[0]);
968                 return 0;
969         }
970
971         file = fopen(proc, "r");
972         if (!file) {
973                 printf("failed open %s: %s\n", proc, strerror(errno));
974                 return 0;
975         }
976
977         while (fgets(buf, sizeof(buf), file) != NULL) {
978                 if (sscanf(buf, "%s %*s %*s %*s %*s %lx", modname, &modaddr) == 2) {
979                         for (mp = mod_paths; mp->name != NULL; mp++) {
980                                 if (!strcmp(mp->name, modname))
981                                         break;
982                         }
983                         if (mp->name) {
984                                 printf("add-symbol-file %s%s%s/%s.o 0x%0lx\n", 
985                                        path, path[0] ? "/" : "", 
986                                        mp->path, mp->name, modaddr);
987                         }
988                 }
989         }
990
991         fclose(file);
992         return 0;
993 }
994
995 int jt_dbg_modules(int argc, char **argv)
996 {
997         int rc = 0;
998         struct utsname sysinfo;
999
1000         rc = uname(&sysinfo);
1001         if (rc) {
1002                 printf("uname() failed: %s\n", strerror(errno));
1003                 return 0;
1004         }
1005
1006         if (sysinfo.release[2] > '4') {
1007                 return jt_dbg_modules_2_5(argc, argv);
1008         } else {
1009                 return jt_dbg_modules_2_4(argc, argv);
1010         }
1011
1012         return 0;
1013 }
1014
1015 int jt_dbg_panic(int argc, char **argv)
1016 {
1017         int rc;
1018         struct libcfs_ioctl_data data;
1019
1020         if (argc != 1) {
1021                 fprintf(stderr, "usage: %s\n", argv[0]);
1022                 return 0;
1023         }
1024
1025         memset(&data, 0, sizeof(data));
1026         if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
1027                 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
1028                 return -1;
1029         }
1030
1031         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PANIC, buf);
1032         if (rc) {
1033                 fprintf(stderr, "IOC_LIBCFS_PANIC failed: %s\n",
1034                         strerror(errno));
1035                 return -1;
1036         }
1037         return 0;
1038 }