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