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