AVR - invalid address used when evaluating a variable in gdb
Original Reporter info from Mantis: ccrause @ccrause
-
Reporter name: Christo Crause
Original Reporter info from Mantis: ccrause @ccrause
- Reporter name: Christo Crause
Description:
When debugging an AVR program in Lazarus I noticed incorrect variable values being displayed. This appears to be caused by gdb requesting the memory content of the wrong address space (flash instead of SRAM).
I've mentioned this on the gdb forum, where the conclusion was that it is probably related to incorrect symbol/debug information, see this reply: https://sourceware.org/ml/gdb/2018-06/msg00013.html
See additional information below for example gdb session.
Steps to reproduce:
Compile following using:
fpc/3.1.1/compiler/ppcrossavr -Tembedded -Pavr -CpAVR25 -MObjFPC -Scghi -O4 -g -l -vewnhibq
-Filib/avr-embedded -Fu../../library -Fu. -FUlib/avr-embedded -FE. -oblink -Wpattiny45 -g -a
-XPavr- -al -Sm -Si -dF_CPU:=8000000 blink.pp
Code:
program blink;
uses
intrinsics;
const
LEDpin = 1 shl 4;
CS00msk = 1 shl 0;
CS01msk = 1 shl 1;
CS02msk = 1 shl 2;
TOIE0msk =
{$ifdef FPC_MCU_ATTINY45}
1 shl 1;
{$else} // defaults to atmega328p
1 shl 0;
{$endif}
var
countsPerSecond: byte;
LEDport: byte absolute PORTB;
LEDdir: byte absolute DDRB;
i: byte = 1;
procedure Timer0Overflow; alias: 'TIMER0_OVF_ISR'; interrupt;
begin
inc(i);
if i > countsPerSecond then
begin
LEDport := LEDport xor LEDpin;
i := 0;
end;
end;
begin
countsPerSecond := (((F_CPU div 1024) + 128) div 256 div 3); // 3 Hz
LEDdir := LEDdir or LEDpin; // Set pin to output
LEDport := LEDport or LEDpin; // Set LED high
TCCR0A := 0;
TCCR0B := CS02msk or CS00msk; // clock prescaler = 1024
// enable timer1 overflow interrupt
{$ifdef FPC_MCU_ATTINY45}
TIMSK
{$else} // defaults to atmega328p
TIMSK0
{$endif}
:= TOIE0msk;
avr_sei;
while True do;
end.
Additional information:
An example of a gdb session:
~/fpc/fpc-avr/src/examples/blink2 $ ~/gdb/gdb-8.1/gdb/avr-gdb-8.1.0-original
GNU gdb (GDB) 8.1
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later &LtPos;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 "--host=x86_64-pc-linux-gnu --target=avr".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
&LtPos;http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
&LtPos;http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
(gdb) file bin/blink.elf
Reading symbols from bin/blink.elf...done.
(gdb) target remote :2345
Remote debugging using :2345
0x00000000 in _START ()
(gdb) load
Loading section .text, size 0xb2 lma 0x0
Loading section .data, size 0x2 lma 0xb2
Start address 0x0, load size 180
Transfer rate: 505 bytes/sec, 25 bytes/write.
(gdb) set language pascal
(gdb) set debug remote 1
(gdb) print i
Sending packet: $m60,1#30...Ack
Packet received: 20
$1 = 32
(gdb) print @i
$2 = (^Sending packet: $m800060,8#ff...Ack
Packet received: 04000A00F6777DD2
Byte) 0x800060 #4
(gdb) print (@i)^
Sending packet: $m800060,1#f8...Ack
Packet received: 04
$3 = 4
(gdb)
Mantis conversion info:
- Mantis ID: 33914
- Build: 38524
- Version: 3.1.1
- Fixed in version: 3.3.1
- Fixed in revision: 42493 (#ff337256)
- Monitored by: » dioannidis (Dimitrios Chr. Ioannidis)