How to debug script in Julia
In Julia's ecosystem, the Debugger.jl package allows us to navigate the program code and inspect its execution flow.
This process, known as code stepping, is active only in the full-fledged and interactive command-line
The debugger package helps find errors and can substantially reduce the time it takes to perfect our program. It provides lots of functions as follows:
Category | Command | Description |
Stepping in and out | n | It helps move to the next line |
s | It helps move to the next call | |
sr | It helps move until next return | |
st | It shows the status of the current function | |
Evaluation | w add expr | It inserts an expression into the evaluation list |
w | It displays all watched and evaluated expressions | |
Breakpoints | bp | It displays all configured breakpoints |
bp add " | It configures a breakpoint in the file | |
bp add func [:line] [ | It inserts a breakpoint to function |
Note: Please visit the
for a complete list of commands. official documentation https://github.com/JuliaDebug/Debugger.jl/blob/master/README.md
Let's discover how to debug Julia's script step by step.
Example
The example below illustrates the steps required to scrutinize a Julia script in a gradual manner:
echo 'Start Julia in command-line REPL mode'
echo ' using Debugger
println(PROGRAM_FILE);
f = function(a = 1, b = 2)
print("Start")
c = 3
print(c)
output = a + b + c
print(output)
print("End")
end
' > script.jlLet's explain the code widget above:
Line 1: Display a message inviting the user to run Julia in command-line REPL mode.
Lines 2–12: Devise a script that will:
Line 2: Load the Debugger package.
Line 3: Print the name of the script involved.
Lines 5–12: Define a simple function to debug and generate its code dynamically to a file called
script.jl.
When running this code widget, we should follow the steps listed below:
Step 1: Go to the "Terminal" tab and run the following command to execute the script elaborated above: bash /usercode/script.sh
Step 2: Type Julia and run it in command-line REPL mode.
Step 3: Run the script named script.jl as given below.
Step 4: Initiate the debugger by invoking the @enter command along with the function f as defined in the code above.
Step 5: Type n multiple times to move progressively from one line to another until we reach the end of the script. Every time, we press n the line to be executed is exhibited in the About to run: as shown in the following image.
Free Resources