1C2PH(1)                Perl Programmers Reference Guide                C2PH(1)
2
3
4

NAME

6       c2ph, pstruct - Dump C structures as generated from "cc -g -S" stabs
7

SYNOPSIS

9           c2ph [-dpnP] [var=val] [files ...]
10
11       OPTIONS
12
13           Options:
14
15           -w  wide; short for: type_width=45 member_width=35 offset_width=8
16           -x  hex; short for:  offset_fmt=x offset_width=08 size_fmt=x size_width=04
17
18           -n  do not generate perl code  (default when invoked as pstruct)
19           -p  generate perl code         (default when invoked as c2ph)
20           -v  generate perl code, with C decls as comments
21
22           -i  do NOT recompute sizes for intrinsic datatypes
23           -a  dump information on intrinsics also
24
25           -t  trace execution
26           -d  spew reams of debugging output
27
28           -slist  give comma-separated list a structures to dump
29

DESCRIPTION

31       The following is the old c2ph.doc documentation by Tom Christiansen
32       <tchrist@perl.com> Date: 25 Jul 91 08:10:21 GMT
33
34       Once upon a time, I wrote a program called pstruct.  It was a perl pro‐
35       gram that tried to parse out C structures and display their member off‐
36       sets for you.  This was especially useful for people looking at binary
37       dumps or poking around the kernel.
38
39       Pstruct was not a pretty program.  Neither was it particularly robust.
40       The problem, you see, was that the C compiler was much better at pars‐
41       ing C than I could ever hope to be.
42
43       So I got smart:  I decided to be lazy and let the C compiler parse the
44       C, which would spit out debugger stabs for me to read.  These were much
45       easier to parse.  It's still not a pretty program, but at least it's
46       more robust.
47
48       Pstruct takes any .c or .h files, or preferably .s ones, since that's
49       the format it is going to massage them into anyway, and spits out list‐
50       ings like this:
51
52        struct tty {
53          int                          tty.t_locker                         000      4
54          int                          tty.t_mutex_index                    004      4
55          struct tty *                 tty.t_tp_virt                        008      4
56          struct clist                 tty.t_rawq                           00c     20
57            int                        tty.t_rawq.c_cc                      00c      4
58            int                        tty.t_rawq.c_cmax                    010      4
59            int                        tty.t_rawq.c_cfx                     014      4
60            int                        tty.t_rawq.c_clx                     018      4
61            struct tty *               tty.t_rawq.c_tp_cpu                  01c      4
62            struct tty *               tty.t_rawq.c_tp_iop                  020      4
63            unsigned char *            tty.t_rawq.c_buf_cpu                 024      4
64            unsigned char *            tty.t_rawq.c_buf_iop                 028      4
65          struct clist                 tty.t_canq                           02c     20
66            int                        tty.t_canq.c_cc                      02c      4
67            int                        tty.t_canq.c_cmax                    030      4
68            int                        tty.t_canq.c_cfx                     034      4
69            int                        tty.t_canq.c_clx                     038      4
70            struct tty *               tty.t_canq.c_tp_cpu                  03c      4
71            struct tty *               tty.t_canq.c_tp_iop                  040      4
72            unsigned char *            tty.t_canq.c_buf_cpu                 044      4
73            unsigned char *            tty.t_canq.c_buf_iop                 048      4
74          struct clist                 tty.t_outq                           04c     20
75            int                        tty.t_outq.c_cc                      04c      4
76            int                        tty.t_outq.c_cmax                    050      4
77            int                        tty.t_outq.c_cfx                     054      4
78            int                        tty.t_outq.c_clx                     058      4
79            struct tty *               tty.t_outq.c_tp_cpu                  05c      4
80            struct tty *               tty.t_outq.c_tp_iop                  060      4
81            unsigned char *            tty.t_outq.c_buf_cpu                 064      4
82            unsigned char *            tty.t_outq.c_buf_iop                 068      4
83          (*int)()                     tty.t_oproc_cpu                      06c      4
84          (*int)()                     tty.t_oproc_iop                      070      4
85          (*int)()                     tty.t_stopproc_cpu                   074      4
86          (*int)()                     tty.t_stopproc_iop                   078      4
87          struct thread *              tty.t_rsel                           07c      4
88
89       etc.
90
91       Actually, this was generated by a particular set of options.  You can
92       control the formatting of each column, whether you prefer wide or fat,
93       hex or decimal, leading zeroes or whatever.
94
95       All you need to be able to use this is a C compiler than generates
96       BSD/GCC-style stabs.  The -g option on native BSD compilers and GCC
97       should get this for you.
98
99       To learn more, just type a bogus option, like -\?, and a long usage
100       message will be provided.  There are a fair number of possibilities.
101
102       If you're only a C programmer, than this is the end of the message for
103       you.  You can quit right now, and if you care to, save off the source
104       and run it when you feel like it.  Or not.
105
106       But if you're a perl programmer, then for you I have something much
107       more wondrous than just a structure offset printer.
108
109       You see, if you call pstruct by its other incybernation, c2ph, you have
110       a code generator that translates C code into perl code!  Well, struc‐
111       ture and union declarations at least, but that's quite a bit.
112
113       Prior to this point, anyone programming in perl who wanted to interact
114       with C programs, like the kernel, was forced to guess the layouts of
115       the C structures, and then hardwire these into his program.  Of course,
116       when you took your wonderfully crafted program to a system where the
117       sgtty structure was laid out differently, your program broke.  Which is
118       a shame.
119
120       We've had Larry's h2ph translator, which helped, but that only works on
121       cpp symbols, not real C, which was also very much needed.  What I offer
122       you is a symbolic way of getting at all the C structures.  I've couched
123       them in terms of packages and functions.  Consider the following pro‐
124       gram:
125
126           #!/usr/local/bin/perl
127
128           require 'syscall.ph';
129           require 'sys/time.ph';
130           require 'sys/resource.ph';
131
132           $ru = "\0" x &rusage'sizeof();
133
134           syscall(&SYS_getrusage, &RUSAGE_SELF, $ru)      && die "getrusage: $!";
135
136           @ru = unpack($t = &rusage'typedef(), $ru);
137
138           $utime =  $ru[ &rusage'ru_utime + &timeval'tv_sec  ]
139                  + ($ru[ &rusage'ru_utime + &timeval'tv_usec ]) / 1e6;
140
141           $stime =  $ru[ &rusage'ru_stime + &timeval'tv_sec  ]
142                  + ($ru[ &rusage'ru_stime + &timeval'tv_usec ]) / 1e6;
143
144           printf "you have used %8.3fs+%8.3fu seconds.\n", $utime, $stime;
145
146       As you see, the name of the package is the name of the structure.  Reg‐
147       ular fields are just their own names.  Plus the following accessor
148       functions are provided for your convenience:
149
150           struct      This takes no arguments, and is merely the number of first-level
151                       elements in the structure.  You would use this for indexing
152                       into arrays of structures, perhaps like this
153
154                           $usec = $u[ &user'u_utimer
155                                       + (&ITIMER_VIRTUAL * &itimerval'struct)
156                                       + &itimerval'it_value
157                                       + &timeval'tv_usec
158                                     ];
159
160           sizeof      Returns the bytes in the structure, or the member if
161                       you pass it an argument, such as
162
163                               &rusage'sizeof(&rusage'ru_utime)
164
165           typedef     This is the perl format definition for passing to pack and
166                       unpack.  If you ask for the typedef of a nothing, you get
167                       the whole structure, otherwise you get that of the member
168                       you ask for.  Padding is taken care of, as is the magic to
169                       guarantee that a union is unpacked into all its aliases.
170                       Bitfields are not quite yet supported however.
171
172           offsetof    This function is the byte offset into the array of that
173                       member.  You may wish to use this for indexing directly
174                       into the packed structure with vec() if you're too lazy
175                       to unpack it.
176
177           typeof      Not to be confused with the typedef accessor function, this
178                       one returns the C type of that field.  This would allow
179                       you to print out a nice structured pretty print of some
180                       structure without knoning anything about it beforehand.
181                       No args to this one is a noop.  Someday I'll post such
182                       a thing to dump out your u structure for you.
183
184       The way I see this being used is like basically this:
185
186               % h2ph <some_include_file.h  >  /usr/lib/perl/tmp.ph
187               % c2ph  some_include_file.h  >> /usr/lib/perl/tmp.ph
188               % install
189
190       It's a little tricker with c2ph because you have to get the includes
191       right.  I can't know this for your system, but it's not usually too
192       terribly difficult.
193
194       The code isn't pretty as I mentioned  -- I never thought it would be a
195       1000- line program when I started, or I might not have begun. :-)  But
196       I would have been less cavalier in how the parts of the program commu‐
197       nicated with each other, etc.  It might also have helped if I didn't
198       have to divine the makeup of the stabs on the fly, and then account for
199       micro differences between my compiler and gcc.
200
201       Anyway, here it is.  Should run on perl v4 or greater.  Maybe less.
202
203        --tom
204
205
206
207perl v5.8.8                       2008-05-05                           C2PH(1)
Impressum