jdk

Форк
0
/
jvmtiThreadState.cpp 
1033 строки · 34.8 Кб
1
/*
2
 * Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
 *
5
 * This code is free software; you can redistribute it and/or modify it
6
 * under the terms of the GNU General Public License version 2 only, as
7
 * published by the Free Software Foundation.
8
 *
9
 * This code is distributed in the hope that it will be useful, but WITHOUT
10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12
 * version 2 for more details (a copy is included in the LICENSE file that
13
 * accompanied this code).
14
 *
15
 * You should have received a copy of the GNU General Public License version
16
 * 2 along with this work; if not, write to the Free Software Foundation,
17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
 *
19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
 * or visit www.oracle.com if you need additional information or have any
21
 * questions.
22
 *
23
 */
24

25
#include "precompiled.hpp"
26
#include "classfile/javaClasses.inline.hpp"
27
#include "jvmtifiles/jvmtiEnv.hpp"
28
#include "memory/resourceArea.hpp"
29
#include "oops/oopHandle.inline.hpp"
30
#include "prims/jvmtiEnvBase.hpp"
31
#include "prims/jvmtiEventController.inline.hpp"
32
#include "prims/jvmtiImpl.hpp"
33
#include "prims/jvmtiThreadState.inline.hpp"
34
#include "runtime/handles.inline.hpp"
35
#include "runtime/interfaceSupport.inline.hpp"
36
#include "runtime/jniHandles.inline.hpp"
37
#include "runtime/safepointVerifiers.hpp"
38
#include "runtime/stackFrameStream.inline.hpp"
39
#include "runtime/vframe.hpp"
40

41
// marker for when the stack depth has been reset and is now unknown.
42
// any negative number would work but small ones might obscure an
43
// underrun error.
44
static const int UNKNOWN_STACK_DEPTH = -99;
45

46
///////////////////////////////////////////////////////////////
47
//
48
// class JvmtiThreadState
49
//
50
// Instances of JvmtiThreadState hang off of each thread.
51
// Thread local storage for JVMTI.
52
//
53

54
JvmtiThreadState *JvmtiThreadState::_head = nullptr;
55
bool JvmtiThreadState::_seen_interp_only_mode = false;
56

57
JvmtiThreadState::JvmtiThreadState(JavaThread* thread, oop thread_oop)
58
  : _thread_event_enable() {
59
  assert(JvmtiThreadState_lock->is_locked(), "sanity check");
60
  _thread               = thread;
61
  _thread_saved         = nullptr;
62
  _exception_state      = ES_CLEARED;
63
  _debuggable           = true;
64
  _hide_single_stepping = false;
65
  _pending_interp_only_mode = false;
66
  _hide_level           = 0;
67
  _pending_step_for_popframe = false;
68
  _class_being_redefined = nullptr;
69
  _class_load_kind = jvmti_class_load_kind_load;
70
  _classes_being_redefined = nullptr;
71
  _head_env_thread_state = nullptr;
72
  _dynamic_code_event_collector = nullptr;
73
  _vm_object_alloc_event_collector = nullptr;
74
  _sampled_object_alloc_event_collector = nullptr;
75
  _the_class_for_redefinition_verification = nullptr;
76
  _scratch_class_for_redefinition_verification = nullptr;
77
  _cur_stack_depth = UNKNOWN_STACK_DEPTH;
78
  _saved_interp_only_mode = 0;
79

80
  // JVMTI ForceEarlyReturn support
81
  _pending_step_for_earlyret = false;
82
  _earlyret_state = earlyret_inactive;
83
  _earlyret_tos = ilgl;
84
  _earlyret_value.j = 0L;
85
  _earlyret_oop = nullptr;
86
  _jvmti_event_queue = nullptr;
87
  _is_virtual = false;
88

89
  _thread_oop_h = OopHandle(JvmtiExport::jvmti_oop_storage(), thread_oop);
90

91
  // add all the JvmtiEnvThreadState to the new JvmtiThreadState
92
  {
93
    JvmtiEnvIterator it;
94
    for (JvmtiEnvBase* env = it.first(); env != nullptr; env = it.next(env)) {
95
      if (env->is_valid()) {
96
        add_env(env);
97
      }
98
    }
99
  }
100

101
  // link us into the list
102
  {
103
    // The thread state list manipulation code must not have safepoints.
104
    // See periodic_clean_up().
105
    debug_only(NoSafepointVerifier nosafepoint;)
106

107
    _prev = nullptr;
108
    _next = _head;
109
    if (_head != nullptr) {
110
      _head->_prev = this;
111
    }
112
    _head = this;
113
  }
114

115
  if (thread_oop != nullptr) {
116
    java_lang_Thread::set_jvmti_thread_state(thread_oop, this);
117
    _is_virtual = java_lang_VirtualThread::is_instance(thread_oop);
118
  }
119

120
  if (thread != nullptr) {
121
    if (thread_oop == nullptr || thread->jvmti_vthread() == nullptr || thread->jvmti_vthread() == thread_oop) {
122
      // The JavaThread for carrier or mounted virtual thread case.
123
      // Set this only if thread_oop is current thread->jvmti_vthread().
124
      thread->set_jvmti_thread_state(this);
125
    }
126
    thread->set_interp_only_mode(0);
127
  }
128
}
129

130

