3

I have a program that I'd like to debug by setting a breakpoint in a non-default constructor, but the breakpoint I set is never hit. Below is an example program where this problem comes up. There is no problem hitting breakpoints set in the main function, but any breakpoints set in the Domain.cpp file are ignored:

Main.cpp:

#include <iostream>
#include "Domain.h"

int main()
{
  Domain y;
  std::cout << y.x << std::endl;  // <- No problem setting breakpoint here
  return 0;
}

Domain.cpp:

#include "Domain.h"

Domain::Domain()
{
  x = 4;  // <- A breakpoint here is skipped
}

Domain.h:

#ifndef DOMAIN_H_
#define DOMAIN_H_

class Domain
{
public:
  int x;
public:
  Domain();
};

#endif /* DOMAIN_H_ */

However, the problem does not exist if I put everything into a single file:

Main2.cpp:

#include <iostream>

int main()
{

  class Domain
  {
  public:
    int x;
    Domain()
    {
      x = 4;  // <- No problem setting breakpoint here now!
    };

  };

  Domain y;

  std::cout << y.x << std::endl;

  return 0;
}

Why is this the case? How can I change this so that I'm able to set breakpoints when I use multiple files?

I can confirm that the breakpoints aren't working both when I run the debugger manually in a terminal and when I run it through Eclipse CDT, where I get the same error discussed in this question which was apparently never answered:

Why does Eclipse CDT ignore breakpoints?

I am using:

  • Eclipse Kepler
  • Mac OSX 10.8.4
  • gdb 6.3.5 (Apple version)
  • gcc 4.2.1 with -O0 and -g3 flags

Please be patient with me. I'm still learning the ropes.

Community
  • 1
  • 1
Neal Kruis
  • 2,055
  • 3
  • 26
  • 49
  • I always had this problem with gdb (among others...), even in command-line. Can you use Visual Studio (which have a way better debugger). – Synxis Aug 05 '13 at 21:16
  • I'd prefer to stick with GNU tools since I am working on a cross platform solution and I prefer to use free (in all senses of the word) tools when possible. – Neal Kruis Aug 05 '13 at 21:59
  • I've seen GDB skip lines on other occasions, but never this. Then again I'm not running "Apple". – d-_-b Aug 06 '13 at 02:46

2 Answers2

2

You are likely hitting this GDB bug.

This bug has long been fixed, but your version of GDB is very old (and Apple is unlikely to update it).

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • Yep...that's the problem. I can't believe Apple hasn't updated this at all. I tried to install the macports version of GDB, but ran into all sorts of code signing problems. Eventually I gave up on trying to develop on Mac and moved to Ubuntu. – Neal Kruis Aug 07 '13 at 22:43
0

This is a very interesting anomaly that I would like to explore further, but I suspect it's related to Eclipse's default GCC settings. Many super basic functions like these get optimized out when they hit the compiler. (one time I tried to track a simple for loop, but the viable was removed entirely on GCC's highest optimization settings)

GreenFox
  • 1
  • 1
  • this is more suited as a comment on the original question than an answer. That being said, I have played with other debug/optimization options without much success. Please let me know if you do find something out. – Neal Kruis Aug 05 '13 at 22:02