|
| 1 | +use super::prelude::*; |
| 2 | +use crate::common::Endianness; |
| 3 | +use crate::common::Pid; |
| 4 | +use crate::protocol::commands::ext::HostInfo; |
| 5 | +use crate::protocol::commands::ext::ProcessInfo; |
| 6 | +use crate::protocol::ResponseWriterError; |
| 7 | +use crate::target::ext::host_info::HostInfoResponse; |
| 8 | +use crate::target::ext::process_info::ProcessInfoResponse; |
| 9 | + |
| 10 | +pub(crate) enum InfoResponse<'a> { |
| 11 | + Pid(Pid), |
| 12 | + Triple(&'a str), |
| 13 | + Endianness(Endianness), |
| 14 | + PointerSize(usize), |
| 15 | +} |
| 16 | + |
| 17 | +impl<'a> InfoResponse<'a> { |
| 18 | + pub(crate) fn write_response<C: Connection>( |
| 19 | + &self, |
| 20 | + res: &mut ResponseWriter<'_, C>, |
| 21 | + ) -> Result<(), ResponseWriterError<C::Error>> { |
| 22 | + match self { |
| 23 | + InfoResponse::Pid(pid) => { |
| 24 | + res.write_str("pid:")?; |
| 25 | + res.write_dec(usize::from(*pid))?; |
| 26 | + } |
| 27 | + InfoResponse::Triple(triple) => { |
| 28 | + res.write_str("triple:")?; |
| 29 | + res.write_hex_buf(triple.as_bytes())?; |
| 30 | + } |
| 31 | + InfoResponse::Endianness(endian) => { |
| 32 | + res.write_str("endian:")?; |
| 33 | + res.write_str(match endian { |
| 34 | + Endianness::Big => "big;", |
| 35 | + Endianness::Little => "little;", |
| 36 | + })?; |
| 37 | + } |
| 38 | + InfoResponse::PointerSize(p) => { |
| 39 | + res.write_str("ptrsize:")?; |
| 40 | + res.write_dec(*p)?; |
| 41 | + } |
| 42 | + } |
| 43 | + res.write_str(";")?; |
| 44 | + Ok(()) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +impl<'a> From<&HostInfoResponse<'a>> for InfoResponse<'a> { |
| 49 | + fn from(resp: &HostInfoResponse<'a>) -> Self { |
| 50 | + match *resp { |
| 51 | + HostInfoResponse::Triple(s) => InfoResponse::Triple(s), |
| 52 | + HostInfoResponse::Endianness(e) => InfoResponse::Endianness(e), |
| 53 | + HostInfoResponse::PointerSize(p) => InfoResponse::PointerSize(p), |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +impl<'a> From<&ProcessInfoResponse<'a>> for InfoResponse<'a> { |
| 59 | + fn from(resp: &ProcessInfoResponse<'a>) -> Self { |
| 60 | + match *resp { |
| 61 | + ProcessInfoResponse::Pid(pid) => InfoResponse::Pid(pid), |
| 62 | + ProcessInfoResponse::Triple(s) => InfoResponse::Triple(s), |
| 63 | + ProcessInfoResponse::Endianness(e) => InfoResponse::Endianness(e), |
| 64 | + ProcessInfoResponse::PointerSize(p) => InfoResponse::PointerSize(p), |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +impl<T: Target, C: Connection> GdbStubImpl<T, C> { |
| 70 | + pub(crate) fn handle_process_info( |
| 71 | + &mut self, |
| 72 | + res: &mut ResponseWriter<'_, C>, |
| 73 | + target: &mut T, |
| 74 | + command: ProcessInfo, |
| 75 | + ) -> Result<HandlerStatus, Error<T::Error, C::Error>> { |
| 76 | + let ops = match target.support_process_info() { |
| 77 | + Some(ops) => ops, |
| 78 | + None => return Ok(HandlerStatus::Handled), |
| 79 | + }; |
| 80 | + |
| 81 | + crate::__dead_code_marker!("process_info", "impl"); |
| 82 | + |
| 83 | + let mut result = Ok(()); |
| 84 | + let mut write_info = |info: &ProcessInfoResponse<'_>| { |
| 85 | + if result.is_ok() { |
| 86 | + if let Err(e) = InfoResponse::from(info).write_response(res) { |
| 87 | + result = Err(e); |
| 88 | + } |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + match command { |
| 93 | + ProcessInfo::qProcessInfo(_cmd) => { |
| 94 | + ops.process_info(&mut write_info) |
| 95 | + .map_err(Error::TargetError)?; |
| 96 | + result?; |
| 97 | + } |
| 98 | + }; |
| 99 | + |
| 100 | + Ok(HandlerStatus::Handled) |
| 101 | + } |
| 102 | + |
| 103 | + pub(crate) fn handle_host_info( |
| 104 | + &mut self, |
| 105 | + res: &mut ResponseWriter<'_, C>, |
| 106 | + target: &mut T, |
| 107 | + command: HostInfo, |
| 108 | + ) -> Result<HandlerStatus, Error<T::Error, C::Error>> { |
| 109 | + let ops = match target.support_host_info() { |
| 110 | + Some(ops) => ops, |
| 111 | + None => return Ok(HandlerStatus::Handled), |
| 112 | + }; |
| 113 | + |
| 114 | + crate::__dead_code_marker!("host_info", "impl"); |
| 115 | + |
| 116 | + let mut result = Ok(()); |
| 117 | + let mut write_info = |info: &HostInfoResponse<'_>| { |
| 118 | + if result.is_ok() { |
| 119 | + if let Err(e) = InfoResponse::from(info).write_response(res) { |
| 120 | + result = Err(e); |
| 121 | + } |
| 122 | + } |
| 123 | + }; |
| 124 | + |
| 125 | + match command { |
| 126 | + HostInfo::qHostInfo(_cmd) => { |
| 127 | + ops.host_info(&mut write_info).map_err(Error::TargetError)?; |
| 128 | + result?; |
| 129 | + } |
| 130 | + }; |
| 131 | + |
| 132 | + Ok(HandlerStatus::Handled) |
| 133 | + } |
| 134 | +} |
0 commit comments