BCI library for Java in C
Asked Answered
C

2

6

I am looking for a BCI (Bytecode Instrumentation) library for Java in C or C++, to use in a JVMTI agent.

Best case scenario would be something like ASM or BCEL in pure C.

The closest thing I have found so far is only a demo java_crw_demo written by Kelly O'Hair a few years ago and used in Sun's/Oracle's tutorials ever since.

Do you know of anything else?

Some rationale: I am building an instrumentation tool to add a getter method to java.lang.Object and overload this getter in every direct subclass of Object. For this reason I cannot use a Java agent. Also, I would like to avoid spawning a secondary JVM to perform the instrumentation - for complexity and speed reasons.

Candlewood answered 30/7, 2012 at 9:38 Comment(2)
It's a good question, but a word of warning on your rationale: you'll find most JVMs don't allow you to instrument j.l.Object.Airway
Yes they will in a certain limited manner: for example I should be able to add no more than 1 non-static method in HotSpot. I cannot add any non-static fields there though... Therefore I have to resort to the approach above: natively implemented getter in Object, field-implemented overriden getter in subclasses. (And HotSpot, being the most-used and most-optimized one, is a good benchmark for this.)Candlewood
E
1

I once started writing one in C quite a while ago, but I didn't get very far due to lack of motivation. AFAIK there's no public releases in C, but it shouldn't be too hard to write a simple one for your needs according to the JVM Specification. You should be particularly interested in the chapter on the class file format, as well as that on the instruction set.

Everyday answered 31/7, 2012 at 16:27 Comment(3)
According to the laundry list of problems found by the author of java_crw_demo, I am afraid it could be too hard - even though I am probably aiming for even less changes than Kelly.Candlewood
Well, most of those problems involve dealing with raw class files; ideally you should parse them into some easy-to-use representation like BCEL and the ASM tree API attempt to do.Everyday
As the only person who has responded at all, enjoy the points:)Candlewood
S
1

The only reasonable solution I have found is to actually pipe the byte codes over to a separate Java program that uses ASM (or the BCI framework of your choice). This is not fast, but you only need to do it once per instrumented class.

Supervisor answered 29/5, 2013 at 2:24 Comment(1)
That is precisely the approach taken by the DiSL project forge.ow2.org/projects/disl that I actually use :)Candlewood

© 2022 - 2024 — McMap. All rights reserved.