From 1ae4cb3c509a8625634304c64b5bd3befe11781c Mon Sep 17 00:00:00 2001
From: Erik Strand <erik.strand@cba.mit.edu>
Date: Thu, 12 Nov 2020 16:36:43 -0500
Subject: [PATCH] Add OpenOCD section

---
 gdb/README.md   | 60 +++++++++++++++++++++++++++++++++++++++++++++++--
 gdb/main.cpp    | 21 +++++++----------
 gdb/openocd.cfg |  7 ++++++
 3 files changed, 73 insertions(+), 15 deletions(-)
 create mode 100644 gdb/openocd.cfg

diff --git a/gdb/README.md b/gdb/README.md
index bc275bb..504c45d 100644
--- a/gdb/README.md
+++ b/gdb/README.md
@@ -22,8 +22,64 @@ build it from [source](https://www.gnu.org/software/gdb/current/).
 ## Debugging on Your Computer
 
 Let's try running the example program in this directory. It should tell us which numbers are prime.
-But there seems to be a problem.
+
+```
+10 is not prime
+11 is prime
+12 is not prime
+13 is prime
+14 is not prime
+15 is not prime
+16 is not prime
+17 is prime
+18 is not prime
+19 is prime
+20 is not prime
+21 is not prime
+22 is not prime
+23 is prime
+24 is not prime
+25 is prime
+26 is not prime
+27 is not prime
+28 is not prime
+29 is prime
+```
+
+But wait, what's going on with 25? We've got a bug.
+
+To find it, run `gdb main`. This launches `gdb`.
+
+Before we do anything else, let's set a breakpoint at line 16: `break main.cpp:16`. Now type `run`.
+
+Your program should be paused at line 16. Try `print i`. Then try `continue`, and `print i` again.
+
+Other helpful commands include:
+
+- `list` shows the line of code we're at, and a few before and after
+- `print <variable>` shows value of a variable
+- `break <fileanme:line>` adds a breakpoint
+- `info breakpoints` prints the existing breakpoints
+- `continue` continues until the next break point, exception, etc.
+- `next` steps forward one line, but doesn't step into functions
+- `step` steps forward one line, and does go inside functions
+- `bt` or `backtrace` shows the current thread
+
+See if you can use these to find the bug.
 
 ## Debugging on Your Microcontroller
 
-I'll refrain from
+On a microcontroller, it's much the same. Except we need some extra hardware and software to talk to
+our micrcontroller for us. I'll use Atmel ICE and OpenOCD.
+
+For ARM microcontrollers, first locate the `.elf` file that you built. Connect Atmel ICE to your
+board and computer. Now you should be able to run `openocd`. It needs a config file, like the one
+in this repo. (You may need to change the board.) You can pass this with the option `-f`, or just
+run OpenOCD in the directory that contains the config file. If everything is working, OpenOCD will
+say it is listening for connections.
+
+Now in another terminal window (or tab or whatever) we can launch `gdb`. Run `gdb your_program.elf`.
+To connect to OpenOCD, run `tar ext :3333` (really intuitive, right?). Now we're running GDB just
+like before, but it's controlling your microcontroller. You may need to type `continue`, and it may
+warn you that the program is already running. This is fine. To pause the program at any given
+moment, hit `CTL-c`.
diff --git a/gdb/main.cpp b/gdb/main.cpp
index 33fd700..80b7d55 100644
--- a/gdb/main.cpp
+++ b/gdb/main.cpp
@@ -11,19 +11,14 @@ bool is_prime(int x) {
     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";
+int main() {
+    for (int i = 10; i < 30; ++i) {
+        bool const prime = is_prime(i);
+        if (prime) {
+            std::cout << i << " is prime\n";
+        } else {
+            std::cout << i << " is not prime\n";
+        }
     }
 
     return 0;
diff --git a/gdb/openocd.cfg b/gdb/openocd.cfg
new file mode 100644
index 0000000..be130be
--- /dev/null
+++ b/gdb/openocd.cfg
@@ -0,0 +1,7 @@
+# Atmel-ICE JTAG/SWD in-circuit debugger.
+interface cmsis-dap
+transport select swd
+
+# Chip info
+set CHIPNAME samd51
+source [find target/atsame5x.cfg]
-- 
GitLab