Author Topic: Any reasonable procedure for debugging with GDB?  (Read 4192 times)

Offline ta0

  • Multiple posting newcomer
  • *
  • Posts: 16
Any reasonable procedure for debugging with GDB?
« on: May 20, 2015, 03:50:14 am »
I have tried googling for an answer, but haven't found anything that didn't look very outdated.

What I want to do is simply to have sane debugging for C++ code, using MinGW under Windows. Almost every variable and function is inaccessible, the best I can get is the hex value of pointers.

Is there any tutorial or walk through on how to use GDB in a productive fashion for C++ code in Code::Blocks? As it is, I'm happy when I can even get to see the content of an std::string. Almost no matter what I put into the "watch" fields, I get an "Not available in current context!" or "<optimized out>". If I try to use the GDB console, it is always a bunch of "Couldn't find method std::istream::eof" this and "Cannot evaluate function -- may be inlined" that.

Of course I use -o0 -g3 -fno-inline, __attribute__ ((used, noinline)) on everything, etc. Rarely helps. (yes, I understand the difference between target and project options)

There is some talk in the wiki of squirrel scripting for making custom views of objects. However, it says (in a somewhat snarky way, I might add) that if you have to ask you ain't never gonna know, and that user scripts are planned to be implemented sometime in the future.

Other forum posts talks about using a python-enabled GDB, didn't get it.

To be honest, I've only ever used the basics of GDB, that was always enough for C code, and with Python it was never a problem. It still bothers me that we seem to be expected to program C++ with basicly no debugging support other than basic break points and prints. After having programmed dynamic languages for some years, where you generally the kind of debugging support you'd logically expect, the whole thing feels a bit retarded, because obviously you need a proper debugger much more desperately when doing the things that C++ is good for, such as bare metal coding. But googling for an answer, it really seems that people still in 2015 do C++ debugging with mere line breaks and vardumps.
« Last Edit: May 20, 2015, 03:52:02 am by ta0 »

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5915
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Any reasonable procedure for debugging with GDB?
« Reply #1 on: May 20, 2015, 09:30:11 am »

Is there any tutorial or walk through on how to use GDB in a productive fashion for C++ code in Code::Blocks? As it is, I'm happy when I can even get to see the content of an std::string. Almost no matter what I put into the "watch" fields, I get an "Not available in current context!" or "<optimized out>". If I try to use the GDB console, it is always a bunch of "Couldn't find method std::istream::eof" this and "Cannot evaluate function -- may be inlined" that.

Of course I use -o0 -g3 -fno-inline, __attribute__ ((used, noinline)) on everything, etc. Rarely helps. (yes, I understand the difference between target and project options)

Can you give us a minimal example(test code and the debugger log) to demonstrate your issue, thanks.
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline ta0

  • Multiple posting newcomer
  • *
  • Posts: 16
Re: Any reasonable procedure for debugging with GDB?
« Reply #2 on: May 20, 2015, 12:59:38 pm »
Ok, I'll condense it into a minimal example reproducing the problems.

Just a question though. It sounds from your reply that what I describe is not how it's supposed to work and that there must be some error somewhere, be it user errors, config errors or bugs. Is this correct? Because from my googling, it seems that my experience with debugging C++ code with GDB is the same as for most people and that eventually you are supposed to switch to Visual Studio if you want to do serious development.

What I would really want is a good explanation of how to use the debug scripting with C::B.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5915
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Any reasonable procedure for debugging with GDB?
« Reply #3 on: May 20, 2015, 02:13:33 pm »
Ok, I'll condense it into a minimal example reproducing the problems.

Just a question though. It sounds from your reply that what I describe is not how it's supposed to work and that there must be some error somewhere, be it user errors, config errors or bugs. Is this correct? Because from my googling, it seems that my experience with debugging C++ code with GDB is the same as for most people and that eventually you are supposed to switch to Visual Studio if you want to do serious development.
GDB works OK here in my system(WinXP), so I have no idea what is your problem.

Quote
What I would really want is a good explanation of how to use the debug scripting with C::B.
What do you mean by "debug scripting with C::B"? I use python pretty printer with GDB. I don't know much about other debug scripts.
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Any reasonable procedure for debugging with GDB?
« Reply #4 on: May 20, 2015, 08:40:54 pm »
Ok, I'll condense it into a minimal example reproducing the problems.
Basic debugging feature should work fine.

... Because from my googling, it seems that my experience with debugging C++ code with GDB is the same as for most people and that eventually you are supposed to switch to Visual Studio if you want to do serious development.
If you're building all your code with mingw, then debugging should work fine.
If you're calling external libraries build with vc++, then it won't work, because gdb doesn't understand the debug info produced by vc++.

What I would really want is a good explanation of how to use the debug scripting with C::B.
Just don't, they are outdated and broken. Their proper replacement are gdb python printer scripts.
But if I were you I'd first make simple variables show in the watches window correctly and then bother with the more complex ones.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline ta0

  • Multiple posting newcomer
  • *
  • Posts: 16
Re: Any reasonable procedure for debugging with GDB?
« Reply #5 on: May 22, 2015, 06:56:17 pm »
What about something really basic, like say I have a vector<string> and want to know what is in it.

If I try to call a print function from the debug console I only get SIGSEGV. And I obviously can't see anything except some hex addresses and <No data fields> in the watch window.

All I want is to be able to see at least something without having to insert cout's all over the place. I hardly think that is having a problem.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Any reasonable procedure for debugging with GDB?
« Reply #6 on: May 22, 2015, 09:07:07 pm »
What about something really basic, like say I have a vector<string> and want to know what is in it.
For this to work you need gdb which supports python pretty printers and also printer scripts for stl.

On linux this is relatively easy to setup, but on windows it is a bit more complicated.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]