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