ref: 34f3df213c0141d904dbe69ff6cf16bc3cfae28c
parent: 059c85dd75f4790625ea3ba19f1c76cf2f3e6b4a
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Wed Jul 29 01:19:24 EDT 2015
ratrace: avoid blank line prints, make writer the parent when there where multiple syscalls returning out of order, it would print blank lines between the exits. avoid this by remembering if the last char written was a newline and conditionally insert newline on out of order return. sometimes, ratrace would return before all messages have been printed. make the writer process the parent so ratrace wont exit until all readers are finished avoiding the problem.
--- a/sys/src/cmd/ratrace.c
+++ b/sys/src/cmd/ratrace.c
@@ -9,7 +9,7 @@
Channel *out;
Channel *quit;
Channel *forkc;
-int nread = 0;
+int readers = 1;
typedef struct Msg Msg;
struct Msg {
@@ -36,8 +36,10 @@
snprint(s->buf, sizeof(s->buf), " = %r\n");
s->pid = r->pid;
sendp(quit, s);
- close(r->tfd);
- close(r->cfd);
+ if(r->tfd >= 0)
+ close(r->tfd);
+ if(r->cfd >= 0)
+ close(r->cfd);
threadexits(nil);
}
@@ -105,14 +107,13 @@
}
void
-writer(void *arg)
+writer(int lastpid)
{
- int lastpid;
+ char lastc = -1;
Alt a[4];
Msg *s;
+ int n;
- lastpid = (int)(uintptr)arg;
-
a[0].op = CHANRCV;
a[0].c = quit;
a[0].v = &s;
@@ -124,20 +125,29 @@
a[2].v = nil;
a[3].op = CHANEND;
- while(nread > 0){
+ while(readers > 0){
switch(alt(a)){
case 0:
- nread--;
+ readers--;
case 1:
if(s->pid != lastpid){
lastpid = s->pid;
- fprint(2, s->buf[1]=='='? "\n%d ...": "\n", lastpid);
+ if(lastc != '\n'){
+ lastc = '\n';
+ write(2, &lastc, 1);
+ }
+ if(s->buf[1] == '=')
+ fprint(2, "%d ...", lastpid);
}
- write(2, s->buf, strlen(s->buf));
+ n = strlen(s->buf);
+ if(n > 0){
+ write(2, s->buf, n);
+ lastc = s->buf[n-1];
+ }
free(s);
break;
case 2:
- nread++;
+ readers++;
break;
}
}
@@ -196,7 +206,8 @@
out = chancreate(sizeof(Msg*), 0);
quit = chancreate(sizeof(Msg*), 0);
forkc = chancreate(sizeof(void*), 0);
- nread++;
- procrfork(writer, (void*)pid, Stacksize, 0);
- reader((void*)pid);
+ procrfork(reader, (void*)pid, Stacksize, 0);
+
+ writer(pid);
+ threadexits(nil);
}