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