/
TraceType.java
513 lines (422 loc) · 17.7 KB
/
TraceType.java
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
package org.jruby.runtime.backtrace;
import java.io.ByteArrayOutputStream;
import java.io.FileDescriptor;
import java.io.PrintStream;
import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyClass;
import org.jruby.RubyException;
import org.jruby.RubyInstanceConfig;
import org.jruby.RubyString;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.log.Logger;
import org.jruby.util.log.LoggerFactory;
public class TraceType {
private static final Logger LOG = LoggerFactory.getLogger(TraceType.class);
private final Gather gather;
private final Format format;
public TraceType(Gather gather, Format format) {
this.gather = gather;
this.format = format;
}
public Gather getGather() {
return gather;
}
public Format getFormat() {
return format;
}
/**
* Get a normal Ruby backtrace, using the current Gather type.
*
* @param context
* @return
*/
public BacktraceData getBacktrace(ThreadContext context) {
return gather.getBacktraceData(context);
}
/**
* Get an integrated Ruby/Java backtrace if the current Gather type is NORMAL
*
* @param context
* @param javaTrace
* @return
*/
public BacktraceData getIntegratedBacktrace(ThreadContext context, StackTraceElement[] javaTrace) {
return gather.getIntegratedBacktraceData(context, javaTrace);
}
public String printBacktrace(RubyException exception, boolean console) {
return format.printBacktrace(exception, console);
}
public static void logBacktrace(RubyStackTraceElement[] trace) {
if (trace == null) trace = RubyStackTraceElement.EMPTY_ARRAY;
final StringBuilder buffer = new StringBuilder(64 + trace.length * 48);
buffer.append("Backtrace generated:\n");
renderBacktraceJRuby(trace, buffer, false);
// NOTE: other logXxx method do not remove the new-line
// ... but if this is desired they should do so as well
//final int len = buffer.length();
//if ( len > 0 && buffer.charAt(len - 1) == '\n' ) {
// buffer.setLength(len - 1); // remove last '\n'
//}
LOG.info(buffer.toString());
}
public static void logException(RubyException exception) {
LOG.info("Exception raised: {} : {}", exception.getMetaClass(), exception);
}
/**
* @deprecated use {@link #logException(org.jruby.RubyException)}
*/
public static void dumpException(RubyException exception) {
logException(exception);
}
public static void dumpBacktrace(RubyException exception) {
Ruby runtime = exception.getRuntime();
System.err.println("Backtrace generated:\n" + printBacktraceJRuby(exception, runtime.getPosix().isatty(FileDescriptor.err)));
}
public static void logCaller(RubyArray trace) {
StringBuilder buffer = new StringBuilder(64 + trace.size() * 48);
buffer.append("Caller backtrace generated:\n");
for (int i = 0; i < trace.size(); i++) {
// elements are already rendered as its an Array<RubyString>
buffer.append(" ").append(trace.eltInternal(i)).append('\n');
}
LOG.info(buffer.toString());
}
/**
* @deprecated use {@link #logCaller(org.jruby.RubyArray)}
*/
public static void dumpCaller(RubyArray trace) {
logCaller(trace);
}
public static void logCaller(RubyStackTraceElement[] trace) {
if (trace == null) trace = RubyStackTraceElement.EMPTY_ARRAY;
LOG.info( formatWithMRIBacktrace("Caller backtrace generated:\n", trace).toString() );
}
private static StringBuilder formatWithMRIBacktrace(final String message, RubyStackTraceElement[] trace) {
StringBuilder buffer = new StringBuilder(64 + trace.length * 48);
buffer.append(message);
renderBacktraceMRI(trace, " ", buffer, false);
return buffer;
}
/**
* @deprecated use {@link #logCaller(org.jruby.runtime.backtrace.RubyStackTraceElement[]) }
*/
public static void dumpCaller(RubyStackTraceElement[] trace) {
logCaller(trace);
}
public static void logWarning(RubyStackTraceElement[] trace) {
if (trace == null) trace = RubyStackTraceElement.EMPTY_ARRAY;
LOG.info( formatWithMRIBacktrace("Warning backtrace generated:\n", trace).toString() );
}
/**
* @deprecated use {@link #logWarning(org.jruby.runtime.backtrace.RubyStackTraceElement[])
*/
public static void dumpWarning(RubyStackTraceElement[] trace) {
logWarning(trace);
}
public static TraceType traceTypeFor(String style) {
if (style.equalsIgnoreCase("raw")) return new TraceType(Gather.RAW, Format.JRUBY);
else if (style.equalsIgnoreCase("ruby_framed")) return new TraceType(Gather.NORMAL, Format.JRUBY);
else if (style.equalsIgnoreCase("normal")) return new TraceType(Gather.NORMAL, Format.JRUBY);
else if (style.equalsIgnoreCase("full")) return new TraceType(Gather.FULL, Format.JRUBY);
else if (style.equalsIgnoreCase("mri")) return new TraceType(Gather.NORMAL, Format.MRI);
else return new TraceType(Gather.NORMAL, Format.JRUBY);
}
public enum Gather {
/**
* Full raw backtraces with all Java frames included.
*/
RAW {
public BacktraceData getBacktraceData(ThreadContext context, StackTraceElement[] javaTrace) {
return new BacktraceData(
javaTrace,
BacktraceElement.EMPTY_ARRAY,
true,
false,
false);
}
},
/**
* A backtrace with interpreted frames intact, but don't remove Java frames.
*/
FULL {
public BacktraceData getBacktraceData(ThreadContext context, StackTraceElement[] javaTrace) {
return new BacktraceData(
javaTrace,
context.getBacktrace(),
true,
false,
false);
}
},
/**
* A normal Ruby-style backtrace, but which includes any non-org.jruby frames
*/
INTEGRATED {
public BacktraceData getBacktraceData(ThreadContext context, StackTraceElement[] javaTrace) {
return new BacktraceData(
javaTrace,
context.getBacktrace(),
false,
false,
true);
}
},
/**
* Normal Ruby-style backtrace, showing only Ruby and core class methods.
*/
NORMAL {
public BacktraceData getBacktraceData(ThreadContext context, StackTraceElement[] javaTrace) {
return new BacktraceData(
javaTrace,
context.getBacktrace(),
false,
context.runtime.getInstanceConfig().getBacktraceMask(),
false);
}
},
/**
* Normal Ruby-style backtrace, showing only Ruby and core class methods.
*/
CALLER {
public BacktraceData getBacktraceData(ThreadContext context, StackTraceElement[] javaTrace) {
return new BacktraceData(
javaTrace,
context.getBacktrace(),
false,
true,
false);
}
};
/**
* Gather backtrace data for a normal Ruby trace.
*
* @param context
* @return
*/
public BacktraceData getBacktraceData(ThreadContext context) {
BacktraceData data = getBacktraceData(context, Thread.currentThread().getStackTrace());
context.runtime.incrementBacktraceCount();
if (RubyInstanceConfig.LOG_BACKTRACES) logBacktrace(data.getBacktrace(context.runtime));
return data;
}
/**
* Gather backtrace data for an integrated trace if the current gather type is "NORMAL", otherwise use the
* current gather type.
*
* @param context
* @param javaTrace
* @return
*/
public BacktraceData getIntegratedBacktraceData(ThreadContext context, StackTraceElement[] javaTrace) {
Gather useGather = this;
if (useGather == NORMAL) {
useGather = INTEGRATED;
}
BacktraceData data = useGather.getBacktraceData(context, javaTrace);
context.runtime.incrementBacktraceCount();
if (RubyInstanceConfig.LOG_BACKTRACES) logBacktrace(data.getBacktrace(context.runtime));
return data;
}
public abstract BacktraceData getBacktraceData(ThreadContext context, StackTraceElement[] javaTrace);
}
public enum Format {
/**
* Formatting like C Ruby
*/
MRI {
public String printBacktrace(RubyException exception, boolean console) {
return printBacktraceMRI(exception, console);
}
public void renderBacktrace(RubyStackTraceElement[] elts, StringBuilder buffer, boolean color) {
renderBacktraceMRI(elts, buffer, color);
}
},
/**
* New JRuby formatting
*/
JRUBY {
public String printBacktrace(RubyException exception, boolean console) {
return printBacktraceJRuby(exception, console);
}
public void renderBacktrace(RubyStackTraceElement[] elts, StringBuilder buffer, boolean color) {
renderBacktraceJRuby(elts, buffer, color);
}
};
public abstract String printBacktrace(RubyException exception, boolean console);
public abstract void renderBacktrace(RubyStackTraceElement[] elts, StringBuilder buffer, boolean color);
}
protected static String printBacktraceMRI(RubyException exception, boolean console) {
final Ruby runtime = exception.getRuntime();
final ThreadContext context = runtime.getCurrentContext();
final IRubyObject backtrace = exception.callMethod(context, "backtrace");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream errorStream = new PrintStream(baos);
boolean printedPosition = false;
if (backtrace.isNil() || !(backtrace instanceof RubyArray)) {
if (context.getFile() != null && context.getFile().length() > 0) {
errorStream.print(context.getFile() + ':' + context.getLine());
printedPosition = true;
} else {
errorStream.print(context.getLine());
printedPosition = true;
}
} else if (((RubyArray) backtrace).getLength() == 0) {
printErrorPos(context, errorStream);
} else {
IRubyObject mesg = ((RubyArray) backtrace).first();
if (mesg.isNil()) {
printErrorPos(context, errorStream);
} else {
errorStream.print(mesg);
printedPosition = true;
}
}
RubyClass type = exception.getMetaClass();
String info = exception.toString();
if (type == runtime.getRuntimeError() && (info == null || info.length() == 0)) {
errorStream.print(": unhandled exception\n");
} else {
if (printedPosition) errorStream.print(": ");
String path = type.getName();
if (info.length() == 0) {
errorStream.print(path + '\n');
} else {
if (path.startsWith("#")) {
path = null;
}
String tail = null;
int idx = info.indexOf('\n');
if (idx != -1) {
tail = info.substring(idx + 1);
info = info.substring(0, idx);
}
errorStream.print(info);
if (path != null) {
errorStream.print(" (" + path + ")\n");
} else {
errorStream.print('\n');
}
if (tail != null) {
errorStream.print(tail + '\n');
}
}
}
exception.printBacktrace(errorStream, 1);
return new String(baos.toByteArray());
}
private static final String FIRST_COLOR = "\033[0;31m";
private static final String KERNEL_COLOR = "\033[0;36m";
private static final String EVAL_COLOR = "\033[0;33m";
private static final String CLEAR_COLOR = "\033[0m";
public static String printBacktraceJRuby(RubyStackTraceElement[] frames, String type, String message, boolean color) {
if (frames == null) frames = RubyStackTraceElement.EMPTY_ARRAY;
StringBuilder buffer = new StringBuilder(64 + frames.length * 48);
buffer.append(type).append(": ").append(message).append('\n');
renderBacktraceJRuby(frames, buffer, color);
return buffer.toString();
}
protected static String printBacktraceJRuby(RubyException exception, boolean console) {
final Ruby runtime = exception.getRuntime();
final ThreadContext context = runtime.getCurrentContext();
boolean color = console && runtime.getInstanceConfig().getBacktraceColor();
// exception line
String message;
try {
message = exception.callMethod(context, "message").toString();
} catch (org.jruby.exceptions.Exception _) {
message = exception.message(context).toString();
}
if (exception.getMetaClass() == runtime.getRuntimeError() && message.length() == 0) {
message = "No current exception";
}
String type = exception.getMetaClass().getName();
RubyStackTraceElement[] frames = exception.getBacktraceElements();
return printBacktraceJRuby(frames, type, message, color);
}
private static void renderBacktraceJRuby(RubyStackTraceElement[] frames, StringBuilder buffer, boolean color) {
// find longest method name
int longestMethod = 0;
for (RubyStackTraceElement frame : frames) {
longestMethod = Math.max(longestMethod, frame.getMethodName().length());
}
// backtrace lines
boolean first = true;
for (RubyStackTraceElement frame : frames) {
if (color) {
if (first) {
buffer.append(FIRST_COLOR);
} else if (frame.isBinding() || frame.getFileName().equals("(eval)")) {
buffer.append(EVAL_COLOR);
} else if (frame.getFileName().indexOf(".java") != -1) {
buffer.append(KERNEL_COLOR);
}
first = false;
}
buffer.append(" ");
// method name
String methodName = frame.getMethodName();
for (int j = 0; j < longestMethod - methodName.length(); j++) {
buffer.append(' ');
}
buffer
.append(methodName)
.append(" at ")
.append(frame.getFileName())
.append(':')
.append(frame.getLineNumber());
if (color) {
buffer.append(CLEAR_COLOR);
}
buffer
.append('\n');
}
}
private static void renderBacktraceMRI(RubyStackTraceElement[] trace, StringBuilder buffer, boolean color) {
renderBacktraceMRI(trace, "", buffer, color);
}
private static void renderBacktraceMRI(RubyStackTraceElement[] trace, String linePrefix, StringBuilder buffer, boolean color) {
for (int i = 0; i < trace.length; i++) {
RubyStackTraceElement element = trace[i];
buffer
.append(linePrefix)
.append(element.getFileName())
.append(':')
.append(element.getLineNumber())
.append(":in `")
.append(element.getMethodName())
.append("'\n");
}
}
public static IRubyObject generateMRIBacktrace(Ruby runtime, RubyStackTraceElement[] trace) {
if (trace == null) return runtime.getNil();
final RubyClass stringClass = runtime.getString();
final IRubyObject[] traceArray = new IRubyObject[trace.length];
final StringBuilder line = new StringBuilder();
for (int i = 0; i < trace.length; i++) {
RubyStackTraceElement element = trace[i];
line.setLength(0);
line.append( element.getFileName() )
.append(':')
.append( element.getLineNumber() )
.append(":in `")
.append( element.getMethodName() )
.append('\'');
traceArray[i] = new RubyString(runtime, stringClass, line.toString()); // must toString
}
return RubyArray.newArrayMayCopy(runtime, traceArray);
}
private static void printErrorPos(ThreadContext context, PrintStream errorStream) {
if (context.getFile() != null && context.getFile().length() > 0) {
if (context.getFrameName() != null) {
errorStream.print(context.getFile() + ':' + context.getLine());
errorStream.print(":in '" + context.getFrameName() + '\'');
} else if (context.getLine() != 0) {
errorStream.print(context.getFile() + ':' + context.getLine());
} else {
errorStream.print(context.getFile());
}
}
}
}