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