Hi,

I tried to add code which prints a note whenever ptrace fails,
something like "<ptrace(OP):err>".

Looks like "whenever" part of this plan is not easy:
in my testing, I was always striking a case when the resulting
log is messed up in some way...

I would like to commit a limited version of this patch,
which reports only *some* ptrace errors, namely,
errors on ptrace restart operations.

While at it, patch removes tcp->ptrace_errno. I added it many months ago,
and it looks that after all it is not needed for ptrace error detection:
I failed to execute a single existing code path which is accessible
through that variable only.

I used test/sigkill_rain for testing. This patch seems
to generate non-messed-up logs. Basically,
sometimes instead of line like this:

10533 sendto(-1, 0x804895e, 17, 0, NULL, 0 <unfinished ...>

strace will emit line like this:

10533 sendto(-1, 0x804895e, 17, 0, NULL, 0 <ptrace(SYSCALL):No such process>

which tells user that strace failed to let sendto syscall
to be entered - process was dead at that point of time.

I think it is (marginally) better than what we had before.

I observed no other changes in logs while testing.

Please review.
--
vda
diff -d -urpN strace.3/defs.h strace.4/defs.h
--- strace.3/defs.h	2012-03-20 16:17:51.736530532 +0100
+++ strace.4/defs.h	2012-03-20 16:14:02.823912072 +0100
@@ -316,7 +316,6 @@ struct tcb {
 	long long ext_arg[MAX_ARGS];	/* System call arguments */
 #endif
 	long u_rval;		/* (first) return value */
-	int ptrace_errno;
 #if SUPPORTED_PERSONALITIES > 1
 	int currpers;		/* Personality at the time of scno update */
 #endif
diff -d -urpN strace.3/strace.c strace.4/strace.c
--- strace.3/strace.c	2012-03-20 16:18:31.223715442 +0100
+++ strace.4/strace.c	2012-03-20 16:22:49.446588509 +0100
@@ -352,10 +352,9 @@ ptrace_restart(int op, struct tcb *tcp,
 	errno = 0;
 	ptrace(op, tcp->pid, (void *) 0, (long) sig);
 	err = errno;
-	if (!err || err == ESRCH)
+	if (!err)
 		return 0;
 
-	tcp->ptrace_errno = err;
 	msg = "SYSCALL";
 	if (op == PTRACE_CONT)
 		msg = "CONT";
@@ -365,6 +364,22 @@ ptrace_restart(int op, struct tcb *tcp,
 	if (op == PTRACE_LISTEN)
 		msg = "LISTEN";
 #endif
+	/*
+	 * Why curcol != 0? Otherwise sometimes we get this:
+	 *
+	 * 10252 kill(10253, SIGKILL)              = 0
+	 *  <ptrace(SYSCALL,10252):No such process>10253 ...next decode...
+	 *
+	 * 10252 died after we retrieved syscall exit data,
+	 * but before we tried to restart it. Log looks ugly.
+	 */
+	if (curcol != 0) {
+		tprintf(" <ptrace(%s):%s>\n", msg, strerror(err));
+		line_ended();
+	}
+	if (err == ESRCH)
+		return 0;
+	errno = err;
 	perror_msg("ptrace(PTRACE_%s,pid:%d,sig:%d)", msg, tcp->pid, sig);
 	return -1;
 }
@@ -537,15 +552,6 @@ printleader(struct tcb *tcp)
 	if (printing_tcp) {
 		outf = printing_tcp->outf;
 		curcol = printing_tcp->curcol;
-		if (printing_tcp->ptrace_errno) {
-			if (printing_tcp->flags & TCB_INSYSCALL) {
-				tprints(" <unavailable>) ");
-				tabto();
-			}
-			tprints("= ? <unavailable>\n");
-			printing_tcp->ptrace_errno = 0;
-			printing_tcp->curcol = 0;
-		}
 		if (printing_tcp->curcol != 0 && (followfork < 2 || printing_tcp == tcp)) {
 			/*
 			 * case 1: we have a shared log (i.e. not -ff), and last line
@@ -2164,10 +2171,10 @@ trace(void)
 		 * (Or it still can be that pesky post-execve SIGTRAP!)
 		 * Handle it.
 		 */
-		if (trace_syscall(tcp) < 0 && !tcp->ptrace_errno) {
-			/* ptrace() failed in trace_syscall() with ESRCH.
+		if (trace_syscall(tcp) < 0) {
+			/* ptrace() failed in trace_syscall().
 			 * Likely a result of process disappearing mid-flight.
-			 * Observed case: exit_group() terminating
+			 * Observed case: exit_group() or SIGKILL terminating
 			 * all processes in thread group.
 			 * We assume that ptrace error was caused by process death.
 			 * We used to detach(tcp) here, but since we no longer
@@ -2182,11 +2189,12 @@ trace(void)
 		sig = 0;
  restart_tracee:
 		/* Remember current print column before continuing. */
-		tcp->curcol = curcol;
 		if (ptrace_restart(PTRACE_SYSCALL, tcp, sig) < 0) {
+			tcp->curcol = curcol;
 			cleanup();
 			return -1;
 		}
+		tcp->curcol = curcol;
 	}
 	return 0;
 }
------------------------------------------------------------------------------
This SF email is sponsosred by:
Try Windows Azure free for 90 days Click Here 
http://p.sf.net/sfu/sfd2d-msazure
_______________________________________________
Strace-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/strace-devel

Reply via email to