Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 11 Jun 2004 22:30:44 GMT
From:      Marcel Moolenaar <marcel@FreeBSD.org>
To:        Perforce Change Reviews <perforce@freebsd.org>
Subject:   PERFORCE change 54661 for review
Message-ID:  <200406112230.i5BMUi29066458@repoman.freebsd.org>

next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=54661

Change 54661 by marcel@marcel_nfs on 2004/06/11 22:30:01

	o Add option -v so that we can add a bit of chatter to help
	  diagnosing behavior.
	o Add option -d so that the user can specify an alternative
	  crash directory. This can be given to savecore, so we should
	  allow it too.
	o Add environment variable KGDB_CRASH_DIR so that kgdb(1) can
	  be used in environments where the crash directory is non-
	  standard and without having to specify the -d option all the
	  time.
	o Also look for kernel.<n> in the crashdir. This helps to keep
	  keep core and kernel in sync. Note that kernel.<n> can also
	  be a directory. We expect the kernel image to be in that
	  directory and named kernel.
	o Fix and update the usage()

Affected files ...

.. //depot/projects/gdb/usr.bin/kgdb/main.c#7 edit

Differences ...

==== //depot/projects/gdb/usr.bin/kgdb/main.c#7 (text+ko) ====

@@ -57,6 +57,8 @@
 pid_t gdb_pid;
 
 int dumpnr;
+int verbose;
+
 char crashdir[PATH_MAX];
 char *kernel;
 char *vmcore;
@@ -70,7 +72,8 @@
 static void
 usage(void)
 {
-	fprintf(stderr, "usage: %s [-d dumpno] [kernel] [core]\n",
+	fprintf(stderr,
+	    "usage: %s [-v] [-d crashdir] [-n dumpnr] [kernel [core]]\n",
 	    getprogname());
 	exit(1);
 }
@@ -107,7 +110,7 @@
 }
 
 static void
-parse_dump(void)
+use_dump(int nr)
 {
 	char path[PATH_MAX];
 	FILE *info;
@@ -115,11 +118,41 @@
 	struct stat st;
 	int l;
 
-	snprintf(path, sizeof(path), "%s/vmcore.%d", crashdir, dumpnr);
+	snprintf(path, sizeof(path), "%s/vmcore.%d", crashdir, nr);
 	if (stat(path, &st) == -1)
 		err(1, path);
+	if (!S_ISREG(st.st_mode))
+		errx(1, "%s: not a regular file", path);
+
 	vmcore = strdup(path);
-	snprintf(path, sizeof(path), "%s/info.%d", crashdir, dumpnr);
+
+	/*
+	 * See if there's a kernel image right here, use it. The kernel
+	 * image is either called kernel.<nr> or is in a subdirectory
+	 * kernel.<nr> and called kernel.
+	 */
+	snprintf(path, sizeof(path), "%s/kernel.%d", crashdir, nr);
+	if (stat(path, &st) == 0) {
+		if (S_ISREG(st.st_mode)) {
+			kernel = strdup(path);
+			return;
+		}
+		if (S_ISDIR(st.st_mode)) {
+			snprintf(path, sizeof(path), "%s/kernel.%d/kernel",
+			    crashdir, nr);
+			if (stat(path, &st) == 0 && S_ISREG(st.st_mode)) {
+				kernel = strdup(path);
+				return;
+			}
+		}
+	}
+
+	/*
+	 * No kernel image here. Parse the dump header. The kernel object
+	 * directory can be found there and we probably have the kernel
+	 * image in it still.
+	 */
+	snprintf(path, sizeof(path), "%s/info.%d", crashdir, nr);
 	info = fopen(path, "r");
 	if (info == NULL) {
 		warn(path);
@@ -133,9 +166,10 @@
 			s = strchr(path, ':');
 			s = (s == NULL) ? path + 4 : s + 1;
 			l = snprintf(path, sizeof(path), "%s/kernel.debug", s);
-			if (stat(path, &st) == -1) {
+			if (stat(path, &st) == -1 || !S_ISREG(st.st_mode)) {
 				path[l - 6] = '\0';
-				if (stat(path, &st) == -1)
+				if (stat(path, &st) == -1 ||
+				    !S_ISREG(st.st_mode))
 					break;
 			}
 			kernel = strdup(path);
@@ -143,6 +177,9 @@
 		}
 	}
 	fclose(info);
+
+	if (verbose && kernel == NULL)
+		warnx("dump %d: no kernel found", nr);
 }
 
 int
@@ -156,16 +193,17 @@
 	int f, nfds, status;
 	int ch;
 
-	strcpy(crashdir, "/var/crash");
-	while ((ch = getopt(argc, argv, "n:")) != -1) {
+	strlcpy(crashdir, "/var/crash", sizeof(crashdir));
+	s = getenv("KGDB_CRASH_DIR");
+	if (s != NULL)
+		strlcpy(crashdir, s, sizeof(crashdir));
+
+	while ((ch = getopt(argc, argv, "d:n:v")) != -1) {
 		switch (ch) {
+		case 'd':
+			strlcpy(crashdir, optarg, sizeof(crashdir));
+			break;
 		case 'n':
-			if (dumpnr) {
-				warnx("option %c specified multiple times",
-				    optopt);
-				usage();
-				/* NOTREACHED */
-			}
 			dumpnr = strtol(optarg, &s, 0);
 			if (dumpnr == 0 || *s != '\0') {
 				warnx("option %c: invalid kernel dump number",
@@ -173,7 +211,9 @@
 				usage();
 				/* NOTREACHED */
 			}
-			parse_dump();
+			break;
+		case 'v':
+			verbose++;
 			break;
 		case '?':
 		default:
@@ -183,6 +223,12 @@
 	argc -= optind;
 	argv += optind;
 
+	if (verbose > 1)
+		warnx("using %s as the crash directory", crashdir);
+
+	if (dumpnr)
+		use_dump(dumpnr);
+
 	if (argc > 0) {
 		if (kernel != NULL)
 			free(kernel);
@@ -201,6 +247,11 @@
 	if (vmcore == NULL)
 		errx(1, "core file not specified");
 
+	if (verbose) {
+		warnx("kernel image: %s", kernel);
+		warnx("core file: %s", vmcore);
+	}
+
 	cmdfile_name = strdup("/tmp/kgdb.XXXXXXXX");
 	if (cmdfile_name == NULL)
 		err(1, "strdup(3)");



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200406112230.i5BMUi29066458>