Cari di Perl 
    Perl User Manual
Daftar Isi
(Sebelumnya) Awk to Perl translatorQuery or change configuration ... (Berikutnya)
Utilities

Dump C structures as generated from cc -g -S stabs

Daftar Isi

NAME

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

SYNOPSIS

  1. c2ph [-dpnP] [var=val] [files ...]

OPTIONS

  1. Options:
  2. -wwide; short for: type_width=45 member_width=35 offset_width=8
  3. -xhex; short for: offset_fmt=x offset_width=08 size_fmt=x size_width=04
  4. -ndo not generate perl code (default when invoked as pstruct)
  5. -pgenerate perl code (default when invoked as c2ph)
  6. -vgenerate perl code, with C decls as comments
  7. -ido NOT recompute sizes for intrinsic datatypes
  8. -adump information on intrinsics also
  9. -ttrace execution
  10. -dspew reams of debugging output
  11. -slist give comma-separated list a structures to dump

DESCRIPTION

The following is the old c2ph.doc documentation by Tom Christiansen<[email protected]>Date: 25 Jul 91 08:10:21 GMT

Once upon a time, I wrote a program called pstruct. It was a perlprogram that tried to parse out C structures and display their memberoffsets for you. This was especially useful for people looking atbinary dumps or poking around the kernel.

Pstruct was not a pretty program. Neither was it particularly robust.The problem, you see, was that the C compiler was much better at parsingC than I could ever hope to be.

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 mucheasier to parse. It's still not a pretty program, but at least it's morerobust.