131
JvmtiThreadState::~JvmtiThreadState()   {
132
  assert(JvmtiThreadState_lock->is_locked(), "sanity check");
133

134
  if (_classes_being_redefined != nullptr) {
135
    delete _classes_being_redefined; // free the GrowableArray on C heap
136
  }
137

138
  // clear this as the state for the thread
139
  get_thread()->set_jvmti_thread_state(nullptr);
140

141
  // zap our env thread states
142
  {
143
    JvmtiEnvBase::entering_dying_thread_env_iteration();
144
    JvmtiEnvThreadStateIterator it(this);
145
    for (JvmtiEnvThreadState* ets = it.first(); ets != nullptr; ) {
146
      JvmtiEnvThreadState* zap = ets;
147
      ets = it.next(ets);
148
      delete zap;
149
    }
150
    JvmtiEnvBase::leaving_dying_thread_env_iteration();
151
  }
152

153
  // remove us from the list
154
  {
155
    // The thread state list manipulation code must not have safepoints.
156
    // See periodic_clean_up().
157
    debug_only(NoSafepointVerifier nosafepoint;)
158

159
    if (_prev == nullptr) {
160
      assert(_head == this, "sanity check");
161
      _head = _next;
162
    } else {
163
      assert(_head != this, "sanity check");
164
      _prev->_next = _next;
165
    }
166
    if (_next != nullptr) {
167
      _next->_prev = _prev;
168
    }
169
    _next = nullptr;
170
    _prev = nullptr;
171
  }
172
  if (get_thread_oop() != nullptr) {
173
    java_lang_Thread::set_jvmti_thread_state(get_thread_oop(), nullptr);
174
  }
175
  _thread_oop_h.release(JvmtiExport::jvmti_oop_storage());
176
}
177

178

179
void
180
JvmtiThreadState::periodic_clean_up() {
181
  assert(SafepointSynchronize::is_at_safepoint(), "at safepoint");
182

183
  // This iteration is initialized with "_head" instead of "JvmtiThreadState::first()"
184
  // because the latter requires the JvmtiThreadState_lock.
185
  // This iteration is safe at a safepoint as well, see the NoSafepointVerifier
186
  // asserts at all list manipulation sites.
187
  for (JvmtiThreadState *state = _head; state != nullptr; state = state->next()) {
188
    // For each environment thread state corresponding to an invalid environment
189
    // unlink it from the list and deallocate it.
190
    JvmtiEnvThreadStateIterator it(state);
191
    JvmtiEnvThreadState* previous_ets = nullptr;
192
    JvmtiEnvThreadState* ets = it.first();
193
    while (ets != nullptr) {
194
      if (ets->get_env()->is_valid()) {
195
        previous_ets = ets;
196
        ets = it.next(ets);
197
      } else {
198
        // This one isn't valid, remove it from the list and deallocate it
199
        JvmtiEnvThreadState* defunct_ets = ets;
200
        ets = ets->next();
201
        if (previous_ets == nullptr) {
202
          assert(state->head_env_thread_state() == defunct_ets, "sanity check");
203
          state->set_head_env_thread_state(ets);
204
        } else {
205
          previous_ets->set_next(ets);
206
        }
207
        delete defunct_ets;
208
      }
209
    }
210
  }
211
}
212

213
//
214
// Virtual Threads Mount State transition (VTMS transition) mechanism
215
//
216

217
// VTMS transitions for one virtual thread are disabled while it is positive
218
volatile int JvmtiVTMSTransitionDisabler::_VTMS_transition_disable_for_one_count = 0;
219

220
// VTMS transitions for all virtual threads are disabled while it is positive
221
volatile int JvmtiVTMSTransitionDisabler::_VTMS_transition_disable_for_all_count = 0;
222

223
// There is an active suspender or resumer.
224
volatile bool JvmtiVTMSTransitionDisabler::_SR_mode = false;
225

226
// Notifications from VirtualThread about VTMS events are enabled.
227
bool JvmtiVTMSTransitionDisabler::_VTMS_notify_jvmti_events = false;
228

229
// The JvmtiVTMSTransitionDisabler sync protocol is enabled if this count > 0.
230
volatile int JvmtiVTMSTransitionDisabler::_sync_protocol_enabled_count = 0;
231

232
// JvmtiVTMSTraansitionDisabler sync protocol is enabled permanently after seeing a suspender.
233
volatile bool JvmtiVTMSTransitionDisabler::_sync_protocol_enabled_permanently = false;
234

235
#ifdef ASSERT
236
void
237
JvmtiVTMSTransitionDisabler::print_info() {
238
  log_error(jvmti)("_VTMS_transition_disable_for_one_count: %d\n", _VTMS_transition_disable_for_one_count);
239
  log_error(jvmti)("_VTMS_transition_disable_for_all_count: %d\n\n", _VTMS_transition_disable_for_all_count);
240
  int attempts = 10000;
241
  for (JavaThreadIteratorWithHandle jtiwh; JavaThread *java_thread = jtiwh.next(); ) {
242
    if (java_thread->VTMS_transition_mark()) {
243
      log_error(jvmti)("jt: %p VTMS_transition_mark: %d\n",
244
                       (void*)java_thread, java_thread->VTMS_transition_mark());
245
    }
246
    ResourceMark rm;
247
    // Handshake with target.
248
    PrintStackTraceClosure pstc;
249
    Handshake::execute(&pstc, java_thread);
250
  }
251
}
252
#endif
253

254
// disable VTMS transitions for one virtual thread
255
// no-op if thread is non-null and not a virtual thread
256
JvmtiVTMSTransitionDisabler::JvmtiVTMSTransitionDisabler(jthread thread)
257
  : _is_SR(false), _thread(thread)
