Commit Graph

418 Commits

Author SHA1 Message Date
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
c34952709d Introduce tprintf_comment and tprints_comment functions
* defs.h (tprintf_comment, tprints_comment): New prototypes.
* strace.c (tvprintf): New function.
(tprintf): Use it.
(tprintf_comment, tprints_comment): New functions.
* aio.c (tprint_lio_opcode): Use tprints_comment.
* dm.c (dm_decode_dm_target_spec, dm_decode_dm_target_deps,
dm_decode_dm_name_list, dm_decode_dm_target_versions,
dm_decode_dm_target_msg, dm_decode_string, dm_known_ioctl): Likewise.
* futex.c (SYS_FUNC(futex)): Likewise.
* perf.c (print_perf_event_attr): Likewise.
* seccomp.c (decode_bpf_code): Likewise.
* util.c (printxvals, printxval_searchn, printflags64): Likewise.
* btrfs.c (print_u64, btrfs_print_key_type, btrfs_print_objectid,
print_key_value_internal): Likewise.
(btrfs_ioctl): Use tprints_comment and tprintf_comment.
* dirent.c (SYS_FUNC(getdents)): Likewise.
* dirent64.c (SYS_FUNC(getdents64)): Likewise.
* execve.c (printargc): Use tprintf_comment.
* tests/btrfs.c (btrfs_test_get_dev_stats_ioctl,
btrfs_test_features_ioctls): Update expected output.
2017-04-24 19:31:54 +00:00
Eugene Syromyatnikov
cd838da878 Derive copyright year from the git commit date
This solves problems like the need to update test suite on every
New Year's Eve.

* Makefile.am (dist-hook): Generate .year.
* copyright-year-gen: New file.
* configure.ac (copyright_year): New m4 variable, defined as the output
of copyright-year-gen script.
(AC_COPYRIGHT): Use it.
(COPYRIGHT_YEAR): New output variable and preprocessor macro.
* strace.c (print_version): Use COPYRIGHT_YEAR.
* strace.spec.in (%prep): Save the value of COPYRIGHT_YEAR autoconf
variable to .year file.
* tests/strace-V.test (config_year): New variable, derived from config.h.
Add sanity checks for $config_year and use it in expected output.

Reported-by: Andreas Schwab <schwab@suse.de>
2017-04-19 08:29:46 +00:00
cb46fea4d5 Delay get_regs invocation
strace used to call get_regs on every stop of a tracee, including cases
when the result is going to be discarded.  Prepare to change this lavish
practice by delaying get_regs invocation until its result is actually
needed.

* defs.h (get_regs): Remove.
* strace.c (trace): Do not call get_regs.
* syscall.c (get_regs): Add static qualifier.
(trace_syscall_exiting, print_pc, get_scno): Call get_regs.
2017-04-11 04:04:37 +00:00
8e3361d39c startup_tcb: move get_scno invocation to a more convenient place
* strace.c (trace): Move get_scno invocation ...
(startup_tcb): ... here.  This is a no-op change that slightly improves
readability.
2017-04-11 04:04:37 +00:00
13ffa22088 get_regs: guard against subsequent invocations
At this moment there is only one call to get_regs,
but there is going to be more of them, so a guard is needed.

This change also adds a clear_regs call before get_regs,
which essentially reverts commit v4.9-288-ge9bfff6.

* strace.c (trace): Call clear_regs before get_regs.
* syscall.c (get_regs): Skip if get_regs_error != -1.
2017-04-11 04:04:37 +00:00
d5152aae15 Prepare for introduction of -e inject= option
As -e fault= injection syntax has been extended to configure various
kinds of injections besides fault injection, the original option name
bacame too narrow.

This change renames internal constants, types, and functions from
"fault" to more generic inject/tamper ones.

* defs.h (fault_opts): Rename to inject_opts.
(FAULT_OPTS_RVAL_DEFAULT): Rename to INJECT_OPTS_RVAL_DEFAULT.
(FAULT_OPTS_RVAL_DISABLE): Rename to INJECT_OPTS_RVAL_DISABLE.
(struct tcb): Rename fault_vec field to inject_vec.
(TCB_FAULT_INJ): Rename to TCB_TAMPERED.
(QUAL_FAULT): Rename to QUAL_INJECT.
(fault_vec): Rename to inject_vec.
All users changed.
* qualify.c (fault_set): Rename to inject_set.
(parse_fault_token): Rename to parse_inject_token.
(parse_fault_expression): Rename to parse_inject_expression.
All callers changed.
* syscall.c (fault_vec): Rename to inject_vec.
(tcb_fault_opts): Rename to tcb_inject_opts.
(inject_syscall_fault_entering): Rename to tamper_with_syscall_entering.
(update_syscall_fault_exiting): Rename to tamper_with_syscall_exiting.
(syscall_fault_injected): Rename to syscall_tampered.
All callers changed.
2017-02-08 09:28:07 +00:00
JingPiao Chen
373220061a strace: move description of -k option to the appropriate place
Follow the example of strace.1 and put the description of -k option
into the  output format group.

