Help With Debugging an AVR Using GDB [SOLVED] - Page 1
Forum section: electronics / general

author: Kalcifer
author_rank: Regular Contributor
timestamp: June 05, 2021, 02:58:50 am
content: I'm trying to debug a program running on an ATtiny84A, and I am having trouble understanding either how to use it, or interpreting its output, or both. For testing purposes, I created a simple program that I have uploaded to the microcontroller which simply sets the value of a test variable `unsigned char test_var=0xAA`, and outputs a LOW to PORTA6. I then put the microcontroller into debug mode, and started debugging with GDB.First I wanted to print the value of the test variable, so I typed `print test_var`, which output `$9 = 170 '\252'`I am guessing `$9` refers to the output count, and I know that 170 = 0xAA, but I don't know what `'\252'` means.Next I changed the variable to a `char` instead of an `unsigned char` and ran the same `print test_var`, I got an output of `$10 = -86 '\252'` which doesn't make a lot of sense to me. As far as I was aware, when you have a signed char (or just char) the MSB of the byte indicates the sign of the number, so if there is a `1` at BIT7 of the byte, it means that its a negative number, so when you look at `0xAA` you have `0b10101010`. At BIT7 you have a `1` so that means that the number is negative, which makes sense; however, the preceding 7 bits `0b0101010`, when converted to decimal is equal to 42, so the number SHOULD be -42, but according to GDB it's -86. What Am I understanding wrong with that?Next I wanted to read the direction register DDRA since I set DDA6 as an output for PORTA6 to be set LOW. From this I would know that the DDA6 bit of DDRA should be a 1 so I wanted to print the contents of DDRA to see that.First I tried `print DDRA` which output `= 0 '\000'` which doesn't make sense because I know that there should be a bit set at DDA6, and yet this is saying that no bits are set in that register. On top of that, there is a '\000' which is different from the '\252' from before.I thought that maybe It needed the physical memory address of the register so I looked at the end of the ATtiny84A datasheet in the Register Summary section and saw that DDRA had memory address 0x1A (0x3A) (I am not sure what the second value in brackets refers to so I tried both), so I then tried `print 0x1A` which output `= 26` and `print 0x3A` which output `= 58` neither of which make sense because I should see 64 indicating that BIT6 of DDRA has been set. And on top of this, these outputs are missing the '\000' and '\252' from before.Where is my understanding going wrong? I'm getting such different values with everything and I have absolutely no idea why, and I would love some help.Thank you!

author: oPossum
author_rank: Super Contributor
timestamp: June 05, 2021, 03:05:49 am
content: \252 is octal. This is a standard C escape code.https://en.wikipedia.org/wiki/Escape_sequences_in_CC typically uses two's compliment not one's compliment.https://en.wikipedia.org/wiki/Two%27s_complement

author: Kalcifer
author_rank: Regular Contributor
timestamp: June 05, 2021, 04:04:09 am
content: Quote from: Kalcifer on June 05, 2021, 02:58:50 amas found elsewhere, the reason I wasn't able to read DDRA was because I had never started the program. After running `continue` the register value updated and output 64 as expected when using the `print DDRA` command; however `print 0x1A` , and `print 0x3A` both still output 26 and 58, and I am unsure as to why.

author: Kalcifer
author_rank: Regular Contributor
timestamp: June 05, 2021, 04:07:12 am
content: Solved. See this Electrical Engineering Stack Exchange post:https://electronics.stackexchange.com/questions/569548/avr-gdb-inconsistencies?noredirect=1#comment1489444_569548

author: bson
author_rank: Unknown
timestamp: June 07, 2021, 08:55:11 pm
content: print/x will print a value in hex. Or just p/x

author: RoGeorge
author_rank: Super Contributor
timestamp: June 07, 2021, 09:19:24 pm
content: You might find GDB more intuitive and productive when used with a GUI/IDE:Only a generic USB to TTL serial adapter and a diode needed (as a one-wire debugger/programmer), the rest is FOSS:https://www.eevblog.com/forum/microcontrollers/avr-(arduino)-linux-debug/

author: TomS_
author_rank: Frequent Contributor
timestamp: June 11, 2021, 05:44:39 am
content: Quote from: Kalcifer on June 05, 2021, 04:04:09 am0x10 hex = 16 decimal0xA = 10Hopefully you can see where this is going...