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