Welcome to Linux Knowledge Base and Tutorial
"The place where you learn linux"
The ONE Campaign to make poverty history

 Create an AccountHome | Submit News | Your Account  

Tutorial Menu
Linux Tutorial Home
Table of Contents

· Introduction to Operating Systems
· Linux Basics
· Working with the System
· Shells and Utilities
· Editing Files
· Basic Administration
· The Operating System
· The X Windowing System
· The Computer Itself
· Networking
· System Monitoring
· Solving Problems
· Security
· Installing and Upgrading
· Linux and Windows

Man Pages
Linux Topics
Test Your Knowledge

Site Menu
Site Map
Copyright Info
Terms of Use
Privacy Info
Masthead / Impressum
Your Account

Private Messages

News Archive
Submit News
User Articles
Web Links


The Web

Who's Online
There are currently, 54 guest(s) and 0 member(s) that are online.

You are an Anonymous user. You can register for free by clicking here




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



           -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

           -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

           -i  do NOT recompute sizes for intrinsic datatypes
           -a  dump information on intrinsics also

           -t  trace execution
           -d  spew reams of debugging output

           -slist  give comma-separated list a structures to dump


       The following is the old c2ph.doc documentation by Tom
       Christiansen <tchrist@perl.com> Date: 25 Jul 91 08:10:21

       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 espe­
       cially useful for people looking at binary dumps or poking
       around the kernel.

       Pstruct was not a pretty program.  Neither was it particu­
       larly robust.  The problem, you see, was that the C com­
       piler was much better at parsing C than I could ever hope
       to be.

       So I got smart:  I decided to be lazy and let the C com­
       piler 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.

       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:

            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


       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.

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

       To learn more, just type a bogus option, like -\?, and a
       long usage message will 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 it when you feel like it.
       Or not.

       But if you're a perl programmer, then for you I have some­
       thing much more wondrous than just a structure offset

       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 get­
       ting at all the C structures.  I've couched them in terms
       of packages and functions.  Consider the following pro­


           require 'syscall.ph';
           require 'sys/time.ph';
           require 'sys/resource.ph';

           $ru = "\0" x &rusage'sizeof();

           syscall(&SYS_getrusage, &RUSAGE_SELF, $ru)      && die "getrusage: $!";

           @ru = unpack($t = &rusage'typedef(), $ru);

           $utime =  $ru[ &rusage'ru_utime + &timeval'tv_sec  ]
                  + ($ru[ &rusage'ru_utime + &timeval'tv_usec ]) / 1e6;

           $stime =  $ru[ &rusage'ru_stime + &timeval'tv_sec  ]
                  + ($ru[ &rusage'ru_stime + &timeval'tv_usec ]) / 1e6;

           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.  Regular fields are just their own names.  Plus
       the following accessor functions are provided for your

           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

                           $usec = $u[ &user'u_utimer
                                       + (&ITIMER_VIRTUAL * &itimerval'struct)
                                       + &itimerval'it_value
                                       + &timeval'tv_usec

           sizeof      Returns the bytes in the structure, or the member if
                       you pass it an argument, such as


           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

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

               % h2ph <some_include_file.h  >  /usr/lib/perl/tmp.ph
               % c2ph  some_include_file.h  >> /usr/lib/perl/tmp.ph
               % 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 have been less cav­
       alier 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

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


perl v5.8.1                 2003-09-23                    C2PH(1)
Show your Support for the Linux Tutorial

Purchase one of the products from our new online shop. For each product you purchase, the Linux Tutorial gets a portion of the proceeds to help keep us going.



Security Code
Security Code
Type Security Code

Don't have an account yet? You can create one. As a registered user you have some advantages like theme manager, comments configuration and post comments with your name.

Help if you can!

Amazon Wish List

Did You Know?
You can choose larger fonts by selecting a different themes.


Tell a Friend About Us

Bookmark and Share

Web site powered by PHP-Nuke

Is this information useful? At the very least you can help by spreading the word to your favorite newsgroups, mailing lists and forums.
All logos and trademarks in this site are property of their respective owner. The comments are property of their posters. Articles are the property of their respective owners. Unless otherwise stated in the body of the article, article content (C) 1994-2013 by James Mohr. All rights reserved. The stylized page/paper, as well as the terms "The Linux Tutorial", "The Linux Server Tutorial", "The Linux Knowledge Base and Tutorial" and "The place where you learn Linux" are service marks of James Mohr. All rights reserved.
The Linux Knowledge Base and Tutorial may contain links to sites on the Internet, which are owned and operated by third parties. The Linux Tutorial is not responsible for the content of any such third-party site. By viewing/utilizing this web site, you have agreed to our disclaimer, terms of use and privacy policy. Use of automated download software ("harvesters") such as wget, httrack, etc. causes the site to quickly exceed its bandwidth limitation and are therefore expressly prohibited. For more details on this, take a look here

PHP-Nuke Copyright © 2004 by Francisco Burzi. This is free software, and you may redistribute it under the GPL. PHP-Nuke comes with absolutely no warranty, for details, see the license.
Page Generation: 0.09 Seconds