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