shantanu
Posts: 5
Joined: Tue Oct 30, 2012 4:05 am

Debugging Assembly code using GDB

Wed Dec 19, 2012 5:33 am

Hi everyone,
I tried tutorials of 'Baking Pi'. It was fun doing that :)
http://www.cl.cam.ac.uk/freshers/raspbe ... /ok03.html

I am trying to write programs by my own logic. So could anybody tell me how t use GDB for debugging assembly programs ??

I tried these commands :

Code: Select all

arm-none-eabi-gcc -g 1_led_on.s
arm-none-eabi-gdb a.out
(gdb) list 1,15
(gdb) b 6

(gdb) run
Starting program: /home/student/pi/OSonPi/self-tried/a.out 
Don't know how to run.  Try "help target".
(gdb) step
The program is not being run.
Do I need to use different commands to assembly debugging?

tufty
Posts: 1456
Joined: Sun Sep 11, 2011 2:32 pm

Re: Debugging Assembly code using GDB

Wed Dec 19, 2012 8:23 am

Given the snippet you posted, it appears that you're trying to run/debug your code on your cross-compiling machine. That's not going to work directly; it probably doesn't have the right processor type to run the code, and almost certainly doesn't have the same peripherals.

You have a couple of options:

1 - run the code on the Pi, debugging through a JTAG adaptor. This involves a fair amount of setup, extra hardware, and possibly (depending on the vintage of your Pi) a certain amount of soldering to teensy tiny pins.
2 - run the code within an ARM simulator such as Qemu, and connect to it using gdb. This involves less hardware buggering about, but can be a total pain to get working. It also requires you to have a separate compile target for code running under the simulator, as the boot process and peripherals are not exactly the same (there's no actual "raspberry pi simulator" that I'm aware of).

Simon

shantanu
Posts: 5
Joined: Tue Oct 30, 2012 4:05 am

Re: Debugging Assembly code using GDB

Wed Dec 19, 2012 9:13 am

Thanks Tufty !
I have the u-link JTAG adapter. Is it compatible ? Anyone tried debugging with JTAG yet ??

Also, Qemu setup for windows is very straightforward.
http://sourceforge.net/projects/rpiqemuwindows/

