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