«

»

Mar 04

Robotics 101 : Using a .gdbinit

In this course you will learn some of gdb uses : creating your own functions/macros and using it to check if your code does what it is supposed to do.
Gdb is a very powerful tool, well mastered it can make a huge difference between a regular programmer and a good one.

Using the .gdbinit file

In order to create your macros and others you need to create .gdbinit file in the folder where you launch gdb from.

Creating macros

You may have noticed that we always use the same 3 instruction when launching gdb, you may also have cursed the moron who coded gdb without thinking of this problem. Hopefully this guy is not a moron, he is even quite clever as you have the possibility to create your own macros and function, thus allowing you to only type a single instruction.
In order to define a macro you have to use the following syntax:


define macro_name
first_instruction
...
...
...
last_instruction
end

Here are some useful macros :


define ri
mon reset init
end
define rc
ri
continue
end
define rl
ri
load
rc
end
define recon
target extended-remote localhost:3333
end

Automatically launching functions when starting gdb

This one is quite easy, simply add at the end of the file (after the definition of the functions you are going to use is actually enough, but it is considered a good practice to write them at the end of the file) the instructions you want to execute (as if you wrote them in a gdb prompt)

ex:

a lot of define
...
...

target extend-remote localhost:3333
mon reset init
load

This would allow you to automatically connect to openocd, reset the board and load your code when you launch gdb.

recon

Last tricks

Using:
print VARIABLE_NAME
Allows you to print the current value of your variable, if you haven’t written a way to communicate with your board yet, it may help you. It can also be shortened to:
p VARIABLE_NAME

Gdb is also quite clever and is able to guess the commands you meant if you only type in their beginnings. An example will be clearer I guess:
tar ext :3333
will be understood as
target extended-remote localhost:3333

As the only command starting by tar is target, the only thing starting by ext is extended-remote, and so on …

Hope this will help you to spare some time while debugging 😉

Felix