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 // dump_stabs.h: Define the StabsToModule class, which receives
35 // STABS debugging information from a parser and adds it to a Breakpad
36 // symbol file.
37 
38 #ifndef BREAKPAD_COMMON_STABS_TO_MODULE_H_
39 #define BREAKPAD_COMMON_STABS_TO_MODULE_H_
40 
41 #include <stdint.h>
42 
43 #include <string>
44 #include <vector>
45 
46 #include "common/module.h"
47 #include "common/stabs_reader.h"
48 #include "common/using_std_string.h"
49 
50 namespace google_breakpad {
51 
52 using std::vector;
53 
54 // A StabsToModule is a handler that receives parsed STABS debugging
55 // information from a StabsReader, and uses that to populate
56 // a Module. (All classes are in the google_breakpad namespace.) A
57 // Module represents the contents of a Breakpad symbol file, and knows
58 // how to write itself out as such. A StabsToModule thus acts as
59 // the bridge between STABS and Breakpad data.
60 // When processing Darwin Mach-O files, this also receives public linker
61 // symbols, like those found in system libraries.
62 class StabsToModule: public google_breakpad::StabsHandler {
63  public:
64   // Receive parsed debugging information from a StabsReader, and
65   // store it all in MODULE.
StabsToModule(Module * module)66   StabsToModule(Module *module) :
67       module_(module),
68       in_compilation_unit_(false),
69       comp_unit_base_address_(0),
70       current_function_(NULL),
71       current_source_file_(NULL),
72       current_source_file_name_(NULL) { }
73   ~StabsToModule();
74 
75   // The standard StabsHandler virtual member functions.
76   bool StartCompilationUnit(const char *name, uint64_t address,
77                             const char *build_directory);
78   bool EndCompilationUnit(uint64_t address);
79   bool StartFunction(const string &name, uint64_t address);
80   bool EndFunction(uint64_t address);
81   bool Line(uint64_t address, const char *name, int number);
82   bool Extern(const string &name, uint64_t address);
83   void Warning(const char *format, ...);
84 
85   // Do any final processing necessary to make module_ contain all the
86   // data provided by the STABS reader.
87   //
88   // Because STABS does not provide reliable size information for
89   // functions and lines, we need to make a pass over the data after
90   // processing all the STABS to compute those sizes.  We take care of
91   // that here.
92   void Finalize();
93 
94  private:
95 
96   // An arbitrary, but very large, size to use for functions whose
97   // size we can't compute properly.
98   static const uint64_t kFallbackSize = 0x10000000;
99 
100   // The module we're contributing debugging info to.
101   Module *module_;
102 
103   // The functions we've generated so far.  We don't add these to
104   // module_ as we parse them.  Instead, we wait until we've computed
105   // their ending address, and their lines' ending addresses.
106   //
107   // We could just stick them in module_ from the outset, but if
108   // module_ already contains data gathered from other debugging
109   // formats, that would complicate the size computation.
110   vector<Module::Function *> functions_;
111 
112   // Boundary addresses.  STABS doesn't necessarily supply sizes for
113   // functions and lines, so we need to compute them ourselves by
114   // finding the next object.
115   vector<Module::Address> boundaries_;
116 
117   // True if we are currently within a compilation unit: we have gotten a
118   // StartCompilationUnit call, but no matching EndCompilationUnit call
119   // yet. We use this for sanity checks.
120   bool in_compilation_unit_;
121 
122   // The base address of the current compilation unit.  We use this to
123   // recognize functions we should omit from the symbol file.  (If you
124   // know the details of why we omit these, please patch this
125   // comment.)
126   Module::Address comp_unit_base_address_;
127 
128   // The function we're currently contributing lines to.
129   Module::Function *current_function_;
130 
131   // The last Module::File we got a line number in.
132   Module::File *current_source_file_;
133 
134   // The pointer in the .stabstr section of the name that
135   // current_source_file_ is built from.  This allows us to quickly
136   // recognize when the current line is in the same file as the
137   // previous one (which it usually is).
138   const char *current_source_file_name_;
139 };
140 
141 }  // namespace google_breakpad
142 
143 #endif  // BREAKPAD_COMMON_STABS_TO_MODULE_H_
144