-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy pathjvmtiEnv.cpp
3437 lines (2884 loc) · 112 KB
/
jvmtiEnv.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, 2016, 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/classLoaderExt.hpp"
#include "classfile/systemDictionary.hpp"
#include "classfile/vmSymbols.hpp"
#include "interpreter/bytecodeStream.hpp"
#include "interpreter/interpreter.hpp"
#include "jvmtifiles/jvmtiEnv.hpp"
#include "memory/resourceArea.hpp"
#include "memory/universe.inline.hpp"
#include "oops/instanceKlass.hpp"
#include "prims/jniCheck.hpp"
#include "prims/jvm_misc.hpp"
#include "prims/jvmtiAgentThread.hpp"
#include "prims/jvmtiClassFileReconstituter.hpp"
#include "prims/jvmtiCodeBlobEvents.hpp"
#include "prims/jvmtiExtensions.hpp"
#include "prims/jvmtiGetLoadedClasses.hpp"
#include "prims/jvmtiImpl.hpp"
#include "prims/jvmtiManageCapabilities.hpp"
#include "prims/jvmtiRawMonitor.hpp"
#include "prims/jvmtiRedefineClasses.hpp"
#include "prims/jvmtiTagMap.hpp"
#include "prims/jvmtiThreadState.inline.hpp"
#include "prims/jvmtiUtil.hpp"
#include "runtime/arguments.hpp"
#include "runtime/deoptimization.hpp"
#include "runtime/interfaceSupport.hpp"
#include "runtime/javaCalls.hpp"
#include "runtime/jfieldIDWorkaround.hpp"
#include "runtime/osThread.hpp"
#include "runtime/reflectionUtils.hpp"
#include "runtime/signature.hpp"
#include "runtime/thread.inline.hpp"
#include "runtime/vframe.hpp"
#include "runtime/vmThread.hpp"
#include "services/threadService.hpp"
#include "utilities/exceptions.hpp"
#include "utilities/preserveException.hpp"
#define FIXLATER 0 // REMOVE this when completed.
// FIXLATER: hook into JvmtiTrace
#define TraceJVMTICalls false
JvmtiEnv::JvmtiEnv(jint version) : JvmtiEnvBase(version) {
}
JvmtiEnv::~JvmtiEnv() {
}
JvmtiEnv*
JvmtiEnv::create_a_jvmti(jint version) {
return new JvmtiEnv(version);
}
// VM operation class to copy jni function table at safepoint.
// More than one java threads or jvmti agents may be reading/
// modifying jni function tables. To reduce the risk of bad
// interaction b/w these threads it is copied at safepoint.
class VM_JNIFunctionTableCopier : public VM_Operation {
private:
const struct JNINativeInterface_ *_function_table;
public:
VM_JNIFunctionTableCopier(const struct JNINativeInterface_ *func_tbl) {
_function_table = func_tbl;
};
VMOp_Type type() const { return VMOp_JNIFunctionTableCopier; }
void doit() {
copy_jni_function_table(_function_table);
};
};
//
// Do not change the "prefix" marker below, everything above it is copied
// unchanged into the filled stub, everything below is controlled by the
// stub filler (only method bodies are carried forward, and then only for
// functionality still in the spec).
//
// end file prefix
//
// Memory Management functions
//
// mem_ptr - pre-checked for NULL
jvmtiError
JvmtiEnv::Allocate(jlong size, unsigned char** mem_ptr) {
return allocate(size, mem_ptr);
} /* end Allocate */
// mem - NULL is a valid value, must be checked
jvmtiError
JvmtiEnv::Deallocate(unsigned char* mem) {
return deallocate(mem);
} /* end Deallocate */
// Threads_lock NOT held, java_thread not protected by lock
// java_thread - pre-checked
// data - NULL is a valid value, must be checked
jvmtiError
JvmtiEnv::SetThreadLocalStorage(JavaThread* java_thread, const void* data) {
JvmtiThreadState* state = java_thread->jvmti_thread_state();
if (state == NULL) {
if (data == NULL) {
// leaving state unset same as data set to NULL
return JVMTI_ERROR_NONE;
}
// otherwise, create the state
state = JvmtiThreadState::state_for(java_thread);
if (state == NULL) {
return JVMTI_ERROR_THREAD_NOT_ALIVE;
}
}
state->env_thread_state(this)->set_agent_thread_local_storage_data((void*)data);
return JVMTI_ERROR_NONE;
} /* end SetThreadLocalStorage */
// Threads_lock NOT held
// thread - NOT pre-checked
// data_ptr - pre-checked for NULL
jvmtiError
JvmtiEnv::GetThreadLocalStorage(jthread thread, void** data_ptr) {
JavaThread* current_thread = JavaThread::current();
if (thread == NULL) {
JvmtiThreadState* state = current_thread->jvmti_thread_state();
*data_ptr = (state == NULL) ? NULL :
state->env_thread_state(this)->get_agent_thread_local_storage_data();
} else {
// jvmti_GetThreadLocalStorage is "in native" and doesn't transition
// the thread to _thread_in_vm. However, when the TLS for a thread
// other than the current thread is required we need to transition
// from native so as to resolve the jthread.
ThreadInVMfromNative __tiv(current_thread);
VM_ENTRY_BASE(jvmtiError, JvmtiEnv::GetThreadLocalStorage , current_thread)
debug_only(VMNativeEntryWrapper __vew;)
oop thread_oop = JNIHandles::resolve_external_guard(thread);
if (thread_oop == NULL) {
return JVMTI_ERROR_INVALID_THREAD;
}
if (!thread_oop->is_a(SystemDictionary::Thread_klass())) {
return JVMTI_ERROR_INVALID_THREAD;
}
JavaThread* java_thread = java_lang_Thread::thread(thread_oop);
if (java_thread == NULL) {
return JVMTI_ERROR_THREAD_NOT_ALIVE;
}
JvmtiThreadState* state = java_thread->jvmti_thread_state();
*data_ptr = (state == NULL) ? NULL :
state->env_thread_state(this)->get_agent_thread_local_storage_data();
}
return JVMTI_ERROR_NONE;
} /* end GetThreadLocalStorage */
//
// Class functions
//
// class_count_ptr - pre-checked for NULL
// classes_ptr - pre-checked for NULL
jvmtiError
JvmtiEnv::GetLoadedClasses(jint* class_count_ptr, jclass** classes_ptr) {
return JvmtiGetLoadedClasses::getLoadedClasses(this, class_count_ptr, classes_ptr);
} /* end GetLoadedClasses */
// initiating_loader - NULL is a valid value, must be checked
// class_count_ptr - pre-checked for NULL
// classes_ptr - pre-checked for NULL
jvmtiError
JvmtiEnv::GetClassLoaderClasses(jobject initiating_loader, jint* class_count_ptr, jclass** classes_ptr) {
return JvmtiGetLoadedClasses::getClassLoaderClasses(this, initiating_loader,
class_count_ptr, classes_ptr);
} /* end GetClassLoaderClasses */
// k_mirror - may be primitive, this must be checked
// is_modifiable_class_ptr - pre-checked for NULL
jvmtiError
JvmtiEnv::IsModifiableClass(oop k_mirror, jboolean* is_modifiable_class_ptr) {
*is_modifiable_class_ptr = VM_RedefineClasses::is_modifiable_class(k_mirror)?
JNI_TRUE : JNI_FALSE;
return JVMTI_ERROR_NONE;
} /* end IsModifiableClass */
// class_count - pre-checked to be greater than or equal to 0
// classes - pre-checked for NULL
jvmtiError
JvmtiEnv::RetransformClasses(jint class_count, const jclass* classes) {
//TODO: add locking
int index;
JavaThread* current_thread = JavaThread::current();
ResourceMark rm(current_thread);
jvmtiClassDefinition* class_definitions =
NEW_RESOURCE_ARRAY(jvmtiClassDefinition, class_count);
NULL_CHECK(class_definitions, JVMTI_ERROR_OUT_OF_MEMORY);
for (index = 0; index < class_count; index++) {
HandleMark hm(current_thread);
jclass jcls = classes[index];
oop k_mirror = JNIHandles::resolve_external_guard(jcls);
if (k_mirror == NULL) {
return JVMTI_ERROR_INVALID_CLASS;
}
if (!k_mirror->is_a(SystemDictionary::Class_klass())) {
return JVMTI_ERROR_INVALID_CLASS;
}
if (java_lang_Class::is_primitive(k_mirror)) {
return JVMTI_ERROR_UNMODIFIABLE_CLASS;
}
Klass* k_oop = java_lang_Class::as_Klass(k_mirror);
KlassHandle klass(current_thread, k_oop);
jint status = klass->jvmti_class_status();
if (status & (JVMTI_CLASS_STATUS_ERROR)) {
return JVMTI_ERROR_INVALID_CLASS;
}
if (status & (JVMTI_CLASS_STATUS_ARRAY)) {
return JVMTI_ERROR_UNMODIFIABLE_CLASS;
}
instanceKlassHandle ikh(current_thread, k_oop);
if (ikh->get_cached_class_file_bytes() == NULL) {
// Not cached, we need to reconstitute the class file from the
// VM representation. We don't attach the reconstituted class
// bytes to the InstanceKlass here because they have not been
// validated and we're not at a safepoint.
constantPoolHandle constants(current_thread, ikh->constants());
MonitorLockerEx ml(constants->lock()); // lock constant pool while we query it
JvmtiClassFileReconstituter reconstituter(ikh);
if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
return reconstituter.get_error();
}
class_definitions[index].class_byte_count = (jint)reconstituter.class_file_size();
class_definitions[index].class_bytes = (unsigned char*)
reconstituter.class_file_bytes();
} else {
// it is cached, get it from the cache
class_definitions[index].class_byte_count = ikh->get_cached_class_file_len();
class_definitions[index].class_bytes = ikh->get_cached_class_file_bytes();
}
class_definitions[index].klass = jcls;
}
VM_RedefineClasses op(class_count, class_definitions, jvmti_class_load_kind_retransform);
VMThread::execute(&op);
return (op.check_error());
} /* end RetransformClasses */
// class_count - pre-checked to be greater than or equal to 0
// class_definitions - pre-checked for NULL
jvmtiError
JvmtiEnv::RedefineClasses(jint class_count, const jvmtiClassDefinition* class_definitions) {
//TODO: add locking
VM_RedefineClasses op(class_count, class_definitions, jvmti_class_load_kind_redefine);
VMThread::execute(&op);
return (op.check_error());
} /* end RedefineClasses */
//
// Object functions
//
// size_ptr - pre-checked for NULL
jvmtiError
JvmtiEnv::GetObjectSize(jobject object, jlong* size_ptr) {
oop mirror = JNIHandles::resolve_external_guard(object);
NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
if (mirror->klass() == SystemDictionary::Class_klass() &&
!java_lang_Class::is_primitive(mirror)) {
Klass* k = java_lang_Class::as_Klass(mirror);
assert(k != NULL, "class for non-primitive mirror must exist");
*size_ptr = (jlong)k->size() * wordSize;
} else {
*size_ptr = (jlong)mirror->size() * wordSize;
}
return JVMTI_ERROR_NONE;
} /* end GetObjectSize */
//
// Method functions
//
// prefix - NULL is a valid value, must be checked
jvmtiError
JvmtiEnv::SetNativeMethodPrefix(const char* prefix) {
return prefix == NULL?
SetNativeMethodPrefixes(0, NULL) :
SetNativeMethodPrefixes(1, (char**)&prefix);
} /* end SetNativeMethodPrefix */
// prefix_count - pre-checked to be greater than or equal to 0
// prefixes - pre-checked for NULL
jvmtiError
JvmtiEnv::SetNativeMethodPrefixes(jint prefix_count, char** prefixes) {
// Have to grab JVMTI thread state lock to be sure that some thread
// isn't accessing the prefixes at the same time we are setting them.
// No locks during VM bring-up.
if (Threads::number_of_threads() == 0) {
return set_native_method_prefixes(prefix_count, prefixes);
} else {
MutexLocker mu(JvmtiThreadState_lock);
return set_native_method_prefixes(prefix_count, prefixes);
}
} /* end SetNativeMethodPrefixes */
//
// Event Management functions
//
// callbacks - NULL is a valid value, must be checked
// size_of_callbacks - pre-checked to be greater than or equal to 0
jvmtiError
JvmtiEnv::SetEventCallbacks(const jvmtiEventCallbacks* callbacks, jint size_of_callbacks) {
JvmtiEventController::set_event_callbacks(this, callbacks, size_of_callbacks);
return JVMTI_ERROR_NONE;
} /* end SetEventCallbacks */
// event_thread - NULL is a valid value, must be checked
jvmtiError
JvmtiEnv::SetEventNotificationMode(jvmtiEventMode mode, jvmtiEvent event_type, jthread event_thread, ...) {
JavaThread* java_thread = NULL;
if (event_thread != NULL) {
oop thread_oop = JNIHandles::resolve_external_guard(event_thread);
if (thread_oop == NULL) {
return JVMTI_ERROR_INVALID_THREAD;
}
if (!thread_oop->is_a(SystemDictionary::Thread_klass())) {
return JVMTI_ERROR_INVALID_THREAD;
}
java_thread = java_lang_Thread::thread(thread_oop);
if (java_thread == NULL) {
return JVMTI_ERROR_THREAD_NOT_ALIVE;
}
}
// event_type must be valid
if (!JvmtiEventController::is_valid_event_type(event_type)) {
return JVMTI_ERROR_INVALID_EVENT_TYPE;
}
// global events cannot be controlled at thread level.
if (java_thread != NULL && JvmtiEventController::is_global_event(event_type)) {
return JVMTI_ERROR_ILLEGAL_ARGUMENT;
}
bool enabled = (mode == JVMTI_ENABLE);
// assure that needed capabilities are present
if (enabled && !JvmtiUtil::has_event_capability(event_type, get_capabilities())) {
return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
}
if (event_type == JVMTI_EVENT_CLASS_FILE_LOAD_HOOK && enabled) {
record_class_file_load_hook_enabled();
}
JvmtiEventController::set_user_enabled(this, java_thread, event_type, enabled);
return JVMTI_ERROR_NONE;
} /* end SetEventNotificationMode */
//
// Capability functions
//
// capabilities_ptr - pre-checked for NULL
jvmtiError
JvmtiEnv::GetPotentialCapabilities(jvmtiCapabilities* capabilities_ptr) {
JvmtiManageCapabilities::get_potential_capabilities(get_capabilities(),
get_prohibited_capabilities(),
capabilities_ptr);
return JVMTI_ERROR_NONE;
} /* end GetPotentialCapabilities */
// capabilities_ptr - pre-checked for NULL
jvmtiError
JvmtiEnv::AddCapabilities(const jvmtiCapabilities* capabilities_ptr) {
return JvmtiManageCapabilities::add_capabilities(get_capabilities(),
get_prohibited_capabilities(),
capabilities_ptr,
get_capabilities());
} /* end AddCapabilities */
// capabilities_ptr - pre-checked for NULL
jvmtiError
JvmtiEnv::RelinquishCapabilities(const jvmtiCapabilities* capabilities_ptr) {
JvmtiManageCapabilities::relinquish_capabilities(get_capabilities(), capabilities_ptr, get_capabilities());
return JVMTI_ERROR_NONE;
} /* end RelinquishCapabilities */
// capabilities_ptr - pre-checked for NULL
jvmtiError
JvmtiEnv::GetCapabilities(jvmtiCapabilities* capabilities_ptr) {
JvmtiManageCapabilities::copy_capabilities(get_capabilities(), capabilities_ptr);
return JVMTI_ERROR_NONE;
} /* end GetCapabilities */
//
// Class Loader Search functions
//
// segment - pre-checked for NULL
jvmtiError
JvmtiEnv::AddToBootstrapClassLoaderSearch(const char* segment) {
jvmtiPhase phase = get_phase();
if (phase == JVMTI_PHASE_ONLOAD) {
Arguments::append_sysclasspath(segment);
return JVMTI_ERROR_NONE;
} else if (use_version_1_0_semantics()) {
// This JvmtiEnv requested version 1.0 semantics and this function
// is only allowed in the ONLOAD phase in version 1.0 so we need to
// return an error here.
return JVMTI_ERROR_WRONG_PHASE;
} else if (phase == JVMTI_PHASE_LIVE) {
// The phase is checked by the wrapper that called this function,
// but this thread could be racing with the thread that is
// terminating the VM so we check one more time.
// create the zip entry
ClassPathZipEntry* zip_entry = ClassLoader::create_class_path_zip_entry(segment);
if (zip_entry == NULL) {
return JVMTI_ERROR_ILLEGAL_ARGUMENT;
}
// lock the loader
Thread* thread = Thread::current();
HandleMark hm;
Handle loader_lock = Handle(thread, SystemDictionary::system_loader_lock());
ObjectLocker ol(loader_lock, thread);
// add the jar file to the bootclasspath
if (TraceClassLoading) {
tty->print_cr("[Opened %s]", zip_entry->name());
}
ClassLoaderExt::append_boot_classpath(zip_entry);
return JVMTI_ERROR_NONE;
} else {
return JVMTI_ERROR_WRONG_PHASE;
}
} /* end AddToBootstrapClassLoaderSearch */
// segment - pre-checked for NULL
jvmtiError
JvmtiEnv::AddToSystemClassLoaderSearch(const char* segment) {
jvmtiPhase phase = get_phase();
if (phase == JVMTI_PHASE_ONLOAD) {
for (SystemProperty* p = Arguments::system_properties(); p != NULL; p = p->next()) {
if (strcmp("java.class.path", p->key()) == 0) {
p->append_value(segment);
break;
}
}
return JVMTI_ERROR_NONE;
} else if (phase == JVMTI_PHASE_LIVE) {
// The phase is checked by the wrapper that called this function,
// but this thread could be racing with the thread that is
// terminating the VM so we check one more time.
HandleMark hm;
// create the zip entry (which will open the zip file and hence
// check that the segment is indeed a zip file).
ClassPathZipEntry* zip_entry = ClassLoader::create_class_path_zip_entry(segment);
if (zip_entry == NULL) {
return JVMTI_ERROR_ILLEGAL_ARGUMENT;
}
delete zip_entry; // no longer needed
// lock the loader
Thread* THREAD = Thread::current();
Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
ObjectLocker ol(loader, THREAD);
// need the path as java.lang.String
Handle path = java_lang_String::create_from_platform_dependent_str(segment, THREAD);
if (HAS_PENDING_EXCEPTION) {
CLEAR_PENDING_EXCEPTION;
return JVMTI_ERROR_INTERNAL;
}
instanceKlassHandle loader_ik(THREAD, loader->klass());
// Invoke the appendToClassPathForInstrumentation method - if the method
// is not found it means the loader doesn't support adding to the class path
// in the live phase.
{
JavaValue res(T_VOID);
JavaCalls::call_special(&res,
loader,
loader_ik,
vmSymbols::appendToClassPathForInstrumentation_name(),
vmSymbols::appendToClassPathForInstrumentation_signature(),
path,
THREAD);
if (HAS_PENDING_EXCEPTION) {
Symbol* ex_name = PENDING_EXCEPTION->klass()->name();
CLEAR_PENDING_EXCEPTION;
if (ex_name == vmSymbols::java_lang_NoSuchMethodError()) {
return JVMTI_ERROR_CLASS_LOADER_UNSUPPORTED;
} else {
return JVMTI_ERROR_INTERNAL;
}
}
}
return JVMTI_ERROR_NONE;
} else {
return JVMTI_ERROR_WRONG_PHASE;
}
} /* end AddToSystemClassLoaderSearch */
//
// General functions
//
// phase_ptr - pre-checked for NULL
jvmtiError
JvmtiEnv::GetPhase(jvmtiPhase* phase_ptr) {
*phase_ptr = get_phase();
return JVMTI_ERROR_NONE;
} /* end GetPhase */
jvmtiError
JvmtiEnv::DisposeEnvironment() {
dispose();
return JVMTI_ERROR_NONE;
} /* end DisposeEnvironment */
// data - NULL is a valid value, must be checked
jvmtiError
JvmtiEnv::SetEnvironmentLocalStorage(const void* data) {
set_env_local_storage(data);
return JVMTI_ERROR_NONE;
} /* end SetEnvironmentLocalStorage */
// data_ptr - pre-checked for NULL
jvmtiError
JvmtiEnv::GetEnvironmentLocalStorage(void** data_ptr) {
*data_ptr = (void*)get_env_local_storage();
return JVMTI_ERROR_NONE;
} /* end GetEnvironmentLocalStorage */
// version_ptr - pre-checked for NULL
jvmtiError
JvmtiEnv::GetVersionNumber(jint* version_ptr) {
*version_ptr = JVMTI_VERSION;
return JVMTI_ERROR_NONE;
} /* end GetVersionNumber */
// name_ptr - pre-checked for NULL
jvmtiError
JvmtiEnv::GetErrorName(jvmtiError error, char** name_ptr) {
if (error < JVMTI_ERROR_NONE || error > JVMTI_ERROR_MAX) {
return JVMTI_ERROR_ILLEGAL_ARGUMENT;
}
const char *name = JvmtiUtil::error_name(error);
if (name == NULL) {
return JVMTI_ERROR_ILLEGAL_ARGUMENT;
}
size_t len = strlen(name) + 1;
jvmtiError err = allocate(len, (unsigned char**)name_ptr);
if (err == JVMTI_ERROR_NONE) {
memcpy(*name_ptr, name, len);
}
return err;
} /* end GetErrorName */
jvmtiError
JvmtiEnv::SetVerboseFlag(jvmtiVerboseFlag flag, jboolean value) {
switch (flag) {
case JVMTI_VERBOSE_OTHER:
// ignore
break;
case JVMTI_VERBOSE_CLASS:
TraceClassLoading = value != 0;
TraceClassUnloading = value != 0;
break;
case JVMTI_VERBOSE_GC:
PrintGC = value != 0;
break;
case JVMTI_VERBOSE_JNI:
PrintJNIResolving = value != 0;
break;
default:
return JVMTI_ERROR_ILLEGAL_ARGUMENT;
};
return JVMTI_ERROR_NONE;
} /* end SetVerboseFlag */
// format_ptr - pre-checked for NULL
jvmtiError
JvmtiEnv::GetJLocationFormat(jvmtiJlocationFormat* format_ptr) {
*format_ptr = JVMTI_JLOCATION_JVMBCI;
return JVMTI_ERROR_NONE;
} /* end GetJLocationFormat */
//
// Thread functions
//
// Threads_lock NOT held
// thread - NOT pre-checked
// thread_state_ptr - pre-checked for NULL
jvmtiError
JvmtiEnv::GetThreadState(jthread thread, jint* thread_state_ptr) {
jint state;
oop thread_oop;
JavaThread* thr;
if (thread == NULL) {
thread_oop = JavaThread::current()->threadObj();
} else {
thread_oop = JNIHandles::resolve_external_guard(thread);
}
if (thread_oop == NULL || !thread_oop->is_a(SystemDictionary::Thread_klass())) {
return JVMTI_ERROR_INVALID_THREAD;
}
// get most state bits
state = (jint)java_lang_Thread::get_thread_status(thread_oop);
// add more state bits
thr = java_lang_Thread::thread(thread_oop);
if (thr != NULL) {
JavaThreadState jts = thr->thread_state();
if (thr->is_being_ext_suspended()) {
state |= JVMTI_THREAD_STATE_SUSPENDED;
}
if (jts == _thread_in_native) {
state |= JVMTI_THREAD_STATE_IN_NATIVE;
}
OSThread* osThread = thr->osthread();
if (osThread != NULL && osThread->interrupted()) {
state |= JVMTI_THREAD_STATE_INTERRUPTED;
}
}
*thread_state_ptr = state;
return JVMTI_ERROR_NONE;
} /* end GetThreadState */
// thread_ptr - pre-checked for NULL
jvmtiError
JvmtiEnv::GetCurrentThread(jthread* thread_ptr) {
JavaThread* current_thread = JavaThread::current();
*thread_ptr = (jthread)JNIHandles::make_local(current_thread, current_thread->threadObj());
return JVMTI_ERROR_NONE;
} /* end GetCurrentThread */
// threads_count_ptr - pre-checked for NULL
// threads_ptr - pre-checked for NULL
jvmtiError
JvmtiEnv::GetAllThreads(jint* threads_count_ptr, jthread** threads_ptr) {
int nthreads = 0;
Handle *thread_objs = NULL;
ResourceMark rm;
HandleMark hm;
// enumerate threads (including agent threads)
ThreadsListEnumerator tle(Thread::current(), true);
nthreads = tle.num_threads();
*threads_count_ptr = nthreads;
if (nthreads == 0) {
*threads_ptr = NULL;
return JVMTI_ERROR_NONE;
}
thread_objs = NEW_RESOURCE_ARRAY(Handle, nthreads);
NULL_CHECK(thread_objs, JVMTI_ERROR_OUT_OF_MEMORY);
for (int i=0; i < nthreads; i++) {
thread_objs[i] = Handle(tle.get_threadObj(i));
}
// have to make global handles outside of Threads_lock
jthread *jthreads = new_jthreadArray(nthreads, thread_objs);
NULL_CHECK(jthreads, JVMTI_ERROR_OUT_OF_MEMORY);
*threads_ptr = jthreads;
return JVMTI_ERROR_NONE;
} /* end GetAllThreads */
// Threads_lock NOT held, java_thread not protected by lock
// java_thread - pre-checked
jvmtiError
JvmtiEnv::SuspendThread(JavaThread* java_thread) {
// don't allow hidden thread suspend request.
if (java_thread->is_hidden_from_external_view()) {
return (JVMTI_ERROR_NONE);
}
{
MutexLockerEx ml(java_thread->SR_lock(), Mutex::_no_safepoint_check_flag);
if (java_thread->is_external_suspend()) {
// don't allow nested external suspend requests.
return (JVMTI_ERROR_THREAD_SUSPENDED);
}
if (java_thread->is_exiting()) { // thread is in the process of exiting
return (JVMTI_ERROR_THREAD_NOT_ALIVE);
}
java_thread->set_external_suspend();
}
if (!JvmtiSuspendControl::suspend(java_thread)) {
// the thread was in the process of exiting
return (JVMTI_ERROR_THREAD_NOT_ALIVE);
}
return JVMTI_ERROR_NONE;
} /* end SuspendThread */
// request_count - pre-checked to be greater than or equal to 0
// request_list - pre-checked for NULL
// results - pre-checked for NULL
jvmtiError
JvmtiEnv::SuspendThreadList(jint request_count, const jthread* request_list, jvmtiError* results) {
int needSafepoint = 0; // > 0 if we need a safepoint
for (int i = 0; i < request_count; i++) {
JavaThread *java_thread = get_JavaThread(request_list[i]);
if (java_thread == NULL) {
results[i] = JVMTI_ERROR_INVALID_THREAD;
continue;
}
// the thread has not yet run or has exited (not on threads list)
if (java_thread->threadObj() == NULL) {
results[i] = JVMTI_ERROR_THREAD_NOT_ALIVE;
continue;
}
if (java_lang_Thread::thread(java_thread->threadObj()) == NULL) {
results[i] = JVMTI_ERROR_THREAD_NOT_ALIVE;
continue;
}
// don't allow hidden thread suspend request.
if (java_thread->is_hidden_from_external_view()) {
results[i] = JVMTI_ERROR_NONE; // indicate successful suspend
continue;
}
{
MutexLockerEx ml(java_thread->SR_lock(), Mutex::_no_safepoint_check_flag);
if (java_thread->is_external_suspend()) {
// don't allow nested external suspend requests.
results[i] = JVMTI_ERROR_THREAD_SUSPENDED;
continue;
}
if (java_thread->is_exiting()) { // thread is in the process of exiting
results[i] = JVMTI_ERROR_THREAD_NOT_ALIVE;
continue;
}
java_thread->set_external_suspend();
}
if (java_thread->thread_state() == _thread_in_native) {
// We need to try and suspend native threads here. Threads in
// other states will self-suspend on their next transition.
if (!JvmtiSuspendControl::suspend(java_thread)) {
// The thread was in the process of exiting. Force another
// safepoint to make sure that this thread transitions.
needSafepoint++;
results[i] = JVMTI_ERROR_THREAD_NOT_ALIVE;
continue;
}
} else {
needSafepoint++;
}
results[i] = JVMTI_ERROR_NONE; // indicate successful suspend
}
if (needSafepoint > 0) {
VM_ForceSafepoint vfs;
VMThread::execute(&vfs);
}
// per-thread suspend results returned via results parameter
return JVMTI_ERROR_NONE;
} /* end SuspendThreadList */
// Threads_lock NOT held, java_thread not protected by lock
// java_thread - pre-checked
jvmtiError
JvmtiEnv::ResumeThread(JavaThread* java_thread) {
// don't allow hidden thread resume request.
if (java_thread->is_hidden_from_external_view()) {
return JVMTI_ERROR_NONE;
}
if (!java_thread->is_being_ext_suspended()) {
return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
}
if (!JvmtiSuspendControl::resume(java_thread)) {
return JVMTI_ERROR_INTERNAL;
}
return JVMTI_ERROR_NONE;
} /* end ResumeThread */
// request_count - pre-checked to be greater than or equal to 0
// request_list - pre-checked for NULL
// results - pre-checked for NULL
jvmtiError
JvmtiEnv::ResumeThreadList(jint request_count, const jthread* request_list, jvmtiError* results) {
for (int i = 0; i < request_count; i++) {
JavaThread *java_thread = get_JavaThread(request_list[i]);
if (java_thread == NULL) {
results[i] = JVMTI_ERROR_INVALID_THREAD;
continue;
}
// don't allow hidden thread resume request.
if (java_thread->is_hidden_from_external_view()) {
results[i] = JVMTI_ERROR_NONE; // indicate successful resume
continue;
}
if (!java_thread->is_being_ext_suspended()) {
results[i] = JVMTI_ERROR_THREAD_NOT_SUSPENDED;
continue;
}
if (!JvmtiSuspendControl::resume(java_thread)) {
results[i] = JVMTI_ERROR_INTERNAL;
continue;
}
results[i] = JVMTI_ERROR_NONE; // indicate successful suspend
}
// per-thread resume results returned via results parameter
return JVMTI_ERROR_NONE;
} /* end ResumeThreadList */
// Threads_lock NOT held, java_thread not protected by lock
// java_thread - pre-checked
jvmtiError
JvmtiEnv::StopThread(JavaThread* java_thread, jobject exception) {
oop e = JNIHandles::resolve_external_guard(exception);
NULL_CHECK(e, JVMTI_ERROR_NULL_POINTER);
JavaThread::send_async_exception(java_thread->threadObj(), e);
return JVMTI_ERROR_NONE;
} /* end StopThread */
// Threads_lock NOT held
// thread - NOT pre-checked
jvmtiError
JvmtiEnv::InterruptThread(jthread thread) {
oop thread_oop = JNIHandles::resolve_external_guard(thread);
if (thread_oop == NULL || !thread_oop->is_a(SystemDictionary::Thread_klass()))
return JVMTI_ERROR_INVALID_THREAD;
JavaThread* current_thread = JavaThread::current();
// Todo: this is a duplicate of JVM_Interrupt; share code in future
// Ensure that the C++ Thread and OSThread structures aren't freed before we operate
MutexLockerEx ml(current_thread->threadObj() == thread_oop ? NULL : Threads_lock);
// We need to re-resolve the java_thread, since a GC might have happened during the
// acquire of the lock
JavaThread* java_thread = java_lang_Thread::thread(JNIHandles::resolve_external_guard(thread));
NULL_CHECK(java_thread, JVMTI_ERROR_THREAD_NOT_ALIVE);
Thread::interrupt(java_thread);
return JVMTI_ERROR_NONE;
} /* end InterruptThread */
// Threads_lock NOT held
// thread - NOT pre-checked
// info_ptr - pre-checked for NULL
jvmtiError
JvmtiEnv::GetThreadInfo(jthread thread, jvmtiThreadInfo* info_ptr) {
ResourceMark rm;
HandleMark hm;
JavaThread* current_thread = JavaThread::current();
// if thread is NULL the current thread is used
oop thread_oop;
if (thread == NULL) {
thread_oop = current_thread->threadObj();
} else {
thread_oop = JNIHandles::resolve_external_guard(thread);
}
if (thread_oop == NULL || !thread_oop->is_a(SystemDictionary::Thread_klass()))
return JVMTI_ERROR_INVALID_THREAD;
Handle thread_obj(current_thread, thread_oop);
Handle name;
ThreadPriority priority;
Handle thread_group;
Handle context_class_loader;
bool is_daemon;
{ MutexLocker mu(Threads_lock);
name = Handle(current_thread, java_lang_Thread::name(thread_obj()));
priority = java_lang_Thread::priority(thread_obj());
thread_group = Handle(current_thread, java_lang_Thread::threadGroup(thread_obj()));
is_daemon = java_lang_Thread::is_daemon(thread_obj());
oop loader = java_lang_Thread::context_class_loader(thread_obj());
context_class_loader = Handle(current_thread, loader);
}
{ const char *n;
if (name() != NULL) {
n = java_lang_String::as_utf8_string(name());
} else {
n = UNICODE::as_utf8(NULL, 0);
}
info_ptr->name = (char *) jvmtiMalloc(strlen(n)+1);
if (info_ptr->name == NULL)
return JVMTI_ERROR_OUT_OF_MEMORY;
strcpy(info_ptr->name, n);
}
info_ptr->is_daemon = is_daemon;
info_ptr->priority = priority;
info_ptr->context_class_loader = (context_class_loader.is_null()) ? NULL :
jni_reference(context_class_loader);
info_ptr->thread_group = jni_reference(thread_group);
return JVMTI_ERROR_NONE;
} /* end GetThreadInfo */
// Threads_lock NOT held, java_thread not protected by lock
// java_thread - pre-checked
// owned_monitor_count_ptr - pre-checked for NULL
// owned_monitors_ptr - pre-checked for NULL
jvmtiError
JvmtiEnv::GetOwnedMonitorInfo(JavaThread* java_thread, jint* owned_monitor_count_ptr, jobject** owned_monitors_ptr) {
jvmtiError err = JVMTI_ERROR_NONE;
JavaThread* calling_thread = JavaThread::current();
// growable array of jvmti monitors info on the C-heap
GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list =