Mirror of the gdb mailing list
 help / color / mirror / Atom feed
* detecting gdb at runtime?
@ 2004-04-12 21:38 Joshua Haberman
  2004-04-12 21:52 ` Daniel Jacobowitz
  0 siblings, 1 reply; 2+ messages in thread
From: Joshua Haberman @ 2004-04-12 21:38 UTC (permalink / raw)
  To: gdb

Is there any way to detect GDB at runtime?

I am currently experimenting with different ways to handle errors in a 
C++ library I am writing.  I am using an Assert() macro to verify 
conditions throughout the program, much like C's assert() function.  I 
believe that the best way to respond to a failed assertion is to throw 
an exception because it gives the client application a chance to 
recover as best it can.  However, if the program is running in a 
debugger, throwing an exception is sub-optimal because it cannot give 
you a stack trace.

My goal is to have my Assert() macro SIGTRAP if a debugger is running 
to break into the debugger, otherwise throw an exception.  Is there any 
way to make this happen?

(Please CC me, I am not subscribed).

Josh Haberman


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: detecting gdb at runtime?
  2004-04-12 21:38 detecting gdb at runtime? Joshua Haberman
@ 2004-04-12 21:52 ` Daniel Jacobowitz
  0 siblings, 0 replies; 2+ messages in thread
From: Daniel Jacobowitz @ 2004-04-12 21:52 UTC (permalink / raw)
  To: Joshua Haberman; +Cc: gdb

On Mon, Apr 12, 2004 at 02:25:17PM -0700, Joshua Haberman wrote:
> Is there any way to detect GDB at runtime?
> 
> I am currently experimenting with different ways to handle errors in a 
> C++ library I am writing.  I am using an Assert() macro to verify 
> conditions throughout the program, much like C's assert() function.  I 
> believe that the best way to respond to a failed assertion is to throw 
> an exception because it gives the client application a chance to 
> recover as best it can.  However, if the program is running in a 
> debugger, throwing an exception is sub-optimal because it cannot give 
> you a stack trace.
> 
> My goal is to have my Assert() macro SIGTRAP if a debugger is running 
> to break into the debugger, otherwise throw an exception.  Is there any 
> way to make this happen?

Yes, but it's probably not what you really want to do.  You could set a
breakpoint (-> provide a sample .gdbinit which sets a breakpoint) at
the place where this particular exception is thrown.  Or you could use
the catch throw command in recent versions of GDB, which will cause a
breakpoint when any exception is thrown.

I think you can non-portably detect an attached debugger using ptrace. 
But, you'll detect things like strace too, and that means you'll crash
when you assert under strace - it doesn't handle breakpoints
gracefully.

-- 
Daniel Jacobowitz
MontaVista Software                         Debian GNU/Linux Developer


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2004-04-12 21:38 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-04-12 21:38 detecting gdb at runtime? Joshua Haberman
2004-04-12 21:52 ` Daniel Jacobowitz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox