Skip to content

Commit c40145c

Browse files
authored
Unify the way of writing a range of data (#64)
1 parent 7890999 commit c40145c

File tree

3 files changed

+22
-24
lines changed

3 files changed

+22
-24
lines changed

src/gdbstub_impl/ext/base.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,7 @@ impl<T: Target, C: Connection> GdbStubImpl<T, C> {
131131
match xml {
132132
Some(xml) => {
133133
let xml = xml.trim().as_bytes();
134-
if cmd.offset < xml.len() {
135-
// still more data
136-
res.write_str("m")?;
137-
res.write_binary(
138-
&xml[cmd.offset..][..cmd.len.min(xml.len() - cmd.offset)],
139-
)?
140-
} else {
141-
// no more data
142-
res.write_str("l")?;
143-
}
134+
res.write_binary_range(xml, cmd.offset, cmd.len)?;
144135
}
145136
// If the target hasn't provided their own XML, then the initial response to
146137
// "qSupported" wouldn't have included "qXfer:features:read", and gdb wouldn't

src/gdbstub_impl/ext/memory_map.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,8 @@ impl<T: Target, C: Connection> GdbStubImpl<T, C> {
1717

1818
let handler_status = match command {
1919
MemoryMap::qXferMemoryMapRead(cmd) => {
20-
let xml = ops.memory_map_xml().trim();
21-
if cmd.offset >= xml.len() {
22-
// no more data
23-
res.write_str("l")?;
24-
} else if cmd.offset + cmd.len >= xml.len() {
25-
// last little bit of data
26-
res.write_str("l")?;
27-
res.write_binary(&xml.as_bytes()[cmd.offset..])?
28-
} else {
29-
// still more data
30-
res.write_str("m")?;
31-
res.write_binary(&xml.as_bytes()[cmd.offset..(cmd.offset + cmd.len)])?
32-
}
33-
20+
let xml = ops.memory_map_xml().trim().as_bytes();
21+
res.write_binary_range(xml, cmd.offset, cmd.len)?;
3422
HandlerStatus::Handled
3523
}
3624
};

src/protocol/response_writer.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,25 @@ impl<'a, C: Connection + 'a> ResponseWriter<'a, C> {
193193
Ok(())
194194
}
195195

196+
/// Write a range of data specified by offset and len.
197+
/// Used by qXfer:_object_:read commands.
198+
pub fn write_binary_range(
199+
&mut self,
200+
data: &[u8],
201+
offset: usize,
202+
len: usize,
203+
) -> Result<(), Error<C::Error>> {
204+
if offset < data.len() {
205+
// still more data
206+
self.write_str("m")?;
207+
self.write_binary(&data[offset..][..len.min(data.len() - offset)])?
208+
} else {
209+
// no more data
210+
self.write_str("l")?;
211+
}
212+
Ok(())
213+
}
214+
196215
/// Write a number as a big-endian hex string using the most compact
197216
/// representation possible (i.e: trimming leading zeros).
198217
pub fn write_num<D: BeBytes + PrimInt>(&mut self, digit: D) -> Result<(), Error<C::Error>> {

0 commit comments

Comments
 (0)