C2110 UNIX and programming Lesson 8 / Module 2 -1C2110 UNIX and programming Petr Kulhanek kulhanek@chemi.muni.cz National Center for Biomolecular Research, Faculty of Science Masaryk University, Kamenice 5, CZ-62500 Brno PS / 2020 Distance form of teaching: Rev2 Lesson 8 / Module 2 C2110 UNIX and programming Lesson 8 / Module 2 -2- Troubleshooting ➢ Syntax errors ➢ Logical errors C2110 UNIX and programming Lesson 8 / Module 2 -3Syntax Erros Syntax errors • writing errors that do not match the language specification • program cannot be translated, place of error is printed by compiler (e.g., C/C++, Fortran) • script cannot be run at all (e.g. ,javascript) • when interpreter encounters an incomprehensible notation during the run of the script, the run is terminated and the line with the error is written to the error output, the error may manifest only under specific run conditions (e.g., bash, awk, gnuplot) Attention! Beginning of the error may be at a different location than indicated by the error message.Troubleshooting: • syntax highlighting • static code analyzers C2110 UNIX and programming Lesson 8 / Module 2 -4bash - Syntax Check 1. The basic syntax check can be performed visually in a text editor, which allows highlighting syntax of programming language. 2. Error message: $ bash test.sh Zadejte pocet: 5 test.sh: line 10: unexpected EOF while looking for matching `"' test.sh: line 16: syntax error: unexpected end of file unterminated string (all in red) turn on display of line numbers C2110 UNIX and programming Lesson 8 / Module 2 -5bash - Static Analysis 3. Static code analyzer: • online • command line https://www.shellcheck.net/ C2110 UNIX and programming Lesson 8 / Module 2 -6Logical Errors Logical errors • program/script can be run, but the result does not meet expectations or is not reproducible • these errors are VERY hard to find/fix - it is advisable to avoid them by thorough design of the algorithm Causes of logical errors: • bad algorithm design (all languages) • work with uninitialized variables (all languages) • work with unallocated/freed memory, writing to unallocated memory (C/C++, Fortran) • concurrence (race condition) for parallel tasks (OpenMP, MPI, threads) Troubleshooting: • dynamic run analyzers (C/C++, Fortran: valgrind) • debugging the program run using a debugger C2110 UNIX and programming Lesson 8 / Module 2 -7debugger - bashdb + visual code 1. Starting the editor: $ module add vscode $ code 2. Install the extension (only once): https://marketplace.visualstudio.com/items?itemName=rogalmic.bash-debug 3. Runtime environment configuration (launch.json, only once): { // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "type": "bashdb", "request": "launch", "name": "Bash-Debug", "cwd": "${workspaceFolder}", "program": "${file}", "args": [], "terminalKind": "integrated" }, ] } Practical example of configuration and use. C2110 UNIX and programming Lesson 8 / Module 2 -8debugger - bashdb + visual code Values ​​of variables (operator: $) Debugger Step Over stops (breakpoints) Current position Terminal: input/output C2110 UNIX and programming Lesson 8 / Module 2 -9Exercise I 1. Step through the running scripts from L08.M01.E01.T03. C2110 UNIX and programming Lesson 8 / Module 2 -10- Installation Installation of shellcheck and bashdb to the Ubuntu OS C2110 UNIX and programming Lesson 8 / Module 2 -11Notes to Installation 1. shellcheck is part of standard packages: 2. bashdb can be installed from source code (see L13.M02) or into Ubuntu 18.04 LTS from NCBR package repository, package name: ncbr-bashdb 3. Visual Studio Code can be installed from package (.deb) or run from a binary archive (.tar.gz). See the documentation for instructions. https://code.visualstudio.com/ $ sudo apt-get install shellcheck https://wolf.ncbr.muni.cz/whitezone/packages/public/18.04/ For Infinity users (C2115): bashdb and extension bashdebug require a system command pkill which is redefined in the Infinity environment. Resolve the collision by running unset command before starting the editor. $ unset pkill