GX-TXT Debugger · Guide 02 — Python debugger: controlled execution and state inspection
This guide is part of the project GX-TXT Script Debugger. The screens come from real executions in GX-TXT dev112-P011-bis, already made for the debugger course; examples use synthetic data. The D codes identify the corresponding exercises of the full course on the debugger.
Download the six practical guides’ scripts (ZIP). To repeat this guide, extract the guide_02/scripts folder files.
D13 — Step Over in a sequence
Step Over performs current education and, in the ordinary case of a function call, returns to the next row of the caller without stopping within the called function.
Prepare the example
Import D13_nested.py to Python Scripts, save in the session broom and set a breakpoint to adjusted = adjust(sample). The example distributed with GX-TXT sums the values 1, 2, 3, 4 and 5: the expected result is NESTED_RESULT 15. The D13_nested.m correspondent uses the same calculation in Octave.
Start View → Debugger → Start Debug. If the initial breakpoint is before the breakpoint, press Continue to reach it. In the first iteration sample is 0 and total is 0. Press Step Over on the adjust(sample) call.
The arrow moves to the total += adjusted line. The panel shows adjusted = 1, while total = 0: the update of the total has yet to be executed. An additional Step Over performs the sum. The red point on the call remains and can stop the subsequent iterations again.
Exercise
To the first iteration which will be total after performing total += adjusted?
Worked solution
Vale 1: the previous total is 0 and adjusted worth 1. The line highlighted in the screenshot has not yet been executed.
D14 — Step Into a function
Step Into allows you to follow a call inside a language function. It is the useful command when the hypothesis concerns the internal calculation, rather than its only result.
Enter to adjust
Use D13_nested.py recorded at Session. Set a breakpoint on the adjusted = adjust(sample) line and start the debug. On the call break, press Step Into. If you leave from the first break of the program, use Continue to reach the breakpoint.
In the illustrated test we find ourselves at the second iteration: sample is 1. The debugger enters the adjust function, shows the increment = value + 1 line and the local value = 1 variable.
The Call Stack panel shows adjust, accumulate and the script level. value is the local topic of the function called; sample instead belongs to the caller. At the first iteration the corresponding values are 0, because range(5) begins with 0.
Exercise
If sample is worth 1, what value will adjust return after performing increment = value + 1?
Worked solution
It will remain 2. Before the assignment, the debugger is on the calculation line and value is 1.
D15 — Step Out and return to the caller
After inspecting a function, Step Out continues its execution until returning to the caller. In this way you do not need to manually follow each instruction left in the function.
Exit to adjust
Open D13_nested.py, put a breakpoint on the adjust(sample) call and use Step Into as in the D14. When you’re stationed in adjust, press Step Out.
In the test, at the second iteration, the debugger returns to accumulate on the total += adjusted line. The panel shows sample = 1, adjusted = 2 and total = 1: the returned value has been assigned, but the upgrade of total is still to be executed. In the first iteration adjusted would be 1 and the previous total 0.
Any breakpoint encountered in the path can affect the next break: always check arrow, stack and state, instead of assuming the point reached.
Exercise
After the screenshot, how much will the total pass when the highlighted line will be executed?
Worked solution
3: the previous total is 1 and the value returned from the second call is 2.
D16 — The Call Stack and the selected frame
The call stack describes how the execution came to the pause point. Each row represents a frame, with the function, file and location associated.
Compare function and caller
With D13_nested.py, stop at adjust using the breakpoint on the call and Step Into, as in the D14. The stack contains adjust, its caller accumulate and the script level.
Select accumulate in the Call Stack panel to examine the caller. In the frame of the called function you find value; in the caller you can compare sample, total and other local variables present at that time.
Selecting a stack row does not run the program and does not move its true execution point. Change the context you are looking at; to resume with the execution commands, check the active frame and the arrow shown. An external frame can refer to another file: always read the path and the line number.
Exercise
If you select the caller while adjust is on break, is the adjust function already back?
Worked solution
No. The selection is used to observe another frame. To continue the function you need an execution command, for example Step Out.
Other steps of this project
- Debugging GNU Octave in GX-TXT: Breakpoints, Variables and Public APIs
- GX-TXT Debugger · Module 08 — Projects, revisions and Sessions
- GX-TXT Debugger · Module 09 — Integrated practical cases
- GX-TXT Debugger · Module 10 — Independent debugging and the final project
- GX-TXT Debugger · Guide 05 — Debugging scientific data: vectors, structures and Datasets
- GX-TXT Debugger · Guide 06 — Debugging a complete scientific workflow