diff --git a/src/main.cpp b/src/main.cpp index 2782997..0071fec 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,34 +1,56 @@ +#include "symbol.hpp" +#include +#include #include #include +#include #include #include -#include -#include -int main(int argc, char** argv) { - cxxopts::Options options("gwatch", "A command-line variable watcher"); - options.add_options() - ("var", "Symbol to watch", cxxopts::value()) - ("exec", "Executable to run", cxxopts::value()) - ("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>()->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())( + "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::endl; - std::cout << result["exec"].as() << std::endl; + auto exec_path = result["exec"].as(); - // auto pArgs = result["--"].as>(); + // 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; }