@@ -1038,7 +1038,7 @@ Engine* Profiler::activeEngine() {
10381038}
10391039
10401040Error Profiler::checkJvmCapabilities () {
1041- if (VM::loaded ()) {
1041+ if (VMCapabilities::available ()) {
10421042 if (!VMStructs::hasJavaThreadId ()) {
10431043 return Error (" Could not find Thread ID field. Unsupported JVM?" );
10441044 }
@@ -1068,9 +1068,10 @@ Error Profiler::start(Arguments& args, bool reset) {
10681068 return Error (" Profiler already started" );
10691069 }
10701070
1071+ VMCapabilities vm_capabilities;
10711072 // If profiler is started from a native app, try to detect a running JVM and attach to it
10721073 if (!VM::loaded ()) {
1073- VM:: tryAttach ();
1074+ vm_capabilities. tryAttach ();
10741075 }
10751076
10761077 Error error = checkJvmCapabilities ();
@@ -1084,6 +1085,8 @@ Error Profiler::start(Arguments& args, bool reset) {
10841085 return Error (" No profiling events specified" );
10851086 } else if ((_event_mask & (_event_mask - 1 )) && args._output != OUTPUT_JFR) {
10861087 return Error (" Only JFR output supports multiple events" );
1088+ } else if (VM::loaded () && !VMCapabilities::available () && (_event_mask & (EM_ALLOC | EM_LOCK))) {
1089+ return Error (" Could not attach to the JVM" );
10871090 } else if (!VM::loaded () && (_event_mask & (EM_ALLOC | EM_LOCK))) {
10881091 return Error (" Profiling event is not supported with non-Java processes" );
10891092 }
@@ -1166,6 +1169,8 @@ Error Profiler::start(Arguments& args, bool reset) {
11661169 return Error (" target-cpu is only supported with perf_events" );
11671170 } else if (_engine != &perf_events && args._record_cpu ) {
11681171 return Error (" record-cpu is only supported with perf_events" );
1172+ } else if (_engine == &instrument && VM::loaded () && !vm_capabilities.available ()) {
1173+ return Error (" Could not attach to the JVM" );
11691174 }
11701175
11711176 _cstack = args._cstack ;
@@ -1242,6 +1247,7 @@ Error Profiler::start(Arguments& args, bool reset) {
12421247 startTimer ();
12431248 }
12441249
1250+ _started_with_vm = vm_capabilities.available ();
12451251 return Error::OK;
12461252
12471253error5:
@@ -1274,6 +1280,11 @@ Error Profiler::stop(bool restart) {
12741280 return Error (" Profiler is not active" );
12751281 }
12761282
1283+ VMCapabilities vm_capabilities;
1284+ if (_started_with_vm && !vm_capabilities.available ()) {
1285+ return Error (" Profiler started with VM, unable to attach to VM, couldn't stop" );
1286+ }
1287+
12771288 uninstallTraps ();
12781289
12791290 if (_event_mask & EM_WALL) wall_clock.stop ();
@@ -1313,6 +1324,7 @@ Error Profiler::check(Arguments& args) {
13131324 return Error (" Profiler already started" );
13141325 }
13151326
1327+ VMCapabilities vm_capabilities;
13161328 Error error = checkJvmCapabilities ();
13171329
13181330 if (!error && args._event != NULL ) {
@@ -1369,6 +1381,8 @@ Error Profiler::dump(Writer& out, Arguments& args) {
13691381 return Error (" Profiler has not started" );
13701382 }
13711383
1384+ VMCapabilities vm_capabilities;
1385+
13721386 if (_state == RUNNING) {
13731387 updateJavaThreadNames ();
13741388 updateNativeThreadNames ();
@@ -1803,7 +1817,7 @@ u64 Profiler::addTimeout(u64 start_micros, int timeout) {
18031817}
18041818
18051819void Profiler::startTimer () {
1806- if (VM::loaded ()) {
1820+ if (VMCapabilities::available ()) {
18071821 JNIEnv* jni = VM::jni ();
18081822 jclass Thread = jni->FindClass (" java/lang/Thread" );
18091823 jmethodID init = jni->GetMethodID (Thread, " <init>" , " (Ljava/lang/String;)V" );
0 commit comments