[LLVMdev] Insert a function call in the code

赵夏 zhaoxiahust at gmail.com
Sun Nov 20 03:14:21 PST 2011


Thank you for your help
I found the problem,
as hook() is a no paramter function,so

                            for(Function::arg_iterator i =
hook->arg_begin(), e = hook->arg_end(); i != e; ++i)
                            {
                                Args.push_back(i); /////////////Wong
                            }

we can add a call by
Instruction *newInst = CallInst::Create(hook, "");
BB->getInstList().insert((Instruction*)BI, newInst);


If you want to insert the function with a paramter, you should add a real
value to Args

在 2011年11月20日 下午3:07,Duncan Sands <baldrick at free.fr>写道:

> Hi 赵夏, please don't send emails only to me: CC the mailing
> list too.  That way others can join in and the discussion is
> archived for the benefit of people with similar questions in
> the future.
>
> On 20/11/11 01:29, 赵夏 wrote:
> > Thank you for your reply,
> > But after I remove the second Type::getVoidTy(M.getContext()),Another
> problem
> > occured
> >
> > opt: Value.cpp:175: void llvm::Value::setName(const llvm::Twine&):
> Assertion
> > `!getType()->isVoidTy() && "Cannot assign a name to void values!"'
> failed.
>
> Probably you are trying to give a name to the call to your function, i.e.
> to
> the value returned by it.  Since your function doesn't return a value
> (since it
> "returns" void), giving the returned value a name makes no sense, thus this
> assertion.
>
> > 0 opt 0x000000000082f5cf
> > 1 opt 0x000000000083181a
> > 2 libpthread.so.0 0x00000033f460f4c0
> > 3 libc.so.6 0x00000033f42329a5 gsignal + 53
> > 4 libc.so.6 0x00000033f4234185 abort + 373
> > 5 libc.so.6 0x00000033f422b935 __assert_fail + 245
> > 6 opt 0x00000000007d866b
> > 7 bishe_insert.so 0x00007f94c62a2b3f
> > 8 opt 0x00000000007c47d7 llvm::MPPassManager::runOnModule(llvm::Module&)
> + 503
> > 9 opt 0x00000000007c4957 llvm::PassManagerImpl::run(llvm::Module&) + 167
> > 10 opt 0x00000000004a43be main + 2734
> > 11 libc.so.6 0x00000033f421ec5d __libc_start_main + 253
> > 12 opt 0x0000000000499d19
> > Stack dump:
> > 0. Program arguments: opt -load
> > /home/kain/Documents/bishe/llvm-2.8/Release+Asserts/lib/bishe_insert.so
> > -bishe_insert
> > 1. Running pass 'test function exist' on module '<stdin>'.
> > Aborted (core dumped)
> >
> >
> > And If I want to insert a function such as void hook(int ),the first
> problem
> > occured
>
> I'm not sure what you mean by "the first problem".  If your function takes
> an integer parameter, you need to pass the appropriate integer type when
> you create the function.
>
> Ciao, Duncan.
>
> >
> >
> >
> > 2011/11/19 Duncan Sands <baldrick at free.fr <mailto:baldrick at free.fr>>
> >
> >     Hi 赵夏,
> >
> >      > hookFunc = M.getOrInsertFunction("hook",
> >      > Type::getVoidTy(M.getContext()), Type::getVoidTy(M.getContext()),
> (Type*)0);
> >
> >     try removing the second Type::getVoidTy. Your function doesn't take
> any
> >     parameters, so you should simply pass an empty list of parameter
> types.
> >     Unlike in C++, a function foo(void) taking no parameters shouldn't be
> >     declared as taking one parameter of void type, it takes no
> parameters.
> >
> >     Ciao, Duncan.
> >     _______________________________________________
> >     LLVM Developers mailing list
> >     LLVMdev at cs.uiuc.edu <mailto:LLVMdev at cs.uiuc.edu>
> http://llvm.cs.uiuc.edu
> >     http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
> >
> >
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20111120/5551c75f/attachment.html>


More information about the llvm-dev mailing list