blob: c29022abbc0f846ccbe6b7748a96a478c18e577e [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 Murdoch4a90d5f2016-03-22 12:00:34 +00005#include "src/disassembler.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"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009#include "src/debug/debug.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010#include "src/deoptimizer.h"
11#include "src/disasm.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012#include "src/macro-assembler.h"
Ben Murdochda12d292016-06-02 14:46:10 +010013#include "src/snapshot/serializer-common.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014#include "src/string-stream.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000015
16namespace v8 {
17namespace internal {
18
19#ifdef ENABLE_DISASSEMBLER
20
Steve Blocka7e24c12009-10-30 11:49:00 +000021class 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 Block44f0eee2011-05-26 01:26:41 +010029
30 EmbeddedVector<char, 128> v8_buffer_;
Steve Blocka7e24c12009-10-30 11:49:00 +000031};
32
33
34const char* V8NameConverter::NameOfAddress(byte* pc) const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000035 const char* name =
36 code_ == NULL ? NULL : code_->GetIsolate()->builtins()->Lookup(pc);
37
Steve Blocka7e24c12009-10-30 11:49:00 +000038 if (name != NULL) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000039 SNPrintF(v8_buffer_, "%s (%p)", name, pc);
Steve Block44f0eee2011-05-26 01:26:41 +010040 return v8_buffer_.start();
Steve Blocka7e24c12009-10-30 11:49:00 +000041 }
42
43 if (code_ != NULL) {
Steve Blockd0582a62009-12-15 09:54:21 +000044 int offs = static_cast<int>(pc - code_->instruction_start());
Steve Blocka7e24c12009-10-30 11:49:00 +000045 // print as code offset, if it seems reasonable
46 if (0 <= offs && offs < code_->instruction_size()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000047 SNPrintF(v8_buffer_, "%d (%p)", offs, pc);
Steve Block44f0eee2011-05-26 01:26:41 +010048 return v8_buffer_.start();
Steve Blocka7e24c12009-10-30 11:49:00 +000049 }
50 }
51
52 return disasm::NameConverter::NameOfAddress(pc);
53}
54
55
56const 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 Bernierd0a1eb72015-03-24 16:35:39 -040063static void DumpBuffer(std::ostream* os, StringBuilder* out) {
64 (*os) << out->Finalize() << std::endl;
Ben Murdoch69a99ed2011-11-30 16:03:39 +000065 out->Reset();
Steve Blocka7e24c12009-10-30 11:49:00 +000066}
67
Ben Murdoch69a99ed2011-11-30 16:03:39 +000068
Steve Blocka7e24c12009-10-30 11:49:00 +000069static const int kOutBufferSize = 2048 + String::kMaxShortPrintLength;
70static const int kRelocInfoPosition = 57;
71
Emily Bernierd0a1eb72015-03-24 16:35:39 -040072static int DecodeIt(Isolate* isolate, std::ostream* os,
73 const V8NameConverter& converter, byte* begin, byte* end) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000074 SealHandleScope shs(isolate);
75 DisallowHeapAllocation no_alloc;
76 ExternalReferenceEncoder ref_encoder(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +000077
78 v8::internal::EmbeddedVector<char, 128> decode_buffer;
79 v8::internal::EmbeddedVector<char, kOutBufferSize> out_buffer;
Ben Murdoch69a99ed2011-11-30 16:03:39 +000080 StringBuilder out(out_buffer.start(), out_buffer.length());
Steve Blocka7e24c12009-10-30 11:49:00 +000081 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 Murdochb8a8cc12014-11-26 15:28:44 +000095 SNPrintF(decode_buffer,
96 "%08x constant",
97 *reinterpret_cast<int32_t*>(pc));
Steve Blocka7e24c12009-10-30 11:49:00 +000098 constants--;
99 pc += 4;
100 } else {
101 int num_const = d.ConstantPoolSizeAt(pc);
102 if (num_const >= 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000103 SNPrintF(decode_buffer,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000104 "%08x constant pool begin (num_const = %d)",
105 *reinterpret_cast<int32_t*>(pc), num_const);
Steve Blocka7e24c12009-10-30 11:49:00 +0000106 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 Murdochc5610432016-08-08 18:44:38 +0100112 SNPrintF(
113 decode_buffer, "%08" V8PRIxPTR " jump table entry %4" PRIuS,
114 reinterpret_cast<intptr_t>(ptr), static_cast<size_t>(ptr - begin));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000115 pc += sizeof(ptr);
Steve Blocka7e24c12009-10-30 11:49:00 +0000116 } 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.
Ben Murdochc5610432016-08-08 18:44:38 +0100149 out.AddFormatted("%p %4" V8PRIdPTRDIFF " ", prev_pc, prev_pc - begin);
Steve Blocka7e24c12009-10-30 11:49:00 +0000150
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 Murdoch4a90d5f2016-03-22 12:00:34 +0000157 RelocInfo relocinfo(isolate, pcs[i], rmodes[i], datas[i],
158 converter.code());
Steve Blocka7e24c12009-10-30 11:49:00 +0000159
160 // Indent the printing of the reloc info.
161 if (i == 0) {
162 // The first reloc info is printed after the disassembled instruction.
163 out.AddPadding(' ', kRelocInfoPosition - out.position());
164 } else {
165 // Additional reloc infos are printed on separate lines.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400166 DumpBuffer(os, &out);
Steve Blocka7e24c12009-10-30 11:49:00 +0000167 out.AddPadding(' ', kRelocInfoPosition);
168 }
169
170 RelocInfo::Mode rmode = relocinfo.rmode();
171 if (RelocInfo::IsPosition(rmode)) {
172 if (RelocInfo::IsStatementPosition(rmode)) {
Ben Murdochc5610432016-08-08 18:44:38 +0100173 out.AddFormatted(" ;; debug: statement %" V8PRIdPTR,
174 relocinfo.data());
Steve Blocka7e24c12009-10-30 11:49:00 +0000175 } else {
Ben Murdochc5610432016-08-08 18:44:38 +0100176 out.AddFormatted(" ;; debug: position %" V8PRIdPTR,
177 relocinfo.data());
Steve Blocka7e24c12009-10-30 11:49:00 +0000178 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000179 } else if (rmode == RelocInfo::DEOPT_REASON) {
180 Deoptimizer::DeoptReason reason =
181 static_cast<Deoptimizer::DeoptReason>(relocinfo.data());
182 out.AddFormatted(" ;; debug: deopt reason '%s'",
183 Deoptimizer::GetDeoptReason(reason));
Ben Murdochc5610432016-08-08 18:44:38 +0100184 } else if (rmode == RelocInfo::DEOPT_ID) {
185 out.AddFormatted(" ;; debug: deopt index %d",
186 static_cast<int>(relocinfo.data()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000187 } else if (rmode == RelocInfo::EMBEDDED_OBJECT) {
188 HeapStringAllocator allocator;
189 StringStream accumulator(&allocator);
190 relocinfo.target_object()->ShortPrint(&accumulator);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000191 base::SmartArrayPointer<const char> obj_name = accumulator.ToCString();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000192 out.AddFormatted(" ;; object: %s", obj_name.get());
Steve Blocka7e24c12009-10-30 11:49:00 +0000193 } else if (rmode == RelocInfo::EXTERNAL_REFERENCE) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000194 const char* reference_name = ref_encoder.NameOfAddress(
195 isolate, relocinfo.target_external_reference());
Steve Blocka7e24c12009-10-30 11:49:00 +0000196 out.AddFormatted(" ;; external reference (%s)", reference_name);
197 } else if (RelocInfo::IsCodeTarget(rmode)) {
198 out.AddFormatted(" ;; code:");
Steve Blocka7e24c12009-10-30 11:49:00 +0000199 Code* code = Code::GetCodeFromTargetAddress(relocinfo.target_address());
200 Code::Kind kind = code->kind();
201 if (code->is_inline_cache_stub()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000202 if (kind == Code::LOAD_IC &&
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000203 LoadICState::GetTypeofMode(code->extra_ic_state()) ==
204 NOT_INSIDE_TYPEOF) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000205 out.AddFormatted(" contextual,");
206 }
207 InlineCacheState ic_state = code->ic_state();
208 out.AddFormatted(" %s, %s", Code::Kind2String(kind),
209 Code::ICState2String(ic_state));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000210 } 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 Murdoch4a90d5f2016-03-22 12:00:34 +0000217 CodeStub::MajorName(major_key));
218 out.AddFormatted("minor: %d", minor_key);
Steve Blocka7e24c12009-10-30 11:49:00 +0000219 } else {
220 out.AddFormatted(" %s", Code::Kind2String(kind));
221 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000222 if (rmode == RelocInfo::CODE_TARGET_WITH_ID) {
223 out.AddFormatted(" (id = %d)", static_cast<int>(relocinfo.data()));
224 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000225 } else if (RelocInfo::IsRuntimeEntry(rmode) &&
226 isolate->deoptimizer_data() != NULL) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100227 // A runtime entry reloinfo might be a deoptimization bailout.
228 Address addr = relocinfo.target_address();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000229 int id = Deoptimizer::GetDeoptimizationId(isolate,
230 addr,
231 Deoptimizer::EAGER);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100232 if (id == Deoptimizer::kNotDeoptimizationEntry) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000233 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 Murdochb0fe1622011-05-05 13:52:32 +0100248 } else {
249 out.AddFormatted(" ;; deoptimization bailout %d", id);
250 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000251 } else {
252 out.AddFormatted(" ;; %s", RelocInfo::RelocModeName(rmode));
253 }
254 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400255 DumpBuffer(os, &out);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000256 }
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 Bernierd0a1eb72015-03-24 16:35:39 -0400264 DumpBuffer(os, &out);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000265 }
266 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000267 }
268
269 delete it;
Steve Blockd0582a62009-12-15 09:54:21 +0000270 return static_cast<int>(pc - begin);
Steve Blocka7e24c12009-10-30 11:49:00 +0000271}
272
273
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400274int Disassembler::Decode(Isolate* isolate, std::ostream* os, byte* begin,
275 byte* end, Code* code) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000276 V8NameConverter v8NameConverter(code);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400277 return DecodeIt(isolate, os, v8NameConverter, begin, end);
Steve Blocka7e24c12009-10-30 11:49:00 +0000278}
279
280#else // ENABLE_DISASSEMBLER
281
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400282int Disassembler::Decode(Isolate* isolate, std::ostream* os, byte* begin,
283 byte* end, Code* code) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000284 return 0;
285}
286
Steve Blocka7e24c12009-10-30 11:49:00 +0000287#endif // ENABLE_DISASSEMBLER
288
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000289} // namespace internal
290} // namespace v8