I am running into the following problem on the latest version of OS X 10.8.2, with latest Xcode 4.5.
Take the following simple piece of code:
#include <iostream>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, const char * argv[])
{
pid_t pid = fork();
if (0 == pid)
{
std::cout << "Child process!\n";
exit(0);
}
else if (-1 == pid)
{
std::cout << "Error forking.\n";
}
else
{
std::cout << "In parent, created pid " << pid << ".\n";
sleep(100000); // Sleep a long time - GDB/LLDB ignores the first sleep statement
sleep(3); // Sleep 3 more seconds - GDB/LLDB doesn't ignore the second sleep statement
std::cout << "Done in parent!\n";
}
return 0;
}
Compile it using clang++ foo.cpp -o foo
or g++ foo.cpp -o foo
and run it using ./foo
, it takes a long time to run, as expected.
Now do either lldb ./foo
or gdb ./foo
, then run
and notice it completes in 3 seconds. Whenever either debugger is used, the first sleep statement is seemingly ignored.
Since Xcode uses lldb by default when running a project, pasting the above code into a blank Xcode project and doing Product->Run will have similar results.
I've tried the same experiment on a Linux machine with gdb 7.2, and the problem does not occur there.
Is this a bug in an older version of gdb that Apple uses (gdb 6.3.50-20050815 (Apple version gdb-1822) ), or is it something else? Perhaps just my computer is messed up, if it doesn't happen to other OS X users?