Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gdb: How do I pause during loop execution?

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?

like image 591
Engineer Avatar asked Jan 02 '12 15:01

Engineer


People also ask

How do you break a loop in gdb?

Like you can cancel a program on the command line, GDB lets you use ctrl-c to stop a program wherever it currently is. Hit ctrl-c now to break the infinite loop.

What does Ctrl-C do in gdb?

Normally when you run a program through GDB you can press Ctrl+C to interrupt it, e.g. if it gets stuck in an infinite loop and you want to get a backtrace.

How do I skip iterations in gdb?

You can use the condition breakpoint. Show activity on this post. In C# for example you can do "continue" to skip iteration. Example of skipping numbers with mod 3 equal 0, so numbers 3, 9, 12, 15 ... will be skipped.


1 Answers

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.

like image 98
unwind Avatar answered Oct 08 '22 19:10

unwind