From 2d11971c7876e7b274d81a0e7a3a6c65004aaa26 Mon Sep 17 00:00:00 2001 From: wargio Date: Thu, 30 May 2024 12:56:18 +0800 Subject: [PATCH] debugging subprocess. --- binrz/rz-test/run.c | 6 ------ librz/util/subprocess.c | 16 +++++++++++++++- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/binrz/rz-test/run.c b/binrz/rz-test/run.c index 3f0df4115fb..a92cff146eb 100644 --- a/binrz/rz-test/run.c +++ b/binrz/rz-test/run.c @@ -23,21 +23,17 @@ static ut8 *crlf2lf(ut8 *str) { return str; } #else -#include #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); @@ -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); diff --git a/librz/util/subprocess.c b/librz/util/subprocess.c index fc97bf73648..9f0f308f97b 100644 --- a/librz/util/subprocess.c +++ b/librz/util/subprocess.c @@ -1032,6 +1032,7 @@ static bool init_pipes(RzSubprocess *proc, const RzSubprocessOpt *opt, int stdin return false; } +#include /** * \brief Start a subprocess, using the options provided in \p opt * @@ -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) { @@ -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) { @@ -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) { @@ -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); @@ -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;