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 "src/code-stubs.h"
8 #include "src/codegen.h"
9 #include "src/debug/debug.h"
10 #include "src/deoptimizer.h"
11 #include "src/disasm.h"
12 #include "src/macro-assembler.h"
13 #include "src/snapshot/serialize.h"
14 #include "src/string-stream.h"
15
16 namespace v8 {
17 namespace internal {
18
19 #ifdef ENABLE_DISASSEMBLER
20
21 class V8NameConverter: public disasm::NameConverter {
22 public:
V8NameConverter(Code * code)23 explicit V8NameConverter(Code* code) : code_(code) {}
24 virtual const char* NameOfAddress(byte* pc) const;
25 virtual const char* NameInCode(byte* addr) const;
code() const26 Code* code() const { return code_; }
27 private:
28 Code* code_;
29
30 EmbeddedVector<char, 128> v8_buffer_;
31 };
32
33
NameOfAddress(byte * pc) const34 const char* V8NameConverter::NameOfAddress(byte* pc) const {
35 const char* name =
36 code_ == NULL ? NULL : code_->GetIsolate()->builtins()->Lookup(pc);
37
38 if (name != NULL) {
39 SNPrintF(v8_buffer_, "%s (%p)", name, pc);
40 return v8_buffer_.start();
41 }
42
43 if (code_ != NULL) {
44 int offs = static_cast<int>(pc - code_->instruction_start());
45 // print as code offset, if it seems reasonable
46 if (0 <= offs && offs < code_->instruction_size()) {
47 SNPrintF(v8_buffer_, "%d (%p)", offs, pc);
48 return v8_buffer_.start();
49 }
50 }
51
52 return disasm::NameConverter::NameOfAddress(pc);
53 }
54
55
NameInCode(byte * addr) const56 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
DumpBuffer(std::ostream * os,StringBuilder * out)63 static void DumpBuffer(std::ostream* os, StringBuilder* out) {
64 (*os) << out->Finalize() << std::endl;
65 out->Reset();
66 }
67
68
69 static const int kOutBufferSize = 2048 + String::kMaxShortPrintLength;
70 static const int kRelocInfoPosition = 57;
71
DecodeIt(Isolate * isolate,std::ostream * os,const V8NameConverter & converter,byte * begin,byte * end)72 static int DecodeIt(Isolate* isolate, std::ostream* os,
73 const V8NameConverter& converter, byte* begin, byte* end) {
74 SealHandleScope shs(isolate);
75 DisallowHeapAllocation no_alloc;
76 ExternalReferenceEncoder ref_encoder(isolate);
77
78 v8::internal::EmbeddedVector<char, 128> decode_buffer;
79 v8::internal::EmbeddedVector<char, kOutBufferSize> out_buffer;
80 StringBuilder out(out_buffer.start(), out_buffer.length());
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) {
95 SNPrintF(decode_buffer,
96 "%08x constant",
97 *reinterpret_cast<int32_t*>(pc));
98 constants--;
99 pc += 4;
100 } else {
101 int num_const = d.ConstantPoolSizeAt(pc);
102 if (num_const >= 0) {
103 SNPrintF(decode_buffer,
104 "%08x constant pool begin (num_const = %d)",
105 *reinterpret_cast<int32_t*>(pc), num_const);
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);
112 SNPrintF(decode_buffer,
113 "%08" V8PRIxPTR " jump table entry %4" V8PRIdPTR,
114 reinterpret_cast<intptr_t>(ptr),
115 ptr - begin);
116 pc += sizeof(ptr);
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
143 // Comments.
144 for (int i = 0; i < comments.length(); i++) {
145 out.AddFormatted(" %s", comments[i]);
146 DumpBuffer(os, &out);
147 }
148
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
158 RelocInfo relocinfo(isolate, pcs[i], rmodes[i], datas[i],
159 converter.code());
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.
167 DumpBuffer(os, &out);
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 }
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));
183 } else if (rmode == RelocInfo::EMBEDDED_OBJECT) {
184 HeapStringAllocator allocator;
185 StringStream accumulator(&allocator);
186 relocinfo.target_object()->ShortPrint(&accumulator);
187 base::SmartArrayPointer<const char> obj_name = accumulator.ToCString();
188 out.AddFormatted(" ;; object: %s", obj_name.get());
189 } else if (rmode == RelocInfo::EXTERNAL_REFERENCE) {
190 const char* reference_name = ref_encoder.NameOfAddress(
191 isolate, relocinfo.target_external_reference());
192 out.AddFormatted(" ;; external reference (%s)", reference_name);
193 } else if (RelocInfo::IsCodeTarget(rmode)) {
194 out.AddFormatted(" ;; code:");
195 Code* code = Code::GetCodeFromTargetAddress(relocinfo.target_address());
196 Code::Kind kind = code->kind();
197 if (code->is_inline_cache_stub()) {
198 if (kind == Code::LOAD_IC &&
199 LoadICState::GetTypeofMode(code->extra_ic_state()) ==
200 NOT_INSIDE_TYPEOF) {
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) {
207 Code::StubType type = code->type();
208 out.AddFormatted(", %s", Code::StubType2String(type));
209 }
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),
217 CodeStub::MajorName(major_key));
218 out.AddFormatted("minor: %d", minor_key);
219 } else {
220 out.AddFormatted(" %s", Code::Kind2String(kind));
221 }
222 if (rmode == RelocInfo::CODE_TARGET_WITH_ID) {
223 out.AddFormatted(" (id = %d)", static_cast<int>(relocinfo.data()));
224 }
225 } else if (RelocInfo::IsRuntimeEntry(rmode) &&
226 isolate->deoptimizer_data() != NULL) {
227 // A runtime entry reloinfo might be a deoptimization bailout.
228 Address addr = relocinfo.target_address();
229 int id = Deoptimizer::GetDeoptimizationId(isolate,
230 addr,
231 Deoptimizer::EAGER);
232 if (id == Deoptimizer::kNotDeoptimizationEntry) {
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 }
248 } else {
249 out.AddFormatted(" ;; deoptimization bailout %d", id);
250 }
251 } else {
252 out.AddFormatted(" ;; %s", RelocInfo::RelocModeName(rmode));
253 }
254 }
255 DumpBuffer(os, &out);
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()));
264 DumpBuffer(os, &out);
265 }
266 }
267 }
268
269 delete it;
270 return static_cast<int>(pc - begin);
271 }
272
273
Decode(Isolate * isolate,std::ostream * os,byte * begin,byte * end,Code * code)274 int Disassembler::Decode(Isolate* isolate, std::ostream* os, byte* begin,
275 byte* end, Code* code) {
276 V8NameConverter v8NameConverter(code);
277 return DecodeIt(isolate, os, v8NameConverter, begin, end);
278 }
279
280 #else // ENABLE_DISASSEMBLER
281
282 int Disassembler::Decode(Isolate* isolate, std::ostream* os, byte* begin,
283 byte* end, Code* code) {
284 return 0;
285 }
286
287 #endif // ENABLE_DISASSEMBLER
288
289 } // namespace internal
290 } // namespace v8
291