50

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?

Community
  • 1
  • 1
Engineer
  • 8,529
  • 7
  • 65
  • 105
  • 1
    Setting a breakpoint at a certain file and a certain line or at a certain method for a certain class won't help you? http://www.unknownroad.com/rtfm/gdbtut/gdbbreak.html – Yaniro Jan 02 '12 at 15:50
  • 1
    @Yaniro, Sadly no. It needs to be time-spaced sampling, as stated in that answer I linked to, in order to have the desired effect. If I do it only in a particularly place in code, I won't see where most of the time is being spent, I'll only see those locations where the breakpoints sit. – Engineer Jan 02 '12 at 16:37
  • Possible duplicate of [pause gdb without breakpoint](http://stackoverflow.com/questions/7706857/pause-gdb-without-breakpoint) – Ciro Santilli OurBigBook.com Feb 17 '16 at 21:37

1 Answers1

86

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.

Jonas G. Drange
  • 8,749
  • 2
  • 27
  • 38
unwind
  • 391,730
  • 64
  • 469
  • 606
  • OK, it is receiving SIGINT now, which is great. Thank you. Only problem is that every time I halt it, I find myself in NlsUpdateSystemLocale() (in kernel32.dll) -- I think it's looking at the wrong thread. If you have any tips, they'd be welcome. Marking as accepted. – Engineer Jan 02 '12 at 16:52
  • 4
    Figured it out, gdb apparently doesn't (always?) know what the correct thread is, it just picks the one at the top of the thread list, at least in my mingw install. I just had to do gdb commands `thread`, `info threads` and `thread n` where n was the number of my running program's thread. Thanks again. – Engineer Jan 02 '12 at 16:59
  • 5
    Ctrl + C not working for some people: http://stackoverflow.com/questions/5857300/gdb-ctrlc-doesnt-interrupt-process-as-it-usually-does – Ciro Santilli OurBigBook.com Oct 28 '15 at 16:32
  • 1
    How do I interrupt it without Ctrl+C? I'm debugging an issue where a program locks up the entire desktop environment, so I no longer have access to the terminal where I ran GDB. Can I interrupt after a specified time, like 10 seconds after the program starts? – Aaron Franke Aug 04 '21 at 23:26