3030import java .io .IOException ;
3131import java .io .InputStream ;
3232import java .io .OutputStream ;
33+ import java .lang .management .ManagementFactory ;
3334import java .nio .file .Files ;
3435import java .nio .file .NoSuchFileException ;
3536import java .nio .file .Paths ;
4142import java .util .Set ;
4243
4344import org .graalvm .launcher .AbstractLanguageLauncher ;
45+ import org .graalvm .nativeimage .ProcessProperties ;
4446import org .graalvm .options .OptionCategory ;
4547import org .graalvm .polyglot .Context ;
4648import org .graalvm .polyglot .Context .Builder ;
@@ -78,6 +80,8 @@ public static void main(String[] args) {
7880 @ Override
7981 protected List <String > preprocessArguments (List <String > arguments , Map <String , String > polyglotOptions ) {
8082 ArrayList <String > unrecognized = new ArrayList <>();
83+ List <String > inputArgs = new ArrayList <>(arguments );
84+ List <String > subprocessArgs = new ArrayList <>();
8185 programArgs = new ArrayList <>();
8286 for (int i = 0 ; i < arguments .size (); i ++) {
8387 String arg = arguments .get (i );
@@ -143,6 +147,21 @@ protected List<String> preprocessArguments(List<String> arguments, Map<String, S
143147 runLD = true ;
144148 programArgs .addAll (arguments .subList (i + 1 , arguments .size ()));
145149 return unrecognized ;
150+ case "-debug-perf" :
151+ subprocessArgs .add ("Dgraal.TraceTruffleCompilation=true" );
152+ subprocessArgs .add ("Dgraal.TraceTrufflePerformanceWarnings=true" );
153+ subprocessArgs .add ("Dgraal.TruffleCompilationExceptionsArePrinted=true" );
154+ inputArgs .remove ("-debug-perf" );
155+ break ;
156+ case "-dump" :
157+ subprocessArgs .add ("Dgraal.Dump=" );
158+ inputArgs .remove ("-dump" );
159+ break ;
160+ case "-compile-truffle-immediately" :
161+ subprocessArgs .add ("Dgraal.TruffleCompileImmediately=true" );
162+ subprocessArgs .add ("Dgraal.TruffleCompilationExceptionsAreThrown=true" );
163+ inputArgs .remove ("-compile-truffle-immediately" );
164+ break ;
146165 default :
147166 if (!arg .startsWith ("-" )) {
148167 inputFile = arg ;
@@ -167,6 +186,10 @@ protected List<String> preprocessArguments(List<String> arguments, Map<String, S
167186 programArgs .add ("" );
168187 }
169188
189+ if (!subprocessArgs .isEmpty ()) {
190+ subExec (inputArgs , subprocessArgs );
191+ }
192+
170193 return unrecognized ;
171194 }
172195
@@ -486,7 +509,7 @@ protected void printHelp(OptionCategory maxCategory) {
486509 // " a defense against denial-of-service attacks\n" +
487510 // "-Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew\n"
488511 // +
489- "-q : don't print version and copyright messages on interactive startup" +
512+ "-q : don't print version and copyright messages on interactive startup\n " +
490513 "-s : don't add user site directory to sys.path; also PYTHONNOUSERSITE\n " +
491514 "-S : don't imply 'import site' on initialization\n " +
492515 // "-t : issue warnings about inconsistent tab usage (-tt: issue errors)\n"
@@ -496,7 +519,7 @@ protected void printHelp(OptionCategory maxCategory) {
496519 "-v : verbose (trace import statements); also PYTHONVERBOSE=x\n " +
497520 " can be supplied multiple times to increase verbosity\n " +
498521 "-V : print the Python version number and exit (also --version)\n " +
499- " when given twice, print more information about the build" +
522+ " when given twice, print more information about the build\n " +
500523 // "-W arg : warning control; arg is
501524 // action:message:category:module:lineno\n" +
502525 // " also PYTHONWARNINGS=arg\n" +
@@ -509,11 +532,14 @@ protected void printHelp(OptionCategory maxCategory) {
509532 "arg ...: arguments passed to program in sys.argv[1:]\n " +
510533 "\n " +
511534 "Arguments specific to GraalPython.\n " +
512- "--show-version : print the Python version number and continue.\n " +
513- "-CC : run the C compiler used for generating GraalPython C extensions.\n " +
514- " All following arguments are passed to the compiler.\n " +
515- "-LD : run the linker used for generating GraalPython C extensions.\n " +
516- " All following arguments are passed to the linker.\n " +
535+ "--show-version : print the Python version number and continue.\n " +
536+ "-CC : run the C compiler used for generating GraalPython C extensions.\n " +
537+ " All following arguments are passed to the compiler.\n " +
538+ "-LD : run the linker used for generating GraalPython C extensions.\n " +
539+ " All following arguments are passed to the linker.\n " +
540+ "-debug-perf : Enable tracing of Truffle compilations and its warnings\n " +
541+ "-dump : Enable dumping of compilation graphs to IGV\n " +
542+ "-compile-truffle-immediately : Start compiling on first invocation and throw compilation exceptions\n " +
517543 "\n " +
518544 "Other environment variables:\n " +
519545 "PYTHONSTARTUP: file executed on interactive startup (no default)\n " +
@@ -678,6 +704,51 @@ private void setupREPL(Context context, ConsoleHandler consoleHandler) {
678704 }
679705 }
680706
707+ /**
708+ * Some system properties have already been read at this point, so to change them, we just
709+ * re-execute the process with the additional options.
710+ */
711+ private static void subExec (List <String > args , List <String > subProcessDefs ) {
712+ List <String > cmd = new ArrayList <>();
713+ if (isAOT ()) {
714+ cmd .add (ProcessProperties .getExecutableName ());
715+ for (String subProcArg : subProcessDefs ) {
716+ assert subProcArg .startsWith ("D" );
717+ cmd .add ("--native." + subProcArg );
718+ }
719+ } else {
720+ cmd .add (System .getProperty ("java.home" ) + File .separator + "bin" + File .separator + "java" );
721+ switch (System .getProperty ("java.vm.name" )) {
722+ case "Java HotSpot(TM) 64-Bit Server VM" :
723+ cmd .add ("-server" );
724+ cmd .add ("-d64" );
725+ break ;
726+ case "Java HotSpot(TM) 64-Bit Client VM" :
727+ cmd .add ("-client" );
728+ cmd .add ("-d64" );
729+ break ;
730+ default :
731+ break ;
732+ }
733+ cmd .addAll (ManagementFactory .getRuntimeMXBean ().getInputArguments ());
734+ cmd .add ("-cp" );
735+ cmd .add (ManagementFactory .getRuntimeMXBean ().getClassPath ());
736+ for (String subProcArg : subProcessDefs ) {
737+ assert subProcArg .startsWith ("D" );
738+ cmd .add ("-" + subProcArg );
739+ }
740+ cmd .add (GraalPythonMain .class .getName ());
741+ }
742+
743+ cmd .addAll (args );
744+ try {
745+ System .exit (new ProcessBuilder (cmd .toArray (new String [0 ])).inheritIO ().start ().waitFor ());
746+ } catch (IOException | InterruptedException e ) {
747+ System .err .println (e .getMessage ());
748+ System .exit (-1 );
749+ }
750+ }
751+
681752 private static final class ExitException extends RuntimeException {
682753 private static final long serialVersionUID = 1L ;
683754 private final int code ;
0 commit comments