-
Notifications
You must be signed in to change notification settings - Fork 7.6k
feat: reflect analysis #3069
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Aresxue
wants to merge
2
commits into
alibaba:master
Choose a base branch
from
Aresxue:feat/reflect_analysis
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+362
−0
Open
feat: reflect analysis #3069
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
245 changes: 245 additions & 0 deletions
245
core/src/main/java/com/taobao/arthas/core/command/klass100/ReflectAnalysisCommand.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,245 @@ | ||
| package com.taobao.arthas.core.command.klass100; | ||
|
|
||
|
|
||
| import com.alibaba.arthas.deps.org.slf4j.Logger; | ||
| import com.alibaba.arthas.deps.org.slf4j.LoggerFactory; | ||
| import com.alibaba.deps.org.objectweb.asm.ClassReader; | ||
| import com.alibaba.deps.org.objectweb.asm.ClassVisitor; | ||
| import com.alibaba.deps.org.objectweb.asm.MethodVisitor; | ||
| import com.alibaba.deps.org.objectweb.asm.Opcodes; | ||
| import com.taobao.arthas.core.command.Constants; | ||
| import com.taobao.arthas.core.command.model.EchoModel; | ||
| import com.taobao.arthas.core.command.model.ReflectAnalysisModel; | ||
| import com.taobao.arthas.core.shell.command.AnnotatedCommand; | ||
| import com.taobao.arthas.core.shell.command.CommandProcess; | ||
| import com.taobao.arthas.core.shell.command.ExitStatus; | ||
| import com.taobao.arthas.core.util.CommandUtils; | ||
| import com.taobao.arthas.core.util.InstrumentationUtils; | ||
| import com.taobao.arthas.core.util.SearchUtils; | ||
| import com.taobao.arthas.core.util.collection.MapUtil; | ||
| import com.taobao.middleware.cli.annotations.Description; | ||
| import com.taobao.middleware.cli.annotations.Name; | ||
| import com.taobao.middleware.cli.annotations.Summary; | ||
| import java.io.BufferedWriter; | ||
| import java.io.File; | ||
| import java.io.FileInputStream; | ||
| import java.io.OutputStreamWriter; | ||
| import java.lang.instrument.Instrumentation; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.concurrent.CompletableFuture; | ||
|
|
||
| import static com.alibaba.deps.org.objectweb.asm.ClassReader.SKIP_DEBUG; | ||
| import static com.alibaba.deps.org.objectweb.asm.ClassReader.SKIP_FRAMES; | ||
|
|
||
| /** | ||
| * @author: Ares | ||
| * @time: 2023-12-19 17:33:14 | ||
| * @description: 反射分析 | ||
| * @version: JDK 1.8 | ||
| */ | ||
| @Name("reflect-analysis") | ||
| @Summary("Analyze the reflection situation within the application") | ||
| @Description(Constants.EXAMPLE + | ||
| " reflect-analysis\n" + | ||
| " reflect-analysis reflect-analysis-result.csv\n" + | ||
| Constants.WIKI + Constants.WIKI_HOME + "dump") | ||
| public class ReflectAnalysisCommand extends AnnotatedCommand { | ||
|
|
||
| private static final Logger LOGGER = LoggerFactory.getLogger(ReflectAnalysisCommand.class); | ||
| private static final String TEMP_DIR = System.getProperty("java.io.tmpdir"); | ||
|
|
||
| @Override | ||
| public void process(CommandProcess process) { | ||
| List<String> args = process.args(); | ||
|
|
||
| if (args.isEmpty()) { | ||
| ExitStatus exitStatus = processImpl(process, null); | ||
| CommandUtils.end(process, exitStatus); | ||
| return; | ||
| } | ||
|
|
||
| String resultFilePath = buildResultFilePath(args); | ||
| CompletableFuture.runAsync(() -> processImpl(process, resultFilePath)); | ||
| String message = String.format("The reflect analysis result is being generated asynchronously, check the %s file later", resultFilePath); | ||
| process.appendResult(new EchoModel(message)); | ||
| CommandUtils.end(process, ExitStatus.success()); | ||
| } | ||
|
|
||
| private String buildResultFilePath(List<String> args) { | ||
| String resultFilePath = args.get(0); | ||
| Path path = Paths.get(resultFilePath); | ||
| if (!path.isAbsolute()) { | ||
| resultFilePath = TEMP_DIR + resultFilePath; | ||
| } | ||
| return resultFilePath; | ||
| } | ||
|
|
||
| private ExitStatus processImpl(CommandProcess process, String resultFilePath) { | ||
| ExitStatus status; | ||
| try { | ||
| Instrumentation inst = process.session().getInstrumentation(); | ||
| // 找到所有反射生成的sun.reflect.GeneratedMethodAccessor类 | ||
| Set<Class<?>> matchedClasses = SearchUtils.searchClass(inst, "sun.reflect.GeneratedMethodAccessor*", false); | ||
| if (null != matchedClasses && !matchedClasses.isEmpty()) { | ||
| ClassDumpTransformer transformer = new ClassDumpTransformer(matchedClasses); | ||
| InstrumentationUtils.retransformClasses(inst, transformer, matchedClasses); | ||
| Map<Class<?>, File> dumpResult = transformer.getDumpResult(); | ||
|
|
||
| Map<String, List<String>> result = MapUtil.newHashMap(dumpResult.size()); | ||
|
|
||
| for (Map.Entry<Class<?>, File> entry : dumpResult.entrySet()) { | ||
| Class<?> clazz = entry.getKey(); | ||
| File classFile = entry.getValue(); | ||
|
|
||
| String generatedMethodAccessorName = clazz.getSimpleName(); | ||
| try (FileInputStream inputStream = new FileInputStream(classFile)) { | ||
| ClassReader classReader = new ClassReader(inputStream); | ||
| ReflectAnalyzerClassVisitor classVisitor = new ReflectAnalyzerClassVisitor(); | ||
| classReader.accept(classVisitor, SKIP_DEBUG | SKIP_FRAMES); | ||
|
|
||
| String invokeClassName = classVisitor.getInvokeClassName(); | ||
| String refName = classVisitor.getRefName(); | ||
|
|
||
| if (invokeClassName != null && refName != null) { | ||
| String key = invokeClassName + "#" + refName; | ||
| result.computeIfAbsent(key, k -> new ArrayList<>()).add(generatedMethodAccessorName); | ||
| } | ||
| } catch (Throwable throwable) { | ||
| LOGGER.warn("analyze class file: {} fail:", classFile.getName(), throwable); | ||
| } | ||
| } | ||
|
|
||
| processResult(process, result, resultFilePath); | ||
| } | ||
| status = ExitStatus.success(); | ||
| } catch (Throwable throwable) { | ||
| LOGGER.error("processing fail:", throwable); | ||
| process.end(-1, "processing fail"); | ||
| status = ExitStatus.failure(-1, "reflect analysis fail"); | ||
| } | ||
| return status; | ||
| } | ||
|
|
||
| private void processResult(CommandProcess process, Map<String, List<String>> result, String resultFilePath) { | ||
| List<ReflectAnalysisModel> resultList = new ArrayList<>(result.size()); | ||
| result.forEach((key, valueList) -> { | ||
| ReflectAnalysisModel reflectAnalysisModel = new ReflectAnalysisModel(); | ||
| reflectAnalysisModel.setRefName(key); | ||
| reflectAnalysisModel.setGeneratedMethodAccessorNameCount(valueList.size()); | ||
| StringBuilder generatedMethodAccessorNames = new StringBuilder(); | ||
| for (String generatedMethodAccessorName : valueList) { | ||
| generatedMethodAccessorNames.append(generatedMethodAccessorName).append(","); | ||
| } | ||
| reflectAnalysisModel.setGeneratedMethodAccessorNames(generatedMethodAccessorNames.substring(0, generatedMethodAccessorNames.length() - 1)); | ||
Aresxue marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| resultList.add(reflectAnalysisModel); | ||
| }); | ||
| resultList.sort((leftMode, rightModel) -> { | ||
| int leftCount = leftMode.getGeneratedMethodAccessorNameCount(); | ||
| int rightCount = rightModel.getGeneratedMethodAccessorNameCount(); | ||
| if (leftCount > rightCount) { | ||
| return -1; | ||
| } else if (leftCount < rightCount) { | ||
| return 1; | ||
| } else { | ||
| return leftMode.getRefName().compareTo(rightModel.getRefName()); | ||
| } | ||
| }); | ||
| if (null == resultFilePath) { | ||
| for (ReflectAnalysisModel reflectAnalysisModel : resultList) { | ||
| process.appendResult(reflectAnalysisModel); | ||
| } | ||
| } else { | ||
| exportDataToCsvFile(resultList, resultFilePath); | ||
| } | ||
| } | ||
|
|
||
| private void exportDataToCsvFile(List<ReflectAnalysisModel> reflectAnalysisModelList, String resultFilePath) { | ||
| LOGGER.info("start write reflect analysis result: {} to file: {}", reflectAnalysisModelList.size(), resultFilePath); | ||
| File file = new File(resultFilePath); | ||
| try { | ||
| if (file.exists()) { | ||
| file.delete(); | ||
| } | ||
| file.createNewFile(); | ||
| // write csv file | ||
| try (BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(file.toPath()), StandardCharsets.UTF_8))) { | ||
Aresxue marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| bufferedWriter.write("count"); | ||
| bufferedWriter.write(","); | ||
| bufferedWriter.write("refName"); | ||
| bufferedWriter.write(","); | ||
| bufferedWriter.write("names"); | ||
| bufferedWriter.newLine(); | ||
| for (ReflectAnalysisModel reflectAnalysisModel : reflectAnalysisModelList) { | ||
| bufferedWriter.write(String.valueOf(reflectAnalysisModel.getGeneratedMethodAccessorNameCount())); | ||
| bufferedWriter.write(","); | ||
| bufferedWriter.write(reflectAnalysisModel.getRefName()); | ||
| bufferedWriter.write(","); | ||
| bufferedWriter.write(reflectAnalysisModel.getGeneratedMethodAccessorNames()); | ||
| bufferedWriter.newLine(); | ||
| } | ||
| LOGGER.info("write csv file end"); | ||
| } | ||
| } catch (Throwable throwable) { | ||
| LOGGER.error("export data to csv file fail:", throwable); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * ASM字节码分析器,用于直接从字节码中提取反射调用信息 | ||
| */ | ||
| private static class ReflectAnalyzerClassVisitor extends ClassVisitor { | ||
|
|
||
| private String invokeClassName; | ||
| private String refName; | ||
|
|
||
| public ReflectAnalyzerClassVisitor() { | ||
| super(Opcodes.ASM9); | ||
| } | ||
|
|
||
| @Override | ||
| public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) { | ||
| if ("invoke".equals(name)) { | ||
| return new InvokeMethodVisitor(); | ||
| } | ||
| return super.visitMethod(access, name, descriptor, signature, exceptions); | ||
| } | ||
|
|
||
| private class InvokeMethodVisitor extends MethodVisitor { | ||
|
|
||
| public InvokeMethodVisitor() { | ||
| super(Opcodes.ASM9); | ||
| } | ||
|
|
||
| @Override | ||
| public void visitMethodInsn(int opcode, String owner, String name, String descriptor, boolean isInterface) { | ||
| // 查找invoke方法中的方法调用,这通常是反射的目标方法 | ||
| if (opcode == Opcodes.INVOKEVIRTUAL || opcode == Opcodes.INVOKESPECIAL | ||
| || opcode == Opcodes.INVOKESTATIC || opcode == Opcodes.INVOKEINTERFACE) { | ||
|
|
||
| // 过滤掉Java标准库和sun包的调用 | ||
| if (!owner.startsWith("java/") && !owner.startsWith("sun/") && !owner.startsWith("jdk/")) { | ||
| invokeClassName = owner.replace('/', '.'); | ||
| refName = name; | ||
| } | ||
| } | ||
| super.visitMethodInsn(opcode, owner, name, descriptor, isInterface); | ||
| } | ||
| } | ||
|
|
||
| public String getInvokeClassName() { | ||
| return invokeClassName; | ||
| } | ||
|
|
||
| public String getRefName() { | ||
| return refName; | ||
| } | ||
| } | ||
|
|
||
| } | ||
46 changes: 46 additions & 0 deletions
46
core/src/main/java/com/taobao/arthas/core/command/model/ReflectAnalysisModel.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package com.taobao.arthas.core.command.model; | ||
|
|
||
| /** | ||
| * @author: Ares | ||
| * @time: 2023-12-20 11:33:32 | ||
| * @description: ReflectAnalysis model | ||
| * @version: JDK 1.8 | ||
| */ | ||
| public class ReflectAnalysisModel extends ResultModel { | ||
|
|
||
| private String refName; | ||
|
|
||
| private String generatedMethodAccessorNames; | ||
|
|
||
| private Integer generatedMethodAccessorNameCount; | ||
|
|
||
| @Override | ||
| public String getType() { | ||
| return "reflect-analysis"; | ||
| } | ||
|
|
||
| public String getRefName() { | ||
| return refName; | ||
| } | ||
|
|
||
| public void setRefName(String refName) { | ||
| this.refName = refName; | ||
| } | ||
|
|
||
| public String getGeneratedMethodAccessorNames() { | ||
| return generatedMethodAccessorNames; | ||
| } | ||
|
|
||
| public void setGeneratedMethodAccessorNames(String generatedMethodAccessorNames) { | ||
| this.generatedMethodAccessorNames = generatedMethodAccessorNames; | ||
| } | ||
|
|
||
| public Integer getGeneratedMethodAccessorNameCount() { | ||
| return generatedMethodAccessorNameCount; | ||
| } | ||
|
|
||
| public void setGeneratedMethodAccessorNameCount(Integer generatedMethodAccessorNameCount) { | ||
| this.generatedMethodAccessorNameCount = generatedMethodAccessorNameCount; | ||
| } | ||
|
|
||
| } |
22 changes: 22 additions & 0 deletions
22
core/src/main/java/com/taobao/arthas/core/command/view/ReflectAnalysisView.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package com.taobao.arthas.core.command.view; | ||
|
|
||
| import com.taobao.arthas.core.command.model.ReflectAnalysisModel; | ||
| import com.taobao.arthas.core.shell.command.CommandProcess; | ||
|
|
||
| /** | ||
| * @author: Ares | ||
| * @time: 2023-12-21 11:45:05 | ||
| * @description: ReflectAnalysis view | ||
| * @version: JDK 1.8 | ||
| */ | ||
| public class ReflectAnalysisView extends ResultView<ReflectAnalysisModel> { | ||
|
|
||
| @Override | ||
| public void draw(CommandProcess process, ReflectAnalysisModel result) { | ||
| String line = String.format("count=%s, refName=%s, names=%s", | ||
| result.getGeneratedMethodAccessorNameCount(), result.getRefName(), | ||
| result.getGeneratedMethodAccessorNames()); | ||
| process.write(line).write("\n"); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
core/src/main/java/com/taobao/arthas/core/util/collection/MapUtil.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package com.taobao.arthas.core.util.collection; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * @author: Ares | ||
| * @time: 2025-09-25 12:34:07 | ||
| * @description: Map util | ||
| * @version: JDK 1.8 | ||
| */ | ||
| public class MapUtil { | ||
|
|
||
| private static final int MAX_POWER_OF_TWO = 1 << (Integer.SIZE - 2); | ||
|
|
||
| /** | ||
| * @see java.util.HashMap#DEFAULT_INITIAL_CAPACITY | ||
| */ | ||
| private static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; | ||
|
|
||
| public static int capacity(int expectedSize) { | ||
| if (expectedSize < 0) { | ||
| return DEFAULT_INITIAL_CAPACITY; | ||
| } | ||
|
|
||
| if (expectedSize < 3) { | ||
| return expectedSize + 1; | ||
| } | ||
| if (expectedSize < MAX_POWER_OF_TWO) { | ||
| // This is the calculation used in JDK8 to resize when a putAll | ||
| // happens; it seems to be the most conservative calculation we | ||
| // can make. 0.75 is the default load factor. | ||
| return (int) ((float) expectedSize / 0.75F + 1.0F); | ||
| } | ||
| return Integer.MAX_VALUE; | ||
| } | ||
|
|
||
| public static <K, V> Map<K, V> newHashMap(int expectedSize) { | ||
| return new HashMap<>(capacity(expectedSize)); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.