Whamcloud - gitweb
62c43a068412a96c99f0eb45f091b61f10bfc61b
[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 #define  _GNU_SOURCE
28
29 #include <stdio.h>
30 #ifdef HAVE_NETDB_H
31 #include <netdb.h>
32 #endif
33 #include <stdlib.h>
34 #include <string.h>
35 #include <fcntl.h>
36 #include <errno.h>
37 #include <unistd.h>
38
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <sys/ioctl.h>
42 #include <sys/stat.h>
43 #include <sys/mman.h>
44 #include <sys/utsname.h>
45
46 #include <portals/api-support.h>
47 #include <portals/ptlctl.h>
48 #include <libcfs/portals_utils.h>
49 #include "parser.h"
50
51 #include <time.h>
52
53 static char rawbuf[8192];
54 static char *buf = rawbuf;
55 static int max = 8192;
56 /*static int g_pfd = -1;*/
57 static int subsystem_mask = ~0;
58 static int debug_mask = ~0;
59
60 #define MAX_MARK_SIZE 100
61
62 static const char *portal_debug_subsystems[] =
63         {"undefined", "mdc", "mds", "osc", 
64          "ost", "class", "log", "llite",
65          "rpc", "mgmt", "portals", "nal", 
66          "pinger", "filter", "ptlbd", "echo", 
67          "ldlm", "lov", "router", "cobd", 
68          "sm", "asobd", "confobd", "lmv", 
69          "cmobd", NULL};
70 static const char *portal_debug_masks[] =
71         {"trace", "inode", "super", "ext2", 
72          "malloc", "cache", "info", "ioctl",
73          "blocks", "net", "warning", "buffs", 
74          "other", "dentry", "portals", "page", 
75          "dlmtrace", "error", "emerg", "ha", 
76          "rpctrace", "vfstrace", "reada", "mmap",
77          "config", "console", "quota", NULL};
78
79 struct debug_daemon_cmd {
80         char *cmd;
81         unsigned int cmdv;
82 };
83
84 static const struct debug_daemon_cmd portal_debug_daemon_cmd[] = {
85         {"start", DEBUG_DAEMON_START},
86         {"stop", DEBUG_DAEMON_STOP},
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 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 /* all strings nul-terminated; only the struct and hdr need to be freed */
231 struct dbg_line {
232         struct ptldebug_header *hdr;
233         char *file;
234         char *fn;
235         char *text;
236         struct list_head chain;
237 };
238
239 /* nurr. */
240 static void list_add_ordered(struct dbg_line *new, struct list_head *head)
241 {
242         struct list_head *pos;
243         struct dbg_line *curr;
244
245         list_for_each(pos, head) {
246                 curr = list_entry(pos, struct dbg_line, chain);
247
248                 if (curr->hdr->ph_sec < new->hdr->ph_sec)
249                         continue;
250                 if (curr->hdr->ph_sec == new->hdr->ph_sec &&
251                     curr->hdr->ph_usec < new->hdr->ph_usec)
252                         continue;
253
254                 list_add(&new->chain, pos->prev);
255                 return;
256         }
257         list_add_tail(&new->chain, head);
258 }
259
260 static void print_saved_records(struct list_head *list, FILE *out)
261 {
262         struct list_head *pos, *tmp;
263
264         list_for_each_safe(pos, tmp, list) {
265                 struct dbg_line *line;
266                 struct ptldebug_header *hdr;
267
268                 line = list_entry(pos, struct dbg_line, chain);
269                 list_del(&line->chain);
270
271                 hdr = line->hdr;
272                 fprintf(out, "%06x:%06x:%u:%u.%06Lu:%u:%u:%u:(%s:%u:%s()) %s",
273                         hdr->ph_subsys, hdr->ph_mask, hdr->ph_cpu_id,
274                         hdr->ph_sec, (unsigned long long)hdr->ph_usec,
275                         hdr->ph_stack, hdr->ph_pid, hdr->ph_extern_pid,
276                         line->file, hdr->ph_line_num, line->fn, line->text);
277                 free(line->hdr);
278                 free(line);
279         }
280 }
281
282 static int parse_buffer(FILE *in, FILE *out)
283 {
284         struct dbg_line *line;
285         struct ptldebug_header *hdr;
286         char buf[4097], *p;
287         int rc;
288         unsigned long dropped = 0, kept = 0;
289         struct list_head chunk_list;
290
291         CFS_INIT_LIST_HEAD(&chunk_list);
292
293         while (1) {
294                 rc = fread(buf, sizeof(hdr->ph_len), 1, in);
295                 if (rc <= 0)
296                         break;
297
298                 hdr = (void *)buf;
299                 if (hdr->ph_len == 0)
300                         break;
301                 if (hdr->ph_len > 4094) {
302                         fprintf(stderr, "unexpected large record: %d bytes.  "
303                                 "aborting.\n",
304                                 hdr->ph_len);
305                         break;
306                 }
307
308                 if (hdr->ph_flags & PH_FLAG_FIRST_RECORD) {
309                         print_saved_records(&chunk_list, out);
310                         assert(list_empty(&chunk_list));
311                 }
312
313                 rc = fread(buf + sizeof(hdr->ph_len), 1,
314                            hdr->ph_len - sizeof(hdr->ph_len), in);
315                 if (rc <= 0)
316                         break;
317
318                 if (hdr->ph_mask &&
319                     (!(subsystem_mask & hdr->ph_subsys) ||
320                      (!(debug_mask & hdr->ph_mask)))) {
321                         dropped++;
322                         continue;
323                 }
324
325                 line = malloc(sizeof(*line));
326                 if (line == NULL) {
327                         fprintf(stderr, "malloc failed; printing accumulated "
328                                 "records and exiting.\n");
329                         break;
330                 }
331
332                 line->hdr = malloc(hdr->ph_len + 1);
333                 if (line->hdr == NULL) {
334                         fprintf(stderr, "malloc failed; printing accumulated "
335                                 "records and exiting.\n");
336                         break;
337                 }
338
339                 p = (void *)line->hdr;
340                 memcpy(line->hdr, buf, hdr->ph_len);
341                 p[hdr->ph_len] = '\0';
342
343                 p += sizeof(*hdr);
344                 line->file = p;
345                 p += strlen(line->file) + 1;
346                 line->fn = p;
347                 p += strlen(line->fn) + 1;
348                 line->text = p;
349
350                 list_add_ordered(line, &chunk_list);
351                 kept++;
352         }
353
354         print_saved_records(&chunk_list, out);
355
356         printf("Debug log: %lu lines, %lu kept, %lu dropped.\n",
357                 dropped + kept, kept, dropped);
358         return 0;
359 }
360
361 int jt_dbg_debug_kernel(int argc, char **argv)
362 {
363         char filename[4096];
364         struct stat st;
365         int rc, raw = 0, fd;
366         FILE *in, *out = stdout;
367
368         if (argc > 3) {
369                 fprintf(stderr, "usage: %s [file] [raw]\n", argv[0]);
370                 return 0;
371         }
372
373         if (argc > 2) {
374                 raw = atoi(argv[2]);
375         } else if (argc > 1 && (argv[1][0] == '0' || argv[1][0] == '1')) {
376                 raw = atoi(argv[1]);
377                 argc--;
378         }
379
380         /* If we are dumping raw (which means no conversion step to ASCII)
381          * then dump directly to any supplied filename, otherwise this is
382          * just a temp file and we dump to the real file at convert time. */
383         if (argc > 1 && raw)
384                 strcpy(filename, argv[1]);
385         else
386                 sprintf(filename, "/tmp/lustre-log.%lu.%u",time(NULL),getpid());
387
388         if (stat(filename, &st) == 0 && S_ISREG(st.st_mode))
389                 unlink(filename);
390
391         fd = open("/proc/sys/portals/dump_kernel", O_WRONLY);
392         if (fd < 0) {
393                 fprintf(stderr, "open(dump_kernel) failed: %s\n",
394                         strerror(errno));
395                 return 1;
396         }
397
398         rc = write(fd, filename, strlen(filename));
399         if (rc != strlen(filename)) {
400                 fprintf(stderr, "write(%s) failed: %s\n", filename,
401                         strerror(errno));
402                 close(fd);
403                 return 1;
404         }
405         close(fd);
406
407         if (raw)
408                 return 0;
409
410         in = fopen(filename, "r");
411         if (in == NULL) {
412                 if (errno == ENOENT) /* no dump file created */
413                         return 0;
414
415                 fprintf(stderr, "fopen(%s) failed: %s\n", filename,
416                         strerror(errno));
417                 return 1;
418         }
419         if (argc > 1) {
420                 out = fopen(argv[1], "w");
421                 if (out == NULL) {
422                         fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
423                                 strerror(errno));
424                         fclose(in);
425                         return 1;
426                 }
427         }
428
429         rc = parse_buffer(in, out);
430         fclose(in);
431         if (argc > 1)
432                 fclose(out);
433         if (rc) {
434                 fprintf(stderr, "parse_buffer failed; leaving tmp file %s "
435                         "behind.\n", filename);
436         } else {
437                 rc = unlink(filename);
438                 if (rc)
439                         fprintf(stderr, "dumped successfully, but couldn't "
440                                 "unlink tmp file %s: %s\n", filename,
441                                 strerror(errno));
442         }
443         return rc;
444 }
445
446 int jt_dbg_debug_file(int argc, char **argv)
447 {
448         int    fdin;
449         int    fdout;
450         FILE  *in;
451         FILE  *out = stdout;
452         int    rc;
453
454         if (argc > 3 || argc < 2) {
455                 fprintf(stderr, "usage: %s <input> [output]\n", argv[0]);
456                 return 0;
457         }
458
459         fdin = open(argv[1], O_RDONLY | O_LARGEFILE);
460         if (fdin == -1) {
461                 fprintf(stderr, "open(%s) failed: %s\n", argv[1],
462                         strerror(errno));
463                 return 1;
464         }
465         in = fdopen(fdin, "r");
466         if (in == NULL) {
467                 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
468                         strerror(errno));
469                 close(fdin);
470                 return 1;
471         }
472         if (argc > 2) {
473                 fdout = open(argv[2], 
474                              O_CREAT | O_TRUNC | O_WRONLY | O_LARGEFILE, 
475                              0600);
476                 if (fdout == -1) {
477                         fprintf(stderr, "open(%s) failed: %s\n", argv[2],
478                                 strerror(errno));
479                         fclose(in);
480                         return 1;
481                 }
482                 out = fdopen(fdout, "w");
483                 if (out == NULL) {
484                         fprintf(stderr, "fopen(%s) failed: %s\n", argv[2],
485                                 strerror(errno));
486                         fclose(in);
487                         close(fdout);
488                         return 1;
489                 }
490         }
491
492         rc = parse_buffer(in, out);
493
494         fclose(in);
495         if (out != stdout)
496                 fclose(out);
497
498         return rc;
499 }
500
501 static int
502 dbg_write_cmd(int fd, char *str)
503 {
504         int    len = strlen(str);
505         int    rc  = write(fd, str, len);
506         
507         return (rc == len ? 0 : 1);
508 }
509
510 const char debug_daemon_usage[] = "usage: %s {start file [MB]|stop}\n";
511 #define DAEMON_FILE "/proc/sys/portals/daemon_file"
512 int jt_dbg_debug_daemon(int argc, char **argv)
513 {
514         int  rc;
515         int  fd;
516
517         if (argc <= 1) {
518                 fprintf(stderr, debug_daemon_usage, argv[0]);
519                 return 1;
520         }
521
522         fd = open(DAEMON_FILE, O_WRONLY);
523         if (fd < 0) {
524                 fprintf(stderr, "open %s failed: %s\n", DAEMON_FILE,
525                         strerror(errno));
526                 return -1;
527         }
528         
529         rc = -1;
530         if (strcasecmp(argv[1], "start") == 0) {
531              if (argc < 3 || argc > 4 ||
532                     (argc == 4 && strlen(argv[3]) > 5)) {
533                         fprintf(stderr, debug_daemon_usage, argv[0]);
534                         goto out;
535                 }
536                 if (argc == 4) {
537                         char       buf[12];
538                         const long min_size = 10;
539                         const long max_size = 20480;
540                         long       size;
541                         char      *end;
542
543                         size = strtoul(argv[3], &end, 0);
544                         if (size < min_size ||
545                             size > max_size ||
546                             *end != 0) {
547                                 fprintf(stderr, "size %s invalid, must be in "
548                                         "the range %ld-%ld MB\n", argv[3],
549                                         min_size, max_size);
550                                 goto out;
551                         }
552                         snprintf(buf, sizeof(buf), "size=%ld", size);
553                         rc = dbg_write_cmd(fd, buf);
554
555                         if (rc != 0) {
556                                 fprintf(stderr, "set %s failed: %s\n",
557                                         buf, strerror(errno));
558                                 goto out;
559                         }
560                 }
561
562                 rc = dbg_write_cmd(fd, argv[2]);
563                 if (rc != 0) {
564                         fprintf(stderr, "start debug_daemon on %s failed: %s\n",
565                                 argv[2], strerror(errno));
566                         goto out;
567                 }
568                 rc = 0;
569                 goto out;
570         }
571         if (strcasecmp(argv[1], "stop") == 0) {
572                 rc = dbg_write_cmd(fd, "stop");
573                 if (rc != 0) {
574                         fprintf(stderr, "stopping debug_daemon failed: %s\n",
575                                 strerror(errno));
576                         goto out;
577                 }
578
579                 rc = 0;
580                 goto out;
581         }
582
583         fprintf(stderr, debug_daemon_usage, argv[0]);
584         rc = -1;
585 out:
586         close(fd);
587         return rc;
588 }
589
590 int jt_dbg_clear_debug_buf(int argc, char **argv)
591 {
592         int rc;
593         struct portal_ioctl_data data;
594
595         if (argc != 1) {
596                 fprintf(stderr, "usage: %s\n", argv[0]);
597                 return 0;
598         }
599
600         memset(&data, 0, sizeof(data));
601         if (portal_ioctl_pack(&data, &buf, max) != 0) {
602                 fprintf(stderr, "portal_ioctl_pack failed.\n");
603                 return -1;
604         }
605
606         rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_CLEAR_DEBUG, buf);
607         if (rc) {
608                 fprintf(stderr, "IOC_PORTAL_CLEAR_DEBUG failed: %s\n",
609                         strerror(errno));
610                 return -1;
611         }
612         return 0;
613 }
614
615 int jt_dbg_mark_debug_buf(int argc, char **argv)
616 {
617         int rc, max_size = MAX_MARK_SIZE-1;
618         struct portal_ioctl_data data;
619         char *text;
620         time_t now = time(NULL);
621
622         if (argc > 1) {
623                 int counter;
624                 text = malloc(MAX_MARK_SIZE);
625                 strncpy(text, argv[1], max_size);
626                 max_size-=strlen(argv[1]);
627                 for(counter = 2; (counter < argc) && (max_size > 0) ; counter++){
628                         strncat(text, " ", 1);
629                         max_size-=1;
630                         strncat(text, argv[counter], max_size);
631                         max_size-=strlen(argv[counter]);
632                 }
633         } else {
634                 text = ctime(&now);
635                 text[strlen(text) - 1] = '\0'; /* stupid \n */
636         }
637         if (!max_size) {
638                 text[MAX_MARK_SIZE - 1] = '\0';
639         }
640
641         memset(&data, 0, sizeof(data));
642         data.ioc_inllen1 = strlen(text) + 1;
643         data.ioc_inlbuf1 = text;
644         if (portal_ioctl_pack(&data, &buf, max) != 0) {
645                 fprintf(stderr, "portal_ioctl_pack failed.\n");
646                 return -1;
647         }
648
649         rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_MARK_DEBUG, buf);
650         if (rc) {
651                 fprintf(stderr, "IOC_PORTAL_MARK_DEBUG failed: %s\n",
652                         strerror(errno));
653                 return -1;
654         }
655         return 0;
656 }
657
658 static struct mod_paths {
659         char *name, *path;
660 } mod_paths[] = {
661         {"libcfs", "portals/libcfs"},
662         {"portals", "portals/portals"},
663         {"ksocknal", "portals/knals/socknal"},
664         {"kptlrouter", "portals/router"},
665         {"lvfs", "lustre/lvfs"},
666         {"obdclass", "lustre/obdclass"},
667         {"llog_test", "lustre/obdclass"},
668         {"ptlrpc", "lustre/ptlrpc"},
669         {"obdext2", "lustre/obdext2"},
670         {"ost", "lustre/ost"},
671         {"osc", "lustre/osc"},
672         {"mds", "lustre/mds"},
673         {"mdc", "lustre/mdc"},
674         {"llite", "lustre/llite"},
675         {"ldiskfs", "lustre/ldiskfs"},
676         {"smfs", "lustre/smfs"},
677         {"obdecho", "lustre/obdecho"},
678         {"ldlm", "lustre/ldlm"},
679         {"obdfilter", "lustre/obdfilter"},
680         {"extN", "lustre/extN"},
681         {"lov", "lustre/lov"},
682         {"lmv", "lustre/lmv"},
683         {"fsfilt_ext3", "lustre/lvfs"},
684         {"fsfilt_extN", "lustre/lvfs"},
685         {"fsfilt_reiserfs", "lustre/lvfs"},
686         {"fsfilt_smfs", "lustre/lvfs"},
687         {"fsfilt_ldiskfs", "lustre/lvfs"},
688         {"mds_ext2", "lustre/mds"},
689         {"mds_ext3", "lustre/mds"},
690         {"mds_extN", "lustre/mds"},
691         {"ptlbd", "lustre/ptlbd"},
692         {"mgmt_svc", "lustre/mgmt"},
693         {"mgmt_cli", "lustre/mgmt"},
694         {"cobd", "lustre/cobd"},
695         {"cmobd", "lustre/cmobd"},
696         {"confobd", "lustre/obdclass"},
697         {NULL, NULL}
698 };
699
700 static int jt_dbg_modules_2_4(int argc, char **argv)
701 {
702 #ifdef HAVE_LINUX_VERSION_H
703 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
704         struct mod_paths *mp;
705         char *path = "..";
706         char *kernel = "linux";
707
708         if (argc >= 2)
709                 path = argv[1];
710         if (argc == 3)
711                 kernel = argv[2];
712         if (argc > 3) {
713                 printf("%s [path] [kernel]\n", argv[0]);
714                 return 0;
715         }
716
717         for (mp = mod_paths; mp->name != NULL; mp++) {
718                 struct module_info info;
719                 int rc;
720                 size_t crap;
721                 int query_module(const char *name, int which, void *buf,
722                                  size_t bufsize, size_t *ret);
723
724                 rc = query_module(mp->name, QM_INFO, &info, sizeof(info),
725                                   &crap);
726                 if (rc < 0) {
727                         if (errno != ENOENT)
728                                 printf("query_module(%s) failed: %s\n",
729                                        mp->name, strerror(errno));
730                 } else {
731                         printf("add-symbol-file %s/%s/%s.o 0x%0lx\n", path,
732                                mp->path, mp->name,
733                                info.addr + sizeof(struct module));
734                 }
735         }
736
737         return 0;
738 #endif // Headers are 2.6-only
739 #endif // !HAVE_LINUX_VERSION_H
740         return -EINVAL;
741 }
742
743 static int jt_dbg_modules_2_5(int argc, char **argv)
744 {
745         struct mod_paths *mp;
746         char *path = "..";
747         char *kernel = "linux";
748         const char *proc = "/proc/modules";
749         char modname[128], others[128];
750         long modaddr;
751         int rc;
752         FILE *file;
753
754         if (argc >= 2)
755                 path = argv[1];
756         if (argc == 3)
757                 kernel = argv[2];
758         if (argc > 3) {
759                 printf("%s [path] [kernel]\n", argv[0]);
760                 return 0;
761         }
762
763         file = fopen(proc, "r");
764         if (!file) {
765                 printf("failed open %s: %s\n", proc, strerror(errno));
766                 return 0;
767         }
768
769         while ((rc = fscanf(file, "%s %s %s %s %s %lx\n",
770                 modname, others, others, others, others, &modaddr)) == 6) {
771                 for (mp = mod_paths; mp->name != NULL; mp++) {
772                         if (!strcmp(mp->name, modname))
773                                 break;
774                 }
775                 if (mp->name) {
776                         printf("add-symbol-file %s/%s/%s.o 0x%0lx\n", path,
777                                mp->path, mp->name, modaddr);
778                 }
779         }
780
781         return 0;
782 }
783
784 int jt_dbg_modules(int argc, char **argv)
785 {
786         int rc = 0;
787         struct utsname sysinfo;
788
789         rc = uname(&sysinfo);
790         if (rc) {
791                 printf("uname() failed: %s\n", strerror(errno));
792                 return 0;
793         }
794
795         if (sysinfo.release[2] > '4') {
796                 return jt_dbg_modules_2_5(argc, argv);
797         } else {
798                 return jt_dbg_modules_2_4(argc, argv);
799         }
800
801         return 0;
802 }
803
804 int jt_dbg_panic(int argc, char **argv)
805 {
806         int rc;
807         struct portal_ioctl_data data;
808
809         if (argc != 1) {
810                 fprintf(stderr, "usage: %s\n", argv[0]);
811                 return 0;
812         }
813
814         memset(&data, 0, sizeof(data));
815         if (portal_ioctl_pack(&data, &buf, max) != 0) {
816                 fprintf(stderr, "portal_ioctl_pack failed.\n");
817                 return -1;
818         }
819
820         rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_PANIC, buf);
821         if (rc) {
822                 fprintf(stderr, "IOC_PORTAL_PANIC failed: %s\n",
823                         strerror(errno));
824                 return -1;
825         }
826         return 0;
827 }