我正在学习如何使用btrace。为此,我创建了一个包含以下代码的spring-boot项目。
@Controller
public class MainController {
private static Logger logger = LoggerFactory.getLogger(MainController.class);
@ResponseBody
@GetMapping("/testFile")
public Map<String, Object> testFile() throws IOException {
File file = new File("/tmp/a");
if (file.exists()) {
file.delete();
}
file.createNewFile();
return ImmutableMap.of("success", true);
}
}然后,我使用mvn spring-boot:run启动了该项目,之后我编写了一个btrace脚本,如下所示。
import com.sun.btrace.annotations.*;
import com.sun.btrace.BTraceUtils;
@BTrace
public class HelloWorld {
@OnMethod(clazz = "java.io.File", method = "createNewFile")
public static void onNewFileCreated(String fileName) {
BTraceUtils.println("New file is being created");
BTraceUtils.println(fileName);
}
}正如您所看到的,当调用java.io.File#createNewFile时,此脚本应该会打印一些内容,这正是上面的控制器所做的事情。然后,我使用以下代码将btrace附加到正在运行的spring-boot项目。
btrace 30716 HelloWorld.java30716是运行的spring-boot项目的PID。然后,我尝试访问http://localhost:8080/testFile,并从运行的spring-boot项目中获得以下额外输出。
objc[30857]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/bin/java (0x10e2744c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x1145e24e0). One of the two will be used. Which one is undefined.
2019-01-04 11:24:49.003 INFO 30857 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-01-04 11:24:49.003 INFO 30857 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2019-01-04 11:24:49.019 INFO 30857 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 16 ms我期望它输出New file is being created,但是它没有。为什么?我做错什么了吗?
发布于 2019-01-04 13:41:38
您的跟踪方法onNewFileCreated(String fileName)不能用于拦截java.io.File.createNewFile(),因为签名不一致(createNewFile()不接受任何参数,而onNewFileCreated()有一个)。如果跟踪方法中有参数(除非它们有BTrace注释),BTrace将尝试将它们“绑定”到被拦截的方法中的参数。如果它不能这样做,它将不会成功拦截该方法。
试一试
@OnMethod(clazz = "java.io.File", method = "createNewFile")
public static void onNewFileCreated() {
BTraceUtils.println("method createNewFile called");
}或
@OnMethod(clazz = "java.io.File", method = "createNewFile")
public static void onNewFileCreated(@ProbeMethodName String methodName) {
BTraceUtils.println("method " + methodName + " called");
}更新1:
首先,您使用的是什么版本的JDK?BTrace似乎不支持JDK >8 (https://github.com/btraceio/btrace/issues/292)。
其次,您是否可以尝试运行此跟踪脚本:
import com.sun.btrace.annotations.*;
import com.sun.btrace.BTraceUtils;
@BTrace
public class TracingScript {
@OnMethod(clazz = "java.io.File", method = "createNewFile")
public static void onNewFileCreated(@ProbeMethodName String methodName) {
BTraceUtils.println("method " + methodName + " called");
}
}针对一个简单的测试应用程序:
import java.io.File;
public class FileCreator {
public static void main(String[] args) throws Exception{
for(int i = 0; i < 250; i++) {
File file = new File("C://Temp//file" + i);
if (file.exists()) {
file.delete();
}
file.createNewFile();
Thread.sleep(10000);
}
}
}这适用于我的BTrace 1.3.11.3 (通过BTrace工作台JVisualVM插件0.6.8,这是我通常使用BTrace的地方)。
https://stackoverflow.com/questions/54032792
复制相似问题