Tools and Techniques For Debugging I
Tools and Techniques For Debugging I
● Understand what IDEs are and how they facilitate software development,
including setting up and exploring an IDE through practical applications.
● Learn about various stand-alone debugging tools and how to use them
effectively, focusing on techniques like breakpoints and stepping-through
functions.
● Delve into tracing techniques that assist in debugging, such as using
"Assert," "Print," and "Stop."
The material in this module relates to the following subject learning outcomes:
Student Expectations
During this module, you are expected to spend time working through the prescribed
material prior to attending class. This will enable a more valuable discussion to take
place in online tutorials or conferences, where you will be able to discuss concepts,
present ideas and explore information collaboratively in greater depth.
4 hours for facilitated study. This includes guided facilitated study, watching or
attending course lectures (live or pre-recorded), responding to facilitator feedback.
8 hours for personal study. This includes reading through course material,
undertaking learning activities and exploring additional related resources.
Learner Guide
The Learner Guide provides the core knowledge for this topic. Work through the
materials and be prepared to discuss these in your forums, tutorials or classes.
Expand All
Panels
Collapse All
Panels
An IDE is a powerful software suite that combines various features and utilities to
facilitate the seamless development of software applications. Essentially, it is a
one-stop solution that aids programmers in writing, testing, and deploying code more
efficiently, effectively shortening the development cycle and enhancing productivity.
Today, IDEs are central to the software development landscape. They have not only
simplified coding but also fostered collaboration and innovation, allowing teams to
work cohesively and leverage each other's expertise to build complex and
sophisticated software solutions. Through features like debugging, code completion,
and collaboration tools, IDEs have truly transformed the way programmers approach
software development.
Explore the core functionalities of an IDE and understand the diverse customisation
opportunities that they offer to cater to individual project needs.
Features of an IDE
Most IDEs provide extensive personalisation options, allowing for the addition of
extensions that support different languages, debuggers, or version control systems.
For example, Visual Studio Code maintains a marketplace for a variety of extensions
to accommodate diverse needs.
● Integrated Terminal
An attribute that optimises your workflow by enabling shell commands
execution directly within the IDE.
● Code Profiling
Certain IDEs offer built-in tools for measuring various metrics such as
memory usage and CPU consumption.
● Database Management
Some IDEs incorporate features for executing SQL queries and managing
databases directly within the platform.
● Dependency Management
Facilitates easy addition, update, or removal of external libraries that your
project relies upon.
● Code Review Tools
These facilitate team members to comment on each other’s code, propose
changes, and merge pull requests directly within the IDE.
● Support for Multiple Languages
Most IDEs are versatile, offering support for a range of programming
languages.
● Collaboration Features
Modern IDEs come equipped with features to foster team collaboration,
including functionalities like pair programming, code sharing, and real-time
coding sessions. Visual Studio, for instance, hosts a feature named “Live
Share” for this purpose.
Further Reading
What is an IDE?
Explore the IDE concept further and discover the different types such as Desktop
IDEs, Cloud IDEs, Mobile App Development IDEs, and Database-Specific IDEs.
Learn how to choose an IDE based on factors like programming language, operating
system, pricing model, and features.
Reference: Geeks for Geeks, n.d., 'What is an IDE?', Geeks for Geeks, accessed 31 January 2024,
<https://www.geeksforgeeks.org/what-is-ide/>
In this case study, we examine a real-world scenario where a team of data scientists
at a prominent research institution leveraged the power of an Integrated
Development Environment (IDE) to streamline their research process and enhance
the quality of their work.
Scenario
A group of data scientists were tasked with developing complex machine learning
models to analyse vast datasets for a critical research project. The team faced
challenges in managing code, data, and computational resources efficiently, with
their workflow involving coding, data analysis, and version control.
To address these challenges, the team opted for JupyterLab, a web-based IDE
specifically tailored for scientific computing and data science workflows. JupyterLab
offers an integrated environment that encompasses text editors, terminals, data file
viewers, and other tools essential for data analysis and scientific computing.
With JupyterLab, the team could centralise all their tools and resources in one place.
They were able to write code, perform data analysis, and manage version control, all
within a single platform. The following features of JupyterLab were particularly
beneficial:
1. Interactive Computing
The team could create and share documents that contained live code,
equations, visualizations, and narrative text, fostering collaborative analysis
and data exploration.
2. Extensibility
JupyterLab offered the team the flexibility to customize and extend their
environment with additional tools and features, aligning with the specific
requirements of their project.
3. Integrated Tools for Data Visualisation
The IDE provided powerful tools for data visualisation, enabling the team to
generate insightful graphs and charts that facilitated data interpretation and
reporting.
4. Version Control
JupyterLab's integration with version control systems allowed the team to
manage changes to their codebase efficiently, tracking revisions and
maintaining a history of modifications.
Outcome
Moreover, the platform's user-friendly interface and powerful tools for data analysis
and visualisation helped the team to glean more insightful findings from their data,
contributing to the success of their research project.
1. Stand-alone debugging tools can offer specialised features not found in IDEs,
making them useful for targeted debugging scenarios.
2. Features like setting breakpoints and stepping into/over functions are
essential for thorough debugging.
3. Some stand-alone debugging tools work across multiple platforms and
languages, offering more flexibility.
Learner Guide
The Learner Guide provides the core knowledge for this topic. Work through the
materials and be prepared to discuss these in your forums, tutorials or classes.
Expand All
Panels
Collapse All
Panels
These are specialised tools designed to function independently from an IDE, offering
functionalities like code inspection, breakpoint management, and sometimes reverse
engineering capabilities. These tools are versatile and can provide deep analysis
and insights into your code’s behavior, often more than what's possible with an
integrated debugger.
1. GDB
2. Links to an external site.
3. (GNU Debugger)
A popular open-source debugger, accommodating numerous languages
including C and C++.
4. WinDbg
5. Links to an external site.
6.
A comprehensive debugger for Windows, facilitating both kernel-mode and
user-mode debugging.
7. Fiddler
8. Links to an external site.
9.
Predominantly used to debug HTTP transactions.
10. Wireshark
11. Links to an external site.
12.
A tool focused on network debugging, it captures and analyses network
packets.
13. Valgrind
14. Links to an external site.
15.
This tool is significant for memory debugging, leak detection, and profiling.
1. Installation
Download and set up the tool compatible with your OS and programming
language.
2. Code Configuration
Adjust your code to be debug-ready, typically by including debugging
information during compilation.
3. Launching the Debugger
Load your code into the opened debugger.
4. Setting Breakpoints
Establish points in the code where you wish to pause for inspection.
5. Start Debugging
Execute the debugger, which will run your code until a breakpoint is reached,
facilitating variable, memory, and CPU registers inspection.
6. Step Through Code
Further execute your code step-by-step to comprehend its behaviour in
various scenarios.
Further Reading
Reference: Sonar staff, n.d. 'debugging: basic beginner's guide', Sonar, accessed 31 January 2024,
<https://www.sonarsource.com/learn/debugging/>
Section 3: Case Study - Debugging Memory Leaks in a Web Application
Background
Scenario
Despite having used the debugging functionalities integrated into their preferred IDE,
the team has struggled to pinpoint the source of the memory leak. The intermittent
nature of the issue and the vast codebase were making it increasingly difficult to
locate the exact point where the leak was occurring. The clock was ticking, and the
pressure to resolve the issue promptly was mounting.
Decision to Use Valgrind
Realising that a more specialised approach was needed, the team decided to utilise
Valgrind, a renowned stand-alone debugging tool. Valgrind is well-regarded in the
industry for its ability to provide detailed insights into memory usage and potential
leaks, often outperforming integrated debugging tools in this aspect.
The team initiated the process by configuring their codebase to include detailed
debugging information, making it compatible with Valgrind's analysis tools. After
setting up Valgrind, they ran the application through various scenarios, with Valgrind
monitoring the memory allocations and deallocations happening in the background.
After a series of rigorous sessions with Valgrind, the team successfully identified the
issue: a complex data structure was not being cleared properly, leading to gradual
memory buildup over time. Armed with this detailed information, they were able to
address the issue at its root, optimizing the code to prevent memory leaks and
improving the overall stability of the application.
Now, the application ran smoother, with significantly reduced chances of crashes due
to memory leaks. The team also documented their experience with Valgrind, creating
a guideline for leveraging stand-alone debugging tools for future projects, fostering a
culture of continuous learning and adaptation.
Topic 3. Tracing Code Execution
1. Tracing techniques such as assert, print, and stop provide real-time insights
into how the code is being executed.
2. Tracing is invaluable for quickly identifying the origins of errors or unexpected
behaviours in code.
3. Some tracing methods allow for conditional monitoring, executing only when
specific conditions are met, thus providing focused debugging.
Learner Guide
The Learner Guide provides the core knowledge for this topic. Work through the
materials and be prepared to discuss these in your forums, tutorials or classes.
Expand All
Panels
Collapse All
Panels
● Manual Tracing
Involves going through the code line-by-line manually, keeping track of
variables and flow of execution.
● Print Statements
Inserting print statements at key points to display the current state or value
of variables.
● Built-in Debuggers in IDEs
These allow you to pause execution, step through code one line at a time,
inspect the state, and even change variables in mid-execution.
● Profiling Tools
Specialised tools that offer in-depth statistics about resource consumption
of functions.
● Tracing Software
Designed to track the execution of code and report back in various
formats.
In this section, we delve deeper into each method of tracing code execution,
providing a comprehensive guide on how to use them effectively, alongside their
advantages and disadvantages.
Manual Tracing
Advantages
It’s a straightforward method that doesn’t require any additional tools, providing a
raw understanding of the code's flow.
Disadvantages
Can be quite time-consuming and prone to human error, especially for complex code
with numerous lines and functions.
Print Statements
This technique involves inserting print statements at key points in the code to
monitor the state or value of variables as the program runs.
How to Use
Depending on the programming language, use functions like print() in Python or
System.out.println() in Java to insert messages that will display the current
state at various stages in the code.
Advantages
It offers a quick way to monitor the code’s behaviour in real-time.
Disadvantages
Can clutter the code, making it less readable, and may not provide in-depth
information for complex debugging scenarios.
How to Use
Configure the debugger in your IDE and set breakpoints at critical points in the code.
Once the debugger is running, you can inspect variables and the flow of execution
as it hits these breakpoints. Additionally, you have the flexibility to modify variables
during runtime to test different scenarios.
Advantages
These debuggers are powerful, offering extensive features that facilitate a deep
analysis of code behaviour.
Disadvantages
May have a steep learning curve, particularly for beginners, and integrating them into
the workflow can sometimes be complex.
Profiling Tools
Profiling tools are specialised utilities that provide detailed statistics on the resource
consumption of various functions in your code, helping to identify bottlenecks and
optimize performance.
How to Use
Run these tools alongside your code. They generate a report outlining aspects like
time-consuming functions and memory usage, offering insights into the areas where
optimisation is necessary.
Advantages
These are excellent resources for optimising code, providing deep insights into the
performance characteristics of your application.
Disadvantages
While great for performance optimisation, they might not be the best tools for
identifying logic errors or understanding complex algorithmic flows.
Tracing Software
Tracing software is designed to meticulously track the execution of code and report
the findings in different formats, offering real-time insights into the program’s
behaviour.
How to Use
Integrate the software into your development environment and run your code through
it. These tools provide a detailed trace of the execution, offering insights that can be
invaluable in debugging complex issues.
Advantages
Can provide real-time insights and comprehensive data about code execution,
facilitating deep analysis and understanding of code behaviour.
Disadvantages
May be an overkill for simple projects and can sometimes introduce a performance
overhead.
Further Reading
Reference: Josh Weinstein 2020, 'Tracing Code in C++', Medium, accessed 31 January 2024,
<https://medium.com/swlh/tracing-code-in-c-fd9470e3bf5>
Faced with a recurrent problem where items would mysteriously vanish from
customers' shopping carts, causing annoyance and a potential drop in sales, the
development team needed to pinpoint the underlying issue. The problem seemed
random and elusive, making the traditional approaches ineffective in diagnosing the
root cause.
Gathering Data
After numerous debugging sessions, the team unearthed the core of the problem: a
malfunction within the updateCart() function. This function, meant to adjust the
shopping cart's status when an item's quantity was altered, harboured a bug that
removed items under specific conditions - notably, when a customer had more than
five items in the cart and chose to modify the quantity of any item.
Implementing the Solution
Having identified the problem, the team embarked on a mission to rectify it:
Outcome
The rigorous approach and the synergistic use of multiple debugging methods
yielded results, and the disappearing items issue was finally resolved. The proactive
response not only ensured a smoother user experience but also potentially saved
the business from significant revenue losses. This episode fortified the team's
confidence in the power of detailed code tracing and showcased how a multifaceted
approach could effectively untangle even the most perplexing issues in software
development.
Further Resources
The resources in this section are for further exploration, providing additional
information for deeper insights.
Expand All
Panels
Collapse All
Panels
Reading: C/C++ Debugging Tools
This article provides an overview of various debugging tools available for C/C++
applications. It covers different aspects of debugging, starting with the importance of
automated testing, including Test-Driven Development and unit test frameworks like
QTestLib, Google Test, and Catch.
Reference: David Faure, 2022, 'C/C++ Debugging Tools', KDAB [online] url:
https://www.kdab.com/c-cpp-debugging-tools/ [Accessed 31 Jan. 2024]
Reference: Anastasia Kazakova & Greg Law, 2021, 'Top Tips for Debugging C++', Jet Brains [online]
url: https://blog.jetbrains.com/clion/2021/06/7-top-tips-for-debugging-cpp/ [Accessed 31 Jan. 2024]
In this talk, experienced developer Sebastian Theophil takes you through the entire
debugging process, starting with a program that crashes. What do we do next?
Which questions do we have to ask? What information do we need? What can we do
to find the cause of the crash? Which tools can help us in this quest, and, last but not
least, what can we do to make sure this bug never happens again?
(44:22)
Reference: Sebastian Theophil, 2023, 'A Guide to Debugging C++ Code: Practical and Interactive
Examples', YouTube [online video] url: https://youtu.be/Rc2hFf1HVow [Accessed 19 Jan, 2024]
Reference: Mike Shah, 2022, 'Back to Basics: Debugging in C++', YouTube [online video] url:
https://youtu.be/YzIBwqWC6EM [Accessed 19 Jan. 2024]
This video explains what the GDB debugger can do, walking you through executing
the GDB tool step-by-step to demonstrate the most common commands to add
breakpoints, watch variables, and pause/resume the execution of C++ code.
GDB Debugging
(18:07)
Reference: pikuma, 2019, 'GDB Debugging: How to Debug a C/C++ program', YouTube [online video]
url: https://youtu.be/gFCQ37jVN3g [Accessed 19 Jan. 2024]
Reference: Joseph Sibony, 2022, 'A Step-by-step Crash Course in C++ Debugging without IDE –
Empowering the terminal by using GDB', Incredibuild blog [online] url:
https://www.incredibuild.com/blog/a-step-by-step-crash-course-in-c-debugging-without-ide [Accessed
31 Jan. 2024]