This document provides an introduction to bytecode instrumentation (BCI) for beginners. It defines BCI as modifying Java classes without source code by instrumenting bytecode. It explains that BCI is supported by the Java Virtual Machine and can be used for profiling, monitoring, code coverage and more. Practical examples are provided on how to get started with BCI using frameworks like ASM, as well as javac and javap.
As the title of the session suggests, the idea was to get together people who are interested in BCI, but don't yet know where they are going with it. I'm not a world expert! Can help you avoid some of the pitfalls (new lines in jar manifest files) Suggestions for tools to help you
Bytecode technically refers to the opcodes in a class, that is the method bodies. However, the whole class file can be modified BCI can be added/removed at will while an application is running
Not a hack, Been available since java 5
At load time can do almost anything: Restrictions: You can't go mad! If you removed a method from a class which implements an interface, the class would no longer verify, and would fail to load Some things can't be done whilst running maybe allow you to replace a newer version if for some reason it was difficult to get a new version/recompile
Examples: Monitoring, logging/tracing, profiling Servlets running in an application server (tomcat, websphere, glassfish) Running the server, no idea what the application does Can inject instrumentation at the top of this method to record URL, time taken, where the request has come from..
There are other mechanisms for doing some of the things mentioned previously Why do them using BCI?
My main focus is dynamic BCI, where the classes are modified either as the JVM loads the classes, or whilst an application is running. Agents can be written in c, or java. Java agents are easier to deal with JVMTI has a larger interface, allows your agent to do more than just BCI
Remember the new line at the end of the manifest!
A class file transformer gets a call back whenver a new class is loaded Retransform is how you trigger modifcation of an already running class
All transformations boil down to: Take an array of bytes, copy and modify the array, give it back to the JVM
Offline- all of the above would be done by reading in a class file into a byte array
Summary Java SDK provides a mechanism for transforming classes Gives you a a byte array Not very much other help Now, need some help to actually twiddle the bytes
The following slides focus on making it easy to add new logic to an existing class Other frameworks are available Javassist, bcel n.b A basic understanding of how a JVM works is still required
Easier to learn, but non-trivial java api Does provide helper methods to do make common tasks easier (e.g. local variable sorter) Now give brief overview, No details emphasis, this is just java, and there are only 3 classes and three methods
This is class one, create a new instance of a method adapter
In method adapter, override one method, create a MethodAdapter class
This gets you to the point of: My class is parsed, I have a method body to create/modify ASM provides methods to add bytecodes but Doesn't tell you what bytecodes to add! Knowledge of the JVM architecture is still required Next step, make creating new logic easier
This is just part of the output of javap Also includes the whole of the constant pool Every method in the class All the meta data (stack maps, line number tabel, variable table) Not much help to go rebuild a similar method in your agent But I do recommend it as a help to understanding how the JVM works
Show the profiler logic in java show the bci logic in java Show byte code plugin to turn that into ASM (show view, other, byte code, use switch to change between asm and non-asm) Paste ASM code into boiler plate method transformer
The following slides focus on the task of - adding new logic to an existing class n.b A basic understanding of how a JVM works is still required