Commit Graph

21 Commits

Author SHA1 Message Date
Hannes Domani
d177315254 Fix right shift of negative numbers
PR31590 shows that right shift of negative numbers doesn't work
correctly since GDB 14:

(gdb) p (-3) >> 1
$1 = -1

GDB 13 and earlier returned the correct value -2.
And there actually is one test that shows the failure:

print -1 >> 1
$84 = 0
(gdb) FAIL: gdb.base/bitshift.exp: lang=asm: rsh neg lhs: print -1 >> 1

The problem was introduced with the change to gmp functions in
commit 303a881f87.
It's wrong because gdb_mpz::operator>> uses mpz_tdif_q_2exp, which
always rounds toward zero, and the gmp docu says this:

For positive n both mpz_fdiv_q_2exp and mpz_tdiv_q_2exp are simple
bitwise right shifts.
For negative n, mpz_fdiv_q_2exp is effectively an arithmetic right shift
treating n as two's complement the same as the bitwise logical functions
do, whereas mpz_tdiv_q_2exp effectively treats n as sign and magnitude.

So this changes mpz_tdiv_q_2exp to mpz_fdiv_q_2exp, since it
does right shifts for both positive and negative numbers.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31590
Approved-By: Tom Tromey <tom@tromey.com>
2024-06-11 20:36:34 +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
44671f3f7f Allow cast of 128-bit integer to pointer
PR rust/31082 points out that casting a 128-bit integer to a pointer
will fail.  This happens because a case in value_cast was not
converted to use GMP.

This patch fixes the problem.  I am not really sure that testing
against the negative value here makes sense, but I opted to just
preserve the existing behavior rather than change it.

Regression tested on x86-64 Fedora 38.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31082
2023-12-08 08:17:40 -07:00
Tom Tromey
3ad6c165ca Rewrite gdb_mpz::operator==
Simon pointed out that the recent changes to gdb_mpz caused a build
failure on amd64 macOS.  It turns out to be somewhat difficult to
overload a method in a way that will work "naturally" for all integer
types; especially in a case like gdb_mpz::operator==, where it's
desirable to special case all integer types that are no wider than
'long'.

After a false start, I came up with this patch, which seems to work.
It applies the desirable GMP special cases directly in the body,
rather than via overloads.

Approved-By: Simon Marchi <simon.marchi@efficios.com>
2023-04-26 08:24:15 -06:00
Tom Tromey
767c4b92bc Additions to gdb_mpz
In preparation for adding more 128-bit support to gdb, a few additions
to gdb_mpz are needed.

First, this adds a new 'as_integer_truncate' method.  This method
works like 'as_integer' but does not require the value to fit in the
target type -- it just truncates.

Second, gdb_mpz::export_bits is changed to handle the somewhat unusual
situation of zero-length types.  This can happen for a Rust '()' type;
but I think other languages have zero-bit integer types as well.

Finally, this adds some operator== overloads.
2023-04-17 10:43:06 -06:00
Tom Tromey
1c805ba063 Add truncation mode to gdb_mpz
This renames gdb_mpz::safe_export to export_bits, and adds a new flag
to export a truncated value.  This is needed by value arithmetic.
2023-03-27 08:20:29 -06:00
Tom Tromey
c225d6b804 Add many operators to gdb_mpz
This adds many operator overloads and other useful methods to gdb_mpz.
This is preparation for using this class for scalar arithmetic in gdb
expression evaluation.
2023-03-27 08:20:28 -06:00
Tom Tromey
d0aa28e155 Hide the implementation of gdb_mpf
This renames the data member of gdb_mpf and makes it private.  It also
adds a single new method to aid in this change.  Unlike the earlier
changes here, I did this one all together because gdb_mpf has very few
uses.
2023-03-14 08:16:39 -06:00
Tom Tromey
8176838655 Rename gdb_mpq::val and make contents private
This changes gdb_mpq to hide its data, and renames the data member
from 'val' to 'm_val', following gdb convention.
2023-03-14 08:16:39 -06:00
Tom Tromey
7607de9431 Add operators and methods to gdb_mpq
This adds some operators and methods to gdb_mpq, in preparation for
making its implementation private.

This only adds the operators currently needed by gdb.  More could be
added as necessary.
2023-03-14 08:16:39 -06:00
Tom Tromey
7aeae94f88 Rename gdb_mpz::val and make contents private
This changes gdb_mpz to hide its data, and renames the data member
from 'val' to 'm_val', following gdb convention.
2023-03-14 08:16:39 -06:00
Tom Tromey
302273ca84 Add methods and operators to gdb_mpz
This adds various methods and operators to gdb_mpz, as a step toward
hiding the implementation.

This only adds the operators that were needed.  Many more could be
added as required.
2023-03-14 08:16:39 -06:00
Tom Tromey
c8a67010d1 Clean up gmp-utils.h includes
gmp-utils.h includes "defs.h", but normally the rule in gdb is that
the .c files include this first.  This patch changes this code to
match the rest of gdb.
2023-03-14 08:16:39 -06:00
Simon Marchi
287de65625 gdb, gdbserver, gdbsupport: fix whitespace issues
Replace spaces with tabs in a bunch of places.

