Index: lldb/tools/debugserver/source/RNBRemote.cpp =================================================================== --- lldb/tools/debugserver/source/RNBRemote.cpp +++ lldb/tools/debugserver/source/RNBRemote.cpp @@ -6068,148 +6068,6 @@ return SendPacket("OK"); } -static bool MachHeaderIsMainExecutable(nub_process_t pid, uint32_t addr_size, - nub_addr_t mach_header_addr, - mach_header &mh) { - DNBLogThreadedIf(LOG_RNB_PROC, "GetMachHeaderForMainExecutable(pid = %u, " - "addr_size = %u, mach_header_addr = " - "0x%16.16llx)", - pid, addr_size, mach_header_addr); - const nub_size_t bytes_read = - DNBProcessMemoryRead(pid, mach_header_addr, sizeof(mh), &mh); - if (bytes_read == sizeof(mh)) { - DNBLogThreadedIf( - LOG_RNB_PROC, "GetMachHeaderForMainExecutable(pid = %u, addr_size = " - "%u, mach_header_addr = 0x%16.16llx): mh = {\n magic = " - "0x%8.8x\n cpu = 0x%8.8x\n sub = 0x%8.8x\n filetype = " - "%u\n ncmds = %u\n sizeofcmds = 0x%8.8x\n flags = " - "0x%8.8x }", - pid, addr_size, mach_header_addr, mh.magic, mh.cputype, mh.cpusubtype, - mh.filetype, mh.ncmds, mh.sizeofcmds, mh.flags); - if ((addr_size == 4 && mh.magic == MH_MAGIC) || - (addr_size == 8 && mh.magic == MH_MAGIC_64)) { - if (mh.filetype == MH_EXECUTE) { - DNBLogThreadedIf(LOG_RNB_PROC, "GetMachHeaderForMainExecutable(pid = " - "%u, addr_size = %u, mach_header_addr = " - "0x%16.16llx) -> this is the " - "executable!!!", - pid, addr_size, mach_header_addr); - return true; - } - } - } - return false; -} - -static nub_addr_t GetMachHeaderForMainExecutable(const nub_process_t pid, - const uint32_t addr_size, - mach_header &mh) { - struct AllImageInfos { - uint32_t version; - uint32_t dylib_info_count; - uint64_t dylib_info_addr; - }; - - uint64_t mach_header_addr = 0; - - const nub_addr_t shlib_addr = DNBProcessGetSharedLibraryInfoAddress(pid); - uint8_t bytes[256]; - nub_size_t bytes_read = 0; - DNBDataRef data(bytes, sizeof(bytes), false); - DNBDataRef::offset_t offset = 0; - data.SetPointerSize(addr_size); - - // When we are sitting at __dyld_start, the kernel has placed the - // address of the mach header of the main executable on the stack. If we - // read the SP and dereference a pointer, we might find the mach header - // for the executable. We also just make sure there is only 1 thread - // since if we are at __dyld_start we shouldn't have multiple threads. - if (DNBProcessGetNumThreads(pid) == 1) { - nub_thread_t tid = DNBProcessGetThreadAtIndex(pid, 0); - if (tid != INVALID_NUB_THREAD) { - DNBRegisterValue sp_value; - if (DNBThreadGetRegisterValueByID(pid, tid, REGISTER_SET_GENERIC, - GENERIC_REGNUM_SP, &sp_value)) { - uint64_t sp = - addr_size == 8 ? sp_value.value.uint64 : sp_value.value.uint32; - bytes_read = DNBProcessMemoryRead(pid, sp, addr_size, bytes); - if (bytes_read == addr_size) { - offset = 0; - mach_header_addr = data.GetPointer(&offset); - if (MachHeaderIsMainExecutable(pid, addr_size, mach_header_addr, mh)) - return mach_header_addr; - } - } - } - } - - // Check the dyld_all_image_info structure for a list of mach header - // since it is a very easy thing to check - if (shlib_addr != INVALID_NUB_ADDRESS) { - bytes_read = - DNBProcessMemoryRead(pid, shlib_addr, sizeof(AllImageInfos), bytes); - if (bytes_read > 0) { - AllImageInfos aii; - offset = 0; - aii.version = data.Get32(&offset); - aii.dylib_info_count = data.Get32(&offset); - if (aii.dylib_info_count > 0) { - aii.dylib_info_addr = data.GetPointer(&offset); - if (aii.dylib_info_addr != 0) { - const size_t image_info_byte_size = 3 * addr_size; - for (uint32_t i = 0; i < aii.dylib_info_count; ++i) { - bytes_read = DNBProcessMemoryRead(pid, aii.dylib_info_addr + - i * image_info_byte_size, - image_info_byte_size, bytes); - if (bytes_read != image_info_byte_size) - break; - offset = 0; - mach_header_addr = data.GetPointer(&offset); - if (MachHeaderIsMainExecutable(pid, addr_size, mach_header_addr, - mh)) - return mach_header_addr; - } - } - } - } - } - - // We failed to find the executable's mach header from the all image - // infos and by dereferencing the stack pointer. Now we fall back to - // enumerating the memory regions and looking for regions that are - // executable. - DNBRegionInfo region_info; - mach_header_addr = 0; - while (DNBProcessMemoryRegionInfo(pid, mach_header_addr, ®ion_info)) { - if (region_info.size == 0) - break; - - if (region_info.permissions & eMemoryPermissionsExecutable) { - DNBLogThreadedIf( - LOG_RNB_PROC, "[0x%16.16llx - 0x%16.16llx) permissions = %c%c%c: " - "checking region for executable mach header", - region_info.addr, region_info.addr + region_info.size, - (region_info.permissions & eMemoryPermissionsReadable) ? 'r' : '-', - (region_info.permissions & eMemoryPermissionsWritable) ? 'w' : '-', - (region_info.permissions & eMemoryPermissionsExecutable) ? 'x' : '-'); - if (MachHeaderIsMainExecutable(pid, addr_size, mach_header_addr, mh)) - return mach_header_addr; - } else { - DNBLogThreadedIf( - LOG_RNB_PROC, - "[0x%16.16llx - 0x%16.16llx): permissions = %c%c%c: skipping region", - region_info.addr, region_info.addr + region_info.size, - (region_info.permissions & eMemoryPermissionsReadable) ? 'r' : '-', - (region_info.permissions & eMemoryPermissionsWritable) ? 'w' : '-', - (region_info.permissions & eMemoryPermissionsExecutable) ? 'x' : '-'); - } - // Set the address to the next mapped region - mach_header_addr = region_info.addr + region_info.size; - } - bzero(&mh, sizeof(mh)); - return INVALID_NUB_ADDRESS; -} - rnb_err_t RNBRemote::HandlePacket_qSymbol(const char *command) { const char *p = command; p += strlen("qSymbol:");