2
votes

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.

2
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
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
I've seen GDB skip lines on other occasions, but never this. Then again I'm not running "Apple". - d-_-b

2 Answers

2
votes

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).

0
votes

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)