toolkit/crashreporter/google-breakpad/src/common/dwarf/functioninfo.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2010 Google Inc. All Rights Reserved.
michael@0 2 //
michael@0 3 // Redistribution and use in source and binary forms, with or without
michael@0 4 // modification, are permitted provided that the following conditions are
michael@0 5 // met:
michael@0 6 //
michael@0 7 // * Redistributions of source code must retain the above copyright
michael@0 8 // notice, this list of conditions and the following disclaimer.
michael@0 9 // * Redistributions in binary form must reproduce the above
michael@0 10 // copyright notice, this list of conditions and the following disclaimer
michael@0 11 // in the documentation and/or other materials provided with the
michael@0 12 // distribution.
michael@0 13 // * Neither the name of Google Inc. nor the names of its
michael@0 14 // contributors may be used to endorse or promote products derived from
michael@0 15 // this software without specific prior written permission.
michael@0 16 //
michael@0 17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 28
michael@0 29 // This is a client for the dwarf2reader to extract function and line
michael@0 30 // information from the debug info.
michael@0 31
michael@0 32 #include <assert.h>
michael@0 33 #include <limits.h>
michael@0 34 #include <stdio.h>
michael@0 35
michael@0 36 #include <map>
michael@0 37 #include <queue>
michael@0 38 #include <vector>
michael@0 39 #include <memory>
michael@0 40
michael@0 41 #include "common/dwarf/functioninfo.h"
michael@0 42 #include "common/dwarf/bytereader.h"
michael@0 43 #include "common/using_std_string.h"
michael@0 44
michael@0 45 namespace dwarf2reader {
michael@0 46
michael@0 47 CULineInfoHandler::CULineInfoHandler(std::vector<SourceFileInfo>* files,
michael@0 48 std::vector<string>* dirs,
michael@0 49 LineMap* linemap):linemap_(linemap),
michael@0 50 files_(files),
michael@0 51 dirs_(dirs) {
michael@0 52 // The dirs and files are 1 indexed, so just make sure we put
michael@0 53 // nothing in the 0 vector.
michael@0 54 assert(dirs->size() == 0);
michael@0 55 assert(files->size() == 0);
michael@0 56 dirs->push_back("");
michael@0 57 SourceFileInfo s;
michael@0 58 s.name = "";
michael@0 59 s.lowpc = ULLONG_MAX;
michael@0 60 files->push_back(s);
michael@0 61 }
michael@0 62
michael@0 63 void CULineInfoHandler::DefineDir(const string& name, uint32 dir_num) {
michael@0 64 // These should never come out of order, actually
michael@0 65 assert(dir_num == dirs_->size());
michael@0 66 dirs_->push_back(name);
michael@0 67 }
michael@0 68
michael@0 69 void CULineInfoHandler::DefineFile(const string& name,
michael@0 70 int32 file_num, uint32 dir_num,
michael@0 71 uint64 mod_time, uint64 length) {
michael@0 72 assert(dir_num >= 0);
michael@0 73 assert(dir_num < dirs_->size());
michael@0 74
michael@0 75 // These should never come out of order, actually.
michael@0 76 if (file_num == (int32)files_->size() || file_num == -1) {
michael@0 77 string dir = dirs_->at(dir_num);
michael@0 78
michael@0 79 SourceFileInfo s;
michael@0 80 s.lowpc = ULLONG_MAX;
michael@0 81
michael@0 82 if (dir == "") {
michael@0 83 s.name = name;
michael@0 84 } else {
michael@0 85 s.name = dir + "/" + name;
michael@0 86 }
michael@0 87
michael@0 88 files_->push_back(s);
michael@0 89 } else {
michael@0 90 fprintf(stderr, "error in DefineFile");
michael@0 91 }
michael@0 92 }
michael@0 93
michael@0 94 void CULineInfoHandler::AddLine(uint64 address, uint64 length, uint32 file_num,
michael@0 95 uint32 line_num, uint32 column_num) {
michael@0 96 if (file_num < files_->size()) {
michael@0 97 linemap_->insert(
michael@0 98 std::make_pair(address,
michael@0 99 std::make_pair(files_->at(file_num).name.c_str(),
michael@0 100 line_num)));
michael@0 101
michael@0 102 if(address < files_->at(file_num).lowpc) {
michael@0 103 files_->at(file_num).lowpc = address;
michael@0 104 }
michael@0 105 } else {
michael@0 106 fprintf(stderr,"error in AddLine");
michael@0 107 }
michael@0 108 }
michael@0 109
michael@0 110 bool CUFunctionInfoHandler::StartCompilationUnit(uint64 offset,
michael@0 111 uint8 address_size,
michael@0 112 uint8 offset_size,
michael@0 113 uint64 cu_length,
michael@0 114 uint8 dwarf_version) {
michael@0 115 current_compilation_unit_offset_ = offset;
michael@0 116 return true;
michael@0 117 }
michael@0 118
michael@0 119
michael@0 120 // For function info, we only care about subprograms and inlined
michael@0 121 // subroutines. For line info, the DW_AT_stmt_list lives in the
michael@0 122 // compile unit tag.
michael@0 123
michael@0 124 bool CUFunctionInfoHandler::StartDIE(uint64 offset, enum DwarfTag tag) {
michael@0 125 switch (tag) {
michael@0 126 case DW_TAG_subprogram:
michael@0 127 case DW_TAG_inlined_subroutine: {
michael@0 128 current_function_info_ = new FunctionInfo;
michael@0 129 current_function_info_->lowpc = current_function_info_->highpc = 0;
michael@0 130 current_function_info_->name = "";
michael@0 131 current_function_info_->line = 0;
michael@0 132 current_function_info_->file = "";
michael@0 133 offset_to_funcinfo_->insert(std::make_pair(offset,
michael@0 134 current_function_info_));
michael@0 135 };
michael@0 136 // FALLTHROUGH
michael@0 137 case DW_TAG_compile_unit:
michael@0 138 return true;
michael@0 139 default:
michael@0 140 return false;
michael@0 141 }
michael@0 142 return false;
michael@0 143 }
michael@0 144
michael@0 145 // Only care about the name attribute for functions
michael@0 146
michael@0 147 void CUFunctionInfoHandler::ProcessAttributeString(uint64 offset,
michael@0 148 enum DwarfAttribute attr,
michael@0 149 enum DwarfForm form,
michael@0 150 const string &data) {
michael@0 151 if (current_function_info_) {
michael@0 152 if (attr == DW_AT_name)
michael@0 153 current_function_info_->name = data;
michael@0 154 else if(attr == DW_AT_MIPS_linkage_name)
michael@0 155 current_function_info_->mangled_name = data;
michael@0 156 }
michael@0 157 }
michael@0 158
michael@0 159 void CUFunctionInfoHandler::ProcessAttributeUnsigned(uint64 offset,
michael@0 160 enum DwarfAttribute attr,
michael@0 161 enum DwarfForm form,
michael@0 162 uint64 data) {
michael@0 163 if (attr == DW_AT_stmt_list) {
michael@0 164 SectionMap::const_iterator iter = sections_.find("__debug_line");
michael@0 165 assert(iter != sections_.end());
michael@0 166
michael@0 167 // this should be a scoped_ptr but we dont' use boost :-(
michael@0 168 std::auto_ptr<LineInfo> lireader(new LineInfo(iter->second.first + data,
michael@0 169 iter->second.second - data,
michael@0 170 reader_, linehandler_));
michael@0 171 lireader->Start();
michael@0 172 } else if (current_function_info_) {
michael@0 173 switch (attr) {
michael@0 174 case DW_AT_low_pc:
michael@0 175 current_function_info_->lowpc = data;
michael@0 176 break;
michael@0 177 case DW_AT_high_pc:
michael@0 178 current_function_info_->highpc = data;
michael@0 179 break;
michael@0 180 case DW_AT_decl_line:
michael@0 181 current_function_info_->line = data;
michael@0 182 break;
michael@0 183 case DW_AT_decl_file:
michael@0 184 current_function_info_->file = files_->at(data).name;
michael@0 185 break;
michael@0 186 default:
michael@0 187 break;
michael@0 188 }
michael@0 189 }
michael@0 190 }
michael@0 191
michael@0 192 void CUFunctionInfoHandler::ProcessAttributeReference(uint64 offset,
michael@0 193 enum DwarfAttribute attr,
michael@0 194 enum DwarfForm form,
michael@0 195 uint64 data) {
michael@0 196 if (current_function_info_) {
michael@0 197 switch (attr) {
michael@0 198 case DW_AT_specification: {
michael@0 199 // Some functions have a "specification" attribute
michael@0 200 // which means they were defined elsewhere. The name
michael@0 201 // attribute is not repeated, and must be taken from
michael@0 202 // the specification DIE. Here we'll assume that
michael@0 203 // any DIE referenced in this manner will already have
michael@0 204 // been seen, but that's not really required by the spec.
michael@0 205 FunctionMap::iterator iter = offset_to_funcinfo_->find(data);
michael@0 206 if (iter != offset_to_funcinfo_->end()) {
michael@0 207 current_function_info_->name = iter->second->name;
michael@0 208 current_function_info_->mangled_name = iter->second->mangled_name;
michael@0 209 } else {
michael@0 210 // If you hit this, this code probably needs to be rewritten.
michael@0 211 fprintf(stderr, "Error: DW_AT_specification was seen before the referenced DIE! (Looking for DIE at offset %08llx, in DIE at offset %08llx)\n", data, offset);
michael@0 212 }
michael@0 213 break;
michael@0 214 }
michael@0 215 default:
michael@0 216 break;
michael@0 217 }
michael@0 218 }
michael@0 219 }
michael@0 220
michael@0 221 void CUFunctionInfoHandler::EndDIE(uint64 offset) {
michael@0 222 if (current_function_info_ && current_function_info_->lowpc)
michael@0 223 address_to_funcinfo_->insert(std::make_pair(current_function_info_->lowpc,
michael@0 224 current_function_info_));
michael@0 225 }
michael@0 226
michael@0 227 } // namespace dwarf2reader

mercurial