Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5 | #include "src/disassembler.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7 | #include "src/code-stubs.h" |
| 8 | #include "src/codegen.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9 | #include "src/debug/debug.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10 | #include "src/deoptimizer.h" |
| 11 | #include "src/disasm.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12 | #include "src/macro-assembler.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13 | #include "src/snapshot/serialize.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14 | #include "src/string-stream.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 15 | |
| 16 | namespace v8 { |
| 17 | namespace internal { |
| 18 | |
| 19 | #ifdef ENABLE_DISASSEMBLER |
| 20 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 21 | class V8NameConverter: public disasm::NameConverter { |
| 22 | public: |
| 23 | explicit V8NameConverter(Code* code) : code_(code) {} |
| 24 | virtual const char* NameOfAddress(byte* pc) const; |
| 25 | virtual const char* NameInCode(byte* addr) const; |
| 26 | Code* code() const { return code_; } |
| 27 | private: |
| 28 | Code* code_; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 29 | |
| 30 | EmbeddedVector<char, 128> v8_buffer_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | |
| 34 | const char* V8NameConverter::NameOfAddress(byte* pc) const { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 35 | const char* name = |
| 36 | code_ == NULL ? NULL : code_->GetIsolate()->builtins()->Lookup(pc); |
| 37 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 38 | if (name != NULL) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 39 | SNPrintF(v8_buffer_, "%s (%p)", name, pc); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 40 | return v8_buffer_.start(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | if (code_ != NULL) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 44 | int offs = static_cast<int>(pc - code_->instruction_start()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 45 | // print as code offset, if it seems reasonable |
| 46 | if (0 <= offs && offs < code_->instruction_size()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 47 | SNPrintF(v8_buffer_, "%d (%p)", offs, pc); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 48 | return v8_buffer_.start(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
| 52 | return disasm::NameConverter::NameOfAddress(pc); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | const char* V8NameConverter::NameInCode(byte* addr) const { |
| 57 | // The V8NameConverter is used for well known code, so we can "safely" |
| 58 | // dereference pointers in generated code. |
| 59 | return (code_ != NULL) ? reinterpret_cast<const char*>(addr) : ""; |
| 60 | } |
| 61 | |
| 62 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 63 | static void DumpBuffer(std::ostream* os, StringBuilder* out) { |
| 64 | (*os) << out->Finalize() << std::endl; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 65 | out->Reset(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 68 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 69 | static const int kOutBufferSize = 2048 + String::kMaxShortPrintLength; |
| 70 | static const int kRelocInfoPosition = 57; |
| 71 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 72 | static int DecodeIt(Isolate* isolate, std::ostream* os, |
| 73 | const V8NameConverter& converter, byte* begin, byte* end) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 74 | SealHandleScope shs(isolate); |
| 75 | DisallowHeapAllocation no_alloc; |
| 76 | ExternalReferenceEncoder ref_encoder(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 77 | |
| 78 | v8::internal::EmbeddedVector<char, 128> decode_buffer; |
| 79 | v8::internal::EmbeddedVector<char, kOutBufferSize> out_buffer; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 80 | StringBuilder out(out_buffer.start(), out_buffer.length()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 81 | byte* pc = begin; |
| 82 | disasm::Disassembler d(converter); |
| 83 | RelocIterator* it = NULL; |
| 84 | if (converter.code() != NULL) { |
| 85 | it = new RelocIterator(converter.code()); |
| 86 | } else { |
| 87 | // No relocation information when printing code stubs. |
| 88 | } |
| 89 | int constants = -1; // no constants being decoded at the start |
| 90 | |
| 91 | while (pc < end) { |
| 92 | // First decode instruction so that we know its length. |
| 93 | byte* prev_pc = pc; |
| 94 | if (constants > 0) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 95 | SNPrintF(decode_buffer, |
| 96 | "%08x constant", |
| 97 | *reinterpret_cast<int32_t*>(pc)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 98 | constants--; |
| 99 | pc += 4; |
| 100 | } else { |
| 101 | int num_const = d.ConstantPoolSizeAt(pc); |
| 102 | if (num_const >= 0) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 103 | SNPrintF(decode_buffer, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 104 | "%08x constant pool begin (num_const = %d)", |
| 105 | *reinterpret_cast<int32_t*>(pc), num_const); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 106 | constants = num_const; |
| 107 | pc += 4; |
| 108 | } else if (it != NULL && !it->done() && it->rinfo()->pc() == pc && |
| 109 | it->rinfo()->rmode() == RelocInfo::INTERNAL_REFERENCE) { |
| 110 | // raw pointer embedded in code stream, e.g., jump table |
| 111 | byte* ptr = *reinterpret_cast<byte**>(pc); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 112 | SNPrintF(decode_buffer, |
| 113 | "%08" V8PRIxPTR " jump table entry %4" V8PRIdPTR, |
| 114 | reinterpret_cast<intptr_t>(ptr), |
| 115 | ptr - begin); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 116 | pc += sizeof(ptr); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 117 | } else { |
| 118 | decode_buffer[0] = '\0'; |
| 119 | pc += d.InstructionDecode(decode_buffer, pc); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // Collect RelocInfo for this instruction (prev_pc .. pc-1) |
| 124 | List<const char*> comments(4); |
| 125 | List<byte*> pcs(1); |
| 126 | List<RelocInfo::Mode> rmodes(1); |
| 127 | List<intptr_t> datas(1); |
| 128 | if (it != NULL) { |
| 129 | while (!it->done() && it->rinfo()->pc() < pc) { |
| 130 | if (RelocInfo::IsComment(it->rinfo()->rmode())) { |
| 131 | // For comments just collect the text. |
| 132 | comments.Add(reinterpret_cast<const char*>(it->rinfo()->data())); |
| 133 | } else { |
| 134 | // For other reloc info collect all data. |
| 135 | pcs.Add(it->rinfo()->pc()); |
| 136 | rmodes.Add(it->rinfo()->rmode()); |
| 137 | datas.Add(it->rinfo()->data()); |
| 138 | } |
| 139 | it->next(); |
| 140 | } |
| 141 | } |
| 142 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 143 | // Comments. |
| 144 | for (int i = 0; i < comments.length(); i++) { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 145 | out.AddFormatted(" %s", comments[i]); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 146 | DumpBuffer(os, &out); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 149 | // Instruction address and instruction offset. |
| 150 | out.AddFormatted("%p %4d ", prev_pc, prev_pc - begin); |
| 151 | |
| 152 | // Instruction. |
| 153 | out.AddFormatted("%s", decode_buffer.start()); |
| 154 | |
| 155 | // Print all the reloc info for this instruction which are not comments. |
| 156 | for (int i = 0; i < pcs.length(); i++) { |
| 157 | // Put together the reloc info |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 158 | RelocInfo relocinfo(isolate, pcs[i], rmodes[i], datas[i], |
| 159 | converter.code()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 160 | |
| 161 | // Indent the printing of the reloc info. |
| 162 | if (i == 0) { |
| 163 | // The first reloc info is printed after the disassembled instruction. |
| 164 | out.AddPadding(' ', kRelocInfoPosition - out.position()); |
| 165 | } else { |
| 166 | // Additional reloc infos are printed on separate lines. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 167 | DumpBuffer(os, &out); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 168 | out.AddPadding(' ', kRelocInfoPosition); |
| 169 | } |
| 170 | |
| 171 | RelocInfo::Mode rmode = relocinfo.rmode(); |
| 172 | if (RelocInfo::IsPosition(rmode)) { |
| 173 | if (RelocInfo::IsStatementPosition(rmode)) { |
| 174 | out.AddFormatted(" ;; debug: statement %d", relocinfo.data()); |
| 175 | } else { |
| 176 | out.AddFormatted(" ;; debug: position %d", relocinfo.data()); |
| 177 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 178 | } else if (rmode == RelocInfo::DEOPT_REASON) { |
| 179 | Deoptimizer::DeoptReason reason = |
| 180 | static_cast<Deoptimizer::DeoptReason>(relocinfo.data()); |
| 181 | out.AddFormatted(" ;; debug: deopt reason '%s'", |
| 182 | Deoptimizer::GetDeoptReason(reason)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 183 | } else if (rmode == RelocInfo::EMBEDDED_OBJECT) { |
| 184 | HeapStringAllocator allocator; |
| 185 | StringStream accumulator(&allocator); |
| 186 | relocinfo.target_object()->ShortPrint(&accumulator); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 187 | base::SmartArrayPointer<const char> obj_name = accumulator.ToCString(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 188 | out.AddFormatted(" ;; object: %s", obj_name.get()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 189 | } else if (rmode == RelocInfo::EXTERNAL_REFERENCE) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 190 | const char* reference_name = ref_encoder.NameOfAddress( |
| 191 | isolate, relocinfo.target_external_reference()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 192 | out.AddFormatted(" ;; external reference (%s)", reference_name); |
| 193 | } else if (RelocInfo::IsCodeTarget(rmode)) { |
| 194 | out.AddFormatted(" ;; code:"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 195 | Code* code = Code::GetCodeFromTargetAddress(relocinfo.target_address()); |
| 196 | Code::Kind kind = code->kind(); |
| 197 | if (code->is_inline_cache_stub()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 198 | if (kind == Code::LOAD_IC && |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 199 | LoadICState::GetTypeofMode(code->extra_ic_state()) == |
| 200 | NOT_INSIDE_TYPEOF) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 201 | out.AddFormatted(" contextual,"); |
| 202 | } |
| 203 | InlineCacheState ic_state = code->ic_state(); |
| 204 | out.AddFormatted(" %s, %s", Code::Kind2String(kind), |
| 205 | Code::ICState2String(ic_state)); |
| 206 | if (ic_state == MONOMORPHIC) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 207 | Code::StubType type = code->type(); |
| 208 | out.AddFormatted(", %s", Code::StubType2String(type)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 209 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 210 | } else if (kind == Code::STUB || kind == Code::HANDLER) { |
| 211 | // Get the STUB key and extract major and minor key. |
| 212 | uint32_t key = code->stub_key(); |
| 213 | uint32_t minor_key = CodeStub::MinorKeyFromKey(key); |
| 214 | CodeStub::Major major_key = CodeStub::GetMajorKey(code); |
| 215 | DCHECK(major_key == CodeStub::MajorKeyFromKey(key)); |
| 216 | out.AddFormatted(" %s, %s, ", Code::Kind2String(kind), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 217 | CodeStub::MajorName(major_key)); |
| 218 | out.AddFormatted("minor: %d", minor_key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 219 | } else { |
| 220 | out.AddFormatted(" %s", Code::Kind2String(kind)); |
| 221 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 222 | if (rmode == RelocInfo::CODE_TARGET_WITH_ID) { |
| 223 | out.AddFormatted(" (id = %d)", static_cast<int>(relocinfo.data())); |
| 224 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 225 | } else if (RelocInfo::IsRuntimeEntry(rmode) && |
| 226 | isolate->deoptimizer_data() != NULL) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 227 | // A runtime entry reloinfo might be a deoptimization bailout. |
| 228 | Address addr = relocinfo.target_address(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 229 | int id = Deoptimizer::GetDeoptimizationId(isolate, |
| 230 | addr, |
| 231 | Deoptimizer::EAGER); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 232 | if (id == Deoptimizer::kNotDeoptimizationEntry) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 233 | id = Deoptimizer::GetDeoptimizationId(isolate, |
| 234 | addr, |
| 235 | Deoptimizer::LAZY); |
| 236 | if (id == Deoptimizer::kNotDeoptimizationEntry) { |
| 237 | id = Deoptimizer::GetDeoptimizationId(isolate, |
| 238 | addr, |
| 239 | Deoptimizer::SOFT); |
| 240 | if (id == Deoptimizer::kNotDeoptimizationEntry) { |
| 241 | out.AddFormatted(" ;; %s", RelocInfo::RelocModeName(rmode)); |
| 242 | } else { |
| 243 | out.AddFormatted(" ;; soft deoptimization bailout %d", id); |
| 244 | } |
| 245 | } else { |
| 246 | out.AddFormatted(" ;; lazy deoptimization bailout %d", id); |
| 247 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 248 | } else { |
| 249 | out.AddFormatted(" ;; deoptimization bailout %d", id); |
| 250 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 251 | } else { |
| 252 | out.AddFormatted(" ;; %s", RelocInfo::RelocModeName(rmode)); |
| 253 | } |
| 254 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 255 | DumpBuffer(os, &out); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | // Emit comments following the last instruction (if any). |
| 259 | if (it != NULL) { |
| 260 | for ( ; !it->done(); it->next()) { |
| 261 | if (RelocInfo::IsComment(it->rinfo()->rmode())) { |
| 262 | out.AddFormatted(" %s", |
| 263 | reinterpret_cast<const char*>(it->rinfo()->data())); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 264 | DumpBuffer(os, &out); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 265 | } |
| 266 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | delete it; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 270 | return static_cast<int>(pc - begin); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 274 | int Disassembler::Decode(Isolate* isolate, std::ostream* os, byte* begin, |
| 275 | byte* end, Code* code) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 276 | V8NameConverter v8NameConverter(code); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 277 | return DecodeIt(isolate, os, v8NameConverter, begin, end); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | #else // ENABLE_DISASSEMBLER |
| 281 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 282 | int Disassembler::Decode(Isolate* isolate, std::ostream* os, byte* begin, |
| 283 | byte* end, Code* code) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 284 | return 0; |
| 285 | } |
| 286 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 287 | #endif // ENABLE_DISASSEMBLER |
| 288 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 289 | } // namespace internal |
| 290 | } // namespace v8 |