Using Objective-C Metadata to Generate a Class Dependency Graph

This guy came up with a pretty neat tool for generating a class dependency graph, but he relies on parsing the source code and searching for #import directives.

This is neat, but there are a lot of problems. Last but not least, it does not take into account import imports or prefix headers, and whether classes are actually used in the file the import refers to in reality.

I would like to do something more like class-dump and examine the Objective-C metadata stored in the Mach-O file to generate a representation of the class dependencies in memory.

I would prefer not to do this from scratch, so I wonder:

  • Is it already done?
  • Is there an open source library that would provide me with the basic tools needed to extract this information (a library that parses the Mach-O file and creates an Objective-C facade of the information contained inside), so that I could iterate over all the classes, their methods, properties, ivars, etc. and check references to other classes). I believe that source-dump-source would be a good start.
  • If you have experience with this, am I trying to make this possible?
  • What obstacles do I need to overcome?
+6
source share
3 answers

Is it already done?

Not that I knew.

Is there an open source library that would provide me with Fundamental Tools Do I need to extract this information?

At the core of class-dump is libMachObjC , which does what you want, that is, it parses all classes / methods / ivars and much more, the API is very clean, it should be very easy to use.

If you have experience with this, am I trying to execute a doable?

Unfortunately, no, because some classes do not declare a real class, but use id . For example, here is information that can be extracted from a UIKit class reset:

 @interface UITableView : UIScrollView <NSCoding> { int _style; id <UITableViewDataSource> _dataSource; id _rowData; ... 

Type _rowData ivar id , but if you check at runtime, you will see that _rowData is an instance of the UITableViewRowData class. This information is missing from the Mach-O binary, so you cannot find the relationship between UITableView and UITableViewRowData . The same applies to method parameters.

+3
source

Is it already done?

yes - but I cannot recommend a good public implementation

Is there an open source library that would provide me with the basic tools needed to extract this information (a library that examines the Mach-O file and creates an Objective-C facade of the information contained inside) so that I can iterate through all the classes , their methods, properties, ivars, etc. and check references to other classes). I believe the original source source will be a good start.

In most cases, using it would be useful with objc/... runtime objc/... , rather than checking a binary file.

If you have experience with this, am I trying to make this possible?

Yes. I did something similar using objc runtime.

What obstacles do I need to overcome?

which depends a lot on the level of detail you want ... implementation time if you don't find such an implementation, but I believe that you will find several options if you google more esoteric functions in the objc runtime; maybe you find it in a (open) anchor or bridge language?

if you end up writing one, you can get registered objc classes with objc_getClassList , and then access the properties / information you want from there.

+1
source

Here's a solution that relies on the information in mach.o files and generates a graph dependency on this information: https://github.com/PaulTaykalo/objc-dependency-visualizer enter image description here

0
source

Source: https://habr.com/ru/post/896062/


All Articles