Skip to content

Commit 6c8ef05

Browse files
committed
Consistently use error_msg instead of fprintf(stderr)
* linux/alpha/get_scno.c: Use error_msg. * linux/arm/get_scno.c: Likewise. * linux/mips/get_scno.c: Likewise. * linux/sh/get_scno.c: Likewise. * linux/x86_64/get_scno.c: Likewise. * exit.c (sys_exit): Likewise. * pathtrace.c (pathtrace_select, pathtrace_match): Likewise. * strace.c (alloctcb, droptcb, detach, startup_attach, test_ptrace_seize, init, cleanup, print_debug_info, maybe_allocate_tcb, startup_tcb, trace): Likewise. * syscall.c (update_personality, trace_syscall_exiting, get_scno): Likewise. * unwind.c (DPRINTF): Likewise. * tests/bexecve.test: Update patterns. * tests/detach-stopped.test: Likewise.
1 parent df38991 commit 6c8ef05

File tree

12 files changed

+66
-89
lines changed

12 files changed

+66
-89
lines changed

exit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
SYS_FUNC(exit)
44
{
55
if (exiting(tcp)) {
6-
fprintf(stderr, "_exit returned!\n");
6+
error_msg("_exit returned!");
77
return -1;
88
}
99
/* special case: we stop tracing this process, finish line now */

linux/alpha/get_scno.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ if (upeek(tcp->pid, REG_R0, &scno) < 0)
1010
if (!SCNO_IN_RANGE(scno)) {
1111
if (alpha_a3 == 0 || alpha_a3 == -1) {
1212
if (debug_flag)
13-
fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
13+
error_msg("stray syscall exit: r0 = %ld", scno);
1414
return 0;
1515
}
1616
}

linux/arm/get_scno.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ if (errno)
1515
if ((unsigned long) scno != 0xef000000) {
1616
/* No, it's OABI */
1717
if ((scno & 0x0ff00000) != 0x0f900000) {
18-
fprintf(stderr, "pid %d unknown syscall trap 0x%08lx\n",
18+
error_msg("pid %d unknown syscall trap 0x%08lx",
1919
tcp->pid, scno);
2020
return -1;
2121
}
@@ -39,8 +39,7 @@ scno = shuffle_scno(scno);
3939
*/
4040
if (arm_regs.ARM_ip && !SCNO_IN_RANGE(scno)) {
4141
if (debug_flag)
42-
fprintf(stderr,
43-
"pid %d stray syscall exit: ARM_ip = %ld, scno = %ld\n",
44-
tcp->pid, arm_regs.ARM_ip, shuffle_scno(scno));
42+
error_msg("pid %d stray syscall exit: ARM_ip = %ld, scno = %ld",
43+
tcp->pid, arm_regs.ARM_ip, shuffle_scno(scno));
4544
return 0;
4645
}

linux/mips/get_scno.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ scno = mips_REG_V0;
33
if (!SCNO_IN_RANGE(scno)) {
44
if (mips_REG_A3 == 0 || mips_REG_A3 == (uint64_t) -1) {
55
if (debug_flag)
6-
fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
6+
error_msg("stray syscall exit: v0 = %ld", scno);
77
return 0;
88
}
99
}

linux/sh/get_scno.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ if (scno < 0) {
1010
our purposes, make strace print what it *should* have been */
1111
long correct_scno = (scno & 0xff);
1212
if (debug_flag)
13-
fprintf(stderr,
14-
"Detected glibc bug: bogus system call"
15-
" number = %ld, correcting to %ld\n",
16-
scno,
17-
correct_scno);
13+
error_msg("Detected glibc bug: bogus system call"
14+
" number = %ld, correcting to %ld",
15+
scno, correct_scno);
1816
scno = correct_scno;
1917
}

linux/x86_64/get_scno.c

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ switch (x86_64_regs.cs) {
5656
currpers = 0;
5757
break;
5858
default:
59-
fprintf(stderr, "Unknown value CS=0x%08X while "
60-
"detecting personality of process "
61-
"PID=%d\n", (int)x86_64_regs.cs, tcp->pid);
59+
error_msg("Unknown value CS=0x%08X while "
60+
"detecting personality of process PID=%d",
61+
(int)x86_64_regs.cs, tcp->pid);
6262
currpers = current_personality;
6363
break;
6464
}
@@ -73,19 +73,17 @@ rip -= 2;
7373
errno = 0;
7474
call = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)rip, (char *)0);
7575
if (errno)
76-
fprintf(stderr, "ptrace_peektext failed: %s\n",
77-
strerror(errno));
76+
perror_msg("ptrace_peektext failed");
7877
switch (call & 0xffff) {
7978
/* x86-64: syscall = 0x0f 0x05 */
8079
case 0x050f: currpers = 0; break;
8180
/* i386: int 0x80 = 0xcd 0x80 */
8281
case 0x80cd: currpers = 1; break;
8382
default:
8483
currpers = current_personality;
85-
fprintf(stderr,
86-
"Unknown syscall opcode (0x%04X) while "
87-
"detecting personality of process "
88-
"PID=%d\n", (int)call, tcp->pid);
84+
error_msg("Unknown syscall opcode (0x%04X) while "
85+
"detecting personality of process PID=%d",
86+
(int)call, tcp->pid);
8987
break;
9088
}
9189
#endif
@@ -96,9 +94,8 @@ switch (call & 0xffff) {
9694
* Stracing of i386 apps is still supported.
9795
*/
9896
if (currpers == 0) {
99-
fprintf(stderr, "syscall_%lu(...) in unsupported "
100-
"64-bit mode of process PID=%d\n",
101-
scno, tcp->pid);
97+
error_msg("syscall_%lu(...) in unsupported "
98+
"64-bit mode of process PID=%d", scno, tcp->pid);
10299
return 0;
103100
}
104101
currpers &= ~2; /* map 2,1 to 0,1 */

pathtrace.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,7 @@ pathtrace_select(const char *path)
141141
return;
142142
}
143143

144-
fprintf(stderr, "Requested path '%s' resolved into '%s'\n",
145-
path, rpath);
144+
error_msg("Requested path '%s' resolved into '%s'", path, rpath);
146145
storepath(rpath);
147146
}
148147