258
{
259
  if (!Continuations::enabled()) {
260
    return; // JvmtiVTMSTransitionDisabler is no-op without virtual threads
261
  }
262
  if (Thread::current_or_null() == nullptr) {
263
    return;  // Detached thread, can be a call from Agent_OnLoad.
264
  }
265
  if (!sync_protocol_enabled_permanently()) {
266
    JvmtiVTMSTransitionDisabler::inc_sync_protocol_enabled_count();
267
  }
268
  if (_thread != nullptr) {
269
    VTMS_transition_disable_for_one(); // disable VTMS transitions for one virtual thread
270
  } else {
271
    VTMS_transition_disable_for_all(); // disable VTMS transitions for all virtual threads
272
  }
273
}
274

275
// disable VTMS transitions for all virtual threads
276
JvmtiVTMSTransitionDisabler::JvmtiVTMSTransitionDisabler(bool is_SR)
277
  : _is_SR(is_SR), _thread(nullptr)
278
{
279
  if (!Continuations::enabled()) {
280
    return; // JvmtiVTMSTransitionDisabler is no-op without virtual threads
281
  }
282
  if (Thread::current_or_null() == nullptr) {
283
    return;  // Detached thread, can be a call from Agent_OnLoad.
284
  }
285
  if (!sync_protocol_enabled_permanently()) {
286
    JvmtiVTMSTransitionDisabler::inc_sync_protocol_enabled_count();
287
    if (is_SR) {
288
      Atomic::store(&_sync_protocol_enabled_permanently, true);
289
    }
290
  }
291
  VTMS_transition_disable_for_all();
292
}
293

294
JvmtiVTMSTransitionDisabler::~JvmtiVTMSTransitionDisabler() {
295
  if (!Continuations::enabled()) {
296
    return; // JvmtiVTMSTransitionDisabler is a no-op without virtual threads
297
  }
298
  if (Thread::current_or_null() == nullptr) {
299
    return;  // Detached thread, can be a call from Agent_OnLoad.
300
  }
301
  if (_thread != nullptr) {
302
    VTMS_transition_enable_for_one(); // enable VTMS transitions for one virtual thread
303
  } else {
304
    VTMS_transition_enable_for_all(); // enable VTMS transitions for all virtual threads
305
  }
306
  if (!sync_protocol_enabled_permanently()) {
307
    JvmtiVTMSTransitionDisabler::dec_sync_protocol_enabled_count();
308
  }
309
}
310

311
// disable VTMS transitions for one virtual thread
312
void
313
JvmtiVTMSTransitionDisabler::VTMS_transition_disable_for_one() {
314
  assert(_thread != nullptr, "sanity check");
315
  JavaThread* thread = JavaThread::current();
316
  HandleMark hm(thread);
317
  Handle vth = Handle(thread, JNIHandles::resolve_external_guard(_thread));
318
  if (!java_lang_VirtualThread::is_instance(vth())) {
319
    return; // no-op if _thread is not a virtual thread
320
  }
321
  MonitorLocker ml(JvmtiVTMSTransition_lock);
322

323
  while (_SR_mode) { // suspender or resumer is a JvmtiVTMSTransitionDisabler monopolist
324
    ml.wait(10); // wait while there is an active suspender or resumer
325
  }
326
  Atomic::inc(&_VTMS_transition_disable_for_one_count);
327
  java_lang_Thread::inc_VTMS_transition_disable_count(vth());
328

329
  while (java_lang_Thread::is_in_VTMS_transition(vth())) {
330
    ml.wait(10); // wait while the virtual thread is in transition
331
  }
332
#ifdef ASSERT
333
  thread->set_is_VTMS_transition_disabler(true);
334
#endif
335
}
336

337
// disable VTMS transitions for all virtual threads
338
void
339
JvmtiVTMSTransitionDisabler::VTMS_transition_disable_for_all() {
340
  JavaThread* thread = JavaThread::current();
341
  int attempts = 50000;
342
  {
343
    MonitorLocker ml(JvmtiVTMSTransition_lock);
344

345
    assert(!thread->is_in_tmp_VTMS_transition(), "sanity check");
346
    assert(!thread->is_in_VTMS_transition(), "VTMS_transition sanity check");
347
    while (_SR_mode) { // Suspender or resumer is a JvmtiVTMSTransitionDisabler monopolist.
348
      ml.wait(10);     // Wait while there is an active suspender or resumer.
349
    }
350
    if (_is_SR) {
351
      _SR_mode = true;
352
      while (_VTMS_transition_disable_for_all_count > 0 ||
353
             _VTMS_transition_disable_for_one_count > 0) {
354
        ml.wait(10);   // Wait while there is any active jvmtiVTMSTransitionDisabler.
355
      }
356
    }
357
    Atomic::inc(&_VTMS_transition_disable_for_all_count);
358

359
    // Block while some mount/unmount transitions are in progress.
360
    // Debug version fails and prints diagnostic information.
361
    for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) {
362
      while (jt->VTMS_transition_mark()) {
363
        if (ml.wait(10)) {
364
          attempts--;
365
        }
366
        DEBUG_ONLY(if (attempts == 0) break;)
367
      }
368
    }
369
    assert(!thread->is_VTMS_transition_disabler(), "VTMS_transition sanity check");
370
#ifdef ASSERT
371
    if (attempts > 0) {
372
      thread->set_is_VTMS_transition_disabler(true);
373
    }
374
#endif
375
  }
376
#ifdef ASSERT
377
    if (attempts == 0) {
378
      print_info();
379
      fatal("stuck in JvmtiVTMSTransitionDisabler::VTMS_transition_disable");
380
    }
381
#endif
382
}
383

