Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 23 Aug 1998 21:06:24 +0200 (MET DST)
From:      Wolfram Schneider <wosch@panke.de.freebsd.org>
To:        current@FreeBSD.ORG
Subject:   file segment sizes of a core dump
Message-ID:  <199808231906.VAA04727@campa.panke.de>

next in thread | raw e-mail | index | archive | help

I added an option to size(1) which print the
file segment sizes of a core dump.

For example:

$ ./size /bin/sh 
text    data    bss     dec     hex
294912  12288   35656   342856  53b48

$ ./size -c sh.core 
text    data    bss     dec     hex
294912  69632   131072  495616  79000

If nobody objects I will commit the change.

Index: size.1
===================================================================
RCS file: /usr/cvs/src/usr.bin/size/size.1,v
retrieving revision 1.3
diff -u -r1.3 size.1
--- size.1	1997/08/11 07:28:18	1.3
+++ size.1	1998/08/23 17:13:04
@@ -39,6 +39,7 @@
 .Nd display object file segment sizes (text, data and bss)
 .Sh SYNOPSIS
 .Nm size
+.Op Fl c
 .Op Ar object_file ...
 .Sh DESCRIPTION
 .Nm Size
@@ -52,8 +53,20 @@
 .Nm
 attempts to report on the file
 .Pa a.out .
+.Pp
+The options are as follows:
+.Bl -tag -width indent
+.It Fl c
+Print the size of a 
+.Ar core dump 
+file instead a
+.Ar object_file .
+.El
+.Pp
 .Sh SEE ALSO
+.Xr gcore 1
 .Xr a.out 5
+.Xr core 5
 .Sh HISTORY
 A
 .Nm
Index: size.c
===================================================================
RCS file: /usr/cvs/src/usr.bin/size/size.c,v
retrieving revision 1.4
diff -u -r1.4 size.c
--- size.c	1998/07/06 21:01:32	1.4
+++ size.c	1998/08/23 18:58:03
@@ -47,6 +47,9 @@
 
 #include <sys/param.h>
 #include <sys/file.h>
+#include <sys/types.h>
+#include <sys/user.h>
+#include <sys/stat.h>
 #include <err.h>
 #include <a.out.h>
 #include <unistd.h>
@@ -57,6 +60,8 @@
 int	show __P((int, char *));
 static void	usage __P((void));
 
+int coredumpsize; /* run as coredumpsize(1) command */
+
 int
 main(argc, argv)
 	int argc;
@@ -64,8 +69,11 @@
 {
 	int ch, eval;
 
-	while ((ch = getopt(argc, argv, "")) != -1)
+	while ((ch = getopt(argc, argv, "c")) != -1)
 		switch(ch) {
+		case 'c':
+			coredumpsize = 1;
+			break;
 		case '?':
 		default:
 			usage();
@@ -90,6 +98,8 @@
 {
 	static int first = 1;
 	struct exec head;
+	struct user user;
+	struct stat stat;
 	u_long total;
 	int fd;
 
@@ -97,20 +107,47 @@
 		warn("%s", name);
 		return (1);
 	}
-	if (read(fd, &head, sizeof(head)) != sizeof(head) || N_BADMAG(head)) {
-		(void)close(fd);
-		warnx("%s: not in a.out format", name);
-		return (1);
+	
+	if (!coredumpsize) {
+		if (read(fd, &head, sizeof(head)) != sizeof(head) || 
+		    N_BADMAG(head)) {
+			(void)close(fd);
+			warnx("%s: not in a.out format", name);
+			return (1);
+		}
+	} else {
+		if (read(fd, &user, sizeof(user)) != sizeof(user) ||
+		    fstat(fd, &stat) == -1) {
+			(void)close(fd);
+			warnx("%s: not in core format", name);
+			return (1);
+		}
+		if (stat.st_size < ptoa(UPAGES + user.u_dsize + user.u_ssize)) {
+			warnx("%s: seems not to be a valid core dump file", name);
+			close(fd);
+			return(1);
+		}
 	}
 	(void)close(fd);
 
 	if (first) {
 		first = 0;
 		(void)printf("text\tdata\tbss\tdec\thex\n");
+	}
+
+	if (!coredumpsize) {
+		total = head.a_text + head.a_data + head.a_bss;
+		(void)printf("%lu\t%lu\t%lu\t%lu\t%lx", (u_long)head.a_text,
+		     (u_long)head.a_data, (u_long)head.a_bss, total, total);
+	} else {
+		total = ptoa(user.u_tsize) + ptoa(user.u_dsize) + 
+			ptoa(user.u_ssize);
+		(void)printf("%lu\t%lu\t%lu\t%lu\t%lx", 
+			     (u_long)ptoa(user.u_tsize), 
+			     (u_long)ptoa(user.u_dsize), 
+			     (u_long)ptoa(user.u_ssize), total, total);
 	}
-	total = head.a_text + head.a_data + head.a_bss;
-	(void)printf("%lu\t%lu\t%lu\t%lu\t%lx", (u_long)head.a_text,
-	    (u_long)head.a_data, (u_long)head.a_bss, total, total);
+
 	if (count > 1)
 		(void)printf("\t%s", name);
 	(void)printf("\n");
@@ -120,6 +157,6 @@
 static void
 usage()
 {
-	(void)fprintf(stderr, "usage: size [file ...]\n");
+	(void)fprintf(stderr, "usage: size [-c] [file ...]\n");
 	exit(1);
 }

-- 
Wolfram Schneider <wosch@freebsd.org> http://www.freebsd.org/~w/

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-current" in the body of the message



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