### 38.1 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 ·