Next: , Previous: Installing Daikon, Up: Top   [Contents][Index]

3 Example usage for Java, C/C++, C#/F#/Visual Basic, Perl, and Eiffel

Detecting invariants involves two steps:

  1. Obtain one or more data trace files by running your program under the control of a front end (also known as an instrumenter or tracer) that records information about variable values. You can run your program over one or more inputs of your own choosing, such as regression tests or a typical user input session. You may choose to obtain trace data for only part of your program; this can avoid inundating you with output, and can also improve performance.
  2. Run the Daikon invariant detector over the data trace files (see Running Daikon). This detects invariants in the recorded information. You can view the invariants textually, or process them with a variety of tools.

Often, you can run a single command that performs both steps. Among other benefits, this can avoid the need to create the data trace file by sending trace information directly from the instrumented program to Daikon, which is called online execution of Daikon.

This section briefly describes how to obtain data traces for Java, C, C#, Perl, and Eiffel programs, and how to run Daikon. For detailed information about these and other front ends that are available for Daikon, see Front ends (instrumentation).


Next: , Up: Example usage   [Contents][Index]

3.1 Detecting invariants in Java programs

In order to detect invariants in a Java program, run the program using the Chicory front end (see Chicory) to create a data trace file, then run Daikon itself to detect invariants. With the --daikon option to Chicory, a single command performs both steps.

For example, if you usually run

java mypackage.MyClass arg1 arg2 arg3

then instead you would run

java daikon.Chicory --daikon mypackage.MyClass arg1 arg2 arg3

and the Daikon output is written to the terminal.


Next: , Up: Detecting invariants in Java programs   [Contents][Index]

3.1.1 StackAr example

The Daikon distribution contains some sample programs that will help you get practice in running Daikon.

