Byteman - trace all classes and methods in a given package

1.5k views Asked by At

When using Byteman, we have to specify the class and the method in the rule syntax. What if I want to trace program execution using Byteman?

Example: I do not know which methods are being executed when executing a feature of the program. I want to identify the called methods during the feature execution.

Does this mean that I've to add a rule for each method of each class in a given package? Or is there any other way to achieve this?

2

There are 2 answers

0
Dimitar II On

It looks like Byteman is not the right tool for what you want. Better results would be provided by: Understand or JProfiler. Especially in multi-threading environment.

0
Andrew Dinn On

Yes, essentially you need a rule for every method you want to trace (although there is an easy way to do that -- see below).

Byteman deliberately avoids the use of wildcard patterns for the CLASS and METHOD it is injecting into. That's because using these sort of rules would slow the JVM down enormously.

Why? Well, every time a class is loaded Byteman gets asked "Do you want to transform this class by injecting some code into it?". Currently, Byteman indexes all loaded rules by CLASSNAME. So, answering that question involves a hash table lookup (well, actually, two -- one with the bare name and another with the package qualified name). This means that no answers (which is almost always the right answer) are super-quick. If Byteman were to allow patterns for the CLASSNAME then it could not rely on a simple hash lookup.

If, for example you had a pattern like CLASS org..Foo and 2 classes like org.my.app.FooBar and org.my.app.Bletch how would you decide that the first one matches and the second one does not? You would have to try a pattern match for each pattern rule for every class name. Even a single pattern match is a lot more expensive than a hash table lookup. If you used multiple pattern-based rules then the cost would multiply according to the number of rules.

Ok, so how can you work round this limitation of Byteman? If you want to instrument a lot of classes + methods then I suggest you use a program to generate a rule script containing a rule for each class+method you are interested in. Write a program which reads a file containing entries like

class_name1 method_pattern1 class_name2 method_pattern2 . . .

The file says I want to instrument all methods of CLASS class_name1 whose methods match method_pattern1 and so on.

So long as the target jars are in your classpath you can use the current classloader to load each class by name (call this.getClass().getClassLoader() to get the classloader and then call classloader.loadClass(class_name) to get the desired class). Use reflection to get a list of the class's methods. For each method, if the name of that method matches the corresponding method_pattern output an AT ENTRY rule and/or an AT EXIT rule to your script file.

If you want to see some code whcih does something similar to this look at the contrib/dtest package which is part of the Byteman sources.

https://github.com/bytemanproject/byteman/tree/master/contrib/dtest

If you have any further questions about Byteman the please coem and ask them on the official Byteman user forum provided by the project:

https://developer.jboss.org/en/byteman?view=discussions

regards,

Andrew Dinn (Byteman Project Lead)