Lecture 4 - Debugging & Development Tools Flashcards
Memory stores data, but what else?
Program code and instructions.
Is it possible to have a pointer to a function, if so what is it callled.
yes , function pointer
What is the type of a function pointer?
return_type (*)(argument_types)
When could having a function pointer be useful?
Having different print functions for node of a binary search tree.
Are function names automatically converted to pointers?
YES
What is a syntactical bug?
A grammar bug, it is something picked up by the compiler e.g. mispelling a keyword
What is a semantic bug?
Error in our logic , not flagged by the compiler, but the code doesn’t do what we want it to do .
What is a debugger?
A controlled enviroment in which we can run our program and investigate it’s execution.
What is static analysis?
When we reason about a program’s behaviour without running it.
What is dynamic analysis?
Adding instructions to program in order to be able to detect bugs at runtime.
What are the 2 most popular debuggers for C?
lldb and gdb
How to we need to compile a C program in order to be able to debug it?
With a g flag, this adds debug information to the binary exectuable
After compiling with a g flag do we simply run the program normally to debug ?
No, we start the debugger and load the program executable
EG.
gdb –argc ./program arg1 arg2
lldb – ./program arg1 arg2
How to exit a debugger?
type quit
How to set a breakpoint in the debugger?
b followed by the line number
How to execute the next line in the debugger?
n
What is a seg fault?
An error raised by the hardware notigying the OS that our program has attempted to access a restricted area of memory
-> os immediately terminates program execution
Common Seg Fault Causes?
- dangling pointers
- dereferencing a NULL pointer
- Writing to read only memory
- Buffer overflow
- stack/heap overflow
What is a dangling pointer?
A pointer that doesn’t point to a valid object of an appropriate type. E.g. deallocated memory
What does the bt command do in the debugger?
Shows the backtrace after an error. This shows program calls leading to error.
Does the compiler do some static analyses every time we compile?
YES
What is good practice when compiling C programs?
enabling -Wall -Werror
How to we invoke the static analyzer
clang –analyze –ana;yzer-output html program.c
- we have a flag
- we have an output format
What is a massive drawback of static analysis?
Quite expensive to perform, not worth to do in every build.
What does the analyzer report contain?
Potential bugs and their explanations
What is clang-tidy?
It is a linter tool, that allows us to enfodce coding guidelines and modernize source code.
How is clang tidy turned on?
Including – somewhere in our command
What are the 5 most important sanitizers?
- AddressSanitizer
- MemorySanitizer
- LeakSanitizer
- UndefinedBehaviourSanitizer
- ThreadSanitizer
What is the address sanitizer?
A memory error detector
-> out of bounds access
-> use after free
-> double free
What is a memory sanitizer?
a detector of uninitialized reads
What is a leak sanitizer?
a memory leak detector
What is a undefined behaviour sanitizer?
detector of undefined behaviour
What is a thread sanitizer?
a data race detector
What is undefined behaviour?
These are semantic bugs (fault in our logic). These are due to the fact the compiler doesn’t enforce a certain behaviour (e.g. dereferencing a NULL pointer).
e.g integer overflow
Are C and C++ considered safe languages?
No
What does it mean to be an unsafe language ?
executing an erroneous operation causes the entire program to be meaningless, as opposed to just the erroneous operation having an unpredictable result.