tests: fix SCM_RIGHTS test for big-endian systems

* tests/scm_rights.c (main): Send zero integer to avoid issues with
endianness.
* tests/scm_rights-fd.test: Update grep patterns.
Implement xlat generator

* bootstrap: New file.
* xlat/gen.sh: Likewise.
* Makefile.am: Include xlat/Makemodule.am
(EXTRA_DIST): Add $(XLAT_INPUT_FILES), $(XLAT_HEADER_FILES), and
xlat/gen.sh.
Use bootstrap script consistently

Now that ./xlat/gen.sh has to be run before autoreconf,
replace all autoreconf calls with ./bootstrap call.

* bootstrap: Forward arguments to autoreconf.
* build_static_example.sh: Replace autoreconf call with bootstrap call.
* make-dist: Likewise.
* qemu_multiarch_testing/README: Likewise.
Enhance xlat generator

* xlat/gen.sh: Define all xlat structs not declared in defs.h as static.
Some symbolic constants are not macros, extend #ifdef check to cover
symbolic constants checked by AC_CHECK_DECLS.
Handle complex symbolic constants in SYMBOL|... form.
Handle symbolic constants in 1<<SYMBOL form.
Handle numeric constants.
Implement #unconditional directive that turns off preprocessor checks.
Implement #unterminated directive that turns off adding XLAT_END.
Rename several xlat structures to avoid collisions

* bjm.c (which): Rename to qm_which.
* ipc.c (msg_flags): Rename to ipc_msg_flags.
* time.c (which): Rename to itimer_which.
Generate xlat/*.in files

Automatically convert xlat structures from *.c files to xlat/*.in files
using "./generate_xlat_in.sh *.c" command.
xlat: cleanup the aftermath of automatic conversion
Compress blank lines

Suppress empty lines left after automated xlat conversion.
Check for LO_FLAGS_READ_ONLY constant

* configure.ac (AC_CHECK_DECLS): Add LO_FLAGS_READ_ONLY.
Check for constants used by waitid function

* configure.ac (AC_CHECK_DECLS): Add P_* constants.
sysctl: update CTL_*, KERN_*, NET_*, and VM_* constants

* configure.ac (AC_CHECK_DECLS): Add CTL_*, KERN_*, NET_*, and
VM_* constants.
* system.c (CTL_PROC, CTL_CPU): Remove definitions.
* xlat/sysctl_*.in: Update.
Add -k option to print stack trace after each syscall

Print the stack trace of the traced process after each system call when
-k option is specified.  It is implemented using libunwind to unwind the
stack and to obtain the function name pointed by the IP.

Based on the code that was originally taken from strace-plus
of Philip J. Guo.

* configure.ac: Add --with-libunwind option.  Check libunwind support.
* Makefile.am: Add libunwind support.
* defs.h (struct tcb) [USE_LIBUNWIND]: Append libunwind specific fields.
[USE_LIBUNWIND] (stack_trace_enabled, alloc_mmap_cache,
delete_mmap_cache, print_stacktrace): New prototypes.
* mem.c (print_mmap, sys_munmap, sys_mprotect): Add libunwind support.
* process.c (sys_execve): Likewise.
* strace.c (usage, alloctcb, droptcb, init): Likewise.
* syscall.c (trace_syscall_exiting): Likewise.
* unwind.c: New file.
* strace.1: Document -k option.
unwind: fix a bug in range updating of binary search

* unwind.c (print_stacktrace): Fix off-by-one error in binary search.

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
Signed-off-by: Luca Clementi <luca.clementi@gmail.com>
unwind: make alloc_mmap_cache function local

* defs.h (alloc_mmap_cache): Remove.
* unwind.c (alloc_mmap_cache): Add static qualifier.

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
unwind: delete mmap cache in free_libunwind_ui

free_libunwind_ui is expected to release all unwind related resources
attached to tcp.

* strace.c (droptcb): Move delete_mmap_cache call ...
* unwind.c (free_libunwind_ui): ... to here.

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
unwind: give all exported functions "unwind_" prefix

* unwind.c (init_unwind_addr_space): Rename to unwind_init.
(init_libunwind_ui): Rename to unwind_tcb_init.
(free_libunwind_ui): Rename to unwind_tcb_fin.
(delete_mmap_cache): Rename to unwind_cache_invalidate.
(print_stacktrace): Rename to unwind_print_stacktrace.
* defs.h: Update prototypes.
* mem.c: All callers updated.
* process.c: Likewise.
* strace.c: Likewise.
* syscall.c: Likewise.

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
unwind: introduce stacktrace_walker

In current implementation, the stack trace is captured and printed at
the same time, in trace_syscall_exiting.  This approach cannot
provide user expected information when a system call changes the
memory mapping.  In such cases, the stack trace should be captured on
entering syscall and printed on exiting.

As the initial step for splitting capturing from printing, this change
introduces stacktrace_walker utility function.  It can be used both for
capturing in trace_syscall_entering and printing in
trace_syscall_exiting.

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
unwind: introduce own debug macro

* unwind.c (DPRINTF): New macro, to be utilized in debugging cache
management code.

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
unwind: introduce queue_t for capturing stacktrace

This is the second step for splitting capturing from printing.

New `queue' field is added to tcb.  Captured stacktrace is stored here.
The field is initialized/finalized at unwind_tcb_init/unwind_tcb_fin.

New API function unwind_capture_stacktrace is added.  This function
captures the currest stack using stracktrace_walker and records it in
tcb.  It's printing is delayed to the next call of
unwind_print_stacktrace.

unwind_print_stacktrace is extended.  Now it checks queue field of
the given tcb at the start of function.  If the function finds a
captured stack trace, the latter is printed using stracktrace_walker.

Currently unwind_capture_stacktrace invocations are added directly to
handlers of mmap, munmap, mprotect, and execve.

Here is the difference of output with/without patch:

(without patch)
  execve("./test-fork", ["./test-fork"], [/* 56 vars */]) = 0
   > /usr/lib64/ld-2.18.so(check_one_fd.part.0+0x82) [0x11f0]

