Date: Thu, 21 Mar 2002 21:50:24 -0500 From: Garance A Drosihn <drosih@rpi.edu> To: phk@FreeBSD.ORG Cc: hackers@FreeBSD.ORG Subject: Re: C-struct dismantling tool... Message-ID: <p05101521b8c0338befde@[128.113.24.47]> In-Reply-To: <Pine.BSF.4.21.0203211256570.17693-100000@InterJet.elischer.org> References: <Pine.BSF.4.21.0203211256570.17693-100000@InterJet.elischer.org>
next in thread | previous in thread | raw e-mail | index | archive | help
At 12:57 PM -0800 3/21/02, Julian Elischer wrote: >On Thu, 21 Mar 2002, John Polstra wrote: > >> >> I thought about the .stabs approach too, and thought it seemed > > promising. Even better might be to use -gdwarf -g3, which in > > theory at least would provide information about #defines. > > > > For well-behaved structs, it's possible that rpcgen could be > > hacked up to do what you want. > >Sounds like a job for an objdump mutant. Or write a C-program which prints offset(x) and sizeof(x) for each field in the struct. It might not be too hard to write a perl or ruby script which could generate such a program for a given struct+include file. Here's a quick & dirty example. Perhaps someone C-smarter than I am could figure out how to reliably add a column indicating if the variable is signed or unsigned, integer or floating-point, plus any other interesting characteristics. The "tricky" lines below are only "tricky" in the sense that it would be tricky to get a script to *know* to add the lines for those fields, such they are a sub-structure. With a little more thought, it could also map out the struct, indicating any bytes which were skipped over. Though it seems to me there should be some way to get the compiler itself to dump out something like this. I know I have seen that in some non-C compilers, and I thought I had also seen it in some C compilers... /* * Grungy little program to print out a stat structure. * Garance/Mar 21/2002 * * cc -Wall -o dumpstat.o dumpstat.c */ #include <sys/stat.h> #include <stddef.h> #include <stdio.h> #define showheader(xStruct) {\ printf("Dump of '%s':\n", #xStruct);\ printf(" offset size fieldname\n");\ } #define showfield(xStruct,xVar) {\ indent = 1; \ for (cp = #xVar; *cp != '\0'; cp++) \ if (*cp == '.') indent += 2;\ printf(" 0x%04lX %4lu%*s- %-1s\n", \ (unsigned long) offsetof(xStruct, xVar), \ (unsigned long) sizeof(((xStruct *)0)->xVar), \ indent, " ", #xVar);\ } #define showtotal(xStruct) {\ printf("Total size = 0x%04lX aka %lu bytes)\n", \ (unsigned long) sizeof(xStruct), \ (unsigned long) sizeof(xStruct));\ } int main(int argc, char **argv) { const char *cp; int indent; showheader(struct stat); showfield(struct stat, st_dev); showfield(struct stat, st_ino); showfield(struct stat, st_mode); showfield(struct stat, st_nlink); showfield(struct stat, st_uid); showfield(struct stat, st_gid); showfield(struct stat, st_rdev); #ifndef _POSIX_SOURCE showfield(struct stat, st_atimespec); showfield(struct stat, st_atimespec.tv_sec); /* tricky */ showfield(struct stat, st_atimespec.tv_nsec); /* tricky */ showfield(struct stat, st_mtimespec); showfield(struct stat, st_mtimespec.tv_sec); /* tricky */ showfield(struct stat, st_mtimespec.tv_nsec); /* tricky */ showfield(struct stat, st_ctimespec); showfield(struct stat, st_ctimespec.tv_sec); /* tricky */ showfield(struct stat, st_ctimespec.tv_nsec); /* tricky */ #else showfield(struct stat, st_atime); showfield(struct stat, st_atimensec); showfield(struct stat, st_mtime); showfield(struct stat, st_mtimensec); showfield(struct stat, st_ctime); showfield(struct stat, st_ctimensec); #endif showfield(struct stat, st_size); showfield(struct stat, st_blocks); showfield(struct stat, st_blksize); showfield(struct stat, st_flags); showfield(struct stat, st_gen); showfield(struct stat, st_qspare); showtotal(struct stat); return 0; } -- Garance Alistair Drosehn = gad@eclipse.acs.rpi.edu Senior Systems Programmer or gad@freebsd.org Rensselaer Polytechnic Institute or drosih@rpi.edu To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?p05101521b8c0338befde>