# debugging

***

**1. Using `assert()` Macro:**

```cpp
assert(x > 0);  // Check if x is greater than 0, else terminate with an assertion error message
```

**2. Using `std::exception`:**

```cpp
try {
    // Code that may throw an exception
} catch (const std::exception& e) {
    std::cerr << "Exception: " << e.what() << std::endl;
}
```

**3. Using `std::cerr` for Printing Error Messages:**

```cpp
std::cerr << "Error: " << errorMessage << std::endl;
```

**4. Using `std::cout` for Printing Debug Information:**

```cpp
std::cout << "Debug: " << debugMessage << std::endl;
```

**5. Using `std::ofstream` for Logging Debug Information:**

```cpp
std::ofstream logFile("debug.log");
logFile << "Debug: " << debugMessage << std::endl;
```

**6. Using GDB (GNU Debugger):**

```cpp
// Set breakpoints at specific lines using `gdb --args ./a.out`
(gdb) break 15
```

**7. Using LLDB (Low Level Debugger):**

```cpp
// Set breakpoints at specific lines using `lldb`
(lldb) break set -f sourcefile.cpp -l 15
```

**8. Using Visual Studio Debugger:**

```cpp
// Set breakpoints in Visual Studio by right-clicking in the code editor and selecting "Breakpoint"
```

**9. Using `valgrind` for Memory Debugging:**

```cpp
// Check for memory leaks and other memory errors using `valgrind --leak-check=full ./a.out`
```

**10. Using `asan` (Address Sanitizer):**

```cpp
// Check for memory errors using `gcc -fsanitize=address ./a.out`
```

**11. Using `msan` (Memory Sanitizer):**

```cpp
// Check for memory errors using `gcc -fsanitize=memory ./a.out`
```

**12. Using `ubsan` (Undefined Behavior Sanitizer):**

```cpp
// Check for undefined behavior using `gcc -fsanitize=undefined ./a.out`
```

**13. Using `tsan` (Thread Sanitizer):**

```cpp
// Check for thread safety violations using `gcc -fsanitize=thread ./a.out`
```

**14. Using `drd` (Data Race Detector):**

```cpp
// Check for data races using `gcc -fdrd ./a.out`
```

**15. Using `gdb` with `bt` Command for Backtrace:**

```cpp
(gdb) bt  // Print a backtrace of the function call stack
```

**16. Using `gdb` with `p` Command for Printing Variables:**

```cpp
(gdb) p x  // Print the value of variable x
```

**17. Using `gdb` with `watch` Command for Watching Variables:**

```cpp
(gdb) watch x  // Set a watchpoint on variable x
```

**18. Using `gdb` with `disassemble` Command for Disassembling Instructions:**

```cpp
(gdb) disassemble main  // Disassemble the main function
```

**19. Using `gdb` with `stepi` Command for Stepping into Functions:**

```cpp
(gdb) stepi  // Step into the next function call
```

**20. Using `gdb` with `nexti` Command for Stepping over Function Calls:**

```cpp
(gdb) nexti  // Step over the next function call
```

**21. Using `gdb` with `finish` Command for Running until Function Returns:**

```cpp
(gdb) finish  // Run until the current function returns
```

**22. Using `gdb` with `continue` Command for Continuing Execution:**

```cpp
(gdb) continue  // Continue execution after hitting a breakpoint
```

**23. Using `gdb` with `list` Command for Listing Source Code:**

```cpp
(gdb) list  // List source code around the current line
```

**24. Using `gdb` with `info` Command for Getting Information:**

```cpp
(gdb) info breakpoints  // List all breakpoints
```

**25. Using `gdb` with `r` Command for Running the Program:**

```cpp
(gdb) r  // Run the program from the beginning
```

**26. Using `gdb` with `b` Command for Setting Breakpoints:**

```cpp
(gdb) b main  // Set a breakpoint at the start of the main function
```

