1 /*
   2  * Copyright (c) 1998, 2013, 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 "ThreadGroupReferenceImpl.h"
  28 #include "threadControl.h"
  29 #include "inStream.h"
  30 #include "outStream.h"
  31 
  32 static jboolean
  33 name(PacketInputStream *in, PacketOutputStream *out)
  34 {
  35     JNIEnv *env;
  36     jthreadGroup group;
  37 
  38     env = getEnv();
  39 
  40     group = inStream_readThreadGroupRef(env, in);
  41     if (inStream_error(in)) {
  42         return JNI_TRUE;
  43     }
  44 
  45     WITH_LOCAL_REFS(env, 1) {
  46 
  47         jvmtiThreadGroupInfo info;
  48 
  49         (void)memset(&info, 0, sizeof(info));
  50         threadGroupInfo(group, &info);
  51         (void)outStream_writeString(out, info.name == NULL ? "" : info.name);
  52         if ( info.name != NULL )
  53             jvmtiDeallocate(info.name);
  54 
  55     } END_WITH_LOCAL_REFS(env);
  56 
  57     return JNI_TRUE;
  58 }
  59 
  60 static jboolean
  61 parent(PacketInputStream *in, PacketOutputStream *out)
  62 {
  63     JNIEnv *env;
  64     jthreadGroup group;
  65 
  66     env = getEnv();
  67 
  68     group = inStream_readThreadGroupRef(env, in);
  69     if (inStream_error(in)) {
  70         return JNI_TRUE;
  71     }
  72 
  73     WITH_LOCAL_REFS(env, 1) {
  74 
  75         jvmtiThreadGroupInfo info;
  76 
  77         (void)memset(&info, 0, sizeof(info));
  78         threadGroupInfo(group, &info);
  79         (void)outStream_writeObjectRef(env, out, info.parent);
  80         if ( info.name != NULL )
  81             jvmtiDeallocate(info.name);
  82 
  83     } END_WITH_LOCAL_REFS(env);
  84 
  85     return JNI_TRUE;
  86 }
  87 
  88 static jboolean
  89 children(PacketInputStream *in, PacketOutputStream *out)
  90 {
  91      JNIEnv *env;
  92      jthreadGroup group;
  93      jboolean is_fiber_group;
  94 
  95      env = getEnv();
  96 
  97      group = inStream_readThreadGroupRef(env, in);
  98      if (inStream_error(in)) {
  99          return JNI_TRUE;
 100      }
 101 
 102      is_fiber_group = isSameObject(env, group, gdata->fiberThreadGroup);
 103 
 104      WITH_LOCAL_REFS(env, 2) {
 105 
 106          jvmtiError error = JVMTI_ERROR_NONE;
 107          jint threadCount;
 108          jint fiberCount = 0;
 109          jint groupCount;
 110          jthread *theThreads;
 111          jthread *theFibers = NULL;
 112          jthread *theGroups;
 113 
 114          if (is_fiber_group) {
 115              /* Get all the Fibers so we can return them. */
 116              theFibers = threadControl_allFibers(&fiberCount);
 117              if (theFibers == NULL && fiberCount != 0) {
 118                  error = JVMTI_ERROR_OUT_OF_MEMORY;
 119              }
 120          } 
 121 
 122          if (error == JVMTI_ERROR_NONE) {
 123              /* Get all the threads in this group so we can return them. */
 124              error = JVMTI_FUNC_PTR(gdata->jvmti,GetThreadGroupChildren)(gdata->jvmti, group,
 125                                               &threadCount,&theThreads,
 126                                               &groupCount, &theGroups);
 127          }
 128 
 129          if (error != JVMTI_ERROR_NONE) {
 130              outStream_setError(out, map2jdwpError(error));
 131          } else {
 132              int i;
 133 
 134              /* Squish out all of the debugger-spawned threads */
 135              threadCount = filterDebugThreads(theThreads, threadCount);
 136 
 137              (void)outStream_writeInt(out, threadCount + fiberCount);
 138              for (i = 0; i < threadCount; i++) {
 139                  (void)outStream_writeObjectRef(env, out, theThreads[i]);
 140              }
 141              for (i = 0; i < fiberCount; i++) {
 142                  (void)outStream_writeObjectRef(env, out, theFibers[i]);
 143              }
 144              (void)outStream_writeInt(out, groupCount);
 145              for (i = 0; i < groupCount; i++) {
 146                  (void)outStream_writeObjectRef(env, out, theGroups[i]);
 147              }
 148 
 149              jvmtiDeallocate(theGroups);
 150              jvmtiDeallocate(theThreads);
 151              jvmtiDeallocate(theFibers);
 152          }
 153 
 154      } END_WITH_LOCAL_REFS(env);
 155 
 156      return JNI_TRUE;
 157 }
 158 
 159 void *ThreadGroupReference_Cmds[] = { (void *)3,
 160                                       (void *)name,
 161                                       (void *)parent,
 162                                       (void *)children };