0

I have a data structure that looks like this:

#include <stdbool.h>
.
.
.
typedef struct Input {
    uint8_t pin;        /**< Pin associated with this input */
    bool state,         /**< Current pin state */
         last_state;    /**< Pin state last time we checked */
} Input;

I initialize the data structure like this:

Input *input = (Input *)malloc(sizeof(Input));
input->pin = 0;
input->state = false;
input->last_state = false;

I am compiling the code with avr-gcc 7.2.0 and running the code using simulavr ("SimularAVR 1.2dev") and attaching to it using avr-gdb.

When I print out the data structure in gdb just after initialization I see this:

(gdb) p *input
$1 = {pin = 0 '\000', state = false}

Note that the last_state member isn't showing up. Futhermore, if I set either one of the existing members, it appears that they are actually referring to the same location:

(gdb) set input->pin=123
(gdb) p *input
$8 = {pin = 123 '{', state = 123}

If I change the data type of state and last_state from bool to, say, uint8_t, like this...

typedef struct Input {
    uint8_t pin;        /**< Pin associated with this input */
    uint8_t state,      /**< Current pin state */
            last_state; /**< Pin state last time we checked */
} Input;

...then everything displays correctly:

(gdb) p *input
$1 = {pin = 0 '\000', state = 0 '\000', last_state = 0 '\000'}

What's going on here? Is this a bug in gdb? In simulavr? Something else? Note that the code itself runs correctly when deployed on the target microcontroller (an attiny85); this is entirely a debugging/simulation issue.

1
  • Hey, -1 voter, I would love some feedback about what in this question offended you. Cheers!
    – larsks
    Apr 23, 2019 at 22:18

1 Answer 1

0

Is this a bug in gdb?

It is definitely a bug in either gdb or avr-gcc -- either the debug info is incorrect (bug in gcc) or GDB is not interpreting it correctly (bug in GDB).

The simulavr has nothing to do with this.

To find out more, readelf -wi a.out and compare the description of struct Input in the working and non-working case. They should only differ in the type of state and last_state fields. If that's what you in fact observe, GDB bug is most likely.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.