384
// enable VTMS transitions for one virtual thread
385
void
386
JvmtiVTMSTransitionDisabler::VTMS_transition_enable_for_one() {
387
  JavaThread* thread = JavaThread::current();
388
  HandleMark hm(thread);
389
  Handle vth = Handle(thread, JNIHandles::resolve_external_guard(_thread));
390
  if (!java_lang_VirtualThread::is_instance(vth())) {
391
    return; // no-op if _thread is not a virtual thread
392
  }
393
  MonitorLocker ml(JvmtiVTMSTransition_lock);
394
  java_lang_Thread::dec_VTMS_transition_disable_count(vth());
395
  Atomic::dec(&_VTMS_transition_disable_for_one_count);
396
  if (_VTMS_transition_disable_for_one_count == 0) {
397
    ml.notify_all();
398
  }
399
#ifdef ASSERT
400
  thread->set_is_VTMS_transition_disabler(false);
401
#endif
402
}
403

404
// enable VTMS transitions for all virtual threads
405
void
406
JvmtiVTMSTransitionDisabler::VTMS_transition_enable_for_all() {
407
  JavaThread* current = JavaThread::current();
408
  {
409
    MonitorLocker ml(JvmtiVTMSTransition_lock);
410
    assert(_VTMS_transition_disable_for_all_count > 0, "VTMS_transition sanity check");
411

412
    if (_is_SR) {  // Disabler is suspender or resumer.
413
      _SR_mode = false;
414
    }
415
    Atomic::dec(&_VTMS_transition_disable_for_all_count);
416
    if (_VTMS_transition_disable_for_all_count == 0 || _is_SR) {
417
      ml.notify_all();
418
    }
419
#ifdef ASSERT
420
    current->set_is_VTMS_transition_disabler(false);
421
#endif
422
  }
423
}
424

425
void
426
JvmtiVTMSTransitionDisabler::start_VTMS_transition(jthread vthread, bool is_mount) {
427
  JavaThread* thread = JavaThread::current();
428
  oop vt = JNIHandles::resolve_external_guard(vthread);
429
  assert(!thread->is_in_VTMS_transition(), "VTMS_transition sanity check");
430

431
  // Avoid using MonitorLocker on performance critical path, use
432
  // two-level synchronization with lock-free operations on state bits.
433
  assert(!thread->VTMS_transition_mark(), "sanity check");
434
  thread->set_VTMS_transition_mark(true); // Try to enter VTMS transition section optmistically.
435
  java_lang_Thread::set_is_in_VTMS_transition(vt, true);
436

437
  if (!sync_protocol_enabled()) {
438
    thread->set_is_in_VTMS_transition(true);
439
    return;
440
  }
441
  HandleMark hm(thread);
442
  Handle vth = Handle(thread, vt);
443
  int attempts = 50000;
444

445
  // Do not allow suspends inside VTMS transitions.
446
  // Block while transitions are disabled or there are suspend requests.
447
  int64_t thread_id = java_lang_Thread::thread_id(vth());  // Cannot use oops while blocked.
448

449
  if (_VTMS_transition_disable_for_all_count > 0 ||
450
      java_lang_Thread::VTMS_transition_disable_count(vth()) > 0 ||
451
      thread->is_suspended() ||
452
      JvmtiVTSuspender::is_vthread_suspended(thread_id)
453
  ) {
454
    // Slow path: undo unsuccessful optimistic set of the VTMS_transition_mark.
455
    // It can cause an extra waiting cycle for VTMS transition disablers.
456
    thread->set_VTMS_transition_mark(false);
457
    java_lang_Thread::set_is_in_VTMS_transition(vth(), false);
458

459
    while (true) {
460
      MonitorLocker ml(JvmtiVTMSTransition_lock);
461

462
      // Do not allow suspends inside VTMS transitions.
463
      // Block while transitions are disabled or there are suspend requests.
464
      if (_VTMS_transition_disable_for_all_count > 0 ||
465
          java_lang_Thread::VTMS_transition_disable_count(vth()) > 0 ||
466
          thread->is_suspended() ||
467
          JvmtiVTSuspender::is_vthread_suspended(thread_id)
468
      ) {
469
        // Block while transitions are disabled or there are suspend requests.
470
        if (ml.wait(10)) {
471
          attempts--;
472
        }
473
        DEBUG_ONLY(if (attempts == 0) break;)
474
        continue;  // ~ThreadBlockInVM has handshake-based suspend point.
475
      }
476
      thread->set_VTMS_transition_mark(true);
477
      java_lang_Thread::set_is_in_VTMS_transition(vth(), true);
478
      break;
479
    }
480
  }
481
#ifdef ASSERT
482
  if (attempts == 0) {
483
    log_error(jvmti)("start_VTMS_transition: thread->is_suspended: %d is_vthread_suspended: %d\n\n",
484
                     thread->is_suspended(), JvmtiVTSuspender::is_vthread_suspended(thread_id));
485
    print_info();
486
    fatal("stuck in JvmtiVTMSTransitionDisabler::start_VTMS_transition");
487
  }
488
#endif
489
  // Enter VTMS transition section.
490
  thread->set_is_in_VTMS_transition(true);
491
}
492

