1
0
mirror of synced 2024-11-15 03:27:40 +01:00
ImHex/plugins/builtin/source/content/providers.cpp

146 lines
6.3 KiB
C++
Raw Normal View History

#include <hex/api/content_registry.hpp>
#include "content/providers/gdb_provider.hpp"
#include "content/providers/file_provider.hpp"
#include "content/providers/null_provider.hpp"
2021-12-12 00:42:12 +01:00
#include "content/providers/disk_provider.hpp"
#include "content/providers/intel_hex_provider.hpp"
#include "content/providers/motorola_srec_provider.hpp"
#include "content/providers/memory_file_provider.hpp"
2022-11-14 10:02:46 +01:00
#include "content/providers/view_provider.hpp"
feat: Added Linux support to the Process Memory Provider (#1331) <!-- Please provide as much information as possible about what your PR aims to do. PRs with no description will most likely be closed until more information is provided. If you're planing on changing fundamental behaviour or add big new features, please open a GitHub Issue first before starting to work on it. If it's not something big and you still want to contact us about it, feel free to do so ! --> ### Problem description <!-- Describe the bug that you fixed/feature request that you implemented, or link to an existing issue describing it --> Implement a Linux backend for the ProcessMemoryProvider plugin. ### Implementation description <!-- Explain what you did to correct the problem --> Most of the provider code is the same between Windows and Linux. The primary differences are: - enumerate PIDs in `/proc/` to get the process list - use `/proc/<PID>/cmdline` as the process name - parse `/proc/<PID>/maps` to get the module list - reading/writing from memory is done using `process_vm_readv`/`process_vm_writev` NOTE: `sudo setcap CAP_SYS_PTRACE=+eip build/imhex` must be run to give the binary permission to read another process' memory. Running as root user should also work but I would not recommend it. ### Additional things The existing translations keys no longer match since I moved the plugin from `windows` to `builtin`. I'm not well versed in C++ so I attempted to keep my changes rather simple. Feedback is very welcome. --------- Co-authored-by: WerWolv <werwolv98@gmail.com>
2023-12-07 23:33:15 +01:00
#include <content/providers/process_memory_provider.hpp>
2023-12-26 23:42:22 +01:00
#include <content/providers/base64_provider.hpp>
#include <popups/popup_notification.hpp>
#include <hex/api/project_file_manager.hpp>
#include <hex/api/task_manager.hpp>
#include <hex/helpers/fmt.hpp>
#include <nlohmann/json.hpp>
#include <toasts/toast_notification.hpp>
#include <wolv/utils/guards.hpp>
namespace hex::plugin::builtin {
void registerProviders() {
ContentRegistry::Provider::add<FileProvider>(false);
ContentRegistry::Provider::add<NullProvider>(false);
#if !defined(OS_WEB)
2023-11-10 20:47:08 +01:00
ContentRegistry::Provider::add<DiskProvider>();
#endif
ContentRegistry::Provider::add<GDBProvider>();
ContentRegistry::Provider::add<IntelHexProvider>();
ContentRegistry::Provider::add<MotorolaSRECProvider>();
2023-12-26 23:42:22 +01:00
ContentRegistry::Provider::add<Base64Provider>();
ContentRegistry::Provider::add<MemoryFileProvider>(false);
2022-11-14 10:02:46 +01:00
ContentRegistry::Provider::add<ViewProvider>(false);
#if defined(OS_WINDOWS) || (defined(OS_LINUX) && !defined(OS_FREEBSD))
feat: Added Linux support to the Process Memory Provider (#1331) <!-- Please provide as much information as possible about what your PR aims to do. PRs with no description will most likely be closed until more information is provided. If you're planing on changing fundamental behaviour or add big new features, please open a GitHub Issue first before starting to work on it. If it's not something big and you still want to contact us about it, feel free to do so ! --> ### Problem description <!-- Describe the bug that you fixed/feature request that you implemented, or link to an existing issue describing it --> Implement a Linux backend for the ProcessMemoryProvider plugin. ### Implementation description <!-- Explain what you did to correct the problem --> Most of the provider code is the same between Windows and Linux. The primary differences are: - enumerate PIDs in `/proc/` to get the process list - use `/proc/<PID>/cmdline` as the process name - parse `/proc/<PID>/maps` to get the module list - reading/writing from memory is done using `process_vm_readv`/`process_vm_writev` NOTE: `sudo setcap CAP_SYS_PTRACE=+eip build/imhex` must be run to give the binary permission to read another process' memory. Running as root user should also work but I would not recommend it. ### Additional things The existing translations keys no longer match since I moved the plugin from `windows` to `builtin`. I'm not well versed in C++ so I attempted to keep my changes rather simple. Feedback is very welcome. --------- Co-authored-by: WerWolv <werwolv98@gmail.com>
2023-12-07 23:33:15 +01:00
ContentRegistry::Provider::add<ProcessMemoryProvider>();
#endif
ProjectFile::registerHandler({
.basePath = "providers",
.required = true,
2023-12-27 16:33:49 +01:00
.load = [](const std::fs::path &basePath, const Tar &tar) {
auto json = nlohmann::json::parse(tar.readString(basePath / "providers.json"));
auto providerIds = json.at("providers").get<std::vector<int>>();
bool success = true;
std::map<hex::prv::Provider*, std::string> providerWarnings;
for (const auto &id : providerIds) {
auto providerSettings = nlohmann::json::parse(tar.readString(basePath / hex::format("{}.json", id)));
auto providerType = providerSettings.at("type").get<std::string>();
2023-12-06 11:05:13 +01:00
auto newProvider = ImHexApi::Provider::createProvider(providerType, true, false);
ON_SCOPE_EXIT {
if (!success) {
for (auto &task : TaskManager::getRunningTasks())
task->interrupt();
TaskManager::runWhenTasksFinished([]{
for (const auto &provider : ImHexApi::Provider::getProviders())
ImHexApi::Provider::remove(provider, true);
});
}
};
2023-12-06 11:05:13 +01:00
if (newProvider == nullptr) {
// If a provider is not created, it will be overwritten when saving the project,
// so we should prevent the project from loading at all
ui::ToastError::open(
hex::format("hex.builtin.popup.error.project.load"_lang,
hex::format("hex.builtin.popup.error.project.load.create_provider"_lang, providerType)
)
);
success = false;
break;
}
2023-12-06 11:05:13 +01:00
newProvider->setID(id);
bool loaded = false;
try {
2023-12-06 11:05:13 +01:00
newProvider->loadSettings(providerSettings.at("settings"));
loaded = true;
} catch (const std::exception &e){
2023-12-06 11:05:13 +01:00
providerWarnings[newProvider] = e.what();
}
if (loaded) {
2023-12-06 11:05:13 +01:00
if (!newProvider->open() || !newProvider->isAvailable() || !newProvider->isReadable()) {
providerWarnings[newProvider] = newProvider->getErrorMessage();
2023-12-27 16:33:49 +01:00
} else {
EventProviderOpened::post(newProvider);
2023-12-27 16:33:49 +01:00
}
}
}
std::string warningMessage;
2023-11-10 20:47:08 +01:00
for (const auto &warning : providerWarnings){
ImHexApi::Provider::remove(warning.first);
warningMessage.append(
hex::format("\n - {} : {}", warning.first->getName(), warning.second));
}
// If no providers were opened, display an error with
// the warnings that happened when opening them
if (ImHexApi::Provider::getProviders().empty()) {
ui::ToastError::open(hex::format("{}{}", "hex.builtin.popup.error.project.load"_lang, "hex.builtin.popup.error.project.load.no_providers"_lang, warningMessage));
return false;
} else {
// Else, if there are warnings, still display them
if (warningMessage.empty()) {
2023-12-27 16:33:49 +01:00
return true;
} else {
ui::ToastWarning::open(hex::format("hex.builtin.popup.error.project.load.some_providers_failed"_lang, warningMessage));
}
return success;
}
},
2023-12-27 16:33:49 +01:00
.store = [](const std::fs::path &basePath, const Tar &tar) {
std::vector<int> providerIds;
for (const auto &provider : ImHexApi::Provider::getProviders()) {
auto id = provider->getID();
providerIds.push_back(id);
nlohmann::json json;
json["type"] = provider->getTypeName();
2023-11-30 14:40:07 +01:00
json["settings"] = provider->storeSettings({});
tar.writeString(basePath / hex::format("{}.json", id), json.dump(4));
}
tar.writeString(basePath / "providers.json",
nlohmann::json({ { "providers", providerIds } }).dump(4)
);
return true;
}
});
}
feat: Added Linux support to the Process Memory Provider (#1331) <!-- Please provide as much information as possible about what your PR aims to do. PRs with no description will most likely be closed until more information is provided. If you're planing on changing fundamental behaviour or add big new features, please open a GitHub Issue first before starting to work on it. If it's not something big and you still want to contact us about it, feel free to do so ! --> ### Problem description <!-- Describe the bug that you fixed/feature request that you implemented, or link to an existing issue describing it --> Implement a Linux backend for the ProcessMemoryProvider plugin. ### Implementation description <!-- Explain what you did to correct the problem --> Most of the provider code is the same between Windows and Linux. The primary differences are: - enumerate PIDs in `/proc/` to get the process list - use `/proc/<PID>/cmdline` as the process name - parse `/proc/<PID>/maps` to get the module list - reading/writing from memory is done using `process_vm_readv`/`process_vm_writev` NOTE: `sudo setcap CAP_SYS_PTRACE=+eip build/imhex` must be run to give the binary permission to read another process' memory. Running as root user should also work but I would not recommend it. ### Additional things The existing translations keys no longer match since I moved the plugin from `windows` to `builtin`. I'm not well versed in C++ so I attempted to keep my changes rather simple. Feedback is very welcome. --------- Co-authored-by: WerWolv <werwolv98@gmail.com>
2023-12-07 23:33:15 +01:00
}