Files
clang-p2996/lldb/source/Plugins/DynamicLoader/Windows-DYLD/DynamicLoaderWindowsDYLD.cpp
Antonio Afonso d668260f1a Correctly use GetLoadedModuleList to take advantage of libraries-svr4
Summary:
Here's a replacement for D62504. I thought I could use LoadModules to implement this but in reality I can't because there are at few issues with it:
* The LoadModules assumes that the list returned by GetLoadedModuleList is comprehensive in the sense that reflects all the mapped segments, however, this is not true, for instance VDSO entry is not there since it's loaded manually by LoadVDSO using GetMemoryRegionInfo and it doesn't represent a specific shared object in disk. Because of this LoadModules will unload the VDSO module.
* The loader (interpreter) module might have also been loaded using GetMemoryRegionInfo, this is true when we launch the process and the rendezvous structure is not yet available (done through LoadInterpreterModule()). The problem here is that this entry will point to the same file name as the one found in /proc/pid/maps, however, when we read the same module from the r_debug.link_map structure it might be under a different name. This is true at least on CentOS where the loader is a symlink. Because of this LoadModules will unload and load the module in a way where the rendezvous breakpoint is unresolved but not resolved again (because we add the new module first and remove the old one after).

The symlink issue might be fixable by first unloading the old and loading the news (but sounds super brittle), however, I'm not sure how to fix the VDSO issue.
Since I can't trust it I'm just going to use GetLoadedModuleList directly with the same logic that we use today for when we read the linked list in lldb. The only safe thing to do here is to only calculate differences between different snapshots of the svr4 packet itself. This will also cut the dependency this plugin has from LoadModules.

I separated the 2 logics into 2 different functions (remote and not remote) because I don't like mixing 2 different logics in the same function with if/else's. Two different functions makes it easier to reason with I believe. However, I did abstract away the logic that decides if we should take a snapshot or add/remove modules so both functions could reuse it.

The other difference between the two is that on the UpdateSOEntriesFromRemote I take the snapshot only once when state = Consistent because I didn't find a good reason to always update that, as we already got the list from state = Add | Remove. I probably should use the same logic on UpdateSOEntries though I don't see a reason not to since it's really using the same data, just read in different places. Any thoughts here?

It might also be worthwhile to add a test to make sure we don't unload modules that were not actually "unloaded" like the vdso. I haven't done this yet though.
This diff is also missing the option for svr4 like proposed in https://reviews.llvm.org/D62503#1564296, I'll start working on this but wanted to have this up first.

Reviewers: labath, jankratochvil, clayborg, xiaobai

Reviewed By: labath

Subscribers: srhines, JDevlieghere, lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D64013

llvm-svn: 367020
2019-07-25 14:28:21 +00:00

226 lines
7.5 KiB
C++

