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