Commit Graph

1803 Commits

Author SHA1 Message Date
Tom Tromey 1a34f210bb Fix the clang build
Simon pointed out that gdb would not build with clang, due to the
addition of -Wimplicit-fallthrough.  This patch fixes the problem by
using -Wimplicit-fallthrough=3 -- this does not work with clang,
bypassing the issue.

Tested by rebuilding with both gcc and clang; and also by verifying
that -Wimplicit-fallthrough=3 is used in the gcc build.

I will file a follow-up bug to convert the fall-through comments to a
form that can be used by both clang and gcc.

gdb/ChangeLog
2018-05-10  Tom Tromey  <tom@tromey.com>

	* configure: Rebuild.
	* warning.m4 (AM_GDB_WARNINGS): Use -Wimplicit-fallthrough=3.

gdb/gdbserver/ChangeLog
2018-05-10  Tom Tromey  <tom@tromey.com>

	* configure: Rebuild.
2018-05-10 10:05:35 -06:00
Joel Brobecker 190852c8ac gdbserver/Windows: crash during connection establishment phase
On Windows, starting a new process with GDBserver seems to work,
in the sense that the program does get started, and GDBserver
confirms that it is listening for GDB to connect. However, as soon as
GDB establishes the connection with GDBserver, and starts discussing
with it, GDBserver crashes, with a SEGV.

This SEGV occurs in remote-utils.c::prepare_resume_reply...

  | regp = current_target_desc ()->expedite_regs;
  | [...]
  | while (*regp)

... because, in our case, REGP is NULL.

This patches fixes the issues by adding a parameter to init_target_desc,
in order to make sure that we always provide the list of registers when
we initialize a target description.

gdb/ChangeLog:

        PR server/23158:
        * regformats/regdat.sh: Adjust script, following the addition
        of the new expedite_regs parameter to init_target_desc.

gdb/gdbserver/ChangeLog:

        PR server/23158:
        * tdesc.h (init_target_desc) <expedite_regs>: New parameter.
        * tdesc.c (init_target_desc) <expedite_regs>: New parameter.
        Use it to set the expedite_regs field in the given tdesc.
        * x86-tdesc.h: New file.
        * linux-aarch64-tdesc.c (aarch64_linux_read_description):
        Adjust following the addition of the new expedite_regs parameter
        to init_target_desc.
        * linux-tic6x-low.c (tic6x_read_description): Likewise.
        * linux-x86-tdesc.c: #include "x86-tdesc.h".
        (i386_linux_read_description, amd64_linux_read_description):
        Adjust following the addition of the new expedite_regs parameter
        to init_target_desc.
        * lynx-i386-low.c: #include "x86-tdesc.h".
        (lynx_i386_arch_setup): Adjust following the addition of the new
        expedite_regs parameter to init_target_desc.
        * nto-x86-low.c: #include "x86-tdesc.h".
        (nto_x86_arch_setup): Adjust following the addition of the new
        expedite_regs parameter to init_target_desc.
        * win32-i386-low.c: #include "x86-tdesc.h".
        (i386_arch_setup): Adjust following the addition of the new
        expedite_regs parameter to init_target_desc.
2018-05-10 11:27:13 -04:00
Joel Brobecker 7dbac825b0 gdbserver/Windows: Fix "no program to debug" error
Trying to start a program with GDBserver on Windows yields
the following error:

    $ gdbserver.exe --once :4444 simple_main.exe
    Killing process(es): 5008
    No program to debug
    Exiting

The error itself comes from the following code shortly after
create_inferior gets called (in server.c::main):

    /* Wait till we are at first instruction in program.  */
    create_inferior (program_path.get (), program_args);
    [...]

    if (last_status.kind == TARGET_WAITKIND_EXITED
        || last_status.kind == TARGET_WAITKIND_SIGNALLED)
      was_running = 0;
    else
      was_running = 1;

    if (!was_running && !multi_mode)
      error ("No program to debug");

What happens is that the "last_status" global starts initialized
as zeroes, which means last_status.kind == TARGET_WAITKIND_EXITED,
and we expect create_inferior to be waiting for the inferior to
start until reaching the SIGTRAP, and to set the "last_status"
global to match that last event we received.

I suspect this is an unintended side-effect of the following change...

    commit 2090129c36
    Date:   Thu Dec 22 21:11:11 2016 -0500
    Subject: Share fork_inferior et al with gdbserver

... which removes some code in server.c that was responsible for
starting the inferior in a functin that was named start_inferior,
and looked like this:

   signal_pid = create_inferior (new_argv[0], &new_argv[0]);
   [...]
   /* Wait till we are at 1st instruction in program, return new pid
      (assuming success).  */
   last_ptid = mywait (pid_to_ptid (signal_pid), &last_status, 0, 0);

The code has been transitioned to using fork_inferior, but sadly,
only for the targets that support it. On Windows, the calls to wait
setting "last_status" simply disappeared.

This patch adds it back in the Windows-specific implementation of
create_inferior.

gdb/gdbserver/ChangeLog:

        PR server/23158:
        * win32-low.c (win32_create_inferior): Add call to my_wait
        setting last_status global.
2018-05-10 11:24:33 -04:00
Joel Brobecker 906994d9d5 [gdbserver/win32] fatal "glob could not process pattern '(null)'" error
Trying to start GDBserver on Windows currently yields the following
error...

    $ gdbserver.exe --once :4444 simple_main.exe
    glob could not process pattern '(null)'.
    Exiting

... after which GDB terminates with a nonzero status.

