Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

help!! btrace issue #685

Open
cf0415 opened this issue May 11, 2024 · 10 comments
Open

help!! btrace issue #685

cf0415 opened this issue May 11, 2024 · 10 comments

Comments

@cf0415
Copy link

cf0415 commented May 11, 2024

Hey, brother!
When I use the BTrace tool to trace methods in java.lang, I don't see any log output. I have already tried to use jdk version 8 , 11 and 17,the btrace version is v2.3.0. Can you provide any insights or suggestions to help me troubleshoot this issue? ?

Here are the steps I followed:

import java.io.IOException;
import java.util.Scanner;
import java.lang.Thread;

public class Demo {
public static void main(String[] args) throws InterruptedException {

     MyThread thread = new MyThread();
     thread.start();
     try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

    for (int i = 0; i < 100; i++) {
        System.out.println("Main thread executing");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

static class MyThread extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("Thread executing");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

}

And,I have used the trace script from the sample directory, as following:

import org.openjdk.btrace.core.annotations.BTrace;
import org.openjdk.btrace.core.annotations.OnMethod;
import org.openjdk.btrace.core.annotations.Self;

import static org.openjdk.btrace.core.BTraceUtils.*;

/*

  • This BTrace script inserts a probe into
  • method entry of java.lang.Thread.start() method.
  • At each Thread.start(), it raises a DTrace probe
  • in addition to printing the name of the thread.
  • A D-script like jthread.d may be used to get the
  • associated DTrace probe events.
    */
    @btrace
    public class ThreadStart {
    @OnMethod(
    clazz = "java.lang.Thread",
    method = "start"
    )
    public static void onnewThread(@self Thread t) {
    D.probe("jthreadstart", Threads.name(t));
    println("starting " + Threads.name(t));
    }
    }
@jbachorik
Copy link
Collaborator

Most probably the problem is trying to raise the D probe while not running on Solaris (I assume you are not running on Solaris).
Please, try using https://github.com/btraceio/btrace/blob/develop/btrace-dist/src/main/resources/samples/AllMethods1.java as an example and see if that fixes your problem.

@cf0415
Copy link
Author

cf0415 commented May 12, 2024

Most probably the problem is trying to raise the D probe while not running on Solaris (I assume you are not running on Solaris). Please, try using https://github.com/btraceio/btrace/blob/develop/btrace-dist/src/main/resources/samples/AllMethods1.java as an example and see if that fixes your problem.

The example provided in the URL, https://github.com/btraceio/btrace/blob/develop/btrace-dist/src/main/resources/samples/AllMethods1.java, works fine and can get trace logs. However, if you change clazz = "/javax.swing../" to clazz = "/java.lang../", there will be no any output. It seems that BTrace versions 2.1.0 and above do not support tracing java.lang, but BTrace versions below 2.0.2 can support it. My runtime environment is Ubuntu 20.04.

@cf0415
Copy link
Author

cf0415 commented May 13, 2024

The example provided in the URL, https://github.com/btraceio/btrace/blob/develop/btrace-dist/src/main/resources/samples/AllMethods1.java, works fine and can get trace logs. However, if you change clazz = "/javax.swing../" to clazz = "/java.lang../", there will be no any output. It seems that BTrace versions 2.1.0 and above do not support tracing java.lang, but BTrace versions below 2.0.2 can support it. My runtime environment is Ubuntu 20.04.

@jbachorik
Copy link
Collaborator

Ah, I see. Currently, BTrace is not able to trace java.lang.* classes - due to the necessity to use invoke dynamic in anything newer than Java 8 there is a good chance of deadlocking the indy bootstrap if some of the lang classes used from that bootstrap internally get instrumented and the instrumentation needs to bootstrap the binding again :/

@cf0415
Copy link
Author

cf0415 commented May 13, 2024

Ah, I see. Currently, BTrace is not able to trace java.lang.* classes - due to the necessity to use invoke dynamic in anything newer than Java 8 there is a good chance of deadlocking the indy bootstrap if some of the lang classes used from that bootstrap internally get instrumented and the instrumentation needs to bootstrap the binding again :/

Are there any good solutions or viable alternatives to address this issue currently?

@jbachorik
Copy link
Collaborator

No, not really. Except of patching and building BTrace yourself.
I did several attempts to relax the filter but it would take tracing the bootstrap process in detail to see which classes are involved and even then it would be brittle as the bootstrap process is an internal detail of JVM and can change from version to version.
An alternative would be to modify the BTrace instrumentation to add guard calls before calling invokedynamic (doing the checks in the generated probe code is too late) - that will increase the amount of disturbance to the instrumented method (adding more branches, potentially changing JIT decisions etc.) and as such I did not have enough motivation to proceed with that, just to allow tracing core Java classes ...

@cf0415
Copy link
Author

cf0415 commented May 13, 2024

No, not really. Except of patching and building BTrace yourself. I did several attempts to relax the filter but it would take tracing the bootstrap process in detail to see which classes are involved and even then it would be brittle as the bootstrap process is an internal detail of JVM and can change from version to version. An alternative would be to modify the BTrace instrumentation to add guard calls before calling invokedynamic (doing the checks in the generated probe code is too late) - that will increase the amount of disturbance to the instrumented method (adding more branches, potentially changing JIT decisions etc.) and as such I did not have enough motivation to proceed with that, just to allow tracing core Java classes ...

Okay,Another question: why BTrace v2.0.2 is unable to enable the unsafe mode?
btrace WARNING:Unable to load BTrace script...... And no any trace output.

@cf0415
Copy link
Author

cf0415 commented May 14, 2024

Okay,Another question: why BTrace v2.0.2 is unable to enable the unsafe mode?
btrace WARNING:Unable to load BTrace script...... And no any trace output.

Copy link

Stale issue message

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 22, 2024
@jbachorik jbachorik reopened this Nov 9, 2024
@jbachorik
Copy link
Collaborator

TBH, I don't know why unsafe mode does not work with 2.0.2

Does it work with 2.2.6 (the most recent version)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants