blob: 837c4e79f3085e048cd62f28e7e46c092592af4d [file] [log] [blame]
#!/usr/bin/env python
# Copyright (c) 2011 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""This will retrieve a PDBs "fingerprint" from it's corresponding executable
image (.dll or .exe). This is used when retrieving the PDB from the symbol
server. The .pdb (or cab compressed .pd_) is expected at a path like:
We can retrieve the same information from the .PDB file itself, but this file
format is much more difficult and undocumented. Instead, we can look at the
DLL's reference to the PDB, and use that to retrieve the information."""
from __future__ import print_function
import os
import sys
# Assume this script is under tools/symsrc/
_SCRIPT_DIR = os.path.dirname(__file__)
_ROOT_DIR = os.path.join(_SCRIPT_DIR, os.pardir, os.pardir)
_PEFILE_DIR = os.path.join(_ROOT_DIR, 'third_party', 'pefile')
sys.path.insert(1, _PEFILE_DIR)
import pefile
__CV_INFO_PDB70_format__ = ('CV_INFO_PDB70',
('4s,CvSignature', '16s,Signature', 'L,Age'))
__GUID_format__ = ('GUID',
('L,Data1', 'H,Data2', 'H,Data3', '8s,Data4'))
def GetPDBInfoFromImg(filename):
"""Returns the PDB fingerprint and the pdb filename given an image file"""
pe = pefile.PE(filename)
if dbg.struct.Type == 2: # IMAGE_DEBUG_TYPE_CODEVIEW
off = dbg.struct.AddressOfRawData
size = dbg.struct.SizeOfData
data = pe.get_memory_mapped_image()[off:off+size]
cv = pefile.Structure(__CV_INFO_PDB70_format__)
cv.PdbFileName = data[cv.sizeof():]
guid = pefile.Structure(__GUID_format__)
guid.Data4_0 = ''.join("%02X" % ord(x) for x in guid.Data4[0:2])
guid.Data4_1 = ''.join("%02X" % ord(x) for x in guid.Data4[2:])
return ("%08X%04X%04X%s%s%d" % (
guid.Data1, guid.Data2, guid.Data3,
guid.Data4_0, guid.Data4_1, cv.Age),
cv.PdbFileName.split('\x00', 1)[0])
def main():
if len(sys.argv) != 2:
print("usage: file.dll")
return 1
(fingerprint, filename) = GetPDBInfoFromImg(sys.argv[1])
print("%s %s" % (fingerprint, filename))
return 0
if __name__ == '__main__':