michael@0: // Copyright (c) 2006, Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: // simple_symbol_supplier.cc: A simple SymbolSupplier implementation michael@0: // michael@0: // See simple_symbol_supplier.h for documentation. michael@0: // michael@0: // Author: Mark Mentovai michael@0: michael@0: #include "processor/simple_symbol_supplier.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "common/using_std_string.h" michael@0: #include "google_breakpad/processor/code_module.h" michael@0: #include "google_breakpad/processor/system_info.h" michael@0: #include "processor/logging.h" michael@0: #include "processor/pathname_stripper.h" michael@0: michael@0: namespace google_breakpad { michael@0: michael@0: static bool file_exists(const string &file_name) { michael@0: struct stat sb; michael@0: return stat(file_name.c_str(), &sb) == 0; michael@0: } michael@0: michael@0: SymbolSupplier::SymbolResult SimpleSymbolSupplier::GetSymbolFile( michael@0: const CodeModule *module, const SystemInfo *system_info, michael@0: string *symbol_file) { michael@0: BPLOG_IF(ERROR, !symbol_file) << "SimpleSymbolSupplier::GetSymbolFile " michael@0: "requires |symbol_file|"; michael@0: assert(symbol_file); michael@0: symbol_file->clear(); michael@0: michael@0: for (unsigned int path_index = 0; path_index < paths_.size(); ++path_index) { michael@0: SymbolResult result; michael@0: if ((result = GetSymbolFileAtPathFromRoot(module, system_info, michael@0: paths_[path_index], michael@0: symbol_file)) != NOT_FOUND) { michael@0: return result; michael@0: } michael@0: } michael@0: return NOT_FOUND; michael@0: } michael@0: michael@0: SymbolSupplier::SymbolResult SimpleSymbolSupplier::GetSymbolFile( michael@0: const CodeModule *module, michael@0: const SystemInfo *system_info, michael@0: string *symbol_file, michael@0: string *symbol_data) { michael@0: assert(symbol_data); michael@0: symbol_data->clear(); michael@0: michael@0: SymbolSupplier::SymbolResult s = GetSymbolFile(module, system_info, symbol_file); michael@0: michael@0: if (s == FOUND) { michael@0: std::ifstream in(symbol_file->c_str()); michael@0: std::getline(in, *symbol_data, string::traits_type::to_char_type( michael@0: string::traits_type::eof())); michael@0: in.close(); michael@0: } michael@0: return s; michael@0: } michael@0: michael@0: SymbolSupplier::SymbolResult SimpleSymbolSupplier::GetCStringSymbolData( michael@0: const CodeModule *module, michael@0: const SystemInfo *system_info, michael@0: string *symbol_file, michael@0: char **symbol_data) { michael@0: assert(symbol_data); michael@0: michael@0: string symbol_data_string; michael@0: SymbolSupplier::SymbolResult s = michael@0: GetSymbolFile(module, system_info, symbol_file, &symbol_data_string); michael@0: michael@0: if (s == FOUND) { michael@0: unsigned int size = symbol_data_string.size() + 1; michael@0: *symbol_data = new char[size]; michael@0: if (*symbol_data == NULL) { michael@0: BPLOG(ERROR) << "Memory allocation for size " << size << " failed"; michael@0: return INTERRUPT; michael@0: } michael@0: memcpy(*symbol_data, symbol_data_string.c_str(), size - 1); michael@0: (*symbol_data)[size - 1] = '\0'; michael@0: memory_buffers_.insert(make_pair(module->code_file(), *symbol_data)); michael@0: } michael@0: return s; michael@0: } michael@0: michael@0: void SimpleSymbolSupplier::FreeSymbolData(const CodeModule *module) { michael@0: if (!module) { michael@0: BPLOG(INFO) << "Cannot free symbol data buffer for NULL module"; michael@0: return; michael@0: } michael@0: michael@0: map::iterator it = memory_buffers_.find(module->code_file()); michael@0: if (it == memory_buffers_.end()) { michael@0: BPLOG(INFO) << "Cannot find symbol data buffer for module " michael@0: << module->code_file(); michael@0: return; michael@0: } michael@0: delete [] it->second; michael@0: memory_buffers_.erase(it); michael@0: } michael@0: michael@0: SymbolSupplier::SymbolResult SimpleSymbolSupplier::GetSymbolFileAtPathFromRoot( michael@0: const CodeModule *module, const SystemInfo *system_info, michael@0: const string &root_path, string *symbol_file) { michael@0: BPLOG_IF(ERROR, !symbol_file) << "SimpleSymbolSupplier::GetSymbolFileAtPath " michael@0: "requires |symbol_file|"; michael@0: assert(symbol_file); michael@0: symbol_file->clear(); michael@0: michael@0: if (!module) michael@0: return NOT_FOUND; michael@0: michael@0: // Start with the base path. michael@0: string path = root_path; michael@0: michael@0: // Append the debug (pdb) file name as a directory name. michael@0: path.append("/"); michael@0: string debug_file_name = PathnameStripper::File(module->debug_file()); michael@0: if (debug_file_name.empty()) { michael@0: BPLOG(ERROR) << "Can't construct symbol file path without debug_file " michael@0: "(code_file = " << michael@0: PathnameStripper::File(module->code_file()) << ")"; michael@0: return NOT_FOUND; michael@0: } michael@0: path.append(debug_file_name); michael@0: michael@0: // Append the identifier as a directory name. michael@0: path.append("/"); michael@0: string identifier = module->debug_identifier(); michael@0: if (identifier.empty()) { michael@0: BPLOG(ERROR) << "Can't construct symbol file path without debug_identifier " michael@0: "(code_file = " << michael@0: PathnameStripper::File(module->code_file()) << michael@0: ", debug_file = " << debug_file_name << ")"; michael@0: return NOT_FOUND; michael@0: } michael@0: path.append(identifier); michael@0: michael@0: // Transform the debug file name into one ending in .sym. If the existing michael@0: // name ends in .pdb, strip the .pdb. Otherwise, add .sym to the non-.pdb michael@0: // name. michael@0: path.append("/"); michael@0: string debug_file_extension; michael@0: if (debug_file_name.size() > 4) michael@0: debug_file_extension = debug_file_name.substr(debug_file_name.size() - 4); michael@0: std::transform(debug_file_extension.begin(), debug_file_extension.end(), michael@0: debug_file_extension.begin(), tolower); michael@0: if (debug_file_extension == ".pdb") { michael@0: path.append(debug_file_name.substr(0, debug_file_name.size() - 4)); michael@0: } else { michael@0: path.append(debug_file_name); michael@0: } michael@0: path.append(".sym"); michael@0: michael@0: if (!file_exists(path)) { michael@0: BPLOG(INFO) << "No symbol file at " << path; michael@0: return NOT_FOUND; michael@0: } michael@0: michael@0: *symbol_file = path; michael@0: return FOUND; michael@0: } michael@0: michael@0: } // namespace google_breakpad