437 Commits

Author SHA1 Message Date
Eugene Syromyatnikov
7dbc3780a6 Add indication of mpers support to strace -V output
* strace.c (print_version): Append information about m32 and mx32
decoding support.
* tests/strace-V.test: Update expected output.
2018-01-11 15:54:33 +00:00
Eugene Syromyatnikov
8cea0b917a Add indication of optional demangle feature strace -V output
* strace.c (print_version) [USE_DEMANGLE]: Append " stack-demangle"
to the features string.
* tests/strace-V.test: Update expected output.
2018-01-11 15:54:33 +00:00
Eugene Syromiatnikov
562abec971 Add tcp argument to clear_regs
* defs.h (clear_regs): Add argument struct tcb *tcp.
* syscall.c (clear_regs): Likewise.
* strace.c (next_event): Pass tcp to clear_regs() call.
2018-01-07 16:46:32 +00:00
Eugene Syromiatnikov
cc057e03ae Include paths.h if it is available
Use the OS definition of _PATH_BSHELL macro if it is available.

* configure.ac (AC_CHECK_HEADERS): Add paths.h.
* strace.c [HAVE_PATHS_H]: Include paths.h.
2018-01-07 16:46:32 +00:00
Eugene Syromiatnikov
6b3f423640 strace.c: avoid duplication of a format string in attach_tcb
* strace.c (attach_tcb): Add task_path variable with the output format.
Use it for the procdir declaration and xsprintf format string argument.
2018-01-07 16:46:32 +00:00
Elvira Khabirova
2e351de149 strace.c: rename set_sigaction to set_sighandler
Since set_sigaction() actually specifies sa_handler and not sa_sigaction,
rename set_sigaction() to set_sighandler() for future definition of
set_sigaction() function as specifying sa_sigaction.

* strace.c (set_sigaction): Rename to set_sighandler.
All callers updated.
2018-01-07 03:11:21 +00:00
026b76444e strace.c: use xsprintf instead of sprintf
* strace.c: Include "xstring.h".
(strerror, newoutf, attach_tcb, print_debug_info): Replace sprintf
with xsprintf.
2018-01-05 12:26:47 +00:00
9cbb505f9f Lift artificial limit on output file names in -ff mode
Starting with commit v4.5.19~88 strace imposed an artificial limit
on output file names in -ff mode, leading to the following absurd
behaviour:

$ strace -ff -o"$(perl -e 'print "/" x 510')/$PWD/log" /bin/true
strace: Can't fopen '////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////.12345': Permission denied

* strace.c (newoutf): Raise buffer size to PATH_MAX.
(init) <followfork >= 2>: Check output file name prefix length.
* tests/options-syntax.test: Check it.
2018-01-04 10:52:07 +00:00
Eugene Syromiatnikov
0b786d6641 Move enum trace_event into a separate header
As it will have users outside strace.c in the future.

