/com.oracle.max.tests/src/test/output/MixedFrames.java

https://github.com/chaoyangnz/maxine · Java · 94 lines · 55 code · 10 blank · 29 comment · 11 complexity · 356614b0580888a6a0ef6b710b341570 MD5 · raw file

  1. /*
  2. * Copyright (c) 2009, 2012, 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.
  8. *
  9. * This code is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * version 2 for more details (a copy is included in the LICENSE file that
  13. * accompanied this code).
  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. package test.output;
  24. import sun.reflect.*;
  25. /**
  26. * Performs a number of recursive calls that go in and out of native code
  27. * until a fixed recursion level is reached. Then the stack frame trace
  28. * is compared with reference VM.
  29. */
  30. public class MixedFrames {
  31. static {
  32. System.loadLibrary("javatest");
  33. }
  34. public static void main(String[] args) {
  35. MixedFrames mixedFrames = new MixedFrames();
  36. mixedFrames.testNative(50);
  37. }
  38. private int i;
  39. private Object o;
  40. native void nativeUpdateFields(int recursion, int i, Object o);
  41. static class OtherClass {
  42. static void updateFields(MixedFrames mf, int recursion, int i) {
  43. mf.nativeUpdateFields(recursion, i, mf);
  44. }
  45. }
  46. private void testNative(int recursion) {
  47. final int intValue = 42;
  48. if (recursion > 0) {
  49. OtherClass.updateFields(this, recursion - 1, intValue);
  50. return;
  51. }
  52. System.gc();
  53. if (i != intValue) {
  54. System.out.println(i + " != " + intValue);
  55. }
  56. if (o != this) {
  57. System.out.println(o + " != " + this);
  58. }
  59. Throwable throwable = new Throwable();
  60. System.out.println("Trace of mixed frame stack:");
  61. for (StackTraceElement e : throwable.getStackTrace()) {
  62. String frame = e.toString();
  63. // Filter frames until stack trace construction in Maxine omits all the necessary system frames.
  64. if (frame.startsWith(MixedFrames.class.getName())) {
  65. System.out.println("\tat " + e);
  66. }
  67. }
  68. int count = 0;
  69. int prefix = 0;
  70. while (true) {
  71. @SuppressWarnings("deprecation")
  72. Class callerClass = Reflection.getCallerClass(count);
  73. if (callerClass == null) {
  74. break;
  75. }
  76. // Filter frames until stack trace construction in Maxine omits all the necessary system frames.
  77. if (callerClass.getName().startsWith(MixedFrames.class.getName())) {
  78. System.out.println(prefix + ": " + callerClass);
  79. prefix++;
  80. }
  81. count++;
  82. }
  83. }
  84. }