@@ -271,7 +270,7 @@ pathtrace_match(struct tcb *tcp)
271270
if (umoven(tcp, tcp->u_arg[0], sizeof oldargs,
272271
oldargs) < 0)
273272
{
274-
fprintf(stderr, "umoven() failed\n");
273+
error_msg("umoven() failed");
275274
return 0;
276275
}
277276
args = oldargs;
@@ -292,7 +291,7 @@ pathtrace_match(struct tcb *tcp)
292291
if (args[i] == 0)
293292
continue;
294293
if (umoven(tcp, args[i], fdsize, fds) < 0) {
295-
fprintf(stderr, "umoven() failed\n");
294+
error_msg("umoven() failed");
296295
continue;
297296
}
298297
for (j = 0;; j++) {

strace.c

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,8 @@ alloctcb(int pid)
701701

702702
nprocs++;
703703
if (debug_flag)
704-
fprintf(stderr, "new tcb for pid %d, active tcbs:%d\n", tcp->pid, nprocs);
704+
error_msg("new tcb for pid %d, active tcbs:%d",
705+
tcp->pid, nprocs);
705706
return tcp;
706707
}
707708
}
@@ -722,7 +723,8 @@ droptcb(struct tcb *tcp)
722723

723724
nprocs--;
724725
if (debug_flag)
725-
fprintf(stderr, "dropped tcb for pid %d, %d remain\n", tcp->pid, nprocs);
726+
error_msg("dropped tcb for pid %d, %d remain",
727+
tcp->pid, nprocs);
726728

