public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom de Vries <tdevries@suse.de>
To: gdb-patches@sourceware.org
Subject: [PATCH 2/2] [gdb] Fix rethrow exception slicing in insert_bp_location
Date: Mon, 24 Oct 2022 10:49:13 +0200	[thread overview]
Message-ID: <20221024084913.19429-3-tdevries@suse.de> (raw)
In-Reply-To: <20221024084913.19429-1-tdevries@suse.de>

The preferred way of rethrowing an exception is by using throw without
expression, because it avoids object slicing of the exception [1].

Fix this in insert_bp_location.

[1] https://en.cppreference.com/w/cpp/language/throw
---
 gdb/breakpoint.c | 28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)

diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index a0653f189b9..a001e78cfb4 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -2647,6 +2647,24 @@ breakpoint_kind (const struct bp_location *bl, CORE_ADDR *addr)
     return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
 }
 
+#define RETHROW_ON_TARGET_CLOSE_ERROR(E)				\
+  do									\
+    {									\
+      if ((E).reason != 0)						\
+	{								\
+	  /* Can't set the breakpoint.  */				\
+									\
+	  if ((E).error == TARGET_CLOSE_ERROR)				\
+	    /* If the target has closed then it will have deleted any	\
+	       breakpoints inserted within the target inferior, as a	\
+	       result any further attempts to interact with the		\
+	       breakpoint objects is not possible.  Just rethrow the	\
+	       error.  Don't use E to rethrow, to prevent object	\
+	       slicing of the exception.  */				\
+	    throw;							\
+	}								\
+    } while (0)
+
 /* Insert a low-level "breakpoint" of some type.  BL is the breakpoint
    location.  Any error messages are printed to TMP_ERROR_STREAM; and
    DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
@@ -2734,6 +2752,7 @@ insert_bp_location (struct bp_location *bl,
 	    }
 	  catch (gdb_exception &e)
 	    {
+	      RETHROW_ON_TARGET_CLOSE_ERROR (e);
 	      bp_excpt = std::move (e);
 	    }
 	}
@@ -2773,6 +2792,7 @@ insert_bp_location (struct bp_location *bl,
 		    }
 		  catch (gdb_exception &e)
 		    {
+		      RETHROW_ON_TARGET_CLOSE_ERROR (e);
 		      bp_excpt = std::move (e);
 		    }
 
@@ -2797,6 +2817,7 @@ insert_bp_location (struct bp_location *bl,
 		}
 	      catch (gdb_exception &e)
 		{
+		  RETHROW_ON_TARGET_CLOSE_ERROR (e);
 		  bp_excpt = std::move (e);
 		}
 	    }
@@ -2811,13 +2832,6 @@ insert_bp_location (struct bp_location *bl,
       if (bp_excpt.reason != 0)
 	{
 	  /* Can't set the breakpoint.  */
-
-	  /* If the target has closed then it will have deleted any
-	     breakpoints inserted within the target inferior, as a result
-	     any further attempts to interact with the breakpoint objects
-	     is not possible.  Just rethrow the error.  */
-	  if (bp_excpt.error == TARGET_CLOSE_ERROR)
-	    throw bp_excpt;
 	  gdb_assert (bl->owner != nullptr);
 
 	  /* In some cases, we might not be able to insert a
-- 
2.35.3


  parent reply	other threads:[~2022-10-24  8:49 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-24  8:49 [PATCH 0/2] [gdb] Fix rethrow exception slicing Tom de Vries
2022-10-24  8:49 ` [PATCH 1/2] [gdb] Fix rethrow exception slicing in pretty_print_insn Tom de Vries
2022-10-24  8:49 ` Tom de Vries [this message]
2022-10-24 16:36   ` [PATCH 2/2] [gdb] Fix rethrow exception slicing in insert_bp_location Pedro Alves
2022-10-24 16:43     ` Tom de Vries
2022-10-24 16:48       ` Simon Marchi
2022-10-25  7:08         ` Tom de Vries
2022-10-24 16:51       ` Pedro Alves
2022-10-25  7:14         ` Tom de Vries
2022-10-25  9:26           ` Pedro Alves
2022-10-24 11:20 ` [PATCH 0/2] [gdb] Fix rethrow exception slicing Andrew Burgess

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221024084913.19429-3-tdevries@suse.de \
    --to=tdevries@suse.de \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).