Skip to content

Commit 931effb

Browse files
committed
Output all instrumented classes into physical files.
1 parent d12c28b commit 931effb

File tree

4 files changed

+112
-5
lines changed

4 files changed

+112
-5
lines changed

apm-sniffer/apm-agent-core/src/main/java/org/skywalking/apm/agent/core/conf/Config.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ public static class Agent {
5252
* memory cost estimated.
5353
*/
5454
public static int SPAN_LIMIT_PER_SEGMENT = 300;
55+
56+
/**
57+
* If true, skywalking agent will save all instrumented classes files. And you can send them to skywalking team,
58+
* in order to resolve compatible problem.
59+
*/
60+
public static boolean IS_OPEN_DEBUGGING_CLASS = false;
5561
}
5662

5763
public static class Collector {
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright 2017, OpenSkywalking Organization All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* Project repository: https://github.com/OpenSkywalking/skywalking
17+
*/
18+
19+
package org.skywalking.apm.agent;
20+
21+
import java.io.File;
22+
import java.io.FileOutputStream;
23+
import java.io.IOException;
24+
import net.bytebuddy.description.type.TypeDescription;
25+
import net.bytebuddy.dynamic.DynamicType;
26+
import org.skywalking.apm.agent.core.boot.AgentPackageNotFoundException;
27+
import org.skywalking.apm.agent.core.boot.AgentPackagePath;
28+
import org.skywalking.apm.agent.core.conf.Config;
29+
import org.skywalking.apm.agent.core.logging.api.ILog;
30+
import org.skywalking.apm.agent.core.logging.api.LogManager;
31+
32+
/**
33+
* @author wu-sheng
34+
*/
35+
public enum InstrumentDebuggingClass {
36+
INSTANCE;
37+
38+
private static final ILog logger = LogManager.getLogger(InstrumentDebuggingClass.class);
39+
private File debuggingClassesRootPath;
40+
41+
public void log(TypeDescription typeDescription, DynamicType dynamicType) {
42+
if (!Config.Agent.IS_OPEN_DEBUGGING_CLASS) {
43+
return;
44+
}
45+
46+
/**
47+
* try to do I/O things in synchronized way, to avoid unexpected situations.
48+
*/
49+
synchronized (INSTANCE) {
50+
try {
51+
if (debuggingClassesRootPath == null) {
52+
try {
53+
debuggingClassesRootPath = new File(AgentPackagePath.getPath(), "/debugging");
54+
if (!debuggingClassesRootPath.exists()) {
55+
debuggingClassesRootPath.mkdir();
56+
}
57+
} catch (AgentPackageNotFoundException e) {
58+
logger.error(e, "Can't find the root path for creating /debugging folder.");
59+
}
60+
}
61+
62+
File newClassFileFolder = new File(debuggingClassesRootPath, typeDescription.getPackage().getActualName().replaceAll("\\.", "/"));
63+
if (!newClassFileFolder.exists()) {
64+
newClassFileFolder.mkdirs();
65+
}
66+
File newClassFile = new File(newClassFileFolder, typeDescription.getSimpleName() + ".class");
67+
FileOutputStream fos = null;
68+
try {
69+
if (newClassFile.exists()) {
70+
newClassFile.delete();
71+
}
72+
newClassFile.createNewFile();
73+
fos = new FileOutputStream(newClassFile);
74+
fos.write(dynamicType.getBytes());
75+
fos.flush();
76+
} catch (IOException e) {
77+
logger.error(e, "Can't save class {} to file." + typeDescription.getActualName());
78+
} finally {
79+
if (fos != null) {
80+
try {
81+
fos.close();
82+
} catch (IOException e) {
83+
84+
}
85+
}
86+
}
87+
} catch (Throwable t) {
88+
logger.error(t, "Save debugging classes fail.");
89+
}
90+
}
91+
}
92+
}

apm-sniffer/apm-agent/src/main/java/org/skywalking/apm/agent/SkyWalkingAgent.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
package org.skywalking.apm.agent;
2020

21+
import java.lang.instrument.Instrumentation;
22+
import java.util.List;
2123
import net.bytebuddy.agent.builder.AgentBuilder;
2224
import net.bytebuddy.description.type.TypeDescription;
2325
import net.bytebuddy.dynamic.DynamicType;
@@ -26,10 +28,11 @@
2628
import org.skywalking.apm.agent.core.conf.SnifferConfigInitializer;
2729
import org.skywalking.apm.agent.core.logging.api.ILog;
2830
import org.skywalking.apm.agent.core.logging.api.LogManager;
29-
import org.skywalking.apm.agent.core.plugin.*;
30-
31-
import java.lang.instrument.Instrumentation;
32-
import java.util.List;
31+
import org.skywalking.apm.agent.core.plugin.AbstractClassEnhancePluginDefine;
32+
import org.skywalking.apm.agent.core.plugin.EnhanceContext;
33+
import org.skywalking.apm.agent.core.plugin.PluginBootstrap;
34+
import org.skywalking.apm.agent.core.plugin.PluginException;
35+
import org.skywalking.apm.agent.core.plugin.PluginFinder;
3336

3437
/**
3538
* The main entrance of sky-waking agent,
@@ -103,6 +106,8 @@ public void onTransformation(TypeDescription typeDescription, ClassLoader classL
103106
if (logger.isDebugEnable()) {
104107
logger.debug("On Transformation class {}.", typeDescription.getName());
105108
}
109+
110+
InstrumentDebuggingClass.INSTANCE.log(typeDescription, dynamicType);
106111
}
107112

108113
@Override
@@ -113,7 +118,7 @@ public void onIgnored(TypeDescription typeDescription, ClassLoader classLoader,
113118

114119
@Override public void onError(String typeName, ClassLoader classLoader, JavaModule module, boolean loaded,
115120
Throwable throwable) {
116-
logger.error("Failed to enhance class " + typeName, throwable);
121+
logger.error("Enhance class " + typeName + " error.", throwable);
117122
}
118123

119124
@Override

apm-sniffer/config/agent.config

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ agent.application_code=Your_ApplicationName
1212
# Ignore the segments if their operation names start with these suffix.
1313
# agent.ignore_suffix=.jpg,.jpeg,.js,.css,.png,.bmp,.gif,.ico,.mp3,.mp4,.html,.svg
1414

15+
# If true, skywalking agent will save all instrumented classes files. And you can send them to skywalking team,
16+
# in order to resolve compatible problem.
17+
# agent.is_open_debugging_class = true
18+
1519
# Server addresses.
1620
# Mapping to `agent_server/jetty/port` in `config/application.yml` of Collector.
1721
# Examples:

0 commit comments

Comments
 (0)