727729
if (tcp->outf) {
728730
if (followfork >= 2) {
@@ -851,8 +853,8 @@ detach(struct tcb *tcp)
851853
}
852854
sig = WSTOPSIG(status);
853855
if (debug_flag)
854-
fprintf(stderr, "detach wait: event:%d sig:%d\n",
855-
(unsigned)status >> 16, sig);
856+
error_msg("detach wait: event:%d sig:%d",
857+
(unsigned)status >> 16, sig);
856858
if (use_seize) {
857859
unsigned event = (unsigned)status >> 16;
858860
if (event == PTRACE_EVENT_STOP /*&& sig == SIGTRAP*/) {
@@ -905,7 +907,7 @@ detach(struct tcb *tcp)
905907

906908
drop:
907909
if (!qflag && (tcp->flags & TCB_ATTACHED))
908-
fprintf(stderr, "Process %u detached\n", tcp->pid);
910+
error_msg("Process %u detached", tcp->pid);
909911

910912
droptcb(tcp);
911913
}
@@ -1007,11 +1009,11 @@ startup_attach(void)
10071009
if (ptrace_attach_or_seize(tid) < 0) {
10081010
++nerr;
10091011
if (debug_flag)
1010-
fprintf(stderr, "attach to pid %d failed\n", tid);
1012+
error_msg("attach to pid %d failed", tid);
10111013
continue;
10121014
}
10131015
if (debug_flag)
1014-
fprintf(stderr, "attach to pid %d succeeded\n", tid);
1016+
error_msg("attach to pid %d succeeded", tid);
10151017
cur_tcp = tcp;
10161018
if (tid != tcp->pid)
10171019
cur_tcp = alloctcb(tid);
@@ -1032,9 +1034,9 @@ startup_attach(void)
10321034
continue;
10331035
}
10341036
if (!qflag) {
1035-
fprintf(stderr, ntid > 1
1036-
? "Process %u attached with %u threads\n"
1037-
: "Process %u attached\n",
1037+
error_msg(ntid > 1
1038+
? "Process %u attached with %u threads"
1039+
: "Process %u attached",
10381040
tcp->pid, ntid);
10391041
}
10401042
if (!(tcp->flags & TCB_ATTACHED)) {
@@ -1055,7 +1057,7 @@ startup_attach(void)
10551057
tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
10561058
newoutf(tcp);
10571059
if (debug_flag)
1058-
fprintf(stderr, "attach to pid %d (main) succeeded\n", tcp->pid);
1060+
error_msg("attach to pid %d (main) succeeded", tcp->pid);
10591061

10601062
if (daemonized_tracer) {
10611063
/*
@@ -1066,9 +1068,7 @@ startup_attach(void)
10661068
}
10671069

10681070
if (!qflag)
1069-
fprintf(stderr,
1070-
"Process %u attached\n",
1071-
tcp->pid);
1071+
error_msg("Process %u attached", tcp->pid);
10721072
} /* for each tcbtab[] */
10731073

10741074
ret:
@@ -1343,7 +1343,7 @@ test_ptrace_seize(void)
13431343
if (ptrace(PTRACE_SEIZE, pid, 0, 0) == 0) {
13441344
post_attach_sigstop = 0; /* this sets use_seize to 1 */
13451345
} else if (debug_flag) {
1346-
fprintf(stderr, "PTRACE_SEIZE doesn't work\n");
1346+
error_msg("PTRACE_SEIZE doesn't work");
13471347
}
13481348

13491349
kill(pid, SIGKILL);
@@ -1651,7 +1651,7 @@ init(int argc, char *argv[])
16511651
PTRACE_O_TRACEFORK |
16521652
PTRACE_O_TRACEVFORK;
16531653
if (debug_flag)
1654-
fprintf(stderr, "ptrace_setoptions = %#x\n", ptrace_setoptions);
1654+
error_msg("ptrace_setoptions = %#x", ptrace_setoptions);
16551655
test_ptrace_seize();
16561656

16571657
/* Check if they want to redirect the output. */
@@ -1782,8 +1782,7 @@ cleanup(void)
17821782
if (!tcp->pid)
17831783
continue;
17841784
if (debug_flag)
1785-
fprintf(stderr,
1786-
"cleanup: looking at pid %u\n", tcp->pid);
1785+
error_msg("cleanup: looking at pid %u", tcp->pid);
17871786
if (tcp->pid == strace_child) {
17881787
kill(tcp->pid, SIGCONT);
17891788
kill(tcp->pid, fatal_sig);
@@ -1844,7 +1843,7 @@ print_debug_info(const int pid, int status)
18441843
e = "STOP";
18451844
sprintf(evbuf, ",EVENT_%s (%u)", e, event);
18461845
}
1847-
fprintf(stderr, " [wait(0x%06x) = %u] %s%s\n", status, pid, buf, evbuf);
1846+
error_msg("[wait(0x%06x) = %u] %s%s", status, pid, buf, evbuf);
18481847
}
18491848

18501849
static struct tcb *
@@ -1869,7 +1868,7 @@ maybe_allocate_tcb(const int pid, int status)
18691868
tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
18701869
newoutf(tcp);
18711870
if (!qflag)
1872-
fprintf(stderr, "Process %d attached\n", pid);
1871+
error_msg("Process %d attached", pid);
18731872
return tcp;
18741873
} else {
18751874
/* This can happen if a clone call used
@@ -1992,15 +1991,14 @@ static void
19921991
startup_tcb(struct tcb *tcp)
19931992
{
19941993
if (debug_flag)
1995-
fprintf(stderr, "pid %d has TCB_STARTUP, initializing it\n",
1996-
tcp->pid);
1994+
error_msg("pid %d has TCB_STARTUP, initializing it", tcp->pid);
19971995

19981996
tcp->flags &= ~TCB_STARTUP;
19991997

20001998
if (!use_seize) {
20011999
if (debug_flag)
2002-
fprintf(stderr, "setting opts 0x%x on pid %d\n",
2003-
ptrace_setoptions, tcp->pid);
2000+
error_msg("setting opts 0x%x on pid %d",
2001+
ptrace_setoptions, tcp->pid);
20042002
if (ptrace(PTRACE_SETOPTIONS, tcp->pid, NULL, ptrace_setoptions) < 0) {
20052003
if (errno != ESRCH) {
20062004
/* Should never happen, really */
@@ -2187,7 +2185,7 @@ trace(void)
21872185
*/
21882186
if (sig == SIGSTOP && (tcp->flags & TCB_IGNORE_ONE_SIGSTOP)) {
21892187
if (debug_flag)
2190-
fprintf(stderr, "ignored SIGSTOP on pid %d\n", tcp->pid);
2188+
error_msg("ignored SIGSTOP on pid %d", tcp->pid);
21912189
tcp->flags &= ~TCB_IGNORE_ONE_SIGSTOP;
21922190
goto restart_tracee_with_sig_0;
21932191
}

syscall.c

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -313,35 +313,23 @@ update_personality(struct tcb *tcp, unsigned int personality)
313313
return;
314314
tcp->currpers = personality;
315315

316-
# if defined(POWERPC64)
317-
if (!qflag) {
318-
static const char *const names[] = {"64 bit", "32 bit"};
319-
fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
320-
tcp->pid, names[personality]);
321-
}
322-
# elif defined(X86_64)
323-
if (!qflag) {
324-
static const char *const names[] = {"64 bit", "32 bit", "x32"};
325-
fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
326-
tcp->pid, names[personality]);
327-
}
328-
# elif defined(X32)
329-
if (!qflag) {
330-
static const char *const names[] = {"x32", "32 bit"};
331-
fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
332-
tcp->pid, names[personality]);
333-
}
334-
# elif defined(AARCH64)
335-
if (!qflag) {
336-
static const char *const names[] = {"32-bit", "AArch64"};
337-
fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
338-
tcp->pid, names[personality]);
339-
}
340-
# elif defined(TILE)
316+
# undef PERSONALITY_NAMES
317+
# if defined POWERPC64
318+
# define PERSONALITY_NAMES {"64 bit", "32 bit"}
319+
# elif defined X86_64
320+
# define PERSONALITY_NAMES {"64 bit", "32 bit", "x32"}
321+
# elif defined X32
322+
# define PERSONALITY_NAMES {"x32", "32 bit"}
323+
# elif defined AARCH64
324+
# define PERSONALITY_NAMES {"32-bit", "AArch64"}
325+
# elif defined TILE
326+
# define PERSONALITY_NAMES {"64-bit", "32-bit"}
327+
# endif
328+
# ifdef PERSONALITY_NAMES
341329
if (!qflag) {
342-
static const char *const names[] = {"64-bit", "32-bit"};
343-
fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
344-
tcp->pid, names[personality]);
330+
static const char *const names[] = PERSONALITY_NAMES;
331+
error_msg("[ Process PID=%d runs in %s mode. ]",
332+
tcp->pid, names[personality]);
345333
}
346334
# endif
347335
}
@@ -1072,8 +1060,7 @@ trace_syscall_exiting(struct tcb *tcp)
10721060
*/
10731061
#endif
10741062
default:
1075-
fprintf(stderr,
1076-
"invalid rval format\n");
1063+
error_msg("invalid rval format");
10771064
break;
10781065
}
10791066
}
@@ -1295,8 +1282,7 @@ get_scno(struct tcb *tcp)
12951282
tcp->s_ent = &unknown;
12961283
tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
12971284
if (debug_flag)
1298-
fprintf(stderr, "pid %d invalid syscall %ld\n",
1299-
tcp->pid, scno);
1285+
error_msg("pid %d invalid syscall %ld", tcp->pid, scno);
13001286
}
13011287
return 1;
13021288
}

tests/bexecve.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ run_strace_redir -enone ./set_ptracer_any false
2020
run_strace_redir -bexecve -enone ./set_ptracer_any false ||
2121
dump_log_and_fail_with "$STRACE $args: unexpected exit status"
2222

23-
pattern_detached='Process [1-9][0-9]* detached'
24-
pattern_personality='\[ Process PID=[1-9][0-9]* runs in .* mode. \]'
23+
pattern_detached='[^:]*strace: Process [1-9][0-9]* detached'
24+
pattern_personality='[^:]*strace: \[ Process PID=[1-9][0-9]* runs in .* mode. \]'
2525

2626
LC_ALL=C grep -x "$pattern_detached" "$LOG" > /dev/null ||
2727
dump_log_and_fail_with "$STRACE $args: output mismatch"

0 commit comments

Comments
 (0)