Commit Graph

297 Commits

Author SHA1 Message Date
Pedro Alves 5018ce90c1 Make target_ops::has_execution take an 'inferior *' instead of a ptid_t
With the multi-target work, each inferior will have its own target
stack, so to call a target method, we'll need to make sure that the
inferior in question is the current one, otherwise target->beneath()
calls will find the target beneath in the wrong inferior.

In some places, it's much more convenient to be able to check whether
an inferior has execution without having to switch to it in order to
call target_has_execution on the right inferior/target stack, to avoid
side effects with switching inferior/thread/program space.

The current target_ops::has_execution method takes a ptid_t as
parameter, which, in a multi-target world, isn't sufficient to
identify the target.  This patch prepares to address that, by changing
the parameter to an inferior pointer instead.  From the inferior,
we'll be able to query its target stack to tell which target is
beneath.

Also adds a new inferior::has_execution() method to make callers a bit
more natural to read.

gdb/ChangeLog:
2020-01-10  Pedro Alves  <palves@redhat.com>

	* corelow.c (core_target::has_execution): Change parameter type to
	inferior pointer.
	* inferior.c (number_of_live_inferiors): Use
	inferior::has_execution instead of target_has_execution_1.
	* inferior.h (inferior::has_execution): New.
	* linux-thread-db.c (thread_db_target::update_thread_list): Use
	inferior::has_execution instead of target_has_execution_1.
	* process-stratum-target.c
	(process_stratum_target::has_execution): Change parameter type to
	inferior pointer.  Check the inferior's PID instead of
	inferior_ptid.
	* process-stratum-target.h
	(process_stratum_target::has_execution): Change parameter type to
	inferior pointer.
	* record-full.c (record_full_core_target::has_execution): Change
	parameter type to inferior pointer.
	* target.c (target_has_execution_1): Change parameter type to
	inferior pointer.
	(target_has_execution_current): Adjust.
	* target.h (target_ops::has_execution): Change parameter type to
	inferior pointer.
	(target_has_execution_1): Change parameter type to inferior
	pointer.  Change return type to bool.
	* tracefile.h (tracefile_target::has_execution): Change parameter
	type to inferior pointer.
2020-01-10 20:05:44 +00:00
Joel Brobecker b811d2c292 Update copyright year range in all GDB files.
gdb/ChangeLog:

        Update copyright year range in all GDB files.
2020-01-01 10:20:53 +04:00
Simon Marchi fe3adccffd Remove info_terminal_command declaration, make definition static
The info_terminal_command declaration in inflow.h does not match the
current definition.  It is not needed anyway, as info_terminal_command
is only used locally, so remove it and make the definition static.

gdb/ChangeLog:

	* inferior.h (info_terminal_command): Remove declaration.
	* inflow.c (info_terminal_command): Make static.

Change-Id: I22c3fcc44244e3cf877b5e27eff189af11c39503
2019-11-26 14:29:20 -05:00
Tom Tromey 80fd282641 Fix type of startup_with_shell in gdbserver
startup_with_shell was changed to be of "bool" type, but I noticed
that the definition in gdbserver disagreed.  This disagreement caused
some regressions on a big-endian machine.

This patch removes the redundant declaration and definition of
startup_with_shell and ensures that such clashes will be diagnosed.

This moves the declaration to common-inferior.h, and introduces a new
common-inferior.c, as suggested by Pedro.

gdb/ChangeLog
2019-10-02  Tom Tromey  <tromey@adacore.com>

	* Makefile.in (COMMON_SFILES): Add common-inferior.c.
	* gdbsupport/common-inferior.c: New file.
	* infcmd.c (startup_with_shell): Don't define.
	* nat/fork-inferior.h (startup_with_shell): Don't declare.
	* gdbsupport/common-inferior.h (startup_with_shell): Declare.
	* inferior.h (startup_with_shell): Don't declare.

gdb/gdbserver/ChangeLog
2019-10-02  Tom Tromey  <tromey@adacore.com>

	* Makefile.in (SFILES): Add common-inferior.c.
	(OBS): Add common-inferior.o.
	* server.c (startup_with_shell): Don't define.
2019-10-02 13:43:41 -06:00
Christian Biesinger 491144b5e2 Change boolean options to bool instead of int
This is for add_setshow_boolean_cmd as well as the gdb::option interface.

gdb/ChangeLog:

2019-09-17  Christian Biesinger  <cbiesinger@google.com>

	* ada-lang.c (ada_ignore_descriptive_types_p): Change to bool.
	(print_signatures): Likewise.
	(trust_pad_over_xvs): Likewise.
	* arch/aarch64-insn.c (aarch64_debug): Likewise.
	* arch/aarch64-insn.h (aarch64_debug): Likewise.
	* arm-linux-nat.c (arm_apcs_32): Likewise.
	* arm-linux-tdep.c (arm_apcs_32): Likewise.
	* arm-nbsd-nat.c (arm_apcs_32): Likewise.
	* arm-tdep.c (arm_debug): Likewise.
	(arm_apcs_32): Likewise.
	* auto-load.c (debug_auto_load): Likewise.
	(auto_load_gdb_scripts): Likewise.
	(global_auto_load): Likewise.
	(auto_load_local_gdbinit): Likewise.
	(auto_load_local_gdbinit_loaded): Likewise.
	* auto-load.h (global_auto_load): Likewise.
	(auto_load_local_gdbinit): Likewise.
	(auto_load_local_gdbinit_loaded): Likewise.
	* breakpoint.c (disconnected_dprintf): Likewise.
	(breakpoint_proceeded): Likewise.
	(automatic_hardware_breakpoints): Likewise.
	(always_inserted_mode): Likewise.
	(target_exact_watchpoints): Likewise.
	(_initialize_breakpoint): Update.
	* breakpoint.h (target_exact_watchpoints): Change to bool.
	* btrace.c (maint_btrace_pt_skip_pad): Likewise.
	* cli/cli-cmds.c (trace_commands): Likewise.
	* cli/cli-cmds.h (trace_commands): Likewise.
	* cli/cli-decode.c (add_setshow_boolean_cmd): Change int* argument
	to bool*.
	* cli/cli-logging.c (logging_overwrite): Change to bool.
	(logging_redirect): Likewise.
	(debug_redirect): Likewise.
	* cli/cli-option.h (option_def) <boolean>: Change return type to bool*.
	(struct boolean_option_def) <get_var_address_cb_>: Change return type
	to bool.
	<boolean_option_def>: Update.
	(struct flag_option_def): Change default type of Context to bool
	from int.
	<flag_option_def>: Change return type of var_address_cb_ to bool*.
	* cli/cli-setshow.c (do_set_command): Cast to bool* instead of int*.
	(get_setshow_command_value_string): Likewise.
	* cli/cli-style.c (cli_styling): Change to bool.
	(source_styling): Likewise.
	* cli/cli-style.h (source_styling): Likewise.
	(cli_styling): Likewise.
	* cli/cli-utils.h (struct qcs_flags) <quiet, cont, silent>: Change
	to bool.
	* command.h (var_types): Update comment.
	(add_setshow_boolean_cmd): Change int* var argument to bool*.
	* compile/compile-cplus-types.c (debug_compile_cplus_types): Change to
	bool.
	(debug_compile_cplus_scopes): Likewise.
	* compile/compile-internal.h (compile_debug): Likewise.
	* compile/compile.c (compile_debug): Likewise.
	(struct compile_options) <raw>: Likewise.
	* cp-support.c (catch_demangler_crashes): Likewise.
	* cris-tdep.c (usr_cmd_cris_version_valid): Likewise.
	(usr_cmd_cris_dwarf2_cfi): Likewise.
	* csky-tdep.c (csky_debug): Likewise.
	* darwin-nat.c (enable_mach_exceptions): Likewise.
	* dcache.c (dcache_enabled_p): Likewise.
	* defs.h (info_verbose): Likewise.
	* demangle.c (demangle): Likewise.
	(asm_demangle): Likewise.
	* dwarf-index-cache.c (debug_index_cache): Likewise.
	* dwarf2-frame.c (dwarf2_frame_unwinders_enabled_p): Likewise.
	* dwarf2-frame.h (dwarf2_frame_unwinders_enabled_p): Likewise.
	* dwarf2read.c (check_physname): Likewise.
	(use_deprecated_index_sections): Likewise.
	(dwarf_always_disassemble): Likewise.
	* eval.c (overload_resolution): Likewise.
	* event-top.c (set_editing_cmd_var): Likewise.
	(exec_done_display_p): Likewise.
	* event-top.h (set_editing_cmd_var): Likewise.
	(exec_done_display_p): Likewise.
	* exec.c (write_files): Likewise.
	* fbsd-nat.c (debug_fbsd_lwp): Likewise
	(debug_fbsd_nat): Likewise.
	* frame.h (struct frame_print_options) <print_raw_frame_arguments>:
	Likewise.
	(struct set_backtrace_options) <backtrace_past_main>: Likewise.
	<backtrace_past_entry> Likewise.
	* gdb-demangle.h (demangle): Likewise.
	(asm_demangle): Likewise.
	* gdb_bfd.c (bfd_sharing): Likewise.
	* gdbcore.h (write_files): Likewise.
	* gdbsupport/common-debug.c (show_debug_regs): Likewise.
	* gdbsupport/common-debug.h (show_debug_regs): Likewise.
	* gdbthread.h (print_thread_events): Likewise.
	* gdbtypes.c (opaque_type_resolution): Likewise.
	(strict_type_checking): Likewise.
	* gnu-nat.c (gnu_debug_flag): Likewise.
	* guile/scm-auto-load.c (auto_load_guile_scripts): Likewise.
	* guile/scm-param.c (pascm_variable): Add boolval.
	(add_setshow_generic): Update.
	(pascm_param_value): Update.
	(pascm_set_param_value_x): Update.
	* hppa-tdep.c (hppa_debug): Change to bool..
	* infcall.c (may_call_functions_p): Likewise.
	(coerce_float_to_double_p): Likewise.
	(unwind_on_signal_p): Likewise.
	(unwind_on_terminating_exception_p): Likewise.
	* infcmd.c (startup_with_shell): Likewise.
	* inferior.c (print_inferior_events): Likewise.
	* inferior.h (startup_with_shell): Likewise.
	(print_inferior_events): Likewise.
	* infrun.c (step_stop_if_no_debug): Likewise.
	(detach_fork): Likewise.
	(debug_displaced): Likewise.
	(disable_randomization): Likewise.
	(non_stop): Likewise.
	(non_stop_1): Likewise.
	(observer_mode): Likewise.
	(observer_mode_1): Likewise.
	(set_observer_mode): Update.
	(sched_multi): Change to bool.
	* infrun.h (debug_displaced): Likewise.
	(sched_multi): Likewise.
	(step_stop_if_no_debug): Likewise.
	(non_stop): Likewise.
	(disable_randomization): Likewise.
	* linux-tdep.c (use_coredump_filter): Likewise.
	(dump_excluded_mappings): Likewise.
	* linux-thread-db.c (auto_load_thread_db): Likewise.
	(check_thread_db_on_load): Likewise.
	* main.c (captured_main_1): Update.
	* maint-test-options.c (struct test_options_opts) <flag_opt, xx1_opt,
	xx2_opt, boolean_opt>: Change to bool.
	* maint-test-settings.c (maintenance_test_settings_boolean): Likewise.
	* maint.c (maintenance_profile_p): Likewise.
	(per_command_time): Likewise.
	(per_command_space): Likewise.
	(per_command_symtab): Likewise.
	* memattr.c (inaccessible_by_default): Likewise.
	* mi/mi-main.c (mi_async): Likewise.
	(mi_async_1): Likewise.
	* mips-tdep.c (mips64_transfers_32bit_regs_p): Likewise.
	* nat/fork-inferior.h (startup_with_shell): Likewise.
	* nat/linux-namespaces.c (debug_linux_namespaces): Likewise.
	* nat/linux-namespaces.h (debug_linux_namespaces): Likewise.
	* nios2-tdep.c (nios2_debug): Likewise.
	* or1k-tdep.c (or1k_debug): Likewise.
	* parse.c (parser_debug): Likewise.
	* parser-defs.h (parser_debug): Likewise.
	* printcmd.c (print_symbol_filename): Likewise.
	* proc-api.c (procfs_trace): Likewise.
	* python/py-auto-load.c (auto_load_python_scripts): Likewise.
	* python/py-param.c (union parmpy_variable): Add "bool boolval" field.
	(set_parameter_value): Update.
	(add_setshow_generic): Update.
	* python/py-value.c (copy_py_bool_obj): Change argument from int*
	to bool*.
	* python/python.c (gdbpy_parameter_value): Cast to bool* instead of
	int*.
	* ravenscar-thread.c (ravenscar_task_support): Change to bool.
	* record-btrace.c (record_btrace_target::store_registers): Update.
	* record-full.c (record_full_memory_query): Change to bool.
	(record_full_stop_at_limit): Likewise.
	* record-full.h (record_full_memory_query): Likewise.
	* remote-notif.c (notif_debug): Likewise.
	* remote-notif.h (notif_debug): Likewise.
	* remote.c (use_range_stepping): Likewise.
	(interrupt_on_connect): Likewise.
	(remote_break): Likewise.
	* ser-tcp.c (tcp_auto_retry): Likewise.
	* ser-unix.c (serial_hwflow): Likewise.
	* skip.c (debug_skip): Likewise.
	* solib-aix.c (solib_aix_debug): Likewise.
	* spu-tdep.c (spu_stop_on_load_p): Likewise.
	(spu_auto_flush_cache_p): Likewise.
	* stack.c (struct backtrace_cmd_options) <full, no_filters, hide>:
	Likewise.
	(struct info_print_options) <quiet>: Likewise.
	* symfile-debug.c (debug_symfile): Likewise.
	* symfile.c (auto_solib_add): Likewise.
	(separate_debug_file_debug): Likewise.
	* symfile.h (auto_solib_add): Likewise.
	(separate_debug_file_debug): Likewise.
	* symtab.c (basenames_may_differ): Likewise.
	(struct filename_partial_match_opts) <dirname, basename>: Likewise.
	(struct info_print_options) <quiet, exclude_minsyms>: Likewise.
	(struct info_types_options) <quiet>: Likewise.
	* symtab.h (demangle): Likewise.
	(basenames_may_differ): Likewise.
	* target-dcache.c (stack_cache_enabled_1): Likewise.
	(code_cache_enabled_1): Likewise.
	* target.c (trust_readonly): Likewise.
	(may_write_registers): Likewise.
	(may_write_memory): Likewise.
	(may_insert_breakpoints): Likewise.
	(may_insert_tracepoints): Likewise.
	(may_insert_fast_tracepoints): Likewise.
	(may_stop): Likewise.
	(auto_connect_native_target): Likewise.
	(target_stop_and_wait): Update.
	(target_async_permitted): Change to bool.
	(target_async_permitted_1): Likewise.
	(may_write_registers_1): Likewise.
	(may_write_memory_1): Likewise.
	(may_insert_breakpoints_1): Likewise.
	(may_insert_tracepoints_1): Likewise.
	(may_insert_fast_tracepoints_1): Likewise.
	(may_stop_1): Likewise.
	* target.h (target_async_permitted): Likewise.
	(may_write_registers): Likewise.
	(may_write_memory): Likewise.
	(may_insert_breakpoints): Likewise.
	(may_insert_tracepoints): Likewise.
	(may_insert_fast_tracepoints): Likewise.
	(may_stop): Likewise.
	* thread.c (struct info_threads_opts) <show_global_ids>: Likewise.
	(make_thread_apply_all_options_def_group): Change argument from int*
	to bool*.
	(thread_apply_all_command): Update.
	(print_thread_events): Change to bool.
	* top.c (confirm): Likewise.
	(command_editing_p): Likewise.
	(history_expansion_p): Likewise.
	(write_history_p): Likewise.
	(info_verbose): Likewise.
	* top.h (confirm): Likewise.
	(history_expansion_p): Likewise.
	* tracepoint.c (disconnected_tracing): Likewise.
	(circular_trace_buffer): Likewise.
	* typeprint.c (print_methods): Likewise.
	(print_typedefs): Likewise.
	* utils.c (debug_timestamp): Likewise.
	(sevenbit_strings): Likewise.
	(pagination_enabled): Likewise.
	* utils.h (sevenbit_strings): Likewise.
	(pagination_enabled): Likewise.
	* valops.c (overload_resolution): Likewise.
	* valprint.h (struct value_print_options) <prettyformat_arrays,
	prettyformat_structs, vtblprint, unionprint, addressprint, objectprint,
	stop_print_at_null, print_array_indexes, deref_ref, static_field_print,
	pascal_static_field_print, raw, summary, symbol_print, finish_print>:
	Likewise.
	* windows-nat.c (new_console): Likewise.
	(cygwin_exceptions): Likewise.
	(new_group): Likewise.
	(debug_exec): Likewise.
	(debug_events): Likewise.
	(debug_memory): Likewise.
	(debug_exceptions): Likewise.
	(useshell): Likewise.
	* windows-tdep.c (maint_display_all_tib): Likewise.
	* xml-support.c (debug_xml): Likewise.
2019-09-18 09:35:12 +09:00
Tom Tromey 268a13a5a3 Rename common to gdbsupport
This is the next patch in the ongoing series to move gdbsever to the
top level.

This patch just renames the "common" directory.  The idea is to do
this move in two parts: first rename the directory (this patch), then
move the directory to the top.  This approach makes the patches a bit
more tractable.

I chose the name "gdbsupport" for the directory.  However, as this
patch was largely written by sed, we could pick a new name without too
much difficulty.

Tested by the buildbot.

gdb/ChangeLog
2019-07-09  Tom Tromey  <tom@tromey.com>

	* contrib/ari/gdb_ari.sh: Change common to gdbsupport.
	* configure: Rebuild.
	* configure.ac: Change common to gdbsupport.
	* gdbsupport: Rename from common.
	* acinclude.m4: Change common to gdbsupport.
	* Makefile.in (CONFIG_SRC_SUBDIR, COMMON_SFILES)
	(HFILES_NO_SRCDIR, stamp-version, ALLDEPFILES): Change common to
	gdbsupport.
	* aarch64-tdep.c, ada-lang.c, ada-lang.h, agent.c, alloc.c,
	amd64-darwin-tdep.c, amd64-dicos-tdep.c, amd64-fbsd-nat.c,
	amd64-fbsd-tdep.c, amd64-linux-nat.c, amd64-linux-tdep.c,
	amd64-nbsd-tdep.c, amd64-obsd-tdep.c, amd64-sol2-tdep.c,
	amd64-tdep.c, amd64-windows-tdep.c, arch-utils.c,
	arch/aarch64-insn.c, arch/aarch64.c, arch/aarch64.h, arch/amd64.c,
	arch/amd64.h, arch/arm-get-next-pcs.c, arch/arm-linux.c,
	arch/arm.c, arch/i386.c, arch/i386.h, arch/ppc-linux-common.c,
	arch/riscv.c, arch/riscv.h, arch/tic6x.c, arm-tdep.c, auto-load.c,
	auxv.c, ax-gdb.c, ax-general.c, ax.h, breakpoint.c, breakpoint.h,
	btrace.c, btrace.h, build-id.c, build-id.h, c-lang.h, charset.c,
	charset.h, cli/cli-cmds.c, cli/cli-cmds.h, cli/cli-decode.c,
	cli/cli-dump.c, cli/cli-option.h, cli/cli-script.c,
	coff-pe-read.c, command.h, compile/compile-c-support.c,
	compile/compile-c.h, compile/compile-cplus-symbols.c,
	compile/compile-cplus-types.c, compile/compile-cplus.h,
	compile/compile-loc2c.c, compile/compile.c, completer.c,
	completer.h, contrib/ari/gdb_ari.sh, corefile.c, corelow.c,
	cp-support.c, cp-support.h, cp-valprint.c, csky-tdep.c, ctf.c,
	darwin-nat.c, debug.c, defs.h, disasm-selftests.c, disasm.c,
	disasm.h, dtrace-probe.c, dwarf-index-cache.c,
	dwarf-index-cache.h, dwarf-index-write.c, dwarf2-frame.c,
	dwarf2expr.c, dwarf2loc.c, dwarf2read.c, event-loop.c,
	event-top.c, exceptions.c, exec.c, extension.h, fbsd-nat.c,
	features/aarch64-core.c, features/aarch64-fpu.c,
	features/aarch64-pauth.c, features/aarch64-sve.c,
	features/i386/32bit-avx.c, features/i386/32bit-avx512.c,
	features/i386/32bit-core.c, features/i386/32bit-linux.c,
	features/i386/32bit-mpx.c, features/i386/32bit-pkeys.c,
	features/i386/32bit-segments.c, features/i386/32bit-sse.c,
	features/i386/64bit-avx.c, features/i386/64bit-avx512.c,
	features/i386/64bit-core.c, features/i386/64bit-linux.c,
	features/i386/64bit-mpx.c, features/i386/64bit-pkeys.c,
	features/i386/64bit-segments.c, features/i386/64bit-sse.c,
	features/i386/x32-core.c, features/riscv/32bit-cpu.c,
	features/riscv/32bit-csr.c, features/riscv/32bit-fpu.c,
	features/riscv/64bit-cpu.c, features/riscv/64bit-csr.c,
	features/riscv/64bit-fpu.c, features/tic6x-c6xp.c,
	features/tic6x-core.c, features/tic6x-gp.c, filename-seen-cache.h,
	findcmd.c, findvar.c, fork-child.c, gcore.c, gdb_bfd.c, gdb_bfd.h,
	gdb_proc_service.h, gdb_regex.c, gdb_select.h, gdb_usleep.c,
	gdbarch-selftests.c, gdbthread.h, gdbtypes.h, gnu-nat.c,
	go32-nat.c, guile/guile.c, guile/scm-ports.c,
	guile/scm-safe-call.c, guile/scm-type.c, i386-fbsd-nat.c,
	i386-fbsd-tdep.c, i386-go32-tdep.c, i386-linux-nat.c,
	i386-linux-tdep.c, i386-tdep.c, i387-tdep.c,
	ia64-libunwind-tdep.c, ia64-linux-nat.c, inf-child.c,
	inf-ptrace.c, infcall.c, infcall.h, infcmd.c, inferior-iter.h,
	inferior.c, inferior.h, inflow.c, inflow.h, infrun.c, infrun.h,
	inline-frame.c, language.h, linespec.c, linux-fork.c, linux-nat.c,
	linux-tdep.c, linux-thread-db.c, location.c, machoread.c,
	macrotab.h, main.c, maint.c, maint.h, memattr.c, memrange.h,
	mi/mi-cmd-break.h, mi/mi-cmd-env.c, mi/mi-cmd-stack.c,
	mi/mi-cmd-var.c, mi/mi-interp.c, mi/mi-main.c, mi/mi-parse.h,
	minsyms.c, mips-linux-tdep.c, namespace.h,
	nat/aarch64-linux-hw-point.c, nat/aarch64-linux-hw-point.h,
	nat/aarch64-linux.c, nat/aarch64-sve-linux-ptrace.c,
	nat/amd64-linux-siginfo.c, nat/fork-inferior.c,
	nat/linux-btrace.c, nat/linux-btrace.h, nat/linux-namespaces.c,
	nat/linux-nat.h, nat/linux-osdata.c, nat/linux-personality.c,
	nat/linux-procfs.c, nat/linux-ptrace.c, nat/linux-ptrace.h,
	nat/linux-waitpid.c, nat/mips-linux-watch.c,
	nat/mips-linux-watch.h, nat/ppc-linux.c, nat/x86-dregs.c,
	nat/x86-dregs.h, nat/x86-linux-dregs.c, nat/x86-linux.c,
	nto-procfs.c, nto-tdep.c, objfile-flags.h, objfiles.c, objfiles.h,
	obsd-nat.c, observable.h, osdata.c, p-valprint.c, parse.c,
	parser-defs.h, ppc-linux-nat.c, printcmd.c, probe.c, proc-api.c,
	procfs.c, producer.c, progspace.h, psymtab.h,
	python/py-framefilter.c, python/py-inferior.c, python/py-ref.h,
	python/py-type.c, python/python.c, record-btrace.c, record-full.c,
	record.c, record.h, regcache-dump.c, regcache.c, regcache.h,
	remote-fileio.c, remote-fileio.h, remote-sim.c, remote.c,
	riscv-tdep.c, rs6000-aix-tdep.c, rust-exp.y, s12z-tdep.c,
	selftest-arch.c, ser-base.c, ser-event.c, ser-pipe.c, ser-tcp.c,
	ser-unix.c, skip.c, solib-aix.c, solib-target.c, solib.c,
	source-cache.c, source.c, source.h, sparc-nat.c, spu-linux-nat.c,
	stack.c, stap-probe.c, symfile-add-flags.h, symfile.c, symfile.h,
	symtab.c, symtab.h, target-descriptions.c, target-descriptions.h,
	target-memory.c, target.c, target.h, target/waitstatus.c,
	target/waitstatus.h, thread-iter.h, thread.c, tilegx-tdep.c,
	top.c, top.h, tracefile-tfile.c, tracefile.c, tracepoint.c,
	tracepoint.h, tui/tui-io.c, ui-file.c, ui-out.h,
	unittests/array-view-selftests.c,
	unittests/child-path-selftests.c, unittests/cli-utils-selftests.c,
	unittests/common-utils-selftests.c,
	unittests/copy_bitwise-selftests.c, unittests/environ-selftests.c,
	unittests/format_pieces-selftests.c,
	unittests/function-view-selftests.c,
	unittests/lookup_name_info-selftests.c,
	unittests/memory-map-selftests.c, unittests/memrange-selftests.c,
	unittests/mkdir-recursive-selftests.c,
	unittests/observable-selftests.c,
	unittests/offset-type-selftests.c, unittests/optional-selftests.c,
	unittests/parse-connection-spec-selftests.c,
	unittests/ptid-selftests.c, unittests/rsp-low-selftests.c,
	unittests/scoped_fd-selftests.c,
	unittests/scoped_mmap-selftests.c,
	unittests/scoped_restore-selftests.c,
	unittests/string_view-selftests.c, unittests/style-selftests.c,
	unittests/tracepoint-selftests.c, unittests/unpack-selftests.c,
	unittests/utils-selftests.c, unittests/xml-utils-selftests.c,
	utils.c, utils.h, valarith.c, valops.c, valprint.c, value.c,
	value.h, varobj.c, varobj.h, windows-nat.c, x86-linux-nat.c,
	xml-support.c, xml-support.h, xml-tdesc.h, xstormy16-tdep.c,
	xtensa-linux-nat.c, dwarf2read.h: Change common to gdbsupport.

