cmds: sys/ctrace -o file; fix rfork handling

Added a -o option to sys/ctrace (if provided, must be the first option)
to redirect the output to a file.
This commit is contained in:
Giacomo Tesio 2017-11-17 01:00:06 +01:00
parent fd30599a90
commit 5a2f74a22c
1 changed files with 30 additions and 9 deletions

View File

@ -10,6 +10,7 @@ Channel *out;
Channel *quit; Channel *quit;
Channel *forkc; Channel *forkc;
int readers = 1; int readers = 1;
int output;
typedef struct Msg Msg; typedef struct Msg Msg;
struct Msg { struct Msg {
@ -80,9 +81,11 @@ reader(void *v)
&& a[5][0] != '-' && a[5][0] != '-'
&& strcmp(a[2], "rfork") == 0) { && strcmp(a[2], "rfork") == 0) {
newpid = strtol(a[5], 0, 0); newpid = strtol(a[5], 0, 0);
if(newpid){
sendp(forkc, (void*)(uintptr_t)newpid); sendp(forkc, (void*)(uintptr_t)newpid);
procrfork(reader, (void*)(uintptr_t)newpid, Stacksize, 0); procrfork(reader, (void*)(uintptr_t)newpid, Stacksize, 0);
} }
}
free(rf); free(rf);
} }
@ -123,14 +126,14 @@ writer(int lastpid)
lastpid = s->pid; lastpid = s->pid;
if(lastc != '\n'){ if(lastc != '\n'){
lastc = '\n'; lastc = '\n';
write(2, &lastc, 1); write(output, &lastc, 1);
} }
if(s->buf[1] == '=') if(s->buf[1] == '=')
fprint(2, "%d ...", lastpid); fprint(output, "%d ...", lastpid);
} }
n = strlen(s->buf); n = strlen(s->buf);
if(n > 0){ if(n > 0){
write(2, s->buf, n); write(output, s->buf, n);
lastc = s->buf[n-1]; lastc = s->buf[n-1];
} }
free(s); free(s);
@ -145,7 +148,7 @@ writer(int lastpid)
void void
usage(void) usage(void)
{ {
fprint(2, "Usage: sys/ctrace [pid] | [-c cmd [arg...]]\n"); fprint(2, "Usage: sys/ctrace [-o file] [pid] | [-c cmd [arg...]]\n");
threadexits("usage"); threadexits("usage");
} }
@ -158,12 +161,13 @@ threadmain(int argc, char **argv)
/* /*
* don't bother with fancy arg processing, because it picks up options * don't bother with fancy arg processing, because it picks up options
* for the command you are starting. Just check for -c as argv[1] * for the command you are starting.
* and then take it from there. * Just check for -o and -c as initial arguments and then take it from there.
*/ */
if (argc < 2) if (argc < 2)
usage(); usage();
if (argv[1][0] == '-') ParseArguments:
if (argv[1][0] == '-'){
switch(argv[1][1]) { switch(argv[1][1]) {
case 'c': case 'c':
if (argc < 3) if (argc < 3)
@ -171,9 +175,26 @@ threadmain(int argc, char **argv)
cmd = strdup(argv[2]); cmd = strdup(argv[2]);
args = &argv[2]; args = &argv[2];
break; break;
case 'o':
if (argc < 4) // sys/ctrace -o file pid
usage();
if (output) // -o can only appear once
usage();
output = ocreate(argv[2], OWRITE|OCEXEC, 0660);
if(output < 0){
fprint(2, "sys/ctrace: cannot open %d: %r\n", argv[2]);
threadexits("output");
}
argc -= 2;
argv += 2;
goto ParseArguments;
default: default:
usage(); usage();
} }
}
if(!output)
output = 2;
/* run a command? */ /* run a command? */
if(cmd) { if(cmd) {