moyix / pdbparse

Python code to parse Microsoft PDB files
Other
316 stars 84 forks source link

Exception thrown on any pdb file #55

Closed changliu98 closed 3 years ago

changliu98 commented 3 years ago

When called the pdbparse.parse("filename"), it would throw an exception with any pdb file

Python 3.8.10 (tags/v3.8.10:3d8993a, May  3 2021, 11:48:03) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pdbparse
>>> pdbparse.parse("vc142.pdb") 
Traceback (most recent call last):
  File "", line 1, in 
  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\pdbparse\__init__.py", line 554, in parse
    return PDB7(f, fast_load)
  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\pdbparse\__init__.py", line 521, in __init__
    self.read_root(self.root_stream)
  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\pdbparse\__init__.py", line 460, in read_root
    pdb_cls(
  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\pdbparse\__init__.py", line 154, in __init__
    self.load()
  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\pdbparse\__init__.py", line 262, in load
    tpis = tpi.parse_stream(self.stream_file, unnamed_hack, elim_fwdrefs)
  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\pdbparse\tpi.py", line 1190, in parse_stream
    types[i].substructs = ListContainer([resolve_typerefs(t, types, min) for t in types[i].substructs])
  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\pdbparse\tpi.py", line 1190, in 
    types[i].substructs = ListContainer([resolve_typerefs(t, types, min) for t in types[i].substructs])
  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\pdbparse\tpi.py", line 1105, in resolve_typerefs
    for attr in type_refs[leaf.leaf_type]:
KeyError: 61938
>>>

caused by function resolve_typerefs(leaf, types, min) when exception happened, it was called with leaf.leaf_type is a int, in the output above it is 61938 and I have seen many other number

The pdb file was generated by a simple helloworld cpp program

changliu98 commented 3 years ago

The compiling toolset version below:

Microsoft Visual Studio Community 2019
Version 16.10.4
VisualStudio.16.Release/16.10.4+31515.178
Microsoft .NET Framework
Version 4.8.04084

Installed Version: Community

Visual C++ 2019   00435-60000-00000-AA609
Microsoft Visual C++ 2019

ASP.NET and Web Tools 2019   16.10.526.50910
ASP.NET and Web Tools 2019

C# Tools   3.10.0-4.21329.37+246ce641f04b67ef017655275d850bf902a8e40f
C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

IntelliCode Extension   1.0
IntelliCode Visual Studio Extension Detailed Info

Microsoft JVM Debugger   1.0
Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

Microsoft MI-Based Debugger   1.0
Provides support for connecting Visual Studio to MI compatible debuggers

Microsoft Visual C++ Wizards   1.0
Microsoft Visual C++ Wizards

Microsoft Visual Studio VC Package   1.0
Microsoft Visual Studio VC Package

NuGet Package Manager   5.10.0
NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/

ProjectServicesPackage Extension   1.0
ProjectServicesPackage Visual Studio Extension Detailed Info

Test Adapter for Boost.Test   1.0
Enables Visual Studio's testing tools with unit tests written for Boost.Test.  The use terms and Third Party Notices are available in the extension installation directory.

Test Adapter for Google Test   1.0
Enables Visual Studio's testing tools with unit tests written for Google Test.  The use terms and Third Party Notices are available in the extension installation directory.

TypeScript Tools   16.0.30429.2002
TypeScript Tools for Microsoft Visual Studio

Visual Basic Tools   3.10.0-4.21329.37+246ce641f04b67ef017655275d850bf902a8e40f
Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Visual Studio Code Debug Adapter Host Package   1.0
Interop layer for hosting Visual Studio Code debug adapters in Visual Studio

Visual Studio Tools for CMake   1.0
Visual Studio Tools for CMake
moyix commented 3 years ago

Could you provide a sample PDB file? I don't have any newer versions of Visual Studio around to test on unfortunately.

changliu98 commented 3 years ago

vc142.zip It is generated with x86 Debug with the tool version mentioned above Source code:

#include 

int main() {
    std::cout << "Hello World!";
    return 0;
}

Error output:

Python 3.8.10 (tags/v3.8.10:3d8993a, May  3 2021, 11:48:03) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pdbparse
>>> pdbparse.parse("vc142.pdb")
Traceback (most recent call last):
  File "", line 1, in 
  File "C:\Users\nagato\AppData\Local\Programs\Python\Python38\lib\site-packages\pdbparse\__init__.py", line 554, in parse
    return PDB7(f, fast_load)
  File "C:\Users\nagato\AppData\Local\Programs\Python\Python38\lib\site-packages\pdbparse\__init__.py", line 521, in __init__
    self.read_root(self.root_stream)
  File "C:\Users\nagato\AppData\Local\Programs\Python\Python38\lib\site-packages\pdbparse\__init__.py", line 460, in read_root
    pdb_cls(
  File "C:\Users\nagato\AppData\Local\Programs\Python\Python38\lib\site-packages\pdbparse\__init__.py", line 154, in __init__
    self.load()
  File "C:\Users\nagato\AppData\Local\Programs\Python\Python38\lib\site-packages\pdbparse\__init__.py", line 262, in load
    tpis = tpi.parse_stream(self.stream_file, unnamed_hack, elim_fwdrefs)
  File "C:\Users\nagato\AppData\Local\Programs\Python\Python38\lib\site-packages\pdbparse\tpi.py", line 1190, in parse_stream
    types[i].substructs = ListContainer([resolve_typerefs(t, types, min) for t in types[i].substructs])
  File "C:\Users\nagato\AppData\Local\Programs\Python\Python38\lib\site-packages\pdbparse\tpi.py", line 1190, in 
    types[i].substructs = ListContainer([resolve_typerefs(t, types, min) for t in types[i].substructs])
  File "C:\Users\nagato\AppData\Local\Programs\Python\Python38\lib\site-packages\pdbparse\tpi.py", line 1105, in resolve_typerefs
    for attr in type_refs[leaf.leaf_type]:
KeyError: 61938
moyix commented 3 years ago

Sorry for the delay. Are you sure you are using the most recent version from git? I tried your PDB and I got a different error caused by the fact that it is missing its DBI stream. However, once I fixed that it loads just fine:

(pdbparse) caterina:pdbparse moyix$ ipython
Python 3.8.11 (default, Jun 29 2021, 03:17:05) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.26.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import pdbparse

In [2]: pdb = pdbparse.parse('/Users/moyix/Downloads/vc142.pdb')

In [3]: pdb.streams
Out[3]: 
[<pdbparse.PDBStream at 0x1086e2310>,
 <pdbparse.PDBInfoStream at 0x1085f83a0>,
 <pdbparse.PDBTypeStream at 0x1085f82e0>,
 <pdbparse.PDBDebugStream at 0x1086d49a0>,
 <pdbparse.PDBStream at 0x1085f8700>,
 <pdbparse.PDBStream at 0x1095365b0>,
 <pdbparse.PDBStream at 0x109536640>,
 <pdbparse.PDBStream at 0x1095366d0>]
changliu98 commented 3 years ago

Thank you!