Have not tried setting it up on Linux :(
Any good links on that ?

tufty
Posts: 1456
Joined: Sun Sep 11, 2011 2:32 pm

Re: Debugging Assembly code using GDB

Wed Dec 19, 2012 9:25 am

The problem's not in setting up qemu, which is more or less a simple configure/make/make install sequence IIRC, but setting up your project to run on it. You need (at the least) different addresses for the peripherals which are common (UART, and SP084 timer mainly), and it's not possible to do anything that relates to the GPU, so you're restricted in exactly what you can debug.

If you have a JTAG adaptor, you're presumably relatively proficient, and as long as you have a revision 2 pi, you should be pretty much good to go. If you have a revision 1 pi, you'll want Dave's JTAG-enabling bootloader on an SD card (it changes various pin assignments, getting you relatively easy access to the JTAG pins) from here : https://github.com/dwelch67/raspberrypi ... er/armjtag. I'm fairly sure the ulink is supported by openOCD, so you should be able to use it from Linux.

I'd say JTAG's probably your best bet.

shantanu
Posts: 5
Joined: Tue Oct 30, 2012 4:05 am

Re: Debugging Assembly code using GDB

Wed Dec 19, 2012 10:10 am

Are you sure about ulink?
http://www.raspberrypi.org/phpBB3/viewtopic.php?p=16199
folks are saying it does not support ARM11 :oops:

I also have Stellaris Launchpad with 'In Circuit Debug Interface'.
Can I use its Debug circuitry anyhow ??

If not, what exactly I need to do to get GDB feature working with Qemu ?

tufty
Posts: 1456
Joined: Sun Sep 11, 2011 2:32 pm

Re: Debugging Assembly code using GDB

Wed Dec 19, 2012 10:44 am

shantanu wrote:Are you sure about ulink?
Nope.
shantanu wrote:If not, what exactly I need to do to get GDB feature working with Qemu ?
I have a script to fire up qemu and gdb with serial via a telnet session on port 1235

Code: Select all

#!/bin/bash
make || exit

# Start up qemu in the background
qemu-system-arm -nographic -M versatilepb -cpu arm1136-r2 -m 128 -device sp804 -device pl011 -serial telnet:192.168.1.4:1235,server,nowait -kernel ./bin/kernel.elf -s -S &

# And fire up the debugger
arm-none-eabi-gdb -nx -x "./gdbinit" --tui ./bin/kernel.elf
That's for a stock qemu v0.15.0, there may be later versions or forks that better mimic the Pi hardware.

My gdbinit looks like this (sets up split screen debugging on a remote target):

Code: Select all

layout split
fs cmd
wh cmd 12
target remote:1234
It's not 100% reliable, sometimes the qemu process goes walkabout and needs killing manually, if you kill the debugged process you need to restart, and so on. But it works enough that you can debug. Unfortunately, you need to debug really early boot stuff (and, annoyingly enough, interrupt handlers) on the pi via the "umm - guess - edit - recompile - try again" cycle, as the boot sequence on qemu isn't exactly the same as on the actual hardware

shantanu
Posts: 5
Joined: Tue Oct 30, 2012 4:05 am

Re: Debugging Assembly code using GDB

Thu Dec 20, 2012 5:34 am

Thanks !
I was thinking whether I can directly debug on raspbian distro.
So I tried this :

Code: Select all

[email protected] ~/OSonPi/self-tried $ as -g 1_led_on.s 
[email protected] ~/OSonPi/self-tried $ ls
1_led_on.s     3_function     5_flashing_morse_code  template
2_led_blink.s  4_timer_blink  a.out                  test.c
[email protected] ~/OSonPi/self-tried $ sudo chmod a+x a.out 
[email protected] ~/OSonPi/self-tried $ gdb a.out
GNU gdb (GDB) 7.4.1-debian
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "arm-linux-gnueabihf".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/pi/OSonPi/self-tried/a.out...done.
(gdb) b 6
Breakpoint 1 at 0x4: file 1_led_on.s, line 6.
(gdb) run
Starting program: /home/pi/OSonPi/self-tried/a.out 
/bin/bash: /home/pi/OSonPi/self-tried/a.out: cannot execute binary file
/bin/bash: /home/pi/OSonPi/self-tried/a.out: Success
During startup program exited with code 126.
(gdb) 
wts going wrong?
I'll also try using qemu !

shantanu
Posts: 5
Joined: Tue Oct 30, 2012 4:05 am

Re: Debugging Assembly code using GDB

Thu Dec 20, 2012 7:48 am

Or is it because in OS environment, we can't access hardware directly;
It should be through files(device nodes) ?
Is there any way to debug in such a scenario?

bobblestiltskins
Posts: 16
Joined: Wed Aug 29, 2012 11:10 am

Re: Debugging Assembly code using GDB

Wed Jan 09, 2013 2:36 pm

shantanu wrote:Thanks !
I was thinking whether I can directly debug on raspbian distro.
So I tried this :

Code: Select all

[email protected] ~/OSonPi/self-tried $ as -g 1_led_on.s 
[email protected] ~/OSonPi/self-tried $ ls
1_led_on.s     3_function     5_flashing_morse_code  template
2_led_blink.s  4_timer_blink  a.out                  test.c
[email protected] ~/OSonPi/self-tried $ sudo chmod a+x a.out 
[email protected] ~/OSonPi/self-tried $ gdb a.out
GNU gdb (GDB) 7.4.1-debian
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "arm-linux-gnueabihf".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/pi/OSonPi/self-tried/a.out...done.
(gdb) b 6
Breakpoint 1 at 0x4: file 1_led_on.s, line 6.
(gdb) run
Starting program: /home/pi/OSonPi/self-tried/a.out 
/bin/bash: /home/pi/OSonPi/self-tried/a.out: cannot execute binary file
/bin/bash: /home/pi/OSonPi/self-tried/a.out: Success
During startup program exited with code 126.
(gdb) 
wts going wrong?

I'll also try using qemu !
I think that you are missing a step in producing the executable? You are assembling to object code and trying to run that?

Generally you should do something like ....

/usr/bin/as -gstabs -o 1_led_on.o 1_led_on.s # assemble the code
/usr/bin/ld -o 1_led_on 1_led_on.o # link to produce an executable

Then you can run the executable via ./1_led_on or run under gdb via gdb ./1_led_on

Bob

User avatar
DavidS
Posts: 3800
Joined: Thu Dec 15, 2011 6:39 am
Location: USA
Contact: Website

Re: Debugging Assembly code using GDB

Wed Jan 09, 2013 2:50 pm

shantanu wrote:Or is it because in OS environment, we can't access hardware directly;
It should be through files(device nodes) ?
Is there any way to debug in such a scenario?
If your code is attempting to directly access the Hardware I believe that Linux will protect against this. If you ned a enviroment to test and build on, under RISC OS you can directly access the Hardware. I do not think that there is a port of GDB and I am not sure which RISC OS Debugers are good for the ARMv6 as I just got my RPi. There are plenty of free debuggers out there for RISC OS if you search a little I am sure that you will find one that supports the AMv6 and suits your needs and style.
RPi = Way for me to have fun and save power.
100% Off Grid.
Household TTL Electricity Usage = 1.4KW/h per day.
500W Solar System, produces 2.8KW/h per day average.

bobblestiltskins
Posts: 16
Joined: Wed Aug 29, 2012 11:10 am

Re: Debugging Assembly code using GDB

Wed Jan 09, 2013 3:03 pm

bobblestiltskins wrote:
shantanu wrote:Thanks !
I was thinking whether I can directly debug on raspbian distro.
So I tried this :

Code: Select all

[email protected] ~/OSonPi/self-tried $ as -g 1_led_on.s 
[email protected] ~/OSonPi/self-tried $ ls
1_led_on.s     3_function     5_flashing_morse_code  template
2_led_blink.s  4_timer_blink  a.out                  test.c
[email protected] ~/OSonPi/self-tried $ sudo chmod a+x a.out 
[email protected] ~/OSonPi/self-tried $ gdb a.out
GNU gdb (GDB) 7.4.1-debian
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "arm-linux-gnueabihf".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/pi/OSonPi/self-tried/a.out...done.
(gdb) b 6
Breakpoint 1 at 0x4: file 1_led_on.s, line 6.
(gdb) run
Starting program: /home/pi/OSonPi/self-tried/a.out 
/bin/bash: /home/pi/OSonPi/self-tried/a.out: cannot execute binary file
/bin/bash: /home/pi/OSonPi/self-tried/a.out: Success
During startup program exited with code 126.
(gdb) 
wts going wrong?

I'll also try using qemu !
I think that you are missing a step in producing the executable? You are assembling to object code and trying to run that?

Generally you should do something like ....

/usr/bin/as -gstabs -o 1_led_on.o 1_led_on.s # assemble the code
/usr/bin/ld -o 1_led_on 1_led_on.o # link to produce an executable

Then you can run the executable via ./1_led_on or run under gdb via gdb ./1_led_on

Bob
If you use gcc to assemble, you can produce the executable in one step via :

gcc -g -o 1_led_on 1_led_on.s

Bob

Return to “Bare metal, Assembly language”