From 3d6e9d2336c9ffcedb10f89631981a23dd518e8e Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Mon, 28 Jan 2019 10:11:10 -0700 Subject: Make exceptions use std::string and be self-managing This changes the exception's "message" member to be a shared_ptr wrapping a std::string. This allows removing the stack of exception messages, because now exceptions will self-destruct when needed. This also adds a noexcept copy constructor and operator= to gdb_exception, plus a "what" method. gdb/ChangeLog 2019-04-08 Tom Tromey * xml-support.c (gdb_xml_parser::parse): Update. * x86-linux-nat.c (x86_linux_nat_target::enable_btrace): Update. * value.c (show_convenience): Update. * unittests/cli-utils-selftests.c (test_number_or_range_parser) (test_parse_flags_qcs): Update. * thread.c (thr_try_catch_cmd): Update. * target.c (target_translate_tls_address): Update. * stack.c (print_frame_arg, read_frame_local, read_frame_arg) (info_frame_command_core, frame_apply_command_count): Update. * rust-exp.y (rust_lex_exception_test): Update. * riscv-tdep.c (riscv_print_one_register_info): Update. * remote.c (remote_target::enable_btrace): Update. * record-btrace.c (record_btrace_enable_warn): Update. * python/py-utils.c (gdbpy_convert_exception): Update. * printcmd.c (do_one_display, print_variable_and_value): Update. * mi/mi-main.c (mi_print_exception): Update. * mi/mi-interp.c (mi_cmd_interpreter_exec): Use SCOPE_EXIT. * mi/mi-cmd-stack.c (list_arg_or_local): Update. * linux-nat.c (linux_nat_target::attach): Update. * linux-fork.c (class scoped_switch_fork_info): Update. * infrun.c (displaced_step_prepare): Update. * infcall.c (call_function_by_hand_dummy): Update. * guile/scm-exception.c (gdbscm_scm_from_gdb_exception): Update. * gnu-v3-abi.c (print_one_vtable): Update. * frame.c (get_prev_frame_always): Update. * f-valprint.c (info_common_command_for_block): Update. * exec.c (try_open_exec_file): Update. * exceptions.c (print_exception, exception_print) (exception_fprintf, exception_print_same): Update. * dwarf2-frame.c (dwarf2_build_frame_info): Update. * dwarf-index-cache.c (index_cache::store) (index_cache::lookup_gdb_index): Update. * darwin-nat.c (maybe_cache_shell): Update. * cp-valprint.c (cp_print_value_fields): Update. * compile/compile-cplus-symbols.c (gcc_cplus_convert_symbol) (gcc_cplus_symbol_address): Update. * compile/compile-c-symbols.c (gcc_convert_symbol) (gcc_symbol_address, generate_c_for_for_one_variable): Update. * common/selftest.c: Update. * common/common-exceptions.h (struct gdb_exception) : Now a std::string. (exception_try_scope_entry, exception_try_scope_exit): Don't declare. (struct exception_try_scope): Remove. (TRY): Don't use exception_try_scope. (struct gdb_exception): Add constructor, operator=. : New method. (struct gdb_exception_RETURN_MASK_ALL) (struct gdb_exception_RETURN_MASK_ERROR) (struct gdb_exception_RETURN_MASK_QUIT): Add constructor. (struct gdb_quit_bad_alloc): Update. * common/common-exceptions.c (exception_none): Change initializer. (struct catcher) : Initialize inline. : Remove member. (current_catcher): Remove. (catchers): New global. (exceptions_state_mc_init): Simplify. (catcher_pop): Remove. (exceptions_state_mc, exceptions_state_mc_catch): Update. (try_scope_depth, exception_try_scope_entry) (exception_try_scope_exit): Remove. (throw_exception_sjlj): Update. (exception_messages, exception_messages_size): Remove. (throw_it): Simplify. (gdb_exception_sliced_copy): Remove. (throw_exception_cxx): Update. * cli/cli-script.c (script_from_file): Update. * breakpoint.c (insert_bp_location, update_breakpoint_locations): Update. * ada-valprint.c (ada_val_print): Update. * ada-lang.c (ada_to_fixed_type_1, ada_exception_name_addr) (create_excep_cond_exprs): Update. gdb/gdbserver/ChangeLog 2019-04-08 Tom Tromey * server.c (handle_btrace_general_set, handle_qxfer_btrace) (handle_qxfer_btrace_conf, detach_or_kill_for_exit_cleanup) (captured_main, main): Update. * gdbreplay.c (main): Update. --- gdb/common/common-exceptions.h | 81 ++++++++++++++++++++++++++++-------------- 1 file changed, 55 insertions(+), 26 deletions(-) (limited to 'gdb/common/common-exceptions.h') diff --git a/gdb/common/common-exceptions.h b/gdb/common/common-exceptions.h index 6cc09ea..54e7404 100644 --- a/gdb/common/common-exceptions.h +++ b/gdb/common/common-exceptions.h @@ -22,6 +22,7 @@ #include #include +#include /* Reasons for calling throw_exceptions(). NOTE: all reason values must be different from zero. enum value 0 is reserved for internal @@ -110,9 +111,47 @@ enum errors { struct gdb_exception { + gdb_exception () + : reason ((enum return_reason) 0), + error (GDB_NO_ERROR) + { + } + + gdb_exception (enum return_reason r, enum errors e) + : reason (r), + error (e) + { + } + + /* The copy constructor exists so that we can mark it "noexcept", + which is a good practice for any sort of exception object. */ + gdb_exception (const gdb_exception &other) noexcept + : reason (other.reason), + error (other.error), + message (other.message) + { + } + + /* The assignment operator exists so that we can mark it "noexcept", + which is a good practice for any sort of exception object. */ + gdb_exception &operator= (const gdb_exception &other) noexcept + { + reason = other.reason; + error = other.error; + message = other.message; + return *this; + } + + /* Return the contents of the exception message, as a C string. The + string remains owned by the exception object. */ + const char *what () const noexcept + { + return message->c_str (); + } + enum return_reason reason; enum errors error; - const char *message; + std::shared_ptr message; }; /* Functions to drive the sjlj-based exceptions state machine. Though @@ -127,8 +166,6 @@ extern int exceptions_state_mc_catch (struct gdb_exception *, int); /* For the C++ try/catch-based TRY/CATCH mechanism. */ -extern void *exception_try_scope_entry (void); -extern void exception_try_scope_exit (void *saved_state); extern void exception_rethrow (void) ATTRIBUTE_NORETURN; /* Macro to wrap up standard try/catch behavior. @@ -178,23 +215,6 @@ extern void exception_rethrow (void) ATTRIBUTE_NORETURN; #define END_CATCH_SJLJ \ } -/* Prevent error/quit during TRY from calling cleanups established - prior to here. This pops out the scope in either case of normal - exit or exception exit. */ -struct exception_try_scope -{ - exception_try_scope () - { - saved_state = exception_try_scope_entry (); - } - ~exception_try_scope () - { - exception_try_scope_exit (saved_state); - } - - void *saved_state; -}; - /* We still need to wrap TRY/CATCH in C++ so that cleanups and C++ exceptions can coexist. @@ -214,7 +234,6 @@ struct exception_try_scope { \ try \ { \ - exception_try_scope exception_try_scope_instance; \ do \ { @@ -236,14 +255,26 @@ struct exception_try_scope struct gdb_exception_RETURN_MASK_ALL : public gdb_exception { + explicit gdb_exception_RETURN_MASK_ALL (const gdb_exception &ex) noexcept + : gdb_exception (ex) + { + } }; struct gdb_exception_RETURN_MASK_ERROR : public gdb_exception_RETURN_MASK_ALL { + explicit gdb_exception_RETURN_MASK_ERROR (const gdb_exception &ex) noexcept + : gdb_exception_RETURN_MASK_ALL (ex) + { + } }; struct gdb_exception_RETURN_MASK_QUIT : public gdb_exception_RETURN_MASK_ALL { + explicit gdb_exception_RETURN_MASK_QUIT (const gdb_exception &ex) noexcept + : gdb_exception_RETURN_MASK_ALL (ex) + { + } }; /* An exception type that inherits from both std::bad_alloc and a gdb @@ -256,12 +287,10 @@ struct gdb_quit_bad_alloc : public gdb_exception_RETURN_MASK_QUIT, public std::bad_alloc { - explicit gdb_quit_bad_alloc (gdb_exception ex) - : std::bad_alloc () + explicit gdb_quit_bad_alloc (const gdb_exception &ex) noexcept + : gdb_exception_RETURN_MASK_QUIT (ex), + std::bad_alloc () { - gdb_exception *self = this; - - *self = ex; } }; -- cgit v1.1