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.1 2006/03/07 15:48:11 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>
51 #include <linker_set.h>
56 #include <frame-unwind.h>
67 extern void symbol_file_add_main (char *args, int from_tty);
72 static char kvm_err[_POSIX2_LINE_MAX];
77 static char crashdir[PATH_MAX];
82 static void (*kgdb_new_objfile_chain)(struct objfile * objfile);
96 "usage: %s [-afqv] [-d crashdir] [-c core | -n dumpnr | -r device]\n"
97 "\t[kernel [core]]\n", getprogname());
102 kernel_from_dumpnr(int nr)
111 * If there's a kernel image right here in the crash directory, then
112 * use it. The kernel image is either called kernel.<nr> or is in a
113 * subdirectory kernel.<nr> and called kernel. The latter allows us
114 * to collect the modules in the same place.
116 snprintf(path, sizeof(path), "%s/kernel.%d", crashdir, nr);
117 if (stat(path, &st) == 0) {
118 if (S_ISREG(st.st_mode)) {
119 kernel = strdup(path);
122 if (S_ISDIR(st.st_mode)) {
123 snprintf(path, sizeof(path), "%s/kernel.%d/kernel",
125 if (stat(path, &st) == 0 && S_ISREG(st.st_mode)) {
126 kernel = strdup(path);
133 * No kernel image here. Parse the dump header. The kernel object
134 * directory can be found there and we probably have the kernel
135 * image still in it. The object directory may also have a kernel
136 * with debugging info (called kernel.debug). If we have a debug
139 snprintf(path, sizeof(path), "%s/info.%d", crashdir, nr);
140 info = fopen(path, "r");
145 while (fgets(path, sizeof(path), info) != NULL) {
147 if (l > 0 && path[l - 1] == '\n')
149 if (strncmp(path, " ", 4) == 0) {
150 s = strchr(path, ':');
151 s = (s == NULL) ? path + 4 : s + 1;
152 l = snprintf(path, sizeof(path), "%s/kernel.debug", s);
153 if (stat(path, &st) == -1 || !S_ISREG(st.st_mode)) {
155 if (stat(path, &st) == -1 ||
156 !S_ISREG(st.st_mode))
159 kernel = strdup(path);
167 kgdb_new_objfile(struct objfile *objfile)
170 printf("XXX: %s(%p)\n", __func__, objfile);
171 if (objfile != NULL) {
177 if (kgdb_new_objfile_chain != NULL)
178 kgdb_new_objfile_chain(objfile);
182 kgdb_parse(const char *exp)
184 struct cleanup *old_chain;
185 struct expression *expr;
191 old_chain = make_cleanup(free_current_contents, &expr);
192 expr = parse_expression(s);
193 val = (expr != NULL) ? evaluate_expression(expr) : NULL;
194 n = (val != NULL) ? value_as_address(val) : 0;
195 do_cleanups(old_chain);
200 #define MSGBUF_SEQ_TO_POS(size, seq) ((seq) % (size))
203 kgdb_init_target(void)
208 kern_desc = open(kernel, O_RDONLY);
210 errx(1, "couldn't open a kernel image");
212 kern_bfd = bfd_fdopenr(kernel, gnutarget, kern_desc);
213 if (kern_bfd == NULL) {
215 errx(1, "\"%s\": can't open to probe ABI: %s.", kernel,
216 bfd_errmsg (bfd_get_error ()));
218 bfd_set_cacheable(kern_bfd, 1);
220 if (!bfd_check_format (kern_bfd, bfd_object)) {
222 errx(1, "\"%s\": not in executable format: %s", kernel,
223 bfd_errmsg(bfd_get_error()));
226 set_gdbarch_from_file (kern_bfd);
229 frame_unwind_prepend_unwinder(current_gdbarch, &kgdb_trgt_trapframe_unwind);
231 symbol_file_add_main (kernel, 0);
233 push_remote_target (remote, 0);
239 kgdb_display_msgbuf(void)
242 int size, rseq, wseq;
246 * Display the unread portion of the message buffer. This gives the
247 * user a some initial data to work from.
249 bufp = kgdb_parse("msgbufp->msg_ptr");
250 size = (int)kgdb_parse("msgbufp->msg_size");
251 rseq = (int)kgdb_parse("msgbufp->msg_bufr");
252 wseq = (int)kgdb_parse("msgbufp->msg_bufx");
253 rseq = MSGBUF_SEQ_TO_POS(size, rseq);
254 wseq = MSGBUF_SEQ_TO_POS(size, wseq);
255 if (bufp == 0 || size == 0 || rseq == wseq)
258 printf("\nUnread portion of the kernel message buffer:\n");
259 while (rseq < wseq) {
260 read_memory(bufp + rseq, &c, 1);
272 kgdb_init(char *argv0 __unused)
276 set_prompt("(kgdb) ");
277 kgdb_display_msgbuf();
278 print_stack_frame(get_selected_frame(),
279 frame_relative_level(get_selected_frame()), 1);
283 main(int argc, char *argv[])
287 struct captured_main_args args;
289 int a, ch, quiet, writecore;
293 strlcpy(crashdir, "/var/crash", sizeof(crashdir));
294 s = getenv("KGDB_CRASH_DIR");
296 strlcpy(crashdir, s, sizeof(crashdir));
298 /* Convert long options into short options. */
299 for (a = 1; a < argc; a++) {
303 /* Long options take either 1 or 2 dashes. */
306 if (strcmp(s, "quiet") == 0)
308 else if (strcmp(s, "fullname") == 0)
316 while ((ch = getopt(argc, argv, "ac:d:fn:qr:vw")) != -1) {
321 case 'c': /* use given core file. */
322 if (vmcore != NULL) {
323 warnx("option %c: can only be specified once",
328 vmcore = strdup(optarg);
330 case 'd': /* lookup dumps in given directory. */
331 strlcpy(crashdir, optarg, sizeof(crashdir));
334 annotation_level = 1;
336 case 'n': /* use dump with given number. */
337 dumpnr = strtol(optarg, &s, 0);
338 if (dumpnr < 0 || *s != '\0') {
339 warnx("option %c: invalid kernel dump number",
348 case 'r': /* use given device for remote session. */
349 if (remote != NULL) {
350 warnx("option %c: can only be specified once",
355 remote = strdup(optarg);
357 case 'v': /* increase verbosity. */
360 case 'w': /* core file is writeable. */
369 if (((vmcore != NULL) ? 1 : 0) + ((dumpnr >= 0) ? 1 : 0) +
370 ((remote != NULL) ? 1 : 0) > 1) {
371 warnx("options -c, -n and -r are mutually exclusive");
377 warnx("using %s as the crash directory", crashdir);
380 kernel = strdup(argv[optind++]);
382 if (argc > optind && (dumpnr >= 0 || remote != NULL)) {
383 warnx("options -n and -r do not take a core file. Ignored");
388 snprintf(path, sizeof(path), "%s/vmcore.%d", crashdir, dumpnr);
389 if (stat(path, &st) == -1)
391 if (!S_ISREG(st.st_mode))
392 errx(1, "%s: not a regular file", path);
393 vmcore = strdup(path);
394 } else if (remote != NULL && remote[0] != ':' && remote[0] != '|') {
395 if (stat(remote, &st) != 0) {
396 snprintf(path, sizeof(path), "/dev/%s", remote);
397 if (stat(path, &st) != 0) {
398 err(1, "%s", remote);
402 remote = strdup(path);
404 if (!S_ISCHR(st.st_mode) && !S_ISFIFO(st.st_mode)) {
405 errx(1, "%s: not a special file, FIFO or socket",
409 } else if (argc > optind) {
411 vmcore = strdup(argv[optind++]);
413 warnx("multiple core files specified. Ignored");
414 } else if (vmcore == NULL && kernel == NULL) {
415 vmcore = strdup(_PATH_MEM);
416 kernel = strdup(getbootfile());
421 warnx("core file: %s", vmcore);
423 warnx("device file: %s", remote);
425 warnx("kernel image: %s", kernel);
429 * At this point we must either have a core file or have a kernel
430 * with a remote target.
432 if (remote != NULL && kernel == NULL) {
433 warnx("remote debugging requires a kernel");
437 if (vmcore == NULL && remote == NULL) {
438 warnx("need a core file or a device for remote debugging");
443 /* If we don't have a kernel image yet, try to find one. */
444 if (kernel == NULL) {
446 kernel_from_dumpnr(dumpnr);
449 errx(1, "couldn't find a suitable kernel image");
451 warnx("kernel image: %s", kernel);
454 if (remote == NULL) {
455 kvm = kvm_openfiles(kernel, vmcore, NULL,
456 writecore ? O_RDWR : O_RDONLY, kvm_err);
463 /* The libgdb code uses optind too. Reset it... */
466 memset (&args, 0, sizeof args);
468 args.argc = 1 + quiet;
471 argv[args.argc] = NULL;
472 args.use_windows = 0;
473 args.interpreter_p = INTERP_CONSOLE;
475 deprecated_init_ui_hook = kgdb_init;
477 return (gdb_main(&args));