Java Code Examples for jdk.jfr.consumer.RecordedStackTrace#getFrames()

The following examples show how to use jdk.jfr.consumer.RecordedStackTrace#getFrames() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PrettyWriter.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private void printStackTrace(RecordedStackTrace stackTrace) {
    println("[");
    List<RecordedFrame> frames = stackTrace.getFrames();
    indent();
    int i = 0;
    while (i < frames.size() && i < getStackDepth()) {
        RecordedFrame frame = frames.get(i);
        if (frame.isJavaFrame()) {
            printIndent();
            printValue(frame, null, "");
            println();
            i++;
        }
    }
    if (stackTrace.isTruncated() || i == getStackDepth()) {
        printIndent();
        println("...");
    }
    retract();
    printIndent();
    println("]");
}
 
Example 2
Source File: TestRecordedMethodDescriptor.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Throwable {
    Recording recording = new Recording();
    recording.enable(MyEvent.class).withStackTrace();
    recording.start();

    MyEvent event = new MyEvent();
    event.begin();
    event.end();
    event.commit();
    recording.stop();

    List<RecordedEvent> recordedEvents = Events.fromRecording(recording);
    assertEquals(1, recordedEvents.size(), "Expected one event");
    RecordedEvent recordedEvent = recordedEvents.get(0);

    RecordedStackTrace stacktrace = recordedEvent.getStackTrace();
    List<RecordedFrame> frames = stacktrace.getFrames();
    assertFalse(frames.isEmpty(), "Stacktrace frames was empty");
    for (RecordedFrame frame : frames) {
        analyzeRecordedMethodDescriptor(frame.getMethod());
    }

    assertTrue(isMainMethodDescriptorRecorded, "main() method descriptor has never been recorded");
}
 
Example 3
Source File: AllocationStackTrace.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static boolean matchingStackTrace(RecordedStackTrace stack, String[] expectedStack) {
    if (stack == null) {
        return false;
    }

    List<RecordedFrame> frames = stack.getFrames();
    int pos = findFramePos(frames, expectedStack[0]);

    if (pos == -1) {
        return false;
    }

    for (String expectedFrame : expectedStack) {
        RecordedFrame f = frames.get(pos++);
        String frame = frameToString(f);

        if (!frame.equals(expectedFrame)) {
            return false;
        }
    }

    return true;
}
 
Example 4
Source File: TestRecordedMethodDescriptor.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Throwable {
    Recording recording = new Recording();
    recording.enable(MyEvent.class).withStackTrace();
    recording.start();

    MyEvent event = new MyEvent();
    event.begin();
    event.end();
    event.commit();
    recording.stop();

    List<RecordedEvent> recordedEvents = Events.fromRecording(recording);
    assertEquals(1, recordedEvents.size(), "Expected one event");
    RecordedEvent recordedEvent = recordedEvents.get(0);

    RecordedStackTrace stacktrace = recordedEvent.getStackTrace();
    List<RecordedFrame> frames = stacktrace.getFrames();
    assertFalse(frames.isEmpty(), "Stacktrace frames was empty");
    for (RecordedFrame frame : frames) {
        analyzeRecordedMethodDescriptor(frame.getMethod());
    }

    assertTrue(isMainMethodDescriptorRecorded, "main() method descriptor has never been recorded");
}
 
Example 5
Source File: OldObjects.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static boolean matchingStackTrace(RecordedStackTrace stack, String[] expectedStack) {
    if (stack == null) {
        return false;
    }

    List<RecordedFrame> frames = stack.getFrames();
    int pos = findFramePos(frames, expectedStack[0]);

    if (pos == -1) {
        return false;
    }

    for (String expectedFrame : expectedStack) {
        RecordedFrame f = frames.get(pos++);
        String frame = frameToString(f);

        if (!frame.contains(expectedFrame)) {
            return false;
        }
    }
    return true;
}
 
Example 6
Source File: OldObjects.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static boolean matchingStackTrace(RecordedStackTrace stack, String[] expectedStack) {
    if (stack == null) {
        return false;
    }

    List<RecordedFrame> frames = stack.getFrames();
    int pos = findFramePos(frames, expectedStack[0]);

    if (pos == -1) {
        return false;
    }

    for (String expectedFrame : expectedStack) {
        RecordedFrame f = frames.get(pos++);
        String frame = frameToString(f);

        if (!frame.contains(expectedFrame)) {
            return false;
        }
    }
    return true;
}
 
