NetBSD Problem Report #36161

From ad@hairylemon.org  Tue Apr 17 11:26:06 2007
Return-Path: <ad@hairylemon.org>
Received: from mail.netbsd.org (mail.netbsd.org [204.152.190.11])
	by narn.NetBSD.org (Postfix) with ESMTP id 762A963B964
	for <gnats-bugs@gnats.NetBSD.org>; Tue, 17 Apr 2007 11:26:06 +0000 (UTC)
Message-Id: <E1Hdloy-0004Qc-DZ@ns0.hairylemon.org>
Date: Tue, 17 Apr 2007 12:26:04 +0100
From: ad@netbsd.org
Sender: Andrew Doran <ad@hairylemon.org>
Reply-To: ad@netbsd.org
To: gnats-bugs@NetBSD.org
Subject: ptrace(2) & PT_SYSCALL does not stop before executing syscall
X-Send-Pr-Version: 3.95

>Number:         36161
>Category:       kern
>Synopsis:       ptrace(2) & PT_SYSCALL does not stop before executing syscall
>Confidential:   no
>Severity:       serious
>Priority:       high
>Responsible:    ad
>State:          closed
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Tue Apr 17 11:30:01 +0000 2007
>Closed-Date:    Fri Apr 20 21:22:41 +0000 2007
>Last-Modified:  Fri Apr 20 21:22:41 +0000 2007
>Originator:     Andrew Doran
>Release:        NetBSD 4.99.17
>Organization:
The NetBSD Project
>Environment:
N/A
>Description:
From a report by njoly@:

I just noticed that tracing syscalls with ptrace(2) & PT_SYSCALL does
not seems to work as expected ... The debugged process seems only
stopped after executing a syscall, but not before.

I made the attached code to illustrate that problem (seen on -current
i386 and amd64). The same program, on FreeBSD/i386 6.1, show 2 ptrace
calls for each syscall as i expected.

njoly@hal [~]> uname -a
NetBSD hal.sis.pasteur.fr 4.99.17 NetBSD 4.99.17 (HAL) #2: Wed Apr 11 15:01:57 CEST 2007
njoly@hal.sis.pasteur.fr:/local/src/NetBSD/obj/i386/sys/arch/i386/compile/HAL i386

njoly@hal [~]> ./ptrace >/dev/null
syscall 0x0 (0).
syscall 0xbbbea000 (-1145135104).
syscall 0x3 (3).
syscall 0x0 (0).
syscall 0xbbbe9000 (-1145139200).
syscall 0x0 (0).
syscall 0x0 (0).
syscall 0x2 (2).
syscall 0x2 (2).
syscall 0x2 (2).
[...]
njoly@hal [~]> ktrace -di /bin/echo foo
foo
njoly@hal [~]> kdump | grep RET
  7677      1 echo     RET   execve JUSTRETURN
  7677      1 echo     RET   mmap -1145135104/0xbbbea000
  7677      1 echo     RET   open 3
  7677      1 echo     RET   __fstat30 0
  7677      1 echo     RET   mmap -1145139200/0xbbbe9000
  7677      1 echo     RET   close 0
  7677      1 echo     RET   munmap 0
  7677      1 echo     RET   open -1 errno 2 No such file or directory
  7677      1 echo     RET   open -1 errno 2 No such file or directory
  7677      1 echo     RET   open -1 errno 2 No such file or directory
[...]
>How-To-Repeat:
Nicholas provided a test case:

#include <sys/types.h>
#include <sys/ptrace.h>
#include <sys/wait.h>
#include <machine/reg.h>
#include <err.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>

#ifdef __amd64__
#define REG regs[_REG_RAX]
#endif
#ifdef __i386__
#define REG r_eax
#endif

int main() {
  int i, ret;
  pid_t pid;
  struct sigaction sa;
  struct reg regs;

  pid = fork();

  switch (pid) {
  case -1:
    err(1, "fork failed");
  case 0:
    pid = getpid();
    if (ptrace(PT_TRACE_ME, 0, 0, 0) < 0) {
      err(1, "ptrace PT_TRACE_ME failed"); }
    kill(pid, SIGSTOP);
    execl("/bin/echo", "echo", "foo", NULL);
    err(1, "execl failed");
  default:
    if (wait(&ret) != pid) {
      err(1, "wait failed"); }
    if (ptrace(PT_SYSCALL, pid, (char *)1, 0)) {
      err(1, "ptrace PT_SYSCALL failed"); }
    while (1) {
      if (wait(&ret) != pid) {
        err(1, "wait failed"); }

      if (WIFEXITED(ret)) {
        break; }
      if (ptrace(PT_GETREGS, pid, &regs, 0) < 0) {
        err(1, "ptrace PT_GETREGS failed"); }
      fprintf(stderr, "syscall 0x%lx (%ld).\n", regs.REG, regs.REG);
      if (ptrace(PT_SYSCALL, pid, (char *)1, 0) < 0) {
        err(1, "ptrace PT_SYSCALL failed"); }
    }
    break; }

  return 0; }
>Fix:
The issue here is that stopping is now always deferred until the LWP sleeps
interruptably or returns to userspace. That's so that any locks held over a
sleep can be released by the LWP before it comes to a halt.

I think the solution is to add a proc_stop_now() that checks for a request
to stop from the debugger, and makes it happen immediatley. That would be
called from process_stoptrace() in place of the call to mi_switch() that's
there now.

>Release-Note:

>Audit-Trail:
From: Andrew Doran <ad@netbsd.org>
To: gnats-bugs@NetBSD.org
Cc: 
Subject: PR/36161 CVS commit: src/sys/kern
Date: Thu, 19 Apr 2007 22:42:11 +0000 (UTC)

 Module Name:	src
 Committed By:	ad
 Date:		Thu Apr 19 22:42:11 UTC 2007

 Modified Files:
 	src/sys/kern: sys_process.c

 Log Message:
 process_stoptrace: after setting a pending stop on curproc, call issignal
 once to have it do the needful. PR kern/36161.


 To generate a diff of this commit:
 cvs rdiff -r1.124 -r1.125 src/sys/kern/sys_process.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

Responsible-Changed-From-To: kern-bug-people->ad
Responsible-Changed-By: ad@netbsd.org
Responsible-Changed-When: Thu, 19 Apr 2007 22:47:49 +0000
Responsible-Changed-Why:
I committed a fix.


State-Changed-From-To: open->feedback
State-Changed-By: ad@netbsd.org
State-Changed-When: Thu, 19 Apr 2007 22:47:49 +0000
State-Changed-Why:
This problem should be fixed. There is still one other issue with ptrace.


State-Changed-From-To: feedback->closed
State-Changed-By: ad@netbsd.org
State-Changed-When: Fri, 20 Apr 2007 21:22:41 +0000
State-Changed-Why:
Submitter confirms fixed in private e-mail.


>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.51 2026/08/10 02:28:17 riastradh Exp $
$NetBSD: gnats_config.sh,v 1.10 2026/05/13 22:00:09 riastradh Exp $
Copyright © 1994-2026 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.