Re: GDB can't parse variables named "memory" or "array"?

Paul Smith <[email protected]>
Newsgroups gmane.comp.gdb.devel
Message-ID <[email protected]>
On Fri, 2017-09-29 at 14:57 -0400, Sergio Durigan Junior wrote:
> On Friday, September 29 2017, Paul Smith wrote:
> > I've tried this with lots of different versions of GDB (7.7.1,
> > 7.11, 7.12, etc.), and none of them work when debugging my programs
> > (not just my main program but all my unit tests as well):
> > 
> >    (gdb) p memory
> >    A syntax error in expression, near `'.
> > 
> >    (gdb) p array
> >    A syntax error in expression, near `'.

> You can also enable "set debug parser on" and/or "set debug
> expression 1" inside your "faulty" GDB and see if it helps with
> anything.

This gave very useful information, actually.  I now see what's
happening, although I can't understand why no one has noticed this so
I'm not sure.

Printing "array" or "memory" shows:

  (gdb) p memory
  Starting parse
  Entering state 0
  Reading a token: Next token is token FILENAME (bval<0x335a1d0>)
  Shifting token FILENAME (bval<0x335a1d0>)
  Entering state 47
  Reducing stack by rule 107 (line 932):
     $1 = token FILENAME (bval<0x335a1d0>)
  -> $$ = nterm block ()
  Stack now 0
  Entering state 57
  Reading a token: Now at end of input.
  A syntax error in expression, near `'.

The problem appears to be with the C++ header files, which don't have
extensions.  Here's a repro case:

$ cat gdbtest.cpp
// must include memory
#include <memory>

class Foo
{
    char* memory;
};

Foo foo;

int main(int, char**)
{
    return 1;
}

$ g++ --ggdb3 -o gdbtest gdbtest.cpp

$ gdb -n gdbtest
 ...
(gdb) br 13
(gdb) run
(gdb) p foo.memory
A syntax error in expression, near `'.


Note that you have to use -ggdb3 to see the problem; just using -g
doesn't show the error.  Also it ends up that the symbol must be part
of a class (or probably struct but not tested): if it's a global or
auto symbol it's interpreted correctly.

Why is GDB even considering a filename to be part of a print
expression?
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.