-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for
qXfer:libraries{,-svr4}:read
(#142)
* Add support for `qXfer:libraries{,-svr4}:read` A toy project I've been working on involves loading a custom kernel and a custom guest into a KVM instance, and these commands enable `gdb` to be able to debug both the guest and the kernel at the same time by reporting where both images are loaded in the address space. Initially this commit only implemented `qXfer:libraries:read` but it turned out that gdb never actually used that command. When implementing `qXfer:libraries-svr4:read`, however, gdb invoked that automatically (presumably it's target-specific). I ended up including both here for completeness. * Add a note to the README * rustfmt * Add armv4t example * Remove support for `libraries` command Unknown how to actually get gdb to use it yet. * Fix a TODO * Update `section_offsets` documentation * Run cargo fmt
- Loading branch information
1 parent
7cba7ca
commit 9790714
Showing
12 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use super::copy_range_to_buf; | ||
use crate::emu::Emu; | ||
use gdbstub::target; | ||
use gdbstub::target::TargetResult; | ||
|
||
impl target::ext::libraries::LibrariesSvr4 for Emu { | ||
fn get_libraries_svr4( | ||
&self, | ||
offset: u64, | ||
length: usize, | ||
buf: &mut [u8], | ||
) -> TargetResult<usize, Self> { | ||
// `l_ld` is the address of the `PT_DYNAMIC` ELF segment, so fake an | ||
// address here. | ||
// | ||
// The `main-lm`, `lm`, and `lmid` seem to refer to in-memory structures | ||
// which gdb may read, but gdb also seems to work well enough if they're | ||
// null-ish or otherwise pointing to non-present things. | ||
let xml = r#" | ||
<library-list-svr4 version="1.0" main-lm="0x4"> | ||
<library name="/test.elf" lm="0x8" l_addr="0" l_ld="0" lmid="0x14"/> | ||
</library-list-svr4> | ||
"# | ||
.trim() | ||
.as_bytes(); | ||
Ok(copy_range_to_buf(xml, offset, length, buf)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use crate::protocol::common::qxfer::ParseAnnex; | ||
use crate::protocol::common::qxfer::QXferReadBase; | ||
|
||
pub type qXferLibrariesSvr4Read<'a> = QXferReadBase<'a, LibrariesSvr4Annex>; | ||
|
||
#[derive(Debug)] | ||
pub struct LibrariesSvr4Annex; | ||
|
||
impl<'a> ParseAnnex<'a> for LibrariesSvr4Annex { | ||
#[inline(always)] | ||
fn from_buf(buf: &[u8]) -> Option<Self> { | ||
if buf != b"" { | ||
return None; | ||
} | ||
|
||
Some(LibrariesSvr4Annex) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use super::prelude::*; | ||
use crate::protocol::commands::ext::LibrariesSvr4; | ||
|
||
impl<T: Target, C: Connection> GdbStubImpl<T, C> { | ||
pub(crate) fn handle_libraries_svr4( | ||
&mut self, | ||
res: &mut ResponseWriter<'_, C>, | ||
target: &mut T, | ||
command: LibrariesSvr4<'_>, | ||
) -> Result<HandlerStatus, Error<T::Error, C::Error>> { | ||
let ops = match target.support_libraries_svr4() { | ||
Some(ops) => ops, | ||
None => return Ok(HandlerStatus::Handled), | ||
}; | ||
|
||
crate::__dead_code_marker!("libraries-svr4", "impl"); | ||
|
||
let handler_status = match command { | ||
LibrariesSvr4::qXferLibrariesSvr4Read(cmd) => { | ||
let ret = ops | ||
.get_libraries_svr4(cmd.offset, cmd.length, cmd.buf) | ||
.handle_error()?; | ||
if ret == 0 { | ||
res.write_str("l")?; | ||
} else { | ||
res.write_str("m")?; | ||
// TODO: add more specific error variant? | ||
res.write_binary(cmd.buf.get(..ret).ok_or(Error::PacketBufferOverflow)?)?; | ||
} | ||
HandlerStatus::Handled | ||
} | ||
}; | ||
|
||
Ok(handler_status) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
//! Report information about the loaded shared libraries for targets where there | ||
//! are possibly multiple files to be debugged mapped into the same address | ||
//! space. | ||
use crate::target::Target; | ||
use crate::target::TargetResult; | ||
|
||
/// Target Extension - List an SVR4 (System-V/Unix) target's libraries. | ||
pub trait LibrariesSvr4: Target { | ||
/// Get library list XML for this target. | ||
/// | ||
/// See the [GDB Documentation] for a description of the format. | ||
/// | ||
/// [GDB Documentation]: https://sourceware.org/gdb/current/onlinedocs/gdb.html/Library-List-Format-for-SVR4-Targets.html | ||
/// | ||
/// Return the number of bytes written into `buf` (which may be less than | ||
/// `length`). | ||
/// | ||
/// If `offset` is greater than the length of the underlying data, return | ||
/// `Ok(0)`. | ||
fn get_libraries_svr4( | ||
&self, | ||
offset: u64, | ||
length: usize, | ||
buf: &mut [u8], | ||
) -> TargetResult<usize, Self>; | ||
} | ||
|
||
define_ext!(LibrariesSvr4Ops, LibrariesSvr4); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters