[RFC master/7.12.1] Don't propagate C++ exceptions across readline using SjLj on SjLj-based exception unwinding

Yao Qi qiyaoltc@gmail.com
Mon Dec 19 14:42:00 GMT 2016


Nowadays, we propagate C++ exceptions across readline using
setjmp/longjmp 89525768cd086a0798a504c81fdf7ebcd4c904e1
(Propagate GDB/C++ exceptions across readline using sj/lj-based TRY/CATCH)
because DWARF-based unwinding can't cross C function (see
details from the commit above).  However, SjLj-based exception
unwinding doesn't have such issue.

What is more, looks longjmp may break the SjLj-based exception
handling, because _Unwind_SjLj_Unregister, which is put the exit
of function, is not executed due to longjmp.

 (gdb) [New Thread 2936.0xb80]
 kill

 Thread 1 received signal SIGSEGV, Segmentation fault.
 0x03ff662b in ?? ()
 top?bt 15
 #0  0x03ff662b in ?? ()
 #1  0x00526b92 in stdin_event_handler (error=0, client_data=0x172ed8)
    at ../../binutils-gdb/gdb/event-top.c:555
 #2  0x00525a94 in handle_file_event (ready_mask=<optimized out>,
    file_ptr=0x3ff5cb8) at ../../binutils-gdb/gdb/event-loop.c:733
 #3  gdb_wait_for_event (block=block@entry=1)
    at ../../binutils-gdb/gdb/event-loop.c:884
 #4  0x00525bfb in gdb_do_one_event ()
    at ../../binutils-gdb/gdb/event-loop.c:347
 #5  0x00525ce5 in start_event_loop ()
    at ../../binutils-gdb/gdb/event-loop.c:371
 #6  0x0051fada in captured_command_loop (data=0x0)
    at ../../binutils-gdb/gdb/main.c:324
 #7  0x0051cf5d in catch_errors (
    func=func@entry=0x51fab0 <captured_command_loop(void*)>,
    func_args=func_args@entry=0x0,
    errstring=errstring@entry=0x7922bf <VEC_interp_factory_p_quick_push(VEC_inte rp_factory_p*, interp_factory*, char const*, unsigned int)::__PRETTY_FUNCTION__+351> "", mask=mask@entry=RETURN_MASK_ALL)
    at ../../binutils-gdb/gdb/exceptions.c:236
 #8  0x00520f0c in captured_main (data=0x328feb4)
    at ../../binutils-gdb/gdb/main.c:1149
 #9  gdb_main (args=args@entry=0x328feb4) at ../../binutils-gdb/gdb/main.c:1159
 #10 0x0071e400 in main (argc=1, argv=0x171220)
    at ../../binutils-gdb/gdb/gdb.c:32

I dig into libcc/unwind-sjlj.c and gcc/except.c, but I still
don't find much clue.  This patch fixes this issue by not propagating
the exception via setjmp/longjmp if __USING_SJLJ_EXCEPTIONS__.

gdb:

2016-12-19  Yao Qi  <yao.qi@linaro.org>

	PR gdb/20977
	* event-top.c (gdb_rl_callback_read_char_wrapper): New function.
	(gdb_rl_callback_read_char_wrapper): Call
	gdb_rl_callback_read_char_wrapper.
	(gdb_rl_callback_handler_1): New function.
	(gdb_rl_callback_handler): Call gdb_rl_callback_handler_1.
---
 gdb/event-top.c | 34 ++++++++++++++++++++++++++++------
 1 file changed, 28 insertions(+), 6 deletions(-)

diff --git a/gdb/event-top.c b/gdb/event-top.c
index acf8474..db3a18f 100644
--- a/gdb/event-top.c
+++ b/gdb/event-top.c
@@ -121,6 +121,14 @@ static struct async_signal_handler *async_sigterm_token;
 void (*after_char_processing_hook) (void);
 
 
+static void
+gdb_rl_callback_read_char_wrapper (void)
+{
+  rl_callback_read_char ();
+  if (after_char_processing_hook)
+    (*after_char_processing_hook) ();
+}
+
 /* Wrapper function for calling into the readline library.  This takes
    care of a couple things:
 
@@ -136,7 +144,8 @@ void (*after_char_processing_hook) (void);
    Any exception that tries to propagate through such code will fail
    and the result is a call to std::terminate.  While some ABIs, such
    as x86-64, require all code to be built with exception tables,
-   others don't.
+   others don't.  However SJLJ-based unwinding doesn't have such
+   problem.
 
    This is a problem when GDB calls some non-EH-aware C library code,
    that calls into GDB again through a callback, and that GDB callback
@@ -162,6 +171,9 @@ void (*after_char_processing_hook) (void);
 static void
 gdb_rl_callback_read_char_wrapper (gdb_client_data client_data)
 {
+#ifdef __USING_SJLJ_EXCEPTIONS__
+  gdb_rl_callback_read_char_wrapper();
+#else
   struct gdb_exception gdb_expt = exception_none;
 
   /* C++ exceptions can't normally be thrown across readline (unless
@@ -170,9 +182,7 @@ gdb_rl_callback_read_char_wrapper (gdb_client_data client_data)
      TRY/CATCH, and rethrow the GDB exception once back in GDB.  */
   TRY_SJLJ
     {
-      rl_callback_read_char ();
-      if (after_char_processing_hook)
-	(*after_char_processing_hook) ();
+      gdb_rl_callback_read_char_wrapper();
     }
   CATCH_SJLJ (ex, RETURN_MASK_ALL)
     {
@@ -183,6 +193,15 @@ gdb_rl_callback_read_char_wrapper (gdb_client_data client_data)
   /* Rethrow using the normal EH mechanism.  */
   if (gdb_expt.reason < 0)
     throw_exception (gdb_expt);
+#endif
+}
+
+static void
+gdb_rl_callback_handler_1 (char *rl)
+{
+  struct ui *ui = current_ui;
+
+  ui->input_handler (rl);
 }
 
 /* GDB's readline callback handler.  Calls the current INPUT_HANDLER,
@@ -192,12 +211,14 @@ gdb_rl_callback_read_char_wrapper (gdb_client_data client_data)
 static void
 gdb_rl_callback_handler (char *rl)
 {
+#ifdef __USING_SJLJ_EXCEPTIONS__
+  gdb_rl_callback_handler_1 (rl);
+#else
   struct gdb_exception gdb_rl_expt = exception_none;
-  struct ui *ui = current_ui;
 
   TRY
     {
-      ui->input_handler (rl);
+      gdb_rl_callback_handler_1 (rl);
     }
   CATCH (ex, RETURN_MASK_ALL)
     {
@@ -214,6 +235,7 @@ gdb_rl_callback_handler (char *rl)
      dtors are NOT run automatically.  */
   if (gdb_rl_expt.reason < 0)
     throw_exception_sjlj (gdb_rl_expt);
+#endif
 }
 
 /* Change the function to be invoked every time there is a character
-- 
1.9.1



More information about the Gdb-patches mailing list