Implement queueing of threads before dispatching them
It is possible that some tracees call a lot of cheap syscalls too fast,
and that can stress the tracer to the point some tracees are not served
for indefinite amount of time. In order to avoid that unfairness, try
to collect all the pending tracees first along with the relevant
information and only then dispatch the events.
* defs.h: Include "list.h".
(struct tcb): Add wait_data_idx, delayed_wait_data, and wait_list
fields.
* strace.c (struct tcb_wait_data): Add "msg" field.
(tcb_wait_tab, tcb_wait_tab_size): New static variables.
(alloctcb): Initialize wait_list.
(droptcb): Remove tcp from wait_list.
(maybe_switch_tcbs): Get old pid from
tcb_wait_tab[tcp->wait_data_idx].msg instead of calling
ptrace(PTRACE_GETEVENTMSG).
(trace_wait_data_size, init_trace_wait_data, copy_trace_wait_data,
free_trace_wait_data, tcb_wait_tab_check_size): New functions, in order
to allow the code outside next_event to operate with wait_data as with
an opaque object (needed for dispatch_event and restart_delayed_tcb).
(next_event): Add pending_tcps, extra_tcp, wait_nohang, elem, and
wait_tab_pos variables; check for elements in pending_tcps and skip
waiting if the list is not empty; check for extra_tcp and skip waiting
along with swapping wait_data_idx with wait_extra_data_idx;
after the initial wait4(), call wait4() in loop with WNOHANG flag set;
fetch siginfo on signal and eventmsg on PTRACE_EVENT_EXEC;
return the first tcp in pending_tcps list.
(dispatch_event): Store a pointer to a copy of tcb_wait_data in
tcp->delayed_wait_data if tcp's restart has to be delayed.
(restart_delayed_tcb): Use tcp->delayed_wait_data, create a stub
tcb_wait_data if it is NULL, free temporary trace_wait_data.
* tests/Makefile.am (XFAIL_TEST): Remove looping_threads.test.
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=478419
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=526740
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=851457
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1609318
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1610774 Co-Authored-by: Dmitry V. Levin <ldv@altlinux.org> Co-Authored-by: Denys Vlasenko <dvlasenk@redhat.com> Co-Authored-by: Andreas Schwab <aschwab@redhat.com> Co-Authored-by: Jeff Law <law@redhat.com> Co-Authored-by: DJ Delorie <dj@redhat.com>
The commit v4.24~48 "Implement -e kvm= option on all architectures",
while removing #ifdef, left formatting of the help message string a bit
inconsistent.
* strace.c (usage): Move "\n\" bit after "kvm", remove double quotes.
Complements: v4.24~48 "Implement -e kvm= option on all architectures"
* strace.c (die): Pass 0 as an argument to cleanup().
(cleanup): Add fatal_sig parameter, remove the local variable
of the same name, do not use interrupted.
(terminate): Initialise a new local variable "sig" with "interrupted",
pass it to cleanup() and use it instead of "interrupted" for exit_code
initialisation.
* xlat/gen.sh: Export LC_ALL=C so it could be omitted from individual
invocations.
(cond_def): Remove LC_ALL=C from sed invocation.
(gen_header): Remove LC_COLLATE=C.
Makefile.am: pass D variable to shell scripts' environment
And set -x if D is set to 1, so the commands called by various shell scripts
are easier to see during debugging.
* Makefile.am (bpf_attr_check.c, sen.h, mpers-m%.stamp, $(srcdir)/CREDITS): Pass
D variable in the environment to the shell script call.
* gen_bpf_attr_check.sh: Set -x if D is set to 1.
* generate_mpers_am.sh: Likewise.
* generate_sen.sh: Likewise.
* maint/gen-contributors-list.sh: Likewise.
* mpers.sh: Likewise.
* mpers_test.sh: Likewise.
tests: add checks for dumpstr output into read-write test
util.c:dumpstr() now aligns offsets in accordance with the total length
of the dump and also may perform only partial dump, so let's add checks
for these cases.
* tests/fill_memory.c (fill_memory_ex): Change the type of the period
argument from unsigned char to unsigned int.
* tests/tests.h (fill_memory_ex): Likewise.
* tests/read-write.c (dump_str_ex): Rename from dump_str, add idx_w
argument, support len greater than 240 bytes.
(dump_str): New function, wrapper for dump_str_ex with the default index
width.
(print_hex): Add checks for dumpstr output, close fd 1 later.
Use a buffer of a limited size, use proper type for dump amount, avoid
hard-coding of byte counts, calculate output buffer size more accurately
and minimise its rewriting, pad offset with zeros in accordance
with expected output amount.
* defs.h (dumpstr): Change the type of len argument from int to
kernel_ulong_t.
* macros.h (ROUNDUP_DIV): New macro.
(ROUNDUP): Rewrite using ROUNDUP_DIV.
* util.c (ILOG2_ITER_): New macro.
(ilog2_64, ilog2_32): New functions.
(ilog2_klong): New macro, wrapper around ilog2_32/ilog2_64, so (potentially
more expensive) ilog2_64 is not used for ilog2 calculation
of a kernel_ulong_t-typed variable on architectures with 32-bit kernel long.
(dumpstr): Update.
Co-Authored-by: Dmitry V. Levin <ldv@altlinux.org>
Character class checks and hexadecimal number formatting are open-coded
all over the place, let's try to de-duplicate them a bit.
* print_utils.h: New file.
* Makefile.am (strace_SOURCES): Add it.
* util.c: Include "print_utils.h".
(string_quote, dumpstr): Use sprint_byte_hex and is_print.
* v4l2.c: Include "print_utils.h".
(print_pixelformat): Use is_print and BYTE_HEX_CHARS_PRINTF_QUOTED,
add a note that the routine is rather generic.
cleanup() has exactly two call sites, die() and terminate(), and the
first one resets cflag in order to prevent call_summary() invocation.
Move the call to terminate() in order to avoid bogus cflag reset.
Dmitry V. Levin [Tue, 19 Feb 2019 01:39:32 +0000 (01:39 +0000)]
build: fix --with-libiberty
* m4/st_demangle.m4 (st_DEMANGLE): Move the check for cplus_demangle
out of AC_CHECK_HEADERS because the latter checks for demangle.h in two
different places and an absence of demangle.h in one of these places
would cause a build error in --with-libiberty=yes mode.
* NEWS: Mention this fix.
Dmitry V. Levin [Sun, 17 Feb 2019 00:10:26 +0000 (00:10 +0000)]
hppa: workaround kernel bug in syscall number tampering
Linux kernel commit v4.6-rc2~20^2 introduced a regression:
when tracer changes syscall number to -1, the kernel fails
to initialize %r28 with -ENOSYS and subsequently fails
to return the error code of the failed syscall to userspace.
Workaround this by initializing %r28 ourselves.
* linux/arch_defs_.h (ARCH_NEEDS_SET_ERROR_FOR_SCNO_TAMPERING): Define
to 0.
* linux/hppa/arch_defs_.h (ARCH_NEEDS_SET_ERROR_FOR_SCNO_TAMPERING):
Define to 1.
* syscall.c (tamper_with_syscall_entering)
[ARCH_NEEDS_SET_ERROR_FOR_SCNO_TAMPERING]: When changing syscall number
to -1, set the return value as well.
Dmitry V. Levin [Fri, 15 Feb 2019 21:22:30 +0000 (21:22 +0000)]
mips o32: fix 7th syscall argument with PTRACE_GET_SYSCALL_INFO API
As PTRACE_GET_SYSCALL_INFO API supports 6 syscall arguments,
all architectures that have syscalls with more than 6 arguments
(i.e. mips o32) have to fetch extra arguments manually when using
PTRACE_GET_SYSCALL_INFO API.
* linux/mips/get_syscall_args.c [LINUX_MIPSO32]
(arch_get_syscall_args_extra): New function.
* syscall.c (get_syscall_args) [MAX_ARGS > 6]: Use it.
Dmitry V. Levin [Mon, 11 Feb 2019 21:00:05 +0000 (21:00 +0000)]
tests: robustify preadv2-pwritev2 test against odd kernels
The test used to assume that either both preadv2 and pwritev2 syscalls
are implemented or both are not implemented, but, apparently, there are
kernels in the wild that implement just preadv2 syscall without
pwritev2.
* tests/preadv2-pwritev2.c (main): Skip the dumpio part of the test
if either preadv2 or pwritev2 syscall is not implemented.
Dmitry V. Levin [Sun, 10 Feb 2019 21:04:41 +0000 (21:04 +0000)]
Fix whitespace issues
* Makefile.am (strace_SOURCES, EXTRA_DIST): Replace SPACE symbol followed
by TAB symbol with TAB symbol.
* kvm.c (is_map_for_file): Likewise.
* tests/gen_tests.in (ioctl_ptp, printsignal-Xverbose): Likewise.
* xlat/skf_ad.in: Likewise.
sockaddr: print sin6_flowinfo field before sin6_addr
The sin6_flowinfo field comes before sin6_addr in the sockaddr_in6
defintion, but for some reason we switch the order when decoding.
This patch fixes the order of decoding.
Dmitry V. Levin [Tue, 8 Jan 2019 19:23:44 +0000 (19:23 +0000)]
mips o32: fix build
Commit 917c2ccf3a67 "Refactor stack pointers" moved mips_REG_* macros
from linux/mips/arch_regs.h to linux/mips/arch_regs.c because these
macros are no longer used outside syscall.c or files included by
syscall.c, but this caused a build regression on mips o32 because
decode_syscall_subcall() uses mips_REG_SP prior to its definition.
Baruch Siach [Tue, 8 Jan 2019 16:36:20 +0000 (18:36 +0200)]
Fix build for no-MMU targets
Commit 7488ce9e23f1 "Check whether PTRACE_GET_SYSCALL_INFO is supported
by the kernel" added a fork() call in test_ptrace_get_syscall_info()
which is included in the main strace executable code. Although the
test_ptrace_get_syscall_info() routine is not called on no-MMU targets,
the compiler can't optimize it out because it is a global symbol.
So build for no-MMU targets currently fails:
strace-ptrace_syscall_info.o: In function `test_ptrace_get_syscall_info':
ptrace_syscall_info.c:(.text+0x1c): undefined reference to `fork'
* ptrace_syscall_info.c (test_ptrace_get_syscall_info): Make
the definition depend on HAVE_FORK.
* strace.c (init): Call test_ptrace_get_syscall_info unconditionally.
* NEWS: Mention this fix.
Co-Authored-by: Dmitry V. Levin <ldv@altlinux.org> Fixes: v4.26~50 "Check whether PTRACE_GET_SYSCALL_INFO is supported by the kernel"
Dmitry V. Levin [Sat, 5 Jan 2019 21:14:42 +0000 (21:14 +0000)]
xlat: update NT_* constants
* xlat/nt_descriptor_types.in (NT_ARM_PAC_MASK, NT_MIPS_MSA): New
constants introduced by Linux kernel commits v5.0-rc1~35^2~21
and v5.0-rc1~30^2~58, respectively.
* NEWS: Mention this.
Dmitry V. Levin [Sat, 5 Jan 2019 21:14:42 +0000 (21:14 +0000)]
xlat: update V4L2_* constants
* xlat/v4l2_buf_types.in (V4L2_BUF_TYPE_META_OUTPUT): New constant
introduced by Linux kernel commit v5.0-rc1~181^2~21.
* xlat/v4l2_device_capabilities_flags.in (V4L2_CAP_META_OUTPUT):
Likewise.
* xlat/v4l2_pix_fmts.in (V4L2_PIX_FMT_CNF4): New constant introduced
by Linux kernel commit v5.0-rc1~182^2~120.
* NEWS: Mention this.
Dmitry V. Levin [Sat, 5 Jan 2019 21:14:42 +0000 (21:14 +0000)]
xlat: update KVM_CAP_* constants
* xlat/kvm_cap.in (KVM_CAP_MANUAL_DIRTY_LOG_PROTECT,
KVM_CAP_HYPERV_CPUID): New constants introduced by Linux kernel commits
v5.0-rc1~156^2~115 and v5.0-rc1~156^2~74, respectively.
* NEWS: Mention this.
Dmitry V. Levin [Sat, 5 Jan 2019 21:14:42 +0000 (21:14 +0000)]
xlat: update BTRFS_FEATURE_INCOMPAT_* constants
* xlat/btrfs_features_incompat.in
(BTRFS_FEATURE_INCOMPAT_METADATA_UUID): New constant introduced by Linux
kernel commit v5.0-rc1~126^2~64.
* NEWS: Mention this.
Martin Lau [Thu, 3 Jan 2019 23:36:22 +0000 (23:36 +0000)]
tests: fix prog_info initialization in bpf-obj_get_info_by_fd-prog*
The sys_bpf(BPF_OBJ_GET_INFO_BY_FD, &bpf_prog_get_info_attr, ...) is
called in a loop. The bpf_prog_get_info_attr.info object is in size 104
but bpf_prog_get_info_attr.info_len is in size 168. Hence, if the prog
is jited, the second iteration onwards will have nr_jited_ksyms == 1
which is asking the kernel to fill the
bpf_prog_get_info_attr.info.jited_ksyms and it is NULL.
* tests/bpf-obj_get_info_by_fd.c (main): Clear memory beyond prog_info
at the start of every iteration.
Co-Authored-by: Dmitry V. Levin <ldv@altlinux.org>
Dmitry V. Levin [Tue, 1 Jan 2019 21:36:08 +0000 (21:36 +0000)]
tests: fix strace-k.test on alpha
Change strace-k.test to use chdir syscall instead of getpid syscall
because the latter is not universally available, e.g. there is no getpid
syscall on alpha.
Dmitry V. Levin [Wed, 26 Dec 2018 03:38:33 +0000 (03:38 +0000)]
xlat: update BPF_* constants
* xlat/bpf_map_types.in (BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE): New
constant introduced by Linux kernel commit v4.20-rc1~14^2~124^2~12^2~7.
(BPF_MAP_TYPE_QUEUE, BPF_MAP_TYPE_STACK): New constants introduced
by Linux kernel commit v4.20-rc1~151^2~32^2~11^2~3.
* xlat/bpf_commands.in (BPF_MAP_LOOKUP_AND_DELETE_ELEM): New constant
introduced by Linux kernel commit v4.20-rc1~151^2~32^2~11^2~2.
* tests/bpf.c (BPF_MAP_CREATE_checks): Update.
xlat: update V4L2_CID_* and V4L2_PIX_FMT_* constants
* xlat/v4l2_control_ids.in (V4L2_CID_MPEG_VIDEO_MPEG2_SLICE_PARAMS,
V4L2_CID_MPEG_VIDEO_MPEG2_QUANTIZATION): New constants, introduced
by Linux commit v4.20-rc1~51^2~4.
* xlat/v4l2_pix_fmts.in (V4L2_PIX_FMT_MPEG2_SLICE): Likewise.
(V4L2_PIX_FMT_SUNXI_TILED_NV12): New constant, introduced by Linux
commit v4.20-rc1~51^2~3.
* NEWS: Mention this.
Co-Authored-by: Dmitry V. Levin <ldv@altlinux.org>
xlat: add REL_RESERVED and REL_WHEEL_HI_RES to evdev_relative_axes
* xlat/evdev_relative_axes.in (REL_RESERVED): New constant, introduced
by Linux commit v4.20-rc1~133^2~2^2.
(REL_WHEEL_HI_RES): New constant, introduced by Linux commit
v4.20-rc1~133^2~2^2~6 and updated by Linux commit v4.20-rc1~133^2~2^2.
* NEWS: Mention this.
Co-Authored-by: Dmitry V. Levin <ldv@altlinux.org>
* xlat/kvm_cap.in (KVM_CAP_NESTED_STATE): New constant, introduced
by Linux commit v4.19-rc1~87^2~62.
(KVM_CAP_ARM_INJECT_SERROR_ESR): New constant, introduced by Linux
commit v4.19-rc1~55^2~12^2~14, merged with its final value
in v4.19-rc1~55^2~12.
(KVM_CAP_MSR_PLATFORM_INFO): New constant, introduced by Linux commit
v4.19-rc5~8^2~2.
(KVM_CAP_PPC_NESTED_HV): New constant, introduced by Linux commit
v4.20-rc1~113^2~107^2~1.
(KVM_CAP_HYPERV_SEND_IPI): New constant, introduced by Linux commit
v4.20-rc1~113^2~72.
KVM_CAP_COALESCED_PIO): New constant, introduced by Linux commit
v4.20-rc1~113^2~31.
(KVM_CAP_HYPERV_ENLIGHTENED_VMCS): New constant, introduced by Linux
commit v4.20-rc1~113^2~25.
(KVM_CAP_EXCEPTION_PAYLOAD): New constant, introduced by Linux commit
v4.20-rc1~113^2~8.
(KVM_CAP_ARM_VM_IPA_SIZE): New constant, introduced by Linux commit
v4.20-rc1~113^2~5^2~8 and updated by Linux commit
v4.20-rc1~113^2~5.
* NEWS: Mention this.
Co-Authored-by: Dmitry V. Levin <ldv@altlinux.org>