[PATCH] fix mch_job_status giving wrong answer for ptraced processes

19 views
Skip to first unread message

strager

unread,
Mar 4, 2023, 5:06:06 AM3/4/23
to vim...@vim.org
(I think my previous email didn't send. I also fixed a bug in my previous patch.)

I want to debug a child process of Vim using a debugger like lldb. These
debuggers use ptrace. ptrace has the effect of reparenting the process.

This reparenting of Vim's child processes confuses Vim. mch_job_status
calls waitpid to detect whether the child process is alive or not, but
waitpid returns -1 (ECHILD) for these ptrace-reparented processes.

Teach Vim to check if the process is really alive using the kill
syscall with signal 0. If it is alive with another parent, treat the job
as still running.
---
src/os_unix.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)

diff --git src/os_unix.c src/os_unix.c
index 0dfbd4977..59698544d 100644
--- src/os_unix.c
+++ src/os_unix.c
@@ -5863,10 +5863,18 @@ mch_job_status(job_T *job)
# endif
if (wait_pid == -1)
{
+ int waitpid_error = errno;
+ if (waitpid_error == ECHILD && mch_process_running(job->jv_pid))
+ {
+ // The process is alive, but it was reparented (for example by
+ // ptrace called by a debugger like lldb or gdb).
+ // HACK(strager): This assumes that process IDs are not reused.
+ return "run";
+ }
// process must have exited
if (job->jv_status < JOB_ENDED)
ch_log(job->jv_channel, "Job no longer exists: %s",
- strerror(errno));
+ strerror(waitpid_error));
goto return_dead;
}
if (wait_pid == 0)
--
2.33.1
0001-fix-mch_job_status-giving-wrong-answer-for-ptraced-p.patch

strager

unread,
Mar 4, 2023, 5:06:06 AM3/4/23
to vim...@vim.org
I want to debug a child process of Vim using a debugger like lldb. These
debuggers use ptrace. ptrace has the effect of reparenting the process.

This reparenting of Vim's child processes confuses Vim. mch_job_status
calls waitpid to detect whether the child process is alive or not, but
waitpid returns -1 (ECHILD) for these ptrace-reparented processes.

Teach Vim to check if the process is really alive using the kill
syscall with signal 0. If it is alive with another parent, treat the job
as still running.
---
src/os_unix.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)

diff --git src/os_unix.c src/os_unix.c
index 0dfbd4977..0d587d812 100644
--- src/os_unix.c
+++ src/os_unix.c
@@ -5863,10 +5863,18 @@ mch_job_status(job_T *job)
# endif
if (wait_pid == -1)
{
+ int waitpid_error = errno;
+ if (mch_process_running(job->jv_pid))
0001-fix-mch_job_status-giving-wrong-answer-for-ptraced-p.patch

Christian Brabandt

unread,
Mar 4, 2023, 5:18:10 AM3/4/23
to vim...@googlegroups.com, vim...@vim.org

On Sa, 04 Mär 2023, strager wrote:

> (I think my previous email didn't send. I also fixed a bug in my previous patch.)

No, it was just stuck in the moderation queue. I just had to confirm
posting, that's why it came in delayed. Next mails from you should be
automatically allowed however.


Thanks,
Christian
--
Unter den Menschen gibt es viel mehr Kopien als Originale.
-- Pablo Picasso

Bram Moolenaar

unread,
Mar 7, 2023, 10:58:37 AM3/7/23
to vim...@googlegroups.com, strager, vim...@vim.org

[resend, not sure if this got through the first time]
I suppose this is useful.

I wonder if we should check if errno is ECHILD, just to be safe.
Ah, you added that in the second version of the patch.

--
hundred-and-one symptoms of being an internet addict:
220. Your wife asks for sex and you tell her where to find you on IRC.

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///
Reply all
Reply to author
Forward
0 new messages