Example 7
Source File: OldObjects.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public static boolean matchingStackTrace(RecordedStackTrace stack, String[] expectedStack) {
    if (stack == null) {
        return false;
    }

    List<RecordedFrame> frames = stack.getFrames();
    int pos = findFramePos(frames, expectedStack[0]);

    if (pos == -1) {
        return false;
    }

    for (String expectedFrame : expectedStack) {
        RecordedFrame f = frames.get(pos++);
        String frame = frameToString(f);

        if (!frame.contains(expectedFrame)) {
            return false;
        }
    }
    return true;
}
 
Example 8
Source File: AllocationStackTrace.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private static boolean matchingStackTrace(RecordedStackTrace stack, String[] expectedStack) {
    if (stack == null) {
        return false;
    }

    List<RecordedFrame> frames = stack.getFrames();
    int pos = findFramePos(frames, expectedStack[0]);

    if (pos == -1) {
        return false;
    }

    for (String expectedFrame : expectedStack) {
        RecordedFrame f = frames.get(pos++);
        String frame = frameToString(f);

        if (!frame.equals(expectedFrame)) {
            return false;
        }
    }

    return true;
}
 
Example 9
Source File: AllocationStackTrace.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private static boolean matchingStackTrace(RecordedStackTrace stack, String[] expectedStack) {
    if (stack == null) {
        return false;
    }

    List<RecordedFrame> frames = stack.getFrames();
    int pos = findFramePos(frames, expectedStack[0]);

    if (pos == -1) {
        return false;
    }

    for (String expectedFrame : expectedStack) {
        RecordedFrame f = frames.get(pos++);
        String frame = frameToString(f);

        if (!frame.equals(expectedFrame)) {
            return false;
        }
    }

    return true;
}
 
Example 10
Source File: TestMetadataRetention.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void assertStackTrace(RecordedStackTrace stacktrace, final String methodName) {
    for (RecordedFrame f : stacktrace.getFrames()) {
        if (f.getMethod().getName().equals(methodName)) {
            return;
        }
    }
    Asserts.fail("Could not find class " + methodName + " in stack trace " + stacktrace);
}
 
Example 11
Source File: TestGetStackTrace.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void assertStackTrace(RecordedEvent re) {
    assertNotNull(re.getStackTrace());
    RecordedStackTrace strace = re.getStackTrace();
    assertEquals(strace.isTruncated(), false);
    List<RecordedFrame> frames = strace.getFrames();
    assertTrue(frames.size() > 0);
    for (RecordedFrame frame : frames) {
        assertFrame(frame);
    }
}
 
Example 12
Source File: TestMethodGetModifiers.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Throwable {
    Recording recording = new Recording();
    recording.start();

    SimpleEvent ev = new SimpleEvent();
    ev.commit();
    recording.stop();

    List<RecordedEvent> recordedEvents = Events.fromRecording(recording);
    Events.hasEvents(recordedEvents);
    RecordedEvent recordedEvent = recordedEvents.get(0);

    System.out.println("recorded event:" + recordedEvent);

    RecordedStackTrace stacktrace = recordedEvent.getStackTrace();
    List<RecordedFrame> frames = stacktrace.getFrames();
    for (RecordedFrame frame : frames) {
        RecordedMethod method = frame.getMethod();
        if (method.getName().equals("main")) {
            System.out.println("'main' method: " + method);
            int modifiers = TestMethodGetModifiers.class.getDeclaredMethod("main", (Class<?>)String[].class).getModifiers();
            System.out.println("modifiers: " + modifiers);
            Asserts.assertEquals(method.getModifiers(), modifiers, "Incorrect method modifier reported");
            RecordedClass type = method.getType();
            assertNotNull(type, "Recorded class can not be null");
        }

    }
}
 
