Skip to content

Commit

Permalink
Merge pull request #2 from CuriousTommy/coredump_improvements
Browse files Browse the repository at this point in the history
Core Dump improvements
  • Loading branch information
Arsynth committed Nov 21, 2023
2 parents 7a8a32e + e92f6d4 commit 189bd63
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 6 deletions.
8 changes: 6 additions & 2 deletions src/types/load_command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ impl LoadCommand {
let variant = LcVariant::parse(
reader.clone(),
cmd,
cmdsize,
base_offset,
endian,
is_64,
Expand Down Expand Up @@ -253,6 +254,7 @@ impl LcVariant {
fn parse(
reader: RcReader,
cmd: u32,
cmdsize: u32,
command_offset: usize,
endian: Endian,
is_64: bool,
Expand Down Expand Up @@ -333,11 +335,13 @@ impl LcVariant {
Ok(Self::DyldEnvironment(c))
}
LC_THREAD => {
let c = reader_mut.ioread_with(endian)?;
std::mem::drop(reader_mut);
let c = LcThread::parse(reader, cmdsize, base_offset, endian)?;
Ok(Self::Thread(c))
}
LC_UNIXTHREAD => {
let c = reader_mut.ioread_with(endian)?;
std::mem::drop(reader_mut);
let c = LcThread::parse(reader, cmdsize, base_offset, endian)?;
Ok(Self::Thread(c))
}
LC_ROUTINES => {
Expand Down
73 changes: 69 additions & 4 deletions src/types/load_command/thread_command.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,81 @@
use scroll::{IOread, SizeWith};
use crate::RcReader;
use crate::Result;

use scroll::{IOread};

use std::fmt::Debug;
use std::io::{Seek, SeekFrom};
use std::mem::size_of;

use crate::auto_enum_fields::*;
use schnauzer_derive::AutoEnumFields;

const LC_THREAD_FLAVOR_HEADER_SIZE: usize = size_of::<u32>() + size_of::<u32>();

/// `thread_command`
#[repr(C)]
#[derive(Debug, IOread, SizeWith, AutoEnumFields)]
#[derive(AutoEnumFields,Debug)]
pub struct LcThread {
flavor: u32,
count: u32,
pub flavors: Vec<LcThreadFlavor>
}

#[repr(C)]
#[derive(AutoEnumFields)]
pub struct LcThreadFlavor {
pub flavor: u32,
pub count: u32,
/* struct XXX_thread_state state thread state for this flavor */
/* ... */

state_offset: u64
}

impl LcThread {
pub(super) fn parse(reader: RcReader, cmdsize: u32, base_offset: usize, endian: scroll::Endian) -> Result<Self> {
let mut flavors = Vec::new();

let mut flavor_offset = base_offset;
loop {
let flavor = LcThreadFlavor::parse(&reader, flavor_offset, endian)?;
if flavor.flavor != 0 && flavor.count != 0 {
flavor_offset += LC_THREAD_FLAVOR_HEADER_SIZE + flavor.count as usize * size_of::<u32>();
flavors.push(flavor);

if flavor_offset < base_offset + cmdsize as usize {
continue;
}
}

break;
}

Ok(LcThread { flavors })
}
}

impl LcThreadFlavor {
pub(super) fn parse(reader: &RcReader, base_offset: usize, endian: scroll::Endian) -> Result<Self> {
let mut reader_mut = reader.borrow_mut();
reader_mut.seek(SeekFrom::Start(base_offset as u64))?;

let flavor: u32 = reader_mut.ioread_with(endian)?;
let count: u32 = reader_mut.ioread_with(endian)?;

let state_offset = reader_mut.stream_position()?;

Ok(LcThreadFlavor { flavor, count, state_offset })
}

pub fn get_state_offset(&self) -> u64 {
self.state_offset
}
}

impl Debug for LcThreadFlavor {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("LcThreadFlavor")
.field("flavor", &self.flavor)
.field("count", &self.count)
.finish()
}
}

0 comments on commit 189bd63

Please sign in to comment.