493
void
494
JvmtiVTMSTransitionDisabler::finish_VTMS_transition(jthread vthread, bool is_mount) {
495
  JavaThread* thread = JavaThread::current();
496

497
  assert(thread->is_in_VTMS_transition(), "sanity check");
498
  thread->set_is_in_VTMS_transition(false);
499
  oop vt = JNIHandles::resolve_external_guard(vthread);
500
  java_lang_Thread::set_is_in_VTMS_transition(vt, false);
501
  assert(thread->VTMS_transition_mark(), "sanity check");
502
  thread->set_VTMS_transition_mark(false);
503

504
  if (!sync_protocol_enabled()) {
505
    return;
506
  }
507
  int64_t thread_id = java_lang_Thread::thread_id(vt);
508

509
  // Unblock waiting VTMS transition disablers.
510
  if (_VTMS_transition_disable_for_one_count > 0 ||
511
      _VTMS_transition_disable_for_all_count > 0) {
512
    MonitorLocker ml(JvmtiVTMSTransition_lock);
513
    ml.notify_all();
514
  }
515
  // In unmount case the carrier thread is attached after unmount transition.
516
  // Check and block it if there was external suspend request.
517
  int attempts = 10000;
518
  if (!is_mount && thread->is_carrier_thread_suspended()) {
519
    while (true) {
520
      MonitorLocker ml(JvmtiVTMSTransition_lock);
521

522
      // Block while there are suspend requests.
523
      if ((!is_mount && thread->is_carrier_thread_suspended()) ||
524
          (is_mount && JvmtiVTSuspender::is_vthread_suspended(thread_id))
525
      ) {
526
        // Block while there are suspend requests.
527
        if (ml.wait(10)) {
528
          attempts--;
529
        }
530
        DEBUG_ONLY(if (attempts == 0) break;)
531
        continue;
532
      }
533
      break;
534
    }
535
  }
536
#ifdef ASSERT
537
  if (attempts == 0) {
538
    log_error(jvmti)("finish_VTMS_transition: thread->is_suspended: %d is_vthread_suspended: %d\n\n",
539
                     thread->is_suspended(), JvmtiVTSuspender::is_vthread_suspended(thread_id));
540
    print_info();
541
    fatal("stuck in JvmtiVTMSTransitionDisabler::finish_VTMS_transition");
542
  }
543
#endif
544
}
545

546
// set VTMS transition bit value in JavaThread and java.lang.VirtualThread object
547
void JvmtiVTMSTransitionDisabler::set_is_in_VTMS_transition(JavaThread* thread, jobject vthread, bool in_trans) {
548
  oop vt = JNIHandles::resolve_external_guard(vthread);
549
  java_lang_Thread::set_is_in_VTMS_transition(vt, in_trans);
550
  thread->set_is_in_VTMS_transition(in_trans);
551
}
552

553
void
554
JvmtiVTMSTransitionDisabler::VTMS_vthread_start(jobject vthread) {
555
  VTMS_mount_end(vthread);
556
  JavaThread* thread = JavaThread::current();
557

558
  assert(!thread->is_in_VTMS_transition(), "sanity check");
559
  assert(!thread->is_in_tmp_VTMS_transition(), "sanity check");
560

561
  // If interp_only_mode has been enabled then we must eagerly create JvmtiThreadState
562
  // objects for globally enabled virtual thread filtered events. Otherwise,
563
  // it is an important optimization to create JvmtiThreadState objects lazily.
564
  // This optimization is disabled when watchpoint capabilities are present. It is to
565
  // work around a bug with virtual thread frames which can be not deoptimized in time.
566
  if (JvmtiThreadState::seen_interp_only_mode() ||
567
      JvmtiExport::should_post_field_access() ||
568
      JvmtiExport::should_post_field_modification()){
569
    JvmtiEventController::thread_started(thread);
570
  }
571
  if (JvmtiExport::should_post_vthread_start()) {
572
    JvmtiExport::post_vthread_start(vthread);
573
  }
574
  // post VirtualThreadMount event after VirtualThreadStart
575
  if (JvmtiExport::should_post_vthread_mount()) {
576
    JvmtiExport::post_vthread_mount(vthread);
577
  }
578
}
579

580
void
581
JvmtiVTMSTransitionDisabler::VTMS_vthread_end(jobject vthread) {
582
  JavaThread* thread = JavaThread::current();
583

584
  assert(!thread->is_in_VTMS_transition(), "sanity check");
585
  assert(!thread->is_in_tmp_VTMS_transition(), "sanity check");
586

587
  // post VirtualThreadUnmount event before VirtualThreadEnd
588
  if (JvmtiExport::should_post_vthread_unmount()) {
589
    JvmtiExport::post_vthread_unmount(vthread);
590
  }
591
  if (JvmtiExport::should_post_vthread_end()) {
592
    JvmtiExport::post_vthread_end(vthread);
593
  }
594
  VTMS_unmount_begin(vthread, /* last_unmount */ true);
595
  if (thread->jvmti_thread_state() != nullptr) {
596
    JvmtiExport::cleanup_thread(thread);
597
    assert(thread->jvmti_thread_state() == nullptr, "should be null");
598
    assert(java_lang_Thread::jvmti_thread_state(JNIHandles::resolve(vthread)) == nullptr, "should be null");
599
  }
600
  thread->rebind_to_jvmti_thread_state_of(thread->threadObj());
601
}
602

603
void
604
JvmtiVTMSTransitionDisabler::VTMS_vthread_mount(jobject vthread, bool hide) {
605
  if (hide) {
606
    VTMS_mount_begin(vthread);
607
  } else {
608
    VTMS_mount_end(vthread);
609
    if (JvmtiExport::should_post_vthread_mount()) {
610
      JvmtiExport::post_vthread_mount(vthread);
611
    }
612
  }
613
}
614

615
void
616
JvmtiVTMSTransitionDisabler::VTMS_vthread_unmount(jobject vthread, bool hide) {
617
  if (hide) {
618
    if (JvmtiExport::should_post_vthread_unmount()) {
619
      JvmtiExport::post_vthread_unmount(vthread);
620
    }
621
    VTMS_unmount_begin(vthread, /* last_unmount */ false);
622
  } else {
623
    VTMS_unmount_end(vthread);
624
  }
625
}
626