gdb/gdbserver/ChangeLog
2019-07-09  Tom Tromey  <tom@tromey.com>

	* configure: Rebuild.
	* configure.ac: Change common to gdbsupport.
	* acinclude.m4: Change common to gdbsupport.
	* Makefile.in (SFILES, OBS, GDBREPLAY_OBS, IPA_OBJS)
	(version-generated.c, gdbsupport/%-ipa.o, gdbsupport/%.o): Change
	common to gdbsupport.
	* ax.c, event-loop.c, fork-child.c, gdb_proc_service.h,
	gdbreplay.c, gdbthread.h, hostio-errno.c, hostio.c, i387-fp.c,
	inferiors.c, inferiors.h, linux-aarch64-tdesc-selftest.c,
	linux-amd64-ipa.c, linux-i386-ipa.c, linux-low.c,
	linux-tic6x-low.c, linux-x86-low.c, linux-x86-tdesc-selftest.c,
	linux-x86-tdesc.c, lynx-i386-low.c, lynx-low.c, mem-break.h,
	nto-x86-low.c, regcache.c, regcache.h, remote-utils.c, server.c,
	server.h, spu-low.c, symbol.c, target.h, tdesc.c, tdesc.h,
	thread-db.c, tracepoint.c, win32-i386-low.c, win32-low.c: Change
	common to gdbsupport.
2019-07-09 07:45:38 -06:00
Philippe Waroquiers ee3c5f8968 Fix GDB crash when registers cannot be modified.
This crash was detected when using GDB with the valgrind gdbserver.
To reproduce:

valgrind sleep 10000

In another window:
gdb
target remote | vgdb
p printf("make sleep print something\n")
=>
terminate called after throwing an instance of 'gdb_exception_error'
Aborted

The problem is that the valgrind gdbserver does not allow to change
registers when the inferior is blocked in a system call.
GDB then raises an exception.  The exception causes the destructor
of
 typedef std::unique_ptr<infcall_suspend_state, infcall_suspend_state_deleter>
    infcall_suspend_state_up;
to be called.  This destructor itself tries to restore the value of
the registers, and fails similarly.  We must catch the exception in
the destructor to avoid crashing GDB.
If the destructor encounters a problem, no warning is produced if
there is an uncaught exception, as in this case, the user will already
be informed of a problem via this exception.

With this change, no crash anymore, and all the valgrind 3.15 tests
pass succesfully.

gdb/ChangeLog
2019-04-19  Philippe Waroquiers  <philippe.waroquiers@skynet.be>

	* inferior.h (struct infcall_suspend_state_deleter):
	Catch exception in destructor to avoid crash.
2019-04-19 14:11:51 +02:00
Tom Tromey 09e3c4ca13 Update two cleanup comments
This updates another couple of comments to remove mentions of
cleanups.

gdb/ChangeLog
2019-03-06  Tom Tromey  <tom@tromey.com>

	* inferior.h (class inferior): Update comment.
	* gdbthread.h (class thread_info): Update comment.
2019-03-06 14:05:28 -07:00
Tom Tromey 0747795c08 Normalize includes to use common/
This changes all includes to use the form "common/filename.h" rather
than just "filename.h".  This was written by a script.

gdb/ChangeLog
2019-01-25  Tom Tromey  <tom@tromey.com>

	* xtensa-linux-nat.c: Fix common/ includes.
	* xml-support.h: Fix common/ includes.
	* xml-support.c: Fix common/ includes.
	* x86-linux-nat.c: Fix common/ includes.
	* windows-nat.c: Fix common/ includes.
	* varobj.h: Fix common/ includes.
	* varobj.c: Fix common/ includes.
	* value.c: Fix common/ includes.
	* valops.c: Fix common/ includes.
	* utils.c: Fix common/ includes.
	* unittests/xml-utils-selftests.c: Fix common/ includes.
	* unittests/utils-selftests.c: Fix common/ includes.
	* unittests/unpack-selftests.c: Fix common/ includes.
	* unittests/tracepoint-selftests.c: Fix common/ includes.
	* unittests/style-selftests.c: Fix common/ includes.
	* unittests/string_view-selftests.c: Fix common/ includes.
	* unittests/scoped_restore-selftests.c: Fix common/ includes.
	* unittests/scoped_mmap-selftests.c: Fix common/ includes.
	* unittests/scoped_fd-selftests.c: Fix common/ includes.
	* unittests/rsp-low-selftests.c: Fix common/ includes.
	* unittests/parse-connection-spec-selftests.c: Fix common/
	includes.
	* unittests/optional-selftests.c: Fix common/ includes.
	* unittests/offset-type-selftests.c: Fix common/ includes.
	* unittests/observable-selftests.c: Fix common/ includes.
	* unittests/mkdir-recursive-selftests.c: Fix common/ includes.
	* unittests/memrange-selftests.c: Fix common/ includes.
	* unittests/memory-map-selftests.c: Fix common/ includes.
	* unittests/lookup_name_info-selftests.c: Fix common/ includes.
	* unittests/function-view-selftests.c: Fix common/ includes.
	* unittests/environ-selftests.c: Fix common/ includes.
	* unittests/copy_bitwise-selftests.c: Fix common/ includes.
	* unittests/common-utils-selftests.c: Fix common/ includes.
	* unittests/cli-utils-selftests.c: Fix common/ includes.
	* unittests/array-view-selftests.c: Fix common/ includes.
	* ui-file.c: Fix common/ includes.
	* tui/tui-io.c: Fix common/ includes.
	* tracepoint.h: Fix common/ includes.
	* tracepoint.c: Fix common/ includes.
	* tracefile-tfile.c: Fix common/ includes.
	* top.h: Fix common/ includes.
	* top.c: Fix common/ includes.
	* thread.c: Fix common/ includes.
	* target/waitstatus.h: Fix common/ includes.
	* target/waitstatus.c: Fix common/ includes.
	* target.h: Fix common/ includes.
	* target.c: Fix common/ includes.
	* target-memory.c: Fix common/ includes.
	* target-descriptions.c: Fix common/ includes.
	* symtab.h: Fix common/ includes.
	* symfile.c: Fix common/ includes.
	* stap-probe.c: Fix common/ includes.
	* spu-linux-nat.c: Fix common/ includes.
	* sparc-nat.c: Fix common/ includes.
	* source.c: Fix common/ includes.
	* solib.c: Fix common/ includes.
	* solib-target.c: Fix common/ includes.
	* ser-unix.c: Fix common/ includes.
	* ser-tcp.c: Fix common/ includes.
	* ser-pipe.c: Fix common/ includes.
	* ser-base.c: Fix common/ includes.
	* selftest-arch.c: Fix common/ includes.
	* s12z-tdep.c: Fix common/ includes.
	* rust-exp.y: Fix common/ includes.
	* rs6000-aix-tdep.c: Fix common/ includes.
	* riscv-tdep.c: Fix common/ includes.
	* remote.c: Fix common/ includes.
	* remote-notif.h: Fix common/ includes.
	* remote-fileio.h: Fix common/ includes.
	* remote-fileio.c: Fix common/ includes.
	* regcache.h: Fix common/ includes.
	* regcache.c: Fix common/ includes.
	* record-btrace.c: Fix common/ includes.
	* python/python.c: Fix common/ includes.
	* python/py-type.c: Fix common/ includes.
	* python/py-inferior.c: Fix common/ includes.
	* progspace.h: Fix common/ includes.
	* producer.c: Fix common/ includes.
	* procfs.c: Fix common/ includes.
	* proc-api.c: Fix common/ includes.
	* printcmd.c: Fix common/ includes.
	* ppc-linux-nat.c: Fix common/ includes.
	* parser-defs.h: Fix common/ includes.
	* osdata.c: Fix common/ includes.
	* obsd-nat.c: Fix common/ includes.
	* nat/x86-linux.c: Fix common/ includes.
	* nat/x86-linux-dregs.c: Fix common/ includes.
	* nat/x86-dregs.h: Fix common/ includes.
	* nat/x86-dregs.c: Fix common/ includes.
	* nat/ppc-linux.c: Fix common/ includes.
	* nat/mips-linux-watch.h: Fix common/ includes.
	* nat/mips-linux-watch.c: Fix common/ includes.
	* nat/linux-waitpid.c: Fix common/ includes.
	* nat/linux-ptrace.h: Fix common/ includes.
	* nat/linux-ptrace.c: Fix common/ includes.
	* nat/linux-procfs.c: Fix common/ includes.
	* nat/linux-personality.c: Fix common/ includes.
	* nat/linux-osdata.c: Fix common/ includes.
	* nat/linux-namespaces.c: Fix common/ includes.
	* nat/linux-btrace.h: Fix common/ includes.
	* nat/linux-btrace.c: Fix common/ includes.
	* nat/fork-inferior.c: Fix common/ includes.
	* nat/amd64-linux-siginfo.c: Fix common/ includes.
	* nat/aarch64-sve-linux-ptrace.c: Fix common/ includes.
	* nat/aarch64-linux.c: Fix common/ includes.
	* nat/aarch64-linux-hw-point.h: Fix common/ includes.
	* nat/aarch64-linux-hw-point.c: Fix common/ includes.
	* namespace.h: Fix common/ includes.
	* mips-linux-tdep.c: Fix common/ includes.
	* minsyms.c: Fix common/ includes.
	* mi/mi-parse.h: Fix common/ includes.
	* mi/mi-main.c: Fix common/ includes.
	* mi/mi-cmd-env.c: Fix common/ includes.
	* memrange.h: Fix common/ includes.
	* memattr.c: Fix common/ includes.
	* maint.h: Fix common/ includes.
	* maint.c: Fix common/ includes.
	* main.c: Fix common/ includes.
	* machoread.c: Fix common/ includes.
	* location.c: Fix common/ includes.
	* linux-thread-db.c: Fix common/ includes.
	* linux-nat.c: Fix common/ includes.
	* linux-fork.c: Fix common/ includes.
	* inline-frame.c: Fix common/ includes.
	* infrun.c: Fix common/ includes.
	* inflow.c: Fix common/ includes.
	* inferior.h: Fix common/ includes.
	* inferior.c: Fix common/ includes.
	* infcmd.c: Fix common/ includes.
	* inf-ptrace.c: Fix common/ includes.
	* inf-child.c: Fix common/ includes.
	* ia64-linux-nat.c: Fix common/ includes.
	* i387-tdep.c: Fix common/ includes.
	* i386-tdep.c: Fix common/ includes.
	* i386-linux-tdep.c: Fix common/ includes.
	* i386-linux-nat.c: Fix common/ includes.
	* i386-go32-tdep.c: Fix common/ includes.
	* i386-fbsd-tdep.c: Fix common/ includes.
	* i386-fbsd-nat.c: Fix common/ includes.
	* guile/scm-type.c: Fix common/ includes.
	* guile/guile.c: Fix common/ includes.
	* go32-nat.c: Fix common/ includes.
	* gnu-nat.c: Fix common/ includes.
	* gdbthread.h: Fix common/ includes.
	* gdbarch-selftests.c: Fix common/ includes.
	* gdb_usleep.c: Fix common/ includes.
	* gdb_select.h: Fix common/ includes.
	* gdb_bfd.c: Fix common/ includes.
	* gcore.c: Fix common/ includes.
	* fork-child.c: Fix common/ includes.
	* findvar.c: Fix common/ includes.
	* fbsd-nat.c: Fix common/ includes.
	* event-top.c: Fix common/ includes.
	* event-loop.c: Fix common/ includes.
	* dwarf2read.c: Fix common/ includes.
	* dwarf2loc.c: Fix common/ includes.
	* dwarf2-frame.c: Fix common/ includes.
	* dwarf-index-cache.c: Fix common/ includes.
	* dtrace-probe.c: Fix common/ includes.
	* disasm-selftests.c: Fix common/ includes.
	* defs.h: Fix common/ includes.
	* csky-tdep.c: Fix common/ includes.
	* cp-valprint.c: Fix common/ includes.
	* cp-support.h: Fix common/ includes.
	* cp-support.c: Fix common/ includes.
	* corelow.c: Fix common/ includes.
	* completer.h: Fix common/ includes.
	* completer.c: Fix common/ includes.
	* compile/compile.c: Fix common/ includes.
	* compile/compile-loc2c.c: Fix common/ includes.
	* compile/compile-cplus-types.c: Fix common/ includes.
	* compile/compile-cplus-symbols.c: Fix common/ includes.
	* command.h: Fix common/ includes.
	* cli/cli-dump.c: Fix common/ includes.
	* cli/cli-cmds.c: Fix common/ includes.
	* charset.c: Fix common/ includes.
	* build-id.c: Fix common/ includes.
	* btrace.h: Fix common/ includes.
	* btrace.c: Fix common/ includes.
	* breakpoint.h: Fix common/ includes.
	* breakpoint.c: Fix common/ includes.
	* ax.h:
	(enum agent_op): Fix common/ includes.
	* ax-general.c (struct aop_map): Fix common/ includes.
	* ax-gdb.c: Fix common/ includes.
	* auxv.c: Fix common/ includes.
	* auto-load.c: Fix common/ includes.
	* arm-tdep.c: Fix common/ includes.
	* arch/riscv.c: Fix common/ includes.
	* arch/ppc-linux-common.c: Fix common/ includes.
	* arch/i386.c: Fix common/ includes.
	* arch/arm.c: Fix common/ includes.
	* arch/arm-linux.c: Fix common/ includes.
	* arch/arm-get-next-pcs.c: Fix common/ includes.
	* arch/amd64.c: Fix common/ includes.
	* arch/aarch64.c: Fix common/ includes.
	* arch/aarch64-insn.c: Fix common/ includes.
	* arch-utils.c: Fix common/ includes.
	* amd64-windows-tdep.c: Fix common/ includes.
	* amd64-tdep.c: Fix common/ includes.
	* amd64-sol2-tdep.c: Fix common/ includes.
	* amd64-obsd-tdep.c: Fix common/ includes.
	* amd64-nbsd-tdep.c: Fix common/ includes.
	* amd64-linux-tdep.c: Fix common/ includes.
	* amd64-linux-nat.c: Fix common/ includes.
	* amd64-fbsd-tdep.c: Fix common/ includes.
	* amd64-fbsd-nat.c: Fix common/ includes.
	* amd64-dicos-tdep.c: Fix common/ includes.
	* amd64-darwin-tdep.c: Fix common/ includes.
	* agent.c: Fix common/ includes.
	* ada-lang.h: Fix common/ includes.
	* ada-lang.c: Fix common/ includes.
	* aarch64-tdep.c: Fix common/ includes.

gdb/gdbserver/ChangeLog
2019-01-25  Tom Tromey  <tom@tromey.com>

	* win32-low.c: Fix common/ includes.
	* win32-i386-low.c: Fix common/ includes.
	* tracepoint.c: Fix common/ includes.
	* thread-db.c: Fix common/ includes.
	* target.h: Fix common/ includes.
	* symbol.c: Fix common/ includes.
	* spu-low.c: Fix common/ includes.
	* server.h: Fix common/ includes.
	* server.c: Fix common/ includes.
	* remote-utils.c: Fix common/ includes.
	* regcache.h: Fix common/ includes.
	* regcache.c: Fix common/ includes.
	* nto-x86-low.c: Fix common/ includes.
	* notif.h: Fix common/ includes.
	* mem-break.h: Fix common/ includes.
	* lynx-low.c: Fix common/ includes.
	* lynx-i386-low.c: Fix common/ includes.
	* linux-x86-tdesc-selftest.c: Fix common/ includes.
	* linux-x86-low.c: Fix common/ includes.
	* linux-low.c: Fix common/ includes.
	* inferiors.h: Fix common/ includes.
	* i387-fp.c: Fix common/ includes.
	* hostio.c: Fix common/ includes.
	* hostio-errno.c: Fix common/ includes.
	* gdbthread.h: Fix common/ includes.
	* gdbreplay.c: Fix common/ includes.
	* fork-child.c: Fix common/ includes.
	* event-loop.c: Fix common/ includes.
	* ax.c:
	(enum gdb_agent_op): Fix common/ includes.
2019-01-25 15:28:16 -07:00
Tom Tromey 5419bdae55 Replace delete_longjmp_breakpoint_cleanup with a forward_scope_exit type
This removes delete_longjmp_breakpoint_cleanup in favor of forward_scope_exit.

gdb/ChangeLog:
2019-01-23  Tom Tromey  <tom@tromey.com>
	    Andrew Burgess  <andrew.burgess@embecosm.com>
	    Pedro Alves  <palves@redhat.com>

	* breakpoint.c (until_break_command): Use
	delete_longjmp_breakpoint_cleanup class.
	* infcmd.c (delete_longjmp_breakpoint_cleanup): Remove function.
	(until_next_command): Use delete_longjmp_breakpoint_cleanup class.
	* inferior.h: Include forward-scope-exit.h.
	(delete_longjmp_breakpoint_cleanup): Replace function declaration
	with FORWARD_SCOPE_EXIT type.
2019-01-23 19:04:26 +00:00
Simon Marchi d20172fc53 Place displaced step data directly in inferior structure
This patch moves the per-inferior data related to displaced stepping to
be directly in the inferior structure, rather than in a container on the
side.

On notable difference is that previously, we deleted the state on
inferior exit, which guaranteed a clean state if re-using the inferior
for a new run or attach.  We now need to reset the state manually.

At the same time, I changed step_saved_copy to be a gdb::byte_vector, so
it is automatically freed on destruction (which should plug the leak
reported here [1]).

[1] https://sourceware.org/ml/gdb-patches/2018-11/msg00202.html

gdb/ChangeLog:

	* inferior.h (class inferior) <displaced_step_state>: New field.
	* infrun.h (struct displaced_step_state): Move here from
	infrun.c.  Initialize fields, add constructor.
	<inf>: Remove field.
	<reset>: New method.
	* infrun.c (struct displaced_step_inferior_state): Move to
	infrun.h.
	(displaced_step_inferior_states): Remove.
	(get_displaced_stepping_state): Adust.
	(displaced_step_in_progress_any_inferior): Adjust.
	(displaced_step_in_progress_thread): Adjust.
	(displaced_step_in_progress): Adjust.
	(add_displaced_stepping_state): Remove.
	(get_displaced_step_closure_by_addr): Adjust.
	(remove_displaced_stepping_state): Remove.
	(infrun_inferior_exit): Call displaced_step_state.reset.
	(use_displaced_stepping): Don't check for NULL.
	(displaced_step_prepare_throw): Call
	get_displaced_stepping_state.
	(displaced_step_fixup): Don't check for NULL.
	(prepare_for_detach): Don't check for NULL.
2019-01-02 17:31:08 -05:00
Joel Brobecker 42a4f53d2b Update copyright year range in all GDB files.
This commit applies all changes made after running the gdb/copyright.py
script.

Note that one file was flagged by the script, due to an invalid
copyright header
(gdb/unittests/basic_string_view/element_access/char/empty.cc).
As the file was copied from GCC's libstdc++-v3 testsuite, this commit
leaves this file untouched for the time being; a patch to fix the header
was sent to gcc-patches first.

gdb/ChangeLog:

	Update copyright year range in all GDB files.
2019-01-01 10:01:51 +04:00
Pedro Alves 0803633106 Per-inferior thread list, thread ranges/iterators, down with ALL_THREADS, etc.
As preparation for multi-target, this patch makes each inferior have
its own thread list.

This isn't absolutely necessary for multi-target, but simplifies
things.  It originally stemmed from the desire to eliminate the
init_thread_list calls sprinkled around, plus it makes it more
efficient to iterate over threads of a given inferior (no need to
always iterate over threads of all inferiors).

We still need to iterate over threads of all inferiors in a number of
places, which means we'd need adjust the ALL_THREADS /
ALL_NON_EXITED_THREADS macros.  However, naively tweaking those macros
to have an extra for loop, like:

     #define ALL_THREADS (thr, inf) \
       for (inf = inferior_list; inf; inf = inf->next) \
	 for (thr = inf->thread_list; thr; thr = thr->next)

causes problems with code that does "break" or "continue" within the
ALL_THREADS loop body.  Plus, we need to declare the extra "inf" local
variable in order to pass it as temporary variable to ALL_THREADS
(etc.)

It gets even trickier when we consider extending the macros to filter
out threads matching a ptid_t and a target.  The macros become tricker
to read/write.  Been there.

An alternative (which was my next attempt), is to replace the
ALL_THREADS etc. iteration style with for_each_all_threads,
for_each_non_exited_threads, etc. functions which would take a
callback as parameter, which would usually be passed a lambda.
However, I did not find that satisfactory at all, because the
resulting code ends up a little less natural / more noisy to read,
write and debug/step-through (due to use of lambdas), and in many
places where we use "continue;" to skip to the next thread now need to
use "return;".  (I ran into hard to debug bugs caused by a
continue/return confusion.)

I.e., before:

    ALL_NON_EXITED_THREADS (tp)
      {
	if (tp->not_what_I_want)
	  continue;
	// do something
      }

would turn into:

    for_each_non_exited_thread ([&] (thread_info *tp)
      {
	if (tp->not_what_I_want)
	  return;
	// do something
      });

Lastly, the solution I settled with was to replace the ALL_THREADS /
ALL_NON_EXITED_THREADS / ALL_INFERIORS macros with (C++20-like) ranges
and iterators, such that you can instead naturaly iterate over
threads/inferiors using range-for, like e.g,.:

   // all threads, including THREAD_EXITED threads.
   for (thread_info *tp : all_threads ())
     { .... }

   // all non-exited threads.
   for (thread_info *tp : all_non_exited_threads ())
     { .... }

   // all non-exited threads of INF inferior.
   for (thread_info *tp : inf->non_exited_threads ())
     { .... }

The all_non_exited_threads() function takes an optional filter ptid_t as
parameter, which is quite convenient when we need to iterate over
threads matching that filter.  See e.g., how the
set_executing/set_stop_requested/finish_thread_state etc. functions in
thread.c end up being simplified.

