diff --git a/gdb/.gitignore b/gdb/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e6f7875b095f8a418192247c0593716050b668f2 --- /dev/null +++ b/gdb/.gitignore @@ -0,0 +1,2 @@ +main +main.dSYM diff --git a/gdb/Makefile b/gdb/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..0790ce2b12c22838fab78dce49217e195aa19dfc --- /dev/null +++ b/gdb/Makefile @@ -0,0 +1,12 @@ +# Change your compiler here if you're not using gcc. +CXX = g++ + +# The flag -g adds debugging symbols to the executable. These are necessary for debugging with gdb. +CFLAGS = -Wall -g + +main: main.cpp + $(CXX) $(CFLAGS) -o $@ $< + +.PHONY: clean +clean: + rm main diff --git a/gdb/README.md b/gdb/README.md new file mode 100644 index 0000000000000000000000000000000000000000..a9b6a61dfcf5f66513e1f34b134bbbbf0b4b9515 --- /dev/null +++ b/gdb/README.md @@ -0,0 +1,26 @@ +# GDB + +[GDB](https://www.gnu.org/software/gdb/) is a program that looks inside other programs. It's a +really handy tool for tracking down hard to find bugs. + +## Setup + +First, GDB is only useful if you have a way to compile programs (usually C or C++, but GDB does +support a variety of other languages as well). So to use it on your computer, you'll probably want +to get set up with [gcc and make](https://gitlab.cba.mit.edu/pub/hello-world/c_cpp_and_make). For +debugging microcontrollers, you'll need a compiler that supports your particular chip (`gcc-avr`, +`arm-none-eabi`, etc.). + +To debug your microcontroller, you'll need some on-chip debugging software. +[OpenOCD](https://gitlab.cba.mit.edu/pub/hello-world/tools/-/tree/master/openocd) is the option I'm +familiar with. + +To install GDB itself, on Linux or Mac you can probably use your system's package manager (`apt` or +`yum` on linux, [`homebrew`](https://brew.sh/) on Mac). If you want complete hacker cred, you could +build it from [source](https://www.gnu.org/software/gdb/current/). + +## Debugging on Your Computer + +## Debugging on Your Microcontroller + +I'll refrain from diff --git a/gdb/main.cpp b/gdb/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fb3a887137c2b7e5326f2e8b3ab7aec57b6a87d0 --- /dev/null +++ b/gdb/main.cpp @@ -0,0 +1,29 @@ +#include <iostream> + +// This is a simple function that checks if numbers are prime. Or does it? There may be a bug... +bool is_prime(int x) { + for (int i = 2; i < x / 2; ++i) { + if (x % i == 0) { + return false; + } + } + return true; +} + +int main(int argc, const char* argv[]) { + // If the user didn't give us a number, exit. + if (argc < 2) { + printf("Please type a number.\n"); + return 0; + } + int x = atoi(argv[1]); + + bool const prime = is_prime(x); + if (prime) { + std::cout << x << " is prime\n"; + } else { + std::cout << x << " is not prime\n"; + } + + return 0; +}