Skip to content

Commit

Permalink
Extend TestStackTraceEvent to include method signature and modifiers …
Browse files Browse the repository at this point in the history
…checks.
  • Loading branch information
jovanstevanovic committed May 23, 2024
1 parent 5d849ca commit ac9eab3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -458,24 +458,24 @@ static void fillSourceFields(FrameInfoQueryResult result) {
VMError.guarantee(methodIndex >= 0 && methodIndex < NonmovableArrays.lengthOf(methodEncodings) / entryBytes);

Pointer p = NonmovableArrays.addressOf(methodEncodings, methodIndex * entryBytes);
int classIndex = readSourceFieldOffset(p, shortClass, classOffset);
int classIndex = readIndex(p, shortClass, classOffset);
Class<?> sourceClass = NonmovableArrays.getObject(CodeInfoAccess.getClasses(info), classIndex);
int methodNameIndex = readSourceFieldOffset(p, shortName, nameOffset);
int methodNameIndex = readIndex(p, shortName, nameOffset);
String sourceMethodName = NonmovableArrays.getObject(CodeInfoAccess.getMemberNames(info), methodNameIndex);

String sourceMethodSignature = CodeInfoEncoder.Encoders.INVALID_METHOD_SIGNATURE;
int sourceSignatureModifiers = CodeInfoEncoder.Encoders.INVALID_METHOD_MODIFIERS;
if (CodeInfoEncoder.shouldEncodeAllMethodMetadata()) {
int sourceSignatureIndex = readSourceFieldOffset(p, shortSignature, signatureOffset);
int sourceSignatureIndex = readIndex(p, shortSignature, signatureOffset);
sourceMethodSignature = NonmovableArrays.getObject(CodeInfoAccess.getOtherStrings(info), sourceSignatureIndex);

sourceSignatureModifiers = readSourceFieldOffset(p, true, modifierOffset);
sourceSignatureModifiers = readIndex(p, true, modifierOffset);
}
result.setSourceFields(sourceClass, sourceMethodName, sourceMethodSignature, sourceSignatureModifiers);
}

@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
private static int readSourceFieldOffset(Pointer p, boolean isShort, int offset) {
private static int readIndex(Pointer p, boolean isShort, int offset) {
return isShort ? (p.readShort(offset) & 0xffff) : p.readInt(offset);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,35 @@
package com.oracle.svm.test.jfr;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import jdk.jfr.consumer.RecordedThread;
import org.junit.Assert;
import org.junit.Test;

import com.oracle.svm.test.jfr.events.StackTraceEvent;

import jdk.jfr.Recording;
import jdk.jfr.consumer.RecordedEvent;
import jdk.jfr.consumer.RecordedFrame;
import jdk.jfr.consumer.RecordedMethod;
import jdk.jfr.consumer.RecordedStackTrace;

/**
* Test if event ({@link StackTraceEvent}) with stacktrace payload is working.
*/
public class TestStackTraceEvent extends JfrRecordingTest {

private static final JfrSeenMethod junitTest = new JfrSeenMethod("test", "()V", 1);
private static final JfrSeenMethod svmJunitMain = new JfrSeenMethod("main", "([Ljava/lang/String;)V", 9);
private static final JfrSeenMethod javaMainRun = new JfrSeenMethod("doRun", "(ILorg/graalvm/nativeimage/c/type/CCharPointerPointer;)I", 10);

@Test
public void test() throws Throwable {
String[] events = new String[]{StackTraceEvent.class.getName()};
Expand All @@ -57,5 +72,42 @@ public void test() throws Throwable {

private static void validateEvents(List<RecordedEvent> events) {
assertEquals(1, events.size());
RecordedEvent event = events.getFirst();

long sampledThreadId = event.<RecordedThread> getValue("eventThread").getJavaThreadId();
assertTrue(sampledThreadId > 0);

RecordedStackTrace stackTrace = event.getStackTrace();
assertNotNull(stackTrace);

List<RecordedFrame> frames = stackTrace.getFrames();
assertFalse(frames.isEmpty());

Set<JfrSeenMethod> seenMethod = new HashSet<>();
for (RecordedFrame frame : frames) {
RecordedMethod method = frame.getMethod();
assertNotNull(method);

String methodName = method.getName();
assertNotNull(methodName);
assertFalse(methodName.isEmpty());

String methodDescriptor = method.getDescriptor();
assertNotNull(methodDescriptor);
assertFalse(methodDescriptor.isEmpty());

int methodModifiers = method.getModifiers();
assertTrue(methodModifiers >= 0);

seenMethod.add(new JfrSeenMethod(methodName, methodDescriptor, methodModifiers));
}

Assert.assertTrue(seenMethod.contains(junitTest));
Assert.assertTrue(seenMethod.contains(javaMainRun));
Assert.assertTrue(seenMethod.contains(svmJunitMain));
}

private record JfrSeenMethod(String name, String descriptor, int modifier) {

}
}

0 comments on commit ac9eab3

Please sign in to comment.