OLD | NEW |
| (Empty) |
1 // -*- mode: c++ -*- | |
2 | |
3 // Copyright (c) 2010 Google Inc. All Rights Reserved. | |
4 // | |
5 // Redistribution and use in source and binary forms, with or without | |
6 // modification, are permitted provided that the following conditions are | |
7 // met: | |
8 // | |
9 // * Redistributions of source code must retain the above copyright | |
10 // notice, this list of conditions and the following disclaimer. | |
11 // * Redistributions in binary form must reproduce the above | |
12 // copyright notice, this list of conditions and the following disclaimer | |
13 // in the documentation and/or other materials provided with the | |
14 // distribution. | |
15 // * Neither the name of Google Inc. nor the names of its | |
16 // contributors may be used to endorse or promote products derived from | |
17 // this software without specific prior written permission. | |
18 // | |
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
30 | |
31 // Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com> | |
32 | |
33 // stabs_reader.h: Define StabsReader, a parser for STABS debugging | |
34 // information. A description of the STABS debugging format can be | |
35 // found at: | |
36 // | |
37 // http://sourceware.org/gdb/current/onlinedocs/stabs_toc.html | |
38 // | |
39 // The comments here assume you understand the format. | |
40 // | |
41 // This parser assumes that the system's <a.out.h> and <stab.h> | |
42 // headers accurately describe the layout of the STABS data; this code | |
43 // will not parse STABS data for a system with a different address | |
44 // size or endianness. | |
45 | |
46 #ifndef COMMON_LINUX_STABS_READER_H__ | |
47 #define COMMON_LINUX_STABS_READER_H__ | |
48 | |
49 #include "dwarf\types.h" | |
50 //#include <stdint.h> | |
51 #include <cstddef> | |
52 //#include <a.out.h> | |
53 | |
54 #include <string> | |
55 | |
56 namespace google_breakpad { | |
57 | |
58 class StabsHandler; | |
59 | |
60 class StabsReader { | |
61 public: | |
62 // Create a reader for the STABS debug information whose .stab | |
63 // section is the STAB_SIZE bytes at STAB, and whose .stabstr | |
64 // section is the STABSTR_SIZE bytes at STABSTR. The reader will | |
65 // call the member functions of HANDLER to report the information it | |
66 // finds, when the reader's 'Process' member function is called. | |
67 // | |
68 // Note that, in ELF, the .stabstr section should be found using the | |
69 // 'sh_link' field of the .stab section header, not by name. | |
70 StabsReader(const uint8_t *stab, size_t stab_size, | |
71 const uint8_t *stabstr, size_t stabstr_size, | |
72 StabsHandler *handler); | |
73 | |
74 // Process the STABS data, calling the handler's member functions to | |
75 // report what we find. While the handler functions return true, | |
76 // continue to process until we reach the end of the section. If we | |
77 // processed the entire section and all handlers returned true, | |
78 // return true. If any handler returned false, return false. | |
79 bool Process(); | |
80 | |
81 private: | |
82 // Return the name of the current symbol. | |
83 const char *SymbolString(); | |
84 | |
85 // Return the value of the current symbol. | |
86 const uint64_t SymbolValue() { | |
87 return symbol_->n_value; | |
88 } | |
89 | |
90 // Process a compilation unit starting at symbol_. Return true | |
91 // to continue processing, or false to abort. | |
92 bool ProcessCompilationUnit(); | |
93 | |
94 // Process a function in current_source_file_ starting at symbol_. | |
95 // Return true to continue processing, or false to abort. | |
96 bool ProcessFunction(); | |
97 | |
98 // The debugging information we're reading. | |
99 const struct nlist *symbols_, *symbols_end_; | |
100 const uint8_t *stabstr_; | |
101 size_t stabstr_size_; | |
102 | |
103 StabsHandler *handler_; | |
104 | |
105 // The offset of the current compilation unit's strings within stabstr_. | |
106 size_t string_offset_; | |
107 | |
108 // The value string_offset_ should have for the next compilation unit, | |
109 // as established by N_UNDF entries. | |
110 size_t next_cu_string_offset_; | |
111 | |
112 // The current symbol we're processing. | |
113 const struct nlist *symbol_; | |
114 | |
115 // The current source file name. | |
116 const char *current_source_file_; | |
117 }; | |
118 | |
119 // Consumer-provided callback structure for the STABS reader. Clients | |
120 // of the STABS reader provide an instance of this structure. The | |
121 // reader then invokes the member functions of that instance to report | |
122 // the information it finds. | |
123 // | |
124 // The default definitions of the member functions do nothing, and return | |
125 // true so processing will continue. | |
126 class StabsHandler { | |
127 public: | |
128 StabsHandler() { } | |
129 virtual ~StabsHandler() { } | |
130 | |
131 // Some general notes about the handler callback functions: | |
132 | |
133 // Processing proceeds until the end of the .stabs section, or until | |
134 // one of these functions returns false. | |
135 | |
136 // The addresses given are as reported in the STABS info, without | |
137 // regard for whether the module may be loaded at different | |
138 // addresses at different times (a shared library, say). When | |
139 // processing STABS from an ELF shared library, the addresses given | |
140 // all assume the library is loaded at its nominal load address. | |
141 // They are *not* offsets from the nominal load address. If you | |
142 // want offsets, you must subtract off the library's nominal load | |
143 // address. | |
144 | |
145 // The arguments to these functions named FILENAME are all | |
146 // references to strings stored in the .stabstr section. Because | |
147 // both the Linux and Solaris linkers factor out duplicate strings | |
148 // from the .stabstr section, the consumer can assume that if two | |
149 // FILENAME values are different addresses, they represent different | |
150 // file names. | |
151 // | |
152 // Thus, it's safe to use (say) std::map<char *, ...>, which does | |
153 // string address comparisons, not string content comparisons. | |
154 // Since all the strings are in same array of characters --- the | |
155 // .stabstr section --- comparing their addresses produces | |
156 // predictable, if not lexicographically meaningful, results. | |
157 | |
158 // Begin processing a compilation unit whose main source file is | |
159 // named FILENAME, and whose base address is ADDRESS. If | |
160 // BUILD_DIRECTORY is non-NULL, it is the name of the build | |
161 // directory in which the compilation occurred. | |
162 virtual bool StartCompilationUnit(const char *filename, uint64_t address, | |
163 const char *build_directory) { | |
164 return true; | |
165 } | |
166 | |
167 // Finish processing the compilation unit. If ADDRESS is non-zero, | |
168 // it is the ending address of the compilation unit. If ADDRESS is | |
169 // zero, then the compilation unit's ending address is not | |
170 // available, and the consumer must infer it by other means. | |
171 virtual bool EndCompilationUnit(uint64_t address) { return true; } | |
172 | |
173 // Begin processing a function named NAME, whose starting address is | |
174 // ADDRESS. This function belongs to the compilation unit that was | |
175 // most recently started but not ended. | |
176 // | |
177 // Note that, unlike filenames, NAME is not a pointer into the | |
178 // .stabstr section; this is because the name as it appears in the | |
179 // STABS data is followed by type information. The value passed to | |
180 // StartFunction is the function name alone. | |
181 // | |
182 // In languages that use name mangling, like C++, NAME is mangled. | |
183 virtual bool StartFunction(const std::string &name, uint64_t address) { | |
184 return true; | |
185 } | |
186 | |
187 // Finish processing the function. If ADDRESS is non-zero, it is | |
188 // the ending address for the function. If ADDRESS is zero, then | |
189 // the function's ending address is not available, and the consumer | |
190 // must infer it by other means. | |
191 virtual bool EndFunction(uint64_t address) { return true; } | |
192 | |
193 // Report that the code at ADDRESS is attributable to line NUMBER of | |
194 // the source file named FILENAME. The caller must infer the ending | |
195 // address of the line. | |
196 virtual bool Line(uint64_t address, const char *filename, int number) { | |
197 return true; | |
198 } | |
199 | |
200 // Report a warning. FORMAT is a printf-like format string, | |
201 // specifying how to format the subsequent arguments. | |
202 virtual void Warning(const char *format, ...) = 0; | |
203 }; | |
204 | |
205 } // namespace google_breakpad | |
206 | |
207 #endif // COMMON_LINUX_STABS_READER_H__ | |
OLD | NEW |