Whamcloud - gitweb
- Fix some b_1414 build and formatting issues
[fs/lustre-release.git] / lnet / utils / debug.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of Portals, http://www.sf.net/projects/lustre/
7  *
8  *   Portals is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Portals is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Portals; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  * Some day I'll split all of this functionality into a cfs_debug module
22  * of its own.  That day is not today.
23  *
24  */
25
26 #define __USE_FILE_OFFSET64
27
28 #include <stdio.h>
29 #include <netdb.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #include <errno.h>
34 #include <unistd.h>
35 #include <time.h>
36 #ifndef __CYGWIN__
37 # include <syscall.h>
38 #endif
39
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <sys/ioctl.h>
43 #include <sys/stat.h>
44 #include <sys/mman.h>
45
46 #include <linux/version.h>
47
48 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
49 #define BUG()                            /* workaround for module.h includes */
50 #include <linux/module.h>
51 #endif
52
53 #include <portals/api-support.h>
54 #include <portals/ptlctl.h>
55 #include "parser.h"
56
57 static char rawbuf[8192];
58 static char *buf = rawbuf;
59 static int max = 8192;
60 //static int g_pfd = -1;
61 static int subsystem_mask = ~0;
62 static int debug_mask = ~0;
63
64 #define MAX_MARK_SIZE 100
65
66 static const char *portal_debug_subsystems[] =
67         {"undefined", "mdc", "mds", "osc", "ost", "class", "log", "llite",
68          "rpc", "mgmt", "portals", "libcfs", "socknal", "qswnal", "pinger",
69          "filter", "ptlbd", "echo", "ldlm", "lov", "gmnal", "router", "cobd",
70          "ibnal", NULL};
71 static const char *portal_debug_masks[] =
72         {"trace", "inode", "super", "ext2", "malloc", "cache", "info", "ioctl",
73          "blocks", "net", "warning", "buffs", "other", "dentry", "portals",
74          "page", "dlmtrace", "error", "emerg", "ha", "rpctrace", "vfstrace",
75          "reada", NULL};
76
77 struct debug_daemon_cmd {
78         char *cmd;
79         unsigned int cmdv;
80 };
81
82 static const struct debug_daemon_cmd portal_debug_daemon_cmd[] = {
83         {"start", DEBUG_DAEMON_START},
84         {"stop", DEBUG_DAEMON_STOP},
85         {"pause", DEBUG_DAEMON_PAUSE},
86         {"continue", DEBUG_DAEMON_CONTINUE},
87         {0, 0}
88 };
89
90 static int do_debug_mask(char *name, int enable)
91 {
92         int found = 0, i;
93
94         for (i = 0; portal_debug_subsystems[i] != NULL; i++) {
95                 if (strcasecmp(name, portal_debug_subsystems[i]) == 0 ||
96                     strcasecmp(name, "all_subs") == 0) {
97                         printf("%s output from subsystem \"%s\"\n",
98                                 enable ? "Enabling" : "Disabling",
99                                 portal_debug_subsystems[i]);
100                         if (enable)
101                                 subsystem_mask |= (1 << i);
102                         else
103                                 subsystem_mask &= ~(1 << i);
104                         found = 1;
105                 }
106         }
107         for (i = 0; portal_debug_masks[i] != NULL; i++) {
108                 if (strcasecmp(name, portal_debug_masks[i]) == 0 ||
109                     strcasecmp(name, "all_types") == 0) {
110                         printf("%s output of type \"%s\"\n",
111                                 enable ? "Enabling" : "Disabling",
112                                 portal_debug_masks[i]);
113                         if (enable)
114                                 debug_mask |= (1 << i);
115                         else
116                                 debug_mask &= ~(1 << i);
117                         found = 1;
118                 }
119         }
120
121         return found;
122 }
123
124 int dbg_initialize(int argc, char **argv)
125 {
126         return 0;
127 }
128
129 int jt_dbg_filter(int argc, char **argv)
130 {
131         int   i;
132
133         if (argc < 2) {
134                 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
135                         argv[0]);
136                 return 0;
137         }
138
139         for (i = 1; i < argc; i++)
140                 if (!do_debug_mask(argv[i], 0))
141                         fprintf(stderr, "Unknown subsystem or debug type: %s\n",
142                                 argv[i]);
143         return 0;
144 }
145
146 int jt_dbg_show(int argc, char **argv)
147 {
148         int    i;
149
150         if (argc < 2) {
151                 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
152                         argv[0]);
153                 return 0;
154         }
155
156         for (i = 1; i < argc; i++)
157                 if (!do_debug_mask(argv[i], 1))
158                         fprintf(stderr, "Unknown subsystem or debug type: %s\n",
159                                 argv[i]);
160
161         return 0;
162 }
163
164 static int applymask(char* procpath, int value)
165 {
166         int rc;
167         char buf[64];
168         int len = snprintf(buf, 64, "%d", value);
169
170         int fd = open(procpath, O_WRONLY);
171         if (fd == -1) {
172                 fprintf(stderr, "Unable to open %s: %s\n",
173                         procpath, strerror(errno));
174                 return fd;
175         }
176         rc = write(fd, buf, len+1);
177         if (rc<0) {
178                 fprintf(stderr, "Write to %s failed: %s\n",
179                         procpath, strerror(errno));
180                 return rc;
181         }
182         close(fd);
183         return 0;
184 }
185
186 extern char *dump_filename;
187 extern int dump(int dev_id, int opc, void *buf);
188
189 static void applymask_all(unsigned int subs_mask, unsigned int debug_mask)
190 {
191         if (!dump_filename) {
192                 applymask("/proc/sys/portals/subsystem_debug", subs_mask);
193                 applymask("/proc/sys/portals/debug", debug_mask);
194         } else {
195                 struct portals_debug_ioctl_data data;
196
197                 data.hdr.ioc_len = sizeof(data);
198                 data.hdr.ioc_version = 0;
199                 data.subs = subs_mask;
200                 data.debug = debug_mask;
201
202                 dump(OBD_DEV_ID, PTL_IOC_DEBUG_MASK, &data);
203         }
204         printf("Applied subsystem_debug=%d, debug=%d to /proc/sys/portals\n",
205                subs_mask, debug_mask);
206 }
207
208 int jt_dbg_list(int argc, char **argv)
209 {
210         int i;
211
212         if (argc != 2) {
213                 fprintf(stderr, "usage: %s <subs || types>\n", argv[0]);
214                 return 0;
215         }
216
217         if (strcasecmp(argv[1], "subs") == 0) {
218                 printf("Subsystems: all_subs");
219                 for (i = 0; portal_debug_subsystems[i] != NULL; i++)
220                         printf(", %s", portal_debug_subsystems[i]);
221                 printf("\n");
222         } else if (strcasecmp(argv[1], "types") == 0) {
223                 printf("Types: all_types");
224                 for (i = 0; portal_debug_masks[i] != NULL; i++)
225                         printf(", %s", portal_debug_masks[i]);
226                 printf("\n");
227         } else if (strcasecmp(argv[1], "applymasks") == 0) {
228                 applymask_all(subsystem_mask, debug_mask);
229         }
230         return 0;
231 }
232
233 /* if 'raw' is true, don't strip the debug information from the front of the
234  * lines */
235 static void dump_buffer(FILE *fd, char *buf, int size, int raw)
236 {
237         char *p, *z;
238         unsigned long subsystem, debug, dropped = 0, kept = 0;
239
240         while (size) {
241                 p = memchr(buf, '\n', size);
242                 if (!p)
243                         break;
244                 subsystem = strtoul(buf, &z, 16);
245                 debug = strtoul(z + 1, &z, 16);
246
247                 z++;
248                 /* for some reason %*s isn't working. */
249                 *p = '\0';
250                 if ((subsystem_mask & subsystem) &&
251                     (!debug || (debug_mask & debug))) {
252                         if (raw)
253                                 fprintf(fd, "%s\n", buf);
254                         else
255                                 fprintf(fd, "%s\n", z);
256                         //printf("%s\n", buf);
257                         kept++;
258                 } else {
259                         //fprintf(stderr, "dropping line (%lx:%lx): %s\n", subsystem, debug, buf);
260                         dropped++;
261                 }
262                 *p = '\n';
263                 p++;
264                 size -= (p - buf);
265                 buf = p;
266         }
267
268         printf("Debug log: %lu lines, %lu kept, %lu dropped.\n",
269                 dropped + kept, kept, dropped);
270 }
271
272 int jt_dbg_debug_kernel(int argc, char **argv)
273 {
274         int rc, raw = 1;
275         FILE *fd = stdout;
276         const int databuf_size = (6 << 20);
277         struct portal_ioctl_data data, *newdata;
278         char *databuf = NULL;
279
280         if (argc > 3) {
281                 fprintf(stderr, "usage: %s [file] [raw]\n", argv[0]);
282                 return 0;
283         }
284
285         if (argc > 1) {
286                 fd = fopen(argv[1], "w");
287                 if (fd == NULL) {
288                         fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
289                                 strerror(errno));
290                         return -1;
291                 }
292         }
293         if (argc > 2)
294                 raw = atoi(argv[2]);
295
296         databuf = malloc(databuf_size);
297         if (!databuf) {
298                 fprintf(stderr, "No memory for buffer.\n");
299                 goto out;
300         }
301
302         memset(&data, 0, sizeof(data));
303         data.ioc_plen1 = databuf_size;
304         data.ioc_pbuf1 = databuf;
305
306         if (portal_ioctl_pack(&data, &buf, max) != 0) {
307                 fprintf(stderr, "portal_ioctl_pack failed.\n");
308                 goto out;
309         }
310
311         rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_GET_DEBUG, buf);
312         if (rc) {
313                 fprintf(stderr, "IOC_PORTAL_GET_DEBUG failed: %s\n",
314                         strerror(errno));
315                 goto out;
316         }
317
318         newdata = (struct portal_ioctl_data *)buf;
319         if (newdata->ioc_size > 0)
320                 dump_buffer(fd, databuf, newdata->ioc_size, raw);
321
322  out:
323         if (databuf)
324                 free(databuf);
325         if (fd != stdout)
326                 fclose(fd);
327         return 0;
328 }
329
330 int jt_dbg_debug_daemon(int argc, char **argv)
331 {
332         int i, rc;
333         unsigned int cmd = 0;
334         FILE *fd = stdout;
335         struct portal_ioctl_data data;
336
337         if (argc <= 1) {
338                 fprintf(stderr, "usage: %s [start file <#MB>|stop|pause|"
339                         "continue]\n", argv[0]);
340                 return 0;
341         }
342         for (i = 0; portal_debug_daemon_cmd[i].cmd != NULL; i++) {
343                 if (strcasecmp(argv[1], portal_debug_daemon_cmd[i].cmd) == 0) {
344                         cmd = portal_debug_daemon_cmd[i].cmdv;
345                         break;
346                 }
347         }
348         if (portal_debug_daemon_cmd[i].cmd == NULL) {
349                 fprintf(stderr, "usage: %s [start file <#MB>|stop|pause|"
350                         "continue]\n", argv[0]);
351                 return 0;
352         }
353         memset(&data, 0, sizeof(data));
354         if (cmd == DEBUG_DAEMON_START) {
355                 if (argc < 3) {
356                         fprintf(stderr, "usage: %s [start file <#MB>|stop|"
357                                 "pause|continue]\n", argv[0]);
358                         return 0;
359                 }
360                 if (access(argv[2], F_OK) != 0) {
361                         fd = fopen(argv[2], "w");
362                         if (fd != NULL) {
363                                 fclose(fd);
364                                 remove(argv[2]);
365                                 goto ok;
366                         }
367                 }
368                 if (access(argv[2], W_OK) == 0)
369                         goto ok;
370                 fprintf(stderr, "fopen(%s) failed: %s\n", argv[2],
371                         strerror(errno));
372                 return -1;
373 ok:
374                 data.ioc_inllen1 = strlen(argv[2]) + 1;
375                 data.ioc_inlbuf1 = argv[2];
376                 data.ioc_misc = 0;
377                 if (argc == 4) {
378                         unsigned long size;
379                         errno = 0;
380                         size = strtoul(argv[3], NULL, 0);
381                         if (errno) {
382                                 fprintf(stderr, "file size(%s): error %s\n",
383                                         argv[3], strerror(errno));
384                                 return -1;
385                         }
386                         data.ioc_misc = size;
387                 }
388         }
389         data.ioc_count = cmd;
390         if (portal_ioctl_pack(&data, &buf, max) != 0) {
391                 fprintf(stderr, "portal_ioctl_pack failed.\n");
392                 return -1;
393         }
394         rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_SET_DAEMON, buf);
395         if (rc < 0) {
396                 fprintf(stderr, "IOC_PORTAL_SET_DEMON failed: %s\n",
397                                 strerror(errno));
398                 return rc;
399         }
400         return 0;
401 }
402
403 int jt_dbg_debug_file(int argc, char **argv)
404 {
405         int rc, fd = -1, raw = 1;
406         FILE *output = stdout;
407         char *databuf = NULL;
408         struct stat statbuf;
409
410         if (argc > 4 || argc < 2) {
411                 fprintf(stderr, "usage: %s <input> [output] [raw]\n", argv[0]);
412                 return 0;
413         }
414
415         fd = open(argv[1], O_RDONLY);
416         if (fd < 0) {
417                 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
418                         strerror(errno));
419                 return -1;
420         }
421
422         rc = fstat(fd, &statbuf);
423         if (rc < 0) {
424                 fprintf(stderr, "fstat failed: %s\n", strerror(errno));
425                 goto out;
426         }
427
428         if (argc >= 3) {
429                 output = fopen(argv[2], "w");
430                 if (output == NULL) {
431                         fprintf(stderr, "fopen(%s) failed: %s\n", argv[2],
432                                 strerror(errno));
433                         goto out;
434                 }
435         }
436
437         if (argc == 4)
438                 raw = atoi(argv[3]);
439
440         databuf = mmap(NULL, statbuf.st_size, PROT_READ | PROT_WRITE,
441                        MAP_PRIVATE, fd, 0);
442         if (databuf == NULL) {
443                 fprintf(stderr, "mmap failed: %s\n", strerror(errno));
444                 goto out;
445         }
446
447         dump_buffer(output, databuf, statbuf.st_size, raw);
448
449  out:
450         if (databuf)
451                 munmap(databuf, statbuf.st_size);
452         if (output != stdout)
453                 fclose(output);
454         if (fd > 0)
455                 close(fd);
456         return 0;
457 }
458
459 int jt_dbg_clear_debug_buf(int argc, char **argv)
460 {
461         int rc;
462         struct portal_ioctl_data data;
463
464         if (argc != 1) {
465                 fprintf(stderr, "usage: %s\n", argv[0]);
466                 return 0;
467         }
468
469         memset(&data, 0, sizeof(data));
470         if (portal_ioctl_pack(&data, &buf, max) != 0) {
471                 fprintf(stderr, "portal_ioctl_pack failed.\n");
472                 return -1;
473         }
474
475         rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_CLEAR_DEBUG, buf);
476         if (rc) {
477                 fprintf(stderr, "IOC_PORTAL_CLEAR_DEBUG failed: %s\n",
478                         strerror(errno));
479                 return -1;
480         }
481         return 0;
482 }
483
484 int jt_dbg_mark_debug_buf(int argc, char **argv)
485 {
486         int rc, max_size = MAX_MARK_SIZE-1;
487         struct portal_ioctl_data data;
488         char *text;
489         time_t now = time(NULL);
490
491         if (argc > 1) {
492                 int counter;
493                 text = malloc(MAX_MARK_SIZE);
494                 strncpy(text, argv[1], max_size);
495                 max_size-=strlen(argv[1]);
496                 for(counter = 2; (counter < argc) && (max_size > 0) ; counter++){
497                         strncat(text, " ", 1);
498                         max_size-=1;
499                         strncat(text, argv[counter], max_size);
500                         max_size-=strlen(argv[counter]);
501                 }
502         } else {
503                 text = ctime(&now);
504                 text[strlen(text) - 1] = '\0'; /* stupid \n */
505         }
506         if (!max_size) {
507                 text[MAX_MARK_SIZE - 1] = '\0';
508         }
509
510         memset(&data, 0, sizeof(data));
511         data.ioc_inllen1 = strlen(text) + 1;
512         data.ioc_inlbuf1 = text;
513         if (portal_ioctl_pack(&data, &buf, max) != 0) {
514                 fprintf(stderr, "portal_ioctl_pack failed.\n");
515                 return -1;
516         }
517
518         rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_MARK_DEBUG, buf);
519         if (rc) {
520                 fprintf(stderr, "IOC_PORTAL_MARK_DEBUG failed: %s\n",
521                         strerror(errno));
522                 return -1;
523         }
524         return 0;
525 }
526
527 static struct mod_paths {
528         char *name, *path;
529 } mod_paths[] = {
530         {"libcfs", "lustre/portals/libcfs"},
531         {"portals", "lustre/portals/portals"},
532         {"ksocknal", "lustre/portals/knals/socknal"},
533         {"kptlrouter", "lustre/portals/router"},
534         {"lvfs", "lustre/lvfs"},
535         {"obdclass", "lustre/obdclass"},
536         {"llog_test", "lustre/obdclass"},
537         {"ptlrpc", "lustre/ptlrpc"},
538         {"obdext2", "lustre/obdext2"},
539         {"ost", "lustre/ost"},
540         {"osc", "lustre/osc"},
541         {"mds", "lustre/mds"},
542         {"mdc", "lustre/mdc"},
543         {"llite", "lustre/llite"},
544         {"obdecho", "lustre/obdecho"},
545         {"ldlm", "lustre/ldlm"},
546         {"obdfilter", "lustre/obdfilter"},
547         {"extN", "lustre/extN"},
548         {"lov", "lustre/lov"},
549         {"fsfilt_ext3", "lustre/lvfs"},
550         {"fsfilt_extN", "lustre/lvfs"},
551         {"fsfilt_reiserfs", "lustre/lvfs"},
552         {"mds_ext2", "lustre/mds"},
553         {"mds_ext3", "lustre/mds"},
554         {"mds_extN", "lustre/mds"},
555         {"ptlbd", "lustre/ptlbd"},
556         {"mgmt_svc", "lustre/mgmt"},
557         {"mgmt_cli", "lustre/mgmt"},
558         {NULL, NULL}
559 };
560
561 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
562 int jt_dbg_modules(int argc, char **argv)
563 {
564         struct mod_paths *mp;
565         char *path = "..";
566         char *kernel = "linux";
567
568         if (argc >= 2)
569                 path = argv[1];
570         if (argc == 3)
571                 kernel = argv[2];
572         if (argc > 3) {
573                 printf("%s [path] [kernel]\n", argv[0]);
574                 return 0;
575         }
576
577         for (mp = mod_paths; mp->name != NULL; mp++) {
578                 struct module_info info;
579                 int rc;
580                 size_t crap;
581                 int query_module(const char *name, int which, void *buf,
582                                  size_t bufsize, size_t *ret);
583
584                 rc = query_module(mp->name, QM_INFO, &info, sizeof(info),
585                                   &crap);
586                 if (rc < 0) {
587                         if (errno != ENOENT)
588                                 printf("query_module(%s) failed: %s\n",
589                                        mp->name, strerror(errno));
590                 } else {
591                         printf("add-symbol-file %s/%s/%s.o 0x%0lx\n", path,
592                                mp->path, mp->name,
593                                info.addr + sizeof(struct module));
594                 }
595         }
596
597         return 0;
598 }
599 #else
600 int jt_dbg_modules(int argc, char **argv)
601 {
602         struct mod_paths *mp;
603         char *path = "..";
604         char *kernel = "linux";
605         const char *proc = "/proc/modules";
606         char modname[128], others[128];
607         long modaddr;
608         int rc;
609         FILE *file;
610
611         if (argc >= 2)
612                 path = argv[1];
613         if (argc == 3)
614                 kernel = argv[2];
615         if (argc > 3) {
616                 printf("%s [path] [kernel]\n", argv[0]);
617                 return 0;
618         }
619
620         file = fopen(proc, "r");
621         if (!file) {
622                 printf("failed open %s: %s\n", proc, strerror(errno));
623                 return 0;
624         }
625
626         while ((rc = fscanf(file, "%s %s %s %s %s %lx\n",
627                 modname, others, others, others, others, &modaddr)) == 6) {
628                 for (mp = mod_paths; mp->name != NULL; mp++) {
629                         if (!strcmp(mp->name, modname))
630                                 break;
631                 }
632                 if (mp->name) {
633                         printf("add-symbol-file %s/%s/%s.o 0x%0lx\n", path,
634                                mp->path, mp->name, modaddr);
635                 }
636         }
637
638         return 0;
639 }
640 #endif /* linux 2.5 */
641
642 int jt_dbg_panic(int argc, char **argv)
643 {
644         int rc;
645         struct portal_ioctl_data data;
646
647         if (argc != 1) {
648                 fprintf(stderr, "usage: %s\n", argv[0]);
649                 return 0;
650         }
651
652         memset(&data, 0, sizeof(data));
653         if (portal_ioctl_pack(&data, &buf, max) != 0) {
654                 fprintf(stderr, "portal_ioctl_pack failed.\n");
655                 return -1;
656         }
657
658         rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_PANIC, buf);
659         if (rc) {
660                 fprintf(stderr, "IOC_PORTAL_PANIC failed: %s\n",
661                         strerror(errno));
662                 return -1;
663         }
664         return 0;
665 }