Skip to content
Snippets Groups Projects
Commit 09e21b02 authored by Dominik Zobel's avatar Dominik Zobel
Browse files

Draft for debugging strategies lecture

parent 3296bfdd
No related branches found
No related tags found
1 merge request!56Debugging Strategies lecture notes
Pipeline #66620 passed
......@@ -31,7 +31,7 @@ website:
- "lectures/programming-paradigms/slides.qmd"
- "lectures/data-structures/slides.qmd"
- "lectures/complexity/slides.qmd"
# - "lectures/debugging/slides.qmd"
- "lectures/debugging-strategies/slides.qmd"
# - "lectures/good-scientific-practice/slides.qmd"
# - "lectures/user-experience/slides.qmd"
# - "lectures/testing/slides.qmd"
......@@ -47,7 +47,7 @@ website:
- "exercises/programming_paradigms.qmd"
- "exercises/data_structures.qmd"
- "exercises/complexity.qmd"
# - "exercises/debugging.qmd"
# - "exercises/debugging-strategies.qmd"
# - "exercises/good_scientific_practice.qmd"
# - "exercises/user_experience.qmd"
# - "exercises/testing.qmd"
......
---
title: "Debugging Strategies"
author: "René Redler and Dominik Zobel"
---
# Debugging Strategies
**Testing:** Trying to break programs to make them more robust
**Debugging:** Trying to fix a broken program
## Issue, Bug, ...
Discussion of code behaviour
# Different kind of errors
One possible distinction:
- Compile-time errors
- Run-time errors
## Compile-time errors {.leftalign}
_Setting compiler flags for compiled languages like Fortran and C_
Check the manuals
- [GCC manuals](https://gcc.gnu.org/onlinedocs/)
- [Intel Fortran compiler options](https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/2024-1/compiler-options-001.html)
Also gcc compile time checks and options like `-Wall` and `-Wextra`
## Fixing compile-time errors
- Learn to understand what the compiler tries to tell you
## Run-time errors (1/2) {.leftalign}
_Setting compiler flags for compiled languages like Fortran and C_
Typically using `-g` for Intel, GCC and many other compilers
In the Fortran world, also
- gfortran: `-fbacktrace`, `-fsanitize=bounds-strict`
- intel (ifort): `-traceback`, `-check bounds`, `-check all`
## Run-time errors (2/2) {.leftalign}
For Fortran programs try with more than just one compiler.
HPC systems usally provide native compiler plus gcc/gfortan. Try both variants.
$\Rightarrow$_Example program with issue illustrating the compiler flags_
$\Rightarrow$_Show use of `gdb`_
## Fixing run-time errors
- Produce sensible debug messages to determine code area with the issue
- Use proper compiler flags for debug output
- Check prerequisites/environment
- Use debugger
## Locating the right error message (1/2) {.leftalign}
```Python
def _extend_number(num):
return 10*num + (num % 10) -1
def extend_number(num):
try:
for idx in range(num):
num = _extend_number(num)
except:
num = extend_number(num)
return num
print(extend_number('6'))
```
:::{.smaller}
Expected output:
:::
```
6543210
```
## Locating the right error message (2/2)
Output from previous code (last 15 lines)
```
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 3, in extend_number
RecursionError: maximum recursion depth exceeded while calling a Python object
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 6, in extend_number
File "<stdin>", line 6, in extend_number
File "<stdin>", line 6, in extend_number
[Previous line repeated 996 more times]
RecursionError: maximum recursion depth exceeded
```
<!--
issues in line 9 and 13
-->
## Hands-On! {.handson}
1. What are the actual issues in the code from the previous slides?
2. How to rectify them?
## General strategies
- Search online for this or similar issues
- Add output verbosity, especially around where the error occured
- Narrow down the code where the error occured (divide and conquer)
- Use a debugger
- Did it work before? What changed since?
# Debugging broken code
## Using debuggers {.leftalign}
Idea of debuggers:
- Investigate code before/at the issue interactively
- Set breakpoints to check actual values at specific code positions
Some debuggers:
- `pdb` (Python debugger)
- `gdb` (GNU debugger)
- commercial debuggers (like `ddt` on Levante)
## gdb {.leftalign}
Using gdb to inspect a core dump
- to get a core dump
```bash
limit -c unlimited
a.out
```
- to inspect the `core` dump
```bash
gdb a.out core
```
# How to make debugging easier
## Logging
Consider using logging mechanisms
- Very minimal logger for Python
```Python
import logging
logging.basicConfig(filename='output.log', level=logging.WARNING, datefmt='%H:%M:%S',
format='[%(asctime)s] %(levelname)-8s in %(pathname)s:%(lineno)d %(message)s')
logging.warning('Example warning message')
logging.error('Example error message')
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment