add --var option and watch mode structure
This commit is contained in:
parent
106a620dfc
commit
52d857ebbc
33
src/main.cpp
33
src/main.cpp
@ -5,13 +5,17 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <libdwarf.h>
|
#include <libdwarf.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <sys/ptrace.h>
|
||||||
|
#include <sys/user.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
cxxopts::Options options("gwatch",
|
cxxopts::Options options("gwatch",
|
||||||
"List global integer variables in a binary");
|
"Watch global variable reads/writes in a binary");
|
||||||
options.add_options()("exec", "Executable to analyze",
|
options.add_options()("exec", "Executable to watch",
|
||||||
cxxopts::value<std::filesystem::path>())(
|
cxxopts::value<std::filesystem::path>())(
|
||||||
|
"var", "Variable name to watch", cxxopts::value<std::string>())(
|
||||||
"h,help", "Print usage");
|
"h,help", "Print usage");
|
||||||
|
|
||||||
auto result = options.parse(argc, argv);
|
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>();
|
auto exec_path = result["exec"].as<std::filesystem::path>();
|
||||||
|
bool watch_mode = result.count("var");
|
||||||
|
|
||||||
// Open the binary file
|
// Open the binary file
|
||||||
int fd = open(exec_path.c_str(), O_RDONLY);
|
int fd = open(exec_path.c_str(), O_RDONLY);
|
||||||
@ -44,9 +49,31 @@ int main(int argc, char **argv) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// List global integer variables
|
|
||||||
Symbol symbol;
|
Symbol symbol;
|
||||||
|
|
||||||
|
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);
|
symbol.list_global_integer_vars(dbg);
|
||||||
|
}
|
||||||
|
|
||||||
// Cleanup
|
// Cleanup
|
||||||
dwarf_finish(dbg);
|
dwarf_finish(dbg);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user