From: Tom Tromey Date: Wed, 7 Mar 2018 22:55:01 +0000 (-0700) Subject: Change enable_thread_stack_temporaries to an RAII class X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=fdf07f3aeba5906fec462fba33801c173862f241;p=binutils-gdb.git Change enable_thread_stack_temporaries to an RAII class This started as a patch to change enable_thread_stack_temporaries to be an RAII class, but then I noticed that this code used a VEC, so I went ahead and did a bit more C++-ification, changing stack_temporaries_enabled to a bool and changing stack_temporaries to a std::vector. Regression tested by the buildbot. gdb/ChangeLog 2018-03-08 Tom Tromey * infcall.c (struct call_return_meta_info) : Remove. (get_call_return_value, call_function_by_hand_dummy): Update. * thread.c (disable_thread_stack_temporaries): Remove. (enable_thread_stack_temporaries): Remove. (thread_stack_temporaries_enabled_p): Return bool. (push_thread_stack_temporary, value_in_thread_stack_temporaries) (get_last_thread_stack_temporary): Update. * eval.c (evaluate_subexp): Update. * gdbthread.h (class enable_thread_stack_temporaries): Now a class, not a function. (value_ptr, value_vec): Remove typedefs. (class thread_info) : Now bool. : Now a std::vector. (thread_stack_temporaries_enabled_p) (value_in_thread_stack_temporaries): Return bool. --- diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 114f4515c38..3d9db8b8d68 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,22 @@ +2018-03-08 Tom Tromey + + * infcall.c (struct call_return_meta_info) + : Remove. + (get_call_return_value, call_function_by_hand_dummy): Update. + * thread.c (disable_thread_stack_temporaries): Remove. + (enable_thread_stack_temporaries): Remove. + (thread_stack_temporaries_enabled_p): Return bool. + (push_thread_stack_temporary, value_in_thread_stack_temporaries) + (get_last_thread_stack_temporary): Update. + * eval.c (evaluate_subexp): Update. + * gdbthread.h (class enable_thread_stack_temporaries): Now a + class, not a function. + (value_ptr, value_vec): Remove typedefs. + (class thread_info) : Now bool. + : Now a std::vector. + (thread_stack_temporaries_enabled_p) + (value_in_thread_stack_temporaries): Return bool. + 2018-03-08 Simon Marchi * remote.c (putpkt_binary): Fix omitted bytes reporting. diff --git a/gdb/eval.c b/gdb/eval.c index 4899011a58f..b2032c35f1d 100644 --- a/gdb/eval.c +++ b/gdb/eval.c @@ -69,27 +69,20 @@ struct value * evaluate_subexp (struct type *expect_type, struct expression *exp, int *pos, enum noside noside) { - struct cleanup *cleanups; struct value *retval; - int cleanup_temps = 0; + gdb::optional stack_temporaries; if (*pos == 0 && target_has_execution && exp->language_defn->la_language == language_cplus && !thread_stack_temporaries_enabled_p (inferior_ptid)) - { - cleanups = enable_thread_stack_temporaries (inferior_ptid); - cleanup_temps = 1; - } + stack_temporaries.emplace (inferior_ptid); retval = (*exp->language_defn->la_exp_desc->evaluate_exp) (expect_type, exp, pos, noside); - if (cleanup_temps) - { - if (value_in_thread_stack_temporaries (retval, inferior_ptid)) - retval = value_non_lval (retval); - do_cleanups (cleanups); - } + if (stack_temporaries.has_value () + && value_in_thread_stack_temporaries (retval, inferior_ptid)) + retval = value_non_lval (retval); return retval; } diff --git a/gdb/gdbthread.h b/gdb/gdbthread.h index cffdfb9188e..9b468dbda7b 100644 --- a/gdb/gdbthread.h +++ b/gdb/gdbthread.h @@ -175,10 +175,6 @@ struct thread_suspend_state CORE_ADDR stop_pc; }; -typedef struct value *value_ptr; -DEF_VEC_P (value_ptr); -typedef VEC (value_ptr) value_vec; - /* Base class for target-specific thread data. */ struct private_thread_info { @@ -358,11 +354,11 @@ public: /* Flag which indicates that the stack temporaries should be stored while evaluating expressions. */ - int stack_temporaries_enabled = 0; + bool stack_temporaries_enabled = false; /* Values that are stored as temporaries on stack while evaluating expressions. */ - value_vec *stack_temporaries = NULL; + std::vector stack_temporaries; /* Step-over chain. A thread is in the step-over queue if these are non-NULL. If only a single thread is in the chain, then these @@ -634,15 +630,48 @@ extern void delete_exited_threads (void); int pc_in_thread_step_range (CORE_ADDR pc, struct thread_info *thread); -extern struct cleanup *enable_thread_stack_temporaries (ptid_t ptid); +/* Enable storing stack temporaries for thread with id PTID and + disable and clear the stack temporaries on destruction. */ + +class enable_thread_stack_temporaries +{ +public: + + explicit enable_thread_stack_temporaries (ptid_t ptid) + : m_ptid (ptid) + { + struct thread_info *tp = find_thread_ptid (ptid); + + gdb_assert (tp != NULL); + tp->stack_temporaries_enabled = true; + tp->stack_temporaries.clear (); + } + + ~enable_thread_stack_temporaries () + { + struct thread_info *tp = find_thread_ptid (m_ptid); + + if (tp != NULL) + { + tp->stack_temporaries_enabled = false; + tp->stack_temporaries.clear (); + } + } + + DISABLE_COPY_AND_ASSIGN (enable_thread_stack_temporaries); + +private: + + ptid_t m_ptid; +}; -extern int thread_stack_temporaries_enabled_p (ptid_t ptid); +extern bool thread_stack_temporaries_enabled_p (ptid_t ptid); extern void push_thread_stack_temporary (ptid_t ptid, struct value *v); extern struct value *get_last_thread_stack_temporary (ptid_t); -extern int value_in_thread_stack_temporaries (struct value *, ptid_t); +extern bool value_in_thread_stack_temporaries (struct value *, ptid_t); /* Add TP to the end of its inferior's pending step-over chain. */ diff --git a/gdb/infcall.c b/gdb/infcall.c index b7f4a176db5..54f82ba02c3 100644 --- a/gdb/infcall.c +++ b/gdb/infcall.c @@ -409,9 +409,6 @@ struct call_return_meta_info /* If using a structure return, this is the structure's address. */ CORE_ADDR struct_addr; - - /* Whether stack temporaries are enabled. */ - int stack_temporaries_enabled; }; /* Extract the called function's return value. */ @@ -420,7 +417,7 @@ static struct value * get_call_return_value (struct call_return_meta_info *ri) { struct value *retval = NULL; - int stack_temporaries = thread_stack_temporaries_enabled_p (inferior_ptid); + bool stack_temporaries = thread_stack_temporaries_enabled_p (inferior_ptid); if (TYPE_CODE (ri->value_type) == TYPE_CODE_VOID) retval = allocate_value (ri->value_type); @@ -739,7 +736,7 @@ call_function_by_hand_dummy (struct value *function, ptid_t call_thread_ptid; struct gdb_exception e; char name_buf[RAW_FUNCTION_ADDRESS_SIZE]; - int stack_temporaries = thread_stack_temporaries_enabled_p (inferior_ptid); + bool stack_temporaries = thread_stack_temporaries_enabled_p (inferior_ptid); if (TYPE_CODE (ftype) == TYPE_CODE_PTR) ftype = check_typedef (TYPE_TARGET_TYPE (ftype)); diff --git a/gdb/thread.c b/gdb/thread.c index 36cfcdd6329..a99fbdc1707 100644 --- a/gdb/thread.c +++ b/gdb/thread.c @@ -749,55 +749,16 @@ delete_exited_threads (void) } } -/* Disable storing stack temporaries for the thread whose id is - stored in DATA. */ - -static void -disable_thread_stack_temporaries (void *data) -{ - ptid_t *pd = (ptid_t *) data; - struct thread_info *tp = find_thread_ptid (*pd); - - if (tp != NULL) - { - tp->stack_temporaries_enabled = 0; - VEC_free (value_ptr, tp->stack_temporaries); - } - - xfree (pd); -} - -/* Enable storing stack temporaries for thread with id PTID and return a - cleanup which can disable and clear the stack temporaries. */ - -struct cleanup * -enable_thread_stack_temporaries (ptid_t ptid) -{ - struct thread_info *tp = find_thread_ptid (ptid); - ptid_t *data; - struct cleanup *c; - - gdb_assert (tp != NULL); - - tp->stack_temporaries_enabled = 1; - tp->stack_temporaries = NULL; - data = XNEW (ptid_t); - *data = ptid; - c = make_cleanup (disable_thread_stack_temporaries, data); - - return c; -} - -/* Return non-zero value if stack temporaies are enabled for the thread +/* Return true value if stack temporaies are enabled for the thread with id PTID. */ -int +bool thread_stack_temporaries_enabled_p (ptid_t ptid) { struct thread_info *tp = find_thread_ptid (ptid); if (tp == NULL) - return 0; + return false; else return tp->stack_temporaries_enabled; } @@ -810,29 +771,23 @@ push_thread_stack_temporary (ptid_t ptid, struct value *v) struct thread_info *tp = find_thread_ptid (ptid); gdb_assert (tp != NULL && tp->stack_temporaries_enabled); - VEC_safe_push (value_ptr, tp->stack_temporaries, v); + tp->stack_temporaries.push_back (v); } -/* Return 1 if VAL is among the stack temporaries of the thread - with id PTID. Return 0 otherwise. */ +/* Return true if VAL is among the stack temporaries of the thread + with id PTID. Return false otherwise. */ -int +bool value_in_thread_stack_temporaries (struct value *val, ptid_t ptid) { struct thread_info *tp = find_thread_ptid (ptid); gdb_assert (tp != NULL && tp->stack_temporaries_enabled); - if (!VEC_empty (value_ptr, tp->stack_temporaries)) - { - struct value *v; - int i; + for (struct value *v : tp->stack_temporaries) + if (v == val) + return true; - for (i = 0; VEC_iterate (value_ptr, tp->stack_temporaries, i, v); i++) - if (v == val) - return 1; - } - - return 0; + return false; } /* Return the last of the stack temporaries for thread with id PTID. @@ -845,8 +800,8 @@ get_last_thread_stack_temporary (ptid_t ptid) struct thread_info *tp = find_thread_ptid (ptid); gdb_assert (tp != NULL); - if (!VEC_empty (value_ptr, tp->stack_temporaries)) - lastval = VEC_last (value_ptr, tp->stack_temporaries); + if (!tp->stack_temporaries.empty ()) + lastval = tp->stack_temporaries.back (); return lastval; }