[lldb][NFC] Fix all formatting errors in .cpp file headers
Summary:
A *.cpp file header in LLDB (and in LLDB) should like this:
```
//===-- TestUtilities.cpp -------------------------------------------------===//
```
However in LLDB most of our source files have arbitrary changes to this format and
these changes are spreading through LLDB as folks usually just use the existing
source files as templates for their new files (most notably the unnecessary
editor language indicator `-*- C++ -*-` is spreading and in every review
someone is pointing out that this is wrong, resulting in people pointing out that this
is done in the same way in other files).
This patch removes most of these inconsistencies including the editor language indicators,
all the different missing/additional '-' characters, files that center the file name, missing
trailing `===//` (mostly caused by clang-format breaking the line).
Reviewers: aprantl, espindola, jfb, shafik, JDevlieghere
Reviewed By: JDevlieghere
Subscribers: dexonsmith, wuzish, emaste, sdardis, nemanjai, kbarton, MaskRay, atanasyan, arphaman, jfb, abidh, jsji, JDevlieghere, usaxena95, lldb-commits
Tags: #lldb
Differential Revision: https://reviews.llvm.org/D73258
2020-01-24 15:23:27 +08:00
|
|
|
//===-- DynamicLoader.cpp -------------------------------------------------===//
|
2010-06-09 00:52:24 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2010-06-09 00:52:24 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "lldb/Target/DynamicLoader.h"
|
2017-04-07 05:28:29 +08:00
|
|
|
|
2014-02-07 03:02:19 +08:00
|
|
|
#include "lldb/Core/Module.h"
|
2018-11-12 07:16:43 +08:00
|
|
|
#include "lldb/Core/ModuleList.h"
|
2014-02-07 03:02:19 +08:00
|
|
|
#include "lldb/Core/ModuleSpec.h"
|
2016-07-22 20:55:35 +08:00
|
|
|
#include "lldb/Core/PluginManager.h"
|
2014-02-07 03:02:19 +08:00
|
|
|
#include "lldb/Core/Section.h"
|
2018-11-12 07:16:43 +08:00
|
|
|
#include "lldb/Symbol/ObjectFile.h"
|
2016-07-22 20:55:35 +08:00
|
|
|
#include "lldb/Target/MemoryRegionInfo.h"
|
|
|
|
#include "lldb/Target/Process.h"
|
|
|
|
#include "lldb/Target/Target.h"
|
2018-11-12 07:16:43 +08:00
|
|
|
#include "lldb/Utility/ConstString.h"
|
|
|
|
#include "lldb/lldb-private-interfaces.h"
|
2017-04-07 05:28:29 +08:00
|
|
|
|
2018-11-12 07:16:43 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2017-04-07 05:28:29 +08:00
|
|
|
|
2018-11-12 07:16:43 +08:00
|
|
|
#include <memory>
|
2017-04-07 05:28:29 +08:00
|
|
|
|
2018-11-12 07:16:43 +08:00
|
|
|
#include <assert.h>
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
using namespace lldb;
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
|
|
|
DynamicLoader *DynamicLoader::FindPlugin(Process *process,
|
|
|
|
const char *plugin_name) {
|
2016-03-03 08:51:40 +08:00
|
|
|
DynamicLoaderCreateInstance create_callback = nullptr;
|
2010-06-09 00:52:24 +08:00
|
|
|
if (plugin_name) {
|
2013-05-11 05:47:16 +08:00
|
|
|
ConstString const_plugin_name(plugin_name);
|
|
|
|
create_callback =
|
|
|
|
PluginManager::GetDynamicLoaderCreateCallbackForPluginName(
|
|
|
|
const_plugin_name);
|
2010-06-09 00:52:24 +08:00
|
|
|
if (create_callback) {
|
2019-02-13 14:25:41 +08:00
|
|
|
std::unique_ptr<DynamicLoader> instance_up(
|
2013-04-19 06:45:39 +08:00
|
|
|
create_callback(process, true));
|
2019-02-13 14:25:41 +08:00
|
|
|
if (instance_up)
|
|
|
|
return instance_up.release();
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
} else {
|
2016-03-03 08:51:40 +08:00
|
|
|
for (uint32_t idx = 0;
|
|
|
|
(create_callback =
|
|
|
|
PluginManager::GetDynamicLoaderCreateCallbackAtIndex(idx)) !=
|
|
|
|
nullptr;
|
|
|
|
++idx) {
|
2019-02-13 14:25:41 +08:00
|
|
|
std::unique_ptr<DynamicLoader> instance_up(
|
2013-04-19 06:45:39 +08:00
|
|
|
create_callback(process, false));
|
2019-02-13 14:25:41 +08:00
|
|
|
if (instance_up)
|
|
|
|
return instance_up.release();
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2016-03-03 08:51:40 +08:00
|
|
|
return nullptr;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DynamicLoader::DynamicLoader(Process *process) : m_process(process) {}
|
|
|
|
|
2016-03-03 08:51:40 +08:00
|
|
|
DynamicLoader::~DynamicLoader() = default;
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2018-05-01 00:49:04 +08:00
|
|
|
// Accessosors to the global setting as to whether to stop at image (shared
|
|
|
|
// library) loading/unloading.
|
2016-03-03 08:51:40 +08:00
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
bool DynamicLoader::GetStopWhenImagesChange() const {
|
2013-01-26 10:19:28 +08:00
|
|
|
return m_process->GetStopOnSharedLibraryEvents();
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DynamicLoader::SetStopWhenImagesChange(bool stop) {
|
2013-01-26 10:19:28 +08:00
|
|
|
m_process->SetStopOnSharedLibraryEvents(stop);
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2014-02-07 03:02:19 +08:00
|
|
|
ModuleSP DynamicLoader::GetTargetExecutable() {
|
|
|
|
Target &target = m_process->GetTarget();
|
|
|
|
ModuleSP executable = target.GetExecutableModule();
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2016-03-03 08:51:40 +08:00
|
|
|
if (executable) {
|
2018-11-02 01:09:25 +08:00
|
|
|
if (FileSystem::Instance().Exists(executable->GetFileSpec())) {
|
2014-02-07 03:02:19 +08:00
|
|
|
ModuleSpec module_spec(executable->GetFileSpec(),
|
|
|
|
executable->GetArchitecture());
|
2017-04-07 05:28:29 +08:00
|
|
|
auto module_sp = std::make_shared<Module>(module_spec);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2018-05-01 00:49:04 +08:00
|
|
|
// Check if the executable has changed and set it to the target
|
|
|
|
// executable if they differ.
|
2016-03-03 08:51:40 +08:00
|
|
|
if (module_sp && module_sp->GetUUID().IsValid() &&
|
|
|
|
executable->GetUUID().IsValid()) {
|
2014-02-07 03:02:19 +08:00
|
|
|
if (module_sp->GetUUID() != executable->GetUUID())
|
|
|
|
executable.reset();
|
|
|
|
} else if (executable->FileHasChanged()) {
|
|
|
|
executable.reset();
|
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2016-03-03 08:51:40 +08:00
|
|
|
if (!executable) {
|
2019-04-09 07:03:02 +08:00
|
|
|
executable = target.GetOrCreateModule(module_spec, true /* notify */);
|
2014-02-07 03:02:19 +08:00
|
|
|
if (executable.get() != target.GetExecutableModulePointer()) {
|
2018-05-01 00:49:04 +08:00
|
|
|
// Don't load dependent images since we are in dyld where we will
|
|
|
|
// know and find out about all images that are loaded
|
2018-09-20 17:09:05 +08:00
|
|
|
target.SetExecutableModule(executable, eLoadDependentsNo);
|
2014-02-07 03:02:19 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2014-02-07 03:02:19 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2014-02-07 03:02:19 +08:00
|
|
|
return executable;
|
|
|
|
}
|
|
|
|
|
2015-08-24 18:21:55 +08:00
|
|
|
void DynamicLoader::UpdateLoadedSections(ModuleSP module, addr_t link_map_addr,
|
|
|
|
addr_t base_addr,
|
|
|
|
bool base_addr_is_offset) {
|
|
|
|
UpdateLoadedSectionsCommon(module, base_addr, base_addr_is_offset);
|
2014-02-07 03:02:19 +08:00
|
|
|
}
|
|
|
|
|
2015-08-24 18:21:55 +08:00
|
|
|
void DynamicLoader::UpdateLoadedSectionsCommon(ModuleSP module,
|
|
|
|
addr_t base_addr,
|
|
|
|
bool base_addr_is_offset) {
|
2014-02-07 03:02:19 +08:00
|
|
|
bool changed;
|
2014-02-08 06:54:47 +08:00
|
|
|
module->SetLoadAddress(m_process->GetTarget(), base_addr, base_addr_is_offset,
|
|
|
|
changed);
|
2014-02-07 03:02:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DynamicLoader::UnloadSections(const ModuleSP module) {
|
|
|
|
UnloadSectionsCommon(module);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DynamicLoader::UnloadSectionsCommon(const ModuleSP module) {
|
|
|
|
Target &target = m_process->GetTarget();
|
|
|
|
const SectionList *sections = GetSectionListFromModule(module);
|
|
|
|
|
|
|
|
assert(sections && "SectionList missing from unloaded module.");
|
|
|
|
|
|
|
|
const size_t num_sections = sections->GetSize();
|
|
|
|
for (size_t i = 0; i < num_sections; ++i) {
|
|
|
|
SectionSP section_sp(sections->GetSectionAtIndex(i));
|
|
|
|
target.SetSectionUnloaded(section_sp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const SectionList *
|
|
|
|
DynamicLoader::GetSectionListFromModule(const ModuleSP module) const {
|
|
|
|
SectionList *sections = nullptr;
|
2016-03-03 08:51:40 +08:00
|
|
|
if (module) {
|
2014-02-07 03:02:19 +08:00
|
|
|
ObjectFile *obj_file = module->GetObjectFile();
|
2016-03-03 08:51:40 +08:00
|
|
|
if (obj_file != nullptr) {
|
2014-02-07 03:02:19 +08:00
|
|
|
sections = obj_file->GetSectionList();
|
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2014-02-07 03:02:19 +08:00
|
|
|
return sections;
|
|
|
|
}
|
|
|
|
|
2015-08-24 18:21:55 +08:00
|
|
|
ModuleSP DynamicLoader::LoadModuleAtAddress(const FileSpec &file,
|
|
|
|
addr_t link_map_addr,
|
|
|
|
addr_t base_addr,
|
|
|
|
bool base_addr_is_offset) {
|
2014-02-07 03:02:19 +08:00
|
|
|
Target &target = m_process->GetTarget();
|
|
|
|
ModuleList &modules = target.GetImages();
|
2016-07-22 20:55:35 +08:00
|
|
|
ModuleSpec module_spec(file, target.GetArchitecture());
|
2014-02-07 03:02:19 +08:00
|
|
|
ModuleSP module_sp;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2014-02-07 03:02:19 +08:00
|
|
|
if ((module_sp = modules.FindFirstModule(module_spec))) {
|
2015-08-24 18:21:55 +08:00
|
|
|
UpdateLoadedSections(module_sp, link_map_addr, base_addr,
|
|
|
|
base_addr_is_offset);
|
2016-07-22 20:55:35 +08:00
|
|
|
return module_sp;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2014-02-07 03:02:19 +08:00
|
|
|
|
2019-04-09 07:03:02 +08:00
|
|
|
if ((module_sp = target.GetOrCreateModule(module_spec,
|
|
|
|
true /* notify */))) {
|
2015-08-24 18:21:55 +08:00
|
|
|
UpdateLoadedSections(module_sp, link_map_addr, base_addr,
|
|
|
|
base_addr_is_offset);
|
2016-07-22 20:55:35 +08:00
|
|
|
return module_sp;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
2016-07-22 20:55:35 +08:00
|
|
|
bool check_alternative_file_name = true;
|
|
|
|
if (base_addr_is_offset) {
|
|
|
|
// Try to fetch the load address of the file from the process as we need
|
2018-05-01 00:49:04 +08:00
|
|
|
// absolute load address to read the file out of the memory instead of a
|
|
|
|
// load bias.
|
2016-07-22 20:55:35 +08:00
|
|
|
bool is_loaded = false;
|
|
|
|
lldb::addr_t load_addr;
|
2017-05-12 12:51:55 +08:00
|
|
|
Status error = m_process->GetFileLoadAddress(file, is_loaded, load_addr);
|
2016-07-22 20:55:35 +08:00
|
|
|
if (error.Success() && is_loaded) {
|
|
|
|
check_alternative_file_name = false;
|
|
|
|
base_addr = load_addr;
|
2014-02-07 03:02:19 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
2018-05-01 00:49:04 +08:00
|
|
|
// We failed to find the module based on its name. Lets try to check if we
|
|
|
|
// can find a different name based on the memory region info.
|
2016-07-22 20:55:35 +08:00
|
|
|
if (check_alternative_file_name) {
|
|
|
|
MemoryRegionInfo memory_info;
|
2017-05-12 12:51:55 +08:00
|
|
|
Status error = m_process->GetMemoryRegionInfo(base_addr, memory_info);
|
2016-07-22 20:55:35 +08:00
|
|
|
if (error.Success() && memory_info.GetMapped() &&
|
2017-03-31 18:55:55 +08:00
|
|
|
memory_info.GetRange().GetRangeBase() == base_addr &&
|
|
|
|
!(memory_info.GetName().IsEmpty())) {
|
2020-02-11 16:05:37 +08:00
|
|
|
ModuleSpec new_module_spec(FileSpec(memory_info.GetName().GetStringRef()),
|
2018-11-02 05:05:36 +08:00
|
|
|
target.GetArchitecture());
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2016-07-22 20:55:35 +08:00
|
|
|
if ((module_sp = modules.FindFirstModule(new_module_spec))) {
|
2015-08-24 18:21:55 +08:00
|
|
|
UpdateLoadedSections(module_sp, link_map_addr, base_addr, false);
|
2016-07-22 20:55:35 +08:00
|
|
|
return module_sp;
|
2015-08-24 18:21:55 +08:00
|
|
|
}
|
2016-07-22 20:55:35 +08:00
|
|
|
|
2019-04-09 07:03:02 +08:00
|
|
|
if ((module_sp = target.GetOrCreateModule(new_module_spec,
|
|
|
|
true /* notify */))) {
|
2016-07-22 20:55:35 +08:00
|
|
|
UpdateLoadedSections(module_sp, link_map_addr, base_addr, false);
|
|
|
|
return module_sp;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2016-07-22 20:55:35 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2016-07-22 20:55:35 +08:00
|
|
|
|
|
|
|
if ((module_sp = m_process->ReadModuleFromMemory(file, base_addr))) {
|
|
|
|
UpdateLoadedSections(module_sp, link_map_addr, base_addr, false);
|
|
|
|
target.GetImages().AppendIfNeeded(module_sp);
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
2014-02-07 03:02:19 +08:00
|
|
|
return module_sp;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t DynamicLoader::ReadUnsignedIntWithSizeInBytes(addr_t addr,
|
|
|
|
int size_in_bytes) {
|
2017-05-12 12:51:55 +08:00
|
|
|
Status error;
|
2014-02-07 03:02:19 +08:00
|
|
|
uint64_t value =
|
|
|
|
m_process->ReadUnsignedIntegerFromMemory(addr, size_in_bytes, 0, error);
|
|
|
|
if (error.Fail())
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return (int64_t)value;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr_t DynamicLoader::ReadPointer(addr_t addr) {
|
2017-05-12 12:51:55 +08:00
|
|
|
Status error;
|
2014-02-07 03:02:19 +08:00
|
|
|
addr_t value = m_process->ReadPointerFromMemory(addr, error);
|
|
|
|
if (error.Fail())
|
|
|
|
return LLDB_INVALID_ADDRESS;
|
|
|
|
else
|
|
|
|
return value;
|
|
|
|
}
|
2017-03-01 02:57:54 +08:00
|
|
|
|
|
|
|
void DynamicLoader::LoadOperatingSystemPlugin(bool flush)
|
|
|
|
{
|
|
|
|
if (m_process)
|
|
|
|
m_process->LoadOperatingSystemPlugin(flush);
|
|
|
|
}
|
|
|
|
|