Commit Graph

15 Commits

Author SHA1 Message Date
Tom de Vries
e686338105 [gdbsupport] Add gdb::array_view::{iterator,const_iterator}
While trying to substitute some std::vector type A in the code with a
gdb::array_view:
...
- using A = std::vector<T>
+ using A = gdb::array_view<T>
....
I ran into the problem that the code was using A::iterator while
gdb::array_view doesn't define such a type.

Fix this by:
- adding types gdb::array_view::iterator and gdb::array_view::const_iterator,
- using them in gdb::array_view::(c)begin and gdb::array_view::(c)end, as is
  usual, and
- using them explicitly in a unit test.

Tested on aarch64-linux.

Approved-By: Tom Tromey <tom@tromey.com>
2024-10-19 08:10:38 +02:00
Tom de Vries
6c8052f6b2 [gdbsupport] Use std::span-style iterators for gdb::array_view
There's a plan to replace gdb::array_view with std::span (PR31422), and making
gdb::array_view more like std::span helps with that.

One difference is that std::span has:
...
constexpr iterator begin() const noexcept;
constexpr const_iterator cbegin() const noexcept;
...
while gdb::array_view has:
...
constexpr T *begin () noexcept;
constexpr const T *begin () const noexcept;
...

Fix this by renaming the second variant to cbegin, and making the first
variant const.

Likewise for gdb::array_view::end.

Tested on aarch64-linux.

Approved-By: Tom Tromey <tom@tromey.com>
2024-10-19 08:10:38 +02:00
Andrew Burgess
1d506c26d9 Update copyright year range in header of all files managed by GDB
This commit is the result of the following actions:

  - Running gdb/copyright.py to update all of the copyright headers to
    include 2024,

  - Manually updating a few files the copyright.py script told me to
    update, these files had copyright headers embedded within the
    file,

  - Regenerating gdbsupport/Makefile.in to refresh it's copyright
    date,

  - Using grep to find other files that still mentioned 2023.  If
    these files were updated last year from 2022 to 2023 then I've
    updated them this year to 2024.

I'm sure I've probably missed some dates.  Feel free to fix them up as
you spot them.
2024-01-12 15:49:57 +00:00
Tom Tromey
dead89d276 Enable some C++14 code in array-view.h
This changes gdbsupport/array-view.h to enable some code that is
C++14-specific.

Approved-By: Pedro Alves <pedro@palves.net>
2023-11-29 14:29:44 -07:00
Simon Marchi
8cb2c7fc2e gdbsupport: mark array_view::slice with [[nodiscard]]
I (almost) had a bug where I did:

    buffer.slice (...)

but I meant:

    buffer = buffer.slice (...)

The first one does nothing, it creates a new array_view but without
using it, it's useless.  Mark the slice methods with [[nodiscard]]
(which is standard C++17) so that error would generate a warning.