627
void
628
JvmtiVTMSTransitionDisabler::VTMS_mount_begin(jobject vthread) {
629
  JavaThread* thread = JavaThread::current();
630
  assert(!thread->is_in_tmp_VTMS_transition(), "sanity check");
631
  assert(!thread->is_in_VTMS_transition(), "sanity check");
632
  start_VTMS_transition(vthread, /* is_mount */ true);
633
}
634

635
void
636
JvmtiVTMSTransitionDisabler::VTMS_mount_end(jobject vthread) {
637
  JavaThread* thread = JavaThread::current();
638
  oop vt = JNIHandles::resolve(vthread);
639

640
  thread->rebind_to_jvmti_thread_state_of(vt);
641

642
  assert(thread->is_in_VTMS_transition(), "sanity check");
643
  assert(!thread->is_in_tmp_VTMS_transition(), "sanity check");
644
  finish_VTMS_transition(vthread, /* is_mount */ true);
645
}
646

647
void
648
JvmtiVTMSTransitionDisabler::VTMS_unmount_begin(jobject vthread, bool last_unmount) {
649
  JavaThread* thread = JavaThread::current();
650

651
  assert(!thread->is_in_tmp_VTMS_transition(), "sanity check");
652
  assert(!thread->is_in_VTMS_transition(), "sanity check");
653

654
  start_VTMS_transition(vthread, /* is_mount */ false);
655
  if (!last_unmount) {
656
    thread->rebind_to_jvmti_thread_state_of(thread->threadObj());
657
  }
658
}
659

660
void
661
JvmtiVTMSTransitionDisabler::VTMS_unmount_end(jobject vthread) {
662
  JavaThread* thread = JavaThread::current();
663
  assert(thread->is_in_VTMS_transition(), "sanity check");
664
  assert(!thread->is_in_tmp_VTMS_transition(), "sanity check");
665
  finish_VTMS_transition(vthread, /* is_mount */ false);
666
}
667

668

669
//
670
// Virtual Threads Suspend/Resume management
671
//
672

673
JvmtiVTSuspender::SR_Mode
674
JvmtiVTSuspender::_SR_mode = SR_none;
675

676
VirtualThreadList*
677
JvmtiVTSuspender::_suspended_list = new VirtualThreadList();
678

679
VirtualThreadList*
680
JvmtiVTSuspender::_not_suspended_list = new VirtualThreadList();
681

682
void
683
JvmtiVTSuspender::register_all_vthreads_suspend() {
684
  MonitorLocker ml(JvmtiVTMSTransition_lock);
685

686
  _SR_mode = SR_all;
687
  _suspended_list->invalidate();
688
  _not_suspended_list->invalidate();
689
}
690

691
void
692
JvmtiVTSuspender::register_all_vthreads_resume() {
693
  MonitorLocker ml(JvmtiVTMSTransition_lock);
694

695
  _SR_mode = SR_none;
696
  _suspended_list->invalidate();
697
  _not_suspended_list->invalidate();
698
}
699

700
void
701
JvmtiVTSuspender::register_vthread_suspend(oop vt) {
702
  int64_t id = java_lang_Thread::thread_id(vt);
703
  MonitorLocker ml(JvmtiVTMSTransition_lock);
704

705
  if (_SR_mode == SR_all) {
706
    assert(_not_suspended_list->contains(id),
707
           "register_vthread_suspend sanity check");
708
    _not_suspended_list->remove(id);
709
  } else {
710
    assert(!_suspended_list->contains(id),
711
           "register_vthread_suspend sanity check");
712
    _SR_mode = SR_ind;
713
    _suspended_list->append(id);
714
  }
715
}
716

717
void
718
JvmtiVTSuspender::register_vthread_resume(oop vt) {
719
  int64_t id = java_lang_Thread::thread_id(vt);
720
  MonitorLocker ml(JvmtiVTMSTransition_lock);
721

722
  if (_SR_mode == SR_all) {
723
    assert(!_not_suspended_list->contains(id),
724
           "register_vthread_resume sanity check");
725
    _not_suspended_list->append(id);
726
  } else if (_SR_mode == SR_ind) {
727
    assert(_suspended_list->contains(id),
728
           "register_vthread_resume check");
729
    _suspended_list->remove(id);
730
    if (_suspended_list->length() == 0) {
731
      _SR_mode = SR_none;
732
    }
733
  } else {
734
    assert(false, "register_vthread_resume: no suspend mode enabled");
735
  }
736
}
737

738
bool
739
JvmtiVTSuspender::is_vthread_suspended(int64_t thread_id) {
740
  bool suspend_is_needed =
741
   (_SR_mode == SR_all && !_not_suspended_list->contains(thread_id)) ||
742
   (_SR_mode == SR_ind && _suspended_list->contains(thread_id));
743

744
  return suspend_is_needed;
745
}
746

747
bool
748
JvmtiVTSuspender::is_vthread_suspended(oop vt) {
749
  return is_vthread_suspended(java_lang_Thread::thread_id(vt));
750
}
751

752
void JvmtiThreadState::add_env(JvmtiEnvBase *env) {
753
  assert(JvmtiThreadState_lock->is_locked(), "sanity check");
754

755
  JvmtiEnvThreadState *new_ets = new JvmtiEnvThreadState(this, env);
756
  // add this environment thread state to the end of the list (order is important)
757
  {
758
    // list deallocation (which occurs at a safepoint) cannot occur simultaneously
759
    debug_only(NoSafepointVerifier nosafepoint;)
760

761
    JvmtiEnvThreadStateIterator it(this);
762
    JvmtiEnvThreadState* previous_ets = nullptr;
763
    for (JvmtiEnvThreadState* ets = it.first(); ets != nullptr; ets = it.next(ets)) {
764
      previous_ets = ets;
765
    }
766
    if (previous_ets == nullptr) {
767
      set_head_env_thread_state(new_ets);
768
    } else {
769
      previous_ets->set_next(new_ets);
770
    }
771
  }
772
}
773

