|
| 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 | +} |
0 commit comments