CINXE.COM

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

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>c2ph, pstruct - Dump C structures as generated from cc -g -S stabs - Perldoc Browser</title> <link rel="search" href="/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser"> <link rel="canonical" href="https://perldoc.perl.org/c2ph"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/stackoverflow-light.min.css" integrity="sha512-cG1IdFxqipi3gqLmksLtuk13C+hBa57a6zpWxMeoY3Q9O6ooFxq50DayCdm0QrDgZjMUn23z/0PMZlgft7Yp5Q==" crossorigin="anonymous" /> <style> body { background: #f4f4f5; color: #020202; } .navbar-dark { background-image: -webkit-linear-gradient(top, #005f85 0, #002e49 100%); background-image: -o-linear-gradient(top, #005f85 0, #002e49 100%); background-image: linear-gradient(to bottom, #005f85 0, #002e49 100%); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff005f85', endColorstr='#ff002e49', GradientType=0); background-repeat: repeat-x; } .navbar-dark .navbar-nav .nav-link, .navbar-dark .navbar-nav .nav-link:focus { color: #fff } .navbar-dark .navbar-nav .nav-link:hover { color: #ffef68 } #wrapperlicious { margin: 0 auto; font: 0.9em 'Helvetica Neue', Helvetica, sans-serif; font-weight: normal; line-height: 1.5em; margin: 0; padding: 0; } #wrapperlicious h1 { font-size: 1.5em } #wrapperlicious h2 { font-size: 1.3em } #wrapperlicious h3 { font-size: 1.1em } #wrapperlicious h4 { font-size: 0.9em } #wrapperlicious h1, #wrapperlicious h2, #wrapperlicious h3, #wrapperlicious h4, #wrapperlicious dt { color: #020202; margin-top: 1em; margin-bottom: 1em; position: relative; font-weight: bold; } #wrapperlicious a { color: inherit; text-decoration: underline } #wrapperlicious #toc { text-decoration: none } #wrapperlicious a:hover { color: #2a2a2a } #wrapperlicious a img { border: 0 } #wrapperlicious :not(pre) > code { color: inherit; background-color: rgba(0, 0, 0, 0.04); border-radius: 3px; font: 0.9em Consolas, Menlo, Monaco, monospace; padding: 0.3em; } #wrapperlicious dd { margin: 0; margin-left: 2em; } #wrapperlicious dt { color: #2a2a2a; font-weight: bold; margin-left: 0.9em; } #wrapperlicious p { margin-bottom: 1em; margin-top: 1em; } #wrapperlicious li > p { margin-bottom: 0; margin-top: 0; } #wrapperlicious pre { border: 1px solid #c1c1c1; border-radius: 3px; font: 100% Consolas, Menlo, Monaco, monospace; margin-bottom: 1em; margin-top: 1em; } #wrapperlicious pre > code { display: block; background-color: #f6f6f6; font: 0.9em Consolas, Menlo, Monaco, monospace; line-height: 1.5em; text-align: left; white-space: pre; padding: 1em; } #wrapperlicious dl, #wrapperlicious ol, #wrapperlicious ul { margin-bottom: 1em; margin-top: 1em; } #wrapperlicious ul { list-style-type: square; } #wrapperlicious ul ul { margin-bottom: 0px; margin-top: 0px; } #footer { font-size: 0.8em; padding-top: 0.5em; text-align: center; } #more { display: inline; font-size: 0.8em; } #perldocdiv { background-color: #fff; border: 1px solid #c1c1c1; border-bottom-left-radius: 5px; border-bottom-right-radius: 5px; margin-left: auto; margin-right: auto; padding: 3em; padding-top: 1em; max-width: 960px; } #moduleversion { float: right } #wrapperlicious .leading-notice { font-style: italic; padding-left: 1em; margin-top: 1em; margin-bottom: 1em; } #wrapperlicious .permalink { display: none; left: -0.75em; position: absolute; padding-right: 0.25em; text-decoration: none; } #wrapperlicious h1:hover .permalink, #wrapperlicious h2:hover .permalink, #wrapperlicious h3:hover .permalink, #wrapperlicious h4:hover .permalink, #wrapperlicious dt:hover .permalink { display: block; } </style> <!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-KVNWBNT5FB"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-KVNWBNT5FB'); gtag('config', 'UA-50555-3'); </script> </head> <body> <nav class="navbar navbar-expand-md navbar-dark bg-dark justify-content-between"> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <a class="navbar-brand" href="/"><img src="/images/perl_camel_30.png" width="30" height="30" class="d-inline-block align-top" alt="Perl Camel Logo"> Perldoc Browser</a> <div class="collapse navbar-collapse" id="navbarNav"> <ul class="navbar-nav mr-auto"> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-stable" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">5.18.0</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-stable"> <a class="dropdown-item" href="/c2ph">Latest</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1/c2ph">5.40.1</a> <a class="dropdown-item" href="/5.40.0/c2ph">5.40.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3/c2ph">5.38.3</a> <a class="dropdown-item" href="/5.38.2/c2ph">5.38.2</a> <a class="dropdown-item" href="/5.38.1/c2ph">5.38.1</a> <a class="dropdown-item" href="/5.38.0/c2ph">5.38.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.36.3/c2ph">5.36.3</a> <a class="dropdown-item" href="/5.36.2/c2ph">5.36.2</a> <a class="dropdown-item" href="/5.36.1/c2ph">5.36.1</a> <a class="dropdown-item" href="/5.36.0/c2ph">5.36.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.34.3/c2ph">5.34.3</a> <a class="dropdown-item" href="/5.34.2/c2ph">5.34.2</a> <a class="dropdown-item" href="/5.34.1/c2ph">5.34.1</a> <a class="dropdown-item" href="/5.34.0/c2ph">5.34.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.32.1/c2ph">5.32.1</a> <a class="dropdown-item" href="/5.32.0/c2ph">5.32.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.30.3/c2ph">5.30.3</a> <a class="dropdown-item" href="/5.30.2/c2ph">5.30.2</a> <a class="dropdown-item" href="/5.30.1/c2ph">5.30.1</a> <a class="dropdown-item" href="/5.30.0/c2ph">5.30.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.28.3/c2ph">5.28.3</a> <a class="dropdown-item" href="/5.28.2/c2ph">5.28.2</a> <a class="dropdown-item" href="/5.28.1/c2ph">5.28.1</a> <a class="dropdown-item" href="/5.28.0/c2ph">5.28.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.26.3/c2ph">5.26.3</a> <a class="dropdown-item" href="/5.26.2/c2ph">5.26.2</a> <a class="dropdown-item" href="/5.26.1/c2ph">5.26.1</a> <a class="dropdown-item" href="/5.26.0/c2ph">5.26.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.24.4/c2ph">5.24.4</a> <a class="dropdown-item" href="/5.24.3/c2ph">5.24.3</a> <a class="dropdown-item" href="/5.24.2/c2ph">5.24.2</a> <a class="dropdown-item" href="/5.24.1/c2ph">5.24.1</a> <a class="dropdown-item" href="/5.24.0/c2ph">5.24.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.22.4/c2ph">5.22.4</a> <a class="dropdown-item" href="/5.22.3/c2ph">5.22.3</a> <a class="dropdown-item" href="/5.22.2/c2ph">5.22.2</a> <a class="dropdown-item" href="/5.22.1/c2ph">5.22.1</a> <a class="dropdown-item" href="/5.22.0/c2ph">5.22.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.20.3/c2ph">5.20.3</a> <a class="dropdown-item" href="/5.20.2/c2ph">5.20.2</a> <a class="dropdown-item" href="/5.20.1/c2ph">5.20.1</a> <a class="dropdown-item" href="/5.20.0/c2ph">5.20.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.18.4/c2ph">5.18.4</a> <a class="dropdown-item" href="/5.18.3/c2ph">5.18.3</a> <a class="dropdown-item" href="/5.18.2/c2ph">5.18.2</a> <a class="dropdown-item" href="/5.18.1/c2ph">5.18.1</a> <a class="dropdown-item active" href="/5.18.0/c2ph">5.18.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.16.3/c2ph">5.16.3</a> <a class="dropdown-item" href="/5.16.2/c2ph">5.16.2</a> <a class="dropdown-item" href="/5.16.1/c2ph">5.16.1</a> <a class="dropdown-item" href="/5.16.0/c2ph">5.16.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.14.4/c2ph">5.14.4</a> <a class="dropdown-item" href="/5.14.3/c2ph">5.14.3</a> <a class="dropdown-item" href="/5.14.2/c2ph">5.14.2</a> <a class="dropdown-item" href="/5.14.1/c2ph">5.14.1</a> <a class="dropdown-item" href="/5.14.0/c2ph">5.14.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.12.5/c2ph">5.12.5</a> <a class="dropdown-item" href="/5.12.4/c2ph">5.12.4</a> <a class="dropdown-item" href="/5.12.3/c2ph">5.12.3</a> <a class="dropdown-item" href="/5.12.2/c2ph">5.12.2</a> <a class="dropdown-item" href="/5.12.1/c2ph">5.12.1</a> <a class="dropdown-item" href="/5.12.0/c2ph">5.12.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.10.1/c2ph">5.10.1</a> <a class="dropdown-item" href="/5.10.0/c2ph">5.10.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.8.9/c2ph">5.8.9</a> <a class="dropdown-item" href="/5.8.8/c2ph">5.8.8</a> <a class="dropdown-item" href="/5.8.7/c2ph">5.8.7</a> <a class="dropdown-item" href="/5.8.6/c2ph">5.8.6</a> <a class="dropdown-item" href="/5.8.5/c2ph">5.8.5</a> <a class="dropdown-item" href="/5.8.4/c2ph">5.8.4</a> <a class="dropdown-item" href="/5.8.3/c2ph">5.8.3</a> <a class="dropdown-item" href="/5.8.2/c2ph">5.8.2</a> <a class="dropdown-item" href="/5.8.1/c2ph">5.8.1</a> <a class="dropdown-item" href="/5.8.0/c2ph">5.8.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.6.2/c2ph">5.6.2</a> <a class="dropdown-item" href="/5.6.1/c2ph">5.6.1</a> <a class="dropdown-item" href="/5.6.0/c2ph">5.6.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.005_04/c2ph">5.005_04</a> <a class="dropdown-item" href="/5.005_03/c2ph">5.005_03</a> <a class="dropdown-item" href="/5.005_02/c2ph">5.005_02</a> <a class="dropdown-item" href="/5.005_01/c2ph">5.005_01</a> <a class="dropdown-item" href="/5.005/c2ph">5.005</a> </div> </li> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-dev" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dev</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-dev"> <a class="dropdown-item" href="/blead/c2ph">blead</a> <a class="dropdown-item" href="/5.41.9/c2ph">5.41.9</a> <a class="dropdown-item" href="/5.41.8/c2ph">5.41.8</a> <a class="dropdown-item" href="/5.41.7/c2ph">5.41.7</a> <a class="dropdown-item" href="/5.41.6/c2ph">5.41.6</a> <a class="dropdown-item" href="/5.41.5/c2ph">5.41.5</a> <a class="dropdown-item" href="/5.41.4/c2ph">5.41.4</a> <a class="dropdown-item" href="/5.41.3/c2ph">5.41.3</a> <a class="dropdown-item" href="/5.41.2/c2ph">5.41.2</a> <a class="dropdown-item" href="/5.41.1/c2ph">5.41.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1-RC1/c2ph">5.40.1-RC1</a> <a class="dropdown-item" href="/5.40.0-RC2/c2ph">5.40.0-RC2</a> <a class="dropdown-item" href="/5.40.0-RC1/c2ph">5.40.0-RC1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.39.10/c2ph">5.39.10</a> <a class="dropdown-item" href="/5.39.9/c2ph">5.39.9</a> <a class="dropdown-item" href="/5.39.8/c2ph">5.39.8</a> <a class="dropdown-item" href="/5.39.7/c2ph">5.39.7</a> <a class="dropdown-item" href="/5.39.6/c2ph">5.39.6</a> <a class="dropdown-item" href="/5.39.5/c2ph">5.39.5</a> <a class="dropdown-item" href="/5.39.4/c2ph">5.39.4</a> <a class="dropdown-item" href="/5.39.3/c2ph">5.39.3</a> <a class="dropdown-item" href="/5.39.2/c2ph">5.39.2</a> <a class="dropdown-item" href="/5.39.1/c2ph">5.39.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3-RC1/c2ph">5.38.3-RC1</a> </div> </li> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-nav" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Documentation</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-nav"> <a class="dropdown-item" href="/5.18.0/perl">Perl</a> <a class="dropdown-item" href="/5.18.0/perlintro">Intro</a> <a class="dropdown-item" href="/5.18.0/perl#Tutorials">Tutorials</a> <a class="dropdown-item" href="/5.18.0/perlfaq">FAQs</a> <a class="dropdown-item" href="/5.18.0/perl#Reference-Manual">Reference</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.18.0/perlop">Operators</a> <a class="dropdown-item" href="/5.18.0/functions">Functions</a> <a class="dropdown-item" href="/5.18.0/variables">Variables</a> <a class="dropdown-item" href="/5.18.0/modules">Modules</a> <a class="dropdown-item" href="/5.18.0/perlutil">Utilities</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.18.0/perldelta">Release Notes</a> <a class="dropdown-item" href="/5.18.0/perlcommunity">Community</a> <a class="dropdown-item" href="/5.18.0/perlhist">History</a> </div> </li> </ul> <ul class="navbar-nav"> <script> function set_expand (expand) { var perldocdiv = document.getElementById('perldocdiv'); var width = window.getComputedStyle(perldocdiv).getPropertyValue('max-width'); var expanded = (width == '' || width == 'none') ? true : false; if (expand === null) { expand = !expanded; } if ((expand && !expanded) || (!expand && expanded)) { perldocdiv.style.setProperty('max-width', expand ? 'none' : '960px'); var button_classlist = document.getElementById('content-expand-button').classList; if (expand) { button_classlist.add('btn-light'); button_classlist.remove('btn-outline-light'); } else { button_classlist.add('btn-outline-light'); button_classlist.remove('btn-light'); } } return expand; } function toggle_expand () { var expand = set_expand(null); document.cookie = 'perldoc_expand=' + (expand ? 1 : 0) + '; path=/; expires=Tue, 19 Jan 2038 03:14:07 UTC'; } function read_expand () { return document.cookie.split(';').some(function (item) { return item.indexOf('perldoc_expand=1') >= 0 }); } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { if (read_expand()) { set_expand(true); } }); } else if (read_expand()) { set_expand(true); } </script> <button id="content-expand-button" type="button" class="btn btn-outline-light d-none d-lg-inline-block mr-4" onclick="toggle_expand()">Expand</button> </ul> <form class="form-inline" method="get" action="/5.18.0/search"> <input class="form-control mr-3" type="search" name="q" placeholder="Search" aria-label="Search" value=""> </form> </div> </nav> <div id="wrapperlicious" class="container-fluid"> <div id="perldocdiv"> <div id="links"> <a href="/5.18.0/c2ph">c2ph</a> <div id="more"> (<a href="/5.18.0/c2ph.txt">source</a>, <a href="https://metacpan.org/pod/c2ph">CPAN</a>) </div> </div> <div class="leading-notice"> You are viewing the version of this documentation from Perl 5.18.0. <a href="/c2ph">View the latest version</a> </div> <h1><a id="toc">CONTENTS</a></h1> <ul> <li> <a class="text-decoration-none" href="#NAME">NAME</a> </li> <li> <a class="text-decoration-none" href="#SYNOPSIS">SYNOPSIS</a> <ul> <li> <a class="text-decoration-none" href="#OPTIONS">OPTIONS</a> </li> </ul> </li> <li> <a class="text-decoration-none" href="#DESCRIPTION">DESCRIPTION</a> </li> </ul> <h1 id="NAME"><a class="permalink" href="#NAME">#</a>NAME</h1> <p>c2ph, pstruct - Dump C structures as generated from <code>cc -g -S</code> stabs</p> <h1 id="SYNOPSIS"><a class="permalink" href="#SYNOPSIS">#</a>SYNOPSIS</h1> <pre><code class="plaintext">c2ph [-dpnP] [var=val] [files ...]</code></pre> <h2 id="OPTIONS"><a class="permalink" href="#OPTIONS">#</a>OPTIONS</h2> <pre><code class="plaintext">Options: -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</code></pre> <h1 id="DESCRIPTION"><a class="permalink" href="#DESCRIPTION">#</a>DESCRIPTION</h1> <p>The following is the old c2ph.doc documentation by Tom Christiansen &lt;tchrist@perl.com&gt; Date: 25 Jul 91 08:10:21 GMT</p> <p>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.</p> <p>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.</p> <p>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&#39;s still not a pretty program, but at least it&#39;s more robust.</p> <p>Pstruct takes any .c or .h files, or preferably .s ones, since that&#39;s the format it is going to massage them into anyway, and spits out listings like this:</p> <pre><code>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</code></pre> <p>etc.</p> <p>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.</p> <p>All you need to be able to use this is a C compiler than generates BSD/GCC-style stabs. The <b>-g</b> option on native BSD compilers and GCC should get this for you.</p> <p>To learn more, just type a bogus option, like <b>-\?</b>, and a long usage message will be provided. There are a fair number of possibilities.</p> <p>If you&#39;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.</p> <p>But if you&#39;re a perl programmer, then for you I have something much more wondrous than just a structure offset printer.</p> <p>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&#39;s quite a bit.</p> <p>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.</p> <p>We&#39;ve had Larry&#39;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&#39;ve couched them in terms of packages and functions. Consider the following program:</p> <pre><code> #!/usr/local/bin/perl require &#39;syscall.ph&#39;; require &#39;sys/time.ph&#39;; require &#39;sys/resource.ph&#39;; $ru = &quot;\0&quot; x &amp;rusage&#39;sizeof(); syscall(&amp;SYS_getrusage, &amp;RUSAGE_SELF, $ru) &amp;&amp; die &quot;getrusage: $!&quot;; @ru = unpack($t = &amp;rusage&#39;typedef(), $ru); $utime = $ru[ &amp;rusage&#39;ru_utime + &amp;timeval&#39;tv_sec ] + ($ru[ &amp;rusage&#39;ru_utime + &amp;timeval&#39;tv_usec ]) / 1e6; $stime = $ru[ &amp;rusage&#39;ru_stime + &amp;timeval&#39;tv_sec ] + ($ru[ &amp;rusage&#39;ru_stime + &amp;timeval&#39;tv_usec ]) / 1e6; printf &quot;you have used %8.3fs+%8.3fu seconds.\n&quot;, $utime, $stime;</code></pre> <p>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:</p> <pre><code> 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[ &amp;user&#39;u_utimer + (&amp;ITIMER_VIRTUAL * &amp;itimerval&#39;struct) + &amp;itimerval&#39;it_value + &amp;timeval&#39;tv_usec ]; sizeof Returns the bytes in the structure, or the member if you pass it an argument, such as &amp;rusage&#39;sizeof(&amp;rusage&#39;ru_utime) 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. 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&#39;re too lazy to unpack it. 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&#39;ll post such a thing to dump out your u structure for you.</code></pre> <p>The way I see this being used is like basically this:</p> <pre><code class="plaintext">% h2ph &lt;some_include_file.h &gt; /usr/lib/perl/tmp.ph % c2ph some_include_file.h &gt;&gt; /usr/lib/perl/tmp.ph % install</code></pre> <p>It&#39;s a little tricker with c2ph because you have to get the includes right. I can&#39;t know this for your system, but it&#39;s not usually too terribly difficult.</p> <p>The code isn&#39;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&#39;t have to divine the makeup of the stabs on the fly, and then account for micro differences between my compiler and gcc.</p> <p>Anyway, here it is. Should run on perl v4 or greater. Maybe less.</p> <pre><code class="plaintext">--tom</code></pre> </div> <div id="footer"> <p>Perldoc Browser is maintained by Dan Book (<a href="https://metacpan.org/author/DBOOK">DBOOK</a>). Please contact him via the <a href="https://github.com/Grinnz/perldoc-browser/issues">GitHub issue tracker</a> or <a href="mailto:dbook@cpan.org">email</a> regarding any issues with the site itself, search, or rendering of documentation.</p> <p>The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via the <a href="https://github.com/Perl/perl5/issues">Perl issue tracker</a>, the <a href="https://lists.perl.org/list/perl5-porters.html">mailing list</a>, or <a href="https://kiwiirc.com/client/irc.perl.org/p5p">IRC</a> to report any issues with the contents or format of the documentation.</p> </div> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js" integrity="sha512-/DXTXr6nQodMUiq+IUJYCt2PPOUjrHJ9wFrqpJ3XkgPNOZVfMok7cRw6CSxyCQxXn6ozlESsSh1/sMCTF1rL/g==" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js" integrity="sha512-ubuT8Z88WxezgSqf3RLuNi5lmjstiJcyezx34yIU2gAHonIi27Na7atqzUZCOoY4CExaoFumzOsFQ2Ch+I/HCw==" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script> <script src="/js/highlight.pack.js"></script> <script>hljs.highlightAll();</script> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10