GNOME Builder 3.26 ships a number of new features, including improved symbol searching, inline documentation and integrated debugging. Prior to this, developers could code and compile their program in Builder, but would need to switch to a separate debugger (either gdb or a graphical debugger like Nemiver). Now that Builder integrates a debugger, a developer can write, compile and run their program with an attached debugger right inside Builder!
Install GNOME Builder
Builder is available in the main Fedora repositories. Install it with dnf or GNOME Software. If you haven’t yet upgraded to Fedora 27, Builder 3.26 is also available in a Flatpak hosted on Flathub and can be installed on any Linux distribution that Flatpak supports.
Clone the project
Clone the stop-watch project in Builder by selecting Open Project from the application menu. Click the Clone… button and enter https://pagure.io/fedora-magazine/stop-watch.git into the Repository URL field. Click the Clone button to clone and open the project.
Debugging the project
Run the Stop Watch app by clicking the Run button (or press Control + F5). After the project compiles, the Stop Watch window opens.
Note that the Run button may be disabled at first. This is because Builder is downloading the necessary runtimes needed to develop the application. Check the transfers pop-up in the right of the menubar for progress on these downloads. Once the downloads have completed, the build button will enable.
Now try starting the stop watch by clicking Start. Uh-oh. It’s not counting properly. It appears the timer never gets past 1 second.
Open src/stop-watch-window.c and set a breakpoint in the start_button_clicked_cb function by clicking on the line number (line 58 in this case). This will allow you to pause the program once the clicked signal handler is invoked on the Start button.
Now Run with Debugger by clicking the expand arrow next to the Run button, or press F5. Builder automatically sets a breakpoint at the entrypoint to your program (the entry to the main function). Below the editor view, there is a control panel showing the debug controls. Click the Continue button to continue the program execution, presenting the Stop Watch window. Now click the Start button again. This time the breakpoint inside start_button_clicked_cb catches and the program pauses again.
Once again, the debugging control panel will appear. Step over any function calls to maintain execution within this signal handler. As you step, the current line will advance. Since self->timer_started is FALSE, control jumps into the else block. Continue stepping, watching the flow of control through the program until you reach the last line of the signal handler. Look closely at line 76. Looks like the programmer forgot to toggle the timer_started variable! Change line 76 to:
self->timer_started = !self->timer_started;
Now run the program again. Click Start and the timer starts counting seconds properly.