1 // Copyright 2010 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.
5 #ifdef ENABLE_GDB_JIT_INTERFACE
8 #include "src/base/platform/platform.h"
9 #include "src/bootstrapper.h"
10 #include "src/compiler.h"
11 #include "src/frames-inl.h"
12 #include "src/frames.h"
13 #include "src/gdb-jit.h"
14 #include "src/global-handles.h"
15 #include "src/messages.h"
16 #include "src/natives.h"
17 #include "src/ostreams.h"
18 #include "src/scopes.h"
27 typedef MachO DebugObject;
28 typedef MachOSection DebugSection;
33 typedef ELF DebugObject;
34 typedef ELFSection DebugSection;
37 class Writer BASE_EMBEDDED {
39 explicit Writer(DebugObject* debug_object)
40 : debug_object_(debug_object),
43 buffer_(reinterpret_cast<byte*>(malloc(capacity_))) {
50 uintptr_t position() const {
57 Slot(Writer* w, uintptr_t offset) : w_(w), offset_(offset) { }
60 return w_->RawSlotAt<T>(offset_);
63 void set(const T& value) {
64 *w_->RawSlotAt<T>(offset_) = value;
68 return Slot<T>(w_, offset_ + sizeof(T) * i);
77 void Write(const T& val) {
78 Ensure(position_ + sizeof(T));
79 *RawSlotAt<T>(position_) = val;
80 position_ += sizeof(T);
84 Slot<T> SlotAt(uintptr_t offset) {
85 Ensure(offset + sizeof(T));
86 return Slot<T>(this, offset);
90 Slot<T> CreateSlotHere() {
91 return CreateSlotsHere<T>(1);
95 Slot<T> CreateSlotsHere(uint32_t count) {
96 uintptr_t slot_position = position_;
97 position_ += sizeof(T) * count;
99 return SlotAt<T>(slot_position);
102 void Ensure(uintptr_t pos) {
103 if (capacity_ < pos) {
104 while (capacity_ < pos) capacity_ *= 2;
105 buffer_ = reinterpret_cast<byte*>(realloc(buffer_, capacity_));
109 DebugObject* debug_object() { return debug_object_; }
111 byte* buffer() { return buffer_; }
113 void Align(uintptr_t align) {
114 uintptr_t delta = position_ % align;
115 if (delta == 0) return;
116 uintptr_t padding = align - delta;
117 Ensure(position_ += padding);
118 DCHECK((position_ % align) == 0);
121 void WriteULEB128(uintptr_t value) {
123 uint8_t byte = value & 0x7F;
125 if (value != 0) byte |= 0x80;
126 Write<uint8_t>(byte);
127 } while (value != 0);
130 void WriteSLEB128(intptr_t value) {
133 int8_t byte = value & 0x7F;
134 bool byte_sign = byte & 0x40;
137 if ((value == 0 && !byte_sign) || (value == -1 && byte_sign)) {
147 void WriteString(const char* str) {
154 template<typename T> friend class Slot;
157 T* RawSlotAt(uintptr_t offset) {
158 DCHECK(offset < capacity_ && offset + sizeof(T) <= capacity_);
159 return reinterpret_cast<T*>(&buffer_[offset]);
162 DebugObject* debug_object_;
168 class ELFStringTable;
170 template<typename THeader>
171 class DebugSectionBase : public ZoneObject {
173 virtual ~DebugSectionBase() { }
175 virtual void WriteBody(Writer::Slot<THeader> header, Writer* writer) {
176 uintptr_t start = writer->position();
177 if (WriteBodyInternal(writer)) {
178 uintptr_t end = writer->position();
179 header->offset = start;
180 #if defined(__MACH_O)
183 header->size = end - start;
187 virtual bool WriteBodyInternal(Writer* writer) {
191 typedef THeader Header;
195 struct MachOSectionHeader {
198 #if V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_X87
215 class MachOSection : public DebugSectionBase<MachOSectionHeader> {
219 S_ATTR_COALESCED = 0xbu,
220 S_ATTR_SOME_INSTRUCTIONS = 0x400u,
221 S_ATTR_DEBUG = 0x02000000u,
222 S_ATTR_PURE_INSTRUCTIONS = 0x80000000u
225 MachOSection(const char* name,
234 DCHECK(IsPowerOf2(align));
235 align_ = WhichPowerOf2(align_);
239 virtual ~MachOSection() { }
241 virtual void PopulateHeader(Writer::Slot<Header> header) {
245 header->align = align_;
248 header->flags = flags_;
249 header->reserved1 = 0;
250 header->reserved2 = 0;
251 memset(header->sectname, 0, sizeof(header->sectname));
252 memset(header->segname, 0, sizeof(header->segname));
253 DCHECK(strlen(name_) < sizeof(header->sectname));
254 DCHECK(strlen(segment_) < sizeof(header->segname));
255 strncpy(header->sectname, name_, sizeof(header->sectname));
256 strncpy(header->segname, segment_, sizeof(header->segname));
261 const char* segment_;
267 struct ELFSectionHeader {
277 uintptr_t entry_size;
282 class ELFSection : public DebugSectionBase<ELFSectionHeader> {
297 TYPE_LOPROC = 0x70000000,
298 TYPE_X86_64_UNWIND = 0x70000001,
299 TYPE_HIPROC = 0x7fffffff,
300 TYPE_LOUSER = 0x80000000,
301 TYPE_HIUSER = 0xffffffff
310 enum SpecialIndexes {
311 INDEX_ABSOLUTE = 0xfff1
314 ELFSection(const char* name, Type type, uintptr_t align)
315 : name_(name), type_(type), align_(align) { }
317 virtual ~ELFSection() { }
319 void PopulateHeader(Writer::Slot<Header> header, ELFStringTable* strtab);
321 virtual void WriteBody(Writer::Slot<Header> header, Writer* w) {
322 uintptr_t start = w->position();
323 if (WriteBodyInternal(w)) {
324 uintptr_t end = w->position();
325 header->offset = start;
326 header->size = end - start;
330 virtual bool WriteBodyInternal(Writer* w) {
334 uint16_t index() const { return index_; }
335 void set_index(uint16_t index) { index_ = index; }
338 virtual void PopulateHeader(Writer::Slot<Header> header) {
345 header->entry_size = 0;
354 #endif // defined(__ELF)
357 #if defined(__MACH_O)
358 class MachOTextSection : public MachOSection {
360 MachOTextSection(uintptr_t align,
363 : MachOSection("__text",
366 MachOSection::S_REGULAR |
367 MachOSection::S_ATTR_SOME_INSTRUCTIONS |
368 MachOSection::S_ATTR_PURE_INSTRUCTIONS),
373 virtual void PopulateHeader(Writer::Slot<Header> header) {
374 MachOSection::PopulateHeader(header);
375 header->addr = addr_;
376 header->size = size_;
383 #endif // defined(__MACH_O)
387 class FullHeaderELFSection : public ELFSection {
389 FullHeaderELFSection(const char* name,
396 : ELFSection(name, type, align),
403 virtual void PopulateHeader(Writer::Slot<Header> header) {
404 ELFSection::PopulateHeader(header);
405 header->address = addr_;
406 header->offset = offset_;
407 header->size = size_;
408 header->flags = flags_;
419 class ELFStringTable : public ELFSection {
421 explicit ELFStringTable(const char* name)
422 : ELFSection(name, TYPE_STRTAB, 1), writer_(NULL), offset_(0), size_(0) {
425 uintptr_t Add(const char* str) {
426 if (*str == '\0') return 0;
428 uintptr_t offset = size_;
433 void AttachWriter(Writer* w) {
435 offset_ = writer_->position();
437 // First entry in the string table should be an empty string.
441 void DetachWriter() {
445 virtual void WriteBody(Writer::Slot<Header> header, Writer* w) {
446 DCHECK(writer_ == NULL);
447 header->offset = offset_;
448 header->size = size_;
452 void WriteString(const char* str) {
453 uintptr_t written = 0;
455 writer_->Write(*str);
468 void ELFSection::PopulateHeader(Writer::Slot<ELFSection::Header> header,
469 ELFStringTable* strtab) {
470 header->name = strtab->Add(name_);
471 header->type = type_;
472 header->alignment = align_;
473 PopulateHeader(header);
475 #endif // defined(__ELF)
478 #if defined(__MACH_O)
479 class MachO BASE_EMBEDDED {
481 explicit MachO(Zone* zone) : zone_(zone), sections_(6, zone) { }
483 uint32_t AddSection(MachOSection* section) {
484 sections_.Add(section, zone_);
485 return sections_.length() - 1;
488 void Write(Writer* w, uintptr_t code_start, uintptr_t code_size) {
489 Writer::Slot<MachOHeader> header = WriteHeader(w);
490 uintptr_t load_command_start = w->position();
491 Writer::Slot<MachOSegmentCommand> cmd = WriteSegmentCommand(w,
494 WriteSections(w, cmd, header, load_command_start);
506 #if V8_TARGET_ARCH_X64
511 struct MachOSegmentCommand {
515 #if V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_X87
532 enum MachOLoadCommandCmd {
533 LC_SEGMENT_32 = 0x00000001u,
534 LC_SEGMENT_64 = 0x00000019u
538 Writer::Slot<MachOHeader> WriteHeader(Writer* w) {
539 DCHECK(w->position() == 0);
540 Writer::Slot<MachOHeader> header = w->CreateSlotHere<MachOHeader>();
541 #if V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_X87
542 header->magic = 0xFEEDFACEu;
543 header->cputype = 7; // i386
544 header->cpusubtype = 3; // CPU_SUBTYPE_I386_ALL
545 #elif V8_TARGET_ARCH_X64
546 header->magic = 0xFEEDFACFu;
547 header->cputype = 7 | 0x01000000; // i386 | 64-bit ABI
548 header->cpusubtype = 3; // CPU_SUBTYPE_I386_ALL
549 header->reserved = 0;
551 #error Unsupported target architecture.
553 header->filetype = 0x1; // MH_OBJECT
555 header->sizeofcmds = 0;
561 Writer::Slot<MachOSegmentCommand> WriteSegmentCommand(Writer* w,
562 uintptr_t code_start,
563 uintptr_t code_size) {
564 Writer::Slot<MachOSegmentCommand> cmd =
565 w->CreateSlotHere<MachOSegmentCommand>();
566 #if V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_X87
567 cmd->cmd = LC_SEGMENT_32;
569 cmd->cmd = LC_SEGMENT_64;
571 cmd->vmaddr = code_start;
572 cmd->vmsize = code_size;
578 cmd->nsects = sections_.length();
579 memset(cmd->segname, 0, 16);
580 cmd->cmdsize = sizeof(MachOSegmentCommand) + sizeof(MachOSection::Header) *
586 void WriteSections(Writer* w,
587 Writer::Slot<MachOSegmentCommand> cmd,
588 Writer::Slot<MachOHeader> header,
589 uintptr_t load_command_start) {
590 Writer::Slot<MachOSection::Header> headers =
591 w->CreateSlotsHere<MachOSection::Header>(sections_.length());
592 cmd->fileoff = w->position();
593 header->sizeofcmds = w->position() - load_command_start;
594 for (int section = 0; section < sections_.length(); ++section) {
595 sections_[section]->PopulateHeader(headers.at(section));
596 sections_[section]->WriteBody(headers.at(section), w);
598 cmd->filesize = w->position() - (uintptr_t)cmd->fileoff;
602 ZoneList<MachOSection*> sections_;
604 #endif // defined(__MACH_O)
608 class ELF BASE_EMBEDDED {
610 explicit ELF(Zone* zone) : zone_(zone), sections_(6, zone) {
611 sections_.Add(new(zone) ELFSection("", ELFSection::TYPE_NULL, 0), zone);
612 sections_.Add(new(zone) ELFStringTable(".shstrtab"), zone);
615 void Write(Writer* w) {
617 WriteSectionTable(w);
621 ELFSection* SectionAt(uint32_t index) {
622 return sections_[index];
625 uint32_t AddSection(ELFSection* section) {
626 sections_.Add(section, zone_);
627 section->set_index(sections_.length() - 1);
628 return sections_.length() - 1;
638 uintptr_t pht_offset;
639 uintptr_t sht_offset;
641 uint16_t header_size;
642 uint16_t pht_entry_size;
643 uint16_t pht_entry_num;
644 uint16_t sht_entry_size;
645 uint16_t sht_entry_num;
646 uint16_t sht_strtab_index;
650 void WriteHeader(Writer* w) {
651 DCHECK(w->position() == 0);
652 Writer::Slot<ELFHeader> header = w->CreateSlotHere<ELFHeader>();
653 #if (V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_ARM || V8_TARGET_ARCH_X87 || \
654 (V8_TARGET_ARCH_X64 && V8_TARGET_ARCH_32_BIT))
655 const uint8_t ident[16] =
656 { 0x7f, 'E', 'L', 'F', 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0};
657 #elif V8_TARGET_ARCH_X64 && V8_TARGET_ARCH_64_BIT
658 const uint8_t ident[16] =
659 { 0x7f, 'E', 'L', 'F', 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0};
661 #error Unsupported target architecture.
663 memcpy(header->ident, ident, 16);
665 #if V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_X87
667 #elif V8_TARGET_ARCH_X64
668 // Processor identification value for x64 is 62 as defined in
669 // System V ABI, AMD64 Supplement
670 // http://www.x86-64.org/documentation/abi.pdf
671 header->machine = 62;
672 #elif V8_TARGET_ARCH_ARM
673 // Set to EM_ARM, defined as 40, in "ARM ELF File Format" at
674 // infocenter.arm.com/help/topic/com.arm.doc.dui0101a/DUI0101A_Elf.pdf
675 header->machine = 40;
677 #error Unsupported target architecture.
681 header->pht_offset = 0;
682 header->sht_offset = sizeof(ELFHeader); // Section table follows header.
684 header->header_size = sizeof(ELFHeader);
685 header->pht_entry_size = 0;
686 header->pht_entry_num = 0;
687 header->sht_entry_size = sizeof(ELFSection::Header);
688 header->sht_entry_num = sections_.length();
689 header->sht_strtab_index = 1;
692 void WriteSectionTable(Writer* w) {
693 // Section headers table immediately follows file header.
694 DCHECK(w->position() == sizeof(ELFHeader));
696 Writer::Slot<ELFSection::Header> headers =
697 w->CreateSlotsHere<ELFSection::Header>(sections_.length());
699 // String table for section table is the first section.
700 ELFStringTable* strtab = static_cast<ELFStringTable*>(SectionAt(1));
701 strtab->AttachWriter(w);
702 for (int i = 0, length = sections_.length();
705 sections_[i]->PopulateHeader(headers.at(i), strtab);
707 strtab->DetachWriter();
710 int SectionHeaderPosition(uint32_t section_index) {
711 return sizeof(ELFHeader) + sizeof(ELFSection::Header) * section_index;
714 void WriteSections(Writer* w) {
715 Writer::Slot<ELFSection::Header> headers =
716 w->SlotAt<ELFSection::Header>(sizeof(ELFHeader));
718 for (int i = 0, length = sections_.length();
721 sections_[i]->WriteBody(headers.at(i), w);
726 ZoneList<ELFSection*> sections_;
730 class ELFSymbol BASE_EMBEDDED {
750 ELFSymbol(const char* name,
759 info((binding << 4) | type),
764 Binding binding() const {
765 return static_cast<Binding>(info >> 4);
767 #if (V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_ARM || V8_TARGET_ARCH_X87 || \
768 (V8_TARGET_ARCH_X64 && V8_TARGET_ARCH_32_BIT))
769 struct SerializedLayout {
770 SerializedLayout(uint32_t name,
779 info((binding << 4) | type),
791 #elif V8_TARGET_ARCH_X64 && V8_TARGET_ARCH_64_BIT
792 struct SerializedLayout {
793 SerializedLayout(uint32_t name,
800 info((binding << 4) | type),
816 void Write(Writer::Slot<SerializedLayout> s, ELFStringTable* t) {
817 // Convert symbol names from strings to indexes in the string table.
818 s->name = t->Add(name);
823 s->section = section;
836 class ELFSymbolTable : public ELFSection {
838 ELFSymbolTable(const char* name, Zone* zone)
839 : ELFSection(name, TYPE_SYMTAB, sizeof(uintptr_t)),
844 virtual void WriteBody(Writer::Slot<Header> header, Writer* w) {
845 w->Align(header->alignment);
846 int total_symbols = locals_.length() + globals_.length() + 1;
847 header->offset = w->position();
849 Writer::Slot<ELFSymbol::SerializedLayout> symbols =
850 w->CreateSlotsHere<ELFSymbol::SerializedLayout>(total_symbols);
852 header->size = w->position() - header->offset;
854 // String table for this symbol table should follow it in the section table.
855 ELFStringTable* strtab =
856 static_cast<ELFStringTable*>(w->debug_object()->SectionAt(index() + 1));
857 strtab->AttachWriter(w);
858 symbols.at(0).set(ELFSymbol::SerializedLayout(0,
861 ELFSymbol::BIND_LOCAL,
862 ELFSymbol::TYPE_NOTYPE,
864 WriteSymbolsList(&locals_, symbols.at(1), strtab);
865 WriteSymbolsList(&globals_, symbols.at(locals_.length() + 1), strtab);
866 strtab->DetachWriter();
869 void Add(const ELFSymbol& symbol, Zone* zone) {
870 if (symbol.binding() == ELFSymbol::BIND_LOCAL) {
871 locals_.Add(symbol, zone);
873 globals_.Add(symbol, zone);
878 virtual void PopulateHeader(Writer::Slot<Header> header) {
879 ELFSection::PopulateHeader(header);
880 // We are assuming that string table will follow symbol table.
881 header->link = index() + 1;
882 header->info = locals_.length() + 1;
883 header->entry_size = sizeof(ELFSymbol::SerializedLayout);
887 void WriteSymbolsList(const ZoneList<ELFSymbol>* src,
888 Writer::Slot<ELFSymbol::SerializedLayout> dst,
889 ELFStringTable* strtab) {
890 for (int i = 0, len = src->length();
893 src->at(i).Write(dst.at(i), strtab);
897 ZoneList<ELFSymbol> locals_;
898 ZoneList<ELFSymbol> globals_;
900 #endif // defined(__ELF)
903 class LineInfo : public Malloced {
905 LineInfo() : pc_info_(10) {}
907 void SetPosition(intptr_t pc, int pos, bool is_statement) {
908 AddPCInfo(PCInfo(pc, pos, is_statement));
912 PCInfo(intptr_t pc, int pos, bool is_statement)
913 : pc_(pc), pos_(pos), is_statement_(is_statement) {}
920 List<PCInfo>* pc_info() { return &pc_info_; }
923 void AddPCInfo(const PCInfo& pc_info) { pc_info_.Add(pc_info); }
925 List<PCInfo> pc_info_;
929 class CodeDescription BASE_EMBEDDED {
931 #if V8_TARGET_ARCH_X64
940 CodeDescription(const char* name, Code* code, Handle<Script> script,
941 LineInfo* lineinfo, GDBJITInterface::CodeTag tag,
942 CompilationInfo* info)
950 const char* name() const {
954 LineInfo* lineinfo() const { return lineinfo_; }
956 GDBJITInterface::CodeTag tag() const {
960 CompilationInfo* info() const {
964 bool IsInfoAvailable() const {
965 return info_ != NULL;
968 uintptr_t CodeStart() const {
969 return reinterpret_cast<uintptr_t>(code_->instruction_start());
972 uintptr_t CodeEnd() const {
973 return reinterpret_cast<uintptr_t>(code_->instruction_end());
976 uintptr_t CodeSize() const {
977 return CodeEnd() - CodeStart();
980 bool IsLineInfoAvailable() {
981 return !script_.is_null() &&
982 script_->source()->IsString() &&
983 script_->HasValidSource() &&
984 script_->name()->IsString() &&
988 #if V8_TARGET_ARCH_X64
989 uintptr_t GetStackStateStartAddress(StackState state) const {
990 DCHECK(state < STACK_STATE_MAX);
991 return stack_state_start_addresses_[state];
994 void SetStackStateStartAddress(StackState state, uintptr_t addr) {
995 DCHECK(state < STACK_STATE_MAX);
996 stack_state_start_addresses_[state] = addr;
1000 SmartArrayPointer<char> GetFilename() {
1001 return String::cast(script_->name())->ToCString();
1004 int GetScriptLineNumber(int pos) {
1005 return script_->GetLineNumber(pos) + 1;
1012 Handle<Script> script_;
1013 LineInfo* lineinfo_;
1014 GDBJITInterface::CodeTag tag_;
1015 CompilationInfo* info_;
1016 #if V8_TARGET_ARCH_X64
1017 uintptr_t stack_state_start_addresses_[STACK_STATE_MAX];
1022 static void CreateSymbolsTable(CodeDescription* desc,
1025 int text_section_index) {
1026 ELFSymbolTable* symtab = new(zone) ELFSymbolTable(".symtab", zone);
1027 ELFStringTable* strtab = new(zone) ELFStringTable(".strtab");
1029 // Symbol table should be followed by the linked string table.
1030 elf->AddSection(symtab);
1031 elf->AddSection(strtab);
1033 symtab->Add(ELFSymbol("V8 Code",
1036 ELFSymbol::BIND_LOCAL,
1037 ELFSymbol::TYPE_FILE,
1038 ELFSection::INDEX_ABSOLUTE),
1041 symtab->Add(ELFSymbol(desc->name(),
1044 ELFSymbol::BIND_GLOBAL,
1045 ELFSymbol::TYPE_FUNC,
1046 text_section_index),
1049 #endif // defined(__ELF)
1052 class DebugInfoSection : public DebugSection {
1054 explicit DebugInfoSection(CodeDescription* desc)
1056 : ELFSection(".debug_info", TYPE_PROGBITS, 1),
1058 : MachOSection("__debug_info",
1061 MachOSection::S_REGULAR | MachOSection::S_ATTR_DEBUG),
1066 enum DWARF2LocationOp {
1075 DW_OP_fbreg = 0x91 // 1 param: SLEB128 offset
1078 enum DWARF2Encoding {
1079 DW_ATE_ADDRESS = 0x1,
1083 bool WriteBodyInternal(Writer* w) {
1084 uintptr_t cu_start = w->position();
1085 Writer::Slot<uint32_t> size = w->CreateSlotHere<uint32_t>();
1086 uintptr_t start = w->position();
1087 w->Write<uint16_t>(2); // DWARF version.
1088 w->Write<uint32_t>(0); // Abbreviation table offset.
1089 w->Write<uint8_t>(sizeof(intptr_t));
1091 w->WriteULEB128(1); // Abbreviation code.
1092 w->WriteString(desc_->GetFilename().get());
1093 w->Write<intptr_t>(desc_->CodeStart());
1094 w->Write<intptr_t>(desc_->CodeStart() + desc_->CodeSize());
1095 w->Write<uint32_t>(0);
1097 uint32_t ty_offset = static_cast<uint32_t>(w->position() - cu_start);
1099 w->Write<uint8_t>(kPointerSize);
1100 w->WriteString("v8value");
1102 if (desc_->IsInfoAvailable()) {
1103 Scope* scope = desc_->info()->scope();
1105 w->WriteString(desc_->name());
1106 w->Write<intptr_t>(desc_->CodeStart());
1107 w->Write<intptr_t>(desc_->CodeStart() + desc_->CodeSize());
1108 Writer::Slot<uint32_t> fb_block_size = w->CreateSlotHere<uint32_t>();
1109 uintptr_t fb_block_start = w->position();
1110 #if V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_X87
1111 w->Write<uint8_t>(DW_OP_reg5); // The frame pointer's here on ia32
1112 #elif V8_TARGET_ARCH_X64
1113 w->Write<uint8_t>(DW_OP_reg6); // and here on x64.
1114 #elif V8_TARGET_ARCH_ARM
1116 #elif V8_TARGET_ARCH_MIPS
1118 #elif V8_TARGET_ARCH_MIPS64
1121 #error Unsupported target architecture.
1123 fb_block_size.set(static_cast<uint32_t>(w->position() - fb_block_start));
1125 int params = scope->num_parameters();
1126 int slots = scope->num_stack_slots();
1127 int context_slots = scope->ContextLocalCount();
1128 // The real slot ID is internal_slots + context_slot_id.
1129 int internal_slots = Context::MIN_CONTEXT_SLOTS;
1130 int locals = scope->StackLocalCount();
1131 int current_abbreviation = 4;
1133 for (int param = 0; param < params; ++param) {
1134 w->WriteULEB128(current_abbreviation++);
1136 scope->parameter(param)->name()->ToCString(DISALLOW_NULLS).get());
1137 w->Write<uint32_t>(ty_offset);
1138 Writer::Slot<uint32_t> block_size = w->CreateSlotHere<uint32_t>();
1139 uintptr_t block_start = w->position();
1140 w->Write<uint8_t>(DW_OP_fbreg);
1142 JavaScriptFrameConstants::kLastParameterOffset +
1143 kPointerSize * (params - param - 1));
1144 block_size.set(static_cast<uint32_t>(w->position() - block_start));
1147 EmbeddedVector<char, 256> buffer;
1148 StringBuilder builder(buffer.start(), buffer.length());
1150 for (int slot = 0; slot < slots; ++slot) {
1151 w->WriteULEB128(current_abbreviation++);
1153 builder.AddFormatted("slot%d", slot);
1154 w->WriteString(builder.Finalize());
1157 // See contexts.h for more information.
1158 DCHECK(Context::MIN_CONTEXT_SLOTS == 4);
1159 DCHECK(Context::CLOSURE_INDEX == 0);
1160 DCHECK(Context::PREVIOUS_INDEX == 1);
1161 DCHECK(Context::EXTENSION_INDEX == 2);
1162 DCHECK(Context::GLOBAL_OBJECT_INDEX == 3);
1163 w->WriteULEB128(current_abbreviation++);
1164 w->WriteString(".closure");
1165 w->WriteULEB128(current_abbreviation++);
1166 w->WriteString(".previous");
1167 w->WriteULEB128(current_abbreviation++);
1168 w->WriteString(".extension");
1169 w->WriteULEB128(current_abbreviation++);
1170 w->WriteString(".global");
1172 for (int context_slot = 0;
1173 context_slot < context_slots;
1175 w->WriteULEB128(current_abbreviation++);
1177 builder.AddFormatted("context_slot%d", context_slot + internal_slots);
1178 w->WriteString(builder.Finalize());
1181 ZoneList<Variable*> stack_locals(locals, scope->zone());
1182 ZoneList<Variable*> context_locals(context_slots, scope->zone());
1183 scope->CollectStackAndContextLocals(&stack_locals, &context_locals);
1184 for (int local = 0; local < locals; ++local) {
1185 w->WriteULEB128(current_abbreviation++);
1187 stack_locals[local]->name()->ToCString(DISALLOW_NULLS).get());
1188 w->Write<uint32_t>(ty_offset);
1189 Writer::Slot<uint32_t> block_size = w->CreateSlotHere<uint32_t>();
1190 uintptr_t block_start = w->position();
1191 w->Write<uint8_t>(DW_OP_fbreg);
1193 JavaScriptFrameConstants::kLocal0Offset -
1194 kPointerSize * local);
1195 block_size.set(static_cast<uint32_t>(w->position() - block_start));
1199 w->WriteULEB128(current_abbreviation++);
1200 w->WriteString("__function");
1201 w->Write<uint32_t>(ty_offset);
1202 Writer::Slot<uint32_t> block_size = w->CreateSlotHere<uint32_t>();
1203 uintptr_t block_start = w->position();
1204 w->Write<uint8_t>(DW_OP_fbreg);
1205 w->WriteSLEB128(JavaScriptFrameConstants::kFunctionOffset);
1206 block_size.set(static_cast<uint32_t>(w->position() - block_start));
1210 w->WriteULEB128(current_abbreviation++);
1211 w->WriteString("__context");
1212 w->Write<uint32_t>(ty_offset);
1213 Writer::Slot<uint32_t> block_size = w->CreateSlotHere<uint32_t>();
1214 uintptr_t block_start = w->position();
1215 w->Write<uint8_t>(DW_OP_fbreg);
1216 w->WriteSLEB128(StandardFrameConstants::kContextOffset);
1217 block_size.set(static_cast<uint32_t>(w->position() - block_start));
1220 w->WriteULEB128(0); // Terminate the sub program.
1223 w->WriteULEB128(0); // Terminate the compile unit.
1224 size.set(static_cast<uint32_t>(w->position() - start));
1229 CodeDescription* desc_;
1233 class DebugAbbrevSection : public DebugSection {
1235 explicit DebugAbbrevSection(CodeDescription* desc)
1237 : ELFSection(".debug_abbrev", TYPE_PROGBITS, 1),
1239 : MachOSection("__debug_abbrev",
1242 MachOSection::S_REGULAR | MachOSection::S_ATTR_DEBUG),
1246 // DWARF2 standard, figure 14.
1248 DW_TAG_FORMAL_PARAMETER = 0x05,
1249 DW_TAG_POINTER_TYPE = 0xf,
1250 DW_TAG_COMPILE_UNIT = 0x11,
1251 DW_TAG_STRUCTURE_TYPE = 0x13,
1252 DW_TAG_BASE_TYPE = 0x24,
1253 DW_TAG_SUBPROGRAM = 0x2e,
1254 DW_TAG_VARIABLE = 0x34
1257 // DWARF2 standard, figure 16.
1258 enum DWARF2ChildrenDetermination {
1263 // DWARF standard, figure 17.
1264 enum DWARF2Attribute {
1265 DW_AT_LOCATION = 0x2,
1267 DW_AT_BYTE_SIZE = 0xb,
1268 DW_AT_STMT_LIST = 0x10,
1269 DW_AT_LOW_PC = 0x11,
1270 DW_AT_HIGH_PC = 0x12,
1271 DW_AT_ENCODING = 0x3e,
1272 DW_AT_FRAME_BASE = 0x40,
1276 // DWARF2 standard, figure 19.
1277 enum DWARF2AttributeForm {
1279 DW_FORM_BLOCK4 = 0x4,
1280 DW_FORM_STRING = 0x8,
1281 DW_FORM_DATA4 = 0x6,
1282 DW_FORM_BLOCK = 0x9,
1283 DW_FORM_DATA1 = 0xb,
1288 void WriteVariableAbbreviation(Writer* w,
1289 int abbreviation_code,
1291 bool is_parameter) {
1292 w->WriteULEB128(abbreviation_code);
1293 w->WriteULEB128(is_parameter ? DW_TAG_FORMAL_PARAMETER : DW_TAG_VARIABLE);
1294 w->Write<uint8_t>(DW_CHILDREN_NO);
1295 w->WriteULEB128(DW_AT_NAME);
1296 w->WriteULEB128(DW_FORM_STRING);
1298 w->WriteULEB128(DW_AT_TYPE);
1299 w->WriteULEB128(DW_FORM_REF4);
1300 w->WriteULEB128(DW_AT_LOCATION);
1301 w->WriteULEB128(DW_FORM_BLOCK4);
1307 bool WriteBodyInternal(Writer* w) {
1308 int current_abbreviation = 1;
1309 bool extra_info = desc_->IsInfoAvailable();
1310 DCHECK(desc_->IsLineInfoAvailable());
1311 w->WriteULEB128(current_abbreviation++);
1312 w->WriteULEB128(DW_TAG_COMPILE_UNIT);
1313 w->Write<uint8_t>(extra_info ? DW_CHILDREN_YES : DW_CHILDREN_NO);
1314 w->WriteULEB128(DW_AT_NAME);
1315 w->WriteULEB128(DW_FORM_STRING);
1316 w->WriteULEB128(DW_AT_LOW_PC);
1317 w->WriteULEB128(DW_FORM_ADDR);
1318 w->WriteULEB128(DW_AT_HIGH_PC);
1319 w->WriteULEB128(DW_FORM_ADDR);
1320 w->WriteULEB128(DW_AT_STMT_LIST);
1321 w->WriteULEB128(DW_FORM_DATA4);
1326 Scope* scope = desc_->info()->scope();
1327 int params = scope->num_parameters();
1328 int slots = scope->num_stack_slots();
1329 int context_slots = scope->ContextLocalCount();
1330 // The real slot ID is internal_slots + context_slot_id.
1331 int internal_slots = Context::MIN_CONTEXT_SLOTS;
1332 int locals = scope->StackLocalCount();
1333 // Total children is params + slots + context_slots + internal_slots +
1334 // locals + 2 (__function and __context).
1336 // The extra duplication below seems to be necessary to keep
1337 // gdb from getting upset on OSX.
1338 w->WriteULEB128(current_abbreviation++); // Abbreviation code.
1339 w->WriteULEB128(DW_TAG_SUBPROGRAM);
1340 w->Write<uint8_t>(DW_CHILDREN_YES);
1341 w->WriteULEB128(DW_AT_NAME);
1342 w->WriteULEB128(DW_FORM_STRING);
1343 w->WriteULEB128(DW_AT_LOW_PC);
1344 w->WriteULEB128(DW_FORM_ADDR);
1345 w->WriteULEB128(DW_AT_HIGH_PC);
1346 w->WriteULEB128(DW_FORM_ADDR);
1347 w->WriteULEB128(DW_AT_FRAME_BASE);
1348 w->WriteULEB128(DW_FORM_BLOCK4);
1352 w->WriteULEB128(current_abbreviation++);
1353 w->WriteULEB128(DW_TAG_STRUCTURE_TYPE);
1354 w->Write<uint8_t>(DW_CHILDREN_NO);
1355 w->WriteULEB128(DW_AT_BYTE_SIZE);
1356 w->WriteULEB128(DW_FORM_DATA1);
1357 w->WriteULEB128(DW_AT_NAME);
1358 w->WriteULEB128(DW_FORM_STRING);
1362 for (int param = 0; param < params; ++param) {
1363 WriteVariableAbbreviation(w, current_abbreviation++, true, true);
1366 for (int slot = 0; slot < slots; ++slot) {
1367 WriteVariableAbbreviation(w, current_abbreviation++, false, false);
1370 for (int internal_slot = 0;
1371 internal_slot < internal_slots;
1373 WriteVariableAbbreviation(w, current_abbreviation++, false, false);
1376 for (int context_slot = 0;
1377 context_slot < context_slots;
1379 WriteVariableAbbreviation(w, current_abbreviation++, false, false);
1382 for (int local = 0; local < locals; ++local) {
1383 WriteVariableAbbreviation(w, current_abbreviation++, true, false);
1387 WriteVariableAbbreviation(w, current_abbreviation++, true, false);
1390 WriteVariableAbbreviation(w, current_abbreviation++, true, false);
1392 w->WriteULEB128(0); // Terminate the sibling list.
1395 w->WriteULEB128(0); // Terminate the table.
1400 CodeDescription* desc_;
1404 class DebugLineSection : public DebugSection {
1406 explicit DebugLineSection(CodeDescription* desc)
1408 : ELFSection(".debug_line", TYPE_PROGBITS, 1),
1410 : MachOSection("__debug_line",
1413 MachOSection::S_REGULAR | MachOSection::S_ATTR_DEBUG),
1417 // DWARF2 standard, figure 34.
1418 enum DWARF2Opcodes {
1420 DW_LNS_ADVANCE_PC = 2,
1421 DW_LNS_ADVANCE_LINE = 3,
1422 DW_LNS_SET_FILE = 4,
1423 DW_LNS_SET_COLUMN = 5,
1424 DW_LNS_NEGATE_STMT = 6
1427 // DWARF2 standard, figure 35.
1428 enum DWARF2ExtendedOpcode {
1429 DW_LNE_END_SEQUENCE = 1,
1430 DW_LNE_SET_ADDRESS = 2,
1431 DW_LNE_DEFINE_FILE = 3
1434 bool WriteBodyInternal(Writer* w) {
1436 Writer::Slot<uint32_t> total_length = w->CreateSlotHere<uint32_t>();
1437 uintptr_t start = w->position();
1439 // Used for special opcodes
1440 const int8_t line_base = 1;
1441 const uint8_t line_range = 7;
1442 const int8_t max_line_incr = (line_base + line_range - 1);
1443 const uint8_t opcode_base = DW_LNS_NEGATE_STMT + 1;
1445 w->Write<uint16_t>(2); // Field version.
1446 Writer::Slot<uint32_t> prologue_length = w->CreateSlotHere<uint32_t>();
1447 uintptr_t prologue_start = w->position();
1448 w->Write<uint8_t>(1); // Field minimum_instruction_length.
1449 w->Write<uint8_t>(1); // Field default_is_stmt.
1450 w->Write<int8_t>(line_base); // Field line_base.
1451 w->Write<uint8_t>(line_range); // Field line_range.
1452 w->Write<uint8_t>(opcode_base); // Field opcode_base.
1453 w->Write<uint8_t>(0); // DW_LNS_COPY operands count.
1454 w->Write<uint8_t>(1); // DW_LNS_ADVANCE_PC operands count.
1455 w->Write<uint8_t>(1); // DW_LNS_ADVANCE_LINE operands count.
1456 w->Write<uint8_t>(1); // DW_LNS_SET_FILE operands count.
1457 w->Write<uint8_t>(1); // DW_LNS_SET_COLUMN operands count.
1458 w->Write<uint8_t>(0); // DW_LNS_NEGATE_STMT operands count.
1459 w->Write<uint8_t>(0); // Empty include_directories sequence.
1460 w->WriteString(desc_->GetFilename().get()); // File name.
1461 w->WriteULEB128(0); // Current directory.
1462 w->WriteULEB128(0); // Unknown modification time.
1463 w->WriteULEB128(0); // Unknown file size.
1464 w->Write<uint8_t>(0);
1465 prologue_length.set(static_cast<uint32_t>(w->position() - prologue_start));
1467 WriteExtendedOpcode(w, DW_LNE_SET_ADDRESS, sizeof(intptr_t));
1468 w->Write<intptr_t>(desc_->CodeStart());
1469 w->Write<uint8_t>(DW_LNS_COPY);
1473 bool is_statement = true;
1475 List<LineInfo::PCInfo>* pc_info = desc_->lineinfo()->pc_info();
1476 pc_info->Sort(&ComparePCInfo);
1478 int pc_info_length = pc_info->length();
1479 for (int i = 0; i < pc_info_length; i++) {
1480 LineInfo::PCInfo* info = &pc_info->at(i);
1481 DCHECK(info->pc_ >= pc);
1483 // Reduce bloating in the debug line table by removing duplicate line
1484 // entries (per DWARF2 standard).
1485 intptr_t new_line = desc_->GetScriptLineNumber(info->pos_);
1486 if (new_line == line) {
1490 // Mark statement boundaries. For a better debugging experience, mark
1491 // the last pc address in the function as a statement (e.g. "}"), so that
1492 // a user can see the result of the last line executed in the function,
1493 // should control reach the end.
1494 if ((i+1) == pc_info_length) {
1495 if (!is_statement) {
1496 w->Write<uint8_t>(DW_LNS_NEGATE_STMT);
1498 } else if (is_statement != info->is_statement_) {
1499 w->Write<uint8_t>(DW_LNS_NEGATE_STMT);
1500 is_statement = !is_statement;
1503 // Generate special opcodes, if possible. This results in more compact
1504 // debug line tables. See the DWARF 2.0 standard to learn more about
1506 uintptr_t pc_diff = info->pc_ - pc;
1507 intptr_t line_diff = new_line - line;
1509 // Compute special opcode (see DWARF 2.0 standard)
1510 intptr_t special_opcode = (line_diff - line_base) +
1511 (line_range * pc_diff) + opcode_base;
1513 // If special_opcode is less than or equal to 255, it can be used as a
1514 // special opcode. If line_diff is larger than the max line increment
1515 // allowed for a special opcode, or if line_diff is less than the minimum
1516 // line that can be added to the line register (i.e. line_base), then
1517 // special_opcode can't be used.
1518 if ((special_opcode >= opcode_base) && (special_opcode <= 255) &&
1519 (line_diff <= max_line_incr) && (line_diff >= line_base)) {
1520 w->Write<uint8_t>(special_opcode);
1522 w->Write<uint8_t>(DW_LNS_ADVANCE_PC);
1523 w->WriteSLEB128(pc_diff);
1524 w->Write<uint8_t>(DW_LNS_ADVANCE_LINE);
1525 w->WriteSLEB128(line_diff);
1526 w->Write<uint8_t>(DW_LNS_COPY);
1529 // Increment the pc and line operands.
1533 // Advance the pc to the end of the routine, since the end sequence opcode
1535 w->Write<uint8_t>(DW_LNS_ADVANCE_PC);
1536 w->WriteSLEB128(desc_->CodeSize() - pc);
1537 WriteExtendedOpcode(w, DW_LNE_END_SEQUENCE, 0);
1538 total_length.set(static_cast<uint32_t>(w->position() - start));
1543 void WriteExtendedOpcode(Writer* w,
1544 DWARF2ExtendedOpcode op,
1545 size_t operands_size) {
1546 w->Write<uint8_t>(0);
1547 w->WriteULEB128(operands_size + 1);
1548 w->Write<uint8_t>(op);
1551 static int ComparePCInfo(const LineInfo::PCInfo* a,
1552 const LineInfo::PCInfo* b) {
1553 if (a->pc_ == b->pc_) {
1554 if (a->is_statement_ != b->is_statement_) {
1555 return b->is_statement_ ? +1 : -1;
1558 } else if (a->pc_ > b->pc_) {
1565 CodeDescription* desc_;
1569 #if V8_TARGET_ARCH_X64
1571 class UnwindInfoSection : public DebugSection {
1573 explicit UnwindInfoSection(CodeDescription* desc);
1574 virtual bool WriteBodyInternal(Writer* w);
1576 int WriteCIE(Writer* w);
1577 void WriteFDE(Writer* w, int);
1579 void WriteFDEStateOnEntry(Writer* w);
1580 void WriteFDEStateAfterRBPPush(Writer* w);
1581 void WriteFDEStateAfterRBPSet(Writer* w);
1582 void WriteFDEStateAfterRBPPop(Writer* w);
1584 void WriteLength(Writer* w,
1585 Writer::Slot<uint32_t>* length_slot,
1586 int initial_position);
1589 CodeDescription* desc_;
1591 // DWARF3 Specification, Table 7.23
1592 enum CFIInstructions {
1593 DW_CFA_ADVANCE_LOC = 0x40,
1594 DW_CFA_OFFSET = 0x80,
1595 DW_CFA_RESTORE = 0xC0,
1597 DW_CFA_SET_LOC = 0x01,
1598 DW_CFA_ADVANCE_LOC1 = 0x02,
1599 DW_CFA_ADVANCE_LOC2 = 0x03,
1600 DW_CFA_ADVANCE_LOC4 = 0x04,
1601 DW_CFA_OFFSET_EXTENDED = 0x05,
1602 DW_CFA_RESTORE_EXTENDED = 0x06,
1603 DW_CFA_UNDEFINED = 0x07,
1604 DW_CFA_SAME_VALUE = 0x08,
1605 DW_CFA_REGISTER = 0x09,
1606 DW_CFA_REMEMBER_STATE = 0x0A,
1607 DW_CFA_RESTORE_STATE = 0x0B,
1608 DW_CFA_DEF_CFA = 0x0C,
1609 DW_CFA_DEF_CFA_REGISTER = 0x0D,
1610 DW_CFA_DEF_CFA_OFFSET = 0x0E,
1612 DW_CFA_DEF_CFA_EXPRESSION = 0x0F,
1613 DW_CFA_EXPRESSION = 0x10,
1614 DW_CFA_OFFSET_EXTENDED_SF = 0x11,
1615 DW_CFA_DEF_CFA_SF = 0x12,
1616 DW_CFA_DEF_CFA_OFFSET_SF = 0x13,
1617 DW_CFA_VAL_OFFSET = 0x14,
1618 DW_CFA_VAL_OFFSET_SF = 0x15,
1619 DW_CFA_VAL_EXPRESSION = 0x16
1622 // System V ABI, AMD64 Supplement, Version 0.99.5, Figure 3.36
1623 enum RegisterMapping {
1624 // Only the relevant ones have been added to reduce clutter.
1633 CODE_ALIGN_FACTOR = 1,
1634 DATA_ALIGN_FACTOR = 1,
1635 RETURN_ADDRESS_REGISTER = AMD64_RA
1640 void UnwindInfoSection::WriteLength(Writer* w,
1641 Writer::Slot<uint32_t>* length_slot,
1642 int initial_position) {
1643 uint32_t align = (w->position() - initial_position) % kPointerSize;
1646 for (uint32_t i = 0; i < (kPointerSize - align); i++) {
1647 w->Write<uint8_t>(DW_CFA_NOP);
1651 DCHECK((w->position() - initial_position) % kPointerSize == 0);
1652 length_slot->set(w->position() - initial_position);
1656 UnwindInfoSection::UnwindInfoSection(CodeDescription* desc)
1658 : ELFSection(".eh_frame", TYPE_X86_64_UNWIND, 1),
1660 : MachOSection("__eh_frame", "__TEXT", sizeof(uintptr_t),
1661 MachOSection::S_REGULAR),
1665 int UnwindInfoSection::WriteCIE(Writer* w) {
1666 Writer::Slot<uint32_t> cie_length_slot = w->CreateSlotHere<uint32_t>();
1667 uint32_t cie_position = w->position();
1669 // Write out the CIE header. Currently no 'common instructions' are
1670 // emitted onto the CIE; every FDE has its own set of instructions.
1672 w->Write<uint32_t>(CIE_ID);
1673 w->Write<uint8_t>(CIE_VERSION);
1674 w->Write<uint8_t>(0); // Null augmentation string.
1675 w->WriteSLEB128(CODE_ALIGN_FACTOR);
1676 w->WriteSLEB128(DATA_ALIGN_FACTOR);
1677 w->Write<uint8_t>(RETURN_ADDRESS_REGISTER);
1679 WriteLength(w, &cie_length_slot, cie_position);
1681 return cie_position;
1685 void UnwindInfoSection::WriteFDE(Writer* w, int cie_position) {
1686 // The only FDE for this function. The CFA is the current RBP.
1687 Writer::Slot<uint32_t> fde_length_slot = w->CreateSlotHere<uint32_t>();
1688 int fde_position = w->position();
1689 w->Write<int32_t>(fde_position - cie_position + 4);
1691 w->Write<uintptr_t>(desc_->CodeStart());
1692 w->Write<uintptr_t>(desc_->CodeSize());
1694 WriteFDEStateOnEntry(w);
1695 WriteFDEStateAfterRBPPush(w);
1696 WriteFDEStateAfterRBPSet(w);
1697 WriteFDEStateAfterRBPPop(w);
1699 WriteLength(w, &fde_length_slot, fde_position);
1703 void UnwindInfoSection::WriteFDEStateOnEntry(Writer* w) {
1704 // The first state, just after the control has been transferred to the the
1707 // RBP for this function will be the value of RSP after pushing the RBP
1708 // for the previous function. The previous RBP has not been pushed yet.
1709 w->Write<uint8_t>(DW_CFA_DEF_CFA_SF);
1710 w->WriteULEB128(AMD64_RSP);
1711 w->WriteSLEB128(-kPointerSize);
1713 // The RA is stored at location CFA + kCallerPCOffset. This is an invariant,
1714 // and hence omitted from the next states.
1715 w->Write<uint8_t>(DW_CFA_OFFSET_EXTENDED);
1716 w->WriteULEB128(AMD64_RA);
1717 w->WriteSLEB128(StandardFrameConstants::kCallerPCOffset);
1719 // The RBP of the previous function is still in RBP.
1720 w->Write<uint8_t>(DW_CFA_SAME_VALUE);
1721 w->WriteULEB128(AMD64_RBP);
1723 // Last location described by this entry.
1724 w->Write<uint8_t>(DW_CFA_SET_LOC);
1726 desc_->GetStackStateStartAddress(CodeDescription::POST_RBP_PUSH));
1730 void UnwindInfoSection::WriteFDEStateAfterRBPPush(Writer* w) {
1731 // The second state, just after RBP has been pushed.
1733 // RBP / CFA for this function is now the current RSP, so just set the
1734 // offset from the previous rule (from -8) to 0.
1735 w->Write<uint8_t>(DW_CFA_DEF_CFA_OFFSET);
1738 // The previous RBP is stored at CFA + kCallerFPOffset. This is an invariant
1739 // in this and the next state, and hence omitted in the next state.
1740 w->Write<uint8_t>(DW_CFA_OFFSET_EXTENDED);
1741 w->WriteULEB128(AMD64_RBP);
1742 w->WriteSLEB128(StandardFrameConstants::kCallerFPOffset);
1744 // Last location described by this entry.
1745 w->Write<uint8_t>(DW_CFA_SET_LOC);
1747 desc_->GetStackStateStartAddress(CodeDescription::POST_RBP_SET));
1751 void UnwindInfoSection::WriteFDEStateAfterRBPSet(Writer* w) {
1752 // The third state, after the RBP has been set.
1754 // The CFA can now directly be set to RBP.
1755 w->Write<uint8_t>(DW_CFA_DEF_CFA);
1756 w->WriteULEB128(AMD64_RBP);
1759 // Last location described by this entry.
1760 w->Write<uint8_t>(DW_CFA_SET_LOC);
1762 desc_->GetStackStateStartAddress(CodeDescription::POST_RBP_POP));
1766 void UnwindInfoSection::WriteFDEStateAfterRBPPop(Writer* w) {
1767 // The fourth (final) state. The RBP has been popped (just before issuing a
1770 // The CFA can is now calculated in the same way as in the first state.
1771 w->Write<uint8_t>(DW_CFA_DEF_CFA_SF);
1772 w->WriteULEB128(AMD64_RSP);
1773 w->WriteSLEB128(-kPointerSize);
1776 w->Write<uint8_t>(DW_CFA_OFFSET_EXTENDED);
1777 w->WriteULEB128(AMD64_RBP);
1778 w->WriteSLEB128(StandardFrameConstants::kCallerFPOffset);
1780 // Last location described by this entry.
1781 w->Write<uint8_t>(DW_CFA_SET_LOC);
1782 w->Write<uint64_t>(desc_->CodeEnd());
1786 bool UnwindInfoSection::WriteBodyInternal(Writer* w) {
1787 uint32_t cie_position = WriteCIE(w);
1788 WriteFDE(w, cie_position);
1793 #endif // V8_TARGET_ARCH_X64
1795 static void CreateDWARFSections(CodeDescription* desc,
1798 if (desc->IsLineInfoAvailable()) {
1799 obj->AddSection(new(zone) DebugInfoSection(desc));
1800 obj->AddSection(new(zone) DebugAbbrevSection(desc));
1801 obj->AddSection(new(zone) DebugLineSection(desc));
1803 #if V8_TARGET_ARCH_X64
1804 obj->AddSection(new(zone) UnwindInfoSection(desc));
1809 // -------------------------------------------------------------------
1810 // Binary GDB JIT Interface as described in
1811 // http://sourceware.org/gdb/onlinedocs/gdb/Declarations.html
1819 struct JITCodeEntry {
1820 JITCodeEntry* next_;
1821 JITCodeEntry* prev_;
1822 Address symfile_addr_;
1823 uint64_t symfile_size_;
1826 struct JITDescriptor {
1828 uint32_t action_flag_;
1829 JITCodeEntry* relevant_entry_;
1830 JITCodeEntry* first_entry_;
1833 // GDB will place breakpoint into this function.
1834 // To prevent GCC from inlining or removing it we place noinline attribute
1835 // and inline assembler statement inside.
1836 void __attribute__((noinline)) __jit_debug_register_code() {
1840 // GDB will inspect contents of this descriptor.
1841 // Static initialization is necessary to prevent GDB from seeing
1842 // uninitialized descriptor.
1843 JITDescriptor __jit_debug_descriptor = { 1, 0, 0, 0 };
1846 void __gdb_print_v8_object(Object* object) {
1847 OFStream os(stdout);
1855 static JITCodeEntry* CreateCodeEntry(Address symfile_addr,
1856 uintptr_t symfile_size) {
1857 JITCodeEntry* entry = static_cast<JITCodeEntry*>(
1858 malloc(sizeof(JITCodeEntry) + symfile_size));
1860 entry->symfile_addr_ = reinterpret_cast<Address>(entry + 1);
1861 entry->symfile_size_ = symfile_size;
1862 MemCopy(entry->symfile_addr_, symfile_addr, symfile_size);
1864 entry->prev_ = entry->next_ = NULL;
1870 static void DestroyCodeEntry(JITCodeEntry* entry) {
1875 static void RegisterCodeEntry(JITCodeEntry* entry,
1876 bool dump_if_enabled,
1877 const char* name_hint) {
1878 #if defined(DEBUG) && !V8_OS_WIN
1879 static int file_num = 0;
1880 if (FLAG_gdbjit_dump && dump_if_enabled) {
1881 static const int kMaxFileNameSize = 64;
1882 static const char* kElfFilePrefix = "/tmp/elfdump";
1883 static const char* kObjFileExt = ".o";
1886 SNPrintF(Vector<char>(file_name, kMaxFileNameSize),
1889 (name_hint != NULL) ? name_hint : "",
1892 WriteBytes(file_name, entry->symfile_addr_, entry->symfile_size_);
1896 entry->next_ = __jit_debug_descriptor.first_entry_;
1897 if (entry->next_ != NULL) entry->next_->prev_ = entry;
1898 __jit_debug_descriptor.first_entry_ =
1899 __jit_debug_descriptor.relevant_entry_ = entry;
1901 __jit_debug_descriptor.action_flag_ = JIT_REGISTER_FN;
1902 __jit_debug_register_code();
1906 static void UnregisterCodeEntry(JITCodeEntry* entry) {
1907 if (entry->prev_ != NULL) {
1908 entry->prev_->next_ = entry->next_;
1910 __jit_debug_descriptor.first_entry_ = entry->next_;
1913 if (entry->next_ != NULL) {
1914 entry->next_->prev_ = entry->prev_;
1917 __jit_debug_descriptor.relevant_entry_ = entry;
1918 __jit_debug_descriptor.action_flag_ = JIT_UNREGISTER_FN;
1919 __jit_debug_register_code();
1923 static JITCodeEntry* CreateELFObject(CodeDescription* desc, Isolate* isolate) {
1926 MachO mach_o(&zone);
1929 mach_o.AddSection(new(&zone) MachOTextSection(kCodeAlignment,
1933 CreateDWARFSections(desc, &zone, &mach_o);
1935 mach_o.Write(&w, desc->CodeStart(), desc->CodeSize());
1941 int text_section_index = elf.AddSection(
1942 new(&zone) FullHeaderELFSection(
1944 ELFSection::TYPE_NOBITS,
1949 ELFSection::FLAG_ALLOC | ELFSection::FLAG_EXEC));
1951 CreateSymbolsTable(desc, &zone, &elf, text_section_index);
1953 CreateDWARFSections(desc, &zone, &elf);
1958 return CreateCodeEntry(w.buffer(), w.position());
1962 static bool SameCodeObjects(void* key1, void* key2) {
1963 return key1 == key2;
1967 static HashMap* GetEntries() {
1968 static HashMap* entries = NULL;
1969 if (entries == NULL) {
1970 entries = new HashMap(&SameCodeObjects);
1976 static uint32_t HashForCodeObject(Code* code) {
1977 static const uintptr_t kGoldenRatio = 2654435761u;
1978 uintptr_t hash = reinterpret_cast<uintptr_t>(code->address());
1979 return static_cast<uint32_t>((hash >> kCodeAlignmentBits) * kGoldenRatio);
1983 static const intptr_t kLineInfoTag = 0x1;
1986 static bool IsLineInfoTagged(void* ptr) {
1987 return 0 != (reinterpret_cast<intptr_t>(ptr) & kLineInfoTag);
1991 static void* TagLineInfo(LineInfo* ptr) {
1992 return reinterpret_cast<void*>(
1993 reinterpret_cast<intptr_t>(ptr) | kLineInfoTag);
1997 static LineInfo* UntagLineInfo(void* ptr) {
1998 return reinterpret_cast<LineInfo*>(reinterpret_cast<intptr_t>(ptr) &
2003 void GDBJITInterface::AddCode(Handle<Name> name,
2004 Handle<Script> script,
2006 CompilationInfo* info) {
2007 if (!FLAG_gdbjit) return;
2009 Script::InitLineEnds(script);
2011 if (!name.is_null() && name->IsString()) {
2012 SmartArrayPointer<char> name_cstring =
2013 Handle<String>::cast(name)->ToCString(DISALLOW_NULLS);
2014 AddCode(name_cstring.get(), *code, GDBJITInterface::FUNCTION, *script,
2017 AddCode("", *code, GDBJITInterface::FUNCTION, *script, info);
2022 static void AddUnwindInfo(CodeDescription* desc) {
2023 #if V8_TARGET_ARCH_X64
2024 if (desc->tag() == GDBJITInterface::FUNCTION) {
2025 // To avoid propagating unwinding information through
2026 // compilation pipeline we use an approximation.
2027 // For most use cases this should not affect usability.
2028 static const int kFramePointerPushOffset = 1;
2029 static const int kFramePointerSetOffset = 4;
2030 static const int kFramePointerPopOffset = -3;
2032 uintptr_t frame_pointer_push_address =
2033 desc->CodeStart() + kFramePointerPushOffset;
2035 uintptr_t frame_pointer_set_address =
2036 desc->CodeStart() + kFramePointerSetOffset;
2038 uintptr_t frame_pointer_pop_address =
2039 desc->CodeEnd() + kFramePointerPopOffset;
2041 desc->SetStackStateStartAddress(CodeDescription::POST_RBP_PUSH,
2042 frame_pointer_push_address);
2043 desc->SetStackStateStartAddress(CodeDescription::POST_RBP_SET,
2044 frame_pointer_set_address);
2045 desc->SetStackStateStartAddress(CodeDescription::POST_RBP_POP,
2046 frame_pointer_pop_address);
2048 desc->SetStackStateStartAddress(CodeDescription::POST_RBP_PUSH,
2050 desc->SetStackStateStartAddress(CodeDescription::POST_RBP_SET,
2052 desc->SetStackStateStartAddress(CodeDescription::POST_RBP_POP,
2055 #endif // V8_TARGET_ARCH_X64
2059 static base::LazyMutex mutex = LAZY_MUTEX_INITIALIZER;
2062 void GDBJITInterface::AddCode(const char* name,
2064 GDBJITInterface::CodeTag tag,
2066 CompilationInfo* info) {
2067 base::LockGuard<base::Mutex> lock_guard(mutex.Pointer());
2068 DisallowHeapAllocation no_gc;
2070 HashMap::Entry* e = GetEntries()->Lookup(code, HashForCodeObject(code), true);
2071 if (e->value != NULL && !IsLineInfoTagged(e->value)) return;
2073 LineInfo* lineinfo = UntagLineInfo(e->value);
2074 CodeDescription code_desc(name,
2076 script != NULL ? Handle<Script>(script)
2082 if (!FLAG_gdbjit_full && !code_desc.IsLineInfoAvailable()) {
2084 GetEntries()->Remove(code, HashForCodeObject(code));
2088 AddUnwindInfo(&code_desc);
2089 Isolate* isolate = code->GetIsolate();
2090 JITCodeEntry* entry = CreateELFObject(&code_desc, isolate);
2091 DCHECK(!IsLineInfoTagged(entry));
2096 const char* name_hint = NULL;
2097 bool should_dump = false;
2098 if (FLAG_gdbjit_dump) {
2099 if (strlen(FLAG_gdbjit_dump_filter) == 0) {
2102 } else if (name != NULL) {
2103 name_hint = strstr(name, FLAG_gdbjit_dump_filter);
2104 should_dump = (name_hint != NULL);
2107 RegisterCodeEntry(entry, should_dump, name_hint);
2111 void GDBJITInterface::RemoveCode(Code* code) {
2112 if (!FLAG_gdbjit) return;
2114 base::LockGuard<base::Mutex> lock_guard(mutex.Pointer());
2115 HashMap::Entry* e = GetEntries()->Lookup(code,
2116 HashForCodeObject(code),
2118 if (e == NULL) return;
2120 if (IsLineInfoTagged(e->value)) {
2121 delete UntagLineInfo(e->value);
2123 JITCodeEntry* entry = static_cast<JITCodeEntry*>(e->value);
2124 UnregisterCodeEntry(entry);
2125 DestroyCodeEntry(entry);
2128 GetEntries()->Remove(code, HashForCodeObject(code));
2132 void GDBJITInterface::RemoveCodeRange(Address start, Address end) {
2133 HashMap* entries = GetEntries();
2134 Zone zone(Isolate::Current());
2135 ZoneList<Code*> dead_codes(1, &zone);
2137 for (HashMap::Entry* e = entries->Start(); e != NULL; e = entries->Next(e)) {
2138 Code* code = reinterpret_cast<Code*>(e->key);
2139 if (code->address() >= start && code->address() < end) {
2140 dead_codes.Add(code, &zone);
2144 for (int i = 0; i < dead_codes.length(); i++) {
2145 RemoveCode(dead_codes.at(i));
2150 static void RegisterDetailedLineInfo(Code* code, LineInfo* line_info) {
2151 base::LockGuard<base::Mutex> lock_guard(mutex.Pointer());
2152 DCHECK(!IsLineInfoTagged(line_info));
2153 HashMap::Entry* e = GetEntries()->Lookup(code, HashForCodeObject(code), true);
2154 DCHECK(e->value == NULL);
2155 e->value = TagLineInfo(line_info);
2159 void GDBJITInterface::EventHandler(const v8::JitCodeEvent* event) {
2160 if (!FLAG_gdbjit) return;
2161 switch (event->type) {
2162 case v8::JitCodeEvent::CODE_ADDED: {
2163 Code* code = Code::GetCodeFromTargetAddress(
2164 reinterpret_cast<Address>(event->code_start));
2165 if (code->kind() == Code::OPTIMIZED_FUNCTION ||
2166 code->kind() == Code::FUNCTION) {
2169 EmbeddedVector<char, 256> buffer;
2170 StringBuilder builder(buffer.start(), buffer.length());
2171 builder.AddSubstring(event->name.str, static_cast<int>(event->name.len));
2172 AddCode(builder.Finalize(), code, NON_FUNCTION, NULL, NULL);
2175 case v8::JitCodeEvent::CODE_MOVED:
2177 case v8::JitCodeEvent::CODE_REMOVED: {
2178 Code* code = Code::GetCodeFromTargetAddress(
2179 reinterpret_cast<Address>(event->code_start));
2183 case v8::JitCodeEvent::CODE_ADD_LINE_POS_INFO: {
2184 LineInfo* line_info = reinterpret_cast<LineInfo*>(event->user_data);
2185 line_info->SetPosition(static_cast<intptr_t>(event->line_info.offset),
2186 static_cast<int>(event->line_info.pos),
2187 event->line_info.position_type ==
2188 v8::JitCodeEvent::STATEMENT_POSITION);
2191 case v8::JitCodeEvent::CODE_START_LINE_INFO_RECORDING: {
2192 v8::JitCodeEvent* mutable_event = const_cast<v8::JitCodeEvent*>(event);
2193 mutable_event->user_data = new LineInfo();
2196 case v8::JitCodeEvent::CODE_END_LINE_INFO_RECORDING: {
2197 LineInfo* line_info = reinterpret_cast<LineInfo*>(event->user_data);
2198 Code* code = Code::GetCodeFromTargetAddress(
2199 reinterpret_cast<Address>(event->code_start));
2200 RegisterDetailedLineInfo(code, line_info);
2207 } } // namespace v8::internal