]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/gdbserver/tdesc.c
Commonise tdesc_reg and makes use of it in gdbserver tdesc
[thirdparty/binutils-gdb.git] / gdb / gdbserver / tdesc.c
1 /* Copyright (C) 2012-2018 Free Software Foundation, Inc.
2
3 This file is part of GDB.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17
18 #include "server.h"
19 #include "tdesc.h"
20 #include "regdef.h"
21
22 #ifndef IN_PROCESS_AGENT
23
24 target_desc::~target_desc ()
25 {
26 int i;
27
28 xfree ((char *) arch);
29 xfree ((char *) osabi);
30 }
31
32 bool target_desc::operator== (const target_desc &other) const
33 {
34 if (reg_defs != other.reg_defs)
35 return false;
36
37 /* Compare expedite_regs. */
38 int i = 0;
39 for (; expedite_regs[i] != NULL; i++)
40 {
41 if (strcmp (expedite_regs[i], other.expedite_regs[i]) != 0)
42 return false;
43 }
44 if (other.expedite_regs[i] != NULL)
45 return false;
46
47 return true;
48 }
49
50 #endif
51
52 void
53 init_target_desc (struct target_desc *tdesc)
54 {
55 int offset = 0;
56
57 /* Go through all the features and populate reg_defs. */
58 for (const tdesc_reg_up &treg : tdesc->registers)
59 {
60 int regnum = treg->target_regnum;
61
62 /* Register number will increase (possibly with gaps) or be zero. */
63 gdb_assert (regnum == 0 || regnum >= tdesc->reg_defs.size ());
64
65 if (regnum != 0)
66 tdesc->reg_defs.resize (regnum, reg (offset));
67
68 tdesc->reg_defs.emplace_back (treg->name.c_str (), offset, treg->bitsize);
69 offset += treg->bitsize;
70 }
71
72 tdesc->registers_size = offset / 8;
73
74 /* Make sure PBUFSIZ is large enough to hold a full register
75 packet. */
76 gdb_assert (2 * tdesc->registers_size + 32 <= PBUFSIZ);
77 }
78
79 struct target_desc *
80 allocate_target_description (void)
81 {
82 return new target_desc ();
83 }
84
85 #ifndef IN_PROCESS_AGENT
86
87 static const struct target_desc default_description {};
88
89 void
90 copy_target_description (struct target_desc *dest,
91 const struct target_desc *src)
92 {
93 dest->reg_defs = src->reg_defs;
94 dest->expedite_regs = src->expedite_regs;
95 dest->registers_size = src->registers_size;
96 dest->xmltarget = src->xmltarget;
97 }
98
99 const struct target_desc *
100 current_target_desc (void)
101 {
102 if (current_thread == NULL)
103 return &default_description;
104
105 return current_process ()->tdesc;
106 }
107
108 /* See common/tdesc.h. */
109
110 void
111 set_tdesc_architecture (struct target_desc *target_desc,
112 const char *name)
113 {
114 target_desc->arch = xstrdup (name);
115 }
116
117 /* See common/tdesc.h. */
118
119 void
120 set_tdesc_osabi (struct target_desc *target_desc, const char *name)
121 {
122 target_desc->osabi = xstrdup (name);
123 }
124
125 /* Return a string which is of XML format, including XML target
126 description to be sent to GDB. */
127
128 const char *
129 tdesc_get_features_xml (target_desc *tdesc)
130 {
131 /* Either .xmltarget or .features is not NULL. */
132 gdb_assert (tdesc->xmltarget != NULL
133 || (!tdesc->features.empty ()
134 && tdesc->arch != NULL));
135
136 if (tdesc->xmltarget == NULL)
137 {
138 std::string buffer ("@<?xml version=\"1.0\"?>");
139
140 buffer += "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">";
141 buffer += "<target>";
142 buffer += "<architecture>";
143 buffer += tdesc->arch;
144 buffer += "</architecture>";
145
146 if (tdesc->osabi != nullptr)
147 {
148 buffer += "<osabi>";
149 buffer += tdesc->osabi;
150 buffer += "</osabi>";
151 }
152
153
154 for (const std::string &xml : tdesc->features)
155 {
156 buffer += "<xi:include href=\"";
157 buffer += xml;
158 buffer += "\"/>";
159 }
160
161 buffer += "</target>";
162
163 tdesc->xmltarget = xstrdup (buffer.c_str ());
164 }
165
166 return tdesc->xmltarget;
167 }
168 #endif
169
170 struct tdesc_type
171 {};
172
173 /* See common/tdesc.h. */
174
175 struct tdesc_feature *
176 tdesc_create_feature (struct target_desc *tdesc, const char *name,
177 const char *xml)
178 {
179 #ifndef IN_PROCESS_AGENT
180 tdesc->features.emplace_back (xml);
181 #endif
182 return tdesc;
183 }
184
185 /* See common/tdesc.h. */
186
187 tdesc_type_with_fields *
188 tdesc_create_flags (struct tdesc_feature *feature, const char *name,
189 int size)
190 {
191 return NULL;
192 }
193
194 /* See common/tdesc.h. */
195
196 void
197 tdesc_add_flag (tdesc_type_with_fields *type, int start,
198 const char *flag_name)
199 {}
200
201 /* See common/tdesc.h. */
202
203 struct tdesc_type *
204 tdesc_named_type (const struct tdesc_feature *feature, const char *id)
205 {
206 return NULL;
207 }
208
209 /* See common/tdesc.h. */
210
211 tdesc_type_with_fields *
212 tdesc_create_union (struct tdesc_feature *feature, const char *id)
213 {
214 return NULL;
215 }
216
217 /* See common/tdesc.h. */
218
219 tdesc_type_with_fields *
220 tdesc_create_struct (struct tdesc_feature *feature, const char *id)
221 {
222 return NULL;
223 }
224
225 /* See common/tdesc.h. */
226
227 void
228 tdesc_create_reg (struct tdesc_feature *feature, const char *name,
229 int regnum, int save_restore, const char *group,
230 int bitsize, const char *type)
231 {
232 struct target_desc *tdesc = (struct target_desc *) feature;
233
234 tdesc_reg *reg = new tdesc_reg (feature, name, regnum, save_restore,
235 group, bitsize, type);
236
237 tdesc->registers.emplace_back (reg);
238 }
239
240 /* See common/tdesc.h. */
241
242 struct tdesc_type *
243 tdesc_create_vector (struct tdesc_feature *feature, const char *name,
244 struct tdesc_type *field_type, int count)
245 {
246 return NULL;
247 }
248
249 void
250 tdesc_add_bitfield (tdesc_type_with_fields *type, const char *field_name,
251 int start, int end)
252 {}
253
254 /* See common/tdesc.h. */
255
256 void
257 tdesc_add_field (tdesc_type_with_fields *type, const char *field_name,
258 struct tdesc_type *field_type)
259 {}
260
261 /* See common/tdesc.h. */
262
263 void
264 tdesc_set_struct_size (tdesc_type_with_fields *type, int size)
265 {
266 }