Automatic date update in version.in
[binutils-gdb.git] / gdbserver / tdesc.cc
blobda1287abbbed5933bf8ceddd4aa5d7a70ad4d473
1 /* Copyright (C) 2012-2024 Free Software Foundation, Inc.
3 This file is part of GDB.
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.
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.
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/>. */
18 #include "tdesc.h"
19 #include "regdef.h"
21 #ifndef IN_PROCESS_AGENT
23 bool target_desc::operator== (const target_desc &other) const
25 if (reg_defs != other.reg_defs)
26 return false;
28 /* Compare the two vectors of expedited registers. They will only match
29 if the following conditions are met:
31 - Both vectors have the same number of elements.
32 - Both vectors contain the same elements.
33 - The elements of both vectors appear in the same order. */
34 if (expedite_regs != other.expedite_regs)
35 return false;
37 return true;
40 #endif
42 void target_desc::accept (tdesc_element_visitor &v) const
44 #ifndef IN_PROCESS_AGENT
45 v.visit_pre (this);
47 for (const tdesc_feature_up &feature : features)
48 feature->accept (v);
50 v.visit_post (this);
51 #endif
54 void
55 init_target_desc (struct target_desc *tdesc,
56 const char **expedite_regs,
57 enum gdb_osabi osabi)
59 int offset = 0;
61 /* Go through all the features and populate reg_defs. */
62 for (const tdesc_feature_up &feature : tdesc->features)
63 for (const tdesc_reg_up &treg : feature->registers)
65 int regnum = treg->target_regnum;
67 /* Register number will increase (possibly with gaps) or be zero. */
68 gdb_assert (regnum == 0 || regnum >= tdesc->reg_defs.size ());
70 if (regnum != 0)
71 tdesc->reg_defs.resize (regnum, gdb::reg (offset));
73 tdesc->reg_defs.emplace_back (treg->name.c_str (), offset,
74 treg->bitsize);
75 offset += treg->bitsize;
78 tdesc->registers_size = offset / 8;
80 /* Make sure PBUFSIZ is large enough to hold a full register
81 packet. */
82 gdb_assert (2 * tdesc->registers_size + 32 <= PBUFSIZ);
84 #ifndef IN_PROCESS_AGENT
85 /* Drop the contents of the previous vector, if any. */
86 tdesc->expedite_regs.clear ();
88 /* Initialize the vector with new expedite registers contents. */
89 int expedite_count = 0;
90 while (expedite_regs[expedite_count] != nullptr)
91 tdesc->expedite_regs.push_back (expedite_regs[expedite_count++]);
93 set_tdesc_osabi (tdesc, osabi);
94 #endif
97 /* See gdbsupport/tdesc.h. */
99 target_desc_up
100 allocate_target_description (void)
102 return target_desc_up (new target_desc ());
105 /* See gdbsupport/tdesc.h. */
107 void
108 target_desc_deleter::operator() (struct target_desc *target_desc) const
110 delete target_desc;
113 #ifndef IN_PROCESS_AGENT
115 static const struct target_desc default_description {};
117 void
118 copy_target_description (struct target_desc *dest,
119 const struct target_desc *src)
121 dest->reg_defs = src->reg_defs;
122 dest->expedite_regs = src->expedite_regs;
123 dest->registers_size = src->registers_size;
124 dest->xmltarget = src->xmltarget;
127 const struct target_desc *
128 current_target_desc (void)
130 if (current_thread == NULL)
131 return &default_description;
133 return current_process ()->tdesc;
136 /* An empty structure. */
138 struct tdesc_compatible_info { };
140 /* See gdbsupport/tdesc.h. */
142 const std::vector<tdesc_compatible_info_up> &
143 tdesc_compatible_info_list (const target_desc *target_desc)
145 static std::vector<tdesc_compatible_info_up> empty;
146 return empty;
149 /* See gdbsupport/tdesc.h. */
151 const char *
152 tdesc_compatible_info_arch_name (const tdesc_compatible_info_up &c_info)
154 return nullptr;
157 /* See gdbsupport/tdesc.h. */
159 const char *
160 tdesc_architecture_name (const struct target_desc *target_desc)
162 return target_desc->arch.get ();
165 /* See gdbsupport/tdesc.h. */
167 void
168 set_tdesc_architecture (struct target_desc *target_desc,
169 const char *name)
171 target_desc->arch = make_unique_xstrdup (name);
174 /* See gdbsupport/tdesc.h. */
176 const char *
177 tdesc_osabi_name (const struct target_desc *target_desc)
179 return target_desc->osabi.get ();
182 /* See gdbsupport/tdesc.h. */
184 void
185 set_tdesc_osabi (struct target_desc *target_desc, enum gdb_osabi osabi)
187 const char *name = gdbarch_osabi_name (osabi);
188 target_desc->osabi = make_unique_xstrdup (name);
191 /* See gdbsupport/tdesc.h. */
193 const char *
194 tdesc_get_features_xml (const target_desc *tdesc)
196 /* Either .xmltarget or .features is not NULL. */
197 gdb_assert (tdesc->xmltarget != NULL
198 || (!tdesc->features.empty ()
199 && tdesc_architecture_name (tdesc) != nullptr));
201 if (tdesc->xmltarget == NULL)
203 std::string buffer ("@");
204 print_xml_feature v (&buffer);
205 tdesc->accept (v);
206 tdesc->xmltarget = xstrdup (buffer.c_str ());
209 return tdesc->xmltarget;
211 #endif
213 /* See gdbsupport/tdesc.h. */
215 struct tdesc_feature *
216 tdesc_create_feature (struct target_desc *tdesc, const char *name)
218 struct tdesc_feature *new_feature = new tdesc_feature (name);
219 tdesc->features.emplace_back (new_feature);
220 return new_feature;
223 /* See gdbsupport/tdesc.h. */
225 bool
226 tdesc_contains_feature (const target_desc *tdesc, const std::string &feature)
228 gdb_assert (tdesc != nullptr);
230 for (const tdesc_feature_up &f : tdesc->features)
232 if (f->name == feature)
233 return true;
236 return false;