.\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32 .\" .\" Standard preamble: .\" ======================================================================== .de Sh \" Subsection heading .br .if t .Sp .ne 5 .PP \fB\\$1\fR .PP .. .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. | will give a .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' .\" expand to `' in nroff, nothing in troff, for use with C<>. .tr \(*W-|\(bv\*(Tr .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' 'br\} .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . nr % 0 . rr F .\} .\" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .hy 0 .if n .na .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "C2PH 1" .TH C2PH 1 "2007-05-11" "perl v5.8.8" "Perl Programmers Reference Guide" .SH "NAME" c2ph, pstruct \- Dump C structures as generated from \f(CW\*(C`cc \-g \-S\*(C'\fR stabs .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& c2ph [-dpnP] [var=val] [files ...] .Ve .Sh "\s-1OPTIONS\s0" .IX Subsection "OPTIONS" .Vb 1 \& Options: .Ve .PP .Vb 2 \& -w wide; short for: type_width=45 member_width=35 offset_width=8 \& -x hex; short for: offset_fmt=x offset_width=08 size_fmt=x size_width=04 .Ve .PP .Vb 3 \& -n do not generate perl code (default when invoked as pstruct) \& -p generate perl code (default when invoked as c2ph) \& -v generate perl code, with C decls as comments .Ve .PP .Vb 2 \& -i do NOT recompute sizes for intrinsic datatypes \& -a dump information on intrinsics also .Ve .PP .Vb 2 \& -t trace execution \& -d spew reams of debugging output .Ve .PP .Vb 1 \& -slist give comma-separated list a structures to dump .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" The following is the old c2ph.doc documentation by Tom Christiansen Date: 25 Jul 91 08:10:21 \s-1GMT\s0 .PP Once upon a time, I wrote a program called pstruct. It was a perl program that tried to parse out C structures and display their member offsets for you. This was especially useful for people looking at binary dumps or poking around the kernel. .PP Pstruct was not a pretty program. Neither was it particularly robust. The problem, you see, was that the C compiler was much better at parsing C than I could ever hope to be. .PP So I got smart: I decided to be lazy and let the C compiler parse the C, which would spit out debugger stabs for me to read. These were much easier to parse. It's still not a pretty program, but at least it's more robust. .PP Pstruct takes any .c or .h files, or preferably .s ones, since that's the format it is going to massage them into anyway, and spits out listings like this: .PP .Vb 36 \& struct tty { \& int tty.t_locker 000 4 \& int tty.t_mutex_index 004 4 \& struct tty * tty.t_tp_virt 008 4 \& struct clist tty.t_rawq 00c 20 \& int tty.t_rawq.c_cc 00c 4 \& int tty.t_rawq.c_cmax 010 4 \& int tty.t_rawq.c_cfx 014 4 \& int tty.t_rawq.c_clx 018 4 \& struct tty * tty.t_rawq.c_tp_cpu 01c 4 \& struct tty * tty.t_rawq.c_tp_iop 020 4 \& unsigned char * tty.t_rawq.c_buf_cpu 024 4 \& unsigned char * tty.t_rawq.c_buf_iop 028 4 \& struct clist tty.t_canq 02c 20 \& int tty.t_canq.c_cc 02c 4 \& int tty.t_canq.c_cmax 030 4 \& int tty.t_canq.c_cfx 034 4 \& int tty.t_canq.c_clx 038 4 \& struct tty * tty.t_canq.c_tp_cpu 03c 4 \& struct tty * tty.t_canq.c_tp_iop 040 4 \& unsigned char * tty.t_canq.c_buf_cpu 044 4 \& unsigned char * tty.t_canq.c_buf_iop 048 4 \& struct clist tty.t_outq 04c 20 \& int tty.t_outq.c_cc 04c 4 \& int tty.t_outq.c_cmax 050 4 \& int tty.t_outq.c_cfx 054 4 \& int tty.t_outq.c_clx 058 4 \& struct tty * tty.t_outq.c_tp_cpu 05c 4 \& struct tty * tty.t_outq.c_tp_iop 060 4 \& unsigned char * tty.t_outq.c_buf_cpu 064 4 \& unsigned char * tty.t_outq.c_buf_iop 068 4 \& (*int)() tty.t_oproc_cpu 06c 4 \& (*int)() tty.t_oproc_iop 070 4 \& (*int)() tty.t_stopproc_cpu 074 4 \& (*int)() tty.t_stopproc_iop 078 4 \& struct thread * tty.t_rsel 07c 4 .Ve .PP etc. .PP Actually, this was generated by a particular set of options. You can control the formatting of each column, whether you prefer wide or fat, hex or decimal, leading zeroes or whatever. .PP All you need to be able to use this is a C compiler than generates BSD/GCC\-style stabs. The \fB\-g\fR option on native \s-1BSD\s0 compilers and \s-1GCC\s0 should get this for you. .PP To learn more, just type a bogus option, like \fB\-\e?\fR, and a long usage message will be provided. There are a fair number of possibilities. .PP If you're only a C programmer, than this is the end of the message for you. You can quit right now, and if you care to, save off the source and run it when you feel like it. Or not. .PP But if you're a perl programmer, then for you I have something much more wondrous than just a structure offset printer. .PP You see, if you call pstruct by its other incybernation, c2ph, you have a code generator that translates C code into perl code! Well, structure and union declarations at least, but that's quite a bit. .PP Prior to this point, anyone programming in perl who wanted to interact with C programs, like the kernel, was forced to guess the layouts of the C structures, and then hardwire these into his program. Of course, when you took your wonderfully crafted program to a system where the sgtty structure was laid out differently, your program broke. Which is a shame. .PP We've had Larry's h2ph translator, which helped, but that only works on cpp symbols, not real C, which was also very much needed. What I offer you is a symbolic way of getting at all the C structures. I've couched them in terms of packages and functions. Consider the following program: .PP .Vb 1 \& #!/usr/local/bin/perl .Ve .PP .Vb 3 \& require 'syscall.ph'; \& require 'sys/time.ph'; \& require 'sys/resource.ph'; .Ve .PP .Vb 1 \& $ru = "\e0" x &rusage'sizeof(); .Ve .PP .Vb 1 \& syscall(&SYS_getrusage, &RUSAGE_SELF, $ru) && die "getrusage: $!"; .Ve .PP .Vb 1 \& @ru = unpack($t = &rusage'typedef(), $ru); .Ve .PP .Vb 2 \& $utime = $ru[ &rusage'ru_utime + &timeval'tv_sec ] \& + ($ru[ &rusage'ru_utime + &timeval'tv_usec ]) / 1e6; .Ve .PP .Vb 2 \& $stime = $ru[ &rusage'ru_stime + &timeval'tv_sec ] \& + ($ru[ &rusage'ru_stime + &timeval'tv_usec ]) / 1e6; .Ve .PP .Vb 1 \& printf "you have used %8.3fs+%8.3fu seconds.\en", $utime, $stime; .Ve .PP As you see, the name of the package is the name of the structure. Regular fields are just their own names. Plus the following accessor functions are provided for your convenience: .PP .Vb 3 \& struct This takes no arguments, and is merely the number of first-level \& elements in the structure. You would use this for indexing \& into arrays of structures, perhaps like this .Ve .PP .Vb 5 \& $usec = $u[ &user'u_utimer \& + (&ITIMER_VIRTUAL * &itimerval'struct) \& + &itimerval'it_value \& + &timeval'tv_usec \& ]; .Ve .PP .Vb 2 \& sizeof Returns the bytes in the structure, or the member if \& you pass it an argument, such as .Ve .PP .Vb 1 \& &rusage'sizeof(&rusage'ru_utime) .Ve .PP .Vb 6 \& typedef This is the perl format definition for passing to pack and \& unpack. If you ask for the typedef of a nothing, you get \& the whole structure, otherwise you get that of the member \& you ask for. Padding is taken care of, as is the magic to \& guarantee that a union is unpacked into all its aliases. \& Bitfields are not quite yet supported however. .Ve .PP .Vb 4 \& offsetof This function is the byte offset into the array of that \& member. You may wish to use this for indexing directly \& into the packed structure with vec() if you're too lazy \& to unpack it. .Ve .PP .Vb 6 \& typeof Not to be confused with the typedef accessor function, this \& one returns the C type of that field. This would allow \& you to print out a nice structured pretty print of some \& structure without knoning anything about it beforehand. \& No args to this one is a noop. Someday I'll post such \& a thing to dump out your u structure for you. .Ve .PP The way I see this being used is like basically this: .PP .Vb 3 \& % h2ph /usr/lib/perl/tmp.ph \& % c2ph some_include_file.h >> /usr/lib/perl/tmp.ph \& % install .Ve .PP It's a little tricker with c2ph because you have to get the includes right. I can't know this for your system, but it's not usually too terribly difficult. .PP The code isn't pretty as I mentioned \*(-- I never thought it would be a 1000\- line program when I started, or I might not have begun. :\-) But I would have been less cavalier in how the parts of the program communicated with each other, etc. It might also have helped if I didn't have to divine the makeup of the stabs on the fly, and then account for micro differences between my compiler and gcc. .PP Anyway, here it is. Should run on perl v4 or greater. Maybe less. .PP .Vb 1 \& --tom .Ve