PageRenderTime 48ms CodeModel.GetById 14ms app.highlight 24ms RepoModel.GetById 1ms app.codeStats 0ms

/thirdparty/breakpad/common/windows/pdb_source_line_writer.h

http://github.com/tomahawk-player/tomahawk
C++ Header | 238 lines | 78 code | 43 blank | 117 comment | 5 complexity | b4654b58f9f1bd575d5d11e08ef25293 MD5 | raw file
  1// Copyright (c) 2006, Google Inc.
  2// All rights reserved.
  3//
  4// Redistribution and use in source and binary forms, with or without
  5// modification, are permitted provided that the following conditions are
  6// met:
  7//
  8//     * Redistributions of source code must retain the above copyright
  9// notice, this list of conditions and the following disclaimer.
 10//     * Redistributions in binary form must reproduce the above
 11// copyright notice, this list of conditions and the following disclaimer
 12// in the documentation and/or other materials provided with the
 13// distribution.
 14//     * Neither the name of Google Inc. nor the names of its
 15// contributors may be used to endorse or promote products derived from
 16// this software without specific prior written permission.
 17//
 18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 29
 30// PDBSourceLineWriter uses a pdb file produced by Visual C++ to output
 31// a line/address map for use with BasicSourceLineResolver.
 32
 33#ifndef _PDB_SOURCE_LINE_WRITER_H__
 34#define _PDB_SOURCE_LINE_WRITER_H__
 35
 36#include <atlcomcli.h>
 37
 38#include <hash_map>
 39#include <string>
 40
 41struct IDiaEnumLineNumbers;
 42struct IDiaSession;
 43struct IDiaSymbol;
 44
 45namespace google_breakpad {
 46
 47using std::wstring;
 48using stdext::hash_map;
 49
 50// A structure that carries information that identifies a pdb file.
 51struct PDBModuleInfo {
 52 public:
 53  // The basename of the pdb file from which information was loaded.
 54  wstring debug_file;
 55
 56  // The pdb's identifier.  For recent pdb files, the identifier consists
 57  // of the pdb's guid, in uppercase hexadecimal form without any dashes
 58  // or separators, followed immediately by the pdb's age, also in
 59  // uppercase hexadecimal form.  For older pdb files which have no guid,
 60  // the identifier is the pdb's 32-bit signature value, in zero-padded
 61  // hexadecimal form, followed immediately by the pdb's age, in lowercase
 62  // hexadecimal form.
 63  wstring debug_identifier;
 64
 65  // A string identifying the cpu that the pdb is associated with.
 66  // Currently, this may be "x86" or "unknown".
 67  wstring cpu;
 68};
 69
 70// A structure that carries information that identifies a PE file,
 71// either an EXE or a DLL.
 72struct PEModuleInfo {
 73  // The basename of the PE file.
 74  wstring code_file;
 75
 76  // The PE file's code identifier, which consists of its timestamp
 77  // and file size concatenated together into a single hex string.
 78  // (The fields IMAGE_OPTIONAL_HEADER::SizeOfImage and
 79  // IMAGE_FILE_HEADER::TimeDateStamp, as defined in the ImageHlp
 80  // documentation.) This is not well documented, if it's documented
 81  // at all, but it's what symstore does and what DbgHelp supports.
 82  wstring code_identifier;
 83};
 84
 85class PDBSourceLineWriter {
 86 public:
 87  enum FileFormat {
 88    PDB_FILE,  // a .pdb file containing debug symbols
 89    EXE_FILE,  // a .exe or .dll file
 90    ANY_FILE   // try PDB_FILE and then EXE_FILE
 91  };
 92
 93  explicit PDBSourceLineWriter();
 94  ~PDBSourceLineWriter();
 95
 96  // Opens the given file.  For executable files, the corresponding pdb
 97  // file must be available; Open will be if it is not.
 98  // If there is already a pdb file open, it is automatically closed.
 99  // Returns true on success.
100  bool Open(const wstring &file, FileFormat format);
101
102  // Locates the pdb file for the given executable (exe or dll) file,
103  // and opens it.  If there is already a pdb file open, it is automatically
104  // closed.  Returns true on success.
105  bool OpenExecutable(const wstring &exe_file);
106
107  // Writes a map file from the current pdb file to the given file stream.
108  // Returns true on success.
109  bool WriteMap(FILE *map_file);
110
111  // Closes the current pdb file and its associated resources.
112  void Close();
113
114  // Retrieves information about the module's debugging file.  Returns
115  // true on success and false on failure.
116  bool GetModuleInfo(PDBModuleInfo *info);
117
118  // Retrieves information about the module's PE file.  Returns
119  // true on success and false on failure.
120  bool GetPEInfo(PEModuleInfo *info);
121
122  // Sets uses_guid to true if the opened file uses a new-style CodeView
123  // record with a 128-bit GUID, or false if the opened file uses an old-style
124  // CodeView record.  When no GUID is available, a 32-bit signature should be
125  // used to identify the module instead.  If the information cannot be
126  // determined, this method returns false.
127  bool UsesGUID(bool *uses_guid);
128
129 private:
130  // Outputs the line/address pairs for each line in the enumerator.
131  // Returns true on success.
132  bool PrintLines(IDiaEnumLineNumbers *lines);
133
134  // Outputs a function address and name, followed by its source line list.
135  // block can be the same object as function, or it can be a reference
136  // to a code block that is lexically part of this function, but
137  // resides at a separate address.
138  // Returns true on success.
139  bool PrintFunction(IDiaSymbol *function, IDiaSymbol *block);
140
141  // Outputs all functions as described above.  Returns true on success.
142  bool PrintFunctions();
143
144  // Outputs all of the source files in the session's pdb file.
145  // Returns true on success.
146  bool PrintSourceFiles();
147
148  // Outputs all of the frame information necessary to construct stack
149  // backtraces in the absence of frame pointers.  Returns true on success.
150  bool PrintFrameData();
151
152  // Outputs a single public symbol address and name, if the symbol corresponds
153  // to a code address.  Returns true on success.  If symbol is does not
154  // correspond to code, returns true without outputting anything.
155  bool PrintCodePublicSymbol(IDiaSymbol *symbol);
156
157  // Outputs a line identifying the PDB file that is being dumped, along with
158  // its uuid and age.
159  bool PrintPDBInfo();
160
161  // Outputs a line identifying the PE file corresponding to the PDB
162  // file that is being dumped, along with its code identifier,
163  // which consists of its timestamp and file size.
164  bool PrintPEInfo();
165
166  // Returns true if this filename has already been seen,
167  // and an ID is stored for it, or false if it has not.
168  bool FileIDIsCached(const wstring &file) {
169    return unique_files_.find(file) != unique_files_.end();
170  };
171
172  // Cache this filename and ID for later reuse.
173  void CacheFileID(const wstring &file, DWORD id) {
174    unique_files_[file] = id;
175  };
176
177  // Store this ID in the cache as a duplicate for this filename.
178  void StoreDuplicateFileID(const wstring &file, DWORD id) {
179    hash_map<wstring, DWORD>::iterator iter = unique_files_.find(file);
180    if (iter != unique_files_.end()) {
181      // map this id to the previously seen one
182      file_ids_[id] = iter->second;
183    }
184  };
185
186  // Given a file's unique ID, return the ID that should be used to
187  // reference it. There may be multiple files with identical filenames
188  // but different unique IDs. The cache attempts to coalesce these into
189  // one ID per unique filename.
190  DWORD GetRealFileID(DWORD id) {
191    hash_map<DWORD, DWORD>::iterator iter = file_ids_.find(id);
192    if (iter == file_ids_.end())
193      return id;
194    return iter->second;
195  };
196
197  // Find the PE file corresponding to the loaded PDB file, and
198  // set the code_file_ member. Returns false on failure.
199  bool FindPEFile();
200
201  // Returns the function name for a symbol.  If possible, the name is
202  // undecorated.  If the symbol's decorated form indicates the size of
203  // parameters on the stack, this information is returned in stack_param_size.
204  // Returns true on success.  If the symbol doesn't encode parameter size
205  // information, stack_param_size is set to -1.
206  static bool GetSymbolFunctionName(IDiaSymbol *function, BSTR *name,
207                                    int *stack_param_size);
208
209  // Returns the number of bytes of stack space used for a function's
210  // parameters.  function must have the tag SymTagFunction.  In the event of
211  // a failure, returns 0, which is also a valid number of bytes.
212  static int GetFunctionStackParamSize(IDiaSymbol *function);
213
214  // The filename of the PE file corresponding to the currently-open
215  // pdb file.
216  wstring code_file_;
217
218  // The session for the currently-open pdb file.
219  CComPtr<IDiaSession> session_;
220
221  // The current output file for this WriteMap invocation.
222  FILE *output_;
223
224  // There may be many duplicate filenames with different IDs.
225  // This maps from the DIA "unique ID" to a single ID per unique
226  // filename.
227  hash_map<DWORD, DWORD> file_ids_;
228  // This maps unique filenames to file IDs.
229  hash_map<wstring, DWORD> unique_files_;
230
231  // Disallow copy ctor and operator=
232  PDBSourceLineWriter(const PDBSourceLineWriter&);
233  void operator=(const PDBSourceLineWriter&);
234};
235
236}  // namespace google_breakpad
237
238#endif  // _PDB_SOURCE_LINE_WRITER_H__