Use [p]error_msg[_and_die] where appropriate. No logic changes.
Resulting size changes:
text data bss dec hex filename
17445 16 8572 26033 65b1 strace.o.old
16850 16 8572 25438 635e strace.o
* strace.c: Replace fprintf[+cleanup]+exit with [p]error_msg_and_die,
fprintf("progname: ...") with [p]error_msg where appropriate.
Signed-off-by: Denys Vlasenko <dvlasenk@redhat.com>
diff --git a/strace.c b/strace.c
index 470bc08..ae8fb6c 100644
--- a/strace.c
+++ b/strace.c
@@ -173,9 +173,7 @@
#endif /* USE_PROCFS */
static void
-usage(ofp, exitval)
-FILE *ofp;
-int exitval;
+usage(FILE *ofp, int exitval)
{
fprintf(ofp, "\
usage: strace [-CdDffhiqrtttTvVxxy] [-a column] [-e expr] ... [-o file]\n\
@@ -318,9 +316,7 @@
int euid = geteuid(), uid = getuid();
if (euid != uid && setreuid(euid, uid) < 0) {
- fprintf(stderr, "%s: setreuid: %s\n",
- progname, strerror(errno));
- exit(1);
+ perror_msg_and_die("setreuid");
}
#endif
}
@@ -551,8 +547,7 @@
if (strchr(filename, '/')) {
if (strlen(filename) > sizeof pathname - 1) {
errno = ENAMETOOLONG;
- perror("strace: exec");
- exit(1);
+ perror_msg_and_die("exec");
}
strcpy(pathname, filename);
}
@@ -600,15 +595,11 @@
}
}
if (stat(pathname, &statbuf) < 0) {
- fprintf(stderr, "%s: %s: command not found\n",
- progname, filename);
- exit(1);
+ perror_msg_and_die("Can't stat '%s'", filename);
}
strace_child = pid = fork();
if (pid < 0) {
- perror("strace: fork");
- cleanup();
- exit(1);
+ perror_msg_and_die("fork");
}
if ((pid != 0 && daemonized_tracer) /* -D: parent to become a traced process */
|| (pid == 0 && !daemonized_tracer) /* not -D: child to become a traced process */
@@ -634,8 +625,7 @@
if (!daemonized_tracer) {
if (ptrace(PTRACE_TRACEME, 0, (char *) 1, 0) < 0) {
- perror("strace: ptrace(PTRACE_TRACEME, ...)");
- exit(1);
+ perror_msg_and_die("ptrace(PTRACE_TRACEME, ...)");
}
if (debug)
kill(pid, SIGSTOP);
@@ -656,16 +646,13 @@
*/
if (username != NULL) {
if (initgroups(username, run_gid) < 0) {
- perror("initgroups");
- exit(1);
+ perror_msg_and_die("initgroups");
}
if (setregid(run_gid, run_egid) < 0) {
- perror("setregid");
- exit(1);
+ perror_msg_and_die("setregid");
}
if (setreuid(run_uid, run_euid) < 0) {
- perror("setreuid");
- exit(1);
+ perror_msg_and_die("setreuid");
}
}
}
@@ -702,8 +689,7 @@
#endif /* !USE_PROCFS */
execv(pathname, argv);
- perror("strace: exec");
- _exit(1);
+ perror_msg_and_die("exec");
}
/* We are the tracer. */
@@ -717,9 +703,7 @@
}
#ifdef USE_PROCFS
if (proc_open(tcp, 0) < 0) {
- fprintf(stderr, "trouble opening proc file\n");
- cleanup();
- exit(1);
+ perror_msg_and_die("trouble opening proc file");
}
#endif /* USE_PROCFS */
}
@@ -891,22 +875,18 @@
int optF = 0;
struct sigaction sa;
- static char buf[BUFSIZ];
-
progname = argv[0] ? argv[0] : "strace";
strace_tracer_pid = getpid();
/* Allocate the initial tcbtab. */
tcbtabsize = argc; /* Surely enough for all -p args. */
- if ((tcbtab = calloc(tcbtabsize, sizeof tcbtab[0])) == NULL) {
- fprintf(stderr, "%s: out of memory\n", progname);
- exit(1);
- }
- if ((tcbtab[0] = calloc(tcbtabsize, sizeof tcbtab[0][0])) == NULL) {
- fprintf(stderr, "%s: out of memory\n", progname);
- exit(1);
- }
+ tcbtab = calloc(tcbtabsize, sizeof tcbtab[0]);
+ if (tcbtab == NULL)
+ error_msg_and_die("Out of memory");
+ tcbtab[0] = calloc(tcbtabsize, sizeof tcbtab[0][0]);
+ if (tcbtab[0] == NULL)
+ error_msg_and_die("Out of memory");
for (tcp = tcbtab[0]; tcp < &tcbtab[0][tcbtabsize]; ++tcp)
tcbtab[tcp - tcbtab[0]] = &tcbtab[0][tcp - tcbtab[0]];
@@ -927,17 +907,13 @@
switch (c) {
case 'c':
if (cflag == CFLAG_BOTH) {
- fprintf(stderr, "%s: -c and -C are mutually exclusive options\n",
- progname);
- exit(1);
+ error_msg_and_die("-c and -C are mutually exclusive options");
}
cflag = CFLAG_ONLY_STATS;
break;
case 'C':
if (cflag == CFLAG_ONLY_STATS) {
- fprintf(stderr, "%s: -c and -C are mutually exclusive options\n",
- progname);
- exit(1);
+ error_msg_and_die("-c and -C are mutually exclusive options");
}
cflag = CFLAG_BOTH;
break;
@@ -1004,12 +980,11 @@
break;
case 'p':
if ((pid = atoi(optarg)) <= 0) {
- fprintf(stderr, "%s: Invalid process id: %s\n",
- progname, optarg);
+ error_msg("Invalid process id: '%s'", optarg);
break;
}
if (pid == strace_tracer_pid) {
- fprintf(stderr, "%s: I'm sorry, I can't let you do that, Dave.\n", progname);
+ error_msg("I'm sorry, I can't let you do that, Dave.");
break;
}
tcp = alloc_tcb(pid, 0);
@@ -1019,17 +994,13 @@
case 'P':
tracing_paths = 1;
if (pathtrace_select(optarg)) {
- fprintf(stderr, "%s : failed to select path '%s'\n", progname, optarg);
- exit(1);
+ error_msg_and_die("Failed to select path '%s'", optarg);
}
break;
case 's':
max_strlen = atoi(optarg);
if (max_strlen < 0) {
- fprintf(stderr,
- "%s: invalid -s argument: %s\n",
- progname, optarg);
- exit(1);
+ error_msg_and_die("Invalid -s argument: '%s'", optarg);
}
break;
case 'S':
@@ -1040,9 +1011,7 @@
break;
case 'E':
if (putenv(optarg) < 0) {
- fprintf(stderr, "%s: out of memory\n",
- progname);
- exit(1);
+ error_msg_and_die("Out of memory");
}
break;
default:
@@ -1055,20 +1024,14 @@
usage(stderr, 1);
if (pflag_seen && daemonized_tracer) {
- fprintf(stderr,
- "%s: -D and -p are mutually exclusive options\n",
- progname);
- exit(1);
+ error_msg_and_die("-D and -p are mutually exclusive options");
}
if (!followfork)
followfork = optF;
if (followfork > 1 && cflag) {
- fprintf(stderr,
- "%s: (-c or -C) and -ff are mutually exclusive options\n",
- progname);
- exit(1);
+ error_msg_and_die("(-c or -C) and -ff are mutually exclusive options");
}
/* See if they want to run as another user. */
@@ -1076,15 +1039,10 @@
struct passwd *pent;
if (getuid() != 0 || geteuid() != 0) {
- fprintf(stderr,
- "%s: you must be root to use the -u option\n",
- progname);
- exit(1);
+ error_msg_and_die("You must be root to use the -u option");
}
if ((pent = getpwnam(username)) == NULL) {
- fprintf(stderr, "%s: cannot find user `%s'\n",
- progname, username);
- exit(1);
+ error_msg_and_die("Cannot find user '%s'", username);
}
run_uid = pent->pw_uid;
run_gid = pent->pw_gid;
@@ -1125,8 +1083,10 @@
outf = strace_fopen(outfname);
}
- if (!outfname || outfname[0] == '|' || outfname[0] == '!')
+ if (!outfname || outfname[0] == '|' || outfname[0] == '!') {
+ static char buf[BUFSIZ];
setvbuf(outf, buf, _IOLBF, BUFSIZ);
+ }
if (outfname && optind < argc) {
interactive = 0;
qflag = 1;
@@ -1326,7 +1286,7 @@
return -1;
}
if (tcp->status.PR_FLAGS & PR_ASLEEP)
- break;
+ break;
}
}
#ifndef FREEBSD
@@ -1877,8 +1837,7 @@
free(pollv);
pollv = (struct pollfd *) malloc(nprocs * sizeof pollv[0]);
if (pollv == NULL) {
- fprintf(stderr, "%s: out of memory\n", progname);
- exit(1);
+ error_msg_and_die("Out of memory");
}
for (i = j = 0; i < tcbtabsize; i++) {
@@ -1890,8 +1849,7 @@
j++;
}
if (j != nprocs) {
- fprintf(stderr, "strace: proc miscount\n");
- exit(1);
+ error_msg_and_die("proc miscount");
}
}
@@ -1903,8 +1861,7 @@
int i;
if (pipe(proc_poll_pipe) < 0) {
- perror("pipe");
- exit(1);
+ perror_msg_and_die("pipe");
}
for (i = 0; i < 2; i++) {
set_cloexec_flag(proc_poll_pipe[i]);
@@ -1921,8 +1878,7 @@
if ((n = read(proc_poll_pipe[0], &pollinfo, sizeof(pollinfo))) < 0)
return n;
if (n != sizeof(struct proc_pollfd)) {
- fprintf(stderr, "panic: short read: %d\n", n);
- exit(1);
+ error_msg_and_die("panic: short read: %d", n);
}
for (i = 0; i < nprocs; i++) {
if (pollv[i].fd == pollinfo.fd)
@@ -1954,8 +1910,7 @@
switch (fork()) {
case -1:
- perror("fork");
- _exit(1);
+ perror_msg_and_die("fork");
case 0:
break;
default:
@@ -1978,8 +1933,7 @@
sigemptyset(&empty_set);
if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
- perror("getrlimit(RLIMIT_NOFILE, ...)");
- _exit(1);
+ perror_msg_and_die("getrlimit(RLIMIT_NOFILE, ...)");
}
n = rl.rlim_cur;
for (i = 0; i < n; i++) {
@@ -2045,8 +1999,7 @@
if (pollv[j].revents & (POLLHUP | POLLERR)) {
tcp = pfd2tcb(pollv[j].fd);
if (!tcp) {
- fprintf(stderr, "strace: lost proc\n");
- exit(1);
+ error_msg_and_die("lost proc");
}
droptcb(tcp);
return -1;
@@ -2056,8 +2009,7 @@
return pollv[j].fd;
}
}
- fprintf(stderr, "strace: nothing ready\n");
- exit(1);
+ error_msg_and_die("nothing ready");
}
static int
@@ -2139,8 +2091,7 @@
/* Look up `pfd' in our table. */
if ((tcp = pfd2tcb(pfd)) == NULL) {
- fprintf(stderr, "unknown pfd: %u\n", pfd);
- exit(1);
+ error_msg_and_die("unknown pfd: %u", pfd);
}
#ifdef POLL_HACK
FOUND:
@@ -2187,8 +2138,7 @@
droptcb(tcp);
continue;
default:
- perror("PIOCWSTOP");
- exit(1);
+ perror_msg_and_die("PIOCWSTOP");
}
}
@@ -2234,8 +2184,7 @@
if (tcp->status.PR_FLAGS & PR_ASLEEP) {
tcp->status.PR_WHY = PR_SYSENTRY;
if (trace_syscall(tcp) < 0) {
- fprintf(stderr, "syscall trouble\n");
- exit(1);
+ error_msg_and_die("syscall trouble");
}
}
break;
@@ -2246,8 +2195,7 @@
#endif
case PR_SYSEXIT:
if (trace_syscall(tcp) < 0) {
- fprintf(stderr, "syscall trouble\n");
- exit(1);
+ error_msg_and_die("syscall trouble");
}
break;
case PR_SIGNALLED:
@@ -2280,8 +2228,7 @@
continue;
#endif
default:
- fprintf(stderr, "odd stop %d\n", tcp->status.PR_WHY);
- exit(1);
+ error_msg_and_die("odd stop %d", tcp->status.PR_WHY);
break;
}
/* Remember current print column before continuing. */
@@ -2293,8 +2240,7 @@
if (IOCTL(tcp->pfd, PIOCRUN, 0) < 0)
#endif
{
- perror("PIOCRUN");
- exit(1);
+ perror_msg_and_die("PIOCRUN");
}
}
return 0;
@@ -2369,11 +2315,7 @@
if (do_ptrace(PTRACE_GETEVENTMSG, tcp, NULL, &childpid) < 0) {
if (errno != ESRCH) {
- fprintf(stderr, "\
-%s: handle_ptrace_event: ptrace cannot get new child's pid\n",
- progname);
- cleanup();
- exit(1);
+ error_msg_and_die("Cannot get new child's pid");
}
return -1;
}
@@ -2424,8 +2366,7 @@
pid = wait4(-1, &status, __WCLONE,
cflag ? &ru : NULL);
if (pid == -1) {
- fprintf(stderr, "strace: clone wait4 "
- "failed: %s\n", strerror(errno));
+ perror_msg("wait4(__WCLONE) failed");
}
}
#else
@@ -2490,10 +2431,9 @@
CLONE_PTRACE itself. */
#endif
{
- fprintf(stderr, "unknown pid: %u\n", pid);
if (WIFSTOPPED(status))
ptrace(PTRACE_CONT, pid, (char *) 1, 0);
- exit(1);
+ error_msg_and_die("Unknown pid: %u", pid);
}
}
/* set current output file */