Optimize out PTRACE_PEEKUSER with -i

strace -i was fetching PC with a separate PEEKUSER
despite having GETREGS data:

ptrace(PTRACE_GETREGS, 22331, 0, 0x8087f00) = 0
ptrace(PTRACE_PEEKUSER, 22331, 4*EIP, [0x80dd7b7]) = 0
write(3, "[080dd7b7] ioctl(0, SNDCTL_TMR_T"..., 82) = 82
ptrace(PTRACE_SYSCALL, 22331, 0, SIG_0) = 0

Now it does this:

ptrace(PTRACE_GETREGS, 22549, 0, 0x8087ea0) = 0
write(3, "[080dd7b7] ioctl(0, SNDCTL_TMR_T"..., 82) = 82
ptrace(PTRACE_SYSCALL, 22549, 0, SIG_0) = 0

Analogous improvement in sys_sigreturn() is also implemented.

* defs.h: Declare extern struct pt_regs regs for SPARC[64] and ARM.
Declare clear_regs(), get_regs() and get_regs_error flag variable.
* strace.c (trace): Call get_regs(pid) as soon as we know the tcb
and that it is stopped.
* syscall.c (get_regs): New function. Used to fetch registers early,
just after tracee has stopped.
(printcall): Move it here from util.c. Use global regs.REG data,
if available on the arch, instead of re-fetching it.
(get_scno): Use global regs.REG data.
(get_syscall_result): Likewise.
* signal.c (sys_sigreturn): Likewise.
* util.c (printcall): Moved to syscall.c.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Do not compile getrval2() if not needed

* syscall.c (getrval2): Do not compile it for architetures where
it isn't ever used.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Small optimization for SPARC[64] get_scno

* syscall.c: Remove static unsigned long trap veriable.
(get_scno): Use local trap variable.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Add tilegx support to strace

tilegx support has been in the kernel since 3.0.
In addition, fix some issues with the tilepro support already
present in strace, primarily the decision to use the
<asm/unistd.h> numbering space for system calls.

* defs.h [TILE]: Include <asm/ptrace.h> and provide an extern
struct pt_regs tile_regs for efficiency.  Provide compat 32-bit
personality via SUPPORTED_PERSONALITIES, PERSONALITY0_WORDSIZE,
PERSONALITY1_WORDSIZE, and DEFAULT_PERSONALITY.
* linux/tile/errnoent1.h: New file, includes linux/errnoent.h.
* linux/tile/ioctlent1.h: New file, includes linux/ioctlent.h.
* linux/tile/signalent1.h: New file, includes linux/signalent.h.
* linux/tile/syscallent.h: Update with new asm-generic syscalls.
The version previously committed was the from the first tile patch
to LKML, which subsequently was changed to use <asm-generic/unistd.h>.
* linux/tile/syscallent1.h: Copy from linux/tile/syscallent.h.
* mem.c (addtileflags) [TILE]: use %ld properly for a "long" variable.
* process.c [TILE]: Choose clone arguments correctly and properly
suppress all "struct user" related offsets in user_struct_offsets.
* signal.c [TILE]: Use tile_regs not upeek.
* syscall.c (update_personality) [TILE]: Print mode.
(PT_FLAGS_COMPAT) [TILE]: Provide if not in system headers.
(tile_regs) [TILE]: Define 'struct pt_regs' variable to hold state.
(get_regs) [TILE]: use PTRACE_GETREGS to set tile_regs rather than using upeek.
(get_scno) [TILE]: Set personality.
(get_syscall_args) [TILE]: Use tile_regs.
(get_syscall_result) [TILE]: Update tile_regs.
(get_error) [TILE]: Use tile_regs.
(printcall) [TILE]: Print pc.
(arg0_offset, arg1_offset, restore_arg0, restore_arg1) [TILE]:
Properly handle tile call semantics and support tilegx.

Signed-off-by: Chris Metcalf <cmetcalf@tilera.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Simple bug fix for x86_86

* syscall.c (printcall): Use x86_64_regs.rip, not x86_64_regs.ip.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
x86_64: fix compilation warning introduced in previous commit

* syscall.c (printcall): Cast x86_64_regs.rip to the type being printed.
Print 64-bit instruction pointers zero padded

* syscall.c (printcall): Print 64-bit instruction pointers zero padded.
mmap: decode MAP_UNINITIALIZED

* mem.c (mmap_flags): Add MAP_UNINITIALIZED.

Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
mount: decode MS_NOSEC

