Whamcloud - gitweb
land b1_4_bgl on HEAD (20050404_1913)
[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 #else
38 #include "ioctl.h"
39 #endif
40 #include <fcntl.h>
41 #include <errno.h>
42 #include <unistd.h>
43
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <sys/ioctl.h>
47 #include <sys/stat.h>
48 #include <sys/mman.h>
49 #include <sys/utsname.h>
50
51 #include <portals/api-support.h>
52 #include <portals/ptlctl.h>
53 #include <libcfs/portals_utils.h>
54 #include "parser.h"
55
56 #include <time.h>
57
58 static char rawbuf[8192];
59 static char *buf = rawbuf;
60 static int max = 8192;
61 /*static int g_pfd = -1;*/
62 static int subsystem_mask = ~0;
63 static int debug_mask = ~0;
64
65 #define MAX_MARK_SIZE 100
66
67 static const char *portal_debug_subsystems[] =
68         {"undefined", "mdc", "mds", "osc", 
69          "ost", "class", "log", "llite",
70          "rpc", "mgmt", "portals", "nal", 
71          "pinger", "filter", "ptlbd", "echo", 
72          "ldlm", "lov", "router", "cobd", 
73          "sm", "asobd", "confobd", "lmv", 
74          "cmobd", "sec", NULL};
75 static const char *portal_debug_masks[] =
76         {"trace", "inode", "super", "ext2", 
77          "malloc", "cache", "info", "ioctl",
78          "blocks", "net", "warning", "buffs", 
79          "other", "dentry", "portals", "page", 
80          "dlmtrace", "error", "emerg", "ha", 
81          "rpctrace", "vfstrace", "reada", "mmap",
82          "config", "console", "quota", "sec", NULL};
83
84 struct debug_daemon_cmd {
85         char *cmd;
86         unsigned int cmdv;
87 };
88
89 static const struct debug_daemon_cmd portal_debug_daemon_cmd[] = {
90         {"start", DEBUG_DAEMON_START},
91         {"stop", DEBUG_DAEMON_STOP},
92         {0, 0}
93 };
94
95 static int do_debug_mask(char *name, int enable)
96 {
97         int found = 0, i;
98
99         for (i = 0; portal_debug_subsystems[i] != NULL; i++) {
100                 if (strcasecmp(name, portal_debug_subsystems[i]) == 0 ||
101                     strcasecmp(name, "all_subs") == 0) {
102                         printf("%s output from subsystem \"%s\"\n",
103                                 enable ? "Enabling" : "Disabling",
104                                 portal_debug_subsystems[i]);
105                         if (enable)
106                                 subsystem_mask |= (1 << i);
107                         else
108                                 subsystem_mask &= ~(1 << i);
109                         found = 1;
110                 }
111         }
112         for (i = 0; portal_debug_masks[i] != NULL; i++) {
113                 if (strcasecmp(name, portal_debug_masks[i]) == 0 ||
114                     strcasecmp(name, "all_types") == 0) {
115                         printf("%s output of type \"%s\"\n",
116                                 enable ? "Enabling" : "Disabling",
117                                 portal_debug_masks[i]);
118                         if (enable)
119                                 debug_mask |= (1 << i);
120                         else
121                                 debug_mask &= ~(1 << i);
122                         found = 1;
123                 }
124         }
125
126         return found;
127 }
128
129 int dbg_initialize(int argc, char **argv)
130 {
131         return 0;
132 }
133
134 int jt_dbg_filter(int argc, char **argv)
135 {
136         int   i;
137
138         if (argc < 2) {
139                 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
140                         argv[0]);
141                 return 0;
142         }
143
144         for (i = 1; i < argc; i++)
145                 if (!do_debug_mask(argv[i], 0))
146                         fprintf(stderr, "Unknown subsystem or debug type: %s\n",
147                                 argv[i]);
148         return 0;
149 }
150
151 int jt_dbg_show(int argc, char **argv)
152 {
153         int    i;
154
155         if (argc < 2) {
156                 fprintf(stderr, "usage: %s <subsystem ID or debug mask>\n",
157                         argv[0]);
158                 return 0;
159         }
160
161         for (i = 1; i < argc; i++)
162                 if (!do_debug_mask(argv[i], 1))
163                         fprintf(stderr, "Unknown subsystem or debug type: %s\n",
164                                 argv[i]);
165
166         return 0;
167 }
168
169 static int applymask(char* procpath, int value)
170 {
171         int rc;
172         char buf[64];
173         int len = snprintf(buf, 64, "%d", value);
174
175         int fd = open(procpath, O_WRONLY);
176         if (fd == -1) {
177                 fprintf(stderr, "Unable to open %s: %s\n",
178                         procpath, strerror(errno));
179                 return fd;
180         }
181         rc = write(fd, buf, len+1);
182         if (rc<0) {
183                 fprintf(stderr, "Write to %s failed: %s\n",
184                         procpath, strerror(errno));
185                 return rc;
186         }
187         close(fd);
188         return 0;
189 }
190
191 static void applymask_all(unsigned int subs_mask, unsigned int debug_mask)
192 {
193         if (!dump_filename) {
194                 applymask("/proc/sys/portals/subsystem_debug", subs_mask);
195                 applymask("/proc/sys/portals/debug", debug_mask);
196         } else {
197                 struct portals_debug_ioctl_data data;
198
199                 data.hdr.ioc_len = sizeof(data);
200                 data.hdr.ioc_version = 0;
201                 data.subs = subs_mask;
202                 data.debug = debug_mask;
203
204                 dump(OBD_DEV_ID, PTL_IOC_DEBUG_MASK, &data);
205         }
206         printf("Applied subsystem_debug=%d, debug=%d to /proc/sys/portals\n",
207                subs_mask, debug_mask);
208 }
209
210 int jt_dbg_list(int argc, char **argv)
211 {
212         int i;
213
214         if (argc != 2) {
215                 fprintf(stderr, "usage: %s <subs || types>\n", argv[0]);
216                 return 0;
217         }
218
219         if (strcasecmp(argv[1], "subs") == 0) {
220                 printf("Subsystems: all_subs");
221                 for (i = 0; portal_debug_subsystems[i] != NULL; i++)
222                         printf(", %s", portal_debug_subsystems[i]);
223                 printf("\n");
224         } else if (strcasecmp(argv[1], "types") == 0) {
225                 printf("Types: all_types");
226                 for (i = 0; portal_debug_masks[i] != NULL; i++)
227                         printf(", %s", portal_debug_masks[i]);
228                 printf("\n");
229         } else if (strcasecmp(argv[1], "applymasks") == 0) {
230                 applymask_all(subsystem_mask, debug_mask);
231         }
232         return 0;
233 }
234
235 /* all strings nul-terminated; only the struct and hdr need to be freed */
236 struct dbg_line {
237         struct ptldebug_header *hdr;
238         char *file;
239         char *fn;
240         char *text;
241         struct list_head chain;
242 };
243
244 /* nurr. */
245 static void list_add_ordered(struct dbg_line *new, struct list_head *head)
246 {
247         struct list_head *pos;
248         struct dbg_line *curr;
249
250         list_for_each(pos, head) {
251                 curr = list_entry(pos, struct dbg_line, chain);
252
253                 if (curr->hdr->ph_sec < new->hdr->ph_sec)
254                         continue;
255                 if (curr->hdr->ph_sec == new->hdr->ph_sec &&
256                     curr->hdr->ph_usec < new->hdr->ph_usec)
257                         continue;
258
259                 list_add(&new->chain, pos->prev);
260                 return;
261         }
262         list_add_tail(&new->chain, head);
263 }
264
265 static void print_saved_records(struct list_head *list, FILE *out)
266 {
267         struct list_head *pos, *tmp;
268
269         list_for_each_safe(pos, tmp, list) {
270                 struct dbg_line *line;
271                 struct ptldebug_header *hdr;
272
273                 line = list_entry(pos, struct dbg_line, chain);
274                 list_del(&line->chain);
275
276                 hdr = line->hdr;
277                 fprintf(out, "%06x:%06x:%u:%u.%06Lu:%u:%u:%u:(%s:%u:%s()) %s",
278                         hdr->ph_subsys, hdr->ph_mask, hdr->ph_cpu_id,
279                         hdr->ph_sec, (unsigned long long)hdr->ph_usec,
280                         hdr->ph_stack, hdr->ph_pid, hdr->ph_extern_pid,
281                         line->file, hdr->ph_line_num, line->fn, line->text);
282                 free(line->hdr);
283                 free(line);
284         }
285 }
286
287 static int parse_buffer(FILE *in, FILE *out)
288 {
289         struct dbg_line *line;
290         struct ptldebug_header *hdr;
291         char buf[4097], *p;
292         int rc;
293         unsigned long dropped = 0, kept = 0;
294         struct list_head chunk_list;
295
296         CFS_INIT_LIST_HEAD(&chunk_list);
297
298         while (1) {
299                 rc = fread(buf, sizeof(hdr->ph_len), 1, in);
300                 if (rc <= 0)
301                         break;
302
303                 hdr = (void *)buf;
304                 if (hdr->ph_len == 0)
305                         break;
306                 if (hdr->ph_len > 4094) {
307                         fprintf(stderr, "unexpected large record: %d bytes.  "
308                                 "aborting.\n",
309                                 hdr->ph_len);
310                         break;
311                 }
312
313                 if (hdr->ph_flags & PH_FLAG_FIRST_RECORD) {
314                         print_saved_records(&chunk_list, out);
315                         assert(list_empty(&chunk_list));
316                 }
317
318                 rc = fread(buf + sizeof(hdr->ph_len), 1,
319                            hdr->ph_len - sizeof(hdr->ph_len), in);
320                 if (rc <= 0)
321                         break;
322
323                 if (hdr->ph_mask &&
324                     (!(subsystem_mask & hdr->ph_subsys) ||
325                      (!(debug_mask & hdr->ph_mask)))) {
326                         dropped++;
327                         continue;
328                 }
329
330                 line = malloc(sizeof(*line));
331                 if (line == NULL) {
332                         fprintf(stderr, "malloc failed; printing accumulated "
333                                 "records and exiting.\n");
334                         break;
335                 }
336
337                 line->hdr = malloc(hdr->ph_len + 1);
338                 if (line->hdr == NULL) {
339                         fprintf(stderr, "malloc failed; printing accumulated "
340                                 "records and exiting.\n");
341                         break;
342                 }
343
344                 p = (void *)line->hdr;
345                 memcpy(line->hdr, buf, hdr->ph_len);
346                 p[hdr->ph_len] = '\0';
347
348                 p += sizeof(*hdr);
349                 line->file = p;
350                 p += strlen(line->file) + 1;
351                 line->fn = p;
352                 p += strlen(line->fn) + 1;
353                 line->text = p;
354
355                 list_add_ordered(line, &chunk_list);
356                 kept++;
357         }
358
359         print_saved_records(&chunk_list, out);
360
361         printf("Debug log: %lu lines, %lu kept, %lu dropped.\n",
362                 dropped + kept, kept, dropped);
363         return 0;
364 }
365
366 int jt_dbg_debug_kernel(int argc, char **argv)
367 {
368         char filename[4096];
369         struct stat st;
370         int rc, raw = 0, fd;
371         FILE *in, *out = stdout;
372
373         if (argc > 3) {
374                 fprintf(stderr, "usage: %s [file] [raw]\n", argv[0]);
375                 return 0;
376         }
377
378         if (argc > 2) {
379                 raw = atoi(argv[2]);
380         } else if (argc > 1 && (argv[1][0] == '0' || argv[1][0] == '1')) {
381                 raw = atoi(argv[1]);
382                 argc--;
383         }
384
385         /* If we are dumping raw (which means no conversion step to ASCII)
386          * then dump directly to any supplied filename, otherwise this is
387          * just a temp file and we dump to the real file at convert time. */
388         if (argc > 1 && raw)
389                 strcpy(filename, argv[1]);
390         else
391                 sprintf(filename, "/tmp/lustre-log.%lu.%u",time(NULL),getpid());
392
393         if (stat(filename, &st) == 0 && S_ISREG(st.st_mode))
394                 unlink(filename);
395
396         fd = open("/proc/sys/portals/dump_kernel", O_WRONLY);
397         if (fd < 0) {
398                 fprintf(stderr, "open(dump_kernel) failed: %s\n",
399                         strerror(errno));
400                 return 1;
401         }
402
403         rc = write(fd, filename, strlen(filename));
404         if (rc != strlen(filename)) {
405                 fprintf(stderr, "write(%s) failed: %s\n", filename,
406                         strerror(errno));
407                 close(fd);
408                 return 1;
409         }
410         close(fd);
411
412         if (raw)
413                 return 0;
414
415         in = fopen(filename, "r");
416         if (in == NULL) {
417                 if (errno == ENOENT) /* no dump file created */
418                         return 0;
419
420                 fprintf(stderr, "fopen(%s) failed: %s\n", filename,
421                         strerror(errno));
422                 return 1;
423         }
424         if (argc > 1) {
425                 out = fopen(argv[1], "w");
426                 if (out == NULL) {
427                         fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
428                                 strerror(errno));
429                         fclose(in);
430                         return 1;
431                 }
432         }
433
434         rc = parse_buffer(in, out);
435         fclose(in);
436         if (argc > 1)
437                 fclose(out);
438         if (rc) {
439                 fprintf(stderr, "parse_buffer failed; leaving tmp file %s "
440                         "behind.\n", filename);
441         } else {
442                 rc = unlink(filename);
443                 if (rc)
444                         fprintf(stderr, "dumped successfully, but couldn't "
445                                 "unlink tmp file %s: %s\n", filename,
446                                 strerror(errno));
447         }
448         return rc;
449 }
450
451 int jt_dbg_debug_file(int argc, char **argv)
452 {
453         int    fdin;
454         int    fdout;
455         FILE  *in;
456         FILE  *out = stdout;
457         int    rc;
458
459         if (argc > 3 || argc < 2) {
460                 fprintf(stderr, "usage: %s <input> [output]\n", argv[0]);
461                 return 0;
462         }
463
464         fdin = open(argv[1], O_RDONLY | O_LARGEFILE);
465         if (fdin == -1) {
466                 fprintf(stderr, "open(%s) failed: %s\n", argv[1],
467                         strerror(errno));
468                 return 1;
469         }
470         in = fdopen(fdin, "r");
471         if (in == NULL) {
472                 fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
473                         strerror(errno));
474                 close(fdin);
475                 return 1;
476         }
477         if (argc > 2) {
478                 fdout = open(argv[2], 
479                              O_CREAT | O_TRUNC | O_WRONLY | O_LARGEFILE, 
480                              0600);
481                 if (fdout == -1) {
482                         fprintf(stderr, "open(%s) failed: %s\n", argv[2],
483                                 strerror(errno));
484                         fclose(in);
485                         return 1;
486                 }
487                 out = fdopen(fdout, "w");
488                 if (out == NULL) {
489                         fprintf(stderr, "fopen(%s) failed: %s\n", argv[2],
490                                 strerror(errno));
491                         fclose(in);
492                         close(fdout);
493                         return 1;
494                 }
495         }
496
497         rc = parse_buffer(in, out);
498
499         fclose(in);
500         if (out != stdout)
501                 fclose(out);
502
503         return rc;
504 }
505
506 static int
507 dbg_write_cmd(int fd, char *str)
508 {
509         int    len = strlen(str);
510         int    rc  = write(fd, str, len);
511         
512         return (rc == len ? 0 : 1);
513 }
514
515 const char debug_daemon_usage[] = "usage: %s {start file [MB]|stop}\n";
516 #define DAEMON_FILE "/proc/sys/portals/daemon_file"
517 int jt_dbg_debug_daemon(int argc, char **argv)
518 {
519         int  rc;
520         int  fd;
521
522         if (argc <= 1) {
523                 fprintf(stderr, debug_daemon_usage, argv[0]);
524                 return 1;
525         }
526
527         fd = open(DAEMON_FILE, O_WRONLY);
528         if (fd < 0) {
529                 fprintf(stderr, "open %s failed: %s\n", DAEMON_FILE,
530                         strerror(errno));
531                 return -1;
532         }
533         
534         rc = -1;
535         if (strcasecmp(argv[1], "start") == 0) {
536              if (argc < 3 || argc > 4 ||
537                     (argc == 4 && strlen(argv[3]) > 5)) {
538                         fprintf(stderr, debug_daemon_usage, argv[0]);
539                         goto out;
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                         snprintf(buf, sizeof(buf), "size=%ld", size);
558                         rc = dbg_write_cmd(fd, buf);
559
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, argv[2]);
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                 rc = 0;
574                 goto out;
575         }
576         if (strcasecmp(argv[1], "stop") == 0) {
577                 rc = dbg_write_cmd(fd, "stop");
578                 if (rc != 0) {
579                         fprintf(stderr, "stopping debug_daemon failed: %s\n",
580                                 strerror(errno));
581                         goto out;
582                 }
583
584                 rc = 0;
585                 goto out;
586         }
587
588         fprintf(stderr, debug_daemon_usage, argv[0]);
589         rc = -1;
590 out:
591         close(fd);
592         return rc;
593 }
594
595 int jt_dbg_clear_debug_buf(int argc, char **argv)
596 {
597         int rc;
598         struct portal_ioctl_data data;
599
600         if (argc != 1) {
601                 fprintf(stderr, "usage: %s\n", argv[0]);
602                 return 0;
603         }
604
605         memset(&data, 0, sizeof(data));
606         if (portal_ioctl_pack(&data, &buf, max) != 0) {
607                 fprintf(stderr, "portal_ioctl_pack failed.\n");
608                 return -1;
609         }
610
611         rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_CLEAR_DEBUG, buf);
612         if (rc) {
613                 fprintf(stderr, "IOC_PORTAL_CLEAR_DEBUG failed: %s\n",
614                         strerror(errno));
615                 return -1;
616         }
617         return 0;
618 }
619
620 int jt_dbg_mark_debug_buf(int argc, char **argv)
621 {
622         int rc, max_size = MAX_MARK_SIZE-1;
623         struct portal_ioctl_data data;
624         char *text;
625         time_t now = time(NULL);
626
627         if (argc > 1) {
628                 int counter;
629                 text = malloc(MAX_MARK_SIZE);
630                 strncpy(text, argv[1], max_size);
631                 max_size-=strlen(argv[1]);
632                 for(counter = 2; (counter < argc) && (max_size > 0) ; counter++){
633                         strncat(text, " ", 1);
634                         max_size-=1;
635                         strncat(text, argv[counter], max_size);
636                         max_size-=strlen(argv[counter]);
637                 }
638         } else {
639                 text = ctime(&now);
640                 text[strlen(text) - 1] = '\0'; /* stupid \n */
641         }
642         if (!max_size) {
643                 text[MAX_MARK_SIZE - 1] = '\0';
644         }
645
646         memset(&data, 0, sizeof(data));
647         data.ioc_inllen1 = strlen(text) + 1;
648         data.ioc_inlbuf1 = text;
649         if (portal_ioctl_pack(&data, &buf, max) != 0) {
650                 fprintf(stderr, "portal_ioctl_pack failed.\n");
651                 return -1;
652         }
653
654         rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_MARK_DEBUG, buf);
655         if (rc) {
656                 fprintf(stderr, "IOC_PORTAL_MARK_DEBUG failed: %s\n",
657                         strerror(errno));
658                 return -1;
659         }
660         return 0;
661 }
662
663 static struct mod_paths {
664         char *name, *path;
665 } mod_paths[] = {
666         {"libcfs", "portals/libcfs"},
667         {"portals", "portals/portals"},
668         {"ksocknal", "portals/knals/socknal"},
669         {"kptlrouter", "portals/router"},
670         {"lvfs", "lustre/lvfs"},
671         {"obdclass", "lustre/obdclass"},
672         {"llog_test", "lustre/obdclass"},
673         {"ptlrpcs", "lustre/sec"},
674         {"ptlrpcs_gss", "lustre/sec/gss"},
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         {"confobd", "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/%s.o 0x%0lx\n", path,
739                                path[0] ? "/" : "", 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/%s.o 0x%0lx\n", path,
784                                path[0] ? "/" : "", 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 }