From dfb69f8e28ed3795eec20db127d6bfc0ef26e42f Mon Sep 17 00:00:00 2001 From: Gabriel Ionita Date: Sun, 26 Oct 2025 18:40:00 +0100 Subject: [PATCH] add support for passing arguments to target program --- src/tracer.cpp | 14 ++++++++++++-- src/tracer.hpp | 4 +++- test_with_args.c | 23 +++++++++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 test_with_args.c diff --git a/src/tracer.cpp b/src/tracer.cpp index 5bec9ba..7a8938f 100644 --- a/src/tracer.cpp +++ b/src/tracer.cpp @@ -91,13 +91,23 @@ uint64_t Tracer::get_load_address(pid_t pid, const std::string &exec_path) { return 0; } -void Tracer::watch_variable(const std::string &exec_path, uint64_t address, size_t size) { +void Tracer::watch_variable(const std::string &exec_path, uint64_t address, size_t size, + const std::vector &args) { pid_t pid = fork(); if (pid == 0) { // Child process: execute the target binary ptrace(PTRACE_TRACEME, 0, 0, 0); - execl(exec_path.c_str(), exec_path.c_str(), nullptr); + + // Build argv array for execv + std::vector argv_vec; + argv_vec.push_back(const_cast(exec_path.c_str())); + for (const auto& arg : args) { + argv_vec.push_back(const_cast(arg.c_str())); + } + argv_vec.push_back(nullptr); + + execv(exec_path.c_str(), argv_vec.data()); std::cerr << "Error: Failed to execute " << exec_path << std::endl; exit(1); } else if (pid > 0) { diff --git a/src/tracer.hpp b/src/tracer.hpp index c38e8c9..25313e2 100644 --- a/src/tracer.hpp +++ b/src/tracer.hpp @@ -3,10 +3,12 @@ #include #include +#include class Tracer { public: - void watch_variable(const std::string &exec_path, uint64_t address, size_t size); + void watch_variable(const std::string &exec_path, uint64_t address, size_t size, + const std::vector &args = {}); private: void setup_hardware_watchpoint(pid_t pid, uint64_t address, size_t size); diff --git a/test_with_args.c b/test_with_args.c new file mode 100644 index 0000000..7fa960d --- /dev/null +++ b/test_with_args.c @@ -0,0 +1,23 @@ +#include +#include + +volatile int global_counter = 0; + +int main(int argc, char **argv) { + printf("Program started with %d arguments\n", argc - 1); + + for (int i = 1; i < argc; i++) { + printf(" arg[%d]: %s\n", i, argv[i]); + } + + global_counter = argc; + printf("Set global_counter to %d\n", global_counter); + + if (argc > 1) { + int val = atoi(argv[1]); + global_counter += val; + printf("Added %d, global_counter now = %d\n", val, global_counter); + } + + return 0; +}