6

I normally use a debugger such as cgdb so I thankfully hadn't needed to resort to system calls for debugging, until now.

I have a binary that takes around 30 minutes to load in gdb and frequently crashes the debugger thereafter, probably due to the very large number of symbols. This makes my normal workflow (loading gdb, adding breakpoints, identifying problem variables, etc.) untenable.

In windows I would at this point begin adding lots of cout and system("pause") (poor man's breakpoint) statements throughout my code; but how do I do this in a unix-like environment?

13

You may want to read from cin to get a poor man’s pause – it will wait for you to type an Enter, rather than resuming while you’re getting coffee (as sleep() will).

6

I sometimes resort to this

#define BRK()  do { printf("%s %d\n", __FILE__, __LINE__); getchar(); } while (0)
...
BRK();  // Stop and wait for enter

Also, function instrumentation might help: GCC allows to hook into every function enter/exit, see https://stackoverflow.com/questions/2281739/automatically-adding-enter-exit-function-logs-to-a-project

  • Maybe your C is different from mine, but I either have to put parens in the define or delete them in the call for this to work. Otherwise, that's a handy tip! – labyrinth Jan 9 '15 at 18:18
  • Yeah, thanks for the correction, added parens – valenok Jan 10 '15 at 0:22
5

Same thing for cout/cerr, and you can just use sleep() - see man 3 sleep or man 3 usleep for more info.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.