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 <iostream>
#include <libdwarf.h>
#include <string>
#include <unistd.h>
#include <cxxopts.hpp>
#include <vector>
int main(int argc, char** argv) {
cxxopts::Options options("gwatch", "A command-line variable watcher");
options.add_options()
("var", "Symbol to watch", cxxopts::value<std::string>())
("exec", "Executable to run", cxxopts::value<std::filesystem::path>())
("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({}));
int main(int argc, char **argv) {
cxxopts::Options options("gwatch",
"List global integer variables in a binary");
options.add_options()("exec", "Executable to analyze",
cxxopts::value<std::filesystem::path>())(
"h,help", "Print usage");
auto result = options.parse(argc, argv);
if (result.count("help") || argc < 2) {
std::cout << options.help();
exit(0);
if (result.count("help") || argc < 2 || !result.count("exec")) {
std::cout << options.help() << std::endl;
return 0;
}
std::cout << result["var"].as<std::string>() << std::endl;
std::cout << result["exec"].as<std::filesystem::path>() << std::endl;
auto exec_path = result["exec"].as<std::filesystem::path>();
// 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) {
// std::cout << x << std::endl;
// }
// Initialize libdwarf
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;
}