| 1 | #!/usr/bin/perl -I/home/phil/perl/cpan/DataTableText/lib/ | ||||||||||
| 2 | #------------------------------------------------------------------------------- | ||||||||||
| 3 | # Extract macro values and structure details from C programs. | ||||||||||
| 4 | # Philip R Brenan at appaapps dot com, Appa Apps Ltd Inc., 2021 | ||||||||||
| 5 | #------------------------------------------------------------------------------- | ||||||||||
| 6 | # podDocumentation | ||||||||||
| 7 | package Asm::C; | ||||||||||
| 8 | 1 | our $VERSION = "20210330"; | |||||||||
| 9 | 1 | ▶ | use warnings FATAL => qw(all); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 1 | ▶ | use Data::Dump qw(dump); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 1 | ▶ | use Data::Table::Text qw(:all); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 1 | ▶ | use feature qw(say current_sub); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | #D1 Asm::C # Extract macro values and structure details from C programs. | ||||||||||
| 17 | |||||||||||
| 18 | #D2 Structures # Extract structure details from C programs. | ||||||||||
| 19 | |||||||||||
| 20 | 1 | my %extractCStructure; # Structured extracted from C files | |||||||||
| 21 | |||||||||||
| 22 | sub extractCStructure($) # Extract the details of a structure | ||||||||||
| 23 | 15 | ▶ | {my ($input) = @_; # Input C file - a temporary one is ok | ||||||||
|
Subroutine
extractCStructure: called CC=15 SCAR=27.2
Pod
documented
| |||||||||||
| 24 | |||||||||||
| 25 | 15 | ▶ | return $extractCStructure{$input} if exists $extractCStructure{$input}; # Return cached value if it exists | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | 1 | ▶ | return undef unless confirmHasCommandLineCommand(q(gcc)); # Check that we have gcc | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | |||||||||||
| 28 | 1 | ▶ | my $inputFile = -f $input ? $input : writeTempFile($input); # Make sure input is in a file | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | |||||||||||
| 30 | 1 | my $e = qq(gcc -c -x c -fno-eliminate-unused-debug-symbols -fno-eliminate-unused-debug-types -gdwarf $inputFile -o a.out; readelf -w a.out; rm a.out); | |||||||||
| 31 | 1 | my @e = qx($e); # Structure details via dwarf debugging info | |||||||||
| 32 | 1 | my @s; # Structure | |||||||||
| 33 | |||||||||||
| 34 | 1 | for my $e(@e) # Each line of dwarf | |||||||||
| 35 | 240 | ▶ | {if ($e =~ m(<(\w+)><(\w+)>: Abbrev Number:\s+(\w+)\s+(.*))) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 10 | {push @s, [[$1, $2, $3, $4]]; | |||||||||
| 37 | } | ||||||||||
| 38 | 240 | ▶ | if ($e =~ m(<(\w+)>\s+(\w+)\s*:\s(.*))) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 50 | {push $s[-1]->@*, [$1, $2, $3]; | |||||||||
| 40 | } | ||||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | 1 | my %s; my %b; # Structure details, base details | |||||||||
| 44 | 1 | for my $i(keys @s) # Each dwarf | |||||||||
| 45 | 10 | ▶ | {if ($s[$i][0][3] =~ m(DW_TAG_structure_type)) # Structure followed by fields | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 1 | {my $name = $s[$i][1][2] =~ s/\(.*\):\s*//gsr;; | |||||||||
| 47 | 1 | my $size = $s[$i][2][2]; | |||||||||
| 48 | 1 | $s{$name} = genHash('structure', size=>$size, fields=>{}); | |||||||||
| 49 | 1 | ▶ | for(my $j = $i + 1; $j < @s; ++$j) # Following tag fields | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | 4 | ▶ | {last unless $s[$j][0][3] =~ m(DW_TAG_member); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 3 | my $field = $s[$j][1][2] =~ s/\(.*\):\s*//gsr; | |||||||||
| 52 | 3 | my $type = $s[$j][5][2]; | |||||||||
| 53 | 3 | my $loc = $s[$j][6][2]; | |||||||||
| 54 | 3 | $type =~ s(<0x|>) ()gs; | |||||||||
| 55 | 3 | $s{$name}->fields->{$field} = genHash('field', | |||||||||
| 56 | field=>$field, type=>$type, loc=>$loc, size=>undef); | ||||||||||
| 57 | } | ||||||||||
| 58 | } | ||||||||||
| 59 | 10 | ▶ | if ( $s[$i][0][3] =~ m(DW_TAG_base_type)) # Base types | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | 1 | {my $offset = $s[$i][0][1]; | |||||||||
| 61 | 1 | my $size = $s[$i][1][2]; | |||||||||
| 62 | 1 | my $type = $s[$i][3][2]; | |||||||||
| 63 | 1 | $b{$offset} = genHash('base', size=>$size, type=>$type); | |||||||||
| 64 | } | ||||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | 1 | for my $s(keys %s) # Fix references to base types | |||||||||
| 68 | 1 | {my $fields = $s{$s}->fields; | |||||||||
| 69 | 1 | for my $f(sort keys %$fields) | |||||||||
| 70 | 3 | {my $type = $$fields{$f}->type; | |||||||||
| 71 | 3 | ▶ | if (my $b = $b{$type}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 3 | {$$fields{$f}->size = $b->size; | |||||||||
| 73 | 3 | $$fields{$f}->type = $b->type; | |||||||||
| 74 | } | ||||||||||
| 75 | else | ||||||||||
| 76 | 0 | {say STDERR "No base for offset: $type"; | |||||||||
| 77 | } | ||||||||||
| 78 | } | ||||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | 1 | $extractCStructure{$input} = \%s # Structure details | |||||||||
| 82 | } # extractCStructure | ||||||||||
| 83 | |||||||||||
| 84 | sub extractCField($$$) # Extract the details of a field in a structure in a C file | ||||||||||
| 85 | 12 | ▶ | {my ($input, $structure, $field) = @_; # Input file, structure name, field within structure | ||||||||
|
Subroutine
extractCField: called CC=4 SCAR=15.2
Pod
documented
| |||||||||||
| 86 | 12 | ▶ | if (my $s = extractCStructure $input) # Structures in file | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 12 | ▶ | {if (my $S = $$s{$structure}) # Found structure | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 12 | ▶ | {if (my $F = $S->fields) # Structure has fields | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 12 | {return $$F{$field}; # Field detail | |||||||||
| 90 | } | ||||||||||
| 91 | } | ||||||||||
| 92 | } | ||||||||||
| 93 | undef # Parse failed or no such structure | ||||||||||
| 94 | 0 | } # extractCField | |||||||||
| 95 | |||||||||||
| 96 | sub extractCFieldLoc($$$) # Extract the offset to the location of a field in a structure in a C file | ||||||||||
| 97 | 3 | ▶ | {my ($input, $structure, $field) = @_; # Input file, structure name, field within structure | ||||||||
|
Subroutine
extractCFieldLoc: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||
| 98 | 3 | ▶ | if (my $f = extractCField($input, $structure, $field)) # Structures in file | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | 3 | {return $f->loc; # Offset to field location | |||||||||
| 100 | } | ||||||||||
| 101 | undef # Parse failed or no such structure or no such field | ||||||||||
| 102 | 0 | } # extractCFieldLoc | |||||||||
| 103 | |||||||||||
| 104 | sub extractCFieldSize($$$) # Extract the size of a field in a structure in a C file | ||||||||||
| 105 | 3 | ▶ | {my ($input, $structure, $field) = @_; # Input file, structure name, field within structure | ||||||||
|
Subroutine
extractCFieldSize: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||
| 106 | 3 | ▶ | if (my $f = extractCField($input, $structure, $field)) # Structures in file | ||||||||
|
Branch: 1 branch
| |||||||||||
| 107 | 3 | {return $f->size; # Size of field | |||||||||
| 108 | } | ||||||||||
| 109 | undef # Parse failed or no such structure or no such field | ||||||||||
| 110 | 0 | } # extractCFieldSize | |||||||||
| 111 | |||||||||||
| 112 | sub extractCFieldType($$$) # Extract the type of a field in a structure in a C file | ||||||||||
| 113 | 3 | ▶ | {my ($input, $structure, $field) = @_; # Input file, structure name, field within structure | ||||||||
|
Subroutine
extractCFieldType: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||
| 114 | 3 | ▶ | if (my $f = extractCField($input, $structure, $field)) # Structures in file | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | 3 | {return $f->type; # Type of field | |||||||||
| 116 | } | ||||||||||
| 117 | undef # Parse failed or no such structure or no such field | ||||||||||
| 118 | 0 | } # extractCFieldType | |||||||||
| 119 | |||||||||||
| 120 | sub extractCStructureFields($$) # Extract the names of the fields in a C structure | ||||||||||
| 121 | 1 | ▶ | {my ($input, $structure) = @_; # Input file, structure name | ||||||||
|
Subroutine
extractCStructureFields: called CC=4 SCAR=15.2
Pod
documented
| |||||||||||
| 122 | 1 | ▶ | if (my $s = extractCStructure $input) # Structures in file | ||||||||
|
Branch: 1 branch
| |||||||||||
| 123 | 1 | ▶ | {if (my $S = $$s{$structure}) # Found structure | ||||||||
|
Branch: 1 branch
| |||||||||||
| 124 | 1 | ▶ | {if (my $F = $S->fields) # Structure has fields | ||||||||
|
Branch: 1 branch
| |||||||||||
| 125 | 1 | {return sort keys %$F; # Return names of fields in structure in ascending order | |||||||||
| 126 | } | ||||||||||
| 127 | } | ||||||||||
| 128 | } | ||||||||||
| 129 | () # Parse failed or no such structure | ||||||||||
| 130 | 0 | } # extractCStructureSize | |||||||||
| 131 | |||||||||||
| 132 | sub extractCStructureSize($$) # Extract the size of a C structure | ||||||||||
| 133 | 1 | ▶ | {my ($input, $structure) = @_; # Input file, structure name | ||||||||
|
Subroutine
extractCStructureSize: called CC=3 SCAR=12.0
Pod
documented
| |||||||||||
| 134 | 1 | ▶ | if (my $s = extractCStructure $input) # Structures in file | ||||||||
|
Branch: 1 branch
| |||||||||||
| 135 | 1 | ▶ | {if (my $S = $$s{$structure}) # Found structure | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | 1 | {return $S->size; # Return structure size | |||||||||
| 137 | } | ||||||||||
| 138 | } | ||||||||||
| 139 | undef # Parse failed or no such structure | ||||||||||
| 140 | 0 | } # extractCStructureSize | |||||||||
| 141 | |||||||||||
| 142 | #D2 Macros # Extract macro values from C header files | ||||||||||
| 143 | |||||||||||
| 144 | my %extractMacroDefinitionsFromCHeaderFile; # Cache macro definitions | ||||||||||
| 145 | |||||||||||
| 146 | sub extractMacroDefinitionsFromCHeaderFile($) # Extract the macro definitions found in a C header file using gcc | ||||||||||
| 147 | 2 | ▶ | {my ($includeFile) = @_; # C Header file name as it would be entered in a C program | ||||||||
|
Subroutine
extractMacroDefinitionsFromCHeaderFile: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 148 | 2 | my $d = $extractMacroDefinitionsFromCHeaderFile{$includeFile}; # Cached macro definitions | |||||||||
| 149 | 2 | ▶ | return $d if $d; # Return cached value | ||||||||
|
Branch: 1 branch
| |||||||||||
| 150 | |||||||||||
| 151 | 1 | confirmHasCommandLineCommand("gcc"); # Check gcc | |||||||||
| 152 | 1 | my @l = qx(gcc -E -dM -include "$includeFile" - < /dev/null); # Use gcc to extract macro definitions | |||||||||
| 153 | |||||||||||
| 154 | 1 | my %d; | |||||||||
| 155 | 1 | for my $l(@l) # Extract macro definitions | |||||||||
| 156 | 530 | ▶ | {if ($l =~ m(\A#define\s+(\S+)\s+(\S+)(.*))) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 157 | 512 | {$d{$1} = $2; | |||||||||
| 158 | } | ||||||||||
| 159 | } | ||||||||||
| 160 | |||||||||||
| 161 | 1 | $extractMacroDefinitionsFromCHeaderFile{$includeFile} = \%d; # Return definitions | |||||||||
| 162 | } | ||||||||||
| 163 | |||||||||||
| 164 | sub extractMacroDefinitionFromCHeaderFile($$) # Extract a macro definitions found in a C header file using gcc | ||||||||||
| 165 | 1 | ▶ | {my ($includeFile, $macro) = @_; # C Header file name as it would be entered in a C program, macro name | ||||||||
|
Subroutine
extractMacroDefinitionFromCHeaderFile: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||
| 166 | 1 | ▶ | if (my $d = extractMacroDefinitionsFromCHeaderFile($includeFile)) # Get macro definitions | ||||||||
|
Branch: 1 branch
| |||||||||||
| 167 | 1 | {return $$d{$macro}; | |||||||||
| 168 | } | ||||||||||
| 169 | undef | ||||||||||
| 170 | 0 | } | |||||||||
| 171 | |||||||||||
| 172 | #d | ||||||||||
| 173 | #------------------------------------------------------------------------------- | ||||||||||
| 174 | # Export - eeee | ||||||||||
| 175 | #------------------------------------------------------------------------------- | ||||||||||
| 176 | |||||||||||
| 177 | 1 | ▶ | use Exporter qw(import); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 178 | |||||||||||
| 179 | 1 | ▶ | use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 180 | |||||||||||
| 181 | 1 | @ISA = qw(Exporter); | |||||||||
| 182 | 1 | @EXPORT = qw(); | |||||||||
| 183 | 1 | @EXPORT_OK = qw( | |||||||||
| 184 | extractCField | ||||||||||
| 185 | extractCFieldLoc | ||||||||||
| 186 | extractCFieldSize | ||||||||||
| 187 | extractCFieldType | ||||||||||
| 188 | extractCStructure | ||||||||||
| 189 | extractCStructureFields | ||||||||||
| 190 | extractCStructureSize | ||||||||||
| 191 | extractMacroDefinitionFromCHeaderFile | ||||||||||
| 192 | extractMacroDefinitionsFromCHeaderFile | ||||||||||
| 193 | ); | ||||||||||
| 194 | 1 | %EXPORT_TAGS = (all=>[@EXPORT, @EXPORT_OK]); | |||||||||
| 195 | |||||||||||
| 196 | # podDocumentation | ||||||||||
| 197 | =pod | ||||||||||
| 198 | |||||||||||
| 199 | =encoding utf-8 | ||||||||||
| 200 | |||||||||||
| 201 | =head1 Name | ||||||||||
| 202 | |||||||||||
| 203 | Asm::C - Extract macro values and structure details from C programs. | ||||||||||
| 204 | |||||||||||
| 205 | =head1 Synopsis | ||||||||||
| 206 | |||||||||||
| 207 | =head2 Extract structure details from C programs. | ||||||||||
| 208 | |||||||||||
| 209 | Given: | ||||||||||
| 210 | |||||||||||
| 211 | struct S | ||||||||||
| 212 | {int a; | ||||||||||
| 213 | int b; | ||||||||||
| 214 | int c; | ||||||||||
| 215 | } s; | ||||||||||
| 216 | void main() {} | ||||||||||
| 217 | |||||||||||
| 218 | Get: | ||||||||||
| 219 | |||||||||||
| 220 | is_deeply extractCStructure($input), | ||||||||||
| 221 | { S => bless({ | ||||||||||
| 222 | fields => { | ||||||||||
| 223 | a => bless({field => "a", loc => 0, size => 4, type => "int"}, "field"), | ||||||||||
| 224 | b => bless({field => "b", loc => 4, size => 4, type => "int"}, "field"), | ||||||||||
| 225 | c => bless({field => "c", loc => 8, size => 4, type => "int"}, "field"), | ||||||||||
| 226 | }, | ||||||||||
| 227 | size => 12, | ||||||||||
| 228 | }, "structure")}; | ||||||||||
| 229 | |||||||||||
| 230 | =head2 Extract macro values from a C header file. | ||||||||||
| 231 | |||||||||||
| 232 | Find the value of a macro definition in a C header file: | ||||||||||
| 233 | |||||||||||
| 234 | my $m = extractMacroDefinitionsFromCHeaderFile("linux/mman.h"); | ||||||||||
| 235 | |||||||||||
| 236 | is_deeply $$m{MAP_ANONYMOUS}, "0x20"; | ||||||||||
| 237 | |||||||||||
| 238 | =head1 Description | ||||||||||
| 239 | |||||||||||
| 240 | Extract macro values and structure details from C programs. | ||||||||||
| 241 | |||||||||||
| 242 | |||||||||||
| 243 | Version "20210328". | ||||||||||
| 244 | |||||||||||
| 245 | |||||||||||
| 246 | The following sections describe the methods in each functional area of this | ||||||||||
| 247 | module. For an alphabetic listing of all methods by name see L<Index|/Index>. | ||||||||||
| 248 | |||||||||||
| 249 | |||||||||||
| 250 | |||||||||||
| 251 | =head1 Asm::C | ||||||||||
| 252 | |||||||||||
| 253 | Extract macro values and structure details from C programs. | ||||||||||
| 254 | |||||||||||
| 255 | =head2 Structures | ||||||||||
| 256 | |||||||||||
| 257 | Extract structure details from C programs. | ||||||||||
| 258 | |||||||||||
| 259 | =head3 extractCStructure($input) | ||||||||||
| 260 | |||||||||||
| 261 | Extract the details of a structure | ||||||||||
| 262 | |||||||||||
| 263 | Parameter Description | ||||||||||
| 264 | 1 $input Input C file - a temporary one is ok | ||||||||||
| 265 | |||||||||||
| 266 | B<Example:> | ||||||||||
| 267 | |||||||||||
| 268 | |||||||||||
| 269 | |||||||||||
| 270 | is_deeply extractCStructure($input), # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲 | ||||||||||
| 271 | |||||||||||
| 272 | |||||||||||
| 273 | |||||||||||
| 274 | =head3 extractCField($input, $structure, $field) | ||||||||||
| 275 | |||||||||||
| 276 | Extract the details of a field in a structure in a C file | ||||||||||
| 277 | |||||||||||
| 278 | Parameter Description | ||||||||||
| 279 | 1 $input Input file | ||||||||||
| 280 | 2 $structure Structure name | ||||||||||
| 281 | 3 $field Field within structure | ||||||||||
| 282 | |||||||||||
| 283 | B<Example:> | ||||||||||
| 284 | |||||||||||
| 285 | |||||||||||
| 286 | if (1) | ||||||||||
| 287 | {my $input = writeTempFile <<END; | ||||||||||
| 288 | struct S | ||||||||||
| 289 | {int a; | ||||||||||
| 290 | int b; | ||||||||||
| 291 | int c; | ||||||||||
| 292 | } s; | ||||||||||
| 293 | |||||||||||
| 294 | |||||||||||
| 295 | =head3 extractCFieldLoc($input, $structure, $field) | ||||||||||
| 296 | |||||||||||
| 297 | Extract the offset to the location of a field in a structure in a C file | ||||||||||
| 298 | |||||||||||
| 299 | Parameter Description | ||||||||||
| 300 | 1 $input Input file | ||||||||||
| 301 | 2 $structure Structure name | ||||||||||
| 302 | 3 $field Field within structure | ||||||||||
| 303 | |||||||||||
| 304 | B<Example:> | ||||||||||
| 305 | |||||||||||
| 306 | |||||||||||
| 307 | if (1) | ||||||||||
| 308 | {my $input = writeTempFile <<END; | ||||||||||
| 309 | struct S | ||||||||||
| 310 | {int a; | ||||||||||
| 311 | int b; | ||||||||||
| 312 | int c; | ||||||||||
| 313 | } s; | ||||||||||
| 314 | |||||||||||
| 315 | |||||||||||
| 316 | =head3 extractCFieldSize($input, $structure, $field) | ||||||||||
| 317 | |||||||||||
| 318 | Extract the size of a field in a structure in a C file | ||||||||||
| 319 | |||||||||||
| 320 | Parameter Description | ||||||||||
| 321 | 1 $input Input file | ||||||||||
| 322 | 2 $structure Structure name | ||||||||||
| 323 | 3 $field Field within structure | ||||||||||
| 324 | |||||||||||
| 325 | B<Example:> | ||||||||||
| 326 | |||||||||||
| 327 | |||||||||||
| 328 | if (1) | ||||||||||
| 329 | {my $input = writeTempFile <<END; | ||||||||||
| 330 | struct S | ||||||||||
| 331 | {int a; | ||||||||||
| 332 | int b; | ||||||||||
| 333 | int c; | ||||||||||
| 334 | } s; | ||||||||||
| 335 | |||||||||||
| 336 | |||||||||||
| 337 | =head3 extractCFieldType($input, $structure, $field) | ||||||||||
| 338 | |||||||||||
| 339 | Extract the type of a field in a structure in a C file | ||||||||||
| 340 | |||||||||||
| 341 | Parameter Description | ||||||||||
| 342 | 1 $input Input file | ||||||||||
| 343 | 2 $structure Structure name | ||||||||||
| 344 | 3 $field Field within structure | ||||||||||
| 345 | |||||||||||
| 346 | B<Example:> | ||||||||||
| 347 | |||||||||||
| 348 | |||||||||||
| 349 | if (1) | ||||||||||
| 350 | {my $input = writeTempFile <<END; | ||||||||||
| 351 | struct S | ||||||||||
| 352 | {int a; | ||||||||||
| 353 | int b; | ||||||||||
| 354 | int c; | ||||||||||
| 355 | } s; | ||||||||||
| 356 | |||||||||||
| 357 | |||||||||||
| 358 | =head3 extractCStructureFields($input, $structure) | ||||||||||
| 359 | |||||||||||
| 360 | Extract the names of the fields in a C structure | ||||||||||
| 361 | |||||||||||
| 362 | Parameter Description | ||||||||||
| 363 | 1 $input Input file | ||||||||||
| 364 | 2 $structure Structure name | ||||||||||
| 365 | |||||||||||
| 366 | B<Example:> | ||||||||||
| 367 | |||||||||||
| 368 | |||||||||||
| 369 | if (1) | ||||||||||
| 370 | {my $input = writeTempFile <<END; | ||||||||||
| 371 | struct S | ||||||||||
| 372 | {int a; | ||||||||||
| 373 | int b; | ||||||||||
| 374 | int c; | ||||||||||
| 375 | } s; | ||||||||||
| 376 | |||||||||||
| 377 | |||||||||||
| 378 | =head3 extractCStructureSize($input, $structure) | ||||||||||
| 379 | |||||||||||
| 380 | Extract the size of a C structure | ||||||||||
| 381 | |||||||||||
| 382 | Parameter Description | ||||||||||
| 383 | 1 $input Input file | ||||||||||
| 384 | 2 $structure Structure name | ||||||||||
| 385 | |||||||||||
| 386 | B<Example:> | ||||||||||
| 387 | |||||||||||
| 388 | |||||||||||
| 389 | if (1) | ||||||||||
| 390 | {my $input = writeTempFile <<END; | ||||||||||
| 391 | struct S | ||||||||||
| 392 | {int a; | ||||||||||
| 393 | int b; | ||||||||||
| 394 | int c; | ||||||||||
| 395 | } s; | ||||||||||
| 396 | |||||||||||
| 397 | |||||||||||
| 398 | =head2 Macros | ||||||||||
| 399 | |||||||||||
| 400 | Extract macro values from C header files | ||||||||||
| 401 | |||||||||||
| 402 | =head3 extractMacroDefinitionsFromCHeaderFile($includeFile) | ||||||||||
| 403 | |||||||||||
| 404 | Extract the macro definitions found in a C header file using gcc | ||||||||||
| 405 | |||||||||||
| 406 | Parameter Description | ||||||||||
| 407 | 1 $includeFile C Header file name as it would be entered in a C program | ||||||||||
| 408 | |||||||||||
| 409 | B<Example:> | ||||||||||
| 410 | |||||||||||
| 411 | |||||||||||
| 412 | if (1) | ||||||||||
| 413 | {my $h = "linux/mman.h"; | ||||||||||
| 414 | |||||||||||
| 415 | my $m = extractMacroDefinitionsFromCHeaderFile("linux/mman.h"); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲 | ||||||||||
| 416 | |||||||||||
| 417 | is_deeply $$m{MAP_ANONYMOUS}, "0x20"; | ||||||||||
| 418 | ok extractMacroDefinitionFromCHeaderFile("linux/mman.h", q(PROT_WRITE)) eq "0x2"; | ||||||||||
| 419 | } | ||||||||||
| 420 | |||||||||||
| 421 | |||||||||||
| 422 | =head3 extractMacroDefinitionFromCHeaderFile($includeFile, $macro) | ||||||||||
| 423 | |||||||||||
| 424 | Extract a macro definitions found in a C header file using gcc | ||||||||||
| 425 | |||||||||||
| 426 | Parameter Description | ||||||||||
| 427 | 1 $includeFile C Header file name as it would be entered in a C program | ||||||||||
| 428 | 2 $macro Macro name | ||||||||||
| 429 | |||||||||||
| 430 | B<Example:> | ||||||||||
| 431 | |||||||||||
| 432 | |||||||||||
| 433 | if (1) | ||||||||||
| 434 | {my $h = "linux/mman.h"; | ||||||||||
| 435 | my $m = extractMacroDefinitionsFromCHeaderFile("linux/mman.h"); | ||||||||||
| 436 | is_deeply $$m{MAP_ANONYMOUS}, "0x20"; | ||||||||||
| 437 | |||||||||||
| 438 | ok extractMacroDefinitionFromCHeaderFile("linux/mman.h", q(PROT_WRITE)) eq "0x2"; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲 | ||||||||||
| 439 | |||||||||||
| 440 | } | ||||||||||
| 441 | |||||||||||
| 442 | |||||||||||
| 443 | |||||||||||
| 444 | =head1 Index | ||||||||||
| 445 | |||||||||||
| 446 | |||||||||||
| 447 | 1 L<extractCField|/extractCField> - Extract the details of a field in a structure in a C file | ||||||||||
| 448 | |||||||||||
| 449 | 2 L<extractCFieldLoc|/extractCFieldLoc> - Extract the offset to the location of a field in a structure in a C file | ||||||||||
| 450 | |||||||||||
| 451 | 3 L<extractCFieldSize|/extractCFieldSize> - Extract the size of a field in a structure in a C file | ||||||||||
| 452 | |||||||||||
| 453 | 4 L<extractCFieldType|/extractCFieldType> - Extract the type of a field in a structure in a C file | ||||||||||
| 454 | |||||||||||
| 455 | 5 L<extractCStructure|/extractCStructure> - Extract the details of a structure | ||||||||||
| 456 | |||||||||||
| 457 | 6 L<extractCStructureFields|/extractCStructureFields> - Extract the names of the fields in a C structure | ||||||||||
| 458 | |||||||||||
| 459 | 7 L<extractCStructureSize|/extractCStructureSize> - Extract the size of a C structure | ||||||||||
| 460 | |||||||||||
| 461 | 8 L<extractMacroDefinitionFromCHeaderFile|/extractMacroDefinitionFromCHeaderFile> - Extract a macro definitions found in a C header file using gcc | ||||||||||
| 462 | |||||||||||
| 463 | 9 L<extractMacroDefinitionsFromCHeaderFile|/extractMacroDefinitionsFromCHeaderFile> - Extract the macro definitions found in a C header file using gcc | ||||||||||
| 464 | |||||||||||
| 465 | =head1 Installation | ||||||||||
| 466 | |||||||||||
| 467 | This module is written in 100% Pure Perl and, thus, it is easy to read, | ||||||||||
| 468 | comprehend, use, modify and install via B<cpan>: | ||||||||||
| 469 | |||||||||||
| 470 | sudo cpan install Asm::C | ||||||||||
| 471 | |||||||||||
| 472 | =head1 Author | ||||||||||
| 473 | |||||||||||
| 474 | L<philiprbrenan@gmail.com|mailto:philiprbrenan@gmail.com> | ||||||||||
| 475 | |||||||||||
| 476 | L<http://www.appaapps.com|http://www.appaapps.com> | ||||||||||
| 477 | |||||||||||
| 478 | =head1 Copyright | ||||||||||
| 479 | |||||||||||
| 480 | Copyright (c) 2016-2021 Philip R Brenan. | ||||||||||
| 481 | |||||||||||
| 482 | This module is free software. It may be used, redistributed and/or modified | ||||||||||
| 483 | under the same terms as Perl itself. | ||||||||||
| 484 | |||||||||||
| 485 | =cut | ||||||||||
| 486 | |||||||||||
| 487 | |||||||||||
| 488 | |||||||||||
| 489 | # Tests and documentation | ||||||||||
| 490 | |||||||||||
| 491 | sub test | ||||||||||
| 492 | 1 | ▶ | {my $p = __PACKAGE__; | ||||||||
|
Subroutine
test: called CC=5 SCAR=16.3
Pod
undocumented
| |||||||||||
| 493 | 1 | binmode($_, ":utf8") for *STDOUT, *STDERR; | |||||||||
| 494 | 1 | ▶ | return if eval "eof(${p}::DATA)"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 495 | 1 | my $s = eval "join('', <${p}::DATA>)"; | |||||||||
| 496 | 1 | ▶ | $@ and die $@; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 497 | 1 | ▶ | eval $s; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 498 | 1 | ▶ | $@ and die $@; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 499 | 1 | 1 | |||||||||
| 500 | } | ||||||||||
| 501 | |||||||||||
| 502 | 1 | ▶ | test unless caller; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 503 | |||||||||||
| 504 | 1 | 1; | |||||||||
| 505 | # podDocumentation | ||||||||||
| 506 | __DATA__ | ||||||||||