Skip to content
Snippets Groups Projects
Commit 12b118fa authored by Erik Strand's avatar Erik Strand
Browse files

Add initial gdb content

parent eeb84fbb
No related branches found
No related tags found
No related merge requests found
main
main.dSYM
# 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
# 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
#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;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment