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
|
/*
* 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"
#include "jni_tools.hpp"
extern "C" {
#define PASSED 0
#define STATUS_FAILED 2
typedef struct {
const char *name;
const char *sig;
jint value;
} var_info;
typedef struct {
jboolean isObsolete;
const char *name;
const char *sig;
jint line;
jint count;
var_info *vars;
const char *desc;
} frame_info;
static jvmtiEnv *jvmti = nullptr;
static jvmtiCapabilities caps;
static jvmtiEventCallbacks callbacks;
static jint result = PASSED;
static jboolean printdump = JNI_FALSE;
static jbyteArray classBytes;
static jmethodID midRun = nullptr;
static jmethodID mid1 = nullptr;
static jmethodID mid2 = nullptr;
static jfieldID fid1 = nullptr;
static jfieldID fid2 = nullptr;
static jint stepEventsExpected = 0;
static jint bpEventsExpected = 0;
static jint popEventsExpected = 0;
static jint accessEventsExpected = 0;
static jint modificationEventsExpected = 0;
static jint exceptionEventsExpected = 0;
static jint catchEventsExpected = 0;
static jint stepEventsCount = 0;
static jint bpEventsCount = 0;
static jint popEventsCount = 0;
static jint accessEventsCount = 0;
static jint modificationEventsCount = 0;
static jint exceptionEventsCount = 0;
static jint catchEventsCount = 0;
static jint redefinesCount = 0;
static const char *cls_exp = "Lnsk/jvmti/RedefineClasses/redefclass027a;";
static var_info run[] = {
{ "this", "Lnsk/jvmti/RedefineClasses/redefclass027a;", 0 },
{ "localInt1", "I", 1 }
};
static var_info method1[] = {
{ "this", "Lnsk/jvmti/RedefineClasses/redefclass027a;", 0 },
{ "argInt1", "I", 10 },
{ "localInt2", "I", 2 },
{ "ex", "Ljava/lang/Exception;", 0 }
};
static var_info pop[] = {
{ "this", "Lnsk/jvmti/RedefineClasses/redefclass027a;", 0 },
{ "argInt1", "I", 10 },
{ "localInt2", "I", 2 },
{ "ex", "Ljava/lang/Exception;", 0 }
};
static var_info method2[] = {
{ "this", "Lnsk/jvmti/RedefineClasses/redefclass027a;", 0 },
{ "argInt2", "I", 20 },
{ "localInt4", "I", 4 }
};
static frame_info frames[] = {
{ JNI_FALSE, "run", "()V", 97, 2, run, "bp" },
{ JNI_FALSE, "run", "()V", 97, 2, run, "step" },
{ JNI_FALSE, "run", "()V", 99, 2, run, "mod" },
{ JNI_FALSE, "method2", "(I)V", 116, 3, method2, "acc" },
{ JNI_FALSE, "method2", "(I)V", 117, 3, method2, "exc" },
{ JNI_FALSE, "method1", "(I)V", 108, 4, method1, "catch" },
{ JNI_FALSE, "method1", "(I)V", 112, 4, pop, "pop" },
};
void check(jvmtiEnv *jvmti_env, jthread thr, jclass cls, jmethodID mid,
jlocation loc, jint i) {
jvmtiError err;
char *sigClass, *name = nullptr, *sig = nullptr, *generic;
const char *desc;
jboolean is_obsolete;
jvmtiLineNumberEntry *lines = nullptr;
jvmtiLocalVariableEntry *table = nullptr;
jint line = -1;
jint entryCount = 0;
jint varValue = -1;
jint j, k;
char buffer[32];
if (i >= (jint) (sizeof(frames)/sizeof(frame_info))) {
printf("%d -- too many frames\n", i);
result = STATUS_FAILED;
return;
}
desc = frames[i].desc;
err = jvmti_env->GetClassSignature(cls, &sigClass, &generic);
if (err != JVMTI_ERROR_NONE) {
printf("(GetClassSignature#%s) unexpected error: %s (%d)\n",
desc, TranslateError(err), err);
result = STATUS_FAILED;
}
if (sigClass == nullptr || strcmp(sigClass, cls_exp) != 0) {
printf("(%s) wrong class sig: \"%s\",", desc, sigClass);
printf(" expected: \"%s\"\n", cls_exp);
if (sigClass != nullptr) {
jvmti_env->Deallocate((unsigned char *)sigClass);
}
result = STATUS_FAILED;
} else {
err = jvmti_env->GetMethodName(mid, &name, &sig, &generic);
if (err != JVMTI_ERROR_NONE) {
printf("(GetMethodName#%s) unexpected error: %s (%d)\n",
desc, TranslateError(err), err);
result = STATUS_FAILED;
}
err = jvmti_env->IsMethodObsolete(mid, &is_obsolete);
if (err != JVMTI_ERROR_NONE) {
printf("(IsMethodObsolete#%s) unexpected error: %s (%d)\n",
desc, TranslateError(err), err);
result = STATUS_FAILED;
}
if (printdump == JNI_TRUE) {
printf(">>> %s: \"%s.%s%s\"%s", desc, sigClass, name, sig,
(is_obsolete == JNI_TRUE) ? " (obsolete)" : "");
printf(", location=%s\n", jlong_to_string(loc, buffer));
}
if (frames[i].isObsolete != is_obsolete) {
printf("(%s) %s obsolete method\n", desc,
(is_obsolete == JNI_TRUE) ? "unexpected" : "should be");
result = STATUS_FAILED;
}
if (name == nullptr || strcmp(name, frames[i].name) != 0) {
printf("(%s) wrong method name: \"%s\",", desc, name);
printf(" expected: \"%s\"\n", frames[i].name);
result = STATUS_FAILED;
}
if (sig == nullptr || strcmp(sig, frames[i].sig) != 0) {
printf("(%s) wrong method sig: \"%s\",", desc, sig);
printf(" expected: \"%s\"\n", frames[i].sig);
result = STATUS_FAILED;
}
err = jvmti_env->GetLineNumberTable(mid,
&entryCount, &lines);
if (err != JVMTI_ERROR_NONE) {
printf("(GetLineNumberTable#%s) unexpected error: %s (%d)\n",
desc, TranslateError(err), err);
result = STATUS_FAILED;
}
if (lines != nullptr && entryCount > 0) {
for (k = 0; k < entryCount; k++) {
if (loc < lines[k].start_location) {
break;
}
}
line = lines[k-1].line_number;
}
if (line != frames[i].line) {
printf("(%s) wrong line number: %d, expected: %d\n",
desc, line, frames[i].line);
result = STATUS_FAILED;
}
err = jvmti_env->GetLocalVariableTable(mid,
&entryCount, &table);
if (err != JVMTI_ERROR_NONE) {
printf("(GetLocalVariableTable#%s) unexpected error: %s (%d)\n",
desc, TranslateError(err), err);
result = STATUS_FAILED;
}
if (frames[i].count != entryCount) {
printf("(%s) wrong number of locals: %d, expected: %d\n",
desc, entryCount, frames[i].count);
result = STATUS_FAILED;
}
if (table != nullptr) {
for (k = 0; k < frames[i].count; k++) {
for (j = 0; j < entryCount; j++) {
if (strcmp(table[j].name, frames[i].vars[k].name) == 0 &&
strcmp(table[j].signature, frames[i].vars[k].sig) == 0) {
if (printdump == JNI_TRUE) {
printf(">>> var \"%s:%s\": ",
table[j].name, table[j].signature);
printf("start_location=%s, length=%d",
jlong_to_string(table[j].start_location, buffer),
table[j].length);
}
if (table[j].signature[0] == 'I' &&
loc >= table[j].start_location &&
loc <= (table[j].start_location + table[j].length)) {
err = jvmti_env->GetLocalInt(thr,
0, table[j].slot, &varValue);
if (err != JVMTI_ERROR_NONE) {
printf("(GetLocalInt#%s) unexpected error: %s (%d)\n",
desc, TranslateError(err), err);
result = STATUS_FAILED;
}
else {
if (printdump == JNI_TRUE) {
printf(", tvalue=%d\n", varValue);
}
if (varValue != frames[i].vars[k].value) {
printf("(%s) wrong local var \"%s:%s\" value: %d,",
desc, table[j].name, table[j].signature,
varValue);
printf(" expected: %d\n", frames[i].vars[k].value);
result = STATUS_FAILED;
}
}
} else if (printdump == JNI_TRUE) {
printf("\n");
}
break;
}
}
if (j == entryCount) {
printf("(%s) var \"%s %s\" not found\n",
desc, frames[i].vars[k].name, frames[i].vars[k].sig);
result = STATUS_FAILED;
}
}
}
}
if (sigClass != nullptr) {
jvmti_env->Deallocate((unsigned char *)sigClass);
}
if (name != nullptr) {
jvmti_env->Deallocate((unsigned char *)name);
}
if (sig != nullptr) {
jvmti_env->Deallocate((unsigned char *)sig);
}
if (lines != nullptr) {
jvmti_env->Deallocate((unsigned char *)lines);
}
if (table != nullptr) {
for (j = 0; j < entryCount; j++) {
jvmti_env->Deallocate((unsigned char *)(table[j].name));
jvmti_env->Deallocate((unsigned char *)(table[j].signature));
}
jvmti_env->Deallocate((unsigned char *)table);
}
}
void redefine(jvmtiEnv *jvmti_env, JNIEnv *env, jclass cls) {
jvmtiClassDefinition classDef;
jvmtiError err;
classDef.klass = cls;
classDef.class_byte_count = env->GetArrayLength(classBytes);
classDef.class_bytes = (unsigned char *) env->GetByteArrayElements(classBytes, nullptr);
if (printdump == JNI_TRUE) {
printf(">>> about to call RedefineClasses %d\n", redefinesCount);
}
err = jvmti_env->RedefineClasses(1, &classDef);
if (err != JVMTI_ERROR_NONE) {
printf("(RedefineClasses#%d) unexpected error: %s (%d)\n",
redefinesCount, TranslateError(err), err);
result = STATUS_FAILED;
}
redefinesCount++;
}
void JNICALL Breakpoint(jvmtiEnv *jvmti_env, JNIEnv *env,
jthread thread, jmethodID method, jlocation location) {
jvmtiError err;
jclass klass;
if (midRun != method) {
printf("bp: don't know where we get called from\n");
result = STATUS_FAILED;
return;
}
if (printdump == JNI_TRUE) {
printf(">>> breakpoint in \"run\"\n");
}
err = jvmti_env->GetMethodDeclaringClass(method, &klass);
if (err != JVMTI_ERROR_NONE) {
printf("(GetMethodDeclaringClass) unexpected error: %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
return;
}
check(jvmti_env, thread, klass, method, location, 0);
bpEventsCount++;
err = jvmti_env->ClearBreakpoint(midRun, 0);
if (err != JVMTI_ERROR_NONE) {
printf("(ClearBreakpoint) unexpected error: %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
}
redefine(jvmti_env, (JNIEnv *)env, klass);
err = jvmti_env->SetEventNotificationMode(JVMTI_ENABLE,
JVMTI_EVENT_SINGLE_STEP, thread);
if (err == JVMTI_ERROR_NONE) {
stepEventsExpected++;
} else {
printf("Cannot enable single step: %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
}
}
void JNICALL SingleStep(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thread,
jmethodID method, jlocation location) {
jvmtiError err;
jclass klass;
if (midRun != method) {
printf("step: don't know where we get called from\n");
result = STATUS_FAILED;
return;
}
if (printdump == JNI_TRUE) {
printf(">>> single step in \"run\"\n");
}
err = jvmti_env->GetMethodDeclaringClass(method, &klass);
if (err != JVMTI_ERROR_NONE) {
printf("(GetMethodDeclaringClass) unexpected error: %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
return;
}
check(jvmti_env, thread, klass, method, location, 1);
stepEventsCount++;
err = jvmti_env->SetEventNotificationMode(JVMTI_DISABLE,
JVMTI_EVENT_SINGLE_STEP, thread);
if (err != JVMTI_ERROR_NONE) {
printf("Cannot disable single step: %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
}
redefine(jvmti_env, (JNIEnv *)env, klass);
if (caps.can_generate_exception_events) {
err = jvmti_env->SetEventNotificationMode(JVMTI_ENABLE,
JVMTI_EVENT_EXCEPTION, thread);
if (err == JVMTI_ERROR_NONE) {
exceptionEventsExpected++;
} else {
printf("Cannot enable exception: %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
}
}
}
void JNICALL FieldModification(jvmtiEnv *jvmti_env, JNIEnv *env,
jthread thread, jmethodID method, jlocation location,
jclass field_klass, jobject obj, jfieldID field, char sig, jvalue new_value) {
jvmtiError err;
jclass klass;
if (printdump == JNI_TRUE) {
printf(">>> field modification\n");
}
err = jvmti_env->GetMethodDeclaringClass(method, &klass);
if (err != JVMTI_ERROR_NONE) {
printf("(GetMethodDeclaringClass) unexpected error: %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
return;
}
check(jvmti_env, thread, klass, method, location, 2);
modificationEventsCount++;
redefine(jvmti_env, (JNIEnv *)env, klass);
}
void JNICALL FieldAccess(jvmtiEnv *jvmti_env, JNIEnv *env,
jthread thread, jmethodID method, jlocation location,
jclass field_klass, jobject obj, jfieldID field) {
jvmtiError err;
jclass klass;
if (printdump == JNI_TRUE) {
printf(">>> field access\n");
}
err = jvmti_env->GetMethodDeclaringClass(method, &klass);
if (err != JVMTI_ERROR_NONE) {
printf("(GetMethodDeclaringClass) unexpected error: %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
return;
}
check(jvmti_env, thread, klass, method, location, 3);
accessEventsCount++;
redefine(jvmti_env, (JNIEnv *)env, klass);
}
void JNICALL Exception(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thread,
jmethodID method, jlocation location, jobject exception,
jmethodID catch_method, jlocation catch_location) {
jvmtiError err;
jclass klass;
if (printdump == JNI_TRUE) {
printf(">>> exception\n");
}
err = jvmti_env->GetMethodDeclaringClass(method, &klass);
if (err != JVMTI_ERROR_NONE) {
printf("(GetMethodDeclaringClass) unexpected error: %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
return;
}
check(jvmti_env, thread, klass, method, location, 4);
exceptionEventsCount++;
err = jvmti_env->SetEventNotificationMode(JVMTI_DISABLE,
JVMTI_EVENT_EXCEPTION, thread);
if (err != JVMTI_ERROR_NONE) {
printf("Cannot disable exception: %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
}
redefine(jvmti_env, (JNIEnv *)env, klass);
err = jvmti_env->SetEventNotificationMode(JVMTI_ENABLE,
JVMTI_EVENT_EXCEPTION_CATCH, thread);
if (err == JVMTI_ERROR_NONE) {
catchEventsExpected++;
} else {
printf("Cannot enable exception catch: %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
}
}
void JNICALL ExceptionCatch(jvmtiEnv *jvmti_env, JNIEnv *env,
jthread thread, jmethodID method, jlocation location, jobject exception) {
jvmtiError err;
jclass klass;
if (printdump == JNI_TRUE) {
printf(">>> catch\n");
}
err = jvmti_env->GetMethodDeclaringClass(method, &klass);
if (err != JVMTI_ERROR_NONE) {
printf("(GetMethodDeclaringClass) unexpected error: %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
return;
}
check(jvmti_env, thread, klass, method, location, 5);
catchEventsCount++;
redefine(jvmti_env, (JNIEnv *)env, klass);
err = jvmti_env->SetEventNotificationMode(JVMTI_DISABLE,
JVMTI_EVENT_EXCEPTION_CATCH, thread);
if (err != JVMTI_ERROR_NONE) {
printf("Cannot disable exception catch: %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
}
if (caps.can_generate_frame_pop_events) {
err = jvmti_env->SetEventNotificationMode(JVMTI_ENABLE,
JVMTI_EVENT_FRAME_POP, nullptr);
if (err != JVMTI_ERROR_NONE) {
printf("Failed to enable FRAME_POP event: %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
} else {
err = jvmti_env->NotifyFramePop(thread, 0);
if (err != JVMTI_ERROR_NONE) {
printf("(NotifyFramePop) unexpected error: %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
} else {
popEventsExpected++;
}
}
}
}
void JNICALL FramePop(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thread,
jmethodID method, jboolean wasPopedByException) {
jvmtiError err;
jclass klass;
jmethodID mid;
jlocation loc;
if (printdump == JNI_TRUE) {
printf(">>> frame pop\n");
}
err = jvmti_env->GetFrameLocation(thread, 0, &mid, &loc);
if (err != JVMTI_ERROR_NONE) {
printf("(GetFrameLocation#pop) unexpected error: %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
}
err = jvmti_env->GetMethodDeclaringClass(method, &klass);
if (err != JVMTI_ERROR_NONE) {
printf("(GetMethodDeclaringClass) unexpected error: %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
return;
}
check(jvmti_env, thread, klass, method, loc, 6);
popEventsCount++;
redefine(jvmti_env, (JNIEnv *)env, klass);
}
#ifdef STATIC_BUILD
JNIEXPORT jint JNICALL Agent_OnLoad_redefclass027(JavaVM *jvm, char *options, void *reserved) {
return Agent_Initialize(jvm, options, reserved);
}
JNIEXPORT jint JNICALL Agent_OnAttach_redefclass027(JavaVM *jvm, char *options, void *reserved) {
return Agent_Initialize(jvm, options, reserved);
}
JNIEXPORT jint JNI_OnLoad_redefclass027(JavaVM *jvm, char *options, void *reserved) {
return JNI_VERSION_1_8;
}
#endif
jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
jvmtiError err;
jint res;
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_redefine_classes) {
printf("Warning: RedefineClasses is not implemented\n");
}
if (!caps.can_get_line_numbers) {
printf("Warning: GetLineNumberTable is not implemented\n");
}
if (!caps.can_access_local_variables) {
printf("Warning: access to local variables is not implemented\n");
}
if (caps.can_redefine_classes) {
if (caps.can_generate_breakpoint_events) {
callbacks.Breakpoint = &Breakpoint;
} else {
printf("Warning: Breakpoint event is not implemented\n");
}
if (caps.can_generate_single_step_events) {
callbacks.SingleStep = &SingleStep;
} else {
printf("Warning: SingleStep event is not implemented\n");
}
if (caps.can_generate_exception_events) {
callbacks.Exception = &Exception;
callbacks.ExceptionCatch = &ExceptionCatch;
} else {
printf("Warning: exception events are not implemented\n");
}
if (caps.can_generate_frame_pop_events) {
callbacks.FramePop = &FramePop;
} else {
printf("Warning: FramePop event is not implemented\n");
}
if (caps.can_generate_field_access_events) {
callbacks.FieldAccess = &FieldAccess;
} else {
printf("Warning: FieldAccess event is not implemented\n");
}
if (caps.can_generate_field_modification_events) {
callbacks.FieldModification = &FieldModification;
} else {
printf("Warning: FieldModification event is not implemented\n");
}
err = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
if (err != JVMTI_ERROR_NONE) {
printf("(SetEventCallbacks) unexpected error: %s (%d)\n",
TranslateError(err), err);
return JNI_ERR;
}
}
return JNI_OK;
}
JNIEXPORT void JNICALL
Java_nsk_jvmti_RedefineClasses_redefclass027_getReady(JNIEnv *env, jclass cls,
jclass clazz, jbyteArray bytes) {
jvmtiError err;
if (jvmti == nullptr) {
printf("JVMTI client was not properly loaded!\n");
result = STATUS_FAILED;
return;
}
if (!caps.can_redefine_classes ||
!caps.can_get_line_numbers ||
!caps.can_access_local_variables) return;
classBytes = (jbyteArray) env->NewGlobalRef(bytes);
midRun = env->GetMethodID(clazz, "run", "()V");
if (midRun == nullptr) {
printf("Cannot find Method ID for method run\n");
result = STATUS_FAILED;
}
mid1 = env->GetMethodID(clazz, "method1", "(I)V");
if (mid1 == nullptr) {
printf("Cannot find Method ID for method1\n");
result = STATUS_FAILED;
}
mid2 = env->GetMethodID(clazz, "method2", "(I)V");
if (mid2 == nullptr) {
printf("Cannot find Method ID for method2\n");
result = STATUS_FAILED;
}
if (caps.can_generate_breakpoint_events && midRun != nullptr) {
err = jvmti->SetBreakpoint(midRun, 0);
if (err != JVMTI_ERROR_NONE) {
printf("(SetBreakpoint) unexpected error: %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
} else {
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;
} else {
bpEventsExpected++;
}
}
}
fid1 = env->GetStaticFieldID(clazz, "staticInt", "I");
if (fid1 == nullptr) {
printf("Cannot find Field ID for staticInt\n");
result = STATUS_FAILED;
}
if (caps.can_generate_field_modification_events && fid1 != nullptr) {
err = jvmti->SetFieldModificationWatch(clazz, fid1);
if (err != JVMTI_ERROR_NONE) {
printf("(SetFieldModificationWatch) unexpected error: %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
} else {
err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,
JVMTI_EVENT_FIELD_MODIFICATION, nullptr);
if (err != JVMTI_ERROR_NONE) {
printf("Failed to enable FIELD_MODIFICATION event: %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
} else {
modificationEventsExpected++;
}
}
}
fid2 = env->GetFieldID(clazz, "instanceInt", "I");
if (fid2 == nullptr) {
printf("Cannot find Field ID for instanceInt\n");
result = STATUS_FAILED;
}
if (caps.can_generate_field_access_events && fid2 != nullptr) {
err = jvmti->SetFieldAccessWatch(clazz, fid2);
if (err != JVMTI_ERROR_NONE) {
printf("(SetFieldAccessWatch) unexpected error: %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
} else {
err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,
JVMTI_EVENT_FIELD_ACCESS, nullptr);
if (err != JVMTI_ERROR_NONE) {
printf("Failed to enable FIELD_ACCESS event: %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
} else {
accessEventsExpected++;
}
}
}
}
JNIEXPORT jint JNICALL
Java_nsk_jvmti_RedefineClasses_redefclass027_check(JNIEnv *env, jclass cls) {
if (bpEventsCount != bpEventsExpected) {
printf("Wrong number of breakpoint events: %d, expected: %d\n",
bpEventsCount, bpEventsExpected);
result = STATUS_FAILED;
}
if (stepEventsCount != stepEventsExpected) {
printf("Wrong number of step events: %d, expected: %d\n",
stepEventsCount, stepEventsExpected);
result = STATUS_FAILED;
}
if (modificationEventsCount != modificationEventsExpected) {
printf("Wrong number of modification watch events: %d, expected: %d\n",
modificationEventsCount, modificationEventsExpected);
result = STATUS_FAILED;
}
if (accessEventsCount != accessEventsExpected) {
printf("Wrong number of access watch events: %d, expected: %d\n",
accessEventsCount, accessEventsExpected);
result = STATUS_FAILED;
}
if (exceptionEventsCount != exceptionEventsExpected) {
printf("Wrong number of exception events: %d, expected: %d\n",
exceptionEventsCount, exceptionEventsExpected);
result = STATUS_FAILED;
}
if (catchEventsCount != catchEventsExpected) {
printf("Wrong number of catch exception events: %d, expected: %d\n",
catchEventsCount, catchEventsExpected);
result = STATUS_FAILED;
}
if (popEventsCount != popEventsExpected) {
printf("Wrong number of frame pop events: %d, expected: %d\n",
popEventsCount, popEventsExpected);
result = STATUS_FAILED;
}
return result;
}
}
|