diff options
author | ted.mielczarek <ted.mielczarek@4c0a9323-5329-0410-9bdc-e9ce6186880e> | 2010-10-05 19:39:23 +0000 |
---|---|---|
committer | ted.mielczarek <ted.mielczarek@4c0a9323-5329-0410-9bdc-e9ce6186880e> | 2010-10-05 19:39:23 +0000 |
commit | d35f113d020aa1cb4f18aace03eca4eb8705dad2 (patch) | |
tree | c06b506129ba2f3b480d5b8f461e5cbf38a78925 /src/common/windows/pdb_source_line_writer.cc | |
parent | ditch libtool, only build static libs (diff) | |
download | breakpad-d35f113d020aa1cb4f18aace03eca4eb8705dad2.tar.xz |
Make dump_syms output an INFO CODE_ID line that includes the code file and code identifier. (Currently disabled to give Breakpad users time to update their processor code.)
R=mark at http://breakpad.appspot.com/180001/show
git-svn-id: http://google-breakpad.googlecode.com/svn/trunk@710 4c0a9323-5329-0410-9bdc-e9ce6186880e
Diffstat (limited to 'src/common/windows/pdb_source_line_writer.cc')
-rw-r--r-- | src/common/windows/pdb_source_line_writer.cc | 124 |
1 files changed, 119 insertions, 5 deletions
diff --git a/src/common/windows/pdb_source_line_writer.cc b/src/common/windows/pdb_source_line_writer.cc index 48a35bed..9969530b 100644 --- a/src/common/windows/pdb_source_line_writer.cc +++ b/src/common/windows/pdb_source_line_writer.cc @@ -28,8 +28,8 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include <atlbase.h> -#include <DbgHelp.h> #include <dia2.h> +#include <ImageHlp.h> #include <stdio.h> #include "common/windows/string_utils-inl.h" @@ -45,6 +45,24 @@ namespace google_breakpad { +using std::vector; + +// A helper class to scope a PLOADED_IMAGE. +class AutoImage { + public: + explicit AutoImage(PLOADED_IMAGE img) : img_(img) {} + ~AutoImage() { + if (img_) + ImageUnload(img_); + } + + operator PLOADED_IMAGE() { return img_; } + PLOADED_IMAGE operator->() { return img_; } + + private: + PLOADED_IMAGE img_; +}; + PDBSourceLineWriter::PDBSourceLineWriter() : output_(NULL) { } @@ -78,6 +96,7 @@ bool PDBSourceLineWriter::Open(const wstring &file, FileFormat format) { fprintf(stderr, "loadDataForExe failed\n"); return false; } + code_file_ = file; break; case ANY_FILE: if (FAILED(data_source->loadDataFromPdb(file.c_str()))) { @@ -85,6 +104,7 @@ bool PDBSourceLineWriter::Open(const wstring &file, FileFormat format) { fprintf(stderr, "loadDataForPdb and loadDataFromExe failed\n"); return false; } + code_file_ = file; } break; default: @@ -498,6 +518,18 @@ bool PDBSourceLineWriter::PrintPDBInfo() { return true; } +bool PDBSourceLineWriter::PrintPEInfo() { + PEModuleInfo info; + if (!GetPEInfo(&info)) { + return false; + } + + fprintf(output_, "INFO CODE_ID %ws %ws\n", + info.code_identifier.c_str(), + info.code_file.c_str()); + return true; +} + // wcstol_positive_strict is sort of like wcstol, but much stricter. string // should be a buffer pointing to a null-terminated string containing only // decimal digits. If the entire string can be converted to an integer @@ -535,6 +567,35 @@ static bool wcstol_positive_strict(wchar_t *string, int *result) { return true; } +bool PDBSourceLineWriter::FindPEFile() { + CComPtr<IDiaSymbol> global; + if (FAILED(session_->get_globalScope(&global))) { + fprintf(stderr, "get_globalScope failed\n"); + return false; + } + + CComBSTR symbols_file; + if (SUCCEEDED(global->get_symbolsFileName(&symbols_file))) { + wstring file(symbols_file); + + // Look for an EXE or DLL file. + const wchar_t *extensions[] = { L"exe", L"dll" }; + for (int i = 0; i < sizeof(extensions) / sizeof(extensions[0]); i++) { + size_t dot_pos = file.find_last_of(L"."); + if (dot_pos != wstring::npos) { + file.replace(dot_pos + 1, wstring::npos, extensions[i]); + // Check if this file exists. + if (GetFileAttributesW(file.c_str()) != INVALID_FILE_ATTRIBUTES) { + code_file_ = file; + return true; + } + } + } + } + + return false; +} + // static bool PDBSourceLineWriter::GetSymbolFunctionName(IDiaSymbol *function, BSTR *name, @@ -737,10 +798,15 @@ next_child: bool PDBSourceLineWriter::WriteMap(FILE *map_file) { output_ = map_file; - bool ret = PrintPDBInfo() && - PrintSourceFiles() && - PrintFunctions() && - PrintFrameData(); + bool ret = PrintPDBInfo(); + // TODO(ted): This is currently disabled to allow Breakpad users to update + // processing infrastructure. + // This is not a critical piece of the symbol file. + // PrintPEInfo(); + ret = ret && + PrintSourceFiles() && + PrintFunctions() && + PrintFrameData(); output_ = NULL; return ret; @@ -846,6 +912,54 @@ bool PDBSourceLineWriter::GetModuleInfo(PDBModuleInfo *info) { return true; } +bool PDBSourceLineWriter::GetPEInfo(PEModuleInfo *info) { + if (!info) { + return false; + } + + if (code_file_.empty() && !FindPEFile()) { + fprintf(stderr, "Couldn't locate EXE or DLL file.\n"); + return false; + } + + // Convert wchar to native charset because ImageLoad only takes + // a PSTR as input. + string code_file; + if (!WindowsStringUtils::safe_wcstombs(code_file_, &code_file)) { + return false; + } + + AutoImage img(ImageLoad((PSTR)code_file.c_str(), NULL)); + if (!img) { + fprintf(stderr, "Failed to open PE file: %s\n", code_file.c_str()); + return false; + } + + info->code_file = WindowsStringUtils::GetBaseName(code_file_); + + // The date and time that the file was created by the linker. + DWORD TimeDateStamp = img->FileHeader->FileHeader.TimeDateStamp; + // The size of the file in bytes, including all headers. + DWORD SizeOfImage = 0; + PIMAGE_OPTIONAL_HEADER64 opt = + &((PIMAGE_NT_HEADERS64)img->FileHeader)->OptionalHeader; + if (opt->Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) { + // 64-bit PE file. + SizeOfImage = opt->SizeOfImage; + } + else { + // 32-bit PE file. + SizeOfImage = img->FileHeader->OptionalHeader.SizeOfImage; + } + wchar_t code_identifier[32]; + swprintf(code_identifier, + sizeof(code_identifier) / sizeof(code_identifier[0]), + L"%08X%X", TimeDateStamp, SizeOfImage); + info->code_identifier = code_identifier; + + return true; +} + bool PDBSourceLineWriter::UsesGUID(bool *uses_guid) { if (!uses_guid) return false; |