Change-Id: If0f87180f1d13028dc178e5a8af7882a067868b0
2023-03-09 16:32:00 -05: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
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
Joel Brobecker
63c457b911 gmp-utils: protect gdb_mpz exports against out-of-range values
The gdb_mpz class currently provides a couple of methods which
essentially export an mpz_t value into either a buffer, or an integral
type. The export is based on using the mpz_export function which
we discovered can be a bit treacherous if used without caution.

In particular, the initial motivation for this patch was to catch
situations where the mpz_t value was so large that it would not fit
in the destination area. mpz_export does not know the size of
the buffer, and therefore can happily write past the end of our buffer.

While designing a solution to the above problem, I also discovered
that we also needed to be careful when exporting signed numbers.
In particular, numbers which are larger than the maximum value
for a given signed type size, but no so large as to fit in the
*unsigned* version with the same size, would end up being exported
incorrectly. This is related to the fact that mpz_export ignores
the sign of the value being exportd, and assumes an unsigned export.
Thus, for such large values, the appears as if mpz_export is able
to fit our value into our buffer, but in fact, it does not.

Also, I noticed that gdb_mpz::write wasn't taking its unsigned_p
parameter, which was a hole.

For all these reasons, a new low-level private method called
"safe_export" has been added to class gdb_mpz, whose goal is
to perform all necessary checks and manipulations for a safe
and correct export. As a bonus, this method allows us to factorize
the handling of negative value exports.

The gdb_mpz::as_integer and gdb_mpz::write methods are then simplified
to take advantage of this new safe_export method.

gdb/ChangeLog:

        * gmp-utils.h (gdb_mpz::safe_export): New private method.
        (gdb_mpz::as_integer): Reimplement using gdb_mpz::safe_export.
        * gmp-utils.c (gdb_mpz::write): Rewrite using gdb_mpz::safe_export.
        (gdb_mpz::safe_export): New method.
        * unittests/gmp-utils-selftests .c (gdb_mpz_as_integer):
        Update function description.
        (check_as_integer_raises_out_of_range_error): New function.
        (gdb_mpz_as_integer_out_of_range): New function.
        (_initialize_gmp_utils_selftests): Register
        gdb_mpz_as_integer_out_of_range as a selftest.
2020-12-05 23:56:59 -05:00
Joel Brobecker
c9f0b43fe4 gmp-utils: Convert the read/write methods to using gdb::array_view
This commit changes the interfaces of some of the methods declared
in gmp-utils to take a gdb::array_view of gdb_byte instead of a
(gdb_byte *, size) couple.

This makes these methods' API probably more C++-idiomatic.

        * gmp-utils.h (gdb_mpz::read): Change buf and len parameters
        into one single gdb::array_view parameter.
        (gdb_mpz::write): Likewise.
        (gdb_mpq::read_fixed_point, gdb_mpq::write_fixed_point): Likewise.
        * gmp-utils.c (gdb_mpz::read): Change buf and len parameters
        into one single gdb::array_view parameter.
        Adjust implementation accordingly.
        (gdb_mpz::write): Likewise.
        (gdb_mpq::read_fixed_point, gdb_mpq::write_fixed_point): Likewise.
        * unittests/gmp-utils-selftests.c: Adapt following changes above.
        * valarith.c, valops.c, valprint.c, value.c: Likewise.
2020-11-23 21:46:38 -05:00
Joel Brobecker
987b670356 change and rename gmp_string_asprintf to return an std::string
This was suggested by Simon during a code review of this package upstream.
The upside is that this makes the function's API more natural and C++.
The downside is an extra malloc, which might be the reason why we went
for using a unique_xmalloc_ptr in the first place. Since this function
is not expected to be called frequently, the API improvement might be
worth the performance impact.

gdb/ChangeLog:

        * gmp-utils.h (gmp_string_printf): Rename from gmp_string_asprintf.
        Change return type to std::string. Update all callers.
        * gmp-utils.c (gmp_string_printf): Likewise.
2020-11-23 21:45:35 -05:00
Joel Brobecker
b34c74ab9a gmp-utils: New API to simply use of GMP's integer/rational/float objects
This API was motivated by a number of reasons:
  - GMP's API does not handle "long long" and "unsigned long long",
    so using LONGEST and ULONGEST is not straightforward;
  - Automate the need to initialize GMP objects before use, and
    clear them when no longer used.

However, this API grew also to help with similar matter such
as formatting to a string, and also reading/writing fixed-point
values from byte buffers.

Dedicated unit testing is also added.

gdb/ChangeLog:

        * gmp-utils.h,  gmp-utils.h: New file.
        * unittests/gmp-utils-selftests.c: New file.
        * Makefile.in (SUBDIR_UNITTESTS_SRCS): Add
        unittests/gmp-utils-selftests.c.
        (COMMON_SFILES) Add gmp-utils.c.
        (HFILES_NO_SRCDIR): Add gmp-utils.h.
2020-11-15 03:09:44 -05:00