| 1 | package net.minecraft;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import java.io.PrintWriter;
|
| 6 | import java.io.StringWriter;
|
| 7 | import java.io.Writer;
|
| 8 | import java.nio.charset.StandardCharsets;
|
| 9 | import java.nio.file.Files;
|
| 10 | import java.nio.file.Path;
|
| 11 | import java.time.ZonedDateTime;
|
| 12 | import java.time.format.DateTimeFormatter;
|
| 13 | import java.util.List;
|
| 14 | import java.util.Locale;
|
| 15 | import java.util.concurrent.CompletionException;
|
| 16 | import net.minecraft.util.FileUtil;
|
| 17 | import net.minecraft.util.MemoryReserve;
|
| 18 | import org.apache.commons.io.IOUtils;
|
| 19 | import org.apache.commons.lang3.ArrayUtils;
|
| 20 | import org.jspecify.annotations.Nullable;
|
| 21 | import org.slf4j.Logger;
|
| 22 |
|
| 23 | public class CrashReport {
|
| 24 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 25 | private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.ROOT);
|
| 26 | private final String title;
|
| 27 | private final Throwable exception;
|
| 28 | private final List<CrashReportCategory> details = Lists.newArrayList();
|
| 29 | private @Nullable Path saveFile;
|
| 30 | private boolean trackingStackTrace = true;
|
| 31 | private StackTraceElement[] uncategorizedStackTrace = new StackTraceElement[0];
|
| 32 | private final SystemReport systemReport = new SystemReport();
|
| 33 |
|
| 34 | public CrashReport(final String title, final Throwable t) {
|
| 35 | this.title = title;
|
| 36 | this.exception = t;
|
| 37 | }
|
| 38 |
|
| 39 | public String getTitle() {
|
| 40 | return this.title;
|
| 41 | }
|
| 42 |
|
| 43 | public Throwable getException() {
|
| 44 | return this.exception;
|
| 45 | }
|
| 46 |
|
| 47 | public String getDetails() {
|
| 48 | StringBuilder builder = new StringBuilder();
|
| 49 | this.getDetails(builder);
|
| 50 | return builder.toString();
|
| 51 | }
|
| 52 |
|
| 53 | public void getDetails(final StringBuilder builder) {
|
| 54 | if (this.uncategorizedStackTrace.length <= 0 && !this.details.isEmpty()) {
|
| 55 | this.uncategorizedStackTrace = ArrayUtils.subarray(this.details.get(0).getStacktrace(), 0, 1);
|
| 56 | }
|
| 57 |
|
| 58 | if (this.uncategorizedStackTrace.length > 0) {
|
| 59 | builder.append("-- Head --\n");
|
| 60 | builder.append("Thread: ").append(Thread.currentThread().getName()).append("\n");
|
| 61 | builder.append("Stacktrace:\n");
|
| 62 |
|
| 63 | for (StackTraceElement element : this.uncategorizedStackTrace) {
|
| 64 | builder.append("\t").append("at ").append(element);
|
| 65 | builder.append("\n");
|
| 66 | }
|
| 67 |
|
| 68 | builder.append("\n");
|
| 69 | }
|
| 70 |
|
| 71 | for (CrashReportCategory entry : this.details) {
|
| 72 | entry.getDetails(builder);
|
| 73 | builder.append("\n\n");
|
| 74 | }
|
| 75 |
|
| 76 | this.systemReport.appendToCrashReportString(builder);
|
| 77 | }
|
| 78 |
|
| 79 | public String getExceptionMessage() {
|
| 80 | StringWriter writer = null;
|
| 81 | PrintWriter printWriter = null;
|
| 82 | Throwable exception = this.exception;
|
| 83 | if (exception.getMessage() == null) {
|
| 84 | exception = replaceMessage(exception, this.title);
|
| 85 | }
|
| 86 |
|
| 87 | try {
|
| 88 | writer = new StringWriter();
|
| 89 | printWriter = new PrintWriter(writer);
|
| 90 | exception.printStackTrace(printWriter);
|
| 91 | return writer.toString();
|
| 92 | } finally {
|
| 93 | IOUtils.closeQuietly(writer);
|
| 94 | IOUtils.closeQuietly(printWriter);
|
| 95 | }
|
| 96 | }
|
| 97 |
|
| 98 | private static Throwable copyProperties(final Throwable original, final Throwable copy) {
|
| 99 | try {
|
| 100 | Throwable cause = original.getCause();
|
| 101 | if (cause != null) {
|
| 102 | copy.initCause(cause);
|
| 103 | }
|
| 104 |
|
| 105 | Throwable[] suppressed = original.getSuppressed();
|
| 106 |
|
| 107 | for (Throwable throwable : suppressed) {
|
| 108 | copy.addSuppressed(throwable);
|
| 109 | }
|
| 110 |
|
| 111 | copy.setStackTrace(original.getStackTrace());
|
| 112 | return copy;
|
| 113 | } catch (Throwable e) {
|
| 114 | return original;
|
| 115 | }
|
| 116 | }
|
| 117 |
|
| 118 | private static Throwable replaceMessage(final Throwable original, final String title) {
|
| 119 | return switch (original) {
|
| 120 | case NullPointerException var4 -> copyProperties(original, new NullPointerException(title));
|
| 121 | case StackOverflowError var5 -> copyProperties(original, new StackOverflowError(title));
|
| 122 | case OutOfMemoryError var6 -> copyProperties(original, new OutOfMemoryError(title));
|
| 123 | default -> original;
|
| 124 | };
|
| 125 | }
|
| 126 |
|
| 127 | public String getFriendlyReport(final ReportType reportType, final List<String> extraComments) {
|
| 128 | StringBuilder builder = new StringBuilder();
|
| 129 | reportType.appendHeader(builder, extraComments);
|
| 130 | builder.append("Time: ");
|
| 131 | builder.append(DATE_TIME_FORMATTER.format(ZonedDateTime.now()));
|
| 132 | builder.append("\n");
|
| 133 | builder.append("Description: ");
|
| 134 | builder.append(this.title);
|
| 135 | builder.append("\n\n");
|
| 136 | builder.append(this.getExceptionMessage());
|
| 137 | builder.append("\n\nA detailed walkthrough of the error, its code path and all known details is as follows:\n");
|
| 138 |
|
| 139 | for (int i = 0; i < 87; i++) {
|
| 140 | builder.append("-");
|
| 141 | }
|
| 142 |
|
| 143 | builder.append("\n\n");
|
| 144 | this.getDetails(builder);
|
| 145 | return builder.toString();
|
| 146 | }
|
| 147 |
|
| 148 | public String getFriendlyReport(final ReportType reportType) {
|
| 149 | return this.getFriendlyReport(reportType, List.of());
|
| 150 | }
|
| 151 |
|
| 152 | public @Nullable Path getSaveFile() {
|
| 153 | return this.saveFile;
|
| 154 | }
|
| 155 |
|
| 156 | public boolean saveToFile(final Path saveFile, final ReportType reportType, final List<String> extraComments) {
|
| 157 | if (this.saveFile != null) {
|
| 158 | return false;
|
| 159 | }
|
| 160 |
|
| 161 | try {
|
| 162 | if (saveFile.getParent() != null) {
|
| 163 | FileUtil.createDirectoriesSafe(saveFile.getParent());
|
| 164 | }
|
| 165 |
|
| 166 | try (Writer writer = Files.newBufferedWriter(saveFile, StandardCharsets.UTF_8)) {
|
| 167 | writer.write(this.getFriendlyReport(reportType, extraComments));
|
| 168 | }
|
| 169 |
|
| 170 | this.saveFile = saveFile;
|
| 171 | return true;
|
| 172 | } catch (Throwable t) {
|
| 173 | LOGGER.error("Could not save crash report to {}", saveFile, t);
|
| 174 | return false;
|
| 175 | }
|
| 176 | }
|
| 177 |
|
| 178 | public boolean saveToFile(final Path file, final ReportType reportType) {
|
| 179 | return this.saveToFile(file, reportType, List.of());
|
| 180 | }
|
| 181 |
|
| 182 | public SystemReport getSystemReport() {
|
| 183 | return this.systemReport;
|
| 184 | }
|
| 185 |
|
| 186 | public CrashReportCategory addCategory(final String name) {
|
| 187 | return this.addCategory(name, 1);
|
| 188 | }
|
| 189 |
|
| 190 | public CrashReportCategory addCategory(final String name, final int nestedOffset) {
|
| 191 | CrashReportCategory category = new CrashReportCategory(name);
|
| 192 | if (this.trackingStackTrace) {
|
| 193 | int size = category.fillInStackTrace(nestedOffset);
|
| 194 | StackTraceElement[] fullTrace = this.exception.getStackTrace();
|
| 195 | StackTraceElement source = null;
|
| 196 | StackTraceElement next = null;
|
| 197 | int traceIndex = fullTrace.length - size;
|
| 198 | if (traceIndex < 0) {
|
| 199 | LOGGER.error("Negative index in crash report handler ({}/{})", fullTrace.length, size);
|
| 200 | }
|
| 201 |
|
| 202 | if (0 <= traceIndex && traceIndex < fullTrace.length) {
|
| 203 | source = fullTrace[traceIndex];
|
| 204 | if (fullTrace.length + 1 - size < fullTrace.length) {
|
| 205 | next = fullTrace[fullTrace.length + 1 - size];
|
| 206 | }
|
| 207 | }
|
| 208 |
|
| 209 | this.trackingStackTrace = category.validateStackTrace(source, next);
|
| 210 | if (fullTrace.length >= size && 0 <= traceIndex && traceIndex < fullTrace.length) {
|
| 211 | this.uncategorizedStackTrace = new StackTraceElement[traceIndex];
|
| 212 | System.arraycopy(fullTrace, 0, this.uncategorizedStackTrace, 0, this.uncategorizedStackTrace.length);
|
| 213 | } else {
|
| 214 | this.trackingStackTrace = false;
|
| 215 | }
|
| 216 | }
|
| 217 |
|
| 218 | this.details.add(category);
|
| 219 | return category;
|
| 220 | }
|
| 221 |
|
| 222 | public static CrashReport forThrowable(Throwable t, final String title) {
|
| 223 | while (t instanceof CompletionException && t.getCause() != null) {
|
| 224 | t = t.getCause();
|
| 225 | }
|
| 226 |
|
| 227 | CrashReport report;
|
| 228 | if (t instanceof ReportedException reportedException) {
|
| 229 | report = reportedException.getReport();
|
| 230 | } else {
|
| 231 | report = new CrashReport(title, t);
|
| 232 | }
|
| 233 |
|
| 234 | return report;
|
| 235 | }
|
| 236 |
|
| 237 | public static void preload() {
|
| 238 | MemoryReserve.allocate();
|
| 239 | new CrashReport("Don't panic!", new Throwable()).getFriendlyReport(ReportType.CRASH);
|
| 240 | }
|
| 241 | }
|
| 242 |
|