CrashReport .java

net.minecraft.crash.CrashReport (class, 396 lines): members: firstCategoryInCrashReport, getCauseStackTraceOrString, getSectionsInStringBuilder, cause.

net.minecraft.crashclass396 linescommon13.9 KB

API Surface (18)

String call()
String call()
String call()
String call()
String call()
String call()
String call()
String getDescription()
Throwable getCrashCause()
void getSectionsInStringBuilder(StringBuilder builder)
String getCauseStackTraceOrString()
String getCompleteReport()
File getFile()
boolean saveToFile(File toFile)
CrashReportCategory getCategory()
CrashReportCategory makeCategory(String name)
CrashReportCategory makeCategoryDepth(String categoryName, int stacktraceLength)
CrashReport makeCrashReport(Throwable causeIn, String descriptionIn)

Fields & Constants (8)

logger
description
cause
theReportCategory
crashReportSections
crashReportFile
firstCategoryInCrashReport
stacktrace

String literals (56)

"System Details"
"Minecraft Version"
"Operating System"
"os.name"
"os.arch"
") version "
"os.version"
"Java Version"
"java.version"
"java.vendor"
"Java VM Version"
"java.vm.name"
"java.vm.info"
"java.vm.vendor"
"Memory"
" bytes ("
"JVM Flags"
"IntCache"
"-- Head --\n"
"Stacktrace:\n"
"---- Minecraft Crash Report ----\n"
"Time: "
"Description: "
"Could not save crash report to "

Same Package (1)

Code Graph: Dependencies (3)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

