PageRenderTime 31ms CodeModel.GetById 16ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/thirdparty/breakpad/common/dwarf_cu_to_module.h

http://github.com/tomahawk-player/tomahawk
C++ Header | 276 lines | 107 code | 47 blank | 122 comment | 0 complexity | fae057819531fc564479524a07a38deb MD5 | raw file
  1// -*- mode: c++ -*-
  2
  3// Copyright (c) 2010 Google Inc.
  4// All rights reserved.
  5//
  6// Redistribution and use in source and binary forms, with or without
  7// modification, are permitted provided that the following conditions are
  8// met:
  9//
 10//     * Redistributions of source code must retain the above copyright
 11// notice, this list of conditions and the following disclaimer.
 12//     * Redistributions in binary form must reproduce the above
 13// copyright notice, this list of conditions and the following disclaimer
 14// in the documentation and/or other materials provided with the
 15// distribution.
 16//     * Neither the name of Google Inc. nor the names of its
 17// contributors may be used to endorse or promote products derived from
 18// this software without specific prior written permission.
 19//
 20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 31
 32// Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com>
 33
 34// Add DWARF debugging information to a Breakpad symbol file. This
 35// file defines the DwarfCUToModule class, which accepts parsed DWARF
 36// data and populates a google_breakpad::Module with the results; the
 37// Module can then write its contents as a Breakpad symbol file.
 38
 39#ifndef COMMON_LINUX_DWARF_CU_TO_MODULE_H__
 40#define COMMON_LINUX_DWARF_CU_TO_MODULE_H__
 41
 42#include <string>
 43
 44#include "common/language.h"
 45#include "common/module.h"
 46#include "common/dwarf/bytereader.h"
 47#include "common/dwarf/dwarf2diehandler.h"
 48#include "common/dwarf/dwarf2reader.h"
 49
 50namespace google_breakpad {
 51
 52using dwarf2reader::AttributeList;
 53using dwarf2reader::DwarfAttribute;
 54using dwarf2reader::DwarfForm;
 55using dwarf2reader::DwarfLanguage;
 56using dwarf2reader::DwarfTag;
 57
 58// Populate a google_breakpad::Module with DWARF debugging information.
 59//
 60// An instance of this class can be provided as a handler to a
 61// dwarf2reader::DIEDispatcher, which can in turn be a handler for a
 62// dwarf2reader::CompilationUnit DWARF parser. The handler uses the results
 63// of parsing to populate a google_breakpad::Module with source file,
 64// function, and source line information.
 65class DwarfCUToModule: public dwarf2reader::RootDIEHandler {
 66  struct FilePrivate;
 67 public:
 68
 69  // Information global to the DWARF-bearing file we are processing,
 70  // for use by DwarfCUToModule. Each DwarfCUToModule instance deals
 71  // with a single compilation unit within the file, but information
 72  // global to the whole file is held here. The client is responsible
 73  // for filling it in appropriately (except for the 'file_private'
 74  // field, which the constructor and destructor take care of), and
 75  // then providing it to the DwarfCUToModule instance for each
 76  // compilation unit we process in that file.
 77  struct FileContext {
 78    FileContext(const string &filename_arg, Module *module_arg);
 79    ~FileContext();
 80
 81    // The name of this file, for use in error messages.
 82    string filename;
 83
 84    // A map of this file's sections, used for finding other DWARF
 85    // sections that the .debug_info section may refer to.
 86    dwarf2reader::SectionMap section_map;
 87
 88    // The Module to which we're contributing definitions.
 89    Module *module;
 90
 91    // Inter-compilation unit data used internally by the handlers.
 92    FilePrivate *file_private;
 93  };
 94
 95  // An abstract base class for functors that handle DWARF line data
 96  // for DwarfCUToModule. DwarfCUToModule could certainly just use
 97  // dwarf2reader::LineInfo itself directly, but decoupling things
 98  // this way makes unit testing a little easier.
 99  class LineToModuleFunctor {
100   public:
101    LineToModuleFunctor() { }
102    virtual ~LineToModuleFunctor() { }
103
104    // Populate MODULE and LINES with source file names and code/line
105    // mappings, given a pointer to some DWARF line number data
106    // PROGRAM, and an overestimate of its size. Add no zero-length
107    // lines to LINES.
108    virtual void operator()(const char *program, uint64 length,
109                            Module *module, vector<Module::Line> *lines) = 0;
110  };
111
112  // The interface DwarfCUToModule uses to report warnings. The member
113  // function definitions for this class write messages to stderr, but
114  // you can override them if you'd like to detect or report these
115  // conditions yourself.
116  class WarningReporter {
117   public:
118    // Warn about problems in the DWARF file FILENAME, in the
119    // compilation unit at OFFSET.
120    WarningReporter(const string &filename, uint64 cu_offset)
121        : filename_(filename), cu_offset_(cu_offset), printed_cu_header_(false),
122          printed_unpaired_header_(false),
123          uncovered_warnings_enabled_(false) { }
124    virtual ~WarningReporter() { }
125
126    // Set the name of the compilation unit we're processing to NAME.
127    virtual void SetCUName(const string &name) { cu_name_ = name; }
128
129    // Accessor and setter for uncovered_warnings_enabled_.
130    // UncoveredFunction and UncoveredLine only report a problem if that is
131    // true. By default, these warnings are disabled, because those
132    // conditions occur occasionally in healthy code.
133    virtual bool uncovered_warnings_enabled() const {
134      return uncovered_warnings_enabled_;
135    }
136    virtual void set_uncovered_warnings_enabled(bool value) {
137      uncovered_warnings_enabled_ = value;
138    }
139
140    // A DW_AT_specification in the DIE at OFFSET refers to a DIE we
141    // haven't processed yet, or that wasn't marked as a declaration,
142    // at TARGET.
143    virtual void UnknownSpecification(uint64 offset, uint64 target);
144
145    // A DW_AT_abstract_origin in the DIE at OFFSET refers to a DIE we
146    // haven't processed yet, or that wasn't marked as inline, at TARGET.
147    virtual void UnknownAbstractOrigin(uint64 offset, uint64 target);
148
149    // We were unable to find the DWARF section named SECTION_NAME.
150    virtual void MissingSection(const string &section_name);
151
152    // The CU's DW_AT_stmt_list offset OFFSET is bogus.
153    virtual void BadLineInfoOffset(uint64 offset);
154
155    // FUNCTION includes code covered by no line number data.
156    virtual void UncoveredFunction(const Module::Function &function);
157
158    // Line number NUMBER in LINE_FILE, of length LENGTH, includes code
159    // covered by no function.
160    virtual void UncoveredLine(const Module::Line &line);
161
162    // The DW_TAG_subprogram DIE at OFFSET has no name specified directly
163    // in the DIE, nor via a DW_AT_specification or DW_AT_abstract_origin
164    // link.
165    virtual void UnnamedFunction(uint64 offset);
166
167   protected:
168    string filename_;
169    uint64 cu_offset_;
170    string cu_name_;
171    bool printed_cu_header_;
172    bool printed_unpaired_header_;
173    bool uncovered_warnings_enabled_;
174
175   private:
176    // Print a per-CU heading, once.
177    void CUHeading();
178    // Print an unpaired function/line heading, once.
179    void UncoveredHeading();
180  };
181
182  // Create a DWARF debugging info handler for a compilation unit
183  // within FILE_CONTEXT. This uses information received from the
184  // dwarf2reader::CompilationUnit DWARF parser to populate
185  // FILE_CONTEXT->module. Use LINE_READER to handle the compilation
186  // unit's line number data. Use REPORTER to report problems with the
187  // data we find.
188  DwarfCUToModule(FileContext *file_context,
189                  LineToModuleFunctor *line_reader,
190                  WarningReporter *reporter);
191  ~DwarfCUToModule();
192
193  void ProcessAttributeSigned(enum DwarfAttribute attr,
194                              enum DwarfForm form,
195                              int64 data);
196  void ProcessAttributeUnsigned(enum DwarfAttribute attr,
197                                enum DwarfForm form,
198                                uint64 data);
199  void ProcessAttributeString(enum DwarfAttribute attr,
200                              enum DwarfForm form,
201                              const string &data);
202  bool EndAttributes();
203  DIEHandler *FindChildHandler(uint64 offset, enum DwarfTag tag,
204                               const AttributeList &attrs);
205
206  // Assign all our source Lines to the Functions that cover their
207  // addresses, and then add them to module_.
208  void Finish();
209
210  bool StartCompilationUnit(uint64 offset, uint8 address_size,
211                            uint8 offset_size, uint64 cu_length,
212                            uint8 dwarf_version);
213  bool StartRootDIE(uint64 offset, enum DwarfTag tag,
214                    const AttributeList& attrs);
215
216 private:
217
218  // Used internally by the handler. Full definitions are in
219  // dwarf_cu_to_module.cc.
220  struct FilePrivate;
221  struct Specification;
222  struct CUContext;
223  struct DIEContext;
224  class GenericDIEHandler;
225  class FuncHandler;
226  class NamedScopeHandler;
227
228  // A map from section offsets to specifications.
229  typedef map<uint64, Specification> SpecificationByOffset;
230
231  // Set this compilation unit's source language to LANGUAGE.
232  void SetLanguage(DwarfLanguage language);
233  
234  // Read source line information at OFFSET in the .debug_line
235  // section.  Record source files in module_, but record source lines
236  // in lines_; we apportion them to functions in
237  // AssignLinesToFunctions.
238  void ReadSourceLines(uint64 offset);
239
240  // Assign the lines in lines_ to the individual line lists of the
241  // functions in functions_.  (DWARF line information maps an entire
242  // compilation unit at a time, and gives no indication of which
243  // lines belong to which functions, beyond their addresses.)
244  void AssignLinesToFunctions();
245
246  // The only reason cu_context_ and child_context_ are pointers is
247  // that we want to keep their definitions private to
248  // dwarf_cu_to_module.cc, instead of listing them all here. They are
249  // owned by this DwarfCUToModule: the constructor sets them, and the
250  // destructor deletes them.
251
252  // The functor to use to handle line number data.
253  LineToModuleFunctor *line_reader_;
254
255  // This compilation unit's context.
256  CUContext *cu_context_;
257
258  // A context for our children.
259  DIEContext *child_context_;
260
261  // True if this compilation unit has source line information.
262  bool has_source_line_info_;
263
264  // The offset of this compilation unit's line number information in
265  // the .debug_line section.
266  uint64 source_line_offset_;
267
268  // The line numbers we have seen thus far.  We accumulate these here
269  // during parsing.  Then, in Finish, we call AssignLinesToFunctions
270  // to dole them out to the appropriate functions.
271  vector<Module::Line> lines_;
272};
273
274} // namespace google_breakpad
275
276#endif  // COMMON_LINUX_DWARF_CU_TO_MODULE_H__