Most of the patch thus is about adding the infrustructure for allowing
the above.  Later on when we get to actual multi-target, these
functions/ranges/iterators will gain a "target_ops *" parameter so
that e.g., we can iterate over all threads of a given target that
match a given filter ptid_t.

The only entry points users needs to be aware of are the
all_threads/all_non_exited_threads etc. functions seen above.  Thus,
those functions are declared in gdbthread.h/inferior.h.  The actual
iterators/ranges are mainly "internals" and thus are put out of view
in the new thread-iter.h/thread-iter.c/inferior-iter.h files.  That
keeps the gdbthread.h/inferior.h headers quite a bit more readable.

A common/safe-iterator.h header is added which adds a template that
can be used to build "safe" iterators, which are forward iterators
that can be used to replace the ALL_THREADS_SAFE macro and other
instances of the same idiom in future.

There's a little bit of shuffling of code between
gdbthread.h/thread.c/inferior.h in the patch.  That is necessary in
order to avoid circular dependencies between the
gdbthread.h/inferior.h headers.

As for the init_thread_list calls sprinkled around, they're all
eliminated by this patch, and a new, central call is added to
inferior_appeared.  Note how also related to that, there's a call to
init_wait_for_inferior in remote.c that is eliminated.
init_wait_for_inferior is currently responsible for discarding skipped
inline frames, which had to be moved elsewhere.  Given that nowadays
we always have a thread even for single-threaded processes, the
natural place is to delete a frame's inline frame info when we delete
the thread.  I.e., from clear_thread_inferior_resources.

gdb/ChangeLog:
2018-11-22  Pedro Alves  <palves@redhat.com>

	* Makefile.in (COMMON_SFILES): Add thread-iter.c.
	* breakpoint.c (breakpoints_should_be_inserted_now): Replace
	ALL_NON_EXITED_THREADS with all_non_exited_threads.
	(print_one_breakpoint_location): Replace ALL_INFERIORS with
	all_inferiors.
	* bsd-kvm.c: Include inferior.h.
	* btrace.c (btrace_free_objfile): Replace ALL_NON_EXITED_THREADS
	with all_non_exited_threads.
	* common/filtered-iterator.h: New.
	* common/safe-iterator.h: New.
	* corelow.c (core_target_open): Don't call init_thread_list here.
	* darwin-nat.c (thread_info_from_private_thread_info): Replace
	ALL_THREADS with all_threads.
	* fbsd-nat.c (fbsd_nat_target::resume): Replace
	ALL_NON_EXITED_THREADS with inf->non_exited_threads.
	* fbsd-tdep.c (fbsd_make_corefile_notes): Replace
	ALL_NON_EXITED_THREADS with inf->non_exited_threads.
	* fork-child.c (postfork_hook): Don't call init_thread_list here.
	* gdbarch-selftests.c (register_to_value_test): Adjust.
	* gdbthread.h: Don't include "inferior.h" here.
	(struct inferior): Forward declare.
	(enum step_over_calls_kind): Moved here from inferior.h.
	(thread_info::deletable): Definition moved to thread.c.
	(find_thread_ptid (inferior *, ptid_t)): Declare.
	(ALL_THREADS, ALL_THREADS_BY_INFERIOR, ALL_THREADS_SAFE): Delete.
	Include "thread-iter.h".
	(all_threads, all_non_exited_threads, all_threads_safe): New.
	(any_thread_p): Declare.
	(thread_list): Delete.
	* infcmd.c (signal_command): Replace ALL_NON_EXITED_THREADS with
	all_non_exited_threads.
	(proceed_after_attach_callback): Delete.
	(proceed_after_attach): Take an inferior pointer instead of an
	integer PID.  Adjust to use range-for.
	(attach_post_wait): Pass down inferior pointer instead of pid.
	Use range-for instead of ALL_NON_EXITED_THREADS.
	(detach_command): Remove init_thread_list call.
	* inferior-iter.h: New.
	* inferior.c (struct delete_thread_of_inferior_arg): Delete.
	(delete_thread_of_inferior): Delete.
	(delete_inferior, exit_inferior_1): Use range-for with
	inf->threads_safe() instead of iterate_over_threads.
	(inferior_appeared): Call init_thread_list here.
	(discard_all_inferiors): Use all_non_exited_inferiors.
	(find_inferior_id, find_inferior_pid): Use all_inferiors.
	(iterate_over_inferiors): Use all_inferiors_safe.
	(have_inferiors, number_of_live_inferiors): Use
	all_non_exited_inferiors.
	(number_of_inferiors): Use all_inferiors and std::distance.
	(print_inferior): Use all_inferiors.
	* inferior.h: Include gdbthread.h.
	(enum step_over_calls_kind): Moved to gdbthread.h.
	(struct inferior) <thread_list>: New field.
	<threads, non_exited_threads, threads_safe>: New methods.
	(ALL_INFERIORS): Delete.
	Include "inferior-iter.h".
	(ALL_NON_EXITED_INFERIORS): Delete.
	(all_inferiors_safe, all_inferiors, all_non_exited_inferiors): New
	functions.
	* inflow.c (child_interrupt, child_pass_ctrlc): Replace
	ALL_NON_EXITED_THREADS with all_non_exited_threads.
	* infrun.c (follow_exec): Use all_threads_safe.
	(clear_proceed_status, proceed): Use all_non_exited_threads.
	(init_wait_for_inferior): Don't clear inline frame state here.
	(infrun_thread_stop_requested, for_each_just_stopped_thread): Use
	all_threads instead of ALL_NON_EXITED_THREADS.
	(random_pending_event_thread): Use all_non_exited_threads instead
	of ALL_NON_EXITED_THREADS.  Use a lambda for repeated code.
	(clean_up_just_stopped_threads_fsms): Use all_non_exited_threads
	instead of ALL_NON_EXITED_THREADS.
	(handle_no_resumed): Use all_non_exited_threads instead of
	ALL_NON_EXITED_THREADS.  Use all_inferiors instead of
	ALL_INFERIORS.
	(restart_threads, switch_back_to_stepped_thread): Use
	all_non_exited_threads instead of ALL_NON_EXITED_THREADS.
	* linux-nat.c (check_zombie_leaders): Replace ALL_INFERIORS with
	all_inferiors.
	(kill_unfollowed_fork_children): Use inf->non_exited_threads
	instead of ALL_NON_EXITED_THREADS.
	* linux-tdep.c (linux_make_corefile_notes): Use
	inf->non_exited_threads instead of ALL_NON_EXITED_THREADS.
	* linux-thread-db.c (thread_db_target::update_thread_list):
	Replace ALL_INFERIORS with all_inferiors.
	(thread_db_target::thread_handle_to_thread_info): Use
	inf->non_exited_threads instead of ALL_NON_EXITED_THREADS.
	* mi/mi-interp.c (multiple_inferiors_p): New.
	(mi_on_resume_1): Simplify using all_non_exited_threads and
	multiple_inferiors_p.
	* mi/mi-main.c (mi_cmd_thread_list_ids): Use all_non_exited_threads
	instead of ALL_NON_EXITED_THREADS.
	* nto-procfs.c (nto_procfs_target::open): Don't call
	init_thread_list here.
	* record-btrace.c (record_btrace_target_open)
	(record_btrace_target::stop_recording)
	(record_btrace_target::close)
	(record_btrace_target::record_is_replaying)
	(record_btrace_target::resume, record_btrace_target::wait)
	(record_btrace_target::record_stop_replaying): Use
	all_non_exited_threads instead of ALL_NON_EXITED_THREADS.
	* record-full.c (record_full_wait_1): Use all_non_exited_threads
	instead of ALL_NON_EXITED_THREADS.
	* regcache.c (cooked_read_test): Remove reference to global
	thread_list.
	* remote-sim.c (gdbsim_target::create_inferior): Don't call
	init_thread_list here.
	* remote.c (remote_target::update_thread_list): Use
	all_threads_safe instead of ALL_NON_EXITED_THREADS.
	(remote_target::process_initial_stop_replies): Replace
	ALL_INFERIORS with all_non_exited_inferiors and use
	all_non_exited_threads instead of ALL_NON_EXITED_THREADS.
	(remote_target::open_1): Don't call init_thread_list here.
	(remote_target::append_pending_thread_resumptions)
	(remote_target::remote_resume_with_hc): Use all_non_exited_threads
	instead of ALL_NON_EXITED_THREADS.
	(remote_target::commit_resume)
	(remote_target::remove_new_fork_children): Replace ALL_INFERIORS
	with all_non_exited_inferiors and use all_non_exited_threads
	instead of ALL_NON_EXITED_THREADS.
	(remote_target::kill_new_fork_children): Use
	all_non_exited_threads instead of ALL_NON_EXITED_THREADS.  Remove
	init_thread_list and init_wait_for_inferior calls.
	(remote_target::remote_btrace_maybe_reopen)
	(remote_target::thread_handle_to_thread_info): Use
	all_non_exited_threads instead of ALL_NON_EXITED_THREADS.
	* target.c (target_terminal::restore_inferior)
	(target_terminal_is_ours_kind): Replace ALL_INFERIORS with
	all_non_exited_inferiors.
	* thread-iter.c: New file.
	* thread-iter.h: New file.
	* thread.c: Include "inline-frame.h".
	(thread_list): Delete.
	(clear_thread_inferior_resources): Call clear_inline_frame_state.
	(init_thread_list): Use all_threads_safe instead of
	ALL_THREADS_SAFE.  Adjust to per-inferior thread lists.
	(new_thread): Adjust to per-inferior thread lists.
	(add_thread_silent): Pass inferior to find_thread_ptid.
	(thread_info::deletable): New, moved from the header.
	(delete_thread_1): Adjust to per-inferior thread lists.
	(find_thread_global_id): Use inf->threads().
	(find_thread_ptid): Use find_inferior_ptid and pass inferior to
	find_thread_ptid.
	(find_thread_ptid(inferior*, ptid_t)): New overload.
	(iterate_over_threads): Use all_threads_safe.
	(any_thread_p): New.
	(thread_count): Use all_threads and std::distance.
	(live_threads_count): Use all_non_exited_threads and
	std::distance.
	(valid_global_thread_id): Use all_threads.
	(in_thread_list): Use find_thread_ptid.
	(first_thread_of_inferior): Adjust to per-inferior thread lists.
	(any_thread_of_inferior, any_live_thread_of_inferior): Use
	inf->non_exited_threads().
	(prune_threads, delete_exited_threads): Use all_threads_safe.
	(thread_change_ptid): Pass inferior pointer to find_thread_ptid.
	(set_resumed, set_running): Use all_non_exited_threads.
	(is_thread_state, is_stopped, is_exited, is_running)
	(is_executing): Delete.
	(set_executing, set_stop_requested, finish_thread_state): Use
	all_non_exited_threads.
	(print_thread_info_1): Use all_inferiors and all_threads.
	(thread_apply_all_command): Use all_non_exited_threads.
	(thread_find_command): Use all_threads.
	(update_threads_executing): Use all_non_exited_threads.
	* tid-parse.c (parse_thread_id): Use inf->threads.
	* x86-bsd-nat.c (x86bsd_dr_set): Use inf->non_exited_threads ().
2018-11-22 16:13:23 +00:00
Tom Tromey cb5248409d Make save_infcall_*_state return unique pointers
Simon pointed out that save_infcall_suspend_state and
save_infcall_control_state could return unique pointers.  This patch
implements this idea.

gdb/ChangeLog
2018-09-17  Tom Tromey  <tom@tromey.com>

	* infrun.c (save_infcall_suspend_state): Return
	infcall_suspend_state_up.
	(save_infcall_control_state): Return infcall_control_state_up.
	* inferior.h (save_infcall_suspend_state)
	(save_infcall_control_state): Declare later.  Return unique
	pointers.
2018-09-17 00:42:19 -06:00
Tom Tromey c7c4d3fa80 Remove two infrun cleanups
This removes a couple of cleanups from infrun by introducing a couple
of unique_ptr specializations.

gdb/ChangeLog
2018-09-17  Tom Tromey  <tom@tromey.com>

	* inferior.h (struct infcall_suspend_state_deleter): New.
	(infcall_suspend_state_up): New typedef.
	(struct infcall_control_state_deleter): New.
	(infcall_control_state_up): New typedef.
	(make_cleanup_restore_infcall_suspend_state)
	(make_cleanup_restore_infcall_control_state): Don't declare.
	* infcall.c (call_function_by_hand_dummy): Update.
	* infrun.c (do_restore_infcall_suspend_state_cleanup)
	(make_cleanup_restore_infcall_suspend_state): Remove.
	(do_restore_infcall_control_state_cleanup)
	(make_cleanup_restore_infcall_control_state): Remove.
2018-09-17 00:42:18 -06:00
Tom Tromey ee841dd8fe Use new and delete for struct infcall_control_state
This changes infrun.c to use new and delete for infcall_control_state.

gdb/ChangeLog
2018-09-17  Tom Tromey  <tom@tromey.com>

	* gdbthread.h (struct thread_control_state): Add initializer.
	(class thread_info) <control>: Remove initializer.
	* inferior.h (struct inferior_control_state): Add initializer.
	(class inferior) <control>: Remove initializer.
	(exit_inferior_1): Update.
	* infrun.c (struct infcall_control_state): Add constructors.
	(save_infcall_control_state): Use new.
	(restore_infcall_control_state, discard_infcall_control_state):
	Use delete.
2018-09-17 00:42:18 -06:00
Tom Tromey cbaaa0cafc Change get_inferior_args to return const char *
I noticed that get_inferior_args should return const char *, because
it is just returning a reference to something owned by the inferior.

I'm checking this in.

gdb/ChangeLog
2018-09-15  Tom Tromey  <tom@tromey.com>

	* infcmd.c (get_inferior_args): Return const char *.
	* inferior.h (get_inferior_args): Return type now const.
	* linux-tdep.c (linux_fill_prpsinfo): Update.
	* procfs.c (procfs_target::make_corefile_notes): Update.
2018-09-15 16:31:58 -06:00
Tom Tromey e99b03dcf4 Remove ptid_get_pid
This removes ptid_get_pid in favor of calling the ptid_t::pid method.

gdb/ChangeLog
2018-07-03  Tom Tromey  <tom@tromey.com>

	* common/ptid.c (ptid_get_pid): Remove.
	* common/ptid.h (ptid_get_pid): Don't declare.
	* aarch64-linux-nat.c: Update.
	* ada-lang.c: Update.
	* aix-thread.c: Update.
	* alpha-bsd-nat.c: Update.
	* amd64-fbsd-nat.c: Update.
	* amd64-linux-nat.c: Update.
	* arm-linux-nat.c: Update.
	* arm-nbsd-nat.c: Update.
	* auxv.c: Update.
	* break-catch-syscall.c: Update.
	* breakpoint.c: Update.
	* bsd-uthread.c: Update.
	* corelow.c: Update.
	* ctf.c: Update.
	* darwin-nat.c: Update.
	* fbsd-nat.c: Update.
	* fbsd-tdep.c: Update.
	* gcore.c: Update.
	* gnu-nat.c: Update.
	* hppa-nbsd-nat.c: Update.
	* hppa-obsd-nat.c: Update.
	* i386-fbsd-nat.c: Update.
	* ia64-linux-nat.c: Update.
	* inf-ptrace.c: Update.
	* infcmd.c: Update.
	* inferior.c: Update.
	* inferior.h: Update.
	* inflow.c: Update.
	* infrun.c: Update.
	* linux-fork.c: Update.
	* linux-nat.c: Update.
	* linux-tdep.c: Update.
	* linux-thread-db.c: Update.
	* m68k-bsd-nat.c: Update.
	* mi/mi-interp.c: Update.
	* mi/mi-main.c: Update.
	* mips-linux-nat.c: Update.
	* mips-nbsd-nat.c: Update.
	* mips64-obsd-nat.c: Update.
	* nat/aarch64-linux-hw-point.c: Update.
	* nat/aarch64-linux.c: Update.
	* nat/linux-btrace.c: Update.
	* nat/linux-osdata.c: Update.
	* nat/linux-procfs.c: Update.
	* nat/x86-linux-dregs.c: Update.
	* nto-procfs.c: Update.
	* obsd-nat.c: Update.
	* ppc-linux-nat.c: Update.
	* ppc-nbsd-nat.c: Update.
	* ppc-obsd-nat.c: Update.
	* proc-service.c: Update.
	* procfs.c: Update.
	* python/py-inferior.c: Update.
	* python/py-infthread.c: Update.
	* ravenscar-thread.c: Update.
	* record.c: Update.
	* remote-sim.c: Update.
	* remote.c: Update.
	* rs6000-nat.c: Update.
	* s390-linux-nat.c: Update.
	* sh-nbsd-nat.c: Update.
	* sol-thread.c: Update.
	* sparc-nat.c: Update.
	* sparc64-tdep.c: Update.
	* spu-linux-nat.c: Update.
	* spu-tdep.c: Update.
	* target-debug.h: Update.
	* target.c: Update.
	* thread.c: Update.
	* tid-parse.c: Update.
	* tracefile-tfile.c: Update.
	* vax-bsd-nat.c: Update.
	* windows-nat.c: Update.
	* x86-linux-nat.c: Update.
	* x86-nat.c: Update.

gdb/gdbserver/ChangeLog
2018-07-03  Tom Tromey  <tom@tromey.com>

	* linux-low.c: Update.
	* linux-mips-low.c: Update.
	* lynx-low.c: Update.
	* mem-break.c: Update.
	* nto-low.c: Update.
	* remote-utils.c: Update.
	* server.c: Update.
	* spu-low.c: Update.
	* target.c: Update.
	* tracepoint.c: Update.
2018-07-03 11:36:42 -06:00
Pedro Alves f2ffa92bbc gdb: Eliminate the 'stop_pc' global
In my multi-target work, I need to add a few more
scoped_restore_current_thread and switch_to_thread calls in some
places, and in some lower-level places I was fighting against the fact
that switch_to_thread reads/refreshes the stop_pc global.

Instead of piling on workarounds, let's just finally eliminate the
stop_pc global.  We already have the per-thread
thread_info->suspend.stop_pc field, so it's mainly a matter of using
that more/instead.

gdb/ChangeLog:
2018-06-28  Pedro Alves  <palves@redhat.com>

	* gdbthread.h (struct thread_suspend_state) <stop_pc>: Extend
	comments.
	(switch_to_thread_no_regs): Adjust comment.
	* infcmd.c (stop_pc): Delete.
	(post_create_inferior, info_program_command): Replace references
	to stop_pc with references to thread_info->suspend.stop_pc.
	* inferior.h (stop_pc): Delete declaration.
	* infrun.c (proceed, handle_syscall_event, fill_in_stop_func)
	(handle_inferior_event_1, handle_signal_stop)
	(process_event_stop_test, keep_going_stepped_thread)
	(handle_step_into_function, handle_step_into_function_backward)
	(print_stop_location): Replace references to stop_pc with
	references to thread_info->suspend.stop_pc.
	(struct infcall_suspend_state) <stop_pc>: Delete field.
	(save_infcall_suspend_state, restore_infcall_suspend_state):
	Remove references to inf_stat->stop_pc.
	* linux-fork.c (fork_load_infrun_state): Likewise.
	* record-btrace.c (record_btrace_set_replay): Likewise.
	* record-full.c (record_full_goto_entry): Likewise.
	* remote.c (print_one_stopped_thread): Likewise.
	* target.c (target_resume): Extend comment.
	* thread.c (set_executing_thread): New.
	(set_executing): Use it.
	(switch_to_thread_no_regs, switch_to_no_thread, switch_to_thread):
	Remove references to stop_pc.
2018-06-28 17:12:07 +01:00
Pedro Alves 75cbc781e3 gdb: For macOS, s/thread_info/struct thread_info/
The macOS build currently fails with several instances of this problem:

  In file included from ../../src/gdb/darwin-nat.h:22:0,
		   from ../../src/gdb/i386-darwin-nat.c:37:
  ../../src/gdb/gdbthread.h:376:59: error: type/value mismatch at argument 1 in template parameter list for 'template<class T, class Policy> class gdb::ref_ptr'
     = gdb::ref_ptr<thread_info, refcounted_object_ref_policy>;
							     ^
  ../../src/gdb/gdbthread.h:376:59: note:   expected a type, got 'thread_info'
  ../../src/gdb/gdbthread.h:396:28: error: variable or field 'delete_thread' declared void
   extern void delete_thread (thread_info *thread);
			      ^
(...)

This is because there's a thread_info function in the Darwin/XNU/mach API:

 http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/thread_info.html

Fix this in the same way it had been fixed in commit 7aabaf9d4a
("Create private_thread_info hierarchy"), by adding an explicit
"struct" keyword.

gdb/ChangeLog:
2018-06-25  Pedro Alves  <palves@redhat.com>

	* gdbthread.h (thread_info_ref, delete_thread)
	(delete_thread_silent, first_thread_of_inferior)
	(any_thread_of_inferior, switch_to_thread)
	(enable_thread_stack_temporaries)
	(thread_stack_temporaries_enabled_p, push_thread_stack_temporary)
	(get_last_thread_stack_temporary)
	(value_in_thread_stack_temporaries, can_access_registers_thread):
	Spell out "struct thread_info" instead of just "thread_info".
	* inferior.h (notice_new_inferior): Likewise.
2018-06-25 17:43:14 +01:00
Pedro Alves 00431a78b2 Use thread_info and inferior pointers more throughout
This is more preparation bits for multi-target support.

In a multi-target scenario, we need to address the case of different
processes/threads running on different targets that happen to have the
same PID/PTID.  E.g., we can have both process 123 in target 1, and
process 123 in target 2, while they're in reality different processes
running on different machines.  Or maybe we've loaded multiple
instances of the same core file.  Etc.

To address this, in my WIP multi-target branch, threads and processes
are uniquely identified by the (process_stratum target_ops *, ptid_t)
and (process_stratum target_ops *, pid) tuples respectively.  I.e.,
each process_stratum instance has its own thread/process number space.

As you can imagine, that requires passing around target_ops * pointers
in a number of functions where we're currently passing only a ptid_t
or an int.  E.g., when we look up a thread_info object by ptid_t in
find_thread_ptid, the ptid_t alone isn't sufficient.

In many cases though, we already have the thread_info or inferior
pointer handy, but we "lose" it somewhere along the call stack, only
to look it up again by ptid_t/pid.  Since thread_info or inferior
objects know their parent target, if we pass around thread_info or
inferior pointers when possible, we avoid having to add extra
target_ops parameters to many functions, and also, we eliminate a
number of by ptid_t/int lookups.

So that's what this patch does.  In a bit more detail:

- Changes a number of functions and methods to take a thread_info or
  inferior pointer instead of a ptid_t or int parameter.

- Changes a number of structure fields from ptid_t/int to inferior or
  thread_info pointers.

- Uses the inferior_thread() function whenever possible instead of
  inferior_ptid.

- Uses thread_info pointers directly when possible instead of the
  is_running/is_stopped etc. routines that require a lookup.

- A number of functions are eliminated along the way, such as:

  int valid_gdb_inferior_id (int num);
  int pid_to_gdb_inferior_id (int pid);
  int gdb_inferior_id_to_pid (int num);
  int in_inferior_list (int pid);

- A few structures and places hold a thread_info pointer across
  inferior execution, so now they take a strong reference to the
  (refcounted) thread_info object to avoid the thread_info pointer
  getting stale.  This is done in enable_thread_stack_temporaries and
  in the infcall.c code.

- Related, there's a spot in infcall.c where using a RAII object to
  handle the refcount would be handy, so a gdb::ref_ptr specialization
  for thread_info is added (thread_info_ref, in gdbthread.h), along
  with a gdb_ref_ptr policy that works for all refcounted_object types
  (in common/refcounted-object.h).

