oldmanpushcart / greys-anatomy

Java诊断工具
Apache License 2.0
4.02k stars 1.22k forks source link

为什么分agent和core两个包 #210

Closed lzmhehe closed 7 years ago

lzmhehe commented 7 years ago

您好 非常感谢greys团队开源的这个项目,对我的实际工作,提供很大帮助。 有个疑问 为什么项目会分两个包,agent仅是一个空壳, 把agent的内容放到core中,attach的时候再把core.jar传个目标jvm是否可以

YuhuiFeng commented 7 years ago

个人觉得这样分包处理更好,这样把核心代码分开加载,能有效的通过类加载器对greys的类进行隔离,这样可以防止对目标应用的污染

oldmanpushcart commented 7 years ago

@YuhuiFeng 说得对,可以看下AgentLauncher的ClassLoader加载截图

+----------------------------------------------------+----------------------------------------------------------------------------------+
|                                         class-info | com.github.ompc.greys.agent.AgentLauncher                                        |
+----------------------------------------------------+----------------------------------------------------------------------------------+
|                                        code-source | /Users/vlinux/.greys/lib/1.7.6.4/greys/greys-agent.jar                           |
+----------------------------------------------------+----------------------------------------------------------------------------------+
|                                               name | com.github.ompc.greys.agent.AgentLauncher                                        |
+----------------------------------------------------+----------------------------------------------------------------------------------+
|                                        isInterface | false                                                                            |
+----------------------------------------------------+----------------------------------------------------------------------------------+
|                                       isAnnotation | false                                                                            |
+----------------------------------------------------+----------------------------------------------------------------------------------+
|                                             isEnum | false                                                                            |
+----------------------------------------------------+----------------------------------------------------------------------------------+
|                                   isAnonymousClass | false                                                                            |
+----------------------------------------------------+----------------------------------------------------------------------------------+
|                                            isArray | false                                                                            |
+----------------------------------------------------+----------------------------------------------------------------------------------+
|                                       isLocalClass | false                                                                            |
+----------------------------------------------------+----------------------------------------------------------------------------------+
|                                      isMemberClass | false                                                                            |
+----------------------------------------------------+----------------------------------------------------------------------------------+
|                                        isPrimitive | false                                                                            |
+----------------------------------------------------+----------------------------------------------------------------------------------+
|                                        isSynthetic | false                                                                            |
+----------------------------------------------------+----------------------------------------------------------------------------------+
|                                        simple-name | AgentLauncher                                                                    |
+----------------------------------------------------+----------------------------------------------------------------------------------+
|                                           modifier | public                                                                           |
+----------------------------------------------------+----------------------------------------------------------------------------------+
|                                         annotation |                                                                                  |
+----------------------------------------------------+----------------------------------------------------------------------------------+
|                                         interfaces |                                                                                  |
+----------------------------------------------------+----------------------------------------------------------------------------------+
|                                        super-class | java.lang.Object                                                                 |
+----------------------------------------------------+----------------------------------------------------------------------------------+
|                                       class-loader | sun.misc.Launcher$AppClassLoader@2a139a55                                        |
|                                                    |   `-sun.misc.Launcher$ExtClassLoader@655e7a34                                    |
+----------------------------------------------------+----------------------------------------------------------------------------------+

可以看到在agent.jar被attach所加载的时候是被AppClassLoader所加载,如果core.jar作为引导jar包被加载进来,在后续的编码工作中一个不下心,很容易就被AppClassLoader所误加载其他非greys的类。

所以为了后续整个工程的维护方便,单独隔离了一个agent模块出来,这个模块的pom引用都被非常严格的限制。

lzmhehe commented 6 years ago

多谢作者 我自己写了一段代码,被classloader搞晕后,就马上理解你们为什么这么处理了

oldmanpushcart commented 6 years ago

纸上得来终觉浅,绝知此事须 宫刑 !:)