6

I'm working with a C++ library. The library uses several namespaces. When debugging, I have to prefix every symbol name with the namespace prefix. It causes a lot of extra work and typing.

C++ has the using namespace X concept to make symbols available with more ease (lots of hand waiving). I'm looking for similar in GDB. For example, instead of b MyLibNamespace::Foo::bar, I want to b Foo::bar.

GDB does not appear to have help related to namespaces, but I'm probably doing something wrong:

(gdb) help namespace
Undefined command: "namespace".  Try "help".
(gdb) namespace help
Undefined command: "namespace".  Try "help".

How do I tell GDB to use a namespace prefix so I don't have to provide it for every symbol name?

jww
  • 97,681
  • 90
  • 411
  • 885

2 Answers2

0

How do I tell GDB to use a namespace prefix so I don't have to provide it for every symbol name?

There doesn't appear to be any such support in current GDB (as of 2017-08-13).

You can probably implement it using Python scripting to define a new command. Documentation.

Beware, this is entirely non-trivial proposition.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
0

How do I tell GDB to use a namespace prefix so I don't have to provide it for every symbol name?

You might consider a work-around...

I have (on occasion) added one or more (C++) functions to my class definitions file. (.cc), but they are not part of the class(s).

They are not part of the application, and are harmlessly removed when you are done with them.

They generally 'dump' info (with names d1(), d2(), etc.)

But they can also do practically any thing useful for your debugging effort, Usually, it is not the case that you thought of this specific test effort ahead of time.

So, your edit/compile/link iteration is simply: stop gdb, open the file, add a useful function, line, and resume gdb. Keep this 'diagnostic' code simple. Hopefully the result is ultimately time saving.

I can find no examples (in my files) at the moment. I suppose I discard these functions quickly once I've overcome a particular challenge.


Anyway ... this demo worked just a few minutes ago ...

When working in gdb near my class Foo_t, part of namespace DTB, etc. the d1 I've created knows how to access a particular instance of Foo_t (in some convenient way), and, can easily dump the current state of the instance using a Foo method to do so. Perhaps d1 can look like this:

void d1() { objDer.f("xxx"); } // a derived instance,
//                             the class has a long complex name.

Now, in gdb, run to a breakpoint somewhere when that instance exists, and is initialized, and use gdb print command to run d1 ...

(gdb) p d1()

that is a short gdb command to get at the instance and run a method.

2785528
  • 5,438
  • 2
  • 18
  • 20