disassembler.cc 11.9 KB
Newer Older
1
// Copyright 2011 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5
#include "src/v8.h"
6

7 8 9 10 11 12 13 14 15
#include "src/code-stubs.h"
#include "src/codegen.h"
#include "src/debug.h"
#include "src/deoptimizer.h"
#include "src/disasm.h"
#include "src/disassembler.h"
#include "src/macro-assembler.h"
#include "src/serialize.h"
#include "src/string-stream.h"
16

17 18
namespace v8 {
namespace internal {
19 20 21 22 23 24

#ifdef ENABLE_DISASSEMBLER

void Disassembler::Dump(FILE* f, byte* begin, byte* end) {
  for (byte* pc = begin; pc < end; pc++) {
    if (f == NULL) {
25 26 27 28
      PrintF("%" V8PRIxPTR "  %4" V8PRIdPTR "  %02x\n",
             reinterpret_cast<intptr_t>(pc),
             pc - begin,
             *pc);
29
    } else {
30 31
      PrintF(f, "%" V8PRIxPTR "  %4" V8PRIdPTR "  %02x\n",
             reinterpret_cast<uintptr_t>(pc), pc - begin, *pc);
32 33 34 35 36 37 38 39 40
    }
  }
}


class V8NameConverter: public disasm::NameConverter {
 public:
  explicit V8NameConverter(Code* code) : code_(code) {}
  virtual const char* NameOfAddress(byte* pc) const;
41
  virtual const char* NameInCode(byte* addr) const;
42 43 44
  Code* code() const { return code_; }
 private:
  Code* code_;
45 46

