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