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