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