Whamcloud - gitweb
39af760e137bbde49bc3ffbceb4cf49fdc3b9eed
[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_mask &&
410                     (!(subsystem_mask & hdr->ph_subsys) ||
411                      (!(debug_mask & hdr->ph_mask)))) {
412                         dropped++;
413                         continue;
414                 }
415
416                 line = malloc(sizeof(*line));
417                 if (line == NULL) {
418                         fprintf(stderr, "malloc failed; printing accumulated "
419                                 "records and exiting.\n");
420                         break;
421                 }
422
423                 line->hdr = malloc(hdr->ph_len + 1);
424                 if (line->hdr == NULL) {
425                         free(line);
426                         fprintf(stderr, "malloc failed; printing accumulated "
427                                 "records and exiting.\n");
428                         break;
429                 }
430
431                 p = (void *)line->hdr;
432                 memcpy(line->hdr, buf, hdr->ph_len);
433                 p[hdr->ph_len] = '\0';
434
435                 p += sizeof(*hdr);
436                 line->file = p;
437                 p += strlen(line->file) + 1;
438                 line->fn = p;
439                 p += strlen(line->fn) + 1;
440                 line->text = p;
441
442                 if (!add_rec(line, &linev, &linev_len, kept)) {
443                         fprintf(stderr, "malloc failed; printing accumulated " 
444                                 "records and exiting.\n");
445                         break;
446                 }        
447                 kept++;
448         }
449
450         if (linev) {
451                 qsort(linev, kept, sizeof(struct dbg_line *), cmp_rec);
452                 print_rec(linev, kept, out);
453         }
454
455         printf("Debug log: %lu lines, %lu kept, %lu dropped.\n",
456                 dropped + kept, kept, dropped);
457         return 0;
458 }
459
460 int jt_dbg_debug_kernel(int argc, char **argv)
461 {
462         char filename[4096];
463         struct stat st;
464         int rc, raw = 0, fd;
465         FILE *in, *out = stdout;
466
467         if (argc > 3) {
468                 fprintf(stderr, "usage: %s [file] [raw]\n", argv[0]);
469                 return 0;
470         }
471
472         if (argc > 2) {
473                 raw = atoi(argv[2]);
474         } else if (argc > 1 && (argv[1][0] == '0' || argv[1][0] == '1')) {
475                 raw = atoi(argv[1]);
476                 argc--;
477         }
478
479         /* If we are dumping raw (which means no conversion step to ASCII)
480          * then dump directly to any supplied filename, otherwise this is
481          * just a temp file and we dump to the real file at convert time. */
482         if (argc > 1 && raw)
483                 strcpy(filename, argv[1]);
484         else
485                 sprintf(filename, "/tmp/lustre-log."CFS_TIME_T".%u",
486                         time(NULL),getpid());
487
488         if (stat(filename, &st) == 0 && S_ISREG(st.st_mode))
489                 unlink(filename);
490
491         fd = dbg_open_ctlhandle(DUMP_KERNEL_CTL_NAME);
492         if (fd < 0) {
493                 fprintf(stderr, "open(dump_kernel) failed: %s\n",
494                         strerror(errno));
495                 return 1;
496         }
497
498         rc = dbg_write_cmd(fd, filename, strlen(filename));
499         if (rc != 0) {
500                 fprintf(stderr, "write(%s) failed: %s\n", filename,
501                         strerror(errno));
502                 close(fd);
503                 return 1;
504         }
505         dbg_close_ctlhandle(fd);
506
507         if (raw)
508                 return 0;
509
510         in = fopen(filename, "r");
511         if (in == NULL) {
512                 if (errno == ENOENT) /* no dump file created */
513                         return 0;
514
515                 fprintf(stderr, "fopen(%s) failed: %s\n", filename,
516                         strerror(errno));
517                 return 1;
518         }
519         if (argc > 1) {
520                 out = fopen(argv[1], "w");
521                 if (out == NULL) {
522                         fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
523                                 strerror(errno));
524                         fclose(in);
525                         return 1;
526                 }
527         }
528
529         rc = parse_buffer(in, out);
530         fclose(in);
531         if (argc > 1)
532                 fclose(out);
533         if (rc) {
534                 fprintf(stderr, "parse_buffer failed; leaving tmp file %s "
535                         "behind.\n", filename);
536         } else {
537                 rc = unlink(filename);
538                 if (rc)
539                         fprintf(stderr, "dumped successfully, but couldn't "
540                                 "unlink tmp file %s: %s\n", filename,
541                                 strerror(errno));
542         }
543         return rc;
544 }
545
546 int jt_dbg_debug_file(int argc, char **argv)
547 {
548         int    fdin;
549         int    fdout;
550         FILE  *in;
551         FILE  *out = stdout;
552         int    rc;
553
554         if (argc > 3 || argc < 2) {
555                 fprintf(stderr, "usage: %s <input> [output]\n", argv[0]);
556                 return 0;
557         }
558
559         fdin = open(argv[1], O_RDONLY | O_LARGEFILE);
560         if (fdin == -1) {
561                 fprintf(stderr, "open(%s) failed: %s\n", argv[1],
562                         strerror(errno));
563                 return 1;
564         }
565         in = fdopen(fdin, "r");
566         if (in == NULL) {
567                 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
568                         strerror(errno));
569                 close(fdin);
570                 return 1;
571         }
572         if (argc > 2) {
573                 fdout = open(argv[2],
574                              O_CREAT | O_TRUNC | O_WRONLY | O_LARGEFILE,
575                              0600);
576                 if (fdout == -1) {
577                         fprintf(stderr, "open(%s) failed: %s\n", argv[2],
578                                 strerror(errno));
579                         fclose(in);
580                         return 1;
581                 }
582                 out = fdopen(fdout, "w");
583                 if (out == NULL) {
584                         fprintf(stderr, "fopen(%s) failed: %s\n", argv[2],
585                                 strerror(errno));
586                         fclose(in);
587                         close(fdout);
588                         return 1;
589                 }
590         }
591
592         rc = parse_buffer(in, out);
593
594         fclose(in);
595         if (out != stdout)
596                 fclose(out);
597
598         return rc;
599 }
600
601 const char debug_daemon_usage[] = "usage: %s {start file [MB]|stop}\n";
602
603 int jt_dbg_debug_daemon(int argc, char **argv)
604 {
605         int  rc;
606         int  fd;
607
608         if (argc <= 1) {
609                 fprintf(stderr, debug_daemon_usage, argv[0]);
610                 return 1;
611         }
612
613         fd = dbg_open_ctlhandle(DAEMON_CTL_NAME);
614         if (fd < 0)
615                 return -1;
616
617         rc = -1;
618         if (strcasecmp(argv[1], "start") == 0) {
619              if (argc < 3 || argc > 4 ||
620                     (argc == 4 && strlen(argv[3]) > 5)) {
621                         fprintf(stderr, debug_daemon_usage, argv[0]);
622                         goto out;
623                 }
624                 if (argc == 4) {
625                         char       buf[12];
626                         const long min_size = 10;
627                         const long max_size = 20480;
628                         long       size;
629                         char      *end;
630
631                         size = strtoul(argv[3], &end, 0);
632                         if (size < min_size ||
633                             size > max_size ||
634                             *end != 0) {
635                                 fprintf(stderr, "size %s invalid, must be in "
636                                         "the range %ld-%ld MB\n", argv[3],
637                                         min_size, max_size);
638                                 goto out;
639                         }
640                         snprintf(buf, sizeof(buf), "size=%ld", size);
641                         rc = dbg_write_cmd(fd, buf, strlen(buf));
642
643                         if (rc != 0) {
644                                 fprintf(stderr, "set %s failed: %s\n",
645                                         buf, strerror(errno));
646                                 goto out;
647                         }
648                 }
649
650                 rc = dbg_write_cmd(fd, argv[2], strlen(argv[2]));
651                 if (rc != 0) {
652                         fprintf(stderr, "start debug_daemon on %s failed: %s\n",
653                                 argv[2], strerror(errno));
654                         goto out;
655                 }
656                 rc = 0;
657                 goto out;
658         }
659         if (strcasecmp(argv[1], "stop") == 0) {
660                 rc = dbg_write_cmd(fd, "stop", 4);
661                 if (rc != 0) {
662                         fprintf(stderr, "stopping debug_daemon failed: %s\n",
663                                 strerror(errno));
664                         goto out;
665                 }
666
667                 rc = 0;
668                 goto out;
669         }
670
671         fprintf(stderr, debug_daemon_usage, argv[0]);
672         rc = -1;
673 out:
674         dbg_close_ctlhandle(fd);
675         return rc;
676 }
677
678 int jt_dbg_clear_debug_buf(int argc, char **argv)
679 {
680         int rc;
681         struct libcfs_ioctl_data data;
682
683         if (argc != 1) {
684                 fprintf(stderr, "usage: %s\n", argv[0]);
685                 return 0;
686         }
687
688         memset(&data, 0, sizeof(data));
689         if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
690                 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
691                 return -1;
692         }
693
694         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLEAR_DEBUG, buf);
695         if (rc) {
696                 fprintf(stderr, "IOC_LIBCFS_CLEAR_DEBUG failed: %s\n",
697                         strerror(errno));
698                 return -1;
699         }
700         return 0;
701 }
702
703 int jt_dbg_mark_debug_buf(int argc, char **argv)
704 {
705         static char scratch[MAX_MARK_SIZE] = { '\0' };
706         int rc, max_size = MAX_MARK_SIZE-1;
707         struct libcfs_ioctl_data data = { 0 };
708         char *text;
709         time_t now = time(NULL);
710
711         if (argc > 1) {
712                 int count;
713                 text = scratch;
714                 strncpy(text, argv[1], max_size);
715                 max_size-=strlen(argv[1]);
716                 for (count = 2; (count < argc) && (max_size > 0); count++){
717                         strncat(text, " ", max_size);
718                         max_size -= 1;
719                         strncat(text, argv[count], max_size);
720                         max_size -= strlen(argv[count]);
721                 }
722         } else {
723                 text = ctime(&now);
724         }
725
726         data.ioc_inllen1 = strlen(text) + 1;
727         data.ioc_inlbuf1 = text;
728         if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
729                 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
730                 return -1;
731         }
732
733         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_MARK_DEBUG, buf);
734         if (rc) {
735                 fprintf(stderr, "IOC_LIBCFS_MARK_DEBUG failed: %s\n",
736                         strerror(errno));
737                 return -1;
738         }
739         return 0;
740 }
741
742 static struct mod_paths {
743         char *name, *path;
744 } mod_paths[] = {
745         {"libcfs", "lnet/libcfs"},
746         {"lnet", "lnet/lnet"},
747         {"kciblnd", "lnet/klnds/ciblnd"},
748         {"kgmlnd", "lnet/klnds/gmlnd"},
749         {"kmxlnd", "lnet/klnds/mxlnd"},
750         {"kiiblnd", "lnet/klnds/iiblnd"},
751         {"ko2iblnd", "lnet/klnds/o2iblnd"},
752         {"kopeniblnd", "lnet/klnds/openiblnd"},
753         {"kptllnd", "lnet/klnds/ptllnd"},
754         {"kqswlnd", "lnet/klnds/qswlnd"},
755         {"kralnd", "lnet/klnds/ralnd"},
756         {"ksocklnd", "lnet/klnds/socklnd"},
757         {"ktdilnd", "lnet/klnds/tdilnd"},
758         {"kviblnd", "lnet/klnds/viblnd"},
759         {"lvfs", "lustre/lvfs"},
760         {"obdclass", "lustre/obdclass"},
761         {"llog_test", "lustre/obdclass"},
762         {"ptlrpc_gss", "lustre/ptlrpc/gss"},
763         {"ptlrpc", "lustre/ptlrpc"},
764         {"gks", "lustre/sec/gks"},
765         {"gkc", "lustre/sec/gks"},
766         {"ost", "lustre/ost"},
767         {"osc", "lustre/osc"},
768         {"mds", "lustre/mds"},
769         {"mdc", "lustre/mdc"},
770         {"llite", "lustre/llite"},
771         {"lustre", "lustre/llite"},
772         {"llite_lloop", "lustre/llite"},
773         {"ldiskfs", "ldiskfs/ldiskfs"},
774         {"smfs", "lustre/smfs"},
775         {"obdecho", "lustre/obdecho"},
776         {"ldlm", "lustre/ldlm"},
777         {"obdfilter", "lustre/obdfilter"},
778         {"lov", "lustre/lov"},
779         {"lmv", "lustre/lmv"},
780         {"fsfilt_ext3", "lustre/lvfs"},
781         {"fsfilt_reiserfs", "lustre/lvfs"},
782         {"fsfilt_smfs", "lustre/lvfs"},
783         {"fsfilt_ldiskfs", "lustre/lvfs"},
784         {"mds_ext3", "lustre/mds"},
785         {"cobd", "lustre/cobd"},
786         {"cmobd", "lustre/cmobd"},
787         {"lquota", "lustre/quota"},
788         {"mgs", "lustre/mgs"},
789         {"mgc", "lustre/mgc"},
790         {"mdt", "lustre/mdt"},
791         {"mdd", "lustre/mdd"},
792         {"osd", "lustre/osd"},
793         {"cmm", "lustre/cmm"},
794         {"fid", "lustre/fid"},
795         {"fld", "lustre/fld"},
796         {NULL, NULL}
797 };
798
799 static int jt_dbg_modules_2_4(int argc, char **argv)
800 {
801 #ifdef HAVE_LINUX_VERSION_H
802 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
803         struct mod_paths *mp;
804         char *path = "";
805         char *kernel = "linux";
806
807         if (argc >= 2)
808                 path = argv[1];
809         if (argc == 3)
810                 kernel = argv[2];
811         if (argc > 3) {
812                 printf("%s [path] [kernel]\n", argv[0]);
813                 return 0;
814         }
815
816         for (mp = mod_paths; mp->name != NULL; mp++) {
817                 struct module_info info;
818                 int rc;
819                 size_t crap;
820                 int query_module(const char *name, int which, void *buf,
821                                  size_t bufsize, size_t *ret);
822
823                 rc = query_module(mp->name, QM_INFO, &info, sizeof(info),
824                                   &crap);
825                 if (rc < 0) {
826                         if (errno != ENOENT)
827                                 printf("query_module(%s) failed: %s\n",
828                                        mp->name, strerror(errno));
829                 } else {
830                         printf("add-symbol-file %s%s%s/%s.o 0x%0lx\n", path,
831                                path[0] ? "/" : "", mp->path, mp->name,
832                                info.addr + sizeof(struct module));
833                 }
834         }
835
836         return 0;
837 #endif // Headers are 2.6-only
838 #endif // !HAVE_LINUX_VERSION_H
839         return -EINVAL;
840 }
841
842 static int jt_dbg_modules_2_5(int argc, char **argv)
843 {
844         struct mod_paths *mp;
845         char *path = "";
846         char *kernel = "linux";
847         const char *proc = "/proc/modules";
848         char modname[128], others[4096];
849         long modaddr;
850         int rc;
851         FILE *file;
852
853         if (argc >= 2)
854                 path = argv[1];
855         if (argc == 3)
856                 kernel = argv[2];
857         if (argc > 3) {
858                 printf("%s [path] [kernel]\n", argv[0]);
859                 return 0;
860         }
861
862         file = fopen(proc, "r");
863         if (!file) {
864                 printf("failed open %s: %s\n", proc, strerror(errno));
865                 return 0;
866         }
867
868         while ((rc = fscanf(file, "%s %s %s %s %s %lx\n",
869                 modname, others, others, others, others, &modaddr)) == 6) {
870                 for (mp = mod_paths; mp->name != NULL; mp++) {
871                         if (!strcmp(mp->name, modname))
872                                 break;
873                 }
874                 if (mp->name) {
875                         printf("add-symbol-file %s%s%s/%s.o 0x%0lx\n", path,
876                                path[0] ? "/" : "", mp->path, mp->name, modaddr);
877                 }
878         }
879
880         fclose(file);
881         return 0;
882 }
883
884 int jt_dbg_modules(int argc, char **argv)
885 {
886         int rc = 0;
887         struct utsname sysinfo;
888
889         rc = uname(&sysinfo);
890         if (rc) {
891                 printf("uname() failed: %s\n", strerror(errno));
892                 return 0;
893         }
894
895         if (sysinfo.release[2] > '4') {
896                 return jt_dbg_modules_2_5(argc, argv);
897         } else {
898                 return jt_dbg_modules_2_4(argc, argv);
899         }
900
901         return 0;
902 }
903
904 int jt_dbg_panic(int argc, char **argv)
905 {
906         int rc;
907         struct libcfs_ioctl_data data;
908
909         if (argc != 1) {
910                 fprintf(stderr, "usage: %s\n", argv[0]);
911                 return 0;
912         }
913
914         memset(&data, 0, sizeof(data));
915         if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
916                 fprintf(stderr, "libcfs_ioctl_pack failed.\n");
917                 return -1;
918         }
919
920         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PANIC, buf);
921         if (rc) {
922                 fprintf(stderr, "IOC_LIBCFS_PANIC failed: %s\n",
923                         strerror(errno));
924                 return -1;
925         }
926         return 0;
927 }