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