Pstruct takes any .c or .h files, or preferably .s ones, since that'sthe format it is going to massage them into anyway, and spits outlistings like this:

  1. struct tty {
  2. int tty.t_locker 000 4
  3. int tty.t_mutex_index 004 4
  4. struct tty * tty.t_tp_virt 008 4
  5. struct clist tty.t_rawq 00c 20
  6. int tty.t_rawq.c_cc 00c 4
  7. int tty.t_rawq.c_cmax 010 4
  8. int tty.t_rawq.c_cfx 014 4
  9. int tty.t_rawq.c_clx 018 4
  10. struct tty * tty.t_rawq.c_tp_cpu 01c 4
  11. struct tty * tty.t_rawq.c_tp_iop 020 4
  12. unsigned char * tty.t_rawq.c_buf_cpu 024 4
  13. unsigned char * tty.t_rawq.c_buf_iop 028 4
  14. struct clist tty.t_canq 02c 20
  15. int tty.t_canq.c_cc 02c 4
  16. int tty.t_canq.c_cmax 030 4
  17. int tty.t_canq.c_cfx 034 4
  18. int tty.t_canq.c_clx 038 4
  19. struct tty * tty.t_canq.c_tp_cpu 03c 4
  20. struct tty * tty.t_canq.c_tp_iop 040 4
  21. unsigned char * tty.t_canq.c_buf_cpu 044 4
  22. unsigned char * tty.t_canq.c_buf_iop 048 4
  23. struct clist tty.t_outq 04c 20
  24. int tty.t_outq.c_cc 04c 4
  25. int tty.t_outq.c_cmax 050 4
  26. int tty.t_outq.c_cfx 054 4
  27. int tty.t_outq.c_clx 058 4
  28. struct tty * tty.t_outq.c_tp_cpu 05c 4
  29. struct tty * tty.t_outq.c_tp_iop 060 4
  30. unsigned char * tty.t_outq.c_buf_cpu 064 4
  31. unsigned char * tty.t_outq.c_buf_iop 068 4
  32. (*int)() tty.t_oproc_cpu 06c 4
  33. (*int)() tty.t_oproc_iop 070 4
  34. (*int)() tty.t_stopproc_cpu 074 4
  35. (*int)() tty.t_stopproc_iop 078 4
  36. struct thread * tty.t_rsel 07c 4

etc.

Actually, this was generated by a particular set of options. You can controlthe formatting of each column, whether you prefer wide or fat, hex or decimal,leading zeroes or whatever.

All you need to be able to use this is a C compiler than generatesBSD/GCC-style stabs. The -g option on native BSD compilers and GCCshould get this for you.

To learn more, just type a bogus option, like -\?, and a long usage messagewill be provided. There are a fair number of possibilities.

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 itwhen you feel like it. Or not.

But if you're a perl programmer, then for you I have something much morewondrous than just a structure offset printer.

You see, if you call pstruct by its other incybernation, c2ph, you have a codegenerator that translates C code into perl code! Well, structure and uniondeclarations at least, but that's quite a bit.

Prior to this point, anyone programming in perl who wanted to interactwith C programs, like the kernel, was forced to guess the layouts ofthe C structures, and then hardwire these into his program. Of course,when you took your wonderfully crafted program to a system where thesgtty structure was laid out differently, your program broke. Which isa shame.

We've had Larry's h2ph translator, which helped, but that only works oncpp symbols, not real C, which was also very much needed. What I offeryou is a symbolic way of getting at all the C structures. I've couchedthem in terms of packages and functions. Consider the following program:

  1. #!/usr/local/bin/perl
  2. require 'syscall.ph';
  3. require 'sys/time.ph';
  4. require 'sys/resource.ph';
  5. $ru = "\0" x &rusage'sizeof();
  6. syscall(&SYS_getrusage, &RUSAGE_SELF, $ru) && die "getrusage: $!";
  7. @ru = unpack($t = &rusage'typedef(), $ru);
  8. $utime = $ru[ &rusage'ru_utime + &timeval'tv_sec ]
  9. + ($ru[ &rusage'ru_utime + &timeval'tv_usec ]) / 1e6;
  10. $stime = $ru[ &rusage'ru_stime + &timeval'tv_sec ]
  11. + ($ru[ &rusage'ru_stime + &timeval'tv_usec ]) / 1e6;
  12. printf "you have used %8.3fs+%8.3fu seconds.\n", $utime, $stime;

As you see, the name of the package is the name of the structure. Regularfields are just their own names. Plus the following accessor functions areprovided for your convenience:

  1. structThis takes no arguments, and is merely the number of first-level
  2. elements in the structure. You would use this for indexing
  3. into arrays of structures, perhaps like this
  4. $usec = $u[ &user'u_utimer
  5. + (&ITIMER_VIRTUAL * &itimerval'struct)
  6. + &itimerval'it_value
  7. + &timeval'tv_usec
  8. ];
  9. sizeof Returns the bytes in the structure, or the member if
  10. you pass it an argument, such as
  11. &rusage'sizeof(&rusage'ru_utime)
  12. typedef This is the perl format definition for passing to pack and
  13. unpack. If you ask for the typedef of a nothing, you get
  14. the whole structure, otherwise you get that of the member
  15. you ask for. Padding is taken care of, as is the magic to
  16. guarantee that a union is unpacked into all its aliases.
  17. Bitfields are not quite yet supported however.
  18. offsetofThis function is the byte offset into the array of that
  19. member. You may wish to use this for indexing directly
  20. into the packed structure with vec() if you're too lazy
  21. to unpack it.
  22. typeofNot to be confused with the typedef accessor function, this
  23. one returns the C type of that field. This would allow
  24. you to print out a nice structured pretty print of some
  25. structure without knoning anything about it beforehand.
  26. No args to this one is a noop. Someday I'll post such
  27. a thing to dump out your u structure for you.

The way I see this being used is like basically this:

  1. % h2ph <some_include_file.h > /usr/lib/perl/tmp.ph
  2. % c2ph some_include_file.h >> /usr/lib/perl/tmp.ph
  3. % install

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.

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 havebeen less cavalier in how the parts of the program communicated with eachother, etc. It might also have helped if I didn't have to divine the makeupof the stabs on the fly, and then account for micro differences between mycompiler and gcc.

Anyway, here it is. Should run on perl v4 or greater. Maybe less.

  1. --tom
 
Source : perldoc.perl.org - Official documentation for the Perl programming language
Site maintained by Jon Allen (JJ)     See the project page for more details
Documentation maintained by the Perl 5 Porters
(Sebelumnya) Awk to Perl translatorQuery or change configuration ... (Berikutnya)