1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/crashreporter/google-breakpad/src/common/stabs_to_module.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,200 @@ 1.4 +// Copyright (c) 2010 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 +// Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com> 1.34 + 1.35 +// dump_stabs.cc --- implement the StabsToModule class. 1.36 + 1.37 +#include <assert.h> 1.38 +#include <cxxabi.h> 1.39 +#include <stdarg.h> 1.40 +#include <stdio.h> 1.41 + 1.42 +#include <algorithm> 1.43 + 1.44 +#include "common/stabs_to_module.h" 1.45 +#include "common/using_std_string.h" 1.46 + 1.47 +namespace google_breakpad { 1.48 + 1.49 +// Demangle using abi call. 1.50 +// Older GCC may not support it. 1.51 +static string Demangle(const string &mangled) { 1.52 + int status = 0; 1.53 + char *demangled = abi::__cxa_demangle(mangled.c_str(), NULL, NULL, &status); 1.54 + if (status == 0 && demangled != NULL) { 1.55 + string str(demangled); 1.56 + free(demangled); 1.57 + return str; 1.58 + } 1.59 + return string(mangled); 1.60 +} 1.61 + 1.62 +StabsToModule::~StabsToModule() { 1.63 + // Free any functions we've accumulated but not added to the module. 1.64 + for (vector<Module::Function *>::const_iterator func_it = functions_.begin(); 1.65 + func_it != functions_.end(); func_it++) 1.66 + delete *func_it; 1.67 + // Free any function that we're currently within. 1.68 + delete current_function_; 1.69 +} 1.70 + 1.71 +bool StabsToModule::StartCompilationUnit(const char *name, uint64_t address, 1.72 + const char *build_directory) { 1.73 + assert(!in_compilation_unit_); 1.74 + in_compilation_unit_ = true; 1.75 + current_source_file_name_ = name; 1.76 + current_source_file_ = module_->FindFile(name); 1.77 + comp_unit_base_address_ = address; 1.78 + boundaries_.push_back(static_cast<Module::Address>(address)); 1.79 + return true; 1.80 +} 1.81 + 1.82 +bool StabsToModule::EndCompilationUnit(uint64_t address) { 1.83 + assert(in_compilation_unit_); 1.84 + in_compilation_unit_ = false; 1.85 + comp_unit_base_address_ = 0; 1.86 + current_source_file_ = NULL; 1.87 + current_source_file_name_ = NULL; 1.88 + if (address) 1.89 + boundaries_.push_back(static_cast<Module::Address>(address)); 1.90 + return true; 1.91 +} 1.92 + 1.93 +bool StabsToModule::StartFunction(const string &name, 1.94 + uint64_t address) { 1.95 + assert(!current_function_); 1.96 + Module::Function *f = new Module::Function; 1.97 + f->name = Demangle(name); 1.98 + f->address = address; 1.99 + f->size = 0; // We compute this in StabsToModule::Finalize(). 1.100 + f->parameter_size = 0; // We don't provide this information. 1.101 + current_function_ = f; 1.102 + boundaries_.push_back(static_cast<Module::Address>(address)); 1.103 + return true; 1.104 +} 1.105 + 1.106 +bool StabsToModule::EndFunction(uint64_t address) { 1.107 + assert(current_function_); 1.108 + // Functions in this compilation unit should have address bigger 1.109 + // than the compilation unit's starting address. There may be a lot 1.110 + // of duplicated entries for functions in the STABS data. We will 1.111 + // count on the Module to remove the duplicates. 1.112 + if (current_function_->address >= comp_unit_base_address_) 1.113 + functions_.push_back(current_function_); 1.114 + else 1.115 + delete current_function_; 1.116 + current_function_ = NULL; 1.117 + if (address) 1.118 + boundaries_.push_back(static_cast<Module::Address>(address)); 1.119 + return true; 1.120 +} 1.121 + 1.122 +bool StabsToModule::Line(uint64_t address, const char *name, int number) { 1.123 + assert(current_function_); 1.124 + assert(current_source_file_); 1.125 + if (name != current_source_file_name_) { 1.126 + current_source_file_ = module_->FindFile(name); 1.127 + current_source_file_name_ = name; 1.128 + } 1.129 + Module::Line line; 1.130 + line.address = address; 1.131 + line.size = 0; // We compute this in StabsToModule::Finalize(). 1.132 + line.file = current_source_file_; 1.133 + line.number = number; 1.134 + current_function_->lines.push_back(line); 1.135 + return true; 1.136 +} 1.137 + 1.138 +bool StabsToModule::Extern(const string &name, uint64_t address) { 1.139 + Module::Extern *ext = new Module::Extern; 1.140 + // Older libstdc++ demangle implementations can crash on unexpected 1.141 + // input, so be careful about what gets passed in. 1.142 + if (name.compare(0, 3, "__Z") == 0) { 1.143 + ext->name = Demangle(name.substr(1)); 1.144 + } else if (name[0] == '_') { 1.145 + ext->name = name.substr(1); 1.146 + } else { 1.147 + ext->name = name; 1.148 + } 1.149 + ext->address = address; 1.150 + module_->AddExtern(ext); 1.151 + return true; 1.152 +} 1.153 + 1.154 +void StabsToModule::Warning(const char *format, ...) { 1.155 + va_list args; 1.156 + va_start(args, format); 1.157 + vfprintf(stderr, format, args); 1.158 + va_end(args); 1.159 +} 1.160 + 1.161 +void StabsToModule::Finalize() { 1.162 + // Sort our boundary list, so we can search it quickly. 1.163 + sort(boundaries_.begin(), boundaries_.end()); 1.164 + // Sort all functions by address, just for neatness. 1.165 + sort(functions_.begin(), functions_.end(), 1.166 + Module::Function::CompareByAddress); 1.167 + 1.168 + for (vector<Module::Function *>::const_iterator func_it = functions_.begin(); 1.169 + func_it != functions_.end(); 1.170 + func_it++) { 1.171 + Module::Function *f = *func_it; 1.172 + // Compute the function f's size. 1.173 + vector<Module::Address>::const_iterator boundary 1.174 + = std::upper_bound(boundaries_.begin(), boundaries_.end(), f->address); 1.175 + if (boundary != boundaries_.end()) 1.176 + f->size = *boundary - f->address; 1.177 + else 1.178 + // If this is the last function in the module, and the STABS 1.179 + // reader was unable to give us its ending address, then assign 1.180 + // it a bogus, very large value. This will happen at most once 1.181 + // per module: since we've added all functions' addresses to the 1.182 + // boundary table, only one can be the last. 1.183 + f->size = kFallbackSize; 1.184 + 1.185 + // Compute sizes for each of the function f's lines --- if it has any. 1.186 + if (!f->lines.empty()) { 1.187 + stable_sort(f->lines.begin(), f->lines.end(), 1.188 + Module::Line::CompareByAddress); 1.189 + vector<Module::Line>::iterator last_line = f->lines.end() - 1; 1.190 + for (vector<Module::Line>::iterator line_it = f->lines.begin(); 1.191 + line_it != last_line; line_it++) 1.192 + line_it[0].size = line_it[1].address - line_it[0].address; 1.193 + // Compute the size of the last line from f's end address. 1.194 + last_line->size = (f->address + f->size) - last_line->address; 1.195 + } 1.196 + } 1.197 + // Now that everything has a size, add our functions to the module, and 1.198 + // dispose of our private list. 1.199 + module_->AddFunctions(functions_.begin(), functions_.end()); 1.200 + functions_.clear(); 1.201 +} 1.202 + 1.203 +} // namespace google_breakpad