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