The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

Name

Asm::C - Extract macro values and structure details from C programs.

Synopsis

Extract structure details from C programs.

Given:

  struct S
   {int a;
    int b;
    int c;
   } s;
  void main() {}

Get:

  is_deeply extractCStructure($input),
 { S => bless({
     fields => {
       a => bless({field => "a", loc => 0, size => 4, type => "int"}, "field"),
       b => bless({field => "b", loc => 4, size => 4, type => "int"}, "field"),
       c => bless({field => "c", loc => 8, size => 4, type => "int"}, "field"),
     },
     size   => 12,
  }, "structure")};

Extract macro values from a C header file.

Find the value of a macro definition in a C header file:

  my $m = extractMacroDefinitionsFromCHeaderFile("linux/mman.h");

  is_deeply $$m{MAP_ANONYMOUS}, "0x20";

Description

Extract macro values and structure details from C programs.

Version "20210328".

The following sections describe the methods in each functional area of this module. For an alphabetic listing of all methods by name see Index.

Asm::C

Extract macro values and structure details from C programs.

Structures

Extract structure details from C programs.

extractCStructure($input)

Extract the details of a structure

     Parameter  Description
  1  $input     Input C file - a temporary one is ok

Example:

    is_deeply extractCStructure($input),                                            # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲

extractCField($input, $structure, $field)

Extract the details of a field in a structure in a C file

     Parameter   Description
  1  $input      Input file
  2  $structure  Structure name
  3  $field      Field within structure

Example:

  if (1)
   {my $input = writeTempFile <<END;
  struct S
   {int a;
    int b;
    int c;
   } s;

extractCFieldLoc($input, $structure, $field)

Extract the offset to the location of a field in a structure in a C file

     Parameter   Description
  1  $input      Input file
  2  $structure  Structure name
  3  $field      Field within structure

Example:

  if (1)
   {my $input = writeTempFile <<END;
  struct S
   {int a;
    int b;
    int c;
   } s;

extractCFieldSize($input, $structure, $field)

Extract the size of a field in a structure in a C file

     Parameter   Description
  1  $input      Input file
  2  $structure  Structure name
  3  $field      Field within structure

Example:

  if (1)
   {my $input = writeTempFile <<END;
  struct S
   {int a;
    int b;
    int c;
   } s;

extractCFieldType($input, $structure, $field)

Extract the type of a field in a structure in a C file

     Parameter   Description
  1  $input      Input file
  2  $structure  Structure name
  3  $field      Field within structure

Example:

  if (1)
   {my $input = writeTempFile <<END;
  struct S
   {int a;
    int b;
    int c;
   } s;

extractCStructureFields($input, $structure)

Extract the names of the fields in a C structure

     Parameter   Description
  1  $input      Input file
  2  $structure  Structure name

Example:

  if (1)
   {my $input = writeTempFile <<END;
  struct S
   {int a;
    int b;
    int c;
   } s;

extractCStructureSize($input, $structure)

Extract the size of a C structure

     Parameter   Description
  1  $input      Input file
  2  $structure  Structure name

Example:

  if (1)
   {my $input = writeTempFile <<END;
  struct S
   {int a;
    int b;
    int c;
   } s;

Macros

Extract macro values from C header files

extractMacroDefinitionsFromCHeaderFile($includeFile)

Extract the macro definitions found in a C header file using gcc

     Parameter     Description
  1  $includeFile  C Header file name as it would be entered in a C program

Example:

  if (1)
   {my $h = "linux/mman.h";

    my $m = extractMacroDefinitionsFromCHeaderFile("linux/mman.h");  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲

    is_deeply $$m{MAP_ANONYMOUS}, "0x20";
    ok extractMacroDefinitionFromCHeaderFile("linux/mman.h", q(PROT_WRITE)) eq "0x2";
   }

extractMacroDefinitionFromCHeaderFile($includeFile, $macro)

Extract a macro definitions found in a C header file using gcc

     Parameter     Description
  1  $includeFile  C Header file name as it would be entered in a C program
  2  $macro        Macro name

Example:

  if (1)
   {my $h = "linux/mman.h";
    my $m = extractMacroDefinitionsFromCHeaderFile("linux/mman.h");
    is_deeply $$m{MAP_ANONYMOUS}, "0x20";

    ok extractMacroDefinitionFromCHeaderFile("linux/mman.h", q(PROT_WRITE)) eq "0x2";  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲

   }

Index

1 extractCField - Extract the details of a field in a structure in a C file

2 extractCFieldLoc - Extract the offset to the location of a field in a structure in a C file

3 extractCFieldSize - Extract the size of a field in a structure in a C file

4 extractCFieldType - Extract the type of a field in a structure in a C file

5 extractCStructure - Extract the details of a structure

6 extractCStructureFields - Extract the names of the fields in a C structure

7 extractCStructureSize - Extract the size of a C structure

8 extractMacroDefinitionFromCHeaderFile - Extract a macro definitions found in a C header file using gcc

9 extractMacroDefinitionsFromCHeaderFile - Extract the macro definitions found in a C header file using gcc

Installation

This module is written in 100% Pure Perl and, thus, it is easy to read, comprehend, use, modify and install via cpan:

  sudo cpan install Asm::C

Author

philiprbrenan@gmail.com

http://www.appaapps.com

Copyright

Copyright (c) 2016-2021 Philip R Brenan.

This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.