| 1 | .\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13 |
| 2 | .\" |
| 3 | .\" Standard preamble: |
| 4 | .\" ======================================================================== |
| 5 | .de Sh \" Subsection heading |
| 6 | .br |
| 7 | .if t .Sp |
| 8 | .ne 5 |
| 9 | .PP |
| 10 | \fB\\$1\fR |
| 11 | .PP |
| 12 | .. |
| 13 | .de Sp \" Vertical space (when we can't use .PP) |
| 14 | .if t .sp .5v |
| 15 | .if n .sp |
| 16 | .. |
| 17 | .de Vb \" Begin verbatim text |
| 18 | .ft CW |
| 19 | .nf |
| 20 | .ne \\$1 |
| 21 | .. |
| 22 | .de Ve \" End verbatim text |
| 23 | .ft R |
| 24 | .fi |
| 25 | .. |
| 26 | .\" Set up some character translations and predefined strings. \*(-- will |
| 27 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left |
| 28 | .\" double quote, and \*(R" will give a right double quote. | will give a |
| 29 | .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to |
| 30 | .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' |
| 31 | .\" expand to `' in nroff, nothing in troff, for use with C<>. |
| 32 | .tr \(*W-|\(bv\*(Tr |
| 33 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' |
| 34 | .ie n \{\ |
| 35 | . ds -- \(*W- |
| 36 | . ds PI pi |
| 37 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch |
| 38 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch |
| 39 | . ds L" "" |
| 40 | . ds R" "" |
| 41 | . ds C` "" |
| 42 | . ds C' "" |
| 43 | 'br\} |
| 44 | .el\{\ |
| 45 | . ds -- \|\(em\| |
| 46 | . ds PI \(*p |
| 47 | . ds L" `` |
| 48 | . ds R" '' |
| 49 | 'br\} |
| 50 | .\" |
| 51 | .\" If the F register is turned on, we'll generate index entries on stderr for |
| 52 | .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index |
| 53 | .\" entries marked with X<> in POD. Of course, you'll have to process the |
| 54 | .\" output yourself in some meaningful fashion. |
| 55 | .if \nF \{\ |
| 56 | . de IX |
| 57 | . tm Index:\\$1\t\\n%\t"\\$2" |
| 58 | .. |
| 59 | . nr % 0 |
| 60 | . rr F |
| 61 | .\} |
| 62 | .\" |
| 63 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 64 | .\" way too many mistakes in technical documents. |
| 65 | .hy 0 |
| 66 | .if n .na |
| 67 | .\" |
| 68 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). |
| 69 | .\" Fear. Run. Save yourself. No user-serviceable parts. |
| 70 | . \" fudge factors for nroff and troff |
| 71 | .if n \{\ |
| 72 | . ds #H 0 |
| 73 | . ds #V .8m |
| 74 | . ds #F .3m |
| 75 | . ds #[ \f1 |
| 76 | . ds #] \fP |
| 77 | .\} |
| 78 | .if t \{\ |
| 79 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) |
| 80 | . ds #V .6m |
| 81 | . ds #F 0 |
| 82 | . ds #[ \& |
| 83 | . ds #] \& |
| 84 | .\} |
| 85 | . \" simple accents for nroff and troff |
| 86 | .if n \{\ |
| 87 | . ds ' \& |
| 88 | . ds ` \& |
| 89 | . ds ^ \& |
| 90 | . ds , \& |
| 91 | . ds ~ ~ |
| 92 | . ds / |
| 93 | .\} |
| 94 | .if t \{\ |
| 95 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" |
| 96 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' |
| 97 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' |
| 98 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' |
| 99 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' |
| 100 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' |
| 101 | .\} |
| 102 | . \" troff and (daisy-wheel) nroff accents |
| 103 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' |
| 104 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' |
| 105 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] |
| 106 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' |
| 107 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' |
| 108 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] |
| 109 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] |
| 110 | .ds ae a\h'-(\w'a'u*4/10)'e |
| 111 | .ds Ae A\h'-(\w'A'u*4/10)'E |
| 112 | . \" corrections for vroff |
| 113 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' |
| 114 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' |
| 115 | . \" for low resolution devices (crt and lpr) |
| 116 | .if \n(.H>23 .if \n(.V>19 \ |
| 117 | \{\ |
| 118 | . ds : e |
| 119 | . ds 8 ss |
| 120 | . ds o a |
| 121 | . ds d- d\h'-1'\(ga |
| 122 | . ds D- D\h'-1'\(hy |
| 123 | . ds th \o'bp' |
| 124 | . ds Th \o'LP' |
| 125 | . ds ae ae |
| 126 | . ds Ae AE |
| 127 | .\} |
| 128 | .rm #[ #] #H #V #F C |
| 129 | .\" ======================================================================== |
| 130 | .\" |
| 131 | .IX Title "C2PH 1" |
| 132 | .TH C2PH 1 "2002-08-28" "perl v5.8.0" "Perl Programmers Reference Guide" |
| 133 | .SH "NAME" |
| 134 | c2ph, pstruct \- Dump C structures as generated from \f(CW\*(C`cc \-g \-S\*(C'\fR stabs |
| 135 | .SH "SYNOPSIS" |
| 136 | .IX Header "SYNOPSIS" |
| 137 | .Vb 1 |
| 138 | \& c2ph [-dpnP] [var=val] [files ...] |
| 139 | .Ve |
| 140 | .Sh "\s-1OPTIONS\s0" |
| 141 | .IX Subsection "OPTIONS" |
| 142 | .Vb 1 |
| 143 | \& Options: |
| 144 | .Ve |
| 145 | .PP |
| 146 | .Vb 2 |
| 147 | \& -w wide; short for: type_width=45 member_width=35 offset_width=8 |
| 148 | \& -x hex; short for: offset_fmt=x offset_width=08 size_fmt=x size_width=04 |
| 149 | .Ve |
| 150 | .PP |
| 151 | .Vb 3 |
| 152 | \& -n do not generate perl code (default when invoked as pstruct) |
| 153 | \& -p generate perl code (default when invoked as c2ph) |
| 154 | \& -v generate perl code, with C decls as comments |
| 155 | .Ve |
| 156 | .PP |
| 157 | .Vb 2 |
| 158 | \& -i do NOT recompute sizes for intrinsic datatypes |
| 159 | \& -a dump information on intrinsics also |
| 160 | .Ve |
| 161 | .PP |
| 162 | .Vb 2 |
| 163 | \& -t trace execution |
| 164 | \& -d spew reams of debugging output |
| 165 | .Ve |
| 166 | .PP |
| 167 | .Vb 1 |
| 168 | \& -slist give comma-separated list a structures to dump |
| 169 | .Ve |
| 170 | .SH "DESCRIPTION" |
| 171 | .IX Header "DESCRIPTION" |
| 172 | The following is the old c2ph.doc documentation by Tom Christiansen |
| 173 | <tchrist@perl.com> |
| 174 | Date: 25 Jul 91 08:10:21 \s-1GMT\s0 |
| 175 | .PP |
| 176 | Once upon a time, I wrote a program called pstruct. It was a perl |
| 177 | program that tried to parse out C structures and display their member |
| 178 | offsets for you. This was especially useful for people looking at |
| 179 | binary dumps or poking around the kernel. |
| 180 | .PP |
| 181 | Pstruct was not a pretty program. Neither was it particularly robust. |
| 182 | The problem, you see, was that the C compiler was much better at parsing |
| 183 | C than I could ever hope to be. |
| 184 | .PP |
| 185 | So I got smart: I decided to be lazy and let the C compiler parse the C, |
| 186 | which would spit out debugger stabs for me to read. These were much |
| 187 | easier to parse. It's still not a pretty program, but at least it's more |
| 188 | robust. |
| 189 | .PP |
| 190 | Pstruct takes any .c or .h files, or preferably .s ones, since that's |
| 191 | the format it is going to massage them into anyway, and spits out |
| 192 | listings like this: |
| 193 | .PP |
| 194 | .Vb 36 |
| 195 | \& struct tty { |
| 196 | \& int tty.t_locker 000 4 |
| 197 | \& int tty.t_mutex_index 004 4 |
| 198 | \& struct tty * tty.t_tp_virt 008 4 |
| 199 | \& struct clist tty.t_rawq 00c 20 |
| 200 | \& int tty.t_rawq.c_cc 00c 4 |
| 201 | \& int tty.t_rawq.c_cmax 010 4 |
| 202 | \& int tty.t_rawq.c_cfx 014 4 |
| 203 | \& int tty.t_rawq.c_clx 018 4 |
| 204 | \& struct tty * tty.t_rawq.c_tp_cpu 01c 4 |
| 205 | \& struct tty * tty.t_rawq.c_tp_iop 020 4 |
| 206 | \& unsigned char * tty.t_rawq.c_buf_cpu 024 4 |
| 207 | \& unsigned char * tty.t_rawq.c_buf_iop 028 4 |
| 208 | \& struct clist tty.t_canq 02c 20 |
| 209 | \& int tty.t_canq.c_cc 02c 4 |
| 210 | \& int tty.t_canq.c_cmax 030 4 |
| 211 | \& int tty.t_canq.c_cfx 034 4 |
| 212 | \& int tty.t_canq.c_clx 038 4 |
| 213 | \& struct tty * tty.t_canq.c_tp_cpu 03c 4 |
| 214 | \& struct tty * tty.t_canq.c_tp_iop 040 4 |
| 215 | \& unsigned char * tty.t_canq.c_buf_cpu 044 4 |
| 216 | \& unsigned char * tty.t_canq.c_buf_iop 048 4 |
| 217 | \& struct clist tty.t_outq 04c 20 |
| 218 | \& int tty.t_outq.c_cc 04c 4 |
| 219 | \& int tty.t_outq.c_cmax 050 4 |
| 220 | \& int tty.t_outq.c_cfx 054 4 |
| 221 | \& int tty.t_outq.c_clx 058 4 |
| 222 | \& struct tty * tty.t_outq.c_tp_cpu 05c 4 |
| 223 | \& struct tty * tty.t_outq.c_tp_iop 060 4 |
| 224 | \& unsigned char * tty.t_outq.c_buf_cpu 064 4 |
| 225 | \& unsigned char * tty.t_outq.c_buf_iop 068 4 |
| 226 | \& (*int)() tty.t_oproc_cpu 06c 4 |
| 227 | \& (*int)() tty.t_oproc_iop 070 4 |
| 228 | \& (*int)() tty.t_stopproc_cpu 074 4 |
| 229 | \& (*int)() tty.t_stopproc_iop 078 4 |
| 230 | \& struct thread * tty.t_rsel 07c 4 |
| 231 | .Ve |
| 232 | .PP |
| 233 | etc. |
| 234 | .PP |
| 235 | Actually, this was generated by a particular set of options. You can control |
| 236 | the formatting of each column, whether you prefer wide or fat, hex or decimal, |
| 237 | leading zeroes or whatever. |
| 238 | .PP |
| 239 | All you need to be able to use this is a C compiler than generates |
| 240 | BSD/GCC\-style stabs. The \fB\-g\fR option on native \s-1BSD\s0 compilers and \s-1GCC\s0 |
| 241 | should get this for you. |
| 242 | .PP |
| 243 | To learn more, just type a bogus option, like \fB\-\e?\fR, and a long usage message |
| 244 | will be provided. There are a fair number of possibilities. |
| 245 | .PP |
| 246 | If you're only a C programmer, than this is the end of the message for you. |
| 247 | You can quit right now, and if you care to, save off the source and run it |
| 248 | when you feel like it. Or not. |
| 249 | .PP |
| 250 | But if you're a perl programmer, then for you I have something much more |
| 251 | wondrous than just a structure offset printer. |
| 252 | .PP |
| 253 | You see, if you call pstruct by its other incybernation, c2ph, you have a code |
| 254 | generator that translates C code into perl code! Well, structure and union |
| 255 | declarations at least, but that's quite a bit. |
| 256 | .PP |
| 257 | Prior to this point, anyone programming in perl who wanted to interact |
| 258 | with C programs, like the kernel, was forced to guess the layouts of |
| 259 | the C strutures, and then hardwire these into his program. Of course, |
| 260 | when you took your wonderfully crafted program to a system where the |
| 261 | sgtty structure was laid out differently, you program broke. Which is |
| 262 | a shame. |
| 263 | .PP |
| 264 | We've had Larry's h2ph translator, which helped, but that only works on |
| 265 | cpp symbols, not real C, which was also very much needed. What I offer |
| 266 | you is a symbolic way of getting at all the C structures. I've couched |
| 267 | them in terms of packages and functions. Consider the following program: |
| 268 | .PP |
| 269 | .Vb 1 |
| 270 | \& #!/usr/local/bin/perl |
| 271 | .Ve |
| 272 | .PP |
| 273 | .Vb 3 |
| 274 | \& require 'syscall.ph'; |
| 275 | \& require 'sys/time.ph'; |
| 276 | \& require 'sys/resource.ph'; |
| 277 | .Ve |
| 278 | .PP |
| 279 | .Vb 1 |
| 280 | \& $ru = "\e0" x &rusage'sizeof(); |
| 281 | .Ve |
| 282 | .PP |
| 283 | .Vb 1 |
| 284 | \& syscall(&SYS_getrusage, &RUSAGE_SELF, $ru) && die "getrusage: $!"; |
| 285 | .Ve |
| 286 | .PP |
| 287 | .Vb 1 |
| 288 | \& @ru = unpack($t = &rusage'typedef(), $ru); |
| 289 | .Ve |
| 290 | .PP |
| 291 | .Vb 2 |
| 292 | \& $utime = $ru[ &rusage'ru_utime + &timeval'tv_sec ] |
| 293 | \& + ($ru[ &rusage'ru_utime + &timeval'tv_usec ]) / 1e6; |
| 294 | .Ve |
| 295 | .PP |
| 296 | .Vb 2 |
| 297 | \& $stime = $ru[ &rusage'ru_stime + &timeval'tv_sec ] |
| 298 | \& + ($ru[ &rusage'ru_stime + &timeval'tv_usec ]) / 1e6; |
| 299 | .Ve |
| 300 | .PP |
| 301 | .Vb 1 |
| 302 | \& printf "you have used %8.3fs+%8.3fu seconds.\en", $utime, $stime; |
| 303 | .Ve |
| 304 | .PP |
| 305 | As you see, the name of the package is the name of the structure. Regular |
| 306 | fields are just their own names. Plus the following accessor functions are |
| 307 | provided for your convenience: |
| 308 | .PP |
| 309 | .Vb 3 |
| 310 | \& struct This takes no arguments, and is merely the number of first-level |
| 311 | \& elements in the structure. You would use this for indexing |
| 312 | \& into arrays of structures, perhaps like this |
| 313 | .Ve |
| 314 | .PP |
| 315 | .Vb 5 |
| 316 | \& $usec = $u[ &user'u_utimer |
| 317 | \& + (&ITIMER_VIRTUAL * &itimerval'struct) |
| 318 | \& + &itimerval'it_value |
| 319 | \& + &timeval'tv_usec |
| 320 | \& ]; |
| 321 | .Ve |
| 322 | .PP |
| 323 | .Vb 2 |
| 324 | \& sizeof Returns the bytes in the structure, or the member if |
| 325 | \& you pass it an argument, such as |
| 326 | .Ve |
| 327 | .PP |
| 328 | .Vb 1 |
| 329 | \& &rusage'sizeof(&rusage'ru_utime) |
| 330 | .Ve |
| 331 | .PP |
| 332 | .Vb 6 |
| 333 | \& typedef This is the perl format definition for passing to pack and |
| 334 | \& unpack. If you ask for the typedef of a nothing, you get |
| 335 | \& the whole structure, otherwise you get that of the member |
| 336 | \& you ask for. Padding is taken care of, as is the magic to |
| 337 | \& guarantee that a union is unpacked into all its aliases. |
| 338 | \& Bitfields are not quite yet supported however. |
| 339 | .Ve |
| 340 | .PP |
| 341 | .Vb 4 |
| 342 | \& offsetof This function is the byte offset into the array of that |
| 343 | \& member. You may wish to use this for indexing directly |
| 344 | \& into the packed structure with vec() if you're too lazy |
| 345 | \& to unpack it. |
| 346 | .Ve |
| 347 | .PP |
| 348 | .Vb 6 |
| 349 | \& typeof Not to be confused with the typedef accessor function, this |
| 350 | \& one returns the C type of that field. This would allow |
| 351 | \& you to print out a nice structured pretty print of some |
| 352 | \& structure without knoning anything about it beforehand. |
| 353 | \& No args to this one is a noop. Someday I'll post such |
| 354 | \& a thing to dump out your u structure for you. |
| 355 | .Ve |
| 356 | .PP |
| 357 | The way I see this being used is like basically this: |
| 358 | .PP |
| 359 | .Vb 3 |
| 360 | \& % h2ph <some_include_file.h > /usr/lib/perl/tmp.ph |
| 361 | \& % c2ph some_include_file.h >> /usr/lib/perl/tmp.ph |
| 362 | \& % install |
| 363 | .Ve |
| 364 | .PP |
| 365 | It's a little tricker with c2ph because you have to get the includes right. |
| 366 | I can't know this for your system, but it's not usually too terribly difficult. |
| 367 | .PP |
| 368 | The code isn't pretty as I mentioned \*(-- I never thought it would be a 1000\- |
| 369 | line program when I started, or I might not have begun. :\-) But I would have |
| 370 | been less cavalier in how the parts of the program communicated with each |
| 371 | other, etc. It might also have helped if I didn't have to divine the makeup |
| 372 | of the stabs on the fly, and then account for micro differences between my |
| 373 | compiler and gcc. |
| 374 | .PP |
| 375 | Anyway, here it is. Should run on perl v4 or greater. Maybe less. |
| 376 | .PP |
| 377 | .Vb 1 |
| 378 | \& --tom |
| 379 | .Ve |