# HG changeset patch # User Omair Majid # Date 1454433207 18000 # Node ID 842d04b266c64f076a8f41c68682fb1a8153ed5c # Parent d06754b2b04f55a1645ed04bb063f8d285956ccf Disable profiler output on target JVM PR 2831 Reviewed-by: aazores, jerboaa Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2016-January/017594.html Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2016-February/017633.html diff -r d06754b2b04f -r 842d04b266c6 vm-profiler/jvm-agent/src/main/java/com/redhat/thermostat/vm/profiler/agent/jvm/AsmBasedInstrumentor.java --- a/vm-profiler/jvm-agent/src/main/java/com/redhat/thermostat/vm/profiler/agent/jvm/AsmBasedInstrumentor.java Tue Jan 26 10:18:13 2016 -0500 +++ b/vm-profiler/jvm-agent/src/main/java/com/redhat/thermostat/vm/profiler/agent/jvm/AsmBasedInstrumentor.java Tue Feb 02 12:13:27 2016 -0500 @@ -69,8 +69,8 @@ return data; } catch (Exception e) { - System.err.println("Error transforming: " + className); - e.printStackTrace(); + Debug.printlnError("Error transforming: " + className); + Debug.printStackTrace(e); throw new AssertionError(e); } } diff -r d06754b2b04f -r 842d04b266c6 vm-profiler/jvm-agent/src/main/java/com/redhat/thermostat/vm/profiler/agent/jvm/Debug.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vm-profiler/jvm-agent/src/main/java/com/redhat/thermostat/vm/profiler/agent/jvm/Debug.java Tue Feb 02 12:13:27 2016 -0500 @@ -0,0 +1,78 @@ +/* + * Copyright 2012-2015 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * . + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.vm.profiler.agent.jvm; + +/** + * Central place for debugging so we can control whether output is displayed or + * not. In general, it's not a good idea to display output to stdout/stderr of a + * random program since it may not be expected at all. + */ +public class Debug { + + private static boolean printEnabled = false; + + private Debug() { + // do not instantiate + } + + public static void setPrintEnabled(boolean enabled) { + Debug.printEnabled = enabled; + } + + public static boolean getPrintEnabled() { + return printEnabled; + } + + public static void println(String message) { + if (printEnabled) { + System.out.println(message); + } + } + + public static void printlnError(String message) { + if (printEnabled) { + System.err.println(message); + } + } + + public static void printStackTrace(Throwable t) { + if (printEnabled) { + t.printStackTrace(); + } + } + +} diff -r d06754b2b04f -r 842d04b266c6 vm-profiler/jvm-agent/src/main/java/com/redhat/thermostat/vm/profiler/agent/jvm/InstrumentationControl.java --- a/vm-profiler/jvm-agent/src/main/java/com/redhat/thermostat/vm/profiler/agent/jvm/InstrumentationControl.java Tue Jan 26 10:18:13 2016 -0500 +++ b/vm-profiler/jvm-agent/src/main/java/com/redhat/thermostat/vm/profiler/agent/jvm/InstrumentationControl.java Tue Feb 02 12:13:27 2016 -0500 @@ -144,21 +144,21 @@ } if (toTransform.size() > 0) { - System.out.println("AGENT: Retransforming " + toTransform.size() + " classes"); + Debug.println("AGENT: Retransforming " + toTransform.size() + " classes"); for (Class klass : toTransform) { try { instrumentation.retransformClasses(klass); } catch (UnmodifiableClassException e) { throw new AssertionError("Tried to modify an unmodifiable class", e); } catch (Error e) { - System.err.println("Failed to trasnform: " + klass.getName()); - System.err.println("Unable to retransform all classes. Some classes may not have been instrumented correctly!"); - e.printStackTrace(); + Debug.printlnError("Failed to transform: " + klass.getName()); + Debug.printlnError("Unable to retransform all classes. Some classes may not have been instrumented correctly!"); + Debug.printStackTrace(e); } } } long end = System.nanoTime(); - System.out.println("AGENT: Retansforming took: " + (end - start) + "ns"); + Debug.println("AGENT: Retansforming took: " + (end - start) + "ns"); } private void writeResultsToDiskIfNotWritten() { @@ -175,7 +175,7 @@ try { out = resultsFile.getWriter(); Map data = recorder.getData(); - System.out.println("AGENT: Writing " + data.size() + " results to: " + path); + Debug.println("AGENT: Writing " + data.size() + " results to: " + path); for (Map.Entry entry : data.entrySet()) { out.write(entry.getValue().get() + "\t" + entry.getKey() + "\n"); } @@ -188,11 +188,11 @@ } } catch (IOException e) { // well, we are screwed - e.printStackTrace(); + Debug.printStackTrace(e); } } } catch (IOException e) { - e.printStackTrace(); + Debug.printStackTrace(e); } } diff -r d06754b2b04f -r 842d04b266c6 vm-profiler/jvm-agent/src/main/java/com/redhat/thermostat/vm/profiler/agent/jvm/Main.java --- a/vm-profiler/jvm-agent/src/main/java/com/redhat/thermostat/vm/profiler/agent/jvm/Main.java Tue Jan 26 10:18:13 2016 -0500 +++ b/vm-profiler/jvm-agent/src/main/java/com/redhat/thermostat/vm/profiler/agent/jvm/Main.java Tue Feb 02 12:13:27 2016 -0500 @@ -64,8 +64,8 @@ ObjectName name = new ObjectName("com.redhat.thermostat:type=InstrumentationControl"); server.registerMBean(control, name); } catch (Exception e) { - System.err.println("Unable to attach agent"); - e.printStackTrace(); + Debug.printlnError("Unable to attach agent"); + Debug.printStackTrace(e); } } diff -r d06754b2b04f -r 842d04b266c6 vm-profiler/jvm-agent/src/main/java/com/redhat/thermostat/vm/profiler/agent/jvm/ProfilerAgent.java --- a/vm-profiler/jvm-agent/src/main/java/com/redhat/thermostat/vm/profiler/agent/jvm/ProfilerAgent.java Tue Jan 26 10:18:13 2016 -0500 +++ b/vm-profiler/jvm-agent/src/main/java/com/redhat/thermostat/vm/profiler/agent/jvm/ProfilerAgent.java Tue Feb 02 12:13:27 2016 -0500 @@ -59,7 +59,7 @@ private static void initializeAgent(String args, Instrumentation instrumentation) { - System.out.println("AGENT: loaded"); + Debug.println("AGENT: loaded"); // This is the saner approach, but for now we are brute-forcing a // hardcoded path using a manifest entry @@ -70,7 +70,7 @@ } private static void addJarsToClassPath(String jars, Instrumentation instrumentation) throws AssertionError { - // System.out.println("Classpath: " + System.getProperty("java.class.path")); + Debug.println("Classpath: " + System.getProperty("java.class.path")); boolean addToBoot = true; String[] jarPaths = jars.split(":"); for (String jarPath : jarPaths) { @@ -83,7 +83,7 @@ } else { instrumentation.appendToSystemClassLoaderSearch(jarFile); } - System.out.println("AGENT: Added" + jarPath + " to " + (addToBoot ? "bootstrap" : "system") + " classpath"); + Debug.println("AGENT: Added '" + jarPath + "' to " + (addToBoot ? "bootstrap" : "system") + " classpath"); } catch (IOException e) { throw new AssertionError(jarFile + " not found!"); } @@ -99,23 +99,23 @@ Method runMethod = klass.getMethod("run"); runMethod.invoke(main); } catch (ClassNotFoundException e) { - e.printStackTrace(); - System.err.println("Unable to initialize agent"); + Debug.printStackTrace(e); + Debug.printlnError("Unable to initialize agent"); } catch (NoSuchMethodException e) { - e.printStackTrace(); - System.err.println("Unable to initialize agent"); + Debug.printStackTrace(e); + Debug.printlnError("Unable to initialize agent"); } catch (IllegalAccessException e) { - e.printStackTrace(); - System.err.println("Unable to initialize agent"); + Debug.printStackTrace(e); + Debug.printlnError("Unable to initialize agent"); } catch (InstantiationException e) { - e.printStackTrace(); - System.err.println("Unable to initialize agent"); + Debug.printStackTrace(e); + Debug.printlnError("Unable to initialize agent"); } catch (InvocationTargetException e) { - e.printStackTrace(); - System.err.println("Unable to initialize agent"); + Debug.printStackTrace(e); + Debug.printlnError("Unable to initialize agent"); } catch (SecurityException e) { - e.printStackTrace(); - System.err.println("Unable to initialize agent"); + Debug.printStackTrace(e); + Debug.printlnError("Unable to initialize agent"); } } diff -r d06754b2b04f -r 842d04b266c6 vm-profiler/jvm-agent/src/test/java/com/redhat/thermostat/vm/profiler/agent/jvm/DebugTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vm-profiler/jvm-agent/src/test/java/com/redhat/thermostat/vm/profiler/agent/jvm/DebugTest.java Tue Feb 02 12:13:27 2016 -0500 @@ -0,0 +1,94 @@ +/* + * Copyright 2012-2015 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * . + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.vm.profiler.agent.jvm; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; + +import java.io.PrintStream; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class DebugTest { + + private PrintStream originalSystemOut; + private PrintStream originalSystemErr; + private boolean originalPrintEnabled; + + private PrintStream systemOut; + private PrintStream systemErr; + + @Before + public void setup() { + originalPrintEnabled = Debug.getPrintEnabled(); + + originalSystemOut = System.out; + systemOut = mock(PrintStream.class); + System.setOut(systemOut); + + originalSystemErr = System.err; + systemErr = mock(PrintStream.class); + System.setErr(systemErr); + } + + @After + public void tearDown() { + System.setErr(originalSystemErr); + System.setOut(originalSystemOut); + + Debug.setPrintEnabled(originalPrintEnabled); + } + + @Test + public void verifyNoOutputWhenPrintingIsDisabled() throws Exception { + Debug.setPrintEnabled(false); + Debug.println("foobar!"); + verifyNoMoreInteractions(systemErr, systemOut); + } + + @Test + public void verifyOutputWhenPrintingIsEnabled() throws Exception { + Debug.setPrintEnabled(true); + Debug.printlnError("foobar!"); + verify(systemErr).println("foobar!"); + verifyNoMoreInteractions(systemOut); + } + +}