jimblandy | 3e768ed | 2010-03-16 16:31:49 +0000 | [diff] [blame] | 1 | // -*- mode: c++ -*- |
| 2 | |
| 3 | // Copyright (c) 2010, Google Inc. |
| 4 | // All rights reserved. |
| 5 | // |
| 6 | // Redistribution and use in source and binary forms, with or without |
| 7 | // modification, are permitted provided that the following conditions are |
| 8 | // met: |
| 9 | // |
| 10 | // * Redistributions of source code must retain the above copyright |
| 11 | // notice, this list of conditions and the following disclaimer. |
| 12 | // * Redistributions in binary form must reproduce the above |
| 13 | // copyright notice, this list of conditions and the following disclaimer |
| 14 | // in the documentation and/or other materials provided with the |
| 15 | // distribution. |
| 16 | // * Neither the name of Google Inc. nor the names of its |
| 17 | // contributors may be used to endorse or promote products derived from |
| 18 | // this software without specific prior written permission. |
| 19 | // |
| 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | |
| 32 | // Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com> |
| 33 | |
| 34 | // Implementation of google_breakpad::DwarfCFIToModule. |
| 35 | // See dwarf_cfi_to_module.h for details. |
| 36 | |
| 37 | #include <sstream> |
| 38 | |
jimblandy | 8785524 | 2010-04-05 19:40:17 +0000 | [diff] [blame] | 39 | #include "common/dwarf_cfi_to_module.h" |
jimblandy | 3e768ed | 2010-03-16 16:31:49 +0000 | [diff] [blame] | 40 | |
| 41 | namespace google_breakpad { |
| 42 | |
| 43 | using std::ostringstream; |
| 44 | |
jimblandy | 504280a | 2010-04-28 18:14:59 +0000 | [diff] [blame] | 45 | vector<string> DwarfCFIToModule::RegisterNames::MakeVector( |
| 46 | const char * const *strings, |
| 47 | size_t size) { |
| 48 | vector<string> names(strings, strings + size); |
| 49 | return names; |
| 50 | } |
| 51 | |
| 52 | vector<string> DwarfCFIToModule::RegisterNames::I386() { |
| 53 | static const char *const names[] = { |
| 54 | "$eax", "$ecx", "$edx", "$ebx", "$esp", "$ebp", "$esi", "$edi", |
| 55 | "$eip", "$eflags", "$unused1", |
| 56 | "$st0", "$st1", "$st2", "$st3", "$st4", "$st5", "$st6", "$st7", |
| 57 | "$unused2", "$unused3", |
| 58 | "$xmm0", "$xmm1", "$xmm2", "$xmm3", "$xmm4", "$xmm5", "$xmm6", "$xmm7", |
| 59 | "$mm0", "$mm1", "$mm2", "$mm3", "$mm4", "$mm5", "$mm6", "$mm7", |
| 60 | "$fcw", "$fsw", "$mxcsr", |
| 61 | "$es", "$cs", "$ss", "$ds", "$fs", "$gs", "$unused4", "$unused5", |
| 62 | "$tr", "$ldtr" |
| 63 | }; |
| 64 | |
| 65 | return MakeVector(names, sizeof(names) / sizeof(names[0])); |
| 66 | } |
| 67 | |
| 68 | vector<string> DwarfCFIToModule::RegisterNames::X86_64() { |
| 69 | static const char *const names[] = { |
| 70 | "$rax", "$rdx", "$rcx", "$rbx", "$rsi", "$rdi", "$rbp", "$rsp", |
| 71 | "$r8", "$r9", "$r10", "$r11", "$r12", "$r13", "$r14", "$r15", |
| 72 | "$rip", |
| 73 | "$xmm0","$xmm1","$xmm2", "$xmm3", "$xmm4", "$xmm5", "$xmm6", "$xmm7", |
| 74 | "$xmm8","$xmm9","$xmm10","$xmm11","$xmm12","$xmm13","$xmm14","$xmm15", |
| 75 | "$st0", "$st1", "$st2", "$st3", "$st4", "$st5", "$st6", "$st7", |
| 76 | "$mm0", "$mm1", "$mm2", "$mm3", "$mm4", "$mm5", "$mm6", "$mm7", |
| 77 | "$rflags", |
| 78 | "$es", "$cs", "$ss", "$ds", "$fs", "$gs", "$unused1", "$unused2", |
| 79 | "$fs.base", "$gs.base", "$unused3", "$unused4", |
| 80 | "$tr", "$ldtr", |
| 81 | "$mxcsr", "$fcw", "$fsw" |
| 82 | }; |
| 83 | |
| 84 | return MakeVector(names, sizeof(names) / sizeof(names[0])); |
| 85 | } |
| 86 | |
| 87 | vector<string> DwarfCFIToModule::RegisterNames::ARM() { |
| 88 | static const char *const names[] = { |
| 89 | "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", |
| 90 | "r8", "r9", "r10", "r11", "r12", "sp", "lr", "pc", |
| 91 | "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7", |
| 92 | "fps", "cpsr" |
| 93 | }; |
| 94 | |
| 95 | return MakeVector(names, sizeof(names) / sizeof(names[0])); |
| 96 | } |
| 97 | |
jimblandy | 3e768ed | 2010-03-16 16:31:49 +0000 | [diff] [blame] | 98 | bool DwarfCFIToModule::Entry(size_t offset, uint64 address, uint64 length, |
| 99 | uint8 version, const string &augmentation, |
| 100 | unsigned return_address) { |
| 101 | assert(!entry_); |
jimblandy | a76aaa1 | 2010-03-16 16:37:50 +0000 | [diff] [blame] | 102 | |
| 103 | // If dwarf2reader::CallFrameInfo can handle this version and |
| 104 | // augmentation, then we should be okay with that, so there's no |
| 105 | // need to check them here. |
jimblandy | 3e768ed | 2010-03-16 16:31:49 +0000 | [diff] [blame] | 106 | |
| 107 | // Get ready to collect entries. |
| 108 | entry_ = new Module::StackFrameEntry; |
| 109 | entry_->address = address; |
| 110 | entry_->size = length; |
| 111 | entry_offset_ = offset; |
| 112 | return_address_ = return_address; |
| 113 | |
| 114 | // Breakpad STACK CFI records must provide a .ra rule, but DWARF CFI |
| 115 | // may not establish any rule for .ra if the return address column |
| 116 | // is an ordinary register, and that register holds the return |
| 117 | // address on entry to the function. So establish an initial .ra |
| 118 | // rule citing the return address register. |
| 119 | if (return_address_ < register_names_.size()) |
| 120 | entry_->initial_rules[".ra"] = register_names_[return_address_]; |
| 121 | |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | string DwarfCFIToModule::RegisterName(int i) { |
| 126 | assert(entry_); |
| 127 | if (i < 0) { |
| 128 | assert(i == kCFARegister); |
| 129 | return ".cfa"; |
| 130 | } |
| 131 | unsigned reg = i; |
| 132 | if (reg == return_address_) |
| 133 | return ".ra"; |
| 134 | |
| 135 | if (0 <= reg && reg < register_names_.size()) |
| 136 | return register_names_[reg]; |
| 137 | |
| 138 | reporter_->UnnamedRegister(entry_offset_, reg); |
| 139 | char buf[30]; |
| 140 | sprintf(buf, "unnamed_register%u", reg); |
| 141 | return buf; |
| 142 | } |
| 143 | |
| 144 | void DwarfCFIToModule::Record(Module::Address address, int reg, |
| 145 | const string &rule) { |
| 146 | assert(entry_); |
| 147 | // Is this one of this entry's initial rules? |
| 148 | if (address == entry_->address) |
| 149 | entry_->initial_rules[RegisterName(reg)] = rule; |
| 150 | // File it under the appropriate address. |
| 151 | else |
| 152 | entry_->rule_changes[address][RegisterName(reg)] = rule; |
| 153 | } |
| 154 | |
| 155 | bool DwarfCFIToModule::UndefinedRule(uint64 address, int reg) { |
| 156 | reporter_->UndefinedNotSupported(entry_offset_, RegisterName(reg)); |
| 157 | // Treat this as a non-fatal error. |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | bool DwarfCFIToModule::SameValueRule(uint64 address, int reg) { |
| 162 | ostringstream s; |
| 163 | s << RegisterName(reg); |
| 164 | Record(address, reg, s.str()); |
| 165 | return true; |
| 166 | } |
| 167 | |
| 168 | bool DwarfCFIToModule::OffsetRule(uint64 address, int reg, |
| 169 | int base_register, long offset) { |
| 170 | ostringstream s; |
| 171 | s << RegisterName(base_register) << " " << offset << " + ^"; |
| 172 | Record(address, reg, s.str()); |
| 173 | return true; |
| 174 | } |
| 175 | |
| 176 | bool DwarfCFIToModule::ValOffsetRule(uint64 address, int reg, |
| 177 | int base_register, long offset) { |
| 178 | ostringstream s; |
| 179 | s << RegisterName(base_register) << " " << offset << " +"; |
| 180 | Record(address, reg, s.str()); |
| 181 | return true; |
| 182 | } |
| 183 | |
| 184 | bool DwarfCFIToModule::RegisterRule(uint64 address, int reg, |
| 185 | int base_register) { |
| 186 | ostringstream s; |
| 187 | s << RegisterName(base_register); |
| 188 | Record(address, reg, s.str()); |
| 189 | return true; |
| 190 | } |
| 191 | |
| 192 | bool DwarfCFIToModule::ExpressionRule(uint64 address, int reg, |
| 193 | const string &expression) { |
| 194 | reporter_->ExpressionsNotSupported(entry_offset_, RegisterName(reg)); |
| 195 | // Treat this as a non-fatal error. |
| 196 | return true; |
| 197 | } |
| 198 | |
| 199 | bool DwarfCFIToModule::ValExpressionRule(uint64 address, int reg, |
| 200 | const string &expression) { |
| 201 | reporter_->ExpressionsNotSupported(entry_offset_, RegisterName(reg)); |
| 202 | // Treat this as a non-fatal error. |
| 203 | return true; |
| 204 | } |
| 205 | |
| 206 | bool DwarfCFIToModule::End() { |
| 207 | module_->AddStackFrameEntry(entry_); |
| 208 | entry_ = NULL; |
| 209 | return true; |
| 210 | } |
| 211 | |
| 212 | void DwarfCFIToModule::Reporter::UnnamedRegister(size_t offset, int reg) { |
| 213 | fprintf(stderr, "%s, section '%s': " |
| 214 | "the call frame entry at offset 0x%zx refers to register %d," |
| 215 | " whose name we don't know\n", |
| 216 | file_.c_str(), section_.c_str(), offset, reg); |
| 217 | } |
| 218 | |
| 219 | void DwarfCFIToModule::Reporter::UndefinedNotSupported(size_t offset, |
| 220 | const string ®) { |
| 221 | fprintf(stderr, "%s, section '%s': " |
| 222 | "the call frame entry at offset 0x%zx sets the rule for " |
| 223 | "register '%s' to 'undefined', but the Breakpad symbol file format" |
| 224 | " cannot express this\n", |
| 225 | file_.c_str(), section_.c_str(), offset, reg.c_str()); |
| 226 | } |
| 227 | |
| 228 | void DwarfCFIToModule::Reporter::ExpressionsNotSupported(size_t offset, |
| 229 | const string ®) { |
| 230 | fprintf(stderr, "%s, section '%s': " |
| 231 | "the call frame entry at offset 0x%zx uses a DWARF expression to" |
| 232 | " describe how to recover register '%s', " |
| 233 | " but this translator cannot yet translate DWARF expressions to" |
| 234 | " Breakpad postfix expressions\n", |
| 235 | file_.c_str(), section_.c_str(), offset, reg.c_str()); |
| 236 | } |
| 237 | |
| 238 | } // namespace google_breakpad |