blob: bedff451e9f87d0e4bcaae5a21c5b4ed9b6d34b3 [file] [log] [blame]
Ben Murdoch086aeea2011-05-13 15:57:08 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#include "src/v8.h"
Steve Blocka7e24c12009-10-30 11:49:00 +00006
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#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 Blocka7e24c12009-10-30 11:49:00 +000016
17namespace v8 {
18namespace internal {
19
20#ifdef ENABLE_DISASSEMBLER
21
Steve Blocka7e24c12009-10-30 11:49:00 +000022class 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 Block44f0eee2011-05-26 01:26:41 +010030
31 EmbeddedVector<char, 128> v8_buffer_;
Steve Blocka7e24c12009-10-30 11:49:00 +000032};
33
34
35const char* V8NameConverter::NameOfAddress(byte* pc) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000036 const char* name = code_->GetIsolate()->builtins()->Lookup(pc);
Steve Blocka7e24c12009-10-30 11:49:00 +000037 if (name != NULL) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000038 SNPrintF(v8_buffer_, "%s (%p)", name, pc);
Steve Block44f0eee2011-05-26 01:26:41 +010039 return v8_buffer_.start();
Steve Blocka7e24c12009-10-30 11:49:00 +000040 }
41
42 if (code_ != NULL) {
Steve Blockd0582a62009-12-15 09:54:21 +000043 int offs = static_cast<int>(pc - code_->instruction_start());
Steve Blocka7e24c12009-10-30 11:49:00 +000044 // print as code offset, if it seems reasonable
45 if (0 <= offs && offs < code_->instruction_size()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000046 SNPrintF(v8_buffer_, "%d (%p)", offs, pc);
Steve Block44f0eee2011-05-26 01:26:41 +010047 return v8_buffer_.start();
Steve Blocka7e24c12009-10-30 11:49:00 +000048 }
49 }
50
51 return disasm::NameConverter::NameOfAddress(pc);
52}
53
54
55const 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 Bernierd0a1eb72015-03-24 16:35:39 -040062static void DumpBuffer(std::ostream* os, StringBuilder* out) {
63 (*os) << out->Finalize() << std::endl;
Ben Murdoch69a99ed2011-11-30 16:03:39 +000064 out->Reset();
Steve Blocka7e24c12009-10-30 11:49:00 +000065}
66
Ben Murdoch69a99ed2011-11-30 16:03:39 +000067
Steve Blocka7e24c12009-10-30 11:49:00 +000068static const int kOutBufferSize = 2048 + String::kMaxShortPrintLength;
69static const int kRelocInfoPosition = 57;
70
Emily Bernierd0a1eb72015-03-24 16:35:39 -040071static int DecodeIt(Isolate* isolate, std::ostream* os,
72 const V8NameConverter& converter, byte* begin, byte* end) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000073 SealHandleScope shs(isolate);
74 DisallowHeapAllocation no_alloc;
75 ExternalReferenceEncoder ref_encoder(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +000076
77 v8::internal::EmbeddedVector<char, 128> decode_buffer;
78 v8::internal::EmbeddedVector<char, kOutBufferSize> out_buffer;
Ben Murdoch69a99ed2011-11-30 16:03:39 +000079 StringBuilder out(out_buffer.start(), out_buffer.length());
Steve Blocka7e24c12009-10-30 11:49:00 +000080 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 Murdochb8a8cc12014-11-26 15:28:44 +000094 SNPrintF(decode_buffer,
95 "%08x constant",
96 *reinterpret_cast<int32_t*>(pc));
Steve Blocka7e24c12009-10-30 11:49:00 +000097 constants--;
98 pc += 4;
99 } else {
100 int num_const = d.ConstantPoolSizeAt(pc);
101 if (num_const >= 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000102 SNPrintF(decode_buffer,
103 "%08x constant pool begin",
104 *reinterpret_cast<int32_t*>(pc));
Steve Blocka7e24c12009-10-30 11:49:00 +0000105 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 Murdochb8a8cc12014-11-26 15:28:44 +0000111 SNPrintF(decode_buffer,
112 "%08" V8PRIxPTR " jump table entry %4" V8PRIdPTR,
113 reinterpret_cast<intptr_t>(ptr),
114 ptr - begin);
Steve Blocka7e24c12009-10-30 11:49:00 +0000115 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 Blocka7e24c12009-10-30 11:49:00 +0000142 // Comments.
143 for (int i = 0; i < comments.length(); i++) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000144 out.AddFormatted(" %s", comments[i]);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400145 DumpBuffer(os, &out);
Steve Blocka7e24c12009-10-30 11:49:00 +0000146 }
147
Steve Blocka7e24c12009-10-30 11:49:00 +0000148 // 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 Murdochb8a8cc12014-11-26 15:28:44 +0000157 RelocInfo relocinfo(pcs[i], rmodes[i], datas[i], converter.code());
Steve Blocka7e24c12009-10-30 11:49:00 +0000158
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 Bernierd0a1eb72015-03-24 16:35:39 -0400165 DumpBuffer(os, &out);
Steve Blocka7e24c12009-10-30 11:49:00 +0000166 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 Murdoch589d6972011-11-30 16:04:58 +0000180 SmartArrayPointer<const char> obj_name = accumulator.ToCString();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000181 out.AddFormatted(" ;; object: %s", obj_name.get());
Steve Blocka7e24c12009-10-30 11:49:00 +0000182 } else if (rmode == RelocInfo::EXTERNAL_REFERENCE) {
183 const char* reference_name =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000184 ref_encoder.NameOfAddress(relocinfo.target_reference());
Steve Blocka7e24c12009-10-30 11:49:00 +0000185 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 Murdochb8a8cc12014-11-26 15:28:44 +0000194 if (kind == Code::LOAD_IC &&
195 LoadICState::GetContextualMode(code->extra_ic_state()) ==
196 CONTEXTUAL) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000197 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 Murdochb8a8cc12014-11-26 15:28:44 +0000203 Code::StubType type = code->type();
204 out.AddFormatted(", %s", Code::StubType2String(type));
Steve Blocka7e24c12009-10-30 11:49:00 +0000205 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000206 } 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 Blocka7e24c12009-10-30 11:49:00 +0000219 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000220 default:
221 out.AddFormatted("minor: %d", minor_key);
Steve Blocka7e24c12009-10-30 11:49:00 +0000222 }
223 } else {
224 out.AddFormatted(" %s", Code::Kind2String(kind));
225 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000226 if (rmode == RelocInfo::CODE_TARGET_WITH_ID) {
227 out.AddFormatted(" (id = %d)", static_cast<int>(relocinfo.data()));
228 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000229 } else if (RelocInfo::IsRuntimeEntry(rmode) &&
230 isolate->deoptimizer_data() != NULL) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100231 // A runtime entry reloinfo might be a deoptimization bailout.
232 Address addr = relocinfo.target_address();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000233 int id = Deoptimizer::GetDeoptimizationId(isolate,
234 addr,
235 Deoptimizer::EAGER);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100236 if (id == Deoptimizer::kNotDeoptimizationEntry) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000237 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 Murdochb0fe1622011-05-05 13:52:32 +0100252 } else {
253 out.AddFormatted(" ;; deoptimization bailout %d", id);
254 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000255 } else {
256 out.AddFormatted(" ;; %s", RelocInfo::RelocModeName(rmode));
257 }
258 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400259 DumpBuffer(os, &out);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000260 }
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 Bernierd0a1eb72015-03-24 16:35:39 -0400268 DumpBuffer(os, &out);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000269 }
270 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000271 }
272
273 delete it;
Steve Blockd0582a62009-12-15 09:54:21 +0000274 return static_cast<int>(pc - begin);
Steve Blocka7e24c12009-10-30 11:49:00 +0000275}
276
277
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400278int Disassembler::Decode(Isolate* isolate, std::ostream* os, byte* begin,
279 byte* end, Code* code) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000280 V8NameConverter v8NameConverter(code);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400281 return DecodeIt(isolate, os, v8NameConverter, begin, end);
Steve Blocka7e24c12009-10-30 11:49:00 +0000282}
283
284#else // ENABLE_DISASSEMBLER
285
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400286int Disassembler::Decode(Isolate* isolate, std::ostream* os, byte* begin,
287 byte* end, Code* code) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000288 return 0;
289}
290
Steve Blocka7e24c12009-10-30 11:49:00 +0000291#endif // ENABLE_DISASSEMBLER
292
293} } // namespace v8::internal