-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor pid_t-related functionality into a new process handle API.
Processes are not pid_t on Windows, so we need a new type for them, and since it is possible for them to have "state", we might as well create an new internal API for these "process handles". Handles are now used when reading memory, when looking up R's symbols, and when doing system-specific things like "suspending the process". This should make these APIs possible to implement for Windows (#3).
- Loading branch information
Showing
10 changed files
with
132 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
#ifndef XRPROF_MEMORY_H | ||
#define XRPROF_MEMORY_H | ||
|
||
#include <unistd.h> /* for pid_t */ | ||
#include "process.h" /* for phandle */ | ||
#include "rdefs.h" /* for RCNTXT, SEXP */ | ||
|
||
size_t copy_address(pid_t pid, void *addr, void *data, size_t len); | ||
int copy_context(pid_t pid, void *addr, RCNTXT *data); | ||
int copy_sexp(pid_t pid, void *addr, SEXP data); | ||
int copy_char(pid_t pid, void *addr, char *data, size_t max_len); | ||
size_t copy_address(phandle pid, void *addr, void *data, size_t len); | ||
int copy_context(phandle pid, void *addr, RCNTXT *data); | ||
int copy_sexp(phandle pid, void *addr, SEXP data); | ||
int copy_char(phandle pid, void *addr, char *data, size_t max_len); | ||
|
||
#endif /* XRPROF_MEMORY_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include <stdio.h> /* for fprintf */ | ||
|
||
#include "process.h" | ||
|
||
#ifdef __linux | ||
#include <sys/ptrace.h> | ||
#include <sys/types.h> | ||
#include <sys/wait.h> | ||
|
||
int proc_create(phandle *out, void *data) { | ||
pid_t *pid = (pid_t *) data; | ||
*out = *pid; | ||
if (ptrace(PTRACE_SEIZE, *out, NULL, NULL)) { | ||
perror("fatal: Failed to attach to remote process"); | ||
return -1; | ||
} | ||
return 0; | ||
} | ||
|
||
int proc_suspend(phandle pid) { | ||
if (ptrace(PTRACE_INTERRUPT, pid, NULL, NULL)) { | ||
perror("fatal: Failed to interrupt remote process"); | ||
return -1; | ||
} | ||
|
||
int wstatus; | ||
if (waitpid(pid, &wstatus, 0) < 0) { | ||
perror("fatal: Failed to obtain remote process status information"); | ||
return -1; | ||
} | ||
if (WIFEXITED(wstatus)) { | ||
fprintf(stderr, "Process %d finished.\n", pid); | ||
return -2; | ||
} else if (WIFSTOPPED(wstatus) && WSTOPSIG(wstatus) == SIGCHLD) { | ||
/* Try again. */ | ||
ptrace(PTRACE_CONT, pid, NULL, NULL); | ||
return proc_suspend(pid); | ||
} else if (WIFSTOPPED(wstatus) && WSTOPSIG(wstatus) != SIGTRAP) { | ||
fprintf(stderr, "fatal: Unexpected stop signal in remote process: %d.\n", | ||
WSTOPSIG(wstatus)); | ||
return -1; | ||
} else if (!WIFSTOPPED(wstatus)) { | ||
fprintf(stderr, "fatal: Unexpected remote process status: %d.\n", | ||
WSTOPSIG(wstatus)); | ||
return -1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
int proc_resume(phandle pid) { | ||
if (ptrace(PTRACE_CONT, pid, NULL, NULL)) { | ||
perror("fatal: Failed to continue remote process"); | ||
return -1; | ||
} | ||
return 0; | ||
} | ||
|
||
int proc_destroy(phandle pid) { | ||
/* We don't actually care if this succeeds or not. */ | ||
ptrace(PTRACE_DETACH, pid, NULL, NULL); | ||
return 0; | ||
} | ||
#else | ||
#error "No support for non-Linux platforms." | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#ifndef XRPROF_PROCESS_H | ||
#define XRPROF_PROCESS_H | ||
|
||
#ifdef __unix | ||
#include <unistd.h> /* for pid_t */ | ||
typedef pid_t phandle; | ||
#else | ||
#error "No support for this platform." | ||
#endif | ||
|
||
int proc_create(phandle *out, void *data); | ||
int proc_suspend(phandle pid); | ||
int proc_resume(phandle pid); | ||
int proc_destroy(phandle pid); | ||
|
||
#endif /* XRPROF_PROCESS_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters