Goals

For Java developers, it is quite familiar to use method call traces as instrument of development. Call traces help you to understand what the software does when performing a specific task. They can also help you to find bugs, crashes, endless loops, etc. Besides finding errors, they can help you to statistically analyze behavior by counting specific events. Call traces also make a software transparent. This is especially important for a project like Strus, that has still a very small user base. Transparency can create trust in a software when other peoples' experience is barely available to create this level of trust.

The goal of this article is to show you how to create method call traces in Strus.

Background

Strus is a set of libraries and tools to build search engines. It is split into several small projects (currently 8), one of them called strusTrace. It implements the logging of method call traces.

Introduction

There is no native support in C++ as compiled language for tracing as for example in Java. Tracing is a classical problem you wish to solve as own aspect. How you can bring aspect orientation into a software depends very much on the rules imposed on its components. For implementing tracing in Strus as own aspect, the following two properties of Strus were used:

In Strus every interface is a pure abstract class. This allows to create proxy classes that wrap each method call, and so any access to the class. A proxy can redirect the access to the real implementation while doing something else behind the scenes (like for example to log, what was called).

All objects implementing interfaces are created either by a interface method call or by a "root" object. This pattern helps to control the creation of objects and allows wrapping each object created with a proxy instance.

The project strusTrace uses some code generation to implement a proxy for every strus and strusAnalyzer interface class. Finally, the root objects (object builder interface) are wrapped by a proxy implementation.

Using Call Traces

Call trace logging is configured in a string with semicolon (';') separated assignments of configuration variables. One of them is the identifier of the trace logger to use specified with the configuration variable 'log'. Some trace loggers are part of strusTrace. If the standard loggers are not enough, you can define one on your own and load it as a strus module. The following standard trace loggers are predefined:

dump

logs all methods called immediately to a file or to stdout. This logger can be useful for analyzing behavior in the case of a crash or endless loop. The example

log=dump;file=stdout

prints all methods called and the method termination event to stdout.

json

builds a dependency tree of method calls in JSON and writes this tree to a file or stdout. This is a logger that allows you to navigate in a call tree of a terminated Strus run. The example

log=json;file=tree.json

prints the method call tree as JSON tree to a file tree.json.

count

counts all calls of a specific method of a defined class. You can print the total number of calls to a file or stdout or the number of calls grouped by objects implementing a defined interface. The example

counts the calls of the method seekUpperBound of a DatabaseCursorInterface implementation for any descendant of a PostingIteratorInterface implementation. It outputs a list of lines with pairs of numbers with the object id of the PostingIteratorInterface implementation followed by the number of seekUpperBound calls counted in the context of this object.

breakpoint

calls a function strus_breakpoint when a method is called and the time counter is at a specified value. This method can be used to set a debugger breakpoint at a defined event in a method call trace. The example

log=breakpoint;call=125821

calls the function strus_breakpoint, when the method counter is at 125821. This allows you to start call a program using strus in a debugger and halt execution at the breakpoint defined as a specific event in the call trace.

Call Traces in Language Bindings

To use call traces in language bindings, you have to call the Strus context constructor with an additional string parameter specifying the call trace configuration. Tracing cannot be used in RPC client mode (it does not make sense in an RPC client anyway). For tracing in the RPC context, you can start the RPC server with call tracing configured. Here follows a list of example constructor calls:

Java

Context ctx = new Context( 0, "log=dump;file=stdout");

PHP

$ctx = new StrusContext( 0, "log=dump;file=dump.txt");

Python

ctx = strus.Context( 0, "log=json;file=tree.json" )

Call Traces for Command Line Tools

The strus command line tools like the programs implemented in the project strusUtilities and the RPC server implemented in the project strusRpc can be started with the option -T or --trace followed by the strus trace configuration.

The following example call of strusCreate generates a JSON call tree and prints it to the file tree.json:

The output got quite big for a method just creating a storage. You can imagine that an output can get huge, when doing more complicated stuff.

With the trace logger breakpoint, we can now pick one method call of our output log, say the one with time = 14, a method all of DatabaseTransactionInterface::write and call strusCreate with "log=breakpoint;call=14" as trace configuration. If we do this in a debugger, we can halt on this method call by setting a breakpoint to 'strus_breakpoint'.

Writing Your Own Call Trace Loggers

We introduce here an example trace logger implemented as strus module that can be dynamically loaded by a strus program or language binding. All you have to do is to implement the trace logger interface and build it as strus module.

The following code can be downloaded (see download link at the top of this article). It implements a trace logger that counts the number of method calls for each interface classname methodname pair.

storage successfully created.
Database::Destructor called 1 times
Database::createClient called 1 times
Database::createDatabase called 1 times
Database::getConfigParameters called 1 times
DatabaseClient::Destructor called 1 times
DatabaseClient::createTransaction called 1 times
DatabaseTransaction::Destructor called 1 times
DatabaseTransaction::commit called 1 times
DatabaseTransaction::write called 8 times
Storage::Destructor called 1 times
Storage::createStorage called 1 times
Storage::getConfigParameters called 1 times
StorageObjectBuilder::Destructor called 1 times
StorageObjectBuilder::getDatabase called 1 times
StorageObjectBuilder::getStorage called 1 times

Resume

Thanks to the very restrictive design patterns used in Strus, it is possible to create and implement a proxy universe of the interfaces. This makes things like tracing and RPC in Strus as aspect on its own possible. In this article, we showed how to use call traces in Strus. We hope that the availability of tracing will raise the level of trust and bring more users to Strus. For reading more about Strus, have a look at my other articles about Strus.

License

This article, along with any associated source code and files, is licensed under The MIT License