From patchwork Tue Nov 3 16:48:11 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pedro Alves X-Patchwork-Id: 9542 Received: (qmail 18238 invoked by alias); 3 Nov 2015 16:48:16 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 18213 invoked by uid 89); 3 Nov 2015 16:48:15 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=AWL, BAYES_00, SPF_HELO_PASS, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Tue, 03 Nov 2015 16:48:14 +0000 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (Postfix) with ESMTPS id 43E8AC0D61B8 for ; Tue, 3 Nov 2015 16:48:13 +0000 (UTC) Received: from brno.lan (ovpn01.gateway.prod.ext.ams2.redhat.com [10.39.146.11]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id tA3GmBde002699 for ; Tue, 3 Nov 2015 11:48:12 -0500 From: Pedro Alves To: gdb-patches@sourceware.org Subject: [PATCH] [C++] Always use setjmp/longjmp for exceptions Date: Tue, 3 Nov 2015 16:48:11 +0000 Message-Id: <1446569291-21711-1-git-send-email-palves@redhat.com> We currently throw exceptions from signal handlers (e.g., for Quit/ctrl-c). But throwing C++ exceptions from signal handlers is undefined. (That doesn't restore signal masks, like siglongjmp does, and, because asynchronous signals can arrive at any instruction, we'd have to build _everything_ with -fasync-unwind-tables to make it reliable.) It happens to work on x86_64 GNU/Linux at least, but it's likely broken on other ports. Until we stop throwing from signal handlers, use setjmp/longjmp based exceptions in C++ mode as well. gdb/ChangeLog: 2015-11-03 Pedro Alves * common/common-exceptions.h (GDB_XCPT_SJMP, GDB_XCPT_TRY) (GDB_XCPT_RAW_TRY, GDB_XCPT): Define. Replace __cplusplus checks with GDB_XCPT checks throughout. * common/common-exceptions.c: Replace __cplusplus checks with GDB_XCPT checks throughout. --- gdb/common/common-exceptions.c | 10 +++++----- gdb/common/common-exceptions.h | 39 ++++++++++++++++++++++++++++----------- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/gdb/common/common-exceptions.c b/gdb/common/common-exceptions.c index ce476a2..231bab5 100644 --- a/gdb/common/common-exceptions.c +++ b/gdb/common/common-exceptions.c @@ -22,7 +22,7 @@ const struct gdb_exception exception_none = { (enum return_reason) 0, GDB_NO_ERROR, NULL }; -#ifndef __cplusplus +#if GDB_XCPT == GDB_XCPT_SJMP /* Possible catcher states. */ enum catcher_state { @@ -213,7 +213,7 @@ exceptions_state_mc_action_iter_1 (void) return exceptions_state_mc (CATCH_ITER_1); } -#else /* !__cplusplus */ +#else /* !GDB_XCPT_SJMP */ /* How many nested TRY blocks we have. See exception_messages and throw_it. */ @@ -261,7 +261,7 @@ gdb_exception_sliced_copy (struct gdb_exception *to, const struct gdb_exception *to = *from; } -#endif /* !__cplusplus */ +#endif /* !GDB_XCPT_SJMP */ /* Return EXCEPTION to the nearest containing catch_errors(). */ @@ -272,7 +272,7 @@ throw_exception (struct gdb_exception exception) do_cleanups (all_cleanups ()); -#ifndef __cplusplus +#if GDB_XCPT == GDB_XCPT_SJMP /* Jump to the containing catch_errors() call, communicating REASON to that call via setjmp's return value. Note that REASON can't be zero, by definition in defs.h. */ @@ -320,7 +320,7 @@ throw_it (enum return_reason reason, enum errors error, const char *fmt, { struct gdb_exception e; char *new_message; -#ifndef __cplusplus +#if GDB_XCPT == GDB_XCPT_SJMP int depth = catcher_list_size (); #else int depth = try_scope_depth; diff --git a/gdb/common/common-exceptions.h b/gdb/common/common-exceptions.h index 51795b1..def08b1 100644 --- a/gdb/common/common-exceptions.h +++ b/gdb/common/common-exceptions.h @@ -116,12 +116,32 @@ struct gdb_exception const char *message; }; +/* The different exception mechanisms that TRY/CATCH can map to. */ + +/* Make GDB exceptions use setjmp/longjmp behind the scenes. This is + the only mode supported when GDB is built as a C program. */ +#define GDB_XCPT_SJMP 1 + +/* Make GDB exceptions use try/catch behind the scenes. Can't be made + the default until we stop throwing exceptions from signal + handlers. */ +#define GDB_XCPT_TRY 2 + +/* Specify this mode to build with TRY/CATCH mapped directly to raw + try/catch. GDB won't work correctly, but building that way catches + code tryin to break/continue out of the try block, along with + spurious code between the TRY and the CATCH block. */ +#define GDB_XCPT_RAW_TRY 3 + +/* Always use setjmp/longmp, even in C++ mode. */ +#define GDB_XCPT GDB_XCPT_SJMP + /* Functions to drive the exceptions state machine. Though declared here by necessity, these functions should be considered internal to the exceptions subsystem and not used other than via the TRY/CATCH macros defined below. */ -#ifndef __cplusplus +#if GDB_XCPT == GDB_XCPT_SJMP extern SIGJMP_BUF *exceptions_state_mc_init (void); extern int exceptions_state_mc_action_iter (void); extern int exceptions_state_mc_action_iter_1 (void); @@ -157,7 +177,7 @@ extern void exception_rethrow (void); */ -#ifndef __cplusplus +#if GDB_XCPT == GDB_XCPT_SJMP #define TRY \ { \ @@ -176,7 +196,9 @@ extern void exception_rethrow (void); #define END_CATCH \ } -#else +#endif /* GDB_XCPT_SJMP */ + +#if GDB_XCPT == GDB_XCPT_TRY || GDB_XCPT == GDB_XCPT_RAW_TRY /* Prevent error/quit during TRY from calling cleanups established prior to here. This pops out the scope in either case of normal @@ -195,13 +217,7 @@ struct exception_try_scope void *saved_state; }; -/* Define this to build with TRY/CATCH mapped directly to raw - try/catch. GDB won't work correctly, but building that way catches - code tryin to break/continue out of the try block, along with - spurious code between the TRY and the CATCH block. */ -//#define USE_RAW_CXX_TRY - -#ifndef USE_RAW_CXX_TRY +#if GDB_XCPT == GDB_XCPT_TRY /* We still need to wrap TRY/CATCH in C++ so that cleanups and C++ exceptions can coexist. The TRY blocked is wrapped in a @@ -224,6 +240,7 @@ struct exception_try_scope { \ exception_rethrow (); \ } + #else #define TRY try @@ -249,7 +266,7 @@ struct gdb_exception_RETURN_MASK_QUIT : public gdb_exception_RETURN_MASK_ALL { }; -#endif +#endif /* GDB_XCPT_TRY || GDB_XCPT_RAW_TRY */ /* *INDENT-ON* */