I guess that many functions could be marked as nodiscard, essentially
function that is pure (doesn't have side-effects).  But this one seems
particularly easy to mis-use.

Change-Id: Ib39a0a65a5728a3cfd68a02ae31635810baeaccb
Approved-By: Tom Tromey <tom@tromey.com>
2023-11-03 14:27:26 -04:00
Tom de Vries
3bfdcabbc2 [gdb] Fix more typos
Fix some more typos:
- distinquish -> distinguish
- actualy -> actually
- singe -> single
- frash -> frame
- chid -> child
- dissassembler -> disassembler
- uninitalized -> uninitialized
- precontidion -> precondition
- regsiters -> registers
- marge -> merge
- sate -> state
- garanteed -> guaranteed
- explictly -> explicitly
- prefices (nonstandard plural) -> prefixes
- bondary -> boundary
- formated -> formatted
- ithe -> the
- arrav -> array
- coresponding -> corresponding
- owend -> owned
- fials -> fails
- diasm -> disasm
- ture -> true
- tpye -> type

There's one code change, the name of macro SIG_CODE_BONDARY_FAULT changed to
SIG_CODE_BOUNDARY_FAULT.

Tested on x86_64-linux.
2023-06-05 12:53:15 +02:00
Andrew Burgess
a6e5abae4e gdb: move displaced_step_dump_bytes into gdbsupport (and rename)
It was pointed out during review of another patch that the function
displaced_step_dump_bytes really isn't specific to displaced stepping,
and should really get a more generic name and move into gdbsupport/.

This commit does just that.  The function is renamed to
bytes_to_string and is moved into gdbsupport/common-utils.{cc,h}.  The
function implementation doesn't really change. Much...

... I have updated the function to take an array view, which makes it
slightly easier to call in a couple of places where we already have a
gdb::bytes_vector.  I've then added an inline wrapper to convert a raw
pointer and length into an array view, which is used in places where
we don't easily have a gdb::bytes_vector (or similar).

Updated all users of displaced_step_dump_bytes.

There should be no user visible changes after this commit.

Finally, I ended up having to add an include of gdb_assert.h into
array-view.h.  When I include array-view.h into common-utils.h I ran
into build problems because array-view.h calls gdb_assert.

Approved-By: Simon Marchi <simon.marchi@efficios.com>
2023-03-29 08:57:10 +01:00
Joel Brobecker
213516ef31 Update copyright year range in header of all files managed by GDB
This commit is the result of running the gdb/copyright.py script,
which automated the update of the copyright year range for all
source files managed by the GDB project to be updated to include
year 2023.
2023-01-01 17:01:16 +04:00
Joel Brobecker
4a94e36819 Automatic Copyright Year update after running gdb/copyright.py
This commit brings all the changes made by running gdb/copyright.py
as per GDB's Start of New Year Procedure.

For the avoidance of doubt, all changes in this commits were
performed by the script.
2022-01-01 19:13:23 +04:00
Simon Marchi
4bce7cdaf4 gdbsupport: add array_view copy function
An assertion was recently added to array_view::operator[] to ensure we
don't do out of bounds accesses.  However, when the array_view is copied
to or from using memcpy, it bypasses that safety.

To address this, add a `copy` free function that copies data from an
array view to another, ensuring that the destination and source array
views have the same size.  When copying to or from parts of an
array_view, we are expected to use gdb::array_view::slice, which does
its own bounds check.  With all that, any copy operation that goes out
of bounds should be caught by an assertion at runtime.

copy is implemented using std::copy and std::copy_backward, which, at
least on libstdc++, appears to pick memmove when copying trivial data.
So in the end there shouldn't be much difference vs using a bare memcpy,
as we do right now.  When copying non-trivial data, std::copy and
std::copy_backward assigns each element in a loop.

To properly support overlapping ranges, we must use std::copy or
std::copy_backward, depending on whether the destination is before the
source or vice-versa.  std::copy and std::copy_backward don't support
copying exactly overlapping ranges (where the source range is equal to
the destination range).  But in this case, no copy is needed anyway, so
we do nothing.

The order of parameters of the new copy function is based on std::copy
and std::copy_backward, where the source comes before the destination.

Change a few randomly selected spots to use the new function, to show
how it can be used.

Add a test for the new function, testing both with arrays of a trivial
type (int) and of a non-trivial type (foo).  Test non-overlapping
ranges as well as three kinds of overlapping ranges: source before dest,
dest before source, and dest == source.

Change-Id: Ibeaca04e0028410fd44ce82f72e60058d6230a03
2021-12-03 16:37:36 -05:00
Simon Marchi
911438f9f4 gdbsupport: fix array-view compilation with c++11 && _GLIBCXX_DEBUG
When building with -std=c++11 and -D_GLIBCXX_DEBUG=1, we get some errors
like:

      CXX    unittests/array-view-selftests.o
    In file included from /home/smarchi/src/binutils-gdb/gdb/utils.h:25,
                     from /home/smarchi/src/binutils-gdb/gdb/defs.h:630,
                     from /home/smarchi/src/binutils-gdb/gdb/unittests/array-view-selftests.c:20:
    /home/smarchi/src/binutils-gdb/gdb/../gdbsupport/array-view.h: In instantiation of constexpr gdb::array_view<T> gdb::array_view<T>::slice(gdb::array_view<T>::size_type, gdb::array_view<T>::size_type) const [with T = unsigned char; gdb::array_view<T>::size_type = long unsigned int:
    /home/smarchi/src/binutils-gdb/gdb/unittests/array-view-selftests.c:532:29:   required from here
    /home/smarchi/src/binutils-gdb/gdb/../gdbsupport/array-view.h:192:3: error: body of constexpr function constexpr gdb::array_view<T> gdb::array_view<T>::slice(gdb::array_view<T>::size_type, gdb::array_view<T>::size_type) const [with T = unsigned char; gdb::array_view<T>::size_type = long unsigned int not a return-statement
      192 |   }
          |   ^

This is because constexpr functions in c++11 can only consist of a
single return statement, so we can't have the gdb_assert in there.  Make
the gdb_assert presence conditional to the __cplusplus version, to
enable it only for c++14 and later.

Change-Id: I2ac33f7b4bd1765ddc3ac8d07445b16ac1f340f0
2021-11-20 07:36:47 -05:00
Lancelot SIX
e92f2b5eef Improve gdb::array_view ctor from contiguous containers
While reading the interface of gdb::array_view, I realized that the
constructor that builds an array_view on top of a contiguous container
(such as std::vector, std::array or even gdb::array_view) can be
missused.

Lets consider the following code sample:

	struct Parent
	{
	  Parent (int a): a { a } {}
	  int a;
	};

	std::ostream &operator<< (std::ostream& os, const Parent & p)
	{ os << "Parent {a=" << p.a << "}"; return os; }

	struct Child : public Parent
	{
	  Child (int a, int b): Parent { a }, b { b } {}
	  int b;
	};

	std::ostream &operator<< (std::ostream& os, const Child & p)
	{ os << "Child {a=" << p.a << ", b=" << p.b << "}"; return os; }

	template <typename T>
	void print (const gdb::array_view<const T> &p)
	{
	  std::for_each (p.begin (), p.end (), [](const T &p) { std::cout << p << '\n'; });
	}

Then with the current interface nothinng prevents this usage of
array_view to be done:

	const std::array<Child, 3> elts = {
	  Child {1, 2},
	  Child {3, 4},
	  Child {5, 6}
	};
	print_all<Parent> (elts);

This compiles fine and produces the following output:

	Parent {a=1}
	Parent {a=2}
	Parent {a=3}

which is obviously wrong.  There is nowhere in memory a Parent-like
object for which the A member is 2 and this call to print_all<Parent>
shold not compile at all (calling print_all<Child> is however fine).

This comes down to the fact that a Child* is convertible into a Parent*,
and that an array view is constructed to a pointer to the first element
and a size.  The valid type pointed to that can be used with this
constructor are restricted using SFINAE, which requires that a
pointer to a member into the underlying container can be converted into a
pointer the array_view's data type.

This patch proposes to change the constraints on the gdb::array_view
ctor which accepts a container now requires that the (decayed) type of
the elements in the container match the (decayed) type of the array_view
being constructed.

Applying this change required minimum adjustment in GDB codebase, which
are also included in this patch.

Tested by rebuilding.
2021-11-08 21:55:36 +00:00
Simon Marchi
d9f82e9313 gdbsupport: add assertions in array_view
Add assertions to ensure we don't access an array_view out of bounds.
Enable these assertions only when _GLIBCXX_DEBUG is set, as we did for
gdb::optional.

Change-Id: Iffaee38252405073735ed123c8e57fde6b2c6be3
2021-10-25 14:51:44 -04:00
Joel Brobecker
3666a04883 Update copyright year range in all GDB files
This commits the result of running gdb/copyright.py as per our Start
of New Year procedure...

gdb/ChangeLog

        Update copyright year range in copyright header of all GDB files.
2021-01-01 12:12:21 +04:00
Tom Tromey
01027315f5 Move gdbsupport to the top level
This patch moves the gdbsupport directory to the top level.  This is
the next step in the ongoing project to move gdbserver to the top
level.

The bulk of this patch was created by "git mv gdb/gdbsupport gdbsupport".

This patch then adds a build system to gdbsupport and wires it into
the top level.  Then it changes gdb to use the top-level build.

gdbserver, on the other hand, is not yet changed.  It still does its
own build of gdbsupport.

ChangeLog
2020-01-14  Tom Tromey  <tom@tromey.com>

	* src-release.sh (GDB_SUPPORT_DIRS): Add gdbsupport.
	* MAINTAINERS: Add gdbsupport.
	* configure: Rebuild.
	* configure.ac (configdirs): Add gdbsupport.
	* gdbsupport: New directory, move from gdb/gdbsupport.
	* Makefile.def (host_modules, dependencies): Add gnulib.
	* Makefile.in: Rebuild.

gdb/ChangeLog
2020-01-14  Tom Tromey  <tom@tromey.com>

	* nat/x86-linux-dregs.c: Include configh.h.
	* nat/linux-ptrace.c: Include configh.h.
	* nat/linux-btrace.c: Include configh.h.
	* defs.h: Include config.h, bfd.h.
	* configure.ac: Don't source common.host.
	(CONFIG_OBS, CONFIG_SRCS): Remove gdbsupport files.
	* configure: Rebuild.
	* acinclude.m4: Update path.
	* Makefile.in (SUPPORT, LIBSUPPORT, INCSUPPORT): New variables.
	(CONFIG_SRC_SUBDIR): Remove gdbsupport.
	(INTERNAL_CFLAGS_BASE): Add INCSUPPORT.
	(CLIBS): Add LIBSUPPORT.
	(CDEPS): Likewise.
	(COMMON_SFILES): Remove gdbsupport files.
	(HFILES_NO_SRCDIR): Likewise.
	(stamp-version): Update path to create-version.sh.
	(ALLDEPFILES): Remove gdbsupport files.

gdb/gdbserver/ChangeLog
2020-01-14  Tom Tromey  <tom@tromey.com>

	* server.h: Include config.h.
	* gdbreplay.c: Include config.h.
	* configure: Rebuild.
	* configure.ac: Don't source common.host.
	* acinclude.m4: Update path.
	* Makefile.in (INCSUPPORT): New variable.
	(INCLUDE_CFLAGS): Add INCSUPPORT.
	(SFILES): Update paths.
	(version-generated.c): Update path to create-version.sh.
	(gdbsupport/%-ipa.o, gdbsupport/%.o): Update paths.

gdbsupport/ChangeLog
2020-01-14  Tom Tromey  <tom@tromey.com>

	* common-defs.h: Add GDBSERVER case.  Update includes.
	* acinclude.m4, aclocal.m4, config.in, configure, configure.ac,
	Makefile.am, Makefile.in, README: New files.
	* Moved from ../gdb/gdbsupport/

Change-Id: I07632e7798635c1bab389bf885971e584fb4bb78
2020-01-14 16:25:02 -07:00