* strace.c (usage): Move description of -k option to the output
format group.
2017-01-12 22:46:28 +00:00
600457fa9d Print hexadecimal integer in error diagnostics using %#x
The formerly used format string %x led to misleading output as there was
no clear indication sometimes whether the printed integer was decimal or
hexadecimal.

* aio.c (startup_child, test_ptrace_seize): Print status
using %#x format specifier instead of %x.
2017-01-07 16:44:44 +00:00
17a2ba8415 Drop vsprintf.c
Drop custom vfprintf implementation that was disabled by default.
Apparently nobody tests strace with this vfprintf enabled, otherwise
multiple uses of format specifiers not supported by this custom vfprintf
would not left unnoticed.

The GNU C library is not the only libc available, so those who want
faster implementations of libc functions are encouraged to try building
strace with other libc implementations.

* vsprintf.c: Remove.
* Makefile.am (strace_SOURCES): Remove vsprintf.c.
* defs.h (USE_CUSTOM_PRINTF, strace_vfprintf): Remove.
* strace.c (tprintf): Replace strace_vfprintf with vfprintf.
2017-01-04 13:22:19 +00:00
b58f75c5d4 Remove checks of __NR_* availability from strace source code
* strace.c: Remove __NR_tkill checks, assume it is always defined.
* util.c: Remove __NR_process_vm_readv checks and fallback definitions,
assume it is always defined.
2017-01-03 13:46:26 +00:00
2dd365f912 Include "scno.h" in files that check __NR_* constants
* strace.c: Include "scno.h".
* util.c: Likewise.
2017-01-03 11:27:35 +00:00
9eeb033ea6 Update all publicly shown copyright year number ranges
* COPYING: Update copyright year number range.
* configure.ac: Likewise.
* strace.c: Likewise.
* tests/strace-V.test: Use the current year for the check.
2017-01-01 11:33:40 +00:00
326b98927a defs.h: stop including <asm/unistd.h>
Limit the number of files where <asm/unistd.h> is included
to only those few that need it.

* defs.h: Do not include <asm/unistd.h>.
* clone.c: Include <asm/unistd.h>.
* strace.c: Likewise.
* syscall.c: Likewise.
* util.c: Likewise.
2016-12-31 22:12:08 +00:00
232d8c6c12 Update the publicly shown copyright information
Set the publicly shown copyright holder to "The strace developers.".

* COPYING: Update copyright information.
* configure.ac: Add AC_COPYRIGHT.
* strace.c (print_version): New function.
(init): Use it.
* tests/strace-V.test: Update expected output.
2016-12-28 18:33:46 +00:00
Seraphime Kirkovski
a864a32a76 Implement signal injection
This extends the fault injection capability with :signal=SIG option
which injects a signal on entering each syscall from the specified set.

:signal and :error options are complementary, if they are both specified
the syscall will be fault injected as usual and the specified signal
will be delivered to the tracee.

* defs.h (struct fault_opts): Change the type of err field to int16_t,
add signo field.
(trace_syscall): Add a pointer argument.
* qualify.c: Include "nsig.h".
(parse_fault_token): Handle signal= option.
(qualify_fault): Update default fault_opts.
* strace.c (trace): Forward signal number from trace_syscall
to ptrace_restart(PTRACE_SYSCALL).
* syscall.c (inject_syscall_fault_entering): Add pointer argument,
save there the signal number specified by fault options.  Do not inject
a syscall fault unless instructed by fault options.
(update_syscall_fault_exiting): Update the error code injection check.
(trace_syscall_entering): Add pointer argument, forward it to
inject_syscall_fault_entering.
(trace_syscall): Add pointer argument, forward it to
trace_syscall_entering.

Signed-off-by: Seraphime Kirkovski <kirkseraph@gmail.com>
Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
2016-12-28 18:33:28 +00:00
857398e779 Remove redundant casts of ptrace arguments
* strace.c (maybe_switch_tcbs, trace): Do not cast ptrace 4th argument
from pointer to long type.
2016-12-24 14:58:41 +00:00
0868093228 ptrace_restart: cleanup
* strace.c (ptrace_restart): Replace the sequence of "if" statements
with a "switch" statement.
2016-12-24 14:58:39 +00:00
5691f4e23a ptrace_restart: change types of integer arguments to unsigned
* strace.c (ptrace_restart): Change types of "op" and "sig"
from int to unsigned int.
2016-12-24 14:58:29 +00:00
Sean Stangl
54c7792bfd Fix libunwind segfault when -p is passed before -k
* strace.c (init) [USE_LIBUNWIND]: Call unwind_tcb_init after
unwind_init for all allocated tcb structures if stack trace is enabled.
* unwind.c (unwind_tcb_init): Skip if tcb is already initialized.

Co-authored-by: Dmitry V. Levin <ldv@altlinux.org>
2016-12-13 20:21:22 +00:00