1 use super::prelude::*;
2 use crate::arch::Arch;
3 use crate::protocol::commands::ext::TargetXml;
4 
5 impl<T: Target, C: Connection> GdbStubImpl<T, C> {
handle_target_xml( &mut self, res: &mut ResponseWriter<'_, C>, target: &mut T, command: TargetXml<'_>, ) -> Result<HandlerStatus, Error<T::Error, C::Error>>6     pub(crate) fn handle_target_xml(
7         &mut self,
8         res: &mut ResponseWriter<'_, C>,
9         target: &mut T,
10         command: TargetXml<'_>,
11     ) -> Result<HandlerStatus, Error<T::Error, C::Error>> {
12         if !target.use_target_description_xml() {
13             return Ok(HandlerStatus::Handled);
14         }
15 
16         let handler_status = match command {
17             TargetXml::qXferFeaturesRead(cmd) => {
18                 let ret = if let Some(ops) = target.support_target_description_xml_override() {
19                     ops.target_description_xml(cmd.annex.name, cmd.offset, cmd.length, cmd.buf)
20                         .handle_error()?
21                 } else if let Some(xml) = T::Arch::target_description_xml() {
22                     if cmd.annex.name != b"target.xml" {
23                         // TODO: not the best error... should probably report to the user the
24                         // <xi:include> isn't supported at the Arch level (yet)
25                         return Err(Error::PacketUnexpected);
26                     }
27 
28                     let xml = xml.trim().as_bytes();
29                     let xml_len = xml.len();
30 
31                     let start = xml_len.min(cmd.offset as usize);
32                     let end = xml_len.min((cmd.offset as usize).saturating_add(cmd.length));
33 
34                     // LLVM isn't smart enough to realize that `start <= end`, and fails to elide a
35                     // `slice_end_index_len_fail` check unless we include this seemingly useless
36                     // call to `min`.
37                     let data = &xml[start.min(end)..end];
38 
39                     let n = data.len().min(cmd.buf.len());
40                     cmd.buf[..n].copy_from_slice(&data[..n]);
41                     n
42                 } else {
43                     // If the target hasn't provided their own XML, then the initial response to
44                     // "qSupported" wouldn't have included "qXfer:features:read", and gdb wouldn't
45                     // send this packet unless it was explicitly marked as supported.
46                     return Err(Error::PacketUnexpected);
47                 };
48 
49                 if ret == 0 {
50                     res.write_str("l")?;
51                 } else {
52                     res.write_str("m")?;
53                     // TODO: add more specific error variant?
54                     res.write_binary(cmd.buf.get(..ret).ok_or(Error::PacketBufferOverflow)?)?;
55                 }
56                 HandlerStatus::Handled
57             }
58         };
59 
60         Ok(handler_status)
61     }
62 }
63