how to do C++ Instrumentation with Clang?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

how to do C++ Instrumentation with Clang?

Xin Wang via cfe-dev

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
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to do C++ Instrumentation with Clang?

Xin Wang via cfe-dev

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
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to do C++ Instrumentation with Clang?

Xin Wang via cfe-dev
In reply to this post by Xin Wang via cfe-dev
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 <[hidden email]> 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
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev



_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Loading...