PageRenderTime 36ms CodeModel.GetById 14ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 1ms

/thirdparty/breakpad/processor/simple_symbol_supplier.cc

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