1 /*
  2  * Copyright (c) 1998, 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.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 #include "util.h"
 27 #include "VirtualMachineImpl.h"
 28 #include "commonRef.h"
 29 #include "inStream.h"
 30 #include "outStream.h"
 31 #include "eventHandler.h"
 32 #include "eventHelper.h"
 33 #include "threadControl.h"
 34 #include "SDE.h"
 35 #include "FrameID.h"
 36 
 37 static char *versionName = "Java Debug Wire Protocol (Reference Implementation)";
 38 
 39 static jboolean
 40 version(PacketInputStream *in, PacketOutputStream *out)
 41 {
 42     char buf[500];
 43     char *vmName;
 44     char *vmVersion;
 45     char *vmInfo;
 46 
 47     /* Now the JDWP versions are the same as JVMTI versions */
 48     int majorVersion = jvmtiMajorVersion();
 49     int minorVersion = 0;
 50 
 51     if (gdata->vmDead) {
 52         outStream_setError(out, JDWP_ERROR(VM_DEAD));
 53         return JNI_TRUE;
 54     }
 55 
 56     vmVersion = gdata->property_java_version;
 57     if (vmVersion == NULL) {
 58         vmVersion = "<unknown>";
 59     }
 60     vmName = gdata->property_java_vm_name;
 61     if (vmName == NULL) {
 62         vmName = "<unknown>";
 63     }
 64     vmInfo = gdata->property_java_vm_info;
 65     if (vmInfo == NULL) {
 66         vmInfo = "<unknown>";
 67     }
 68 
 69     /*
 70      * Write the descriptive version information
 71      */
 72     (void)snprintf(buf, sizeof(buf),
 73                 "%s version %d.%d\nJVM Debug Interface version %d.%d\n"
 74                  "JVM version %s (%s, %s)",
 75                   versionName, majorVersion, minorVersion,
 76                   jvmtiMajorVersion(), jvmtiMinorVersion(),
 77                   vmVersion, vmName, vmInfo);
 78     (void)outStream_writeString(out, buf);
 79 
 80     /*
 81      * Write the JDWP version numbers
 82      */
 83     (void)outStream_writeInt(out, majorVersion);
 84     (void)outStream_writeInt(out, minorVersion);
 85 
 86     /*
 87      * Write the VM version and name
 88      */
 89     (void)outStream_writeString(out, vmVersion);
 90     (void)outStream_writeString(out, vmName);
 91 
 92     return JNI_TRUE;
 93 }
 94 
 95 static jboolean
 96 classesForSignature(PacketInputStream *in, PacketOutputStream *out)
 97 {
 98     JNIEnv *env;
 99     char *signature;
100 
101     if (gdata->vmDead) {
102         outStream_setError(out, JDWP_ERROR(VM_DEAD));
103         return JNI_TRUE;
104     }
105 
106     signature = inStream_readString(in);
107     if (signature == NULL) {
108         outStream_setError(out, JDWP_ERROR(OUT_OF_MEMORY));
109         return JNI_TRUE;
110     }
111     if (inStream_error(in)) {
112         return JNI_TRUE;
113     }
114 
115     env = getEnv();
116 
117     WITH_LOCAL_REFS(env, 1) {
118 
119         jint classCount;
120         jclass *theClasses;
121         jvmtiError error;
122 
123         error = allLoadedClasses(&theClasses, &classCount);
124         if ( error == JVMTI_ERROR_NONE ) {
125             /* Count classes in theClasses which match signature */
126             int matchCount = 0;
127             /* Count classes written to the JDWP connection */
128             int writtenCount = 0;
129             int i;
130 
131             for (i = 0; i < classCount; i++) {
132                 jclass clazz = theClasses[i];
133                 jint status = classStatus(clazz);
134                 char *candidate_signature = NULL;
135                 jint wanted =
136                     (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY|
137                      JVMTI_CLASS_STATUS_PRIMITIVE);
138 
139                 /* We want prepared classes, primitives, and arrays only */
140                 if ((status & wanted) == 0) {
141                     continue;
142                 }
143 
144                 error = classSignature(clazz, &candidate_signature, NULL);
145                 if (error != JVMTI_ERROR_NONE) {
146                   // Clazz become invalid since the time we get the class list
147                   // Skip this entry
148                   if (error == JVMTI_ERROR_INVALID_CLASS) {
149                     continue;
150                   }
151 
152                   break;
153                 }
154 
155                 if (strcmp(candidate_signature, signature) == 0) {
156                     /* Float interesting classes (those that
157                      * are matching and are prepared) to the
158                      * beginning of the array.
159                      */
160                     theClasses[i] = theClasses[matchCount];
161                     theClasses[matchCount++] = clazz;
162                 }
163                 jvmtiDeallocate(candidate_signature);
164             }
165 
166             /* At this point matching prepared classes occupy
167              * indicies 0 thru matchCount-1 of theClasses.
168              */
169 
170             if ( error ==  JVMTI_ERROR_NONE ) {
171                 (void)outStream_writeInt(out, matchCount);
172                 for (; writtenCount < matchCount; writtenCount++) {
173                     jclass clazz = theClasses[writtenCount];
174                     jint status = classStatus(clazz);
175                     jbyte tag = referenceTypeTag(clazz);
176                     (void)outStream_writeByte(out, tag);
177                     (void)outStream_writeObjectRef(env, out, clazz);
178                     (void)outStream_writeInt(out, map2jdwpClassStatus(status));
179                     /* No point in continuing if there's an error */
180                     if (outStream_error(out)) {
181                         break;
182                     }
183                 }
184             }
185 
186             jvmtiDeallocate(theClasses);
187         }
188 
189         if ( error != JVMTI_ERROR_NONE ) {
190             outStream_setError(out, map2jdwpError(error));
191         }
192 
193     } END_WITH_LOCAL_REFS(env);
194 
195     jvmtiDeallocate(signature);
196 
197     return JNI_TRUE;
198 }
199 
200 static jboolean
201 allModules(PacketInputStream *in, PacketOutputStream *out)
202 {
203     JNIEnv *env;
204 
205     if (gdata->vmDead) {
206         outStream_setError(out, JDWP_ERROR(VM_DEAD));
207         return JNI_TRUE;
208     }
209 
210     env = getEnv();
211 
212     WITH_LOCAL_REFS(env, 1) {
213 
214         jint count = 0;
215         jint i = 0;
216         jobject* modules = NULL;
217         jvmtiError error = JVMTI_ERROR_NONE;
218 
219         error = JVMTI_FUNC_PTR(gdata->jvmti, GetAllModules) (gdata->jvmti, &count, &modules);
220         if (error != JVMTI_ERROR_NONE) {
221             outStream_setError(out, map2jdwpError(error));
222         } else {
223             (void)outStream_writeInt(out, count);
224             for (i = 0; i < count; i++) {
225                 (void)outStream_writeModuleRef(env, out, modules[i]);
226             }
227             jvmtiDeallocate(modules);
228         }
229 
230     } END_WITH_LOCAL_REFS(env);
231 
232     return JNI_TRUE;
233 }
234 
235 static jboolean
236 allClasses1(PacketInputStream *in, PacketOutputStream *out, int outputGenerics)
237 {
238     JNIEnv *env;
239 
240     if (gdata->vmDead) {
241         outStream_setError(out, JDWP_ERROR(VM_DEAD));
242         return JNI_TRUE;
243     }
244 
245     env = getEnv();
246 
247     WITH_LOCAL_REFS(env, 1) {
248 
249         jint classCount;
250         jclass *theClasses;
251         jvmtiError error;
252 
253         error = allLoadedClasses(&theClasses, &classCount);
254         if ( error != JVMTI_ERROR_NONE ) {
255             outStream_setError(out, map2jdwpError(error));
256         } else {
257             /* Count classes in theClasses which are prepared */
258             int prepCount = 0;
259             /* Count classes written to the JDWP connection */
260             int writtenCount = 0;
261             int i;
262 
263             for (i=0; i<classCount; i++) {
264                 jclass clazz = theClasses[i];
265                 jint status = classStatus(clazz);
266                 jint wanted =
267                     (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY);
268 
269                 /* We want prepared classes and arrays only */
270                 if ((status & wanted) != 0) {
271                     /* Float interesting classes (those that
272                      * are prepared) to the beginning of the array.
273                      */
274                     theClasses[i] = theClasses[prepCount];
275                     theClasses[prepCount++] = clazz;
276                 }
277             }
278 
279             /* At this point prepared classes occupy
280              * indicies 0 thru prepCount-1 of theClasses.
281              */
282 
283             (void)outStream_writeInt(out, prepCount);
284             for (; writtenCount < prepCount; writtenCount++) {
285                 char *signature = NULL;
286                 char *genericSignature = NULL;
287                 jclass clazz = theClasses[writtenCount];
288                 jint status = classStatus(clazz);
289                 jbyte tag = referenceTypeTag(clazz);
290                 jvmtiError error;
291 
292                 error = classSignature(clazz, &signature, &genericSignature);
293                 if (error != JVMTI_ERROR_NONE) {
294                     outStream_setError(out, map2jdwpError(error));
295                     break;
296                 }
297 
298                 (void)outStream_writeByte(out, tag);
299                 (void)outStream_writeObjectRef(env, out, clazz);
300                 (void)outStream_writeString(out, signature);
301                 if (outputGenerics == 1) {
302                     writeGenericSignature(out, genericSignature);
303                 }
304 
305                 (void)outStream_writeInt(out, map2jdwpClassStatus(status));
306                 jvmtiDeallocate(signature);
307                 if (genericSignature != NULL) {
308                   jvmtiDeallocate(genericSignature);
309                 }
310 
311                 /* No point in continuing if there's an error */
312                 if (outStream_error(out)) {
313                     break;
314                 }
315             }
316             jvmtiDeallocate(theClasses);
317         }
318 
319     } END_WITH_LOCAL_REFS(env);
320 
321     return JNI_TRUE;
322 }
323 
324 static jboolean
325 allClasses(PacketInputStream *in, PacketOutputStream *out)
326 {
327     return allClasses1(in, out, 0);
328 }
329 
330 static jboolean
331 allClassesWithGeneric(PacketInputStream *in, PacketOutputStream *out)
332 {
333     return allClasses1(in, out, 1);
334 }
335 
336   /***********************************************************/
337 
338 
339 static jboolean
340 instanceCounts(PacketInputStream *in, PacketOutputStream *out)
341 {
342     jint classCount;
343     jclass *classes;
344     JNIEnv *env;
345     int ii;
346 
347     if (gdata->vmDead) {
348         outStream_setError(out, JDWP_ERROR(VM_DEAD));
349         return JNI_TRUE;
350     }
351 
352     classCount = inStream_readInt(in);
353 
354     if (inStream_error(in)) {
355         return JNI_TRUE;
356     }
357     if (classCount == 0) {
358         (void)outStream_writeInt(out, 0);
359         return JNI_TRUE;
360     }
361     if (classCount < 0) {
362         outStream_setError(out, JDWP_ERROR(ILLEGAL_ARGUMENT));
363         return JNI_TRUE;
364     }
365     env = getEnv();
366     classes = jvmtiAllocate(classCount * (int)sizeof(jclass));
367     for (ii = 0; ii < classCount; ii++) {
368         jdwpError errorCode;
369         classes[ii] = inStream_readClassRef(env, in);
370         errorCode = inStream_error(in);
371         if (errorCode != JDWP_ERROR(NONE)) {
372             /*
373              * A class could have been unloaded/gc'd so
374              * if we get an error, just ignore it and keep
375              * going.  An instanceCount of 0 will be returned.
376              */
377             if (errorCode == JDWP_ERROR(INVALID_OBJECT) ||
378                 errorCode == JDWP_ERROR(INVALID_CLASS)) {
379                 inStream_clearError(in);
380                 classes[ii] = NULL;
381                 continue;
382             }
383             jvmtiDeallocate(classes);
384             return JNI_TRUE;
385         }
386     }
387 
388     WITH_LOCAL_REFS(env, 1) {
389         jlong      *counts;
390         jvmtiError error;
391 
392         counts = jvmtiAllocate(classCount * (int)sizeof(jlong));
393         /* Iterate over heap getting info on these classes */
394         error = classInstanceCounts(classCount, classes, counts);
395         if (error != JVMTI_ERROR_NONE) {
396             outStream_setError(out, map2jdwpError(error));
397         } else {
398             (void)outStream_writeInt(out, classCount);
399             for (ii = 0; ii < classCount; ii++) {
400                 (void)outStream_writeLong(out, counts[ii]);
401             }
402         }
403         jvmtiDeallocate(counts);
404     } END_WITH_LOCAL_REFS(env);
405     jvmtiDeallocate(classes);
406     return JNI_TRUE;
407 }
408 
409 static jboolean
410 redefineClasses(PacketInputStream *in, PacketOutputStream *out)
411 {
412     jvmtiClassDefinition *classDefs;
413     jboolean ok = JNI_TRUE;
414     jint classCount;
415     jint i;
416     JNIEnv *env;
417 
418     if (gdata->vmDead) {
419         /* quietly ignore */
420         return JNI_TRUE;
421     }
422 
423     classCount = inStream_readInt(in);
424     if (inStream_error(in)) {
425         return JNI_TRUE;
426     }
427     if ( classCount == 0 ) {
428         return JNI_TRUE;
429     }
430     /*LINTED*/
431     classDefs = jvmtiAllocate(classCount*(int)sizeof(jvmtiClassDefinition));
432     if (classDefs == NULL) {
433         outStream_setError(out, JDWP_ERROR(OUT_OF_MEMORY));
434         return JNI_TRUE;
435     }
436     /*LINTED*/
437     (void)memset(classDefs, 0, classCount*sizeof(jvmtiClassDefinition));
438 
439     env = getEnv();
440     for (i = 0; i < classCount; ++i) {
441         int byteCount;
442         unsigned char * bytes;
443         jclass clazz;
444 
445         clazz = inStream_readClassRef(env, in);
446         if (inStream_error(in)) {
447             ok = JNI_FALSE;
448             break;
449         }
450         byteCount = inStream_readInt(in);
451         if (inStream_error(in)) {
452             ok = JNI_FALSE;
453             break;
454         }
455         if ( byteCount <= 0 ) {
456             outStream_setError(out, JDWP_ERROR(INVALID_CLASS_FORMAT));
457             ok = JNI_FALSE;
458             break;
459         }
460         bytes = (unsigned char *)jvmtiAllocate(byteCount);
461         if (bytes == NULL) {
462             outStream_setError(out, JDWP_ERROR(OUT_OF_MEMORY));
463             ok = JNI_FALSE;
464             break;
465         }
466         (void)inStream_readBytes(in, byteCount, (jbyte *)bytes);
467         if (inStream_error(in)) {
468             ok = JNI_FALSE;
469             break;
470         }
471 
472         classDefs[i].klass = clazz;
473         classDefs[i].class_byte_count = byteCount;
474         classDefs[i].class_bytes = bytes;
475     }
476 
477     if (ok == JNI_TRUE) {
478         jvmtiError error;
479 
480         error = JVMTI_FUNC_PTR(gdata->jvmti,RedefineClasses)
481                         (gdata->jvmti, classCount, classDefs);
482         if (error != JVMTI_ERROR_NONE) {
483             outStream_setError(out, map2jdwpError(error));
484         } else {
485             /* zap our BP info */
486             for ( i = 0 ; i < classCount; i++ ) {
487                 eventHandler_freeClassBreakpoints(classDefs[i].klass);
488             }
489         }
490     }
491 
492     /* free up allocated memory */
493     for ( i = 0 ; i < classCount; i++ ) {
494         if ( classDefs[i].class_bytes != NULL ) {
495             jvmtiDeallocate((void*)classDefs[i].class_bytes);
496         }
497     }
498     jvmtiDeallocate(classDefs);
499 
500     return JNI_TRUE;
501 }
502 
503 static jboolean
504 setDefaultStratum(PacketInputStream *in, PacketOutputStream *out)
505 {
506     char *stratumId;
507 
508     if (gdata->vmDead) {
509         /* quietly ignore */
510         return JNI_TRUE;
511     }
512 
513     stratumId = inStream_readString(in);
514     if (inStream_error(in)) {
515         return JNI_TRUE;
516     } else if (strcmp(stratumId, "") == 0) {
517         stratumId = NULL;
518     }
519     setGlobalStratumId(stratumId);
520 
521     return JNI_TRUE;
522 }
523 
524 static jboolean
525 getAllThreads(PacketInputStream *in, PacketOutputStream *out)
526 {
527     JNIEnv *env;
528 
529     if (gdata->vmDead) {
530         outStream_setError(out, JDWP_ERROR(VM_DEAD));
531         return JNI_TRUE;
532     }
533 
534     env = getEnv();
535 
536     WITH_LOCAL_REFS(env, 1) {
537 
538         int i;
539         jint threadCount;
540         jint vthreadCount;
541         jthread *theThreads;
542         jthread *theVThreads;
543 
544         theThreads = allThreads(&threadCount);
545         if (gdata->enumerateVThreads) {
546             theVThreads = threadControl_allVThreads(&vthreadCount);
547         } else {
548             theVThreads = NULL;
549             vthreadCount = 0;
550         }
551 
552         if (theThreads == NULL || (theVThreads == NULL && vthreadCount != 0)) {
553             outStream_setError(out, JDWP_ERROR(OUT_OF_MEMORY));
554         } else {
555             /* Squish out all of the debugger-spawned threads */
556             threadCount = filterDebugThreads(theThreads, threadCount);
557 
558             (void)outStream_writeInt(out, threadCount + vthreadCount);
559             for (i = 0; i < vthreadCount; i++) {
560                 (void)outStream_writeObjectRef(env, out, theVThreads[i]);
561             }
562             for (i = 0; i < threadCount; i++) {
563                 (void)outStream_writeObjectRef(env, out, theThreads[i]);
564             }
565 
566             jvmtiDeallocate(theThreads);
567             jvmtiDeallocate(theVThreads);
568         }
569 
570     } END_WITH_LOCAL_REFS(env);
571 
572     return JNI_TRUE;
573 }
574 
575 static jboolean
576 topLevelThreadGroups(PacketInputStream *in, PacketOutputStream *out)
577 {
578     JNIEnv *env;
579 
580     if (gdata->vmDead) {
581         outStream_setError(out, JDWP_ERROR(VM_DEAD));
582         return JNI_TRUE;
583     }
584 
585     env = getEnv();
586 
587     WITH_LOCAL_REFS(env, 1) {
588 
589         jvmtiError error;
590         jint groupCount;
591         jthreadGroup *groups;
592 
593         groups = NULL;
594         error = JVMTI_FUNC_PTR(gdata->jvmti,GetTopThreadGroups)
595                     (gdata->jvmti, &groupCount, &groups);
596         if (error != JVMTI_ERROR_NONE) {
597             outStream_setError(out, map2jdwpError(error));
598         } else {
599             int i;
600 
601             (void)outStream_writeInt(out, groupCount);
602             for (i = 0; i < groupCount; i++) {
603                 (void)outStream_writeObjectRef(env, out, groups[i]);
604             }
605 
606             jvmtiDeallocate(groups);
607         }
608 
609     } END_WITH_LOCAL_REFS(env);
610 
611     return JNI_TRUE;
612 }
613 
614 static jboolean
615 dispose(PacketInputStream *in, PacketOutputStream *out)
616 {
617     return JNI_TRUE;
618 }
619 
620 static jboolean
621 idSizes(PacketInputStream *in, PacketOutputStream *out)
622 {
623     (void)outStream_writeInt(out, sizeof(jfieldID));    /* fields */
624     (void)outStream_writeInt(out, sizeof(jmethodID));   /* methods */
625     (void)outStream_writeInt(out, sizeof(jlong));       /* objects */
626     (void)outStream_writeInt(out, sizeof(jlong));       /* referent types */
627     (void)outStream_writeInt(out, sizeof(FrameID));    /* frames */
628     return JNI_TRUE;
629 }
630 
631 static jboolean
632 suspend(PacketInputStream *in, PacketOutputStream *out)
633 {
634     jvmtiError error;
635 
636     if (gdata->vmDead) {
637         outStream_setError(out, JDWP_ERROR(VM_DEAD));
638         return JNI_TRUE;
639     }
640     error = threadControl_suspendAll();
641     if (error != JVMTI_ERROR_NONE) {
642         outStream_setError(out, map2jdwpError(error));
643     }
644     return JNI_TRUE;
645 }
646 
647 static jboolean
648 resume(PacketInputStream *in, PacketOutputStream *out)
649 {
650     jvmtiError error;
651 
652     if (gdata->vmDead) {
653         outStream_setError(out, JDWP_ERROR(VM_DEAD));
654         return JNI_TRUE;
655     }
656     error = threadControl_resumeAll();
657     if (error != JVMTI_ERROR_NONE) {
658         outStream_setError(out, map2jdwpError(error));
659     }
660     return JNI_TRUE;
661 }
662 
663 static jboolean
664 doExit(PacketInputStream *in, PacketOutputStream *out)
665 {
666     jint exitCode;
667 
668     exitCode = inStream_readInt(in);
669     if (gdata->vmDead) {
670         /* quietly ignore */
671         return JNI_FALSE;
672     }
673 
674     /* We send the reply from here because we are about to exit. */
675     if (inStream_error(in)) {
676         outStream_setError(out, inStream_error(in));
677     }
678     outStream_sendReply(out);
679 
680     forceExit(exitCode);
681 
682     /* Shouldn't get here */
683     JDI_ASSERT(JNI_FALSE);
684 
685     /* Shut up the compiler */
686     return JNI_FALSE;
687 
688 }
689 
690 static jboolean
691 createString(PacketInputStream *in, PacketOutputStream *out)
692 {
693     JNIEnv *env;
694     char *cstring;
695 
696     if (gdata->vmDead) {
697         outStream_setError(out, JDWP_ERROR(VM_DEAD));
698         return JNI_TRUE;
699     }
700 
701     cstring = inStream_readString(in);
702     if (cstring == NULL) {
703         outStream_setError(out, JDWP_ERROR(OUT_OF_MEMORY));
704         return JNI_TRUE;
705     }
706     if (inStream_error(in)) {
707         return JNI_TRUE;
708     }
709 
710     env = getEnv();
711 
712     WITH_LOCAL_REFS(env, 1) {
713 
714         jstring string;
715 
716         string = JNI_FUNC_PTR(env,NewStringUTF)(env, cstring);
717         if (JNI_FUNC_PTR(env,ExceptionOccurred)(env)) {
718             outStream_setError(out, JDWP_ERROR(OUT_OF_MEMORY));
719         } else {
720             (void)outStream_writeObjectRef(env, out, string);
721         }
722 
723     } END_WITH_LOCAL_REFS(env);
724 
725     jvmtiDeallocate(cstring);
726 
727     return JNI_TRUE;
728 }
729 
730 static jboolean
731 capabilities(PacketInputStream *in, PacketOutputStream *out)
732 {
733     jvmtiCapabilities caps;
734     jvmtiError error;
735 
736     if (gdata->vmDead) {
737         outStream_setError(out, JDWP_ERROR(VM_DEAD));
738         return JNI_TRUE;
739     }
740     error = jvmtiGetCapabilities(&caps);
741     if (error != JVMTI_ERROR_NONE) {
742         outStream_setError(out, map2jdwpError(error));
743         return JNI_TRUE;
744     }
745 
746     (void)outStream_writeBoolean(out, (jboolean)caps.can_generate_field_modification_events);
747     (void)outStream_writeBoolean(out, (jboolean)caps.can_generate_field_access_events);
748     (void)outStream_writeBoolean(out, (jboolean)caps.can_get_bytecodes);
749     (void)outStream_writeBoolean(out, (jboolean)caps.can_get_synthetic_attribute);
750     (void)outStream_writeBoolean(out, (jboolean)caps.can_get_owned_monitor_info);
751     (void)outStream_writeBoolean(out, (jboolean)caps.can_get_current_contended_monitor);
752     (void)outStream_writeBoolean(out, (jboolean)caps.can_get_monitor_info);
753     return JNI_TRUE;
754 }
755 
756 static jboolean
757 capabilitiesNew(PacketInputStream *in, PacketOutputStream *out)
758 {
759     jvmtiCapabilities caps;
760     jvmtiError error;
761 
762     if (gdata->vmDead) {
763         outStream_setError(out, JDWP_ERROR(VM_DEAD));
764         return JNI_TRUE;
765     }
766     error = jvmtiGetCapabilities(&caps);
767     if (error != JVMTI_ERROR_NONE) {
768         outStream_setError(out, map2jdwpError(error));
769         return JNI_TRUE;
770     }
771 
772     (void)outStream_writeBoolean(out, (jboolean)caps.can_generate_field_modification_events);
773     (void)outStream_writeBoolean(out, (jboolean)caps.can_generate_field_access_events);
774     (void)outStream_writeBoolean(out, (jboolean)caps.can_get_bytecodes);
775     (void)outStream_writeBoolean(out, (jboolean)caps.can_get_synthetic_attribute);
776     (void)outStream_writeBoolean(out, (jboolean)caps.can_get_owned_monitor_info);
777     (void)outStream_writeBoolean(out, (jboolean)caps.can_get_current_contended_monitor);
778     (void)outStream_writeBoolean(out, (jboolean)caps.can_get_monitor_info);
779 
780     /* new since JDWP version 1.4 */
781     (void)outStream_writeBoolean(out, (jboolean)caps.can_redefine_classes);
782     (void)outStream_writeBoolean(out, (jboolean)JNI_FALSE /* can_add_method */ );
783     (void)outStream_writeBoolean(out, (jboolean)JNI_FALSE /* can_unrestrictedly_redefine_classes */ );
784     /* 11: canPopFrames */
785     (void)outStream_writeBoolean(out, (jboolean)caps.can_pop_frame);
786     /* 12: canUseInstanceFilters */
787     (void)outStream_writeBoolean(out, (jboolean)JNI_TRUE);
788     /* 13: canGetSourceDebugExtension */
789     (void)outStream_writeBoolean(out, (jboolean)caps.can_get_source_debug_extension);
790     /* 14: canRequestVMDeathEvent */
791     (void)outStream_writeBoolean(out, (jboolean)JNI_TRUE);
792     /* 15: canSetDefaultStratum */
793     (void)outStream_writeBoolean(out, (jboolean)JNI_TRUE);
794     /* 16: canGetInstanceInfo */
795     (void)outStream_writeBoolean(out, (jboolean)caps.can_tag_objects);
796     /* 17: canRequestMonitorEvents */
797     (void)outStream_writeBoolean(out, (jboolean)caps.can_generate_monitor_events);
798     /* 18: canGetMonitorFrameInfo */
799     (void)outStream_writeBoolean(out, (jboolean)caps.can_get_owned_monitor_stack_depth_info);
800     /* remaining reserved */
801     (void)outStream_writeBoolean(out, (jboolean)JNI_FALSE); /* 19 */
802     /* 20 Can get constant pool information */
803     (void)outStream_writeBoolean(out, (jboolean)caps.can_get_constant_pool);
804     /* 21 Can force early return */
805     (void)outStream_writeBoolean(out, (jboolean)caps.can_force_early_return);
806     /* 22 Supports virtual threads, temporary capability */
807     (void)outStream_writeBoolean(out, (jboolean)JNI_TRUE);
808 
809     (void)outStream_writeBoolean(out, (jboolean)JNI_FALSE); /* 23 */
810     (void)outStream_writeBoolean(out, (jboolean)JNI_FALSE); /* 24 */
811     (void)outStream_writeBoolean(out, (jboolean)JNI_FALSE); /* 25 */
812     (void)outStream_writeBoolean(out, (jboolean)JNI_FALSE); /* 26 */
813     (void)outStream_writeBoolean(out, (jboolean)JNI_FALSE); /* 27 */
814     (void)outStream_writeBoolean(out, (jboolean)JNI_FALSE); /* 28 */
815     (void)outStream_writeBoolean(out, (jboolean)JNI_FALSE); /* 29 */
816     (void)outStream_writeBoolean(out, (jboolean)JNI_FALSE); /* 30 */
817     (void)outStream_writeBoolean(out, (jboolean)JNI_FALSE); /* 31 */
818     (void)outStream_writeBoolean(out, (jboolean)JNI_FALSE); /* 32 */
819     return JNI_TRUE;
820 }
821 
822 static int
823 countPaths(char *string) {
824     int cnt = 1; /* always have one */
825     char *pos = string;
826     char *ps;
827 
828     ps = gdata->property_path_separator;
829     if ( ps == NULL ) {
830         ps = ";";
831     }
832     while ((pos = strchr(pos, ps[0])) != NULL) {
833         ++cnt;
834         ++pos;
835     }
836     return cnt;
837 }
838 
839 static void
840 writePaths(PacketOutputStream *out, char *string) {
841     char *pos;
842     char *ps;
843     char *buf;
844     int   npaths;
845     int   i;
846 
847     buf = jvmtiAllocate((int)strlen(string)+1);
848 
849     npaths = countPaths(string);
850     (void)outStream_writeInt(out, npaths);
851 
852     ps = gdata->property_path_separator;
853     if ( ps == NULL ) {
854         ps = ";";
855     }
856 
857     pos = string;
858     for ( i = 0 ; i < npaths && pos != NULL; i++ ) {
859         char *psPos;
860         int   plen;
861 
862         psPos = strchr(pos, ps[0]);
863         if ( psPos == NULL ) {
864             plen = (int)strlen(pos);
865         } else {
866             plen = (int)(psPos-pos);
867             psPos++;
868         }
869         (void)memcpy(buf, pos, plen);
870         buf[plen] = 0;
871         (void)outStream_writeString(out, buf);
872         pos = psPos;
873     }
874 
875     jvmtiDeallocate(buf);
876 }
877 
878 static jboolean
879 classPaths(PacketInputStream *in, PacketOutputStream *out)
880 {
881     char *ud;
882     char *cp;
883 
884     ud = gdata->property_user_dir;
885     if ( ud == NULL ) {
886         ud = "";
887     }
888     cp = gdata->property_java_class_path;
889     if ( cp == NULL ) {
890         cp = "";
891     }
892     (void)outStream_writeString(out, ud);
893     writePaths(out, cp);
894     (void)outStream_writeInt(out, 0); // no bootclasspath
895     return JNI_TRUE;
896 }
897 
898 static jboolean
899 disposeObjects(PacketInputStream *in, PacketOutputStream *out)
900 {
901     int i;
902     int refCount;
903     jlong id;
904     int requestCount;
905     JNIEnv *env;
906 
907     if (gdata->vmDead) {
908         /* quietly ignore */
909         return JNI_TRUE;
910     }
911 
912     requestCount = inStream_readInt(in);
913     if (inStream_error(in)) {
914         return JNI_TRUE;
915     }
916 
917     env = getEnv();
918     for (i = 0; i < requestCount; i++) {
919         id = inStream_readObjectID(in);
920         refCount = inStream_readInt(in);
921         if (inStream_error(in)) {
922             return JNI_TRUE;
923         }
924         commonRef_releaseMultiple(env, id, refCount);
925     }
926 
927     return JNI_TRUE;
928 }
929 
930 static jboolean
931 holdEvents(PacketInputStream *in, PacketOutputStream *out)
932 {
933     eventHelper_holdEvents();
934     return JNI_TRUE;
935 }
936 
937 static jboolean
938 releaseEvents(PacketInputStream *in, PacketOutputStream *out)
939 {
940     eventHelper_releaseEvents();
941     return JNI_TRUE;
942 }
943 
944 Command VirtualMachine_Commands[] = {
945     {version, "Version"},
946     {classesForSignature, "ClassesForSignature"},
947     {allClasses, "AllClasses"},
948     {getAllThreads, "GetAllThreads"},
949     {topLevelThreadGroups, "TopLevelThreadGroups"},
950     {dispose, "Dispose"},
951     {idSizes, "IDSizes"},
952     {suspend, "Suspend"},
953     {resume, "Resume"},
954     {doExit, "DoExit"},
955     {createString, "CreateString"},
956     {capabilities, "Capabilities"},
957     {classPaths, "ClassPaths"},
958     {disposeObjects, "DisposeObjects"},
959     {holdEvents, "HoldEvents"},
960     {releaseEvents, "ReleaseEvents"},
961     {capabilitiesNew, "CapabilitiesNew"},
962     {redefineClasses, "RedefineClasses"},
963     {setDefaultStratum, "SetDefaultStratum"},
964     {allClassesWithGeneric, "AllClassesWithGeneric"},
965     {instanceCounts, "InstanceCounts"},
966     {allModules, "AllModules"}
967 };
968 
969 DEBUG_DISPATCH_DEFINE_CMDSET(VirtualMachine)