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