* trace_event.h (enum trace_event): Move it from...
* strace.c (enum trace_event): ...here.
(#include "trace_event.h"): New header, for enum trace_event.
* Makefile.am (strace_SOURCES): Add trace_event.h.
2018-01-04 00:16:21 +01:00
Eugene Syromyatnikov
576e2db4d0 Call get_scno during startup_tcb only for forcibly attached processes
Otherwise it makes little sense on most arches to try to get syscall
number.

* defs.h (TCB_GRABBED): New tcb flag.
* strace.c (attach_tcb): Set TCB_GRABBED for the tcb.
(startup_tcb): Call get_scno() only if process is grabbed and its
registers may contain syscall number information.

Reported-by: Dmitry V. Levin <ldv@altlinux.org>
Closes: https://github.com/strace/strace/issues/22
2018-01-03 23:56:48 +01:00
Eugene Syromyatnikov
45fa947494 strace.c: sort getopt switch branches alphabetically
There's no reason to have them out of order. This also (almost) matches
their order in the getopt call argument, introduced in
v4.20-64-g61c03be.

* strace.c (init): Sort branches in the getopt switch routine.
2018-01-01 17:34:57 +01:00
Eugene Syromyatnikov
910364a5dc strace.c: check for (non)zero argc instead of argv[0]
Since we can never be sure that we have NULL behind the last entry of
the argv array.

* strace.c (init): Check for non-zero argc before/instead of argv[0].
2018-01-01 17:25:53 +01:00
Eugene Syromyatnikov
fcf4b6afdc strace.c: inform the user about -F option obsolescence
* strace.c (init): Print an error message if the option is used.

Co-authored-by: Dmitry V. Levin <ldv@altlinux.org>
2017-12-20 22:58:42 +00:00
Eugene Syromyatnikov
61c03be596 strace.c: sort option arguments alphabetically
Except for the ones that enabled by configure options.

While we are here, let's also change the indentation of while expression
continuation in order to separate it from the following switch clause.

* strace.c (init): Reorder options in getopt() call.
2017-12-20 22:58:42 +00:00
Eugene Syromiatnikov
67c4b41ed0 strace.c: use _exit() instead of exit() in child's die()
Let's keep child's death routine as minimalistic as possible for the
cases when vfork() is used instead of fork() on systems that lack MMU.

* strace.c (die) <!(strace_tracer_pid == getpid())>: Call _exit().
2017-12-20 22:58:42 +00:00
Eugene Syromiatnikov
607b4a6560 strace.c: use xgrowarray
* strace.c (tcbtabsize): Change type to size_t.
(expand_tcbtab): Rewrite in order to utilise xgrowarray
instead of manual dynamic array management.
2017-12-14 00:05:04 +00:00
Eugene Syromiatnikov
146444206c strace.c: minor style fixes
* strace.c (test_ptrace_seize): Remove excess braces.
(init): Wrap overly long string literal.  Add braces
to the else if branch.
2017-12-13 00:35:31 +00:00
Eugene Syromiatnikov
a1d9d66142 strace.c: use *_func_* message printing macros where appropriate
In order to provide some additional bits of information in weird error
cases or just replace existing usages of __func__/literal function name.

* strace.c (detach): Use perror_func_msg() instead of perror_msg().
(startup_attach, open_dummy_desc, startup_child, test_ptrace_seize): Use
perror_func_msg_and_die() instead of perror_msg_and_die().
(test_ptrace_seize): Use error_func_msg_and_die() instead of
error_msg_and_die().
2017-12-13 00:35:31 +00:00
Eugene Syromiatnikov
dc4831f242 strace.c: replace if (debug_flag) ... with suitable debug printing macros
* strace.c (alloc_tcb, droptcb, detach, attach_tcb, test_ptrace_seize,
init, startup_tcb): Replace if (debug_flag) error_msg(...) occurrences
with debug_error_msg(...).
(attach_tcb): Replace if (debug_flag) perror_msg(...) with
debug_perror_msg(...).
(cleanup, startup_tcb): Replace if (debug_flag) error_msg(...)
occurrences with debug_func_msg() in attempt to provide additional
information.
2017-12-13 00:35:31 +00:00
5d7623a502 Hide struct number_set implementation details from users
* number_set.h (number_slot_t, struct number_set): Move to number_set.c.
(struct number_set): Add forward declaration.
(read_set, write_set, signal_set): Change prototypes from objects
to pointers.
* filter_qualify.c (read_set, write_set, signal_set): Change definitions
from objects to pointers.
(abbrev_set, inject_set, raw_set, trace_set, verbose_set): Change
definitions from arrays to pointers.
(qualify_read): Initialize read_set before first use.
(qualify_write): Initialize write_set before first use.
(qualify_signals): Initialize signal_set before first use.
(qualify_trace): Initialize trace_set before first use.
(qualify_abbrev): Initialize abbrev_set before first use.
(qualify_verbose): Initialize verbose_set before first use.
(qualify_raw): Initialize raw_set before first use.
(qualify_inject_common): Initialize inject_set before first use.
* strace.c (print_signalled, print_stopped): Update signal_set usage.
* syscall.c (dumpio): Update usage of read_set and write_set.
2017-08-22 21:23:49 +00:00
67c97082ac Move number_set interface to separate files
* number_set.c: New file.
* number_set.h: Likewise.
* Makefile.am (strace_SOURCES): Add them.
* basic_filters.c: Include "number_set.h".
(number_slot_t, struct number_set): Move to number_set.h.
(BITS_PER_SLOT, number_setbit, number_isset, reallocate_number_set,
add_number_to_set, is_number_in_set): Move to number_set.c.
* defs.h (struct number_set): Remove forward declaration.
(read_set, write_set, signal_set, is_number_in_set): Move to number_set.h.
* filter.h (add_number_to_set): Move to number_set.h.
* filter_qualify.c: Include "number_set.h".
(number_slot_t, struct number_set): Remove.
* strace.c: Include "number_set.h".
* syscall.c: Likewise.
2017-08-22 21:23:49 +00:00
Edgar Kaziakhmedov
b4f16886e7 Move err/mem subroutines to separate files
In order to allow usage of utility functions by other binaries
included in the strace package (like the upcoming asinfo utility),
these functions should be moved to separate files.

* error_prints.h: New file.
* xmalloc.h: Likewise.
* defs.h: Include "xmalloc.h" and "error_prints.h".
(error_msg, error_msg_and_die, error_msg_and_help, perror_msg,
perror_msg_and_die): Move to error_prints.h.
(xcalloc, xmalloc, xreallocarray, xstrdup, xstrndup): Move to xmalloc.h.
* strace.c (die): Remove static quialifier to make visible
by error_prints.c.
(error_msg, error_msg_and_die, error_msg_and_help, perror_msg,
perror_msg_and_die, verror_msg): Move ...
* error_prints.c: ... to the new file.
* xmalloc.c: Include "config.h", <stdlib.h>, <string.h>,
"error_prints.h", and "xmalloc.h" instead of "defs.h".
Use int instead of bool.  Fix codestyle.
* Makefile.am (strace_SOURCES): Add error_prints.c, error_prints.h,
and xmalloc.h.

Signed-off-by: Edgar Kaziakhmedov <edgar.kaziakhmedov@virtuozzo.com>
Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
2017-08-07 10:59:22 +00:00
330f4633d5 Simplify handling of unexpected tracees
* strace.c (maybe_allocate_tcb) <WIFSTOPPED(status) && !followfork>:
Remove the dance around possible re-injection of WSTOPSIG(status)
as the only observable stop here is the initial ptrace-stop.
2017-08-06 15:10:56 +00:00
7a35b711df Fix handling of unexpected tracees when PTRACE_SEIZE is not in use
* strace.c (maybe_allocate_tcb) <WIFSTOPPED(status) && !followfork>:
The expected ptrace stop signal in case of !use seize is not
syscall_trap_sig but SIGSTOP.  An idea of using PTRACE_GETSIGINFO to
distinguish signal stops that should be re-injected from other kinds
of stops didn't work out due to kernel implementation peculiarities
of initial ptrace-stop.
2017-08-06 13:27:07 +00:00
255bc6fded Enhance fflush error diagnostics
* strace.c (flush_tcp_output): New function.
(line_ended, droptcb, print_event_exit): Use it to flush tcp->outf.
* tests/fflush.c: New file.
* tests/fflush.test: New test.
* tests/Makefile.am (MISC_TESTS): Add it.
* tests/.gitignore: Add fflush.
* tests/pure_executables.list: Likewise.
2017-08-06 11:26:52 +00:00
Eugene Syromyatnikov
1b93f4032a Improve handling of unexpected tracees
When receiving a ptrace stop of an unexpected child, handle it
in the most transparent way possible:
- detach it instead of PTRACE_CONT'ing;
- send it the signal with which it has been stopped.
This should hopefully help to deal with processes that have been created
with misused CLONE_PTRACE flag set.

* strace.c (maybe_allocate_tcb) <WIFSTOPPED(status) && !followfork>:
Calculate the signal similarly to the way next_event does,
forward it to the unexpected tracee, and detach the tracee.
2017-08-05 21:26:35 +00:00
Eugene Syromyatnikov
66731fe8fe Print enabled optional features in strace version output
In order to provide information to user what optionally built features
are available.

* strace.c (print_version): New variable "features".  Print features string
after non-liability disclaimer (or "(none)" in case it is empty).
(print_version) [USE_LIBUNWIND]: Concatenate "stack-unwind" into features
string.
* tests/strace-V.tests (getoption): New function.
Update check in accordance with updated output.
2017-07-31 18:04:22 +00:00
30861c9321 Do not copy optarg unnecessarily
There is no need to copy the optarg string since it is a pointer
into the original argv array, not into a static area
that might be overwritten.

* strace.c (username, outfname): Add const qualifier.
(init): Do not xstrdup optarg to initialize outfname and username.
2017-07-21 01:30:26 +00:00
373265e699 Fix error diagnostics in case of zero argc
* strace.c (init): Adjust argc along with argv,
check that adjusted argc is sane.
* tests/zeroargc.c: New file.
* tests/.gitignore: Add zeroargc.
* tests/Makefile.am (check_PROGRAMS): Likewise.
* tests/options-syntax.test: Check strace error diagnostics
in case of zero argc.
2017-07-02 10:01:22 +00:00
340b00aaa7 Use program_invocation_name instead of a local progname variable
Emulate program_invocation_name only if it is not provided by libc.

* configure.ac: Check for program_invocation_name variable.
* strace.c (progname): Remove.
[!HAVE_PROGRAM_INVOCATION_NAME] (program_invocation_name): New variable.
(verror_msg, error_msg_and_help): Use it instead of progname.
(init): Initialize program_invocation_name instead of progname.
2017-07-02 00:31:50 +00:00
2698d1d060 Unexport die_out_of_memory
Print more specific error diagnostics than a generic "Out of memory"
when an error happens outside xmalloc.c.

* defs.h (die_out_of_memory): Remove prototype.
* strace.c (strace_popen, init): Call perror_msg_and_die instead
of die_out_of_memory.
* unwind.c (unwind_tcb_init): Likewise.
* xmalloc.c (die_out_of_memory): Add static qualifier.
2017-06-26 22:41:28 +00:00
11621507fb Check -s argument early
* util.c (printstr_ex): Move the check that -s argument
does not exceed -1U / 4 ...
* strace.c (init): ... here.
* tests/options-syntax.test: Check it.
2017-06-26 22:30:19 +00:00
184e94ba05 Fix a few spacing style issues
Reported by kernel's checkpatch.pl script.
2017-06-17 22:54:08 +00:00
3baf7ef953 Fix a few initialization style issues
Do not initialise statics and globals to 0, NULL, or false.

Reported by kernel's checkpatch.pl script.
2017-06-17 20:49:09 +00:00
86bce103a6 Fix function prototypes with unspecified arguments
Change functions declared as taking unspecified number of arguments
of unspecified type to functions that take no arguments.

Reported by kernel's checkpatch.pl script.
2017-06-17 20:49:09 +00:00
a74c084611 Fix a few sizeof style issues
Reported by kernel's checkpatch.pl script.
2017-06-17 18:49:58 +00:00
7f2b636a22 Fix a few misspellings in comments
Reported by kernel's checkpatch.pl script.
2017-06-17 10:40:05 +00:00
Victor Krapivensky
f099124e84 syscall.c: split trace_syscall() into 6 functions
This change removes the trace_syscall function.  Now, the code that uses
syscall.c trace functions is expected to check whether it is a syscall
entry or exit (with entering(tcp)/exiting(tcp)) itself, and then make
an appropriate sequence of function calls.

* defs.h: Update comment on TCB_INSYSCALL.
(trace_syscall): Remove prototype.
(syscall_entering_decode, syscall_entering_trace,
syscall_entering_finish, syscall_exiting_decode, syscall_exiting_trace,
syscall_exiting_finish): New prototypes.
* strace.c (trace_syscall): New static replacement for old trace_syscall.
* syscall.c (trace_syscall): Remove.
(trace_syscall_entering): Split into ...
(syscall_entering_decode, syscall_entering_trace,
syscall_entering_finish): ... new functions.
(trace_syscall_exiting): Split into ...
(syscall_exiting_decode, syscall_exiting_trace,
syscall_exiting_finish): ... new functions.
2017-06-05 19:25:04 +00:00
Victor Krapivensky
0a9d8bfd84 strace.c: move termination code to a separate function
This will be needed for the pull-style API.

* strace.c (terminate): New function.
(main): Use it.
2017-06-05 11:48:47 +00:00
32cff9dc88 strace.c: handle PTRACE_EVENT_EXEC like other events
There is nothing unusual about PTRACE_EVENT_EXEC that would require
a special treatment.

* strace.c (next_event): Move PTRACE_EVENT_EXEC handling to the main
switch statement.
2017-06-02 21:49:02 +00:00
4a1a41cb7e strace.c: remove dead code
As strace does not use WCONTINUED flag, there is no need to handle
WIFCONTINUED status.

* strace.c (print_debug_info) [WIFCONTINUED]: Remove.
(next_event): Remove redundant !WIFSTOPPED check.
2017-06-02 20:08:39 +00:00
Victor Krapivensky
6b4efe9f01 strace.c: refactor trace into two functions
This change decouples waiting for next event from reacting to it.  This
makes the control flow easier to understand, and serves as a preparation
for implementing a pull-style API for LuaJIT.

* strace.c (enum trace_event): New enum.
(trace): Split into ...
(next_event, dispatch_event): ... new functions.
(main): Use them.
2017-06-02 14:20:09 +00:00
556edf91e3 Enable coverage when strace is self terminated by signal
The data collected by -fprofile-arcs during program run is saved
on program exit.  When program is terminated by signal, the data
is not saved.  However, libgcov provides an interface to dump
the data at runtime.

* m4/ax_code_coverage.m4 (AX_CODE_COVERAGE): Add -DENABLE_COVERAGE_GCOV
to CODE_COVERAGE_CPPFLAGS.
* strace.c [ENABLE_COVERAGE_GCOV] (__gcov_flush): New prototype.
(main) [ENABLE_COVERAGE_GCOV]: Call __gcov_flush() before raise()
and sigprocmask() calls that might cause program termination.
2017-05-27 18:58:31 +00:00
47b93f2a7a Fix termination signal forwarding
When traced command is terminated by a blocked signal, unblock
that signal to ensure strace termination with the same signal.

* strace.c (main): Unblock the signal raised for strace termination.
* NEWS: Mention this change.
2017-05-27 17:59:44 +00:00
e97a66faa1 Do not reset SIGCHLD handler in tracees to SIG_DFL
While strace resets SIGCHLD handler to the default action so that
waitpid definitely works without losing track of children, tracees
should not inherit this change.

* strace.c (struct exec_params): Add child_sa field.
(init): When setting SIGCHLD handler to SIG_DFL, save the old handler.
(exec_or_die): Restore SIGCHLD handler if it was different from SIG_DFL
at startup.
* NEWS: Mention this change.
2017-05-27 17:59:44 +00:00
e0fd7e7035 Refactor setting of signal handlers to reduce redundancy
* strace.c (set_sigaction): New function.
(init): Use it instead of sigaction with sigaddset.
2017-05-27 17:59:44 +00:00
f83b49b03a Fix unblocking of signals in interactive mode
In interactive mode (-I2), those signals that were blocked at startup
will remain blocked for the whole period of strace execution.

* strace.c (empty_set): Rename to start_set.  All users changed.
(init): Initialize start_set and blocked_set to the set of currently
blocked signals.
(startup_attach, trace): Use SIG_SETMASK instead of SIG_BLOCK.
* NEWS: Mention this change.
2017-05-27 10:00:43 +00:00
ab2efa7a54 Cleanup interactive check
* strace.c (init): Check for INTR_WHILE_WAIT mode the same way
as in other places.
2017-05-26 23:16:57 +00:00
Eugene Syromyatnikov
d86b6bc773 Unify capitalisation of (c) in copyright notices 2017-05-26 19:43:11 +00:00
Eugene Syromyatnikov
9d095c7860 Add copyright headers 2017-05-22 17:33:51 +00:00