gdb/ChangeLog:
2018-06-21  Pedro Alves  <palves@redhat.com>

	* ada-lang.h (ada_get_task_number): Take a thread_info pointer
	instead of a ptid_t.  All callers adjusted.
	* ada-tasks.c (ada_get_task_number): Likewise.  All callers
	adjusted.
	(print_ada_task_info, display_current_task_id, task_command_1):
	Adjust.
	* breakpoint.c (watchpoint_in_thread_scope): Adjust to use
	inferior_thread.
	(breakpoint_kind): Adjust.
	(remove_breakpoints_pid): Rename to ...
	(remove_breakpoints_inf): ... this.  Adjust to take an inferior
	pointer.  All callers adjusted.
	(bpstat_clear_actions): Use inferior_thread.
	(get_bpstat_thread): New.
	(bpstat_do_actions): Use it.
	(bpstat_check_breakpoint_conditions, bpstat_stop_status): Adjust
	to take a thread_info pointer.  All callers adjusted.
	(set_longjmp_breakpoint_for_call_dummy, set_momentary_breakpoint)
	(breakpoint_re_set_thread): Use inferior_thread.
	* breakpoint.h (struct inferior): Forward declare.
	(bpstat_stop_status): Update.
	(remove_breakpoints_pid): Delete.
	(remove_breakpoints_inf): New.
	* bsd-uthread.c (bsd_uthread_target::wait)
	(bsd_uthread_target::update_thread_list): Use find_thread_ptid.
	* btrace.c (btrace_add_pc, btrace_enable, btrace_fetch)
	(maint_btrace_packet_history_cmd)
	(maint_btrace_clear_packet_history_cmd): Adjust.
	(maint_btrace_clear_cmd, maint_info_btrace_cmd): Adjust to use
	inferior_thread.
	* cli/cli-interp.c: Include "inferior.h".
	* common/refcounted-object.h (struct
	refcounted_object_ref_policy): New.
	* compile/compile-object-load.c: Include gdbthread.h.
	(store_regs): Use inferior_thread.
	* corelow.c (core_target::close): Use current_inferior.
	(core_target_open): Adjust to use first_thread_of_inferior and use
	the current inferior.
	* ctf.c (ctf_target::close): Adjust to use current_inferior.
	* dummy-frame.c (dummy_frame_id) <ptid>: Delete, replaced by ...
	<thread>: ... this new field.  All references adjusted.
	(dummy_frame_pop, dummy_frame_discard, register_dummy_frame_dtor):
	Take a thread_info pointer instead of a ptid_t.
	* dummy-frame.h (dummy_frame_push, dummy_frame_pop)
	(dummy_frame_discard, register_dummy_frame_dtor): Take a
	thread_info pointer instead of a ptid_t.
	* elfread.c: Include "inferior.h".
	(elf_gnu_ifunc_resolver_stop, elf_gnu_ifunc_resolver_return_stop):
	Use inferior_thread.
	* eval.c (evaluate_subexp): Likewise.
	* frame.c (frame_pop, has_stack_frames, find_frame_sal): Use
	inferior_thread.
	* gdb_proc_service.h (struct thread_info): Forward declare.
	(struct ps_prochandle) <ptid>: Delete, replaced by ...
	<thread>: ... this new field.  All references adjusted.
	* gdbarch.h, gdbarch.c: Regenerate.
	* gdbarch.sh (get_syscall_number): Replace 'ptid' parameter with a
	'thread' parameter.  All implementations and callers adjusted.
	* gdbthread.h (thread_info) <set_running>: New method.
	(delete_thread, delete_thread_silent): Take a thread_info pointer
	instead of a ptid.
	(global_thread_id_to_ptid, ptid_to_global_thread_id): Delete.
	(first_thread_of_process): Delete, replaced by ...
	(first_thread_of_inferior): ... this new function.  All callers
	adjusted.
	(any_live_thread_of_process): Delete, replaced by ...
	(any_live_thread_of_inferior): ... this new function.  All callers
	adjusted.
	(switch_to_thread, switch_to_no_thread): Declare.
	(is_executing): Delete.
	(enable_thread_stack_temporaries): Update comment.
	<enable_thread_stack_temporaries>: Take a thread_info pointer
	instead of a ptid_t.  Incref the thread.
	<~enable_thread_stack_temporaries>: Decref the thread.
	<m_ptid>: Delete
	<m_thr>: New.
	(thread_stack_temporaries_enabled_p, push_thread_stack_temporary)
	(get_last_thread_stack_temporary)
	(value_in_thread_stack_temporaries, can_access_registers_thread):
	Take a thread_info pointer instead of a ptid_t.  All callers
	adjusted.
	* infcall.c (get_call_return_value): Use inferior_thread.
	(run_inferior_call): Work with thread pointers instead of ptid_t.
	(call_function_by_hand_dummy): Work with thread pointers instead
	of ptid_t.  Use thread_info_ref.
	* infcmd.c (proceed_thread_callback): Access thread's state
	directly.
	(ensure_valid_thread, ensure_not_running): Use inferior_thread,
	access thread's state directly.
	(continue_command): Use inferior_thread.
	(info_program_command): Use find_thread_ptid and access thread
	state directly.
	(proceed_after_attach_callback): Use thread state directly.
	(notice_new_inferior): Take a thread_info pointer instead of a
	ptid_t.  All callers adjusted.
	(exit_inferior): Take an inferior pointer instead of a pid.  All
	callers adjusted.
	(exit_inferior_silent): New.
	(detach_inferior): Delete.
	(valid_gdb_inferior_id, pid_to_gdb_inferior_id)
	(gdb_inferior_id_to_pid, in_inferior_list): Delete.
	(detach_inferior_command, kill_inferior_command): Use
	find_inferior_id instead of valid_gdb_inferior_id and
	gdb_inferior_id_to_pid.
	(inferior_command): Use inferior and thread pointers.
	* inferior.h (struct thread_info): Forward declare.
	(notice_new_inferior): Take a thread_info pointer instead of a
	ptid_t.  All callers adjusted.
	(detach_inferior): Delete declaration.
	(exit_inferior, exit_inferior_silent): Take an inferior pointer
	instead of a pid.  All callers adjusted.
	(gdb_inferior_id_to_pid, pid_to_gdb_inferior_id, in_inferior_list)
	(valid_gdb_inferior_id): Delete.
	* infrun.c (follow_fork_inferior, proceed_after_vfork_done)
	(handle_vfork_child_exec_or_exit, follow_exec): Adjust.
	(struct displaced_step_inferior_state) <pid>: Delete, replaced by
	...
	<inf>: ... this new field.
	<step_ptid>: Delete, replaced by ...
	<step_thread>: ... this new field.
	(get_displaced_stepping_state): Take an inferior pointer instead
	of a pid.  All callers adjusted.
	(displaced_step_in_progress_any_inferior): Adjust.
	(displaced_step_in_progress_thread): Take a thread pointer instead
	of a ptid_t.  All callers adjusted.
	(displaced_step_in_progress, add_displaced_stepping_state): Take
	an inferior pointer instead of a pid.  All callers adjusted.
	(get_displaced_step_closure_by_addr): Adjust.
	(remove_displaced_stepping_state): Take an inferior pointer
	instead of a pid.  All callers adjusted.
	(displaced_step_prepare_throw, displaced_step_prepare)
	(displaced_step_fixup): Take a thread pointer instead of a ptid_t.
	All callers adjusted.
	(start_step_over): Adjust.
	(infrun_thread_ptid_changed): Remove bit updating ptids in the
	displaced step queue.
	(do_target_resume): Adjust.
	(fetch_inferior_event): Use inferior_thread.
	(context_switch, get_inferior_stop_soon): Take an
	execution_control_state pointer instead of a ptid_t.  All callers
	adjusted.
	(switch_to_thread_cleanup): Delete.
	(stop_all_threads): Use scoped_restore_current_thread.
	* inline-frame.c: Include "gdbthread.h".
	(inline_state) <inline_state>: Take a thread pointer instead of a
	ptid_t.  All callers adjusted.
	<ptid>: Delete, replaced by ...
	<thread>: ... this new field.
	(find_inline_frame_state): Take a thread pointer instead of a
	ptid_t.  All callers adjusted.
	(skip_inline_frames, step_into_inline_frame)
	(inline_skipped_frames, inline_skipped_symbol): Take a thread
	pointer instead of a ptid_t.  All callers adjusted.
	* inline-frame.h (skip_inline_frames, step_into_inline_frame)
	(inline_skipped_frames, inline_skipped_symbol): Likewise.
	* linux-fork.c (delete_checkpoint_command): Adjust to use thread
	pointers directly.
	* linux-nat.c (get_detach_signal): Likewise.
	* linux-thread-db.c (thread_from_lwp): New 'stopped' parameter.
	(thread_db_notice_clone): Adjust.
	(thread_db_find_new_threads_silently)
	(thread_db_find_new_threads_2, thread_db_find_new_threads_1): Take
	a thread pointer instead of a ptid_t.  All callers adjusted.
	* mi/mi-cmd-var.c: Include "inferior.h".
	(mi_cmd_var_update_iter): Update to use thread pointers.
	* mi/mi-interp.c (mi_new_thread): Update to use the thread's
	inferior directly.
	(mi_output_running_pid, mi_inferior_count): Delete, bits factored
	out to ...
	(mi_output_running): ... this new function.
	(mi_on_resume_1): Adjust to use it.
	(mi_user_selected_context_changed): Adjust to use inferior_thread.
	* mi/mi-main.c (proceed_thread): Adjust to use thread pointers
	directly.
	(interrupt_thread_callback): : Adjust to use thread and inferior
	pointers.
	* proc-service.c: Include "gdbthread.h".
	(ps_pglobal_lookup): Adjust to use the thread's inferior directly.
	* progspace-and-thread.c: Include "inferior.h".
	* progspace.c: Include "inferior.h".
	* python/py-exitedevent.c (create_exited_event_object): Adjust to
	hold a reference to an inferior_object.
	* python/py-finishbreakpoint.c (bpfinishpy_init): Adjust to use
	inferior_thread.
	* python/py-inferior.c (struct inferior_object): Give the type a
	tag name instead of a typedef.
	(python_on_normal_stop): No need to check if the current thread is
	listed.
	(inferior_to_inferior_object): Change return type to
	inferior_object.  All callers adjusted.
	(find_thread_object): Delete, bits factored out to ...
	(thread_to_thread_object): ... this new function.
	* python/py-infthread.c (create_thread_object): Use
	inferior_to_inferior_object.
	(thpy_is_stopped): Use thread pointer directly.
	(gdbpy_selected_thread): Use inferior_thread.
	* python/py-record-btrace.c (btpy_list_object) <ptid>: Delete
	field, replaced with ...
	<thread>: ... this new field.  All users adjusted.
	(btpy_insn_or_gap_new): Drop const.
	(btpy_list_new): Take a thread pointer instead of a ptid_t.  All
	callers adjusted.
	* python/py-record.c: Include "gdbthread.h".
	(recpy_insn_new, recpy_func_new): Take a thread pointer instead of
	a ptid_t.  All callers adjusted.
	(gdbpy_current_recording): Use inferior_thread.
	* python/py-record.h (recpy_record_object) <ptid>: Delete
	field, replaced with ...
	<thread>: ... this new field.  All users adjusted.
	(recpy_element_object) <ptid>: Delete
	field, replaced with ...
	<thread>: ... this new field.  All users adjusted.
	(recpy_insn_new, recpy_func_new): Take a thread pointer instead of
	a ptid_t.  All callers adjusted.
	* python/py-threadevent.c: Include "gdbthread.h".
	(get_event_thread): Use thread_to_thread_object.
	* python/python-internal.h (struct inferior_object): Forward
	declare.
	(find_thread_object, find_inferior_object): Delete declarations.
	(thread_to_thread_object, inferior_to_inferior_object): New
	declarations.
	* record-btrace.c: Include "inferior.h".
	(require_btrace_thread): Use inferior_thread.
	(record_btrace_frame_sniffer)
	(record_btrace_tailcall_frame_sniffer): Use inferior_thread.
	(get_thread_current_frame): Use scoped_restore_current_thread and
	switch_to_thread.
	(get_thread_current_frame): Use thread pointer directly.
	(record_btrace_replay_at_breakpoint): Use thread's inferior
	pointer directly.
	* record-full.c: Include "inferior.h".
	* regcache.c: Include "gdbthread.h".
	(get_thread_arch_regcache): Use the inferior's address space
	directly.
	(get_thread_regcache, registers_changed_thread): New.
	* regcache.h (get_thread_regcache(thread_info *thread)): New
	overload.
	(registers_changed_thread): New.
	(remote_target) <remote_detach_1>: Swap order of parameters.
	(remote_add_thread): <remote_add_thread>: Return the new thread.
	(get_remote_thread_info(ptid_t)): New overload.
	(remote_target::remote_notice_new_inferior): Use thread pointers
	directly.
	(remote_target::process_initial_stop_replies): Use
	thread_info::set_running.
	(remote_target::remote_detach_1, remote_target::detach)
	(extended_remote_target::detach): Adjust.
	* stack.c (frame_show_address): Use inferior_thread.
	* target-debug.h (target_debug_print_thread_info_pp): New.
	* target-delegates.c: Regenerate.
	* target.c (default_thread_address_space): Delete.
	(memory_xfer_partial_1): Use current_inferior.
	(target_detach): Use current_inferior.
	(target_thread_address_space): Delete.
	(generic_mourn_inferior): Use current_inferior.
	* target.h (struct target_ops) <thread_address_space>: Delete.
	(target_thread_address_space): Delete.
	* thread.c (init_thread_list): Use ALL_THREADS_SAFE.  Use thread
	pointers directly.
	(delete_thread_1, delete_thread, delete_thread_silent): Take a
	thread pointer instead of a ptid_t.  Adjust all callers.
	(ptid_to_global_thread_id, global_thread_id_to_ptid): Delete.
	(first_thread_of_process): Delete, replaced by ...
	(first_thread_of_inferior): ... this new function.  All callers
	adjusted.
	(any_thread_of_process): Rename to ...
	(any_thread_of_inferior): ... this, and take an inferior pointer.
	(any_live_thread_of_process): Rename to ...
	(any_live_thread_of_inferior): ... this, and take an inferior
	pointer.
	(thread_stack_temporaries_enabled_p, push_thread_stack_temporary)
	(value_in_thread_stack_temporaries)
	(get_last_thread_stack_temporary): Take a thread pointer instead
	of a ptid_t.  Adjust all callers.
	(thread_info::set_running): New.
	(validate_registers_access): Use inferior_thread.
	(can_access_registers_ptid): Rename to ...
	(can_access_registers_thread): ... this, and take a thread
	pointer.
	(print_thread_info_1): Adjust to compare thread pointers instead
	of ptids.
	(switch_to_no_thread, switch_to_thread): Make extern.
	(scoped_restore_current_thread::~scoped_restore_current_thread):
	Use m_thread pointer directly.
	(scoped_restore_current_thread::scoped_restore_current_thread):
	Use inferior_thread.
	(thread_command): Use thread pointer directly.
	(thread_num_make_value_helper): Use inferior_thread.
	* top.c (execute_command): Use inferior_thread.
	* tui/tui-interp.c: Include "inferior.h".
	* varobj.c (varobj_create): Use inferior_thread.
	(value_of_root_1): Use find_thread_global_id instead of
	global_thread_id_to_ptid.
2018-06-21 17:09:31 +01:00
Sergio Durigan Junior f67c0c9171 Enable 'set print inferior-events' and improve detach/fork/kill/exit messages
This patch aims to turn 'set print inferior-events' always on, and do
some cleanup on the messages printed by GDB when various inferior
events happen (attach, detach, fork, kill, exit).

To make sure that the patch is correct, I've tested it with a handful
of combinations of 'set follow-fork-mode', 'set detach-on-fork' and
'set print inferior-events'.  In the end, I decided to make my
hand-made test into an official testcase.  More on that below.

Using the following program as an example:

  #include <unistd.h>
  int main ()
  {
    fork ();
    return 0;
  }

We see the following outputs from the patched GDB:

- With 'set print inferior-events on':

    (gdb) r
    Starting program: a.out
    [Detaching after fork from child process 27749]
    [Inferior 1 (process 27745) exited normally]
    (gdb)

- With 'set print inferior-events off':

    (gdb) r
    Starting program: a.out
    [Inferior 1 (process 27823) exited normally]
    (gdb)

  Comparing this against an unpatched GDB:

- With 'set print inferior-events off' and 'set follow-fork-mode
  child':

    (gdb) r
    Starting program: a.out
    [Inferior 2 (process 5993) exited normally]
    (gdb)

  Compare this against an unpatched GDB:

    (unpatched-gdb) r
    Starting program: a.out
    [New process 5702]
    [Inferior 2 (process 5702) exited normally]
    (unpatched-gdb)

  It is possible to notice that, in this scenario, the patched GDB
  will lose the '[New process %d]' message.

- With 'set print inferior-events on', 'set follow-fork-mode child'
  and 'set detach-on-fork on':

    (gdb) r
    Starting program: a.out
    [Attaching after process 27905 fork to child process 27909]
    [New inferior 2 (process 27909)]
    [Detaching after fork from parent process 27905]
    [Inferior 1 (process 27905) detached]
    [Inferior 2 (process 27909) exited normally]
    (gdb)

  Compare this output with an unpatched GDB, using the same settings:

    (unpatched-gdb) r
    Starting program: a.out
    [New inferior 28033]
    [Inferior 28029 detached]
    [New process 28033]
    [Inferior 2 (process 28033) exited normally]
    [Inferior 28033 exited]
    (unpatched-gdb)

As can be seen above, I've also made a few modifications to messages
that are printed when 'set print inferior-events' is on.  For example,
a few of the messages did not contain the '[' and ']' as
prefix/suffix, which led to a few inconsistencies like:

  Attaching after process 22995 fork to child process 22999.
  [New inferior 22999]
  Detaching after fork from child process 22999.
  [Inferior 22995 detached]
  [Inferior 2 (process 22999) exited normally]

So I took the opportunity and included the square brackets where
applicable.  I have also made the existing messages more uniform, by
always printing "Inferior %d (process %d)..." where applicable.  This
makes it easier to identify the inferior number and the PID number
from the messages.

As suggested by Pedro, the "[Inferior %d exited]" message from
'exit_inferior' has been removed, because it got duplicated when
'inferior-events' is on.  I'm also using the
'add_{thread,inferior}_silent' versions (instead of their verbose
counterparts) on some locations, also to avoid duplicated messages.
For example, a patched GDB with 'set print inferior-events on', 'set
detach-on-fork on' and 'set follow-fork-mode child', but using
'add_thread', would print:

  (gdb) run
  Starting program: a.out
  [Attaching after process 25088 fork to child process 25092.]
  [New inferior 25092]   <--- duplicated
  [Detaching after fork from child process 25092.]
  [Inferior 25088 detached]
  [New process 25092]    <--- duplicated
  [Inferior 2 (process 25092) exited normally]

But if we use 'add_thread_silent' (with the same configuration as
before):

  (gdb) run
  Starting program: a.out
  [Attaching after process 31606 fork to child process 31610]
  [New inferior 2 (process 31610)]
  [Detaching after fork from parent process 31606]
  [Inferior 1 (process 31606) detached]
  [Inferior 2 (process 31610) exited normally]

As for the tests, the configuration options being exercised are:

- follow-fork-mode: child/parent
- detach-on-fork: on/off
- print inferior-events: on/off

It was also necessary to perform adjustments on several testcases,
because the expected messages changed considerably.

Built and regtested on BuildBot, without regressions.

gdb/ChangeLog:
2018-04-24  Jan Kratochvil  <jan.kratochvil@redhat.com>
	    Sergio Durigan Junior  <sergiodj@redhat.com>
	    Pedro Alves  <palves@redhat.com>

	* infcmd.c (kill_command): Print message when inferior has
	been killed.
	* inferior.c (print_inferior_events): Remove 'static'.  Set as
	'1'.
	(add_inferior): Improve message printed when
	'print_inferior_events' is on.
	(exit_inferior): Remove message printed when
	'print_inferior_events' is on.
	(detach_inferior): Improve message printed when
	'print_inferior_events' is on.
	(initialize_inferiors): Use 'add_inferior_silent' to set
	'current_inferior_'.
	* inferior.h (print_inferior_events): Declare here as
	'extern'.
	* infrun.c (follow_fork_inferior): Print '[Attaching...]' or
	'[Detaching...]' messages when 'print_inferior_events' is on.
	Use 'add_thread_silent' instead of 'add_thread'.  Add '[' and ']'
	as prefix/suffix for messages.  Remove periods.  Fix erroneous
	'Detaching after fork from child...', replace it by '... from
	parent...'.
	(handle_vfork_child_exec_or_exit): Add '[' and ']' as
	prefix/suffix when printing 'Detaching...' messages.  Print
	them when 'print_inferior_events' is on.
	* remote.c (remote_detach_1): Print message when detaching
	from inferior and '!is_fork_parent'.

gdb/testsuite/ChangeLog:
2018-04-24  Jan Kratochvil  <jan.kratochvil@redhat.com>
	    Sergio Durigan Junior  <sergiodj@redhat.com>
	    Pedro Alves  <palves@redhat.com>

	* gdb.base/attach-non-pgrp-leader.exp: Adjust 'Detaching...'
	regexps to expect for '[Inferior ... detached]' as well.
	* gdb.base/attach.exp: Likewise.
	* gdb.base/catch-syscall.exp (check_for_program_end): Adjust
	"gdb_continue_to_end".
	(test_catch_syscall_with_wrong_args): Likewise.
	* gdb.base/foll-fork.exp: Adjust regexps to match '[' and
	']'.  Don't set 'verbose' on.
	* gdb.base/foll-vfork.exp: Likewise.
	* gdb.base/fork-print-inferior-events.c: New file.
	* gdb.base/fork-print-inferior-events.exp: New file.
	* gdb.base/hook-stop.exp: Adjust regexps to expect for new
	'[Inferior ... has been killed]' message.
	* gdb.base/kill-after-signal.exp: Likewise.
	* gdb.base/solib-overlap.exp: Adjust regexps to expect for new
	detach message.
	* gdb.threads/kill.exp: Adjust regexps to expect for new kill
	message.
	* gdb.threads/clone-attach-detach.exp: Adjust 'Detaching...'
	regexps to expect for '[Inferior ... detached]' as well.
	* gdb.threads/process-dies-while-detaching.exp: Likewise.
2018-04-24 15:46:15 -04:00
Yao Qi daf6667d1f Class readonly_detached_regcache
This patch adds a new class (type) for readonly regcache, which is
created via regcache::save.  readonly_detached_regcache inherits
readable_regcache.

gdb:

2018-02-21  Yao Qi  <yao.qi@linaro.org>

	* dummy-frame.c (dummy_frame_cache) <prev_regcache>: Use
	readonly_detached_regcache.
	(dummy_frame_prev_register): Use regcache->cooked_read.
	* frame.c (frame_save_as_regcache): Change return type.
	(frame_pop): Update.
	* frame.h (frame_save_as_regcache): Update declaration.
	* inferior.h (get_infcall_suspend_state_regcache): Update
	declaration.
	* infrun.c (infcall_suspend_state) <registers>: use
	readonly_detached_regcache.
	(save_infcall_suspend_state): Don't use regcache_dup.
	(get_infcall_suspend_state_regcache): Change return type.
	* linux-fork.c (struct fork_info) <savedregs>: Change to
	readonly_detached_regcache.
	<pc>: New field.
	(fork_save_infrun_state): Don't use regcache_dup.
	(info_checkpoints_command): Adjust.
	* mi/mi-main.c (register_changed_p): Update declaration.
	(mi_cmd_data_list_changed_registers): Use
	readonly_detached_regcache.
	(register_changed_p): Change parameter type to
	readonly_detached_regcache.
	* ppc-linux-tdep.c (ppu2spu_cache) <regcache>: Use
	readonly_detached_regcache.
	(ppu2spu_sniffer): Construct a new readonly_detached_regcache.
	* regcache.c (readonly_detached_regcache::readonly_detached_regcache):
	New.
	(regcache::save): Move it to reg_buffer.
	(regcache::restore): Change parameter type.
	(regcache_dup): Remove.
	* regcache.h (reg_buffer) <save>: New method.
	(readonly_detached_regcache): New class.
	* spu-tdep.c (spu2ppu_cache) <regcache>: Use
	readonly_detached_regcache.
	(spu2ppu_sniffer): Construct a new readonly_detached_regcache.
2018-02-21 11:20:03 +00:00
Pedro Alves e671cd59d7 Per-inferior target_terminal state, fix PR gdb/13211, more
In my multi-target branch I ran into problems with GDB's terminal
handling that exist in master as well, with multi-inferior debugging.

