From cbe2ebd686831fdfa657d7096c32fe67da704372 Mon Sep 17 00:00:00 2001 From: Alexandre Montplaisir Date: Thu, 4 Feb 2016 18:37:37 -0500 Subject: [PATCH] Introduce a verbose mode for the Java agent If the LTTNG_UST_DEBUG environment variable is defined, log messages from the Java agent will be sent to stderr. This is in line with the rest of UST. Signed-off-by: Alexandre Montplaisir Signed-off-by: Mathieu Desnoyers --- .../java/lttng-ust-agent-common/Makefile.am | 7 ++- .../agent/client/LttngTcpSessiondClient.java | 14 ++++++ .../agent/context/ContextInfoSerializer.java | 6 +++ .../ust/agent/utils/LttngUstAgentLogger.java | 46 +++++++++++++++++++ 4 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/utils/LttngUstAgentLogger.java diff --git a/liblttng-ust-java-agent/java/lttng-ust-agent-common/Makefile.am b/liblttng-ust-java-agent/java/lttng-ust-agent-common/Makefile.am index 683e451b..8ef4bc23 100644 --- a/liblttng-ust-java-agent/java/lttng-ust-agent-common/Makefile.am +++ b/liblttng-ust-java-agent/java/lttng-ust-agent-common/Makefile.am @@ -30,7 +30,8 @@ dist_noinst_JAVA = $(pkgpath)/AbstractLttngAgent.java \ $(pkgpath)/filter/FilterChangeNotifier.java \ $(pkgpath)/filter/IFilterChangeListener.java \ $(pkgpath)/session/EventRule.java \ - $(pkgpath)/session/LogLevelSelector.java + $(pkgpath)/session/LogLevelSelector.java \ + $(pkgpath)/utils/LttngUstAgentLogger.java dist_noinst_DATA = $(jarfile_manifest) @@ -41,7 +42,8 @@ classes = $(pkgpath)/*.class \ $(pkgpath)/client/*.class \ $(pkgpath)/context/*.class \ $(pkgpath)/filter/*.class \ - $(pkgpath)/session/*.class + $(pkgpath)/session/*.class \ + $(pkgpath)/utils/*.class $(jarfile): classnoinst.stamp $(JAR) cfm $(JARFLAGS) $@ $(jarfile_manifest) $(classes) && rm -f $(jarfile_symlink) && $(LN_S) $@ $(jarfile_symlink) @@ -63,4 +65,5 @@ CLEANFILES = $(jarfile) $(pkgpath)/*.class \ $(pkgpath)/context/*.class \ $(pkgpath)/filter/*.class \ $(pkgpath)/session/*.class \ + $(pkgpath)/utils/*.class \ $(juljniout)/org_lttng_ust_agent_context_LttngContextApi.h diff --git a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/LttngTcpSessiondClient.java b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/LttngTcpSessiondClient.java index 5b5d50c4..6177feac 100644 --- a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/LttngTcpSessiondClient.java +++ b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/LttngTcpSessiondClient.java @@ -32,6 +32,8 @@ import java.nio.ByteOrder; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; +import org.lttng.ust.agent.utils.LttngUstAgentLogger; + /** * Client for agents to connect to a local session daemon, using a TCP socket. * @@ -109,12 +111,14 @@ public class LttngTcpSessiondClient implements Runnable { /* * Connect to the session daemon before anything else. */ + LttngUstAgentLogger.log(getClass(), "Connecting to sessiond"); connectToSessiond(); /* * Register to the session daemon as the Java component of the * UST application. */ + LttngUstAgentLogger.log(getClass(), "Registering to sessiond"); registerToSessiond(); /* @@ -122,6 +126,7 @@ public class LttngTcpSessiondClient implements Runnable { * session daemon. This will return if and only if there is a * fatal error or the socket closes. */ + LttngUstAgentLogger.log(getClass(), "Waiting on sessiond commands..."); handleSessiondCmd(); } catch (UnknownHostException uhe) { uhe.printStackTrace(); @@ -139,6 +144,7 @@ public class LttngTcpSessiondClient implements Runnable { * Dispose this client and close any socket connection it may hold. */ public void close() { + LttngUstAgentLogger.log(getClass(), "Closing client"); this.quit = true; try { @@ -247,6 +253,7 @@ public class LttngTcpSessiondClient implements Runnable { * We don't send any reply to the registration done command. * This just marks the end of the initial session setup. */ + LttngUstAgentLogger.log(getClass(), "Registration done"); continue; } case CMD_LIST: @@ -254,6 +261,7 @@ public class LttngTcpSessiondClient implements Runnable { SessiondCommand listLoggerCmd = new SessiondListLoggersCommand(); LttngAgentResponse response = listLoggerCmd.execute(logAgent); responseData = response.getBytes(); + LttngUstAgentLogger.log(getClass(), "Received list loggers command"); break; } case CMD_EVENT_ENABLE: @@ -266,6 +274,7 @@ public class LttngTcpSessiondClient implements Runnable { SessiondCommand enableEventCmd = new SessiondEnableEventCommand(inputData); LttngAgentResponse response = enableEventCmd.execute(logAgent); responseData = response.getBytes(); + LttngUstAgentLogger.log(getClass(), "Received enable event command"); break; } case CMD_EVENT_DISABLE: @@ -278,6 +287,7 @@ public class LttngTcpSessiondClient implements Runnable { SessiondCommand disableEventCmd = new SessiondDisableEventCommand(inputData); LttngAgentResponse response = disableEventCmd.execute(logAgent); responseData = response.getBytes(); + LttngUstAgentLogger.log(getClass(), "Received disable event command"); break; } case CMD_APP_CTX_ENABLE: @@ -290,6 +300,7 @@ public class LttngTcpSessiondClient implements Runnable { SessiondCommand enableAppCtxCmd = new SessiondEnableAppContextCommand(inputData); LttngAgentResponse response = enableAppCtxCmd.execute(logAgent); responseData = response.getBytes(); + LttngUstAgentLogger.log(getClass(), "Received enable app-context command"); break; } case CMD_APP_CTX_DISABLE: @@ -302,6 +313,7 @@ public class LttngTcpSessiondClient implements Runnable { SessiondCommand disableAppCtxCmd = new SessiondDisableAppContextCommand(inputData); LttngAgentResponse response = disableAppCtxCmd.execute(logAgent); responseData = response.getBytes(); + LttngUstAgentLogger.log(getClass(), "Received disable app-context command"); break; } default: @@ -310,11 +322,13 @@ public class LttngTcpSessiondClient implements Runnable { responseData = new byte[4]; ByteBuffer buf = ByteBuffer.wrap(responseData); buf.order(ByteOrder.BIG_ENDIAN); + LttngUstAgentLogger.log(getClass(), "Received unknown command, ignoring"); break; } } /* Send response to the session daemon. */ + LttngUstAgentLogger.log(getClass(), "Sending response"); this.outToSessiond.write(responseData, 0, responseData.length); this.outToSessiond.flush(); } diff --git a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/context/ContextInfoSerializer.java b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/context/ContextInfoSerializer.java index 57382bd1..be613043 100644 --- a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/context/ContextInfoSerializer.java +++ b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/context/ContextInfoSerializer.java @@ -24,6 +24,8 @@ import java.nio.charset.Charset; import java.util.Collection; import java.util.Map; +import org.lttng.ust.agent.utils.LttngUstAgentLogger; + /** * This class is used to serialize the list of "context info" objects to pass * through JNI. @@ -142,6 +144,10 @@ public class ContextInfoSerializer { buffer.put(strArray); buffer.position(buffer.position() + remainingBytes); + LttngUstAgentLogger.log(ContextInfoSerializer.class, + "ContextInfoSerializer: Context to be sent through JNI: " + fullContextName + '=' + + (contextInfo == null ? "null" : contextInfo.toString())); + serializeContextInfo(buffer, contextInfo); } } diff --git a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/utils/LttngUstAgentLogger.java b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/utils/LttngUstAgentLogger.java new file mode 100644 index 00000000..16f23f7c --- /dev/null +++ b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/utils/LttngUstAgentLogger.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2016 - EfficiOS Inc., Alexandre Montplaisir + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License, version 2.1 only, + * as published by the Free Software Foundation. + * + * This library 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 Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package org.lttng.ust.agent.utils; + +/** + * Logging infrastructure for the lttng-ust Java agent. It prints log messages + * to stderr but only when the environment variable LTTNG_UST_DEBUG is defined. + * + * @author Alexandre Montplaisir + */ +public class LttngUstAgentLogger { + + private static final String ENV_VAR_NAME = "LTTNG_UST_DEBUG"; + private static final boolean LOGGING_ENABLED = (System.getenv(ENV_VAR_NAME) == null ? false : true); + + /** + * Log event. Will be printed to stderr if the environment variable + * "LTTNG_UST_DEBUG" is defined. + * + * @param c + * The class logging the message (should normally be called with + * {@link #getClass()}). + * @param message + * The message to print + */ + public static void log(Class c, String message) { + if (LOGGING_ENABLED) { + System.err.println(c.getSimpleName() + ": " + message); + } + } +} -- 2.34.1