Uso de metadatos de Objective-C para generar un gráfico de dependencia de clases

This guy came up with a pretty neat tool to generate a class dependency graph - however, it relies on parsing your source code and looking for #import directivas

This is neat, but I have a number of problems with this. Not least of which is it doesn't take into account imports of imports nor prefix headers nor whether-or-not the class(es) in the file referenced by the import are actually being used.

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

I'd rather not do this from scratch, so I'm wondering:

  • Has it already been done?
  • Is there an open-source library which would provide me with the foundational tools I need to extract this information (a library which examines the Mach-O file and creates a façade of the Objective-C information contained within - such that I could iterate over all of the classes, their methods, properties, ivars, etc and scan for references to other classes) I figure class-dump's source would be a good place to start.
  • If you have experience in this sort of thing, is what I'm trying to accomplish feasible?
  • What roadblocks will I need to overcome?

preguntado el 28 de agosto de 11 a las 04:08

3 Respuestas

Has it already been done?

No que yo sepa.

Is there an open-source library which would provide me with the foundational tools I need to extract this information?

En el Centro de volcado de clases is libMachObjC which does exatly what you want, i.e. parse all classes/methods/ivars and more. The API is very clean, it should be very easy to use.

If you have experience in this sort of thing, is what I'm trying to accomplish feasible?

Unfortunately, no because some classes don't declare the real class but use id instead. For example, here is the information that can be extracted from a class-dump of UIKit:

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

Al _rowData ivar type information is id but if you check at runtime you will see that _rowData es una instancia del UITableViewRowData class. This information is not present in the Mach-O binary so you have no way to find the relation between UITableView y UITableViewRowData. The same applies for method parameters.

Respondido 29 ago 11, 16:08

Thanks for the response! I understand the id issue. However, one of two possibilities exist: UN.) The actual class being used for _rowData isn't actually a dependency of your sample class. or SEGUNDO.) The actual class being used for _rowData is referenced by one of your sample class' methods - in which case, scanning the contents of all the methods would allow me to find that dependency. - Steve

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

respondido 27 nov., 15:01

Has it already been done?

yes - but i can't recommend a good public implementation

Is there an open-source library which would provide me with the foundational tools I need to extract this information (a library which examines the Mach-O file and creates a façade of the Objective-C information contained within - such that I could iterate over all of the classes, their methods, properties, ivars, etc and scan for references to other classes) I figure class-dump's source would be a good place to start.

most use cases would benefit by using the objc runtime facilities objc/... rather than examining the binary.

If you have experience in this sort of thing, is what I'm trying to accomplish feasible?

yes. i've done something similar using the objc runtime.

What roadblocks will I need to overcome?

that depends largely on the level of detail you want... implementation time if you find no such implementation, but i figure you will find a few options if you google the more esoteric functions in the objc runtime; perhaps you would find one in an (open) language binding or bridge?

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

Respondido 28 ago 11, 10:08

Justin, thanks for the feedback! I'm not aware of any objective c runtime methods which will let me see what classes are being used from inside a method. I know I can list the methods available on a class, and get a lst of classes, but how would I know that class Foo was used inside some method? - Steve

@Steve right. you'll be able to get detailed object layouts, extract public and private interfaces, protocol lists, etc (although objc object arguments decay to id in the abi), but this approach will not tell you what goes on in the implementation. Instruments, grpof, and gcov can help you with that. since objc is a very dynamic language, it is ideal to evaluate the execution for such types. if you need that level of detail - class dump may be a good starting point. i was not originally under the impression you wanted that much detail. - Justin

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.