Source Level Debugging
======================

Maxima has a built-in source level debugger. The user can set a breakpoint at a function, and then step line by line from there.  The call stack may be examined, together with the variables bound at that level.

The command :help or :h shows the list of debugger commands. (In general, commands may be abbreviated if the abbreviation is unique.  If not unique, the alternatives will be listed.) Within the debugger, the user can also use any ordinary Maxima functions to examine, define, and manipulate variables and expressions.

A breakpoint is set by the :br command at the Maxima prompt. Within the debugger, the user can advance one line at a time using the :n (``next'') command. The :bt (``backtrace'') command shows a list of stack frames. The :r (``resume'') command exits the debugger and continues with execution.  These commands are demonstrated in the example below.

     (%i1) load ("/tmp/foobar.mac");

     (%o1)                           /tmp/foobar.mac

     (%i2) :br foo
     Turning on debugging debugmode(true)
     Bkpt 0 for foo (in /tmp/foobar.mac line 1)

     (%i2) bar (2,3);
     Bkpt 0:(foobar.mac 1)
     /tmp/foobar.mac:1::

     (dbm:1) :bt                        <-- :bt typed here gives a backtrace
     #0: foo(y=5)(foobar.mac line 1)
     #1: bar(x=2,y=3)(foobar.mac line 9)

     (dbm:1) :n                         <-- Here type :n to advance line
     (foobar.mac 2)
     /tmp/foobar.mac:2::

     (dbm:1) :n                         <-- Here type :n to advance line
     (foobar.mac 3)
     /tmp/foobar.mac:3::

     (dbm:1) u;                         <-- Investigate value of u
     28

     (dbm:1) u: 33;                     <-- Change u to be 33
     33

     (dbm:1) :r                         <-- Type :r to resume the computation

     (%o2)                                1094

The file /tmp/foobar.mac is the following:

     foo(y) := block ([u:y^2],
       u: u+3,
       u: u^2,
       u);

     bar(x,y) := (
       x: x+2,
       y: y+2,
       x: foo(y),
       x+y);

USE OF THE DEBUGGER THROUGH EMACS

If the user is running the code under GNU emacs in a shell window (dbl shell), or is running the graphical interface version, Xmaxima, then if he stops at a break point, he will see his current position in the source file which will be displayed in the other half of the window, either highlighted in red, or with a little arrow pointing at the right line.  He can advance single lines at a time by typing M-n (Alt-n).

