Package: dwarfgen
DwarfgenDescriptionUse dwarfgen toGenerate datastructures that promote easy domain specific analysis of codeGenerate code in other languagesGenerate IDL's (Interactive Data Language)Dwarfgen introduces JIDL (JSON IDL) to the already crowded IDL space.  JIDL
is less about being an IDL and more about being an intermediate representation
of your code for downstream processing.// sample.cpp, compiled with '-g -fPIC -gdwarf-2' and default alignmenttypedefstructMyStruct{chara;intb;}MyStruct;// output of 'dwarfgen --file sample.o --to-idl jidl --to-idl-dest ~/jidl'{"namespaces":{},"enumerations":{},"unions":{},"structures":{"MyStruct":{"byteSize":8,"members":{"a":{"byteOffset":0,"byteSize":1,"type":"char","accessibility":"public"},"b":{"byteOffset":4,"byteSize":4,"type":"int","accessibility":"public"}}}}}# Some simple domain-specific static analysisimportjsonwithopen('/path/to/jidl.json','r')asf:jidl=json.load(f)# list all public integersforname,datainjidl['structures'].items():formember_name,member_dataindata['members'].items():ifmember_data['accessibility']=='public'andmember_data['type']=='int':print("{}.{}".format(name,member_name)+" is a public int!")The above example isn't extremely complicated or even useful.  It's meant to illustrate the simplicity of JIDL analysis
where source code analysis would be rather difficult to implement correctly.  The amount of effort required for source
level analysis only increases when multiple languages are involved.A more useful example would be to ensure types are identical in size, within and across shared objects.importjsonwithopen('/path/to/jidl_1.json','r')asf:jidl_1=json.load(f)withopen('/path/to/jid_2.json','r')asf:jidl_2=json.load(f)# get structure 1 sizestructure_1_size=jidl_1['structures']['Structure1']['byteSize']# get structure 2 size, that resides in a different namespace and is of a different namestructure_2_size=jidl_2['namespaces']['InnerNamespace']['structures']['Structure2']['byteSize']# ensure they are equalifstructure_1_size!=structure_2_size:print("Structure1 != InnerNamespace::Structure2 !!")One thing that can be easily missed when analyzing source code directly are packing rules of languages as well as
compiler options that can adjust alignment of structures.For exampletypedefstructStructA{chara;intb;}StructA;#pragma pack(1)typedefstructStructB{chara;intb;}StructB;StructA bytesize is 8 while StructB bytesize is 5.  The#pragma pack(1)statement tells the compiler to not add
padding to the structure.  This is easily handled by dwarfgen but can be difficult to implement correctly otherwise.In general, analyzing the output of the compiler hides almost all complicating factors versus analyzing the source code
directly.  The simple reason is that you would have to re-implement all compiler specific rules in your analysis
(including platform specific differences)Installpip install dwarfgenDWARF Version Support:heavy_check_mark: - Language specific features implemented:warning: - Core DWARF features implemented, language specific features are not implemented:x: - Not implementedAdaCC++CobolFortranDWARF v2:heavy_check_mark::heavy_check_mark::heavy_check_mark::warning::warning:DWARF v3:heavy_check_mark::heavy_check_mark::heavy_check_mark::warning::warning:DWARF v4:heavy_check_mark::heavy_check_mark::heavy_check_mark::warning::warning:DWARF v5:x::x::x::x::x:Compiler Support:heavy_check_mark: - Tested:warning: - Untested:x: - Not ImplementedgccllvmDWARF v2:heavy_check_mark::warning:DWARF v3:heavy_check_mark::warning:DWARF v4:heavy_check_mark::warning:DWARF v5:x::x:Language-Feature Support:heavy_check_mark: - Implemented:warning: - Results may vary:x: - Not implementedCommonAccessibilityArraysCustom TypesDWARF v2:heavy_check_mark::heavy_check_mark::heavy_check_mark:DWARF v3:heavy_check_mark::heavy_check_mark::heavy_check_mark:DWARF v4:heavy_check_mark::heavy_check_mark::heavy_check_mark:DWARF v5:x::x::x:AdaConstrained TypesRecordsRepspecTagged TypesDWARF v2:heavy_check_mark::heavy_check_mark::heavy_check_mark::heavy_check_mark:DWARF v3:heavy_check_mark::heavy_check_mark::heavy_check_mark::heavy_check_mark:DWARF v4:heavy_check_mark::heavy_check_mark::heavy_check_mark::heavy_check_mark:DWARF v5:x::x::x::x:CEnumerationsStructuresUnionsBitfieldsDWARF v2:heavy_check_mark::heavy_check_mark::heavy_check_mark::heavy_check_mark:DWARF v3:heavy_check_mark::heavy_check_mark::heavy_check_mark::heavy_check_mark:DWARF v4:heavy_check_mark::heavy_check_mark::heavy_check_mark::heavy_check_mark:DWARF v5:x::x::x::x:C++EnumerationsStructuresClassesUnionsBitfieldsInheritanceSTLTemplatesDWARF v2:heavy_check_mark::heavy_check_mark::heavy_check_mark::heavy_check_mark::heavy_check_mark::heavy_check_mark::warning::warning:DWARF v3:heavy_check_mark::heavy_check_mark::heavy_check_mark::heavy_check_mark::heavy_check_mark::heavy_check_mark::warning::warning:DWARF v4:heavy_check_mark::heavy_check_mark::heavy_check_mark::heavy_check_mark::heavy_check_mark::heavy_check_mark::warning::warning:DWARF v5:x::x::x::x::x::x::x::x:Examples# Help commandpython-mdwarfgen-h# Generate JIDL from a shared objectpython-mdwarfgen--file/path/to/shared_object.so--to-idljidl--to-idl-dest~/jidl# Generate cpp code from a shared objectpython-mdwarfgen--file/path/to/shared_object.so--to-langcpp--to-lang-dest~/autogen/cpp# Register a custom language generator module and generate in that languagepython-mdwarfgen--file/path/to/shared_object.so\--lang-generatorpython/path/to/module\--to-langpython\--to-lang-dest~/autogen/python