Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1371 lines
37 KiB

  1. @rem = '--*-Perl-*--
  2. @echo off
  3. if "%OS%" == "Windows_NT" goto WinNT
  4. perl -x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9
  5. goto endofperl
  6. :WinNT
  7. perl -x -S %0 %*
  8. if NOT "%COMSPEC%" == "%SystemRoot%\system32\cmd.exe" goto endofperl
  9. if %errorlevel% == 9009 echo You do not have Perl in your PATH.
  10. if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul
  11. goto endofperl
  12. @rem ';
  13. #!perl
  14. #line 15
  15. eval 'exec C:\Perl\bin\perl.exe -S $0 ${1+"$@"}'
  16. if $running_under_some_shell;
  17. #
  18. #
  19. # c2ph (aka pstruct)
  20. # Tom Christiansen, <[email protected]>
  21. #
  22. # As pstruct, dump C structures as generated from 'cc -g -S' stabs.
  23. # As c2ph, do this PLUS generate perl code for getting at the structures.
  24. #
  25. # See the usage message for more. If this isn't enough, read the code.
  26. #
  27. =head1 NAME
  28. c2ph, pstruct - Dump C structures as generated from C<cc -g -S> stabs
  29. =head1 SYNOPSIS
  30. c2ph [-dpnP] [var=val] [files ...]
  31. =head2 OPTIONS
  32. Options:
  33. -w wide; short for: type_width=45 member_width=35 offset_width=8
  34. -x hex; short for: offset_fmt=x offset_width=08 size_fmt=x size_width=04
  35. -n do not generate perl code (default when invoked as pstruct)
  36. -p generate perl code (default when invoked as c2ph)
  37. -v generate perl code, with C decls as comments
  38. -i do NOT recompute sizes for intrinsic datatypes
  39. -a dump information on intrinsics also
  40. -t trace execution
  41. -d spew reams of debugging output
  42. -slist give comma-separated list a structures to dump
  43. =head1 DESCRIPTION
  44. The following is the old c2ph.doc documentation by Tom Christiansen
  45. <[email protected]>
  46. Date: 25 Jul 91 08:10:21 GMT
  47. Once upon a time, I wrote a program called pstruct. It was a perl
  48. program that tried to parse out C structures and display their member
  49. offsets for you. This was especially useful for people looking at
  50. binary dumps or poking around the kernel.
  51. Pstruct was not a pretty program. Neither was it particularly robust.
  52. The problem, you see, was that the C compiler was much better at parsing
  53. C than I could ever hope to be.
  54. So I got smart: I decided to be lazy and let the C compiler parse the C,
  55. which would spit out debugger stabs for me to read. These were much
  56. easier to parse. It's still not a pretty program, but at least it's more
  57. robust.
  58. Pstruct takes any .c or .h files, or preferably .s ones, since that's
  59. the format it is going to massage them into anyway, and spits out
  60. listings like this:
  61. struct tty {
  62. int tty.t_locker 000 4
  63. int tty.t_mutex_index 004 4
  64. struct tty * tty.t_tp_virt 008 4
  65. struct clist tty.t_rawq 00c 20
  66. int tty.t_rawq.c_cc 00c 4
  67. int tty.t_rawq.c_cmax 010 4
  68. int tty.t_rawq.c_cfx 014 4
  69. int tty.t_rawq.c_clx 018 4
  70. struct tty * tty.t_rawq.c_tp_cpu 01c 4
  71. struct tty * tty.t_rawq.c_tp_iop 020 4
  72. unsigned char * tty.t_rawq.c_buf_cpu 024 4
  73. unsigned char * tty.t_rawq.c_buf_iop 028 4
  74. struct clist tty.t_canq 02c 20
  75. int tty.t_canq.c_cc 02c 4
  76. int tty.t_canq.c_cmax 030 4
  77. int tty.t_canq.c_cfx 034 4
  78. int tty.t_canq.c_clx 038 4
  79. struct tty * tty.t_canq.c_tp_cpu 03c 4
  80. struct tty * tty.t_canq.c_tp_iop 040 4
  81. unsigned char * tty.t_canq.c_buf_cpu 044 4
  82. unsigned char * tty.t_canq.c_buf_iop 048 4
  83. struct clist tty.t_outq 04c 20
  84. int tty.t_outq.c_cc 04c 4
  85. int tty.t_outq.c_cmax 050 4
  86. int tty.t_outq.c_cfx 054 4
  87. int tty.t_outq.c_clx 058 4
  88. struct tty * tty.t_outq.c_tp_cpu 05c 4
  89. struct tty * tty.t_outq.c_tp_iop 060 4
  90. unsigned char * tty.t_outq.c_buf_cpu 064 4
  91. unsigned char * tty.t_outq.c_buf_iop 068 4
  92. (*int)() tty.t_oproc_cpu 06c 4
  93. (*int)() tty.t_oproc_iop 070 4
  94. (*int)() tty.t_stopproc_cpu 074 4
  95. (*int)() tty.t_stopproc_iop 078 4
  96. struct thread * tty.t_rsel 07c 4
  97. etc.
  98. Actually, this was generated by a particular set of options. You can control
  99. the formatting of each column, whether you prefer wide or fat, hex or decimal,
  100. leading zeroes or whatever.
  101. All you need to be able to use this is a C compiler than generates
  102. BSD/GCC-style stabs. The B<-g> option on native BSD compilers and GCC
  103. should get this for you.
  104. To learn more, just type a bogus option, like B<-\?>, and a long usage message
  105. will be provided. There are a fair number of possibilities.
  106. If you're only a C programmer, than this is the end of the message for you.
  107. You can quit right now, and if you care to, save off the source and run it
  108. when you feel like it. Or not.
  109. But if you're a perl programmer, then for you I have something much more
  110. wondrous than just a structure offset printer.
  111. You see, if you call pstruct by its other incybernation, c2ph, you have a code
  112. generator that translates C code into perl code! Well, structure and union
  113. declarations at least, but that's quite a bit.
  114. Prior to this point, anyone programming in perl who wanted to interact
  115. with C programs, like the kernel, was forced to guess the layouts of
  116. the C strutures, and then hardwire these into his program. Of course,
  117. when you took your wonderfully crafted program to a system where the
  118. sgtty structure was laid out differently, you program broke. Which is
  119. a shame.
  120. We've had Larry's h2ph translator, which helped, but that only works on
  121. cpp symbols, not real C, which was also very much needed. What I offer
  122. you is a symbolic way of getting at all the C structures. I've couched
  123. them in terms of packages and functions. Consider the following program:
  124. #!/usr/local/bin/perl
  125. require 'syscall.ph';
  126. require 'sys/time.ph';
  127. require 'sys/resource.ph';
  128. $ru = "\0" x &rusage'sizeof();
  129. syscall(&SYS_getrusage, &RUSAGE_SELF, $ru) && die "getrusage: $!";
  130. @ru = unpack($t = &rusage'typedef(), $ru);
  131. $utime = $ru[ &rusage'ru_utime + &timeval'tv_sec ]
  132. + ($ru[ &rusage'ru_utime + &timeval'tv_usec ]) / 1e6;
  133. $stime = $ru[ &rusage'ru_stime + &timeval'tv_sec ]
  134. + ($ru[ &rusage'ru_stime + &timeval'tv_usec ]) / 1e6;
  135. printf "you have used %8.3fs+%8.3fu seconds.\n", $utime, $stime;
  136. As you see, the name of the package is the name of the structure. Regular
  137. fields are just their own names. Plus the following accessor functions are
  138. provided for your convenience:
  139. struct This takes no arguments, and is merely the number of first-level
  140. elements in the structure. You would use this for indexing
  141. into arrays of structures, perhaps like this
  142. $usec = $u[ &user'u_utimer
  143. + (&ITIMER_VIRTUAL * &itimerval'struct)
  144. + &itimerval'it_value
  145. + &timeval'tv_usec
  146. ];
  147. sizeof Returns the bytes in the structure, or the member if
  148. you pass it an argument, such as
  149. &rusage'sizeof(&rusage'ru_utime)
  150. typedef This is the perl format definition for passing to pack and
  151. unpack. If you ask for the typedef of a nothing, you get
  152. the whole structure, otherwise you get that of the member
  153. you ask for. Padding is taken care of, as is the magic to
  154. guarantee that a union is unpacked into all its aliases.
  155. Bitfields are not quite yet supported however.
  156. offsetof This function is the byte offset into the array of that
  157. member. You may wish to use this for indexing directly
  158. into the packed structure with vec() if you're too lazy
  159. to unpack it.
  160. typeof Not to be confused with the typedef accessor function, this
  161. one returns the C type of that field. This would allow
  162. you to print out a nice structured pretty print of some
  163. structure without knoning anything about it beforehand.
  164. No args to this one is a noop. Someday I'll post such
  165. a thing to dump out your u structure for you.
  166. The way I see this being used is like basically this:
  167. % h2ph <some_include_file.h > /usr/lib/perl/tmp.ph
  168. % c2ph some_include_file.h >> /usr/lib/perl/tmp.ph
  169. % install
  170. It's a little tricker with c2ph because you have to get the includes right.
  171. I can't know this for your system, but it's not usually too terribly difficult.
  172. The code isn't pretty as I mentioned -- I never thought it would be a 1000-
  173. line program when I started, or I might not have begun. :-) But I would have
  174. been less cavalier in how the parts of the program communicated with each
  175. other, etc. It might also have helped if I didn't have to divine the makeup
  176. of the stabs on the fly, and then account for micro differences between my
  177. compiler and gcc.
  178. Anyway, here it is. Should run on perl v4 or greater. Maybe less.
  179. --tom
  180. =cut
  181. $RCSID = '$Id: c2ph,v 1.7 95/10/28 10:41:47 tchrist Exp Locker: tchrist $';
  182. ######################################################################
  183. # some handy data definitions. many of these can be reset later.
  184. $bitorder = 'b'; # ascending; set to B for descending bit fields
  185. %intrinsics =
  186. %template = (
  187. 'char', 'c',
  188. 'unsigned char', 'C',
  189. 'short', 's',
  190. 'short int', 's',
  191. 'unsigned short', 'S',
  192. 'unsigned short int', 'S',
  193. 'short unsigned int', 'S',
  194. 'int', 'i',
  195. 'unsigned int', 'I',
  196. 'long', 'l',
  197. 'long int', 'l',
  198. 'unsigned long', 'L',
  199. 'unsigned long', 'L',
  200. 'long unsigned int', 'L',
  201. 'unsigned long int', 'L',
  202. 'long long', 'q',
  203. 'long long int', 'q',
  204. 'unsigned long long', 'Q',
  205. 'unsigned long long int', 'Q',
  206. 'float', 'f',
  207. 'double', 'd',
  208. 'pointer', 'p',
  209. 'null', 'x',
  210. 'neganull', 'X',
  211. 'bit', $bitorder,
  212. );
  213. &buildscrunchlist;
  214. delete $intrinsics{'neganull'};
  215. delete $intrinsics{'bit'};
  216. delete $intrinsics{'null'};
  217. # use -s to recompute sizes
  218. %sizeof = (
  219. 'char', '1',
  220. 'unsigned char', '1',
  221. 'short', '2',
  222. 'short int', '2',
  223. 'unsigned short', '2',
  224. 'unsigned short int', '2',
  225. 'short unsigned int', '2',
  226. 'int', '4',
  227. 'unsigned int', '4',
  228. 'long', '4',
  229. 'long int', '4',
  230. 'unsigned long', '4',
  231. 'unsigned long int', '4',
  232. 'long unsigned int', '4',
  233. 'long long', '8',
  234. 'long long int', '8',
  235. 'unsigned long long', '8',
  236. 'unsigned long long int', '8',
  237. 'float', '4',
  238. 'double', '8',
  239. 'pointer', '4',
  240. );
  241. ($type_width, $member_width, $offset_width, $size_width) = (20, 20, 6, 5);
  242. ($offset_fmt, $size_fmt) = ('d', 'd');
  243. $indent = 2;
  244. $CC = 'cc';
  245. $CFLAGS = '-g -S';
  246. $DEFINES = '';
  247. $perl++ if $0 =~ m#/?c2ph$#;
  248. require 'getopts.pl';
  249. eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_]+=)(.*)/ && shift;
  250. &Getopts('aixdpvtnws:') || &usage(0);
  251. $opt_d && $debug++;
  252. $opt_t && $trace++;
  253. $opt_p && $perl++;
  254. $opt_v && $verbose++;
  255. $opt_n && ($perl = 0);
  256. if ($opt_w) {
  257. ($type_width, $member_width, $offset_width) = (45, 35, 8);
  258. }
  259. if ($opt_x) {
  260. ($offset_fmt, $offset_width, $size_fmt, $size_width) = ( 'x', '08', 'x', 04 );
  261. }
  262. eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_]+=)(.*)/ && shift;
  263. sub PLUMBER {
  264. select(STDERR);
  265. print "oops, apperent pager foulup\n";
  266. $isatty++;
  267. &usage(1);
  268. }
  269. sub usage {
  270. local($oops) = @_;
  271. unless (-t STDOUT) {
  272. select(STDERR);
  273. } elsif (!$oops) {
  274. $isatty++;
  275. $| = 1;
  276. print "hit <RETURN> for further explanation: ";
  277. <STDIN>;
  278. open (PIPE, "|". ($ENV{PAGER} || 'more'));
  279. $SIG{PIPE} = PLUMBER;
  280. select(PIPE);
  281. }
  282. print "usage: $0 [-dpnP] [var=val] [files ...]\n";
  283. exit unless $isatty;
  284. print <<EOF;
  285. Options:
  286. -w wide; short for: type_width=45 member_width=35 offset_width=8
  287. -x hex; short for: offset_fmt=x offset_width=08 size_fmt=x size_width=04
  288. -n do not generate perl code (default when invoked as pstruct)
  289. -p generate perl code (default when invoked as c2ph)
  290. -v generate perl code, with C decls as comments
  291. -i do NOT recompute sizes for intrinsic datatypes
  292. -a dump information on intrinsics also
  293. -t trace execution
  294. -d spew reams of debugging output
  295. -slist give comma-separated list a structures to dump
  296. Var Name Default Value Meaning
  297. EOF
  298. &defvar('CC', 'which_compiler to call');
  299. &defvar('CFLAGS', 'how to generate *.s files with stabs');
  300. &defvar('DEFINES','any extra cflags or cpp defines, like -I, -D, -U');
  301. print "\n";
  302. &defvar('type_width', 'width of type field (column 1)');
  303. &defvar('member_width', 'width of member field (column 2)');
  304. &defvar('offset_width', 'width of offset field (column 3)');
  305. &defvar('size_width', 'width of size field (column 4)');
  306. print "\n";
  307. &defvar('offset_fmt', 'sprintf format type for offset');
  308. &defvar('size_fmt', 'sprintf format type for size');
  309. print "\n";
  310. &defvar('indent', 'how far to indent each nesting level');
  311. print <<'EOF';
  312. If any *.[ch] files are given, these will be catted together into
  313. a temporary *.c file and sent through:
  314. $CC $CFLAGS $DEFINES
  315. and the resulting *.s groped for stab information. If no files are
  316. supplied, then stdin is read directly with the assumption that it
  317. contains stab information. All other liens will be ignored. At
  318. most one *.s file should be supplied.
  319. EOF
  320. close PIPE;
  321. exit 1;
  322. }
  323. sub defvar {
  324. local($var, $msg) = @_;
  325. printf "%-16s%-15s %s\n", $var, eval "\$$var", $msg;
  326. }
  327. $recurse = 1;
  328. if (@ARGV) {
  329. if (grep(!/\.[csh]$/,@ARGV)) {
  330. warn "Only *.[csh] files expected!\n";
  331. &usage;
  332. }
  333. elsif (grep(/\.s$/,@ARGV)) {
  334. if (@ARGV > 1) {
  335. warn "Only one *.s file allowed!\n";
  336. &usage;
  337. }
  338. }
  339. elsif (@ARGV == 1 && $ARGV[0] =~ /\.c$/) {
  340. local($dir, $file) = $ARGV[0] =~ m#(.*/)?(.*)$#;
  341. $chdir = "cd $dir; " if $dir;
  342. &system("$chdir$CC $CFLAGS $DEFINES $file") && exit 1;
  343. $ARGV[0] =~ s/\.c$/.s/;
  344. }
  345. else {
  346. $TMP = "/tmp/c2ph.$$.c";
  347. &system("cat @ARGV > $TMP") && exit 1;
  348. &system("cd /tmp; $CC $CFLAGS $DEFINES $TMP") && exit 1;
  349. unlink $TMP;
  350. $TMP =~ s/\.c$/.s/;
  351. @ARGV = ($TMP);
  352. }
  353. }
  354. if ($opt_s) {
  355. for (split(/[\s,]+/, $opt_s)) {
  356. $interested{$_}++;
  357. }
  358. }
  359. $| = 1 if $debug;
  360. main: {
  361. if ($trace) {
  362. if (-t && !@ARGV) {
  363. print STDERR "reading from your keyboard: ";
  364. } else {
  365. print STDERR "reading from " . (@ARGV ? "@ARGV" : "<STDIN>").": ";
  366. }
  367. }
  368. STAB: while (<>) {
  369. if ($trace && !($. % 10)) {
  370. $lineno = $..'';
  371. print STDERR $lineno, "\b" x length($lineno);
  372. }
  373. next unless /^\s*\.stabs\s+/;
  374. $line = $_;
  375. s/^\s*\.stabs\s+//;
  376. if (s/\\\\"[d,]+$//) {
  377. $saveline .= $line;
  378. $savebar = $_;
  379. next STAB;
  380. }
  381. if ($saveline) {
  382. s/^"//;
  383. $_ = $savebar . $_;
  384. $line = $saveline;
  385. }
  386. &stab;
  387. $savebar = $saveline = undef;
  388. }
  389. print STDERR "$.\n" if $trace;
  390. unlink $TMP if $TMP;
  391. &compute_intrinsics if $perl && !$opt_i;
  392. print STDERR "resolving types\n" if $trace;
  393. &resolve_types;
  394. &adjust_start_addrs;
  395. $sum = 2 + $type_width + $member_width;
  396. $pmask1 = "%-${type_width}s %-${member_width}s";
  397. $pmask2 = "%-${sum}s %${offset_width}${offset_fmt}%s %${size_width}${size_fmt}%s";
  398. if ($perl) {
  399. # resolve template -- should be in stab define order, but even this isn't enough.
  400. print STDERR "\nbuilding type templates: " if $trace;
  401. for $i (reverse 0..$#type) {
  402. next unless defined($name = $type[$i]);
  403. next unless defined $struct{$name};
  404. ($iname = $name) =~ s/\..*//;
  405. $build_recursed = 0;
  406. &build_template($name) unless defined $template{&psou($name)} ||
  407. $opt_s && !$interested{$iname};
  408. }
  409. print STDERR "\n\n" if $trace;
  410. }
  411. print STDERR "dumping structs: " if $trace;
  412. local($iam);
  413. foreach $name (sort keys %struct) {
  414. ($iname = $name) =~ s/\..*//;
  415. next if $opt_s && !$interested{$iname};
  416. print STDERR "$name " if $trace;
  417. undef @sizeof;
  418. undef @typedef;
  419. undef @offsetof;
  420. undef @indices;
  421. undef @typeof;
  422. undef @fieldnames;
  423. $mname = &munge($name);
  424. $fname = &psou($name);
  425. print "# " if $perl && $verbose;
  426. $pcode = '';
  427. print "$fname {\n" if !$perl || $verbose;
  428. $template{$fname} = &scrunch($template{$fname}) if $perl;
  429. &pstruct($name,$name,0);
  430. print "# " if $perl && $verbose;
  431. print "}\n" if !$perl || $verbose;
  432. print "\n" if $perl && $verbose;
  433. if ($perl) {
  434. print "$pcode";
  435. printf("\nsub %-32s { %4d; }\n\n", "${mname}'struct", $countof{$name});
  436. print <<EOF;
  437. sub ${mname}'typedef {
  438. local(\$${mname}'index) = shift;
  439. defined \$${mname}'index
  440. ? \$${mname}'typedef[\$${mname}'index]
  441. : \$${mname}'typedef;
  442. }
  443. EOF
  444. print <<EOF;
  445. sub ${mname}'sizeof {
  446. local(\$${mname}'index) = shift;
  447. defined \$${mname}'index
  448. ? \$${mname}'sizeof[\$${mname}'index]
  449. : \$${mname}'sizeof;
  450. }
  451. EOF
  452. print <<EOF;
  453. sub ${mname}'offsetof {
  454. local(\$${mname}'index) = shift;
  455. defined \$${mname}index
  456. ? \$${mname}'offsetof[\$${mname}'index]
  457. : \$${mname}'sizeof;
  458. }
  459. EOF
  460. print <<EOF;
  461. sub ${mname}'typeof {
  462. local(\$${mname}'index) = shift;
  463. defined \$${mname}index
  464. ? \$${mname}'typeof[\$${mname}'index]
  465. : '$name';
  466. }
  467. EOF
  468. print <<EOF;
  469. sub ${mname}'fieldnames {
  470. \@${mname}'fieldnames;
  471. }
  472. EOF
  473. $iam = ($isastruct{$name} && 's') || ($isaunion{$name} && 'u');
  474. print <<EOF;
  475. sub ${mname}'isastruct {
  476. '$iam';
  477. }
  478. EOF
  479. print "\$${mname}'typedef = '" . &scrunch($template{$fname})
  480. . "';\n";
  481. print "\$${mname}'sizeof = $sizeof{$name};\n\n";
  482. print "\@${mname}'indices = (", &squishseq(@indices), ");\n";
  483. print "\n";
  484. print "\@${mname}'typedef[\@${mname}'indices] = (",
  485. join("\n\t", '', @typedef), "\n );\n\n";
  486. print "\@${mname}'sizeof[\@${mname}'indices] = (",
  487. join("\n\t", '', @sizeof), "\n );\n\n";
  488. print "\@${mname}'offsetof[\@${mname}'indices] = (",
  489. join("\n\t", '', @offsetof), "\n );\n\n";
  490. print "\@${mname}'typeof[\@${mname}'indices] = (",
  491. join("\n\t", '', @typeof), "\n );\n\n";
  492. print "\@${mname}'fieldnames[\@${mname}'indices] = (",
  493. join("\n\t", '', @fieldnames), "\n );\n\n";
  494. $template_printed{$fname}++;
  495. $size_printed{$fname}++;
  496. }
  497. print "\n";
  498. }
  499. print STDERR "\n" if $trace;
  500. unless ($perl && $opt_a) {
  501. print "\n1;\n" if $perl;
  502. exit;
  503. }
  504. foreach $name (sort bysizevalue keys %intrinsics) {
  505. next if $size_printed{$name};
  506. print '$',&munge($name),"'sizeof = ", $sizeof{$name}, ";\n";
  507. }
  508. print "\n";
  509. sub bysizevalue { $sizeof{$a} <=> $sizeof{$b}; }
  510. foreach $name (sort keys %intrinsics) {
  511. print '$',&munge($name),"'typedef = '", $template{$name}, "';\n";
  512. }
  513. print "\n1;\n" if $perl;
  514. exit;
  515. }
  516. ########################################################################################
  517. sub stab {
  518. next unless $continued || /:[\$\w]+(\(\d+,\d+\))?=[\*\$\w]+/; # (\d+,\d+) is for sun
  519. s/"// || next;
  520. s/",([x\d]+),([x\d]+),([x\d]+),.*// || next;
  521. next if /^\s*$/;
  522. $size = $3 if $3;
  523. $_ = $continued . $_ if length($continued);
  524. if (s/\\\\$//) {
  525. # if last 2 chars of string are '\\' then stab is continued
  526. # in next stab entry
  527. chop;
  528. $continued = $_;
  529. next;
  530. }
  531. $continued = '';
  532. $line = $_;
  533. if (($name, $pdecl) = /^([\$ \w]+):[tT]((\d+)(=[rufs*](\d+))+)$/) {
  534. print "$name is a typedef for some funky pointers: $pdecl\n" if $debug;
  535. &pdecl($pdecl);
  536. next;
  537. }
  538. if (/(([ \w]+):t(\d+|\(\d+,\d+\)))=r?(\d+|\(\d+,\d+\))(;\d+;\d+;)?/) {
  539. local($ident) = $2;
  540. push(@intrinsics, $ident);
  541. $typeno = &typeno($3);
  542. $type[$typeno] = $ident;
  543. print STDERR "intrinsic $ident in new type $typeno\n" if $debug;
  544. next;
  545. }
  546. if (($name, $typeordef, $typeno, $extra, $struct, $_)
  547. = /^([\$ \w]+):([ustT])(\d+|\(\d+,\d+\))(=[rufs*](\d+))?(.*)$/)
  548. {
  549. $typeno = &typeno($typeno); # sun foolery
  550. }
  551. elsif (/^[\$\w]+:/) {
  552. next; # variable
  553. }
  554. else {
  555. warn "can't grok stab: <$_> in: $line " if $_;
  556. next;
  557. }
  558. #warn "got size $size for $name\n";
  559. $sizeof{$name} = $size if $size;
  560. s/;[-\d]*;[-\d]*;$//; # we don't care about ranges
  561. $typenos{$name} = $typeno;
  562. unless (defined $type[$typeno]) {
  563. &panic("type 0??") unless $typeno;
  564. $type[$typeno] = $name unless defined $type[$typeno];
  565. printf "new type $typeno is $name" if $debug;
  566. if ($extra =~ /\*/ && defined $type[$struct]) {
  567. print ", a typedef for a pointer to " , $type[$struct] if $debug;
  568. }
  569. } else {
  570. printf "%s is type %d", $name, $typeno if $debug;
  571. print ", a typedef for " , $type[$typeno] if $debug;
  572. }
  573. print "\n" if $debug;
  574. #next unless $extra =~ /[su*]/;
  575. #$type[$struct] = $name;
  576. if ($extra =~ /[us*]/) {
  577. &sou($name, $extra);
  578. $_ = &sdecl($name, $_, 0);
  579. }
  580. elsif (/^=ar/) {
  581. print "it's a bare array typedef -- that's pretty sick\n" if $debug;
  582. $_ = "$typeno$_";
  583. $scripts = '';
  584. $_ = &adecl($_,1);
  585. }
  586. elsif (s/((\w+):t(\d+|\(\d+,\d+\)))?=r?(;\d+;\d+;)?//) { # the ?'s are for gcc
  587. push(@intrinsics, $2);
  588. $typeno = &typeno($3);
  589. $type[$typeno] = $2;
  590. print STDERR "intrinsic $2 in new type $typeno\n" if $debug;
  591. }
  592. elsif (s/^=e//) { # blessed be thy compiler; mine won't do this
  593. &edecl;
  594. }
  595. else {
  596. warn "Funny remainder for $name on line $_ left in $line " if $_;
  597. }
  598. }
  599. sub typeno { # sun thinks types are (0,27) instead of just 27
  600. local($_) = @_;
  601. s/\(\d+,(\d+)\)/$1/;
  602. $_;
  603. }
  604. sub pstruct {
  605. local($what,$prefix,$base) = @_;
  606. local($field, $fieldname, $typeno, $count, $offset, $entry);
  607. local($fieldtype);
  608. local($type, $tname);
  609. local($mytype, $mycount, $entry2);
  610. local($struct_count) = 0;
  611. local($pad, $revpad, $length, $prepad, $lastoffset, $lastlength, $fmt);
  612. local($bits,$bytes);
  613. local($template);
  614. local($mname) = &munge($name);
  615. sub munge {
  616. local($_) = @_;
  617. s/[\s\$\.]/_/g;
  618. $_;
  619. }
  620. local($sname) = &psou($what);
  621. $nesting++;
  622. for $field (split(/;/, $struct{$what})) {
  623. $pad = $prepad = 0;
  624. $entry = '';
  625. ($fieldname, $typeno, $count, $offset, $length) = split(/,/, $field);
  626. $type = $type[$typeno];
  627. $type =~ /([^[]*)(\[.*\])?/;
  628. $mytype = $1;
  629. $count .= $2;
  630. $fieldtype = &psou($mytype);
  631. local($fname) = &psou($name);
  632. if ($build_templates) {
  633. $pad = ($offset - ($lastoffset + $lastlength))/8
  634. if defined $lastoffset;
  635. if (! $finished_template{$sname}) {
  636. if ($isaunion{$what}) {
  637. $template{$sname} .= 'X' x $revpad . ' ' if $revpad;
  638. } else {
  639. $template{$sname} .= 'x' x $pad . ' ' if $pad;
  640. }
  641. }
  642. $template = &fetch_template($type);
  643. &repeat_template($template,$count);
  644. if (! $finished_template{$sname}) {
  645. $template{$sname} .= $template;
  646. }
  647. $revpad = $length/8 if $isaunion{$what};
  648. ($lastoffset, $lastlength) = ($offset, $length);
  649. } else {
  650. print '# ' if $perl && $verbose;
  651. $entry = sprintf($pmask1,
  652. ' ' x ($nesting * $indent) . $fieldtype,
  653. "$prefix.$fieldname" . $count);
  654. $entry =~ s/(\*+)( )/$2$1/;
  655. printf $pmask2,
  656. $entry,
  657. ($base+$offset)/8,
  658. ($bits = ($base+$offset)%8) ? ".$bits" : " ",
  659. $length/8,
  660. ($bits = $length % 8) ? ".$bits": ""
  661. if !$perl || $verbose;
  662. if ($perl) {
  663. $template = &fetch_template($type);
  664. &repeat_template($template,$count);
  665. }
  666. if ($perl && $nesting == 1) {
  667. push(@sizeof, int($length/8) .",\t# $fieldname");
  668. push(@offsetof, int($offset/8) .",\t# $fieldname");
  669. local($little) = &scrunch($template);
  670. push(@typedef, "'$little', \t# $fieldname");
  671. $type =~ s/(struct|union) //;
  672. push(@typeof, "'$mytype" . ($count ? $count : '') .
  673. "',\t# $fieldname");
  674. push(@fieldnames, "'$fieldname',");
  675. }
  676. print ' ', ' ' x $indent x $nesting, $template
  677. if $perl && $verbose;
  678. print "\n" if !$perl || $verbose;
  679. }
  680. if ($perl) {
  681. local($mycount) = defined $struct{$mytype} ? $countof{$mytype} : 1;
  682. $mycount *= &scripts2count($count) if $count;
  683. if ($nesting==1 && !$build_templates) {
  684. $pcode .= sprintf("sub %-32s { %4d; }\n",
  685. "${mname}'${fieldname}", $struct_count);
  686. push(@indices, $struct_count);
  687. }
  688. $struct_count += $mycount;
  689. }
  690. &pstruct($type, "$prefix.$fieldname", $base+$offset)
  691. if $recurse && defined $struct{$type};
  692. }
  693. $countof{$what} = $struct_count unless defined $countof{$whati};
  694. $template{$sname} .= '$' if $build_templates;
  695. $finished_template{$sname}++;
  696. if ($build_templates && !defined $sizeof{$name}) {
  697. local($fmt) = &scrunch($template{$sname});
  698. print STDERR "no size for $name, punting with $fmt..." if $debug;
  699. eval '$sizeof{$name} = length(pack($fmt, ()))';
  700. if ($@) {
  701. chop $@;
  702. warn "couldn't get size for \$name: $@";
  703. } else {
  704. print STDERR $sizeof{$name}, "\n" if $debUg;
  705. }
  706. }
  707. --$nesting;
  708. }
  709. sub psize {
  710. local($me) = @_;
  711. local($amstruct) = $struct{$me} ? 'struct ' : '';
  712. print '$sizeof{\'', $amstruct, $me, '\'} = ';
  713. printf "%d;\n", $sizeof{$me};
  714. }
  715. sub pdecl {
  716. local($pdecl) = @_;
  717. local(@pdecls);
  718. local($tname);
  719. warn "pdecl: $pdecl\n" if $debug;
  720. $pdecl =~ s/\(\d+,(\d+)\)/$1/g;
  721. $pdecl =~ s/\*//g;
  722. @pdecls = split(/=/, $pdecl);
  723. $typeno = $pdecls[0];
  724. $tname = pop @pdecls;
  725. if ($tname =~ s/^f//) { $tname = "$tname&"; }
  726. #else { $tname = "$tname*"; }
  727. for (reverse @pdecls) {
  728. $tname .= s/^f// ? "&" : "*";
  729. #$tname =~ s/^f(.*)/$1&/;
  730. print "type[$_] is $tname\n" if $debug;
  731. $type[$_] = $tname unless defined $type[$_];
  732. }
  733. }
  734. sub adecl {
  735. ($arraytype, $unknown, $lower, $upper) = ();
  736. #local($typeno);
  737. # global $typeno, @type
  738. local($_, $typedef) = @_;
  739. while (s/^((\d+|\(\d+,\d+\))=)?ar(\d+|\(\d+,\d+\));//) {
  740. ($arraytype, $unknown) = ($2, $3);
  741. $arraytype = &typeno($arraytype);
  742. $unknown = &typeno($unknown);
  743. if (s/^(\d+);(\d+);//) {
  744. ($lower, $upper) = ($1, $2);
  745. $scripts .= '[' . ($upper+1) . ']';
  746. } else {
  747. warn "can't find array bounds: $_";
  748. }
  749. }
  750. if (s/^([(,)\d*f=]*),(\d+),(\d+);//) {
  751. ($start, $length) = ($2, $3);
  752. $whatis = $1;
  753. if ($whatis =~ /^(\d+|\(\d+,\d+\))=/) {
  754. $typeno = &typeno($1);
  755. &pdecl($whatis);
  756. } else {
  757. $typeno = &typeno($whatis);
  758. }
  759. } elsif (s/^(\d+)(=[*suf]\d*)//) {
  760. local($whatis) = $2;
  761. if ($whatis =~ /[f*]/) {
  762. &pdecl($whatis);
  763. } elsif ($whatis =~ /[su]/) { #
  764. print "$prefix.$fieldname is an array$scripts anon structs; disgusting\n"
  765. if $debug;
  766. #$type[$typeno] = $name unless defined $type[$typeno];
  767. ##printf "new type $typeno is $name" if $debug;
  768. $typeno = $1;
  769. $type[$typeno] = "$prefix.$fieldname";
  770. local($name) = $type[$typeno];
  771. &sou($name, $whatis);
  772. $_ = &sdecl($name, $_, $start+$offset);
  773. 1;
  774. $start = $start{$name};
  775. $offset = $sizeof{$name};
  776. $length = $offset;
  777. } else {
  778. warn "what's this? $whatis in $line ";
  779. }
  780. } elsif (/^\d+$/) {
  781. $typeno = $_;
  782. } else {
  783. warn "bad array stab: $_ in $line ";
  784. next STAB;
  785. }
  786. #local($wasdef) = defined($type[$typeno]) && $debug;
  787. #if ($typedef) {
  788. #print "redefining $type[$typeno] to " if $wasdef;
  789. #$type[$typeno] = "$whatis$scripts"; # unless defined $type[$typeno];
  790. #print "$type[$typeno]\n" if $wasdef;
  791. #} else {
  792. #$type[$arraytype] = $type[$typeno] unless defined $type[$arraytype];
  793. #}
  794. $type[$arraytype] = "$type[$typeno]$scripts" if defined $type[$typeno];
  795. print "type[$arraytype] is $type[$arraytype]\n" if $debug;
  796. print "$prefix.$fieldname is an array of $type[$arraytype]\n" if $debug;
  797. $_;
  798. }
  799. sub sdecl {
  800. local($prefix, $_, $offset) = @_;
  801. local($fieldname, $scripts, $type, $arraytype, $unknown,
  802. $whatis, $pdecl, $upper,$lower, $start,$length) = ();
  803. local($typeno,$sou);
  804. SFIELD:
  805. while (/^([^;]+);/) {
  806. $scripts = '';
  807. warn "sdecl $_\n" if $debug;
  808. if (s/^([\$\w]+)://) {
  809. $fieldname = $1;
  810. } elsif (s/(\d+)=([us])(\d+|\(\d+,\d+\))//) { #
  811. $typeno = &typeno($1);
  812. $type[$typeno] = "$prefix.$fieldname";
  813. local($name) = "$prefix.$fieldname";
  814. &sou($name,$2);
  815. $_ = &sdecl("$prefix.$fieldname", $_, $start+$offset);
  816. $start = $start{$name};
  817. $offset += $sizeof{$name};
  818. #print "done with anon, start is $start, offset is $offset\n";
  819. #next SFIELD;
  820. } else {
  821. warn "weird field $_ of $line" if $debug;
  822. next STAB;
  823. #$fieldname = &gensym;
  824. #$_ = &sdecl("$prefix.$fieldname", $_, $start+$offset);
  825. }
  826. if (/^(\d+|\(\d+,\d+\))=ar/) {
  827. $_ = &adecl($_);
  828. }
  829. elsif (s/^(\d+|\(\d+,\d+\))?,(\d+),(\d+);//) {
  830. ($start, $length) = ($2, $3);
  831. &panic("no length?") unless $length;
  832. $typeno = &typeno($1) if $1;
  833. }
  834. elsif (s/^(\d+)=xs\w+:,(\d+),(\d+);//) {
  835. ($start, $length) = ($2, $3);
  836. &panic("no length?") unless $length;
  837. $typeno = &typeno($1) if $1;
  838. }
  839. elsif (s/^((\d+|\(\d+,\d+\))(=[*f](\d+|\(\d+,\d+\)))+),(\d+),(\d+);//) {
  840. ($pdecl, $start, $length) = ($1,$5,$6);
  841. &pdecl($pdecl);
  842. }
  843. elsif (s/(\d+)=([us])(\d+|\(\d+,\d+\))//) { # the dratted anon struct
  844. ($typeno, $sou) = ($1, $2);
  845. $typeno = &typeno($typeno);
  846. if (defined($type[$typeno])) {
  847. warn "now how did we get type $1 in $fieldname of $line?";
  848. } else {
  849. print "anon type $typeno is $prefix.$fieldname\n" if $debug;
  850. $type[$typeno] = "$prefix.$fieldname" unless defined $type[$typeno];
  851. };
  852. local($name) = "$prefix.$fieldname";
  853. &sou($name,$sou);
  854. print "anon ".($isastruct{$name}) ? "struct":"union"." for $prefix.$fieldname\n" if $debug;
  855. $type[$typeno] = "$prefix.$fieldname";
  856. $_ = &sdecl("$prefix.$fieldname", $_, $start+$offset);
  857. $start = $start{$name};
  858. $length = $sizeof{$name};
  859. }
  860. else {
  861. warn "can't grok stab for $name ($_) in line $line ";
  862. next STAB;
  863. }
  864. &panic("no length for $prefix.$fieldname") unless $length;
  865. $struct{$name} .= join(',', $fieldname, $typeno, $scripts, $start, $length) . ';';
  866. }
  867. if (s/;\d*,(\d+),(\d+);//) {
  868. local($start, $size) = ($1, $2);
  869. $sizeof{$prefix} = $size;
  870. print "start of $prefix is $start, size of $sizeof{$prefix}\n" if $debug;
  871. $start{$prefix} = $start;
  872. }
  873. $_;
  874. }
  875. sub edecl {
  876. s/;$//;
  877. $enum{$name} = $_;
  878. $_ = '';
  879. }
  880. sub resolve_types {
  881. local($sou);
  882. for $i (0 .. $#type) {
  883. next unless defined $type[$i];
  884. $_ = $type[$i];
  885. unless (/\d/) {
  886. print "type[$i] $type[$i]\n" if $debug;
  887. next;
  888. }
  889. print "type[$i] $_ ==> " if $debug;
  890. s/^(\d+)(\**)\&\*(\**)/"$2($3".&type($1) . ')()'/e;
  891. s/^(\d+)\&/&type($1)/e;
  892. s/^(\d+)/&type($1)/e;
  893. s/(\*+)([^*]+)(\*+)/$1$3$2/;
  894. s/\((\*+)(\w+)(\*+)\)/$3($1$2)/;
  895. s/^(\d+)([\*\[].*)/&type($1).$2/e;
  896. #s/(\d+)(\*|(\[[\[\]\d\*]+]\])+)/&type($1).$2/ge;
  897. $type[$i] = $_;
  898. print "$_\n" if $debug;
  899. }
  900. }
  901. sub type { &psou($type[$_[0]] || "<UNDEFINED>"); }
  902. sub adjust_start_addrs {
  903. for (sort keys %start) {
  904. ($basename = $_) =~ s/\.[^.]+$//;
  905. $start{$_} += $start{$basename};
  906. print "start: $_ @ $start{$_}\n" if $debug;
  907. }
  908. }
  909. sub sou {
  910. local($what, $_) = @_;
  911. /u/ && $isaunion{$what}++;
  912. /s/ && $isastruct{$what}++;
  913. }
  914. sub psou {
  915. local($what) = @_;
  916. local($prefix) = '';
  917. if ($isaunion{$what}) {
  918. $prefix = 'union ';
  919. } elsif ($isastruct{$what}) {
  920. $prefix = 'struct ';
  921. }
  922. $prefix . $what;
  923. }
  924. sub scrunch {
  925. local($_) = @_;
  926. return '' if $_ eq '';
  927. study;
  928. s/\$//g;
  929. s/ / /g;
  930. 1 while s/(\w) \1/$1$1/g;
  931. # i wanna say this, but perl resists my efforts:
  932. # s/(\w)(\1+)/$2 . length($1)/ge;
  933. &quick_scrunch;
  934. s/ $//;
  935. $_;
  936. }
  937. sub buildscrunchlist {
  938. $scrunch_code = "sub quick_scrunch {\n";
  939. for (values %intrinsics) {
  940. $scrunch_code .= "\ts/(${_}{2,})/'$_' . length(\$1)/ge;\n";
  941. }
  942. $scrunch_code .= "}\n";
  943. print "$scrunch_code" if $debug;
  944. eval $scrunch_code;
  945. &panic("can't eval scrunch_code $@ \nscrunch_code") if $@;
  946. }
  947. sub fetch_template {
  948. local($mytype) = @_;
  949. local($fmt);
  950. local($count) = 1;
  951. &panic("why do you care?") unless $perl;
  952. if ($mytype =~ s/(\[\d+\])+$//) {
  953. $count .= $1;
  954. }
  955. if ($mytype =~ /\*/) {
  956. $fmt = $template{'pointer'};
  957. }
  958. elsif (defined $template{$mytype}) {
  959. $fmt = $template{$mytype};
  960. }
  961. elsif (defined $struct{$mytype}) {
  962. if (!defined $template{&psou($mytype)}) {
  963. &build_template($mytype) unless $mytype eq $name;
  964. }
  965. elsif ($template{&psou($mytype)} !~ /\$$/) {
  966. #warn "incomplete template for $mytype\n";
  967. }
  968. $fmt = $template{&psou($mytype)} || '?';
  969. }
  970. else {
  971. warn "unknown fmt for $mytype\n";
  972. $fmt = '?';
  973. }
  974. $fmt x $count . ' ';
  975. }
  976. sub compute_intrinsics {
  977. local($TMP) = "/tmp/c2ph-i.$$.c";
  978. open (TMP, ">$TMP") || die "can't open $TMP: $!";
  979. select(TMP);
  980. print STDERR "computing intrinsic sizes: " if $trace;
  981. undef %intrinsics;
  982. print <<'EOF';
  983. main() {
  984. char *mask = "%d %s\n";
  985. EOF
  986. for $type (@intrinsics) {
  987. next if !$type || $type eq 'void' || $type =~ /complex/; # sun stuff
  988. print <<"EOF";
  989. printf(mask,sizeof($type), "$type");
  990. EOF
  991. }
  992. print <<'EOF';
  993. printf(mask,sizeof(char *), "pointer");
  994. exit(0);
  995. }
  996. EOF
  997. close TMP;
  998. select(STDOUT);
  999. open(PIPE, "cd /tmp && $CC $TMP && /tmp/a.out|");
  1000. while (<PIPE>) {
  1001. chop;
  1002. split(' ',$_,2);;
  1003. print "intrinsic $_[1] is size $_[0]\n" if $debug;
  1004. $sizeof{$_[1]} = $_[0];
  1005. $intrinsics{$_[1]} = $template{$_[0]};
  1006. }
  1007. close(PIPE) || die "couldn't read intrinsics!";
  1008. unlink($TMP, '/tmp/a.out');
  1009. print STDERR "done\n" if $trace;
  1010. }
  1011. sub scripts2count {
  1012. local($_) = @_;
  1013. s/^\[//;
  1014. s/\]$//;
  1015. s/\]\[/*/g;
  1016. $_ = eval;
  1017. &panic("$_: $@") if $@;
  1018. $_;
  1019. }
  1020. sub system {
  1021. print STDERR "@_\n" if $trace;
  1022. system @_;
  1023. }
  1024. sub build_template {
  1025. local($name) = @_;
  1026. &panic("already got a template for $name") if defined $template{$name};
  1027. local($build_templates) = 1;
  1028. local($lparen) = '(' x $build_recursed;
  1029. local($rparen) = ')' x $build_recursed;
  1030. print STDERR "$lparen$name$rparen " if $trace;
  1031. $build_recursed++;
  1032. &pstruct($name,$name,0);
  1033. print STDERR "TEMPLATE for $name is ", $template{&psou($name)}, "\n" if $debug;
  1034. --$build_recursed;
  1035. }
  1036. sub panic {
  1037. select(STDERR);
  1038. print "\npanic: @_\n";
  1039. exit 1 if $] <= 4.003; # caller broken
  1040. local($i,$_);
  1041. local($p,$f,$l,$s,$h,$a,@a,@sub);
  1042. for ($i = 0; ($p,$f,$l,$s,$h,$w) = caller($i); $i++) {
  1043. @a = @DB'args;
  1044. for (@a) {
  1045. if (/^StB\000/ && length($_) == length($_main{'_main'})) {
  1046. $_ = sprintf("%s",$_);
  1047. }
  1048. else {
  1049. s/'/\\'/g;
  1050. s/([^\0]*)/'$1'/ unless /^-?[\d.]+$/;
  1051. s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
  1052. s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
  1053. }
  1054. }
  1055. $w = $w ? '@ = ' : '$ = ';
  1056. $a = $h ? '(' . join(', ', @a) . ')' : '';
  1057. push(@sub, "$w&$s$a from file $f line $l\n");
  1058. last if $signal;
  1059. }
  1060. for ($i=0; $i <= $#sub; $i++) {
  1061. last if $signal;
  1062. print $sub[$i];
  1063. }
  1064. exit 1;
  1065. }
  1066. sub squishseq {
  1067. local($num);
  1068. local($last) = -1e8;
  1069. local($string);
  1070. local($seq) = '..';
  1071. while (defined($num = shift)) {
  1072. if ($num == ($last + 1)) {
  1073. $string .= $seq unless $inseq++;
  1074. $last = $num;
  1075. next;
  1076. } elsif ($inseq) {
  1077. $string .= $last unless $last == -1e8;
  1078. }
  1079. $string .= ',' if defined $string;
  1080. $string .= $num;
  1081. $last = $num;
  1082. $inseq = 0;
  1083. }
  1084. $string .= $last if $inseq && $last != -e18;
  1085. $string;
  1086. }
  1087. sub repeat_template {
  1088. # local($template, $scripts) = @_; have to change caller's values
  1089. if ( $_[1] ) {
  1090. local($ncount) = &scripts2count($_[1]);
  1091. if ($_[0] =~ /^\s*c\s*$/i) {
  1092. $_[0] = "A$ncount ";
  1093. $_[1] = '';
  1094. } else {
  1095. $_[0] = $template x $ncount;
  1096. }
  1097. }
  1098. }
  1099. __END__
  1100. :endofperl