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