This repository has been archived on 2024-10-22. You can view files and clone it, but cannot push or open issues or pull requests.
AJP/DiSLProject2022/src-profiler/ex11/Instrumentation.java

74 lines
2.8 KiB
Java
Raw Permalink Normal View History

2023-01-02 09:46:59 +00:00
package ex11;
import ch.usi.dag.disl.annotation.Before;
2023-01-20 17:26:55 +00:00
import ch.usi.dag.disl.annotation.ThreadLocal;
import ch.usi.dag.disl.marker.BodyMarker;
import ch.usi.dag.disl.marker.BytecodeMarker;
import ch.usi.dag.disl.staticcontext.InstructionStaticContext;
2023-01-20 17:26:55 +00:00
import ch.usi.dag.disl.staticcontext.InvocationStaticContext;
import ch.usi.dag.disl.staticcontext.MethodStaticContext;
2023-01-02 09:46:59 +00:00
public class Instrumentation {
2023-01-20 17:26:55 +00:00
@ThreadLocal
private static int opCode;
2023-01-20 17:26:55 +00:00
@ThreadLocal
private static int index;
2023-01-20 17:26:55 +00:00
@ThreadLocal
private static String callee;
2023-01-20 17:26:55 +00:00
@ThreadLocal
private static String caller;
@ThreadLocal
private static boolean initialized;
@Before(marker = BytecodeMarker.class, args = "invokevirtual, invokeinterface")
static void atObjectRefCallSite(final InstructionStaticContext isc,
final MethodStaticContext msc,
final InvocationStaticContext ivc) {
opCode = isc.getOpcode();
index = isc.getIndex();
caller = msc.thisMethodFullName().concat(msc.thisMethodDescriptor());
callee = ivc.getInternalName().concat(ivc.getDescriptor());
initialized = true;
}
@Before(marker = BytecodeMarker.class, args = "invokedynamic")
static void atObjectRefCallSiteDynamic(final InstructionStaticContext isc,
final MethodStaticContext msc) {
2023-01-20 17:26:55 +00:00
opCode = isc.getOpcode();
index = isc.getIndex();
caller = msc.thisMethodFullName().concat(msc.thisMethodDescriptor());
callee = "[UNDETERMINED]";
initialized = true;
}
@Before(marker = BodyMarker.class, guard = IsDynamicTargetGuard.class)
2023-01-20 17:26:55 +00:00
static void beforeMethod(MethodStaticContext msc) {
// we can ignore here constructors and private methods since we know they must be invoked with "invokespecial"
// and static methods since they are called with "invokestatic" which performs static binding.
// Instrumenting the constructor of java.lang.Object would actually cause a segmentation fault of the JVM.
2023-01-20 17:26:55 +00:00
if (initialized) {
Profiler.registerCall(new CallInfo(opCode, index, caller, callee, msc.getUniqueInternalName()));
initialized = false;
}
}
2023-01-20 17:26:55 +00:00
@Before(marker = BytecodeMarker.class, args = "invokestatic, invokespecial")
static void atStaticCallSite(final InstructionStaticContext isc,
final MethodStaticContext msc,
2023-01-20 17:26:55 +00:00
final InvocationStaticContext ivc) {
final String v = ivc.getInternalName().concat(ivc.getDescriptor());
Profiler.registerCall(new CallInfo(
isc.getOpcode(),
isc.getIndex(),
msc.thisMethodFullName().concat(msc.thisMethodDescriptor()),
2023-01-20 17:26:55 +00:00
v,
v
));
}
2023-01-02 09:46:59 +00:00
}