CrashReport.java396 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.crash;
2
3
import com.google.common.collect.Lists;
4
import java.io.File;
5
import java.io.FileWriter;
6
import java.io.PrintWriter;
7
import java.io.StringWriter;
8
import java.io.Writer;
9
import java.lang.management.ManagementFactory;
10
import java.lang.management.RuntimeMXBean;
11
import java.text.SimpleDateFormat;
12
import java.util.Date;
13
import java.util.List;
14
import java.util.concurrent.Callable;
15
import net.minecraft.util.ReportedException;
16
import net.minecraft.world.gen.layer.IntCache;
17
import org.apache.commons.io.IOUtils;
18
import org.apache.commons.lang3.ArrayUtils;
19
import org.apache.logging.log4j.LogManager;
20
import org.apache.logging.log4j.Logger;
21
22
public class CrashReport
23
{
24
    private static final Logger logger = LogManager.getLogger();
25
26
    /** Description of the crash report. */
27
    private final String description;
28
29
    /** The Throwable that is the "cause" for this crash and Crash Report. */
30
    private final Throwable cause;
31
32
    /** Category of crash */
33
    private final CrashReportCategory theReportCategory = new CrashReportCategory(this, "System Details");
34
    private final List<CrashReportCategory> crashReportSections = Lists.<CrashReportCategory>newArrayList();
35
36
    /** File of crash report. */
37
    private File crashReportFile;
38
39
    /** Is true when the current category is the first in the crash report */
40
    private boolean firstCategoryInCrashReport = true;
41
    private StackTraceElement[] stacktrace = new StackTraceElement[0];
42
43
    public CrashReport(String descriptionIn, Throwable causeThrowable)
44
    {
45
        this.description = descriptionIn;
46
        this.cause = causeThrowable;
47
        this.populateEnvironment();
48
    }
49
50
    /**
51
     * Populates this crash report with initial information about the running server and operating system / java
52
     * environment
53
     */
54
    private void populateEnvironment()
55
    {
56
        this.theReportCategory.addCrashSectionCallable("Minecraft Version", new Callable<String>()
57
        {
58
            public String call()
59
            {
60
                return "1.8.9";
61
            }
62
        });
63
        this.theReportCategory.addCrashSectionCallable("Operating System", new Callable<String>()
64
        {
65
            public String call()
66
            {
67
                return System.getProperty("os.name") + " (" + System.getProperty("os.arch") + ") version " + System.getProperty("os.version");
68
            }
69
        });
70
        this.theReportCategory.addCrashSectionCallable("Java Version", new Callable<String>()
71
        {
72
            public String call()
73
            {
74
                return System.getProperty("java.version") + ", " + System.getProperty("java.vendor");
75
            }
76
        });
77
        this.theReportCategory.addCrashSectionCallable("Java VM Version", new Callable<String>()
78
        {
79
            public String call()
80
            {
81
                return System.getProperty("java.vm.name") + " (" + System.getProperty("java.vm.info") + "), " + System.getProperty("java.vm.vendor");
82
            }
83
        });
84
        this.theReportCategory.addCrashSectionCallable("Memory", new Callable<String>()
85
        {
86
            public String call()
87
            {
88
                Runtime runtime = Runtime.getRuntime();
89
                long i = runtime.maxMemory();
90
                long j = runtime.totalMemory();
91
                long k = runtime.freeMemory();
92
                long l = i / 1024L / 1024L;
93
                long i1 = j / 1024L / 1024L;
94
                long j1 = k / 1024L / 1024L;
95
                return k + " bytes (" + j1 + " MB) / " + j + " bytes (" + i1 + " MB) up to " + i + " bytes (" + l + " MB)";
96
            }
97
        });
98
        this.theReportCategory.addCrashSectionCallable("JVM Flags", new Callable<String>()
99
        {
100
            public String call()
101
            {
102
                RuntimeMXBean runtimemxbean = ManagementFactory.getRuntimeMXBean();
103
                List<String> list = runtimemxbean.getInputArguments();
104
                int i = 0;
105
                StringBuilder stringbuilder = new StringBuilder();
106
107
                for (String s : list)
108
                {
109
                    if (s.startsWith("-X"))
110
                    {
111
                        if (i++ > 0)
112
                        {
113
                            stringbuilder.append(" ");
114
                        }
115
116
                        stringbuilder.append(s);
117
                    }
118
                }
119
120
                return String.format("%d total; %s", new Object[] {Integer.valueOf(i), stringbuilder.toString()});
121
            }
122
        });
123
        this.theReportCategory.addCrashSectionCallable("IntCache", new Callable<String>()
124
        {
125
            public String call() throws Exception
126
            {
127
                return IntCache.getCacheSizes();
128
            }
129
        });
130
    }
131
132
    /**
133
     * Returns the description of the Crash Report.
134
     */
135
    public String getDescription()
136
    {
137
        return this.description;
138
    }
139
140
    /**
141
     * Returns the Throwable object that is the cause for the crash and Crash Report.
142
     */
143
    public Throwable getCrashCause()
144
    {
145
        return this.cause;
146
    }
147
148
    /**
149
     * Gets the various sections of the crash report into the given StringBuilder
150
     */
151
    public void getSectionsInStringBuilder(StringBuilder builder)
152
    {
153
        if ((this.stacktrace == null || this.stacktrace.length <= 0) && this.crashReportSections.size() > 0)
154
        {
155
            this.stacktrace = (StackTraceElement[])ArrayUtils.subarray(((CrashReportCategory)this.crashReportSections.get(0)).getStackTrace(), 0, 1);
156
        }
157
158
        if (this.stacktrace != null && this.stacktrace.length > 0)
159
        {
160
            builder.append("-- Head --\n");
161
            builder.append("Stacktrace:\n");
162
163
            for (StackTraceElement stacktraceelement : this.stacktrace)
164
            {
165
                builder.append("\t").append("at ").append(stacktraceelement.toString());
166
                builder.append("\n");
167
            }
168
169
            builder.append("\n");
170
        }
171
172
        for (CrashReportCategory crashreportcategory : this.crashReportSections)
173
        {
174
            crashreportcategory.appendToStringBuilder(builder);
175
            builder.append("\n\n");
176
        }
177
178
        this.theReportCategory.appendToStringBuilder(builder);
179
    }
180
181
    /**
182
     * Gets the stack trace of the Throwable that caused this crash report, or if that fails, the cause .toString().
183
     */
184
    public String getCauseStackTraceOrString()
185
    {
186
        StringWriter stringwriter = null;
187
        PrintWriter printwriter = null;
188
        Throwable throwable = this.cause;
189
190
        if (throwable.getMessage() == null)
191
        {
192
            if (throwable instanceof NullPointerException)
193
            {
194
                throwable = new NullPointerException(this.description);
195
            }
196
            else if (throwable instanceof StackOverflowError)
197
            {
198
                throwable = new StackOverflowError(this.description);
199
            }
200
            else if (throwable instanceof OutOfMemoryError)
201
            {
202
                throwable = new OutOfMemoryError(this.description);
203
            }
204
205
            throwable.setStackTrace(this.cause.getStackTrace());
206
        }
207
208
        String s = throwable.toString();
209
210
        try
211
        {
212
            stringwriter = new StringWriter();
213
            printwriter = new PrintWriter(stringwriter);
214
            throwable.printStackTrace(printwriter);
215
            s = stringwriter.toString();
216
        }
217
        finally
218
        {
219
            IOUtils.closeQuietly((Writer)stringwriter);
220
            IOUtils.closeQuietly((Writer)printwriter);
221
        }
222
223
        return s;
224
    }
225
226
    /**
227
     * Gets the complete report with headers, stack trace, and different sections as a string.
228
     */
229
    public String getCompleteReport()
230
    {
231
        StringBuilder stringbuilder = new StringBuilder();
232
        stringbuilder.append("---- Minecraft Crash Report ----\n");
233
        stringbuilder.append("// ");
234
        stringbuilder.append(getWittyComment());
235
        stringbuilder.append("\n\n");
236
        stringbuilder.append("Time: ");
237
        stringbuilder.append((new SimpleDateFormat()).format(new Date()));
238
        stringbuilder.append("\n");
239
        stringbuilder.append("Description: ");
240
        stringbuilder.append(this.description);
241
        stringbuilder.append("\n\n");
242
        stringbuilder.append(this.getCauseStackTraceOrString());
243
        stringbuilder.append("\n\nA detailed walkthrough of the error, its code path and all known details is as follows:\n");
244
245
        for (int i = 0; i < 87; ++i)
246
        {
247
            stringbuilder.append("-");
248
        }
249
250
        stringbuilder.append("\n\n");
251
        this.getSectionsInStringBuilder(stringbuilder);
252
        return stringbuilder.toString();
253
    }
254
255
    /**
256
     * Gets the file this crash report is saved into.
257
     */
258
    public File getFile()
259
    {
260
        return this.crashReportFile;
261
    }
262
263
    /**
264
     * Saves this CrashReport to the given file and returns a value indicating whether we were successful at doing so.
265
     */
266
    public boolean saveToFile(File toFile)
267
    {
268
        if (this.crashReportFile != null)
269
        {
270
            return false;
271
        }
272
        else
273
        {
274
            if (toFile.getParentFile() != null)
275
            {
276
                toFile.getParentFile().mkdirs();
277
            }
278
279
            try
280
            {
281
                FileWriter filewriter = new FileWriter(toFile);
282
                filewriter.write(this.getCompleteReport());
283
                filewriter.close();
284
                this.crashReportFile = toFile;
285
                return true;
286
            }
287
            catch (Throwable throwable)
288
            {
289
                logger.error("Could not save crash report to " + toFile, throwable);
290
                return false;
291
            }
292
        }
293
    }
294
295
    public CrashReportCategory getCategory()
296
    {
297
        return this.theReportCategory;
298
    }
299
300
    /**
301
     * Creates a CrashReportCategory
302
     */
303
    public CrashReportCategory makeCategory(String name)
304
    {
305
        return this.makeCategoryDepth(name, 1);
306
    }
307
308
    /**
309
     * Creates a CrashReportCategory for the given stack trace depth
310
     */
311
    public CrashReportCategory makeCategoryDepth(String categoryName, int stacktraceLength)
312
    {
313
        CrashReportCategory crashreportcategory = new CrashReportCategory(this, categoryName);
314
315
        if (this.firstCategoryInCrashReport)
316
        {
317
            int i = crashreportcategory.getPrunedStackTrace(stacktraceLength);
318
            StackTraceElement[] astacktraceelement = this.cause.getStackTrace();
319
            StackTraceElement stacktraceelement = null;
320
            StackTraceElement stacktraceelement1 = null;
321
            int j = astacktraceelement.length - i;
322
323
            if (j < 0)
324
            {
325
                System.out.println("Negative index in crash report handler (" + astacktraceelement.length + "/" + i + ")");
326
            }
327
328
            if (astacktraceelement != null && 0 <= j && j < astacktraceelement.length)
329
            {
330
                stacktraceelement = astacktraceelement[j];
331
332
                if (astacktraceelement.length + 1 - i < astacktraceelement.length)
333
                {
334
                    stacktraceelement1 = astacktraceelement[astacktraceelement.length + 1 - i];
335
                }
336
            }
337
338
            this.firstCategoryInCrashReport = crashreportcategory.firstTwoElementsOfStackTraceMatch(stacktraceelement, stacktraceelement1);
339
340
            if (i > 0 && !this.crashReportSections.isEmpty())
341
            {
342
                CrashReportCategory crashreportcategory1 = (CrashReportCategory)this.crashReportSections.get(this.crashReportSections.size() - 1);
343
                crashreportcategory1.trimStackTraceEntriesFromBottom(i);
344
            }
345
            else if (astacktraceelement != null && astacktraceelement.length >= i && 0 <= j && j < astacktraceelement.length)
346
            {
347
                this.stacktrace = new StackTraceElement[j];
348
                System.arraycopy(astacktraceelement, 0, this.stacktrace, 0, this.stacktrace.length);
349
            }
350
            else
351
            {
352
                this.firstCategoryInCrashReport = false;
353
            }
354
        }
355
356
        this.crashReportSections.add(crashreportcategory);
357
        return crashreportcategory;
358
    }
359
360
    /**
361
     * Gets a random witty comment for inclusion in this CrashReport
362
     */
363
    private static String getWittyComment()
364
    {
365
        String[] astring = new String[] {"Who set us up the TNT?", "Everything\'s going to plan. No, really, that was supposed to happen.", "Uh... Did I do that?", "Oops.", "Why did you do that?", "I feel sad now :(", "My bad.", "I\'m sorry, Dave.", "I let you down. Sorry :(", "On the bright side, I bought you a teddy bear!", "Daisy, daisy...", "Oh - I know what I did wrong!", "Hey, that tickles! Hehehe!", "I blame Dinnerbone.", "You should try our sister game, Minceraft!", "Don\'t be sad. I\'ll do better next time, I promise!", "Don\'t be sad, have a hug! <3", "I just don\'t know what went wrong :(", "Shall we play a game?", "Quite honestly, I wouldn\'t worry myself about that.", "I bet Cylons wouldn\'t have this problem.", "Sorry :(", "Surprise! Haha. Well, this is awkward.", "Would you like a cupcake?", "Hi. I\'m Minecraft, and I\'m a crashaholic.", "Ooh. Shiny.", "This doesn\'t make any sense!", "Why is it breaking :(", "Don\'t do that.", "Ouch. That hurt :(", "You\'re mean.", "This is a token for 1 free hug. Redeem at your nearest Mojangsta: [~~HUG~~]", "There are four lights!", "But it works on my machine."};
366
367
        try
368
        {
369
            return astring[(int)(System.nanoTime() % (long)astring.length)];
370
        }
371
        catch (Throwable var2)
372
        {
373
            return "Witty comment unavailable :(";
374
        }
375
    }
376
377
    /**
378
     * Creates a crash report for the exception
379
     */
380
    public static CrashReport makeCrashReport(Throwable causeIn, String descriptionIn)
381
    {
382
        CrashReport crashreport;
383
384
        if (causeIn instanceof ReportedException)
385
        {
386
            crashreport = ((ReportedException)causeIn).getCrashReport();
387
        }
388
        else
389
        {
390
            crashreport = new CrashReport(descriptionIn, causeIn);
391
        }
392
393
        return crashreport;
394
    }
395
}
396

Deobfuscated MCP 1.8.9 source of CrashReport from Marcelektro/MavenMCP-1.8.9 (net/minecraft/crash/CrashReport.java), stored verbatim.