Example 13
Source File: TestMethodGetModifiers.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Throwable {
    Recording recording = new Recording();
    recording.start();

    SimpleEvent ev = new SimpleEvent();
    ev.commit();
    recording.stop();

    List<RecordedEvent> recordedEvents = Events.fromRecording(recording);
    Events.hasEvents(recordedEvents);
    RecordedEvent recordedEvent = recordedEvents.get(0);

    System.out.println("recorded event:" + recordedEvent);

    RecordedStackTrace stacktrace = recordedEvent.getStackTrace();
    List<RecordedFrame> frames = stacktrace.getFrames();
    for (RecordedFrame frame : frames) {
        RecordedMethod method = frame.getMethod();
        if (method.getName().equals("main")) {
            System.out.println("'main' method: " + method);
            int modifiers = TestMethodGetModifiers.class.getDeclaredMethod("main", (Class<?>)String[].class).getModifiers();
            System.out.println("modifiers: " + modifiers);
            Asserts.assertEquals(method.getModifiers(), modifiers, "Incorrect method modifier reported");
            RecordedClass type = method.getType();
            assertNotNull(type, "Recorded class can not be null");
        }

    }
}
 
Example 14
Source File: TestFullStackTrace.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void checkEvent(RecordedEvent event, int expectedDepth) throws Throwable {
    RecordedStackTrace stacktrace = null;
    try {
        stacktrace = event.getStackTrace();
        List<RecordedFrame> frames = stacktrace.getFrames();
        Asserts.assertEquals(Math.min(MAX_DEPTH, expectedDepth), frames.size(), "Wrong stacktrace depth. Expected:" + expectedDepth);
        List<String> expectedMethods = getExpectedMethods(expectedDepth);
        Asserts.assertEquals(expectedMethods.size(), frames.size(), "Wrong expectedMethods depth. Test error.");

        for (int i = 0; i < frames.size(); ++i) {
            String name = frames.get(i).getMethod().getName();
            String expectedName = expectedMethods.get(i);
            System.out.printf("method[%d]=%s, expected=%s%n", i, name, expectedName);
            Asserts.assertEquals(name, expectedName, "Wrong method name");
        }

        boolean isTruncated = stacktrace.isTruncated();
        boolean isTruncateExpected = expectedDepth > MAX_DEPTH;
        Asserts.assertEquals(isTruncated, isTruncateExpected, "Wrong value for isTruncated. Expected:" + isTruncateExpected);

        String firstMethod = frames.get(frames.size() - 1).getMethod().getName();
        boolean isFullTrace = "run".equals(firstMethod);
        String msg = String.format("Wrong values for isTruncated=%b, isFullTrace=%b", isTruncated, isFullTrace);
        Asserts.assertTrue(isTruncated != isFullTrace, msg);
    } catch (Throwable t) {
        System.out.println(String.format("stacktrace:%n%s", stacktrace));
        throw t;
    }
}
 
Example 15
Source File: StressAllocationGCEvents.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private void checkEvent(RecordedEvent event) throws Exception {
    // skip check if allocation failure comes not from diver

    RecordedThread thread = event.getThread();
    String threadName = thread.getJavaName();

    if (!threadName.contains(THREAD_NAME)) {
        System.out.println("Skip event not from pool (from internals)");
        System.out.println(" Thread Id: " + thread.getJavaThreadId()
                + " Thread name: " + threadName);
        return;
    }

    RecordedStackTrace stackTrace = event.getStackTrace();

    List<RecordedFrame> frames = stackTrace.getFrames();
    //String[] stacktrace = StackTraceHelper.buildStackTraceFromFrames(frames);

    if (!(frames.get(0).getMethod().getName().equals(DIVER_FRAME_NAME))) {
        System.out.println("Skip stacktrace check for: \n"
                + String.join("\n", threadName));
        return;
    }

    assertTrue(frames.size() > RECURSION_DEPTH,
            "Stack trace should contain at least one more entry than the ones generated by the test recursion");
    for (int i = 0; i < RECURSION_DEPTH; i++) {
        assertEquals(frames.get(i).getMethod().getName(), DIVER_FRAME_NAME,
                "Frame " + i + " is wrong: \n"
                + String.join("\n", threadName));
    }
    assertNotEquals(frames.get(RECURSION_DEPTH).getMethod().getName(), DIVER_FRAME_NAME,
            "Too many diver frames: \n"
            + String.join("\n", threadName));
}
 
Example 16
Source File: TestGetStackTrace.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void assertStackTrace(RecordedEvent re) {
    assertNotNull(re.getStackTrace());
    RecordedStackTrace strace = re.getStackTrace();
    assertEquals(strace.isTruncated(), false);
    List<RecordedFrame> frames = strace.getFrames();
    assertTrue(frames.size() > 0);
    for (RecordedFrame frame : frames) {
        assertFrame(frame);
    }
}
 
