The Mosel Debugger
In Chapter Correcting errors in Mosel models we have seen how the Mosel Parser helps detect syntax errors during compilation. Other types of errors that are in general more difficult to analyze are mistakes in the data or logical errors in the formulation of Mosel models. The Mosel Debugger may help tracing these.
Using the Mosel Debugger
In this section we shall be working with the model prime2.mos. This is the same model for calculating prime numbers as the example we have seen in Section Working with sets, but with a LIMIT value set to 20,000.
Mosel models that are to be run in the debugger need to be compiled with the option G. The Mosel debugger is started with the command debug (it will automatically compile with the required settings):
mosel debug prime.mos
and terminated by typing quit. Just as for the run command the user may specify new settings for the model parameters immediately following the debug command:
mosel debug prime2.mos 'LIMIT=50'
Once the debugger is started, type in the following sequence of commands (Mosel's output is highlighted in bold face):
dbg> break 31 Breakpoint 1-1 set at prime2.mos:31 dbg> bcond 1-1 getsize(SNumbers) < 10 dbg> cont Prime numbers between 2 and 50: Breakpoint 1-1. 31 while (not(n in SNumbers)) n+=1 dbg> print n 13 dbg> display SNumbers 1(0): SNumbers = [17 19 23 29 31 37 41 43 47] dbg> display SPrime 2(0): SPrime = [2 3 5 7 11 13] dbg> cont Breakpoint 1-1. 31 while (not(n in SNumbers)) n+=1 1(0): SNumbers = [19 23 29 31 37 41 43 47] 2(0): SPrime = [2 3 5 7 11 13 17] dbg> cont Breakpoint 1-1. 31 while (not(n in SNumbers)) n+=1 1(0): SNumbers = [23 29 31 37 41 43 47] 2(0): SPrime = [2 3 5 7 11 13 17 19] dbg> cont Breakpoint 1-1. 31 while (not(n in SNumbers)) n+=1 1(0): SNumbers = [29 31 37 41 43 47] 2(0): SPrime = [2 3 5 7 11 13 17 19 23] dbg> quit |
This small example uses many of the standard debugging commands (for a complete list, including commands for navigating in the Mosel stack that are not shown here, please see the Section 'Running Mosel – Command line interpreter: debugger' of the introduction chapter of the Mosel Language Reference Manual):
- break
- Set a breakpoint in the given line. A breakpoint is deleted with delete followed by the breakpoint number. The command breakpoints lists all currently defined breakpoints.
- bcond
- Set a condition on a breakpoint (using the number of the breakpoint returned by the break command). Conditions are logical expressions formed according to the standard rules in Mosel (use of brackets, connectors and and or). They may contain any of the functions listed below.
- cont
- Continue the execution up to the next breakpoint (or to the end of the program). A line-wise evaluation is possible by using next or step (the former jumps over loops and subroutines, the latter steps into them).
- display
- Show the current value of a model object or an expression at every step of the debugger. A display is removed by calling undisplay followed by the number of the display.
- Show (once) the current value of a model object.
The following simple Mosel functions may be used with debugger commands (in conditions or with print / display):
- Arithmetic functions: abs, ceil, floor, round
- Accessing solution values: getsol, getdual, getrcost, getactivity, getslack
- Other: getparam, getsize
Debugging concurrent models
The Mosel debugger can be used with concurrent (sub)models. A few temporary edits to the model files may be necessary in this case (to be removed for production versions!):
- We need to use the compilation flag 'G' with all models that are to be debugged: this option is applied automatically by the Mosel debugger for the master model, but we need to use it explicitly for the submodels. For instance, if the submodels are compiled from the master model, we need to modify the compilation statement to include this flag:
if compile("G","prime2d.mos")<>0 then exit(1); end-if
- Breakpoints on submodels can only be set once the corresponding submodels have been started. If their execution is too fast to allow for user input in the debugger, we recommend to insert a 'sleep' at the start of the submodel (this subroutine is provided by the module mmsystem that needs to be loaded by the model) during the debugging phase. For example adding a 'sleep' of 5 seconds:
sleep(5000)
We can now start the debugger for the master model with a command like the following:
mosel debug runprime2d.mos 'LIMIT=50'
Once the debugger is started, type in the following sequence of commands (Mosel's output is highlighted in bold face):
dbg> break 29 Breakpoint 1-1 set at runprime2d.mos:29 dbg> cont 29 run(modPrime, "LIMIT="+LIMIT) dbg> next 30 wait [model #2 starting] dbg> model 2 * "prime2d.mos":18 18 LIMIT=20000 dbg> break 34 Breakpoint 2-1 set at prime2d.mos:34 dbg> bcond 2-1 getsize(SNumbers) < 10 dbg> cont Prime numbers between 2 and 50: Breakpoint 2-1. 34 while (not(n in SNumbers)) n+=1 dbg> print n 13 dbg> display SNumbers 1(#1): SNumbers = [17 19 23 29 31 37 41 43 47] dbg> display SPrime 2(#1): SPrime = [2 3 5 7 11 13] dbg> cont Breakpoint 2-1. 34 while (not(n in SNumbers)) n+=1 1(#1): SNumbers = [19 23 29 31 37 41 43 47] 2(#1): SPrime = [2 3 5 7 11 13 17] dbg> cont Breakpoint 2-1. ... dbg> quit |
This sequence sets a breakpoint on the submodel 'run' command in the master model. After the submodel is started it switches to using the submodel ('model 2') as the active model in the debugger; we can then enter the debug commands for the submodel. Notice that the command cont applies globally to all running models, whereas next or step refer to the selected active model.
Debugger in Xpress Workbench
With Xpress Workbench the debugger is started by clicking on the button . Workbench will automatically recompile the model with the required debugging flag. Breakpoints are set by clicking onto the gray area (left to the line number if it is displayed) preceding each row in the editor window, breakpoint conditions can be added via the right mouse button menu on the breakpoint icon. Clicking on the breakpoint icon deletes the breakpoint.
![]() ![]() |
Delete breakpoint/desactivated breakpoint. |
![]() ![]() |
Delete a conditional breakpoint/desactivated conditional breakpoint. |
Navigating in the debugger is possible by clicking on the corresponding buttons:
![]() ![]() |
Activate/desactivate all breakpoints. |
![]() ![]() |
Start/stop the debugger. |
![]() ![]() |
Resume/suspend model execution. |
![]() |
Step over an expression. |
![]() |
Step into an expression. |
![]() |
Step out of an expression. |
During a debugging session, the current position is indicated via a green arrow left to the line numbers (changing to yellow on breakpoints
). Expand the Variables display in the Debugger pane on the right side of the workspace to observe the values of model entities.