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