Skip to content

Commit ddfe2e8

Browse files
authored
Have a stable symbol to test ffm tracing (#11906)
Have a stable symbol to test ffm tracing Co-authored-by: andrea.marziali <andrea.marziali@datadoghq.com>
1 parent 34ff871 commit ddfe2e8

1 file changed

Lines changed: 17 additions & 43 deletions

File tree

dd-java-agent/instrumentation/java/java-lang/java-lang-22.0/src/test/groovy/FFMInstrumentationForkedTest.groovy

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -136,58 +136,32 @@ class FFMInstrumentationForkedTest extends InstrumentationSpecification {
136136

137137
def "should trace ffm calls using libraryLookup by path for library loaded with System.load"() {
138138
setup:
139-
injectSysConfig("trace.native.methods", "libjvm[*]")
140-
final String libName = System.mapLibraryName("jvm")
141-
final Path libPath = Path.of(System.getProperty("java.home"), "lib", "server", libName)
139+
injectSysConfig("trace.native.methods", "libdt_socket[*]")
140+
final String libName = System.mapLibraryName("dt_socket")
141+
final Path libPath = Path.of(System.getProperty("java.home"), "lib", libName)
142142

143143
when:
144144
LoaderUtil.loadLibrary(libPath)
145-
try (final Arena arena = Arena.ofConfined()) {
146-
final SymbolLookup libLookup = LoaderUtil.loaderLookup()
147-
FunctionDescriptor fd = FunctionDescriptor.of(
148-
ValueLayout.JAVA_INT, // jint return
149-
ValueLayout.ADDRESS, // JavaVM**
150-
ValueLayout.JAVA_INT, // jsize bufLen
151-
ValueLayout.ADDRESS // jsize* nVMs
152-
)
153-
154-
// this is a quite stable symbol (it's in the public API)
155-
MemorySegment sym =
156-
libLookup.find("JNI_GetCreatedJavaVMs")
157-
.orElseThrow()
158-
159-
MethodHandle methodHandle = Linker.nativeLinker().downcallHandle(sym, fd)
160-
161-
// Allocate space for JavaVM* (we only expect 1 VM)
162-
MemorySegment vmBuf = arena.allocate(ValueLayout.ADDRESS)
163-
164-
// Allocate space for jsize nVMs
165-
MemorySegment nVMs = arena.allocate(ValueLayout.JAVA_INT)
166-
167-
int result = (int) methodHandle.invokeWithArguments(
168-
vmBuf,
169-
1,
170-
nVMs
171-
)
172-
173-
int count = nVMs.get(ValueLayout.JAVA_INT, 0)
174-
175-
System.out.println("Return code: " + result)
176-
System.out.println("Number of VMs: " + count)
177-
178-
if (count > 0) {
179-
MemorySegment vmPtr = vmBuf.get(ValueLayout.ADDRESS, 0)
180-
System.out.println("JavaVM pointer: " + vmPtr)
181-
}
182-
}
183-
145+
final SymbolLookup libLookup = LoaderUtil.loaderLookup()
146+
final MemorySegment onLoadAddr = libLookup.findOrThrow("jdwpTransport_OnLoad")
147+
final MethodHandle onLoadHandle =
148+
Linker.nativeLinker().downcallHandle(
149+
onLoadAddr,
150+
FunctionDescriptor.of(
151+
ValueLayout.JAVA_INT,
152+
ValueLayout.ADDRESS,
153+
ValueLayout.ADDRESS,
154+
ValueLayout.JAVA_INT,
155+
ValueLayout.ADDRESS))
156+
// version 0 is rejected before any argument is dereferenced
157+
onLoadHandle.invokeWithArguments(MemorySegment.NULL, MemorySegment.NULL, 0, MemorySegment.NULL)
184158

185159
then:
186160
assertTraces(1) {
187161
trace(1) {
188162
span {
189163
operationName "trace.native"
190-
resourceName "libjvm.JNI_GetCreatedJavaVMs"
164+
resourceName "libdt_socket.jdwpTransport_OnLoad"
191165
tags {
192166
"$Tags.COMPONENT" "trace-ffm"
193167
defaultTags()

0 commit comments

Comments
 (0)