(with patch)
  execve("./test-fork", ["./test-fork"], [/* 54 vars */]) = 0
   > /usr/lib64/libc-2.18.so(execve+0x7) [0xbcd27]
   > /home/yamato/var/strace/strace(exec_or_die+0x10c) [0x26ac]
   > /home/yamato/var/strace/strace(startup_child+0x346) [0x134f6]
   > /home/yamato/var/strace/strace(init+0x89f) [0x13dff]
   > /home/yamato/var/strace/strace(main+0xa) [0x26ca]
   > /usr/lib64/libc-2.18.so(__libc_start_main+0xf5) [0x21d65]
   > /home/yamato/var/strace/strace(_start+0x29) [0x2799]

In older version output lines of captured elements were built when
printing.  In this version they are built when capturing the stack.
As result, unneeded dynamic memory allocations are avoided.
Suggested by Luca Clementi.

In older version the combination of snprintf and realloc were used.
In this version they are replaced with asprintf.
Suggested by Dmitry Levin.

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
unwind: implement automatic mmap cache invalidation

A mmap cache belonging to a tcb was updated when a system call which
changed the memory mapping was called.  This implementation was assumed
the mapping was changed only by the tcb.  However, this assumption is
incorrect if the target application is multi-threaded; more than two
tcbs can shared the same memory mapping and a tcb can modify it without
being noticed by the others.

This change introduces a global integer variable mmap_cache_generation,
and mmap_cache_generation field to struct tcb.  The variable
is incremented each time a process enters a syscall that can modify its
memory mapping.  Each tcb records the value of this variable at the
moment if  building its mmap cache.  Every mmap cache associated with
the given tcb can be validated by comparing its mmap_cache_generation
field with the variable mmap_cache_generation.

This implementation is inefficient.  If strace attaches two processes
which don't share the memory mapping, rebuilding mmap cache of a tcb
triggered by another tcb's mmap system call is not necessary.

Signed-off-by: Masatake YAMATO <yamato@redhat.com>