|
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 // simple_symbol_supplier.cc: A simple SymbolSupplier implementation |
|
31 // |
|
32 // See simple_symbol_supplier.h for documentation. |
|
33 // |
|
34 // Author: Mark Mentovai |
|
35 |
|
36 #include "processor/simple_symbol_supplier.h" |
|
37 |
|
38 #include <assert.h> |
|
39 #include <string.h> |
|
40 #include <sys/types.h> |
|
41 #include <sys/stat.h> |
|
42 |
|
43 #include <algorithm> |
|
44 #include <iostream> |
|
45 #include <fstream> |
|
46 |
|
47 #include "common/using_std_string.h" |
|
48 #include "google_breakpad/processor/code_module.h" |
|
49 #include "google_breakpad/processor/system_info.h" |
|
50 #include "processor/logging.h" |
|
51 #include "processor/pathname_stripper.h" |
|
52 |
|
53 namespace google_breakpad { |
|
54 |
|
55 static bool file_exists(const string &file_name) { |
|
56 struct stat sb; |
|
57 return stat(file_name.c_str(), &sb) == 0; |
|
58 } |
|
59 |
|
60 SymbolSupplier::SymbolResult SimpleSymbolSupplier::GetSymbolFile( |
|
61 const CodeModule *module, const SystemInfo *system_info, |
|
62 string *symbol_file) { |
|
63 BPLOG_IF(ERROR, !symbol_file) << "SimpleSymbolSupplier::GetSymbolFile " |
|
64 "requires |symbol_file|"; |
|
65 assert(symbol_file); |
|
66 symbol_file->clear(); |
|
67 |
|
68 for (unsigned int path_index = 0; path_index < paths_.size(); ++path_index) { |
|
69 SymbolResult result; |
|
70 if ((result = GetSymbolFileAtPathFromRoot(module, system_info, |
|
71 paths_[path_index], |
|
72 symbol_file)) != NOT_FOUND) { |
|
73 return result; |
|
74 } |
|
75 } |
|
76 return NOT_FOUND; |
|
77 } |
|
78 |
|
79 SymbolSupplier::SymbolResult SimpleSymbolSupplier::GetSymbolFile( |
|
80 const CodeModule *module, |
|
81 const SystemInfo *system_info, |
|
82 string *symbol_file, |
|
83 string *symbol_data) { |
|
84 assert(symbol_data); |
|
85 symbol_data->clear(); |
|
86 |
|
87 SymbolSupplier::SymbolResult s = GetSymbolFile(module, system_info, symbol_file); |
|
88 |
|
89 if (s == FOUND) { |
|
90 std::ifstream in(symbol_file->c_str()); |
|
91 std::getline(in, *symbol_data, string::traits_type::to_char_type( |
|
92 string::traits_type::eof())); |
|
93 in.close(); |
|
94 } |
|
95 return s; |
|
96 } |
|
97 |
|
98 SymbolSupplier::SymbolResult SimpleSymbolSupplier::GetCStringSymbolData( |
|
99 const CodeModule *module, |
|
100 const SystemInfo *system_info, |
|
101 string *symbol_file, |
|
102 char **symbol_data) { |
|
103 assert(symbol_data); |
|
104 |
|
105 string symbol_data_string; |
|
106 SymbolSupplier::SymbolResult s = |
|
107 GetSymbolFile(module, system_info, symbol_file, &symbol_data_string); |
|
108 |
|
109 if (s == FOUND) { |
|
110 unsigned int size = symbol_data_string.size() + 1; |
|
111 *symbol_data = new char[size]; |
|
112 if (*symbol_data == NULL) { |
|
113 BPLOG(ERROR) << "Memory allocation for size " << size << " failed"; |
|
114 return INTERRUPT; |
|
115 } |
|
116 memcpy(*symbol_data, symbol_data_string.c_str(), size - 1); |
|
117 (*symbol_data)[size - 1] = '\0'; |
|
118 memory_buffers_.insert(make_pair(module->code_file(), *symbol_data)); |
|
119 } |
|
120 return s; |
|
121 } |
|
122 |
|
123 void SimpleSymbolSupplier::FreeSymbolData(const CodeModule *module) { |
|
124 if (!module) { |
|
125 BPLOG(INFO) << "Cannot free symbol data buffer for NULL module"; |
|
126 return; |
|
127 } |
|
128 |
|
129 map<string, char *>::iterator it = memory_buffers_.find(module->code_file()); |
|
130 if (it == memory_buffers_.end()) { |
|
131 BPLOG(INFO) << "Cannot find symbol data buffer for module " |
|
132 << module->code_file(); |
|
133 return; |
|
134 } |
|
135 delete [] it->second; |
|
136 memory_buffers_.erase(it); |
|
137 } |
|
138 |
|
139 SymbolSupplier::SymbolResult SimpleSymbolSupplier::GetSymbolFileAtPathFromRoot( |
|
140 const CodeModule *module, const SystemInfo *system_info, |
|
141 const string &root_path, string *symbol_file) { |
|
142 BPLOG_IF(ERROR, !symbol_file) << "SimpleSymbolSupplier::GetSymbolFileAtPath " |
|
143 "requires |symbol_file|"; |
|
144 assert(symbol_file); |
|
145 symbol_file->clear(); |
|
146 |
|
147 if (!module) |
|
148 return NOT_FOUND; |
|
149 |
|
150 // Start with the base path. |
|
151 string path = root_path; |
|
152 |
|
153 // Append the debug (pdb) file name as a directory name. |
|
154 path.append("/"); |
|
155 string debug_file_name = PathnameStripper::File(module->debug_file()); |
|
156 if (debug_file_name.empty()) { |
|
157 BPLOG(ERROR) << "Can't construct symbol file path without debug_file " |
|
158 "(code_file = " << |
|
159 PathnameStripper::File(module->code_file()) << ")"; |
|
160 return NOT_FOUND; |
|
161 } |
|
162 path.append(debug_file_name); |
|
163 |
|
164 // Append the identifier as a directory name. |
|
165 path.append("/"); |
|
166 string identifier = module->debug_identifier(); |
|
167 if (identifier.empty()) { |
|
168 BPLOG(ERROR) << "Can't construct symbol file path without debug_identifier " |
|
169 "(code_file = " << |
|
170 PathnameStripper::File(module->code_file()) << |
|
171 ", debug_file = " << debug_file_name << ")"; |
|
172 return NOT_FOUND; |
|
173 } |
|
174 path.append(identifier); |
|
175 |
|
176 // Transform the debug file name into one ending in .sym. If the existing |
|
177 // name ends in .pdb, strip the .pdb. Otherwise, add .sym to the non-.pdb |
|
178 // name. |
|
179 path.append("/"); |
|
180 string debug_file_extension; |
|
181 if (debug_file_name.size() > 4) |
|
182 debug_file_extension = debug_file_name.substr(debug_file_name.size() - 4); |
|
183 std::transform(debug_file_extension.begin(), debug_file_extension.end(), |
|
184 debug_file_extension.begin(), tolower); |
|
185 if (debug_file_extension == ".pdb") { |
|
186 path.append(debug_file_name.substr(0, debug_file_name.size() - 4)); |
|
187 } else { |
|
188 path.append(debug_file_name); |
|
189 } |
|
190 path.append(".sym"); |
|
191 |
|
192 if (!file_exists(path)) { |
|
193 BPLOG(INFO) << "No symbol file at " << path; |
|
194 return NOT_FOUND; |
|
195 } |
|
196 |
|
197 *symbol_file = path; |
|
198 return FOUND; |
|
199 } |
|
200 |
|
201 } // namespace google_breakpad |