Skip to content

Commit

Permalink
debugging subprocess.
Browse files Browse the repository at this point in the history
  • Loading branch information
wargio committed May 30, 2024
1 parent 3abd929 commit 2d11971
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
6 changes: 0 additions & 6 deletions binrz/rz-test/run.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,17 @@ static ut8 *crlf2lf(ut8 *str) {
return str;
}
#else
#include <pthread.h>
#define crlf2lf(x) (x)
#endif

static RzSubprocessOutput *subprocess_runner(const char *file, const char *args[], size_t args_size,
const char *envvars[], const char *envvals[], size_t env_size, ut64 timeout_ms, void *user) {
eprintf("[%p] rz_subprocess_start\n", pthread_self());
RzSubprocess *proc = rz_subprocess_start(file, args, args_size, envvars, envvals, env_size);
if (!proc) {
return NULL;
}
eprintf("[%p] rz_subprocess_wait\n", pthread_self());
RzSubprocessWaitReason r = rz_subprocess_wait(proc, timeout_ms);
if (r == RZ_SUBPROCESS_TIMEDOUT) {
eprintf("[%p] rz_subprocess_kill\n", pthread_self());
rz_subprocess_kill(proc);
}
RzSubprocessOutput *out = rz_subprocess_drain(proc);
Expand Down Expand Up @@ -158,9 +154,7 @@ static RzSubprocessOutput *run_rz_test(RzTestRunConfig *config, ut64 timeout_ms,
#else
size_t env_size = load_plugins ? 0 : 1;
#endif
eprintf("[%p] run: %s\n", pthread_self(), cmds);
RzSubprocessOutput *out = runner(config->rz_cmd, args.v.a, rz_pvector_len(&args), envvars, envvals, env_size, timeout_ms, user);
eprintf("[%p] end: %s\n", pthread_self(), cmds);
rz_pvector_clear(&args);
#if __WINDOWS__
free(wcmds);
Expand Down
16 changes: 15 additions & 1 deletion librz/util/subprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,7 @@ static bool init_pipes(RzSubprocess *proc, const RzSubprocessOpt *opt, int stdin
return false;
}

#include <pthread.h>
/**
* \brief Start a subprocess, using the options provided in \p opt
*
Expand Down Expand Up @@ -1096,8 +1097,9 @@ RZ_API RZ_OWN RzSubprocess *rz_subprocess_start_opt(RZ_NONNULL const RzSubproces
goto error;
} else if (proc->pid == 0) {
// child

eprintf("[%p] child process!!\n", pthread_self());
if (stderr_pipe[1] != -1) {
eprintf("[%p] child dup2(stderr_pipe[1], STDERR_FILENO)!!\n", pthread_self());
while ((dup2(stderr_pipe[1], STDERR_FILENO) == -1) && (errno == EINTR)) {
}
if (proc->stderr_fd != proc->stdout_fd && proc->stderr_fd != proc->master_fd) {
Expand All @@ -1107,6 +1109,7 @@ RZ_API RZ_OWN RzSubprocess *rz_subprocess_start_opt(RZ_NONNULL const RzSubproces
}

if (stdout_pipe[1] != -1) {
eprintf("[%p] child dup2(stdout_pipe[1], STDOUT_FILENO)!!\n", pthread_self());
while ((dup2(stdout_pipe[1], STDOUT_FILENO) == -1) && (errno == EINTR)) {
}
if (proc->stdout_fd != proc->master_fd) {
Expand All @@ -1115,6 +1118,7 @@ RZ_API RZ_OWN RzSubprocess *rz_subprocess_start_opt(RZ_NONNULL const RzSubproces
}
}
if (stdin_pipe[0] != -1) {
eprintf("[%p] child dup2(stdin_pipe[0], STDIN_FILENO)!!\n", pthread_self());
while ((dup2(stdin_pipe[0], STDIN_FILENO) == -1) && (errno == EINTR)) {
}
if (proc->stdin_fd != proc->master_fd) {
Expand All @@ -1123,20 +1127,27 @@ RZ_API RZ_OWN RzSubprocess *rz_subprocess_start_opt(RZ_NONNULL const RzSubproces
}
}

eprintf("[%p] child closing master!!\n", pthread_self());
if (proc->master_fd != -1 && close(proc->master_fd)) {
perror("close");
}
eprintf("[%p] child closing slave!!\n", pthread_self());
if (proc->slave_fd != -1 && close(proc->slave_fd)) {
perror("close");
}

// Use the previously created environment
eprintf("[%p] child set env!!\n", pthread_self());
rz_sys_set_environ(child_env);

eprintf("[%p] child execvp(%s)!!\n", pthread_self(), opt->file);
rz_sys_execvp(opt->file, argv);

eprintf("[%p] exit!!\n", pthread_self());
perror("exec");
rz_sys_exit(-1, true);
}
eprintf("[%p] parent process!!\n", pthread_self());
destroy_child_env(child_env);
free(argv);

Expand Down Expand Up @@ -1311,8 +1322,11 @@ static RzSubprocessWaitReason subprocess_wait(RzSubprocess *proc, ut64 timeout_m
timeout_s.tv_sec = usec_diff / RZ_USEC_PER_SEC;
timeout_s.tv_usec = usec_diff % RZ_USEC_PER_SEC;
timeout = &timeout_s;
eprintf("[%p] timeout_ms: %llu\n", pthread_self(), timeout_ms);
}
eprintf("[%p] select wait\n", pthread_self());
r = select(nfds, &rfds, NULL, NULL, timeout);
eprintf("[%p] select done\n", pthread_self());
if (r < 0) {
if (errno == EINTR) {
continue;
Expand Down

0 comments on commit 2d11971

Please sign in to comment.