This patch adds a testcase for said problems
(gdb.multi/multi-term-settings.exp), fixes the problems, fixes PR
gdb/13211 as well (and adds a testcase for that too,
gdb.base/interrupt-daemon.exp).

The basis of the problem I ran into is the following.  Consider a
scenario where you have:

 - inferior 1 - started with "attach", process is running on some
   other terminal.

 - inferior 2 - started with "run", process is sharing gdb's terminal.

In this scenario, when you stop/resume both inferiors, you want GDB to
save/restore the terminal settings of inferior 2, the one that is
sharing GDB's terminal.  I.e., you want inferior 2 to "own" the
terminal (in target_terminal::is_ours/target_terminal::is_inferior
sense).

Unfortunately, that's not what you get currently.  Because GDB doesn't
know whether an attached inferior is actually sharing GDB's terminal,
it tries to save/restore its settings anyway, ignoring errors.  In
this case, this is pointless, because inferior 1 is running on a
different terminal, but GDB doesn't know better.

And then, because it is only possible to have the terminal settings of
a single inferior be in effect at a time, or make one inferior/pgrp be
the terminal's foreground pgrp (aka, only one inferior can "own" the
terminal, ignoring fork children here), if GDB happens to try to
restore the terminal settings of inferior 1 first, then GDB never
restores the terminal settings of inferior 2.

