update main to list global integer variables

This commit is contained in:
Gabriel Ionita 2025-10-25 09:15:00 +01:00
parent 273a322f91
commit c918c1e2e2
Signed by: gabi
SSH Key Fingerprint: SHA256:mrbYmB/SGtDvT3etRoS6pDrMYWxR0/B5GSF6rR3qhhc

View File

@ -1,34 +1,56 @@
#include "symbol.hpp"
#include <cxxopts.hpp>
#include <fcntl.h>
#include <filesystem> #include <filesystem>
#include <iostream> #include <iostream>
#include <libdwarf.h>
#include <string> #include <string>
#include <unistd.h> #include <unistd.h>
#include <cxxopts.hpp>
#include <vector>
int main(int argc, char** argv) { int main(int argc, char **argv) {
cxxopts::Options options("gwatch", "A command-line variable watcher"); cxxopts::Options options("gwatch",
options.add_options() "List global integer variables in a binary");
("var", "Symbol to watch", cxxopts::value<std::string>()) options.add_options()("exec", "Executable to analyze",
("exec", "Executable to run", cxxopts::value<std::filesystem::path>()) cxxopts::value<std::filesystem::path>())(
("h,help", "Print usage"); "h,help", "Print usage");
// TODO: Figure out a way to receive arguments for the executable being ran
// ("--", "Arguents to provide to the executable", cxxopts::value<std::vector<std::string>>()->default_value({}));
auto result = options.parse(argc, argv); auto result = options.parse(argc, argv);
if (result.count("help") || argc < 2) { if (result.count("help") || argc < 2 || !result.count("exec")) {
std::cout << options.help(); std::cout << options.help() << std::endl;
exit(0); return 0;
} }
std::cout << result["var"].as<std::string>() << std::endl; auto exec_path = result["exec"].as<std::filesystem::path>();
std::cout << result["exec"].as<std::filesystem::path>() << std::endl;
// auto pArgs = result["--"].as<std::vector<std::string>>(); // Open the binary file
int fd = open(exec_path.c_str(), O_RDONLY);
if (fd < 0) {
std::cerr << "Error: Cannot open file " << exec_path << std::endl;
return 1;
}
// for (auto x : pArgs) { // Initialize libdwarf
// std::cout << x << std::endl; Dwarf_Debug dbg = nullptr;
// } Dwarf_Error err;
int res = dwarf_init_b(fd, DW_GROUPNUMBER_ANY, nullptr, nullptr, &dbg, &err);
if (res != DW_DLV_OK) {
std::cerr << "Error: Failed to initialize DWARF debug info for "
<< exec_path << std::endl;
std::cerr << "Make sure the binary was compiled with debug symbols (-g)"
<< std::endl;
close(fd);
return 1;
}
// List global integer variables
Symbol symbol;
symbol.list_global_integer_vars(dbg);
// Cleanup
dwarf_finish(dbg);
close(fd);
return 0; return 0;
} }