Skip to content

Commit

Permalink
add tracing for write
Browse files Browse the repository at this point in the history
  • Loading branch information
nickbclifford committed Dec 15, 2023
1 parent 4887577 commit d220d4b
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ num-derive = "0.4"
object = { version = "0.32.1", features = ['write'] }
paste = "1.0.5"
scroll = { version = "0.11.0", features = ['derive'] }
scroll-buffer = "0.3.0"
scroll-buffer = "0.3.1"
thiserror = "1.0.31"
tracing = "0.1.40"

Expand Down
6 changes: 6 additions & 0 deletions src/binary/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ macro_rules! heap_writer {
})
}
}

impl std::fmt::Debug for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(stringify!($name))
}
}
};
}

Expand Down
33 changes: 31 additions & 2 deletions src/convert/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ use paste::paste;
use scroll::{ctx::TryIntoCtx, Pwrite};
use scroll_buffer::DynamicBuffer;
use std::collections::HashMap;
use std::fmt::Debug;

#[derive(Debug)]
pub struct Context<'a> {
pub blobs: &'a mut BlobWriter,
pub specs: &'a mut Vec<TypeSpec>,
pub type_cache: &'a mut HashMap<u64, TypeDefOrRef>,
pub blob_scratch: &'a mut DynamicBuffer,
}