This patch fixes that and a few things more along the way:

 - Moves enum target_terminal::terminal_state out of the
   target_terminal class (it's currently private) and makes it a
   scoped enum so that it can be easily used elsewhere.

 - Replaces the inflow.c:terminal_is_ours boolean with a
   target_terminal_state variable.  This allows distinguishing is_ours
   and is_ours_for_output states.  This allows finally making
   child_terminal_ours_1 do something with its "output_only"
   parameter.

 - Makes each inferior have its own copy of the
   is_ours/is_ours_for_output/is_inferior state.

 - Adds a way for GDB to tell whether the inferior is sharing GDB's
   terminal.  Works best on Linux and Solaris; the fallback works just
   as well as currently.

 - With that, we can remove the inf->attach_flag tests from
   child_terminal_inferior/child_terminal_ours.

 - Currently target_ops.to_ours is responsible for both saving the
   current inferior's terminal state, and restoring gdb's state.
   Because each inferior has its own terminal state (possibly handled
   by different targets in a multi-target world, even), we need to
   split the inferior-saving part from the gdb-restoring part.  The
   patch adds a new target_ops.to_save_inferior target method for
   that.

 - Adds a new target_terminal::save_inferior() function, so that
   sequences like:

     scoped_restore_terminal_state save_state;
     target_terminal::ours_for_output ();

   ... restore back inferiors that were
   target_terminal_state::is_inferior before back to is_inferior, and
   leaves inferiors that were is_ours alone.

 - Along the way, this adds a default implementation of
   target_pass_ctrlc to inflow.c (for inf-child.c), that handles
   passing the Ctrl-C to a process running on GDB's terminal or to
   some other process otherwise.

 - Similarly, adds a new target default implementation of
   target_interrupt, for the "interrupt" command.  The current
   implementation of this hook in inf-ptrace.c kills the whole process
   group, but that's incorrect/undesirable because we may not be
   attached to all processes in the process group.  And also, it's
   incorrect because inferior_process_group() doesn't really return
   the inferior's real process group id if the inferior is not a
   process group leader...  This is the cause of PR gdb/13211 [1],
   which this patch fixes.  While at it, that target method's "ptid"
   parameter is eliminated, because it's not really used.

 - A new test is included that exercises and fixes PR gdb/13211, and
   also fixes a GDB issue reported on stackoverflow that I ran into
   while working on this [2].  The problem is similar to PR gdb/13211,
   except that it also triggers with Ctrl-C.  When debugging a daemon
   (i.e., a process that disconnects from the controlling terminal and
   is not a process group leader, then Ctrl-C doesn't work, you just
   can't interrupt the inferior at all, resulting in a hung debug
   session.  The problem is that since the inferior is no longer
   associated with gdb's session / controlling terminal, then trying
   to put the inferior in the foreground fails.  And so Ctrl-C never
   reaches the inferior directly.  pass_signal is only used when the
   inferior is attached, but that is not the case here.  This is fixed
   by the new child_pass_ctrlc.  Without the fix, the new
   interrupt-daemon.exp testcase fails with timeout waiting for a
   SIGINT that never arrives.

[1] PR gdb/13211 - Async / Process group and interrupt not working
https://sourceware.org/bugzilla/show_bug.cgi?id=13211

[2] GDB not reacting Ctrl-C when after fork() and setsid()
https://stackoverflow.com/questions/46101292/gdb-not-reacting-ctrl-c-when-after-fork-and-setsid

Note this patch does _not_ fix:

 - PR gdb/14559 - The 'interrupt' command does not work if sigwait is in use
   https://sourceware.org/bugzilla/show_bug.cgi?id=14559

 - PR gdb/9425 - When using "sigwait" GDB doesn't trap SIGINT. Ctrl+C terminates program when should break gdb.
   https://sourceware.org/bugzilla/show_bug.cgi?id=9425

The only way to fix that that I know of (without changing the kernel)
is to make GDB put inferiors in a separate session (create a
pseudo-tty master/slave pair, make the inferior run with the slave as
its terminal, and have gdb pump output/input on the master end).

gdb/ChangeLog:
2018-01-30  Pedro Alves  <palves@redhat.com>

	PR gdb/13211
	* config.in, configure: Regenerate.
	* configure.ac: Check for getpgid.
	* go32-nat.c (go32_pass_ctrlc): New.
	(go32_target): Install it.
	* inf-child.c (inf_child_target): Install
	child_terminal_save_inferior, child_pass_ctrlc and
	child_interrupt.
	* inf-ptrace.c (inf_ptrace_interrupt): Delete.
	(inf_ptrace_target): No longer install it.
	* infcmd.c (interrupt_target_1): Adjust.
	* inferior.h (child_terminal_save_inferior, child_pass_ctrlc)
	(child_interrupt): Declare.
	(inferior::terminal_state): New.
	* inflow.c (struct terminal_info): Update comments.
	(inferior_process_group): Delete.
	(terminal_is_ours): Delete.
	(gdb_tty_state): New.
	(child_terminal_init): Adjust.
	(is_gdb_terminal, sharing_input_terminal_1)
	(sharing_input_terminal): New functions.
	(child_terminal_inferior): Adjust.  Use sharing_input_terminal.
	Set the process's actual process group in the foreground if
	possible.  Handle is_ours_for_output/is_ours distinction.  Don't
	mark terminal as the inferior's if not sharing GDB's terminal.
	Don't check attach_flag.
	(child_terminal_ours_for_output, child_terminal_ours): Adjust to
	pass down a target_terminal_state.
	(child_terminal_save_inferior): New, factored out from ...
	(child_terminal_ours_1): ... this.  Handle
	target_terminal_state::is_ours_for_output.
	(child_interrupt, child_pass_ctrlc): New.
	(inflow_inferior_exit): Clear the inferior's terminal_state.
	(copy_terminal_info): Copy the inferior's terminal state.
	(_initialize_inflow): Remove reference to terminal_is_ours.
	* inflow.h (inferior_process_group): Delete.
	* nto-procfs.c (nto_handle_sigint, procfs_interrupt): Adjust.
	* procfs.c (procfs_target): Don't install procfs_interrupt.
	(procfs_interrupt): Delete.
	* remote.c (remote_serial_quit_handler): Adjust.
	(remote_interrupt): Remove ptid parameter.  Adjust.
	* target-delegates.c: Regenerate.
	* target.c: Include "terminal.h".
	(target_terminal::terminal_state): Rename to ...
	(target_terminal::m_terminal_state): ... this.
	(target_terminal::init): Adjust.
	(target_terminal::inferior): Adjust to per-inferior
	terminal_state.
	(target_terminal::restore_inferior, target_terminal_is_ours_kind): New.
	(target_terminal::ours, target_terminal::ours_for_output): Use
	target_terminal_is_ours_kind.
	(target_interrupt): Remove ptid parameter.  Adjust.
	(default_target_pass_ctrlc): Adjust.
	* target.h (target_ops::to_terminal_save_inferior): New field.
	(target_ops::to_interrupt): Remove ptid_t parameter.
	(target_interrupt): Remove ptid_t parameter.  Update comment.
	(target_pass_ctrlc): Update comment.
	* target/target.h (target_terminal_state): New scoped enum,
	factored out of ...
	(target_terminal::terminal_state): ... here.
	(target_terminal::inferior): Update comments.
	(target_terminal::restore_inferior): New.
	(target_terminal::is_inferior, target_terminal::is_ours)
	(target_terminal::is_ours_for_output): Adjust.
	(target_terminal::scoped_restore_terminal_state): Adjust to
	rename, and call restore_inferior() instead of inferior().
	(target_terminal::scoped_restore_terminal_state::m_state): Change
	type.
	(target_terminal::terminal_state): Rename to ...
	(target_terminal::m_terminal_state): ... this and change type.

gdb/gdbserver/ChangeLog:
2018-01-30  Pedro Alves  <palves@redhat.com>

	PR gdb/13211
	* target.c (target_terminal::terminal_state): Rename to ...
	(target_terminal::m_terminal_state): ... this.

gdb/testsuite/ChangeLog:
2018-01-30  Pedro Alves  <palves@redhat.com>

	PR gdb/13211
	* gdb.base/interrupt-daemon.c: New.
	* gdb.base/interrupt-daemon.exp: New.
	* gdb.multi/multi-term-settings.c: New.
	* gdb.multi/multi-term-settings.exp: New.
2018-01-30 14:55:18 +00:00
Simon Marchi bc09b0c14f Make linux_nat_detach/thread_db_detach use the inferior parameter
This patch makes these two functions actually use the inferior parameter
added by the previous patch, instead of reading inferior_ptid.  I chose
these two, because they are the one actually used when I detach on my
GNU/Linux system, so they were easy to test.

I took the opportunity to pass the inferior being detached to
inf_ptrace_detach_success, so it could use it too.  From there, it made
sense to add an overload of detach_inferior that takes the inferior
directly rather than the pid, to avoid having to pass inf->pid only for
the callee to look up the inferior structure by pid.

gdb/ChangeLog:

	* inf-ptrace.c (inf_ptrace_detach): Adjust call to
	inf_ptrace_detach_success.
	(inf_ptrace_detach_success): Add inferior parameter, use it
	instead of inferior_ptid, pass it to detach_inferior.
	* inf-ptrace.h (inf_ptrace_detach_success): Add inferior
	parameter.
	* inferior.c (detach_inferior): Add overload that takes an
	inferior object.
	* inferior.h (detach_inferior): Likewise.
	* linux-nat.c (linux_nat_detach): Use the inf parameter, don't
	use inferior_ptid, adjust call to inf_ptrace_detach_success.
	* linux-thread-db.c (thread_db_detach): Use inf parameter.
2018-01-19 11:48:11 -05:00
Joel Brobecker e2882c8578 Update copyright year range in all GDB files
gdb/ChangeLog:

        Update copyright year range in all GDB files
2018-01-02 07:38:06 +04:00
Simon Marchi 089354bb06 Create private_inferior class hierarchy
There are currently multiple definitions of private_inferior, defined in
remote.c and darwin-nat.h.  The patch that poisons XNEW and friends for
non-POD types trips on that, because private_inferior is freed in
~inferior(), where it is an opaque type.  Since the compiler can't tell
whether the type is POD, it gives an error.  Also, we can't start using
C++ features in these structures (make them non-POD) as long as there
are multiple definitions with the same name.  For these reasons, this
patch makes a class hierarchy, with private_inferior being the abstract
base class, and darwin_inferior & remote_inferior inheriting from it.
Destruction is done through the virtual destructor.

I stumbled on some suspicious code in the darwin implementation though.
darwin_check_new_threads does an XCNEW(darwin_thread_t) when it finds a
new thread, allocating a new structure for it (darwin_thread_t is a
typedef for private_thread_info).  It then VEC_safe_pushes it in a
vector defined as DEF_VEC_O (a vector of objects).  This means that the
structure content gets copied in the vector.  The thread_info object is
created with the XCNEW'ed structure as the private thread info, while
the rest of the code works with the instance in the vector.  We have
therefore two distinct instances of darwin_thread_t/private_thread_info
for each thread.  This is not really a problem in practice, because
thread_info::priv is not used in the darwin code.  I still find it weird
and far from ideal, so I tried to fix it by changing the vector to be a
vector of pointers.  There should now be a single instance of the
structure for each thread.  The deallocation of the
darwin_thread_t/private_thread_info structure is done by the thread_info
destructor.

I am able to build on macOS, but not really test, since the port seems a
bit broken.  I am not able to debug reliably on the machine I have
access to, which runs macOS 10.12.6.

gdb/ChangeLog:

	* inferior.h (private_inferior): Define structure type, add
	virtual pure destructor.
	(inferior) <priv>: Change type to unique_ptr.
	* inferior.c (private_inferior::~private_inferior): Provide
	default implementation.
	(inferior::~inferior): Don't free priv field.
	(exit_inferior_1): Likewise.
	* darwin-nat.h (struct darwin_exception_info): Initialize fields.
	(darwin_exception_info): Remove typedef.
	(DEF_VEC_O (darwin_thread_t)); Remove.
	(private_inferior): Rename to ...
	(darwin_private_inferior): ... this, extend private_inferior.
	(get_darwin_inferior): New.
	<threads>: Change type to std::vector of darwin_thread_t pointers.
	* darwin-nat.c (darwin_check_new_threads): Adjust.
	(find_inferior_task_it): Adjust.
	(darwin_find_thread); Adjust.
	(darwin_suspend_inferior): Adjust.
	(darwin_resume_inferior): Adjust.
	(darwin_find_new_inferior): Adjust.
	(darwin_decode_notify_message): Adjust.
	(darwin_send_reply): Adjust.
	(darwin_resume_inferior_threads): Adjust.
	(darwin_suspend_inferior_threads): Adjust.
	(darwin_decode_message): Adjust.
	(darwin_wait): Adjust.
	(darwin_interrupt): Adjust.
	(darwin_deallocate_threads): Adjust.
	(darwin_mourn_inferior): Adjust, don't free private data.
	(darwin_reply_to_all_pending_messages): Adjust.
	(darwin_stop_inferior): Adjust.
	(darwin_setup_exceptions): Adjust.
	(darwin_kill_inferior): Adjust.
	(darwin_setup_request_notification): Adjust.
	(darwin_attach_pid): Adjust.
	(darwin_init_thread_list): Adjust.
	(darwin_setup_fake_stop_event): Adjust.
	(darwin_attach): Adjust.
	(darwin_detach): Adjust.
	(darwin_xfer_partial): Adjust.
	(set_enable_mach_exceptions): Adjust.
	(darwin_pid_to_exec_file): Adjust.
	(darwin_get_ada_task_ptid): Adjust.
	* darwin-nat-info.c (get_task_from_args): Adjust.
	(info_mach_ports_command): Adjust.
	(info_mach_region_command): Adjust.
	(info_mach_exceptions_command): Adjust.
	* remote.c (private_inferior): Rename to ...
	(remote_private_inferior): ... this, initialize fields.
	(get_remote_inferior); New.
	(remote_commit_resume): Use get_remote_inferior.
	(check_pending_event_prevents_wildcard_vcont_callback): Likewise.
2017-11-24 10:39:31 -05:00
Tom Tromey 1d12d88f18 Constify add_info
This patch constifies add_info and updates all the info commands.  The
bulk of this patch was written using a script; and then I did a manual
pass to fix up the remaining compilation errors.

I could not compile every changed file; in particular nto-procfs.c,
gnu-nat.c, and darwin-nat-info.c; but I at least tried to check the
correctness by inspection.

gdb/ChangeLog
2017-11-07  Tom Tromey  <tom@tromey.com>

	* frame.h (info_locals_command, info_args_command): Constify.
	* auto-load.h (auto_load_info_scripts): Constify.
	* inferior.h (registers_info): Constify.
	* copying.c: Rebuild.
	* copying.awk: Constify generated commands.
	* auto-load.c (auto_load_info_scripts)
	(info_auto_load_gdb_scripts): Constify.
	* cli/cli-decode.c (struct cmd_list_element): Take a
	cmd_const_cfunc_ftype.
	* command.h (add_info): Take a cmd_const_cfunc_ftype.
	* tui/tui-win.c (tui_all_windows_info): Constify.
	* python/py-auto-load.c (info_auto_load_python_scripts):
	Constify.
	* cli/cli-cmds.c (show_command): Remove non-const overload.
	* tracepoint.c (info_tvariables_command, info_scope_command):
	Constify.
	(info_static_tracepoint_markers_command): Constify.
	* thread.c (info_threads_command): Constify.
	(print_thread_info_1): Constify.
	* target.c (info_target_command): Constify.
	* symtab.c (info_sources_command, info_functions_command)
	(info_types_command): Constify.
	(info_variables_command): Remove non-const overload.
	* symfile.c (info_ext_lang_command): Constify.
	* stack.c (info_frame_command, info_locals_command)
	(info_args_command): Constify.
	(backtrace_command): Remove non-const overload.
	* source.c (info_source_command, info_line_command): Constify.
	* solib.c (info_sharedlibrary_command): Constify.
	* skip.c (info_skip_command): Constify.
	* ser-go32.c (info_serial_command): Constify.
	* reverse.c (info_bookmarks_command): Constify.
	* printcmd.c (info_symbol_command, info_address_command)
	(info_display_command): Constify.
	* osdata.c (info_osdata_command): Constify.
	* objc-lang.c (info_selectors_command, info_classes_command):
	Constify.
	* nto-procfs.c (procfs_pidlist, procfs_meminfo): Constify.
	* memattr.c (info_mem_command): Constify.
	* macrocmd.c (info_macro_command, info_macros_command): Constify.
	* linux-fork.c (info_checkpoints_command): Constify.
	* infrun.c (info_signals_command): Constify.
	* inflow.c (info_terminal_command): Constify.
	* inferior.c (info_inferiors_command): Constify.
	(print_inferior): Constify.
	* infcmd.c (info_program_command, info_all_registers_command)
	(info_registers_command, info_vector_command)
	(info_float_command): Constify.
	(registers_info): Constify.
	* gnu-nat.c (info_send_rights_cmd, info_recv_rights_cmd)
	(info_port_sets_cmd, info_dead_names_cmd, info_port_rights_cmd):
	Constify.
	* f-valprint.c (info_common_command): Constify.
	* dcache.c (info_dcache_command): Constify.
	(dcache_info_1): Constify.
	* darwin-nat-info.c (info_mach_tasks_command)
	(info_mach_task_command, info_mach_ports_command)
	(info_mach_port_command, info_mach_threads_command)
	(info_mach_thread_command, info_mach_regions_command)
	(info_mach_regions_recurse_command, info_mach_region_command)
	(info_mach_exceptions_command): Constify.
	(get_task_from_args): Constify.
	* cp-support.c (info_vtbl_command): Constify.
	* breakpoint.c (info_watchpoints_command)
	(info_tracepoints_command): Constify.
	(info_breakpoints_command): Remove non-const overload.
	* avr-tdep.c (avr_io_reg_read_command): Constify.
	* auxv.c (info_auxv_command): Constify.
	* ada-tasks.c (info_tasks_command): Constify.
	(info_task): Constify.
	* ada-lang.c (info_exceptions_command): Constify.
2017-11-07 13:59:09 -07:00
Tom Tromey 0b39b52e6e Constify add_com
This changes add_com to take a cmd_const_cfunc_ftype, and then fixes
up all the command implementations.

In most cases this is trivial.

In a couple of places I had to again introduce a temporary non-const
overload.  These overloads will be removed when add_info is
constified.

gdb/ChangeLog
2017-11-07  Tom Tromey  <tom@tromey.com>

	* solib.h (no_shared_libraries): Constify.
	* frame.h (return_command): Constify.
	* cli/cli-cmds.h (quit_command): Constify.
	* top.h (quit_command, execute_command): Constify.
	* target.h (flash_erase_command): Constify.
	* inferior.h (set_inferior_args, attach_command): Constify.
	* tracepoint.h (start_tracing, stop_tracing): Constify.
	* breakpoint.h (break_command, tbreak_command)
	(hbreak_command_wrapper, thbreak_command_wrapper)
	(rbreak_command_wrapper, watch_command_wrapper)
	(awatch_command_wrapper, rwatch_command_wrapper)
	(get_tracepoint_by_number): Constify.
	* symtab.c (info_variables_command, rbreak_command)
	(symtab_symbol_info): Constify.
	(info_variables_command): Add non-const overload.
	* top.c (dont_repeat_command): Constify.
	* breakpoint.c (ignore_command, commands_command)
	(condition_command, tbreak_command, hbreak_command)
	(thbreak_command, clear_command, break_command)
	(info_breakpoints_command, watch_command, rwatch_command)
	(awatch_command, trace_command, ftrace_command, strace_command)
	(trace_pass_command, break_range_command, dprintf_command)
	(agent_printf_command, get_tracepoint_by_number)
	(watch_maybe_just_location, trace_pass_command): Constify.
	(info_breakpoints_command): Add non-const overload.
	* tracefile.c (tsave_command): Constify.
	* infcmd.c (attach_command, disconnect_command, signal_command)
	(queue_signal_command, stepi_command, nexti_command)
	(finish_command, next_command, step_command, until_command)
	(advance_command, jump_command, continue_command, run_command)
	(start_command, starti_command, interrupt_command)
	(run_command_1, set_inferior_args, step_1): Constify.
	* inferior.c (add_inferior_command, remove_inferior_command)
	(clone_inferior_command): Constify.
	* linux-fork.c (checkpoint_command, restart_command): Constify.
	* windows-nat.c (signal_event_command): Constify.
	* guile/guile.c (guile_repl_command, guile_command): Constify.
	* printcmd.c (x_command, display_command, printf_command)
	(output_command, set_command, call_command, print_command)
	(eval_command): Constify.
	(non_const_set_command): Remove.
	(_initialize_printcmd): Update.
	* source.c (forward_search_command, reverse_search_command):
	Constify.
	* jit.c (jit_reader_load_command, jit_reader_unload_command):
	Constify.
	* infrun.c (handle_command): Constify.
	* memattr.c (mem_command): Constify.
	* stack.c (return_command, up_command, up_silently_command)
	(down_command, down_silently_command, frame_command)
	(backtrace_command, func_command, backtrace_command_1): Constify.
	(backtrace_command): Add non-const overload.
	* remote-sim.c (simulator_command): Constify.
	* exec.c (set_section_command): Constify.
	* tracepoint.c (tdump_command, trace_variable_command)
	(tstatus_command, tstop_command, tstart_command)
	(end_actions_pseudocommand, while_stepping_pseudocommand)
	(collect_pseudocommand, teval_pseudocommand, actions_command)
	(start_tracing, stop_tracing): Constify.
	* value.c (init_if_undefined_command): Constify.
	* tui/tui-stack.c (tui_update_command): Constify.
	* tui/tui-win.c (tui_refresh_all_command)
	(tui_set_tab_width_command, tui_set_win_height_command)
	(tui_set_focus_command, tui_scroll_forward_command)
	(tui_scroll_backward_command, tui_scroll_left_command)
	(tui_scroll_right_command, parse_scrolling_args, tui_set_focus)
	(tui_set_win_height): Constify.
	* tui/tui-layout.c (tui_layout_command): Constify.
	* procfs.c (proc_trace_syscalls, proc_trace_sysentry_cmd)
	(proc_trace_sysexit_cmd, proc_untrace_sysentry_cmd)
	(proc_untrace_sysexit_cmd): Constify.
	* remote.c (threadlist_test_cmd, threadinfo_test_cmd)
	(threadset_test_cmd, threadlist_update_test_cmd)
	(threadalive_test): Constify.
	* objc-lang.c (print_object_command): Constify.
	* command.h (add_com): Constify.
	* cli/cli-dump.c (restore_command): Constify.
	* cli/cli-cmds.c (pwd_command, echo_command, quit_command)
	(help_command, complete_command, shell_command, edit_command)
	(list_command, disassemble_command, make_command)
	(apropos_command, alias_command): Constify.
	* cli/cli-script.c (document_command, define_command)
	(while_command, if_command, validate_comname): Constify.
	* cli/cli-decode.c (struct cmd_list_element): Change type of
	"fun".
	* target.c (do_monitor_command, flash_erase_command): Constify.
	* regcache.c (reg_flush_command): Constify.
	* reverse.c (reverse_step, reverse_next, reverse_stepi)
	(reverse_nexti, reverse_continue, reverse_finish)
	(save_bookmark_command, goto_bookmark_command)
	(exec_reverse_once): Constify.
	* python/python.c (python_interactive_command, python_command):
	Constify.
	* typeprint.c (ptype_command, whatis_command, whatis_exp):
	Constify.
	* solib.c (sharedlibrary_command, no_shared_libraries): Constify.
	* gcore.c (gcore_command): Constify.
2017-11-07 13:59:09 -07:00
Tom Tromey 981a3fb359 Constify add_prefix_cmd
This changes add_prefix_cmd to accept a const-taking function as an
argument; then fixes up all the callers.

In a couple of spots I had to add a non-const overload of a function,
because the function is passed to two different command-adding
"constructors".  These overloads are temporary; once constification is
complete they can be removed.

This patch also fixes a typo I happened to notice while constifying.

Note that this touches a couple of files (gnu-nat.c and go32-nat.c)
that I can't build.  So, while I made a best-effort there, I am not
certain they will still compile.

Tested by rebuilding.

gdb/ChangeLog
2017-10-11  Tom Tromey  <tom@tromey.com>

	* gdbthread.h (thread_command): Constify.
	* inferior.h (detach_command): Constify.
	* top.h (set_history, show_history): Constify.
	* arm-tdep.c (set_arm_command, show_arm_command): Constify.
	* serial.c (serial_set_cmd, serial_show_cmd): Constify.
	* bsd-kvm.c (bsd_kvm_cmd): Constify.
	* printcmd.c (set_command): Constify.
	(non_const_set_command): New function.
	* dcache.c (set_dcache_command, show_dcache_command): Constify.
	* breakpoint.c (enable_command, disable_command, delete_command)
	(catch_command, tcatch_command, set_breakpoint_cmd)
	(show_breakpoint_cmd): Constify.
	* macrocmd.c (macro_command): Constify.
	* infcmd.c (unset_command, kill_command, detach_command)
	(info_proc_cmd): Constify.
	* i386-tdep.c (set_mpx_cmd, show_mpx_cmd): Constify.
	* auto-load.c (show_auto_load_cmd, set_auto_load_cmd)
	(info_auto_load_cmd): Constify.
	* target-descriptions.c (set_tdesc_cmd, show_tdesc_cmd)
	(unset_tdesc_cmd): Constify.
	* ada-lang.c (set_ada_command, show_ada_command)
	(maint_set_ada_cmd, maint_show_ada_cmd): Constify.
	* guile/guile.c (set_guile_command, show_guile_command)
	(info_guile_command): Constify.
	* tui/tui-win.c (tui_command, set_tui_cmd, show_tui_cmd):
	Constify.
	* skip.c (skip_command): Constify.
	* compile/compile.c (_initialize_compile): Constify.
	* dwarf2read.c (set_dwarf_cmd, show_dwarf_cmd): Constify.
	* btrace.c (maint_btrace_cmd, maint_btrace_set_cmd)
	(maint_btrace_show_cmd, maint_btrace_pt_set_cmd)
	(maint_btrace_pt_show_cmd): Constify.
	* remote.c (set_remote_cmd, show_remote_cmd, remote_command):
	Constify.
	* python/python.c (user_show_python, user_set_python): Constify.
	* mips-tdep.c (set_mips_command, show_mips_command)
	(set_mipsfpu_command): Constify.
	* record-btrace.c (cmd_record_btrace_start)
	(cmd_set_record_btrace, cmd_show_record_btrace)
	(cmd_set_record_btrace_bts, cmd_show_record_btrace_bts)
	(cmd_set_record_btrace_pt, cmd_show_record_btrace_pt): Constify.
	* rs6000-tdep.c (set_powerpc_command, show_powerpc_command):
	Constify.
	* symfile.c (overlay_command): Constify.
	* spu-tdep.c (set_spu_command, show_spu_command): Constify.
	* cli/cli-logging.c (set_logging_command, show_logging_command):
	Constify.
	* cli/cli-dump.c (dump_command, append_command)
	(srec_dump_command, ihex_dump_command, verilog_dump_command)
	(tekhex_dump_command, binary_dump_command)
	(binary_append_command): Constify.
	* cli/cli-decode.c (struct cmd_list_element): Change type of
	"fun".
	* cli/cli-cmds.c (info_command, show_command, set_debug)
	(show_debug): Constify.
	(show_command): Add non-const overload.
	* top.c (set_history, show_history): Constify.
	* sh-tdep.c (set_sh_command, show_sh_command): Constify.
	* command.h (add_prefix_cmd): Accept a cmd_const_cfunc_ftype.
	* target.c (target_command): Constify.
	* sparc64-tdep.c (info_adi_command): Constify.
	* record-full.c (cmd_record_full_start): Constify.
	(set_record_full_command): Constify.  Fix typo.
	(show_record_full_command): Constify.
	* thread.c (thread_command, thread_apply_command): Constify.
	* memattr.c (dummy_cmd): Constify.
	* value.c (function_command): Constify.
	* frame.c (set_backtrace_cmd, show_backtrace_cmd): Constify.
	* probe.c (info_probes_command): Constify.
	* ser-tcp.c (set_tcp_cmd, show_tcp_cmd): Constify.
	* gnu-nat.c (set_task_cmd, show_task_cmd, set_thread_cmd)
	(show_thread_cmd, set_thread_default_cmd)
	(show_thread_default_cmd): Constify.
	(check_empty): Constify.
	* tracepoint.c (tfind_command): Constify.
	* cp-support.c (maint_cplus_command): Constify.
	* windows-tdep.c (info_w32_command): Constify.
	* record.c (cmd_record_start, set_record_command)
	(show_record_command, info_record_command, cmd_record_goto):
	Constify.
	* ravenscar-thread.c (set_ravenscar_command)
	(show_ravenscar_command): Constify.
	* utils.c (set_internal_problem_cmd, show_internal_problem_cmd):
	Constify.
	(add_internal_problem_command): Remove casts.
	* arc-tdep.c (maintenance_print_arc_command): Constify.
	* valprint.c (set_print, show_print, set_print_raw)
	(show_print_raw): Constify.
	* maint.c (maintenance_command, maintenance_info_command)
	(maintenance_print_command, maintenance_set_cmd)
	(maintenance_show_cmd, set_per_command_cmd)
	(show_per_command_cmd, maintenance_check_command): Constify.
	* language.c (set_check, show_check): Constify.
	* typeprint.c (show_print_type, set_print_type): Constify.
	* go32-nat.c (go32_info_dos_command): Constify.
2017-10-11 16:21:02 -06:00
Sergio Durigan Junior d092c5a246 Implement "set cwd" command on GDB
This commit adds new "set/show cwd" commands, which are used to
set/show the current working directory of the inferior that will be
started.

The idea here is that "set cwd" will become the de facto way of
setting the inferior's cwd.  Currently, the user can use "cd" for
that, but there are side effects: with "cd", GDB also switches to
another directory, and that can impact the loading of scripts and
other files.  With "set cwd", we separate the logic into a new
command.

To maintain backward compatibility, if the user issues a "cd" command
but doesn't use "set cwd", then the inferior's cwd will still be
changed according to what the user specified.  However, "set cwd" has
precedence over "cd", so it can always be used to override it.

"set cwd" works in the following way:

- If the user sets the inferior's cwd by using "set cwd", then this
  directory is saved into current_inferior ()->cwd and is used when
  the inferior is started (see below).

- If the user doesn't set the inferior's cwd by using "set cwd", but
  rather use the "cd" command as before, then this directory is
  inherited by the inferior because GDB will have chdir'd into it.

On Unix-like hosts, the way the directory is changed before the
inferior execution is by expanding the user set directory before the
fork, and then "chdir" after the call to fork/vfork on
"fork_inferior", but before the actual execution.  On Windows, the
inferior cwd set by the user is passed directly to the CreateProcess
call, which takes care of the actual chdir for us.

This way, we'll make sure that GDB's cwd is not affected by the user
set cwd.

gdb/ChangeLog:
2017-10-04  Sergio Durigan Junior  <sergiodj@redhat.com>

	* NEWS (New commands): Mention "set/show cwd".
	* cli/cli-cmds.c (_initialize_cli_cmds): Mention "set cwd" on
	"cd" command's help text.
	* common/common-inferior.h (get_inferior_cwd): New prototype.
	* infcmd.c (inferior_cwd_scratch): New global variable.
	(set_inferior_cwd): New function.
	(get_inferior_cwd): Likewise.
	(set_cwd_command): Likewise.
	(show_cwd_command): Likewise.
	(_initialize_infcmd): Add "set/show cwd" commands.
	* inferior.h (class inferior) <cwd>: New field.
	* nat/fork-inferior.c: Include "gdb_tilde_expand.h".
	(fork_inferior): Change inferior's cwd before its execution.
	* windows-nat.c (windows_create_inferior): Pass inferior's cwd
	to CreateProcess.

gdb/gdbserver/ChangeLog:
2017-10-04  Sergio Durigan Junior  <sergiodj@redhat.com>

	* inferiors.c (current_inferior_cwd): New global variable.
	(get_inferior_cwd): New function.
	* inferiors.h (struct process_info) <cwd>: New field.

gdb/doc/ChangeLog:
2017-10-04  Sergio Durigan Junior  <sergiodj@redhat.com>

	* gdb.texinfo (Starting your Program) <The working directory.>:
	Mention new "set cwd" command.
	(Working Directory) <Your Program's Working Directory>:
	Rephrase to explain that "set cwd" exists and is the default
	way to change the inferior's cwd.

gdb/testsuite/ChangeLog:
2017-10-04  Sergio Durigan Junior  <sergiodj@redhat.com>

	* gdb.base/set-cwd.c: New file.
	* gdb.base/set-cwd.exp: Likewise.
2017-10-04 01:59:30 -04:00
Yao Qi d654162044 Use DISABLE_COPY_AND_ASSIGN
We have many classes that copy cotr and assignment operator are deleted,
so this patch replaces these existing mechanical code with macro
DISABLE_COPY_AND_ASSIGN.

gdb:

2017-09-19  Yao Qi  <yao.qi@linaro.org>

	* annotate.h (struct annotate_arg_emitter): Use
	DISABLE_COPY_AND_ASSIGN.
	* common/refcounted-object.h (refcounted_object): Likewise.
	* completer.h (struct completion_result): Likewise.
	* dwarf2read.c (struct dwarf2_per_objfile): Likewise.
	* filename-seen-cache.h (filename_seen_cache): Likewise.
	* gdbcore.h (thread_section_name): Likewise.
	* gdb_regex.h (compiled_regex): Likewise.
	* gdbthread.h (scoped_restore_current_thread): Likewise.
	* inferior.h (scoped_restore_current_inferior): Likewise.
	* jit.c (jit_reader): Likewise.
	* linespec.h (struct linespec_result): Likewise.
	* mi/mi-parse.h (struct mi_parse): Likewise.
	* nat/fork-inferior.c (execv_argv): Likewise.
	* progspace.h (scoped_restore_current_program_space): Likewise.
	* python/python-internal.h (class gdbpy_enter): Likewise.
	* regcache.h (regcache): Likewise.
	* target-descriptions.c (struct tdesc_reg): Likewise.
	(struct tdesc_type): Likewise.
	(struct tdesc_feature): Likewise.
	* ui-out.h (ui_out_emit_type): Likewise.
2017-09-19 10:10:03 +01:00
Simon Marchi 11db943032 Rename some command functions
This patch renames a few functions implementing CLI commands to follow
the style <command-name>_command, so that they are easier to search for.

gdb/ChangeLog:

	* breakpoint.c (breakpoints_info): Rename to ...
	(info_breakpoints_command): ... this.
	(watchpoints_info): Rename to ...
	(info_watchpoints_command): ... this.
	(tracepoints_info): Rename to ...
	(info_tracepoints_command): ... this.
	(_initialize_breakpoint): Adjust.
	* dcache.c (dcache_info): Rename to ...
	(info_display_command): ... this.
	(_initialize_dcache): Adjust.
	* frame.h (args_info): Rename to ...
	(info_args_command): ... this.
	(locals_info): Rename to ...
	(info_locals_command): ... this.
	* infcmd.c (nofp_registers_info): Rename to ...
	(info_registers_command): ... this.
	(float_info): Rename to ...
	(info_float_command): ... this.
	(program_info): Rename to ...
	(info_program_command): ... this.
	(all_registers_info): Rename to ...
	(info_all_registers_command): ... this.
	(vector_info): Rename to ...
	(info_vector_command): ... this.
	(float_info): Rename to ...
	(info_float_command): ... this.
	(_initialize_infcmd): Adjust.
	* inferior.h (term_info): Rename to ...
	(info_terminal_command): ... this.
	* inflow.c (term_info): Rename to ...
	(info_terminal_command): ... this.
	(_initialize_inflow): Adjust.
	* infrun.c (signals_info): Rename to ...
	(info_signals_command): ... this.
	(_initialize_infrun): Adjust.
	* objc-lang.c (classes_info): Rename to ...
	(info_classes_command): ... this.
	(selectors_info): Rename to ...
	(info_selectors_command): ... this.
	(_initialize_objc_language): Adjust.
	* printcmd.c (sym_info): Rename to ...
	(info_symbol_command): ... this.
	(address_info): Rename to ...
	(info_address_command): ... this.
	(display_info): Rename to ...
	(info_display_command): ... this.
	(_initialize_printcmd): Adjust.
	* reverse.c (bookmarks_info): Rename to ...
	(info_breakpoints_command): ... this.
	(_initialize_reverse): Adjust.
	* ser-go32.c (dos_info): Rename to ...
	(info_serial_command): ... this.
	(_initialize_ser_dos): Adjust.
	* skip.c (skip_info): Rename to ...
	(info_skip_command): ... this.
	(_initialize_step_skip): Adjust.
	* source.c (line_info): Rename to ...
	(info_line_command): ... this.
	(source_info): Rename to ...
	(info_source_command)
	* stack.c (frame_info): Rename to ...
	(info_frame_command): ... this.
	(locals_info): Rename to ...
	(info_locals_command): ... this.
	(args_info): Rename to ...
	(info_args_command): ... this.
	(_initialize_stack): Adjust.
	* symtab.c (sources_info): Rename to ...
	(info_sources_command): ... this.
	(variables_info): Rename to ...
	(info_variables_command): ... this.
	(functions_info): Rename to ...
	(info_functions_command): ... this.
	(types_info): Rename to ...
	(info_types_command): ... this.
	(_initialize_symtab): Adjust.
	* target.c (target_info): Rename to ...
	(info_target_command): ... this.
	(initialize_targets): Adjust.
	* tracepoint.c (tvariables_info): Rename to ...
	(info_tvariables_command): ... this.
	(scope_info): Rename to ...
	(info_scope_command): ... this.
	(trace_dump_actions): Adjust.
	(_initialize_tracepoint): Adjust.
2017-08-22 22:09:55 +02:00
Tom Tromey 2989a3651d Remove save_inferior_ptid
This removes save_inferior_ptid, a cleanup function, in favor of
scoped_restore.

This also fixes a possible (it seems unlikely that it could happen in
practice) memory leak -- save_inferior_ptid should have used
make_cleanup_dtor, because it allocated memory.

I tested this on the buildbot.  However, there are two caveats to
this.  First, sometimes it seems I misread the results.  Second, I
think this patch touches some platforms that can't be tested by the
buildbot.  So, extra care seems warranted.

ChangeLog
2017-08-18  Tom Tromey  <tom@tromey.com>
	    Pedro Alves  <palves@redhat.com>

	* spu-multiarch.c (parse_spufs_run): Use scoped_restore.
	* sol-thread.c (sol_thread_resume, sol_thread_wait)
	(sol_thread_xfer_partial, rw_common): Use scoped_restore.
	* procfs.c (procfs_do_thread_registers): Use scoped_restore.
	* proc-service.c (ps_xfer_memory): Use scoped_restore.
	* linux-tdep.c (linux_corefile_thread): Remove a cleanup.
	(linux_get_siginfo_data): Add "thread" argument.  Use
	scoped_restore.
	* linux-nat.c (linux_child_follow_fork)
	(check_stopped_by_watchpoint): Use scoped_restore.
	* infrun.c (displaced_step_prepare_throw, write_memory_ptid)
	(THREAD_STOPPED_BY, handle_signal_stop): Use scoped_restore.
	(restore_inferior_ptid, save_inferior_ptid): Remove.
	* btrace.c (btrace_fetch): Use scoped_restore.
	* bsd-uthread.c (bsd_uthread_fetch_registers)
	(bsd_uthread_store_registers): Use scoped_restore.
	* breakpoint.c (reattach_breakpoints, detach_breakpoints): Use
	scoped_restore.
	* aix-thread.c (aix_thread_resume, aix_thread_wait)
	(aix_thread_xfer_partial): Use scoped_restore.
	* inferior.h (save_inferior_ptid): Remove.
2017-08-18 11:06:26 -06:00
Sergio Durigan Junior 9a6c7d9c02 C++ify gdb/common/environ.c
As part of the preparation necessary for my upcoming task, I'd like to
propose that we turn gdb_environ into a class.  The approach taken
here is simple: the class gdb_environ contains everything that is
needed to manipulate the environment variables.  These variables are
stored in an std::vector<char *>, which can be converted to a 'char
**' and passed as argument to functions that need it.

The usage has not changed much.  As per Pedro's suggestion, this class
uses a static factory method initialization.  This means that when an
instance is created, it is initially empty.  When needed, it has to be
initialized using the static method 'from_host_environ'.

As mentioned before, this is a preparation for an upcoming work that I
will be posting in the next few weeks or so.  For that work, I'll
probably create another data structure that will contain all the
environment variables that were set by the user using the 'set
environment' command, because I'll need access to them.  This will be
much easier with the class-ification of gdb_environ.

As noted, this has been regression-tested with the new version of
environ.exp and no regressions were found.

gdb/ChangeLog:
2017-06-20  Sergio Durigan Junior  <sergiodj@redhat.com>

	* Makefile.in (SUBDIR_UNITTESTS_SRCS): Add
	'unittests/environ-selftests.c'.
	(SUBDIR_UNITTESTS_OBS): Add 'environ-selftests.o'.
	* charset.c (find_charset_names): Declare object 'iconv_env'.
	Update code to use 'iconv_env' object.  Remove call to
	'free_environ'.
	* common/environ.c: Include <utility>.
	(make_environ): Delete function.
	(free_environ): Delete function.
	(gdb_environ::clear): New function.
	(gdb_environ::operator=): New function.
	(gdb_environ::get): Likewise.
	(environ_vector): Delete function.
	(set_in_environ): Delete function.
	(gdb_environ::set): New function.
	(unset_in_environ): Delete function.
	(gdb_environ::unset): New function.
	(gdb_environ::envp): Likewise.
	* common/environ.h: Include <vector>.
	(struct gdb_environ): Delete; transform into...
	(class gdb_environ): ... this class.
	(free_environ): Delete prototype.
	(init_environ, get_in_environ, set_in_environ, unset_in_environ,
	environ_vector): Likewise.
	* infcmd.c (run_command_1): Update code to call
	'envp' from 'gdb_environ' class.
	(environment_info): Update code to call methods from 'gdb_environ'
	class.
	(unset_environment_command): Likewise.
	(path_info): Likewise.
	(path_command): Likewise.
	* inferior.c (inferior::~inferior): Delete call to 'free_environ'.
	(inferior::inferior): Initialize 'environment' using the host's
	information.
	* inferior.h: Remove forward declaration of 'struct gdb_environ'.
	Include "environ.h".
	(class inferior) <environment>: Change type from 'struct
	gdb_environ' to 'gdb_environ'.
	* mi/mi-cmd-env.c (mi_cmd_env_path): Update code to call
	methods from 'gdb_environ' class.
	* solib.c (solib_find_1): Likewise
	* unittests/environ-selftests.c: New file.

gdb/gdbserver/ChangeLog:
2017-06-20  Sergio Durigan Junior  <sergiodj@redhat.com>

	* linux-low.c (linux_create_inferior): Adjust code to access the
	environment information via 'gdb_environ' class.
	* lynx-low.c (lynx_create_inferior): Likewise.
	* server.c (our_environ): Make it an instance of 'gdb_environ'.
	(get_environ): Return a pointer to 'our_environ'.
	(captured_main): Initialize 'our_environ'.
	* server.h (get_environ): Adjust prototype.
	* spu-low.c (spu_create_inferior): Adjust code to access the
	environment information via 'gdb_environ' class.
2017-06-20 08:59:27 -04:00
Sergio Durigan Junior 2090129c36 Share fork_inferior et al with gdbserver
This is the most important (and the biggest, sorry) patch of the
series.  It moves fork_inferior from gdb/fork-child.c to
nat/fork-inferior.c and makes all the necessary adjustments to both
GDB and gdbserver to make sure everything works OK.

There is no "most important change" with this patch; all changes are
made in a progressive way, making sure that gdbserver had the
necessary features while not breaking GDB at the same time.

I decided to go ahead and implement a partial support for starting the
inferior with a shell on gdbserver, although the full feature comes in
the next patch.  The user won't have the option to disable the
startup-with-shell, and also won't be able to change which shell
gdbserver will use (other than setting the $SHELL environment
variable, that is).

Everything is working as expected, and no regressions were present
during the tests.

gdb/ChangeLog:
2017-06-07  Sergio Durigan Junior  <sergiodj@redhat.com>
	    Pedro Alves  <palves@redhat.com>

	* Makefile.in (HFILES_NO_SRCDIR): Add "common/common-inferior.h"
	and "nat/fork-inferior.h".
	* common/common-inferior.h: New file, with contents from
	"gdb/inferior.h".
	* commom/common-utils.c: Include "common-utils.h".
	(stringify_argv): New function.
	* common/common-utils.h (stringify_argv): New prototype.
	* configure.nat: Add "fork-inferior.o" as a dependency for
	"*linux*", "fbsd*" and "nbsd*" hosts.
	* corefile.c (get_exec_file): Update comment.
	* darwin-nat.c (darwin_ptrace_him): Call "gdb_startup_inferior"
	instead of "startup_inferior".
	(darwin_create_inferior): Call "add_thread_silent" after
	"fork_inferior".
	* fork-child.c: Cleanup unnecessary includes.
	(SHELL_FILE): Move to "common/common-fork-child.c".
	(environ): Likewise.
	(exec_wrapper): Initialize.
	(get_exec_wrapper): New function.
	(breakup_args): Move to "common/common-fork-child.c"; rename to
	"breakup_args_for_exec".
	(escape_bang_in_quoted_argument): Move to
	"common/common-fork-child.c".
	(saved_ui): New variable.
	(prefork_hook): New function.
	(postfork_hook): Likewise.
	(postfork_child_hook): Likewise.
	(gdb_startup_inferior): Likewise.
	(fork_inferior): Move to "common/common-fork-child.c".  Update
	function to support gdbserver.
	(startup_inferior): Likewise.
	* gdbcore.h (get_exec_file): Remove declaration.
	* gnu-nat.c (gnu_create_inferior): Call "gdb_startup_inferior"
	instead of "startup_inferior".  Call "add_thread_silent" after
	"fork_inferior".
	* inf-ptrace.c: Include "nat/fork-inferior.h" and "utils.h".
	(inf_ptrace_create_inferior): Call "gdb_startup_inferior"
	instead of "startup_inferior".  Call "add_thread_silent" after
	"fork_inferior".
	* inferior.h: Include "common-inferior.h".
	(trace_start_error): Move to "common/common-utils.h".
	(trace_start_error_with_name): Likewise.
	(fork_inferior): Move prototype to "nat/fork-inferior.h".
	(startup_inferior): Likewise.
	(gdb_startup_inferior): New prototype.
	* nat/fork-inferior.c: New file, with contents from "fork-child.c".
	* nat/fork-inferior.h: New file.
	* procfs.c (procfs_init_inferior): Call "gdb_startup_inferior"
	instead of "startup_inferior".  Call "add_thread_silent" after
	"fork_inferior".
	* target.h (target_terminal_init): Move prototype to
	"target/target.h".
	(target_terminal_inferior): Likewise.
	(target_terminal_ours): Likewise.
	* target/target.h (target_terminal_init): New prototype, moved
	from "target.h".
	(target_terminal_inferior): Likewise.
	(target_terminal_ours): Likewise.
	* utils.c (gdb_flush_out_err): New function.

gdb/gdbserver/ChangeLog:
2017-06-07  Sergio Durigan Junior  <sergiodj@redhat.com>
	    Pedro Alves  <palves@redhat.com>

	* Makefile.in (SFILES): Add "nat/fork-inferior.o".
	* configure: Regenerate.
	* configure.srv (srv_linux_obj): Add "fork-child.o" and
	"fork-inferior.o".
	(i[34567]86-*-lynxos*): Likewise.
	(spu*-*-*): Likewise.
	* fork-child.c: New file.
	* linux-low.c: Include "common-inferior.h", "nat/fork-inferior.h"
	and "environ.h".
	(linux_ptrace_fun): New function.
	(linux_create_inferior): Adjust function prototype to reflect
	change on "target.h".  Adjust function code to use
	"fork_inferior".
	(linux_request_interrupt): Delete "signal_pid".
	* lynx-low.c: Include "common-inferior.h" and "nat/fork-inferior.h".
	(lynx_ptrace_fun): New function.
	(lynx_create_inferior): Adjust function prototype to reflect
	change on "target.h".  Adjust function code to use
	"fork_inferior".
	* nto-low.c (nto_create_inferior): Adjust function prototype and
	code to reflect change on "target.h".  Update comments.
	* server.c: Include "common-inferior.h", "nat/fork-inferior.h",
	"common-terminal.h" and "environ.h".
	(terminal_fd): Moved to fork-child.c.
	(old_foreground_pgrp): Likewise.
	(restore_old_foreground_pgrp): Likewise.
	(last_status): Make it global.
	(last_ptid): Likewise.
	(our_environ): New variable.
	(startup_with_shell): Likewise.
	(program_name): Likewise.
	(program_argv): Rename to...
	(program_args): ...this.
	(wrapper_argv): New variable.
	(start_inferior): Delete function.
	(get_exec_wrapper): New function.
	(get_exec_file): Likewise.
	(get_environ): Likewise.
	(prefork_hook): Likewise.
	(post_fork_inferior): Likewise.
	(postfork_hook): Likewise.
	(postfork_child_hook): Likewise.
	(handle_v_run): Update code to deal with arguments coming from the
	remote host.  Update calls from "start_inferior" to
	"create_inferior".
	(captured_main): Likewise.  Initialize environment variable.  Call
	"have_job_control".
	* server.h (post_fork_inferior): New prototype.
	(get_environ): Likewise.
	(last_status): Declare.
	(last_ptid): Likewise.
	(signal_pid): Likewise.
	* spu-low.c: Include "common-inferior.h" and "nat/fork-inferior.h".
	(spu_ptrace_fun): New function.
	(spu_create_inferior): Adjust function prototype to reflect change
	on "target.h".  Adjust function code to use "fork_inferior".
	* target.c (target_terminal_init): New function.
	(target_terminal_inferior): Likewise.
	(target_terminal_ours): Likewise.
	* target.h: Include <vector>.
	(struct target_ops) <create_inferior>: Update prototype.
	(create_inferior): Update macro.
	* utils.c (gdb_flush_out_err): New function.
	* win32-low.c (win32_create_inferior): Adjust function prototype
	and code to reflect change on "target.h".

gdb/testsuite/ChangeLog:
2017-06-07  Sergio Durigan Junior  <sergiodj@redhat.com>

	* gdb.server/non-existing-program.exp: Update regex in order to
	reflect the fact that gdbserver is now using fork_inferior (with a
	shell) to startup the inferior.
2017-06-07 19:56:09 -04:00
Pedro Alves 5ed8105e02 RAII-fy make_cleanup_restore_current_thread & friends
After all the make_cleanup_restore_current_thread fixing, I thought
I'd convert that and its relatives (which are all cleanups) to RAII
classes.

scoped_restore_current_pspace_and_thread was put in a separate file to
avoid a circular dependency.

Tested on x86-64 Fedora 23, native and gdbserver.

gdb/ChangeLog:
2017-05-04  Pedro Alves  <palves@redhat.com>

	* Makefile.in (SFILES): Add progspace-and-thread.c.
	(HFILES_NO_SRCDIR): Add progspace-and-thread.h.
	(COMMON_OBS): Add progspace-and-thread.o.
	* breakpoint.c: Include "progspace-and-thread.h".
	(update_inserted_breakpoint_locations)
	(insert_breakpoint_locations, create_longjmp_master_breakpoint):
	Use scoped_restore_current_pspace_and_thread.
	(create_std_terminate_master_breakpoint): Use
	scoped_restore_current_program_space.
	(remove_breakpoint): Use scoped_restore_current_pspace_and_thread.
	(print_breakpoint_location): Use
	scoped_restore_current_program_space.
	(bp_loc_is_permanent): Use
	scoped_restore_current_pspace_and_thread.
	(resolve_sal_pc): Use scoped_restore_current_pspace_and_thread.
	(download_tracepoint_locations): Use
	scoped_restore_current_pspace_and_thread.
	(breakpoint_re_set): Use scoped_restore_current_pspace_and_thread.
	* exec.c (exec_close_1): Use scoped_restore_current_program_space.
	(enum step_over_calls_kind): Moved from inferior.h.
	(class scoped_restore_current_thread): New class.
	* gdbthread.h (make_cleanup_restore_current_thread): Delete
	declaration.
	(scoped_restore_current_thread): New class.
	* infcmd.c: Include "common/gdb_optional.h".
	(continue_1, proceed_after_attach): Use
	scoped_restore_current_thread.
	(notice_new_inferior): Use scoped_restore_current_thread.
	* inferior.c: Include "progspace-and-thread.h".
	(restore_inferior, save_current_inferior): Delete.
	(add_inferior_command, clone_inferior_command): Use
	scoped_restore_current_pspace_and_thread.
	* inferior.h (scoped_restore_current_inferior): New class.
	* infrun.c: Include "progspace-and-thread.h" and
	"common/gdb_optional.h".
	(follow_fork_inferior): Use
	scoped_restore_current_pspace_and_thread.
	(scoped_restore_exited_inferior): New class.
	(handle_vfork_child_exec_or_exit): Use
	scoped_restore_exited_inferior,
	scoped_restore_current_pspace_and_thread,
	scoped_restore_current_thread and scoped_restore.
	(fetch_inferior_event): Use scoped_restore_current_thread.
	* linespec.c (decode_line_full, decode_line_1): Use
	scoped_restore_current_program_space.
	* mi/mi-main.c: Include "progspace-and-thread.h".
	(exec_continue): Use scoped_restore_current_thread.
	(mi_cmd_exec_run): Use scoped_restore_current_pspace_and_thread.
	(mi_cmd_trace_frame_collected): Use scoped_restore_current_thread.
	* proc-service.c (ps_pglobal_lookup): Use
	scoped_restore_current_program_space.
	* progspace-and-thread.c: New file.
	* progspace-and-thread.h: New file.
	* progspace.c (release_program_space, clone_program_space): Use
	scoped_restore_current_program_space.
	(restore_program_space, save_current_program_space)
	(save_current_space_and_thread): Delete.
	(switch_to_program_space_and_thread): Moved to
	progspace-and-thread.c.
	* progspace.h (save_current_program_space)
	(save_current_space_and_thread): Delete declarations.
	(scoped_restore_current_program_space): New class.
	* remote.c (remote_btrace_maybe_reopen): Use
	scoped_restore_current_thread.
	* symtab.c: Include "progspace-and-thread.h".
	(skip_prologue_sal): Use scoped_restore_current_pspace_and_thread.
	* thread.c (print_thread_info_1): Use
	scoped_restore_current_thread.
	(struct current_thread_cleanup): Delete.
	(do_restore_current_thread_cleanup)
	(restore_current_thread_cleanup_dtor): Rename/convert both to ...
	(scoped_restore_current_thread::~scoped_restore_current_thread):
	... this new dtor.
	(make_cleanup_restore_current_thread): Rename/convert to ...
	(scoped_restore_current_thread::scoped_restore_current_thread):
	... this new ctor.
	(thread_apply_all_command): Use scoped_restore_current_thread.
	(thread_apply_command): Use scoped_restore_current_thread.
	* tracepoint.c (tdump_command): Use scoped_restore_current_thread.
	* varobj.c (value_of_root_1): Use scoped_restore_current_thread.
2017-05-04 15:18:44 +01:00
Pedro Alves 3a3fd0fd2c Fix removing inferiors from within "thread apply" commands
This patch fixes an internal error exposed by a test that does
something like:

  define kill-and-remove
    kill inferiors 2
    remove-inferiors 2
  end

  # Start one inferior.
  start

  # Start another inferior.
  add-inferior 2
  inferior 2
  start

  # Kill and remove inferior 1 while inferior 2 is selected.
  thread apply 1.1 kill-and-remove

The internal error looks like this:

 Thread 1.1 (Thread 0x7ffff7fc2700 (LWP 20677)):
 [Switching to inferior 1 [process 20677] (gdb/testsuite/outputs/gdb.threads/threadapply/threadapply)]
 [Switching to thread 1.1 (Thread 0x7ffff7fc2700 (LWP 20677))]
 #0  main () at src/gdb/testsuite/gdb.threads/threadapply.c:38
 38          for (i = 0; i < NUM; i++)
 src/gdb/inferior.c:66: internal-error: void set_current_inferior(inferior*): Assertion `inf != NULL' failed.
 A problem internal to GDB has been detected,
 further debugging may prove unreliable.
 Quit this debugging session? (y or n) FAIL: gdb.threads/threadapply.exp: kill_and_remove_inferior: try kill-and-remove: thread apply 1.1 kill-and-remove (GDB internal error)

There are several problems around this area of the code.  One is that
in do_restore_current_thread_cleanup, we do a look up of inferior by
ptid, which can find the wrong inferior if the previously selected
inferior exited and some other inferior was started with a reused pid
(rare, but still...).

The other problem is that the "remove-inferiors" command rejects
attempts to remove the current inferior, but when we get to
"remove-inferiors" in a "thread apply THR remove-inferiors 2" command,
the current inferior is the inferior of thread THR, not the previously
selected inferior, so if the previously selected inferior was inferior
2, that command still manages to wipe it, and then gdb restores the
old selected inferior, which is now a dangling pointer...

So the fix here is:

- Make make_cleanup_restore_current_thread store a pointer to the
  previously selected inferior directly, and use it directly instead
  of doing ptid look ups.

- Add a refcount to inferiors, very similar to thread_info's refcount,
  that is incremented/decremented by
  make_cleanup_restore_current_thread, and checked before deleting an
  inferior.  To avoid duplication, a new refcounted_object type is
  added, that both thread_info and inferior inherit from.

gdb/ChangeLog:
2017-04-19  Pedro Alves  <palves@redhat.com>

	* common/refcounted-object.h: New file.
	* gdbthread.h: Include "common/refcounted-object.h".
	(thread_info): Inherit from refcounted_object and add comments.
	(thread_info::incref, thread_info::decref)
	(thread_info::m_refcount): Delete.
	(thread_info::deletable): Use the refcounted_object::refcount()
	method.
	* inferior.c (current_inferior_): Add comment.
	(set_current_inferior): Increment/decrement refcounts.
	(prune_inferiors, remove_inferior_command): Skip inferiors marked
	not-deletable instead of comparing with the current inferior.
	(initialize_inferiors): Increment the initial inferior's refcount.
	* inferior.h (struct inferior): Forward declare.
	Include "common/refcounted-object.h".
	(current_inferior, set_current_inferior): Move declaration to
	before struct inferior's definition, and fix comment.
	(inferior): Inherit from refcounted_object.  Add comments.
	* thread.c (switch_to_thread_no_regs): Reference the thread's
	inferior pointer directly instead of doing a ptid lookup.
	(switch_to_no_thread): New function.
	(switch_to_thread(thread_info *)): New function, factored out
	from ...
	(switch_to_thread(ptid_t)): ... this.
	(restore_current_thread): Delete.
	(current_thread_cleanup): Remove 'inf_id' and 'was_removable'
	fields, and add 'inf' field.
	(do_restore_current_thread_cleanup): Check whether old->inf is
	alive instead of looking up an inferior by ptid.  Use
	switch_to_thread and switch_to_no_thread.
	(restore_current_thread_cleanup_dtor): Use old->inf directly
	instead of lookup up an inferior by id.  Decref the inferior.
	Don't restore 'removable'.
	(make_cleanup_restore_current_thread): Same the inferior pointer
	in old, instead of the inferior number.  Incref the inferior.
	Don't save/clear 'removable'.

gdb/testsuite/ChangeLog:
2017-04-19  Pedro Alves  <palves@redhat.com>

	* gdb.threads/threadapply.exp (kill_and_remove_inferior): New
	procedure.
	(top level): Call it.
	* lib/gdb.exp (gdb_define_cmd): New procedure.
2017-04-19 13:12:23 +01:00
Pedro Alves 9bcb1f1630 Make inferior::detaching a bool, and introduce scoped_restore::release()
I left making inferior::detaching a bool to a separate patch, because
doing that makes a make_cleanup_restore_integer call in
infrun.c:prepare_for_detach no longer compile (passing a 'bool *' when
an 'int *' is expected).  Since we want to get rid of cleanups anyway,
I looked at converting that to a scoped_restore.  However,
prepare_for_detach wants to discard the cleanup on success, and
scoped_restore doesn't have an equivalent for that.  So I added one --
I called it "release()" because it seems like a natural fit in the way
standard components call similarly-spirited methods, and, it's also
what the proposal for a generic scope guard calls it too, AFAICS:

  http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4189.pdf

I've added some scoped_guard unit tests, while at it.

gdb/ChangeLog:
2017-04-19  Pedro Alves  <palves@redhat.com>

	* Makefile.in (SUBDIR_UNITTESTS_SRCS): Add
	unittests/scoped_restore-selftests.c.
	(SUBDIR_UNITTESTS_OBS): Add scoped_restore-selftests.o.
	* common/scoped_restore.h (scoped_restore_base): Make "class".
	(scoped_restore_base::release): New public method.
	(scoped_restore_base::scoped_restore_base): New protected ctor.
	(scoped_restore_base::m_saved_var): New protected field.
	(scoped_restore_tmpl::scoped_restore_tmpl(T*)): Initialize the
	scoped_restore_base base class instead of m_saved_var directly.
	(scoped_restore_tmpl::scoped_restore_tmpl(T*, T2)): Likewise.
	(scoped_restore_tmpl::scoped_restore_tmpl(const
	scoped_restore_tmpl<T>&)): Likewise.
	(scoped_restore_tmpl::~scoped_restore_tmpl): Use the saved_var
	method.
	(scoped_restore_tmpl::saved_var): New method.
	(scoped_restore_tmpl::m_saved_var): Delete.
	* inferior.h (inferior::detaching): Now a bool.
	* infrun.c (prepare_for_detach): Use a scoped_restore instead of a
	cleanup.
	* unittests/scoped_restore-selftests.c: New file.
2017-04-19 13:12:23 +01:00
Pedro Alves 0550c95595 Make inferior a class with cdtors, and use new/delete
struct inferior became a non-POD when enum_flags was made a non-POD,
so we should be allocating/destroying inferiors with new/delete, etc.
That's what this commit does.

Note: this commit makes all boolean fields of inferior be "bool",
except the "detaching" field.  That'll require more work, so I split
it to a separate patch.

gdb/ChangeLog:
2017-04-13  Pedro Alves  <palves@redhat.com>

	* inferior.c (free_inferior): Convert to ...
	(inferior::~inferior): ... this dtor.
	(inferior::inferior): New ctor, factored out from ...
	(add_inferior_silent): ... here.  Allocate the inferior with a new
	expression.
	(delete_inferior): Call delete instead of free_inferior.
	* inferior.h (gdb_environ, continuation): Forward declare.
	(inferior): Now a class.  Add in-class initialization to all
	members.  Make boolean fields bool, except 'detaching'.
	(inferior::inferior): New explicit ctor.
	(inferior::~inferior): New.
2017-04-13 16:18:16 +01:00
Pedro Alves e3d60dfc00 GC inferior.c:init_inferior_list
Not used anywhere.  This was actually never used.  It came in because
I originally created inferior.c by copying thread.c, and doing
s/thread/inferior/g, and missed that nothing needs this.  :-)

gdb/ChangeLog:
2017-04-13  Pedro Alves  <palves@redhat.com>

	* inferior.c (init_inferior_list): Delete.
	* inferior.h (init_inferior_list): Delete.
2017-04-13 16:18:16 +01:00
Sergio Durigan Junior 7c5ded6a00 C++-fy and prepare for sharing fork_inferior
As a preparation for the next patch, which will move fork_inferior
from GDB to common/ (and therefore share it with gdbserver), it is
interesting to convert a few functions to C++.

This patch touches functions related to parsing command-line arguments
to the inferior (see gdb/fork-child.c:breakup_args), the way the
arguments are stored on fork_inferior (using std::vector instead of
char **), and the code responsible for dealing with argv also on
gdbserver.

I've taken this opportunity and decided to constify a few arguments to
fork_inferior/create_inferior as well, in order to make the code
cleaner.  And now, on gdbserver, we're using xstrdup everywhere and
aren't checking for memory allocation failures anymore, as requested
by Pedro:

  <https://sourceware.org/ml/gdb-patches/2017-03/msg00191.html>
  Message-Id: <025ebdb9-90d9-d54a-c055-57ed2406b812@redhat.com>

  Pedro Alves wrote:

  > On the "== NULL" check: IIUC, the old NULL check was there to
  > handle strdup returning NULL due to out-of-memory.
  > See NULL checks and comments further above in this function.
  > Now that you're using a std::vector, that doesn't work or make
  > sense any longer, since if push_back fails to allocate space for
  > its internal buffer (with operator new), our operator new replacement
  > (common/new-op.c) calls malloc_failure, which aborts gdbserver.
  >
  > Not sure it makes sense to handle out-of-memory specially in
  > the gdb/rsp-facing functions nowadays (maybe git blame/log/patch
  > submission for that code shows some guidelines).  Maybe (or, probably)
  > it's OK to stop caring about it, but then we should consistently remove
  > left over code, by using xstrdup instead and remove the NULL checks.

IMO this refactoring was very good to increase the readability of the
code as well, because some parts of the argument handling were
unnecessarily confusing before.

gdb/ChangeLog:
2017-04-12  Sergio Durigan Junior  <sergiodj@redhat.com>

	* common/common-utils.c (free_vector_argv): New function.
	* common/common-utils.h: Include <vector>.
	(free_vector_argv): New prototype.
	* darwin-nat.c (darwin_create_inferior): Rewrite function
	prototype in order to constify "exec_file" and accept a
	"std::string" for "allargs".
	* fork-child.c: Include <vector>.
	(breakup_args): Rewrite function, using C++.
	(fork_inferior): Rewrite function header, constify "exec_file_arg"
	and accept "std::string" for "allargs".  Update the code to
	calculate "argv" based on "allargs".  Update calls to "exec_fun"
	and "execvp".
	* gnu-nat.c (gnu_create_inferior): Rewrite function prototype in
	order to constify "exec_file" and accept a "std::string" for
	"allargs".
	* go32-nat.c (go32_create_inferior): Likewise.
	* inf-ptrace.c (inf_ptrace_create_inferior): Likewise.
	* infcmd.c (run_command_1): Constify "exec_file".  Use
	"std::string" for inferior arguments.
	* inferior.h (fork_inferior): Update prototype.
	* linux-nat.c (linux_nat_create_inferior): Rewrite function
	prototype in order to constify "exec_file" and accept a
	"std::string" for "allargs".
	* nto-procfs.c (procfs_create_inferior): Likewise.
	* procfs.c (procfs_create_inferior): Likewise.
	* remote-sim.c (gdbsim_create_inferior): Likewise.
	* remote.c (extended_remote_run): Update code to accept
	"std::string" as argument.
	(extended_remote_create_inferior): Rewrite function prototype in
	order to constify "exec_file" and accept a "std::string" for
	"allargs".
	* rs6000-nat.c (super_create_inferior): Likewise.
	(rs6000_create_inferior): Likewise.
	* target.h (struct target_ops) <to_create_inferior>: Likewise.
	* windows-nat.c (windows_create_inferior): Likewise.

gdb/gdbserver/ChangeLog:
2017-04-12  Sergio Durigan Junior  <sergiodj@redhat.com>

	* server.c: Include <vector>.
	<program_argv, wrapper_argv>: Convert to std::vector.
	(start_inferior): Rewrite function to use C++.
	(handle_v_run): Likewise.  Update code that calculates the argv
	based on the vRun packet; use C++.
	(captured_main): Likewise.
2017-04-12 01:02:03 -04:00
Sergio Durigan Junior 0db8980cc0 PR gdb/16188: Verify PTRACE_TRACEME succeeded
This patch fixes PR gdb/16188, which is about the fact that
fork_inferior doesn't verify the return value of the "traceme_fun"
callback.  On most targets, this callback is actually a wrapper to a
ptrace call that does a PTRACE_TRACEME on the forked GDB process that
will eventually become the inferior.

Thanks to Pedro, this second version of the patch is simpler and more
more logical.  Basically, two helper functions are added:
trace_start_error and trace_start_error_with_name.  The former can be
used when there is a customized error message to be printed to the
user.  The latter works like perror_with_name, so you just need to
pass the function that error'd.

Both helper functions mentioned above do basically the same thing:
print the error message to stderr and call _exit, properly terminating
the forked inferior.

Most of the patch takes care of guarding the necessary system calls
against errors on the "traceme_fun" callbacks.  It is not right to
call error on these situations, so I've replaced these calls with the
proper helper function call.

Regression-tested on BuildBot.

Thanks,

gdb/ChangeLog:
2017-02-20  Sergio Durigan Junior  <sergiodj@redhat.com>
	    Pedro Alves  <palves@redhat.com>

	PR gdb/16188
	* darwin-nat.c (darwin_ptrace_me): Check if calls to system
	calls succeeded.
	* fork-child.c (trace_start_error): New function.
	(trace_start_error_with_name): Likewise.
	* gnu-nat.c (gnu_ptrace_me): Check if call to PTRACE succeeded.
	* inf-ptrace.c (inf_ptrace_me): Likewise.
	* inferior.h (trace_start_error): New prototype.
	(trace_start_error_with_name): Likewise.
2017-02-20 07:48:44 -05:00
Joel Brobecker 61baf725ec update copyright year range in GDB files
This applies the second part of GDB's End of Year Procedure, which
updates the copyright year range in all of GDB's files.

gdb/ChangeLog:

        Update copyright year range in all GDB files.
2017-01-01 10:52:34 +04:00
Pedro Alves b15cc25cbe Make symfile_add_flags and objfile->flags strongly typed
This makes these flag types be "enum flag" types.  The benefit is
making use of C++'s stronger typing -- mixing the flags types by
mistake errors at compile time.

This caught one old bug in symbol_file_add_main_1 already, fixed by
this patch as well:

  @@ -1318,7 +1326,7 @@ symbol_file_add_main_1 (const char *args, int from_tty, int flags)
	what is frameless.  */
     reinit_frame_cache ();

  -  if ((flags & SYMFILE_NO_READ) == 0)
  +  if ((add_flags & SYMFILE_NO_READ) == 0)
       set_initial_language ();
   }

Above, "flags" are objfile flags, not symfile_add_flags.  So that was
actually checking for "flag & OBJF_PSYMTABS_READ", which has the same
value as SYMFILE_NO_READ...

I moved the flags definitions to separate files to break circular
dependencies.

Built with --enable-targets=all and tested on x86-64 Fedora 23.

gdb/ChangeLog:
2016-10-26  Pedro Alves  <palves@redhat.com>

	* coffread.c (coff_symfile_read): Use symfile_add_flags.
	* dbxread.c (dbx_symfile_read): Ditto.
	* elfread.c (elf_symfile_read): Ditto.
	* inferior.h: Include symfile-add-flags.h.
	(struct inferior) <symfile_flags>: Now symfile_add_flags.
	* machoread.c (macho_add_oso_symfile, macho_symfile_read_all_oso)
	(macho_symfile_read, mipscoff_symfile_read): Use
	symfile_add_flags.
	* objfile-flags.h: New file.
	* objfiles.c (allocate_objfile): Use objfile_flags.
	* objfiles.h: Include objfile-flags.h.
	(struct objfile) <flags>: Now an objfile_flags.
	(OBJF_REORDERED, OBJF_SHARED, OBJF_READNOW, OBJF_USERLOADED)
	(OBJF_PSYMTABS_READ, OBJF_MAINLINE, OBJF_NOT_FILENAME): Delete.
	Converted to an enum-flags in objfile-flags.h.
	(allocate_objfile): Use objfile_flags.
	* python/py-objfile.c (objfpy_add_separate_debug_file): Remove
	unnecessary local.
	* solib.c (solib_read_symbols, solib_add)
	(reload_shared_libraries_1): Use symfile_add_flags.
	* solib.h: Include "symfile-add-flags.h".
	(solib_read_symbols): Use symfile_add_flags.
	* symfile-add-flags.h: New file.
	* symfile-debug.c (debug_sym_read): Use symfile_add_flags.
	* symfile-mem.c (symbol_file_add_from_memory): Use
	symfile_add_flags.
	* symfile.c (read_symbols, syms_from_objfile_1)
	(syms_from_objfile, finish_new_objfile): Use symfile_add_flags.
	(symbol_file_add_with_addrs): Use symfile_add_flags and
	objfile_flags.
	(symbol_file_add_separate): Use symfile_add_flags.
	(symbol_file_add_from_bfd, symbol_file_add): Use symfile_add_flags
	and objfile_flags.
	(symbol_file_add_main_1): : Use objfile_flags.  Fix add_flags vs
	flags confusion.
	(symbol_file_command): Use objfile_flags.
	(add_symbol_file_command): Use symfile_add_flags and
	objfile_flags.
	(clear_symtab_users): Use symfile_add_flags.
	* symfile.h: Include "symfile-add-flags.h" and "objfile-flags.h".
	(struct sym_fns) <sym_read>: Use symfile_add_flags.
	(clear_symtab_users): Use symfile_add_flags.
	(enum symfile_add_flags): Delete, moved to symfile-add-flags.h and
	converted to enum-flags.
	(symbol_file_add, symbol_file_add_from_bfd)
	(symbol_file_add_separate): Use symfile_add_flags.
	* xcoffread.c (xcoff_initial_scan): Use symfile_add_flags.
2016-10-26 16:47:10 +01:00
Pedro Alves 85ad3aaf40 gdb: Coalesce/aggregate (async) vCont packets/actions
Currently, with "maint set target-non-stop on", that is, when gdb
connects with the non-stop/asynchronous variant of the remote
protocol, even with "set non-stop off", GDB always sends one vCont
packet per thread resumed.  This patch makes GDB aggregate and
coalesce vCont packets, so we send vCont packets like "vCont;s:p1.1;c"
in non-stop mode too.

Basically, this is done by:

  - Adding a new target method target_commit_resume that is called
    after calling target_resume one or more times.  When resuming a
    batch of threads, we'll only call target_commit_resume once after
    calling target_resume for all threads.

  - Making the remote target defer sending the actual vCont packet to
    target_commit_resume.

Special care must be taken to avoid sending a vCont action with a
"wildcard" thread-id (all threads of process / all threads) when that
would resume threads/processes that should not be resumed.  See
remote_commit_resume comments for details.

Unlike all-stop's remote_resume implementation, this handles the case
of too many actions resulting in a too-big vCont packet, by flushing
the vCont packet and starting a new one.

E.g., imagining that the "c" action in:

  vCont;s:1;c

overflows the packet buffer, we split the actions like:

  vCont;s:1
  vCont;c

Tested on x86_64 Fedora 20, with and without "maint set
target-non-stop on".

Also tested with a hack that makes remote_commit_resume flush the vCont
packet after every action appended (which caught a few bugs).

gdb/ChangeLog:
2016-10-26  Pedro Alves  <palves@redhat.com>

	* inferior.h (ALL_NON_EXITED_INFERIORS): New macro.
	* infrun.c (do_target_resume): Call target_commit_resume.
	(proceed): Defer target_commit_resume while looping over threads,
	resuming them.  Call target_commit_resume at the end.
	* record-btrace.c (record_btrace_commit_resume): New function.
	(init_record_btrace_ops): Install it as to_commit_resume method.
	* record-full.c (record_full_commit_resume): New function.
	(record_full_wait_1): Call the beneath target's to_commit_resume
	method.
	(init_record_full_ops): Install record_full_commit_resume as
	to_commit_resume method.
	* remote.c (struct private_thread_info) <last_resume_step,
	last_resume_sig, vcont_resumed>: New fields.
	(remote_add_thread): Set the new thread's vcont_resumed flag.
	(demand_private_info): Delete.
	(get_private_info_thread, get_private_info_ptid): New functions.
	(remote_update_thread_list): Adjust.
	(process_initial_stop_replies): Clear the thread's vcont_resumed
	flag.
	(remote_resume): If connected in non-stop mode, record the resume
	request and return early.
	(struct private_inferior): New.
	(struct vcont_builder): New.
	(vcont_builder_restart, vcont_builder_flush)
	(vcont_builder_push_action): New functions.
	(MAX_ACTION_SIZE): New macro.
	(remote_commit_resume): New function.
	(thread_pending_fork_status, is_pending_fork_parent_thread): New
	functions.
	(check_pending_event_prevents_wildcard_vcont_callback)
	(check_pending_events_prevent_wildcard_vcont): New functions.
	(process_stop_reply): Adjust.  Clear the thread's vcont_resumed
	flag.
	(init_remote_ops): Install remote_commit_resume.
	* target-delegates.c: Regenerate.
	* target.c (defer_target_commit_resume): New global.
	(target_commit_resume, make_cleanup_defer_target_commit_resume):
	New functions.
	* target.h (struct target_ops) <to_commit_resume>: New field.
	(target_resume): Update comments.
	(target_commit_resume): New declaration.
2016-10-26 16:22:50 +01:00
Antoine Tremblay 4034d0ff52 Emit inferior, thread and frame selection events to all UIs
With this patch, when an inferior, thread or frame is explicitly
selected by the user, notifications will appear on all CLI and MI UIs.
When a GDB console is integrated in a front-end, this allows the
front-end to follow a selection made by the user ont he CLI, and it
informs the user about selection changes made behind the scenes by the
front-end.

This patch addresses PR gdb/20487.

In order to communicate frame changes to the front-end, this patch adds
a new field to the =thread-selected event for the selected frame.  The
idea is that since inferior/thread/frame can be seen as a composition,
it makes sense to send them together in the same event.  The vision
would be to eventually send the inferior information as well, if we find
that it's needed, although the "=thread-selected" event would be
ill-named for that job.

Front-ends need to handle this new field if they want to follow the
frame selection changes that originate from the console.  The format of
the frame attribute is the same as what is found in the *stopped events.

Here's a detailed example for each command and the events they generate:

thread
------

1. CLI command:

     thread 1.3

   MI event:

     =thread-selected,id="3",frame={...}

2. MI command:

     -thread-select 3

   CLI event:

     [Switching to thread 1.3 ...]

3. MI command (CLI-in-MI):

     thread 1.3

   MI event/reply:

     &"thread 1.3\n"
     ~"#0  child_sub_function () ...
     =thread-selected,id="3",frame={level="0",...}
     ^done

frame
-----

1. CLI command:

     frame 1

   MI event:

     =thread-selected,id="3",frame={level="1",...}

2. MI command:

     -stack-select-frame 1

   CLI event:

     #1  0x00000000004007f0 in child_function...

3. MI command (CLI-in-MI):

     frame 1

   MI event/reply:

     &"frame 1\n"
     ~"#1  0x00000000004007f9 in ..."
     =thread-selected,id="3",frame={level="1"...}
     ^done

inferior
--------

Inferior selection events only go from the console to MI, since there's
no way to select the inferior in pure MI.

1. CLI command:

     inferior 2

   MI event:

     =thread-selected,id="3"

Note that if the user selects an inferior that is not started or exited,
the MI doesn't receive a notification.  Since there is no threads to
select, the =thread-selected event does not apply...

2. MI command (CLI-in-MI):

     inferior 2

   MI event/reply:

     &"inferior 2\n"
     ~"[Switching to inferior 2 ...]"
     =thread-selected,id="4",frame={level="0"...}
     ^done

Internal implementation detail: this patch makes it possible to suppress
notifications caused by a CLI command, like what is done in mi-interp.c.
This means that it's now possible to use the
add_com_suppress_notification function to register a command with some
event suppressed.  It is used to implement the select-frame command in
this patch.

The function command_notifies_uscc_observer was added to extract
the rather complicated logical expression from the if statement.  It is
also now clearer what that logic does: if the command used by the user
already notifies the user_selected_context_changed observer, there is
not need to notify it again.  It therefore protects again emitting the
event twice.

No regressions, tested on ubuntu 14.04 x86 with target boards unix and
native-extended-gdbserver.

gdb/ChangeLog:

YYYY-MM-DD  Antoine Tremblay  <antoine.tremblay@ericsson.com>
YYYY-MM-DD  Simon Marchi  <simon.marchi@ericsson.com>

	PR gdb/20487
	* NEWS: Mention new frame field of =thread-selected event.
	* cli/cli-decode.c (add_cmd): Initialize c->suppress_notification.
	(add_com_suppress_notification): New function definition.
	(cmd_func): Set and restore the suppress_notification flag.
	* cli/cli-deicode.h (struct cmd_list_element)
	<suppress_notification>: New field.
	* cli/cli-interp.c (cli_suppress_notification): New global variable.
	(cli_on_user_selected_context_changed): New function.
	(_initialize_cli_interp): Attach to user_selected_context_changed
	observer.
	* command.h (struct cli_suppress_notification): New structure.
	(cli_suppress_notification): New global variable declaration.
	(add_com_suppress_notification): New function declaration.
	* defs.h (enum user_selected_what_flag): New enum.
	(user_selected_what): New enum flag type.
	* frame.h (print_stack_frame_to_uiout): New function declaration.
	* gdbthread.h (print_selected_thread_frame): New function declaration.
	* inferior.c (print_selected_inferior): New function definition.
	(inferior_command): Remove printing of inferior/thread/frame switch
	notifications, notify user_selected_context_changed observer.
	* inferior.h (print_selected_inferior): New function declaration.
	* mi/mi-cmds.c (struct mi_cmd): Add user_selected_context
	suppression to stack-select-frame and thread-select commands.
	* mi/mi-interp.c (struct mi_suppress_notification)
	<user_selected_context>: Initialize.
	(mi_user_selected_context_changed): New function definition.
	(_initialize_mi_interp): Attach to user_selected_context_changed.
	* mi/mi-main.c (mi_cmd_thread_select): Print thread selection reply.
	(mi_execute_command): Handle notification suppression.  Notify
	user_selected_context_changed observer on thread change instead of printing
	event directly.  Don't send it if command already sends the notification.
	(command_notifies_uscc_observer): New function.
	(mi_cmd_execute): Don't handle notification suppression.
	* mi/mi-main.h (struct mi_suppress_notification)
	<user_selected_context>: New field.
	* stack.c (print_stack_frame_to_uiout): New function definition.
	(select_frame_command): Notify user_selected_context_changed
	observer.
	(frame_command): Call print_selected_thread_frame if there's no frame
	change or notify user_selected_context_changed observer if there is.
	(up_command): Notify user_selected_context_changed observer.
	(down_command): Likewise.
	(_initialize_stack): Suppress user_selected_context notification for
	command select-frame.
	* thread.c (thread_command): Notify
	user_selected_context_changed if the thread has changed, print
	thread info directly if it hasn't.
	(do_captured_thread_select): Do not print thread switch event.
	(print_selected_thread_frame): New function definition.
	* tui/tui-interp.c (tui_on_user_selected_context_changed):
	New function definition.
	(_initialize_tui_interp): Attach to user_selected_context_changed
	observer.

gdb/doc/ChangeLog:

	PR gdb/20487
	* gdb.texinfo (Context management): Update mention of frame
	change notifications.
	(gdb/mi Async Records): Document frame field in
	=thread-select event.
	* observer.texi (GDB Observers): New user_selected_context_changed
	observer.

gdb/testsuite/ChangeLog:

	PR gdb/20487
	* gdb.mi/mi-pthreads.exp (check_mi_thread_command_set): Adapt
	=thread-select-event check.
2016-10-03 16:54:58 -04:00
Pedro Alves 5d5658a1d3 Per-inferior/Inferior-qualified thread IDs
This commit changes GDB to track thread numbers per-inferior.  Then,
if you're debugging multiple inferiors, GDB displays
"inferior-num.thread-num" instead of just "thread-num" whenever it
needs to display a thread:

 (gdb) info inferiors
   Num  Description       Executable
   1    process 6022     /home/pedro/gdb/tests/threads
 * 2    process 6037     /home/pedro/gdb/tests/threads
 (gdb) info threads
   Id   Target Id         Frame
   1.1  Thread 0x7ffff7fc2740 (LWP 6022) "threads" (running)
   1.2  Thread 0x7ffff77c0700 (LWP 6028) "threads" (running)
   1.3  Thread 0x7ffff7fc2740 (LWP 6032) "threads" (running)
   2.1  Thread 0x7ffff7fc1700 (LWP 6037) "threads" (running)
   2.2  Thread 0x7ffff77c0700 (LWP 6038) "threads" (running)
 * 2.3  Thread 0x7ffff7fc2740 (LWP 6039) "threads" (running)
 (gdb)
...
 (gdb) thread 1.1
 [Switching to thread 1.1 (Thread 0x7ffff7fc2740 (LWP 8155))]
 (gdb)
...

etc.

You can still use "thread NUM", in which case GDB infers you're
referring to thread NUM of the current inferior.

The $_thread convenience var and Python's InferiorThread.num attribute
are remapped to the new per-inferior thread number.  It's a backward
compatibility break, but since it only matters when debugging multiple
inferiors, I think it's worth doing.

Because MI thread IDs need to be a single integer, we keep giving
threads a global identifier, _in addition_ to the per-inferior number,
and make MI always refer to the global thread IDs.  IOW, nothing
changes from a MI frontend's perspective.

Similarly, since Python's Breakpoint.thread and Guile's
breakpoint-thread/set-breakpoint-thread breakpoint methods need to
work with integers, those are adjusted to work with global thread IDs
too.  Follow up patches will provide convenient means to access
threads' global IDs.

To avoid potencially confusing users (which also avoids updating much
of the testsuite), if there's only one inferior and its ID is "1",
IOW, the user hasn't done anything multi-process/inferior related,
then the "INF." part of thread IDs is not shown.  E.g,.:

 (gdb) info inferiors
   Num  Description       Executable
 * 1    process 15275     /home/pedro/gdb/tests/threads
 (gdb) info threads
   Id   Target Id         Frame
 * 1    Thread 0x7ffff7fc1740 (LWP 15275) "threads" main () at threads.c:40
 (gdb) add-inferior
 Added inferior 2
 (gdb) info threads
   Id   Target Id         Frame
 * 1.1  Thread 0x7ffff7fc1740 (LWP 15275) "threads" main () at threads.c:40
 (gdb)

No regressions on x86_64 Fedora 20.

gdb/ChangeLog:
2016-01-13  Pedro Alves  <palves@redhat.com>

	* NEWS: Mention that thread IDs are now per inferior and global
	thread IDs.
	* Makefile.in (SFILES): Add tid-parse.c.
	(COMMON_OBS): Add tid-parse.o.
	(HFILES_NO_SRCDIR): Add tid-parse.h.
	* ada-tasks.c: Adjust to use ptid_to_global_thread_id.
	* breakpoint.c (insert_breakpoint_locations)
	(remove_threaded_breakpoints, bpstat_check_breakpoint_conditions)
	(print_one_breakpoint_location, set_longjmp_breakpoint)
	(check_longjmp_breakpoint_for_call_dummy)
	(set_momentary_breakpoint): Adjust to use global IDs.
	(find_condition_and_thread, watch_command_1): Use parse_thread_id.
	(until_break_command, longjmp_bkpt_dtor)
	(breakpoint_re_set_thread, insert_single_step_breakpoint): Adjust
	to use global IDs.
	* dummy-frame.c (pop_dummy_frame_bpt): Adjust to use
	ptid_to_global_thread_id.
	* elfread.c (elf_gnu_ifunc_resolver_stop): Likewise.
	* gdbthread.h (struct thread_info): Rename field 'num' to
	'global_num.  Add new fields 'per_inf_num' and 'inf'.
	(thread_id_to_pid): Rename thread_id_to_pid to
	global_thread_id_to_ptid.
	(pid_to_thread_id): Rename to ...
	(ptid_to_global_thread_id): ... this.
	(valid_thread_id): Rename to ...
	(valid_global_thread_id): ... this.
	(find_thread_id): Rename to ...
	(find_thread_global_id): ... this.
	(ALL_THREADS, ALL_THREADS_BY_INFERIOR): Declare.
	(print_thread_info): Add comment.
	* tid-parse.h: New file.
	* tid-parse.c: New file.
	* infcmd.c (step_command_fsm_prepare)
	(step_command_fsm_should_stop): Adjust to use the global thread
	ID.
	(until_next_command, until_next_command)
	(finish_command_fsm_should_stop): Adjust to use the global thread
	ID.
	(attach_post_wait): Adjust to check the inferior number too.
	* inferior.h (struct inferior) <highest_thread_num>: New field.
	* infrun.c (handle_signal_stop)
	(insert_exception_resume_breakpoint)
	(insert_exception_resume_from_probe): Adjust to use the global
	thread ID.
	* record-btrace.c (record_btrace_open): Use global thread IDs.
	* remote.c (process_initial_stop_replies): Also consider the
	inferior number.
	* target.c (target_pre_inferior): Clear the inferior's highest
	thread num.
	* thread.c (clear_thread_inferior_resources): Adjust to use the
	global thread ID.
	(new_thread): New inferior parameter.  Adjust to use it.  Set both
	the thread's global ID and the thread's per-inferior ID.
	(add_thread_silent): Adjust.
	(find_thread_global_id): New.
	(find_thread_id): Make static.  Adjust to rename.
	(valid_thread_id): Rename to ...
	(valid_global_thread_id): ... this.
	(pid_to_thread_id): Rename to ...
	(ptid_to_global_thread_id): ... this.
	(thread_id_to_pid): Rename to ...
	(global_thread_id_to_ptid): ... this.  Adjust.
	(first_thread_of_process): Adjust.
	(do_captured_list_thread_ids): Adjust to use global thread IDs.
	(should_print_thread): New function.
	(print_thread_info): Rename to ...
	(print_thread_info_1): ... this, and add new show_global_ids
	parameter.  Handle it.  Iterate over inferiors.
	(print_thread_info): Reimplement as wrapper around
	print_thread_info_1.
	(show_inferior_qualified_tids): New function.
	(print_thread_id): Use it.
	(tp_array_compar): Compare inferior numbers too.
	(thread_apply_command): Use tid_range_parser.
	(do_captured_thread_select): Use parse_thread_id.
	(thread_id_make_value): Adjust.
	(_initialize_thread): Adjust "info threads" help string.
	* varobj.c (struct varobj_root): Update comment.
	(varobj_create): Adjust to use global thread IDs.
	(value_of_root_1): Adjust to use global_thread_id_to_ptid.
	* windows-tdep.c (display_tib): No longer accept an argument.
	* cli/cli-utils.c (get_number_trailer): Make extern.
	* cli/cli-utils.h (get_number_trailer): Declare.
	(get_number_const): Adjust documentation.
	* mi/mi-cmd-var.c (mi_cmd_var_update_iter): Adjust to use global
	thread IDs.
	* mi/mi-interp.c (mi_new_thread, mi_thread_exit)
	(mi_on_normal_stop, mi_output_running_pid, mi_on_resume):
	* mi/mi-main.c (mi_execute_command, mi_cmd_execute): Likewise.
	* guile/scm-breakpoint.c (gdbscm_set_breakpoint_thread_x):
	Likewise.
	* python/py-breakpoint.c (bppy_set_thread): Likewise.
	* python/py-finishbreakpoint.c (bpfinishpy_init): Likewise.
	* python/py-infthread.c (thpy_get_num): Add comment and return the
	per-inferior thread ID.
	(thread_object_getset): Update comment of "num".

gdb/testsuite/ChangeLog:
2016-01-07  Pedro Alves  <palves@redhat.com>

	* gdb.base/break.exp: Adjust to output changes.
	* gdb.base/hbreak2.exp: Likewise.
	* gdb.base/sepdebug.exp: Likewise.
	* gdb.base/watch_thread_num.exp: Likewise.
	* gdb.linespec/keywords.exp: Likewise.
	* gdb.multi/info-threads.exp: Likewise.
	* gdb.threads/thread-find.exp: Likewise.
	* gdb.multi/tids.c: New file.
	* gdb.multi/tids.exp: New file.

gdb/doc/ChangeLog:
2016-01-07  Pedro Alves  <palves@redhat.com>

	* gdb.texinfo (Threads): Document per-inferior thread IDs,
	qualified thread IDs, global thread IDs and thread ID lists.
	(Set Watchpoints, Thread-Specific Breakpoints): Adjust to refer to
	thread IDs.
	(Convenience Vars): Document the $_thread convenience variable.
	(Ada Tasks): Adjust to refer to thread IDs.
	(GDB/MI Async Records, GDB/MI Thread Commands, GDB/MI Ada Tasking
	Commands, GDB/MI Variable Objects): Update to mention global
	thread IDs.
	* guile.texi (Breakpoints In Guile)
	<breakpoint-thread/set-breakpoint-thread breakpoint>: Mention
	global thread IDs instead of thread IDs.
	* python.texi (Threads In Python): Adjust documentation of
	InferiorThread.num.
	(Breakpoint.thread): Mention global thread IDs instead of thread
	IDs.
2016-01-13 10:59:43 +00:00
Joel Brobecker 618f726fcb GDB copyright headers update after running GDB's copyright.py script.
gdb/ChangeLog:

        Update year range in copyright notice of all files.
2016-01-01 08:43:22 +04:00
Don Breazeal 8020350c52 Target remote mode fork and exec event support
This patch implements support for fork and exec events with target remote
mode Linux targets.  For such targets with Linux kernels 2.5.46 and later,
this enables follow-fork-mode, detach-on-fork and fork and exec
catchpoints.

The changes required to implement this included:

 * Don't exit from gdbserver if there are still active inferiors.

 * Allow changing the active process in remote mode.

 * Enable fork and exec events in remote mode.

 * Print "Ending remote debugging" only when disconnecting.

 * Combine remote_kill and extended_remote_kill into a single function
   that can handle the multiple inferior case for target remote.  Also,
   the same thing for remote_mourn and extended_remote_mourn.

 * Enable process-style ptids in target remote.

 * Remove restriction on multiprocess mode in target remote.

gdb/gdbserver/ChangeLog:

	* server.c (process_serial_event): Don't exit from gdbserver
	in remote mode if there are still active inferiors.

gdb/ChangeLog:

	* inferior.c (number_of_live_inferiors): New function.
	(have_live_inferiors): Use number_of_live_inferiors in place
	of duplicate code.
	* inferior.h (number_of_live_inferiors): Declare new function.
	* remote.c (set_general_process): Remove restriction on target
	remote mode.
	(remote_query_supported): Likewise.
	(remote_detach_1): Exit in target remote mode only when there
	is just one live inferior left.
	(remote_disconnect): Unpush the target directly instead of
	calling remote_mourn.
	(remote_kill): Rewrite function to handle both target remote
	and extended-remote.  Call remote_kill_k.
	(remote_kill_k): New function.
	(extended_remote_kill): Delete function.
	(remote_mourn, extended_remote_mourn): Combine functions into
	one, remote_mourn, and enable extended functionality for target
	remote.
	(remote_pid_to_str): Enable "process" style ptid string for
	target remote.
	(remote_supports_multi_process): Remove restriction on target
	remote mode.
2015-12-14 11:18:05 -08:00