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