add support for passing arguments to target program
This commit is contained in:
parent
eb88701937
commit
dfb69f8e28
@ -91,13 +91,23 @@ uint64_t Tracer::get_load_address(pid_t pid, const std::string &exec_path) {
|
|||||||
return 0;
|
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<std::string> &args) {
|
||||||
pid_t pid = fork();
|
pid_t pid = fork();
|
||||||
|
|
||||||
if (pid == 0) {
|
if (pid == 0) {
|
||||||
// Child process: execute the target binary
|
// Child process: execute the target binary
|
||||||
ptrace(PTRACE_TRACEME, 0, 0, 0);
|
ptrace(PTRACE_TRACEME, 0, 0, 0);
|
||||||
execl(exec_path.c_str(), exec_path.c_str(), nullptr);
|
|
||||||
|
// Build argv array for execv
|
||||||
|
std::vector<char*> argv_vec;
|
||||||
|
argv_vec.push_back(const_cast<char*>(exec_path.c_str()));
|
||||||
|
for (const auto& arg : args) {
|
||||||
|
argv_vec.push_back(const_cast<char*>(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;
|
std::cerr << "Error: Failed to execute " << exec_path << std::endl;
|
||||||
exit(1);
|
exit(1);
|
||||||
} else if (pid > 0) {
|
} else if (pid > 0) {
|
||||||
|
|||||||
@ -3,10 +3,12 @@
|
|||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
class Tracer {
|
class Tracer {
|
||||||
public:
|
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<std::string> &args = {});
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void setup_hardware_watchpoint(pid_t pid, uint64_t address, size_t size);
|
void setup_hardware_watchpoint(pid_t pid, uint64_t address, size_t size);
|
||||||
|
|||||||
23
test_with_args.c
Normal file
23
test_with_args.c
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user