Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-507: Debug agents display log messages.

Dan3106cdfd5125
diff --git a/Ghidra/Debug/Framework-Debugging/certification.manifest b/Ghidra/Debug/Framework-Debugging/certification.manifest
index 84620e1e60..5bbc754b9b 100644
--- a/Ghidra/Debug/Framework-Debugging/certification.manifest
+++ b/Ghidra/Debug/Framework-Debugging/certification.manifest
@@ -1,4 +1,7 @@
##VERSION: 2.0
Module.manifest||GHIDRA||||END|
data/ExtensionPoint.manifest||GHIDRA||||END|
+src/main/resources/agent.log4j.xml||GHIDRA||||END|
+src/main/resources/log4j-appender-console.xml||GHIDRA||||END|
+src/main/resources/log4j-appender-logpanel.xml||GHIDRA||||END|
src/test/resources/ghidra/dbg/model/test_schema.xml||GHIDRA||||END|
diff --git a/Ghidra/Debug/Framework-Debugging/src/main/java/ghidra/dbg/agent/AgentWindow.java b/Ghidra/Debug/Framework-Debugging/src/main/java/ghidra/dbg/agent/AgentWindow.java
index 0681d442ad..6cf4961152 100644
--- a/Ghidra/Debug/Framework-Debugging/src/main/java/ghidra/dbg/agent/AgentWindow.java
+++ b/Ghidra/Debug/Framework-Debugging/src/main/java/ghidra/dbg/agent/AgentWindow.java
@@ -23,27 +23,18 @@ import java.net.SocketAddress;
import javax.swing.*;
import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.LoggerContext;
-import org.apache.logging.log4j.core.appender.AbstractAppender;
-import org.apache.logging.log4j.core.config.Property;
+import org.apache.logging.log4j.core.config.Configuration;
-public class AgentWindow extends JFrame implements WindowListener {
- public static final int MAX_LOG_CHARS = 10000;
+import ghidra.framework.LoggingInitialization;
+import ghidra.util.Msg;
+import ghidra.util.Swing;
+import log.LogListener;
+import log.LogPanelAppender;
- protected class WindowAppender extends AbstractAppender {
- protected WindowAppender() {
- super("agentAppender", null, null, true, Property.EMPTY_ARRAY);
- }
-
- @Override
- public void append(LogEvent event) {
- String allText = logArea.getText() + "\n" + event.getMessage().getFormattedMessage();
- logArea.setText(
- allText.substring(Math.max(0, allText.length() - MAX_LOG_CHARS), allText.length()));
- // TODO: Scroll to bottom
- }
- }
+public class AgentWindow extends JFrame implements WindowListener, LogListener {
+ public static final int MAX_LOG_CHARS = 100000;
protected final JTextArea logArea = new JTextArea();
protected final JScrollPane logScroll = new JScrollPane(logArea);
@@ -55,15 +46,38 @@ public class AgentWindow extends JFrame implements WindowListener {
add(new JLabel("<html>This agent is listening at <b>" + localAddress +
"</b>. Close this window to terminate it.</html>"), BorderLayout.NORTH);
logArea.setEditable(false);
- logArea.setFont(Font.getFont(Font.MONOSPACED));
+ logArea.setFont(Font.decode(Font.MONOSPACED));
logArea.setAutoscrolls(true);
logScroll.setAutoscrolls(true);
add(logScroll);
setMinimumSize(new Dimension(400, 300));
setVisible(true);
+ System.setProperty("log4j.configuration", "agent.log4j.xml");
+ LoggingInitialization.initializeLoggingSystem();
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
- ctx.getConfiguration().addAppender(new WindowAppender());
+ Configuration config = ctx.getConfiguration();
+ Appender appender = config.getAppender("logPanel");
+ if (!(appender instanceof LogPanelAppender)) {
+ Msg.error(this, "Couldn't find LogPanelAppender instance in the Log4j context. " +
+ "Nothing will be logged to the agent's window.");
+ }
+ else {
+ LogPanelAppender logPanelAppender = (LogPanelAppender) appender;
+ logPanelAppender.setLogListener(this);
+ }
+ }
+
+ @Override
+ public void messageLogged(String message, boolean isError) {
+ String fMessage = isError ? "<font color=\"red\">" + message + "</font>" : message;
+ Swing.runIfSwingOrRunLater(() -> {
+ String allText = logArea.getText() + fMessage + "\n";
+ logArea.setText(
+ allText.substring(Math.max(0, allText.length() - MAX_LOG_CHARS), allText.length()));
+ JScrollBar vScroll = logScroll.getVerticalScrollBar();
+ vScroll.setValue(vScroll.getMaximum());
+ });
}
@Override
diff --git a/Ghidra/Debug/Framework-Debugging/src/main/resources/agent.log4j.xml b/Ghidra/Debug/Framework-Debugging/src/main/resources/agent.log4j.xml
new file mode 100644
index 0000000000..3f75fa4f4d
--- /dev/null
+++ b/Ghidra/Debug/Framework-Debugging/src/main/resources/agent.log4j.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+ Basic Log4j configuration.
+
+ Note the 'packages' attribute; this is required if an appender needs to reference a class
+ in the codebase. For this configuration it's necessary for the 'logPanel' appender, which
+ references LogPanelAppender.java.
+-->
+<Configuration monitorInterval="30" xmlns:xi="http://www.w3.org/2001/XInclude" packages="log">
+ <Appenders>
+ <xi:include href="log4j-appender-console.xml"/>
+ <xi:include href="log4j-appender-logpanel.xml"/>
+ </Appenders>
+
+ <Loggers>
+ <Logger name="db" level="DEBUG"/>
+ <Logger name="docking" level="DEBUG"/>
+ <Logger name="ghidra" level="DEBUG"/>
+ <Logger name="reverse" level="DEBUG"/>
+ <Logger name="util" level="DEBUG"/>
+
+ <Root level="ALL">
+ <AppenderRef ref="console" level="DEBUG"/>
+ <AppenderRef ref="logPanel" level="DEBUG"/>
+ </Root>
+ </Loggers>
+</Configuration>
diff --git a/Ghidra/Debug/Framework-Debugging/src/main/resources/log4j-appender-console.xml b/Ghidra/Debug/Framework-Debugging/src/main/resources/log4j-appender-console.xml
new file mode 100644
index 0000000000..f1e2365c18
--- /dev/null
+++ b/Ghidra/Debug/Framework-Debugging/src/main/resources/log4j-appender-console.xml
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!-- Log4j appender that writes messages to the console. -->
+<Console
+ name="console"
+ target="SYSTEM_OUT">
+ <PatternLayout pattern="%-5p %m (%c{1}) %ex %n"/>
+</Console>
+
diff --git a/Ghidra/Debug/Framework-Debugging/src/main/resources/log4j-appender-logpanel.xml b/Ghidra/Debug/Framework-Debugging/src/main/resources/log4j-appender-logpanel.xml
new file mode 100644
index 0000000000..ce2a41dcee
--- /dev/null
+++ b/Ghidra/Debug/Framework-Debugging/src/main/resources/log4j-appender-logpanel.xml
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!-- Log4j appender that writes messages to the {@link LogPanelAppender}. -->
+<LogPanelAppender name="logPanel">
+ <PatternLayout pattern="%-5p %m %ex"/>
+</LogPanelAppender>
+