2 * Copyright (c) 2004 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 * $FreeBSD: src/gnu/usr.bin/gdb/kgdb/main.c,v 1.11 2006/01/04 23:17:52 kan Exp $
27 * $DragonFly: src/gnu/usr.bin/gdb/kgdb/kgdb.c,v 1.2 2007/08/25 21:59:05 corecode Exp $
30 #include <sys/cdefs.h>
32 #include <sys/param.h>
34 #include <sys/types.h>
35 #include <sys/ioctl.h>
36 #include <sys/resource.h>
37 #include <sys/select.h>
40 #include <sys/msgbuf.h>
52 #include <linker_set.h>
57 #include <frame-unwind.h>
68 extern void symbol_file_add_main (char *args, int from_tty);
73 static char kvm_err[_POSIX2_LINE_MAX];
78 static char crashdir[PATH_MAX];
83 static void (*kgdb_new_objfile_chain)(struct objfile * objfile);
86 lookup(const char *sym)
90 nl[0].n_name = (char *)(uintptr_t)sym;
92 if (kvm_nlist(kvm, nl) != 0) {
93 warnx("kvm_nlist(%s): %s", sym, kvm_geterr(kvm));
96 return (nl[0].n_value);
111 "usage: %s [-afqv] [-d crashdir] [-c core | -n dumpnr | -r device]\n"
112 "\t[kernel [core]]\n", getprogname());
117 kernel_from_dumpnr(int nr)
126 * If there's a kernel image right here in the crash directory, then
127 * use it. The kernel image is either called kernel.<nr> or is in a
128 * subdirectory kernel.<nr> and called kernel. The latter allows us
129 * to collect the modules in the same place.
131 snprintf(path, sizeof(path), "%s/kernel.%d", crashdir, nr);
132 if (stat(path, &st) == 0) {
133 if (S_ISREG(st.st_mode)) {
134 kernel = strdup(path);
137 if (S_ISDIR(st.st_mode)) {
138 snprintf(path, sizeof(path), "%s/kernel.%d/kernel",
140 if (stat(path, &st) == 0 && S_ISREG(st.st_mode)) {
141 kernel = strdup(path);
148 * No kernel image here. Parse the dump header. The kernel object
149 * directory can be found there and we probably have the kernel
150 * image still in it. The object directory may also have a kernel
151 * with debugging info (called kernel.debug). If we have a debug
154 snprintf(path, sizeof(path), "%s/info.%d", crashdir, nr);
155 info = fopen(path, "r");
160 while (fgets(path, sizeof(path), info) != NULL) {
162 if (l > 0 && path[l - 1] == '\n')
164 if (strncmp(path, " ", 4) == 0) {
165 s = strchr(path, ':');
166 s = (s == NULL) ? path + 4 : s + 1;
167 l = snprintf(path, sizeof(path), "%s/kernel.debug", s);
168 if (stat(path, &st) == -1 || !S_ISREG(st.st_mode)) {
170 if (stat(path, &st) == -1 ||
171 !S_ISREG(st.st_mode))
174 kernel = strdup(path);
182 kgdb_new_objfile(struct objfile *objfile)
185 printf("XXX: %s(%p)\n", __func__, objfile);
186 if (objfile != NULL) {
192 if (kgdb_new_objfile_chain != NULL)
193 kgdb_new_objfile_chain(objfile);
197 kgdb_parse(const char *exp)
199 struct cleanup *old_chain;
200 struct expression *expr;
206 old_chain = make_cleanup(free_current_contents, &expr);
207 expr = parse_expression(s);
208 val = (expr != NULL) ? evaluate_expression(expr) : NULL;
209 n = (val != NULL) ? value_as_address(val) : 0;
210 do_cleanups(old_chain);
215 #define MSGBUF_SEQ_TO_POS(size, seq) ((seq) % (size))
218 kgdb_init_target(void)
223 kern_desc = open(kernel, O_RDONLY);
225 errx(1, "couldn't open a kernel image");
227 kern_bfd = bfd_fdopenr(kernel, gnutarget, kern_desc);
228 if (kern_bfd == NULL) {
230 errx(1, "\"%s\": can't open to probe ABI: %s.", kernel,
231 bfd_errmsg (bfd_get_error ()));
233 bfd_set_cacheable(kern_bfd, 1);
235 if (!bfd_check_format (kern_bfd, bfd_object)) {
237 errx(1, "\"%s\": not in executable format: %s", kernel,
238 bfd_errmsg(bfd_get_error()));
241 set_gdbarch_from_file (kern_bfd);
244 frame_unwind_prepend_unwinder(current_gdbarch, &kgdb_trgt_trapframe_unwind);
246 symbol_file_add_main (kernel, 0);
248 push_remote_target (remote, 0);
254 kgdb_display_msgbuf(void)
257 struct msgbuf *bufp, buf;
262 * Display the unread portion of the message buffer. This gives the
263 * user a some initial data to work from.
265 addr = lookup("_msgbufp");
268 read_memory((CORE_ADDR)addr, (char *)&bufp, sizeof(bufp));
269 read_memory((CORE_ADDR)bufp, (char *)&buf, sizeof(buf));
270 if (buf.msg_size == 0 || buf.msg_bufr == buf.msg_bufx)
272 rseq = MSGBUF_SEQ_TO_POS(buf.msg_size, buf.msg_bufr);
273 wseq = MSGBUF_SEQ_TO_POS(buf.msg_size, buf.msg_bufx);
275 printf("\nUnread portion of the kernel message buffer:\n");
276 while (rseq < wseq) {
277 read_memory((CORE_ADDR)buf.msg_ptr + rseq, &c, 1);
280 if (rseq == buf.msg_size)
289 kgdb_init(char *argv0 __unused)
293 set_prompt("(kgdb) ");
294 kgdb_display_msgbuf();
295 print_stack_frame(get_selected_frame(),
296 frame_relative_level(get_selected_frame()), 1);
300 main(int argc, char *argv[])
304 struct captured_main_args args;
306 int a, ch, quiet, writecore;
310 strlcpy(crashdir, "/var/crash", sizeof(crashdir));
311 s = getenv("KGDB_CRASH_DIR");
313 strlcpy(crashdir, s, sizeof(crashdir));
315 /* Convert long options into short options. */
316 for (a = 1; a < argc; a++) {
320 /* Long options take either 1 or 2 dashes. */
323 if (strcmp(s, "quiet") == 0)
325 else if (strcmp(s, "fullname") == 0)
333 while ((ch = getopt(argc, argv, "ac:d:fn:qr:vw")) != -1) {
338 case 'c': /* use given core file. */
339 if (vmcore != NULL) {
340 warnx("option %c: can only be specified once",
345 vmcore = strdup(optarg);
347 case 'd': /* lookup dumps in given directory. */
348 strlcpy(crashdir, optarg, sizeof(crashdir));
351 annotation_level = 1;
353 case 'n': /* use dump with given number. */
354 dumpnr = strtol(optarg, &s, 0);
355 if (dumpnr < 0 || *s != '\0') {
356 warnx("option %c: invalid kernel dump number",
365 case 'r': /* use given device for remote session. */
366 if (remote != NULL) {
367 warnx("option %c: can only be specified once",
372 remote = strdup(optarg);
374 case 'v': /* increase verbosity. */
377 case 'w': /* core file is writeable. */
386 if (((vmcore != NULL) ? 1 : 0) + ((dumpnr >= 0) ? 1 : 0) +
387 ((remote != NULL) ? 1 : 0) > 1) {
388 warnx("options -c, -n and -r are mutually exclusive");
394 warnx("using %s as the crash directory", crashdir);
397 kernel = strdup(argv[optind++]);
399 if (argc > optind && (dumpnr >= 0 || remote != NULL)) {
400 warnx("options -n and -r do not take a core file. Ignored");
405 snprintf(path, sizeof(path), "%s/vmcore.%d", crashdir, dumpnr);
406 if (stat(path, &st) == -1)
408 if (!S_ISREG(st.st_mode))
409 errx(1, "%s: not a regular file", path);
410 vmcore = strdup(path);
411 } else if (remote != NULL && remote[0] != ':' && remote[0] != '|') {
412 if (stat(remote, &st) != 0) {
413 snprintf(path, sizeof(path), "/dev/%s", remote);
414 if (stat(path, &st) != 0) {
415 err(1, "%s", remote);
419 remote = strdup(path);
421 if (!S_ISCHR(st.st_mode) && !S_ISFIFO(st.st_mode)) {
422 errx(1, "%s: not a special file, FIFO or socket",
426 } else if (argc > optind) {
428 vmcore = strdup(argv[optind++]);
430 warnx("multiple core files specified. Ignored");
431 } else if (vmcore == NULL && kernel == NULL) {
432 vmcore = strdup(_PATH_MEM);
433 kernel = strdup(getbootfile());
438 warnx("core file: %s", vmcore);
440 warnx("device file: %s", remote);
442 warnx("kernel image: %s", kernel);
446 * At this point we must either have a core file or have a kernel
447 * with a remote target.
449 if (remote != NULL && kernel == NULL) {
450 warnx("remote debugging requires a kernel");
454 if (vmcore == NULL && remote == NULL) {
455 warnx("need a core file or a device for remote debugging");
460 /* If we don't have a kernel image yet, try to find one. */
461 if (kernel == NULL) {
463 kernel_from_dumpnr(dumpnr);
466 errx(1, "couldn't find a suitable kernel image");
468 warnx("kernel image: %s", kernel);
471 if (remote == NULL) {
472 kvm = kvm_openfiles(kernel, vmcore, NULL,
473 writecore ? O_RDWR : O_RDONLY, kvm_err);
480 /* The libgdb code uses optind too. Reset it... */
483 memset (&args, 0, sizeof args);
485 args.argc = 1 + quiet;
488 argv[args.argc] = NULL;
489 args.use_windows = 0;
490 args.interpreter_p = INTERP_CONSOLE;
492 deprecated_init_ui_hook = kgdb_init;
494 return (gdb_main(&args));