774
void JvmtiThreadState::enter_interp_only_mode() {
775
  assert(_thread != nullptr, "sanity check");
776
  _seen_interp_only_mode = true;
777
  _thread->increment_interp_only_mode();
778
  invalidate_cur_stack_depth();
779
}
780

781
void JvmtiThreadState::leave_interp_only_mode() {
782
  assert(is_interp_only_mode(), "leaving interp only when not in interp only mode");
783
  if (_thread == nullptr) {
784
    // Unmounted virtual thread updates the saved value.
785
    --_saved_interp_only_mode;
786
  } else {
787
    _thread->decrement_interp_only_mode();
788
  }
789
}
790

791

792
// Helper routine used in several places
793
int JvmtiThreadState::count_frames() {
794
  JavaThread* thread = get_thread_or_saved();
795
  javaVFrame *jvf;
796
  ResourceMark rm;
797
  if (thread == nullptr) {
798
    oop thread_obj = get_thread_oop();
799
    jvf = JvmtiEnvBase::get_vthread_jvf(thread_obj);
800
  } else {
801
#ifdef ASSERT
802
    Thread *current_thread = Thread::current();
803
#endif
804
    assert(SafepointSynchronize::is_at_safepoint() ||
805
           thread->is_handshake_safe_for(current_thread),
806
           "call by myself / at safepoint / at handshake");
807
    if (!thread->has_last_Java_frame()) return 0;  // No Java frames.
808
    // TBD: This might need to be corrected for detached carrier threads.
809
    RegisterMap reg_map(thread,
810
                        RegisterMap::UpdateMap::skip,
811
                        RegisterMap::ProcessFrames::skip,
812
                        RegisterMap::WalkContinuation::include);
813
    jvf = thread->last_java_vframe(&reg_map);
814
    jvf = JvmtiEnvBase::check_and_skip_hidden_frames(thread, jvf);
815
  }
816
  return (int)JvmtiEnvBase::get_frame_count(jvf);
817
}
818

819

820
void JvmtiThreadState::invalidate_cur_stack_depth() {
821
  assert(SafepointSynchronize::is_at_safepoint() ||
822
         get_thread()->is_handshake_safe_for(Thread::current()),
823
         "bad synchronization with owner thread");
824

825
  _cur_stack_depth = UNKNOWN_STACK_DEPTH;
826
}
827

828
void JvmtiThreadState::incr_cur_stack_depth() {
829
  guarantee(JavaThread::current() == get_thread(), "must be current thread");
830

831
  if (!is_interp_only_mode()) {
832
    _cur_stack_depth = UNKNOWN_STACK_DEPTH;
833
  }
834
  if (_cur_stack_depth != UNKNOWN_STACK_DEPTH) {
835
    ++_cur_stack_depth;
836
  }
837
}
838

839
void JvmtiThreadState::decr_cur_stack_depth() {
840
  guarantee(JavaThread::current() == get_thread(), "must be current thread");
841

842
  if (!is_interp_only_mode()) {
843
    _cur_stack_depth = UNKNOWN_STACK_DEPTH;
844
  }
845
  if (_cur_stack_depth != UNKNOWN_STACK_DEPTH) {
846
    --_cur_stack_depth;
847
    assert(_cur_stack_depth >= 0, "incr/decr_cur_stack_depth mismatch");
848
  }
849
}
850

851
int JvmtiThreadState::cur_stack_depth() {
852
  Thread *current = Thread::current();
853
  guarantee(get_thread()->is_handshake_safe_for(current),
854
            "must be current thread or direct handshake");
855

856
  if (!is_interp_only_mode() || _cur_stack_depth == UNKNOWN_STACK_DEPTH) {
857
    _cur_stack_depth = count_frames();
858
  } else {
859
#ifdef ASSERT
860
    if (EnableJVMTIStackDepthAsserts) {
861
      // heavy weight assert
862
      jint num_frames = count_frames();
863
      assert(_cur_stack_depth == num_frames, "cur_stack_depth out of sync _cur_stack_depth: %d num_frames: %d", _cur_stack_depth, num_frames);
864
    }
865
#endif
866
  }
867
  return _cur_stack_depth;
868
}
869

870
void JvmtiThreadState::process_pending_step_for_popframe() {
871
  // We are single stepping as the last part of the PopFrame() dance
872
  // so we have some house keeping to do.
873

874
  JavaThread *thr = get_thread();
875
  if (thr->popframe_condition() != JavaThread::popframe_inactive) {
876
    // If the popframe_condition field is not popframe_inactive, then
877
    // we missed all of the popframe_field cleanup points:
878
    //
879
    // - unpack_frames() was not called (nothing to deopt)
880
    // - remove_activation_preserving_args_entry() was not called
881
    //   (did not get suspended in a call_vm() family call and did
882
    //   not complete a call_vm() family call on the way here)
883
    thr->clear_popframe_condition();
884
  }
885

886
  // clearing the flag indicates we are done with the PopFrame() dance
887
  clr_pending_step_for_popframe();
888

889
  // If exception was thrown in this frame, need to reset jvmti thread state.
890
  // Single stepping may not get enabled correctly by the agent since
891
  // exception state is passed in MethodExit event which may be sent at some
892
  // time in the future. JDWP agent ignores MethodExit events if caused by
893
  // an exception.
894
  //
895
  if (is_exception_detected()) {
896
    clear_exception_state();
897
  }
898
  // If step is pending for popframe then it may not be
899
  // a repeat step. The new_bci and method_id is same as current_bci
900
  // and current method_id after pop and step for recursive calls.
901
  // Force the step by clearing the last location.
902
  JvmtiEnvThreadStateIterator it(this);
903
  for (JvmtiEnvThreadState* ets = it.first(); ets != nullptr; ets = it.next(ets)) {
904
    ets->clear_current_location();
905
  }
906
}
907

