source_line_resolver_base.cc 10.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
// Copyright (c) 2010 Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// source_line_resolver_base.cc: Implementation of SourceLineResolverBase.
//
// See source_line_resolver_base.h and source_line_resolver_base_types.h for
// more documentation.
//
// Author: Siyang Xie (lambxsy@google.com)

#include <stdio.h>
#include <string.h>
#include <sys/stat.h>

#include <map>
#include <utility>

#include "google_breakpad/processor/source_line_resolver_base.h"
#include "processor/source_line_resolver_base_types.h"
#include "processor/module_factory.h"

using std::map;
using std::make_pair;

namespace google_breakpad {

SourceLineResolverBase::SourceLineResolverBase(
    ModuleFactory *module_factory)
  : modules_(new ModuleMap),
    corrupt_modules_(new ModuleSet),
    memory_buffers_(new MemoryMap),
    module_factory_(module_factory) {
}

SourceLineResolverBase::~SourceLineResolverBase() {
  ModuleMap::iterator it;
  // Iterate through ModuleMap and delete all loaded modules.
  for (it = modules_->begin(); it != modules_->end(); ++it) {
    // Delete individual module.
    delete it->second;
  }
  // Delete the map of modules.
  delete modules_;
  modules_ = NULL;

  // Delete the set of corrupt modules.
  delete corrupt_modules_;
  corrupt_modules_ = NULL;

  MemoryMap::iterator iter = memory_buffers_->begin();
  for (; iter != memory_buffers_->end(); ++iter) {
    delete [] iter->second;
  }
  // Delete the map of memory buffers.
  delete memory_buffers_;
  memory_buffers_ = NULL;

  delete module_factory_;
  module_factory_ = NULL;
}

bool SourceLineResolverBase::ReadSymbolFile(const string &map_file,
                                            char **symbol_data,
                                            size_t *symbol_data_size) {
  if (symbol_data == NULL || symbol_data_size == NULL) {
    BPLOG(ERROR) << "Could not Read file into Null memory pointer";
    return false;
  }

  struct stat buf;
  int error_code = stat(map_file.c_str(), &buf);
  if (error_code == -1) {
    string error_string;
    error_code = ErrnoString(&error_string);
    BPLOG(ERROR) << "Could not open " << map_file <<
        ", error " << error_code << ": " << error_string;
    return false;
  }

  off_t file_size = buf.st_size;

  // Allocate memory for file contents, plus a null terminator
  // since we may use strtok() on the contents.
  *symbol_data_size = file_size + 1;
  *symbol_data = new char[file_size + 1];

  if (*symbol_data == NULL) {
    BPLOG(ERROR) << "Could not allocate memory for " << map_file;
    return false;
  }

  BPLOG(INFO) << "Opening " << map_file;

  FILE *f = fopen(map_file.c_str(), "rt");
  if (!f) {
    string error_string;
    error_code = ErrnoString(&error_string);
    BPLOG(ERROR) << "Could not open " << map_file <<
        ", error " << error_code << ": " << error_string;
    delete [] (*symbol_data);
    *symbol_data = NULL;
    return false;
  }

  AutoFileCloser closer(f);

  int items_read = 0;

  items_read = fread(*symbol_data, 1, file_size, f);

  if (items_read != file_size) {
    string error_string;
    error_code = ErrnoString(&error_string);
    BPLOG(ERROR) << "Could not slurp " << map_file <<
        ", error " << error_code << ": " << error_string;
    delete [] (*symbol_data);
    *symbol_data = NULL;
    return false;
  }

  (*symbol_data)[file_size] = '\0';
  return true;
}

bool SourceLineResolverBase::LoadModule(const CodeModule *module,
                                        const string &map_file) {
  if (module == NULL)
    return false;

  // Make sure we don't already have a module with the given name.
  if (modules_->find(module->code_file()) != modules_->end()) {
    BPLOG(INFO) << "Symbols for module " << module->code_file()
                << " already loaded";
    return false;
  }

  BPLOG(INFO) << "Loading symbols for module " << module->code_file()
              << " from " << map_file;

  char *memory_buffer;
  size_t memory_buffer_size;
  if (!ReadSymbolFile(map_file, &memory_buffer, &memory_buffer_size))
    return false;

  BPLOG(INFO) << "Read symbol file " << map_file << " succeeded";

  bool load_result = LoadModuleUsingMemoryBuffer(module, memory_buffer,
                                                 memory_buffer_size);

  if (load_result && !ShouldDeleteMemoryBufferAfterLoadModule()) {
    // memory_buffer has to stay alive as long as the module.
    memory_buffers_->insert(make_pair(module->code_file(), memory_buffer));
  } else {
    delete [] memory_buffer;
  }

  return load_result;
}

bool SourceLineResolverBase::LoadModuleUsingMapBuffer(
    const CodeModule *module, const string &map_buffer) {
  if (module == NULL)
    return false;

  // Make sure we don't already have a module with the given name.
  if (modules_->find(module->code_file()) != modules_->end()) {
    BPLOG(INFO) << "Symbols for module " << module->code_file()
                << " already loaded";
    return false;
  }

  size_t memory_buffer_size = map_buffer.size() + 1;
  char *memory_buffer = new char[memory_buffer_size];
  if (memory_buffer == NULL) {
    BPLOG(ERROR) << "Could not allocate memory for " << module->code_file();
    return false;
  }

  // Can't use strcpy, as the data may contain '\0's before the end.
  memcpy(memory_buffer, map_buffer.c_str(), map_buffer.size());
  memory_buffer[map_buffer.size()] = '\0';

  bool load_result = LoadModuleUsingMemoryBuffer(module, memory_buffer,
                                                 memory_buffer_size);

  if (load_result && !ShouldDeleteMemoryBufferAfterLoadModule()) {
    // memory_buffer has to stay alive as long as the module.
    memory_buffers_->insert(make_pair(module->code_file(), memory_buffer));
  } else {
    delete [] memory_buffer;
  }

  return load_result;
}

bool SourceLineResolverBase::LoadModuleUsingMemoryBuffer(
    const CodeModule *module,
    char *memory_buffer,
    size_t memory_buffer_size) {
  if (!module)
    return false;

  // Make sure we don't already have a module with the given name.
  if (modules_->find(module->code_file()) != modules_->end()) {
    BPLOG(INFO) << "Symbols for module " << module->code_file()
                << " already loaded";
    return false;
  }

  BPLOG(INFO) << "Loading symbols for module " << module->code_file()
             << " from memory buffer";

  Module *basic_module = module_factory_->CreateModule(module->code_file());

  // Ownership of memory is NOT transfered to Module::LoadMapFromMemory().
  if (!basic_module->LoadMapFromMemory(memory_buffer, memory_buffer_size)) {
    BPLOG(ERROR) << "Too many error while parsing symbol data for module "
                 << module->code_file();
    // Returning false from here would be an indication that the symbols for
    // this module are missing which would be wrong.  Intentionally fall through
    // and add the module to both the modules_ and the corrupt_modules_ lists.
    assert(basic_module->IsCorrupt());
  }

  modules_->insert(make_pair(module->code_file(), basic_module));
  if (basic_module->IsCorrupt()) {
    corrupt_modules_->insert(module->code_file());
  }
  return true;
}

bool SourceLineResolverBase::ShouldDeleteMemoryBufferAfterLoadModule() {
  return true;
}

void SourceLineResolverBase::UnloadModule(const CodeModule *code_module) {
  if (!code_module)
    return;

  ModuleMap::iterator mod_iter = modules_->find(code_module->code_file());
  if (mod_iter != modules_->end()) {
    Module *symbol_module = mod_iter->second;
    delete symbol_module;
    corrupt_modules_->erase(mod_iter->first);
    modules_->erase(mod_iter);
  }

  if (ShouldDeleteMemoryBufferAfterLoadModule()) {
    // No-op.  Because we never store any memory buffers.
  } else {
    // There may be a buffer stored locally, we need to find and delete it.
    MemoryMap::iterator iter = memory_buffers_->find(code_module->code_file());
    if (iter != memory_buffers_->end()) {
      delete [] iter->second;
      memory_buffers_->erase(iter);
    }
  }
}

bool SourceLineResolverBase::HasModule(const CodeModule *module) {
  if (!module)
    return false;
  return modules_->find(module->code_file()) != modules_->end();
}

bool SourceLineResolverBase::IsModuleCorrupt(const CodeModule *module) {
  if (!module)
    return false;
  return corrupt_modules_->find(module->code_file()) != corrupt_modules_->end();
}

void SourceLineResolverBase::FillSourceLineInfo(StackFrame *frame) {
  if (frame->module) {
    ModuleMap::const_iterator it = modules_->find(frame->module->code_file());
    if (it != modules_->end()) {
      it->second->LookupAddress(frame);
    }
  }
}

WindowsFrameInfo *SourceLineResolverBase::FindWindowsFrameInfo(
    const StackFrame *frame) {
  if (frame->module) {
    ModuleMap::const_iterator it = modules_->find(frame->module->code_file());
    if (it != modules_->end()) {
      return it->second->FindWindowsFrameInfo(frame);
    }
  }
  return NULL;
}

CFIFrameInfo *SourceLineResolverBase::FindCFIFrameInfo(
    const StackFrame *frame) {
  if (frame->module) {
    ModuleMap::const_iterator it = modules_->find(frame->module->code_file());
    if (it != modules_->end()) {
      return it->second->FindCFIFrameInfo(frame);
    }
  }
  return NULL;
}

bool SourceLineResolverBase::CompareString::operator()(
    const string &s1, const string &s2) const {
  return strcmp(s1.c_str(), s2.c_str()) < 0;
}

bool SourceLineResolverBase::Module::ParseCFIRuleSet(
    const string &rule_set, CFIFrameInfo *frame_info) const {
  CFIFrameInfoParseHandler handler(frame_info);
  CFIRuleParser parser(&handler);
  return parser.Parse(rule_set);
}

}  // namespace google_breakpad