target-descriptions.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027
  1. /* Target description support for GDB.
  2. Copyright (C) 2006-2022 Free Software Foundation, Inc.
  3. Contributed by CodeSourcery.
  4. 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.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "defs.h"
  16. #include "arch-utils.h"
  17. #include "gdbcmd.h"
  18. #include "gdbtypes.h"
  19. #include "reggroups.h"
  20. #include "target.h"
  21. #include "target-descriptions.h"
  22. #include "xml-support.h"
  23. #include "xml-tdesc.h"
  24. #include "osabi.h"
  25. #include "gdbsupport/gdb_obstack.h"
  26. #include "hashtab.h"
  27. #include "inferior.h"
  28. #include <algorithm>
  29. #include "completer.h"
  30. #include "readline/tilde.h" /* tilde_expand */
  31. /* Types. */
  32. struct property
  33. {
  34. property (const std::string &key_, const std::string &value_)
  35. : key (key_), value (value_)
  36. {}
  37. std::string key;
  38. std::string value;
  39. };
  40. /* Convert a tdesc_type to a gdb type. */
  41. static type *
  42. make_gdb_type (struct gdbarch *gdbarch, struct tdesc_type *ttype)
  43. {
  44. class gdb_type_creator : public tdesc_element_visitor
  45. {
  46. public:
  47. gdb_type_creator (struct gdbarch *gdbarch)
  48. : m_gdbarch (gdbarch)
  49. {}
  50. type *get_type ()
  51. {
  52. return m_type;
  53. }
  54. void visit (const tdesc_type_builtin *e) override
  55. {
  56. switch (e->kind)
  57. {
  58. /* Predefined types. */
  59. case TDESC_TYPE_BOOL:
  60. m_type = builtin_type (m_gdbarch)->builtin_bool;
  61. return;
  62. case TDESC_TYPE_INT8:
  63. m_type = builtin_type (m_gdbarch)->builtin_int8;
  64. return;
  65. case TDESC_TYPE_INT16:
  66. m_type = builtin_type (m_gdbarch)->builtin_int16;
  67. return;
  68. case TDESC_TYPE_INT32:
  69. m_type = builtin_type (m_gdbarch)->builtin_int32;
  70. return;
  71. case TDESC_TYPE_INT64:
  72. m_type = builtin_type (m_gdbarch)->builtin_int64;
  73. return;
  74. case TDESC_TYPE_INT128:
  75. m_type = builtin_type (m_gdbarch)->builtin_int128;
  76. return;
  77. case TDESC_TYPE_UINT8:
  78. m_type = builtin_type (m_gdbarch)->builtin_uint8;
  79. return;
  80. case TDESC_TYPE_UINT16:
  81. m_type = builtin_type (m_gdbarch)->builtin_uint16;
  82. return;
  83. case TDESC_TYPE_UINT32:
  84. m_type = builtin_type (m_gdbarch)->builtin_uint32;
  85. return;
  86. case TDESC_TYPE_UINT64:
  87. m_type = builtin_type (m_gdbarch)->builtin_uint64;
  88. return;
  89. case TDESC_TYPE_UINT128:
  90. m_type = builtin_type (m_gdbarch)->builtin_uint128;
  91. return;
  92. case TDESC_TYPE_CODE_PTR:
  93. m_type = builtin_type (m_gdbarch)->builtin_func_ptr;
  94. return;
  95. case TDESC_TYPE_DATA_PTR:
  96. m_type = builtin_type (m_gdbarch)->builtin_data_ptr;
  97. return;
  98. }
  99. m_type = tdesc_find_type (m_gdbarch, e->name.c_str ());
  100. if (m_type != NULL)
  101. return;
  102. switch (e->kind)
  103. {
  104. case TDESC_TYPE_IEEE_HALF:
  105. m_type = arch_float_type (m_gdbarch, -1, "builtin_type_ieee_half",
  106. floatformats_ieee_half);
  107. return;
  108. case TDESC_TYPE_IEEE_SINGLE:
  109. m_type = arch_float_type (m_gdbarch, -1, "builtin_type_ieee_single",
  110. floatformats_ieee_single);
  111. return;
  112. case TDESC_TYPE_IEEE_DOUBLE:
  113. m_type = arch_float_type (m_gdbarch, -1, "builtin_type_ieee_double",
  114. floatformats_ieee_double);
  115. return;
  116. case TDESC_TYPE_ARM_FPA_EXT:
  117. m_type = arch_float_type (m_gdbarch, -1, "builtin_type_arm_ext",
  118. floatformats_arm_ext);
  119. return;
  120. case TDESC_TYPE_I387_EXT:
  121. m_type = arch_float_type (m_gdbarch, -1, "builtin_type_i387_ext",
  122. floatformats_i387_ext);
  123. return;
  124. case TDESC_TYPE_BFLOAT16:
  125. m_type = arch_float_type (m_gdbarch, -1, "builtin_type_bfloat16",
  126. floatformats_bfloat16);
  127. return;
  128. }
  129. internal_error (__FILE__, __LINE__,
  130. "Type \"%s\" has an unknown kind %d",
  131. e->name.c_str (), e->kind);
  132. }
  133. void visit (const tdesc_type_vector *e) override
  134. {
  135. m_type = tdesc_find_type (m_gdbarch, e->name.c_str ());
  136. if (m_type != NULL)
  137. return;
  138. type *element_gdb_type = make_gdb_type (m_gdbarch, e->element_type);
  139. m_type = init_vector_type (element_gdb_type, e->count);
  140. m_type->set_name (xstrdup (e->name.c_str ()));
  141. return;
  142. }
  143. void visit (const tdesc_type_with_fields *e) override
  144. {
  145. m_type = tdesc_find_type (m_gdbarch, e->name.c_str ());
  146. if (m_type != NULL)
  147. return;
  148. switch (e->kind)
  149. {
  150. case TDESC_TYPE_STRUCT:
  151. make_gdb_type_struct (e);
  152. return;
  153. case TDESC_TYPE_UNION:
  154. make_gdb_type_union (e);
  155. return;
  156. case TDESC_TYPE_FLAGS:
  157. make_gdb_type_flags (e);
  158. return;
  159. case TDESC_TYPE_ENUM:
  160. make_gdb_type_enum (e);
  161. return;
  162. }
  163. internal_error (__FILE__, __LINE__,
  164. "Type \"%s\" has an unknown kind %d",
  165. e->name.c_str (), e->kind);
  166. }
  167. private:
  168. void make_gdb_type_struct (const tdesc_type_with_fields *e)
  169. {
  170. m_type = arch_composite_type (m_gdbarch, NULL, TYPE_CODE_STRUCT);
  171. m_type->set_name (xstrdup (e->name.c_str ()));
  172. for (const tdesc_type_field &f : e->fields)
  173. {
  174. if (f.start != -1 && f.end != -1)
  175. {
  176. /* Bitfield. */
  177. struct field *fld;
  178. struct type *field_gdb_type;
  179. int bitsize, total_size;
  180. /* This invariant should be preserved while creating types. */
  181. gdb_assert (e->size != 0);
  182. if (f.type != NULL)
  183. field_gdb_type = make_gdb_type (m_gdbarch, f.type);
  184. else if (e->size > 4)
  185. field_gdb_type = builtin_type (m_gdbarch)->builtin_uint64;
  186. else
  187. field_gdb_type = builtin_type (m_gdbarch)->builtin_uint32;
  188. fld = append_composite_type_field_raw
  189. (m_type, xstrdup (f.name.c_str ()), field_gdb_type);
  190. /* For little-endian, BITPOS counts from the LSB of
  191. the structure and marks the LSB of the field. For
  192. big-endian, BITPOS counts from the MSB of the
  193. structure and marks the MSB of the field. Either
  194. way, it is the number of bits to the "left" of the
  195. field. To calculate this in big-endian, we need
  196. the total size of the structure. */
  197. bitsize = f.end - f.start + 1;
  198. total_size = e->size * TARGET_CHAR_BIT;
  199. if (gdbarch_byte_order (m_gdbarch) == BFD_ENDIAN_BIG)
  200. fld->set_loc_bitpos (total_size - f.start - bitsize);
  201. else
  202. fld->set_loc_bitpos (f.start);
  203. FIELD_BITSIZE (fld[0]) = bitsize;
  204. }
  205. else
  206. {
  207. gdb_assert (f.start == -1 && f.end == -1);
  208. type *field_gdb_type = make_gdb_type (m_gdbarch, f.type);
  209. append_composite_type_field (m_type,
  210. xstrdup (f.name.c_str ()),
  211. field_gdb_type);
  212. }
  213. }
  214. if (e->size != 0)
  215. TYPE_LENGTH (m_type) = e->size;
  216. }
  217. void make_gdb_type_union (const tdesc_type_with_fields *e)
  218. {
  219. m_type = arch_composite_type (m_gdbarch, NULL, TYPE_CODE_UNION);
  220. m_type->set_name (xstrdup (e->name.c_str ()));
  221. for (const tdesc_type_field &f : e->fields)
  222. {
  223. type* field_gdb_type = make_gdb_type (m_gdbarch, f.type);
  224. append_composite_type_field (m_type, xstrdup (f.name.c_str ()),
  225. field_gdb_type);
  226. /* If any of the children of a union are vectors, flag the
  227. union as a vector also. This allows e.g. a union of two
  228. vector types to show up automatically in "info vector". */
  229. if (field_gdb_type->is_vector ())
  230. m_type->set_is_vector (true);
  231. }
  232. }
  233. void make_gdb_type_flags (const tdesc_type_with_fields *e)
  234. {
  235. m_type = arch_flags_type (m_gdbarch, e->name.c_str (),
  236. e->size * TARGET_CHAR_BIT);
  237. for (const tdesc_type_field &f : e->fields)
  238. {
  239. int bitsize = f.end - f.start + 1;
  240. gdb_assert (f.type != NULL);
  241. type *field_gdb_type = make_gdb_type (m_gdbarch, f.type);
  242. append_flags_type_field (m_type, f.start, bitsize,
  243. field_gdb_type, f.name.c_str ());
  244. }
  245. }
  246. void make_gdb_type_enum (const tdesc_type_with_fields *e)
  247. {
  248. m_type = arch_type (m_gdbarch, TYPE_CODE_ENUM, e->size * TARGET_CHAR_BIT,
  249. e->name.c_str ());
  250. m_type->set_is_unsigned (true);
  251. for (const tdesc_type_field &f : e->fields)
  252. {
  253. struct field *fld
  254. = append_composite_type_field_raw (m_type,
  255. xstrdup (f.name.c_str ()),
  256. NULL);
  257. fld->set_loc_enumval (f.start);
  258. }
  259. }
  260. /* The gdbarch used. */
  261. struct gdbarch *m_gdbarch;
  262. /* The type created. */
  263. type *m_type;
  264. };
  265. gdb_type_creator gdb_type (gdbarch);
  266. ttype->accept (gdb_type);
  267. return gdb_type.get_type ();
  268. }
  269. /* Wrapper around bfd_arch_info_type. A class with this name is used in
  270. the API that is shared between gdb and gdbserver code, but gdbserver
  271. doesn't use compatibility information, so its version of this class is
  272. empty. */
  273. class tdesc_compatible_info
  274. {
  275. public:
  276. /* Constructor. */
  277. explicit tdesc_compatible_info (const bfd_arch_info_type *arch)
  278. : m_arch (arch)
  279. { /* Nothing. */ }
  280. /* Access the contained pointer. */
  281. const bfd_arch_info_type *arch () const
  282. { return m_arch; }
  283. private:
  284. /* Architecture information looked up from the <compatible> entity within
  285. a target description. */
  286. const bfd_arch_info_type *m_arch;
  287. };
  288. /* A target description. */
  289. struct target_desc : tdesc_element
  290. {
  291. target_desc ()
  292. {}
  293. virtual ~target_desc () = default;
  294. target_desc (const target_desc &) = delete;
  295. void operator= (const target_desc &) = delete;
  296. /* The architecture reported by the target, if any. */
  297. const struct bfd_arch_info *arch = NULL;
  298. /* The osabi reported by the target, if any; GDB_OSABI_UNKNOWN
  299. otherwise. */
  300. enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
  301. /* The list of compatible architectures reported by the target. */
  302. std::vector<tdesc_compatible_info_up> compatible;
  303. /* Any architecture-specific properties specified by the target. */
  304. std::vector<property> properties;
  305. /* The features associated with this target. */
  306. std::vector<tdesc_feature_up> features;
  307. /* Used to cache the generated xml version of the target description. */
  308. mutable char *xmltarget = nullptr;
  309. void accept (tdesc_element_visitor &v) const override
  310. {
  311. v.visit_pre (this);
  312. for (const tdesc_feature_up &feature : features)
  313. feature->accept (v);
  314. v.visit_post (this);
  315. }
  316. bool operator== (const target_desc &other) const
  317. {
  318. if (arch != other.arch)
  319. return false;
  320. if (osabi != other.osabi)
  321. return false;
  322. if (features.size () != other.features.size ())
  323. return false;
  324. for (int ix = 0; ix < features.size (); ix++)
  325. {
  326. const tdesc_feature_up &feature1 = features[ix];
  327. const tdesc_feature_up &feature2 = other.features[ix];
  328. if (feature1 != feature2 && *feature1 != *feature2)
  329. return false;
  330. }
  331. return true;
  332. }
  333. bool operator!= (const target_desc &other) const
  334. {
  335. return !(*this == other);
  336. }
  337. };
  338. /* Per-architecture data associated with a target description. The
  339. target description may be shared by multiple architectures, but
  340. this data is private to one gdbarch. */
  341. struct tdesc_arch_reg
  342. {
  343. tdesc_arch_reg (tdesc_reg *reg_, struct type *type_)
  344. : reg (reg_), type (type_)
  345. {}
  346. struct tdesc_reg *reg;
  347. struct type *type;
  348. };
  349. struct tdesc_arch_data
  350. {
  351. /* A list of register/type pairs, indexed by GDB's internal register number.
  352. During initialization of the gdbarch this list is used to store
  353. registers which the architecture assigns a fixed register number.
  354. Registers which are NULL in this array, or off the end, are
  355. treated as zero-sized and nameless (i.e. placeholders in the
  356. numbering). */
  357. std::vector<tdesc_arch_reg> arch_regs;
  358. /* Functions which report the register name, type, and reggroups for
  359. pseudo-registers. */
  360. gdbarch_register_name_ftype *pseudo_register_name = NULL;
  361. gdbarch_register_type_ftype *pseudo_register_type = NULL;
  362. gdbarch_register_reggroup_p_ftype *pseudo_register_reggroup_p = NULL;
  363. };
  364. /* Info about an inferior's target description. There's one of these
  365. for each inferior. */
  366. struct target_desc_info
  367. {
  368. /* A flag indicating that a description has already been fetched
  369. from the target, so it should not be queried again. */
  370. bool fetched = false;
  371. /* The description fetched from the target, or NULL if the target
  372. did not supply any description. Only valid when
  373. FETCHED is set. Only the description initialization
  374. code should access this; normally, the description should be
  375. accessed through the gdbarch object. */
  376. const struct target_desc *tdesc = nullptr;
  377. /* If not empty, the filename to read a target description from, as set by
  378. "set tdesc filename ...".
  379. If empty, there is not filename specified by the user. */
  380. std::string filename;
  381. };
  382. /* Get the inferior INF's target description info, allocating one on
  383. the stop if necessary. */
  384. static struct target_desc_info *
  385. get_tdesc_info (struct inferior *inf)
  386. {
  387. if (inf->tdesc_info == NULL)
  388. inf->tdesc_info = new target_desc_info;
  389. return inf->tdesc_info;
  390. }
  391. /* A handle for architecture-specific data associated with the
  392. target description (see struct tdesc_arch_data). */
  393. static struct gdbarch_data *tdesc_data;
  394. /* See target-descriptions.h. */
  395. int
  396. target_desc_info_from_user_p (struct target_desc_info *info)
  397. {
  398. return info != nullptr && !info->filename.empty ();
  399. }
  400. /* See target-descriptions.h. */
  401. void
  402. copy_inferior_target_desc_info (struct inferior *destinf, struct inferior *srcinf)
  403. {
  404. struct target_desc_info *src = get_tdesc_info (srcinf);
  405. struct target_desc_info *dest = get_tdesc_info (destinf);
  406. *dest = *src;
  407. }
  408. /* See target-descriptions.h. */
  409. void
  410. target_desc_info_free (struct target_desc_info *tdesc_info)
  411. {
  412. delete tdesc_info;
  413. }
  414. /* The string manipulated by the "set tdesc filename ..." command. */
  415. static std::string tdesc_filename_cmd_string;
  416. /* Fetch the current target's description, and switch the current
  417. architecture to one which incorporates that description. */
  418. void
  419. target_find_description (void)
  420. {
  421. target_desc_info *tdesc_info = get_tdesc_info (current_inferior ());
  422. /* If we've already fetched a description from the target, don't do
  423. it again. This allows a target to fetch the description early,
  424. during its to_open or to_create_inferior, if it needs extra
  425. information about the target to initialize. */
  426. if (tdesc_info->fetched)
  427. return;
  428. /* The current architecture should not have any target description
  429. specified. It should have been cleared, e.g. when we
  430. disconnected from the previous target. */
  431. gdb_assert (gdbarch_target_desc (target_gdbarch ()) == NULL);
  432. /* First try to fetch an XML description from the user-specified
  433. file. */
  434. tdesc_info->tdesc = nullptr;
  435. if (!tdesc_info->filename.empty ())
  436. tdesc_info->tdesc = file_read_description_xml (tdesc_info->filename.data ());
  437. /* Next try to read the description from the current target using
  438. target objects. */
  439. if (tdesc_info->tdesc == nullptr)
  440. tdesc_info->tdesc = target_read_description_xml
  441. (current_inferior ()->top_target ());
  442. /* If that failed try a target-specific hook. */
  443. if (tdesc_info->tdesc == nullptr)
  444. tdesc_info->tdesc = target_read_description
  445. (current_inferior ()->top_target ());
  446. /* If a non-NULL description was returned, then update the current
  447. architecture. */
  448. if (tdesc_info->tdesc != nullptr)
  449. {
  450. struct gdbarch_info info;
  451. info.target_desc = tdesc_info->tdesc;
  452. if (!gdbarch_update_p (info))
  453. warning (_("Architecture rejected target-supplied description"));
  454. else
  455. {
  456. struct tdesc_arch_data *data;
  457. data = ((struct tdesc_arch_data *)
  458. gdbarch_data (target_gdbarch (), tdesc_data));
  459. if (tdesc_has_registers (tdesc_info->tdesc)
  460. && data->arch_regs.empty ())
  461. warning (_("Target-supplied registers are not supported "
  462. "by the current architecture"));
  463. }
  464. }
  465. /* Now that we know this description is usable, record that we
  466. fetched it. */
  467. tdesc_info->fetched = true;
  468. }
  469. /* Discard any description fetched from the current target, and switch
  470. the current architecture to one with no target description. */
  471. void
  472. target_clear_description (void)
  473. {
  474. target_desc_info *tdesc_info = get_tdesc_info (current_inferior ());
  475. if (!tdesc_info->fetched)
  476. return;
  477. tdesc_info->fetched = false;
  478. tdesc_info->tdesc = nullptr;
  479. gdbarch_info info;
  480. if (!gdbarch_update_p (info))
  481. internal_error (__FILE__, __LINE__,
  482. _("Could not remove target-supplied description"));
  483. }
  484. /* Return the global current target description. This should only be
  485. used by gdbarch initialization code; most access should be through
  486. an existing gdbarch. */
  487. const struct target_desc *
  488. target_current_description (void)
  489. {
  490. target_desc_info *tdesc_info = get_tdesc_info (current_inferior ());
  491. if (tdesc_info->fetched)
  492. return tdesc_info->tdesc;
  493. return NULL;
  494. }
  495. /* Return non-zero if this target description is compatible
  496. with the given BFD architecture. */
  497. int
  498. tdesc_compatible_p (const struct target_desc *target_desc,
  499. const struct bfd_arch_info *arch)
  500. {
  501. for (const tdesc_compatible_info_up &compat : target_desc->compatible)
  502. {
  503. if (compat->arch () == arch
  504. || arch->compatible (arch, compat->arch ())
  505. || compat->arch ()->compatible (compat->arch (), arch))
  506. return 1;
  507. }
  508. return 0;
  509. }
  510. /* Direct accessors for target descriptions. */
  511. /* Return the string value of a property named KEY, or NULL if the
  512. property was not specified. */
  513. const char *
  514. tdesc_property (const struct target_desc *target_desc, const char *key)
  515. {
  516. for (const property &prop : target_desc->properties)
  517. if (prop.key == key)
  518. return prop.value.c_str ();
  519. return NULL;
  520. }
  521. /* Return the BFD architecture associated with this target
  522. description, or NULL if no architecture was specified. */
  523. const struct bfd_arch_info *
  524. tdesc_architecture (const struct target_desc *target_desc)
  525. {
  526. return target_desc->arch;
  527. }
  528. /* See gdbsupport/tdesc.h. */
  529. const char *
  530. tdesc_architecture_name (const struct target_desc *target_desc)
  531. {
  532. if (target_desc->arch != NULL)
  533. return target_desc->arch->printable_name;
  534. return NULL;
  535. }
  536. /* See gdbsupport/tdesc.h. */
  537. const std::vector<tdesc_compatible_info_up> &
  538. tdesc_compatible_info_list (const target_desc *target_desc)
  539. {
  540. return target_desc->compatible;
  541. }
  542. /* See gdbsupport/tdesc.h. */
  543. const char *
  544. tdesc_compatible_info_arch_name (const tdesc_compatible_info_up &compatible)
  545. {
  546. return compatible->arch ()->printable_name;
  547. }
  548. /* Return the OSABI associated with this target description, or
  549. GDB_OSABI_UNKNOWN if no osabi was specified. */
  550. enum gdb_osabi
  551. tdesc_osabi (const struct target_desc *target_desc)
  552. {
  553. return target_desc->osabi;
  554. }
  555. /* See gdbsupport/tdesc.h. */
  556. const char *
  557. tdesc_osabi_name (const struct target_desc *target_desc)
  558. {
  559. enum gdb_osabi osabi = tdesc_osabi (target_desc);
  560. if (osabi > GDB_OSABI_UNKNOWN && osabi < GDB_OSABI_INVALID)
  561. return gdbarch_osabi_name (osabi);
  562. return nullptr;
  563. }
  564. /* Return 1 if this target description includes any registers. */
  565. int
  566. tdesc_has_registers (const struct target_desc *target_desc)
  567. {
  568. if (target_desc == NULL)
  569. return 0;
  570. for (const tdesc_feature_up &feature : target_desc->features)
  571. if (!feature->registers.empty ())
  572. return 1;
  573. return 0;
  574. }
  575. /* Return the feature with the given name, if present, or NULL if
  576. the named feature is not found. */
  577. const struct tdesc_feature *
  578. tdesc_find_feature (const struct target_desc *target_desc,
  579. const char *name)
  580. {
  581. for (const tdesc_feature_up &feature : target_desc->features)
  582. if (feature->name == name)
  583. return feature.get ();
  584. return NULL;
  585. }
  586. /* Return the name of FEATURE. */
  587. const char *
  588. tdesc_feature_name (const struct tdesc_feature *feature)
  589. {
  590. return feature->name.c_str ();
  591. }
  592. /* Lookup type associated with ID. */
  593. struct type *
  594. tdesc_find_type (struct gdbarch *gdbarch, const char *id)
  595. {
  596. tdesc_arch_data *data
  597. = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
  598. for (const tdesc_arch_reg &reg : data->arch_regs)
  599. {
  600. if (reg.reg
  601. && reg.reg->tdesc_type
  602. && reg.type
  603. && reg.reg->tdesc_type->name == id)
  604. return reg.type;
  605. }
  606. return NULL;
  607. }
  608. /* Support for registers from target descriptions. */
  609. /* Construct the per-gdbarch data. */
  610. static void *
  611. tdesc_data_init (struct obstack *obstack)
  612. {
  613. return obstack_new<tdesc_arch_data> (obstack);
  614. }
  615. /* Similar, but for the temporary copy used during architecture
  616. initialization. */
  617. tdesc_arch_data_up
  618. tdesc_data_alloc (void)
  619. {
  620. return tdesc_arch_data_up (new tdesc_arch_data ());
  621. }
  622. /* See target-descriptions.h. */
  623. void
  624. tdesc_arch_data_deleter::operator() (struct tdesc_arch_data *data) const
  625. {
  626. delete data;
  627. }
  628. /* Search FEATURE for a register named NAME. */
  629. static struct tdesc_reg *
  630. tdesc_find_register_early (const struct tdesc_feature *feature,
  631. const char *name)
  632. {
  633. for (const tdesc_reg_up &reg : feature->registers)
  634. if (strcasecmp (reg->name.c_str (), name) == 0)
  635. return reg.get ();
  636. return NULL;
  637. }
  638. /* Search FEATURE for a register named NAME. Assign REGNO to it. */
  639. int
  640. tdesc_numbered_register (const struct tdesc_feature *feature,
  641. struct tdesc_arch_data *data,
  642. int regno, const char *name)
  643. {
  644. struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
  645. if (reg == NULL)
  646. return 0;
  647. /* Make sure the vector includes a REGNO'th element. */
  648. while (regno >= data->arch_regs.size ())
  649. data->arch_regs.emplace_back (nullptr, nullptr);
  650. data->arch_regs[regno] = tdesc_arch_reg (reg, NULL);
  651. return 1;
  652. }
  653. /* Search FEATURE for a register named NAME, but do not assign a fixed
  654. register number to it. */
  655. int
  656. tdesc_unnumbered_register (const struct tdesc_feature *feature,
  657. const char *name)
  658. {
  659. struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
  660. if (reg == NULL)
  661. return 0;
  662. return 1;
  663. }
  664. /* Search FEATURE for a register whose name is in NAMES and assign
  665. REGNO to it. */
  666. int
  667. tdesc_numbered_register_choices (const struct tdesc_feature *feature,
  668. struct tdesc_arch_data *data,
  669. int regno, const char *const names[])
  670. {
  671. int i;
  672. for (i = 0; names[i] != NULL; i++)
  673. if (tdesc_numbered_register (feature, data, regno, names[i]))
  674. return 1;
  675. return 0;
  676. }
  677. /* Search FEATURE for a register named NAME, and return its size in
  678. bits. The register must exist. */
  679. int
  680. tdesc_register_bitsize (const struct tdesc_feature *feature, const char *name)
  681. {
  682. struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
  683. gdb_assert (reg != NULL);
  684. return reg->bitsize;
  685. }
  686. /* Look up a register by its GDB internal register number. */
  687. static struct tdesc_arch_reg *
  688. tdesc_find_arch_register (struct gdbarch *gdbarch, int regno)
  689. {
  690. struct tdesc_arch_data *data;
  691. data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
  692. if (regno < data->arch_regs.size ())
  693. return &data->arch_regs[regno];
  694. else
  695. return NULL;
  696. }
  697. static struct tdesc_reg *
  698. tdesc_find_register (struct gdbarch *gdbarch, int regno)
  699. {
  700. struct tdesc_arch_reg *reg = tdesc_find_arch_register (gdbarch, regno);
  701. return reg? reg->reg : NULL;
  702. }
  703. /* Return the name of register REGNO, from the target description or
  704. from an architecture-provided pseudo_register_name method. */
  705. const char *
  706. tdesc_register_name (struct gdbarch *gdbarch, int regno)
  707. {
  708. struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
  709. int num_regs = gdbarch_num_regs (gdbarch);
  710. if (reg != NULL)
  711. return reg->name.c_str ();
  712. if (regno >= num_regs && regno < gdbarch_num_cooked_regs (gdbarch))
  713. {
  714. struct tdesc_arch_data *data
  715. = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
  716. gdb_assert (data->pseudo_register_name != NULL);
  717. return data->pseudo_register_name (gdbarch, regno);
  718. }
  719. return "";
  720. }
  721. struct type *
  722. tdesc_register_type (struct gdbarch *gdbarch, int regno)
  723. {
  724. struct tdesc_arch_reg *arch_reg = tdesc_find_arch_register (gdbarch, regno);
  725. struct tdesc_reg *reg = arch_reg? arch_reg->reg : NULL;
  726. int num_regs = gdbarch_num_regs (gdbarch);
  727. int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
  728. if (reg == NULL && regno >= num_regs && regno < num_regs + num_pseudo_regs)
  729. {
  730. struct tdesc_arch_data *data
  731. = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
  732. gdb_assert (data->pseudo_register_type != NULL);
  733. return data->pseudo_register_type (gdbarch, regno);
  734. }
  735. if (reg == NULL)
  736. /* Return "int0_t", since "void" has a misleading size of one. */
  737. return builtin_type (gdbarch)->builtin_int0;
  738. if (arch_reg->type == NULL)
  739. {
  740. /* First check for a predefined or target defined type. */
  741. if (reg->tdesc_type)
  742. arch_reg->type = make_gdb_type (gdbarch, reg->tdesc_type);
  743. /* Next try size-sensitive type shortcuts. */
  744. else if (reg->type == "float")
  745. {
  746. if (reg->bitsize == gdbarch_float_bit (gdbarch))
  747. arch_reg->type = builtin_type (gdbarch)->builtin_float;
  748. else if (reg->bitsize == gdbarch_double_bit (gdbarch))
  749. arch_reg->type = builtin_type (gdbarch)->builtin_double;
  750. else if (reg->bitsize == gdbarch_long_double_bit (gdbarch))
  751. arch_reg->type = builtin_type (gdbarch)->builtin_long_double;
  752. else
  753. {
  754. warning (_("Register \"%s\" has an unsupported size (%d bits)"),
  755. reg->name.c_str (), reg->bitsize);
  756. arch_reg->type = builtin_type (gdbarch)->builtin_double;
  757. }
  758. }
  759. else if (reg->type == "int")
  760. {
  761. if (reg->bitsize == gdbarch_long_bit (gdbarch))
  762. arch_reg->type = builtin_type (gdbarch)->builtin_long;
  763. else if (reg->bitsize == TARGET_CHAR_BIT)
  764. arch_reg->type = builtin_type (gdbarch)->builtin_char;
  765. else if (reg->bitsize == gdbarch_short_bit (gdbarch))
  766. arch_reg->type = builtin_type (gdbarch)->builtin_short;
  767. else if (reg->bitsize == gdbarch_int_bit (gdbarch))
  768. arch_reg->type = builtin_type (gdbarch)->builtin_int;
  769. else if (reg->bitsize == gdbarch_long_long_bit (gdbarch))
  770. arch_reg->type = builtin_type (gdbarch)->builtin_long_long;
  771. else if (reg->bitsize == gdbarch_ptr_bit (gdbarch))
  772. /* A bit desperate by this point... */
  773. arch_reg->type = builtin_type (gdbarch)->builtin_data_ptr;
  774. else
  775. {
  776. warning (_("Register \"%s\" has an unsupported size (%d bits)"),
  777. reg->name.c_str (), reg->bitsize);
  778. arch_reg->type = builtin_type (gdbarch)->builtin_long;
  779. }
  780. }
  781. if (arch_reg->type == NULL)
  782. internal_error (__FILE__, __LINE__,
  783. "Register \"%s\" has an unknown type \"%s\"",
  784. reg->name.c_str (), reg->type.c_str ());
  785. }
  786. return arch_reg->type;
  787. }
  788. static int
  789. tdesc_remote_register_number (struct gdbarch *gdbarch, int regno)
  790. {
  791. struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
  792. if (reg != NULL)
  793. return reg->target_regnum;
  794. else
  795. return -1;
  796. }
  797. /* Check whether REGNUM is a member of REGGROUP. Registers from the
  798. target description may be classified as general, float, vector or other
  799. register groups registered with reggroup_add(). Unlike a gdbarch
  800. register_reggroup_p method, this function will return -1 if it does not
  801. know; the caller should handle registers with no specified group.
  802. The names of containing features are not used. This might be extended
  803. to display registers in some more useful groupings.
  804. The save-restore flag is also implemented here. */
  805. int
  806. tdesc_register_in_reggroup_p (struct gdbarch *gdbarch, int regno,
  807. const struct reggroup *reggroup)
  808. {
  809. struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
  810. if (reg != NULL && !reg->group.empty ()
  811. && (reg->group == reggroup->name ()))
  812. return 1;
  813. if (reg != NULL
  814. && (reggroup == save_reggroup || reggroup == restore_reggroup))
  815. return reg->save_restore;
  816. return -1;
  817. }
  818. /* Check whether REGNUM is a member of REGGROUP. Registers with no
  819. group specified go to the default reggroup function and are handled
  820. by type. */
  821. static int
  822. tdesc_register_reggroup_p (struct gdbarch *gdbarch, int regno,
  823. const struct reggroup *reggroup)
  824. {
  825. int num_regs = gdbarch_num_regs (gdbarch);
  826. int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
  827. int ret;
  828. if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
  829. {
  830. struct tdesc_arch_data *data
  831. = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
  832. if (data->pseudo_register_reggroup_p != NULL)
  833. return data->pseudo_register_reggroup_p (gdbarch, regno, reggroup);
  834. /* Otherwise fall through to the default reggroup_p. */
  835. }
  836. ret = tdesc_register_in_reggroup_p (gdbarch, regno, reggroup);
  837. if (ret != -1)
  838. return ret;
  839. return default_register_reggroup_p (gdbarch, regno, reggroup);
  840. }
  841. /* Record architecture-specific functions to call for pseudo-register
  842. support. */
  843. void
  844. set_tdesc_pseudo_register_name (struct gdbarch *gdbarch,
  845. gdbarch_register_name_ftype *pseudo_name)
  846. {
  847. struct tdesc_arch_data *data
  848. = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
  849. data->pseudo_register_name = pseudo_name;
  850. }
  851. void
  852. set_tdesc_pseudo_register_type (struct gdbarch *gdbarch,
  853. gdbarch_register_type_ftype *pseudo_type)
  854. {
  855. struct tdesc_arch_data *data
  856. = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
  857. data->pseudo_register_type = pseudo_type;
  858. }
  859. void
  860. set_tdesc_pseudo_register_reggroup_p
  861. (struct gdbarch *gdbarch,
  862. gdbarch_register_reggroup_p_ftype *pseudo_reggroup_p)
  863. {
  864. struct tdesc_arch_data *data
  865. = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
  866. data->pseudo_register_reggroup_p = pseudo_reggroup_p;
  867. }
  868. /* Update GDBARCH to use the target description for registers. */
  869. void
  870. tdesc_use_registers (struct gdbarch *gdbarch,
  871. const struct target_desc *target_desc,
  872. tdesc_arch_data_up &&early_data,
  873. tdesc_unknown_register_ftype unk_reg_cb)
  874. {
  875. int num_regs = gdbarch_num_regs (gdbarch);
  876. struct tdesc_arch_data *data;
  877. /* We can't use the description for registers if it doesn't describe
  878. any. This function should only be called after validating
  879. registers, so the caller should know that registers are
  880. included. */
  881. gdb_assert (tdesc_has_registers (target_desc));
  882. data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
  883. data->arch_regs = std::move (early_data->arch_regs);
  884. /* Build up a set of all registers, so that we can assign register
  885. numbers where needed. The hash table expands as necessary, so
  886. the initial size is arbitrary. */
  887. htab_up reg_hash (htab_create (37, htab_hash_pointer, htab_eq_pointer,
  888. NULL));
  889. for (const tdesc_feature_up &feature : target_desc->features)
  890. for (const tdesc_reg_up &reg : feature->registers)
  891. {
  892. void **slot = htab_find_slot (reg_hash.get (), reg.get (), INSERT);
  893. *slot = reg.get ();
  894. /* Add reggroup if its new. */
  895. if (!reg->group.empty ())
  896. if (reggroup_find (gdbarch, reg->group.c_str ()) == NULL)
  897. reggroup_add (gdbarch, reggroup_gdbarch_new (gdbarch,
  898. reg->group.c_str (),
  899. USER_REGGROUP));
  900. }
  901. /* Remove any registers which were assigned numbers by the
  902. architecture. */
  903. for (const tdesc_arch_reg &arch_reg : data->arch_regs)
  904. if (arch_reg.reg != NULL)
  905. htab_remove_elt (reg_hash.get (), arch_reg.reg);
  906. /* Assign numbers to the remaining registers and add them to the
  907. list of registers. The new numbers are always above gdbarch_num_regs.
  908. Iterate over the features, not the hash table, so that the order
  909. matches that in the target description. */
  910. gdb_assert (data->arch_regs.size () <= num_regs);
  911. while (data->arch_regs.size () < num_regs)
  912. data->arch_regs.emplace_back (nullptr, nullptr);
  913. /* First we give the target a chance to number previously unknown
  914. registers. This allows targets to record the numbers assigned based
  915. on which feature the register was from. */
  916. if (unk_reg_cb != NULL)
  917. {
  918. for (const tdesc_feature_up &feature : target_desc->features)
  919. for (const tdesc_reg_up &reg : feature->registers)
  920. if (htab_find (reg_hash.get (), reg.get ()) != NULL)
  921. {
  922. int regno = unk_reg_cb (gdbarch, feature.get (),
  923. reg->name.c_str (), num_regs);
  924. gdb_assert (regno == -1 || regno >= num_regs);
  925. if (regno != -1)
  926. {
  927. while (regno >= data->arch_regs.size ())
  928. data->arch_regs.emplace_back (nullptr, nullptr);
  929. data->arch_regs[regno] = tdesc_arch_reg (reg.get (), NULL);
  930. num_regs = regno + 1;
  931. htab_remove_elt (reg_hash.get (), reg.get ());
  932. }
  933. }
  934. }
  935. /* Ensure the array was sized correctly above. */
  936. gdb_assert (data->arch_regs.size () == num_regs);
  937. /* Now in a final pass we assign register numbers to any remaining
  938. unnumbered registers. */
  939. for (const tdesc_feature_up &feature : target_desc->features)
  940. for (const tdesc_reg_up &reg : feature->registers)
  941. if (htab_find (reg_hash.get (), reg.get ()) != NULL)
  942. {
  943. data->arch_regs.emplace_back (reg.get (), nullptr);
  944. num_regs++;
  945. }
  946. /* Update the architecture. */
  947. set_gdbarch_num_regs (gdbarch, num_regs);
  948. set_gdbarch_register_name (gdbarch, tdesc_register_name);
  949. set_gdbarch_register_type (gdbarch, tdesc_register_type);
  950. set_gdbarch_remote_register_number (gdbarch,
  951. tdesc_remote_register_number);
  952. set_gdbarch_register_reggroup_p (gdbarch, tdesc_register_reggroup_p);
  953. }
  954. /* See gdbsupport/tdesc.h. */
  955. struct tdesc_feature *
  956. tdesc_create_feature (struct target_desc *tdesc, const char *name)
  957. {
  958. struct tdesc_feature *new_feature = new tdesc_feature (name);
  959. tdesc->features.emplace_back (new_feature);
  960. return new_feature;
  961. }
  962. /* See gdbsupport/tdesc.h. */
  963. target_desc_up
  964. allocate_target_description (void)
  965. {
  966. return target_desc_up (new target_desc ());
  967. }
  968. /* See gdbsupport/tdesc.h. */
  969. void
  970. target_desc_deleter::operator() (struct target_desc *target_desc) const
  971. {
  972. delete target_desc;
  973. }
  974. void
  975. tdesc_add_compatible (struct target_desc *target_desc,
  976. const struct bfd_arch_info *compatible)
  977. {
  978. /* If this instance of GDB is compiled without BFD support for the
  979. compatible architecture, simply ignore it -- we would not be able
  980. to handle it anyway. */
  981. if (compatible == NULL)
  982. return;
  983. for (const tdesc_compatible_info_up &compat : target_desc->compatible)
  984. if (compat->arch () == compatible)
  985. internal_error (__FILE__, __LINE__,
  986. _("Attempted to add duplicate "
  987. "compatible architecture \"%s\""),
  988. compatible->printable_name);
  989. target_desc->compatible.push_back
  990. (std::unique_ptr<tdesc_compatible_info>
  991. (new tdesc_compatible_info (compatible)));
  992. }
  993. void
  994. set_tdesc_property (struct target_desc *target_desc,
  995. const char *key, const char *value)
  996. {
  997. gdb_assert (key != NULL && value != NULL);
  998. if (tdesc_property (target_desc, key) != NULL)
  999. internal_error (__FILE__, __LINE__,
  1000. _("Attempted to add duplicate property \"%s\""), key);
  1001. target_desc->properties.emplace_back (key, value);
  1002. }
  1003. /* See gdbsupport/tdesc.h. */
  1004. void
  1005. set_tdesc_architecture (struct target_desc *target_desc,
  1006. const char *name)
  1007. {
  1008. set_tdesc_architecture (target_desc, bfd_scan_arch (name));
  1009. }
  1010. void
  1011. set_tdesc_architecture (struct target_desc *target_desc,
  1012. const struct bfd_arch_info *arch)
  1013. {
  1014. target_desc->arch = arch;
  1015. }
  1016. /* See gdbsupport/tdesc.h. */
  1017. void
  1018. set_tdesc_osabi (struct target_desc *target_desc, const char *name)
  1019. {
  1020. set_tdesc_osabi (target_desc, osabi_from_tdesc_string (name));
  1021. }
  1022. void
  1023. set_tdesc_osabi (struct target_desc *target_desc, enum gdb_osabi osabi)
  1024. {
  1025. target_desc->osabi = osabi;
  1026. }
  1027. static struct cmd_list_element *tdesc_set_cmdlist, *tdesc_show_cmdlist;
  1028. static struct cmd_list_element *tdesc_unset_cmdlist;
  1029. /* Helper functions for the CLI commands. */
  1030. static void
  1031. set_tdesc_filename_cmd (const char *args, int from_tty,
  1032. struct cmd_list_element *c)
  1033. {
  1034. target_desc_info *tdesc_info = get_tdesc_info (current_inferior ());
  1035. tdesc_info->filename = tdesc_filename_cmd_string;
  1036. target_clear_description ();
  1037. target_find_description ();
  1038. }
  1039. static void
  1040. show_tdesc_filename_cmd (struct ui_file *file, int from_tty,
  1041. struct cmd_list_element *c,
  1042. const char *value)
  1043. {
  1044. value = get_tdesc_info (current_inferior ())->filename.data ();
  1045. if (value != NULL && *value != '\0')
  1046. gdb_printf (file,
  1047. _("The target description will be read from \"%s\".\n"),
  1048. value);
  1049. else
  1050. gdb_printf (file,
  1051. _("The target description will be "
  1052. "read from the target.\n"));
  1053. }
  1054. static void
  1055. unset_tdesc_filename_cmd (const char *args, int from_tty)
  1056. {
  1057. target_desc_info *tdesc_info = get_tdesc_info (current_inferior ());
  1058. tdesc_info->filename.clear ();
  1059. target_clear_description ();
  1060. target_find_description ();
  1061. }
  1062. /* Print target description in C. */
  1063. class print_c_tdesc : public tdesc_element_visitor
  1064. {
  1065. public:
  1066. print_c_tdesc (std::string &filename_after_features)
  1067. : m_filename_after_features (filename_after_features)
  1068. {
  1069. const char *inp;
  1070. char *outp;
  1071. const char *filename = lbasename (m_filename_after_features.c_str ());
  1072. m_function = (char *) xmalloc (strlen (filename) + 1);
  1073. for (inp = filename, outp = m_function; *inp != '\0'; inp++)
  1074. if (*inp == '.')
  1075. break;
  1076. else if (*inp == '-')
  1077. *outp++ = '_';
  1078. else if (*inp == ' ')
  1079. *outp++ = '_';
  1080. else
  1081. *outp++ = *inp;
  1082. *outp = '\0';
  1083. /* Standard boilerplate. */
  1084. gdb_printf ("/* THIS FILE IS GENERATED. "
  1085. "-*- buffer-read-only: t -*- vi"
  1086. ":set ro:\n");
  1087. }
  1088. ~print_c_tdesc ()
  1089. {
  1090. xfree (m_function);
  1091. }
  1092. void visit_pre (const target_desc *e) override
  1093. {
  1094. gdb_printf (" Original: %s */\n\n",
  1095. lbasename (m_filename_after_features.c_str ()));
  1096. gdb_printf ("#include \"defs.h\"\n");
  1097. gdb_printf ("#include \"osabi.h\"\n");
  1098. gdb_printf ("#include \"target-descriptions.h\"\n");
  1099. gdb_printf ("\n");
  1100. gdb_printf ("struct target_desc *tdesc_%s;\n", m_function);
  1101. gdb_printf ("static void\n");
  1102. gdb_printf ("initialize_tdesc_%s (void)\n", m_function);
  1103. gdb_printf ("{\n");
  1104. gdb_printf
  1105. (" target_desc_up result = allocate_target_description ();\n");
  1106. if (tdesc_architecture (e) != NULL)
  1107. {
  1108. gdb_printf
  1109. (" set_tdesc_architecture (result.get (), bfd_scan_arch (\"%s\"));\n",
  1110. tdesc_architecture (e)->printable_name);
  1111. gdb_printf ("\n");
  1112. }
  1113. if (tdesc_osabi (e) > GDB_OSABI_UNKNOWN
  1114. && tdesc_osabi (e) < GDB_OSABI_INVALID)
  1115. {
  1116. gdb_printf
  1117. (" set_tdesc_osabi (result.get (), osabi_from_tdesc_string (\"%s\"));\n",
  1118. gdbarch_osabi_name (tdesc_osabi (e)));
  1119. gdb_printf ("\n");
  1120. }
  1121. for (const tdesc_compatible_info_up &compatible : e->compatible)
  1122. gdb_printf
  1123. (" tdesc_add_compatible (result.get (), bfd_scan_arch (\"%s\"));\n",
  1124. compatible->arch ()->printable_name);
  1125. if (!e->compatible.empty ())
  1126. gdb_printf ("\n");
  1127. for (const property &prop : e->properties)
  1128. gdb_printf (" set_tdesc_property (result.get (), \"%s\", \"%s\");\n",
  1129. prop.key.c_str (), prop.value.c_str ());
  1130. gdb_printf (" struct tdesc_feature *feature;\n");
  1131. }
  1132. void visit_pre (const tdesc_feature *e) override
  1133. {
  1134. gdb_printf ("\n feature = tdesc_create_feature (result.get (), \"%s\");\n",
  1135. e->name.c_str ());
  1136. }
  1137. void visit_post (const tdesc_feature *e) override
  1138. {}
  1139. void visit_post (const target_desc *e) override
  1140. {
  1141. gdb_printf ("\n tdesc_%s = result.release ();\n", m_function);
  1142. gdb_printf ("}\n");
  1143. }
  1144. void visit (const tdesc_type_builtin *type) override
  1145. {
  1146. error (_("C output is not supported type \"%s\"."), type->name.c_str ());
  1147. }
  1148. void visit (const tdesc_type_vector *type) override
  1149. {
  1150. if (!m_printed_element_type)
  1151. {
  1152. gdb_printf (" tdesc_type *element_type;\n");
  1153. m_printed_element_type = true;
  1154. }
  1155. gdb_printf
  1156. (" element_type = tdesc_named_type (feature, \"%s\");\n",
  1157. type->element_type->name.c_str ());
  1158. gdb_printf
  1159. (" tdesc_create_vector (feature, \"%s\", element_type, %d);\n",
  1160. type->name.c_str (), type->count);
  1161. gdb_printf ("\n");
  1162. }
  1163. void visit (const tdesc_type_with_fields *type) override
  1164. {
  1165. if (!m_printed_type_with_fields)
  1166. {
  1167. gdb_printf (" tdesc_type_with_fields *type_with_fields;\n");
  1168. m_printed_type_with_fields = true;
  1169. }
  1170. switch (type->kind)
  1171. {
  1172. case TDESC_TYPE_STRUCT:
  1173. case TDESC_TYPE_FLAGS:
  1174. if (type->kind == TDESC_TYPE_STRUCT)
  1175. {
  1176. gdb_printf
  1177. (" type_with_fields = tdesc_create_struct (feature, \"%s\");\n",
  1178. type->name.c_str ());
  1179. if (type->size != 0)
  1180. gdb_printf
  1181. (" tdesc_set_struct_size (type_with_fields, %d);\n", type->size);
  1182. }
  1183. else
  1184. {
  1185. gdb_printf
  1186. (" type_with_fields = tdesc_create_flags (feature, \"%s\", %d);\n",
  1187. type->name.c_str (), type->size);
  1188. }
  1189. for (const tdesc_type_field &f : type->fields)
  1190. {
  1191. const char *type_name;
  1192. gdb_assert (f.type != NULL);
  1193. type_name = f.type->name.c_str ();
  1194. /* To minimize changes to generated files, don't emit type
  1195. info for fields that have defaulted types. */
  1196. if (f.start != -1)
  1197. {
  1198. gdb_assert (f.end != -1);
  1199. if (f.type->kind == TDESC_TYPE_BOOL)
  1200. {
  1201. gdb_assert (f.start == f.end);
  1202. gdb_printf
  1203. (" tdesc_add_flag (type_with_fields, %d, \"%s\");\n",
  1204. f.start, f.name.c_str ());
  1205. }
  1206. else if ((type->size == 4 && f.type->kind == TDESC_TYPE_UINT32)
  1207. || (type->size == 8
  1208. && f.type->kind == TDESC_TYPE_UINT64))
  1209. {
  1210. gdb_printf
  1211. (" tdesc_add_bitfield (type_with_fields, \"%s\", %d, %d);\n",
  1212. f.name.c_str (), f.start, f.end);
  1213. }
  1214. else
  1215. {
  1216. printf_field_type_assignment
  1217. ("tdesc_named_type (feature, \"%s\");\n",
  1218. type_name);
  1219. gdb_printf
  1220. (" tdesc_add_typed_bitfield (type_with_fields, \"%s\","
  1221. " %d, %d, field_type);\n",
  1222. f.name.c_str (), f.start, f.end);
  1223. }
  1224. }
  1225. else /* Not a bitfield. */
  1226. {
  1227. gdb_assert (f.end == -1);
  1228. gdb_assert (type->kind == TDESC_TYPE_STRUCT);
  1229. printf_field_type_assignment
  1230. ("tdesc_named_type (feature, \"%s\");\n", type_name);
  1231. gdb_printf
  1232. (" tdesc_add_field (type_with_fields, \"%s\", field_type);\n",
  1233. f.name.c_str ());
  1234. }
  1235. }
  1236. break;
  1237. case TDESC_TYPE_UNION:
  1238. gdb_printf
  1239. (" type_with_fields = tdesc_create_union (feature, \"%s\");\n",
  1240. type->name.c_str ());
  1241. for (const tdesc_type_field &f : type->fields)
  1242. {
  1243. printf_field_type_assignment
  1244. ("tdesc_named_type (feature, \"%s\");\n", f.type->name.c_str ());
  1245. gdb_printf
  1246. (" tdesc_add_field (type_with_fields, \"%s\", field_type);\n",
  1247. f.name.c_str ());
  1248. }
  1249. break;
  1250. case TDESC_TYPE_ENUM:
  1251. gdb_printf
  1252. (" type_with_fields = tdesc_create_enum (feature, \"%s\", %d);\n",
  1253. type->name.c_str (), type->size);
  1254. for (const tdesc_type_field &f : type->fields)
  1255. gdb_printf
  1256. (" tdesc_add_enum_value (type_with_fields, %d, \"%s\");\n",
  1257. f.start, f.name.c_str ());
  1258. break;
  1259. default:
  1260. error (_("C output is not supported type \"%s\"."), type->name.c_str ());
  1261. }
  1262. gdb_printf ("\n");
  1263. }
  1264. void visit (const tdesc_reg *reg) override
  1265. {
  1266. gdb_printf (" tdesc_create_reg (feature, \"%s\", %ld, %d, ",
  1267. reg->name.c_str (), reg->target_regnum,
  1268. reg->save_restore);
  1269. if (!reg->group.empty ())
  1270. gdb_printf ("\"%s\", ", reg->group.c_str ());
  1271. else
  1272. gdb_printf ("NULL, ");
  1273. gdb_printf ("%d, \"%s\");\n", reg->bitsize, reg->type.c_str ());
  1274. }
  1275. protected:
  1276. std::string m_filename_after_features;
  1277. private:
  1278. /* Print an assignment to the field_type variable. Print the declaration
  1279. of field_type if that has not been done yet. */
  1280. ATTRIBUTE_PRINTF (2, 3)
  1281. void printf_field_type_assignment (const char *fmt, ...)
  1282. {
  1283. if (!m_printed_field_type)
  1284. {
  1285. gdb_printf (" tdesc_type *field_type;\n");
  1286. m_printed_field_type = true;
  1287. }
  1288. gdb_printf (" field_type = ");
  1289. va_list args;
  1290. va_start (args, fmt);
  1291. gdb_vprintf (fmt, args);
  1292. va_end (args);
  1293. }
  1294. char *m_function;
  1295. /* Did we print "struct tdesc_type *element_type;" yet? */
  1296. bool m_printed_element_type = false;
  1297. /* Did we print "struct tdesc_type_with_fields *element_type;" yet? */
  1298. bool m_printed_type_with_fields = false;
  1299. /* Did we print "struct tdesc_type *field_type;" yet? */
  1300. bool m_printed_field_type = false;
  1301. };
  1302. /* Print target description feature in C. */
  1303. class print_c_feature : public print_c_tdesc
  1304. {
  1305. public:
  1306. print_c_feature (std::string &file)
  1307. : print_c_tdesc (file)
  1308. {
  1309. /* Trim ".tmp". */
  1310. auto const pos = m_filename_after_features.find_last_of ('.');
  1311. m_filename_after_features = m_filename_after_features.substr (0, pos);
  1312. }
  1313. void visit_pre (const target_desc *e) override
  1314. {
  1315. gdb_printf (" Original: %s */\n\n",
  1316. lbasename (m_filename_after_features.c_str ()));
  1317. gdb_printf ("#include \"gdbsupport/tdesc.h\"\n");
  1318. gdb_printf ("\n");
  1319. }
  1320. void visit_post (const target_desc *e) override
  1321. {}
  1322. void visit_pre (const tdesc_feature *e) override
  1323. {
  1324. std::string name (m_filename_after_features);
  1325. auto pos = name.find_first_of ('.');
  1326. name = name.substr (0, pos);
  1327. std::replace (name.begin (), name.end (), '/', '_');
  1328. std::replace (name.begin (), name.end (), '-', '_');
  1329. gdb_printf ("static int\n");
  1330. gdb_printf ("create_feature_%s ", name.c_str ());
  1331. gdb_printf ("(struct target_desc *result, long regnum)\n");
  1332. gdb_printf ("{\n");
  1333. gdb_printf (" struct tdesc_feature *feature;\n");
  1334. gdb_printf
  1335. ("\n feature = tdesc_create_feature (result, \"%s\");\n",
  1336. e->name.c_str ());
  1337. }
  1338. void visit_post (const tdesc_feature *e) override
  1339. {
  1340. gdb_printf (" return regnum;\n");
  1341. gdb_printf ("}\n");
  1342. }
  1343. void visit (const tdesc_reg *reg) override
  1344. {
  1345. /* Most "reg" in XML target descriptions don't have "regnum"
  1346. attribute, so the register number is allocated sequentially.
  1347. In case that reg has "regnum" attribute, register number
  1348. should be set by that explicitly. */
  1349. if (reg->target_regnum < m_next_regnum)
  1350. {
  1351. /* The integrity check, it can catch some errors on register
  1352. number collision, like this,
  1353. <reg name="x0" bitsize="32"/>
  1354. <reg name="x1" bitsize="32"/>
  1355. <reg name="x2" bitsize="32"/>
  1356. <reg name="x3" bitsize="32"/>
  1357. <reg name="ps" bitsize="32" regnum="3"/>
  1358. but it also has false negatives. The target description
  1359. below is correct,
  1360. <reg name="x1" bitsize="32" regnum="1"/>
  1361. <reg name="x3" bitsize="32" regnum="3"/>
  1362. <reg name="x2" bitsize="32" regnum="2"/>
  1363. <reg name="x4" bitsize="32" regnum="4"/>
  1364. but it is not a good practice, so still error on this,
  1365. and also print the message so that it can be saved in the
  1366. generated c file. */
  1367. gdb_printf ("ERROR: \"regnum\" attribute %ld ",
  1368. reg->target_regnum);
  1369. gdb_printf ("is not the largest number (%d).\n",
  1370. m_next_regnum);
  1371. error (_("\"regnum\" attribute %ld is not the largest number (%d)."),
  1372. reg->target_regnum, m_next_regnum);
  1373. }
  1374. if (reg->target_regnum > m_next_regnum)
  1375. {
  1376. gdb_printf (" regnum = %ld;\n", reg->target_regnum);
  1377. m_next_regnum = reg->target_regnum;
  1378. }
  1379. gdb_printf (" tdesc_create_reg (feature, \"%s\", regnum++, %d, ",
  1380. reg->name.c_str (), reg->save_restore);
  1381. if (!reg->group.empty ())
  1382. gdb_printf ("\"%s\", ", reg->group.c_str ());
  1383. else
  1384. gdb_printf ("NULL, ");
  1385. gdb_printf ("%d, \"%s\");\n", reg->bitsize, reg->type.c_str ());
  1386. m_next_regnum++;
  1387. }
  1388. private:
  1389. /* The register number to use for the next register we see. */
  1390. int m_next_regnum = 0;
  1391. };
  1392. /* See gdbsupport/tdesc.h. */
  1393. const char *
  1394. tdesc_get_features_xml (const target_desc *tdesc)
  1395. {
  1396. if (tdesc->xmltarget == nullptr)
  1397. {
  1398. std::string buffer ("@");
  1399. print_xml_feature v (&buffer);
  1400. tdesc->accept (v);
  1401. tdesc->xmltarget = xstrdup (buffer.c_str ());
  1402. }
  1403. return tdesc->xmltarget;
  1404. }
  1405. /* Data structures and functions to setup the option flags for 'maintenance
  1406. print c-tdesc command. */
  1407. struct maint_print_c_tdesc_options
  1408. {
  1409. /* True when the '-single-feature' flag was passed. */
  1410. bool single_feature = false;
  1411. };
  1412. using maint_print_c_tdesc_opt_def
  1413. = gdb::option::flag_option_def<maint_print_c_tdesc_options>;
  1414. static const gdb::option::option_def maint_print_c_tdesc_opt_defs[] = {
  1415. maint_print_c_tdesc_opt_def {
  1416. "single-feature",
  1417. [] (maint_print_c_tdesc_options *opt) { return &opt->single_feature; },
  1418. N_("Print C description of just a single feature.")
  1419. },
  1420. };
  1421. static inline gdb::option::option_def_group
  1422. make_maint_print_c_tdesc_options_def_group (maint_print_c_tdesc_options *opts)
  1423. {
  1424. return {{maint_print_c_tdesc_opt_defs}, opts};
  1425. }
  1426. /* Implement 'maintenance print c-tdesc' command. */
  1427. static void
  1428. maint_print_c_tdesc_cmd (const char *args, int from_tty)
  1429. {
  1430. const struct target_desc *tdesc;
  1431. const char *filename;
  1432. maint_print_c_tdesc_options opts;
  1433. auto grp = make_maint_print_c_tdesc_options_def_group (&opts);
  1434. gdb::option::process_options
  1435. (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, grp);
  1436. if (args == NULL)
  1437. {
  1438. /* Use the global target-supplied description, not the current
  1439. architecture's. This lets a GDB for one architecture generate C
  1440. for another architecture's description, even though the gdbarch
  1441. initialization code will reject the new description. */
  1442. target_desc_info *tdesc_info = get_tdesc_info (current_inferior ());
  1443. tdesc = tdesc_info->tdesc;
  1444. filename = tdesc_info->filename.data ();
  1445. }
  1446. else
  1447. {
  1448. /* Use the target description from the XML file. */
  1449. filename = args;
  1450. tdesc = file_read_description_xml (filename);
  1451. }
  1452. if (tdesc == NULL)
  1453. error (_("There is no target description to print."));
  1454. if (filename == NULL)
  1455. filename = "fetched from target";
  1456. std::string filename_after_features (filename);
  1457. auto loc = filename_after_features.rfind ("/features/");
  1458. if (loc != std::string::npos)
  1459. filename_after_features = filename_after_features.substr (loc + 10);
  1460. /* Print c files for target features instead of target descriptions,
  1461. because c files got from target features are more flexible than the
  1462. counterparts. */
  1463. if (opts.single_feature)
  1464. {
  1465. if (tdesc->features.size () != 1)
  1466. error (_("only target descriptions with 1 feature can be used "
  1467. "with -single-feature option"));
  1468. print_c_feature v (filename_after_features);
  1469. tdesc->accept (v);
  1470. }
  1471. else
  1472. {
  1473. print_c_tdesc v (filename_after_features);
  1474. tdesc->accept (v);
  1475. }
  1476. }
  1477. /* Completer for the "backtrace" command. */
  1478. static void
  1479. maint_print_c_tdesc_cmd_completer (struct cmd_list_element *ignore,
  1480. completion_tracker &tracker,
  1481. const char *text, const char *word)
  1482. {
  1483. auto grp = make_maint_print_c_tdesc_options_def_group (nullptr);
  1484. if (gdb::option::complete_options
  1485. (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, grp))
  1486. return;
  1487. word = advance_to_filename_complete_word_point (tracker, text);
  1488. filename_completer (ignore, tracker, text, word);
  1489. }
  1490. /* Implement the maintenance print xml-tdesc command. */
  1491. static void
  1492. maint_print_xml_tdesc_cmd (const char *args, int from_tty)
  1493. {
  1494. const struct target_desc *tdesc;
  1495. if (args == NULL)
  1496. {
  1497. /* Use the global target-supplied description, not the current
  1498. architecture's. This lets a GDB for one architecture generate XML
  1499. for another architecture's description, even though the gdbarch
  1500. initialization code will reject the new description. */
  1501. tdesc = get_tdesc_info (current_inferior ())->tdesc;
  1502. }
  1503. else
  1504. {
  1505. /* Use the target description from the XML file. */
  1506. tdesc = file_read_description_xml (args);
  1507. }
  1508. if (tdesc == NULL)
  1509. error (_("There is no target description to print."));
  1510. std::string buf;
  1511. print_xml_feature v (&buf);
  1512. tdesc->accept (v);
  1513. gdb_puts (buf.c_str ());
  1514. }
  1515. namespace selftests {
  1516. /* A reference target description, used for testing (see record_xml_tdesc). */
  1517. struct xml_test_tdesc
  1518. {
  1519. xml_test_tdesc (const char *name, std::unique_ptr<const target_desc> &&tdesc)
  1520. : name (name), tdesc (std::move (tdesc))
  1521. {}
  1522. const char *name;
  1523. std::unique_ptr<const target_desc> tdesc;
  1524. };
  1525. static std::vector<xml_test_tdesc> xml_tdesc;
  1526. #if GDB_SELF_TEST
  1527. /* See target-descriptions.h. */
  1528. void
  1529. record_xml_tdesc (const char *xml_file, const struct target_desc *tdesc)
  1530. {
  1531. xml_tdesc.emplace_back (xml_file, std::unique_ptr<const target_desc> (tdesc));
  1532. }
  1533. #endif
  1534. }
  1535. /* Test the conversion process of a target description to/from xml: Take a target
  1536. description TDESC, convert to xml, back to a description, and confirm the new
  1537. tdesc is identical to the original. */
  1538. static bool
  1539. maintenance_check_tdesc_xml_convert (const target_desc *tdesc, const char *name)
  1540. {
  1541. const char *xml = tdesc_get_features_xml (tdesc);
  1542. if (xml == nullptr || *xml != '@')
  1543. {
  1544. gdb_printf (_("Could not convert description for %s to xml.\n"),
  1545. name);
  1546. return false;
  1547. }
  1548. const target_desc *tdesc_trans = string_read_description_xml (xml + 1);
  1549. if (tdesc_trans == nullptr)
  1550. {
  1551. gdb_printf (_("Could not convert description for %s from xml.\n"),
  1552. name);
  1553. return false;
  1554. }
  1555. else if (*tdesc != *tdesc_trans)
  1556. {
  1557. gdb_printf (_("Converted description for %s does not match.\n"),
  1558. name);
  1559. return false;
  1560. }
  1561. return true;
  1562. }
  1563. /* Check that the target descriptions created dynamically by
  1564. architecture-specific code equal the descriptions created from XML files
  1565. found in the specified directory DIR. */
  1566. static void
  1567. maintenance_check_xml_descriptions (const char *dir, int from_tty)
  1568. {
  1569. if (dir == NULL)
  1570. error (_("Missing dir name"));
  1571. gdb::unique_xmalloc_ptr<char> dir1 (tilde_expand (dir));
  1572. std::string feature_dir (dir1.get ());
  1573. unsigned int failed = 0;
  1574. for (auto const &e : selftests::xml_tdesc)
  1575. {
  1576. std::string tdesc_xml = (feature_dir + SLASH_STRING + e.name);
  1577. const target_desc *tdesc
  1578. = file_read_description_xml (tdesc_xml.data ());
  1579. if (tdesc == NULL || *tdesc != *e.tdesc)
  1580. {
  1581. gdb_printf ( _("Descriptions for %s do not match.\n"), e.name);
  1582. failed++;
  1583. }
  1584. else if (!maintenance_check_tdesc_xml_convert (tdesc, e.name)
  1585. || !maintenance_check_tdesc_xml_convert (e.tdesc.get (), e.name))
  1586. failed++;
  1587. }
  1588. gdb_printf (_("Tested %lu XML files, %d failed\n"),
  1589. (long) selftests::xml_tdesc.size (), failed);
  1590. }
  1591. void _initialize_target_descriptions ();
  1592. void
  1593. _initialize_target_descriptions ()
  1594. {
  1595. cmd_list_element *cmd;
  1596. tdesc_data = gdbarch_data_register_pre_init (tdesc_data_init);
  1597. add_setshow_prefix_cmd ("tdesc", class_maintenance,
  1598. _("Set target description specific variables."),
  1599. _("Show target description specific variables."),
  1600. &tdesc_set_cmdlist, &tdesc_show_cmdlist,
  1601. &setlist, &showlist);
  1602. add_basic_prefix_cmd ("tdesc", class_maintenance, _("\
  1603. Unset target description specific variables."),
  1604. &tdesc_unset_cmdlist,
  1605. 0 /* allow-unknown */, &unsetlist);
  1606. add_setshow_filename_cmd ("filename", class_obscure,
  1607. &tdesc_filename_cmd_string,
  1608. _("\
  1609. Set the file to read for an XML target description."), _("\
  1610. Show the file to read for an XML target description."), _("\
  1611. When set, GDB will read the target description from a local\n\
  1612. file instead of querying the remote target."),
  1613. set_tdesc_filename_cmd,
  1614. show_tdesc_filename_cmd,
  1615. &tdesc_set_cmdlist, &tdesc_show_cmdlist);
  1616. add_cmd ("filename", class_obscure, unset_tdesc_filename_cmd, _("\
  1617. Unset the file to read for an XML target description.\n\
  1618. When unset, GDB will read the description from the target."),
  1619. &tdesc_unset_cmdlist);
  1620. auto grp = make_maint_print_c_tdesc_options_def_group (nullptr);
  1621. static std::string help_text
  1622. = gdb::option::build_help (_("\
  1623. Print the current target description as a C source file.\n\
  1624. Usage: maintenance print c-tdesc [OPTION] [FILENAME]\n\
  1625. \n\
  1626. Options:\n\
  1627. %OPTIONS%\n\
  1628. \n\
  1629. When FILENAME is not provided then print the current target\n\
  1630. description, otherwise an XML target description is read from\n\
  1631. FILENAME and printed as a C function.\n\
  1632. \n\
  1633. When '-single-feature' is used then the target description should\n\
  1634. contain a single feature and the generated C code will only create\n\
  1635. that feature within an already existing target_desc object."), grp);
  1636. cmd = add_cmd ("c-tdesc", class_maintenance, maint_print_c_tdesc_cmd,
  1637. help_text.c_str (), &maintenanceprintlist);
  1638. set_cmd_completer_handle_brkchars (cmd, maint_print_c_tdesc_cmd_completer);
  1639. cmd = add_cmd ("xml-tdesc", class_maintenance, maint_print_xml_tdesc_cmd, _("\
  1640. Print the current target description as an XML file."),
  1641. &maintenanceprintlist);
  1642. set_cmd_completer (cmd, filename_completer);
  1643. cmd = add_cmd ("xml-descriptions", class_maintenance,
  1644. maintenance_check_xml_descriptions, _("\
  1645. Check equality of GDB target descriptions and XML created descriptions.\n\
  1646. Check the target descriptions created in GDB equal the descriptions\n\
  1647. created from XML files in the directory.\n\
  1648. The parameter is the directory name."),
  1649. &maintenancechecklist);
  1650. set_cmd_completer (cmd, filename_completer);
  1651. }