**27. Using `gdb` with `tbreak` Command for Setting Temporary Breakpoints:**

```cpp
(gdb) tbreak main  // Set a temporary breakpoint at the start of the main function
```

**28. Using `gdb` with `n` Command for Stepping to the Next Statement:**

```cpp
(gdb) n  // Step to the next statement
```

**29. Using `gdb` with `s` Command for Stepping into Functions:**

```cpp
(gdb) s  // Step into the next function
```

**30. Using `gdb` with `until` Command for Stepping until a Given Line:**

```cpp
(gdb) until 100  // Step until line 100 is reached
```

**31. Using `gdb` with `watch` Command for Watching Variables:**

```cpp
(gdb) watch x  // Watch the value of variable x
```

**32. Using `gdb` with `print` Command for Printing Values:**

```cpp
(gdb) print x  // Print the value of variable x
```

**33. Using `gdb` with `display` Command for Displaying Values:**

```cpp
(gdb) display x  // Display the value of variable x every time it changes
```

**34. Using `gdb` with `set` Command for Setting Variables:**

```cpp
(gdb) set x = 10  // Set the value of variable x to 10
```

**35. Using `gdb` with `x` Command for Examining Memory:**

```cpp
(gdb) x/10x &x  // Examine 10 words of memory starting at the address of variable x
```

**36. Using `gdb` with `bt` Command for Backtracing the Call Stack:**

```cpp
(gdb) bt  // Print a backtrace of the call stack
```

**37. Using `gdb` with `info` Command for Getting Information:**

```cpp
(gdb) info locals  // Print the values of local variables
```

**38. Using `gdb` with `source` Command for Reading Source Code:**

```cpp
(gdb) source main.c  // Read the source code of the main.c file
```

**39. Using `gdb` with `help` Command for Getting Help:**

```cpp
(gdb) help bt  // Get help on the bt command
```

**40. Using `gdb` with `quit` Command for Quitting the Debugger:**

```cpp
(gdb) quit  // Quit the gdb debugger
```

**41. Using `printf()` for Simple Debugging:**

```cpp
printf("Debug: x = %d\n", x);  // Print the value of variable x for debugging purposes
```

**42. Using `cout` for Debugging in C++:**

```cpp
std::cout << "Debug: x = " << x << std::endl;  // Print the value of variable x for debugging purposes
```

**43. Using `cerr` for Error Handling:**

```cpp
std::cerr << "Error: " << errorMessage << std::endl;  // Print an error message to the standard error stream
```

**44. Using `assert()` for Assertions:**

```cpp
assert(x > 0);  // Check if x is greater than 0, otherwise terminate the program with an assertion error
```

**45. Using `throw` for Throwing Exceptions:**

```cpp
throw std::runtime_error("Error: Invalid argument");  // Throw an exception with an error message
```

**46. Using `try-catch` for Catching Exceptions:**

```cpp
try {
    // Code that may throw an exception
} catch (const std::exception& e) {
    std::cerr << "Error: " << e.what() << std::endl;  // Handle the exception
}
```

**47. Using `debugger` Keyword for Entering Debugger:**

```cpp
debugger;  // Enter the debugger at runtime
```

**48. Using `_CrtSetBreakAlloc()` for Detecting Memory Leaks:**

```cpp
_CrtSetBreakAlloc(123);  // Set a breakpoint on the 123rd memory allocation
```

**49. Using `_CrtDumpMemoryLeaks()` for Dumping Memory Leaks:**

```cpp
_CrtDumpMemoryLeaks();  // Dump any memory leaks in the program
```

**50. Using a Custom Debugger:**

```cpp
class Debugger {
public:
    void log(const std::string& message) {
        std::cerr << "Debug: " << message << std::endl;
    }
};

Debugger debugger;  // Create a debugger object
debugger.log("x = " + std::to_string(x));  // Log the value of variable x for debugging purposes
```
