Debugging in Yacas

# 6. Debugging in Yacas

##

6.1 Introduction

When writing a code segment, it is generally a good idea to separate the problem into many small functions. Not only can you then reuse these functions on other problems, but it makes debugging easier too.

For debugging a faulty function, in addition to the usual trial-and-error method and the "print everything" method, Yacas offers some trace facilities. You can try to trace applications of rules during evaluation of the function (`TraceRule()`, `TraceExp()`) or see the stack after an error has occurred (`TraceStack()`).

There is also an interactive debugger, which shall be introduced in this chapter.

Finally, you may want to run a debugging version of Yacas. This version of the executable maintains more information about the operations it performs, and can report on this.

This chapter will start with the interactive debugger, as it is the easiest and most useful feature to use, and then proceed to explain the trace and profiling facilities. Finally, the internal workings of the debugger will be explained. It is highly customizable (in fact, most of the debugging code is written in Yacas itself), so for bugs that are really difficult to track one can write custom code to track it.

##

6.2 The trace facilities

The trace facilities are:

`TraceExp` : traces the full expression, showing all calls to user- or system-defined functions, their arguments, and the return values. For complex functions this can become a long list of function calls.
`TraceRule` : traces one single user-defined function (rule). It shows each invocation, the arguments passed in, and the returned values. This is useful for tracking the behavior of that function in the environment it is intended to be used in.
`TraceStack` : shows a few last function calls before an error has occurred.
`Profile` : report on statistics (number of times functions were called, etc.). Useful for performance analysis.

The online manual pages (e.g. `?TraceStack`) have more information about the use of these functions.

An example invocation of `TraceRule` is

    In> TraceRule(x+y)2+3*5+4;

Which should then show something to the effect of

      TrEnter(2+3*5+4);
        TrEnter(2+3*5);
           TrArg(2,2);
              TrArg(3*5,15);
           TrLeave(2+3*5,17);
            TrArg(2+3*5,17);
            TrArg(4,4);
        TrLeave(2+3*5+4,21);
    Out> 21;