add --var option and watch mode structure

This commit is contained in:
Gabriel Ionita 2025-10-25 16:40:00 +01:00
parent 106a620dfc
commit 52d857ebbc
Signed by: gabi
SSH Key Fingerprint: SHA256:mrbYmB/SGtDvT3etRoS6pDrMYWxR0/B5GSF6rR3qhhc

View File

@ -5,13 +5,17 @@
#include <iostream>
#include <libdwarf.h>
#include <string>
#include <sys/ptrace.h>
#include <sys/user.h>
#include <sys/wait.h>
#include <unistd.h>
int main(int argc, char **argv) {
cxxopts::Options options("gwatch",
"List global integer variables in a binary");
options.add_options()("exec", "Executable to analyze",
"Watch global variable reads/writes in a binary");
options.add_options()("exec", "Executable to watch",
cxxopts::value<std::filesystem::path>())(
"var", "Variable name to watch", cxxopts::value<std::string>())(
"h,help", "Print usage");
auto result = options.parse(argc, argv);
@ -22,6 +26,7 @@ int main(int argc, char **argv) {
}
auto exec_path = result["exec"].as<std::filesystem::path>();
bool watch_mode = result.count("var");
// Open the binary file
int fd = open(exec_path.c_str(), O_RDONLY);
@ -44,9 +49,31 @@ int main(int argc, char **argv) {
return 1;
}
// List global integer variables
Symbol symbol;
symbol.list_global_integer_vars(dbg);
if (watch_mode) {
// Watch mode: find the variable and set up ptrace
auto var_name = result["var"].as<std::string>();
auto var_info = symbol.find_global_var(dbg, var_name);
if (!var_info) {
std::cerr << "Error: Variable '" << var_name << "' not found" << std::endl;
dwarf_finish(dbg);
close(fd);
return 1;
}
std::cout << "Watching variable: " << var_info->name << std::endl;
std::cout << "Address: 0x" << std::hex << var_info->address << std::dec << std::endl;
std::cout << "Size: " << var_info->size << " bytes" << std::endl;
std::cout << std::string(50, '-') << std::endl;
// TODO: Implement ptrace watchpoint
std::cerr << "Ptrace watchpoint not yet implemented" << std::endl;
} else {
// List mode: show all global integer variables
symbol.list_global_integer_vars(dbg);
}
// Cleanup
dwarf_finish(dbg);