This is because create_process in win32-low.c calls gdb_tilde_expand
with the result of a call to get_inferior_cwd without verifying that
the returned directory is not NULL:

    | static BOOL
    | create_process (const char *program, char *args,
    |                 DWORD flags, PROCESS_INFORMATION *pi)
    | {
    |   const char *inferior_cwd = get_inferior_cwd ();
    |   std::string expanded_infcwd = gdb_tilde_expand (inferior_cwd);

This patch avoids this by only calling gdb_tilde_expand when
INFERIOR_CWD is not NULL, which is similar to what is done on
GNU/Linux for instance.

gdb/gdbserver/ChangeLog:

        PR server/23158:
        * win32-low.c (create_process): Only call gdb_tilde_expand if
        inferior_cwd is not NULL.
2018-05-10 11:23:10 -04:00
Andrew Burgess 8ee22052f6 gdb/x86: Handle kernels using compact xsave format
For GNU/Linux on x86-64, if the target is using the xsave format for
passing the floating-point information from the inferior then there
currently exists a bug relating to the x87 control registers, and the
mxcsr register.

The xsave format allows different floating-point features to be lazily
enabled, a bit in the xsave format tells GDB which floating-point
features have been enabled, and which have not.

Currently in GDB, when reading the floating point state, we check the
xsave bit flags, if the feature is enabled then we read the feature
from the xsave buffer, and if the feature is not enabled, then we
supply the default value from within GDB.

Within GDB, when writing the floating point state, we first fetch the
xsave state from the target and then, for any feature that is not yet
enabled, we write the default values into the xsave buffer.  Next we
compare the regcache value with the value in the xsave buffer, and, if
the value has changed we update the value in the xsave buffer, and
mark the feature enabled in the xsave bit flags.

The problem then, is that the x87 control registers were not following
this pattern.  We assumed that these registers were always written out
by the kernel, and we always wrote them out to the xsave buffer (but
didn't enabled the feature).  The result of this is that if the kernel
had not yet enabled the x87 feature then within GDB we would see
random values for the x87 floating point control registers, and if the
user tried to modify one of these register, that modification would be
lost.

Finally, the mxcsr register was also broken in the same way as the x87
control registers.  The added complexity with this case is that the
mxcsr register is part of both the avx and sse floating point feature
set.  When reading or writing this register we need to check that at
least one of these features is enabled.

This bug was present in native GDB, and within gdbserver.  Both are
fixed with this commit.

gdb/ChangeLog:

	* common/x86-xstate.h (I387_FCTRL_INIT_VAL): New constant.
	(I387_MXCSR_INIT_VAL): New constant.
	* amd64-tdep.c (amd64_supply_xsave): Only read state from xsave
	buffer if it was supplied by the inferior.
	* i387-tdep.c (i387_supply_fsave): Use I387_MXCSR_INIT_VAL.
	(i387_xsave_get_clear_bv): New function.
	(i387_supply_xsave): Only read x87 control registers from the
	xsave buffer if the feature is enabled, and the state will have
	been written, otherwise, provide a suitable default.
	(i387_collect_xsave): Pre-clear all registers in xsave buffer,
	including x87 control registers.  Update control registers if they
	have changed from the default value, and mark features as enabled
	as required.
	* i387-tdep.h (i387_xsave_get_clear_bv): Declare.

gdb/gdbserver/ChangeLog:

	* i387-fp.c (i387_cache_to_xsave): Only write x87 control
	registers to the cache if their values have changed.
	(i387_xsave_to_cache): Provide default values for x87 control
	registers when these features are available, but disabled.
	* regcache.c (supply_register_by_name_zeroed): New function.
	* regcache.h (supply_register_by_name_zeroed): Declare new
	function.

gdb/testsuite/ChangeLog:

	* gdb.arch/amd64-init-x87-values.S: New file.
	* gdb.arch/amd64-init-x87-values.exp: New file.
2018-05-08 18:03:46 +01:00
Tom Tromey aff689d36d Add -Wduplicated-cond
This adds -Wduplicated-cond to warnings.m4.  This caught one bug.

I tried adding -Wduplicated-branches as well, but it results in some
spurious failures from code like this in cgen.h:

    #define CGEN_ATTR_TYPE(n) \
    struct { unsigned int bool_; \
	     CGEN_ATTR_VALUE_TYPE nonbool[(n) ? (n) : 1]; }

This will trigger a warning if passed n==1, which seems like a
perfectly valid thing to do; and there were other issues like this as
well.

ChangeLog
2018-05-07  Tom Tromey  <tom@tromey.com>

	* configure: Rebuild.
	* warning.m4 (AM_GDB_WARNINGS): Add -Wduplicated-cond.

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

	* configure: Rebuild.
2018-05-07 08:47:38 -06:00
Tom Tromey 85e26832a0 Add -Wimplicit-fallthrough
This adds -Wimplicit-fallthrough to the set of default warnings.

2018-05-04  Tom Tromey  <tom@tromey.com>

	* configure: Rebuild.
	* warning.m4 (AM_GDB_WARNINGS): Add -Wimplicit-fallthrough.

gdbserver/ChangeLog
2018-05-04  Tom Tromey  <tom@tromey.com>

	* configure: Rebuild.
2018-05-04 22:04:46 -06:00
Jan Kratochvil a3b60e4588 aarch64: PR 19806: watchpoints: false negatives + PR 20207 contiguous ones
Some unaligned watchpoints were currently missed.

On old kernels as specified in
	kernel RFE: aarch64: ptrace: BAS: Support any contiguous range (edit)
	https://sourceware.org/bugzilla/show_bug.cgi?id=20207
after this patch some other unaligned watchpoints will get reported as false
positives.

With new kernels all the watchpoints should work exactly.

There may be a regresion that it now less merges watchpoints so that with
multiple overlapping watchpoints it may run out of the 4 hardware watchpoint
registers.  But as discussed in the original thread GDB needs some generic
watchpoints merging framework to be used by all the target specific code.
Even current FSF GDB code does not merge it perfectly.  Also with the more
precise watchpoints one can technically merge them less.  And I do not think
it matters too much to improve mergeability only for old kernels.
Still even on new kernels some better merging logic would make sense.

There remains one issue:
	kernel-4.15.14-300.fc27.armv7hl
	FAIL: gdb.base/watchpoint-unaligned.exp: continue
	FAIL: gdb.base/watchpoint-unaligned.exp: continue
	(gdb) continue
	Continuing.
	Unexpected error setting watchpoint: Invalid argument.
	(gdb) FAIL: gdb.base/watchpoint-unaligned.exp: continue
But that looks as a kernel bug to me.
(1) It is not a regression by this patch.
(2) It is unrelated to this patch.

gdb/ChangeLog
2018-05-04  Jan Kratochvil  <jan.kratochvil@redhat.com>
	    Pedro Alves <palves@redhat.com>

	PR breakpoints/19806 and support for PR external/20207.
	* NEWS: Mention Aarch64 watchpoint improvements.
	* aarch64-linux-nat.c (aarch64_linux_stopped_data_address): Fix missed
	watchpoints and PR external/20207 watchpoints.
	* nat/aarch64-linux-hw-point.c
	(kernel_supports_any_contiguous_range): New.
	(aarch64_watchpoint_offset): New.
	(aarch64_watchpoint_length): Support PR external/20207 watchpoints.
	(aarch64_point_encode_ctrl_reg): New parameter offset, new asserts.
	(aarch64_point_is_aligned): Support PR external/20207 watchpoints.
	(aarch64_align_watchpoint): New parameters aligned_offset_p and
	next_addr_orig_p.  Support PR external/20207 watchpoints.
	(aarch64_downgrade_regs): New.
	(aarch64_dr_state_insert_one_point): New parameters offset and
	addr_orig.
	(aarch64_dr_state_remove_one_point): Likewise.
	(aarch64_handle_breakpoint): Update caller.
	(aarch64_handle_aligned_watchpoint): Likewise.
	(aarch64_handle_unaligned_watchpoint): Support addr_orig and
	aligned_offset.
	(aarch64_linux_set_debug_regs): Remove const from state.  Call
	aarch64_downgrade_regs.
	(aarch64_show_debug_reg_state): Print also dr_addr_orig_wp.
	* nat/aarch64-linux-hw-point.h (DR_CONTROL_LENGTH): Rename to ...
	(DR_CONTROL_MASK): ... this.
	(struct aarch64_debug_reg_state): New field dr_addr_orig_wp.
	(unsigned int aarch64_watchpoint_offset): New prototype.
	(aarch64_linux_set_debug_regs): Remove const from state.
	* utils.c (align_up, align_down): Move to ...
	* common/common-utils.c (align_up, align_down): ... here.
	* utils.h (align_up, align_down): Move to ...
	* common/common-utils.h (align_up, align_down): ... here.

gdb/gdbserver/ChangeLog
2018-05-04  Jan Kratochvil  <jan.kratochvil@redhat.com>
	    Pedro Alves <palves@redhat.com>

	* linux-aarch64-low.c (aarch64_stopped_data_address):
	Likewise.

gdb/testsuite/ChangeLog
2018-05-04  Jan Kratochvil  <jan.kratochvil@redhat.com>
	    Pedro Alves <palves@redhat.com>

	PR breakpoints/19806 and support for PR external/20207.
	* gdb.base/watchpoint-unaligned.c: New file.
	* gdb.base/watchpoint-unaligned.exp: New file.
2018-05-04 22:26:46 +02:00
Tom Tromey 632e107b32 Enable -Wsuggest-override
I noticed the existence of -Wsuggest-override and so this patch
enables it for gdb.  It found a few spots that could use "override".
Also I went ahead and removed all uses of the "OVERRIDE" macro.

Using override is beneficial because it makes it harder to change a
base class and then forget to change a derived class.

Tested by the buildbot.

ChangeLog
2018-04-27  Tom Tromey  <tom@tromey.com>

	* configure: Rebuild.
	* warning.m4 (AM_GDB_WARNINGS): Add -Wsuggest-override.
	* dwarf2loc.c (class dwarf_evaluate_loc_desc): Use "override", not
	"OVERRIDE".
	(class symbol_needs_eval_context): Likewise.
	* dwarf2read.c (mock_mapped_index::symbol_name_count)
	(mock_mapped_index::symbol_name_at): Use "override".  Remove
	"virtual".
	* dwarf2-frame.c (dwarf_expr_executor::get_addr_index): Use
	"override".
	(class dwarf_expr_executor): Use "override", not "OVERRIDE".
	* aarch64-tdep.c (instruction_reader::read): Use "override".
	(instruction_reader_test::read): Likewise.
	* arm-tdep.c (instruction_reader::read): Use "override".
	(instruction_reader_thumb::read): Likewise.

gdbserver/ChangeLog
2018-04-27  Tom Tromey  <tom@tromey.com>

	* configure: Rebuild.
2018-04-27 12:53:14 -06:00
Tom Tromey 458412c368 Regenerate gdb/configure and gdbserver/configure
Pedro pointed out that gdb/configure and gdbserver/configure weren't
updated after some recent *.m4 changes.

This patch rebuilds those files.  Tested by rebuilding.  Pedro
approved this in the thread where he raised this issue, so I'm pushing
it in.

ChangeLog
2018-04-23  Tom Tromey  <tom@tromey.com>

	* configure: Rebuild.

gdbserver/ChangeLog
2018-04-23  Tom Tromey  <tom@tromey.com>

	* configure: Rebuild.
2018-04-23 09:29:41 -06:00
Simon Marchi f31c089e78 Fix dependency tracking in gdbserver subdirectories
The dependency tracking (the thing that knows which source file included
which other source file during last build to know what to rebuild when
an included file changes) is broken for gdbserver subdirectories (arch
and common).

The dependency tracking files are created in the form

  arch/.deps/i386.Po

but we try to include

  .deps/arch/i386.Po

An easy smoke test is too "touch" the gdb/features/i386/32bit-core.c
file in the source directory and try to rebuild gdbserver.  This file is
included by gdb/arch/i386.c, so it should cause
gdb/gdbserver/arch/i386.o in the build directory to be rebuilt.  It
currently isn't rebuilt, but is with this patch applied.

This patch copies the technique used in GDB to transform the dep file
paths to the proper form.

Also, while testing using the depcomp method of dependency tracking (by
just hacking the condition), I noticed that depcomp was not found.  The
path to depcomp seems to be missing a "..".

gdb/gdbserver/ChangeLog:

	* Makefile.in (depcomp): Add "..".
	(all_deps_files): New and use it.
2018-04-19 13:23:32 -04:00
Alan Hayward b319b0984b Remove xml files from gdbserver
For ports which use new target descriptions, remove
the xml files from being built into gdbserver.

gdbserver/
	* configure.srv (aarch64*-*-linux*): Don't include xml.
	(i[34567]86-*-cygwin*): Likewise.
	(i[34567]86-*-linux*): Likewise.
	(i[34567]86-*-lynxos*): Likewise.
	(i[34567]86-*-mingw32ce*): Likewise.
	(i[34567]86-*-mingw*): Likewise.
	(i[34567]86-*-nto*): Likewise.
	(tic6x-*-uclinux): Likewise.
	(x86_64-*-linux*): Likewise.
	(x86_64-*-mingw*): Likewise.
	(x86_64-*-cygwin*): Likewise.
2018-04-18 21:03:05 +01:00
Alan Hayward 3b74854b8d Remove xml file references from target descriptions
gdb/
	* common/tdesc.h (tdesc_create_feature): Remove xml filename
	parameter.
	* features/aarch64-core.c (create_feature_aarch64_core):
	Regenerate.
	* features/aarch64-fpu.c (create_feature_aarch64_fpu):
	Likewise.
	* features/i386/32bit-avx.c (create_feature_i386_32bit_avx):
	Likewise.
	* features/i386/32bit-avx512.c
	(create_feature_i386_32bit_avx512): Likewise.
	* features/i386/32bit-core.c (create_feature_i386_32bit_core):
	Likewise.
	* features/i386/32bit-linux.c (create_feature_i386_32bit_linux):
	Likewise.
	* features/i386/32bit-mpx.c (create_feature_i386_32bit_mpx):
	Likewise.
	* features/i386/32bit-pkeys.c (create_feature_i386_32bit_pkeys):
	Likewise.
	* features/i386/32bit-sse.c (create_feature_i386_32bit_sse):
	Likewise.
	* features/i386/64bit-avx.c (create_feature_i386_64bit_avx):
	Likewise.
	* features/i386/64bit-avx512.c
	(create_feature_i386_64bit_avx512): Likewise.
	* features/i386/64bit-core.c (create_feature_i386_64bit_core):
	Likewise.
	* features/i386/64bit-linux.c (create_feature_i386_64bit_linux):
	Likewise.
	* features/i386/64bit-mpx.c (create_feature_i386_64bit_mpx):
	Likewise.
	* features/i386/64bit-pkeys.c (create_feature_i386_64bit_pkeys):
	Likewise.
	* features/i386/64bit-segments.c
	(create_feature_i386_64bit_segments): Likewise.
	* features/i386/64bit-sse.c (create_feature_i386_64bit_sse):
	Likewise.
	* features/i386/x32-core.c
	(create_feature_i386_x32_core): Likewise.
	* features/tic6x-c6xp.c (create_feature_tic6x_c6xp): Likewise.
	* features/tic6x-core.c (create_feature_tic6x_core): Likewise.
	* features/tic6x-gp.c (create_feature_tic6x_gp): Likewise.
	* target-descriptions.c: In generated code, don't pass xml
	filename.

gdbserver/
	* tdesc.c: Remove xml parameter.
2018-04-18 20:49:37 +01:00
Alan Hayward e98577a9dc Create xml from target descriptions
Add a print_xml_feature visitor class which turns a
target description into xml. Both gdb and gdbserver can do this.

gdb/
	* common/tdesc.c (print_xml_feature::visit_pre): Add xml parsing.
	(print_xml_feature::visit_post): Likewise.
	(print_xml_feature::visit): Likewise.
	* common/tdesc.h (tdesc_get_features_xml): Use const tdesc.
	(print_xml_feature): Add new class.
	* regformats/regdat.sh: Null xmltarget on feature targets.
	* target-descriptions.c (struct target_desc): Add xmltarget.
	(maintenance_check_tdesc_xml_convert): Add unittest function.
	(tdesc_get_features_xml): Add function to get xml.
	(maintenance_check_xml_descriptions): Test xml generation.
	* xml-tdesc.c (string_read_description_xml): Add function.
	* xml-tdesc.h (string_read_description_xml): Add declaration.

gdbserver/
	* gdb/gdbserver/server.c (get_features_xml): Remove cast.
	* tdesc.c (void target_desc::accept): Fill in function.
	(tdesc_get_features_xml): Remove old xml creation.
	(print_xml_feature::visit_pre): Add xml vistor.
	* tdesc.h (struct target_desc): Make xmltarget mutable.
	(tdesc_get_features_xml): Remove declaration.
2018-04-18 20:44:39 +01:00
Alan Hayward d278f585af Add tdesc osabi and architecture functions
gdb/
	* common/tdesc.h (tdesc_architecture_name): Add new declaration.
	(tdesc_osabi_name): Likewise.
	* target-descriptions.c (tdesc_architecture_name): Add new function.
	(tdesc_osabi_name): Likewise.

gdbserver/
	* tdesc.c (tdesc_architecture_name): Add new function.
	(tdesc_osabi_name): Likewise.
	(tdesc_get_features_xml): Use new functions.
2018-04-18 14:00:43 +01:00
Alan Hayward eee8a18dd2 Commonise tdesc types and makes use of them in gdbserver tdesc
gdb/
	* common/tdesc.c (tdesc_predefined_type): Move to here.
	(tdesc_named_type): Likewise.
	(tdesc_create_vector): Likewise.
	(tdesc_create_struct): Likewise.
	(tdesc_set_struct_size): Likewise.
	(tdesc_create_union): Likewise.
	(tdesc_create_flags): Likewise.
	(tdesc_create_enum): Likewise.
	(tdesc_add_field): Likewise.
	(tdesc_add_typed_bitfield): Likewise.
	(tdesc_add_bitfield): Likewise.
	(tdesc_add_flag): Likewise.
	(tdesc_add_enum_value): Likewise.
	* common/tdesc.h (struct tdesc_type_builtin): Likewise.
	(struct tdesc_type_vector): Likewise.
	(struct tdesc_type_field): Likewise.
	(struct tdesc_type_with_fields): Likewise.
	(tdesc_create_enum): Add declaration.
	(tdesc_add_typed_bitfield): Likewise.
	(tdesc_add_enum_value): Likewise.
	* target-descriptions.c (tdesc_type_field): Move from here.
	(tdesc_type_builtin): Likewise.
	(tdesc_type_vector): Likewise.
	(tdesc_type_with_fields): Likewise.
	(tdesc_predefined_types): Likewise.
	(tdesc_named_type): Likewise.
	(tdesc_create_vector): Likewise.
	(tdesc_create_struct): Likewise.
	(tdesc_set_struct_size): Likewise.
	(tdesc_create_union): Likewise.
	(tdesc_create_flags): Likewise.
	(tdesc_create_enum): Likewise.
	(tdesc_add_field): Likewise.
	(tdesc_add_typed_bitfield): Likewise.
	(tdesc_add_bitfield): Likewise.
	(tdesc_add_flag): Likewise.
	(tdesc_add_enum_value): Likewise.
	* gdb/target-descriptions.h (tdesc_create_enum): Likewise.
	(tdesc_add_typed_bitfield): Likewise.
	(tdesc_add_enum_value): Likewise.

gdbserver/
	* tdesc.c (tdesc_create_flags): Remove.
	(tdesc_add_flag): Likewise.
	(tdesc_named_type): Likewise.
	(tdesc_create_union): Likewise.
	(tdesc_create_struct): Likewise.
	(tdesc_create_vector): Likewise.
	(tdesc_add_bitfield): Likewise.
	(tdesc_add_field): Likewise.
	(tdesc_set_struct_size): Likewise.
2018-04-18 14:00:39 +01:00
Alan Hayward 82ec9bc705 Commonise tdesc_feature and makes use of it in gdbserver tdesc
gdb/
	* common/tdesc.c (tdesc_feature::accept): Move to here.
	(tdesc_feature::operator==): Likewise.
	(tdesc_create_reg): Likewise.
	* common/tdesc.h (tdesc_type_kind): Likewise.
	(struct tdesc_type): Likewise.
	(struct tdesc_feature): Likewise.
	* regformats/regdat.sh: Create a feature.
	* target-descriptions.c (tdesc_type_kind): Move from here.
	(tdesc_type): Likewise.
	(tdesc_type_up): Likewise.
	(tdesc_feature): Likewise.
	(tdesc_create_reg): Likewise.

gdbserver/
	* tdesc.c (~target_desc): Remove implictly deleted items.
	(init_target_desc): Iterate all features.
	(tdesc_get_features_xml): Use vector.
	(tdesc_create_feature): Create feature.
	* tdesc.h (tdesc_feature) Remove
	(target_desc): Add features.
2018-04-18 14:00:34 +01:00
Alan Hayward ea3e7d7179 Commonise tdesc_reg and makes use of it in gdbserver tdesc
gdb/
	* Makefile.in: Add arch/tdesc.c
	* common/tdesc.c: New file.
	* common/tdesc.h (tdesc_element_visitor): Move to here.
	(tdesc_element): Likewise.
	(tdesc_reg): Likewise.
	(tdesc_reg_up): Likewise.
	* regformats/regdef.h (reg): Add offset to constructors.
	* target-descriptions.c (tdesc_element_visitor): Move from here.
	(tdesc_element): Likewise.
	(tdesc_reg): Likewise.
	(tdesc_reg_up): Likewise.

gdbserver/
	* Makefile.in: Add common/tdesc.c
	* tdesc.c (init_target_desc): init all reg_defs from register vector.
	(tdesc_create_reg): Create tdesc_reg.
	* tdesc.h (tdesc_feature): Add register vector.
2018-04-18 14:00:30 +01:00
Simon Marchi 17d08cd413 Use std::vector and std::string instead of VEC(char_ptr) in gdbserver tdesc
This is a straightforward replacement, no change in behavior are
intended/expected.

gdb/gdbserver/ChangeLog:

	* tdesc.h (struct target_desc) <features>: Change type to
	std::vector<std::string>.
	* tdesc.c (target_desc::~target_desc): Adjust to std::vector
	changes.
	(tdesc_get_features_xml): Likewise.
	(tdesc_create_feature): Likewise.
2018-03-30 17:18:55 -04:00
Alan Hayward 5cd3e386e0 Make gdbserver reg_defs a vector of objects
gdb/
	* regformats/regdef.h (reg): Add constructors.

gdb/gdbserver/
	* regcache.c (find_register_by_number): Return a ref.
	(find_regno): Use references.
	(register_size): Likewise.
	(register_data): Likewise.
	* tdesc.c (target_desc::~target_desc): Remove free calls.
	(target_desc::operator==): Use std::vector compare.
	(init_target_desc): Use reference.
	(tdesc_create_reg): Use reg constructors.
	* tdesc.h (struct target_desc): Replace pointer with object.
2018-03-26 10:54:55 +01:00
Alan Hayward dff7492c9b Make gdbserver find_register_by_number static
gdbserver/
	* regcache.c (find_register_by_number): Make static.
	(find_regno): Use find_register_by_number
	* regcache.h (struct reg): Remove declaration.
2018-03-23 14:49:09 +00:00
Alan Hayward d80e524238 Move gdbserver tdesc header funcs to c file
gdbserver/
	* tdesc.c (target_desc::~target_desc): Move to here.
	(target_desc::operator==): Likewise.
	* tdesc.h (target_desc::~target_desc): Move from here.
	(target_desc::operator==): Likewise.
2018-03-23 11:08:27 +00:00
Andreas Arnez f69c5afb18 S390: Correct brace style in s390_get_wordsize
This corrects bad formatting in the newly introduced function
s390_get_wordsize.

gdb/gdbserver/ChangeLog:

	* linux-s390-low.c (s390_get_wordsize): Correct brace style.
2018-03-22 10:02:18 +01:00
Andreas Arnez ce29f8439f S390: Make IPA recognize tdescs with guarded storage
The in-process agent does not handle tdescs with guarded storage yet.
This is fixed.

gdb/gdbserver/ChangeLog:

	* linux-s390-ipa.c (get_ipa_tdesc): Add handling for
	S390_TDESC_GS.
	* linux-s390-low.c (s390_get_ipa_tdesc_idx): Likewise.
	(initialize_low_tracepoint): Call init_registers_s390x_gs_linux64
	and init_registers_s390_gs_linux64.
2018-03-21 17:41:45 +01:00
Andreas Arnez c49bd90bdb S390: gdbserver: Don't write guarded storage registers
On S390, the guarded storage register set is only valid if guarded storage
is active.  Reading/writing the register set yields errors if this is not
the case.  Then gdbserver emits warnings like these:

  Warning: ptrace(regsets_store_inferior_registers): No data available

Apart from confusing the user, this can also lead to test case failures
due to unexpected output.  To suppress this, make the guarded storage
regsets read-only for now.

gdb/gdbserver/ChangeLog:

	* linux-s390-low.c (s390_fill_gs): Remove function.
	(s390_fill_gsbc): Remove function.
	(s390_regsets): Set fill functions for the guarded storage regsets
	to NULL.
2018-03-21 17:41:45 +01:00
Andreas Arnez 7edb9bd32b S390: Enable re-attaching with native-extended-gdbserver
On s390x, when running attach.exp with native-extended-gdbserver,
gdbserver crashes in find_regno like this:

  .../regcache.c:252: A problem internal to GDBserver has been detected.
  Unknown register tdb0 requested

On the GDB side it looks like this:

  (gdb) attach 31568
  Attaching to process 31568
  Remote connection closed

The test case attempts to attach to a new process via the already running
gdbserver.  Thus s390_arch_setup is called a second time, and that's where
the problem occurs.  In order to determine the word width (32 or 64 bits),
s390_arch_setup reads the pswm register through the regcache.  For that it
uses a temporary tdesc which is supposed to work for all s390 targets,
since the actual tdesc has not been determined yet.  But in this second
round this doesn't work, because s390_regsets has been updated already and
now contains regsets not described by the temporary tdesc, such as the one
containing tdb0.

This is fixed by rearranging the logic in s390_arch_setup.

gdb/gdbserver/ChangeLog:

	* linux-s390-low.c (s390_get_hwcap): Replace tdesc parameter by
	the word size.  Add comment.
	(s390_get_wordsize): New function.
	(s390_arch_setup): No longer select a temporary tdesc to fetch the
	pswm with it.  Instead, use s390_get_wordsize to determine the
	word size first and derive the correct tdesc from that directly.
2018-03-21 17:41:44 +01:00
Simon Marchi 39be3c7e98 Add silent Makefile rules
Many projects (e.g. the Linux kernel) and build systems use "silent"
rules, which means that they'll only print a summary of what's being
done instead of printing all the detailed command lines.  While chatting
on the #gdb IRC channel, I realized a few people (including me) thought
it would be nice to have it in GDB too.

The idea is that too much text is not useful, the important information
gets lost.  If there's only the essential information, it's more likely
to be useful.  Most of the time, when I look at the build output, it's
to see how it's progressing.  By just printing a brief summary of each
operation, I can easily spot what's currently being compiled and
therefore how the build progresses (with time you know the order in
which files are compiled almost by heart).

As with other projects (Linux, automake-based things, probably others),
it's possible to print the complete command lines by passing V=1 to make
(or any other non-zero value).

I had one hesitation about this: when people report build failures, we
are more likely to miss the full compile command line.  We'll probably
sometimes need to ask people to include the build log with "make V=1".
I don't think it's a big downside, if other projects the size of the
Linux kernel can live with it, I'm sure we can too.

gdb/ChangeLog:

	* silent-rules.mk: New.
	* Makefile.in: Include silent-rules.mk
	(srcdir, VPATH, top_srcdir): Move up.
	(COMPILE): Add ECHO_CXX.
	(test-cp-name-parser$(EXEEXT)): Add ECHO_CXXLD.
	(init.c): Add ECHO_INIT_C.
	(gdb$(EXEEXT)): Add SILENCE and ECHO_CXXLD.
	(version.c): Add ECHO_GEN.
	(printcmd.o): Add ECHO_CXX.
	(target-float.o): Add ECHO_CXX.
	(ada-exp.o): Add ECHO_CXX.
	(stamp-xml): Add SILENCE and ECHO_GEN_XML_BUILTIN.
	(insight$(EXEEXT)): Add ECHO_CXXLD.
	* gnulib/configure.ac: Add AM_SILENT_RULES.
	* gnulib/aclocal.m4: Re-generate.
	* gnulib/configure: Re-generate.
	* gnulib/import/Makefile.in: Re-generate.

gdb/gdbserver/ChangeLog:

	* Makefile.in: Include silent-rules.mk.
	(srcdir, abs_top_srcdir, abs_srcdir, VPATH): Move up.
	(COMPILE): Add ECHO_CXX.
	(gdbserver$(EXEEXT)): Add SILENCE and ECHO_CXXLD.
	(gdbreplay$(EXEEXT)): Add SILENCE and ECHO_CXXLD.
	($(IPA_LIB)): Add SILENCE and ECHO_CXXLD.
	(version-generated.c): Add ECHO_GEN.
	(stamp-xml): Add SILENCE and ECHO_GEN_XML_BUILTIN_GENERATED.
	(IPAGENT_COMPILE): Add ECHO_CXX.
	(%-generated.c): Add ECHO_REGDAT.
2018-03-16 16:30:25 -04:00
Tom Tromey 3ae9ce5dd7 Special case NULL when using printf's %s format
This changes the printf command's %s and %ls formats to special-case
NULL, and print "(null)" for these.  This is PR cli/14977.  This
behavior seems a bit friendlier; I was undecided on whether other
invalid pointers should be handled specially somehow, so for the time
being I've left those out.

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

	PR cli/14977:
	* printcmd.c (printf_c_string, printf_wide_c_string): Special case
	for NULL.

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

	PR cli/14977:
	* ax.c (ax_printf): Special case for NULL.

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

	PR cli/14977:
	* gdb.base/printcmds.exp (test_printf): Add printf test of %s with
	a null pointer.
	* gdb.base/wchar.exp: Likewise.
2018-03-14 09:44:34 -06:00
Simon Marchi e6a58aa8a7 Add xml_escape_text_append and use it
[This patch should go on top of "linux_qxfer_libraries_svr4: Use
 std::string", I should have sent them together as a series.]

I noticed that linux_qxfer_libraries_svr4 used xml_escape_text, which
returns an std::string.  That string is then copied into a larger
buffer.  It would be more efficient if we had a version of
xml_escape_text which appended to an existing string instead of
returning a new one.  This is what this patch does.

I manually verified that the output of linux_qxfer_libraries_svr4 didn't
change before/after the patch.

gdb/ChangeLog:

	* common/xml-utils.c (xml_escape_text): Move code to...
	(xml_escape_text_append): ... this new function.
	* common/xml-utils.h (xml_escape_text_append): New declaration.
	* unittests/xml-utils-selftests.c (test_xml_escape_text_append):
	New function.
	(_initialize_xml_utils): register test_xml_escape_text_append as
	a selftest.

gdb/gdbserver/ChangeLog:

	* linux-low.c (linux_qxfer_libraries_svr4): Use
	xml_escape_text_append.
2018-03-08 18:04:46 -05:00
Simon Marchi f6e8a41e67 linux_qxfer_libraries_svr4: Use std::string
Use std::string, removing some manual memory management.

gdb/gdbserver/ChangeLog:

	* linux-low.c (linux_qxfer_libraries_svr4): Use std::string.
2018-03-08 18:04:07 -05:00
Simon Marchi b9671caf8f handle_general_set: Remove useless xstrdup
Unless I'm missing something very obvious, this xstrdup seems
unnecessary to me.  We can pass "mode" directly to sprintf.

gdb/gdbserver/ChangeLog:

	* server.c (handle_general_set): Remove unnecessary xstrdup.
2018-03-02 23:27:47 -05:00
Simon Marchi e80aaf6183 Make delim_string_to_char_ptr_vec return an std::vector
This patch makes delim_string_to_char_ptr_vec and all related functions
use std::vector of gdb::unique_xmalloc_ptr.  This allows getting rid of
make_cleanup_free_char_ptr_vec.  Returning a vector of
unique_xmalloc_ptr instead of std::string allows to minimize the impacts
on the calling code.  We can evaluate later whether we could/should
return a vector of std::strings instead.

gdb/ChangeLog:

	* common/gdb_vecs.h (make_cleanup_free_char_ptr_vec): Remove.
	(delim_string_to_char_ptr_vec): Return std::vector of
	gdb::unique_xmalloc_ptr.
	(dirnames_to_char_ptr_vec_append): Take std::vector of
	gdb::unique_xmalloc_ptr.
	(dirnames_to_char_ptr_vec): Return std::vector of
	gdb::unique_xmalloc_ptr.
	* common/gdb_vecs.c (delim_string_to_char_ptr_vec_append):
	Take std::vector of gdb::unique_xmalloc_ptr, adjust the code.
	(delim_string_to_char_ptr_vec): Return an std::vector of
	gdb::unique_xmalloc_ptr, adjust the code.
	(dirnames_to_char_ptr_vec_append): Take an std::vector of
	gdb::unique_xmalloc_ptr, adjust the code.
	(dirnames_to_char_ptr_vec): Return an std::vector of
	gdb::unique_xmalloc_ptr, adjust the code.
	* auto-load.c (auto_load_safe_path_vec): Change type to
	std::vector of gdb::unique_xmalloc_ptr.
	(auto_load_expand_dir_vars): Return an std::vector of
	gdb::unique_xmalloc_ptr, adjust the code.
	(auto_load_safe_path_vec_update): Adjust.
	(filename_is_in_auto_load_safe_path_vec): Adjust.
	(auto_load_objfile_script_1): Adjust.
	* build-id.c (build_id_to_debug_bfd): Adjust.
	* linux-thread-db.c (thread_db_load_search): Adjust.
	* source.c (add_path): Adjust.
	(openp): Adjust.
	* symfile.c (find_separate_debug_file): Adjust.
	* utils.c (do_free_char_ptr_vec): Remove.
	(make_cleanup_free_char_ptr_vec): Remove.

gdb/gdbserver/ChangeLog:

	* server.c (parse_debug_format_options): Adjust to
	delim_string_to_char_ptr_vec changes.
	* thread-db.c (thread_db_load_search): Adjust to
	dirnames_to_char_ptr_vec changes.
2018-03-02 23:22:07 -05:00
Markus Metzger b1223e7890 btrace, gdbserver: check btrace target pointers
By removing the supports_btrace gdbserver target method we relied on GDB
trying to enable branch tracing and failing on the attempt.

For targets that do not provide the btrace methods, however, an initial
request from GDB for the branch trace configuration to detect whether
gdbserver is already recording resulted in a protocol error.

Have the btrace target methods throw a "Target does not suppor branch
tracing" error and be prepared to handle exceptions in all functions that
call btrace target methods.  We therefore turn the target_* macros into
static inline functions.

Also remove the additional btrace target method checks that resulted in
the above protocol error.

Thanks to Maciej W. Rozycki <macro@mips.com> for reporting this.

gdbserver/
	* target.h (target_enable_btrace, target_disable_btrace)
	(target_read_btrace, target_read_btrace_conf): Turn macro into
	inline function.  Throw error if target method is not defined.
	* server.c (handle_qxfer_btrace, handle_qxfer_btrace_conf): Remove
	check for btrace target method.  Be prepared to handle exceptions
	from btrace target methods.
2018-03-01 12:25:24 +01:00
Sergio Durigan Junior 815615463b Change order of error message printed when gdbserver can't find CWD
I forgot to address Pedro's comment about my last patch and change the
order of the message printed when getcwd returns NULL on gdbserver.
This obvious commit does it.

gdb/gdbserver/ChangeLog:
2018-02-28  Sergio Durigan Junior  <sergiodj@redhat.com>

	* server.c (captured_main): Change order of error message printed
	when the current working directory cannot be found.
2018-02-28 11:43:48 -05:00
Sergio Durigan Junior 25e3c82c0e Make gdbserver work with filename-only binaries
Simon mentioned on IRC that, after the startup-with-shell feature has
been implemented on gdbserver, it is not possible to specify a
filename-only binary, like:

  $ gdbserver :1234 a.out
  /bin/bash: line 0: exec: a.out: not found
  During startup program exited with code 127.
  Exiting

This happens on systems where the current directory "." is not listed
in the PATH environment variable.  Although including "." in the PATH
variable is a possible workaround, this can be considered a regression
because before startup-with-shell it was possible to use only the
filename (due to reason that gdbserver used "exec*" directly).

The idea of the patch is to verify if the program path provided by the
user (or by the remote protocol) contains a directory separator
character.  If it doesn't, it means we're dealing with a filename-only
binary, so we call "gdb_abspath" to properly expand it and transform
it into a full path.  Otherwise, we leave the program path untouched.
This mimicks the behaviour seen on GDB (look at "openp" and
"attach_inferior", for example).

I am also submitting a testcase which exercises the scenario described
above.  This test requires gdbserver to be executed in a different CWD
than the original, so I also created a helper function, "with_cwd" (on
testsuite/lib/gdb.exp), which takes care of cd'ing into and out of the
specified dir.

Built and regtested on BuildBot, without regressions.

gdb/ChangeLog:
2018-02-28  Sergio Durigan Junior  <sergiodj@redhat.com>
	    Simon Marchi  <simon.marchi@polymtl.ca>

	* common/common-utils.c: Include "sys/stat.h".
	(is_regular_file): Move here from "source.c"; change return
	type to "bool".
	* common/common-utils.h (is_regular_file): New prototype.
	* common/pathstuff.c (contains_dir_separator): New function.
	* common/pathstuff.h (contains_dir_separator): New prototype.
	* source.c: Don't include "sys/stat.h".
	(is_regular_file): Move to "common/common-utils.c".

gdb/gdbserver/ChangeLog:
2018-02-28  Sergio Durigan Junior  <sergiodj@redhat.com>

	* server.c: Include "filenames.h" and "pathstuff.h".
	(program_name): Delete variable.
	(program_path): New anonymous class.
	(get_exec_wrapper): Use "program_path" instead of
	"program_name".
	(handle_v_run): Likewise.
	(captured_main): Likewise.
	(process_serial_event): Likewise.

gdb/testsuite/ChangeLog:
2018-02-28  Sergio Durigan Junior  <sergiodj@redhat.com>

	* gdb.server/abspath.exp: New file.
	* lib/gdb.exp (with_cwd): New procedure.
2018-02-28 11:37:10 -05:00
Sergio Durigan Junior b4987c956d Create new common/pathstuff.[ch]
This commit moves the path manipulation routines found on utils.c to a
new common/pathstuff.c, and updates the Makefile.in's accordingly.
The routines moved are "gdb_realpath", "gdb_realpath_keepfile" and
"gdb_abspath".

This will be needed because gdbserver will have to call "gdb_abspath"
on my next patch, which implements a way to expand the path of the
inferior provided by the user in order to allow specifying just the
binary name when starting gdbserver, like:

  $ gdbserver :1234 a.out

With the recent addition of the startup-with-shell feature on
gdbserver, this scenario doesn't work anymore if the user doesn't have
the current directory listed in the PATH variable.

I had to do a minor adjustment on "gdb_abspath" because we don't have
access to "tilde_expand" on gdbserver, so now the function is using
"gdb_tilde_expand" instead.  Otherwise, the code is the same.

Regression tested on the BuildBot, without regressions.

gdb/ChangeLog:
2018-02-28  Sergio Durigan Junior  <sergiodj@redhat.com>

	* Makefile.in (COMMON_SFILES): Add "common/pathstuff.c".
	(HFILES_NO_SRCDIR): Add "common/pathstuff.h".
	* auto-load.c: Include "common/pathstuff.h".
	* common/common-def.h (current_directory): Move here.
	* common/gdb_tilde_expand.c (gdb_tilde_expand_up): New
	function.
	* common/gdb_tilde_expand.h (gdb_tilde_expand_up): New
	prototype.
	* common/pathstuff.c: New file.
	* common/pathstuff.h: New file.
	* compile/compile.c: Include "common/pathstuff.h".
	* defs.h (current_directory): Move to "common/common-defs.h".
	* dwarf2read.c: Include "common/pathstuff.h".
	* exec.c: Likewise.
	* guile/scm-safe-call.c: Likewise.
	* linux-thread-db.c: Likewise.
	* main.c: Likewise.
	* nto-tdep.c: Likewise.
	* objfiles.c: Likewise.
	* source.c: Likewise.
	* symtab.c: Likewise.
	* utils.c: Include "common/pathstuff.h".
	(gdb_realpath): Move to "common/pathstuff.c".
	(gdb_realpath_keepfile): Likewise.
	(gdb_abspath): Likewise.
	* utils.h (gdb_realpath): Move to "common/pathstuff.h".
	(gdb_realpath_keepfile): Likewise.
	(gdb_abspath): Likewise.

gdb/gdbserver/ChangeLog:
2018-02-28  Sergio Durigan Junior  <sergiodj@redhat.com>

	* Makefile.in (SFILES): Add "$(srcdir)/common/pathstuff.c".
	(OBJS): Add "pathstuff.o".
	* server.c (current_directory): New global variable.
	(captured_main): Initialize "current_directory".
2018-02-28 11:34:39 -05:00
Alan Hayward f46cd62a69 Move arch/tdesc.h to common/tdesc.h
gdb/
	* arch/amd64.h: Use common/tdesc.h.
	* arch/i386.c: Likewise.
	* arch/i386.h: Likewise.
	* arch/tic6x.c: Likewise.
	* arch/tdesc.h: Move file from here...
	* common/tdesc.h: ...to here.
	* features/aarch64-core.c: Regenerate.
	* features/aarch64-fpu.c: Regenerate.
	* features/i386/32bit-avx.c: Regenerate.
	* features/i386/32bit-avx512.c: Regenerate.
	* features/i386/32bit-core.c: Regenerate.
	* features/i386/32bit-linux.c: Regenerate.
	* features/i386/32bit-mpx.c: Regenerate.
	* features/i386/32bit-pkeys.c: Regenerate.
	* features/i386/32bit-sse.c: Regenerate.
	* features/i386/64bit-avx.c: Regenerate.
	* features/i386/64bit-avx512.c: Regenerate.
	* features/i386/64bit-core.c: Regenerate.
	* features/i386/64bit-linux.c: Regenerate.
	* features/i386/64bit-mpx.c: Regenerate.
	* features/i386/64bit-pkeys.c: Regenerate.
	* features/i386/64bit-segments.c: Regenerate.
	* features/i386/64bit-sse.c: Regenerate.
	* features/i386/x32-core.c: Regenerate.
	* features/tic6x-c6xp.c: Regenerate.
	* features/tic6x-core.c: Regenerate.
	* features/tic6x-gp.c: Regenerate.
	* target-descriptions.c: Use common/tdesc.h.
	* target-descriptions.h: Likewise.

gdbserver/
	* tdesc.c: Use common/tdesc.h.
	* tdesc.h: Likewise.
2018-02-26 11:46:57 +00:00
Alan Hayward a543c5ca7c Fix make 3.81 build errors
gdbserver/
	* Makefile.in: Switch order of make rules.
2018-02-20 10:04:30 +00:00
Alan Hayward b5884fa710 Add common/ dir in build directories
gdb/
	* Makefile.in: (COMMON_SFILES): Add common/*.c files.
	(SFILES): Remove common/*.c files.
	(COMMON_OBS): Remove some *.o files built from common/*.c files.
	* common/common.host: Add common reference.
	* configure.ac: Likewise.
	* configure: Regenerate.

gdbserver/
	* Makefile.in: Add common directory in build.
	* configure.ac: Add common reference.
	* configure: Regenerate.
2018-02-19 09:37:24 +00:00
Markus Metzger de6242d307 btrace, gdbserver: remove the to_supports_btrace target method
Remove the to_supports_btrace target method and instead rely on detecting errors
when trying to enable recording.  This will also provide a suitable error
message explaining why recording is not possible.

For remote debugging, gdbserver will now always advertise branch tracing related
packets.  When talking to an older GDB, this will cause GDB to try to enable
branch tracing and gdbserver to report a suitable error message every time.

An older gdbserver will not advertise branch tracing related packets if the
one-time check failed, so a newer GDB with this patch will fail to enable branch
tracing at remote_enable_btrace() rather than at btrace_enable().  The error
message is the same in both cases so there should be no user-visible change.

gdb/
	* btrace.c (btrace_enable): Remove target_supports_btrace call.
	* nat/linux-btrace.c (perf_event_pt_event_type): Move.
	(kernel_supports_bts, kernel_supports_pt, linux_supports_bts)
	(linux_supports_pt, linux_supports_btrace): Remove.
	(linux_enable_bts): Call cpu_supports_bts.
	* nat/linux-btrace.h (linux_supports_btrace): Remove.
	* remote.c (remote_supports_btrace): Remove.
	(init_remote_ops): Remove remote_supports_btrace.
	* target-delegates.c: Regenerated.
	* target.c (target_supports_btrace): Remove.
	* target.h (target_ops) <to_supports_btrace>: Remove
	(target_supports_btrace): Remove.
	* x86-linux-nat.c (x86_linux_create_target): Remove
	linux_supports_btrace.

gdbserver/
	* linux-low.c (linux_target_ops): Remove linux_supports_btrace.
	* nto-low.c (nto_target_ops): Remove NULL for supports_btrace.
	* spu-low.c (spu_target_ops): Likewise.
	* win32-low.c (win32_target_ops): Likewise.
	* server.c (supported_btrace_packets): Report packets unconditionally.
	* target.h (target_ops) <supports_btrace>: Remove.
	(target_supports_btrace): Remove.
2018-02-09 14:03:20 +01:00
Markus Metzger 9ee23a853c btrace, gdbserver: use exceptions to convey btrace enable/disable errors
Change error reporting to use exceptions and be prepared to catch them in
gdbserver.  We use the exception message in our error reply to GDB.

This may remove some detail from the error message in the native case since
errno is no longer printed.  Later patches will improve that.

We're still using error strings on the RSP level.  This patch does not affect
the interoperability of older/newer GDB/gdbserver.

gdbserver/
	* server.c (handle_btrace_enable_bts, handle_btrace_enable_pt)
	(handle_btrace_disable): Change return type to void.  Use exceptions
	to report errors.
	(handle_btrace_general_set): Catch exception and copy message to
	return message.

gdb/
	* nat/linux-btrace.c (linux_enable_btrace): Throw exception if enabling
	btrace failed.
	* x86-linux-nat.c (x86_linux_enable_btrace): Catch btrace enabling
	exception and use message in own exception.
2018-02-09 14:03:19 +01:00
Tom Tromey 8ce47547b3 Remove make_cleanup_restore_current_thread from gdbserver
This removes make_cleanup_restore_current_thread from gdbserver,
replacing it with a use of scoped_restore.

2018-02-08  Tom Tromey  <tom@tromey.com>

	* linux-low.c (install_software_single_step_breakpoints): Use
	make_scoped_restore.
	* inferiors.c (make_cleanup_restore_current_thread): Remove.
	(do_restore_current_thread_cleanup): Remove.
	* gdbthread.h (make_cleanup_restore_current_thread): Don't
	declare.
2018-02-08 11:46:55 -07:00
Tom Tromey 45dd3607e2 Remove a cleanup from gdbserver
This removes a cleanup from gdbserver's set_raw_breakpoint_at,
replacing it with unique_xmalloc_ptr.

2018-02-08  Tom Tromey  <tom@tromey.com>

	* mem-break.c (set_raw_breakpoint_at): Use
	gdb::unique_xmalloc_ptr.
2018-02-08 11:46:55 -07: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
James Clarke a0aad53764 gdb: Add missing #ifdef USE_THREAD_DB to gdbserver
Otherwise, linking fails with:

  [...]/linux-low.c:664: undefined reference to `thread_db_notice_clone(thread_info*, ptid_t)'

gdb/gdbserver/ChangeLog:

	* linux-low.c (handle_extended_wait): Surround call to
	thread_db_notice_clone with #ifdef USE_THREAD_DB.
2018-01-19 18:18:58 +00:00
Simon Marchi 4d9b86e175 Make linux_ptrace_attach_fail_reason return an std::string
This patch makes linux_ptrace_attach_fail_reason and
linux_ptrace_attach_fail_reason_string return std::string.  It also
replaces usages of struct buffer with std::string.  This allows getting
rid of a cleanup in in linux_ptrace_attach_fail_reason_string and
simplifies the code in general.

Something that looks odd to me is that in
linux_ptrace_attach_fail_reason, if the two messages are appended, there
is no separating space or \n, so the result won't be very nice.  I left
it as-is for now though.

gdb/ChangeLog:

	* nat/linux-ptrace.h (linux_ptrace_attach_fail_reason): Return
	std::string.
	(linux_ptrace_attach_fail_reason_string): Likewise.
	* nat/linux-ptrace.c (linux_ptrace_attach_fail_reason):
	Likewise.
	(linux_ptrace_attach_fail_reason_string): Likewise.
	* linux-nat.c (attach_proc_task_lwp_callback): Adjust.

gdb/gdbserver/ChangeLog:

	* linux-low.c (attach_proc_task_lwp_callback): Adjust to
	linux_ptrace_attach_fail_reason_string now returning an
	std::string.
	(linux_attach): Likewise.
	* thread-db.c (attach_thread): Likewise.
2018-01-17 12:34:50 -05:00
Eldar Abusalimov f517c1805a configure: Fix test for fs_base/gs_base in <sys/user.h>
Make <sys/types.h> be included prior to including <sys/user.h>.

glibc versions older than 2.14 use __uintNN_t types within certain
structures defined in <sys/user.h> probably assuming these types are
defined prior to including the header. This results in the following
`configure` feature test compilation error that makes it think that
`struct user_regs_struct` doesn't have `fs_base`/`gs_base` fields,
althouh it does.

    configure:13617: checking for struct user_regs_struct.fs_base
    configure:13617: gcc -c -g -O2 -I/linux/include conftest.c >&5
    In file included from conftest.c:158:0:
    /usr/include/sys/user.h:32:3: error: unknown type name '__uint16_t'
       __uint16_t  cwd;
       ^
    /usr/include/sys/user.h:33:3: error: unknown type name '__uint16_t'
       __uint16_t  swd;
       ^
    /usr/include/sys/user.h:34:3: error: unknown type name '__uint16_t'
       __uint16_t  ftw;
       ^
    /usr/include/sys/user.h:35:3: error: unknown type name '__uint16_t'
       __uint16_t  fop;
       ^
    /usr/include/sys/user.h:36:3: error: unknown type name '__uint64_t'
       __uint64_t  rip;
       ^
    /usr/include/sys/user.h:37:3: error: unknown type name '__uint64_t'
       __uint64_t  rdp;
       ^
    /usr/include/sys/user.h:38:3: error: unknown type name '__uint32_t'
       __uint32_t  mxcsr;
       ^
    /usr/include/sys/user.h:39:3: error: unknown type name '__uint32_t'
       __uint32_t  mxcr_mask;
       ^
    /usr/include/sys/user.h:40:3: error: unknown type name '__uint32_t'
       __uint32_t  st_space[32];   /* 8*16 bytes for each FP-reg = 128 bytes */
       ^
    /usr/include/sys/user.h:41:3: error: unknown type name '__uint32_t'
       __uint32_t  xmm_space[64];  /* 16*16 bytes for each XMM-reg = 256 bytes */
       ^
    /usr/include/sys/user.h:42:3: error: unknown type name '__uint32_t'
       __uint32_t  padding[24];
       ^
    configure:13617: $? = 1
    configure: failed program was:
    | /* confdefs.h */
    ...
    | /* end confdefs.h.  */
    | #include <sys/user.h>
    |
    | int
    | main ()
    | {
    | static struct user_regs_struct ac_aggr;
    | if (ac_aggr.fs_base)
    | return 0;
    |   ;
    |   return 0;
    | }

Recent glibc versions don't use typedef'ed int types in <sys/user.h>,
thus allowing it to be included as is
(glibc commit d79a9c949c84e7f0ba33e87447c47af833e9f11a).
However there're still some distros alive that use older glibc,
for instance, RHEL/CentOS 6 package glibc 2.12.

Also affects PR gdb/21559:

    ../../gdb/regcache.c:1087: internal-error: void regcache_raw_supply(regcache, int, const void): Assertion `regnum >= 0 && regnum < regcache->descr->nr_raw_registers' failed.

As noted by Andrew Paprocki, who submitted the PR
(https://sourceware.org/bugzilla/show_bug.cgi?id=21559#c3):

    > It should be noted that modifying `configure` to force on
    > `HAVE_STRUCT_USER_REGS_STRUCT_FS_BASE` and
    > `HAVE_STRUCT_USER_REGS_STRUCT_GS_BASE` fixes this issue. For some
    > reason the `configure` tests for `fs_base` and `gs_base` fail
    > even though `sys/user.h` on RHEL5 has the fields defined in
    > `user_regs_struct`.

Note that this patch does NOT fix the root cause of PR gdb/21559,
although now that `configure` properly detects the presence of the
fields and sets HAVE_XXX accordingly, the execution takes another
path, which doesn't lead to the assertion failure in question.

gdb/ChangeLog:
2018-01-17  Eldar Abusalimov  <eldar.abusalimov@jetbrains.com>

	PR gdb/21559
	* configure.ac: Include <sys/types.h> prior to <sys/user.h> when
	checking for fs_base/gs_base fields in struct user_regs_struct.
	* configure: Regenerate.

gdb/gdbserver/ChangeLog:
2018-01-17  Eldar Abusalimov  <eldar.abusalimov@jetbrains.com>

	PR gdb/21559
	* configure.ac: Include <sys/types.h> prior to <sys/user.h> when
	checking for fs_base/gs_base fields in struct user_regs_struct.
	* configure: Regenerate.
2018-01-17 13:02:19 +00:00
Yao Qi 9a70f35c8d Mark register unavailable when PTRACE_PEEKUSER fails
As described in PR 18749, GDB/GDBserver may get an error on accessing
memory or register because the thread may disappear.  However, some
path doesn't expect the error.  This patch fixes this problem by
marking the register unavailable when PTRACE_PEEKUSER fails instead
of throwing error.

gdb/gdbserver:

2018-01-16  Yao Qi  <yao.qi@linaro.org>

	PR gdb/18749
	* linux-low.c (fetch_register): Call supply_register instead of
	error.
2018-01-16 09:05:39 +00:00
Yao Qi 605fd3c659 Fix GDBserver build failure when $development is false
When we set bfd/development.sh:$development to false, GDBserver failed to
build,

selftest.o: In function `selftests::run_tests(char const*)':
binutils-gdb/gdb/gdbserver/../common/selftest.c:97:undefined reference to `selftests::reset()'
collect2: error: ld returned 1 exit status

selftest.o shouldn't be compiled and linked when $development is false.
With this patch, in release mode, GDBserver doesn't nothing with option
--selftest,

$ ./gdbserver --selftest=foo
Selftests are not available in a non-development build.
$ ./gdbserver --selftest
Selftests are not available in a non-development build.

gdb/gdbserver:

2018-01-08  Yao Qi  <yao.qi@linaro.org>
	    Simon Marchi  <simon.marchi@ericsson.com>

	* Makefile.in (OBS): Remove selftest.o.
	* configure.ac: Set srv_selftest_objs if $development is true.
	(GDBSERVER_DEPFILES): Append $srv_selftest_objs.
	* configure: Re-generated.
	* server.c (captured_main): Wrap variable selftest_filter with
	GDB_SELF_TEST.

gdb/testsuite:

2018-01-08  Simon Marchi  <simon.marchi@ericsson.com>

	* gdb.server/unittest.exp: Match the output in non-development
	mode.
2018-01-08 10:09:33 +00:00
Simon Marchi 2cc050302c Make parse_debug_format_options return an std::string
This avoid having to manually free the return value.

gdb/gdbserver/ChangeLog:

	* server.c (parse_debug_format_options): Return std::string.
	(handle_monitor_command, captured_main): Adjust.
2018-01-07 10:50:48 -05:00
Pedro Alves e379cee61f Fix regression: cannot start with LD_PRELOAD=libSegFault.so (PR gdb/18653#c7)
At https://sourceware.org/bugzilla/show_bug.cgi?id=18653#c7, Andrew
reports that the fix for PR gdb/18653 made GDB useless if you preload
libSegFault.so, because GDB internal-errors on startup:

 $ LD_PRELOAD=libSegFault.so gdb
 src/gdb/common/signals-state-save-restore.c:64: internal-error: unexpected signal handler
 A problem internal to GDB has been detected,
 further debugging may prove unreliable.
 Aborted (core dumped)
 $

The internal error comes from the code saving the signal dispositions
inherited from gdb's parent:

 (top-gdb) bt
 #0  0x000000000056b001 in internal_error(char const*, int, char const*, ...) (file=0xaf5f38 "src/gdb/common/signals-state-save-restore.c", line=64, fmt=0xaf5f18 "unexpected signal handler") at src/gdb/common/errors.c:54
 #1  0x00000000005752c9 in save_original_signals_state() () at src/gdb/common/signals-state-save-restore.c:64
 #2  0x00000000007425de in captured_main_1(captured_main_args*) (context=0x7fffffffd860)
     at src/gdb/main.c:509
 #3  0x0000000000743622 in captured_main(void*) (data=0x7fffffffd860) at src/gdb/main.c:1145
 During symbol reading, cannot get low and high bounds for subprogram DIE at 24065.
 #4  0x00000000007436f9 in gdb_main(captured_main_args*) (args=0x7fffffffd860) at src/gdb/main.c:1171
 #5  0x0000000000413acd in main(int, char**) (argc=1, argv=0x7fffffffd968) at src/gdb/gdb.c:32

This commit downgrades the internal error to a warning.  You'll get
instead:

~~~
 $ LD_PRELOAD=libSegFault.so gdb
 warning: Found custom handler for signal 11 (Segmentation fault) preinstalled.
 Some signal dispositions inherited from the environment (SIG_DFL/SIG_IGN)
 won't be propagated to spawned programs.
 GNU gdb (GDB) 8.0.50.20171213-git
 Copyright (C) 2017 Free Software Foundation, Inc.
 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
 This is free software: you are free to change and redistribute it.
 There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
 and "show warranty" for details.
 This GDB was configured as "x86_64-pc-linux-gnu".
 Type "show configuration" for configuration details.
 For bug reporting instructions, please see:
 <http://www.gnu.org/software/gdb/bugs/>.
 Find the GDB manual and other documentation resources online at:
 <http://www.gnu.org/software/gdb/documentation/>.
 For help, type "help".
 Type "apropos word" to search for commands related to "word"...
 (gdb)
~~~

This also moves the location where save_original_signals_state is
called a bit further below (to after option processing), so that "-q"
disables the warning:

~~~
 $ LD_PRELOAD=libSegFault.so gdb -q
 (gdb)
~~~

New testcase included.

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

	PR gdb/18653
	* common/signals-state-save-restore.c
	(save_original_signals_state): New parameter 'quiet'.  Warn if we
	find a custom handler preinstalled, instead of internal erroring.
	But only warn if !quiet.
	* common/signals-state-save-restore.h
	(save_original_signals_state): New parameter 'quiet'.
	* main.c (captured_main_1): Move save_original_signals_state call
	after option handling, and pass QUIET.

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

	PR gdb/18653
	* server.c (captured_main): Pass quiet=false to
	save_original_signals_state.

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

	PR gdb/18653
	* gdb.base/libsegfault.exp: New.
2018-01-05 18:26:18 +00: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
Joel Brobecker 82e1e79a42 Update copyright year in version message of GDB, GDBserver and GDBreplay
gdb/ChangeLog:

	* top.c (print_gdb_version): Update Copyright year in version
	message.

gdb/gdbserver/ChangeLog:

	* gdbreplay.c (gdbreplay_version): Update copyright year in
	version message.
	* server.c (gdbserver_version): Likewise.
2018-01-02 07:38:05 +04:00
Tom Tromey 8e481c3ba8 C++-ify parse_format_string
This replaces parse_format_string with a class, removing some
constructors along the way.  While doing this, I found that one
argument to gen_printf is unused, so I removed it.

Also, I am not completely sure, but the use of `release' in
maint_agent_printf_command and parse_cmd_to_aexpr seems like it may
leak expressions.

Regression tested by the buildbot.

ChangeLog
2017-12-08  Tom Tromey  <tom@tromey.com>

	* printcmd.c (ui_printf): Update.  Use std::vector.
	* common/format.h (struct format_piece): Add constructor.
	<string>: Now const.
	(class format_pieces): New class.
	(parse_format_string, free_format_pieces)
	(free_format_pieces_cleanup): Remove.
	* common/format.c (format_pieces::format_pieces): Rename from
	parse_format_string.  Update.
	(free_format_pieces, free_format_pieces_cleanup): Remove.
	* breakpoint.c (parse_cmd_to_aexpr): Update.  Use std::vector.
	* ax-gdb.h (gen_printf): Remove argument.
	* ax-gdb.c (gen_printf): Remove "frags" argument.
	(maint_agent_printf_command): Update.  Use std::vector.

gdbserver/ChangeLog
2017-12-08  Tom Tromey  <tom@tromey.com>

	* ax.c (ax_printf): Update.
2017-12-08 10:23:43 -07:00
Yao Qi a880623024 Initialize target description early in IPA
Target descriptions are allocated lazily, that is fine in GDBserver,
but it is not safe to call malloc in gdb_collect in IPA, because we
can set a fast tracepoint in malloc, and when the tracepoint is hit,
gdb_collect/malloc is called, deadlock or memory corruption may be
triggered.

 #0  0xf7cfc200 in malloc ()
 #1  0xf7efdc07 in operator new(unsigned int) ()
 #2  0xf7ef7636 in allocate_target_description() ()
 #3  0xf7efcbe1 in i386_create_target_description(unsigned long long, bool) ()
 #4  0xf7efb474 in i386_linux_read_description(unsigned long long) ()
 #5  0xf7efb190 in get_ipa_tdesc(int) ()
 #6  0xf7ef9baa in gdb_collect ()

The fix is to initialize all target descriptions earlier, when the
IPA is loaded.  In order to guarantee malloc is not called in IPA
in gdb_collect, I change the test to set a breakpoint on malloc, if
IPA gdb_collect calls malloc, program will hit the breakpoint, and
test fail.

continue
Continuing.

Thread 1 "" hit Breakpoint 5, 0xf7cfc200 in malloc ()
(gdb) FAIL: gdb.trace/ftrace.exp: advance through tracing

gdb/gdbserver:

2017-12-07  Yao Qi  <yao.qi@linaro.org>

	* linux-aarch64-ipa.c (initialize_low_tracepoint): Call
	aarch64_linux_read_description.
	* linux-amd64-ipa.c (idx2mask): New array.
	(get_ipa_tdesc): Move idx2mask out.
	(initialize_low_tracepoint): Initialize target descriptions.
	* linux-i386-ipa.c (idx2mask): New array.
	(get_ipa_tdesc): Move idx2mask out.
	(initialize_low_tracepoint): Initialize target descriptions.

gdb/testsuite:

2017-12-07  Yao Qi  <yao.qi@linaro.org>

	* gdb.trace/ftrace.exp (run_trace_experiment): Set breakpoint on
	malloc and catch syscall.
2017-12-07 17:07:01 +00:00
Simon Marchi d4a0e8b57d Split tdesc_type into multiple classes
This patch makes tdesc_type an abstract base class and creates three
subclasses:

- tdesc_type_builtin, for builtin types
- tdesc_type_vector, for vector types
- tdesc_type_with_fields, for struct, union, flag and enum types

This allows getting rid of the union in tdesc_type and to not allow the
std::vector separately.  I tried to go further and create separate
classes for struct, union, flag and enum, but it proved too difficult.
One problem is that from the point of the of the target description
code, the types tdesc_type_* are opaque (only forward-declared).
Therefore, it doesn't know about inheritance relationship between those
classes.  This makes it impossible to make functions that accept a
pointer to a base class and pass a pointer to a derived class, for
example.  I think this patch here is a good compromise, and if somebody
wants to improve things further, the door is open.

A make_gdb_type virtual pure method is added to tdesc_type, which
replaces the current tdesc_gdb_type function.  Calling this method on a
tdesc_type returns the corresponding built gdb type.

gdb/ChangeLog:

	* target-descriptions.c (struct tdesc_type): Use default
	destructor.
	<u>: Remove.
	<accept>: Remove.
	(struct tdesc_type_builtin): New.
	(struct tdesc_type_vector): New.
	(struct tdesc_type_with_fields): New.
	(tdesc_predefined_types): Change type to tdesc_type_builtin[].
	(tdesc_gdb_type): Remove.
	(tdesc_register_type): Adjust.
	(tdesc_create_vector): Create tdesc_type_vector.
	(tdesc_create_struct): Create tdesc_type_with_fields.
	(tdesc_set_struct_size): Change parameter type.
	(tdesc_create_union): Create tdesc_type_with_fields.
	(tdesc_create_flags): Likewise.
	(tdesc_create_enum): Likewise.
	(tdesc_add_field): Change parameter type.
	(tdesc_add_typed_bitfield): Likewise.
	(tdesc_add_bitfield): Likewise.
	(tdesc_add_flag): Likewise.
	(tdesc_add_enum_value): Likewise.
	(print_c_tdesc) <visit>: Remove overload with tdesc_type
	parameter, add overloads for tdesc_type_builtin,
	tdesc_type_with_fields and tdesc_type_vector.
	<m_printed_type>: Remove.
	<m_printed_element_type, m_printed_type_with_fields>: Add.
	* target-descriptions.h (tdesc_create_enum): Change return type.
	(tdesc_add_typed_bitfield): Change parameter type.
	(tdesc_add_enum_value): Change parameter type.
	* xml-tdesc.c (struct tdesc_parsing_data) <current_type>: Change
	type to tdesc_type_with_fields.
	(tdesc_start_struct): Adjust.
	(tdesc_start_flags): Adjust.
	(tdesc_start_enum): Adjust.
	(tdesc_start_field): Adjust.
	* arch/tdesc.h (struct tdesc_type_builtin): Forward-declare.
	(struct tdesc_type_vector): Forward-declare.
	(struct tdesc_type_with_fields): Forward-declare.
	(tdesc_create_struct): Change return type.
	(tdesc_create_union): Likewise.
	(tdesc_create_flags): Likewise.
	(tdesc_add_field): Change parameter type.
	(tdesc_set_struct_size): Likewise.
	(tdesc_add_bitfield): Likewise.
	(tdesc_add_flag): Likewise.
	* features: Re-generate C files.

gdb/gdbserver/ChangeLog:

	* tdesc.c (struct tdesc_type): Change return type.
	(tdesc_add_flag): Change parameter type.
	(tdesc_add_bitfield): Likewise.
	(tdesc_add_field): Likewise.
	(tdesc_set_struct_size): Likewise.
2017-12-05 16:30:28 -05:00
Simon Marchi 798a7429f9 Remove some unused variables
This patch removes some unused variables, found with -Wunused.  I have
not removed everything reported by -Wunused, because some expressions
such as

  struct type *arg_type = check_typedef (value_type);

in bfin-tdep.c could have an unexpected but important side-effect.  I
removed others that I considered more low-risk, such as:

  struct gdbarch *gdbarch = get_objfile_arch (objfile);

I tested building with Python 2/Python 3/no Python, with/without expat,
with/without libipt and with/without babeltrace.

gdb/ChangeLog:

	* ada-lang.c (ada_collect_symbol_completion_matches): Remove
	unused variables.
	(ada_is_redundant_range_encoding): Likewise.
	* ada-varobj.c (ada_varobj_get_value_of_array_variable):
	Likewise.
	* alpha-tdep.c (alpha_software_single_step): Likewise.
	* arm-tdep.c (_initialize_arm_tdep): Likewise.
	* auto-load.c (info_auto_load_cmd): Likewise.
	* break-catch-syscall.c (insert_catch_syscall): Likewise.
	(remove_catch_syscall): Likewise.
	* breakpoint.c (condition_completer): Likewise.
	(clear_command): Likewise.
	(update_breakpoint_locations): Likewise.
	* btrace.c (btrace_disable): Likewise.
	(btrace_teardown): Likewise.
	(btrace_maint_update_pt_packets): Likewise.
	(maint_btrace_clear_cmd): Likewise.
	* cli/cli-decode.c (lookup_cmd_1): Likewise.
	(lookup_cmd_composition): Likewise.
	* cli/cli-dump.c (scan_filename): Likewise.
	(restore_command): Likewise.
	* compile/compile-loc2c.c (compute_stack_depth): Likewise.
	* compile/compile-object-load.c (compile_object_load): Likewise.
	* compile/compile-object-run.c (compile_object_run): Likewise.
	* compile/compile.c (compile_to_object): Likewise.
	* completer.c (filename_completer): Likewise.
	(complete_files_symbols): Likewise.
	(complete_expression): Likewise.
	* corelow.c (core_open): Likewise.
	* ctf.c (ctf_start): Likewise.
	(ctf_write_status): Likewise.
	(ctf_write_uploaded_tsv): Likewise.
	(ctf_write_definition_end): Likewise.
	(ctf_open_dir): Likewise.
	(ctf_xfer_partial): Likewise.
	(ctf_trace_find): Likewise.
	* disasm.c (gdb_pretty_print_disassembler::pretty_print_insn):
	Likewise.
	* dwarf2loc.c (allocate_piece_closure): Likewise.
	(indirect_pieced_value): Likewise.
	(dwarf2_evaluate_loc_desc_full): Likewise.
	* dwarf2read.c (dw2_expand_marked_cus): Likewise.
	(dw2_expand_symtabs_matching): Likewise.
	(dw2_map_symbol_filenames): Likewise.
	(read_and_check_comp_unit_head): Likewise.
	(read_cutu_die_from_dwo): Likewise.
	(lookup_dwo_unit): Likewise.
	(read_comp_units_from_section): Likewise.
	(dwarf2_compute_name): Likewise.
	(handle_DW_AT_stmt_list): Likewise.
	(create_cus_hash_table): Likewise.
	(create_dwp_v2_section): Likewise.
	(dwarf2_rnglists_process): Likewise.
	(dwarf2_ranges_process): Likewise.
	(dwarf2_record_block_ranges): Likewise.
	(is_vtable_name): Likewise.
	(read_formatted_entries): Likewise.
	(skip_form_bytes): Likewise.
	* elfread.c (elf_symtab_read): Likewise.
	* exec.c (exec_file_command): Likewise.
	* f-valprint.c (f_val_print): Likewise.
	(info_common_command_for_block): Likewise.
	* guile/guile.c (initialize_scheme_side): Likewise.
	* guile/scm-breakpoint.c (gdbscm_breakpoint_commands): Likewise.
	* guile/scm-cmd.c (cmdscm_completer): Likewise.
	(gdbscm_register_command_x): Likewise.
	* guile/scm-frame.c (gdbscm_frame_read_var): Likewise.
	* guile/scm-param.c (gdbscm_parameter_value): Likewise.
	* guile/scm-ports.c (file_port_magic): Likewise.
	* guile/scm-pretty-print.c (ppscm_search_pp_list): Likewise.
	(ppscm_pretty_print_one_value): Likewise.
	(ppscm_print_children): Likewise.
	* guile/scm-string.c (gdbscm_string_to_argv): Likewise.
	* guile/scm-symtab.c (gdbscm_sal_symtab): Likewise.
	* guile/scm-type.c (gdbscm_type_next_field_x): Likewise.
	* guile/scm-utils.c (gdbscm_parse_function_args): Likewise.
	* i386-tdep.c (i386_register_reggroup_p): Likewise.
	* infcmd.c (run_command_1): Likewise.
	(until_next_fsm_clean_up): Likewise.
	* linespec.c (linespec_complete): Likewise.
	(find_label_symbols): Likewise.
	* m2-valprint.c (m2_val_print): Likewise.
	* memattr.c (require_user_regions): Likewise.
	(lookup_mem_region): Likewise.
	(disable_mem_command): Likewise.
	(mem_delete): Likewise.
	* mep-tdep.c (mep_register_name): Likewise.
	(mep_analyze_prologue): Likewise.
	* mi/mi-cmd-file.c (mi_cmd_file_list_shared_libraries): Likewise.
	* mi/mi-interp.c (mi_on_sync_execution_done): Likewise.
	* mi/mi-main.c (mi_cmd_trace_frame_collected): Likewise.
	* microblaze-linux-tdep.c (microblaze_linux_init_abi): Likewise.
	* minidebug.c (lzma_open): Likewise.
	* minsyms.c (lookup_minimal_symbol): Likewise.
	* mips-linux-tdep.c (mips64_fill_fpregset): Likewise.
	* mips-tdep.c (mips_stub_frame_sniffer): Likewise.
	(mips_o64_return_value): Likewise.
	(mips_single_step_through_delay): Likewise.
	(_initialize_mips_tdep): Likewise.
	* nios2-tdep.c (nios2_push_dummy_call): Likewise.
	(nios2_software_single_step): Likewise.
	* parse.c (find_minsym_type_and_address): Likewise.
	* psymtab.c (psym_relocate): Likewise.
	* python/py-breakpoint.c (bppy_get_commands): Likewise.
	(gdbpy_breakpoint_modified): Likewise.
	* python/py-infevents.c (create_inferior_call_event_object):
	Likewise.
	* python/py-record-btrace.c (btpy_list_item): Likewise.
	* python/py-type.c (typy_str): Likewise.
	* python/py-value.c (valpy_call): Likewise.
	* python/python.c (do_start_initialization): Likewise.
	* record-btrace.c (record_btrace_insn_history_range): Likewise.
	(record_btrace_call_history_range): Likewise.
	(record_btrace_record_method): Likewise.
	(record_btrace_xfer_partial): Likewise.
	(btrace_get_frame_function): Likewise.
	* record-full.c (record_full_open): Likewise.
	* record.c (get_context_size): Likewise.
	* registry.h (DEFINE_REGISTRY): Likewise.
	* remote-fileio.c (remote_fileio_request): Likewise.
	* remote.c (remote_update_thread_list): Likewise.
	(remote_check_symbols): Likewise.
	(remote_commit_resume): Likewise.
	(remote_interrupt): Likewise.
	(remote_insert_breakpoint): Likewise.
	(compare_sections_command): Likewise.
	* rust-exp.y (super_name): Likewise.
	(lex_string): Likewise.
	(convert_ast_to_type): Likewise.
	(convert_ast_to_expression): Likewise.
	* rust-lang.c (rust_print_struct_def): Likewise.
	(rust_print_type): Likewise.
	(rust_evaluate_subexp): Likewise.
	* rx-tdep.c (rx_register_type): Likewise.
	* ser-event.c (serial_event_clear): Likewise.
	* serial.c (serial_open): Likewise.
	* spu-tdep.c (spu_overlay_new_objfile): Likewise.
	* symfile.c (section_is_overlay): Likewise.
	(overlay_unmapped_address): Likewise.
	(overlay_mapped_address): Likewise.
	(simple_overlay_update_1): Likewise.
	(simple_overlay_update): Likewise.
	* symtab.c (symbol_find_demangled_name): Likewise.
	(search_symbols): Likewise.
	* target-descriptions.c (tdesc_predefined_type): Likewise.
	* target.c (target_commit_resume): Likewise.
	* thread.c (print_selected_thread_frame): Likewise.
	* top.c (new_ui_command): Likewise.
	(gdb_readline_no_editing): Likewise.
	* tracefile-tfile.c (tfile_open): Likewise.
	* tracepoint.c (create_tsv_from_upload): Likewise.
	* utils.c (quit): Likewise.
	(defaulted_query): Likewise.
	* valarith.c (value_concat): Likewise.
	* xml-syscall.c (xml_list_syscalls_by_group): Likewise.
	* xml-tdesc.c (target_fetch_description_xml): Likewise.
	* xtensa-tdep.c (xtensa_pseudo_register_read): Likewise.
	(xtensa_pseudo_register_write): Likewise.

gdb/gdbserver/ChangeLog:

	* regcache.c (registers_to_string): Remove unused variable.
2017-12-05 16:05:50 -05:00
Simon Marchi c0e15c9bfd Remove for_each_inferior_with_data
Remove for_each_inferior_with_data, replacing its sole usage with
for_each_thread.

gdb/gdbserver/ChangeLog:

	* inferiors.c (for_each_inferior_with_data): Remove.
	* inferiors.h (for_each_inferior_with_data): Remove.
	* server.c (handle_qxfer_threads_worker): Change parameter type.
	(handle_qxfer_threads_proper): Use for_each_thread.
2017-12-02 20:38:37 -05:00
Simon Marchi f004534791 Remove for_each_inferior
This patch removes for_each_inferior, replacing all its usages with
for_each_thread.

gdb/gdbserver/ChangeLog:

	* inferiors.c (for_each_inferior): Remove.
	(clear_inferiors): Use for_each_thread.
	* inferiors.h (for_each_inferior): Remove.
	* linux-low.c (linux_wait_for_event_filtered): Use
	for_each_thread.
	(linux_stabilize_threads): Likewise.
	* regcache.c (regcache_release): Likewise.
	* server.c (gdb_wants_all_threads_stopped): Likewise.
	(clear_pending_status_callback): Remove.
	(handle_status): Use for_each_thread.
	(captured_main): Likewise.
	* win32-low.c (child_init_thread_list): Likewise.
	(win32_clear_inferiors): Likewise.
	(fake_breakpoint_event): Likewise.
2017-12-02 20:37:53 -05:00
Simon Marchi 9521758bf9 Remove find_inferior
All the usages of find_inferior were removed, so the function itself can
be removed.

gdb/gdbserver/ChangeLog:

	* inferiors.h (find_inferior): Remove.
	* inferiors.c (find_inferior): Remove.
2017-12-02 20:36:45 -05:00
Simon Marchi 8f86d7aa85 Update comments
These functions were modified in the previous patch series, but I forgot
to update some comments.

gdb/gdbserver/ChangeLog:

	* linux-low.c (resume_status_pending_p): Update comment.
	(need_step_over_p): Update comment.
2017-12-02 20:36:44 -05:00
Simon Marchi e2b4407579 Remove usages of find_inferior that call proceed_one_lwp
Replace with for_each_thread.

gdb/gdbserver/ChangeLog:

	* linux-low.c (proceed_one_lwp): Return void, change parameter
	type.
	(unsuspend_and_proceed_one_lwp): Likewise.
	(proceed_all_lwps): Use for_each_thread.
	(unstop_all_lwps): Likewise.
2017-12-02 20:36:43 -05:00
Simon Marchi c80825ff32 Remove usage of find_inferior in linux_resume
Replace with for_each_thread.

gdb/gdbserver/ChangeLog:

	* linux-low.c (linux_resume_one_thread): Return void, take
	parameter directly.
	(linux_resume): Use for_each_thread.
2017-12-02 20:36:42 -05:00
Simon Marchi df3e4dbe66 Remove usages of find_inferior in stop_all_lwps
Replace with for_each_thread.

gdb/gdbserver/ChangeLog:

	* linux-low.c (send_sigstop_callback): Return void, change
	parameter type.  Rename to...
	(send_sigstop): ... this.
	(suspend_and_send_sigstop_callback): Return void, change parameter
	type.  Rename to...
	(suspend_and_send_sigstop): ... this.
	(stop_all_lwps): Use for_each_thread.
2017-12-02 20:36:42 -05:00
Simon Marchi 5a6b0a41df Remove usage of find_inferior in linux_stabilize_threads
Replace with find_thread.

gdb/gdbserver/ChangeLog:

	* linux-low.c (lwp_running): Return bool, remove unused
	argument.
	(linux_stabilize_threads): Use find_thread.
2017-12-02 20:36:41 -05:00
Simon Marchi 39a64da5fc Remove usages of find_inferior in select_event_lwp
Replace with find_thread/for_each_thread.  I inlined the callbacks,
because they are relatively simple.

gdb/gdbserver/ChangeLog:

	* linux-low.c (select_singlestep_lwp_callback): Remove.
	(count_events_callback): Remove.
	(select_event_lwp_callback): Remove.
	(select_event_lwp): Use find_thread/for_each_thread.
2017-12-02 20:36:40 -05:00
Simon Marchi a1385b7b88 Remove usages of find_inferior calling not_stopped_callback
Replace with find_thread.  Writing a lambda inline in directly in the if
conditions would be a bit messy, so I chose to assign them to variables
instead.

gdb/gdbserver/ChangeLog:

	* linux-low.c (not_stopped_callback): Return bool, take filter
	argument directly.
	(linux_wait_for_event_filtered): Use find_thread.
	(linux_wait_1): Likewise.
2017-12-02 20:36:39 -05:00
Simon Marchi 454296a2c1 Remove usage of find_inferior in find_lwp_pid
Replace with find_thread.  We could almost use find_thread_ptid, except
that find_lwp_pid uses the pid of the input ptid of the lwp is 0, so the
behavior is not quite the same.

gdb/gdbserver/ChangeLog:

	* linux-low.c (same_lwp): Remove.
	(find_lwp_pid): Use find_thread.
2017-12-02 20:36:38 -05:00
Simon Marchi 6b2a85daf5 Remove usage of find_inferior in linux_mourn
Replace with for_each_thread with pid filtering.  The callback becomes
trivial enough that it's better to inline it.

gdb/gdbserver/ChangeLog:

	* linux-low.c (delete_lwp_callback): Remove.
	(linux_mourn): Use for_each_thread.
2017-12-02 20:36:37 -05:00
Simon Marchi 798a38e8de Remove usage of find_inferior in linux_detach
Replace with for_each_thread with pid filtering.

gdb/gdbserver/ChangeLog:

	* linux-low.c (linux_detach_lwp_callback): Return void, remove
	args parameter, don't check for pid.
	(linux_detach): Use for_each_thread.
2017-12-02 20:36:36 -05:00
Simon Marchi e4eb0dec0b Remove usage of find_inferior in last_thread_of_process_p
Replace it with find_thread.  I also modified the code a bit to use a
lambda and a boolean.

gdb/gdbserver/ChangeLog:

	* linux-low.c (struct counter): Remove.
	(second_thread_of_pid_p): Remove.
	(last_thread_of_process_p): Use find_thread.
2017-12-02 20:36:36 -05:00
Simon Marchi 83e1b6c13a Remove find_inferior_in_random
Replace with find_thread_in_random.

gdb/gdbserver/ChangeLog:

	* inferiors.c (find_inferior_in_random): Remove.
	* inferiors.h (find_inferior_in_random): Remove.
	* linux-low.c (status_pending_p_callback): Return bool, accept
	parameter ptid directly.
	(linux_wait_for_event_filtered): Use find_thread_in_random.
	(linux_wait_1): Likewise.
2017-12-02 20:36:35 -05:00
Simon Marchi 8dc7b443a6 Remove find_inferior_id
Remove find_inferior_id, replacing its usages with find_thread_ptid.
find_thread_ptid was implemented using find_inferior_id, so move the
implementation there instead.

gdb/gdbserver/ChangeLog:

	* inferiors.c (find_inferior_id): Remove.
	(find_thread_ptid): Move implemention from find_inferior_id to
	here.
	* inferiors.h (find_inferior_id): Remove.
	* server.c (handle_status): Use find_thread_ptid.
	(process_serial_event): Likewise.
	* thread-db.c (find_one_thread): Likewise.
	(thread_db_thread_handle): Likewise.
	* win32-low.c (thread_rec): Likewise.
	(child_delete_thread): Likewise.
	(win32_thread_alive): Likewise.
	(get_child_debug_event): Likewise.
2017-12-02 20:36:34 -05:00
Simon Marchi da25033cd9 Remove usages of find_inferior in linux-mips-low.c
Replace with for_each_thread with pid filtering.  This allows
simplifying the callback a little bit.

gdb/gdbserver/ChangeLog:

	* linux-mips-low.c (update_watch_registers_callback): Return
	void, remove pid_p parameter, don't check for pid.
	(mips_insert_point, mips_remove_point): Use for_each_thread.
2017-12-02 20:36:33 -05:00
Simon Marchi c91bb56b06 Remove usage of find_inferior in lynx_mourn
Replace it with for_each_thread with pid filtering.  We can remove
lynx_delete_thread_callback and pass remove_thread directly.

I can't build/test this change, but it should be obvious enough.

gdb/gdbserver/ChangeLog:

	* lynx.low (lynx_delete_thread_callback): Remove.
	(lynx_mourn): Use for_each_thread.
2017-12-02 20:36:32 -05:00
Simon Marchi 634a3254c8 Remove usage of find_inferior in regcache_invalidate_pid
Replace with for_each_thread with pid filtering.
regcache_invalidate_one is not longer needed, as it was only used to
filter the pid.  We can call regcache_invalidate_thread directly.

gdb/gdbserver/ChangeLog:

	* regcache.c (regcache_invalidate_one): Remove.
	(regcache_invalidate_pid): use for_each_thread.
2017-12-02 20:36:32 -05:00
Rainer Orth 44122162ae Remove ioctl-based procfs support on Solaris
This is the previously mentioned patch to get rid of
unstructured/ioctl-based procfs support in procfs.c.  Given that support
for structured procfs was introduced in Solaris 2.6 back in 1997 and
we're just removing support for Solaris < 10, there's no point in
carrying that baggage (and tons of support for IRIX and OSF/1 as well)
around any longer.

Most of the patch should be straightforward (removing support for
!NEW_PROC_API, non-Solaris OSes and pre-Solaris 10 quirks).

Only a few points need explanations:

* <sys/syscall.h> was already included unconditionally in most places,
  so there's no need to have guards in a few remaining ones.

* configure.host already obsoletes i?86-*-sysv4.2, i?86-*-sysv5, so
  NEW_PROC_API detection for those in configure.ac can go.

* I'm still including <sys/procfs.h> with #define _STRUCTURED_PROC 1.
  Theoretically, it would be better to include <procfs.h> on Solaris
  (which includes that define), but that breaks the build over
  <procfs.h> vs. gdb's "procfs.h", and doesn't exist on Linux.

* I've regenerated syscall_table[] in proc-events.c with a small script
  from Solaris 10, 11.3, 11.4 <sys/syscall.h>, so there should be no
  traces of older Solaris versions and other OSes left.

* prsysent_t and DYNAMIC_SYSCALLS was only used for AIX 5, but AIX
  doesn't use procfs.c any longer, so all related code can go.

The patch was generated with diff -w so one can easier see changes
without being distracted by simple reindentations.

So far, it has only been compiled and smoke-tested on
amd64-pc-solaris2.1[01], sparcv9-sun-solaris2.1[01], and
x86_64-pc-linux-gnu.  Certainly needs more testing (Solaris 11.3
vs. 11.4, 32-bit gdb, testsuite once I've figured out what's wrong on
Solaris 10 etc.), but it's enough to get a first impression how much
cleanup is possible here.

	* configure.ac Don't check for sys/fault.h, sys/syscall.h,
	sys/proc.h.
	(NEW_PROC_API): Remove.
	(prsysent_t, pr_sigset_t, pr_sigaction64_t, pr_siginfo64_t):
	Likewise.
	* common/common.m4 (GDB_AC_COMMON): Don't check for sys/syscall.h.
	* configure: Regenerate.
	* config.in: Regenerate.
	* gdbserver/configure: Regenerate.
	* gdbserver/config.in: Regenerate.

	* i386-sol2-nat.c (_initialize_amd64_sol2_nat): Remove
	NEW_PROC_API test.
	* sparc-sol2-nat.c (_initialize_sparc_sol2_nat): Likewise.

	* linux-btrace.c: Remove HAVE_SYS_SYSCALL_H test.

	* proc-api.c: Remove !NEW_PROC_API support.
	Remove HAVE_SYS_PROC_H and HAVE_SYS_USER_H tests.
	Remove tests for macros always defined on Solaris.
	* proc-events.c: Remove !NEW_PROC_API support.
	Remove Remove HAVE_SYS_SYSCALL_H, HAVE_SYS_PROC_H and
	HAVE_SYS_USER_H tests.
	(init_syscall_table): Remove non-Solaris syscalls.
	Remove tests for syscalls present on all Solaris versions.
	Add missing Solaris 10+ syscalls.
	(signal_table): Remove non-Solaris signals.
	Remove tests for signals present on all Solaris versions.
	(fault_table): Remove non-Solaris faults.
	Remove tests for faults present on all Solaris versions.
	* proc-flags.c: Remove !NEW_PROC_API support.
	(pr_flag_table): Remove non-Solaris and pre-Solaris 7 comments.
	Remove non-Solaris flags.
	* proc-why.c: Remove !NEW_PROC_API support.
	(pr_why_table): Remove meaningless comments.
	Remove tests for reasons present on all Solaris versions.
	Remove OSF/1 cases.
	(proc_prettyfprint_why): Likewise.

	* procfs.c: Remove !NEW_PROC_API and DYNAMIC_SYSCALLS support.
	Remove HAVE_SYS_FAULT_H and HAVE_SYS_SYSCALL_H tests.
	Remove WA_READ test, IRIX watchpoint support.
	(gdb_sigset_t, gdb_sigaction_t, gdb_siginfo_t): Replace by base
	types.  Change users.
	(gdb_praddset, gdb_prdelset, gdb_premptysysset, gdb_praddsysset)
	(gdb_prdelset, gdb_pr_issyssetmember): Replace by base macros.
	Change callers.
	Remove CTL_PROC_NAME_FMT tests.
	(gdb_prstatus_t, gdb_lwpstatus_t): Replace by base types.  Change
	users.
	(sysset_t_size): Remove.  Use sizeof (sysset_t) in callers.
	Remove PROCFS_DONT_PIOCSSIG_CURSIG support.
	(proc_modify_flag): Replace GDBRESET by PCUNSET.
	Remove PR_ASYNC, PR_KLC tests.
	(proc_unset_inherit_on_fork): Remove PR_ASYNC test.
	(proc_parent_pid): Remove PCWATCH etc. tests.
	(proc_set_watchpoint): Remove !PCWATCH && !PIOCSWATCH support.
	Remove PCAGENT test.
	(proc_get_nthreads) [PIOCNTHR && PIOCTLIST]: Remove.
	Remove SYS_lwpcreate || SYS_lwp_create test.
	(proc_get_current_thread): Likewise.
	[PIOCNTHR && PIOCTLIST]: Remove.
	[PIOCLSTATUS]: Remove.
	(procfs_debug_inferior): Remove non-Solaris cases, conditionals.
	[PRFS_STOPEXEC]: Remove.
	(syscall_is_lwp_exit): Remove non-Solaris cases, conditionals.
	(syscall_is_exit): Likewise.
	(syscall_is_exec): Likewise.
	(syscall_is_lwp_create): Likewise.
	Remove SYS_syssgi support.
	(procfs_wait): Remove PR_ASYNC, !PIOCSSPCACT tests.
	[SYS_syssgi]: Remove.
	Remove non-Solaris cases, conditionals.
	(unconditionally_kill_inferior) [PROCFS_NEED_PIOCSSIG_FOR_KILL]:
	Remove.
	(procfs_init_inferior) [SYS_syssgi]: Remove.
	(procfs_set_exec_trap) [PRFS_STOPEXEC]: Remove.
	(procfs_inferior_created) [SYS_syssgi]: Remove.
	(procfs_set_watchpoint): Remove !AIX5 test.
	(procfs_stopped_by_watchpoint): Remove FLTWATCH test, FLTKWATCH
	case.
	(mappingflags) [MA_PHYS]: Remove.
	(info_mappings_callback): Remove PCAGENT test.
	Remove PIOCOPENLWP || PCAGENT test.
2017-11-30 16:05:30 +01:00
Tom Tromey 41272101db Change maybe_disable_address_space_randomization to a class
This changes maybe_disable_address_space_randomization to be an RAII
class, rather than having it return a cleanup.

Regression tested by the buildbot.

ChangeLog
2017-11-26  Tom Tromey  <tom@tromey.com>

	* nat/linux-personality.h (class
	maybe_disable_address_space_randomization): New class.
	(maybe_disable_address_space_randomization): Don't declare
	function.
	* nat/linux-personality.c (restore_personality)
	(make_disable_asr_cleanup): Remove.
	(maybe_disable_address_space_randomization): Now a constructor.
	(~maybe_disable_address_space_randomization): New destructor.
	* linux-nat.c (linux_nat_create_inferior): Update.

gdbserver/ChangeLog
2017-11-26  Tom Tromey  <tom@tromey.com>

	* linux-low.c (linux_create_inferior): Update.
2017-11-26 10:42:15 -07:00
Ulrich Weigand f5291a6f32 [spu] Fix spu-linux gdbserver build
Fix a typo in a newly added argument name.

gdb/ChangeLog:
2017-11-24  Ulrich Weigand  <uweigand@de.ibm.com>

	* spu-low.c (spu_create_inferior): Fix typo in argument name.
2017-11-24 22:04:41 +01:00
Alan Hayward 6654d750c7 Add xml selftests for aarch64 target description.
gdb/
	* aarch64-tdep.c (_initialize_aarch64_tdep): Add target desc
	selftest.

gdbserver/
	* configure.srv: Add linux-aarch64-tdesc-selftest.o.
	* linux-aarch64-low.c (initialize_low_arch): Call init func.
	* linux-aarch64-tdesc-selftest.c: New file.
	* linux-aarch64-tdesc.h (initialize_low_tdesc): New declaration.
2017-11-24 11:18:19 +00:00
Alan Hayward 49bdb7ee48 Use flexible target descriptors for aarch64
gdb/
	* aarch64-tdep.c (_initialize_aarch64_tdep): Remove init.
	* arch/aarch64.c (aarch64_create_target_description): Create
	new target description.
	* features/Makefile: Add new files.
	* features/aarch64.c: Remove file.
	* features/aarch64-core.c: New autogenerated file.
	* features/aarch64-fpu.c: New autogenerated file.
	* target-descriptions.c (maint_print_c_tdesc_cmd): Check for aarch64.

gdbserver/
	* linux-aarch64-ipa.c (initialize_low_tracepoint): Remove init.
	* linux-aarch64-low.c (initialize_low_arch): Remove init.
	* linux-aarch64-tdesc.c (aarch64_linux_read_description): Add init.
2017-11-24 11:18:19 +00:00
Alan Hayward d6d7ce5623 gdbserver: add aarch64_create_target_description
gdbserver/
	* configure.srv: Add new files.
	* linux-aarch64-ipa.c (get_ipa_tdesc): Call
	aarch64_linux_read_description.
	* linux-aarch64-low.c (aarch64_linux_read_description):
	Merge with aarch64_arch_setup.
	(aarch64_arch_setup): Call aarch64_linux_read_description.
	* linux-aarch64-tdesc.c: New file.
	* linux-aarch64-tdesc.h: New file.
2017-11-24 11:18:19 +00:00
Yao Qi 506fe5f499 Change tic6x target descriptions
This patch changes tic6x target descriptions to be more flexible.  Rebuild
tic6x-uclinux GDBserver with my x86 g++, and the unit test passes.

gdb:

2017-11-24  Yao Qi  <yao.qi@linaro.org>

	* arch/tic6x.c: New file.
	* arch/tic6x.h: New file.
	* features/Makefile (FEATURE_XMLFILES): Add tic6x-c6xp.xml,
	tic6x-core.xml and tic6x-gp.xml.
	* features/tic6x-c6xp.c: Generated.
	* features/tic6x-core.c: Generated.
	* features/tic6x-gp.c: Generated.
	* target-descriptions.c (maint_print_c_tdesc_cmd): Match
	"tic6x-".

gdb/gdbserver:

2017-11-24  Yao Qi  <yao.qi@linaro.org>

	* configure.srv: Set $srv_regobj for tic6x-linux.
	* linux-tic6x-low.c: Include "arch/tic6x.h" and "tdesc.h".
	(tic6x_read_description): Move some code to tic6x_arch_setup.
	(tic6x_tdesc_test): New function.
	(initialize_low_arch): Call selftests::register_test.
2017-11-24 09:29:43 +00:00
Yao Qi 29f9a56737 Fix build with GCC 8: strncpy -> memcpy
Recent gcc 8 trunk emits the warning below,

../../../binutils-gdb/gdb/gdbserver/remote-utils.c:1204:14: error: ‘char* strncpy(char*, const char*, size_t)’ output truncated before terminating nul copying 6 bytes from a string of the same length [-Werror=stringop-truncation]
      strncpy (buf, "watch:", 6);
      ~~~~~~~~^~~~~~~~~~~~~~~~~~

../../binutils-gdb/gdb/cli/cli-decode.c:1118:15: error: ‘char* strncpy(char*, const char*, size_t)’ specified bound depends on the length of the source argument [-Werror=stringop-overflow=]
       strncpy (cmdtype1 + 1, cmdtype, len - 1);
       ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../binutils-gdb/gdb/cli/cli-decode.c:1110:16: note: length computed here
   len = strlen (cmdtype);
         ~~~~~~~^~~~~~~~~
../../binutils-gdb/gdb/cli/cli-decode.c:1120:15: error: ‘char* strncpy(char*, const char*, size_t)’ specified bound depends on the length of the source argument [-Werror=stringop-overflow=]
       strncpy (cmdtype2, cmdtype, len - 1);
       ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../binutils-gdb/gdb/cli/cli-decode.c:1110:16: note: length computed here
   len = strlen (cmdtype);
         ~~~~~~~^~~~~~~~~

../../binutils-gdb/gdb/cp-namespace.c:1071:11: error: ‘char* strncpy(char*, const char*, size_t)’ output truncated before terminating nul copying 2 bytes from a string of the same length [-Werror=stringop-truncation]
   strncpy (full_name + scope_length, "::", 2);
   ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This patch fixes it by using memcpy instead of strncpy.

gdb:

2017-11-22  Yao Qi  <yao.qi@linaro.org>

	* cli/cli-decode.c (help_list): Use memcpy instead of strncpy.
	* cp-namespace.c (cp_lookup_transparent_type_loop): Likewise.

gdb/gdbserver:

2017-11-22  Yao Qi  <yao.qi@linaro.org>

	* remote-utils.c (prepare_resume_reply): Use memcpy.
2017-11-22 12:22:11 +00:00
Simon Marchi 578290ecaf Remove usage of find_inferior when calling kill_one_lwp_callback
Replace with for_each_thread.

gdb/gdbserver/ChangeLog:

	* linux-low.c (kill_one_lwp_callback): Return void, take
	argument directly, don't filter on pid.
	(linux_kill): Use for_each_thread.
2017-11-19 22:23:28 -05:00
Simon Marchi eca55aec1d Remove usages of find_thread when calling need_step_over_p
Replace with find_thread.

gdb/gdbserver/ChangeLog:

	* linux-low.c (need_step_over_p): Return bool, remove dummy
	argument.
	(linux_resume, proceed_all_lwps): Use find_thread.
2017-11-19 22:23:27 -05:00
Simon Marchi 25c28b4d15 Remove usage of find_thread when calling resume_status_pending_p
Replace with find_thread.  Instead of setting the flag in the callback,
make the callback return true/false, and check the result against NULL
in the caller.

gdb/gdbserver/ChangeLog:

	* linux-low.c (resume_status_pending_p): Return bool, remove
	flag_p argument.
	(linux_resume): Use find_thread.
2017-11-19 22:23:27 -05:00
Simon Marchi 5fdda39248 Remove usage of find_inferior when calling linux_set_resume_request
Replace it with for_each_thread.

gdb/gdbserver/ChangeLog:

	* linux-low.c (struct thread_resume_array): Remove.
	(linux_set_resume_request): Return void, take arguments
	directly.
	(linux_resume): Use for_each_thread.
2017-11-19 22:23:26 -05:00
Simon Marchi fcb056a58d Remove usage of find_inferior in linux_stabilize_threads
Simply replace with find_thread.

gdb/gdbserver/ChangeLog:

	* linux-low.c (stuck_in_jump_pad_callback): Change prototype,
	return bool, remove data argument.
	(linux_stabilize_threads): Use find_thread.
2017-11-19 22:23:25 -05:00
Simon Marchi 139720c5b3 Remove usage of find_inferior in unsuspend_all_lwps
Replace with for_each_thread.  I inlined unsuspend_one_lwp in
unsuspend_all_lwps, since it is very simple.

gdb/gdbserver/ChangeLog:

	* linux-low.c (unsuspend_one_lwp): Remove.
	(unsuspend_all_lwps): Use for_each_thread, inline code from
	unsuspend_one_lwp.
2017-11-19 22:23:24 -05:00
Simon Marchi 6d1e5673fe Remove usage of find_inferior in iterate_over_lwps
Replace find_inferior with find_thread.  Since it may be useful in the
future, I added another overload to find_thread which filters based on a
ptid (using ptid_t::matches), so now iterate_over_lwps doesn't have to
do the filtering itself.  iterate_over_lwps_filter is removed and
inlined into iterate_over_lwps.

gdb/gdbserver/ChangeLog:

	* gdbthread.h (find_thread): Add overload with ptid_t filter.
	* linux-low.c (struct iterate_over_lwps_args): Remove.
	(iterate_over_lwps_filter): Remove.
	(iterate_over_lwps): Use find_thread.
2017-11-19 22:23:23 -05:00
Simon Marchi bbf550d50e Remove usage of find_inferior in reset_lwp_ptrace_options_callback
Replace with for_each_thread, and inline code from
reset_lwp_ptrace_options_callback.

gdb/gdbserver/ChangeLog:

	* linux-low.c (reset_lwp_ptrace_options_callback): Remove.
	(linux_handle_new_gdb_connection): Use for_each_thread, inline
	code from reset_lwp_ptrace_options_callback.
2017-11-19 22:23:23 -05:00
Simon Marchi 00192f7717 Remove usages of find_inferior in linux-arm-low.c
Replace two usages with the overload of for_each_thread that filters on
pid.  It allows to simplify the callback a little bit.

gdb/gdbserver/ChangeLog:

	* linux-arm-low.c (struct update_registers_data): Remove.
	(update_registers_callback): Return void, take arguments
	directly, don't check thread's pid.
	(arm_insert_point, arm_remove_point): Use for_each_thread.
2017-11-19 22:23:22 -05:00
Simon Marchi 2bee2b6ca4 Remove usage of find_inferior in win32-low.c
Replace with for_each_thread.

gdb/gdbserver/ChangeLog:

	* win32-low.c (continue_one_thread): Return void, take argument
	directly.
	(child_continue): Use for_each_thread.
2017-11-19 22:23:21 -05:00
Simon Marchi 0b360f1926 Remove usage of find_inferior in win32-i386-low.c
Straightforward replacement of find_inferior with the overload of
for_each_thread that filters on pid.  I am able to build-test this
patch, but not run it.

gdb/gdbserver/ChangeLog:

	* win32-i386-low.c (update_debug_registers_callback): Rename
	to ...
	(update_debug_registers): ... this, return void, remove pid_p arg.
	(x86_dr_low_set_addr, x86_dr_low_set_control): Use for_each_thread.
2017-11-19 22:23:20 -05:00
Simon Marchi f27866ba9c Make process_info::syscalls_to_catch an std::vector
This patch makes the syscalls_to_catch field of process_info an
std::vector<int>.  The process_info structure must now be
newed/deleted.

In handle_extended_wait, the code that handles exec events destroys the
existing process_info and creates a new one.  It moves the content of
syscalls_to_catch from the old to the new vector.  I used std::move for
that (through an intermediary variable), which should have the same
behavior as the old code.

gdb/gdbserver/ChangeLog:

	* inferiors.h (struct process_info): Add constructor, initialize
	fields..
	<syscalls_to_catch>: Change type to std::vector<int>.
	* inferiors.c (add_process): Allocate process_info with new.
	(remove_process): Free process_info with delete.
	* linux-low.c (handle_extended_wait): Adjust.
	(gdb_catching_syscalls_p, gdb_catch_this_syscall_p): Adjust.
	* server.c (handle_general_set): Adjust.
2017-11-17 13:03:34 -05:00
Pedro Alves e849ea896b GDBserver: Fix ignored Ctrl-C after reconnection
This fixes the issue reported by Dmitry Antipov <dantipov@nvidia.com>
here:
  https://sourceware.org/ml/gdb/2017-10/msg00048.html

The problem is that GDBserver stops listening to Ctrl-C/interrupt
requests if you disconnect and reconnect back.

Dmitry wrote:

~~~
Currently gdbserver installs SIGIO handler just once, in
initialize_async_io() called from captured_main(), and this handler is
removed when remote_desc is closed in remote_close().  Next, when a
new instance of remote_desc is fetched from accept() and has '\003'
arrived, input_interrupt() is never called because it is not
registered as SIGIO handler.
~~~

The fix here is not remove the SIGIO handler in the first place, thus
going back to the original before-first-connection state.

(I haven't gone back to try it, but I think this was a regression
caused by commit 8b20733984 ("[GDBserver] Block and unblock SIGIO"),
which was what made remote_close remove the signal handler.)

New test included.

gdb/gdbserver/ChangeLog:
2017-11-16  Pedro Alves  <palves@redhat.com>

	* remote-utils.c (remote_close): Block SIGIO signals instead of
	uninstalling the SIGIO handler.

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

	* gdb.server/reconnect-ctrl-c.c: New file.
	* gdb.server/reconnect-ctrl-c.exp: New file.
2017-11-16 14:58:51 +00:00
Alan Hayward 1d0aa65c24 Allow osabi to be optional in target descriptions
gdbserver/
	* tdesc.c (tdesc_get_features_xml): Allow null osabi.
2017-11-16 10:09:17 +00:00
Yao Qi 3491a34c4f Fix tic6x-uclinux GDBserver build failure
I can't find a c6x-uclinux c++ compiler, so I use my host g++ to build
tic6x-uclinux GDBserver, and find the following build failures.  They are
not target specific, so I believe they are real errors.  This patch fixes
them.

../binutils-gdb/gdb/gdbserver/linux-tic6x-low.c:313:34: error: invalid
conversion from 'void*' to 'tic6x_register*' [-fpermissive]
   union tic6x_register *regset = buf;
                                  ^
../binutils-gdb/gdb/gdbserver/linux-tic6x-low.c: In function 'void tic6x_store_gregset(regcache*, const void*)':
../binutils-gdb/gdb/gdbserver/linux-tic6x-low.c:324:40: error: invalid
conversion from 'const void*' to 'const tic6x_register*' [-fpermissive]
   const union tic6x_register *regset = buf;
                                        ^

../binutils-gdb/gdb/gdbserver/linux-tic6x-low.c: At global scope:
../binutils-gdb/gdb/gdbserver/linux-tic6x-low.c:359:28: error: redefinition of 'usrregs_info tic6x_usrregs_info'
 static struct usrregs_info tic6x_usrregs_info =
                            ^
../binutils-gdb/gdb/gdbserver/linux-tic6x-low.c:186:28: note: 'usrregs_info tic6x_usrregs_info' previously declared here
 static struct usrregs_info tic6x_usrregs_info;
                            ^

gdb/gdbserver:

2017-11-16  Yao Qi  <yao.qi@linaro.org>

	* linux-tic6x-low.c (tic6x_fill_gregset): Cast buf.
	(tic6x_store_gregset): Likewise.
	(tic6x_usrregs_info): Move it up.
2017-11-16 10:05:27 +00:00
Alan Hayward a602f924c8 Better make rule for arch/ files built for IPA
gdbserver/
	* Makefile.in: Update arch rules.
	* configure.srv: Explicitly mark arch/ files.
2017-11-15 09:59:12 +00:00