home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / SLAKWARE / D12 / PERL1.TGZ / perl1.tar / usr / bin / c2ph < prev    next >
Text File  |  1996-06-28  |  36KB  |  1,356 lines

  1. #!/usr/bin/perl
  2.     eval 'exec perl -S $0 "$@"'
  3.     if 0;
  4. #
  5. #
  6. #   c2ph (aka pstruct)
  7. #   Tom Christiansen, <tchrist@convex.com>
  8. #
  9. #   As pstruct, dump C structures as generated from 'cc -g -S' stabs.
  10. #   As c2ph, do this PLUS generate perl code for getting at the structures.
  11. #
  12. #   See the usage message for more.  If this isn't enough, read the code.
  13. #
  14.  
  15. =head1 NAME
  16.  
  17. c2ph,pstruct - Dump C structures as generated from 'cc -g -S' stabs
  18.  
  19. =head1 SYNOPSIS
  20.  
  21.     c2ph [-dpnP] [var=val] [files ...]
  22.  
  23. =head2 OPTIONS
  24.  
  25.     Options:
  26.  
  27.     -w    wide; short for: type_width=45 member_width=35 offset_width=8
  28.     -x    hex; short for:  offset_fmt=x offset_width=08 size_fmt=x size_width=04
  29.  
  30.     -n    do not generate perl code  (default when invoked as pstruct)
  31.     -p    generate perl code         (default when invoked as c2ph)
  32.     -v    generate perl code, with C decls as comments
  33.  
  34.     -i    do NOT recompute sizes for intrinsic datatypes
  35.     -a    dump information on intrinsics also
  36.  
  37.     -t    trace execution
  38.     -d    spew reams of debugging output
  39.  
  40.     -slist  give comma-separated list a structures to dump
  41.  
  42. =head1 DESCRIPTION
  43.  
  44. The following is the old c2ph.doc documentation by Tom Christiansen
  45. <tchrist@perl.com>
  46. Date: 25 Jul 91 08:10:21 GMT
  47.  
  48. Once upon a time, I wrote a program called pstruct.  It was a perl
  49. program that tried to parse out C structures and display their member
  50. offsets for you.  This was especially useful for people looking at
  51. binary dumps or poking around the kernel.
  52.  
  53. Pstruct was not a pretty program.  Neither was it particularly robust.
  54. The problem, you see, was that the C compiler was much better at parsing
  55. C than I could ever hope to be.
  56.  
  57. So I got smart:  I decided to be lazy and let the C compiler parse the C,
  58. which would spit out debugger stabs for me to read.  These were much
  59. easier to parse.  It's still not a pretty program, but at least it's more
  60. robust.
  61.  
  62. Pstruct takes any .c or .h files, or preferably .s ones, since that's
  63. the format it is going to massage them into anyway, and spits out
  64. listings like this:
  65.  
  66. struct tty {
  67.   int                          tty.t_locker                         000      4
  68.   int                          tty.t_mutex_index                    004      4
  69.   struct tty *                 tty.t_tp_virt                        008      4
  70.   struct clist                 tty.t_rawq                           00c     20
  71.     int                        tty.t_rawq.c_cc                      00c      4
  72.     int                        tty.t_rawq.c_cmax                    010      4
  73.     int                        tty.t_rawq.c_cfx                     014      4
  74.     int                        tty.t_rawq.c_clx                     018      4
  75.     struct tty *               tty.t_rawq.c_tp_cpu                  01c      4
  76.     struct tty *               tty.t_rawq.c_tp_iop                  020      4
  77.     unsigned char *            tty.t_rawq.c_buf_cpu                 024      4
  78.     unsigned char *            tty.t_rawq.c_buf_iop                 028      4
  79.   struct clist                 tty.t_canq                           02c     20
  80.     int                        tty.t_canq.c_cc                      02c      4
  81.     int                        tty.t_canq.c_cmax                    030      4
  82.     int                        tty.t_canq.c_cfx                     034      4
  83.     int                        tty.t_canq.c_clx                     038      4
  84.     struct tty *               tty.t_canq.c_tp_cpu                  03c      4
  85.     struct tty *               tty.t_canq.c_tp_iop                  040      4
  86.     unsigned char *            tty.t_canq.c_buf_cpu                 044      4
  87.     unsigned char *            tty.t_canq.c_buf_iop                 048      4
  88.   struct clist                 tty.t_outq                           04c     20
  89.     int                        tty.t_outq.c_cc                      04c      4
  90.     int                        tty.t_outq.c_cmax                    050      4
  91.     int                        tty.t_outq.c_cfx                     054      4
  92.     int                        tty.t_outq.c_clx                     058      4
  93.     struct tty *               tty.t_outq.c_tp_cpu                  05c      4
  94.     struct tty *               tty.t_outq.c_tp_iop                  060      4
  95.     unsigned char *            tty.t_outq.c_buf_cpu                 064      4
  96.     unsigned char *            tty.t_outq.c_buf_iop                 068      4
  97.   (*int)()                     tty.t_oproc_cpu                      06c      4
  98.   (*int)()                     tty.t_oproc_iop                      070      4
  99.   (*int)()                     tty.t_stopproc_cpu                   074      4
  100.   (*int)()                     tty.t_stopproc_iop                   078      4
  101.   struct thread *              tty.t_rsel                           07c      4
  102.  
  103.   etc.
  104.  
  105.  
  106. Actually, this was generated by a particular set of options.  You can control
  107. the formatting of each column, whether you prefer wide or fat, hex or decimal,
  108. leading zeroes or whatever.
  109.  
  110. All you need to be able to use this is a C compiler than generates
  111. BSD/GCC-style stabs.  The -g option on native BSD compilers and GCC
  112. should get this for you.
  113.  
  114. To learn more, just type a bogus option, like -\?, and a long usage message
  115. will be provided.  There are a fair number of possibilities.
  116.  
  117. If you're only a C programmer, than this is the end of the message for you.
  118. You can quit right now, and if you care to, save off the source and run it
  119. when you feel like it.  Or not.
  120.  
  121.  
  122.  
  123. But if you're a perl programmer, then for you I have something much more
  124. wondrous than just a structure offset printer.
  125.  
  126. You see, if you call pstruct by its other incybernation, c2ph, you have a code
  127. generator that translates C code into perl code!  Well, structure and union
  128. declarations at least, but that's quite a bit.
  129.  
  130. Prior to this point, anyone programming in perl who wanted to interact
  131. with C programs, like the kernel, was forced to guess the layouts of
  132. the C strutures, and then hardwire these into his program.  Of course,
  133. when you took your wonderfully crafted program to a system where the
  134. sgtty structure was laid out differently, you program broke.  Which is
  135. a shame.
  136.  
  137. We've had Larry's h2ph translator, which helped, but that only works on
  138. cpp symbols, not real C, which was also very much needed.  What I offer
  139. you is a symbolic way of getting at all the C structures.  I've couched
  140. them in terms of packages and functions.  Consider the following program:
  141.  
  142.     #!/usr/local/bin/perl
  143.  
  144.     require 'syscall.ph';
  145.     require 'sys/time.ph';
  146.     require 'sys/resource.ph';
  147.  
  148.     $ru = "\0" x &rusage'sizeof();
  149.  
  150.     syscall(&SYS_getrusage, &RUSAGE_SELF, $ru)      && die "getrusage: $!";
  151.  
  152.     @ru = unpack($t = &rusage'typedef(), $ru);
  153.  
  154.     $utime =  $ru[ &rusage'ru_utime + &timeval'tv_sec  ]
  155.        + ($ru[ &rusage'ru_utime + &timeval'tv_usec ]) / 1e6;
  156.  
  157.     $stime =  $ru[ &rusage'ru_stime + &timeval'tv_sec  ]
  158.        + ($ru[ &rusage'ru_stime + &timeval'tv_usec ]) / 1e6;
  159.  
  160.     printf "you have used %8.3fs+%8.3fu seconds.\n", $utime, $stime;
  161.  
  162.  
  163. As you see, the name of the package is the name of the structure.  Regular
  164. fields are just their own names.  Plus the follwoing  accessor functions are
  165. provided for your convenience:
  166.  
  167.     struct    This takes no arguments, and is merely the number of first-level
  168.         elements in the structure.  You would use this for indexing
  169.         into arrays of structures, perhaps like this
  170.  
  171.  
  172.             $usec = $u[ &user'u_utimer
  173.                 + (&ITIMER_VIRTUAL * &itimerval'struct)
  174.                 + &itimerval'it_value
  175.                 + &timeval'tv_usec
  176.                   ];
  177.  
  178.     sizeof       Returns the bytes in the structure, or the member if
  179.              you pass it an argument, such as
  180.  
  181.             &rusage'sizeof(&rusage'ru_utime)
  182.  
  183.     typedef      This is the perl format definition for passing to pack and
  184.              unpack.  If you ask for the typedef of a nothing, you get
  185.              the whole structure, otherwise you get that of the member
  186.              you ask for.  Padding is taken care of, as is the magic to
  187.              guarantee that a union is unpacked into all its aliases.
  188.              Bitfields are not quite yet supported however.
  189.  
  190.     offsetof    This function is the byte offset into the array of that
  191.         member.  You may wish to use this for indexing d