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