Ultimate GDB Guide
Ultimate GDB Guide
6] Continue commands tells gdb to continue execution of program until until next
breakpoint.
continue
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++
3] Structure padding:
-> Compiler didn't read one byte at a time it read one word, size of one word
in 32bit processor is 4 bytes and in 64bit processor it is 8 bytes.
-> struct size is multiple of largest data type in struct.
-> #pragma pack(1) // this will tell compiler to read one byte at a time. this
avoid padding
e.g.
typedef struct {
char ch;
int x;
char name[10];
double y;
} Point;
Memory layout
Offset: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
| ch | PADDING | x (int) | name[10]
| PADDING | y (double) |
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
|1byte| 3 bytes | 4 bytes | 10 bytes
| 6 bytes | 8 bytes |
//
===================================================================================
===================================================================================
===========================
2] Valgrid
-> Valgrind is a tool for debugging and profiling programs written in C and C+
+. It can detect many memory-related errors, such as memory leaks, invalid memory
accesses, and double frees.
-> how to use it
valgrind ./executable_file
3] Assert
-> assert(condition, message)
-> if condition is false then it will print message and exit the program
4] Segmentation fault
-> Segmentation fault is a type of error that occurs when a program attempts to
access a memory location that is not allocated to it. This can happen for a variety
of reasons, such as trying to access a memory location that is out of bounds, or
trying to access a memory location that has been freed.
6] Heap Corruption
Heap corruption occurs when a program modifies the memory allocated on the heap
beyond its allocated boundaries. This can lead to unpredictable behavior, crashes,
or security vulnerabilities.
examples:
1] Buffer overflow:
char* buffer = malloc(10);
strcpy(buffer, "This is a long string that overflows the buffer");
7] Conditional Breakpoint
e.g.
1] break file_no:line_no if (x>1000)
//
===================================================================================
===================================================================================
=====================
//Doing nothing