Under Emacs you should run in a dbl shell, which requires the dbl.el file in the elisp directory. Make sure you install the elisp files or add the Maxima elisp directory to your path: e.g., add the following to your .emacs file or the site-init.el

     (setq load-path (cons "/usr/share/maxima/5.9.1/emacs" load-path))
     (autoload 'dbl "dbl")

then in emacs

     M-x dbl

should start a shell window in which you can run programs, for example Maxima, gcl, gdb etc.   This shell window also knows about source level debugging, and display of source code in the other window.

The user may set a break point at a certain line of the file by typing C-x space.  This figures out which function the cursor is in, and then it sees which line of that function the cursor is on.   If the cursor is on, say, line 2 of foo, then it will insert in the other window the command, ``:br foo 2'', to break foo at its second line.   To have this enabled, the user must have maxima-mode.el turned on in the window in which the file foobar.mac is visiting.  There are additional commands available in that file window, such as evaluating the function into the Maxima, by typing Alt-Control-x.

Categories: Debugging

Keyword Commands
================

Keyword commands are special keywords which are not interpreted as Maxima expressions.  A keyword command can be entered at the Maxima prompt or the debugger prompt, although not at the break prompt. Keyword commands start with a colon, ':'. For example, to evaluate a Lisp form you may type :lisp followed by the form to be evaluated.

     (%i1) :lisp (+ 2 3)
     5

The number of arguments taken depends on the particular command.  Also, you need not type the whole command, just enough to be unique among the break keywords.   Thus :br would suffice for :break.

The keyword commands are listed below.

:break F n
     Set a breakpoint in function F at line offset n from the beginning of the function. If F is given as a string, then it is assumed to be a file, and n is the offset from the beginning of the file. The offset is optional. If not given, it is assumed to be zero (first line of the function or file).

:bt
     Print a backtrace of the stack frames

:continue
     Continue the computation

:delete
     Delete the specified breakpoints, or all if none are specified

:disable
     Disable the specified breakpoints, or all if none are specified

:enable
     Enable the specified breakpoints, or all if none are specified

:frame n
     Print stack frame n, or the current frame if none is specified

:help
     Print help on a debugger command, or all commands if none is specified

:info
     Print information about item

:lisp some-form
     Evaluate some-form as a Lisp form

:lisp-quiet some-form
     Evaluate Lisp form some-form without any output

:next
     Like :step, except :next steps over function calls

:quit
     Quit the current debugger level without completing the computation

:resume
     Continue the computation

:step
     Continue the computation until it reaches a new source line

:top
     Return to the Maxima prompt (from any debugger level) without completing the computation

Note: Keyword commands must (currently) start at the beginning of a line. Not even a single space character is allowed before the colon.

Categories: Debugging

Functions and Variables for Debugging
=====================================

 -- Option variable: debugmode
     Default value: false

     When debugmode is true, Maxima will start the Maxima debugger when a Maxima error occurs.  At this point the user may enter commands to examine the call stack, set breakpoints, step through Maxima code, and so on. See debugging for a list of Maxima debugger commands.

     When debugmode is lisp, Maxima will start the Lisp debugger when a Maxima error occurs.

     In either case, enabling debugmode will not catch Lisp errors.

     Categories: Debugging Global flags

 -- Option variable: refcheck
     Default value: false

     When refcheck is true, Maxima prints a message each time a bound variable is used for the first time in a computation.

     Categories: Evaluation Console interaction Global flags

 -- Option variable: setcheck
     Default value: false

     If setcheck is set to a list of variables (which can be subscripted), Maxima prints a message whenever the variables, or subscripted occurrences of them, are bound with the ordinary assignment operator :, the :: assignment operator, or function argument binding, but not the function assignment := nor the macro assignment ::= operators. The message comprises the name of the variable and the value it is bound to.

     setcheck may be set to all or true thereby including all variables.

     Each new assignment of setcheck establishes a new list of variables to check, and any variables previously assigned to setcheck are forgotten.

     The names assigned to setcheck must be quoted if they would otherwise evaluate to something other than themselves. For example, if x, y, and z are already bound, then enter

          setcheck: ['x, 'y, 'z]$

     to put them on the list of variables to check.

     No printout is generated when a variable on the setcheck list is assigned to itself, e.g., X: 'X.

     Categories: Console interaction Global flags

 -- Option variable: setcheckbreak
     Default value: false

     When setcheckbreak is true, Maxima will present a break prompt whenever a variable on the setcheck list is assigned a new value. The break occurs before the assignment is carried out. At this point, setval holds the value to which the variable is about to be assigned. Hence, one may assign a different value by assigning to setval.

     See also setcheck and setval

     Categories: Console interaction Global flags

 -- System variable: setval

     Holds the value to which a variable is about to be set when a setcheckbreak occurs. Hence, one may assign a different value by assigning to setval.

     See also setcheck and setcheckbreak

     Categories: Console interaction Global variables

 -- Function: timer (F_1, ..., F_N)
     timer (all)
     timer ()

     Given functions F_1, ..., F_N, timer puts each one on the list of functions for which timing statistics are collected. timer(f)$ timer(g)$ puts f and then g onto the list; the list accumulates from one call to the next.

     timer(all) puts all user-defined functions (as named by the global variable functions) on the list of timed functions.

     With no arguments,  timer returns the list of timed functions.

     Maxima records how much time is spent executing each function on the list of timed functions. timer_info returns the timing statistics, including the average time elapsed per function call, the number of calls, and the total time elapsed. untimer removes functions from the list of timed functions.

     timer quotes its arguments. f(x) := x^2$ g:f$ timer(g)$ does not put f on the timer list.

     If trace(f) is in effect, then timer(f) has no effect; trace and timer cannot both be in effect at the same time.

     See also timer_reset and timer_devalue

     Categories: Debugging

 -- Function: timer_reset
     timer_reset (F_1, ..., F_N)
     timer_reset ()

     Given functions F_1, ..., F_N, timer_reset sets the accumulated elapsed time for each function to zero.

     With no arguments, timer_reset sets the accumulated elapsed time for each function on the global timer list to zero.

     Categories: Debugging

 -- Function: untimer (F_1, ..., F_N)
     untimer ()

     Given functions F_1, ..., F_N, untimer removes each function from the timer list.

     With no arguments, untimer removes all functions currently on the timer list.

     After untimer (f) is executed, timer_info (f) still returns previously collected timing statistics, although timer_info() (with no arguments) does not return information about any function not currently on the timer list. timer (f) resets all timing statistics to zero and puts f on the timer list again.

     Categories: Debugging

 -- Option variable: timer_devalue
     Default value: false

     When timer_devalue is true, Maxima subtracts from each timed function the time spent in other timed functions.  Otherwise, the time reported for each function includes the time spent in other functions. Note that time spent in untimed functions is not subtracted from the total time.

     See also timer and timer_info

     Categories: Debugging Global flags

 -- Function: timer_info (F_1, ..., F_N)
     timer_info ()

     Given functions F_1, ..., F_N, timer_info returns a matrix containing timing information for each function. With no arguments, timer_info returns timing information for all functions currently on the timer list.

     The matrix returned by timer_info contains the function name, time per function call, number of function calls, total time, and gctime, which meant "garbage collection time" in the original Macsyma but is now always zero.

     The data from which timer_info constructs its return value can also be obtained by the get function:

          get(f, 'calls);  get(f, 'runtime);  get(f, 'gctime);

     See also timer and timer_reset

     Categories: Debugging

 -- Function: trace (F_1, ..., F_N)
     trace (all)
     trace ()

     Given functions F_1, ..., F_N, trace instructs Maxima to print out debugging information whenever those functions are called. trace(f)$ trace(g)$ puts f and then g onto the list of functions to be traced; the list accumulates from one call to the next.

     trace(all) puts all user-defined functions (as named by the global variable functions) on the list of functions to be traced.

     With no arguments, trace returns a list of all the functions currently being traced.

     The untrace function disables tracing. See also trace_options

     trace quotes its arguments.  Thus, f(x) := x^2$ g:f$ trace(g)$ does not put f on the trace list.

     When a function is redefined, it is removed from the timer list. Thus after timer(f)$ f(x) := x^2$, function f is no longer on the timer list.

     If timer (f) is in effect, then trace (f) has no effect; trace and timer can't both be in effect for the same function.

     Categories: Debugging

 -- Function: trace_options
     trace_options (F, OPTION_1, ..., OPTION_N)
     trace_options (F)

     Sets the trace options for function F. Any previous options are superseded. trace_options (F, ...) has no effect unless trace (F) is also called (either before or after trace_options).

     trace_options (F) resets all options to their default values.

     The following option keywords are recognized. The presence of the option keyword alone enables the option unconditionally. Specifying an option keyword with a predicate function P as its argument makes the option conditional on the predicate.

       * noprint, noprint(P) Do not print a message at function entry and exit.

       * break, break(P) Stop execution before the function is entered, and after the function is exited. See also break. The arguments of the function and its return value are available as trace_break_arg when entering and exiting the function, respectively.

       * lisp_print, lisp_print(P) Display arguments and return values as Lisp objects.

       * info, info(P) Display the return value of P at function entry and exit. The function P may also have side effects, such as displaying output or modifying global variables.

       * errorcatch, errorcatch(P) Catch errors, giving the option to signal an error, retry the function call, or specify a return value.

     The predicate function, if supplied, is called with four arguments.

       * The recursion level for the function, an integer.

       * Whether the function is being entered or exited, indicated by a symbol, either enter or exit, respectively.

       * The name of the function, a symbol.

       * The arguments of the traced function (on entering) or the function return value (on exiting).

     If the predicate function returns false, the corresponding trace option is disabled; if any value other than false value is returned, the trace option is enabled.

     trace_options quotes (does not evaluate) its arguments.

     Examples:

     The presence of the option keyword alone enables the option unconditionally.

          (%i1) ff(n) := if equal(n, 0) then 1 else n * ff(n - 1);
          (%o1)    ff(n) := if equal(n, 0) then 1 else n ff(n - 1)
          (%i2) trace (ff);
          (%o2)                         [ff]
          (%i3) trace_options (ff, lisp_print, break);
          (%o3)                  [lisp_print, break]
          (%i4) ff(3);
          Trace entering ff level 1

          Entering a Maxima break point. Type 'exit;' to resume.
          _trace_break_arg;
          [3]
          _exit;
          (1 ENTER $FF (3))
          Trace entering ff level 2

          Entering a Maxima break point. Type 'exit;' to resume.
          _exit;
           (2 ENTER $FF (2))
          Trace entering ff level 3

          Entering a Maxima break point. Type 'exit;' to resume.
          _exit;
            (3 ENTER $FF (1))
          Trace entering ff level 4

          Entering a Maxima break point. Type 'exit;' to resume.
          _exit;
             (4 ENTER $FF (0))
          Trace exiting ff level 4

          Entering a Maxima break point. Type 'exit;' to resume.
          _exit;
             (4 EXIT $FF 1)
          Trace exiting ff level 3

          Entering a Maxima break point. Type 'exit;' to resume.
          _exit;
            (3 EXIT $FF 1)
          Trace exiting ff level 2

          Entering a Maxima break point. Type 'exit;' to resume.
          _exit;
           (2 EXIT $FF 2)
          Trace exiting ff level 1

          Entering a Maxima break point. Type 'exit;' to resume.
          _trace_break_arg;
          6
          _exit;
          (1 EXIT $FF 6)
          (%o4)                           6

     Specifying an option keyword with a predicate function P as its argument makes the option conditional on the predicate.

          (%i1) ff(n) := if equal(n, 0) then 1 else n * ff(n - 1);
          (%o1)    ff(n) := if equal(n, 0) then 1 else n ff(n - 1)
          (%i2) trace (ff);
          (%o2)                         [ff]
          (%i3) trace_options (ff, break(pp));
          (%o3)                      [break(pp)]
          (%i4) pp (level, direction, fnname, item) := (print (item), fnname = 'ff and level = 3 and direction = 'exit);
          (%o4) pp(level, direction, fnname, item) :=
          (print(item), (fnname = 'ff) and (level = 3)
           and (direction = 'exit))
          (%i5) ff(6);
          [6]
          1 Enter ff [6]
          [5]
           2 Enter ff [5]
          [4]
            3 Enter ff [4]
          [3]
             4 Enter ff [3]
          [2]
              5 Enter ff [2]
          [1]
               6 Enter ff [1]
          [0]
                7 Enter ff [0]
          1
                7 Exit  ff 1
          1
               6 Exit  ff 1
          2
              5 Exit  ff 2
          6
             4 Exit  ff 6
          24
          Trace exiting ff level 3

          Entering a Maxima break point. Type 'exit;' to resume.
          _trace_break_arg;
          24
          _exit;
            3 Exit  ff 24
          120
           2 Exit  ff 120
          720
          1 Exit  ff 720
          (%o5)                          720

     Categories: Debugging

 -- Debugging variable: trace_break_arg

     When a traced function stops at a breakpoint, trace_break_arg is bound to the value of function arguments when entering the function, or the return value of the function, when exiting.

     Breakpoints for traced functions are specified by the option keyword break of the function trace_options, which see.

     Categories: Debugging

 -- Function: untrace
     untrace (F_1, ..., F_N)
     untrace ()

     Given functions F_1, ..., F_N, untrace disables tracing enabled by the trace function. With no arguments, untrace disables tracing for all functions.

     untrace returns a list of the functions for which it disabled tracing.

     Categories: Debugging