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