xref: /netbsd/external/gpl3/gdb/dist/gdbserver/tdesc.cc (revision 1424dfb3)
1*1424dfb3Schristos /* Copyright (C) 2012-2020 Free Software Foundation, Inc.
2*1424dfb3Schristos 
3*1424dfb3Schristos    This file is part of GDB.
4*1424dfb3Schristos 
5*1424dfb3Schristos    This program is free software; you can redistribute it and/or modify
6*1424dfb3Schristos    it under the terms of the GNU General Public License as published by
7*1424dfb3Schristos    the Free Software Foundation; either version 3 of the License, or
8*1424dfb3Schristos    (at your option) any later version.
9*1424dfb3Schristos 
10*1424dfb3Schristos    This program is distributed in the hope that it will be useful,
11*1424dfb3Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
12*1424dfb3Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*1424dfb3Schristos    GNU General Public License for more details.
14*1424dfb3Schristos 
15*1424dfb3Schristos    You should have received a copy of the GNU General Public License
16*1424dfb3Schristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17*1424dfb3Schristos 
18*1424dfb3Schristos #include "server.h"
19*1424dfb3Schristos #include "tdesc.h"
20*1424dfb3Schristos #include "regdef.h"
21*1424dfb3Schristos 
22*1424dfb3Schristos #ifndef IN_PROCESS_AGENT
23*1424dfb3Schristos 
~target_desc()24*1424dfb3Schristos target_desc::~target_desc ()
25*1424dfb3Schristos {
26*1424dfb3Schristos   xfree ((char *) arch);
27*1424dfb3Schristos   xfree ((char *) osabi);
28*1424dfb3Schristos }
29*1424dfb3Schristos 
operator ==(const target_desc & other) const30*1424dfb3Schristos bool target_desc::operator== (const target_desc &other) const
31*1424dfb3Schristos {
32*1424dfb3Schristos   if (reg_defs != other.reg_defs)
33*1424dfb3Schristos     return false;
34*1424dfb3Schristos 
35*1424dfb3Schristos   /* Compare expedite_regs.  */
36*1424dfb3Schristos   int i = 0;
37*1424dfb3Schristos   for (; expedite_regs[i] != NULL; i++)
38*1424dfb3Schristos     {
39*1424dfb3Schristos       if (strcmp (expedite_regs[i], other.expedite_regs[i]) != 0)
40*1424dfb3Schristos 	return false;
41*1424dfb3Schristos     }
42*1424dfb3Schristos   if (other.expedite_regs[i] != NULL)
43*1424dfb3Schristos     return false;
44*1424dfb3Schristos 
45*1424dfb3Schristos   return true;
46*1424dfb3Schristos }
47*1424dfb3Schristos 
48*1424dfb3Schristos #endif
49*1424dfb3Schristos 
accept(tdesc_element_visitor & v) const50*1424dfb3Schristos void target_desc::accept (tdesc_element_visitor &v) const
51*1424dfb3Schristos {
52*1424dfb3Schristos #ifndef IN_PROCESS_AGENT
53*1424dfb3Schristos   v.visit_pre (this);
54*1424dfb3Schristos 
55*1424dfb3Schristos   for (const tdesc_feature_up &feature : features)
56*1424dfb3Schristos     feature->accept (v);
57*1424dfb3Schristos 
58*1424dfb3Schristos   v.visit_post (this);
59*1424dfb3Schristos #endif
60*1424dfb3Schristos }
61*1424dfb3Schristos 
62*1424dfb3Schristos void
init_target_desc(struct target_desc * tdesc,const char ** expedite_regs)63*1424dfb3Schristos init_target_desc (struct target_desc *tdesc,
64*1424dfb3Schristos 		  const char **expedite_regs)
65*1424dfb3Schristos {
66*1424dfb3Schristos   int offset = 0;
67*1424dfb3Schristos 
68*1424dfb3Schristos   /* Go through all the features and populate reg_defs.  */
69*1424dfb3Schristos   for (const tdesc_feature_up &feature : tdesc->features)
70*1424dfb3Schristos     for (const tdesc_reg_up &treg : feature->registers)
71*1424dfb3Schristos       {
72*1424dfb3Schristos 	int regnum = treg->target_regnum;
73*1424dfb3Schristos 
74*1424dfb3Schristos 	/* Register number will increase (possibly with gaps) or be zero.  */
75*1424dfb3Schristos 	gdb_assert (regnum == 0 || regnum >= tdesc->reg_defs.size ());
76*1424dfb3Schristos 
77*1424dfb3Schristos 	if (regnum != 0)
78*1424dfb3Schristos 	  tdesc->reg_defs.resize (regnum, gdb::reg (offset));
79*1424dfb3Schristos 
80*1424dfb3Schristos 	tdesc->reg_defs.emplace_back (treg->name.c_str (), offset,
81*1424dfb3Schristos 				      treg->bitsize);
82*1424dfb3Schristos 	offset += treg->bitsize;
83*1424dfb3Schristos       }
84*1424dfb3Schristos 
85*1424dfb3Schristos   tdesc->registers_size = offset / 8;
86*1424dfb3Schristos 
87*1424dfb3Schristos   /* Make sure PBUFSIZ is large enough to hold a full register
88*1424dfb3Schristos      packet.  */
89*1424dfb3Schristos   gdb_assert (2 * tdesc->registers_size + 32 <= PBUFSIZ);
90*1424dfb3Schristos 
91*1424dfb3Schristos #ifndef IN_PROCESS_AGENT
92*1424dfb3Schristos   tdesc->expedite_regs = expedite_regs;
93*1424dfb3Schristos #endif
94*1424dfb3Schristos }
95*1424dfb3Schristos 
96*1424dfb3Schristos /* See gdbsupport/tdesc.h.  */
97*1424dfb3Schristos 
98*1424dfb3Schristos struct target_desc *
allocate_target_description(void)99*1424dfb3Schristos allocate_target_description (void)
100*1424dfb3Schristos {
101*1424dfb3Schristos   return new target_desc ();
102*1424dfb3Schristos }
103*1424dfb3Schristos 
104*1424dfb3Schristos /* See gdbsupport/tdesc.h.  */
105*1424dfb3Schristos 
106*1424dfb3Schristos void
operator ()(struct target_desc * target_desc) const107*1424dfb3Schristos target_desc_deleter::operator() (struct target_desc *target_desc) const
108*1424dfb3Schristos {
109*1424dfb3Schristos   delete target_desc;
110*1424dfb3Schristos }
111*1424dfb3Schristos 
112*1424dfb3Schristos #ifndef IN_PROCESS_AGENT
113*1424dfb3Schristos 
114*1424dfb3Schristos static const struct target_desc default_description {};
115*1424dfb3Schristos 
116*1424dfb3Schristos void
copy_target_description(struct target_desc * dest,const struct target_desc * src)117*1424dfb3Schristos copy_target_description (struct target_desc *dest,
118*1424dfb3Schristos 			 const struct target_desc *src)
119*1424dfb3Schristos {
120*1424dfb3Schristos   dest->reg_defs = src->reg_defs;
121*1424dfb3Schristos   dest->expedite_regs = src->expedite_regs;
122*1424dfb3Schristos   dest->registers_size = src->registers_size;
123*1424dfb3Schristos   dest->xmltarget = src->xmltarget;
124*1424dfb3Schristos }
125*1424dfb3Schristos 
126*1424dfb3Schristos const struct target_desc *
current_target_desc(void)127*1424dfb3Schristos current_target_desc (void)
128*1424dfb3Schristos {
129*1424dfb3Schristos   if (current_thread == NULL)
130*1424dfb3Schristos     return &default_description;
131*1424dfb3Schristos 
132*1424dfb3Schristos   return current_process ()->tdesc;
133*1424dfb3Schristos }
134*1424dfb3Schristos 
135*1424dfb3Schristos /* An empty structure.  */
136*1424dfb3Schristos 
137*1424dfb3Schristos struct tdesc_compatible_info { };
138*1424dfb3Schristos 
139*1424dfb3Schristos /* See gdbsupport/tdesc.h.  */
140*1424dfb3Schristos 
141*1424dfb3Schristos const std::vector<tdesc_compatible_info_up> &
tdesc_compatible_info_list(const target_desc * target_desc)142*1424dfb3Schristos tdesc_compatible_info_list (const target_desc *target_desc)
143*1424dfb3Schristos {
144*1424dfb3Schristos   static std::vector<tdesc_compatible_info_up> empty;
145*1424dfb3Schristos   return empty;
146*1424dfb3Schristos }
147*1424dfb3Schristos 
148*1424dfb3Schristos /* See gdbsupport/tdesc.h.  */
149*1424dfb3Schristos 
150*1424dfb3Schristos const char *
tdesc_compatible_info_arch_name(const tdesc_compatible_info_up & c_info)151*1424dfb3Schristos tdesc_compatible_info_arch_name (const tdesc_compatible_info_up &c_info)
152*1424dfb3Schristos {
153*1424dfb3Schristos   return nullptr;
154*1424dfb3Schristos }
155*1424dfb3Schristos 
156*1424dfb3Schristos /* See gdbsupport/tdesc.h.  */
157*1424dfb3Schristos 
158*1424dfb3Schristos const char *
tdesc_architecture_name(const struct target_desc * target_desc)159*1424dfb3Schristos tdesc_architecture_name (const struct target_desc *target_desc)
160*1424dfb3Schristos {
161*1424dfb3Schristos   return target_desc->arch;
162*1424dfb3Schristos }
163*1424dfb3Schristos 
164*1424dfb3Schristos /* See gdbsupport/tdesc.h.  */
165*1424dfb3Schristos 
166*1424dfb3Schristos void
set_tdesc_architecture(struct target_desc * target_desc,const char * name)167*1424dfb3Schristos set_tdesc_architecture (struct target_desc *target_desc,
168*1424dfb3Schristos 			const char *name)
169*1424dfb3Schristos {
170*1424dfb3Schristos   target_desc->arch = xstrdup (name);
171*1424dfb3Schristos }
172*1424dfb3Schristos 
173*1424dfb3Schristos /* See gdbsupport/tdesc.h.  */
174*1424dfb3Schristos 
175*1424dfb3Schristos const char *
tdesc_osabi_name(const struct target_desc * target_desc)176*1424dfb3Schristos tdesc_osabi_name (const struct target_desc *target_desc)
177*1424dfb3Schristos {
178*1424dfb3Schristos   return target_desc->osabi;
179*1424dfb3Schristos }
180*1424dfb3Schristos 
181*1424dfb3Schristos /* See gdbsupport/tdesc.h.  */
182*1424dfb3Schristos 
183*1424dfb3Schristos void
set_tdesc_osabi(struct target_desc * target_desc,const char * name)184*1424dfb3Schristos set_tdesc_osabi (struct target_desc *target_desc, const char *name)
185*1424dfb3Schristos {
186*1424dfb3Schristos   target_desc->osabi = xstrdup (name);
187*1424dfb3Schristos }
188*1424dfb3Schristos 
189*1424dfb3Schristos /* See gdbsupport/tdesc.h.  */
190*1424dfb3Schristos 
191*1424dfb3Schristos const char *
tdesc_get_features_xml(const target_desc * tdesc)192*1424dfb3Schristos tdesc_get_features_xml (const target_desc *tdesc)
193*1424dfb3Schristos {
194*1424dfb3Schristos   /* Either .xmltarget or .features is not NULL.  */
195*1424dfb3Schristos   gdb_assert (tdesc->xmltarget != NULL
196*1424dfb3Schristos 	      || (!tdesc->features.empty ()
197*1424dfb3Schristos 		  && tdesc->arch != NULL));
198*1424dfb3Schristos 
199*1424dfb3Schristos   if (tdesc->xmltarget == NULL)
200*1424dfb3Schristos     {
201*1424dfb3Schristos       std::string buffer ("@");
202*1424dfb3Schristos       print_xml_feature v (&buffer);
203*1424dfb3Schristos       tdesc->accept (v);
204*1424dfb3Schristos       tdesc->xmltarget = xstrdup (buffer.c_str ());
205*1424dfb3Schristos     }
206*1424dfb3Schristos 
207*1424dfb3Schristos   return tdesc->xmltarget;
208*1424dfb3Schristos }
209*1424dfb3Schristos #endif
210*1424dfb3Schristos 
211*1424dfb3Schristos /* See gdbsupport/tdesc.h.  */
212*1424dfb3Schristos 
213*1424dfb3Schristos struct tdesc_feature *
tdesc_create_feature(struct target_desc * tdesc,const char * name)214*1424dfb3Schristos tdesc_create_feature (struct target_desc *tdesc, const char *name)
215*1424dfb3Schristos {
216*1424dfb3Schristos   struct tdesc_feature *new_feature = new tdesc_feature (name);
217*1424dfb3Schristos   tdesc->features.emplace_back (new_feature);
218*1424dfb3Schristos   return new_feature;
219*1424dfb3Schristos }
220*1424dfb3Schristos 
221*1424dfb3Schristos /* See gdbsupport/tdesc.h.  */
222*1424dfb3Schristos 
223*1424dfb3Schristos bool
tdesc_contains_feature(const target_desc * tdesc,const std::string & feature)224*1424dfb3Schristos tdesc_contains_feature (const target_desc *tdesc, const std::string &feature)
225*1424dfb3Schristos {
226*1424dfb3Schristos   gdb_assert (tdesc != nullptr);
227*1424dfb3Schristos 
228*1424dfb3Schristos   for (const tdesc_feature_up &f : tdesc->features)
229*1424dfb3Schristos     {
230*1424dfb3Schristos       if (f->name == feature)
231*1424dfb3Schristos 	return true;
232*1424dfb3Schristos     }
233*1424dfb3Schristos 
234*1424dfb3Schristos   return false;
235*1424dfb3Schristos }
236