1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: SirYwell <hannesgreule@outlook.de>
Date: Sat, 10 Jul 2021 11:12:30 +0200
Subject: [PATCH] Rewrite LogEvents to contain the source jars in stack traces
diff --git a/src/main/java/io/papermc/paper/logging/DelegateLogEvent.java b/src/main/java/io/papermc/paper/logging/DelegateLogEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..6ffd1befe64c6c3036c22e05ed1c44808d64bd28
--- /dev/null
+++ b/src/main/java/io/papermc/paper/logging/DelegateLogEvent.java
@@ -0,0 +1,130 @@
+package io.papermc.paper.logging;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.Marker;
+import org.apache.logging.log4j.ThreadContext;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.impl.ThrowableProxy;
+import org.apache.logging.log4j.core.time.Instant;
+import org.apache.logging.log4j.message.Message;
+import org.apache.logging.log4j.util.ReadOnlyStringMap;
+
+import java.util.Map;
+
+public class DelegateLogEvent implements LogEvent {
+ private final LogEvent original;
+
+ protected DelegateLogEvent(LogEvent original) {
+ this.original = original;
+ }
+
+ @Override
+ public LogEvent toImmutable() {
+ return this.original.toImmutable();
+ }
+
+ @Override
+ public Map<String, String> getContextMap() {
+ return this.original.getContextMap();
+ }
+
+ @Override
+ public ReadOnlyStringMap getContextData() {
+ return this.original.getContextData();
+ }
+
+ @Override
+ public ThreadContext.ContextStack getContextStack() {
+ return this.original.getContextStack();
+ }
+
+ @Override
+ public String getLoggerFqcn() {
+ return this.original.getLoggerFqcn();
+ }
+
+ @Override
+ public Level getLevel() {
+ return this.original.getLevel();
+ }
+
+ @Override
+ public String getLoggerName() {
+ return this.original.getLoggerName();
+ }
+
+ @Override
+ public Marker getMarker() {
+ return this.original.getMarker();
+ }
+
+ @Override
+ public Message getMessage() {
+ return this.original.getMessage();
+ }
+
+ @Override
+ public long getTimeMillis() {
+ return this.original.getTimeMillis();
+ }
+
+ @Override
+ public Instant getInstant() {
+ return this.original.getInstant();
+ }
+
+ @Override
+ public StackTraceElement getSource() {
+ return this.original.getSource();
+ }
+
+ @Override
+ public String getThreadName() {
+ return this.original.getThreadName();
+ }
+
+ @Override
+ public long getThreadId() {
+ return this.original.getThreadId();
+ }
+
+ @Override
+ public int getThreadPriority() {
+ return this.original.getThreadPriority();
+ }
+
+ @Override
+ public Throwable getThrown() {
+ return this.original.getThrown();
+ }
+
+ @Override
+ public ThrowableProxy getThrownProxy() {
+ return this.original.getThrownProxy();
+ }
+
+ @Override
+ public boolean isEndOfBatch() {
+ return this.original.isEndOfBatch();
+ }
+
+ @Override
+ public boolean isIncludeLocation() {
+ return this.original.isIncludeLocation();
+ }
+
+ @Override
+ public void setEndOfBatch(boolean endOfBatch) {
+ this.original.setEndOfBatch(endOfBatch);
+ }
+
+ @Override
+ public void setIncludeLocation(boolean locationRequired) {
+ this.original.setIncludeLocation(locationRequired);
+ }
+
+ @Override
+ public long getNanoTime() {
+ return this.original.getNanoTime();
+ }
+}
diff --git a/src/main/java/io/papermc/paper/logging/ExtraClassInfoLogEvent.java b/src/main/java/io/papermc/paper/logging/ExtraClassInfoLogEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..e58dd3db1f97c049610d8f85a6f787f8d1011f73
--- /dev/null
+++ b/src/main/java/io/papermc/paper/logging/ExtraClassInfoLogEvent.java
@@ -0,0 +1,104 @@
+package io.papermc.paper.logging;
+
+import io.papermc.paper.util.ObfHelper;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.impl.ExtendedClassInfo;
+import org.apache.logging.log4j.core.impl.ExtendedStackTraceElement;
+import org.apache.logging.log4j.core.impl.ThrowableProxy;
+
+public class ExtraClassInfoLogEvent extends DelegateLogEvent {
+ private static final Map<Class<?>, ExtendedClassInfo> EXTRA_INFO_CACHE = Collections.synchronizedMap(new LinkedHashMap<>(256, 0.75f, true) {
+ @Override
+ protected boolean removeEldestEntry(final Map.Entry<Class<?>, ExtendedClassInfo> eldest) {
+ return this.size() > 255;
+ }
+ });
+ private static final Method TO_CACHE_ENTRY;
+ private static final Field ELEMENT;
+
+ static {
+ try {
+ final Class<?> cls = Class.forName("org.apache.logging.log4j.core.impl.ThrowableProxyHelper");
+ TO_CACHE_ENTRY = cls.getDeclaredMethod("toCacheEntry", Class.class, boolean.class);
+ TO_CACHE_ENTRY.setAccessible(true);
+ ELEMENT = Class.forName("org.apache.logging.log4j.core.impl.ThrowableProxyHelper.CacheEntry")
+ .getDeclaredField("element");
+ ELEMENT.setAccessible(true);
+ } catch (final ReflectiveOperationException ex) {
+ throw new RuntimeException(ex);
+ }
+ }
+
+ private boolean fixed;
+
+ public ExtraClassInfoLogEvent(LogEvent original) {
+ super(original);
+ }
+
+ @Override
+ public ThrowableProxy getThrownProxy() {
+ if (fixed) {
+ return super.getThrownProxy();
+ }
+ rewriteStackTrace(super.getThrownProxy());
+ fixed = true;
+ return super.getThrownProxy();
+ }
+
+ private void rewriteStackTrace(ThrowableProxy throwable) {
+ ExtendedStackTraceElement[] stackTrace = throwable.getExtendedStackTrace();
+ for (int i = 0; i < stackTrace.length; i++) {
+ ExtendedClassInfo classInfo = stackTrace[i].getExtraClassInfo();
+ if (classInfo.getLocation().equals("?")) {
+ if (this.handleDeobfed(stackTrace, i)) {
+ continue;
+ }
+
+ StackTraceElement element = stackTrace[i].getStackTraceElement();
+ String classLoaderName = element.getClassLoaderName();
+ if (classLoaderName != null) {
+ stackTrace[i] = new ExtendedStackTraceElement(element,
+ new ExtendedClassInfo(classInfo.getExact(), classLoaderName, "?"));
+ }
+ }
+ }
+ if (throwable.getCauseProxy() != null) {
+ rewriteStackTrace(throwable.getCauseProxy());
+ }
+ if (throwable.getSuppressedProxies() != null) {
+ for (ThrowableProxy proxy : throwable.getSuppressedProxies()) {
+ rewriteStackTrace(proxy);
+ }
+ }
+ }
+
+ private boolean handleDeobfed(
+ final ExtendedStackTraceElement[] stackTrace,
+ final int i
+ ) {
+ final String className = stackTrace[i].getClassName();
+ final String reobf = ObfHelper.INSTANCE.reobfClassName(className);
+ if (!reobf.equals(className)) {
+ try {
+ final ExtendedClassInfo extraInfo = EXTRA_INFO_CACHE.computeIfAbsent(Class.forName(reobf), k -> {
+ try {
+ final Object cacheEntry = TO_CACHE_ENTRY.invoke(null, k, true);
+ return (ExtendedClassInfo) ELEMENT.get(cacheEntry);
+ } catch (final ReflectiveOperationException e) {
+ throw new RuntimeException(e);
+ }
+ });
+ stackTrace[i] = new ExtendedStackTraceElement(stackTrace[i].getStackTraceElement(), extraInfo);
+ return true;
+ } catch (final ReflectiveOperationException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ return false;
+ }
+}
diff --git a/src/main/java/io/papermc/paper/logging/ExtraClassInfoRewritePolicy.java b/src/main/java/io/papermc/paper/logging/ExtraClassInfoRewritePolicy.java
new file mode 100644
index 0000000000000000000000000000000000000000..34734bb969a1a74c7a4f9c17d40ebf007ad5d701
--- /dev/null
+++ b/src/main/java/io/papermc/paper/logging/ExtraClassInfoRewritePolicy.java
@@ -0,0 +1,29 @@
+package io.papermc.paper.logging;
+
+import org.apache.logging.log4j.core.Core;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.appender.rewrite.RewritePolicy;
+import org.apache.logging.log4j.core.config.plugins.Plugin;
+import org.apache.logging.log4j.core.config.plugins.PluginFactory;
+import org.jetbrains.annotations.NotNull;
+
+@Plugin(
+ name = "ExtraClassInfoRewritePolicy",
+ category = Core.CATEGORY_NAME,
+ elementType = "rewritePolicy",
+ printObject = true
+)
+public final class ExtraClassInfoRewritePolicy implements RewritePolicy {
+ @Override
+ public LogEvent rewrite(LogEvent source) {
+ if (source.getThrown() != null) {
+ return new ExtraClassInfoLogEvent(source);
+ }
+ return source;
+ }
+
+ @PluginFactory
+ public static @NotNull ExtraClassInfoRewritePolicy createPolicy() {
+ return new ExtraClassInfoRewritePolicy();
+ }
+}
diff --git a/src/main/resources/log4j2.xml b/src/main/resources/log4j2.xml
index 2e421eaac80cf251b32e0bb504dd54a73edf4986..74ccc67e3c12dc5182602fb691ef3ddeb5b53280 100644
--- a/src/main/resources/log4j2.xml
+++ b/src/main/resources/log4j2.xml
@@ -34,6 +34,10 @@
</Async>
<Rewrite name="rewrite">
<StacktraceDeobfuscatingRewritePolicy />
+ <AppenderRef ref="rewrite2"/>
+ </Rewrite>
+ <Rewrite name="rewrite2">
+ <ExtraClassInfoRewritePolicy />
<AppenderRef ref="File"/>
<AppenderRef ref="TerminalConsole" level="info"/>
<AppenderRef ref="ServerGuiConsole" level="info"/>
|