sizeof returns 0 for some arrays

Roman Popov <[email protected]>
Newsgroups gmane.comp.gdb.devel
Message-ID <CAATAM3FNwai8mj7nosRZHECz=pKwTg=icKY94jh0eWqx1jh-TQ@mail.gmail.com>
Hello, while debugging code for SystemC library (
http://www.accellera.org/downloads/standards/systemc) I've noticed that GDB
does not print values of some arrays.  After some investigation it looks
like the root of problem is that size of array is not determined properly.
In some cases I got 0 size instead of valid value.

Unfortunately I was not able to extract a reproducer that does not require
SystemC library.
So I apologize I can't provide an easy way to debug this.


But in case someone have time to download and build library, here is a
short example:

test.cpp:

#include <systemc.h>
struct mod : sc_module {
    sc_in<bool> x[1];
    sc_signal <bool> s[1];
    mod (sc_module_name) {
        // Set breakpoint here !
    }
};
int sc_main (int argc, char**argv) {
    mod m("m");
    return 0;
}


// I've installed SystemC to /opt/systemc
// Please note that same -std flag should be used both during library build
and example build

g++ -std=c++14 -g main.cpp -Wl,-rpath=/opt/systemc/lib -L/opt/systemc/lib
-lsystemc -I/opt/systemc/include

gdb ./a.out
(gdb) break main.cpp:8
(gdb) r
(gdb) p x
$1 = 0x7fffffffd6a8
.. strange, gdb does not prints contents of array.

(gdb) print sizeof(x)
$4 = 0

So GDB thinks that sizeof of array is 0!

Let's try to print first element:
(gdb) print x[0]
$5 = {<sc_core::sc_port<sc_core::sc_signal_in_if<bool>, 1,
sc_core::sc_port_policy::SC_ONE_OR_MORE_BOUND>> =
{<sc_core::sc_port_b<sc_core::sc_signal_in_if<bool> >> =
{<sc_core::sc_port_base> = {<sc_core::sc_object> = {
          _vptr$sc_object = 0x6053b0 <vtable for sc_core::sc_in<bool>+16>,
m_attr_cltn_p = 0x0, m_child_events = std::vector of length 0, capacity 0,
m_child_objects = std::vector of length 0, capacity 0, m_name = "m.port_0",
          m_parent = 0x7fffffffd5c0, m_simc = 0x62f970}, m_bind_info =
0x642230}, m_interface = 0x0, m_interface_vec = std::vector of length 0,
capacity 0}, <No data fields>}, m_traces = 0x0, m_change_finder_p = 0x0,
m_neg_finder_p = 0x0,
  m_pos_finder_p = 0x0, static dummy = <optimized out>}

Works

(gdb) print sizeof(x[0])
$6 = 184

Works

So it is possible to debug the code manually. However it looks like that
all clients rely on sizeof() , so for example my GDB gui does not works.


Python API has same issue:
(gdb) python
>x = gdb.parse_and_eval('x')
>print(x.type)
>print(x.type.sizeof)
>end
sc_core::sc_in<bool> [1]
0



-Roman
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.