]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - gnu/usr.bin/gdb/kgdb/main.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / gnu / usr.bin / gdb / kgdb / main.c
1 /*
2  * Copyright (c) 2004 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/stat.h>
32 #include <sys/types.h>
33 #include <sys/ioctl.h>
34 #include <sys/resource.h>
35 #include <sys/select.h>
36 #include <sys/time.h>
37 #include <sys/wait.h>
38 #include <errno.h>
39 #include <err.h>
40 #include <inttypes.h>
41 #include <kvm.h>
42 #include <limits.h>
43 #include <paths.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48
49 /* libgdb stuff. */
50 #include <defs.h>
51 #include <frame.h>
52 #include <frame-unwind.h>
53 #include <inferior.h>
54 #include <interps.h>
55 #include <cli-out.h>
56 #include <main.h>
57 #include <objfiles.h>
58 #include <target.h>
59 #include <top.h>
60 #include <ui-file.h>
61 #include <bfd.h>
62 #include <gdbcore.h>
63 #include <wrapper.h>
64
65 extern frame_unwind_sniffer_ftype *kgdb_sniffer_kluge;
66
67 #include "kgdb.h"
68
69 static int dumpnr;
70 static int quiet;
71 static int verbose;
72
73 static char crashdir[PATH_MAX];
74 static char *kernel;
75 static char *remote;
76 static char *vmcore;
77 static struct ui_file *parse_gdberr;
78
79 static void (*kgdb_new_objfile_chain)(struct objfile * objfile);
80
81 static void
82 usage(void)
83 {
84
85         fprintf(stderr,
86             "usage: %s [-afqv] [-d crashdir] [-c core | -n dumpnr | -r device]\n"
87             "\t[kernel [core]]\n", getprogname());
88         exit(1);
89 }
90
91 static void
92 kernel_from_dumpnr(int nr)
93 {
94         char path[PATH_MAX];
95         FILE *info;
96         char *s;
97         struct stat st;
98         int l;
99
100         /*
101          * If there's a kernel image right here in the crash directory, then
102          * use it.  The kernel image is either called kernel.<nr> or is in a
103          * subdirectory kernel.<nr> and called kernel.  The latter allows us
104          * to collect the modules in the same place.
105          */
106         snprintf(path, sizeof(path), "%s/kernel.%d", crashdir, nr);
107         if (stat(path, &st) == 0) {
108                 if (S_ISREG(st.st_mode)) {
109                         kernel = strdup(path);
110                         return;
111                 }
112                 if (S_ISDIR(st.st_mode)) {
113                         snprintf(path, sizeof(path), "%s/kernel.%d/kernel",
114                             crashdir, nr);
115                         if (stat(path, &st) == 0 && S_ISREG(st.st_mode)) {
116                                 kernel = strdup(path);
117                                 return;
118                         }
119                 }
120         }
121
122         /*
123          * No kernel image here.  Parse the dump header.  The kernel object
124          * directory can be found there and we probably have the kernel
125          * image still in it.  The object directory may also have a kernel
126          * with debugging info (called kernel.debug).  If we have a debug
127          * kernel, use it.
128          */
129         snprintf(path, sizeof(path), "%s/info.%d", crashdir, nr);
130         info = fopen(path, "r");
131         if (info == NULL) {
132                 warn(path);
133                 return;
134         }
135         while (fgets(path, sizeof(path), info) != NULL) {
136                 l = strlen(path);
137                 if (l > 0 && path[l - 1] == '\n')
138                         path[--l] = '\0';
139                 if (strncmp(path, "    ", 4) == 0) {
140                         s = strchr(path, ':');
141                         s = (s == NULL) ? path + 4 : s + 1;
142                         l = snprintf(path, sizeof(path), "%s/kernel.debug", s);
143                         if (stat(path, &st) == -1 || !S_ISREG(st.st_mode)) {
144                                 path[l - 6] = '\0';
145                                 if (stat(path, &st) == -1 ||
146                                     !S_ISREG(st.st_mode))
147                                         break;
148                         }
149                         kernel = strdup(path);
150                         break;
151                 }
152         }
153         fclose(info);
154 }
155
156 static void
157 kgdb_new_objfile(struct objfile *objfile)
158 {
159         static int once = 1;
160
161         kld_new_objfile(objfile);
162         kgdb_trgt_new_objfile(objfile);
163
164         if (kgdb_new_objfile_chain != NULL)
165                 kgdb_new_objfile_chain(objfile);
166
167         if (once && objfile != NULL && objfile == symfile_objfile) {
168                 /*
169                  * The initial kernel has just been loaded.  Start the
170                  * remote target if we have one.
171                  */
172                 once = 0;
173                 if (remote != NULL)
174                         push_remote_target (remote, 0);
175         }
176 }
177
178 /*
179  * Parse an expression and return its value.  If 'quiet' is true, then
180  * any error messages from the parser are masked.
181  */
182 CORE_ADDR
183 kgdb_parse_1(const char *exp, int quiet)
184 {
185         struct ui_file *old_stderr;
186         struct cleanup *old_chain;
187         struct expression *expr;
188         struct value *val;
189         char *s;
190         CORE_ADDR n;
191
192         old_stderr = gdb_stderr;
193         if (quiet)
194                 gdb_stderr = parse_gdberr;
195         n = 0;
196         s = xstrdup(exp);
197         old_chain = make_cleanup(xfree, s);
198         if (gdb_parse_exp_1(&s, NULL, 0, &expr) && *s == '\0') {
199                 make_cleanup(free_current_contents, &expr);
200                 if (gdb_evaluate_expression(expr, &val))
201                     n = value_as_address(val);
202         }
203         do_cleanups(old_chain);
204         gdb_stderr = old_stderr;
205         return (n);
206 }
207
208 #define MSGBUF_SEQ_TO_POS(size, seq)    ((seq) % (size))
209
210 void
211 kgdb_dmesg(void)
212 {
213         CORE_ADDR bufp;
214         int size, rseq, wseq;
215         char c;
216
217         /*
218          * Display the unread portion of the message buffer. This gives the
219          * user a some initial data to work from.
220          */
221         if (quiet)
222                 return;
223         bufp = kgdb_parse("msgbufp->msg_ptr");
224         size = (int)kgdb_parse("msgbufp->msg_size");
225         rseq = (int)kgdb_parse("msgbufp->msg_rseq");
226         wseq = (int)kgdb_parse("msgbufp->msg_wseq");
227         rseq = MSGBUF_SEQ_TO_POS(size, rseq);
228         wseq = MSGBUF_SEQ_TO_POS(size, wseq);
229         if (bufp == 0 || size == 0 || rseq == wseq)
230                 return;
231
232         printf("\nUnread portion of the kernel message buffer:\n");
233         while (rseq < wseq) {
234                 read_memory(bufp + rseq, &c, 1);
235                 putchar(c);
236                 rseq++;
237                 if (rseq == size)
238                         rseq = 0;
239         }
240         if (c != '\n')
241                 putchar('\n');
242         putchar('\n');
243 }
244
245 static void
246 kgdb_init(char *argv0 __unused)
247 {
248
249         parse_gdberr = mem_fileopen();
250         set_prompt("(kgdb) ");
251         initialize_kgdb_target();
252         initialize_kld_target();
253         kgdb_new_objfile_chain = target_new_objfile_hook;
254         target_new_objfile_hook = kgdb_new_objfile;
255 }
256
257 /*
258  * Remote targets can support any number of syntaxes and we want to
259  * support them all with one addition: we support specifying a device
260  * node for a serial device without the "/dev/" prefix.
261  *
262  * What we do is to stat(2) the existing remote target first.  If that
263  * fails, we try it with "/dev/" prepended.  If that succeeds we use
264  * the resulting path, otherwise we use the original target.  If
265  * either stat(2) succeeds make sure the file is either a character
266  * device or a FIFO.
267  */
268 static void
269 verify_remote(void)
270 {
271         char path[PATH_MAX];
272         struct stat st;
273
274         if (stat(remote, &st) != 0) {
275                 snprintf(path, sizeof(path), "/dev/%s", remote);
276                 if (stat(path, &st) != 0)
277                         return;
278                 free(remote);
279                 remote = strdup(path);
280         }
281         if (!S_ISCHR(st.st_mode) && !S_ISFIFO(st.st_mode))
282                 errx(1, "%s: not a special file, FIFO or socket", remote);
283 }
284
285 static void
286 add_arg(struct captured_main_args *args, char *arg)
287 {
288
289         args->argc++;
290         args->argv = reallocf(args->argv, (args->argc + 1) * sizeof(char *));
291         if (args->argv == NULL)
292                 err(1, "Out of memory building argument list");
293         args->argv[args->argc] = arg;
294 }
295
296 int
297 main(int argc, char *argv[])
298 {
299         char path[PATH_MAX];
300         struct stat st;
301         struct captured_main_args args;
302         char *s;
303         int a, ch;
304
305         dumpnr = -1;
306
307         strlcpy(crashdir, "/var/crash", sizeof(crashdir));
308         s = getenv("KGDB_CRASH_DIR");
309         if (s != NULL)
310                 strlcpy(crashdir, s, sizeof(crashdir));
311
312         /* Convert long options into short options. */
313         for (a = 1; a < argc; a++) {
314                 s = argv[a];
315                 if (s[0] == '-') {
316                         s++;
317                         /* Long options take either 1 or 2 dashes. */
318                         if (s[0] == '-')
319                                 s++;
320                         if (strcmp(s, "quiet") == 0)
321                                 argv[a] = "-q";
322                         else if (strcmp(s, "fullname") == 0)
323                                 argv[a] = "-f";
324                 }
325         }
326
327         quiet = 0;
328         memset (&args, 0, sizeof args);
329         args.use_windows = 0;
330         args.interpreter_p = INTERP_CONSOLE;
331         args.argv = malloc(sizeof(char *));
332         args.argv[0] = argv[0];
333
334         while ((ch = getopt(argc, argv, "ac:d:fn:qr:vw")) != -1) {
335                 switch (ch) {
336                 case 'a':
337                         annotation_level++;
338                         break;
339                 case 'c':       /* use given core file. */
340                         if (vmcore != NULL) {
341                                 warnx("option %c: can only be specified once",
342                                     optopt);
343                                 usage();
344                                 /* NOTREACHED */
345                         }
346                         vmcore = strdup(optarg);
347                         break;
348                 case 'd':       /* lookup dumps in given directory. */
349                         strlcpy(crashdir, optarg, sizeof(crashdir));
350                         break;
351                 case 'f':
352                         annotation_level = 1;
353                         break;
354                 case 'n':       /* use dump with given number. */
355                         dumpnr = strtol(optarg, &s, 0);
356                         if (dumpnr < 0 || *s != '\0') {
357                                 warnx("option %c: invalid kernel dump number",
358                                     optopt);
359                                 usage();
360                                 /* NOTREACHED */
361                         }
362                         break;
363                 case 'q':
364                         quiet = 1;
365                         add_arg(&args, "-q");
366                         break;
367                 case 'r':       /* use given device for remote session. */
368                         if (remote != NULL) {
369                                 warnx("option %c: can only be specified once",
370                                     optopt);
371                                 usage();
372                                 /* NOTREACHED */
373                         }
374                         remote = strdup(optarg);
375                         break;
376                 case 'v':       /* increase verbosity. */
377                         verbose++;
378                         break;
379                 case 'w':       /* core file is writeable. */
380                         add_arg(&args, "--write");
381                         break;
382                 case '?':
383                 default:
384                         usage();
385                 }
386         }
387
388         if (((vmcore != NULL) ? 1 : 0) + ((dumpnr >= 0) ? 1 : 0) +
389             ((remote != NULL) ? 1 : 0) > 1) {
390                 warnx("options -c, -n and -r are mutually exclusive");
391                 usage();
392                 /* NOTREACHED */
393         }
394
395         if (verbose > 1)
396                 warnx("using %s as the crash directory", crashdir);
397
398         if (argc > optind)
399                 kernel = strdup(argv[optind++]);
400
401         if (argc > optind && (dumpnr >= 0 || remote != NULL)) {
402                 warnx("options -n and -r do not take a core file. Ignored");
403                 optind = argc;
404         }
405
406         if (dumpnr >= 0) {
407                 snprintf(path, sizeof(path), "%s/vmcore.%d", crashdir, dumpnr);
408                 if (stat(path, &st) == -1)
409                         err(1, path);
410                 if (!S_ISREG(st.st_mode))
411                         errx(1, "%s: not a regular file", path);
412                 vmcore = strdup(path);
413         } else if (remote != NULL) {
414                 verify_remote();
415         } else if (argc > optind) {
416                 if (vmcore == NULL)
417                         vmcore = strdup(argv[optind++]);
418                 if (argc > optind)
419                         warnx("multiple core files specified. Ignored");
420         } else if (vmcore == NULL && kernel == NULL) {
421                 vmcore = strdup(_PATH_MEM);
422                 kernel = strdup(getbootfile());
423         }
424
425         if (verbose) {
426                 if (vmcore != NULL)
427                         warnx("core file: %s", vmcore);
428                 if (remote != NULL)
429                         warnx("device file: %s", remote);
430                 if (kernel != NULL)
431                         warnx("kernel image: %s", kernel);
432         }
433
434         /* A remote target requires an explicit kernel argument. */
435         if (remote != NULL && kernel == NULL) {
436                 warnx("remote debugging requires a kernel");
437                 usage();
438                 /* NOTREACHED */
439         }
440
441         /* If we don't have a kernel image yet, try to find one. */
442         if (kernel == NULL) {
443                 if (dumpnr >= 0)
444                         kernel_from_dumpnr(dumpnr);
445
446                 if (kernel == NULL)
447                         errx(1, "couldn't find a suitable kernel image");
448                 if (verbose)
449                         warnx("kernel image: %s", kernel);
450         }
451         add_arg(&args, kernel);
452
453         if (vmcore != NULL)
454                 add_arg(&args, vmcore);
455
456         /* The libgdb code uses optind too. Reset it... */
457         optind = 0;
458
459         /* Terminate argv list. */
460         add_arg(&args, NULL);
461
462         init_ui_hook = kgdb_init;
463
464         kgdb_sniffer_kluge = kgdb_trgt_trapframe_sniffer;
465
466         return (gdb_main(&args));
467 }