Instrumentation: string as argument

Hi, I still have some primary problems about using string in llvm pass.
I wanna use string as argument, like that:

extern "C" void logvar(int i, std::string name) {
    std::cout << "Num: " << i << "; Name: " << name << std::endl;
}

And pass.cpp

// Get the function to call from our runtime library.
LLVMContext &Ctx = F.getContext();
std::vector<Type*> paramTypes = {  // Param Types
  Type::getInt32Ty(Ctx),
  Type::getInt8PtrTy(Ctx)  // HERE: use *i8 to represent string, is it right?
};
Type *retType = Type::getVoidTy(Ctx);
FunctionType *logFuncType = FunctionType::get(retType, paramTypes, false);
FunctionCallee logFunc = F.getParent()->getOrInsertFunction("logvar", logFuncType);

Insert logvar in pass.cpp
for store instruction such as store i32 0, i32* %retval, align 4

// for store instruction
void insertLogvar(StoreInst *inst, BasicBlock &B, FunctionCallee logFunc, LLVMContext &Ctx) {
  IRBuilder<> builder(inst);
  builder.SetInsertPoint(&B, ++builder.GetInsertPoint());

  Value *argi = inst->getOperand(0);  // integer
  if (auto constant_int = dyn_cast<ConstantInt>(argi)) {
    Value* argstr = ?   // HERE
    Value* args[] = {argi, argstr}; 
    builder.CreateCall(logFunc, args);
  } else {
    errs() << "store inst has no instance number" << "\n";
  }
}

My question is how to accomplish above code? To match string type.

Best wishes!

You really do not want to be doing that, for many reasons. Use a const char * instead and convert it in logvar if needed. And re: // HERE: use *i8 to represent string, is it right?, no, it is very much not right, but it is if you use const char *.

1 Like

Thank you for your reply! I got it. And would you please tell me how to accomplish Value* argstr = ? ? Tks!

See this reply to your question 3 days ago?

1 Like

Oh, yes! I almost forgot I have realized the function with the help of Tnorthover. It really works!
Thank you very much!