Whamcloud - gitweb
9a11432e14c42d5ec3036973dcc4a8f9cb5c90e7
[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", "mgmt", "lnet", "lnd",
73          "pinger", "filter", "", "echo",
74          "ldlm", "lov", "", "",
75          "", "", "", "lmv",
76          "", "sec", "gss", "", 
77          "mgc", "mgs", "fid", "fld", NULL};
78 static const char *libcfs_debug_masks[] =
79         {"trace", "inode", "super", "ext2",
80          "malloc", "cache", "info", "ioctl",
81          "neterror", "net", "warning", "buffs",
82          "other", "dentry", "nettrace", "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 };
325
326 static int cmp_rec(const void *p1, const void *p2)
327 {
328         struct dbg_line *d1 = *(struct dbg_line **)p1;
329         struct dbg_line *d2 = *(struct dbg_line **)p2;
330
331         if (d1->hdr->ph_sec < d2->hdr->ph_sec)
332                 return -1;
333         if (d1->hdr->ph_sec == d2->hdr->ph_sec &&
334             d1->hdr->ph_usec < d2->hdr->ph_usec)
335                 return -1;
336         if (d1->hdr->ph_sec == d2->hdr->ph_sec &&
337             d1->hdr->ph_usec == d2->hdr->ph_usec)
338                 return 0;
339         return 1;
340 }
341
342 static void print_rec(struct dbg_line **linev, int used, FILE *out)
343 {
344         int i;
345
346         for (i = 0; i < used; i++) {
347                 struct dbg_line *line = linev[i];
348                 struct ptldebug_header *hdr = line->hdr;
349
350                 fprintf(out, "%08x:%08x:%u:%u.%06llu:%u:%u:%u:(%s:%u:%s()) %s",
351                         hdr->ph_subsys, hdr->ph_mask, hdr->ph_cpu_id,
352                         hdr->ph_sec, (unsigned long long)hdr->ph_usec,
353                         hdr->ph_stack, hdr->ph_pid, hdr->ph_extern_pid,
354                         line->file, hdr->ph_line_num, line->fn, line->text);
355                 free(line->hdr);
356                 free(line);
357         }
358         free(linev);
359 }
360
361 static int add_rec(struct dbg_line *line, struct dbg_line ***linevp, int *lenp,
362                    int used)
363 {
364         struct dbg_line **linev = *linevp;
365
366         if (used == *lenp) {
367                 int nlen = *lenp + 512;
368                 int nsize = nlen * sizeof(struct dbg_line *);
369
370                 linev = *linevp ? realloc(*linevp, nsize) : malloc(nsize);
371                 if (!linev)
372                         return 0;
373                 *linevp = linev;
374                 *lenp = nlen;
375         }
376         linev[used] = line; 
377         return 1;
378 }
379
380 static int parse_buffer(FILE *in, FILE *out)
381 {
382         struct dbg_line *line;
383         struct ptldebug_header *hdr;
384         char buf[4097], *p;
385         int rc;
386         unsigned long dropped = 0, kept = 0;
387         struct dbg_line **linev = NULL;
388         int linev_len = 0;
389
390         while (1) {
391                 rc = fread(buf, sizeof(hdr->ph_len) + sizeof(hdr->ph_flags), 1, in);
392                 if (rc <= 0)
393                         break;
394
395                 hdr = (void *)buf;
396                 if (hdr->ph_len == 0)
397                         break;
398                 if (hdr->ph_len > 4094) {
399                         fprintf(stderr, "unexpected large record: %d bytes.  "
400                                 "aborting.\n",
401                                 hdr->ph_len);
402                         break;
403                 }
404
405                 rc = fread(buf + sizeof(hdr->ph_len) + sizeof(hdr->ph_flags), 1,
406                            hdr->ph_len - sizeof(hdr->ph_len) - sizeof(hdr->ph_flags), in);
407                 if (rc <= 0)
408                         break;
409
410                 if (hdr->ph_mask &&
411                     (!(subsystem_mask & hdr->ph_subsys) ||
412                      (!(debug_mask & hdr->ph_mask)))) {
413                         dropped++;
414                         continue;
415                 }
416
417                 line = malloc(sizeof(*line));
418                 if (line == NULL) {
419                         fprintf(stderr, "malloc failed; printing accumulated "
420                                 "records and exiting.\n");
421                         break;
422                 }
423
424                 line->hdr = malloc(hdr->ph_len + 1);
425                 if (line->hdr == NULL) {
426                         free(line);
427                         fprintf(stderr, "malloc failed; printing accumulated "
428                                 "records and exiting.\n");
429                         break;
430                 }
431
432                 p = (void *)line->hdr;
433                 memcpy(line->hdr, buf, hdr->ph_len);
434                 p[hdr->ph_len] = '\0';
435
436                 p += sizeof(*hdr);
437                 line->file = p;
438                 p += strlen(line->file) + 1;
439                 line->fn = p;
440                 p += strlen(line->fn) + 1;
441                 line->text = p;
442
443                 if (!add_rec(line, &linev, &linev_len, kept)) {
444                         fprintf(stderr, "malloc failed; printing accumulated " 
445                                 "records and exiting.\n");
446                         break;
447                 }        
448                 kept++;
449         }
450
451         if (linev) {
452                 qsort(linev, kept, sizeof(struct dbg_line *), cmp_rec);
453                 print_rec(linev, kept, out);
454         }
455
456         printf("Debug log: %lu lines, %lu kept, %lu dropped.\n",
457                 dropped + kept, kept, dropped);
458         return 0;
459 }
460
461 int jt_dbg_debug_kernel(int argc, char **argv)
462 {
463         char filename[4096];
464         struct stat st;
465         int rc, raw = 0, fd;
466         FILE *in, *out = stdout;
467
468         if (argc > 3) {
469                 fprintf(stderr, "usage: %s [file] [raw]\n", argv[0]);
470                 return 0;
471         }
472
473         if (argc > 2) {
474                 raw = atoi(argv[2]);
475         } else if (argc > 1 && (argv[1][0] == '0' || argv[1][0] == '1')) {
476                 raw = atoi(argv[1]);
477                 argc--;
478         }
479
480         /* If we are dumping raw (which means no conversion step to ASCII)
481          * then dump directly to any supplied filename, otherwise this is
482          * just a temp file and we dump to the real file at convert time. */
483         if (argc > 1 && raw)
484                 strcpy(filename, argv[1]);
485         else
486                 sprintf(filename, "/tmp/lustre-log.%lu.%u",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         {"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 }