void CommunicationKDP::DumpPacket(Stream &s, const DataExtractor &packet) { const char *error_desc = NULL; if (packet.GetByteSize() < 8) { error_desc = "error: invalid packet (too short): "; } else { lldb::offset_t offset = 0; const uint8_t first_packet_byte = packet.GetU8(&offset); const uint8_t sequence_id = packet.GetU8(&offset); const uint16_t length = packet.GetU16(&offset); const uint32_t key = packet.GetU32(&offset); const CommandType command = ExtractCommand(first_packet_byte); const char *command_name = GetCommandAsCString(command); if (command_name) { const bool is_reply = ExtractIsReply(first_packet_byte); s.Printf("(running=%i) %s %24s: 0x%2.2x 0x%2.2x 0x%4.4x 0x%8.8x ", IsRunning(), is_reply ? "<--" : "-->", command_name, first_packet_byte, sequence_id, length, key); if (is_reply) { // Dump request reply packets switch (command) { // Commands that return a single 32 bit error case KDP_CONNECT: case KDP_WRITEMEM: case KDP_WRITEMEM64: case KDP_BREAKPOINT_SET: case KDP_BREAKPOINT_REMOVE: case KDP_BREAKPOINT_SET64: case KDP_BREAKPOINT_REMOVE64: case KDP_WRITEREGS: case KDP_LOAD: case KDP_WRITEIOPORT: case KDP_WRITEMSR64: { const uint32_t error = packet.GetU32(&offset); s.Printf(" (error=0x%8.8x)", error); } break; case KDP_DISCONNECT: case KDP_REATTACH: case KDP_HOSTREBOOT: case KDP_SUSPEND: case KDP_RESUMECPUS: case KDP_EXCEPTION: case KDP_TERMINATION: // No return value for the reply, just the header to ack s.PutCString(" ()"); break; case KDP_HOSTINFO: { const uint32_t cpu_mask = packet.GetU32(&offset); const uint32_t cpu_type = packet.GetU32(&offset); const uint32_t cpu_subtype = packet.GetU32(&offset); s.Printf(" (cpu_mask=0x%8.8x, cpu_type=0x%8.8x, cpu_subtype=0x%8.8x)", cpu_mask, cpu_type, cpu_subtype); } break; case KDP_VERSION: { const uint32_t version = packet.GetU32(&offset); const uint32_t feature = packet.GetU32(&offset); s.Printf(" (version=0x%8.8x, feature=0x%8.8x)", version, feature); } break; case KDP_REGIONS: { const uint32_t region_count = packet.GetU32(&offset); s.Printf(" (count = %u", region_count); for (uint32_t i = 0; i < region_count; ++i) { const addr_t region_addr = packet.GetPointer(&offset); const uint32_t region_size = packet.GetU32(&offset); const uint32_t region_prot = packet.GetU32(&offset); s.Printf("\n\tregion[%" PRIu64 "] = { range = [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), size = 0x%8.8x, prot = %s }", region_addr, region_addr, region_addr + region_size, region_size, GetPermissionsAsCString(region_prot)); } } break; case KDP_READMEM: case KDP_READMEM64: case KDP_READPHYSMEM64: { const uint32_t error = packet.GetU32(&offset); const uint32_t count = packet.GetByteSize() - offset; s.Printf(" (error = 0x%8.8x:\n", error); if (count > 0) DumpDataExtractor(packet, &s, // Stream to dump to offset, // Offset within "packet" eFormatBytesWithASCII, // Format to use 1, // Size of each item // in bytes count, // Number of items 16, // Number per line m_last_read_memory_addr, // Don't show addresses // before each line 0, 0); // No bitfields } break; case KDP_READREGS: { const uint32_t error = packet.GetU32(&offset); const uint32_t count = packet.GetByteSize() - offset; s.Printf(" (error = 0x%8.8x regs:\n", error); if (count > 0) DumpDataExtractor(packet, &s, // Stream to dump to offset, // Offset within "packet" eFormatHex, // Format to use m_addr_byte_size, // Size of each item // in bytes count / m_addr_byte_size, // Number of items 16 / m_addr_byte_size, // Number per line LLDB_INVALID_ADDRESS, // Don't // show addresses before // each line 0, 0); // No bitfields } break; case KDP_KERNELVERSION: { const char *kernel_version = packet.PeekCStr(8); s.Printf(" (version = \"%s\")", kernel_version); } break; case KDP_MAXBYTES: { const uint32_t max_bytes = packet.GetU32(&offset); s.Printf(" (max_bytes = 0x%8.8x (%u))", max_bytes, max_bytes); } break; case KDP_IMAGEPATH: { const char *path = packet.GetCStr(&offset); s.Printf(" (path = \"%s\")", path); } break; case KDP_READIOPORT: case KDP_READMSR64: { const uint32_t error = packet.GetU32(&offset); const uint32_t count = packet.GetByteSize() - offset; s.Printf(" (error = 0x%8.8x io:\n", error); if (count > 0) DumpDataExtractor(packet, &s, // Stream to dump to offset, // Offset within "packet" eFormatHex, // Format to use 1, // Size of each item in bytes count, // Number of items 16, // Number per line LLDB_INVALID_ADDRESS, // Don't show addresses // before each line 0, 0); // No bitfields } break; case KDP_DUMPINFO: { const uint32_t count = packet.GetByteSize() - offset; s.Printf(" (count = %u, bytes = \n", count); if (count > 0) DumpDataExtractor(packet, &s, // Stream to dump to offset, // Offset within "packet" eFormatHex, // Format to use 1, // Size of each item in // bytes count, // Number of items 16, // Number per line LLDB_INVALID_ADDRESS, // Don't show addresses // before each line 0, 0); // No bitfields } break; default: s.Printf(" (add support for dumping this packet reply!!!"); break; } } else { // Dump request packets switch (command) { case KDP_CONNECT: { const uint16_t reply_port = ntohs(packet.GetU16(&offset)); const uint16_t exc_port = ntohs(packet.GetU16(&offset)); s.Printf(" (reply_port = %u, exc_port = %u, greeting = \"%s\")", reply_port, exc_port, packet.GetCStr(&offset)); } break; case KDP_DISCONNECT: case KDP_HOSTREBOOT: case KDP_HOSTINFO: case KDP_VERSION: case KDP_REGIONS: case KDP_KERNELVERSION: case KDP_MAXBYTES: case KDP_IMAGEPATH: case KDP_SUSPEND: // No args, just the header in the request... s.PutCString(" ()"); break; case KDP_RESUMECPUS: { const uint32_t cpu_mask = packet.GetU32(&offset); s.Printf(" (cpu_mask = 0x%8.8x)", cpu_mask); } break; case KDP_READMEM: { const uint32_t addr = packet.GetU32(&offset); const uint32_t size = packet.GetU32(&offset); s.Printf(" (addr = 0x%8.8x, size = %u)", addr, size); m_last_read_memory_addr = addr; } break; case KDP_WRITEMEM: { const uint32_t addr = packet.GetU32(&offset); const uint32_t size = packet.GetU32(&offset); s.Printf(" (addr = 0x%8.8x, size = %u, bytes = \n", addr, size); if (size > 0) DumpHexBytes(&s, packet.GetData(&offset, size), size, 32, addr); } break; case KDP_READMEM64: { const uint64_t addr = packet.GetU64(&offset); const uint32_t size = packet.GetU32(&offset); s.Printf(" (addr = 0x%16.16" PRIx64 ", size = %u)", addr, size); m_last_read_memory_addr = addr; } break; case KDP_READPHYSMEM64: { const uint64_t addr = packet.GetU64(&offset); const uint32_t size = packet.GetU32(&offset); const uint32_t lcpu = packet.GetU16(&offset); s.Printf(" (addr = 0x%16.16llx, size = %u, lcpu = %u)", addr, size, lcpu); m_last_read_memory_addr = addr; } break; case KDP_WRITEMEM64: { const uint64_t addr = packet.GetU64(&offset); const uint32_t size = packet.GetU32(&offset); s.Printf(" (addr = 0x%16.16" PRIx64 ", size = %u, bytes = \n", addr, size); if (size > 0) DumpHexBytes(&s, packet.GetData(&offset, size), size, 32, addr); } break; case KDP_WRITEPHYSMEM64: { const uint64_t addr = packet.GetU64(&offset); const uint32_t size = packet.GetU32(&offset); const uint32_t lcpu = packet.GetU16(&offset); s.Printf(" (addr = 0x%16.16llx, size = %u, lcpu = %u, bytes = \n", addr, size, lcpu); if (size > 0) DumpHexBytes(&s, packet.GetData(&offset, size), size, 32, addr); } break; case KDP_READREGS: { const uint32_t cpu = packet.GetU32(&offset); const uint32_t flavor = packet.GetU32(&offset); s.Printf(" (cpu = %u, flavor = %u)", cpu, flavor); } break; case KDP_WRITEREGS: { const uint32_t cpu = packet.GetU32(&offset); const uint32_t flavor = packet.GetU32(&offset); const uint32_t nbytes = packet.GetByteSize() - offset; s.Printf(" (cpu = %u, flavor = %u, regs = \n", cpu, flavor); if (nbytes > 0) DumpDataExtractor(packet, &s, // Stream to dump to offset, // Offset within // "packet" eFormatHex, // Format to use m_addr_byte_size, // Size of each item in // bytes nbytes / m_addr_byte_size, // Number of items 16 / m_addr_byte_size, // Number per line LLDB_INVALID_ADDRESS, // Don't show addresses // before each line 0, 0); // No bitfields } break; case KDP_BREAKPOINT_SET: case KDP_BREAKPOINT_REMOVE: { const uint32_t addr = packet.GetU32(&offset); s.Printf(" (addr = 0x%8.8x)", addr); } break; case KDP_BREAKPOINT_SET64: case KDP_BREAKPOINT_REMOVE64: { const uint64_t addr = packet.GetU64(&offset); s.Printf(" (addr = 0x%16.16" PRIx64 ")", addr); } break; case KDP_LOAD: { const char *path = packet.GetCStr(&offset); s.Printf(" (path = \"%s\")", path); } break; case KDP_EXCEPTION: { const uint32_t count = packet.GetU32(&offset); for (uint32_t i = 0; i < count; ++i) { const uint32_t cpu = packet.GetU32(&offset); const uint32_t exc = packet.GetU32(&offset); const uint32_t code = packet.GetU32(&offset); const uint32_t subcode = packet.GetU32(&offset); const char *exc_cstr = NULL; switch (exc) { case 1: exc_cstr = "EXC_BAD_ACCESS"; break; case 2: exc_cstr = "EXC_BAD_INSTRUCTION"; break; case 3: exc_cstr = "EXC_ARITHMETIC"; break; case 4: exc_cstr = "EXC_EMULATION"; break; case 5: exc_cstr = "EXC_SOFTWARE"; break; case 6: exc_cstr = "EXC_BREAKPOINT"; break; case 7: exc_cstr = "EXC_SYSCALL"; break; case 8: exc_cstr = "EXC_MACH_SYSCALL"; break; case 9: exc_cstr = "EXC_RPC_ALERT"; break; case 10: exc_cstr = "EXC_CRASH"; break; default: break; } s.Printf("{ cpu = 0x%8.8x, exc = %s (%u), code = %u (0x%8.8x), " "subcode = %u (0x%8.8x)} ", cpu, exc_cstr, exc, code, code, subcode, subcode); } } break; case KDP_TERMINATION: { const uint32_t term_code = packet.GetU32(&offset); const uint32_t exit_code = packet.GetU32(&offset); s.Printf(" (term_code = 0x%8.8x (%u), exit_code = 0x%8.8x (%u))", term_code, term_code, exit_code, exit_code); } break; case KDP_REATTACH: { const uint16_t reply_port = ntohs(packet.GetU16(&offset)); s.Printf(" (reply_port = %u)", reply_port); } break; case KDP_READMSR64: { const uint32_t address = packet.GetU32(&offset); const uint16_t lcpu = packet.GetU16(&offset); s.Printf(" (address=0x%8.8x, lcpu=0x%4.4x)", address, lcpu); } break; case KDP_WRITEMSR64: { const uint32_t address = packet.GetU32(&offset); const uint16_t lcpu = packet.GetU16(&offset); const uint32_t nbytes = packet.GetByteSize() - offset; s.Printf(" (address=0x%8.8x, lcpu=0x%4.4x, nbytes=0x%8.8x)", lcpu, address, nbytes); if (nbytes > 0) DumpDataExtractor(packet, &s, // Stream to dump to offset, // Offset within "packet" eFormatHex, // Format to use 1, // Size of each item in // bytes nbytes, // Number of items 16, // Number per line LLDB_INVALID_ADDRESS, // Don't show addresses // before each line 0, 0); // No bitfields } break; case KDP_READIOPORT: { const uint16_t lcpu = packet.GetU16(&offset); const uint16_t address = packet.GetU16(&offset); const uint16_t nbytes = packet.GetU16(&offset); s.Printf(" (lcpu=0x%4.4x, address=0x%4.4x, nbytes=%u)", lcpu, address, nbytes); } break; case KDP_WRITEIOPORT: { const uint16_t lcpu = packet.GetU16(&offset); const uint16_t address = packet.GetU16(&offset); const uint16_t nbytes = packet.GetU16(&offset); s.Printf(" (lcpu = %u, addr = 0x%4.4x, nbytes = %u, bytes = \n", lcpu, address, nbytes); if (nbytes > 0) DumpDataExtractor(packet, &s, // Stream to dump to offset, // Offset within "packet" eFormatHex, // Format to use 1, // Size of each item in // bytes nbytes, // Number of items 16, // Number per line LLDB_INVALID_ADDRESS, // Don't show addresses // before each line 0, 0); // No bitfields } break; case KDP_DUMPINFO: { const uint32_t count = packet.GetByteSize() - offset; s.Printf(" (count = %u, bytes = \n", count); if (count > 0) DumpDataExtractor(packet, &s, // Stream to dump to offset, // Offset within "packet" eFormatHex, // Format to use 1, // Size of each item in bytes count, // Number of items 16, // Number per line LLDB_INVALID_ADDRESS, // Don't show addresses before each line 0, 0); // No bitfields } break; } } } else { error_desc = "error: invalid packet command: "; } } if (error_desc) { s.PutCString(error_desc); DumpDataExtractor(packet, &s, // Stream to dump to 0, // Offset into "packet" eFormatBytes, // Dump as hex bytes 1, // Size of each item is 1 for // single bytes packet.GetByteSize(), // Number of bytes UINT32_MAX, // Num bytes per line LLDB_INVALID_ADDRESS, // Base address 0, 0); // Bitfield info set to not do // anything bitfield related } }
static unsigned ParsePLTRelocations(Symtab *symbol_table, user_id_t start_id, unsigned rel_type, const ELFHeader *hdr, const ELFSectionHeader *rel_hdr, const ELFSectionHeader *plt_hdr, const ELFSectionHeader *sym_hdr, const lldb::SectionSP &plt_section_sp, DataExtractor &rel_data, DataExtractor &symtab_data, DataExtractor &strtab_data) { ELFRelocation rel(rel_type); ELFSymbol symbol; uint32_t offset = 0; const unsigned plt_entsize = plt_hdr->sh_entsize; const unsigned num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize; typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel); reloc_info_fn reloc_type; reloc_info_fn reloc_symbol; if (hdr->Is32Bit()) { reloc_type = ELFRelocation::RelocType32; reloc_symbol = ELFRelocation::RelocSymbol32; } else { reloc_type = ELFRelocation::RelocType64; reloc_symbol = ELFRelocation::RelocSymbol64; } unsigned slot_type = hdr->GetRelocationJumpSlotType(); unsigned i; for (i = 0; i < num_relocations; ++i) { if (rel.Parse(rel_data, &offset) == false) break; if (reloc_type(rel) != slot_type) continue; unsigned symbol_offset = reloc_symbol(rel) * sym_hdr->sh_entsize; uint64_t plt_index = (i + 1) * plt_entsize; if (!symbol.Parse(symtab_data, &symbol_offset)) break; const char *symbol_name = strtab_data.PeekCStr(symbol.st_name); bool is_mangled = symbol_name ? (symbol_name[0] == '_' && symbol_name[1] == 'Z') : false; Symbol jump_symbol( i + start_id, // Symbol table index symbol_name, // symbol name. is_mangled, // is the symbol name mangled? eSymbolTypeTrampoline, // Type of this symbol false, // Is this globally visible? false, // Is this symbol debug info? true, // Is this symbol a trampoline? true, // Is this symbol artificial? plt_section_sp, // Section in which this symbol is defined or null. plt_index, // Offset in section or symbol value. plt_entsize, // Size in bytes of this symbol. 0); // Symbol flags. symbol_table->AddSymbol(jump_symbol); } return i; }
// Parse the load commands for an image uint32_t DynamicLoaderMacOSXDYLD::ParseLoadCommands(const DataExtractor &data, ImageInfo &dylib_info, FileSpec *lc_id_dylinker) { lldb::offset_t offset = 0; uint32_t cmd_idx; Segment segment; dylib_info.Clear(true); for (cmd_idx = 0; cmd_idx < dylib_info.header.ncmds; cmd_idx++) { // Clear out any load command specific data from DYLIB_INFO since we are // about to read it. if (data.ValidOffsetForDataOfSize(offset, sizeof(llvm::MachO::load_command))) { llvm::MachO::load_command load_cmd; lldb::offset_t load_cmd_offset = offset; load_cmd.cmd = data.GetU32(&offset); load_cmd.cmdsize = data.GetU32(&offset); switch (load_cmd.cmd) { case llvm::MachO::LC_SEGMENT: { segment.name.SetTrimmedCStringWithLength( (const char *)data.GetData(&offset, 16), 16); // We are putting 4 uint32_t values 4 uint64_t values so we have to use // multiple 32 bit gets below. segment.vmaddr = data.GetU32(&offset); segment.vmsize = data.GetU32(&offset); segment.fileoff = data.GetU32(&offset); segment.filesize = data.GetU32(&offset); // Extract maxprot, initprot, nsects and flags all at once data.GetU32(&offset, &segment.maxprot, 4); dylib_info.segments.push_back(segment); } break; case llvm::MachO::LC_SEGMENT_64: { segment.name.SetTrimmedCStringWithLength( (const char *)data.GetData(&offset, 16), 16); // Extract vmaddr, vmsize, fileoff, and filesize all at once data.GetU64(&offset, &segment.vmaddr, 4); // Extract maxprot, initprot, nsects and flags all at once data.GetU32(&offset, &segment.maxprot, 4); dylib_info.segments.push_back(segment); } break; case llvm::MachO::LC_ID_DYLINKER: if (lc_id_dylinker) { const lldb::offset_t name_offset = load_cmd_offset + data.GetU32(&offset); const char *path = data.PeekCStr(name_offset); lc_id_dylinker->SetFile(path, FileSpec::Style::native); FileSystem::Instance().Resolve(*lc_id_dylinker); } break; case llvm::MachO::LC_UUID: dylib_info.uuid = UUID::fromOptionalData(data.GetData(&offset, 16), 16); break; default: break; } // Set offset to be the beginning of the next load command. offset = load_cmd_offset + load_cmd.cmdsize; } } // All sections listed in the dyld image info structure will all either be // fixed up already, or they will all be off by a single slide amount that is // determined by finding the first segment that is at file offset zero which // also has bytes (a file size that is greater than zero) in the object file. // Determine the slide amount (if any) const size_t num_sections = dylib_info.segments.size(); for (size_t i = 0; i < num_sections; ++i) { // Iterate through the object file sections to find the first section that // starts of file offset zero and that has bytes in the file... if ((dylib_info.segments[i].fileoff == 0 && dylib_info.segments[i].filesize > 0) || (dylib_info.segments[i].name == "__TEXT")) { dylib_info.slide = dylib_info.address - dylib_info.segments[i].vmaddr; // We have found the slide amount, so we can exit this for loop. break; } } return cmd_idx; }
static unsigned ParseSymbols(Symtab *symtab, user_id_t start_id, SectionList *section_list, const ELFSectionHeader *symtab_shdr, const DataExtractor &symtab_data, const DataExtractor &strtab_data) { ELFSymbol symbol; uint32_t offset = 0; const unsigned num_symbols = symtab_data.GetByteSize() / symtab_shdr->sh_entsize; static ConstString text_section_name(".text"); static ConstString init_section_name(".init"); static ConstString fini_section_name(".fini"); static ConstString ctors_section_name(".ctors"); static ConstString dtors_section_name(".dtors"); static ConstString data_section_name(".data"); static ConstString rodata_section_name(".rodata"); static ConstString rodata1_section_name(".rodata1"); static ConstString data2_section_name(".data1"); static ConstString bss_section_name(".bss"); unsigned i; for (i = 0; i < num_symbols; ++i) { if (symbol.Parse(symtab_data, &offset) == false) break; SectionSP symbol_section_sp; SymbolType symbol_type = eSymbolTypeInvalid; Elf64_Half symbol_idx = symbol.st_shndx; switch (symbol_idx) { case SHN_ABS: symbol_type = eSymbolTypeAbsolute; break; case SHN_UNDEF: symbol_type = eSymbolTypeUndefined; break; default: symbol_section_sp = section_list->GetSectionAtIndex(symbol_idx); break; } switch (symbol.getType()) { default: case STT_NOTYPE: // The symbol's type is not specified. break; case STT_OBJECT: // The symbol is associated with a data object, such as a variable, // an array, etc. symbol_type = eSymbolTypeData; break; case STT_FUNC: // The symbol is associated with a function or other executable code. symbol_type = eSymbolTypeCode; break; case STT_SECTION: // The symbol is associated with a section. Symbol table entries of // this type exist primarily for relocation and normally have // STB_LOCAL binding. break; case STT_FILE: // Conventionally, the symbol's name gives the name of the source // file associated with the object file. A file symbol has STB_LOCAL // binding, its section index is SHN_ABS, and it precedes the other // STB_LOCAL symbols for the file, if it is present. symbol_type = eSymbolTypeObjectFile; break; } if (symbol_type == eSymbolTypeInvalid) { if (symbol_section_sp) { const ConstString §_name = symbol_section_sp->GetName(); if (sect_name == text_section_name || sect_name == init_section_name || sect_name == fini_section_name || sect_name == ctors_section_name || sect_name == dtors_section_name) { symbol_type = eSymbolTypeCode; } else if (sect_name == data_section_name || sect_name == data2_section_name || sect_name == rodata_section_name || sect_name == rodata1_section_name || sect_name == bss_section_name) { symbol_type = eSymbolTypeData; } } } uint64_t symbol_value = symbol.st_value; if (symbol_section_sp) symbol_value -= symbol_section_sp->GetFileAddress(); const char *symbol_name = strtab_data.PeekCStr(symbol.st_name); bool is_global = symbol.getBinding() == STB_GLOBAL; uint32_t flags = symbol.st_other << 8 | symbol.st_info; bool is_mangled = symbol_name ? (symbol_name[0] == '_' && symbol_name[1] == 'Z') : false; Symbol dc_symbol( i + start_id, // ID is the original symbol table index. symbol_name, // Symbol name. is_mangled, // Is the symbol name mangled? symbol_type, // Type of this symbol is_global, // Is this globally visible? false, // Is this symbol debug info? false, // Is this symbol a trampoline? false, // Is this symbol artificial? symbol_section_sp, // Section in which this symbol is defined or null. symbol_value, // Offset in section or symbol value. symbol.st_size, // Size in bytes of this symbol. flags); // Symbol flags. symtab->AddSymbol(dc_symbol); } return i; }