29.01.2014 Views

A CIL Tutorial - Department of Computer Science - ETH Zürich

A CIL Tutorial - Department of Computer Science - ETH Zürich

A CIL Tutorial - Department of Computer Science - ETH Zürich

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 4<br />

Instrumentation<br />

In Chapters 1 and 2, we saw how to use the AST visitor to modify the program by removing<br />

an instruction. It was probably easy to see how to change that example to add statements and<br />

instructions, instead. However, adding function calls is a bit trickier. In particular, the Call<br />

constructor <strong>of</strong> the type Cil.instr requires a Cil.exp expression for the function. We can build an<br />

expression for the function out <strong>of</strong> a varinfo for the function, which we can nd simply by iterating<br />

over the globals <strong>of</strong> a Cil.file. In this example we'll look at some handy patterns for accomplishing<br />

that, and for getting the instrumentation calls into the code.<br />

It almost goes without saying that the ability insert function calls is imensely useful in the<br />

dynamic analysis <strong>of</strong> programs and the implementation <strong>of</strong> new language runtime features.<br />

4.1 tut4.ml<br />

First, we'll add calls to functions that will do some proling <strong>of</strong> loops. At the end <strong>of</strong> each loop, the<br />

instrumented program will print the source location <strong>of</strong> the loop and the number <strong>of</strong> iterations that<br />

ran. First, we'll set up a quick way to refer to the instrumentation calls. Then, we'll write a visitor<br />

to wrap the calls around loops.<br />

4.1.1 Setup <strong>of</strong> instrumentation calls<br />

It isn't strictly necessary to wrap up the varinfos for the instrumentation calls in a record type.<br />

They could just be global variables <strong>of</strong> this module, but I nd this helps me stay a bit more organized.<br />

Also, if you add a new eld to the record, the OCaml compiler gives you error messages if you forget<br />

some part <strong>of</strong> the initialization.<br />

Additionally, in larger projects, I tend to put all <strong>of</strong> this set-up code in its own module, but in<br />

this small example we should be okay.<br />

36

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!