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