Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
tools
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
pub
hello-world
tools
Commits
1ae4cb3c
Commit
1ae4cb3c
authored
4 years ago
by
Erik Strand
Browse files
Options
Downloads
Patches
Plain Diff
Add OpenOCD section
parent
a341b7c3
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
gdb/README.md
+58
-2
58 additions, 2 deletions
gdb/README.md
gdb/main.cpp
+8
-13
8 additions, 13 deletions
gdb/main.cpp
gdb/openocd.cfg
+7
-0
7 additions, 0 deletions
gdb/openocd.cfg
with
73 additions
and
15 deletions
gdb/README.md
+
58
−
2
View file @
1ae4cb3c
...
...
@@ -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`
.
This diff is collapsed.
Click to expand it.
gdb/main.cpp
+
8
−
13
View file @
1ae4cb3c
...
...
@@ -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
;
...
...
This diff is collapsed.
Click to expand it.
gdb/openocd.cfg
0 → 100644
+
7
−
0
View file @
1ae4cb3c
# Atmel-ICE JTAG/SWD in-circuit debugger.
interface
cmsis-dap
transport
select
swd
# Chip info
set
CHIPNAME
samd51
source
[find target/atsame5x.cfg]
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment