Macaulay2 » Documentation
Packages » Macaulay2Doc » The Macaulay2 language » debugging » the debugger
next | previous | forward | backward | up | index | toc

# the debugger

We have a Macaulay2 source file with a pair of functions in it that we can use for demonstrating the debugger. Let's load it so we can run the functions in it.

    i1 : load "Macaulay2Doc/demos/demo1.m2"

We can see what functions were provided to us with listUserSymbols.

    i2 : listUserSymbols

    o2 = symbol  class            value  location of symbol
         ------  -----            -----  ------------------
         g       FunctionClosure  g      /usr/share/Macaulay2/Macaulay2Doc/demos/demo1.m2:11:0-11:1

Let's peek at the code of the function g.

    i3 : code g

    o3 = /usr/share/Macaulay2/Macaulay2Doc/demos/demo1.m2:11:4-14:9: --source code:
         g = y -> (
              c := f(y-1);
              d := f(y-2);
              c+d)

We see that the function g calls a function f, but f is not visible to us (because f is a local variable). In Emacs' Macaulay2 Interaction Mode, pressing return (RET or enter) after positioning the cursor on the output line displaying the file name and line number will bring up the source code in a new buffer.

The first few times we use g, it seems to work.

    i4 : g 4

         17
    o4 = --
          6

    o4 : QQ

    i5 : g 3

         7
    o5 = -
         2

    o5 : QQ

However, the following attempt results in an error, and the debugger starts up automatically.

    i6 : g 2
    /usr/share/Macaulay2/Macaulay2Doc/demos/demo1.m2:8:11:(3):[2]: error: division by zero
    /usr/share/Macaulay2/Macaulay2Doc/demos/demo1.m2:8:11:(3): entering debugger (enter 'help' to see commands)
    /usr/share/Macaulay2/Macaulay2Doc/demos/demo1.m2:8:10-8:13: --source code:
         b := 1/x;

You may use help, as instructed, to view the commands available in the debugger. As suggested by the help display, we can use listLocalSymbols to list the local symbols and their values.

    ii7 : listLocalSymbols

    oo7 = symbol  class            value                                                    location of symbol
          ------  -----            -----                                                    ------------------
          a       String           "hi there"                                               /usr/share/Macaulay2/Macaulay2Doc/demos/demo1.m2:7:5-7:6
          b       Nothing          null                                                     /usr/share/Macaulay2/Macaulay2Doc/demos/demo1.m2:8:5-8:6
          x       ZZ               0                                                        /usr/share/Macaulay2/Macaulay2Doc/demos/demo1.m2:6:5-6:6
          f       FunctionClosure  FunctionClosure[/usr/share/Macaulay2/Macaulay2Doc/demo.  /usr/share/Macaulay2/Macaulay2Doc/demos/demo1.m2:6:0-6:1

We see that the value of x is 0, and that explains the error message about division by zero. The other local symbols are the ones defined in the body of the function f, whose code can now be displayed with code.

    ii8 : code f

    oo8 = /usr/share/Macaulay2/Macaulay2Doc/demos/demo1.m2:6:5-9:9: --source code:
          f := x -> (
               a := "hi there";
               b := 1/x;
               b+1)

We can use step with argument 0 to bypass the current expression.

    ii9 : step 0
    /usr/share/Macaulay2/Macaulay2Doc/demos/demo1.m2:8:11:(3):[2]: --stepping limit reached
    /usr/share/Macaulay2/Macaulay2Doc/demos/demo1.m2:8:11:(3): entering debugger (enter 'help' to see commands)
    /usr/share/Macaulay2/Macaulay2Doc/demos/demo1.m2:8:10-8:13: --source code:
         b := 1/x;

If we decide the problem is one level up, we can use end or the end-of-file character (which often is CTRL-D) to quit this instance of the debugger. In this case, the debugger will be entered again (triggered by the same error indication that caused it to be entered originally) at the point inside the function g from which the function f was called.

    ii10 : end
    /usr/share/Macaulay2/Macaulay2Doc/demos/demo1.m2:13:11:(3): entering debugger (enter 'help' to see commands)
    /usr/share/Macaulay2/Macaulay2Doc/demos/demo1.m2:13:10-13:16: --source code:
         d := f(y-2);

We can use listLocalSymbols again to see the local variables of g.

    ii11 : listLocalSymbols

    oo11 = symbol  class            value                                                    location of symbol
           ------  -----            -----                                                    ------------------
           c       QQ               2                                                        /usr/share/Macaulay2/Macaulay2Doc/demos/demo1.m2:12:5-12:6
           d       Nothing          null                                                     /usr/share/Macaulay2/Macaulay2Doc/demos/demo1.m2:13:5-13:6
           y       ZZ               2                                                        /usr/share/Macaulay2/Macaulay2Doc/demos/demo1.m2:11:4-11:5
           f       FunctionClosure  FunctionClosure[/usr/share/Macaulay2/Macaulay2Doc/demo.  /usr/share/Macaulay2/Macaulay2Doc/demos/demo1.m2:6:0-6:1

After we are done debugging, we can quit the debugger entirely and return to top level with break.

    ii12 : break

The variable errorDepth can be used to control how deep inside the code the debugger should be activated.

## See also

- break -- break from a loop
- end -- stop loading a file
- step -- step by single lines in the debugger
- continue -- continue with the next iteration of a loop
- return -- return from a function
- listLocalSymbols -- display of local symbols and their values
- listUserSymbols -- display the user's symbols
- code -- display source code
- value -- evaluate
- pseudocode -- produce the pseudocode for a function
- disassemble -- disassemble a pseudocode or function
- errorDepth -- set the error printing depth

### Menu

- step -- step by single lines in the debugger

The source of this document is in /build/macaulay2-88fgJW/macaulay2-1.25.11+ds/M2/Macaulay2/packages/Macaulay2Doc/ov_debugging.m2:115:0.