diff --git a/src/main.cpp b/src/main.cpp index 0071fec..496fc6e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,13 +5,17 @@ #include #include #include +#include +#include +#include #include 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())( + "var", "Variable name to watch", cxxopts::value())( "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(); + 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(); + 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);