xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/gdbarch-selftests.c (revision 4d342c046e3288fb5a1edcd33cfec48c41c80664)
1 /* Self tests for gdbarch for GDB, the GNU debugger.
2 
3    Copyright (C) 2017-2019 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 #if GDB_SELF_TEST
22 #include "common/selftest.h"
23 #include "selftest-arch.h"
24 #include "inferior.h"
25 #include "gdbthread.h"
26 #include "target.h"
27 #include "test-target.h"
28 #include "target-float.h"
29 #include "common/def-vector.h"
30 
31 namespace selftests {
32 
33 /* Test gdbarch methods register_to_value and value_to_register.  */
34 
35 static void
36 register_to_value_test (struct gdbarch *gdbarch)
37 {
38   const struct builtin_type *builtin = builtin_type (gdbarch);
39   struct type *types[] =
40     {
41       builtin->builtin_void,
42       builtin->builtin_char,
43       builtin->builtin_short,
44       builtin->builtin_int,
45       builtin->builtin_long,
46       builtin->builtin_signed_char,
47       builtin->builtin_unsigned_short,
48       builtin->builtin_unsigned_int,
49       builtin->builtin_unsigned_long,
50       builtin->builtin_float,
51       builtin->builtin_double,
52       builtin->builtin_long_double,
53       builtin->builtin_complex,
54       builtin->builtin_double_complex,
55       builtin->builtin_string,
56       builtin->builtin_bool,
57       builtin->builtin_long_long,
58       builtin->builtin_unsigned_long_long,
59       builtin->builtin_int8,
60       builtin->builtin_uint8,
61       builtin->builtin_int16,
62       builtin->builtin_uint16,
63       builtin->builtin_int32,
64       builtin->builtin_uint32,
65       builtin->builtin_int64,
66       builtin->builtin_uint64,
67       builtin->builtin_int128,
68       builtin->builtin_uint128,
69       builtin->builtin_char16,
70       builtin->builtin_char32,
71     };
72 
73   /* Error out if debugging something, because we're going to push the
74      test target, which would pop any existing target.  */
75   if (current_top_target ()->stratum () >= process_stratum)
76    error (_("target already pushed"));
77 
78   /* Create a mock environment.  An inferior with a thread, with a
79      process_stratum target pushed.  */
80 
81   test_target_ops mock_target;
82   ptid_t mock_ptid (1, 1);
83   inferior mock_inferior (mock_ptid.pid ());
84   address_space mock_aspace {};
85   mock_inferior.gdbarch = gdbarch;
86   mock_inferior.aspace = &mock_aspace;
87   thread_info mock_thread (&mock_inferior, mock_ptid);
88 
89   scoped_restore restore_thread_list
90     = make_scoped_restore (&mock_inferior.thread_list, &mock_thread);
91 
92   /* Add the mock inferior to the inferior list so that look ups by
93      target+ptid can find it.  */
94   scoped_restore restore_inferior_list
95     = make_scoped_restore (&inferior_list);
96   inferior_list = &mock_inferior;
97 
98   /* Switch to the mock inferior.  */
99   scoped_restore_current_inferior restore_current_inferior;
100   set_current_inferior (&mock_inferior);
101 
102   /* Push the process_stratum target so we can mock accessing
103      registers.  */
104   push_target (&mock_target);
105 
106   /* Pop it again on exit (return/exception).  */
107   SCOPE_EXIT { pop_all_targets_at_and_above (process_stratum); };
108 
109   /* Switch to the mock thread.  */
110   scoped_restore restore_inferior_ptid
111     = make_scoped_restore (&inferior_ptid, mock_ptid);
112 
113   struct frame_info *frame = get_current_frame ();
114   const int num_regs = gdbarch_num_cooked_regs (gdbarch);
115 
116   /* Test gdbarch methods register_to_value and value_to_register with
117      different combinations of register numbers and types.  */
118   for (const auto &type : types)
119     {
120       for (auto regnum = 0; regnum < num_regs; regnum++)
121 	{
122 	  if (gdbarch_convert_register_p (gdbarch, regnum, type))
123 	    {
124 	      std::vector<gdb_byte> expected (TYPE_LENGTH (type), 0);
125 
126 	      if (TYPE_CODE (type) == TYPE_CODE_FLT)
127 		{
128 		  /* Generate valid float format.  */
129 		  target_float_from_string (expected.data (), type, "1.25");
130 		}
131 	      else
132 		{
133 		  for (auto j = 0; j < expected.size (); j++)
134 		    expected[j] = (regnum + j) % 16;
135 		}
136 
137 	      gdbarch_value_to_register (gdbarch, frame, regnum, type,
138 					 expected.data ());
139 
140 	      /* Allocate two bytes more for overflow check.  */
141 	      std::vector<gdb_byte> buf (TYPE_LENGTH (type) + 2, 0);
142 	      int optim, unavail, ok;
143 
144 	      /* Set the fingerprint in the last two bytes.  */
145 	      buf [TYPE_LENGTH (type)]= 'w';
146 	      buf [TYPE_LENGTH (type) + 1]= 'l';
147 	      ok = gdbarch_register_to_value (gdbarch, frame, regnum, type,
148 					      buf.data (), &optim, &unavail);
149 
150 	      SELF_CHECK (ok);
151 	      SELF_CHECK (!optim);
152 	      SELF_CHECK (!unavail);
153 
154 	      SELF_CHECK (buf[TYPE_LENGTH (type)] == 'w');
155 	      SELF_CHECK (buf[TYPE_LENGTH (type) + 1] == 'l');
156 
157 	      for (auto k = 0; k < TYPE_LENGTH(type); k++)
158 		SELF_CHECK (buf[k] == expected[k]);
159 	    }
160 	}
161     }
162 }
163 
164 } // namespace selftests
165 #endif /* GDB_SELF_TEST */
166 
167 void
168 _initialize_gdbarch_selftests (void)
169 {
170 #if GDB_SELF_TEST
171   selftests::register_test_foreach_arch ("register_to_value",
172 					 selftests::register_to_value_test);
173 #endif
174 }
175