diff options
author | Andrew Burgess <andrew.burgess@embecosm.com> | 2019-03-14 13:58:58 +0000 |
---|---|---|
committer | Andrew Burgess <andrew.burgess@embecosm.com> | 2019-04-01 21:41:49 +0100 |
commit | 8bdc16587e26100282094c8eaa8e83180ba57afd (patch) | |
tree | 5e18b77acd219a53d4ac482fd56e060d05927964 /gdb/value.c | |
parent | c29705b71a8ec966478c0dc4712194a95291c6de (diff) | |
download | gdb-8bdc16587e26100282094c8eaa8e83180ba57afd.zip gdb-8bdc16587e26100282094c8eaa8e83180ba57afd.tar.gz gdb-8bdc16587e26100282094c8eaa8e83180ba57afd.tar.bz2 |
gdb: Add $_cimag and $_creal internal functions
Add two new internal functions $_cimag and $_creal that extract the
imaginary and real parts of a complex value.
These internal functions can take a complex value of any type 'float
complex', 'double complex', or 'long double complex' and return a
suitable floating point value 'float', 'double', or 'long double'.
So we can now do this:
(gdb) p z1
$1 = 1.5 + 4.5 * I
(gdb) p $_cimag (z1)
$4 = 4.5
(gdb) p $_creal (z1)
$4 = 1.5
The components of a complex value are not strictly named types in
DWARF, as the complex type is itself the base type. However, once we
are able to extract the components it makes sense to be able to ask
what the type of these components is and get a sensible answer back,
rather than the error we would currently get. Currently GDB says:
(gdb) ptype z1
type = complex double
(gdb) p $_cimag (z1)
$4 = 4.5
(gdb) ptype $
type = <invalid type code 9>
With the changes in dwarf2read.c, GDB now says:
(gdb) ptype z1
type = complex double
(gdb) p $_cimag (z1)
$4 = 4.5
(gdb) ptype $
type = double
Which seems to make more sense.
gdb/ChangeLog:
* NEWS: Mention new internal functions.
* dwarf2read.c (dwarf2_init_complex_target_type): New function.
(read_base_type): Use dwarf2_init_complex_target_type.
* value.c (creal_internal_fn): New function.
(cimag_internal_fn): New function.
(_initialize_values): Register new internal functions.
gdb/doc/ChangeLog:
* gdb.texinfo (Convenience Funs): Document '$_creal' and
'$_cimag'.
gdb/testsuite/ChangeLog:
* gdb.base/complex-parts.c: New file.
* gdb.base/complex-parts.exp: New file.
Diffstat (limited to 'gdb/value.c')
-rw-r--r-- | gdb/value.c | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/gdb/value.c b/gdb/value.c index bcfc084..c0f8a58 100644 --- a/gdb/value.c +++ b/gdb/value.c @@ -3933,6 +3933,44 @@ isvoid_internal_fn (struct gdbarch *gdbarch, return value_from_longest (builtin_type (gdbarch)->builtin_int, ret); } +/* Implementation of the convenience function $_cimag. Extracts the + real part from a complex number. */ + +static struct value * +creal_internal_fn (struct gdbarch *gdbarch, + const struct language_defn *language, + void *cookie, int argc, struct value **argv) +{ + if (argc != 1) + error (_("You must provide one argument for $_creal.")); + + value *cval = argv[0]; + type *ctype = check_typedef (value_type (cval)); + if (TYPE_CODE (ctype) != TYPE_CODE_COMPLEX) + error (_("expected a complex number")); + return value_from_component (cval, TYPE_TARGET_TYPE (ctype), 0); +} + +/* Implementation of the convenience function $_cimag. Extracts the + imaginary part from a complex number. */ + +static struct value * +cimag_internal_fn (struct gdbarch *gdbarch, + const struct language_defn *language, + void *cookie, int argc, + struct value **argv) +{ + if (argc != 1) + error (_("You must provide one argument for $_cimag.")); + + value *cval = argv[0]; + type *ctype = check_typedef (value_type (cval)); + if (TYPE_CODE (ctype) != TYPE_CODE_COMPLEX) + error (_("expected a complex number")); + return value_from_component (cval, TYPE_TARGET_TYPE (ctype), + TYPE_LENGTH (TYPE_TARGET_TYPE (ctype))); +} + #if GDB_SELF_TEST namespace selftests { @@ -4114,6 +4152,20 @@ Usage: $_isvoid (expression)\n\ Return 1 if the expression is void, zero otherwise."), isvoid_internal_fn, NULL); + add_internal_function ("_creal", _("\ +Extract the real part of a complex number.\n\ +Usage: $_creal (expression)\n\ +Return the real part of a complex number, the type depends on the\n\ +type of a complex number."), + creal_internal_fn, NULL); + + add_internal_function ("_cimag", _("\ +Extract the imaginary part of a complex number.\n\ +Usage: $_cimag (expression)\n\ +Return the imaginary part of a complex number, the type depends on the\n\ +type of a complex number."), + cimag_internal_fn, NULL); + add_setshow_zuinteger_unlimited_cmd ("max-value-size", class_support, &max_value_size, _("\ Set maximum sized value gdb will load from the inferior."), _("\ |