Getting actual value of local variables in llvm

2019-05-16 03:46发布

问题:

If I have this example:

int a=0, b=0;

a and b are local variables and make any modifications in their values, such as:

a++;
b++;

I need to get the value in this line code during running MCJIT.

I mean by value not Value class, but the actual integer or any type value.

回答1:

You need to return the value from a JITed LLVM function in order to retrieve it from the code invoking MCJIT.

Check out this Kaleidoscope example.

The relevant code is in HandleTopLevelExpression():

if (FunctionAST *F = ParseTopLevelExpr()) {
  if (Function *LF = F->Codegen()) {
    // JIT the function, returning a function pointer.
    void *FPtr = TheHelper->getPointerToFunction(LF);

    // Cast it to the right type (takes no arguments, returns a double) so we
    // can call it as a native function.
    double (*FP)() = (double (*)())(intptr_t)FPtr;
    fprintf(stderr, "Evaluated to %f\n", FP());
  }
}


回答2:

Put a break point after execution of the statement where you want to check the value. In the console (lldb) po <variable name>.

Although I guess watch point is more suitable for your requirement, add a watch point for the variable like, watchpoint set variable <variable key path>.