* system.c (MS_NOSEC): Define.
(mount_flags): Add MS_NOSEC.

Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
tile: fix merge skew with new get_regs architecture

* defs.h [TILE]: Declare clear_regs(), get_regs() and get_regs_error.
* syscall.c (get_regs) [TILE]: Fix merge skew.
(printcall) [TILE]: fix a compiler warning about pt_reg_t in
a printf expression.

Signed-off-by: Chris Metcalf <cmetcalf@tilera.com>
Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
Improve perf_event_open argument decoding

* configure.ac (AC_CHECK_HEADERS): Add linux/perf_event.h.
* desc.c [HAVE_LINUX_PERF_EVENT_H]: Include <linux/perf_event.h>.
(perf_event_open_flags): New xlat structure.
(sys_perf_event_open): New function.
* linux/dummy.h (sys_perf_event_open): Remove.
* linux/syscall.h (sys_perf_event_open): New prototype.

Signed-off-by: Ben Noordhuis <info@bnoordhuis.nl>
Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
Shortcut tests for fork/exec syscalls

This change should speed up strace by a tiny bit.

More importantly, it makes it much more clear that
fork and exec fixups are not necessary for any reasonably
recent kernels. IOW: syscall_fixup_for_fork_exec() and its callees
are all dead code.

* defs.h: Declare new need_fork_exec_workarounds flag variable.
* strace.c: Define need_fork_exec_workarounds flag variable.
(test_ptrace_setoptions_followfork): Return 0/1 as success/fail indicator.
(test_ptrace_setoptions_for_all): Likewise.
(init): Set need_fork_exec_workarounds to TRUE if needed.
* syscall.c: Rename internal_syscall() to syscall_fixup_for_fork_exec().
(trace_syscall_entering): Call syscall_fixup_for_fork_exec() only if
need_fork_exec_workarounds == TRUE.
(trace_syscall_exiting): Likewise.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Stop needlessly using static variable

* syscall.c: Remove static long psr.
(get_scno): Use local psr variable.
(get_syscall_result): Likewise.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Rename ARM's regs structure to arm_regs

Compile-tested.

* defs.h: Rename regs structure to arm_regs.
* syscall.c: Likewise.
(printcall): Use new name instead of old one.
(get_regs): Likewise.
(get_scno): Likewise.
(get_syscall_args): Likewise.
(get_error): Likewise.
* signal.c (sys_sigreturn): Likewise.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Rename some register statics by prefixing their names with arch.

This makes it easier to grep for them.

* syscall.c: Make IA64's r8, r10 global variables static.
Rename variables:
r8,r10 -> ia64_r8,ia64_r10.
d0 -> m68k_d0.
a3 -> alpha_a3.
r28 -> hppa_r28.
r9 -> sh64_r9.
r10 -> cris_r10.
r3 -> microblaze_r3.
(get_scno): Use new variable names.
(syscall_fixup_on_sysenter): Likewise.
(get_syscall_result): Likewise.
(get_error): Likewise.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Optimize AArch64 handling of 32-bit personality

By putting aarch64_regs and arm_regs into a union,
register copying is eliminated.
No need to check and change personality on syscall exit.

* defs.h: Remove unused NUM_ARM_REGS define. Fix indentation.
* syscall.c: Put aarch64_regs and arm_regs into a union.
(update_personality): Shorten bitness message.
(printcall): Add commented-out PC printing.
(get_regs): Remove now-unnecessary 64-to-32 bits register copying.
(get_syscall_result): Drop personality changing code.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Remove vestigial hacks around non-Linux struct sigactions

* signal.c: Stop using __sighandler_t glibc'ism. Remove SA_HANDLER macro.
Explain why we can't use "sa_handler" as a field name.
(sys_sigaction): Use __sa_handler instead of SA_HANDLER macro.
(sys_rt_sigaction): Likewise.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Dying suddenly with abort() is rude, avoid if possible

* file.c (sys_utime): Don't call abort() if wordsize is strange.
Instead, warn user about it.
* desc.c (printflock): Use the same message string as in sys_utime.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
S390: stop using needlessly static long pc variable

* syscall.c: Remove "static long pc" variable.
(get_scno): Use an automatic long variable instead of a static.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
If we are on a glibc system, assume it's at least glibc 2.1

It is not likely anyone uses glibc older that that:
glibc 2.1.1 was released in 1999

* net.c: Remove test for glibc >= 2.1.
* signal.c: Remove code which is compiled only for glibc < 2.1.
* util.c: Likewise.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>