Skip to content

Commit

Permalink
split qTBuffer/QTBuffer, fix trace explanations
Browse files Browse the repository at this point in the history
  • Loading branch information
cczetier committed Jan 6, 2025
1 parent 943b7f4 commit 93a789d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 51 deletions.
3 changes: 2 additions & 1 deletion src/protocol/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,11 +343,12 @@ commands! {
tracepoints use 'a {
"QTDP" => _QTDP::QTDP<'a>,
"QTinit" => _QTinit::QTinit,
"QTBuffer" => _QTBuffer::QTBuffer<'a>,
"QTBuffer" => _QTBuffer::QTBuffer,
"QTStart" => _QTStart::QTStart,
"QTStop" => _QTStop::QTStop,
"QTFrame" => _QTFrame::QTFrame<'a>,

"qTBuffer" => _qTBuffer::qTBuffer<'a>,
"qTStatus" => _qTStatus::qTStatus,
"qTP" => _qTP::qTP<'a>,
"qTfP" => _qTfP::qTfP,
Expand Down
40 changes: 12 additions & 28 deletions src/protocol/commands/_QTBuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,33 @@ use super::prelude::*;
use crate::target::ext::tracepoints::{BufferShape, TraceBuffer};

#[derive(Debug)]
pub enum QTBuffer<'a>
{
Request { offset: u64, length: usize, data: &'a mut [u8] },
Configure { buffer: TraceBuffer },
}
pub struct QTBuffer(pub TraceBuffer);

impl<'a> ParseCommand<'a> for QTBuffer<'a> {
impl ParseCommand<'_> for QTBuffer {
#[inline(always)]
fn from_packet(buf: PacketBuf<'a>) -> Option<Self> {
let (buf, body_range) = buf.into_raw_buf();
let body = &buf[body_range];
match body {
fn from_packet(buf: PacketBuf<'_>) -> Option<Self> {
match buf.into_body() {
[b':', body @ ..] => {
let mut s = body.split(|b| *b == b':');
let mut s = body.splitn_mut(2, |b| *b == b':');
let opt = s.next()?;
Some(match opt.as_ref() {
match opt.as_ref() {
b"circular" => {
let shape = s.next()?;
QTBuffer::Configure { buffer: TraceBuffer::Shape(match shape {
Some(QTBuffer(TraceBuffer::Shape(match shape {
[b'1'] => Some(BufferShape::Circular),
[b'0'] => Some(BufferShape::Linear),
_ => None,
}?)}
}?)))
},
b"size" => {
let size = s.next()?;
QTBuffer::Configure { buffer: TraceBuffer::Size(match size {
Some(QTBuffer(TraceBuffer::Size(match size {
[b'-', b'1'] => None,
i => Some(decode_hex(i).ok()?)
})}
},
req => {
let mut req_opts = req.split(|b| *b == b',');
let (offset, length) = (req_opts.next()?, req_opts.next()?);
let offset = decode_hex(offset).ok()?;
let length = decode_hex(length).ok()?;
// Our response has to be a hex encoded buffer that fits within
// our packet size, which means we actually have half as much space
// as our slice would indicate.
let (front, _back) = buf.split_at_mut(buf.len() / 2);
QTBuffer::Request { offset, length, data: front }
})))
},
})
_ => None,
}

},
_ => None,
Expand Down
30 changes: 30 additions & 0 deletions src/protocol/commands/_qTBuffer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use super::prelude::*;

#[derive(Debug)]
pub struct qTBuffer<'a> {
pub offset: u64,
pub length: usize,
pub data: &'a mut [u8]
}

impl<'a> ParseCommand<'a> for qTBuffer<'a> {
#[inline(always)]
fn from_packet(buf: PacketBuf<'a>) -> Option<Self> {
let (buf, body_range) = buf.into_raw_buf();
let body = &buf[body_range];
match body {
[b':', body @ ..] => {
let mut req_opts = body.split(|b| *b == b',');
let (offset, length) = (req_opts.next()?, req_opts.next()?);
let offset = decode_hex(offset).ok()?;
let length = decode_hex(length).ok()?;
// Our response has to be a hex encoded buffer that fits within
// our packet size, which means we actually have half as much space
// as our slice would indicate.
let (front, _back) = buf.split_at_mut(buf.len() / 2);
Some(qTBuffer { offset, length, data: front })
},
_ => None
}
}
}
37 changes: 15 additions & 22 deletions src/stub/core_impl/tracepoints.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::prelude::*;
use crate::arch::Arch;
use crate::internal::BeBytes;
use crate::protocol::commands::_QTBuffer::QTBuffer;
use crate::protocol::commands::_qTBuffer::qTBuffer;
use crate::protocol::commands::ext::Tracepoints;
use crate::protocol::commands::prelude::decode_hex;
use crate::protocol::commands::prelude::decode_hex_buf;
Expand Down Expand Up @@ -162,7 +162,7 @@ impl<T: Target, C: Connection> GdbStubImpl<T, C> {
status.write(res)?;
let mut err = None;
ops.trace_experiment_info(&mut |explanation: ExperimentExplanation<'_>| {
if let Err(e) = explanation.write(res) {
if let Err(e) = res.write_str(";").and_then(|()| explanation.write(res)) {
err = Some(e)
}
})
Expand Down Expand Up @@ -205,27 +205,20 @@ impl<T: Target, C: Connection> GdbStubImpl<T, C> {
// TODO: support qRelocInsn?
return Ok(HandlerStatus::NeedsOk);
}
Tracepoints::QTBuffer(buf) => {
match buf {
QTBuffer::Request {
offset,
length,
data,
} => {
let read = ops
.trace_buffer_request(offset, length, data)
.handle_error()?;
if let Some(read) = read {
let read = read.min(data.len());
res.write_hex_buf(&data[..read])?;
} else {
res.write_str("l")?;
}
}
QTBuffer::Configure { buffer } => {
ops.trace_buffer_configure(buffer).handle_error()?;
}
Tracepoints::qTBuffer(buf) => {
let qTBuffer { offset, length, data } = buf;
let read = ops
.trace_buffer_request(offset, length, data)
.handle_error()?;
if let Some(read) = read {
let read = read.min(data.len());
res.write_hex_buf(&data[..read])?;
} else {
res.write_str("l")?;
}
}
Tracepoints::QTBuffer(conf) => {
ops.trace_buffer_configure(conf.0).handle_error()?;
// Documentation doesn't mention this, but it needs OK
return Ok(HandlerStatus::NeedsOk);
}
Expand Down

0 comments on commit 93a789d

Please sign in to comment.