| 338 | } |
| 339 | |
| 340 | int Profiler::getJavaTraceAsync(void* ucontext, ASGCT_CallFrame* frames, int max_depth) { |
| 341 | // Workaround for JDK-8132510: it's not safe to call GetEnv() inside a signal handler |
| 342 | // since JDK 9, so we do it only for threads already registered in ThreadLocalStorage |
| 343 | VMThread* vm_thread = VMThread::current(); |
| 344 | if (vm_thread == NULL) { |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | JNIEnv* jni = vm_thread->jni(); |
| 349 | if (_features.jnienv) { |
| 350 | // jnienv feature is only used in tests to validate JNIEnv discovery through VMStructs. |
| 351 | // Normally, we avoid calling VM::jni() inside a signal handler as it may deadlock. |
| 352 | assert(jni == VM::jni()); |
| 353 | } |
| 354 | if (jni == NULL) { |
| 355 | // Not a Java thread |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | JitWriteProtection jit(false); |
| 360 | ASGCT_CallTrace trace = {jni, 0, frames}; |
| 361 | VM::_asyncGetCallTrace(&trace, max_depth, ucontext); |
| 362 | |
| 363 | if (trace.num_frames > 0) { |
| 364 | return trace.num_frames; |
| 365 | } |
| 366 | |
| 367 | const char* err_string = asgctError(trace.num_frames); |
| 368 | if (err_string == NULL) { |
| 369 | // No Java stack, because thread is not in Java context |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | atomicInc(_failures[-trace.num_frames]); |
| 374 | return makeFrame(frames, BCI_ERROR, err_string); |
| 375 | } |
| 376 | |
| 377 | int Profiler::getJavaTraceJvmti(jvmtiFrameInfo* jvmti_frames, ASGCT_CallFrame* frames, int start_depth, int max_depth) { |
| 378 | int num_frames = 0; |