//===-- DynamicLoaderWindowsDYLD.cpp --------------------------------*- C++
//-*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "DynamicLoaderWindowsDYLD.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/PluginManager.h"
#include "lldb/Target/ExecutionContext.h"
#include "lldb/Target/Platform.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/RegisterContext.h"
#include "lldb/Target/Target.h"
#include "lldb/Target/ThreadPlanStepInstruction.h"
#include "lldb/Utility/Log.h"
#include "llvm/ADT/Triple.h"
using namespace lldb;
using namespace lldb_private;
DynamicLoaderWindowsDYLD::DynamicLoaderWindowsDYLD(Process *process)
: DynamicLoader(process) {}
DynamicLoaderWindowsDYLD::~DynamicLoaderWindowsDYLD() {}
void DynamicLoaderWindowsDYLD::Initialize() {
PluginManager::RegisterPlugin(GetPluginNameStatic(),
GetPluginDescriptionStatic(), CreateInstance);
}
void DynamicLoaderWindowsDYLD::Terminate() {}
ConstString DynamicLoaderWindowsDYLD::GetPluginNameStatic() {
static ConstString g_plugin_name("windows-dyld");
return g_plugin_name;
}
const char *DynamicLoaderWindowsDYLD::GetPluginDescriptionStatic() {
return "Dynamic loader plug-in that watches for shared library "
"loads/unloads in Windows processes.";
}
DynamicLoader *DynamicLoaderWindowsDYLD::CreateInstance(Process *process,
bool force) {
bool should_create = force;
if (!should_create) {
const llvm::Triple &triple_ref =
process->GetTarget().GetArchitecture().GetTriple();
if (triple_ref.getOS() == llvm::Triple::Win32)
should_create = true;
}
if (should_create)
return new DynamicLoaderWindowsDYLD(process);
return nullptr;
}
void DynamicLoaderWindowsDYLD::OnLoadModule(lldb::ModuleSP module_sp,
const ModuleSpec module_spec,
lldb::addr_t module_addr) {
// Resolve the module unless we already have one.
if (!module_sp) {
Status error;
module_sp = m_process->GetTarget().GetOrCreateModule(module_spec,
true /* notify */, &error);
if (error.Fail())
return;
}
m_loaded_modules[module_sp] = module_addr;
UpdateLoadedSectionsCommon(module_sp, module_addr, false);
ModuleList module_list;
module_list.Append(module_sp);
m_process->GetTarget().ModulesDidLoad(module_list);
}
void DynamicLoaderWindowsDYLD::OnUnloadModule(lldb::addr_t module_addr) {
Address resolved_addr;
if (!m_process->GetTarget().ResolveLoadAddress(module_addr, resolved_addr))
return;
ModuleSP module_sp = resolved_addr.GetModule();
if (module_sp) {
m_loaded_modules.erase(module_sp);
UnloadSectionsCommon(module_sp);
ModuleList module_list;
module_list.Append(module_sp);
m_process->GetTarget().ModulesDidUnload(module_list, false);
}
}
lldb::addr_t DynamicLoaderWindowsDYLD::GetLoadAddress(ModuleSP executable) {
// First, see if the load address is already cached.
auto it = m_loaded_modules.find(executable);
if (it != m_loaded_modules.end() && it->second != LLDB_INVALID_ADDRESS)
return it->second;
lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
// Second, try to get it through the process plugins. For a remote process,
// the remote platform will be responsible for providing it.
FileSpec file_spec(executable->GetPlatformFileSpec());
bool is_loaded = false;
Status status =
m_process->GetFileLoadAddress(file_spec, is_loaded, load_addr);
// Servers other than lldb server could respond with a bogus address.
if (status.Success() && is_loaded && load_addr != LLDB_INVALID_ADDRESS) {
m_loaded_modules[executable] = load_addr;
return load_addr;
}
return LLDB_INVALID_ADDRESS;
}
void DynamicLoaderWindowsDYLD::DidAttach() {
Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
LLDB_LOGF(log, "DynamicLoaderWindowsDYLD::%s()", __FUNCTION__);
ModuleSP executable = GetTargetExecutable();
if (!executable.get())
return;
// Try to fetch the load address of the file from the process, since there
// could be randomization of the load address.
lldb::addr_t load_addr = GetLoadAddress(executable);
if (load_addr == LLDB_INVALID_ADDRESS)
return;
// Request the process base address.
lldb::addr_t image_base = m_process->GetImageInfoAddress();
if (image_base == load_addr)
return;
// Rebase the process's modules if there is a mismatch.
UpdateLoadedSections(executable, LLDB_INVALID_ADDRESS, load_addr, false);
ModuleList module_list;
module_list.Append(executable);
m_process->GetTarget().ModulesDidLoad(module_list);
auto error = m_process->LoadModules();
LLDB_LOG_ERROR(log, std::move(error), "failed to load modules: {0}");
}
void DynamicLoaderWindowsDYLD::DidLaunch() {
Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
LLDB_LOGF(log, "DynamicLoaderWindowsDYLD::%s()", __FUNCTION__);
ModuleSP executable = GetTargetExecutable();
if (!executable.get())
return;
lldb::addr_t load_addr = GetLoadAddress(executable);
if (load_addr != LLDB_INVALID_ADDRESS) {
// Update the loaded sections so that the breakpoints can be resolved.
UpdateLoadedSections(executable, LLDB_INVALID_ADDRESS, load_addr, false);
ModuleList module_list;
module_list.Append(executable);
m_process->GetTarget().ModulesDidLoad(module_list);
auto error = m_process->LoadModules();
LLDB_LOG_ERROR(log, std::move(error), "failed to load modules: {0}");
}
}
Status DynamicLoaderWindowsDYLD::CanLoadImage() { return Status(); }
ConstString DynamicLoaderWindowsDYLD::GetPluginName() {
return GetPluginNameStatic();
}
uint32_t DynamicLoaderWindowsDYLD::GetPluginVersion() { return 1; }
ThreadPlanSP
DynamicLoaderWindowsDYLD::GetStepThroughTrampolinePlan(Thread &thread,
bool stop) {
auto arch = m_process->GetTarget().GetArchitecture();
if (arch.GetMachine() != llvm::Triple::x86) {
return ThreadPlanSP();
}
uint64_t pc = thread.GetRegisterContext()->GetPC();
// Max size of an instruction in x86 is 15 bytes.
AddressRange range(pc, 2 * 15);
ExecutionContext exe_ctx(m_process->GetTarget());
DisassemblerSP disassembler_sp = Disassembler::DisassembleRange(
arch, nullptr, nullptr, exe_ctx, range, true);
if (!disassembler_sp) {
return ThreadPlanSP();
}
InstructionList *insn_list = &disassembler_sp->GetInstructionList();
if (insn_list == nullptr) {
return ThreadPlanSP();
}
// First instruction in a x86 Windows trampoline is going to be an indirect
// jump through the IAT and the next one will be a nop (usually there for
// alignment purposes). e.g.:
// 0x70ff4cfc <+956>: jmpl *0x7100c2a8
// 0x70ff4d02 <+962>: nop
auto first_insn = insn_list->GetInstructionAtIndex(0);
auto second_insn = insn_list->GetInstructionAtIndex(1);
if (first_insn == nullptr || second_insn == nullptr ||
strcmp(first_insn->GetMnemonic(&exe_ctx), "jmpl") != 0 ||
strcmp(second_insn->GetMnemonic(&exe_ctx), "nop") != 0) {
return ThreadPlanSP();
}
assert(first_insn->DoesBranch() && !second_insn->DoesBranch());
return ThreadPlanSP(new ThreadPlanStepInstruction(
thread, false, false, eVoteNoOpinion, eVoteNoOpinion));
}