how to do C++ Instrumentation with Clang?

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

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

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