#[tracing::instrument]
pub fn index(t: &impl TypeKind, ctx: &mut Context) -> Result<TypeDefOrRef> {
let hash = crate::utils::hash(t);

Expand All @@ -51,13 +54,15 @@ pub fn index(t: &impl TypeKind, ctx: &mut Context) -> Result<TypeDefOrRef> {
Ok(result)
}

#[tracing::instrument]
pub fn user_index(t: UserType) -> TypeDefOrRef {
match t {
UserType::Definition(d) => TypeDefOrRef::TypeDef(d.0 + 1),
UserType::Reference(r) => TypeDefOrRef::TypeRef(r.0 + 1),
}
}

#[tracing::instrument]
fn source_sig(value_kind: Option<ValueKind>, t: &TypeSource<impl TypeKind>, ctx: &mut Context) -> Result<SType> {
let Some(value_kind) = value_kind else {
return Err(DLLError::CLI(scroll::Error::Custom(
Expand All @@ -80,6 +85,7 @@ fn source_sig(value_kind: Option<ValueKind>, t: &TypeSource<impl TypeKind>, ctx:
})
}

#[tracing::instrument]
pub fn source_index(
value_kind: Option<ValueKind>,
t: &TypeSource<impl TypeKind>,
Expand All @@ -92,23 +98,26 @@ pub fn source_index(
}
}

#[tracing::instrument]
pub fn base_index(base: &BaseType<impl TypeKind>, ctx: &mut Context) -> Result<TypeDefOrRef> {
Ok(match base {
BaseType::Type { value_kind, source } => source_index(*value_kind, source, ctx)?,
rest => into_index(base_sig(rest, ctx)?, ctx)?,
})
}

pub fn into_blob(sig: impl TryIntoCtx<(), DynamicBuffer, Error = scroll::Error>, ctx: &mut Context) -> Result<Blob> {
#[tracing::instrument]
pub fn into_blob(sig: impl TryIntoCtx<(), DynamicBuffer, Error = scroll::Error> + Debug, ctx: &mut Context) -> Result<Blob> {
ctx.blob_scratch.clear();

ctx.blob_scratch.pwrite(sig, 0)?;

Ok(ctx.blobs.write(ctx.blob_scratch.get())?)
}

#[tracing::instrument]
pub(super) fn into_index(
sig: impl TryIntoCtx<(), DynamicBuffer, Error = scroll::Error>,
sig: impl TryIntoCtx<(), DynamicBuffer, Error = scroll::Error> + Debug,
ctx: &mut Context,
) -> Result<TypeDefOrRef> {
let len = ctx.specs.len();
Expand All @@ -121,6 +130,7 @@ pub(super) fn into_index(
Ok(TypeDefOrRef::TypeSpec(len + 1))
}

#[tracing::instrument]
pub(super) fn base_sig(base: &BaseType<impl TypeKind>, ctx: &mut Context) -> Result<SType> {
use BaseType::*;

Expand Down Expand Up @@ -155,6 +165,7 @@ pub(super) fn base_sig(base: &BaseType<impl TypeKind>, ctx: &mut Context) -> Res
})
}

#[tracing::instrument]
fn maybe_unmanaged_method(sig: &MaybeUnmanagedMethod, ctx: &mut Context) -> Result<StandAloneMethodSig> {
Ok(StandAloneMethodSig {
has_this: sig.instance,
Expand All @@ -175,6 +186,7 @@ fn maybe_unmanaged_method(sig: &MaybeUnmanagedMethod, ctx: &mut Context) -> Resu
})
}

#[tracing::instrument]
pub fn custom_modifiers(mods: &[CustomTypeModifier]) -> Vec<CustomMod> {
mods.iter()
.map(|m| match m {
Expand All @@ -184,6 +196,7 @@ pub fn custom_modifiers(mods: &[CustomTypeModifier]) -> Vec<CustomMod> {
.collect()
}

#[tracing::instrument]
fn parameter_sig(p: &Parameter, ctx: &mut Context) -> Result<Param> {
Ok(Param(
custom_modifiers(&p.0),
Expand All @@ -199,6 +212,7 @@ fn parameter_sig(p: &Parameter, ctx: &mut Context) -> Result<Param> {
// into_blob(parameter_sig(p, ctx)?, ctx)
// }

#[tracing::instrument]
fn ret_type_sig(r: &ReturnType, ctx: &mut Context) -> Result<RetType> {
Ok(RetType(
custom_modifiers(&r.0),
Expand All @@ -211,6 +225,7 @@ fn ret_type_sig(r: &ReturnType, ctx: &mut Context) -> Result<RetType> {
))
}

#[tracing::instrument]
fn method_def_sig(sig: &ManagedMethod, ctx: &mut Context) -> Result<MethodDefSig> {
Ok(MethodDefSig {
has_this: sig.instance,
Expand All @@ -225,10 +240,12 @@ fn method_def_sig(sig: &ManagedMethod, ctx: &mut Context) -> Result<MethodDefSig
})
}

#[tracing::instrument]
pub fn method_def(sig: &ManagedMethod, ctx: &mut Context) -> Result<Blob> {
into_blob(method_def_sig(sig, ctx)?, ctx)
}

#[tracing::instrument]
fn method_ref_sig(sig: &ManagedMethod, ctx: &mut Context) -> Result<MethodRefSig> {
Ok(MethodRefSig {
method_def: method_def_sig(sig, ctx)?,
Expand All @@ -241,10 +258,12 @@ fn method_ref_sig(sig: &ManagedMethod, ctx: &mut Context) -> Result<MethodRefSig
})
}

#[tracing::instrument]
pub fn method_ref(sig: &ManagedMethod, ctx: &mut Context) -> Result<Blob> {
into_blob(method_ref_sig(sig, ctx)?, ctx)
}

#[tracing::instrument]
fn field_sig(f: &Field, ctx: &mut Context) -> Result<FieldSig> {
Ok(FieldSig {
custom_modifiers: custom_modifiers(&f.type_modifiers),
Expand All @@ -253,10 +272,12 @@ fn field_sig(f: &Field, ctx: &mut Context) -> Result<FieldSig> {
})
}

#[tracing::instrument]
pub fn field_def(f: &Field, ctx: &mut Context) -> Result<Blob> {
into_blob(field_sig(f, ctx)?, ctx)
}

#[tracing::instrument]
fn field_ref_sig(f: &ExternalFieldReference, ctx: &mut Context) -> Result<FieldSig> {
Ok(FieldSig {
custom_modifiers: custom_modifiers(&f.custom_modifiers),
Expand All @@ -265,10 +286,12 @@ fn field_ref_sig(f: &ExternalFieldReference, ctx: &mut Context) -> Result<FieldS
})
}

#[tracing::instrument]
pub fn field_ref(f: &ExternalFieldReference, ctx: &mut Context) -> Result<Blob> {
into_blob(field_ref_sig(f, ctx)?, ctx)
}

#[tracing::instrument]
fn property_sig(p: &Property, ctx: &mut Context) -> Result<PropertySig> {
Ok(PropertySig {
has_this: !p.static_member,
Expand All @@ -281,10 +304,12 @@ fn property_sig(p: &Property, ctx: &mut Context) -> Result<PropertySig> {
})
}

#[tracing::instrument]
pub fn property(p: &Property, ctx: &mut Context) -> Result<Blob> {
into_blob(property_sig(p, ctx)?, ctx)
}

#[tracing::instrument]
pub fn idx_with_modifiers(t: &impl TypeKind, mods: &[CustomTypeModifier], ctx: &mut Context) -> Result<TypeDefOrRef> {
if let Some(BaseType::Type {
source: TypeSource::User(u),
Expand All @@ -296,6 +321,7 @@ pub fn idx_with_modifiers(t: &impl TypeKind, mods: &[CustomTypeModifier], ctx: &
let sig = t.as_sig(ctx)?;
let mods = custom_modifiers(mods);

#[derive(Debug)]
struct Wrapper(Vec<CustomMod>, SType);
impl TryIntoCtx<(), DynamicBuffer> for Wrapper {
type Error = scroll::Error;
Expand All @@ -316,6 +342,7 @@ pub fn idx_with_modifiers(t: &impl TypeKind, mods: &[CustomTypeModifier], ctx: &
}
}

#[tracing::instrument]
fn local_var_sig(vars: &[LocalVariable], ctx: &mut Context) -> Result<LocalVarSig> {
Ok(LocalVarSig(
vars.iter()
Expand All @@ -339,6 +366,7 @@ fn local_var_sig(vars: &[LocalVariable], ctx: &mut Context) -> Result<LocalVarSi
))
}

#[tracing::instrument]
pub fn local_vars(vars: &[LocalVariable], ctx: &mut Context) -> Result<Blob> {
into_blob(local_var_sig(vars, ctx)?, ctx)
}
Expand All @@ -351,6 +379,7 @@ pub struct MethodContext<'a, T, U> {
pub field_source: &'a U,
}

#[tracing::instrument(skip(m_ctx))]
#[allow(clippy::too_many_lines)]
pub fn instruction(
instruction: &Instruction,
Expand Down
14 changes: 14 additions & 0 deletions src/resolution/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ pub(crate) fn read_impl<'a>(dll: &DLL<'a>, opts: Options) -> Result<Resolution<'
}};
}

debug!("assembly");

let mut assembly = None;
if let Some(a) = tables.assembly.first() {
use assembly::*;
Expand All @@ -248,6 +250,8 @@ pub(crate) fn read_impl<'a>(dll: &DLL<'a>, opts: Options) -> Result<Resolution<'
});
}

debug!("assembly refs");

let assembly_refs = tables
.assembly_ref
.iter()
Expand All @@ -266,6 +270,8 @@ pub(crate) fn read_impl<'a>(dll: &DLL<'a>, opts: Options) -> Result<Resolution<'
})
.collect::<Result<Vec<_>>>()?;

debug!("type definitions");

let mut types = tables
.type_def
.iter()
Expand Down Expand Up @@ -317,6 +323,8 @@ pub(crate) fn read_impl<'a>(dll: &DLL<'a>, opts: Options) -> Result<Resolution<'
})
.collect::<Result<Vec<_>>>()?;

debug!("nested types");

for n in &tables.nested_class {
let nest_idx = n.nested_class.0 - 1;
match types.get_mut(nest_idx) {
Expand Down Expand Up @@ -350,6 +358,8 @@ pub(crate) fn read_impl<'a>(dll: &DLL<'a>, opts: Options) -> Result<Resolution<'
.map(|e| Ok(range_index!(enumerated e => range method_list in type_def indexes method_def)))
.collect::<Result<Vec<_>>>()?;

debug!("files");

let files: Vec<_> = tables
.file
.iter()
Expand All @@ -363,6 +373,8 @@ pub(crate) fn read_impl<'a>(dll: &DLL<'a>, opts: Options) -> Result<Resolution<'
})
.collect::<Result<_>>()?;

debug!("resources");

let resources: Vec<_> = tables
.manifest_resource
.iter()
Expand Down Expand Up @@ -424,6 +436,8 @@ pub(crate) fn read_impl<'a>(dll: &DLL<'a>, opts: Options) -> Result<Resolution<'
})
.collect::<Result<_>>()?;

debug!("exported types");

let exports: Vec<_> = tables
.exported_type
.iter()
Expand Down
Loading

0 comments on commit d220d4b

Please sign in to comment.