908

909
// Class:     JvmtiThreadState
910
// Function:  update_for_pop_top_frame
911
// Description:
912
//   This function removes any frame pop notification request for
913
//   the top frame and invalidates both the current stack depth and
914
//   all cached frameIDs.
915
//
916
// Called by: PopFrame
917
//
918
void JvmtiThreadState::update_for_pop_top_frame() {
919
  if (is_interp_only_mode()) {
920
    // remove any frame pop notification request for the top frame
921
    // in any environment
922
    int popframe_number = cur_stack_depth();
923
    {
924
      JvmtiEnvThreadStateIterator it(this);
925
      for (JvmtiEnvThreadState* ets = it.first(); ets != nullptr; ets = it.next(ets)) {
926
        if (ets->is_frame_pop(popframe_number)) {
927
          ets->clear_frame_pop(popframe_number);
928
        }
929
      }
930
    }
931
    // force stack depth to be recalculated
932
    invalidate_cur_stack_depth();
933
  } else {
934
    assert(!is_enabled(JVMTI_EVENT_FRAME_POP), "Must have no framepops set");
935
  }
936
}
937

938

939
void JvmtiThreadState::process_pending_step_for_earlyret() {
940
  // We are single stepping as the last part of the ForceEarlyReturn
941
  // dance so we have some house keeping to do.
942

943
  if (is_earlyret_pending()) {
944
    // If the earlyret_state field is not earlyret_inactive, then
945
    // we missed all of the earlyret_field cleanup points:
946
    //
947
    // - remove_activation() was not called
948
    //   (did not get suspended in a call_vm() family call and did
949
    //   not complete a call_vm() family call on the way here)
950
    //
951
    // One legitimate way for us to miss all the cleanup points is
952
    // if we got here right after handling a compiled return. If that
953
    // is the case, then we consider our return from compiled code to
954
    // complete the ForceEarlyReturn request and we clear the condition.
955
    clr_earlyret_pending();
956
    set_earlyret_oop(nullptr);
957
    clr_earlyret_value();
958
  }
959

960
  // clearing the flag indicates we are done with
961
  // the ForceEarlyReturn() dance
962
  clr_pending_step_for_earlyret();
963

964
  // If exception was thrown in this frame, need to reset jvmti thread state.
965
  // Single stepping may not get enabled correctly by the agent since
966
  // exception state is passed in MethodExit event which may be sent at some
967
  // time in the future. JDWP agent ignores MethodExit events if caused by
968
  // an exception.
969
  //
970
  if (is_exception_detected()) {
971
    clear_exception_state();
972
  }
973
  // If step is pending for earlyret then it may not be a repeat step.
974
  // The new_bci and method_id is same as current_bci and current
975
  // method_id after earlyret and step for recursive calls.
976
  // Force the step by clearing the last location.
977
  JvmtiEnvThreadStateIterator it(this);
978
  for (JvmtiEnvThreadState* ets = it.first(); ets != nullptr; ets = it.next(ets)) {
979
    ets->clear_current_location();
980
  }
981
}
982

983
void JvmtiThreadState::oops_do(OopClosure* f, NMethodClosure* cf) {
984
  f->do_oop((oop*) &_earlyret_oop);
985

986
  // Keep nmethods from unloading on the event queue
987
  if (_jvmti_event_queue != nullptr) {
988
    _jvmti_event_queue->oops_do(f, cf);
989
  }
990
}
991

992
void JvmtiThreadState::nmethods_do(NMethodClosure* cf) {
993
  // Keep nmethods from unloading on the event queue
994
  if (_jvmti_event_queue != nullptr) {
995
    _jvmti_event_queue->nmethods_do(cf);
996
  }
997
}
998

999
// Thread local event queue.
1000
void JvmtiThreadState::enqueue_event(JvmtiDeferredEvent* event) {
1001
  if (_jvmti_event_queue == nullptr) {
1002
    _jvmti_event_queue = new JvmtiDeferredEventQueue();
1003
  }
1004
  // copy the event
1005
  _jvmti_event_queue->enqueue(*event);
1006
}
1007

1008
void JvmtiThreadState::post_events(JvmtiEnv* env) {
1009
  if (_jvmti_event_queue != nullptr) {
1010
    _jvmti_event_queue->post(env);  // deletes each queue node
1011
    delete _jvmti_event_queue;
1012
    _jvmti_event_queue = nullptr;
1013
  }
1014
}
1015

1016
void JvmtiThreadState::run_nmethod_entry_barriers() {
1017
  if (_jvmti_event_queue != nullptr) {
1018
    _jvmti_event_queue->run_nmethod_entry_barriers();
1019
  }
1020
}
1021

1022
oop JvmtiThreadState::get_thread_oop() {
1023
  return _thread_oop_h.resolve();
1024
}
1025

1026
void JvmtiThreadState::set_thread(JavaThread* thread) {
1027
  _thread_saved = nullptr;  // Common case.
1028
  if (!_is_virtual && thread == nullptr) {
1029
    // Save JavaThread* if carrier thread is being detached.
1030
    _thread_saved = _thread;
1031
  }
1032
  _thread = thread;
1033
}
1034

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.