src/jdk.packager/share/native/library/common/JavaVirtualMachine.cpp
branchJDK-8200758-branch
changeset 56982 e094d5483bd6
parent 56854 aedce3eaaf17
child 56983 01decb67d4f0
equal deleted inserted replaced
56963:eaca4369b068 56982:e094d5483bd6
    26 #include "JavaVirtualMachine.h"
    26 #include "JavaVirtualMachine.h"
    27 #include "Platform.h"
    27 #include "Platform.h"
    28 #include "PlatformString.h"
    28 #include "PlatformString.h"
    29 #include "FilePath.h"
    29 #include "FilePath.h"
    30 #include "Package.h"
    30 #include "Package.h"
    31 #include "Java.h"
    31 #include "JavaTypes.h"
    32 #include "Helpers.h"
    32 #include "Helpers.h"
    33 #include "Messages.h"
    33 #include "Messages.h"
    34 #include "Macros.h"
    34 #include "Macros.h"
    35 #include "PlatformThread.h"
    35 #include "PlatformThread.h"
    36 
    36 
    73     }
    73     }
    74 
    74 
    75     if (FCreateProc == NULL) {
    75     if (FCreateProc == NULL) {
    76         Platform& platform = Platform::GetInstance();
    76         Platform& platform = Platform::GetInstance();
    77         Messages& messages = Messages::GetInstance();
    77         Messages& messages = Messages::GetInstance();
    78         platform.ShowMessage(messages.GetMessage(FAILED_LOCATING_JVM_ENTRY_POINT));
    78         platform.ShowMessage(
       
    79                 messages.GetMessage(FAILED_LOCATING_JVM_ENTRY_POINT));
    79         return false;
    80         return false;
    80     }
    81     }
    81 
    82 
    82     return FCreateProc((int)argc, argv,
    83     return FCreateProc((int)argc, argv,
    83             0, NULL,
    84             0, NULL,
    90             false,
    91             false,
    91             false,
    92             false,
    92             0) == 0;
    93             0) == 0;
    93 }
    94 }
    94 
    95 
    95 //--------------------------------------------------------------------------------------------------
    96 //----------------------------------------------------------------------------
    96 
    97 
    97 JavaOptions::JavaOptions(): FOptions(NULL) {
    98 JavaOptions::JavaOptions(): FOptions(NULL) {
    98 }
    99 }
    99 
   100 
   100 JavaOptions::~JavaOptions() {
   101 JavaOptions::~JavaOptions() {
   171 
   172 
   172 size_t JavaOptions::GetCount() {
   173 size_t JavaOptions::GetCount() {
   173     return FItems.size();
   174     return FItems.size();
   174 }
   175 }
   175 
   176 
   176 // jvmuserargs can have a trailing equals in the key. This needs to be removed to use
   177 // jvmuserargs can have a trailing equals in the key. This needs to be
   177 // other parts of the launcher.
   178 // removed to use other parts of the launcher.
   178 OrderedMap<TString, TString> RemoveTrailingEquals(OrderedMap<TString, TString> Map) {
   179 OrderedMap<TString, TString> RemoveTrailingEquals(
       
   180         OrderedMap<TString, TString> Map) {
   179     OrderedMap<TString, TString> result;
   181     OrderedMap<TString, TString> result;
   180 
   182 
   181     std::vector<TString> keys = Map.GetKeys();
   183     std::vector<TString> keys = Map.GetKeys();
   182 
   184 
   183     for (size_t index = 0; index < keys.size(); index++) {
   185     for (size_t index = 0; index < keys.size(); index++) {
   184         TString name = keys[index];
   186         TString name = keys[index];
   185         TString value;
   187         TString value;
   186 
   188 
   187         if (Map.GetValue(name, value) == true) {
   189         if (Map.GetValue(name, value) == true) {
   188             // If the last character of the key is an equals, then remove it. If there is no
   190             // If the last character of the key is an equals, then remove it.
   189             // equals then combine the two as a key.
   191             // If there is no equals then combine the two as a key.
   190             TString::iterator i = name.end();
   192             TString::iterator i = name.end();
   191             i--;
   193             i--;
   192 
   194 
   193             if (*i == '=') {
   195             if (*i == '=') {
   194                 name = name.substr(0, name.size() - 1);
   196                 name = name.substr(0, name.size() - 1);
   210     }
   212     }
   211 
   213 
   212     return result;
   214     return result;
   213 }
   215 }
   214 
   216 
   215 //--------------------------------------------------------------------------------------------------
   217 //----------------------------------------------------------------------------
   216 
   218 
   217 JavaVirtualMachine::JavaVirtualMachine() {
   219 JavaVirtualMachine::JavaVirtualMachine() {
   218 }
   220 }
   219 
   221 
   220 JavaVirtualMachine::~JavaVirtualMachine(void) {
   222 JavaVirtualMachine::~JavaVirtualMachine(void) {
   230 
   232 
   231     if (modulepath.empty() == false) {
   233     if (modulepath.empty() == false) {
   232         options.AppendValue(_T("-Djava.module.path"), modulepath);
   234         options.AppendValue(_T("-Djava.module.path"), modulepath);
   233     }
   235     }
   234 
   236 
   235     options.AppendValue(_T("-Djava.library.path"), package.GetPackageAppDirectory() + FilePath::PathSeparator() + package.GetPackageLauncherDirectory());
   237     options.AppendValue(_T("-Djava.library.path"),
   236     options.AppendValue(_T("-Djava.launcher.path"), package.GetPackageLauncherDirectory());
   238             package.GetPackageAppDirectory() + FilePath::PathSeparator()
       
   239             + package.GetPackageLauncherDirectory());
       
   240     options.AppendValue(
       
   241             _T("-Djava.launcher.path"), package.GetPackageLauncherDirectory());
   237     options.AppendValue(_T("-Dapp.preferences.id"), package.GetAppID());
   242     options.AppendValue(_T("-Dapp.preferences.id"), package.GetAppID());
   238     options.AppendValues(package.GetJVMArgs());
   243     options.AppendValues(package.GetJVMArgs());
   239     options.AppendValues(RemoveTrailingEquals(package.GetJVMUserArgs()));
   244     options.AppendValues(RemoveTrailingEquals(package.GetJVMUserArgs()));
   240 
   245 
   241 #ifdef DEBUG
   246 #ifdef DEBUG
   242     if (package.Debugging() == dsJava) {
   247     if (package.Debugging() == dsJava) {
   243         options.AppendValue(_T("-Xdebug"), _T(""));
   248         options.AppendValue(_T("-Xdebug"), _T(""));
   244         options.AppendValue(_T("-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=localhost:5005"), _T(""));
   249         options.AppendValue(
       
   250                 _T("-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=localhost:5005"),
       
   251                 _T(""));
   245         platform.ShowMessage(_T("localhost:5005"));
   252         platform.ShowMessage(_T("localhost:5005"));
   246     }
   253     }
   247 #endif //DEBUG
   254 #endif // DEBUG
   248 
   255 
   249     TString maxHeapSizeOption;
   256     TString maxHeapSizeOption;
   250     TString minHeapSizeOption;
   257     TString minHeapSizeOption;
   251 
   258 
   252 
   259 
   253     if (package.GetMemoryState() == PackageBootFields::msAuto) {
   260     if (package.GetMemoryState() == PackageBootFields::msAuto) {
   254         TPlatformNumber memorySize = package.GetMemorySize();
   261         TPlatformNumber memorySize = package.GetMemorySize();
   255         TString memory = PlatformString((size_t)memorySize).toString() + _T("m");
   262         TString memory =
       
   263                 PlatformString((size_t)memorySize).toString() + _T("m");
   256         maxHeapSizeOption = TString(_T("-Xmx")) + memory;
   264         maxHeapSizeOption = TString(_T("-Xmx")) + memory;
   257         options.AppendValue(maxHeapSizeOption, _T(""));
   265         options.AppendValue(maxHeapSizeOption, _T(""));
   258 
   266 
   259         if (memorySize > 256)
   267         if (memorySize > 256)
   260             minHeapSizeOption = _T("-Xms256m");
   268             minHeapSizeOption = _T("-Xms256m");
   275 
   283 
   276     configureLibrary();
   284     configureLibrary();
   277 
   285 
   278     // Initialize the arguments to JLI_Launch()
   286     // Initialize the arguments to JLI_Launch()
   279     //
   287     //
   280     // On Mac OS X JLI_Launch spawns a new thread that actually starts the JVM. This
   288     // On Mac OS X JLI_Launch spawns a new thread that actually starts the JVM.
   281     // new thread simply re-runs main(argc, argv). Therefore we do not want
   289     // This new thread simply re-runs main(argc, argv). Therefore we do not
   282     // to add new args if we are still in the original main thread so we
   290     // want to add new args if we are still in the original main thread so we
   283     // will treat them as command line args provided by the user ...
   291     // will treat them as command line args provided by the user ...
   284     // Only propagate original set of args first time.
   292     // Only propagate original set of args first time.
   285 
   293 
   286     options.AppendValue(_T("-classpath"));
   294     options.AppendValue(_T("-classpath"));
   287     options.AppendValue(classpath);
   295     options.AppendValue(classpath);
   288 
   296 
   289     std::list<TString> vmargs;
   297     std::list<TString> vmargs;
   290     vmargs.push_back(package.GetCommandName());
   298     vmargs.push_back(package.GetCommandName());
   291 
   299 
   292     if (package.HasSplashScreen() == true) {
   300     if (package.HasSplashScreen() == true) {
   293         options.AppendValue(TString(_T("-splash:")) + package.GetSplashScreenFileName(), _T(""));
   301         options.AppendValue(TString(_T("-splash:"))
       
   302                 + package.GetSplashScreenFileName(), _T(""));
   294     }
   303     }
   295 
   304 
   296     if (mainModule.empty() == true) {
   305     if (mainModule.empty() == true) {
   297         options.AppendValue(Helpers::ConvertJavaPathToId(mainClassName), _T(""));
   306         options.AppendValue(Helpers::ConvertJavaPathToId(mainClassName),
   298     }
   307                 _T(""));
   299     else {
   308     } else {
   300         options.AppendValue(_T("-m"));
   309         options.AppendValue(_T("-m"));
   301         options.AppendValue(mainModule);
   310         options.AppendValue(mainModule);
   302     }
   311     }
   303 
   312 
   304     return launchVM(options, vmargs, false);
   313     return launchVM(options, vmargs, false);
   309 
   318 
   310     std::list<TString> vmargs;
   319     std::list<TString> vmargs;
   311     vmargs.push_back(package.GetCommandName());
   320     vmargs.push_back(package.GetCommandName());
   312 
   321 
   313     JavaOptions options;
   322     JavaOptions options;
   314     options.AppendValue(_T("-Djava.library.path"), package.GetPackageAppDirectory()
   323     options.AppendValue(_T("-Djava.library.path"),
   315                       + FilePath::PathSeparator() + package.GetPackageLauncherDirectory());
   324             package.GetPackageAppDirectory() + FilePath::PathSeparator()
   316     options.AppendValue(_T("-Djava.launcher.path"), package.GetPackageLauncherDirectory());
   325             + package.GetPackageLauncherDirectory());
   317     // launch SingleInstanceNewActivation.main() to pass arguments to another instance
   326     options.AppendValue(_T("-Djava.launcher.path"),
       
   327             package.GetPackageLauncherDirectory());
       
   328     // launch SingleInstanceNewActivation.main() to pass arguments to
       
   329     // another instance
   318     options.AppendValue(_T("-m"));
   330     options.AppendValue(_T("-m"));
   319     options.AppendValue(_T("jdk.packager.services/jdk.packager.services.singleton.SingleInstanceNewActivation"));
   331     options.AppendValue(
       
   332             _T("jdk.packager.services/jdk.packager.services.singleton.SingleInstanceNewActivation"));
   320 
   333 
   321     configureLibrary();
   334     configureLibrary();
   322 
   335 
   323     return launchVM(options, vmargs, true);
   336     return launchVM(options, vmargs, true);
   324 }
   337 }
   325 
   338 
   326 void JavaVirtualMachine::configureLibrary() {
   339 void JavaVirtualMachine::configureLibrary() {
   327     Platform& platform = Platform::GetInstance();
   340     Platform& platform = Platform::GetInstance();
   328     Package& package = Package::GetInstance();
   341     Package& package = Package::GetInstance();
   329     // TODO: Clean this up. Because of bug JDK-8131321 the opening of the PE file
   342     // TODO: Clean this up. Because of bug JDK-8131321 the opening of the
   330     // fails in WindowsPlatform.cpp on the check to
   343     // PE file ails in WindowsPlatform.cpp on the check to
   331     // if (pNTHeader->Signature == IMAGE_NT_SIGNATURE)
   344     // if (pNTHeader->Signature == IMAGE_NT_SIGNATURE)
   332     TString libName = package.GetJVMLibraryFileName();
   345     TString libName = package.GetJVMLibraryFileName();
   333 #ifdef _WIN64
   346 #ifdef _WIN64
   334     if (FilePath::FileExists(_T("msvcr100.dll")) == true) {
   347     if (FilePath::FileExists(_T("msvcr100.dll")) == true) {
   335         javaLibrary.AddDependency(_T("msvcr100.dll"));
   348         javaLibrary.AddDependency(_T("msvcr100.dll"));
   336     }
   349     }
   337 #else
   350 #else
   338     javaLibrary.AddDependencies(
   351     javaLibrary.AddDependencies(
   339         platform.FilterOutRuntimeDependenciesForPlatform(platform.GetLibraryImports(libName)));
   352             platform.FilterOutRuntimeDependenciesForPlatform(
       
   353             platform.GetLibraryImports(libName)));
   340 #endif
   354 #endif
   341     javaLibrary.Load(libName);
   355     javaLibrary.Load(libName);
   342 }
   356 }
   343 
   357 
   344 bool JavaVirtualMachine::launchVM(JavaOptions& options, std::list<TString>& vmargs, bool addSiProcessId) {
   358 bool JavaVirtualMachine::launchVM(JavaOptions& options,
       
   359         std::list<TString>& vmargs, bool addSiProcessId) {
   345     Platform& platform = Platform::GetInstance();
   360     Platform& platform = Platform::GetInstance();
   346     Package& package = Package::GetInstance();
   361     Package& package = Package::GetInstance();
   347 
   362 
   348 #ifdef MAC
   363 #ifdef MAC
   349     // Mac adds a ProcessSerialNumber to args when launched from .app
   364     // Mac adds a ProcessSerialNumber to args when launched from .app
   350     // filter out the psn since they it's not expected in the app
   365     // filter out the psn since they it's not expected in the app
   351     if (platform.IsMainThread() == false) {
   366     if (platform.IsMainThread() == false) {
   352         std::list<TString> loptions = options.ToList();
   367         std::list<TString> loptions = options.ToList();
   353         vmargs.splice(vmargs.end(), loptions, loptions.begin(), loptions.end());
   368         vmargs.splice(vmargs.end(), loptions,
       
   369                 loptions.begin(), loptions.end());
   354     }
   370     }
   355 #else
   371 #else
   356     std::list<TString> loptions = options.ToList();
   372     std::list<TString> loptions = options.ToList();
   357     vmargs.splice(vmargs.end(), loptions, loptions.begin(), loptions.end());
   373     vmargs.splice(vmargs.end(), loptions, loptions.begin(), loptions.end());
   358 #endif
   374 #endif
   376         iterator != vmargs.end(); iterator++) {
   392         iterator != vmargs.end(); iterator++) {
   377         TString item = *iterator;
   393         TString item = *iterator;
   378         std::string arg = PlatformString(item).toStdString();
   394         std::string arg = PlatformString(item).toStdString();
   379 #ifdef DEBUG
   395 #ifdef DEBUG
   380         printf("%i %s\n", index, arg.c_str());
   396         printf("%i %s\n", index, arg.c_str());
   381 #endif //DEBUG
   397 #endif // DEBUG
   382         argv[index] = PlatformString::duplicate(arg.c_str());
   398         argv[index] = PlatformString::duplicate(arg.c_str());
   383         index++;
   399         index++;
   384     }
   400     }
   385 
   401 
   386     argv[argc] = NULL;
   402     argv[argc] = NULL;
   387 
   403 
   388 // On Mac we can only free the boot fields if the calling thread is not the main thread.
   404 // On Mac we can only free the boot fields if the calling thread is
       
   405 // not the main thread.
   389 #ifdef MAC
   406 #ifdef MAC
   390     if (platform.IsMainThread() == false) {
   407     if (platform.IsMainThread() == false) {
   391         package.FreeBootFields();
   408         package.FreeBootFields();
   392     }
   409     }
   393 #else
   410 #else
   394     package.FreeBootFields();
   411     package.FreeBootFields();
   395 #endif //MAC
   412 #endif // MAC
   396 
   413 
   397     if (javaLibrary.JavaVMCreate(argc, argv.GetData()) == true) {
   414     if (javaLibrary.JavaVMCreate(argc, argv.GetData()) == true) {
   398         return true;
   415         return true;
   399     }
   416     }
   400 
   417