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