8216529: in case of a crash, javac should print out the parameters passed to it
authorvromero
Wed, 16 Jan 2019 07:01:04 -0500
changeset 53347 6bd052801d02
parent 53346 61866ba87b31
child 53348 331ba84b1e36
8216529: in case of a crash, javac should print out the parameters passed to it Reviewed-by: jjg, cushon
src/jdk.compiler/share/classes/com/sun/tools/javac/main/Main.java
src/jdk.compiler/share/classes/com/sun/tools/javac/resources/javac.properties
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Main.java	Tue Jan 15 12:02:40 2019 +0100
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Main.java	Wed Jan 16 07:01:04 2019 -0500
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -29,12 +29,18 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.PrintWriter;
+import java.io.Writer;
 import java.net.URL;
+import java.nio.file.Files;
 import java.nio.file.NoSuchFileException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.security.CodeSource;
 import java.security.DigestInputStream;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
 import java.util.Set;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -307,6 +313,7 @@
             comp.closeables = comp.closeables.prepend(log.getWriter(WriterKind.NOTICE));
         }
 
+        boolean printArgsToFile = options.isSet("printArgsToFile");
         try {
             comp.compile(args.getFileObjects(), args.getClassNames(), null, List.nil());
 
@@ -338,6 +345,7 @@
             if (twoClassLoadersInUse(iae)) {
                 bugMessage(iae);
             }
+            printArgsToFile = true;
             return Result.ABNORMAL;
         } catch (Throwable ex) {
             // Nasty.  If we've already reported an error, compensate
@@ -345,8 +353,12 @@
             // exceptions.
             if (comp == null || comp.errorCount() == 0 || options.isSet("dev"))
                 bugMessage(ex);
+            printArgsToFile = true;
             return Result.ABNORMAL;
         } finally {
+            if (printArgsToFile) {
+                printArgumentsToFile(argv);
+            }
             if (comp != null) {
                 try {
                     comp.close();
@@ -357,6 +369,29 @@
         }
     }
 
+    void printArgumentsToFile(String... params) {
+        Path out = Paths.get(String.format("javac.%s.args",
+                new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime())));
+        String strOut = "";
+        try {
+            try (Writer w = Files.newBufferedWriter(out)) {
+                for (String param : params) {
+                    param = param.replaceAll("\\\\", "\\\\\\\\");
+                    if (param.matches(".*\\s+.*")) {
+                        param = "\"" + param + "\"";
+                    }
+                    strOut += param + '\n';
+                }
+                w.write(strOut);
+            }
+            log.printLines(PrefixKind.JAVAC, "msg.parameters.output", out.toAbsolutePath());
+        } catch (IOException ioe) {
+            log.printLines(PrefixKind.JAVAC, "msg.parameters.output.error", out.toAbsolutePath());
+            System.err.println(strOut);
+            System.err.println();
+        }
+    }
+
     private boolean twoClassLoadersInUse(IllegalAccessError iae) {
         String msg = iae.getMessage();
         Pattern pattern = Pattern.compile("(?i)(?<=tried to access class )([a-z_$][a-z\\d_$]*\\.)*[a-z_$][a-z\\d_$]*");
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/javac.properties	Tue Jan 15 12:02:40 2019 +0100
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/javac.properties	Wed Jan 16 07:01:04 2019 -0500
@@ -352,7 +352,7 @@
 An exception has occurred in the compiler ({0}). \
 Please file a bug against the Java compiler via the Java bug reporting page (http://bugreport.java.com) \
 after checking the Bug Database (http://bugs.java.com) for duplicates. \
-Include your program and the following diagnostic in your report. Thank you.
+Include your program, the following diagnostic, and the parameters passed to the Java compiler in your report. Thank you.
 
 javac.msg.io=\
 \n\nAn input/output error occurred.\n\
@@ -372,3 +372,9 @@
 
 javac.version={0} {1}
 javac.fullVersion={0} full version "{1}"
+
+javac.msg.parameters.output=\
+printing javac parameters to: {0}
+
+javac.msg.parameters.output.error=\
+error while trying to print javac parameters to: {0}, parameters will follow: