File: getlocal001.cpp

package info (click to toggle)
openjdk-25 25.0.1%2B8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 825,408 kB
  • sloc: java: 5,585,680; cpp: 1,333,948; xml: 1,321,242; ansic: 488,034; asm: 404,003; objc: 21,088; sh: 15,106; javascript: 13,265; python: 8,319; makefile: 2,518; perl: 357; awk: 351; pascal: 103; exp: 83; sed: 72; jsp: 24
file content (503 lines) | stat: -rw-r--r-- 19,193 bytes parent folder | download | duplicates (5)
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
/*
 * Copyright (c) 2003, 2024, 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 <stdio.h>
#include <string.h>
#include "jvmti.h"
#include "agent_common.hpp"
#include "JVMTITools.hpp"

extern "C" {


#ifndef JNI_EARG


#endif

#define PASSED 0
#define STATUS_FAILED 2

static jvmtiEnv *jvmti;
static jvmtiCapabilities caps;
static jvmtiEventCallbacks callbacks;
static jint result = PASSED;
static jboolean printdump = JNI_FALSE;
static jmethodID mid1, mid2, mid3, mid4;
static jfloat fval;
static jdouble dval;

void check1(jvmtiEnv *jvmti_env, jthread thr, jint depth,
        jvmtiLocalVariableEntry *table, jint count) {
    jvmtiError err;
    jlong l = 0;
    jfloat f = 0.0;
    jdouble d = 0.0;
    int i;

    for (i = 0; i < count; i++) {
        if (strcmp(table[i].name, "l") == 0) {
            err = jvmti_env->GetLocalLong(thr, depth, table[i].slot, &l);
            if (err != JVMTI_ERROR_NONE) {
                printf("(GetLocalLong#1) unexpected error: %s (%d)\n",
                       TranslateError(err), err);
                result = STATUS_FAILED;
            }
        } else if (strcmp(table[i].name, "f") == 0) {
            err = jvmti_env->GetLocalFloat(thr, depth, table[i].slot, &f);
            if (err != JVMTI_ERROR_NONE) {
                printf("(GetLocalFloat#1) unexpected error: %s (%d)\n",
                       TranslateError(err), err);
                result = STATUS_FAILED;
            }
        } else if (strcmp(table[i].name, "d") == 0) {
            err = jvmti_env->GetLocalDouble(thr, depth, table[i].slot, &d);
            if (err != JVMTI_ERROR_NONE) {
                printf("(GetLocalDouble#1) unexpected error: %s (%d)\n",
                       TranslateError(err), err);
                result = STATUS_FAILED;
            }
        }
    }
    if ((l != 22) || (f != 6.0) || (d != 7.0)) {
        result = STATUS_FAILED;
        printf("One of values retrieved by GetLocal is wrong (hook):\n");
        printf("    actual: long = 0x%08x%08x, float = %f, double = %f\n",
               (jint)(l >> 32), (jint)l, f, d);
        printf("  expected: long = 0x%08x%08x, float = %f, double = %f\n",
               (jint)0, (jint)22, (double)6.0, (double)7.0);
    }
}

void check2(jvmtiEnv *jvmti_env, jthread thr, jint depth,
        jvmtiLocalVariableEntry *table, jint count) {
    jvmtiError err;
    jint i1 = 0, i2 = 0, i3 = 0, i4 = 0, i5 = JNI_FALSE;
    int i;

    for (i = 0; i < count; i++) {
        if (strcmp(table[i].name, "i1") == 0) {
            err = jvmti_env->GetLocalInt(thr, depth, table[i].slot, &i1);
            if (err != JVMTI_ERROR_NONE) {
                printf("(GetLocalInt#2i1) unexpected error: %s (%d)\n",
                       TranslateError(err), err);
                result = STATUS_FAILED;
            }
        } else if (strcmp(table[i].name, "i2") == 0) {
            err = jvmti_env->GetLocalInt(thr, depth, table[i].slot, &i2);
            if (err != JVMTI_ERROR_NONE) {
                printf("(GetLocalInt#2i2) unexpected error: %s (%d)\n",
                       TranslateError(err), err);
                result = STATUS_FAILED;
            }
        } else if (strcmp(table[i].name, "i3") == 0) {
            err = jvmti_env->GetLocalInt(thr, depth, table[i].slot, &i3);
            if (err != JVMTI_ERROR_NONE) {
                printf("(GetLocalInt#2i3) unexpected error: %s (%d)\n",
                       TranslateError(err), err);
                result = STATUS_FAILED;
            }
        } else if (strcmp(table[i].name, "i4") == 0) {
            err = jvmti_env->GetLocalInt(thr, depth, table[i].slot, &i4);
            if (err != JVMTI_ERROR_NONE) {
                printf("(GetLocalInt#2i4) unexpected error: %s (%d)\n",
                       TranslateError(err), err);
                result = STATUS_FAILED;
            }
        } else if (strcmp(table[i].name, "i5") == 0) {
            err = jvmti_env->GetLocalInt(thr, depth, table[i].slot, &i5);
            if (err != JVMTI_ERROR_NONE) {
                printf("(GetLocalInt#2i5) unexpected error: %s (%d)\n",
                       TranslateError(err), err);
                result = STATUS_FAILED;
            }
        }
    }
    if ((i1 != 1) || (i2 != 1) || (i3 != 1) || (i4 != 1) || (i5 != JNI_TRUE)) {
        result = STATUS_FAILED;
        printf("One of values retrieved by GetLocal is wrong (locals):\n");
        printf("    actual: int=%d, short=%d, char=%d, byte=%d, boolean=%d\n",
               i1, i2, i3, i4, i5);
        printf("  expected: int=%d, short=%d, char=%d, byte=%d, boolean=%d\n",
               1, 1, 1, 1, JNI_TRUE);
    }
}

void check3(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thr, jint depth,
        jvmtiLocalVariableEntry *table, jint count, jmethodID mid) {
    jvmtiError err;
    jobject ob1 = nullptr, ob2 = nullptr;
    jclass cls;
    jfieldID fid;
    jint fldVal = 0;
    jint arr[10];
    int i;

    for (i = 0; i < count; i++) {
        if (strcmp(table[i].name, "ob1") == 0) {
            err = jvmti_env->GetLocalObject(thr, depth,
                table[i].slot, &ob1);
            if (err != JVMTI_ERROR_NONE) {
                printf("(GetLocalObject#1) unexpected error: %s (%d)\n",
                       TranslateError(err), err);
                result = STATUS_FAILED;
                continue;
            }
            err = jvmti_env->GetMethodDeclaringClass(mid, &cls);
            if (err != JVMTI_ERROR_NONE) {
                printf("(GetMethodDeclaringClass) unexpected error: %s (%d)\n",
                       TranslateError(err), err);
                result = STATUS_FAILED;
                continue;
            }
            fid = env->GetFieldID(cls, "fld", "I");
            if (fid == nullptr) {
                printf("Cannot find ID for \"fld\" field of meth03\n");
                env->ExceptionClear();
                result = STATUS_FAILED;
                continue;
            }
            fldVal = env->GetIntField(ob1, fid);
        } else if (strcmp(table[i].name, "ob2") == 0) {
            err = jvmti_env->GetLocalObject(thr, depth,
                table[i].slot, &ob2);
            if (err != JVMTI_ERROR_NONE) {
                printf("(GetLocalObject#2) unexpected error: %s (%d)\n",
                       TranslateError(err), err);
                result = STATUS_FAILED;
            }
            env->GetIntArrayRegion((jintArray) ob2, 0, 10, arr);
        }
    }
    if ((fldVal != 17) || (arr[2] != 8)) {
        result = STATUS_FAILED;
        printf("One of objects retrieved by GetLocal contains wrong value:\n");
        printf("    actual: fldVal = %d, int.arr[2] = %d\n", fldVal, arr[2]);
        printf("  expected: fldVal = %d, int.arr[2] = %d\n", 17, 8);
    }
}

void check4(jvmtiEnv *jvmti_env, jthread thr, jint depth,
        jvmtiLocalVariableEntry *table, jint count) {
    jvmtiError err;
    jint i1 = 0, i2 = 0, i3 = 0, i4 = 0, b = JNI_FALSE;
    jlong l, lval = -100;
    jfloat f = 0.0;
    jdouble d = 0.0;
    int i;

    for (i = 0; i < count; i++) {
        if (strcmp(table[i].name, "i1") == 0) {
            err = jvmti_env->GetLocalInt(thr, depth, table[i].slot, &i1);
            if (err != JVMTI_ERROR_NONE) {
                printf("(GetLocalInt#4i1) unexpected error: %s (%d)\n",
                       TranslateError(err), err);
                result = STATUS_FAILED;
            }
        } else if (strcmp(table[i].name, "i2") == 0) {
            err = jvmti_env->GetLocalInt(thr, depth, table[i].slot, &i2);
            if (err != JVMTI_ERROR_NONE) {
                printf("(GetLocalInt#4i2) unexpected error: %s (%d)\n",
                       TranslateError(err), err);
                result = STATUS_FAILED;
            }
        } else if (strcmp(table[i].name, "i3") == 0) {
            err = jvmti_env->GetLocalInt(thr, depth, table[i].slot, &i3);
            if (err != JVMTI_ERROR_NONE) {
                printf("(GetLocalInt#4i3) unexpected error: %s (%d)\n",
                       TranslateError(err), err);
                result = STATUS_FAILED;
            }
        } else if (strcmp(table[i].name, "i4") == 0) {
            err = jvmti_env->GetLocalInt(thr, depth, table[i].slot, &i4);
            if (err != JVMTI_ERROR_NONE) {
                printf("(GetLocalInt#4i4) unexpected error: %s (%d)\n",
                       TranslateError(err), err);
                result = STATUS_FAILED;
            }
        } else if (strcmp(table[i].name, "b") == 0) {
            err = jvmti_env->GetLocalInt(thr, depth, table[i].slot, &b);
            if (err != JVMTI_ERROR_NONE) {
                printf("(GetLocalInt#4b) unexpected error: %s (%d)\n",
                       TranslateError(err), err);
                result = STATUS_FAILED;
            }
        } else if (strcmp(table[i].name, "l") == 0) {
            err = jvmti_env->GetLocalLong(thr, depth, table[i].slot, &l);
            if (err != JVMTI_ERROR_NONE) {
                printf("(GetLocalLong#4) unexpected error: %s (%d)\n",
                       TranslateError(err), err);
                result = STATUS_FAILED;
            }
        } else if (strcmp(table[i].name, "f") == 0) {
            err = jvmti_env->GetLocalFloat(thr, depth, table[i].slot, &f);
            if (err != JVMTI_ERROR_NONE) {
                printf("(GetLocalFloat#4) unexpected error: %s (%d)\n",
                       TranslateError(err), err);
                result = STATUS_FAILED;
            }
        } else if (strcmp(table[i].name, "d") == 0) {
            err = jvmti_env->GetLocalDouble(thr, depth, table[i].slot, &d);
            if (err != JVMTI_ERROR_NONE) {
                printf("(GetLocalDouble#4) unexpected error: %s (%d)\n",
                       TranslateError(err), err);
                result = STATUS_FAILED;
            }
        }
    }
    if ((i1 != 1) || (i2 != 2) || (i3 != 3) || (i4 != 4) || (b != JNI_TRUE) ||
            (l != lval) || (f != fval) || (d != dval)) {
        result = STATUS_FAILED;
        printf("One of values retrieved by GetLocal is wrong (params):\n");
        printf("    actual: int=%d, short=%d, char=%d, byte=%d, boolean=%d\n",
               i1, i2, i3, i4, b);
        printf("            long = 0x%08x%08x, float = %f, double = %f\n",
               (jint)(l >> 32), (jint)l, f, d);
        printf("  expected: int=%d, short=%d, char=%d, byte=%d, boolean=%d\n",
               1, 2, 3, 4, JNI_TRUE);
        printf("            long = 0x%08x%08x, float = %f, double = %f\n",
               (jint)(lval >> 32), (jint)lval, fval, dval);
    }
}

void JNICALL MethodExit(jvmtiEnv *jvmti_env, JNIEnv *env,
        jthread thr, jmethodID mid,
        jboolean was_poped_by_exception, jvalue return_value) {
    jvmtiError err;
    jvmtiLocalVariableEntry *table = nullptr;
    jint entryCount = 0;
    int i;

    if (mid == mid1 || mid == mid2 || mid == mid3 || mid == mid4) {
        err = jvmti_env->GetLocalVariableTable(mid,
            &entryCount, &table);
        if (err != JVMTI_ERROR_NONE) {
            printf("(GetLocalVariableTable#1) unexpected error: %s (%d)\n",
                   TranslateError(err), err);
            result = STATUS_FAILED;
            return;
        }
        if (mid == mid1) {
            if (printdump == JNI_TRUE) {
                printf(">>> exit: meth01\n");
            }
            check1(jvmti_env, thr, 0, table, entryCount);
        } else if (mid == mid2) {
            if (printdump == JNI_TRUE) {
                printf(">>> exit: meth02\n");
            }
            check2(jvmti_env, thr, 0, table, entryCount);
            mid2 = nullptr;
        } else if (mid == mid3) {
            if (printdump == JNI_TRUE) {
                printf(">>> exit: meth03\n");
            }
            check3(jvmti_env, (JNIEnv *)env, thr, 0, table, entryCount, mid);
        } else if (mid == mid4) {
            if (printdump == JNI_TRUE) {
                printf(">>> exit: meth04\n");
            }
            check4(jvmti_env, thr, 0, table, entryCount);
        }
    }
    if (table != nullptr) {
        for (i = 0; i < entryCount; i++) {
            jvmti_env->Deallocate((unsigned char*)table[i].name);
            jvmti_env->Deallocate((unsigned char*)table[i].signature);
        }
        jvmti_env->Deallocate((unsigned char*)table);
    }
}

void JNICALL Breakpoint(jvmtiEnv *jvmti_env, JNIEnv *env,
        jthread thr, jmethodID method, jlocation location) {
    jvmtiError err;
    jvmtiLocalVariableEntry *table = nullptr;
    jint entryCount = 0;
    jmethodID mid;
    jlocation loc;
    int i;

    err = jvmti_env->GetFrameLocation(thr, 1, &mid, &loc);
    if (err != JVMTI_ERROR_NONE) {
        printf("(GetFrameLocation) unexpected error: %s (%d)\n",
               TranslateError(err), err);
        result = STATUS_FAILED;
        return;
    }
    err = jvmti_env->GetLocalVariableTable(mid, &entryCount, &table);
    if (err != JVMTI_ERROR_NONE) {
        printf("(GetLocalVariableTable#2) unexpected error: %s (%d)\n",
               TranslateError(err), err);
        result = STATUS_FAILED;
        return;
    }
    if (mid == mid1) {
        if (printdump == JNI_TRUE) {
            printf(">>> bp: meth01\n");
        }
        check1(jvmti_env, thr, 1, table, entryCount);
    } else if (mid == mid2) {
        if (printdump == JNI_TRUE) {
            printf(">>> bp: meth02\n");
        }
        check2(jvmti_env, thr, 1, table, entryCount);
    } else if (mid == mid3) {
        if (printdump == JNI_TRUE) {
            printf(">>> bp: meth03\n");
        }
        check3(jvmti_env, (JNIEnv *)env, thr, 1, table, entryCount, mid);
    } else if (mid == mid4) {
        if (printdump == JNI_TRUE) {
            printf(">>> bp: meth04\n");
        }
        check4(jvmti_env, thr, 1, table, entryCount);
    } else {
        printf("ERROR: didn't know where we got called from");
        result = STATUS_FAILED;
    }
    if (table != nullptr) {
        for (i = 0; i < entryCount; i++) {
            jvmti_env->Deallocate((unsigned char*)table[i].name);
            jvmti_env->Deallocate((unsigned char*)table[i].signature);
        }
        jvmti_env->Deallocate((unsigned char*)table);
    }
}

#ifdef STATIC_BUILD
JNIEXPORT jint JNICALL Agent_OnLoad_getlocal001(JavaVM *jvm, char *options, void *reserved) {
    return Agent_Initialize(jvm, options, reserved);
}
JNIEXPORT jint JNICALL Agent_OnAttach_getlocal001(JavaVM *jvm, char *options, void *reserved) {
    return Agent_Initialize(jvm, options, reserved);
}
JNIEXPORT jint JNI_OnLoad_getlocal001(JavaVM *jvm, char *options, void *reserved) {
    return JNI_VERSION_1_8;
}
#endif
jint  Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
    jint res;
    jvmtiError err;

    if (options != nullptr && strcmp(options, "printdump") == 0) {
        printdump = JNI_TRUE;
    }

    res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
    if (res != JNI_OK || jvmti == nullptr) {
        printf("Wrong result of a valid call to GetEnv !\n");
        return JNI_ERR;
    }

    err = jvmti->GetPotentialCapabilities(&caps);
    if (err != JVMTI_ERROR_NONE) {
        printf("(GetPotentialCapabilities) unexpected error: %s (%d)\n",
               TranslateError(err), err);
        return JNI_ERR;
    }

    err = jvmti->AddCapabilities(&caps);
    if (err != JVMTI_ERROR_NONE) {
        printf("(AddCapabilities) unexpected error: %s (%d)\n",
               TranslateError(err), err);
        return JNI_ERR;
    }

    err = jvmti->GetCapabilities(&caps);
    if (err != JVMTI_ERROR_NONE) {
        printf("(GetCapabilities) unexpected error: %s (%d)\n",
               TranslateError(err), err);
        return JNI_ERR;
    }

    if (!caps.can_access_local_variables) {
        printf("Warning: Access to local variables is not implemented\n");
    } else if (caps.can_generate_breakpoint_events &&
               caps.can_generate_method_exit_events) {
        callbacks.MethodExit = &MethodExit;
        callbacks.Breakpoint = &Breakpoint;
        err = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
        if (err != JVMTI_ERROR_NONE) {
            printf("(SetEventCallbacks) unexpected error: %s (%d)\n",
                   TranslateError(err), err);
            return JNI_ERR;
        }
    } else {
        printf("Warning: Breakpoint or MethodExit event is not implemented\n");
    }

    return JNI_OK;
}

JNIEXPORT void JNICALL
Java_nsk_jvmti_GetLocalVariable_getlocal001_getMeth(JNIEnv *env, jclass cls,
        jdouble d, jfloat f) {
    jvmtiError err;
    jmethodID mid;

    if (!caps.can_access_local_variables ||
        !caps.can_generate_breakpoint_events ||
        !caps.can_generate_method_exit_events) return;

    fval = f;
    dval = d;
    mid1 = env->GetMethodID(cls, "meth01", "()D");
    mid2 = env->GetMethodID(cls, "meth02", "(I)V");
    mid3 = env->GetStaticMethodID(cls, "meth03", "(Lnsk/jvmti/GetLocalVariable/getlocal001;)V");
    mid4 = env->GetStaticMethodID(cls, "meth04", "(IJSDCFBZ)V");
    mid = env->GetStaticMethodID(cls, "checkPoint", "()V");
    if (mid == 0 || mid1 == 0 || mid2 == 0 || mid3 == 0 || mid4 == 0) {
        printf("Cannot find Method ID for a method\n");
        env->ExceptionDescribe();
        env->ExceptionClear();
        result = STATUS_FAILED;
        return;
    }
    err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_METHOD_EXIT, nullptr);
    if (err != JVMTI_ERROR_NONE) {
        printf("Failed to enable METHOD_EXIT event: %s (%d)\n",
               TranslateError(err), err);
        result = STATUS_FAILED;
    }
    err = jvmti->SetBreakpoint(mid, 0);
    if (err != JVMTI_ERROR_NONE) {
        printf("Failed to SetBreakpoint: err = %d\n", err);
        result = STATUS_FAILED;
        return;
    }
    err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,
        JVMTI_EVENT_BREAKPOINT, nullptr);
    if (err != JVMTI_ERROR_NONE) {
        printf("Failed to enable BREAKPOINT event: %s (%d)\n",
               TranslateError(err), err);
        result = STATUS_FAILED;
    }
}

JNIEXPORT jint JNICALL
Java_nsk_jvmti_GetLocalVariable_getlocal001_getRes(JNIEnv *env, jclass cls) {
    return result;
}

}