toolkit/crashreporter/google-breakpad/src/processor/simple_symbol_supplier.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/crashreporter/google-breakpad/src/processor/simple_symbol_supplier.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,201 @@
     1.4 +// Copyright (c) 2006, Google Inc.
     1.5 +// All rights reserved.
     1.6 +//
     1.7 +// Redistribution and use in source and binary forms, with or without
     1.8 +// modification, are permitted provided that the following conditions are
     1.9 +// met:
    1.10 +//
    1.11 +//     * Redistributions of source code must retain the above copyright
    1.12 +// notice, this list of conditions and the following disclaimer.
    1.13 +//     * Redistributions in binary form must reproduce the above
    1.14 +// copyright notice, this list of conditions and the following disclaimer
    1.15 +// in the documentation and/or other materials provided with the
    1.16 +// distribution.
    1.17 +//     * Neither the name of Google Inc. nor the names of its
    1.18 +// contributors may be used to endorse or promote products derived from
    1.19 +// this software without specific prior written permission.
    1.20 +//
    1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.32 +
    1.33 +// simple_symbol_supplier.cc: A simple SymbolSupplier implementation
    1.34 +//
    1.35 +// See simple_symbol_supplier.h for documentation.
    1.36 +//
    1.37 +// Author: Mark Mentovai
    1.38 +
    1.39 +#include "processor/simple_symbol_supplier.h"
    1.40 +
    1.41 +#include <assert.h>
    1.42 +#include <string.h>
    1.43 +#include <sys/types.h>
    1.44 +#include <sys/stat.h>
    1.45 +
    1.46 +#include <algorithm>
    1.47 +#include <iostream>
    1.48 +#include <fstream>
    1.49 +
    1.50 +#include "common/using_std_string.h"
    1.51 +#include "google_breakpad/processor/code_module.h"
    1.52 +#include "google_breakpad/processor/system_info.h"
    1.53 +#include "processor/logging.h"
    1.54 +#include "processor/pathname_stripper.h"
    1.55 +
    1.56 +namespace google_breakpad {
    1.57 +
    1.58 +static bool file_exists(const string &file_name) {
    1.59 +  struct stat sb;
    1.60 +  return stat(file_name.c_str(), &sb) == 0;
    1.61 +}
    1.62 +
    1.63 +SymbolSupplier::SymbolResult SimpleSymbolSupplier::GetSymbolFile(
    1.64 +    const CodeModule *module, const SystemInfo *system_info,
    1.65 +    string *symbol_file) {
    1.66 +  BPLOG_IF(ERROR, !symbol_file) << "SimpleSymbolSupplier::GetSymbolFile "
    1.67 +                                   "requires |symbol_file|";
    1.68 +  assert(symbol_file);
    1.69 +  symbol_file->clear();
    1.70 +
    1.71 +  for (unsigned int path_index = 0; path_index < paths_.size(); ++path_index) {
    1.72 +    SymbolResult result;
    1.73 +    if ((result = GetSymbolFileAtPathFromRoot(module, system_info,
    1.74 +                                              paths_[path_index],
    1.75 +                                              symbol_file)) != NOT_FOUND) {
    1.76 +      return result;
    1.77 +    }
    1.78 +  }
    1.79 +  return NOT_FOUND;
    1.80 +}
    1.81 +
    1.82 +SymbolSupplier::SymbolResult SimpleSymbolSupplier::GetSymbolFile(
    1.83 +    const CodeModule *module,
    1.84 +    const SystemInfo *system_info,
    1.85 +    string *symbol_file,
    1.86 +    string *symbol_data) {
    1.87 +  assert(symbol_data);
    1.88 +  symbol_data->clear();
    1.89 +
    1.90 +  SymbolSupplier::SymbolResult s = GetSymbolFile(module, system_info, symbol_file);
    1.91 +
    1.92 +  if (s == FOUND) {
    1.93 +    std::ifstream in(symbol_file->c_str());
    1.94 +    std::getline(in, *symbol_data, string::traits_type::to_char_type(
    1.95 +                     string::traits_type::eof()));
    1.96 +    in.close();
    1.97 +  }
    1.98 +  return s;
    1.99 +}
   1.100 +
   1.101 +SymbolSupplier::SymbolResult SimpleSymbolSupplier::GetCStringSymbolData(
   1.102 +    const CodeModule *module,
   1.103 +    const SystemInfo *system_info,
   1.104 +    string *symbol_file,
   1.105 +    char **symbol_data) {
   1.106 +  assert(symbol_data);
   1.107 +
   1.108 +  string symbol_data_string;
   1.109 +  SymbolSupplier::SymbolResult s =
   1.110 +      GetSymbolFile(module, system_info, symbol_file, &symbol_data_string);
   1.111 +
   1.112 +  if (s == FOUND) {
   1.113 +    unsigned int size = symbol_data_string.size() + 1;
   1.114 +    *symbol_data = new char[size];
   1.115 +    if (*symbol_data == NULL) {
   1.116 +      BPLOG(ERROR) << "Memory allocation for size " << size << " failed";
   1.117 +      return INTERRUPT;
   1.118 +    }
   1.119 +    memcpy(*symbol_data, symbol_data_string.c_str(), size - 1);
   1.120 +    (*symbol_data)[size - 1] = '\0';
   1.121 +    memory_buffers_.insert(make_pair(module->code_file(), *symbol_data));
   1.122 +  }
   1.123 +  return s;
   1.124 +}
   1.125 +
   1.126 +void SimpleSymbolSupplier::FreeSymbolData(const CodeModule *module) {
   1.127 +  if (!module) {
   1.128 +    BPLOG(INFO) << "Cannot free symbol data buffer for NULL module";
   1.129 +    return;
   1.130 +  }
   1.131 +
   1.132 +  map<string, char *>::iterator it = memory_buffers_.find(module->code_file());
   1.133 +  if (it == memory_buffers_.end()) {
   1.134 +    BPLOG(INFO) << "Cannot find symbol data buffer for module "
   1.135 +                << module->code_file();
   1.136 +    return;
   1.137 +  }
   1.138 +  delete [] it->second;
   1.139 +  memory_buffers_.erase(it);
   1.140 +}
   1.141 +
   1.142 +SymbolSupplier::SymbolResult SimpleSymbolSupplier::GetSymbolFileAtPathFromRoot(
   1.143 +    const CodeModule *module, const SystemInfo *system_info,
   1.144 +    const string &root_path, string *symbol_file) {
   1.145 +  BPLOG_IF(ERROR, !symbol_file) << "SimpleSymbolSupplier::GetSymbolFileAtPath "
   1.146 +                                   "requires |symbol_file|";
   1.147 +  assert(symbol_file);
   1.148 +  symbol_file->clear();
   1.149 +
   1.150 +  if (!module)
   1.151 +    return NOT_FOUND;
   1.152 +
   1.153 +  // Start with the base path.
   1.154 +  string path = root_path;
   1.155 +
   1.156 +  // Append the debug (pdb) file name as a directory name.
   1.157 +  path.append("/");
   1.158 +  string debug_file_name = PathnameStripper::File(module->debug_file());
   1.159 +  if (debug_file_name.empty()) {
   1.160 +    BPLOG(ERROR) << "Can't construct symbol file path without debug_file "
   1.161 +                    "(code_file = " <<
   1.162 +                    PathnameStripper::File(module->code_file()) << ")";
   1.163 +    return NOT_FOUND;
   1.164 +  }
   1.165 +  path.append(debug_file_name);
   1.166 +
   1.167 +  // Append the identifier as a directory name.
   1.168 +  path.append("/");
   1.169 +  string identifier = module->debug_identifier();
   1.170 +  if (identifier.empty()) {
   1.171 +    BPLOG(ERROR) << "Can't construct symbol file path without debug_identifier "
   1.172 +                    "(code_file = " <<
   1.173 +                    PathnameStripper::File(module->code_file()) <<
   1.174 +                    ", debug_file = " << debug_file_name << ")";
   1.175 +    return NOT_FOUND;
   1.176 +  }
   1.177 +  path.append(identifier);
   1.178 +
   1.179 +  // Transform the debug file name into one ending in .sym.  If the existing
   1.180 +  // name ends in .pdb, strip the .pdb.  Otherwise, add .sym to the non-.pdb
   1.181 +  // name.
   1.182 +  path.append("/");
   1.183 +  string debug_file_extension;
   1.184 +  if (debug_file_name.size() > 4)
   1.185 +    debug_file_extension = debug_file_name.substr(debug_file_name.size() - 4);
   1.186 +  std::transform(debug_file_extension.begin(), debug_file_extension.end(),
   1.187 +                 debug_file_extension.begin(), tolower);
   1.188 +  if (debug_file_extension == ".pdb") {
   1.189 +    path.append(debug_file_name.substr(0, debug_file_name.size() - 4));
   1.190 +  } else {
   1.191 +    path.append(debug_file_name);
   1.192 +  }
   1.193 +  path.append(".sym");
   1.194 +
   1.195 +  if (!file_exists(path)) {
   1.196 +    BPLOG(INFO) << "No symbol file at " << path;
   1.197 +    return NOT_FOUND;
   1.198 +  }
   1.199 +
   1.200 +  *symbol_file = path;
   1.201 +  return FOUND;
   1.202 +}
   1.203 +
   1.204 +}  // namespace google_breakpad

mercurial