Skip to content

Commit d7e8c74

Browse files
committed
[swift-inspect] add ptrace retries
1 parent 43c9623 commit d7e8c74

File tree

1 file changed

+17
-7
lines changed
  • tools/swift-inspect/Sources/SwiftInspectLinux/SystemHeaders

1 file changed

+17
-7
lines changed

tools/swift-inspect/Sources/SwiftInspectLinux/SystemHeaders/ptrace.h

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,47 @@
1717

1818
#include <linux/ptrace.h>
1919

20+
static inline
21+
int ptrace_wrapper(int op, pid_t pid, void *addr, void *data) {
22+
int retries = 3;
23+
int result;
24+
do {
25+
result = ptrace(op, pid, addr, data);
26+
} while (result < 0 && (errno == EAGAIN || errno == EBUSY) && retries-- > 0);
27+
return result;
28+
}
29+
2030
static inline
2131
int ptrace_attach(pid_t pid) {
22-
return ptrace(PTRACE_ATTACH, pid, 0, 0);
32+
return ptrace_wrapper(PTRACE_ATTACH, pid, 0, 0);
2333
}
2434

2535
static inline
2636
int ptrace_detach(pid_t pid) {
27-
return ptrace(PTRACE_DETACH, pid, 0, 0);
37+
return ptrace_wrapper(PTRACE_DETACH, pid, 0, 0);
2838
}
2939

3040
static inline
3141
int ptrace_continue(pid_t pid) {
32-
return ptrace(PTRACE_CONT, pid, 0, 0);
42+
return ptrace_wrapper(PTRACE_CONT, pid, 0, 0);
3343
}
3444

3545
static inline
3646
int ptrace_getsiginfo(pid_t pid, siginfo_t *siginfo) {
37-
return ptrace(PTRACE_GETSIGINFO, pid, 0, siginfo);
47+
return ptrace_wrapper(PTRACE_GETSIGINFO, pid, 0, siginfo);
3848
}
3949

4050
static inline
4151
int ptrace_pokedata(pid_t pid, unsigned long addr, unsigned long value) {
42-
return ptrace(PTRACE_POKEDATA, pid, addr, value);
52+
return ptrace_wrapper(PTRACE_POKEDATA, pid, (void*)(uintptr_t)addr, (void*)(uintptr_t)value);
4353
}
4454

4555
static inline
4656
int ptrace_getregset(pid_t pid, int type, struct iovec *regs) {
47-
return ptrace(PTRACE_GETREGSET, pid, type, regs);
57+
return ptrace_wrapper(PTRACE_GETREGSET, pid, (void*)(uintptr_t)type, regs);
4858
}
4959

5060
static inline
5161
int ptrace_setregset(pid_t pid, int type, struct iovec *regs) {
52-
return ptrace(PTRACE_SETREGSET, pid, type, regs);
62+
return ptrace_wrapper(PTRACE_SETREGSET, pid, (void*)(uintptr_t)type, regs);
5363
}

0 commit comments

Comments
 (0)