[Soot-list] Newbie's question

mbatch at cs.mcgill.ca mbatch at cs.mcgill.ca
Tue Apr 18 16:31:11 EDT 2006


>> Basically my monitoring program needs to be notified when an object
>> field, a local variable, or a parameter is write or read. Also I would
>> like to know something like the entry/exit points of a method call ...
>>
>> I used to use AspectJ to implement my project.

By the sounds of it, you compiled the program with aspects which reported
read/writes. If so, this sounds like ahead-of-time stuff, so you could
certainly use soot in the same way (ahead-of-time instrumentation).

Soot would work great as it's fairly trivial to find those places in the
code where a field (or local) is read/written as well as all method calls,
and simply add method calls to your analyzer before or after (depending on
what you need) the access.

Nevertheless, JVMTI could work as well for you as since you can monitor a
running java program on a single-step basis (generally every single
bytecode) though this might make the program run much slower than
instrumenting on those places you want to consider (then again, maybe not
if you want to consider every local read/write)... If you are
uncomfortable with c and want to stick with java, Soot might be the best
option.. Otherwise, I found this JVMTI information quickly on a java/sun
forum:

Given the bytecodes "iload 5", we can get all the information from
getbytecode() in the JVMTI. The bytecode buffer returned contains both the
opcodes and the associated operands (cp index, immediate value, local
variable index etc.). You need to parse the byte buffer returned from the
JVM TI API. In a for or while loop fetch each byte, switch on opcode and
in each case read the operands by advancing array index.

Example code which prints out each bytecode of a method:

static void JNICALL callbackSingleStep(jvmtiEnv *jvmti_env,
 JNIEnv* jni_env, jthread thread, jmethodID method, jlocation location)
{
  enter_critical_section(jvmti); {

    jvmtiError error;
    jint bytecodeCountPtr;
    unsigned char *bytecodesPtr;

    error = jvmti->GetBytecodes(method, &bytecodeCountPtr, &bytecodesPtr);

    bytecode = (unsigned char) bytecodesPtr[location];
    bytecode_stat[bytecode]++;

    if (bytecode==187) {
      operand1 = (unsigned char) bytecodesPtr[location+1];
      operand2 =(unsigned char) bytecodesPtr[location+2];
      fprintf(stdout,"bytecode = \t%d\n", bytecode);
      fprintf(stdout,"operand1 = \t%d\n", operand1);
      fprintf(stdout,"operand2 = \t%d\n", operand2);
    }

    error = jvmti->Deallocate(bytecodesPtr);
    check_jvmti_error(jvmti_env, error,
           "Cannot deallocate bytecode pointer");

  } exit_critical_section(jvmti);
}


Hope this helps,

Michael



More information about the Soot-list mailing list