From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 17921 invoked by alias); 12 Apr 2004 21:38:51 -0000 Mailing-List: contact gdb-help@sources.redhat.com; run by ezmlm Precedence: bulk List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-owner@sources.redhat.com Received: (qmail 16712 invoked from network); 12 Apr 2004 21:36:31 -0000 Received: from unknown (HELO nevyn.them.org) (66.93.172.17) by sources.redhat.com with SMTP; 12 Apr 2004 21:36:31 -0000 Received: from drow by nevyn.them.org with local (Exim 4.31 #1 (Debian)) id 1BD96S-0000fX-7A; Mon, 12 Apr 2004 17:36:28 -0400 Date: Mon, 12 Apr 2004 21:52:00 -0000 From: Daniel Jacobowitz To: Joshua Haberman Cc: gdb@sources.redhat.com Subject: Re: detecting gdb at runtime? Message-ID: <20040412213628.GA2459@nevyn.them.org> Mail-Followup-To: Joshua Haberman , gdb@sources.redhat.com References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.5.1+cvs20040105i X-SW-Source: 2004-04/txt/msg00068.txt.bz2 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