xref: /netbsd-src/external/gpl3/gdb/dist/gdb/xml-support.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1 /* Helper routines for parsing XML using Expat.
2 
3    Copyright (C) 2006-2020 Free Software Foundation, Inc.
4 
5    This file is part of GDB.
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19 
20 #include "defs.h"
21 #include "gdbcmd.h"
22 #include "xml-builtin.h"
23 #include "xml-support.h"
24 #include "gdbsupport/filestuff.h"
25 #include "safe-ctype.h"
26 #include <vector>
27 #include <string>
28 
29 /* Debugging flag.  */
30 static bool debug_xml;
31 
32 /* The contents of this file are only useful if XML support is
33    available.  */
34 #ifdef HAVE_LIBEXPAT
35 
36 #include "gdb_expat.h"
37 
38 /* The maximum depth of <xi:include> nesting.  No need to be miserly,
39    we just want to avoid running out of stack on loops.  */
40 #define MAX_XINCLUDE_DEPTH 30
41 
42 /* Simplified XML parser infrastructure.  */
43 
44 /* A parsing level -- used to keep track of the current element
45    nesting.  */
46 struct scope_level
47 {
48   explicit scope_level (const gdb_xml_element *elements_ = NULL)
49     : elements (elements_),
50       element (NULL),
51       seen (0)
52   {}
53 
54   /* Elements we allow at this level.  */
55   const struct gdb_xml_element *elements;
56 
57   /* The element which we are within.  */
58   const struct gdb_xml_element *element;
59 
60   /* Mask of which elements we've seen at this level (used for
61      optional and repeatable checking).  */
62   unsigned int seen;
63 
64   /* Body text accumulation.  */
65   std::string body;
66 };
67 
68 /* The parser itself, and our additional state.  */
69 struct gdb_xml_parser
70 {
71   gdb_xml_parser (const char *name,
72 		  const gdb_xml_element *elements,
73 		  void *user_data);
74   ~gdb_xml_parser();
75 
76   /* Associate DTD_NAME, which must be the name of a compiled-in DTD,
77      with the parser.  */
78   void use_dtd (const char *dtd_name);
79 
80   /* Return the name of the expected / default DTD, if specified.  */
81   const char *dtd_name ()
82   { return m_dtd_name; }
83 
84   /* Invoke the parser on BUFFER.  BUFFER is the data to parse, which
85      should be NUL-terminated.
86 
87      The return value is 0 for success or -1 for error.  It may throw,
88      but only if something unexpected goes wrong during parsing; parse
89      errors will be caught, warned about, and reported as failure.  */
90   int parse (const char *buffer);
91 
92   /* Issue a debugging message.  */
93   void vdebug (const char *format, va_list ap)
94     ATTRIBUTE_PRINTF (2, 0);
95 
96   /* Issue an error message, and stop parsing.  */
97   void verror (const char *format, va_list ap)
98     ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (2, 0);
99 
100   void body_text (const XML_Char *text, int length);
101   void start_element (const XML_Char *name, const XML_Char **attrs);
102   void end_element (const XML_Char *name);
103 
104   /* Return the name of this parser.  */
105   const char *name ()
106   { return m_name; }
107 
108   /* Return the user's callback data, for handlers.  */
109   void *user_data ()
110   { return m_user_data; };
111 
112   /* Are we the special <xi:include> parser?  */
113   void set_is_xinclude (bool is_xinclude)
114   { m_is_xinclude = is_xinclude; }
115 
116   /* A thrown error, if any.  */
117   void set_error (gdb_exception &&error)
118   {
119     m_error = std::move (error);
120 #ifdef HAVE_XML_STOPPARSER
121     XML_StopParser (m_expat_parser, XML_FALSE);
122 #endif
123   }
124 
125   /* Return the underlying expat parser.  */
126   XML_Parser expat_parser ()
127   { return m_expat_parser; }
128 
129 private:
130   /* The underlying expat parser.  */
131   XML_Parser m_expat_parser;
132 
133   /* Name of this parser.  */
134   const char *m_name;
135 
136   /* The user's callback data, for handlers.  */
137   void *m_user_data;
138 
139   /* Scoping stack.  */
140   std::vector<scope_level> m_scopes;
141 
142 /* A thrown error, if any.  */
143   struct gdb_exception m_error;
144 
145   /* The line of the thrown error, or 0.  */
146   int m_last_line;
147 
148   /* The name of the expected / default DTD, if specified.  */
149   const char *m_dtd_name;
150 
151   /* Are we the special <xi:include> parser?  */
152   bool m_is_xinclude;
153 };
154 
155 /* Process some body text.  We accumulate the text for later use; it's
156    wrong to do anything with it immediately, because a single block of
157    text might be broken up into multiple calls to this function.  */
158 
159 void
160 gdb_xml_parser::body_text (const XML_Char *text, int length)
161 {
162   if (m_error.reason < 0)
163     return;
164 
165   scope_level &scope = m_scopes.back ();
166   scope.body.append (text, length);
167 }
168 
169 static void
170 gdb_xml_body_text (void *data, const XML_Char *text, int length)
171 {
172   struct gdb_xml_parser *parser = (struct gdb_xml_parser *) data;
173 
174   parser->body_text (text, length);
175 }
176 
177 /* Issue a debugging message from one of PARSER's handlers.  */
178 
179 void
180 gdb_xml_parser::vdebug (const char *format, va_list ap)
181 {
182   int line = XML_GetCurrentLineNumber (m_expat_parser);
183 
184   std::string message = string_vprintf (format, ap);
185   if (line)
186     fprintf_unfiltered (gdb_stderr, "%s (line %d): %s\n",
187 			m_name, line, message.c_str ());
188   else
189     fprintf_unfiltered (gdb_stderr, "%s: %s\n",
190 			m_name, message.c_str ());
191 }
192 
193 void
194 gdb_xml_debug (struct gdb_xml_parser *parser, const char *format, ...)
195 {
196   if (!debug_xml)
197     return;
198 
199   va_list ap;
200   va_start (ap, format);
201   parser->vdebug (format, ap);
202   va_end (ap);
203 }
204 
205 /* Issue an error message from one of PARSER's handlers, and stop
206    parsing.  */
207 
208 void
209 gdb_xml_parser::verror (const char *format, va_list ap)
210 {
211   int line = XML_GetCurrentLineNumber (m_expat_parser);
212 
213   m_last_line = line;
214   throw_verror (XML_PARSE_ERROR, format, ap);
215 }
216 
217 void
218 gdb_xml_error (struct gdb_xml_parser *parser, const char *format, ...)
219 {
220   va_list ap;
221   va_start (ap, format);
222   parser->verror (format, ap);
223   va_end (ap);
224 }
225 
226 /* Find the attribute named NAME in the set of parsed attributes
227    ATTRIBUTES.  Returns NULL if not found.  */
228 
229 struct gdb_xml_value *
230 xml_find_attribute (std::vector<gdb_xml_value> &attributes,
231 		    const char *name)
232 {
233   for (gdb_xml_value &value : attributes)
234     if (strcmp (value.name, name) == 0)
235       return &value;
236 
237   return NULL;
238 }
239 
240 /* Handle the start of an element.  NAME is the element, and ATTRS are
241    the names and values of this element's attributes.  */
242 
243 void
244 gdb_xml_parser::start_element (const XML_Char *name,
245 			       const XML_Char **attrs)
246 {
247   if (m_error.reason < 0)
248     return;
249 
250   const struct gdb_xml_element *element;
251   const struct gdb_xml_attribute *attribute;
252   unsigned int seen;
253 
254   /* Push an error scope.  If we return or throw an exception before
255      filling this in, it will tell us to ignore children of this
256      element.  Note we don't take a reference to the element yet
257      because further below we'll process the element which may recurse
258      back here and push more elements to the vector.  When the
259      recursion unrolls all such elements will have been popped back
260      already, but if one of those pushes reallocates the vector,
261      previous element references will be invalidated.  */
262   m_scopes.emplace_back ();
263 
264   /* Get a reference to the current scope.  */
265   scope_level &scope = m_scopes[m_scopes.size () - 2];
266 
267   gdb_xml_debug (this, _("Entering element <%s>"), name);
268 
269   /* Find this element in the list of the current scope's allowed
270      children.  Record that we've seen it.  */
271 
272   seen = 1;
273   for (element = scope.elements; element && element->name;
274        element++, seen <<= 1)
275     if (strcmp (element->name, name) == 0)
276       break;
277 
278   if (element == NULL || element->name == NULL)
279     {
280       /* If we're working on XInclude, <xi:include> can be the child
281 	 of absolutely anything.  Copy the previous scope's element
282 	 list into the new scope even if there was no match.  */
283       if (m_is_xinclude)
284 	{
285 	  XML_DefaultCurrent (m_expat_parser);
286 
287 	  scope_level &unknown_scope = m_scopes.back ();
288 	  unknown_scope.elements = scope.elements;
289 	  return;
290 	}
291 
292       gdb_xml_debug (this, _("Element <%s> unknown"), name);
293       return;
294     }
295 
296   if (!(element->flags & GDB_XML_EF_REPEATABLE) && (seen & scope.seen))
297     gdb_xml_error (this, _("Element <%s> only expected once"), name);
298 
299   scope.seen |= seen;
300 
301   std::vector<gdb_xml_value> attributes;
302 
303   for (attribute = element->attributes;
304        attribute != NULL && attribute->name != NULL;
305        attribute++)
306     {
307       const char *val = NULL;
308       const XML_Char **p;
309       void *parsed_value;
310 
311       for (p = attrs; *p != NULL; p += 2)
312 	if (!strcmp (attribute->name, p[0]))
313 	  {
314 	    val = p[1];
315 	    break;
316 	  }
317 
318       if (*p != NULL && val == NULL)
319 	{
320 	  gdb_xml_debug (this, _("Attribute \"%s\" missing a value"),
321 			 attribute->name);
322 	  continue;
323 	}
324 
325       if (*p == NULL && !(attribute->flags & GDB_XML_AF_OPTIONAL))
326 	{
327 	  gdb_xml_error (this, _("Required attribute \"%s\" of "
328 				   "<%s> not specified"),
329 			 attribute->name, element->name);
330 	  continue;
331 	}
332 
333       if (*p == NULL)
334 	continue;
335 
336       gdb_xml_debug (this, _("Parsing attribute %s=\"%s\""),
337 		     attribute->name, val);
338 
339       if (attribute->handler)
340 	parsed_value = attribute->handler (this, attribute, val);
341       else
342 	parsed_value = xstrdup (val);
343 
344       attributes.emplace_back (attribute->name, parsed_value);
345     }
346 
347   /* Check for unrecognized attributes.  */
348   if (debug_xml)
349     {
350       const XML_Char **p;
351 
352       for (p = attrs; *p != NULL; p += 2)
353 	{
354 	  for (attribute = element->attributes;
355 	       attribute != NULL && attribute->name != NULL;
356 	       attribute++)
357 	    if (strcmp (attribute->name, *p) == 0)
358 	      break;
359 
360 	  if (attribute == NULL || attribute->name == NULL)
361 	    gdb_xml_debug (this, _("Ignoring unknown attribute %s"), *p);
362 	}
363     }
364 
365   /* Call the element handler if there is one.  */
366   if (element->start_handler)
367     element->start_handler (this, element, m_user_data, attributes);
368 
369   /* Fill in a new scope level.  Note that we must delay getting a
370      back reference till here because above we might have recursed,
371      which may have reallocated the vector which invalidates
372      iterators/pointers/references.  */
373   scope_level &new_scope = m_scopes.back ();
374   new_scope.element = element;
375   new_scope.elements = element->children;
376 }
377 
378 /* Wrapper for gdb_xml_start_element, to prevent throwing exceptions
379    through expat.  */
380 
381 static void
382 gdb_xml_start_element_wrapper (void *data, const XML_Char *name,
383 			       const XML_Char **attrs)
384 {
385   struct gdb_xml_parser *parser = (struct gdb_xml_parser *) data;
386 
387   try
388     {
389       parser->start_element (name, attrs);
390     }
391   catch (gdb_exception &ex)
392     {
393       parser->set_error (std::move (ex));
394     }
395 }
396 
397 /* Handle the end of an element.  NAME is the current element.  */
398 
399 void
400 gdb_xml_parser::end_element (const XML_Char *name)
401 {
402   if (m_error.reason < 0)
403     return;
404 
405   struct scope_level *scope = &m_scopes.back ();
406   const struct gdb_xml_element *element;
407   unsigned int seen;
408 
409   gdb_xml_debug (this, _("Leaving element <%s>"), name);
410 
411   for (element = scope->elements, seen = 1;
412        element != NULL && element->name != NULL;
413        element++, seen <<= 1)
414     if ((scope->seen & seen) == 0
415 	&& (element->flags & GDB_XML_EF_OPTIONAL) == 0)
416       gdb_xml_error (this, _("Required element <%s> is missing"),
417 		     element->name);
418 
419   /* Call the element processor.  */
420   if (scope->element != NULL && scope->element->end_handler)
421     {
422       const char *body;
423 
424       if (scope->body.empty ())
425 	body = "";
426       else
427 	{
428 	  int length;
429 
430 	  length = scope->body.size ();
431 	  body = scope->body.c_str ();
432 
433 	  /* Strip leading and trailing whitespace.  */
434 	  while (length > 0 && ISSPACE (body[length - 1]))
435 	    length--;
436 	  scope->body.erase (length);
437 	  while (*body && ISSPACE (*body))
438 	    body++;
439 	}
440 
441       scope->element->end_handler (this, scope->element,
442 				   m_user_data, body);
443     }
444   else if (scope->element == NULL)
445     XML_DefaultCurrent (m_expat_parser);
446 
447   /* Pop the scope level.  */
448   m_scopes.pop_back ();
449 }
450 
451 /* Wrapper for gdb_xml_end_element, to prevent throwing exceptions
452    through expat.  */
453 
454 static void
455 gdb_xml_end_element_wrapper (void *data, const XML_Char *name)
456 {
457   struct gdb_xml_parser *parser = (struct gdb_xml_parser *) data;
458 
459   try
460     {
461       parser->end_element (name);
462     }
463   catch (gdb_exception &ex)
464     {
465       parser->set_error (std::move (ex));
466     }
467 }
468 
469 /* Free a parser and all its associated state.  */
470 
471 gdb_xml_parser::~gdb_xml_parser ()
472 {
473   XML_ParserFree (m_expat_parser);
474 }
475 
476 /* Initialize a parser.  */
477 
478 gdb_xml_parser::gdb_xml_parser (const char *name,
479 				const gdb_xml_element *elements,
480 				void *user_data)
481   : m_name (name),
482     m_user_data (user_data),
483     m_last_line (0),
484     m_dtd_name (NULL),
485     m_is_xinclude (false)
486 {
487   m_expat_parser = XML_ParserCreateNS (NULL, '!');
488   if (m_expat_parser == NULL)
489     malloc_failure (0);
490 
491   XML_SetUserData (m_expat_parser, this);
492 
493   /* Set the callbacks.  */
494   XML_SetElementHandler (m_expat_parser, gdb_xml_start_element_wrapper,
495 			 gdb_xml_end_element_wrapper);
496   XML_SetCharacterDataHandler (m_expat_parser, gdb_xml_body_text);
497 
498   /* Initialize the outer scope.  */
499   m_scopes.emplace_back (elements);
500 }
501 
502 /* External entity handler.  The only external entities we support
503    are those compiled into GDB (we do not fetch entities from the
504    target).  */
505 
506 static int XMLCALL
507 gdb_xml_fetch_external_entity (XML_Parser expat_parser,
508 			       const XML_Char *context,
509 			       const XML_Char *base,
510 			       const XML_Char *systemId,
511 			       const XML_Char *publicId)
512 {
513   XML_Parser entity_parser;
514   const char *text;
515   enum XML_Status status;
516 
517   if (systemId == NULL)
518     {
519       gdb_xml_parser *parser
520 	= (gdb_xml_parser *) XML_GetUserData (expat_parser);
521 
522       text = fetch_xml_builtin (parser->dtd_name ());
523       if (text == NULL)
524 	internal_error (__FILE__, __LINE__,
525 			_("could not locate built-in DTD %s"),
526 			parser->dtd_name ());
527     }
528   else
529     {
530       text = fetch_xml_builtin (systemId);
531       if (text == NULL)
532 	return XML_STATUS_ERROR;
533     }
534 
535   entity_parser = XML_ExternalEntityParserCreate (expat_parser,
536 						  context, NULL);
537 
538   /* Don't use our handlers for the contents of the DTD.  Just let expat
539      process it.  */
540   XML_SetElementHandler (entity_parser, NULL, NULL);
541   XML_SetDoctypeDeclHandler (entity_parser, NULL, NULL);
542   XML_SetXmlDeclHandler (entity_parser, NULL);
543   XML_SetDefaultHandler (entity_parser, NULL);
544   XML_SetUserData (entity_parser, NULL);
545 
546   status = XML_Parse (entity_parser, text, strlen (text), 1);
547 
548   XML_ParserFree (entity_parser);
549   return status;
550 }
551 
552 void
553 gdb_xml_parser::use_dtd (const char *dtd_name)
554 {
555   enum XML_Error err;
556 
557   m_dtd_name = dtd_name;
558 
559   XML_SetParamEntityParsing (m_expat_parser,
560 			     XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
561   XML_SetExternalEntityRefHandler (m_expat_parser,
562 				   gdb_xml_fetch_external_entity);
563 
564   /* Even if no DTD is provided, use the built-in DTD anyway.  */
565   err = XML_UseForeignDTD (m_expat_parser, XML_TRUE);
566   if (err != XML_ERROR_NONE)
567     internal_error (__FILE__, __LINE__,
568 		    _("XML_UseForeignDTD failed: %s"),
569 		    XML_ErrorString (err));
570 }
571 
572 /* Invoke PARSER on BUFFER.  BUFFER is the data to parse, which
573    should be NUL-terminated.
574 
575    The return value is 0 for success or -1 for error.  It may throw,
576    but only if something unexpected goes wrong during parsing; parse
577    errors will be caught, warned about, and reported as failure.  */
578 
579 int
580 gdb_xml_parser::parse (const char *buffer)
581 {
582   enum XML_Status status;
583   const char *error_string;
584 
585   gdb_xml_debug (this, _("Starting:\n%s"), buffer);
586 
587   status = XML_Parse (m_expat_parser, buffer, strlen (buffer), 1);
588 
589   if (status == XML_STATUS_OK && m_error.reason == 0)
590     return 0;
591 
592   if (m_error.reason == RETURN_ERROR
593       && m_error.error == XML_PARSE_ERROR)
594     {
595       gdb_assert (m_error.message != NULL);
596       error_string = m_error.what ();
597     }
598   else if (status == XML_STATUS_ERROR)
599     {
600       enum XML_Error err = XML_GetErrorCode (m_expat_parser);
601 
602       error_string = XML_ErrorString (err);
603     }
604   else
605     {
606       gdb_assert (m_error.reason < 0);
607       throw_exception (std::move (m_error));
608     }
609 
610   if (m_last_line != 0)
611     warning (_("while parsing %s (at line %d): %s"), m_name,
612 	     m_last_line, error_string);
613   else
614     warning (_("while parsing %s: %s"), m_name, error_string);
615 
616   return -1;
617 }
618 
619 int
620 gdb_xml_parse_quick (const char *name, const char *dtd_name,
621 		     const struct gdb_xml_element *elements,
622 		     const char *document, void *user_data)
623 {
624   gdb_xml_parser parser (name, elements, user_data);
625   if (dtd_name != NULL)
626     parser.use_dtd (dtd_name);
627   return parser.parse (document);
628 }
629 
630 /* Parse a field VALSTR that we expect to contain an integer value.
631    The integer is returned in *VALP.  The string is parsed with an
632    equivalent to strtoul.
633 
634    Returns 0 for success, -1 for error.  */
635 
636 static int
637 xml_parse_unsigned_integer (const char *valstr, ULONGEST *valp)
638 {
639   const char *endptr;
640   ULONGEST result;
641 
642   if (*valstr == '\0')
643     return -1;
644 
645   result = strtoulst (valstr, &endptr, 0);
646   if (*endptr != '\0')
647     return -1;
648 
649   *valp = result;
650   return 0;
651 }
652 
653 /* Parse an integer string into a ULONGEST and return it, or call
654    gdb_xml_error if it could not be parsed.  */
655 
656 ULONGEST
657 gdb_xml_parse_ulongest (struct gdb_xml_parser *parser, const char *value)
658 {
659   ULONGEST result;
660 
661   if (xml_parse_unsigned_integer (value, &result) != 0)
662     gdb_xml_error (parser, _("Can't convert \"%s\" to an integer"), value);
663 
664   return result;
665 }
666 
667 /* Parse an integer attribute into a ULONGEST.  */
668 
669 void *
670 gdb_xml_parse_attr_ulongest (struct gdb_xml_parser *parser,
671 			     const struct gdb_xml_attribute *attribute,
672 			     const char *value)
673 {
674   ULONGEST result;
675   void *ret;
676 
677   if (xml_parse_unsigned_integer (value, &result) != 0)
678     gdb_xml_error (parser, _("Can't convert %s=\"%s\" to an integer"),
679 		   attribute->name, value);
680 
681   ret = XNEW (ULONGEST);
682   memcpy (ret, &result, sizeof (result));
683   return ret;
684 }
685 
686 /* A handler_data for yes/no boolean values.  */
687 
688 const struct gdb_xml_enum gdb_xml_enums_boolean[] = {
689   { "yes", 1 },
690   { "no", 0 },
691   { NULL, 0 }
692 };
693 
694 /* Map NAME to VALUE.  A struct gdb_xml_enum * should be saved as the
695    value of handler_data when using gdb_xml_parse_attr_enum to parse a
696    fixed list of possible strings.  The list is terminated by an entry
697    with NAME == NULL.  */
698 
699 void *
700 gdb_xml_parse_attr_enum (struct gdb_xml_parser *parser,
701 			 const struct gdb_xml_attribute *attribute,
702 			 const char *value)
703 {
704   const struct gdb_xml_enum *enums
705     = (const struct gdb_xml_enum *) attribute->handler_data;
706   void *ret;
707 
708   for (enums = (const struct gdb_xml_enum *) attribute->handler_data;
709        enums->name != NULL; enums++)
710     if (strcasecmp (enums->name, value) == 0)
711       break;
712 
713   if (enums->name == NULL)
714     gdb_xml_error (parser, _("Unknown attribute value %s=\"%s\""),
715 		 attribute->name, value);
716 
717   ret = xmalloc (sizeof (enums->value));
718   memcpy (ret, &enums->value, sizeof (enums->value));
719   return ret;
720 }
721 
722 
723 /* XInclude processing.  This is done as a separate step from actually
724    parsing the document, so that we can produce a single combined XML
725    document - e.g. to hand to a front end or to simplify comparing two
726    documents.  We make extensive use of XML_DefaultCurrent, to pass
727    input text directly into the output without reformatting or
728    requoting it.
729 
730    We output the DOCTYPE declaration for the first document unchanged,
731    if present, and discard DOCTYPEs from included documents.  Only the
732    one we pass through here is used when we feed the result back to
733    expat.  The XInclude standard explicitly does not discuss
734    validation of the result; we choose to apply the same DTD applied
735    to the outermost document.
736 
737    We can not simply include the external DTD subset in the document
738    as an internal subset, because <!IGNORE> and <!INCLUDE> are valid
739    only in external subsets.  But if we do not pass the DTD into the
740    output at all, default values will not be filled in.
741 
742    We don't pass through any <?xml> declaration because we generate
743    UTF-8, not whatever the input encoding was.  */
744 
745 struct xinclude_parsing_data
746 {
747   xinclude_parsing_data (std::string &output_,
748 			 xml_fetch_another fetcher_, void *fetcher_baton_,
749 			 int include_depth_)
750     : output (output_),
751       skip_depth (0),
752       include_depth (include_depth_),
753       fetcher (fetcher_),
754       fetcher_baton (fetcher_baton_)
755   {}
756 
757   /* Where the output goes.  */
758   std::string &output;
759 
760   /* A count indicating whether we are in an element whose
761      children should not be copied to the output, and if so,
762      how deep we are nested.  This is used for anything inside
763      an xi:include, and for the DTD.  */
764   int skip_depth;
765 
766   /* The number of <xi:include> elements currently being processed,
767      to detect loops.  */
768   int include_depth;
769 
770   /* A function to call to obtain additional features, and its
771      baton.  */
772   xml_fetch_another fetcher;
773   void *fetcher_baton;
774 };
775 
776 static void
777 xinclude_start_include (struct gdb_xml_parser *parser,
778 			const struct gdb_xml_element *element,
779 			void *user_data,
780 			std::vector<gdb_xml_value> &attributes)
781 {
782   struct xinclude_parsing_data *data
783     = (struct xinclude_parsing_data *) user_data;
784   char *href = (char *) xml_find_attribute (attributes, "href")->value.get ();
785 
786   gdb_xml_debug (parser, _("Processing XInclude of \"%s\""), href);
787 
788   if (data->include_depth > MAX_XINCLUDE_DEPTH)
789     gdb_xml_error (parser, _("Maximum XInclude depth (%d) exceeded"),
790 		   MAX_XINCLUDE_DEPTH);
791 
792   gdb::optional<gdb::char_vector> text
793     = data->fetcher (href, data->fetcher_baton);
794   if (!text)
795     gdb_xml_error (parser, _("Could not load XML document \"%s\""), href);
796 
797   if (!xml_process_xincludes (data->output, parser->name (),
798 			      text->data (), data->fetcher,
799 			      data->fetcher_baton,
800 			      data->include_depth + 1))
801     gdb_xml_error (parser, _("Parsing \"%s\" failed"), href);
802 
803   data->skip_depth++;
804 }
805 
806 static void
807 xinclude_end_include (struct gdb_xml_parser *parser,
808 		      const struct gdb_xml_element *element,
809 		      void *user_data, const char *body_text)
810 {
811   struct xinclude_parsing_data *data
812     = (struct xinclude_parsing_data *) user_data;
813 
814   data->skip_depth--;
815 }
816 
817 static void XMLCALL
818 xml_xinclude_default (void *data_, const XML_Char *s, int len)
819 {
820   struct gdb_xml_parser *parser = (struct gdb_xml_parser *) data_;
821   xinclude_parsing_data *data = (xinclude_parsing_data *) parser->user_data ();
822 
823   /* If we are inside of e.g. xi:include or the DTD, don't save this
824      string.  */
825   if (data->skip_depth)
826     return;
827 
828   /* Otherwise just add it to the end of the document we're building
829      up.  */
830   data->output.append (s, len);
831 }
832 
833 static void XMLCALL
834 xml_xinclude_start_doctype (void *data_, const XML_Char *doctypeName,
835 			    const XML_Char *sysid, const XML_Char *pubid,
836 			    int has_internal_subset)
837 {
838   struct gdb_xml_parser *parser = (struct gdb_xml_parser *) data_;
839   xinclude_parsing_data *data = (xinclude_parsing_data *) parser->user_data ();
840 
841   /* Don't print out the doctype, or the contents of the DTD internal
842      subset, if any.  */
843   data->skip_depth++;
844 }
845 
846 static void XMLCALL
847 xml_xinclude_end_doctype (void *data_)
848 {
849   struct gdb_xml_parser *parser = (struct gdb_xml_parser *) data_;
850   xinclude_parsing_data *data = (xinclude_parsing_data *) parser->user_data ();
851 
852   data->skip_depth--;
853 }
854 
855 static void XMLCALL
856 xml_xinclude_xml_decl (void *data_, const XML_Char *version,
857 		       const XML_Char *encoding, int standalone)
858 {
859   /* Do nothing - this function prevents the default handler from
860      being called, thus suppressing the XML declaration from the
861      output.  */
862 }
863 
864 const struct gdb_xml_attribute xinclude_attributes[] = {
865   { "href", GDB_XML_AF_NONE, NULL, NULL },
866   { NULL, GDB_XML_AF_NONE, NULL, NULL }
867 };
868 
869 const struct gdb_xml_element xinclude_elements[] = {
870   { "http://www.w3.org/2001/XInclude!include", xinclude_attributes, NULL,
871     GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
872     xinclude_start_include, xinclude_end_include },
873   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
874 };
875 
876 /* The main entry point for <xi:include> processing.  */
877 
878 bool
879 xml_process_xincludes (std::string &result,
880 		       const char *name, const char *text,
881 		       xml_fetch_another fetcher, void *fetcher_baton,
882 		       int depth)
883 {
884   xinclude_parsing_data data (result, fetcher, fetcher_baton, depth);
885 
886   gdb_xml_parser parser (name, xinclude_elements, &data);
887   parser.set_is_xinclude (true);
888 
889   XML_SetCharacterDataHandler (parser.expat_parser (), NULL);
890   XML_SetDefaultHandler (parser.expat_parser (), xml_xinclude_default);
891 
892   /* Always discard the XML version declarations; the only important
893      thing this provides is encoding, and our result will have been
894      converted to UTF-8.  */
895   XML_SetXmlDeclHandler (parser.expat_parser (), xml_xinclude_xml_decl);
896 
897   if (depth > 0)
898     /* Discard the doctype for included documents.  */
899     XML_SetDoctypeDeclHandler (parser.expat_parser (),
900 			       xml_xinclude_start_doctype,
901 			       xml_xinclude_end_doctype);
902 
903   parser.use_dtd ("xinclude.dtd");
904 
905   if (parser.parse (text) == 0)
906     {
907       if (depth == 0)
908 	gdb_xml_debug (&parser, _("XInclude processing succeeded."));
909       return true;
910     }
911 
912   return false;
913 }
914 #endif /* HAVE_LIBEXPAT */
915 
916 
917 /* Return an XML document which was compiled into GDB, from
918    the given FILENAME, or NULL if the file was not compiled in.  */
919 
920 const char *
921 fetch_xml_builtin (const char *filename)
922 {
923   const char *const (*p)[2];
924 
925   for (p = xml_builtin; (*p)[0]; p++)
926     if (strcmp ((*p)[0], filename) == 0)
927       return (*p)[1];
928 
929   return NULL;
930 }
931 
932 /* A to_xfer_partial helper function which reads XML files which were
933    compiled into GDB.  The target may call this function from its own
934    to_xfer_partial handler, after converting object and annex to the
935    appropriate filename.  */
936 
937 LONGEST
938 xml_builtin_xfer_partial (const char *filename,
939 			  gdb_byte *readbuf, const gdb_byte *writebuf,
940 			  ULONGEST offset, LONGEST len)
941 {
942   const char *buf;
943   LONGEST len_avail;
944 
945   gdb_assert (readbuf != NULL && writebuf == NULL);
946   gdb_assert (filename != NULL);
947 
948   buf = fetch_xml_builtin (filename);
949   if (buf == NULL)
950     return -1;
951 
952   len_avail = strlen (buf);
953   if (offset >= len_avail)
954     return 0;
955 
956   if (len > len_avail - offset)
957     len = len_avail - offset;
958   memcpy (readbuf, buf + offset, len);
959   return len;
960 }
961 
962 
963 static void
964 show_debug_xml (struct ui_file *file, int from_tty,
965 		struct cmd_list_element *c, const char *value)
966 {
967   fprintf_filtered (file, _("XML debugging is %s.\n"), value);
968 }
969 
970 gdb::optional<gdb::char_vector>
971 xml_fetch_content_from_file (const char *filename, void *baton)
972 {
973   const char *dirname = (const char *) baton;
974   gdb_file_up file;
975 
976   if (dirname && *dirname)
977     {
978       char *fullname = concat (dirname, "/", filename, (char *) NULL);
979 
980       file = gdb_fopen_cloexec (fullname, FOPEN_RB);
981       xfree (fullname);
982     }
983   else
984     file = gdb_fopen_cloexec (filename, FOPEN_RB);
985 
986   if (file == NULL)
987     return {};
988 
989   /* Read in the whole file.  */
990 
991   size_t len;
992 
993   if (fseek (file.get (), 0, SEEK_END) == -1)
994     perror_with_name (_("seek to end of file"));
995   len = ftell (file.get ());
996   rewind (file.get ());
997 
998   gdb::char_vector text (len + 1);
999 
1000   if (fread (text.data (), 1, len, file.get ()) != len
1001       || ferror (file.get ()))
1002     {
1003       warning (_("Read error from \"%s\""), filename);
1004       return {};
1005     }
1006 
1007   text.back () = '\0';
1008   return text;
1009 }
1010 
1011 void _initialize_xml_support ();
1012 void _initialize_xml_support ();
1013 void
1014 _initialize_xml_support ()
1015 {
1016   add_setshow_boolean_cmd ("xml", class_maintenance, &debug_xml,
1017 			   _("Set XML parser debugging."),
1018 			   _("Show XML parser debugging."),
1019 			   _("When set, debugging messages for XML parsers "
1020 			     "are displayed."),
1021 			   NULL, show_debug_xml,
1022 			   &setdebuglist, &showdebuglist);
1023 }
1024