gdbarch-selftests.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* Self tests for gdbarch for GDB, the GNU debugger.
  2. Copyright (C) 2017-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "gdbsupport/selftest.h"
  16. #include "selftest-arch.h"
  17. #include "target.h"
  18. #include "test-target.h"
  19. #include "target-float.h"
  20. #include "gdbsupport/def-vector.h"
  21. #include "gdbarch.h"
  22. #include "scoped-mock-context.h"
  23. namespace selftests {
  24. /* Test gdbarch methods register_to_value and value_to_register. */
  25. static void
  26. register_to_value_test (struct gdbarch *gdbarch)
  27. {
  28. const struct builtin_type *builtin = builtin_type (gdbarch);
  29. struct type *types[] =
  30. {
  31. builtin->builtin_void,
  32. builtin->builtin_char,
  33. builtin->builtin_short,
  34. builtin->builtin_int,
  35. builtin->builtin_long,
  36. builtin->builtin_signed_char,
  37. builtin->builtin_unsigned_short,
  38. builtin->builtin_unsigned_int,
  39. builtin->builtin_unsigned_long,
  40. builtin->builtin_float,
  41. builtin->builtin_double,
  42. builtin->builtin_long_double,
  43. builtin->builtin_complex,
  44. builtin->builtin_double_complex,
  45. builtin->builtin_string,
  46. builtin->builtin_bool,
  47. builtin->builtin_long_long,
  48. builtin->builtin_unsigned_long_long,
  49. builtin->builtin_int8,
  50. builtin->builtin_uint8,
  51. builtin->builtin_int16,
  52. builtin->builtin_uint16,
  53. builtin->builtin_int32,
  54. builtin->builtin_uint32,
  55. builtin->builtin_int64,
  56. builtin->builtin_uint64,
  57. builtin->builtin_int128,
  58. builtin->builtin_uint128,
  59. builtin->builtin_char16,
  60. builtin->builtin_char32,
  61. };
  62. scoped_mock_context<test_target_ops> mockctx (gdbarch);
  63. struct frame_info *frame = get_current_frame ();
  64. const int num_regs = gdbarch_num_cooked_regs (gdbarch);
  65. /* Test gdbarch methods register_to_value and value_to_register with
  66. different combinations of register numbers and types. */
  67. for (const auto &type : types)
  68. {
  69. for (auto regnum = 0; regnum < num_regs; regnum++)
  70. {
  71. if (gdbarch_convert_register_p (gdbarch, regnum, type))
  72. {
  73. std::vector<gdb_byte> expected (TYPE_LENGTH (type), 0);
  74. if (type->code () == TYPE_CODE_FLT)
  75. {
  76. /* Generate valid float format. */
  77. target_float_from_string (expected.data (), type, "1.25");
  78. }
  79. else
  80. {
  81. for (auto j = 0; j < expected.size (); j++)
  82. expected[j] = (regnum + j) % 16;
  83. }
  84. gdbarch_value_to_register (gdbarch, frame, regnum, type,
  85. expected.data ());
  86. /* Allocate two bytes more for overflow check. */
  87. std::vector<gdb_byte> buf (TYPE_LENGTH (type) + 2, 0);
  88. int optim, unavail, ok;
  89. /* Set the fingerprint in the last two bytes. */
  90. buf [TYPE_LENGTH (type)]= 'w';
  91. buf [TYPE_LENGTH (type) + 1]= 'l';
  92. ok = gdbarch_register_to_value (gdbarch, frame, regnum, type,
  93. buf.data (), &optim, &unavail);
  94. SELF_CHECK (ok);
  95. SELF_CHECK (!optim);
  96. SELF_CHECK (!unavail);
  97. SELF_CHECK (buf[TYPE_LENGTH (type)] == 'w');
  98. SELF_CHECK (buf[TYPE_LENGTH (type) + 1] == 'l');
  99. for (auto k = 0; k < TYPE_LENGTH(type); k++)
  100. SELF_CHECK (buf[k] == expected[k]);
  101. }
  102. }
  103. }
  104. }
  105. } // namespace selftests
  106. void _initialize_gdbarch_selftests ();
  107. void
  108. _initialize_gdbarch_selftests ()
  109. {
  110. selftests::register_test_foreach_arch ("register_to_value",
  111. selftests::register_to_value_test);
  112. }