使用 Objective-C 元数据生成类依赖图

发布于 2024-12-02 04:01:10 字数 801 浏览 2 评论 0原文

这个人想出了一个非常简洁的工具来生成类依赖图 - 但是,它依赖于解析源代码并查找 #import 指令。

这很简洁,但我对此有很多问题。尤其重要的是,它没有考虑导入的导入,也没有考虑前缀标头,也没有考虑导入引用的文件中的类是否实际被使用。

我想做一些类似于 class-dump 的事情,并检查存储在 Mach-O 文件中的 Objective-C 元数据,以生成类依赖项的内存中表示。

我不想从头开始做这件事,所以我想知道:

  • 它已经完成了吗?
  • 是否有一个开源库可以为我提供提取这些信息所需的基础工具(一个检查 Mach-O 文件并创建其中包含的 Objective-C 信息的外观的库 - 这样我就可以迭代所有的类、它们的方法、属性、ivars 等,并扫描对其他类的引用)我认为 class-dump 的源代码将是一个很好的起点。
  • 如果你有这方面的经验,我想要完成的事情是否可行?
  • 我需要克服哪些障碍?

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 directives.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

星星的轨迹 2024-12-09 04:01:10

已经完成了吗?

据我所知没有。

是否有一个开源库可以为我提供
我需要什么基础工具来提取这些信息?

class-dump 的核心是 libMachObjC ,它完全可以完成您的任务想要,即解析所有类/方法/ivars 等等。 API非常干净,应该很容易使用。

如果您有此类事情的经验,这就是我正在尝试的
实现可行吗?

不幸的是,不能,因为有些类没有声明真正的类,而是使用 id 代替。例如,以下是可以从 UIKit 的类转储中提取的信息:

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

_rowData ivar 类型信息是 id 但如果您在运行时检查,您会看到_rowDataUITableViewRowData 类的实例。此信息不存在于 Mach-O 二进制文件中,因此您无法找到 UITableView 和 UITableViewRowData 之间的关系。这同样适用于方法参数。

Has it already been done?

Not that I know of.

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

At the core of class-dump 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;
    ...

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

╰ゝ天使的微笑 2024-12-09 04:01:10

这是一个依赖于 mach.o 文件中的信息并根据该信息生成图形依赖关系的解决方案: https://github.com/PaulTaykalo/objc-dependency-visualizer在此处输入图像描述

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

紫瑟鸿黎 2024-12-09 04:01:10

已经完成了吗?

是的 - 但我不能推荐一个好的公共实施

是否有一个开源库可以为我提供提取此信息所需的基础工具(一个检查 Mach-O 文件并创建其中包含的 Objective-C 信息的外观的库 - 这样我就可以可以迭代所有类、它们的方法、属性、ivars 等,并扫描对其他类的引用)我认为 class-dump 的源代码将是一个很好的起点。

大多数用例将受益于使用 objc 运行时设施 objc/... 而不是检查二进制文件。

如果您有此类事情的经验,我想要完成的事情是否可行?

是的。我使用 objc 运行时做了类似的事情。

我需要克服哪些障碍?

这很大程度上取决于您想要的详细程度...如果您没有找到这样的实现,那么实现时间,但我认为如果您在 objc 运行时中谷歌搜索更深奥的函数,您会找到一些选项;也许您会在(开放)语言绑定或桥梁中找到一个?

如果您最终自己编写了一个,您可以使用 objc_getClassList 来获取注册的 objc 类,然后从那里访问您想要的属性/信息。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文