1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
|
use std::collections::BTreeSet;
use std::fmt;
use pdb::FallibleIterator;
type TypeSet = BTreeSet<pdb::TypeIndex>;
pub fn type_name<'p>(
type_finder: &pdb::TypeFinder<'p>,
type_index: pdb::TypeIndex,
needed_types: &mut TypeSet,
) -> pdb::Result<String> {
let mut name = match type_finder.find(type_index)?.parse()? {
pdb::TypeData::Primitive(data) => {
let mut name = match data.kind {
pdb::PrimitiveKind::Void => "void".to_string(),
pdb::PrimitiveKind::Char => "char".to_string(),
pdb::PrimitiveKind::UChar => "unsigned char".to_string(),
pdb::PrimitiveKind::I8 => "int8_t".to_string(),
pdb::PrimitiveKind::U8 => "uint8_t".to_string(),
pdb::PrimitiveKind::I16 => "int16_t".to_string(),
pdb::PrimitiveKind::U16 => "uint16_t".to_string(),
pdb::PrimitiveKind::I32 => "int32_t".to_string(),
pdb::PrimitiveKind::U32 => "uint32_t".to_string(),
pdb::PrimitiveKind::I64 => "int64_t".to_string(),
pdb::PrimitiveKind::U64 => "uint64_t".to_string(),
pdb::PrimitiveKind::F32 => "float".to_string(),
pdb::PrimitiveKind::F64 => "double".to_string(),
pdb::PrimitiveKind::Bool8 => "bool".to_string(),
_ => format!("unhandled_primitive.kind /* {:?} */", data.kind),
};
if data.indirection.is_some() {
name.push_str(" *");
}
name
}
pdb::TypeData::Class(data) => {
needed_types.insert(type_index);
data.name.to_string().into_owned()
}
pdb::TypeData::Enumeration(data) => {
needed_types.insert(type_index);
data.name.to_string().into_owned()
}
pdb::TypeData::Union(data) => {
needed_types.insert(type_index);
data.name.to_string().into_owned()
}
pdb::TypeData::Pointer(data) => format!(
"{}*",
type_name(type_finder, data.underlying_type, needed_types)?
),
pdb::TypeData::Modifier(data) => {
if data.constant {
format!(
"const {}",
type_name(type_finder, data.underlying_type, needed_types)?
)
} else if data.volatile {
format!(
"volatile {}",
type_name(type_finder, data.underlying_type, needed_types)?
)
} else {
// ?
type_name(type_finder, data.underlying_type, needed_types)?
}
}
pdb::TypeData::Array(data) => {
let mut name = type_name(type_finder, data.element_type, needed_types)?;
for size in data.dimensions {
name = format!("{}[{}]", name, size);
}
name
}
_ => format!("Type{} /* TODO: figure out how to name it */", type_index),
};
// TODO: search and replace std:: patterns
if name == "std::basic_string<char,std::char_traits<char>,std::allocator<char> >" {
name = "std::string".to_string();
}
Ok(name)
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct Class<'p> {
kind: pdb::ClassKind,
name: pdb::RawString<'p>,
base_classes: Vec<BaseClass>,
fields: Vec<Field<'p>>,
instance_methods: Vec<Method<'p>>,
static_methods: Vec<Method<'p>>,
}
impl<'p> Class<'p> {
#[allow(clippy::unnecessary_wraps)]
fn add_derived_from(
&mut self,
_: &pdb::TypeFinder<'p>,
_: pdb::TypeIndex,
_: &mut TypeSet,
) -> pdb::Result<()> {
// TODO
Ok(())
}
fn add_fields(
&mut self,
type_finder: &pdb::TypeFinder<'p>,
type_index: pdb::TypeIndex,
needed_types: &mut TypeSet,
) -> pdb::Result<()> {
match type_finder.find(type_index)?.parse()? {
pdb::TypeData::FieldList(data) => {
for field in &data.fields {
self.add_field(type_finder, field, needed_types)?;
}
if let Some(continuation) = data.continuation {
// recurse
self.add_fields(type_finder, continuation, needed_types)?;
}
}
other => {
println!(
"trying to Class::add_fields() got {} -> {:?}",
type_index, other
);
panic!("unexpected type in Class::add_fields()");
}
}
Ok(())
}
fn add_field(
&mut self,
type_finder: &pdb::TypeFinder<'p>,
field: &pdb::TypeData<'p>,
needed_types: &mut TypeSet,
) -> pdb::Result<()> {
match *field {
pdb::TypeData::Member(ref data) => {
// TODO: attributes (static, virtual, etc.)
self.fields.push(Field {
type_name: type_name(type_finder, data.field_type, needed_types)?,
name: data.name,
offset: data.offset,
});
}
pdb::TypeData::Method(ref data) => {
let method = Method::find(
data.name,
data.attributes,
type_finder,
data.method_type,
needed_types,
)?;
if data.attributes.is_static() {
self.static_methods.push(method);
} else {
self.instance_methods.push(method);
}
}
pdb::TypeData::OverloadedMethod(ref data) => {
// this just means we have more than one method with the same name
// find the method list
match type_finder.find(data.method_list)?.parse()? {
pdb::TypeData::MethodList(method_list) => {
for pdb::MethodListEntry {
attributes,
method_type,
..
} in method_list.methods
{
// hooray
let method = Method::find(
data.name,
attributes,
type_finder,
method_type,
needed_types,
)?;
if attributes.is_static() {
self.static_methods.push(method);
} else {
self.instance_methods.push(method);
}
}
}
other => {
println!(
"processing OverloadedMethod, expected MethodList, got {} -> {:?}",
data.method_list, other
);
panic!("unexpected type in Class::add_field()");
}
}
}
pdb::TypeData::BaseClass(ref data) => self.base_classes.push(BaseClass {
type_name: type_name(type_finder, data.base_class, needed_types)?,
offset: data.offset,
}),
pdb::TypeData::VirtualBaseClass(ref data) => self.base_classes.push(BaseClass {
type_name: type_name(type_finder, data.base_class, needed_types)?,
offset: data.base_pointer_offset,
}),
_ => {
// ignore everything else even though that's sad
}
}
Ok(())
}
}
impl fmt::Display for Class<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{} {} ",
match self.kind {
pdb::ClassKind::Class => "class",
pdb::ClassKind::Struct => "struct",
pdb::ClassKind::Interface => "interface", // when can this happen?
},
self.name.to_string()
)?;
if !self.base_classes.is_empty() {
for (i, base) in self.base_classes.iter().enumerate() {
let prefix = match i {
0 => ":",
_ => ",",
};
write!(f, "{} {}", prefix, base.type_name)?;
}
}
writeln!(f, " {{")?;
for base in &self.base_classes {
writeln!(
f,
"\t/* offset {:3} */ /* fields for {} */",
base.offset, base.type_name
)?;
}
for field in &self.fields {
writeln!(
f,
"\t/* offset {:3} */ {} {};",
field.offset,
field.type_name,
field.name.to_string()
)?;
}
if !self.instance_methods.is_empty() {
writeln!(f, "\t")?;
for method in &self.instance_methods {
writeln!(
f,
"\t{}{} {}({});",
if method.is_virtual { "virtual " } else { "" },
method.return_type_name,
method.name.to_string(),
method.arguments.join(", ")
)?;
}
}
if !self.static_methods.is_empty() {
writeln!(f, "\t")?;
for method in &self.static_methods {
writeln!(
f,
"\t{}static {} {}({});",
if method.is_virtual { "virtual " } else { "" },
method.return_type_name,
method.name.to_string(),
method.arguments.join(", ")
)?;
}
}
writeln!(f, "}}")?;
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct BaseClass {
type_name: String,
offset: u32,
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct Field<'p> {
type_name: String,
name: pdb::RawString<'p>,
offset: u64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct Method<'p> {
name: pdb::RawString<'p>,
return_type_name: String,
arguments: Vec<String>,
is_virtual: bool,
}
impl<'p> Method<'p> {
fn find(
name: pdb::RawString<'p>,
attributes: pdb::FieldAttributes,
type_finder: &pdb::TypeFinder<'p>,
type_index: pdb::TypeIndex,
needed_types: &mut TypeSet,
) -> pdb::Result<Method<'p>> {
match type_finder.find(type_index)?.parse()? {
pdb::TypeData::MemberFunction(data) => Ok(Method {
name,
return_type_name: type_name(type_finder, data.return_type, needed_types)?,
arguments: argument_list(type_finder, data.argument_list, needed_types)?,
is_virtual: attributes.is_virtual(),
}),
other => {
println!("other: {:?}", other);
Err(pdb::Error::UnimplementedFeature("that"))
}
}
}
}
fn argument_list<'p>(
type_finder: &pdb::TypeFinder<'p>,
type_index: pdb::TypeIndex,
needed_types: &mut TypeSet,
) -> pdb::Result<Vec<String>> {
match type_finder.find(type_index)?.parse()? {
pdb::TypeData::ArgumentList(data) => {
let mut args: Vec<String> = Vec::new();
for arg_type in data.arguments {
args.push(type_name(type_finder, arg_type, needed_types)?);
}
Ok(args)
}
_ => Err(pdb::Error::UnimplementedFeature(
"argument list of non-argument-list type",
)),
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct Enum<'p> {
name: pdb::RawString<'p>,
underlying_type_name: String,
values: Vec<EnumValue<'p>>,
}
impl<'p> Enum<'p> {
fn add_fields(
&mut self,
type_finder: &pdb::TypeFinder<'p>,
type_index: pdb::TypeIndex,
needed_types: &mut TypeSet,
) -> pdb::Result<()> {
match type_finder.find(type_index)?.parse()? {
pdb::TypeData::FieldList(data) => {
for field in &data.fields {
self.add_field(type_finder, field, needed_types);
}
if let Some(continuation) = data.continuation {
// recurse
self.add_fields(type_finder, continuation, needed_types)?;
}
}
other => {
println!(
"trying to Enum::add_fields() got {} -> {:?}",
type_index, other
);
panic!("unexpected type in Enum::add_fields()");
}
}
Ok(())
}
fn add_field(&mut self, _: &pdb::TypeFinder<'p>, field: &pdb::TypeData<'p>, _: &mut TypeSet) {
// ignore everything else even though that's sad
if let pdb::TypeData::Enumerate(ref data) = field {
self.values.push(EnumValue {
name: data.name,
value: data.value,
});
}
}
}
impl fmt::Display for Enum<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(
f,
"enum {} /* stored as {} */ {{",
self.name.to_string(),
self.underlying_type_name
)?;
for value in &self.values {
writeln!(
f,
"\t{} = {},",
value.name.to_string(),
match value.value {
pdb::Variant::U8(v) => format!("0x{:02x}", v),
pdb::Variant::U16(v) => format!("0x{:04x}", v),
pdb::Variant::U32(v) => format!("0x{:08x}", v),
pdb::Variant::U64(v) => format!("0x{:16x}", v),
pdb::Variant::I8(v) => format!("{}", v),
pdb::Variant::I16(v) => format!("{}", v),
pdb::Variant::I32(v) => format!("{}", v),
pdb::Variant::I64(v) => format!("{}", v),
}
)?;
}
writeln!(f, "}}")?;
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct EnumValue<'p> {
name: pdb::RawString<'p>,
value: pdb::Variant,
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct ForwardReference<'p> {
kind: pdb::ClassKind,
name: pdb::RawString<'p>,
}
impl fmt::Display for ForwardReference<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(
f,
"{} {};",
match self.kind {
pdb::ClassKind::Class => "class",
pdb::ClassKind::Struct => "struct",
pdb::ClassKind::Interface => "interface", // when can this happen?
},
self.name.to_string()
)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct Data<'p> {
forward_references: Vec<ForwardReference<'p>>,
classes: Vec<Class<'p>>,
enums: Vec<Enum<'p>>,
}
impl fmt::Display for Data<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "// automatically generated by pdb2hpp\n// do not edit")?;
if !self.forward_references.is_empty() {
writeln!(f)?;
for e in &self.forward_references {
e.fmt(f)?;
}
}
for e in &self.enums {
writeln!(f)?;
e.fmt(f)?;
}
for class in &self.classes {
writeln!(f)?;
class.fmt(f)?;
}
Ok(())
}
}
impl<'p> Data<'p> {
fn new() -> Data<'p> {
Data {
forward_references: Vec::new(),
classes: Vec::new(),
enums: Vec::new(),
}
}
fn add(
&mut self,
type_finder: &pdb::TypeFinder<'p>,
type_index: pdb::TypeIndex,
needed_types: &mut TypeSet,
) -> pdb::Result<()> {
match type_finder.find(type_index)?.parse()? {
pdb::TypeData::Class(data) => {
if data.properties.forward_reference() {
self.forward_references.push(ForwardReference {
kind: data.kind,
name: data.name,
});
return Ok(());
}
let mut class = Class {
kind: data.kind,
name: data.name,
fields: Vec::new(),
base_classes: Vec::new(),
instance_methods: Vec::new(),
static_methods: Vec::new(),
};
if let Some(derived_from) = data.derived_from {
class.add_derived_from(type_finder, derived_from, needed_types)?;
}
if let Some(fields) = data.fields {
class.add_fields(type_finder, fields, needed_types)?;
}
self.classes.insert(0, class);
}
pdb::TypeData::Enumeration(data) => {
let mut e = Enum {
name: data.name,
underlying_type_name: type_name(
type_finder,
data.underlying_type,
needed_types,
)?,
values: Vec::new(),
};
e.add_fields(type_finder, data.fields, needed_types)?;
self.enums.insert(0, e);
}
// ignore
other => eprintln!("warning: don't know how to add {:?}", other),
}
Ok(())
}
}
fn write_class(filename: &str, class_name: &str) -> pdb::Result<()> {
let file = std::fs::File::open(filename)?;
let mut pdb = pdb::PDB::open(file)?;
let type_information = pdb.type_information()?;
let mut type_finder = type_information.finder();
let mut needed_types = TypeSet::new();
let mut data = Data::new();
let mut type_iter = type_information.iter();
while let Some(typ) = type_iter.next()? {
// keep building the index
type_finder.update(&type_iter);
if let Ok(pdb::TypeData::Class(class)) = typ.parse() {
if class.name.as_bytes() == class_name.as_bytes()
&& !class.properties.forward_reference()
{
data.add(&type_finder, typ.index(), &mut needed_types)?;
break;
}
}
}
// add all the needed types iteratively until we're done
while let Some(type_index) = needed_types.iter().next_back().copied() {
// remove it
needed_types.remove(&type_index);
// add the type
data.add(&type_finder, type_index, &mut needed_types)?;
}
if data.classes.is_empty() {
eprintln!("sorry, class {} was not found", class_name);
} else {
println!("{}", data);
}
Ok(())
}
fn print_usage(program: &str, opts: getopts::Options) {
let brief = format!("Usage: {} input.pdb ClassName", program);
print!("{}", opts.usage(&brief));
}
fn main() {
let args: Vec<String> = std::env::args().collect();
let program = args[0].clone();
let mut opts = getopts::Options::new();
opts.optflag("h", "help", "print this help menu");
let matches = match opts.parse(&args[1..]) {
Ok(m) => m,
Err(f) => panic!("{}", f.to_string()),
};
let (filename, class_name) = if matches.free.len() == 2 {
(&matches.free[0], &matches.free[1])
} else {
print_usage(&program, opts);
return;
};
match write_class(filename, class_name) {
Ok(_) => (),
Err(e) => eprintln!("error dumping PDB: {}", e),
}
}
|