[cfe-dev] how to do C++ Instrumentation with Clang?

Gábor Márton via cfe-dev cfe-dev at lists.llvm.org
Fri Jun 23 01:03:38 PDT 2017


Hi,

You can use the -finstrument-functions program option of Clang to
instrument each and every function call in a way to execute code before and
after the body of the functions.

With finstrument-functions you will not be able to replace funtions, the
original function will always be called. Note that there is a prototype
implementation to make it possible to replace functions (that feature might
be really handy in testing legacy code) :
https://github.com/martong/finstrument_mock

I am not sure what is the exact nature of the problem you want to solve,
but perhaps the compiler is not the best tool to do such instrumentation.
Generally the technique you want is called Function/Method Call
Interception (FCI/MCI). One very good dynamic instrumentation tool is the
debugger itself. For static binary instrumentation you could use e.g. Intel
PIN. There is a great article about available FCI methods here:
http://onlinelibrary.wiley.com/doi/10.1002/spe.2501/abstract

Cheers,
Gabor



On Fri, Jun 23, 2017 at 5:50 AM, yonggang_chen via cfe-dev <
cfe-dev at lists.llvm.org> wrote:

> Hello all,
>
>
>
> I have a large project which consists of several millions of C++ code. Now
> I want to add a log function to each line of code. If do it manually, it’s
> almost impossible.
>
>
>
> Suppose my original code is like this:
>
> void snapshot_handler::mouseMoveEvent(IDocView* pDocView, QMouseEvent *
> event)
>
> {
>
>     pDocView->getViewPortWidget()->setCursor(Qt::CrossCursor);
>
>     m_ptEnd = event->pos();
>
>     pDocView->updateView();
>
> }
>
>
>
> I want to log each line of code and the function:
>
> void snapshot_handler::mouseMoveEvent(IDocView* pDocView, QMouseEvent *
> event)
>
> {
>
>     Log << “mouseMoveEvent;
>
>     Log << pDocView->getViewPortWidget()->setCursor(Qt::CrossCursor);
>
>     Log << m_ptEnd = event->pos();
>
>     Log << pDocView->updateView();
>
>     Log << “elapsed time in seconds”;
>
> }
>
>
>
> Can Clang support doing this? If so, how can achieve such requirements?
>
>
>
> Best regards
>
> Yonggang Chen
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20170623/5c283a55/attachment.html>


More information about the cfe-dev mailing list