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