Let's try to future/fool-proof code a bit by introducing variants
of sprintf/snprintf that die if an overflow (or an error) occurs.
They are deemed to be the default choice for printing to some local
string buffer where no sensible error handling is implemented.
* xstring.h: New file.
* Makefile.am (strace_SOURCES): Add it.
Instead of printing to a temporary string. This also fixes a subtle bug
when the output for a billion errors and more is cut and shown
incorrectly.
* count.c (struct call_counts): Change type of calls and errors fields
to unsigned int since they are used and printed as unsigned anyway.
(call_summary_pers): Remove error_str. Print cc->errors
and error_cum using %9.u conversion specification.
This is a part of "Prepare for adding support for Lua scripting"
commit.
* defs.h (personality_names): New external constant declaration.
* syscall.c: (personality_names): New global variable.
(update_personality): Use personality_names for reporting personality
name.
* linux/powerpc/getregs_old.c (getregs_old): Change the first argument
from pid_t pid to struct tcb *tcp. Replace all instances of pid with
tcp->pid.
* linux/x86_64/getregs_old.c (getregs_old): Likewise.
* linux/x86_64/getregs_old.h (getregs_old): Update declaration.
* syscall.c (get_regs) [HAVE_GETREGS_OLD]: Pass tcp to the getregs_old
call instead of tcp->pid.
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
Masatake YAMATO [Mon, 18 Dec 2017 04:24:54 +0000 (13:24 +0900)]
Define macros for renaming xmalloc and xcalloc
I'm planning to link strace with libiberty for mangling C++
symbol names in stack trace enabled with -k option.
Both names, xmalloc and xcalloc, are already defined and used
in libiberty, and they conflict with functions in strace.
Rename xmalloc and xcalloc functions defined in strace
to strace_xmalloc and strace_xcalloc, respectively,
to avoid this conflict.
Dmitry V. Levin [Sun, 24 Dec 2017 15:07:10 +0000 (15:07 +0000)]
syscall: tweak shuffle_scno ifdefery
* syscall.c (shuffle_scno): Check for ARM_FIRST_SHUFFLED_SYSCALL
instead of ARM || AARCH64. This does not result to any code change
but looks more comprehensible.
Dmitry V. Levin [Sun, 24 Dec 2017 12:40:00 +0000 (12:40 +0000)]
syscall: change get_regs to return an error code
* syscall.c (get_regs_error) [!ptrace_getregset_or_getregs]: Do not
define.
(clear_regs) [!ptrace_getregset_or_getregs]: Do not set get_regs_error.
(get_regs) [ptrace_getregset_or_getregs]: Return get_regs_error, all
callers changed to test get_regs return code instead of get_regs_error
static variable, which is now internally used by get_regs() and
clear_regs() only.
(USE_GET_SYSCALL_RESULT_REGS): Do not define.
Use "#ifndef ptrace_getregset_or_getregs"
instead of "#ifdef USE_GET_SYSCALL_RESULT_REGS".
Dmitry V. Levin [Sat, 23 Dec 2017 01:51:29 +0000 (01:51 +0000)]
Remove linux/personality.h
* linux/personality.h: Remove.
* Makefile.am (EXTRA_DIST): Remove it.
* personality.c: Do not include <linux/personality.h>, include
"xlat/personality_flags.h" before "xlat/personality_types.h".
* xlat/personality_flags.in: Provide fallback definitions.
* xlat/personality_types.in: Likewise.
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().
An error returned by asprintf is a poor excuse for dying.
* unwind.c (asprintf_error_str): New string constant.
(sprint_call_or_error): Replace error_msg_and_die() with
perror_func_msg() and usage of asprintf_error_str for a return value.
(queue_print): Free tmp->output line only if it is not
asprintf_error_str.
Since DPRINTF is simply a macro that provides debugging information
along with some information about printing routine call site, now standard
debug_func_* macros are deemed to be a suitable replacement.
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.
* defs.h (debug_flag): Move the declaration...
* error_prints.h (debug_flag): ... here.
(debug_msg, debug_perror_msg): New macro definitions.
Include <stdbool.h> for the debug_flag declaration.
Masatake YAMATO [Mon, 4 Dec 2017 13:08:14 +0000 (22:08 +0900)]
kvm: decode the argument of KVM_SET_USER_MEMORY_REGION ioctl command
* configure.ac (AC_CHECK_TYPES): Add struct kvm_userspace_memory_region.
* xlat/kvm_mem_flags.in: New file.
* kvm.c: Include print_fields.h.
[HAVE_STRUCT_KVM_USERSPACE_MEMORY_REGION]: Include xlat/kvm_mem_flags.h,
(kvm_ioctl_set_user_memory_region): New function.
(kvm_ioctl) [HAVE_STRUCT_KVM_USERSPACE_MEMORY_REGION]
<KVM_SET_USER_MEMORY_REGION>: Use it.
Dmitry V. Levin [Sun, 3 Dec 2017 00:25:43 +0000 (00:25 +0000)]
tests: robustify attach-p-cmd.test against buggy kernels
From time to time various kernels, old and new, just go nuts
and fail attach-p-cmd.test with the following diagnostics:
12345 --- SIGALRM {si_signo=SIGALRM, si_code=SI_KERNEL} ---
-12345 chdir("attach-p-cmd.test -p") = -1 ENOENT (No such file or directory)
+12345 syscall_4294967295(0xffe60934, 0x1c0, 0xffe60934, 0x1, 0xffe609b4, 0xffe60a58) = -1 (errno 38)
+12345 chdir("attach-p-cmd.test -p") = -1 ENOSYS (Function not implemented)
Let's workaround this kernel madness by rewriting the test
without use of SIGALRM.
* tests/attach-p-cmd.h: New file.
* tests/Makefile.am (EXTRA_DIST): Add attach-p-cmd.h.
* tests/attach-p-cmd-cmd.c: Include "attach-p-cmd.h".
(write_pidfile, wait_for_peer_invocation): New functions.
(main): Use them.
* tests/attach-p-cmd-p.c: Include <time.h> and "attach-p-cmd.h".
(wait_for_peer_invocation, wait_for_peer_termination): New functions.
(main): Use them. Do not raise SIGALRM, use nanosleep after peer
termination.
Dmitry V. Levin [Sat, 2 Dec 2017 22:01:34 +0000 (22:01 +0000)]
tests: robustify ipc_msgbuf.test against broken libc
glibc between commits glibc-2.25~130 and glibc-2.26~740 had broken
msgctl(IPC_RMID) on hppa: this operation always failed with EINVAL
because of inappropriate use of IPC_64 flag. Similar issues were
fixed on other niche architectures. Let's workaround these issues
by skipping the test in case of msgctl(IPC_RMID) failure.
* tests/ipc_msgbuf.c (cleanup): Change return type to int,
return 77 in case of msgctl(IPC_RMID) failure.
(main): Explicitly invoke cleanup() at the end.
strace-log-merge: work around corner cases of strace -ttt parsing
Employing strace -ttt for the logs destined to be fed to
strace-log-merge is ultimately the best solution except for the logs
that pass the following dates:
Thu Jan 1 00:00:09 UTC 1970
Thu Jan 1 00:01:39 UTC 1970
Thu Jan 1 00:16:39 UTC 1970
Thu Jan 1 02:46:39 UTC 1970
Fri Jan 2 03:46:39 UTC 1970
Mon Jan 12 13:46:39 UTC 1970
Sun Apr 26 17:46:39 UTC 1970
Sat Mar 3 09:46:39 UTC 1973
Sun Sep 9 01:46:39 UTC 2001
Sat Nov 20 17:46:39 UTC 2286
Wed Nov 16 09:46:39 UTC 5138
Fri Sep 27 01:46:39 UTC 33658
Sun May 20 17:46:39 UTC 318857
Sat Nov 7 09:46:39 UTC 3170843
Sun Jul 5 01:46:39 UTC 31690708
Sat Jan 25 17:46:39 UTC 316889355
Let's lift this limitation.
* strace-log-merge: Generate numeric-only key for sorting, use sort -n
option.
Dmitry V. Levin [Sat, 25 Nov 2017 01:55:44 +0000 (01:55 +0000)]
mpers: fix cross-compiling
In cross builds, a target version of readelf has to be used
instead of a native one.
* configure.ac (AC_CHECK_TOOL): Add readelf.
* Makefile.am (READELF): New variable.
(mpers-m%.stamp): Pass it to $(srcdir)/mpers.sh.
* m4/mpers.m4 (st_MPERS): Pass READELF environment variable
to $srcdir/mpers_test.sh.
* mpers.sh (READELF): New variable.
Use it instead of readelf.
* maint/ioctls_sym.sh: Likewise.
* NEWS: Mention this fix.
Dmitry V. Levin [Wed, 22 Nov 2017 00:14:52 +0000 (00:14 +0000)]
tests: simplify bpf test a bit
* tests/bpf.c (DEF_BPF_INIT_FIRST): New macro.
(init_BPF_MAP_CREATE_first, init_BPF_MAP_LOOKUP_ELEM_first,
init_BPF_PROG_LOAD_first, init_BPF_OBJ_PIN_first,
init_BPF_PROG_ATTACH_first, init_BPF_PROG_TEST_RUN_first,
init_BPF_PROG_GET_NEXT_ID_first, init_BPF_OBJ_GET_INFO_BY_FD_first):
Use it.
Dmitry V. Levin [Tue, 21 Nov 2017 22:32:31 +0000 (22:32 +0000)]
Simplify union bpf_attr field checks
* configure.ac: Use AC_CHECK_MEMBERS instead of st_CHECK_UNION_BPF_ATTR
for union bpf_attr field checks as the former seems to be enough
for the case.
* m4/st_bpf.m4: Remove.
Dmitry V. Levin [Mon, 20 Nov 2017 00:29:10 +0000 (00:29 +0000)]
Implement decoding of BPF_PROG_TEST_RUN command of bpf syscall
* configure.ac: Check for union bpf_attr.test.duration.
* bpf.c (decode_BPF_PROG_TEST_RUN): New function.
(SYS_FUNC(bpf)) <bpf_cmd_decoders>: Use it.
* NEWS: Mention this.
* tests/bpf.c: Include "print_fields.h".
Add macro guard for BPF_PROG_TEST_RUN decoder test.
[HAVE_UNION_BPF_ATTR_TEST_DURATION] (sample_BPF_PROG_TEST_RUN_attr):
New variable.
[HAVE_UNION_BPF_ATTR_TEST_DURATION] (init_BPF_PROG_TEST_RUN_first,
print_BPF_PROG_TEST_RUN_first, init_BPF_PROG_TEST_RUN_attr,
print_BPF_PROG_TEST_RUN_attr): New functions.
(main) [HAVE_UNION_BPF_ATTR_TEST_DURATION]: Use them.
Dmitry V. Levin [Sun, 19 Nov 2017 23:38:58 +0000 (23:38 +0000)]
bpf: update BPF_MAP_CREATE decoding
Implement decoding of union bpf_attr.numa_node field for BPF_MAP_CREATE
command introduced by linux kernel commit v4.14-rc1~130^2~196^2~1.
* configure.ac: Check for numa_node member of union bpf_attr
instead of inner_map_fd.
(decode_BPF_MAP_CREATE): Add numa_node field to the structure, print it.
* NEWS: Mention this.
* tests/bpf.c: Update macro guards of BPF_MAP_CREATE decoder test.
(init_BPF_MAP_CREATE_first, print_BPF_MAP_CREATE_attr): Update expected
output.
(init_BPF_MAP_CREATE_attr): Initialize numa_node field, update offset.