gdb: How do I pause during loop execution?

Engineer picture Engineer · Jan 2, 2012 · Viewed 49.9k times · Source

I'm writing a software renderer in g++ under mingw32 in Windows 7, using NetBeans 7 as my IDE.

I've been needing to profile it of late, and this need has reached critical mass now that I'm past laying down the structure. I looked around, and to me this answer shows the most promise in being simultaneously cross-platform and keeping things simple.

The gist of that approach is that possibly the most basic (and in many ways, the most accurate) way to profile/optimise is to simply sample the stack directly every now and then by halting execution... Unfortunately, NetBeans won't pause. So I'm trying to find out how to do this sampling with gdb directly.

I don't know a great deal about gdb. What I can tell from the man pages though, is that you set breakpoints before running your executable. That doesn't help me.

Does anyone know of a simple approach to getting gdb (or other gnu tools) to either:

  1. Sample the stack when I say so (preferable)
  2. Take a whole bunch of samples at random intervals over a given period

...give my stated configuration?

Answer

unwind picture unwind · Jan 2, 2012

Have you tried simply running your executable in gdb, and then just hitting ^C (Ctrl+C) when you want to interrupt it? That should drop you to gdb's prompt, where you can simply run the where command to see where you are, and then carry on execution with continue.

If you find yourself in a irrelevant thread (e.g. a looping UI thread), use thread, info threads and thread n to go to the correct one, then execute where.