Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/prims/jvmtiImpl.cpp
41145 views
1
/*
2
* Copyright (c) 2003, 2021, 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.hpp"
27
#include "classfile/symbolTable.hpp"
28
#include "code/nmethod.hpp"
29
#include "interpreter/interpreter.hpp"
30
#include "interpreter/oopMapCache.hpp"
31
#include "jvmtifiles/jvmtiEnv.hpp"
32
#include "logging/log.hpp"
33
#include "logging/logStream.hpp"
34
#include "memory/allocation.inline.hpp"
35
#include "memory/resourceArea.hpp"
36
#include "oops/instanceKlass.hpp"
37
#include "oops/klass.inline.hpp"
38
#include "oops/oop.inline.hpp"
39
#include "oops/oopHandle.inline.hpp"
40
#include "prims/jvmtiAgentThread.hpp"
41
#include "prims/jvmtiEventController.inline.hpp"
42
#include "prims/jvmtiImpl.hpp"
43
#include "prims/jvmtiRedefineClasses.hpp"
44
#include "runtime/deoptimization.hpp"
45
#include "runtime/frame.inline.hpp"
46
#include "runtime/handles.inline.hpp"
47
#include "runtime/interfaceSupport.inline.hpp"
48
#include "runtime/javaCalls.hpp"
49
#include "runtime/jniHandles.hpp"
50
#include "runtime/os.hpp"
51
#include "runtime/serviceThread.hpp"
52
#include "runtime/signature.hpp"
53
#include "runtime/thread.inline.hpp"
54
#include "runtime/threadSMR.hpp"
55
#include "runtime/vframe.hpp"
56
#include "runtime/vframe_hp.hpp"
57
#include "runtime/vmOperations.hpp"
58
#include "utilities/exceptions.hpp"
59
60
//
61
// class JvmtiAgentThread
62
//
63
// JavaThread used to wrap a thread started by an agent
64
// using the JVMTI method RunAgentThread.
65
//
66
67
JvmtiAgentThread::JvmtiAgentThread(JvmtiEnv* env, jvmtiStartFunction start_fn, const void *start_arg)
68
: JavaThread(start_function_wrapper) {
69
_env = env;
70
_start_fn = start_fn;
71
_start_arg = start_arg;
72
}
73
74
void
75
JvmtiAgentThread::start_function_wrapper(JavaThread *thread, TRAPS) {
76
// It is expected that any Agent threads will be created as
77
// Java Threads. If this is the case, notification of the creation
78
// of the thread is given in JavaThread::thread_main().
79
assert(thread == JavaThread::current(), "sanity check");
80
81
JvmtiAgentThread *dthread = (JvmtiAgentThread *)thread;
82
dthread->call_start_function();
83
}
84
85
void
86
JvmtiAgentThread::call_start_function() {
87
ThreadToNativeFromVM transition(this);
88
_start_fn(_env->jvmti_external(), jni_environment(), (void*)_start_arg);
89
}
90
91
92
//
93
// class GrowableCache - private methods
94
//
95
96
void GrowableCache::recache() {
97
int len = _elements->length();
98
99
FREE_C_HEAP_ARRAY(address, _cache);
100
_cache = NEW_C_HEAP_ARRAY(address,len+1, mtInternal);
101
102
for (int i=0; i<len; i++) {
103
_cache[i] = _elements->at(i)->getCacheValue();
104
//
105
// The cache entry has gone bad. Without a valid frame pointer
106
// value, the entry is useless so we simply delete it in product
107
// mode. The call to remove() will rebuild the cache again
108
// without the bad entry.
109
//
110
if (_cache[i] == NULL) {
111
assert(false, "cannot recache NULL elements");
112
remove(i);
113
return;
114
}
115
}
116
_cache[len] = NULL;
117
118
_listener_fun(_this_obj,_cache);
119
}
120
121
bool GrowableCache::equals(void* v, GrowableElement *e2) {
122
GrowableElement *e1 = (GrowableElement *) v;
123
assert(e1 != NULL, "e1 != NULL");
124
assert(e2 != NULL, "e2 != NULL");
125
126
return e1->equals(e2);
127
}
128
129
//
130
// class GrowableCache - public methods
131
//
132
133
GrowableCache::GrowableCache() {
134
_this_obj = NULL;
135
_listener_fun = NULL;
136
_elements = NULL;
137
_cache = NULL;
138
}
139
140
GrowableCache::~GrowableCache() {
141
clear();
142
delete _elements;
143
FREE_C_HEAP_ARRAY(address, _cache);
144
}
145
146
void GrowableCache::initialize(void *this_obj, void listener_fun(void *, address*) ) {
147
_this_obj = this_obj;
148
_listener_fun = listener_fun;
149
_elements = new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<GrowableElement*>(5, mtServiceability);
150
recache();
151
}
152
153
// number of elements in the collection
154
int GrowableCache::length() {
155
return _elements->length();
156
}
157
158
// get the value of the index element in the collection
159
GrowableElement* GrowableCache::at(int index) {
160
GrowableElement *e = (GrowableElement *) _elements->at(index);
161
assert(e != NULL, "e != NULL");
162
return e;
163
}
164
165
int GrowableCache::find(GrowableElement* e) {
166
return _elements->find(e, GrowableCache::equals);
167
}
168
169
// append a copy of the element to the end of the collection
170
void GrowableCache::append(GrowableElement* e) {
171
GrowableElement *new_e = e->clone();
172
_elements->append(new_e);
173
recache();
174
}
175
176
// remove the element at index
177
void GrowableCache::remove (int index) {
178
GrowableElement *e = _elements->at(index);
179
assert(e != NULL, "e != NULL");
180
_elements->remove(e);
181
delete e;
182
recache();
183
}
184
185
// clear out all elements, release all heap space and
186
// let our listener know that things have changed.
187
void GrowableCache::clear() {
188
int len = _elements->length();
189
for (int i=0; i<len; i++) {
190
delete _elements->at(i);
191
}
192
_elements->clear();
193
recache();
194
}
195
196
//
197
// class JvmtiBreakpoint
198
//
199
200
JvmtiBreakpoint::JvmtiBreakpoint(Method* m_method, jlocation location)
201
: _method(m_method), _bci((int)location) {
202
assert(_method != NULL, "No method for breakpoint.");
203
assert(_bci >= 0, "Negative bci for breakpoint.");
204
oop class_holder_oop = _method->method_holder()->klass_holder();
205
_class_holder = OopHandle(JvmtiExport::jvmti_oop_storage(), class_holder_oop);
206
}
207
208
JvmtiBreakpoint::~JvmtiBreakpoint() {
209
if (_class_holder.peek() != NULL) {
210
_class_holder.release(JvmtiExport::jvmti_oop_storage());
211
}
212
}
213
214
void JvmtiBreakpoint::copy(JvmtiBreakpoint& bp) {
215
_method = bp._method;
216
_bci = bp._bci;
217
_class_holder = OopHandle(JvmtiExport::jvmti_oop_storage(), bp._class_holder.resolve());
218
}
219
220
bool JvmtiBreakpoint::equals(JvmtiBreakpoint& bp) {
221
return _method == bp._method
222
&& _bci == bp._bci;
223
}
224
225
address JvmtiBreakpoint::getBcp() const {
226
return _method->bcp_from(_bci);
227
}
228
229
void JvmtiBreakpoint::each_method_version_do(method_action meth_act) {
230
((Method*)_method->*meth_act)(_bci);
231
232
// add/remove breakpoint to/from versions of the method that are EMCP.
233
Thread *thread = Thread::current();
234
InstanceKlass* ik = _method->method_holder();
235
Symbol* m_name = _method->name();
236
Symbol* m_signature = _method->signature();
237
238
// search previous versions if they exist
239
for (InstanceKlass* pv_node = ik->previous_versions();
240
pv_node != NULL;
241
pv_node = pv_node->previous_versions()) {
242
Array<Method*>* methods = pv_node->methods();
243
244
for (int i = methods->length() - 1; i >= 0; i--) {
245
Method* method = methods->at(i);
246
// Only set breakpoints in EMCP methods.
247
// EMCP methods are old but not obsolete. Equivalent
248
// Modulo Constant Pool means the method is equivalent except
249
// the constant pool and instructions that access the constant
250
// pool might be different.
251
// If a breakpoint is set in a redefined method, its EMCP methods
252
// must have a breakpoint also.
253
// None of the methods are deleted until none are running.
254
// This code could set a breakpoint in a method that
255
// is never reached, but this won't be noticeable to the programmer.
256
if (!method->is_obsolete() &&
257
method->name() == m_name &&
258
method->signature() == m_signature) {
259
ResourceMark rm;
260
log_debug(redefine, class, breakpoint)
261
("%sing breakpoint in %s(%s)", meth_act == &Method::set_breakpoint ? "sett" : "clear",
262
method->name()->as_C_string(), method->signature()->as_C_string());
263
(method->*meth_act)(_bci);
264
break;
265
}
266
}
267
}
268
}
269
270
void JvmtiBreakpoint::set() {
271
each_method_version_do(&Method::set_breakpoint);
272
}
273
274
void JvmtiBreakpoint::clear() {
275
each_method_version_do(&Method::clear_breakpoint);
276
}
277
278
void JvmtiBreakpoint::print_on(outputStream* out) const {
279
#ifndef PRODUCT
280
ResourceMark rm;
281
const char *class_name = (_method == NULL) ? "NULL" : _method->klass_name()->as_C_string();
282
const char *method_name = (_method == NULL) ? "NULL" : _method->name()->as_C_string();
283
out->print("Breakpoint(%s,%s,%d,%p)", class_name, method_name, _bci, getBcp());
284
#endif
285
}
286
287
288
//
289
// class VM_ChangeBreakpoints
290
//
291
// Modify the Breakpoints data structure at a safepoint
292
//
293
294
void VM_ChangeBreakpoints::doit() {
295
switch (_operation) {
296
case SET_BREAKPOINT:
297
_breakpoints->set_at_safepoint(*_bp);
298
break;
299
case CLEAR_BREAKPOINT:
300
_breakpoints->clear_at_safepoint(*_bp);
301
break;
302
default:
303
assert(false, "Unknown operation");
304
}
305
}
306
307
//
308
// class JvmtiBreakpoints
309
//
310
// a JVMTI internal collection of JvmtiBreakpoint
311
//
312
313
JvmtiBreakpoints::JvmtiBreakpoints(void listener_fun(void *,address *)) {
314
_bps.initialize(this,listener_fun);
315
}
316
317
JvmtiBreakpoints:: ~JvmtiBreakpoints() {}
318
319
void JvmtiBreakpoints::print() {
320
#ifndef PRODUCT
321
LogTarget(Trace, jvmti) log;
322
LogStream log_stream(log);
323
324
int n = _bps.length();
325
for (int i=0; i<n; i++) {
326
JvmtiBreakpoint& bp = _bps.at(i);
327
log_stream.print("%d: ", i);
328
bp.print_on(&log_stream);
329
log_stream.cr();
330
}
331
#endif
332
}
333
334
335
void JvmtiBreakpoints::set_at_safepoint(JvmtiBreakpoint& bp) {
336
assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
337
338
int i = _bps.find(bp);
339
if (i == -1) {
340
_bps.append(bp);
341
bp.set();
342
}
343
}
344
345
void JvmtiBreakpoints::clear_at_safepoint(JvmtiBreakpoint& bp) {
346
assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
347
348
int i = _bps.find(bp);
349
if (i != -1) {
350
_bps.remove(i);
351
bp.clear();
352
}
353
}
354
355
int JvmtiBreakpoints::length() { return _bps.length(); }
356
357
int JvmtiBreakpoints::set(JvmtiBreakpoint& bp) {
358
if ( _bps.find(bp) != -1) {
359
return JVMTI_ERROR_DUPLICATE;
360
}
361
VM_ChangeBreakpoints set_breakpoint(VM_ChangeBreakpoints::SET_BREAKPOINT, &bp);
362
VMThread::execute(&set_breakpoint);
363
return JVMTI_ERROR_NONE;
364
}
365
366
int JvmtiBreakpoints::clear(JvmtiBreakpoint& bp) {
367
if ( _bps.find(bp) == -1) {
368
return JVMTI_ERROR_NOT_FOUND;
369
}
370
371
VM_ChangeBreakpoints clear_breakpoint(VM_ChangeBreakpoints::CLEAR_BREAKPOINT, &bp);
372
VMThread::execute(&clear_breakpoint);
373
return JVMTI_ERROR_NONE;
374
}
375
376
void JvmtiBreakpoints::clearall_in_class_at_safepoint(Klass* klass) {
377
bool changed = true;
378
// We are going to run thru the list of bkpts
379
// and delete some. This deletion probably alters
380
// the list in some implementation defined way such
381
// that when we delete entry i, the next entry might
382
// no longer be at i+1. To be safe, each time we delete
383
// an entry, we'll just start again from the beginning.
384
// We'll stop when we make a pass thru the whole list without
385
// deleting anything.
386
while (changed) {
387
int len = _bps.length();
388
changed = false;
389
for (int i = 0; i < len; i++) {
390
JvmtiBreakpoint& bp = _bps.at(i);
391
if (bp.method()->method_holder() == klass) {
392
bp.clear();
393
_bps.remove(i);
394
// This changed 'i' so we have to start over.
395
changed = true;
396
break;
397
}
398
}
399
}
400
}
401
402
//
403
// class JvmtiCurrentBreakpoints
404
//
405
406
JvmtiBreakpoints *JvmtiCurrentBreakpoints::_jvmti_breakpoints = NULL;
407
address * JvmtiCurrentBreakpoints::_breakpoint_list = NULL;
408
409
410
JvmtiBreakpoints& JvmtiCurrentBreakpoints::get_jvmti_breakpoints() {
411
if (_jvmti_breakpoints != NULL) return (*_jvmti_breakpoints);
412
_jvmti_breakpoints = new JvmtiBreakpoints(listener_fun);
413
assert(_jvmti_breakpoints != NULL, "_jvmti_breakpoints != NULL");
414
return (*_jvmti_breakpoints);
415
}
416
417
void JvmtiCurrentBreakpoints::listener_fun(void *this_obj, address *cache) {
418
JvmtiBreakpoints *this_jvmti = (JvmtiBreakpoints *) this_obj;
419
assert(this_jvmti != NULL, "this_jvmti != NULL");
420
421
debug_only(int n = this_jvmti->length(););
422
assert(cache[n] == NULL, "cache must be NULL terminated");
423
424
set_breakpoint_list(cache);
425
}
426
427
///////////////////////////////////////////////////////////////
428
//
429
// class VM_GetOrSetLocal
430
//
431
432
// Constructor for non-object getter
433
VM_GetOrSetLocal::VM_GetOrSetLocal(JavaThread* thread, jint depth, jint index, BasicType type)
434
: _thread(thread)
435
, _calling_thread(NULL)
436
, _depth(depth)
437
, _index(index)
438
, _type(type)
439
, _jvf(NULL)
440
, _set(false)
441
, _eb(false, NULL, NULL)
442
, _result(JVMTI_ERROR_NONE)
443
{
444
}
445
446
// Constructor for object or non-object setter
447
VM_GetOrSetLocal::VM_GetOrSetLocal(JavaThread* thread, jint depth, jint index, BasicType type, jvalue value)
448
: _thread(thread)
449
, _calling_thread(NULL)
450
, _depth(depth)
451
, _index(index)
452
, _type(type)
453
, _value(value)
454
, _jvf(NULL)
455
, _set(true)
456
, _eb(type == T_OBJECT, JavaThread::current(), thread)
457
, _result(JVMTI_ERROR_NONE)
458
{
459
}
460
461
// Constructor for object getter
462
VM_GetOrSetLocal::VM_GetOrSetLocal(JavaThread* thread, JavaThread* calling_thread, jint depth, int index)
463
: _thread(thread)
464
, _calling_thread(calling_thread)
465
, _depth(depth)
466
, _index(index)
467
, _type(T_OBJECT)
468
, _jvf(NULL)
469
, _set(false)
470
, _eb(true, calling_thread, thread)
471
, _result(JVMTI_ERROR_NONE)
472
{
473
}
474
475
vframe *VM_GetOrSetLocal::get_vframe() {
476
if (!_thread->has_last_Java_frame()) {
477
return NULL;
478
}
479
RegisterMap reg_map(_thread);
480
vframe *vf = _thread->last_java_vframe(&reg_map);
481
int d = 0;
482
while ((vf != NULL) && (d < _depth)) {
483
vf = vf->java_sender();
484
d++;
485
}
486
return vf;
487
}
488
489
javaVFrame *VM_GetOrSetLocal::get_java_vframe() {
490
vframe* vf = get_vframe();
491
if (vf == NULL) {
492
_result = JVMTI_ERROR_NO_MORE_FRAMES;
493
return NULL;
494
}
495
javaVFrame *jvf = (javaVFrame*)vf;
496
497
if (!vf->is_java_frame()) {
498
_result = JVMTI_ERROR_OPAQUE_FRAME;
499
return NULL;
500
}
501
return jvf;
502
}
503
504
// Check that the klass is assignable to a type with the given signature.
505
// Another solution could be to use the function Klass::is_subtype_of(type).
506
// But the type class can be forced to load/initialize eagerly in such a case.
507
// This may cause unexpected consequences like CFLH or class-init JVMTI events.
508
// It is better to avoid such a behavior.
509
bool VM_GetOrSetLocal::is_assignable(const char* ty_sign, Klass* klass, Thread* thread) {
510
assert(ty_sign != NULL, "type signature must not be NULL");
511
assert(thread != NULL, "thread must not be NULL");
512
assert(klass != NULL, "klass must not be NULL");
513
514
int len = (int) strlen(ty_sign);
515
if (ty_sign[0] == JVM_SIGNATURE_CLASS &&
516
ty_sign[len-1] == JVM_SIGNATURE_ENDCLASS) { // Need pure class/interface name
517
ty_sign++;
518
len -= 2;
519
}
520
TempNewSymbol ty_sym = SymbolTable::new_symbol(ty_sign, len);
521
if (klass->name() == ty_sym) {
522
return true;
523
}
524
// Compare primary supers
525
int super_depth = klass->super_depth();
526
int idx;
527
for (idx = 0; idx < super_depth; idx++) {
528
if (klass->primary_super_of_depth(idx)->name() == ty_sym) {
529
return true;
530
}
531
}
532
// Compare secondary supers
533
const Array<Klass*>* sec_supers = klass->secondary_supers();
534
for (idx = 0; idx < sec_supers->length(); idx++) {
535
if (((Klass*) sec_supers->at(idx))->name() == ty_sym) {
536
return true;
537
}
538
}
539
return false;
540
}
541
542
// Checks error conditions:
543
// JVMTI_ERROR_INVALID_SLOT
544
// JVMTI_ERROR_TYPE_MISMATCH
545
// Returns: 'true' - everything is Ok, 'false' - error code
546
547
bool VM_GetOrSetLocal::check_slot_type_lvt(javaVFrame* jvf) {
548
Method* method = jvf->method();
549
jint num_entries = method->localvariable_table_length();
550
if (num_entries == 0) {
551
_result = JVMTI_ERROR_INVALID_SLOT;
552
return false; // There are no slots
553
}
554
int signature_idx = -1;
555
int vf_bci = jvf->bci();
556
LocalVariableTableElement* table = method->localvariable_table_start();
557
for (int i = 0; i < num_entries; i++) {
558
int start_bci = table[i].start_bci;
559
int end_bci = start_bci + table[i].length;
560
561
// Here we assume that locations of LVT entries
562
// with the same slot number cannot be overlapped
563
if (_index == (jint) table[i].slot && start_bci <= vf_bci && vf_bci <= end_bci) {
564
signature_idx = (int) table[i].descriptor_cp_index;
565
break;
566
}
567
}
568
if (signature_idx == -1) {
569
_result = JVMTI_ERROR_INVALID_SLOT;
570
return false; // Incorrect slot index
571
}
572
Symbol* sign_sym = method->constants()->symbol_at(signature_idx);
573
BasicType slot_type = Signature::basic_type(sign_sym);
574
575
switch (slot_type) {
576
case T_BYTE:
577
case T_SHORT:
578
case T_CHAR:
579
case T_BOOLEAN:
580
slot_type = T_INT;
581
break;
582
case T_ARRAY:
583
slot_type = T_OBJECT;
584
break;
585
default:
586
break;
587
};
588
if (_type != slot_type) {
589
_result = JVMTI_ERROR_TYPE_MISMATCH;
590
return false;
591
}
592
593
jobject jobj = _value.l;
594
if (_set && slot_type == T_OBJECT && jobj != NULL) { // NULL reference is allowed
595
// Check that the jobject class matches the return type signature.
596
oop obj = JNIHandles::resolve_external_guard(jobj);
597
NULL_CHECK(obj, (_result = JVMTI_ERROR_INVALID_OBJECT, false));
598
Klass* ob_k = obj->klass();
599
NULL_CHECK(ob_k, (_result = JVMTI_ERROR_INVALID_OBJECT, false));
600
601
const char* signature = (const char *) sign_sym->as_utf8();
602
if (!is_assignable(signature, ob_k, VMThread::vm_thread())) {
603
_result = JVMTI_ERROR_TYPE_MISMATCH;
604
return false;
605
}
606
}
607
return true;
608
}
609
610
bool VM_GetOrSetLocal::check_slot_type_no_lvt(javaVFrame* jvf) {
611
Method* method = jvf->method();
612
jint extra_slot = (_type == T_LONG || _type == T_DOUBLE) ? 1 : 0;
613
614
if (_index < 0 || _index + extra_slot >= method->max_locals()) {
615
_result = JVMTI_ERROR_INVALID_SLOT;
616
return false;
617
}
618
StackValueCollection *locals = _jvf->locals();
619
BasicType slot_type = locals->at(_index)->type();
620
621
if (slot_type == T_CONFLICT) {
622
_result = JVMTI_ERROR_INVALID_SLOT;
623
return false;
624
}
625
if (extra_slot) {
626
BasicType extra_slot_type = locals->at(_index + 1)->type();
627
if (extra_slot_type != T_INT) {
628
_result = JVMTI_ERROR_INVALID_SLOT;
629
return false;
630
}
631
}
632
if (_type != slot_type && (_type == T_OBJECT || slot_type != T_INT)) {
633
_result = JVMTI_ERROR_TYPE_MISMATCH;
634
return false;
635
}
636
return true;
637
}
638
639
static bool can_be_deoptimized(vframe* vf) {
640
return (vf->is_compiled_frame() && vf->fr().can_be_deoptimized());
641
}
642
643
bool VM_GetOrSetLocal::doit_prologue() {
644
if (!_eb.deoptimize_objects(_depth, _depth)) {
645
// The target frame is affected by a reallocation failure.
646
_result = JVMTI_ERROR_OUT_OF_MEMORY;
647
return false;
648
}
649
650
return true;
651
}
652
653
void VM_GetOrSetLocal::doit() {
654
_jvf = _jvf == NULL ? get_java_vframe() : _jvf;
655
if (_jvf == NULL) {
656
return;
657
};
658
659
Method* method = _jvf->method();
660
if (getting_receiver()) {
661
if (method->is_static()) {
662
_result = JVMTI_ERROR_INVALID_SLOT;
663
return;
664
}
665
} else {
666
if (method->is_native()) {
667
_result = JVMTI_ERROR_OPAQUE_FRAME;
668
return;
669
}
670
671
if (!check_slot_type_no_lvt(_jvf)) {
672
return;
673
}
674
if (method->has_localvariable_table() &&
675
!check_slot_type_lvt(_jvf)) {
676
return;
677
}
678
}
679
680
InterpreterOopMap oop_mask;
681
_jvf->method()->mask_for(_jvf->bci(), &oop_mask);
682
if (oop_mask.is_dead(_index)) {
683
// The local can be invalid and uninitialized in the scope of current bci
684
_result = JVMTI_ERROR_INVALID_SLOT;
685
return;
686
}
687
if (_set) {
688
// Force deoptimization of frame if compiled because it's
689
// possible the compiler emitted some locals as constant values,
690
// meaning they are not mutable.
691
if (can_be_deoptimized(_jvf)) {
692
693
// Schedule deoptimization so that eventually the local
694
// update will be written to an interpreter frame.
695
Deoptimization::deoptimize_frame(_jvf->thread(), _jvf->fr().id());
696
697
// Now store a new value for the local which will be applied
698
// once deoptimization occurs. Note however that while this
699
// write is deferred until deoptimization actually happens
700
// can vframe created after this point will have its locals
701
// reflecting this update so as far as anyone can see the
702
// write has already taken place.
703
704
// If we are updating an oop then get the oop from the handle
705
// since the handle will be long gone by the time the deopt
706
// happens. The oop stored in the deferred local will be
707
// gc'd on its own.
708
if (_type == T_OBJECT) {
709
_value.l = cast_from_oop<jobject>(JNIHandles::resolve_external_guard(_value.l));
710
}
711
// Re-read the vframe so we can see that it is deoptimized
712
// [ Only need because of assert in update_local() ]
713
_jvf = get_java_vframe();
714
((compiledVFrame*)_jvf)->update_local(_type, _index, _value);
715
return;
716
}
717
StackValueCollection *locals = _jvf->locals();
718
Thread* current_thread = VMThread::vm_thread();
719
HandleMark hm(current_thread);
720
721
switch (_type) {
722
case T_INT: locals->set_int_at (_index, _value.i); break;
723
case T_LONG: locals->set_long_at (_index, _value.j); break;
724
case T_FLOAT: locals->set_float_at (_index, _value.f); break;
725
case T_DOUBLE: locals->set_double_at(_index, _value.d); break;
726
case T_OBJECT: {
727
Handle ob_h(current_thread, JNIHandles::resolve_external_guard(_value.l));
728
locals->set_obj_at (_index, ob_h);
729
break;
730
}
731
default: ShouldNotReachHere();
732
}
733
_jvf->set_locals(locals);
734
} else {
735
if (_jvf->method()->is_native() && _jvf->is_compiled_frame()) {
736
assert(getting_receiver(), "Can only get here when getting receiver");
737
oop receiver = _jvf->fr().get_native_receiver();
738
_value.l = JNIHandles::make_local(_calling_thread, receiver);
739
} else {
740
StackValueCollection *locals = _jvf->locals();
741
742
switch (_type) {
743
case T_INT: _value.i = locals->int_at (_index); break;
744
case T_LONG: _value.j = locals->long_at (_index); break;
745
case T_FLOAT: _value.f = locals->float_at (_index); break;
746
case T_DOUBLE: _value.d = locals->double_at(_index); break;
747
case T_OBJECT: {
748
// Wrap the oop to be returned in a local JNI handle since
749
// oops_do() no longer applies after doit() is finished.
750
oop obj = locals->obj_at(_index)();
751
_value.l = JNIHandles::make_local(_calling_thread, obj);
752
break;
753
}
754
default: ShouldNotReachHere();
755
}
756
}
757
}
758
}
759
760
761
bool VM_GetOrSetLocal::allow_nested_vm_operations() const {
762
return true; // May need to deoptimize
763
}
764
765
766
VM_GetReceiver::VM_GetReceiver(
767
JavaThread* thread, JavaThread* caller_thread, jint depth)
768
: VM_GetOrSetLocal(thread, caller_thread, depth, 0) {}
769
770
/////////////////////////////////////////////////////////////////////////////////////////
771
772
//
773
// class JvmtiSuspendControl - see comments in jvmtiImpl.hpp
774
//
775
776
bool JvmtiSuspendControl::suspend(JavaThread *java_thread) {
777
return java_thread->java_suspend();
778
}
779
780
bool JvmtiSuspendControl::resume(JavaThread *java_thread) {
781
return java_thread->java_resume();
782
}
783
784
void JvmtiSuspendControl::print() {
785
#ifndef PRODUCT
786
ResourceMark rm;
787
LogStreamHandle(Trace, jvmti) log_stream;
788
log_stream.print("Suspended Threads: [");
789
for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thread = jtiwh.next(); ) {
790
#ifdef JVMTI_TRACE
791
const char *name = JvmtiTrace::safe_get_thread_name(thread);
792
#else
793
const char *name = "";
794
#endif /*JVMTI_TRACE */
795
log_stream.print("%s(%c ", name, thread->is_suspended() ? 'S' : '_');
796
if (!thread->has_last_Java_frame()) {
797
log_stream.print("no stack");
798
}
799
log_stream.print(") ");
800
}
801
log_stream.print_cr("]");
802
#endif
803
}
804
805
JvmtiDeferredEvent JvmtiDeferredEvent::compiled_method_load_event(
806
nmethod* nm) {
807
JvmtiDeferredEvent event = JvmtiDeferredEvent(TYPE_COMPILED_METHOD_LOAD);
808
event._event_data.compiled_method_load = nm;
809
return event;
810
}
811
812
JvmtiDeferredEvent JvmtiDeferredEvent::compiled_method_unload_event(
813
jmethodID id, const void* code) {
814
JvmtiDeferredEvent event = JvmtiDeferredEvent(TYPE_COMPILED_METHOD_UNLOAD);
815
event._event_data.compiled_method_unload.method_id = id;
816
event._event_data.compiled_method_unload.code_begin = code;
817
return event;
818
}
819
820
JvmtiDeferredEvent JvmtiDeferredEvent::dynamic_code_generated_event(
821
const char* name, const void* code_begin, const void* code_end) {
822
JvmtiDeferredEvent event = JvmtiDeferredEvent(TYPE_DYNAMIC_CODE_GENERATED);
823
// Need to make a copy of the name since we don't know how long
824
// the event poster will keep it around after we enqueue the
825
// deferred event and return. strdup() failure is handled in
826
// the post() routine below.
827
event._event_data.dynamic_code_generated.name = os::strdup(name);
828
event._event_data.dynamic_code_generated.code_begin = code_begin;
829
event._event_data.dynamic_code_generated.code_end = code_end;
830
return event;
831
}
832
833
JvmtiDeferredEvent JvmtiDeferredEvent::class_unload_event(const char* name) {
834
JvmtiDeferredEvent event = JvmtiDeferredEvent(TYPE_CLASS_UNLOAD);
835
// Need to make a copy of the name since we don't know how long
836
// the event poster will keep it around after we enqueue the
837
// deferred event and return. strdup() failure is handled in
838
// the post() routine below.
839
event._event_data.class_unload.name = os::strdup(name);
840
return event;
841
}
842
843
void JvmtiDeferredEvent::post() {
844
assert(Thread::current()->is_service_thread(),
845
"Service thread must post enqueued events");
846
switch(_type) {
847
case TYPE_COMPILED_METHOD_LOAD: {
848
nmethod* nm = _event_data.compiled_method_load;
849
JvmtiExport::post_compiled_method_load(nm);
850
break;
851
}
852
case TYPE_COMPILED_METHOD_UNLOAD: {
853
JvmtiExport::post_compiled_method_unload(
854
_event_data.compiled_method_unload.method_id,
855
_event_data.compiled_method_unload.code_begin);
856
break;
857
}
858
case TYPE_DYNAMIC_CODE_GENERATED: {
859
JvmtiExport::post_dynamic_code_generated_internal(
860
// if strdup failed give the event a default name
861
(_event_data.dynamic_code_generated.name == NULL)
862
? "unknown_code" : _event_data.dynamic_code_generated.name,
863
_event_data.dynamic_code_generated.code_begin,
864
_event_data.dynamic_code_generated.code_end);
865
if (_event_data.dynamic_code_generated.name != NULL) {
866
// release our copy
867
os::free((void *)_event_data.dynamic_code_generated.name);
868
}
869
break;
870
}
871
case TYPE_CLASS_UNLOAD: {
872
JvmtiExport::post_class_unload_internal(
873
// if strdup failed give the event a default name
874
(_event_data.class_unload.name == NULL)
875
? "unknown_class" : _event_data.class_unload.name);
876
if (_event_data.class_unload.name != NULL) {
877
// release our copy
878
os::free((void *)_event_data.class_unload.name);
879
}
880
break;
881
}
882
default:
883
ShouldNotReachHere();
884
}
885
}
886
887
void JvmtiDeferredEvent::post_compiled_method_load_event(JvmtiEnv* env) {
888
assert(_type == TYPE_COMPILED_METHOD_LOAD, "only user of this method");
889
nmethod* nm = _event_data.compiled_method_load;
890
JvmtiExport::post_compiled_method_load(env, nm);
891
}
892
893
void JvmtiDeferredEvent::run_nmethod_entry_barriers() {
894
if (_type == TYPE_COMPILED_METHOD_LOAD) {
895
_event_data.compiled_method_load->run_nmethod_entry_barrier();
896
}
897
}
898
899
900
// Keep the nmethod for compiled_method_load from being unloaded.
901
void JvmtiDeferredEvent::oops_do(OopClosure* f, CodeBlobClosure* cf) {
902
if (cf != NULL && _type == TYPE_COMPILED_METHOD_LOAD) {
903
cf->do_code_blob(_event_data.compiled_method_load);
904
}
905
}
906
907
// The sweeper calls this and marks the nmethods here on the stack so that
908
// they cannot be turned into zombies while in the queue.
909
void JvmtiDeferredEvent::nmethods_do(CodeBlobClosure* cf) {
910
if (cf != NULL && _type == TYPE_COMPILED_METHOD_LOAD) {
911
cf->do_code_blob(_event_data.compiled_method_load);
912
}
913
}
914
915
916
bool JvmtiDeferredEventQueue::has_events() {
917
// We save the queued events before the live phase and post them when it starts.
918
// This code could skip saving the events on the queue before the live
919
// phase and ignore them, but this would change how we do things now.
920
// Starting the service thread earlier causes this to be called before the live phase begins.
921
// The events on the queue should all be posted after the live phase so this is an
922
// ok check. Before the live phase, DynamicCodeGenerated events are posted directly.
923
// If we add other types of events to the deferred queue, this could get ugly.
924
return JvmtiEnvBase::get_phase() == JVMTI_PHASE_LIVE && _queue_head != NULL;
925
}
926
927
void JvmtiDeferredEventQueue::enqueue(JvmtiDeferredEvent event) {
928
// Events get added to the end of the queue (and are pulled off the front).
929
QueueNode* node = new QueueNode(event);
930
if (_queue_tail == NULL) {
931
_queue_tail = _queue_head = node;
932
} else {
933
assert(_queue_tail->next() == NULL, "Must be the last element in the list");
934
_queue_tail->set_next(node);
935
_queue_tail = node;
936
}
937
938
assert((_queue_head == NULL) == (_queue_tail == NULL),
939
"Inconsistent queue markers");
940
}
941
942
JvmtiDeferredEvent JvmtiDeferredEventQueue::dequeue() {
943
assert(_queue_head != NULL, "Nothing to dequeue");
944
945
if (_queue_head == NULL) {
946
// Just in case this happens in product; it shouldn't but let's not crash
947
return JvmtiDeferredEvent();
948
}
949
950
QueueNode* node = _queue_head;
951
_queue_head = _queue_head->next();
952
if (_queue_head == NULL) {
953
_queue_tail = NULL;
954
}
955
956
assert((_queue_head == NULL) == (_queue_tail == NULL),
957
"Inconsistent queue markers");
958
959
JvmtiDeferredEvent event = node->event();
960
delete node;
961
return event;
962
}
963
964
void JvmtiDeferredEventQueue::post(JvmtiEnv* env) {
965
// Post and destroy queue nodes
966
while (_queue_head != NULL) {
967
JvmtiDeferredEvent event = dequeue();
968
event.post_compiled_method_load_event(env);
969
}
970
}
971
972
void JvmtiDeferredEventQueue::run_nmethod_entry_barriers() {
973
for(QueueNode* node = _queue_head; node != NULL; node = node->next()) {
974
node->event().run_nmethod_entry_barriers();
975
}
976
}
977
978
979
void JvmtiDeferredEventQueue::oops_do(OopClosure* f, CodeBlobClosure* cf) {
980
for(QueueNode* node = _queue_head; node != NULL; node = node->next()) {
981
node->event().oops_do(f, cf);
982
}
983
}
984
985
void JvmtiDeferredEventQueue::nmethods_do(CodeBlobClosure* cf) {
986
for(QueueNode* node = _queue_head; node != NULL; node = node->next()) {
987
node->event().nmethods_do(cf);
988
}
989
}
990
991