From 68a1ef7391fb6103eba95fd350ccc61e73855d95 Mon Sep 17 00:00:00 2001 From: Alexandre Montplaisir Date: Wed, 9 Sep 2015 15:07:24 -0400 Subject: [PATCH] Fix: Return the correct list of available Java events The "lttng list -j/-l" command should list the events that are currently offered by Java application and available to be enabled. Due to some confusion in the implementation of the corresponding agent command response, it was actually returning the list of events that were enabled in the tracing session. Rectify this by sending the list of loggers of the corresponding domain that have one or more LTTng log handlers attached. The interface method was also renamed from listEnabledEvents() to listAvailableEvents() to make it more representative. Fixes: #933 Signed-off-by: Alexandre Montplaisir Signed-off-by: Mathieu Desnoyers --- .../lttng/ust/agent/AbstractLttngAgent.java | 19 +++----- .../agent/client/ILttngTcpClientListener.java | 11 +++-- .../client/SessiondListLoggersCommand.java | 12 +++--- .../lttng/ust/agent/jul/LttngJulAgent.java | 43 +++++++++++++++++++ .../ust/agent/log4j/LttngLog4jAgent.java | 37 ++++++++++++++++ 5 files changed, 98 insertions(+), 24 deletions(-) diff --git a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/AbstractLttngAgent.java b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/AbstractLttngAgent.java index 8531eaec..e97a7bdb 100644 --- a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/AbstractLttngAgent.java +++ b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/AbstractLttngAgent.java @@ -18,9 +18,8 @@ package org.lttng.ust.agent; +import java.util.Collection; import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; import java.util.Map; import java.util.NavigableMap; import java.util.Set; @@ -234,19 +233,11 @@ public abstract class AbstractLttngAgent return decrementEventCount(eventName, enabledEvents); } + /* + * Implementation of this method is domain-specific. + */ @Override - public Iterable listEnabledEvents() { - List events = new LinkedList(); - - if (enabledWildcards.get() > 0) { - events.add(WILDCARD); - } - for (String prefix : enabledEventPrefixes.keySet()) { - events.add(new String(prefix + WILDCARD)); - } - events.addAll(enabledEvents.keySet()); - return events; - } + public abstract Collection listAvailableEvents(); @Override public boolean isEventEnabled(String eventName) { diff --git a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/ILttngTcpClientListener.java b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/ILttngTcpClientListener.java index 031d4e04..ef0c11d9 100644 --- a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/ILttngTcpClientListener.java +++ b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/ILttngTcpClientListener.java @@ -17,6 +17,8 @@ package org.lttng.ust.agent.client; +import java.util.Collection; + import org.lttng.ust.agent.session.EventRule; /** @@ -53,9 +55,12 @@ public interface ILttngTcpClientListener { boolean eventDisabled(String eventName); /** - * List the all events currently enabled in the current tracing sessions. + * List the events that are available in the agent's tracing domain. + * + * In Java terms, this means loggers that have at least one LTTng log + * handler of their corresponding domain attached. * - * @return The list of enabled events + * @return The list of available events */ - Iterable listEnabledEvents(); + Collection listAvailableEvents(); } diff --git a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondListLoggersCommand.java b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondListLoggersCommand.java index c06eaaad..a8bc1520 100644 --- a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondListLoggersCommand.java +++ b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondListLoggersCommand.java @@ -20,8 +20,7 @@ package org.lttng.ust.agent.client; import java.nio.ByteBuffer; import java.nio.ByteOrder; -import java.util.ArrayList; -import java.util.List; +import java.util.Collection; /** * Session daemon command asking the Java agent to list its registered loggers, @@ -34,11 +33,10 @@ class SessiondListLoggersCommand implements ISessiondCommand { @Override public LttngAgentResponse execute(ILttngTcpClientListener agent) { - final List loggerList = new ArrayList(); + final Collection loggerList = agent.listAvailableEvents(); int dataSize = 0; - for (String event : agent.listEnabledEvents()) { - loggerList.add(event); + for (String event : agent.listAvailableEvents()) { dataSize += event.length() + 1; } @@ -49,10 +47,10 @@ class SessiondListLoggersCommand implements ISessiondCommand { private final static int SIZE = 12; - private final List loggers; + private final Collection loggers; private final int dataSize; - public SessiondListLoggersResponse(List loggers, int dataSize) { + public SessiondListLoggersResponse(Collection loggers, int dataSize) { this.loggers = loggers; this.dataSize = dataSize; } diff --git a/liblttng-ust-java-agent/java/lttng-ust-agent-jul/org/lttng/ust/agent/jul/LttngJulAgent.java b/liblttng-ust-java-agent/java/lttng-ust-agent-jul/org/lttng/ust/agent/jul/LttngJulAgent.java index fa92f645..597ae6b3 100644 --- a/liblttng-ust-java-agent/java/lttng-ust-agent-jul/org/lttng/ust/agent/jul/LttngJulAgent.java +++ b/liblttng-ust-java-agent/java/lttng-ust-agent-jul/org/lttng/ust/agent/jul/LttngJulAgent.java @@ -17,6 +17,14 @@ package org.lttng.ust.agent.jul; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.logging.Handler; +import java.util.logging.LogManager; +import java.util.logging.Logger; + import org.lttng.ust.agent.AbstractLttngAgent; /** @@ -39,4 +47,39 @@ class LttngJulAgent extends AbstractLttngAgent { return instance; } + @Override + public Collection listAvailableEvents() { + List ret = new ArrayList(); + + List loggersNames = Collections.list(LogManager.getLogManager().getLoggerNames()); + for (String name : loggersNames) { + /* + * Skip the root logger. An empty string is not a valid event name + * in LTTng. + */ + if (name.equals("")) { + continue; + } + + /* + * Check if that logger has at least one LTTng JUL handler attached. + */ + Logger logger = Logger.getLogger(name); + if (hasLttngHandlerAttached(logger)) { + ret.add(name); + } + } + + return ret; + } + + private static boolean hasLttngHandlerAttached(Logger logger) { + for (Handler handler : logger.getHandlers()) { + if (handler instanceof LttngLogHandler) { + return true; + } + } + return false; + } + } diff --git a/liblttng-ust-java-agent/java/lttng-ust-agent-log4j/org/lttng/ust/agent/log4j/LttngLog4jAgent.java b/liblttng-ust-java-agent/java/lttng-ust-agent-log4j/org/lttng/ust/agent/log4j/LttngLog4jAgent.java index 5dd84687..7dcff9ec 100644 --- a/liblttng-ust-java-agent/java/lttng-ust-agent-log4j/org/lttng/ust/agent/log4j/LttngLog4jAgent.java +++ b/liblttng-ust-java-agent/java/lttng-ust-agent-log4j/org/lttng/ust/agent/log4j/LttngLog4jAgent.java @@ -17,6 +17,14 @@ package org.lttng.ust.agent.log4j; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +import org.apache.log4j.Appender; +import org.apache.log4j.LogManager; +import org.apache.log4j.Logger; import org.lttng.ust.agent.AbstractLttngAgent; /** @@ -40,4 +48,33 @@ class LttngLog4jAgent extends AbstractLttngAgent { return instance; } + @Override + public Collection listAvailableEvents() { + List ret = new ArrayList(); + + @SuppressWarnings("unchecked") + List loggers = Collections.list(LogManager.getCurrentLoggers()); + for (Logger logger : loggers) { + /* + * Check if that logger has at least one LTTng log4j appender + * attached. + */ + if (hasLttngAppenderAttached(logger)) { + ret.add(logger.getName()); + } + } + + return ret; + } + + private static boolean hasLttngAppenderAttached(Logger logger) { + @SuppressWarnings("unchecked") + List appenders = Collections.list(logger.getAllAppenders()); + for (Appender appender : appenders) { + if (appender instanceof LttngLogAppender) { + return true; + } + } + return false; + } } -- 2.34.1