alibaba / arthas

Alibaba Java Diagnostic Tool Arthas/Alibaba Java诊断利器Arthas
https://arthas.aliyun.com/
Apache License 2.0
35.7k stars 7.51k forks source link

Load class from class path if not found #2924

Closed secfree closed 4 weeks ago

secfree commented 1 month ago

I encountered TypeNotPresentException/ClassNotFoundException exceptions frequently for the following type programs

then when I stack/watch a class method loaded by specific extension class loader, it will encounter the exception.

This PR can resolve this exception.

Below is an example from Alluxio

$ stack alluxio.underfs.hdfs.HdfsUnderFileSystem *
Affect(class count: 0 , method count: 52) cost in 239 ms, listenerId: 1
Enhance error! exception: java.lang.TypeNotPresentException: Type alluxio/underfs/UfsFileStatus not present

and "arthas.log" has the following content

2024-10-16 15:38:54 [arthas-command-execute] WARN  c.t.arthas.core.advisor.Enhancer -transform loader[alluxio.extensions.ExtensionsClassLoader@6c8d1c2b]:class[alluxio/underfs/hdfs/HdfsUnderFileSystem] failed.
java.lang.TypeNotPresentException: Type alluxio/underfs/UfsFileStatus not present
        at com.alibaba.deps.org.objectweb.asm.ClassWriter.getCommonSuperClass(ClassWriter.java:1045)
        at com.alibaba.bytekit.asm.ClassMetaClassWriter.getCommonSuperClass(ClassMetaClassWriter.java:39)
        at com.alibaba.deps.org.objectweb.asm.SymbolTable.addMergedType(SymbolTable.java:1202)
        at com.alibaba.deps.org.objectweb.asm.Frame.merge(Frame.java:1300)
        at com.alibaba.deps.org.objectweb.asm.Frame.merge(Frame.java:1198)
        at com.alibaba.deps.org.objectweb.asm.MethodWriter.computeAllFrames(MethodWriter.java:1611)
        at com.alibaba.deps.org.objectweb.asm.MethodWriter.visitMaxs(MethodWriter.java:1547)
        at com.alibaba.deps.org.objectweb.asm.tree.MethodNode.accept(MethodNode.java:767)
        at com.alibaba.deps.org.objectweb.asm.tree.MethodNode.accept(MethodNode.java:647)
        at com.alibaba.deps.org.objectweb.asm.tree.ClassNode.accept(ClassNode.java:468)
        at com.alibaba.bytekit.utils.AsmUtils.toBytes(AsmUtils.java:80)
        at com.taobao.arthas.core.advisor.Enhancer.transform(Enhancer.java:256)
        at com.taobao.arthas.core.advisor.TransformerManager$1.transform(TransformerManager.java:51)
        at sun.instrument.TransformerManager.transform(TransformerManager.java:188)
        at sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:428)
        at sun.instrument.InstrumentationImpl.retransformClasses0(Native Method)
        at sun.instrument.InstrumentationImpl.retransformClasses(InstrumentationImpl.java:144)
        at com.taobao.arthas.core.advisor.Enhancer.enhance(Enhancer.java:446)
        at com.taobao.arthas.core.command.monitor200.EnhancerCommand.enhance(EnhancerCommand.java:173)
        at com.taobao.arthas.core.command.monitor200.EnhancerCommand.process(EnhancerCommand.java:120)
        at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl.process(AnnotatedCommandImpl.java:82)
        at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl.access$100(AnnotatedCommandImpl.java:18)
        at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl$ProcessHandler.handle(AnnotatedCommandImpl.java:111)
        at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl$ProcessHandler.handle(AnnotatedCommandImpl.java:108)
        at com.taobao.arthas.core.shell.system.impl.ProcessImpl$CommandProcessTask.run(ProcessImpl.java:385)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:750)
Caused by: java.lang.ClassNotFoundException: alluxio.underfs.UfsFileStatus
        at java.net.URLClassLoader.findClass(URLClassLoader.java:387)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:419)
        at com.taobao.arthas.agent.ArthasClassloader.loadClass(ArthasClassloader.java:34)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:352)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:348)
        at com.alibaba.deps.org.objectweb.asm.ClassWriter.getCommonSuperClass(ClassWriter.java:1043)
        ... 31 common frames omitted
CLAassistant commented 1 month ago

CLA assistant check
All committers have signed the CLA.