< prev index next >

test/hotspot/jtreg/vmTestbase/nsk/jvmti/unit/GetLocalVariable/getlocal003/getlocal003.cpp

Print this page
rev 51722 : 8210700: Clean up JNI_ENV_ARG and factorize the macros for vmTestbase/jvmti/unit tests
Summary:
Reviewed-by:


  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  */
  26 
  27 #include <stdio.h>
  28 #include <string.h>
  29 #include "jvmti.h"
  30 #include "jni_tools.h"
  31 #include "agent_common.h"
  32 #include "JVMTITools.h"
  33 
  34 #ifdef __cplusplus
  35 extern "C" {
  36 #endif
  37 
  38 #ifndef JNI_ENV_ARG
  39 
  40 #ifdef __cplusplus
  41 #define JNI_ENV_ARG(x, y) y
  42 #define JNI_ENV_PTR(x) x
  43 #else
  44 #define JNI_ENV_ARG(x,y) x, y
  45 #define JNI_ENV_PTR(x) (*x)
  46 #endif
  47 
  48 #endif
  49 
  50 #define PASSED 0
  51 #define STATUS_FAILED 2
  52 
  53 static jvmtiEnv *jvmti = NULL;
  54 static jvmtiCapabilities caps;
  55 static jvmtiEventCallbacks callbacks;
  56 static jint result = PASSED;
  57 static jboolean printdump = JNI_FALSE;
  58 static jmethodID mid = NULL;
  59 static jvmtiLocalVariableEntry *table = NULL;
  60 static jint entryCount = 0;
  61 static jint methodExitCnt = -1;
  62 
  63 void print_LocalVariableEntry(jvmtiLocalVariableEntry *lvt_elem) {
  64   printf("\n Var name: %s, slot: %d", lvt_elem->name, lvt_elem->slot);
  65   printf(", start_bci: %" LL "d", lvt_elem->start_location);
  66   printf(", end_bci: %" LL "d",   lvt_elem->start_location + lvt_elem->length);
  67   printf(", signature: %s\n", lvt_elem->signature);
  68 }


 134 
 135 #ifdef STATIC_BUILD
 136 JNIEXPORT jint JNICALL Agent_OnLoad_getlocal003(JavaVM *jvm, char *options, void *reserved) {
 137     return Agent_Initialize(jvm, options, reserved);
 138 }
 139 JNIEXPORT jint JNICALL Agent_OnAttach_getlocal003(JavaVM *jvm, char *options, void *reserved) {
 140     return Agent_Initialize(jvm, options, reserved);
 141 }
 142 JNIEXPORT jint JNI_OnLoad_getlocal003(JavaVM *jvm, char *options, void *reserved) {
 143     return JNI_VERSION_1_8;
 144 }
 145 #endif
 146 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 147     jint res;
 148     jvmtiError err;
 149 
 150     if (options != NULL && strcmp(options, "printdump") == 0) {
 151         printdump = JNI_TRUE;
 152     }
 153 
 154     res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti),
 155         JVMTI_VERSION_1_1);
 156     if (res != JNI_OK || jvmti == NULL) {
 157         printf("Wrong result of a valid call to GetEnv!\n");
 158         return JNI_ERR;
 159     }
 160 
 161     err = jvmti->GetPotentialCapabilities(&caps);
 162     if (err != JVMTI_ERROR_NONE) {
 163         printf("(GetPotentialCapabilities) unexpected error: %s (%d)\n",
 164                TranslateError(err), err);
 165         return JNI_ERR;
 166     }
 167 
 168     err = jvmti->AddCapabilities(&caps);
 169     if (err != JVMTI_ERROR_NONE) {
 170         printf("(AddCapabilities) unexpected error: %s (%d)\n",
 171                TranslateError(err), err);
 172         return JNI_ERR;
 173     }
 174 
 175     err = jvmti->GetCapabilities(&caps);


 192     } else {
 193         printf("Warning: MethodExit event is not implemented\n");
 194     }
 195 
 196     return JNI_OK;
 197 }
 198 
 199 JNIEXPORT void JNICALL
 200 Java_nsk_jvmti_unit_GetLocalVariable_getlocal003_getMeth(JNIEnv *env, jclass cls) {
 201     jvmtiError err;
 202 
 203     if (jvmti == NULL) {
 204         printf("JVMTI client was not properly loaded!\n");
 205         result = STATUS_FAILED;
 206         return;
 207     }
 208 
 209     if (!caps.can_access_local_variables ||
 210         !caps.can_generate_method_exit_events) return;
 211 
 212     mid = JNI_ENV_PTR(env)->GetStaticMethodID(JNI_ENV_ARG(env, cls),
 213                                               "staticMeth", "(I)I");
 214     if (mid == NULL) {
 215         printf("Cannot find Method ID for staticMeth\n");
 216         result = STATUS_FAILED;
 217         return;
 218     }
 219 
 220     err = jvmti->GetLocalVariableTable(mid, &entryCount, &table);
 221     if (err != JVMTI_ERROR_NONE) {
 222             printf("(GetLocalVariableTable) unexpected error: %s (%d)\n",
 223                    TranslateError(err), err);
 224             result = STATUS_FAILED;
 225             return;
 226     }
 227 
 228     err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,
 229         JVMTI_EVENT_METHOD_EXIT, NULL);
 230     if (err != JVMTI_ERROR_NONE) {
 231         printf("Failed to enable metod exit event: %s (%d)\n",
 232                TranslateError(err), err);
 233         result = STATUS_FAILED;
 234     }
 235     fflush(stdout);
 236 }
 237 
 238 JNIEXPORT void JNICALL
 239 Java_nsk_jvmti_unit_GetLocalVariable_getlocal003_checkLoc(JNIEnv *env,
 240         jclass cls, jthread thr) {
 241     jvmtiError err;
 242     jvmtiLocalVariableEntry *table;
 243     jint entryCount;
 244     jmethodID mid;
 245     jint locVar;
 246     jint i, j;
 247     int  overlap = 0;
 248 
 249     if (jvmti == NULL) {
 250         return;
 251     }
 252 
 253     mid = JNI_ENV_PTR(env)->GetStaticMethodID(JNI_ENV_ARG(env, cls),
 254                                               "staticMeth", "(I)I");
 255     if (mid == NULL) {
 256         printf("Cannot find Method ID for staticMeth\n");
 257         result = STATUS_FAILED;
 258         return;
 259     }
 260 
 261     err = jvmti->GetLocalVariableTable(mid, &entryCount, &table);
 262     if (err != JVMTI_ERROR_NONE) {
 263         printf("(GetLocalVariableTable) unexpected error: %s (%d)\n",
 264                TranslateError(err), err);
 265         result = STATUS_FAILED;
 266         return;
 267     }
 268 
 269     for (i = 0; i < entryCount; i++) {
 270         print_LocalVariableEntry(&table[i]);
 271 
 272         err = jvmti->GetLocalInt(thr, 1, table[i].slot, &locVar);
 273 
 274         printf(" GetLocalInt: %s (%d)\n", TranslateError(err), err);


 307            }
 308 
 309            printf(" failure: locations of vars with slot #2 are overlaped:\n");
 310            print_LocalVariableEntry(&table[i]);
 311            print_LocalVariableEntry(&table[j]);
 312            overlap++;
 313            result = STATUS_FAILED;
 314         }
 315     }
 316     if (!overlap) {
 317         printf("\n Succes: locations of vars with slot #2 are NOT overlaped\n\n");
 318     }
 319     fflush(stdout);
 320 }
 321 
 322 JNIEXPORT jint JNICALL
 323 Java_nsk_jvmti_unit_GetLocalVariable_getlocal003_getRes(JNIEnv *env, jclass cls) {
 324     return result;
 325 }
 326 
 327 #ifdef __cplusplus
 328 }
 329 #endif


  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  */
  26 
  27 #include <stdio.h>
  28 #include <string.h>
  29 #include "jvmti.h"
  30 #include "jni_tools.h"
  31 #include "agent_common.h"
  32 #include "JVMTITools.h"
  33 

  34 extern "C" {











  35 

  36 
  37 #define PASSED 0
  38 #define STATUS_FAILED 2
  39 
  40 static jvmtiEnv *jvmti = NULL;
  41 static jvmtiCapabilities caps;
  42 static jvmtiEventCallbacks callbacks;
  43 static jint result = PASSED;
  44 static jboolean printdump = JNI_FALSE;
  45 static jmethodID mid = NULL;
  46 static jvmtiLocalVariableEntry *table = NULL;
  47 static jint entryCount = 0;
  48 static jint methodExitCnt = -1;
  49 
  50 void print_LocalVariableEntry(jvmtiLocalVariableEntry *lvt_elem) {
  51   printf("\n Var name: %s, slot: %d", lvt_elem->name, lvt_elem->slot);
  52   printf(", start_bci: %" LL "d", lvt_elem->start_location);
  53   printf(", end_bci: %" LL "d",   lvt_elem->start_location + lvt_elem->length);
  54   printf(", signature: %s\n", lvt_elem->signature);
  55 }


 121 
 122 #ifdef STATIC_BUILD
 123 JNIEXPORT jint JNICALL Agent_OnLoad_getlocal003(JavaVM *jvm, char *options, void *reserved) {
 124     return Agent_Initialize(jvm, options, reserved);
 125 }
 126 JNIEXPORT jint JNICALL Agent_OnAttach_getlocal003(JavaVM *jvm, char *options, void *reserved) {
 127     return Agent_Initialize(jvm, options, reserved);
 128 }
 129 JNIEXPORT jint JNI_OnLoad_getlocal003(JavaVM *jvm, char *options, void *reserved) {
 130     return JNI_VERSION_1_8;
 131 }
 132 #endif
 133 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 134     jint res;
 135     jvmtiError err;
 136 
 137     if (options != NULL && strcmp(options, "printdump") == 0) {
 138         printdump = JNI_TRUE;
 139     }
 140 
 141     res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);

 142     if (res != JNI_OK || jvmti == NULL) {
 143         printf("Wrong result of a valid call to GetEnv!\n");
 144         return JNI_ERR;
 145     }
 146 
 147     err = jvmti->GetPotentialCapabilities(&caps);
 148     if (err != JVMTI_ERROR_NONE) {
 149         printf("(GetPotentialCapabilities) unexpected error: %s (%d)\n",
 150                TranslateError(err), err);
 151         return JNI_ERR;
 152     }
 153 
 154     err = jvmti->AddCapabilities(&caps);
 155     if (err != JVMTI_ERROR_NONE) {
 156         printf("(AddCapabilities) unexpected error: %s (%d)\n",
 157                TranslateError(err), err);
 158         return JNI_ERR;
 159     }
 160 
 161     err = jvmti->GetCapabilities(&caps);


 178     } else {
 179         printf("Warning: MethodExit event is not implemented\n");
 180     }
 181 
 182     return JNI_OK;
 183 }
 184 
 185 JNIEXPORT void JNICALL
 186 Java_nsk_jvmti_unit_GetLocalVariable_getlocal003_getMeth(JNIEnv *env, jclass cls) {
 187     jvmtiError err;
 188 
 189     if (jvmti == NULL) {
 190         printf("JVMTI client was not properly loaded!\n");
 191         result = STATUS_FAILED;
 192         return;
 193     }
 194 
 195     if (!caps.can_access_local_variables ||
 196         !caps.can_generate_method_exit_events) return;
 197 
 198     mid = env->GetStaticMethodID(cls, "staticMeth", "(I)I");

 199     if (mid == NULL) {
 200         printf("Cannot find Method ID for staticMeth\n");
 201         result = STATUS_FAILED;
 202         return;
 203     }
 204 
 205     err = jvmti->GetLocalVariableTable(mid, &entryCount, &table);
 206     if (err != JVMTI_ERROR_NONE) {
 207             printf("(GetLocalVariableTable) unexpected error: %s (%d)\n",
 208                    TranslateError(err), err);
 209             result = STATUS_FAILED;
 210             return;
 211     }
 212 
 213     err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,
 214         JVMTI_EVENT_METHOD_EXIT, NULL);
 215     if (err != JVMTI_ERROR_NONE) {
 216         printf("Failed to enable metod exit event: %s (%d)\n",
 217                TranslateError(err), err);
 218         result = STATUS_FAILED;
 219     }
 220     fflush(stdout);
 221 }
 222 
 223 JNIEXPORT void JNICALL
 224 Java_nsk_jvmti_unit_GetLocalVariable_getlocal003_checkLoc(JNIEnv *env,
 225         jclass cls, jthread thr) {
 226     jvmtiError err;
 227     jvmtiLocalVariableEntry *table;
 228     jint entryCount;
 229     jmethodID mid;
 230     jint locVar;
 231     jint i, j;
 232     int  overlap = 0;
 233 
 234     if (jvmti == NULL) {
 235         return;
 236     }
 237 
 238     mid = env->GetStaticMethodID(cls, "staticMeth", "(I)I");

 239     if (mid == NULL) {
 240         printf("Cannot find Method ID for staticMeth\n");
 241         result = STATUS_FAILED;
 242         return;
 243     }
 244 
 245     err = jvmti->GetLocalVariableTable(mid, &entryCount, &table);
 246     if (err != JVMTI_ERROR_NONE) {
 247         printf("(GetLocalVariableTable) unexpected error: %s (%d)\n",
 248                TranslateError(err), err);
 249         result = STATUS_FAILED;
 250         return;
 251     }
 252 
 253     for (i = 0; i < entryCount; i++) {
 254         print_LocalVariableEntry(&table[i]);
 255 
 256         err = jvmti->GetLocalInt(thr, 1, table[i].slot, &locVar);
 257 
 258         printf(" GetLocalInt: %s (%d)\n", TranslateError(err), err);


 291            }
 292 
 293            printf(" failure: locations of vars with slot #2 are overlaped:\n");
 294            print_LocalVariableEntry(&table[i]);
 295            print_LocalVariableEntry(&table[j]);
 296            overlap++;
 297            result = STATUS_FAILED;
 298         }
 299     }
 300     if (!overlap) {
 301         printf("\n Succes: locations of vars with slot #2 are NOT overlaped\n\n");
 302     }
 303     fflush(stdout);
 304 }
 305 
 306 JNIEXPORT jint JNICALL
 307 Java_nsk_jvmti_unit_GetLocalVariable_getlocal003_getRes(JNIEnv *env, jclass cls) {
 308     return result;
 309 }
 310 

 311 }

< prev index next >