/
jvmtiEnvBase.cpp
1590 lines (1371 loc) · 51 KB
/
jvmtiEnvBase.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include "precompiled.hpp"
#include "classfile/classLoaderDataGraph.hpp"
#include "classfile/moduleEntry.hpp"
#include "classfile/systemDictionary.hpp"
#include "jvmtifiles/jvmtiEnv.hpp"
#include "memory/iterator.hpp"
#include "memory/resourceArea.hpp"
#include "oops/objArrayKlass.hpp"
#include "oops/objArrayOop.hpp"
#include "oops/oop.inline.hpp"
#include "oops/oopHandle.inline.hpp"
#include "prims/jvmtiEnvBase.hpp"
#include "prims/jvmtiEventController.inline.hpp"
#include "prims/jvmtiExtensions.hpp"
#include "prims/jvmtiImpl.hpp"
#include "prims/jvmtiManageCapabilities.hpp"
#include "prims/jvmtiTagMap.hpp"
#include "prims/jvmtiThreadState.inline.hpp"
#include "runtime/biasedLocking.hpp"
#include "runtime/deoptimization.hpp"
#include "runtime/frame.inline.hpp"
#include "runtime/handles.inline.hpp"
#include "runtime/interfaceSupport.inline.hpp"
#include "runtime/jfieldIDWorkaround.hpp"
#include "runtime/jniHandles.inline.hpp"
#include "runtime/objectMonitor.hpp"
#include "runtime/objectMonitor.inline.hpp"
#include "runtime/signature.hpp"
#include "runtime/thread.inline.hpp"
#include "runtime/threadSMR.hpp"
#include "runtime/vframe.hpp"
#include "runtime/vframe_hp.hpp"
#include "runtime/vmThread.hpp"
#include "runtime/vmOperations.hpp"
///////////////////////////////////////////////////////////////
//
// JvmtiEnvBase
//
JvmtiEnvBase* JvmtiEnvBase::_head_environment = NULL;
bool JvmtiEnvBase::_globally_initialized = false;
volatile bool JvmtiEnvBase::_needs_clean_up = false;
jvmtiPhase JvmtiEnvBase::_phase = JVMTI_PHASE_PRIMORDIAL;
volatile int JvmtiEnvBase::_dying_thread_env_iteration_count = 0;
extern jvmtiInterface_1_ jvmti_Interface;
extern jvmtiInterface_1_ jvmtiTrace_Interface;
// perform initializations that must occur before any JVMTI environments
// are released but which should only be initialized once (no matter
// how many environments are created).
void
JvmtiEnvBase::globally_initialize() {
assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
assert(_globally_initialized == false, "bad call");
JvmtiManageCapabilities::initialize();
// register extension functions and events
JvmtiExtensions::register_extensions();
#ifdef JVMTI_TRACE
JvmtiTrace::initialize();
#endif
_globally_initialized = true;
}
void
JvmtiEnvBase::initialize() {
assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
// Add this environment to the end of the environment list (order is important)
{
// This block of code must not contain any safepoints, as list deallocation
// (which occurs at a safepoint) cannot occur simultaneously with this list
// addition. Note: NoSafepointVerifier cannot, currently, be used before
// threads exist.
JvmtiEnvIterator it;
JvmtiEnvBase *previous_env = NULL;
for (JvmtiEnvBase* env = it.first(); env != NULL; env = it.next(env)) {
previous_env = env;
}
if (previous_env == NULL) {
_head_environment = this;
} else {
previous_env->set_next_environment(this);
}
}
if (_globally_initialized == false) {
globally_initialize();
}
}
jvmtiPhase
JvmtiEnvBase::phase() {
// For the JVMTI environments possessed the can_generate_early_vmstart:
// replace JVMTI_PHASE_PRIMORDIAL with JVMTI_PHASE_START
if (_phase == JVMTI_PHASE_PRIMORDIAL &&
JvmtiExport::early_vmstart_recorded() &&
early_vmstart_env()) {
return JVMTI_PHASE_START;
}
return _phase; // Normal case
}
bool
JvmtiEnvBase::is_valid() {
jint value = 0;
// This object might not be a JvmtiEnvBase so we can't assume
// the _magic field is properly aligned. Get the value in a safe
// way and then check against JVMTI_MAGIC.
switch (sizeof(_magic)) {
case 2:
value = Bytes::get_native_u2((address)&_magic);
break;
case 4:
value = Bytes::get_native_u4((address)&_magic);
break;
case 8:
value = Bytes::get_native_u8((address)&_magic);
break;
default:
guarantee(false, "_magic field is an unexpected size");
}
return value == JVMTI_MAGIC;
}
bool
JvmtiEnvBase::use_version_1_0_semantics() {
int major, minor, micro;
JvmtiExport::decode_version_values(_version, &major, &minor, µ);
return major == 1 && minor == 0; // micro version doesn't matter here
}
bool
JvmtiEnvBase::use_version_1_1_semantics() {
int major, minor, micro;
JvmtiExport::decode_version_values(_version, &major, &minor, µ);
return major == 1 && minor == 1; // micro version doesn't matter here
}
bool
JvmtiEnvBase::use_version_1_2_semantics() {
int major, minor, micro;
JvmtiExport::decode_version_values(_version, &major, &minor, µ);
return major == 1 && minor == 2; // micro version doesn't matter here
}
JvmtiEnvBase::JvmtiEnvBase(jint version) : _env_event_enable() {
_version = version;
_env_local_storage = NULL;
_tag_map = NULL;
_native_method_prefix_count = 0;
_native_method_prefixes = NULL;
_next = NULL;
_class_file_load_hook_ever_enabled = false;
// Moot since ClassFileLoadHook not yet enabled.
// But "true" will give a more predictable ClassFileLoadHook behavior
// for environment creation during ClassFileLoadHook.
_is_retransformable = true;
// all callbacks initially NULL
memset(&_event_callbacks,0,sizeof(jvmtiEventCallbacks));
// all capabilities initially off
memset(&_current_capabilities, 0, sizeof(_current_capabilities));
// all prohibited capabilities initially off
memset(&_prohibited_capabilities, 0, sizeof(_prohibited_capabilities));
_magic = JVMTI_MAGIC;
JvmtiEventController::env_initialize((JvmtiEnv*)this);
#ifdef JVMTI_TRACE
_jvmti_external.functions = TraceJVMTI != NULL ? &jvmtiTrace_Interface : &jvmti_Interface;
#else
_jvmti_external.functions = &jvmti_Interface;
#endif
}
void
JvmtiEnvBase::dispose() {
#ifdef JVMTI_TRACE
JvmtiTrace::shutdown();
#endif
// Dispose of event info and let the event controller call us back
// in a locked state (env_dispose, below)
JvmtiEventController::env_dispose(this);
}
void
JvmtiEnvBase::env_dispose() {
assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
// We have been entered with all events disabled on this environment.
// A race to re-enable events (by setting callbacks) is prevented by
// checking for a valid environment when setting callbacks (while
// holding the JvmtiThreadState_lock).
// Mark as invalid.
_magic = DISPOSED_MAGIC;
// Relinquish all capabilities.
jvmtiCapabilities *caps = get_capabilities();
JvmtiManageCapabilities::relinquish_capabilities(caps, caps, caps);
// Same situation as with events (see above)
set_native_method_prefixes(0, NULL);
JvmtiTagMap* tag_map_to_deallocate = _tag_map;
set_tag_map(NULL);
// A tag map can be big, deallocate it now
if (tag_map_to_deallocate != NULL) {
delete tag_map_to_deallocate;
}
_needs_clean_up = true;
}
JvmtiEnvBase::~JvmtiEnvBase() {
assert(SafepointSynchronize::is_at_safepoint(), "sanity check");
// There is a small window of time during which the tag map of a
// disposed environment could have been reallocated.
// Make sure it is gone.
JvmtiTagMap* tag_map_to_deallocate = _tag_map;
set_tag_map(NULL);
// A tag map can be big, deallocate it now
if (tag_map_to_deallocate != NULL) {
delete tag_map_to_deallocate;
}
_magic = BAD_MAGIC;
}
void
JvmtiEnvBase::periodic_clean_up() {
assert(SafepointSynchronize::is_at_safepoint(), "sanity check");
// JvmtiEnvBase reference is saved in JvmtiEnvThreadState. So
// clean up JvmtiThreadState before deleting JvmtiEnv pointer.
JvmtiThreadState::periodic_clean_up();
// Unlink all invalid environments from the list of environments
// and deallocate them
JvmtiEnvIterator it;
JvmtiEnvBase* previous_env = NULL;
JvmtiEnvBase* env = it.first();
while (env != NULL) {
if (env->is_valid()) {
previous_env = env;
env = it.next(env);
} else {
// This one isn't valid, remove it from the list and deallocate it
JvmtiEnvBase* defunct_env = env;
env = it.next(env);
if (previous_env == NULL) {
_head_environment = env;
} else {
previous_env->set_next_environment(env);
}
delete defunct_env;
}
}
}
void
JvmtiEnvBase::check_for_periodic_clean_up() {
assert(SafepointSynchronize::is_at_safepoint(), "sanity check");
class ThreadInsideIterationClosure: public ThreadClosure {
private:
bool _inside;
public:
ThreadInsideIterationClosure() : _inside(false) {};
void do_thread(Thread* thread) {
_inside |= thread->is_inside_jvmti_env_iteration();
}
bool is_inside_jvmti_env_iteration() {
return _inside;
}
};
if (_needs_clean_up) {
// Check if we are currently iterating environment,
// deallocation should not occur if we are
ThreadInsideIterationClosure tiic;
Threads::threads_do(&tiic);
if (!tiic.is_inside_jvmti_env_iteration() &&
!is_inside_dying_thread_env_iteration()) {
_needs_clean_up = false;
JvmtiEnvBase::periodic_clean_up();
}
}
}
void
JvmtiEnvBase::record_first_time_class_file_load_hook_enabled() {
assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(),
"sanity check");
if (!_class_file_load_hook_ever_enabled) {
_class_file_load_hook_ever_enabled = true;
if (get_capabilities()->can_retransform_classes) {
_is_retransformable = true;
} else {
_is_retransformable = false;
// cannot add retransform capability after ClassFileLoadHook has been enabled
get_prohibited_capabilities()->can_retransform_classes = 1;
}
}
}
void
JvmtiEnvBase::record_class_file_load_hook_enabled() {
if (!_class_file_load_hook_ever_enabled) {
if (Threads::number_of_threads() == 0) {
record_first_time_class_file_load_hook_enabled();
} else {
MutexLocker mu(JvmtiThreadState_lock);
record_first_time_class_file_load_hook_enabled();
}
}
}
jvmtiError
JvmtiEnvBase::set_native_method_prefixes(jint prefix_count, char** prefixes) {
assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(),
"sanity check");
int old_prefix_count = get_native_method_prefix_count();
char **old_prefixes = get_native_method_prefixes();
// allocate and install the new prefixex
if (prefix_count == 0 || !is_valid()) {
_native_method_prefix_count = 0;
_native_method_prefixes = NULL;
} else {
// there are prefixes, allocate an array to hold them, and fill it
char** new_prefixes = (char**)os::malloc((prefix_count) * sizeof(char*), mtInternal);
if (new_prefixes == NULL) {
return JVMTI_ERROR_OUT_OF_MEMORY;
}
for (int i = 0; i < prefix_count; i++) {
char* prefix = prefixes[i];
if (prefix == NULL) {
for (int j = 0; j < (i-1); j++) {
os::free(new_prefixes[j]);
}
os::free(new_prefixes);
return JVMTI_ERROR_NULL_POINTER;
}
prefix = os::strdup(prefixes[i]);
if (prefix == NULL) {
for (int j = 0; j < (i-1); j++) {
os::free(new_prefixes[j]);
}
os::free(new_prefixes);
return JVMTI_ERROR_OUT_OF_MEMORY;
}
new_prefixes[i] = prefix;
}
_native_method_prefix_count = prefix_count;
_native_method_prefixes = new_prefixes;
}
// now that we know the new prefixes have been successfully installed we can
// safely remove the old ones
if (old_prefix_count != 0) {
for (int i = 0; i < old_prefix_count; i++) {
os::free(old_prefixes[i]);
}
os::free(old_prefixes);
}
return JVMTI_ERROR_NONE;
}
// Collect all the prefixes which have been set in any JVM TI environments
// by the SetNativeMethodPrefix(es) functions. Be sure to maintain the
// order of environments and the order of prefixes within each environment.
// Return in a resource allocated array.
char**
JvmtiEnvBase::get_all_native_method_prefixes(int* count_ptr) {
assert(Threads::number_of_threads() == 0 ||
SafepointSynchronize::is_at_safepoint() ||
JvmtiThreadState_lock->is_locked(),
"sanity check");
int total_count = 0;
GrowableArray<char*>* prefix_array =new GrowableArray<char*>(5);
JvmtiEnvIterator it;
for (JvmtiEnvBase* env = it.first(); env != NULL; env = it.next(env)) {
int prefix_count = env->get_native_method_prefix_count();
char** prefixes = env->get_native_method_prefixes();
for (int j = 0; j < prefix_count; j++) {
// retrieve a prefix and so that it is safe against asynchronous changes
// copy it into the resource area
char* prefix = prefixes[j];
char* prefix_copy = NEW_RESOURCE_ARRAY(char, strlen(prefix)+1);
strcpy(prefix_copy, prefix);
prefix_array->at_put_grow(total_count++, prefix_copy);
}
}
char** all_prefixes = NEW_RESOURCE_ARRAY(char*, total_count);
char** p = all_prefixes;
for (int i = 0; i < total_count; ++i) {
*p++ = prefix_array->at(i);
}
*count_ptr = total_count;
return all_prefixes;
}
void
JvmtiEnvBase::set_event_callbacks(const jvmtiEventCallbacks* callbacks,
jint size_of_callbacks) {
assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
size_t byte_cnt = sizeof(jvmtiEventCallbacks);
// clear in either case to be sure we got any gap between sizes
memset(&_event_callbacks, 0, byte_cnt);
// Now that JvmtiThreadState_lock is held, prevent a possible race condition where events
// are re-enabled by a call to set event callbacks where the DisposeEnvironment
// occurs after the boiler-plate environment check and before the lock is acquired.
if (callbacks != NULL && is_valid()) {
if (size_of_callbacks < (jint)byte_cnt) {
byte_cnt = size_of_callbacks;
}
memcpy(&_event_callbacks, callbacks, byte_cnt);
}
}
// In the fullness of time, all users of the method should instead
// directly use allocate, besides being cleaner and faster, this will
// mean much better out of memory handling
unsigned char *
JvmtiEnvBase::jvmtiMalloc(jlong size) {
unsigned char* mem = NULL;
jvmtiError result = allocate(size, &mem);
assert(result == JVMTI_ERROR_NONE, "Allocate failed");
return mem;
}
// Handle management
jobject JvmtiEnvBase::jni_reference(Handle hndl) {
return JNIHandles::make_local(hndl());
}
jobject JvmtiEnvBase::jni_reference(JavaThread *thread, Handle hndl) {
return JNIHandles::make_local(thread, hndl());
}
void JvmtiEnvBase::destroy_jni_reference(jobject jobj) {
JNIHandles::destroy_local(jobj);
}
void JvmtiEnvBase::destroy_jni_reference(JavaThread *thread, jobject jobj) {
JNIHandles::destroy_local(jobj); // thread is unused.
}
//
// Threads
//
jobject *
JvmtiEnvBase::new_jobjectArray(int length, Handle *handles) {
if (length == 0) {
return NULL;
}
jobject *objArray = (jobject *) jvmtiMalloc(sizeof(jobject) * length);
NULL_CHECK(objArray, NULL);
for (int i=0; i<length; i++) {
objArray[i] = jni_reference(handles[i]);
}
return objArray;
}
jthread *
JvmtiEnvBase::new_jthreadArray(int length, Handle *handles) {
return (jthread *) new_jobjectArray(length,handles);
}
jthreadGroup *
JvmtiEnvBase::new_jthreadGroupArray(int length, Handle *handles) {
return (jthreadGroup *) new_jobjectArray(length,handles);
}
// return the vframe on the specified thread and depth, NULL if no such frame
vframe*
JvmtiEnvBase::vframeFor(JavaThread* java_thread, jint depth) {
if (!java_thread->has_last_Java_frame()) {
return NULL;
}
RegisterMap reg_map(java_thread);
vframe *vf = java_thread->last_java_vframe(®_map);
int d = 0;
while ((vf != NULL) && (d < depth)) {
vf = vf->java_sender();
d++;
}
return vf;
}
//
// utilities: JNI objects
//
jclass
JvmtiEnvBase::get_jni_class_non_null(Klass* k) {
assert(k != NULL, "k != NULL");
Thread *thread = Thread::current();
return (jclass)jni_reference(Handle(thread, k->java_mirror()));
}
//
// Field Information
//
bool
JvmtiEnvBase::get_field_descriptor(Klass* k, jfieldID field, fieldDescriptor* fd) {
if (!jfieldIDWorkaround::is_valid_jfieldID(k, field)) {
return false;
}
bool found = false;
if (jfieldIDWorkaround::is_static_jfieldID(field)) {
JNIid* id = jfieldIDWorkaround::from_static_jfieldID(field);
found = id->find_local_field(fd);
} else {
// Non-static field. The fieldID is really the offset of the field within the object.
int offset = jfieldIDWorkaround::from_instance_jfieldID(k, field);
found = InstanceKlass::cast(k)->find_field_from_offset(offset, false, fd);
}
return found;
}
//
// Object Monitor Information
//
//
// Count the number of objects for a lightweight monitor. The hobj
// parameter is object that owns the monitor so this routine will
// count the number of times the same object was locked by frames
// in java_thread.
//
jint
JvmtiEnvBase::count_locked_objects(JavaThread *java_thread, Handle hobj) {
jint ret = 0;
if (!java_thread->has_last_Java_frame()) {
return ret; // no Java frames so no monitors
}
ResourceMark rm;
HandleMark hm;
RegisterMap reg_map(java_thread);
for(javaVFrame *jvf=java_thread->last_java_vframe(®_map); jvf != NULL;
jvf = jvf->java_sender()) {
GrowableArray<MonitorInfo*>* mons = jvf->monitors();
if (!mons->is_empty()) {
for (int i = 0; i < mons->length(); i++) {
MonitorInfo *mi = mons->at(i);
if (mi->owner_is_scalar_replaced()) continue;
// see if owner of the monitor is our object
if (mi->owner() != NULL && mi->owner() == hobj()) {
ret++;
}
}
}
}
return ret;
}
jvmtiError
JvmtiEnvBase::get_current_contended_monitor(JavaThread *calling_thread, JavaThread *java_thread, jobject *monitor_ptr) {
JavaThread *current_jt = JavaThread::current();
assert(current_jt == java_thread ||
current_jt == java_thread->active_handshaker(),
"call by myself or at direct handshake");
oop obj = NULL;
// The ObjectMonitor* can't be async deflated since we are either
// at a safepoint or the calling thread is operating on itself so
// it cannot leave the underlying wait()/enter() call.
ObjectMonitor *mon = java_thread->current_waiting_monitor();
if (mon == NULL) {
// thread is not doing an Object.wait() call
mon = java_thread->current_pending_monitor();
if (mon != NULL) {
// The thread is trying to enter() an ObjectMonitor.
obj = (oop)mon->object();
assert(obj != NULL, "ObjectMonitor should have a valid object!");
}
// implied else: no contended ObjectMonitor
} else {
// thread is doing an Object.wait() call
obj = (oop)mon->object();
assert(obj != NULL, "Object.wait() should have an object");
}
if (obj == NULL) {
*monitor_ptr = NULL;
} else {
HandleMark hm;
Handle hobj(current_jt, obj);
*monitor_ptr = jni_reference(calling_thread, hobj);
}
return JVMTI_ERROR_NONE;
}
jvmtiError
JvmtiEnvBase::get_owned_monitors(JavaThread *calling_thread, JavaThread* java_thread,
GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list) {
jvmtiError err = JVMTI_ERROR_NONE;
JavaThread *current_jt = JavaThread::current();
assert(current_jt == java_thread ||
current_jt == java_thread->active_handshaker(),
"call by myself or at direct handshake");
if (java_thread->has_last_Java_frame()) {
ResourceMark rm;
HandleMark hm;
RegisterMap reg_map(java_thread);
int depth = 0;
for (javaVFrame *jvf = java_thread->last_java_vframe(®_map); jvf != NULL;
jvf = jvf->java_sender()) {
if (MaxJavaStackTraceDepth == 0 || depth++ < MaxJavaStackTraceDepth) { // check for stack too deep
// add locked objects for this frame into list
err = get_locked_objects_in_frame(calling_thread, java_thread, jvf, owned_monitors_list, depth-1);
if (err != JVMTI_ERROR_NONE) {
return err;
}
}
}
}
// Get off stack monitors. (e.g. acquired via jni MonitorEnter).
JvmtiMonitorClosure jmc(java_thread, calling_thread, owned_monitors_list, this);
ObjectSynchronizer::monitors_iterate(&jmc);
err = jmc.error();
return err;
}
// Save JNI local handles for any objects that this frame owns.
jvmtiError
JvmtiEnvBase::get_locked_objects_in_frame(JavaThread* calling_thread, JavaThread* java_thread,
javaVFrame *jvf, GrowableArray<jvmtiMonitorStackDepthInfo*>* owned_monitors_list, jint stack_depth) {
jvmtiError err = JVMTI_ERROR_NONE;
ResourceMark rm;
GrowableArray<MonitorInfo*>* mons = jvf->monitors();
if (mons->is_empty()) {
return err; // this javaVFrame holds no monitors
}
HandleMark hm;
oop wait_obj = NULL;
{
// The ObjectMonitor* can't be async deflated since we are either
// at a safepoint or the calling thread is operating on itself so
// it cannot leave the underlying wait() call.
// Save object of current wait() call (if any) for later comparison.
ObjectMonitor *mon = java_thread->current_waiting_monitor();
if (mon != NULL) {
wait_obj = (oop)mon->object();
}
}
oop pending_obj = NULL;
{
// The ObjectMonitor* can't be async deflated since we are either
// at a safepoint or the calling thread is operating on itself so
// it cannot leave the underlying enter() call.
// Save object of current enter() call (if any) for later comparison.
ObjectMonitor *mon = java_thread->current_pending_monitor();
if (mon != NULL) {
pending_obj = (oop)mon->object();
}
}
for (int i = 0; i < mons->length(); i++) {
MonitorInfo *mi = mons->at(i);
if (mi->owner_is_scalar_replaced()) continue;
oop obj = mi->owner();
if (obj == NULL) {
// this monitor doesn't have an owning object so skip it
continue;
}
if (wait_obj == obj) {
// the thread is waiting on this monitor so it isn't really owned
continue;
}
if (pending_obj == obj) {
// the thread is pending on this monitor so it isn't really owned
continue;
}
if (owned_monitors_list->length() > 0) {
// Our list has at least one object on it so we have to check
// for recursive object locking
bool found = false;
for (int j = 0; j < owned_monitors_list->length(); j++) {
jobject jobj = ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(j))->monitor;
oop check = JNIHandles::resolve(jobj);
if (check == obj) {
found = true; // we found the object
break;
}
}
if (found) {
// already have this object so don't include it
continue;
}
}
// add the owning object to our list
jvmtiMonitorStackDepthInfo *jmsdi;
err = allocate(sizeof(jvmtiMonitorStackDepthInfo), (unsigned char **)&jmsdi);
if (err != JVMTI_ERROR_NONE) {
return err;
}
Handle hobj(Thread::current(), obj);
jmsdi->monitor = jni_reference(calling_thread, hobj);
jmsdi->stack_depth = stack_depth;
owned_monitors_list->append(jmsdi);
}
return err;
}
jvmtiError
JvmtiEnvBase::get_stack_trace(JavaThread *java_thread,
jint start_depth, jint max_count,
jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
#ifdef ASSERT
uint32_t debug_bits = 0;
#endif
assert((SafepointSynchronize::is_at_safepoint() ||
java_thread->is_thread_fully_suspended(false, &debug_bits)),
"at safepoint or target thread is suspended");
int count = 0;
if (java_thread->has_last_Java_frame()) {
RegisterMap reg_map(java_thread);
Thread* current_thread = Thread::current();
ResourceMark rm(current_thread);
javaVFrame *jvf = java_thread->last_java_vframe(®_map);
HandleMark hm(current_thread);
if (start_depth != 0) {
if (start_depth > 0) {
for (int j = 0; j < start_depth && jvf != NULL; j++) {
jvf = jvf->java_sender();
}
if (jvf == NULL) {
// start_depth is deeper than the stack depth
return JVMTI_ERROR_ILLEGAL_ARGUMENT;
}
} else { // start_depth < 0
// we are referencing the starting depth based on the oldest
// part of the stack.
// optimize to limit the number of times that java_sender() is called
javaVFrame *jvf_cursor = jvf;
javaVFrame *jvf_prev = NULL;
javaVFrame *jvf_prev_prev = NULL;
int j = 0;
while (jvf_cursor != NULL) {
jvf_prev_prev = jvf_prev;
jvf_prev = jvf_cursor;
for (j = 0; j > start_depth && jvf_cursor != NULL; j--) {
jvf_cursor = jvf_cursor->java_sender();
}
}
if (j == start_depth) {
// previous pointer is exactly where we want to start
jvf = jvf_prev;
} else {
// we need to back up further to get to the right place
if (jvf_prev_prev == NULL) {
// the -start_depth is greater than the stack depth
return JVMTI_ERROR_ILLEGAL_ARGUMENT;
}
// j now is the number of frames on the stack starting with
// jvf_prev, we start from jvf_prev_prev and move older on
// the stack that many, the result is -start_depth frames
// remaining.
jvf = jvf_prev_prev;
for (; j < 0; j++) {
jvf = jvf->java_sender();
}
}
}
}
for (; count < max_count && jvf != NULL; count++) {
frame_buffer[count].method = jvf->method()->jmethod_id();
frame_buffer[count].location = (jvf->method()->is_native() ? -1 : jvf->bci());
jvf = jvf->java_sender();
}
} else {
if (start_depth != 0) {
// no frames and there is a starting depth
return JVMTI_ERROR_ILLEGAL_ARGUMENT;
}
}
*count_ptr = count;
return JVMTI_ERROR_NONE;
}
jvmtiError
JvmtiEnvBase::get_frame_count(JvmtiThreadState *state, jint *count_ptr) {
assert((state != NULL),
"JavaThread should create JvmtiThreadState before calling this method");
*count_ptr = state->count_frames();
return JVMTI_ERROR_NONE;
}
jvmtiError
JvmtiEnvBase::get_frame_location(JavaThread *java_thread, jint depth,
jmethodID* method_ptr, jlocation* location_ptr) {
#ifdef ASSERT
uint32_t debug_bits = 0;
#endif
assert((SafepointSynchronize::is_at_safepoint() ||
java_thread->is_thread_fully_suspended(false, &debug_bits)),
"at safepoint or target thread is suspended");
Thread* current_thread = Thread::current();
ResourceMark rm(current_thread);
vframe *vf = vframeFor(java_thread, depth);
if (vf == NULL) {
return JVMTI_ERROR_NO_MORE_FRAMES;
}
// vframeFor should return a java frame. If it doesn't
// it means we've got an internal error and we return the
// error in product mode. In debug mode we will instead
// attempt to cast the vframe to a javaVFrame and will
// cause an assertion/crash to allow further diagnosis.
#ifdef PRODUCT
if (!vf->is_java_frame()) {
return JVMTI_ERROR_INTERNAL;
}
#endif
HandleMark hm(current_thread);
javaVFrame *jvf = javaVFrame::cast(vf);
Method* method = jvf->method();
if (method->is_native()) {
*location_ptr = -1;
} else {
*location_ptr = jvf->bci();
}
*method_ptr = method->jmethod_id();
return JVMTI_ERROR_NONE;
}
jvmtiError
JvmtiEnvBase::get_object_monitor_usage(JavaThread* calling_thread, jobject object, jvmtiMonitorUsage* info_ptr) {
HandleMark hm;
Handle hobj;
Thread* current_thread = Thread::current();
bool at_safepoint = SafepointSynchronize::is_at_safepoint();
// Check arguments
{
oop mirror = JNIHandles::resolve_external_guard(object);
NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
NULL_CHECK(info_ptr, JVMTI_ERROR_NULL_POINTER);
hobj = Handle(current_thread, mirror);
}
JavaThread *owning_thread = NULL;
ObjectMonitor *mon = NULL;
jvmtiMonitorUsage ret = {
NULL, 0, 0, NULL, 0, NULL
};
uint32_t debug_bits = 0;
// first derive the object's owner and entry_count (if any)
{
// Revoke any biases before querying the mark word
if (at_safepoint) {
BiasedLocking::revoke_at_safepoint(hobj);
} else {
BiasedLocking::revoke(hobj, calling_thread);
}
address owner = NULL;
{
markWord mark = hobj()->mark();
if (!mark.has_monitor()) {
// this object has a lightweight monitor
if (mark.has_locker()) {
owner = (address)mark.locker(); // save the address of the Lock word
}
// implied else: no owner
} else {
// this object has a heavyweight monitor
mon = mark.monitor();
// The owner field of a heavyweight monitor may be NULL for no
// owner, a JavaThread * or it may still be the address of the
// Lock word in a JavaThread's stack. A monitor can be inflated
// by a non-owning JavaThread, but only the owning JavaThread
// can change the owner field from the Lock word to the
// JavaThread * and it may not have done that yet.
owner = (address)mon->owner();
}
}
if (owner != NULL) {
// Use current thread since function can be called from a
// JavaThread or the VMThread.
ThreadsListHandle tlh;
// This monitor is owned so we have to find the owning JavaThread.