Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monitoring variable accesses in C/C++

I'm working on a coverage criterion for multithreaded code and as part of it would like to record accesses to variables. For example, in the code below I'd like to record that variable x was written to and y, z, a[i], and i were read from.

x = y * (int)z + a[i]

I've been looking at doing this using Clang's RecursiveASTVisitor and modifying the source to include recording functionality. However, I'm unsure whether this is a sensible approach as my understanding of how Clang works is very incomplete.

Currently, when I find a statement I check whether it is a BinaryOperator, UnaryOperator, Cast, or DeclRefExpr. (I'll expand what its capable of once I have the basics working.) If it's a BinaryOperator, UnaryOperator, or Cast I check the expression's subexpressions. If it's a DeclRefExpr I can check whether the expression is an lvalue or rvalue (again, simplifying for now), but once I've found DeclRefExpr they are always lvalues. In order to determine whether they were used as lvalues or rvalues I have to check its parent, if it was an lvaluetorvalue cast it was used as an rvalue.

I very much feel like I'm taking the wrong approach to this problem as I can only see it getting much more complicated as I have to consider more complex code.

Would there be a better way to approach this?

Thank you

Edit

I don't intend to record this information statically. I intend to find uses of variables and insert code that will record accesses to these variables when the code is run.

For example, given the code above (x = y * (int)z + a[i];), I would want to produce something like

x = y * (int)z + a[i];
recordAccess(<file>, <line>, "x",    &x,    WRITE);
recordAccess(<file>, <line>, "y",    &y,    READ);
recordAccess(<file>, <line>, "z",    &z,    READ);
recordAccess(<file>, <line>, "a[i]", &a[i], READ);
recordAccess(<file>, <line>, "i",    &i,    READ);
like image 358
tgt Avatar asked Mar 06 '13 16:03

tgt


People also ask

Is there VAR in C?

The best thing I can do is using var , but the var keyword doesn't exist in C. If not var , then what's an equivalent for it in C? C is strictly typed, so you'll need to explicitly define variables with a type. For whatever it's worth, in C++ the auto keyword can perform type inference, but C++ is not the same as C.


1 Answers

As others have indicated, aliasing makes this impossible. Static analysis of code to answer the questions you are interested is not possible. If it was somehow possible to take a source code file and determine the output just by analyzing the syntax, compilers would produce the output of the resulting program, instead of a compiled program. In short, you are trying to answer the halting problem.

Dynamic analysis is what you actually need to answer the questions you are most likely interested in. There is a big market in dynamic analysis of multi-threaded software already.

like image 106
Eric Urban Avatar answered Oct 08 '22 07:10

Eric Urban