  EmbeddedVector<char, 128> v8_buffer_;
47 48 49 50
};


const char* V8NameConverter::NameOfAddress(byte* pc) const {
51
  const char* name = code_->GetIsolate()->builtins()->Lookup(pc);
52
  if (name != NULL) {
53
    SNPrintF(v8_buffer_, "%s  (%p)", name, pc);
54
    return v8_buffer_.start();
55 56 57
  }

  if (code_ != NULL) {
58
    int offs = static_cast<int>(pc - code_->instruction_start());
59 60
    // print as code offset, if it seems reasonable
    if (0 <= offs && offs < code_->instruction_size()) {
61
      SNPrintF(v8_buffer_, "%d  (%p)", offs, pc);
62
      return v8_buffer_.start();
63 64 65 66 67 68 69
    }
  }

  return disasm::NameConverter::NameOfAddress(pc);
}


70 71 72 73 74 75 76
const char* V8NameConverter::NameInCode(byte* addr) const {
  // The V8NameConverter is used for well known code, so we can "safely"
  // dereference pointers in generated code.
  return (code_ != NULL) ? reinterpret_cast<const char*>(addr) : "";
}


77
static void DumpBuffer(FILE* f, StringBuilder* out) {
78
  if (f == NULL) {
79
    PrintF("%s\n", out->Finalize());
80
  } else {
81
    PrintF(f, "%s\n", out->Finalize());
82
  }
83
  out->Reset();
84 85
}

86

87
static const int kOutBufferSize = 2048 + String::kMaxShortPrintLength;
88 89
static const int kRelocInfoPosition = 57;

90 91
static int DecodeIt(Isolate* isolate,
                    FILE* f,
92 93 94
                    const V8NameConverter& converter,
                    byte* begin,
                    byte* end) {
95 96
  SealHandleScope shs(isolate);
  DisallowHeapAllocation no_alloc;
97
  ExternalReferenceEncoder ref_encoder(isolate);
98
  Heap* heap = isolate->heap();
99

100 101
  v8::internal::EmbeddedVector<char, 128> decode_buffer;
  v8::internal::EmbeddedVector<char, kOutBufferSize> out_buffer;
102
  StringBuilder out(out_buffer.start(), out_buffer.length());
103 104 105 106 107 108 109 110
  byte* pc = begin;
  disasm::Disassembler d(converter);
  RelocIterator* it = NULL;
  if (converter.code() != NULL) {
    it = new RelocIterator(converter.code());
  } else {
    // No relocation information when printing code stubs.
  }
111
  int constants = -1;  // no constants being decoded at the start
112 113 114 115

  while (pc < end) {
    // First decode instruction so that we know its length.
    byte* prev_pc = pc;
116
    if (constants > 0) {
117 118 119
      SNPrintF(decode_buffer,
               "%08x       constant",
               *reinterpret_cast<int32_t*>(pc));
120 121 122 123 124
      constants--;
      pc += 4;
    } else {
      int num_const = d.ConstantPoolSizeAt(pc);
      if (num_const >= 0) {
125 126 127
        SNPrintF(decode_buffer,
                 "%08x       constant pool begin",
                 *reinterpret_cast<int32_t*>(pc));
128 129
        constants = num_const;
        pc += 4;
130
      } else if (it != NULL && !it->done() && it->rinfo()->pc() == pc &&
131
          it->rinfo()->rmode() == RelocInfo::INTERNAL_REFERENCE) {
132 133
        // raw pointer embedded in code stream, e.g., jump table
        byte* ptr = *reinterpret_cast<byte**>(pc);
134 135 136 137
        SNPrintF(decode_buffer,
                 "%08" V8PRIxPTR "      jump table entry %4" V8PRIdPTR,
                 reinterpret_cast<intptr_t>(ptr),
                 ptr - begin);
138
        pc += 4;
139 140
      } else {
        decode_buffer[0] = '\0';
141
        pc += d.InstructionDecode(decode_buffer, pc);
142 143
      }
    }
144 145 146 147

    // Collect RelocInfo for this instruction (prev_pc .. pc-1)
    List<const char*> comments(4);
    List<byte*> pcs(1);
148
    List<RelocInfo::Mode> rmodes(1);
149 150 151
    List<intptr_t> datas(1);
    if (it != NULL) {
      while (!it->done() && it->rinfo()->pc() < pc) {
152
        if (RelocInfo::IsComment(it->rinfo()->rmode())) {
153 154 155 156 157 158 159 160 161 162 163 164 165 166
          // For comments just collect the text.
          comments.Add(reinterpret_cast<const char*>(it->rinfo()->data()));
        } else {
          // For other reloc info collect all data.
          pcs.Add(it->rinfo()->pc());
          rmodes.Add(it->rinfo()->rmode());
          datas.Add(it->rinfo()->data());
        }
        it->next();
      }
    }

    // Comments.
    for (int i = 0; i < comments.length(); i++) {
167 168
      out.AddFormatted("                  %s", comments[i]);
      DumpBuffer(f, &out);
169 170 171
    }

    // Instruction address and instruction offset.
172
    out.AddFormatted("%p  %4d  ", prev_pc, prev_pc - begin);
173

174
    // Instruction.
175
    out.AddFormatted("%s", decode_buffer.start());
176 177 178 179

    // Print all the reloc info for this instruction which are not comments.
    for (int i = 0; i < pcs.length(); i++) {
      // Put together the reloc info
180
      RelocInfo relocinfo(pcs[i], rmodes[i], datas[i], converter.code());
181 182 183 184

      // Indent the printing of the reloc info.
      if (i == 0) {
        // The first reloc info is printed after the disassembled instruction.
185
        out.AddPadding(' ', kRelocInfoPosition - out.position());
186 187
      } else {
        // Additional reloc infos are printed on separate lines.
188
        DumpBuffer(f, &out);
189
        out.AddPadding(' ', kRelocInfoPosition);
190 191
      }

192 193 194
      RelocInfo::Mode rmode = relocinfo.rmode();
      if (RelocInfo::IsPosition(rmode)) {
        if (RelocInfo::IsStatementPosition(rmode)) {
195 196 197 198
          out.AddFormatted("    ;; debug: statement %d", relocinfo.data());
        } else {
          out.AddFormatted("    ;; debug: position %d", relocinfo.data());
        }
199
      } else if (rmode == RelocInfo::EMBEDDED_OBJECT) {
200 201 202
        HeapStringAllocator allocator;
        StringStream accumulator(&allocator);
        relocinfo.target_object()->ShortPrint(&accumulator);
203
        SmartArrayPointer<const char> obj_name = accumulator.ToCString();
204
        out.AddFormatted("    ;; object: %s", obj_name.get());
205
      } else if (rmode == RelocInfo::EXTERNAL_REFERENCE) {
206
        const char* reference_name =
207
            ref_encoder.NameOfAddress(relocinfo.target_reference());
208
        out.AddFormatted("    ;; external reference (%s)", reference_name);
209
      } else if (RelocInfo::IsCodeTarget(rmode)) {
210
        out.AddFormatted("    ;; code:");
211
        if (rmode == RelocInfo::CONSTRUCT_CALL) {
212 213
          out.AddFormatted(" constructor,");
        }
214
        Code* code = Code::GetCodeFromTargetAddress(relocinfo.target_address());
215 216
        Code::Kind kind = code->kind();
        if (code->is_inline_cache_stub()) {
217 218
          if (kind == Code::LOAD_IC &&
              LoadIC::GetContextualMode(code->extra_ic_state()) == CONTEXTUAL) {
219 220 221 222 223
            out.AddFormatted(" contextual,");
          }
          InlineCacheState ic_state = code->ic_state();
          out.AddFormatted(" %s, %s", Code::Kind2String(kind),
              Code::ICState2String(ic_state));
224
          if (ic_state == MONOMORPHIC) {
225 226
            Code::StubType type = code->type();
            out.AddFormatted(", %s", Code::StubType2String(type));
227
          }
228
        } else if (kind == Code::STUB || kind == Code::HANDLER) {
229 230
          // Reverse lookup required as the minor key cannot be retrieved
          // from the code object.
231 232
          Object* obj = heap->code_stubs()->SlowReverseLookup(code);
          if (obj != heap->undefined_value()) {
233 234 235 236
            ASSERT(obj->IsSmi());
            // Get the STUB key and extract major and minor key.
            uint32_t key = Smi::cast(obj)->value();
            uint32_t minor_key = CodeStub::MinorKeyFromKey(key);
237 238
            CodeStub::Major major_key = CodeStub::GetMajorKey(code);
            ASSERT(major_key == CodeStub::MajorKeyFromKey(key));
239 240
            out.AddFormatted(" %s, %s, ",
                             Code::Kind2String(kind),
241 242
                             CodeStub::MajorName(major_key, false));
            switch (major_key) {
243 244 245 246
              case CodeStub::CallFunction: {
                int argc =
                    CallFunctionStub::ExtractArgcFromMinorKey(minor_key);
                out.AddFormatted("argc = %d", argc);
247
                break;
248 249
              }
              default:
250
                out.AddFormatted("minor: %d", minor_key);
251 252
            }
          }
253 254
        } else {
          out.AddFormatted(" %s", Code::Kind2String(kind));
255
        }
256 257 258
        if (rmode == RelocInfo::CODE_TARGET_WITH_ID) {
          out.AddFormatted(" (id = %d)", static_cast<int>(relocinfo.data()));
        }
259
      } else if (RelocInfo::IsRuntimeEntry(rmode) &&
260
                 isolate->deoptimizer_data() != NULL) {
261 262
        // A runtime entry reloinfo might be a deoptimization bailout.
        Address addr = relocinfo.target_address();
263 264 265
        int id = Deoptimizer::GetDeoptimizationId(isolate,
                                                  addr,
                                                  Deoptimizer::EAGER);
266
        if (id == Deoptimizer::kNotDeoptimizationEntry) {
267 268 269
          id = Deoptimizer::GetDeoptimizationId(isolate,
                                                addr,
                                                Deoptimizer::LAZY);
270
          if (id == Deoptimizer::kNotDeoptimizationEntry) {
271 272 273 274 275 276 277 278
            id = Deoptimizer::GetDeoptimizationId(isolate,
                                                  addr,
                                                  Deoptimizer::SOFT);
            if (id == Deoptimizer::kNotDeoptimizationEntry) {
              out.AddFormatted("    ;; %s", RelocInfo::RelocModeName(rmode));
            } else {
              out.AddFormatted("    ;; soft deoptimization bailout %d", id);
            }
279 280 281
          } else {
            out.AddFormatted("    ;; lazy deoptimization bailout %d", id);
          }
282 283 284
        } else {
          out.AddFormatted("    ;; deoptimization bailout %d", id);
        }
285 286
      } else {
        out.AddFormatted("    ;; %s", RelocInfo::RelocModeName(rmode));
287 288
      }
    }
289
    DumpBuffer(f, &out);
290 291
  }

292 293 294 295 296 297 298 299 300 301 302
  // Emit comments following the last instruction (if any).
  if (it != NULL) {
    for ( ; !it->done(); it->next()) {
      if (RelocInfo::IsComment(it->rinfo()->rmode())) {
        out.AddFormatted("                  %s",
                         reinterpret_cast<const char*>(it->rinfo()->data()));
        DumpBuffer(f, &out);
      }
    }
  }

303
  delete it;
304
  return static_cast<int>(pc - begin);
305 306 307
}


308
int Disassembler::Decode(Isolate* isolate, FILE* f, byte* begin, byte* end) {
309
  V8NameConverter defaultConverter(NULL);
310
  return DecodeIt(isolate, f, defaultConverter, begin, end);
311 312 313
}


314
// Called by Code::CodePrint.
315
void Disassembler::Decode(FILE* f, Code* code) {
316
  Isolate* isolate = code->GetIsolate();
317
  int decode_size = code->is_crankshafted()
318
      ? static_cast<int>(code->safepoint_table_offset())
319
      : code->instruction_size();
320
  // If there might be a back edge table, stop before reaching it.
321 322
  if (code->kind() == Code::FUNCTION) {
    decode_size =
323
        Min(decode_size, static_cast<int>(code->back_edge_table_offset()));
324 325 326 327
  }

  byte* begin = code->instruction_start();
  byte* end = begin + decode_size;
328
  V8NameConverter v8NameConverter(code);
329
  DecodeIt(isolate, f, v8NameConverter, begin, end);
330
}
331

332 333
#else  // ENABLE_DISASSEMBLER

334
void Disassembler::Dump(FILE* f, byte* begin, byte* end) {}
335 336 337
int Disassembler::Decode(Isolate* isolate, FILE* f, byte* begin, byte* end) {
  return 0;
}
338 339


340
void Disassembler::Decode(FILE* f, Code* code) {}
341 342 343 344

#endif  // ENABLE_DISASSEMBLER

} }  // namespace v8::internal