Example 17
Source File: TestGetStackTrace.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static void assertStackTrace(RecordedEvent re) {
    assertNotNull(re.getStackTrace());
    RecordedStackTrace strace = re.getStackTrace();
    assertEquals(strace.isTruncated(), false);
    List<RecordedFrame> frames = strace.getFrames();
    assertTrue(frames.size() > 0);
    for (RecordedFrame frame : frames) {
        assertFrame(frame);
    }
}
 
Example 18
Source File: TestRecordedFullStackTrace.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void checkEvent(RecordedEvent event, int expectedDepth) throws Throwable {
    RecordedStackTrace stacktrace = null;
    try {
        stacktrace = event.getStackTrace();
        List<RecordedFrame> frames = stacktrace.getFrames();
        Asserts.assertEquals(Math.min(MAX_DEPTH, expectedDepth), frames.size(), "Wrong stacktrace depth. Expected:" + expectedDepth);
        List<String> expectedMethods = getExpectedMethods(expectedDepth);
        Asserts.assertEquals(expectedMethods.size(), frames.size(), "Wrong expectedMethods depth. Test error.");

        for (int i = 0; i < frames.size(); ++i) {
            String name = frames.get(i).getMethod().getName();
            String expectedName = expectedMethods.get(i);
            System.out.printf("method[%d]=%s, expected=%s%n", i, name, expectedName);
            Asserts.assertEquals(name, expectedName, "Wrong method name");
        }

        boolean isTruncated = stacktrace.isTruncated();
        boolean isTruncateExpected = expectedDepth > MAX_DEPTH;
        Asserts.assertEquals(isTruncated, isTruncateExpected, "Wrong value for isTruncated. Expected:" + isTruncateExpected);

        String firstMethod = frames.get(frames.size() - 1).getMethod().getName();
        boolean isFullTrace = "run".equals(firstMethod);
        String msg = String.format("Wrong values for isTruncated=%b, isFullTrace=%b", isTruncated, isFullTrace);
        Asserts.assertTrue(isTruncated != isFullTrace, msg);
    } catch (Throwable t) {
        System.out.println(String.format("stacktrace:%n%s", stacktrace));
        throw t;
    }
}
 
Example 19
Source File: TestShutdownEvent.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
static void validateStackTrace(RecordedStackTrace stackTrace) {
    List<RecordedFrame> frames = stackTrace.getFrames();
    Asserts.assertFalse(frames.isEmpty());
    Asserts.assertTrue(frames.stream()
                       .anyMatch(t -> t.getMethod().getName().equals("runTest")));
}
 
Example 20
Source File: TestRecordedFrame.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public static void doTest(int lineNumber) throws IOException {

        System.out.println("Enetring method");

        Recording recording = new Recording();
        recording.start();

        SimpleEvent ev = new SimpleEvent();
        commitEvent(ev);
        recording.stop();

        List<RecordedEvent> recordedEvents = Events.fromRecording(recording);
        Events.hasEvents(recordedEvents);
        RecordedEvent recordedEvent = recordedEvents.get(0);

        RecordedStackTrace stacktrace = recordedEvent.getStackTrace();
        List<RecordedFrame> frames = stacktrace.getFrames();
        for (RecordedFrame frame : frames) {

            // All frames are java frames
            Asserts.assertTrue(frame.isJavaFrame());
            // Verify the main() method frame
            RecordedMethod method = frame.getMethod();
            if (method.getName().equals("main")) {

                // Frame type
                String type = frame.getType();
                System.out.println("type: " + type);
                Asserts.assertTrue(
                        type.equals("Interpreted")
                        || type.equals("JIT compiled")
                        || type.equals("Inlined"));

                Asserts.assertEquals(lineNumber, frame.getLineNumber());

                boolean isInterpreted = "Interpreted".equals(type);
                boolean expectedInterpreted = "true".equals(System.getProperty("interpreted"));
                Asserts.assertEquals(isInterpreted, expectedInterpreted);

                int bci = frame.getBytecodeIndex();

                System.out.println("bci: " + bci);
                Asserts.assertTrue(bci > 0);
            }

        }

    }