Hi, I want to rewrite the following C code in Zig.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/user.h>
#include <sys/reg.h>
int main() {
pid_t child ;
long orig_eax;
struct user_regs_struct regs;
printf("Enter the PID of the process to trace: ");
scanf("%d", &child);
int status;
if (ptrace(PTRACE_ATTACH, child, NULL, NULL) == -1) {
perror("ptrace");
exit(1);
}
printf("Tracing process %d...\n", child);
while (1) {
wait(&status);
if (WIFEXITED(status)) {
printf("Child process %d exited.\n", child);
break;
}
orig_eax = ptrace(PTRACE_PEEKUSER, child, 8 * ORIG_RAX, NULL);
if (orig_eax == -1) {
perror("ptrace");
break;
}
printf("Syscall number: %ld\n", orig_eax);
ptrace(PTRACE_SYSCALL, child, NULL, NULL);
}
return 0;
}
However, I noticed that no matter what, my std.os.linux.ptrace(PEEKUSER...
return value is always -3
(18446744073709551613
).
const std = @import("std");
pub fn main() !void {
var iter = std.process.args();
_ = iter.skip();
var pid = try std.fmt.parseInt(
std.os.pid_t,
iter.next() orelse @panic("no pid"),
10,
);
try std.os.ptrace(
std.os.linux.PTRACE.ATTACH,
pid,
0,
0,
);
std.log.info("Tracing process {d}...\n", .{pid});
while (true) {
const orig_eax = std.os.linux.ptrace(std.os.linux.PTRACE.PEEKUSER, pid, 8 * std.os.linux.REG.RAX, 0, 0);
std.log.info("Syscall number: {d}\n", .{@as(i32, @truncate(@as(i64, @bitCast(orig_eax))))});
_ = std.os.linux.ptrace(std.os.linux.PTRACE.SYSCALL, pid, 0, 0, 0);
}
}
I searched through Zig’s GitHub issues and didn’t find anything similar. Did I make any mistakes in my code?
My env, zig 0.12.0-dev.888+130227491