lezebulon
lezebulon

Reputation: 7994

is there a tool to log the code execution?

Let's say I have a program made of several "basic" algorithms on integral variables, such as :

if(a<b)
   a += c;

Is there a tool that would allow me to automatically log all the changes made to the different variables at run time?

For instance it would display in that case in a log file:

 "condition passed because 5=a < b=10
     a += 10; because c=10"

or some equivalent.

I am aware that I could manually log each operation but that would be much too complex. Is there any tool that would allow me to do something like that? I don't care about refactoring / recompiling as long as it's not totally manual.

Upvotes: 2

Views: 209

Answers (2)

coproc
coproc

Reputation: 6247

You can write your own integer class that overloads the operators accordingly (with automatic logging). If the class also provides implicit conversion (a constructor from int and a conversion operator to int), then you "only" need to change the types of variables and parameters to have your automatic logging of values. But instead of names you could only log addresses (or something derived from it like var20). With the help of a #define you could easily switch between raw ints (without logging) or your integer class with logging.

To get also the names of the variables into the logging one would either have to rewrite the operators with macros like

if (LESS(a,b))
    INC(a,c)

or have a parser that automatically transforms your code into something like this. But I am not aware of any existing tool providing this.

Upvotes: 1

bames53
bames53

Reputation: 88155

I have a hard time imagining that logging the complete execution of a program like this would be useful. A simple std::cout << "hello, world!\n"; would produce a mass of useless logs.

What do you actually need to do? If you want to debug code you should probably use a debugger to examine the program as it runs instead of using a printf-debugging-gone-horribly-wrong strategy. If you want a way to describe the complete execution for later examination/manipulation you could make sure the program behaves deterministically and just save the program input.

The right solution depends on the actual problem, but it's not likely that complete execution logging is the correct solution to anything.

Upvotes: 0

Related Questions