To detect invariants in the StackAr sample program, perform the following steps after installing Daikon (see Installing Daikon).

  1. Compile the program with the -g switch to enable debugging symbols. (The program and test suite appear in the DataStructures subdirectory directory.)
    cd examples/java-examples/StackAr
    javac -g DataStructures/*.java
    
  2. Run the program under the control of the Chicory front end, pass the information to Daikon, print the inferred invariants, and write a binary representation of the invariants to StackArTester.inv.gz.
    java daikon.Chicory --daikon DataStructures.StackArTester
    

    If you have not set the CLASSPATH as yet, you may add it to the command line for now - but don’t forget to set it later.

    java -cp "../../../daikon.jar:." daikon.Chicory --daikon DataStructures.StackArTester
    

    Alternately, replacing the --daikon argument by --daikon-online has the same effect, but does not write a data trace file to disk.

If you wish to have more control over the invariant detection process, you can split the second step above into multiple steps. Then, the whole process would be as follows:

  1. Compile the program with the -g switch to enable debugging symbols. (The program and test suite appear in the DataStructures subdirectory directory.)
    cd examples/java-examples/StackAr
    javac -g DataStructures/*.java
    
  2. Run the program under the control of the Chicory front end, in order to create a trace file named StackArTester.dtrace.gz.
    java daikon.Chicory DataStructures.StackArTester
    
  3. Run Daikon on the trace file.
    java daikon.Daikon StackArTester.dtrace.gz
    

    Daikon can analyze multiple runs (executions) of the program. You can supply Daikon with multiple trace files:

    java daikon.Chicory --dtrace-file=StackArTester1.dtrace.gz DataStructures.StackArTester
    java daikon.Chicory --dtrace-file=StackArTester2.dtrace.gz DataStructures.StackArTester
    java daikon.Chicory --dtrace-file=StackArTester3.dtrace.gz DataStructures.StackArTester
    java daikon.Daikon StackArTester*.dtrace.gz
    

    (In this example, all the runs are identical, so multiple runs yield the same invariants as one run.)

  4. Examine the invariants. (They were also printed to standard out by the previous step.)

    There are various ways to do this.


Next: , Previous: StackAr example, Up: Detecting invariants in Java programs   [Contents][Index]

3.1.2 Using DynComp with Java programs

DynComp can help to filter Daikon’s output by omitting invariants involving unrelated variables (see DynComp for Java). To do so, run DynComp on the target program first, then pass the resulting .decls file to Chicory. The process would be as follows:

  1. Compile the program with the -g switch to enable debugging symbols. (The program and test suite appear in the DataStructures subdirectory of the StackAr directory.)
    cd examples/java-examples/StackAr
    javac -g DataStructures/*.java
    
  2. Run the program with DynComp to generate comparability information. You should produce an instrumented version of the JDK first (see Instrumenting the JDK with DynComp). By default, comparability information is written to StackArTester.decls-DynComp.
    java daikon.DynComp DataStructures.StackArTester
    
  3. Run the program under the control of the Chicory front end, including comparability information. Pass the information to Daikon, print the inferred invariants, and write a binary representation of the invariants to StackArTester.inv.gz. Note that this runs the target program for a second time.
    java daikon.Chicory --daikon \
         --comparability-file=StackArTester.decls-DynComp \
         DataStructures.StackArTester
    

You could split the third step into multiple steps, as described in StackAr example, to gain more control over the invariant detection process.


Next: , Previous: Using DynComp with Java programs, Up: Detecting invariants in Java programs   [Contents][Index]

3.1.3 Understanding the invariants

This section examines some of the invariants for the StackAr example. For more help interpreting invariants, see Interpreting output.

The StackAr example is an array-based stack implementation. Take a look at DataStructures/StackAr.java to get a sense of the implementation. Now, look at the first section of Daikon output.

======================================================================
StackAr:::OBJECT
this.theArray != null
this.theArray.getClass() == java.lang.Object[].class
this.topOfStack >= -1
this.theArray[this.topOfStack+1..] elements == null
this.theArray[0..this.topOfStack] elements != null
this.topOfStack <= size(this.theArray[])-1
======================================================================

These six annotations describe the representation invariant. The array is never null, and its runtime type is Object[]. The topOfStack index is at least -1 and is less than the length of the array. Finally, the elements of the array are non-null if their index is no more than topOfStack and are null otherwise.

Next, look at the invariants for the top() method. top() has two different exit points, at lines 78 and 79 in the original source. There is a set of invariants for each exit point, as well as a set of invariants that hold for all exit points. Look at the invariants when top() returns at line 79.

======================================================================
StackAr.top():::EXIT79
return == this.theArray[this.topOfStack]
this.theArray == orig(this.theArray)
this.theArray[] == orig(this.theArray[])
this.topOfStack == orig(this.topOfStack)
return != null
this.topOfStack >= 0
this.theArray[this.topOfStack+1..] elements == this.theArray[-1]
======================================================================

The return value is never null, and is equal to the array element at index topOfStack. The top of the stack is at least 0. The array, the elements of the array, and topOfStack are not modified by this method — this method is an observer. The last invariant is not particularly interesting.


Next: , Previous: Understanding the invariants, Up: Detecting invariants in Java programs   [Contents][Index]

3.1.4 Understanding DynComp

To get a sense of how DynComp helps eliminate uninteresting output, take a look at the invariants for the exit point at line 32 of the createItem(int) method.

======================================================================
DataStructures.StackArTester.createItem(int):::EXIT32
this.phase == 4
return.getClass() == int[].class
this.s.topOfStack < orig(i)
this.phase < orig(i)
this.phase != size(this.s.theArray[])
this.maxPhase < orig(i)
======================================================================

The value of phase is always less than the value of i. While this is true for the observed executions, it is not a helpful invariant, since phase and i represent different abstract types; i is a number to be pushed onto the stack, while phase is used for program flow control. Although they are both ints, comparing the two is not meaningful, so this invariant, among others, is omitted from the output when Daikon is run with DynComp.

======================================================================
DataStructures.StackArTester.createItem(int):::EXIT32
this.phase == 4
return.getClass() == int[].class
this.s.topOfStack < orig(i)
======================================================================

Previous: Understanding DynComp, Up: Detecting invariants in Java programs   [Contents][Index]

3.1.5 A second Java example

A second example is located in the examples/java-examples/QueueAr subdirectory. Run this sample using the following steps:

Alternately, you can split the second command into two parts:


Next: , Previous: Detecting invariants in Java programs, Up: Example usage   [Contents][Index]

3.2 Detecting invariants in C/C++ programs

In order to detect invariants over C or C++ programs, you must first install a C/C++ front end (instrumenter). We recommend that you use Kvasir (see Kvasir), and this section gives examples using Kvasir.

To use the C/C++ front end Kvasir with your program, first make sure that your program has been compiled with DWARF-2 format debugging information, such as by giving the -gdwarf-2 flag to GCC when compiling. Then, run your program as usual, but prepend kvasir-dtrace to the command line.

For more information about Kvasir, including more detailed documentation on its command-line options, see Kvasir.


Next: , Up: Detecting invariants in C/C++ programs   [Contents][Index]

3.2.1 C examples

The Daikon distribution comes with several example C programs to enable users to become familiar with running Daikon on C programs. These examples are located in the examples/c-examples directory.

To detect invariants for a program with Kvasir, you need to perform two basic tasks: run the program under Kvasir to create a data trace file (steps 1–2), and run Daikon over the data trace file to produce invariants (steps 3–4). The following instructions are for the wordplay example, which is a program for finding anagrams.

  1. Change to the directory containing the program.
    cd $DAIKONDIR/examples/c-examples/wordplay
    
  2. Compile the program with DWARF-2 debugging information enabled (and all optimizations disabled).
    gcc -gdwarf-2 wordplay.c -o wordplay
    

    Kvasir can also be used for programs constructed by compiling a number of .c files separately, and then linking them together; in such a program, specify -gdwarf-2 when compiling each source file containing code you wish to see invariants about.

  3. Run the program just as you normally would, but prepend kvasir-dtrace to the command line.
    kvasir-dtrace ./wordplay -f words.txt 'daikon dynamic invariant detector'
    

    Any options to the program can be specified as usual; here, for instance, we give commands to look for anagrams of the phrase “Daikon Dynamic Invariant Detector” using words from the file words.txt.

    Executing under Kvasir, the program runs normally, but Kvasir executes additional checks and collects trace information (for this reason, the program will run more slowly than usual). Kvasir creates a directory named daikon-output under the current directory, and creates the wordplay.dtrace file, which lists both variable declarations and values.

    Kvasir will also print messages if it observes your program doing something with undefined effects; these may indicate bugs in your program, or they may be spurious. (If they are bugs, they can also be tracked down by using Valgrind (http://www.valgrind.org/) with its regular memory checking tool; if they do not appear with that tool, they are probably spurious).

  4. Run Daikon on the trace file.
    java daikon.Daikon \
         --config_option daikon.derive.Derivation.disable_derived_variables=true \
         daikon-output/wordplay.dtrace
    

    The invariants are printed to standard output, and a binary representation of the invariants is written to wordplay.inv.gz. Note that the example uses a configuration option to disable the use of derived variables; it can also run without that option, but takes significantly longer.

    Daikon can analyze multiple runs (executions) of the program. You can supply Daikon with multiple trace files:

    kvasir-dtrace --dtrace-file=daikon-output/wordplay1.dtrace \
        ./wordplay -f words.txt 'daikon dynamic invariant detector'
    kvasir-dtrace --dtrace-file=daikon-output/wordplay2.dtrace \
        ./wordplay -f words.txt 'better results from multiple runs'
    kvasir-dtrace --dtrace-file=daikon-output/wordplay3.dtrace \
        ./wordplay -f words.txt 'more testing equals better testing'
    java -Xmx256m daikon.Daikon daikon-output/wordplay*.dtrace
    

    or, you can append information from multiple runs in a single trace file:

    kvasir-dtrace --dtrace-file=daikon-output/wordplay-all.dtrace \
        ./wordplay -f words.txt 'daikon dynamic invariant detector'
    kvasir-dtrace --dtrace-append --dtrace-file=daikon-output/wordplay-all.dtrace \
        ./wordplay -f words.txt 'better results from multiple runs'
    kvasir-dtrace --dtrace-append --dtrace-file=daikon-output/wordplay-all.dtrace \
        ./wordplay -f words.txt 'more testing equals better testing'
    java -Xmx256m daikon.Daikon daikon-output/wordplay-all.dtrace
    
  5. Examine the invariants. As described in StackAr example, there are several ways to do this:

    For help understanding the invariants, see Interpreting output.


Next: , Previous: C examples, Up: Detecting invariants in C/C++ programs   [Contents][Index]

3.2.2 Using DynComp with C programs

Optionally, the DynComp tool can be used along with Kvasir and Daikon. DynComp uses a dynamic analysis to infer which program variables can meaningfully be used together; Daikon can then use this information to restrict the invariants it considers, potentially improving both its performance and the usefulness of its results.

DynComp is enabled as an extra mode of Kvasir; when running with DynComp enabled, Kvasir produces two output files instead of one: in addition to a .dtrace file containing a trace of a particular execution, the information about what variables and functions exist in a program, along with information grouping the variables into abstract types, is stored in a file with the extension .decls. Both of these files must be supplied to Daikon.

For instance, to repeat the wordplay example with DynComp, first rerun kvasir-dtrace giving it the option --with-dyncomp:

kvasir-dtrace --with-dyncomp \
   ./wordplay -f words.txt 'daikon dynamic invariant detector'

Then, supply the .decls file when invoking Daikon:

java daikon.Daikon \
     --config_option daikon.derive.Derivation.disable_derived_variables=true \
     daikon-output/wordplay.decls daikon-output/wordplay.dtrace

For instance, one effect of DynComp that can be seen in the wordplay example concerns the global variables largestlet, rec_anag_count, adjacentdups, specfirstword, maxdepthspec, and silent. These variables are all 0 in the sample execution (for instance, several of them correspond to command-line options that are not enabled), so without DynComp, Daikon gives the invariants that they are all equal. However, DynComp’s analysis finds that the variables are of different abstract types, so it is not meaningful to compare them. When DynComp information is provided, Daikon instead gives separate invariants about the value of each variable.


Previous: Using DynComp with C programs, Up: Detecting invariants in C/C++ programs   [Contents][Index]

3.2.3 Dealing with large examples

Since the default memory size used by a Java virtual machine varies, we suggest that Daikon be run with at least 256 megabytes of memory (and perhaps much more), specified for many JVMs by the option -Xmx256m. For more information about specifying the memory usage for Daikon, see Out of memory.

Disk usage can be reduced by specifying that the front end should compress its output .dtrace files.

In some cases, the time and space requirements of the examples can be reduced by reducing the length of the program run. However, Daikon’s running time depends on both the length of the test run and the size of the program data (such as its use of global variables and nested data structures). The examples also demonstrate disabling derived variables, which significantly improves Daikon’s performance at the cost of producing fewer invariants. For more techniques for using Daikon with large programs and long program runs, see Large dtrace files.


Next: , Previous: Detecting invariants in C/C++ programs, Up: Example usage   [Contents][Index]

3.3 Detecting invariants in C#, F#, and Visual Basic programs

The Daikon front end for .NET languages (C#, F#, and Visual Basic) is called Celeriac.

Please see its documentation at http://code.google.com/p/daikon-dot-net-front-end/wiki/GettingStarted.


Next: , Previous: Detecting invariants in C# programs, Up: Example usage   [Contents][Index]

3.4 Detecting invariants in Perl programs

The Daikon front end for Perl is called dfepl.

Using the Perl front end is a two-pass process: first you must run the annotated program so that the runtime system can dynamically infer the kind of data stored in each variable, and then you must re-annotate and re-run the program with the added type information. This is necessary because Perl programs do not contain type declarations.

dfepl requires version 5.8 or later of Perl.


Next: , Up: Detecting invariants in Perl programs   [Contents][Index]

3.4.1 Instrumenting Perl programs

Perl programs must be instrumented twice. First they must be instrumented without type information. Then, once the first instrumented version has been run to produce type information, they must be instrumented again taking the type information into account.

To instrument a stand-alone Perl program, invoke dfepl with the name of the program as an argument.

dfepl program.pl

To instrument a Perl module or a collection of modules, invoke dfepl either with the name of each module, or with the name of a directory containing the modules. To instrument all the modules in the current directory, give dfepl the argument .. For instance, if the current directory contains a module Acme::Trampoline in Acme/Trampoline.pm and another module Acme::Date in Acme/Date.pm, they can be annotated by either of the following two commands:

dfepl Acme/Trampoline.pm Acme/Date.pm
dfepl .

Once type information is available, run the instrumentation command again with the -T or -t options added to use the produced type information.

For more information about dfepl, see dfepl.


Previous: Instrumenting Perl programs, Up: Detecting invariants in Perl programs   [Contents][Index]

3.4.2 Perl examples

The Daikon distribution includes sample Perl programs suitable for use with Daikon in the examples/perl-examples directory.

Here are step-by-step instructions for examining a simple module, Birthday.pm, as used by a test script test-bday.pl.

  1. Change to the directory containing the Birthday.pm module.
    cd examples/perl-examples
    
  2. Instrument the Birthday.pm file.
    dfepl Birthday.pm
    

    This command creates a directory daikon-untyped, and puts the instrumented version of Birthday.pm into daikon-untyped/Birthday.pm. As the directory name implies, this instrumented version doesn’t contain type information.

  3. Run a test suite using the instrumented Birthday.pm file.
    dtype-perl test_bday.pl 10
    

    The dtype-perl is a script that runs Perl with the appropriate command line options to find the modules used by the Daikon Perl runtime tracing modules, and to use the instrumented versions of modules in daikon-untyped in preference to their original ones. The number 10 is an argument to the test_bday.pl script telling it to run a relatively short test.

    This will also generate a file daikon-instrumented/Birthday.types recording the type of each variable seen during the execution of the instrumented program.

  4. Re-annotate the module using the type information.
    dfepl -T Birthday.pm
    

    This step repeats step 2, except that the -T flag to dfepl tells it to use the type information generated in the previous step, and to put the output in the directory daikon-instrumented. dfepl also converts the type information into a file daikon-output/Birthday.decls containing subroutine declarations suitable for Daikon.

  5. Run the full test suite with the type-instrumented Birthday.pm.
    dtrace-perl test_bday.pl 30
    

    Here we run another test suite, which happens to be the same test_bday.pl, but running for longer. (The example will also work with a smaller number). The script dtrace-perl is similar to dtype-perl mentioned earlier, but looks for instrumented source files in daikon-instrumented.

    This creates daikon-output/test_bday-combined.dtrace, a trace file containing the values of variables at each invocation. (The filename is formed from the name of the test program, with -combined appended because it contains the trace information from all the instrumented modules invoked from the program).

  6. Change to the daikon-output directory to analyze the output.
    cd daikon-output
    
  7. Run Daikon on the trace file
    java daikon.Daikon Birthday.decls test_bday-combined.dtrace
    
  8. Examine the invariants. They are printed to standard output, and they are also saved to file Birthday.inv.gz, which you can manipulate with the PrintInvariants program and other Daikon tools. For example:
    java daikon.PrintInvariants Birthday.inv.gz
    

    Invariants produced from Perl programs can be examined using the same tools as other Daikon invariants.

In the example above, the script test_bday.pl was not itself instrumented; it was only used to test the instrumented code. The Perl front end can also be used to instrument stand-alone Perl programs. The following sequence of commands, similar to those above, show how Daikon can be used with the stand-alone program standalone.pl, also in the examples/perl-examples directory.

dfepl standalone.pl
dtype-perl daikon-untyped/standalone.pl
dfepl -T standalone.pl
dtrace-perl daikon-instrumented/standalone.pl
cd daikon-output
java daikon.Daikon -o standalone.inv standalone-main.decls \
     standalone-combined.dtrace

Note two differences when running a stand-alone program. First, the instrumented versions of the program, in the daikon-untyped or daikon-instrumented directory, are run directly. Second, the declarations file is named after the package in which the subroutines were declared, but since every stand-alone program uses the main package, the name of the program is prepended to the .decls file name to avoid collisions.


Previous: Detecting invariants in Perl programs, Up: Example usage   [Contents][Index]

3.5 Detecting invariants in Eiffel programs

CITADEL is an Eiffel front-end to the Daikon invariant detector. You can obtain Citadel and its documentation from http://se.inf.ethz.ch/people/polikarpova/citadel/.


Previous: Detecting invariants in Perl programs, Up: Example usage   [Contents][Index]