Mark Wielaard [Sun, 5 Jan 2014 19:37:30 +0000 (20:37 +0100)]
libdwfl: Only skip reset of return register for non-CIE-return regno (ppc64).
For PPC64 we skip resetting the return register if it is already set.
This is because on PPC64 there are two DWARF registers numbers that can
represent the same register. Setting the return address again confuses
the unwinder. But we do want to reset it if the register number
(non-translated by the ppc64 ebl) is equal to the actual register number
as set in the CIE as return address. This happens on older toolchains in
.debug_frame where the return address is set to 108, but the ebl abi_cfi
also sets register number 65.
Mark Wielaard [Sat, 4 Jan 2014 22:28:33 +0000 (23:28 +0100)]
tests: Don't use ptrace detach stopped trick. Raise can return.
On older kernels the ptrace detach stop trick doesn't work reliably.
Just keep the child processes attached and stopped during the tests,
dwfl_linux_proc_attach will handle that fine now. Also on older kernels
raise would sometimes return anyway and cause a spurious assert. Just
ignore it.
Mark Wielaard [Sat, 4 Jan 2014 18:19:16 +0000 (19:19 +0100)]
backends: Add PPC64 machine_flag_check.
To distinguish between the current PPC64 ELF ABI and the revised
ELFv2 ABI that will not use function descriptors binutils started
to emit the version (currently 1) in the ehdr e_flags. Recognize
all valid versions (0, 1 or 2) in elflint by adding the hook
ppc64_machine_flag_check.
The testcase originally came from GCC which was testing the runtime
unwinder using _Unwind_ForcedUnwind. Since we are using our own external
unwinder we can just abort at the right place and unwind from there.
Mark Wielaard [Fri, 3 Jan 2014 17:15:02 +0000 (18:15 +0100)]
backends: Add aarch64 abi_cfi.
Setup initial CIE values for aarch64 for use with dwarf_frame functions.
Register info prefix should be the empty string (not NULL) when not used.
Add an EM_AARCH64 testcase to tests/run-addrcfi.sh to check both issues.
Mark Wielaard [Mon, 30 Dec 2013 21:00:57 +0000 (22:00 +0100)]
libdwfl: Add dwfl_core_file_attach and dwfl_linux_proc_attach.
Rewrite __libdwfl_attach_state_for_pid and __libdwfl_attach_state_for_core
as public functions and don't call them from dwfl_linux_proc_report and
dwfl_core_file_report anymore. This lets the user attach state explicitly
independ from how the dwfl modules have been reported. Since attaching
state is an explicit action now the error can be returned directly and we
don't need to keep track of process_attach_error. dwfl_linux_proc_attach
lets the user can tell libdwfl whether caller takes care of ptrace
attaching and stopping the threads under inspection, or whether the
callback needs to take care of that and detaching again.
Mark Wielaard [Sat, 28 Dec 2013 11:58:10 +0000 (12:58 +0100)]
libdwfl: dwfl_linux_proc_find_elf should only return regular files.
When the dwfl_linux_proc_find_elf callback is used together with the
dwfl_linux_proc_report callback that reads /proc/PID/maps files we might
see and try to open special character device files that cannot be normally
read and processed by libelf (and might hang the library on the initial
open or read from the file). Make sure we only try to open and return
regular files.
Mark Wielaard [Tue, 24 Dec 2013 09:37:58 +0000 (10:37 +0100)]
libdwfl: Fix memory leak in linux-core-attach. Allow reiterating threads.
core_next_thread would allocate a new thread_arg each time but never free
it. We only need one active thread_arg to keep the state. Free it when
there are no more threads. It was also not possible to start walking all
threads in the core again. Just reset the note offset at the start.
Mark Wielaard [Mon, 23 Dec 2013 22:21:59 +0000 (23:21 +0100)]
libdwfl: Fix build_id memory leak in dwfl_segment_report_module.
We might already have allocated memory to hold the build_id early in
consider_notes when we called consider_phdr for the program headers
we've read from the image. We would leak that memory when we don't use
it then because we return early/fail. This can be because either we
didn't find the correct bias or we skip the module because it would
conflict in address space with any already existing module of DWFL.
In both cases explicitly free the build_id memory.
Mark Wielaard [Sat, 28 Dec 2013 22:25:54 +0000 (23:25 +0100)]
stack: Add -l, --list-modules. Show module memory map, build-id and files.
Use to list modules detected for process or core file by stack program
and to see build-ids and which main elf and debug files were recognized
by libdwfl callbacks.
Mark Wielaard [Fri, 27 Dec 2013 09:49:51 +0000 (10:49 +0100)]
stack: Improve error checking and exit code handling.
Check up front whether we attached correctly, if not error out. Make sure
callbacks report -1 only on real errors and DWARF_CB_ABORT if exiting early
(but not in error). Handle all errors from frame callback in print_frames
after printing of good frames. Print as much information as possible like
tid, address and module name if known with error messages. Only exit with
exit code zero if everything went fine. Exit with error code one if there
were any non-fatal errors. Exit with error code two if no frames could be
printed or a fatal error occurred.
Mark Wielaard [Mon, 23 Dec 2013 20:19:05 +0000 (21:19 +0100)]
stack: Add --quiet to not resolve addresses, add --raw to not demangle.
Resolving addresses to function symbol names can be expensive. Use -q
to only print addresses (use together with --build-id to process later).
Demangle names by default, but add the -r option to not demangle and
show the raw names.
Mark Wielaard [Mon, 23 Dec 2013 09:46:54 +0000 (10:46 +0100)]
stack: Simplify argument parsing. Don't use dwfl_standard_argp.
We were using dwfl_standard_argp but trying to add our own and substract
some options from it. dwfl_standard_argp also handles kernel, modules,
executables without state and process maps that stack doesn't support.
That made argp parsing somewhat ugly and meant our --help and --usage
didn't really match. Just handle the dwfl_standard_argp options we do
want directly ('-p', '--core', '-e' and '--debuginfo-path'). That way
we can also do sanity checking on the options given.
Mark Wielaard [Sun, 22 Dec 2013 22:48:26 +0000 (23:48 +0100)]
stack: Add new '-n MAXFRAMES' option. Resolve addresses after unwind.
Limit the number of frames printed per thread (defaults to 64) and resolve
addresses to names, modules and source after unwinding so the thread is
only stopped for the minimum time needed to do the actual unwinding. The
thread doesn't need to wait for the lookups and printing of information.
Mark Wielaard [Sat, 21 Dec 2013 20:56:35 +0000 (21:56 +0100)]
libdwfl: Make sure to set the CFI return register only once (for ppc64).
On PPC64 there are two DWARF registers numbers that can represent the
same register. If that register is the CIE return register then we only
want to set it once. The second setting will confuse the unwinder.
Mark Wielaard [Fri, 20 Dec 2013 09:09:12 +0000 (10:09 +0100)]
libdwfl: Add dwfl_getthread_frames.
dwfl_getthread_frames is a convenience function for when the user is only
interested in one specific thread id of a process. It can be implemented by
a simple wrapper function that removes an extra callback layer just to
filter on thread id. But it also provides an optimized path to getting
access to just one particular Dwfl_Thread of the Dwfl process by providing
and (optional) new callback for the state provider. The pid_thread_callbacks
now provide an (optional) pid_getthread that doesn't need to travers all
threads anymore. Which is implemented for the linux-pid-attach provider.
stack now uses this to implement a new '-1' option that shows just one
specific thread of a process.
Mark Wielaard [Sat, 21 Dec 2013 18:39:19 +0000 (19:39 +0100)]
tests: backtrace.c only check we caught the last instruction on x86_64.
On some architectures gcc might introduce some "padding instructions"
at the end of the function (like on ppc64). So only assert we are at the
last instruction of backtracegen if on x86_64 native. In theory the assert
could even fail on that architectures, but in practice it doesn't and it
is a nice test to have.
Mark Wielaard [Fri, 20 Dec 2013 23:04:21 +0000 (00:04 +0100)]
libdwfl: Correct nested asprintf result check in report_kernel_archive.
Because of wrongly placed parens the result of only one asprintf call
was checked correctly. Causing dwfl_linux_kernel_report_offline to return
ENOMEM. Rewrite nested if unlikely check into separate if statements to
make clear what is actually being checked and what the actual unlikely
condition is.
Reported against systemtap "build-id difficulties with hand-built kernels"
https://sourceware.org/bugzilla/show_bug.cgi?id=16358
Reported-by: Crestez Dan Leonard <lcrestez@ixiacom.com> Signed-off-by: Mark Wielaard <mjw@redhat.com>
Mark Wielaard [Thu, 19 Dec 2013 15:11:19 +0000 (16:11 +0100)]
backends: aarch64 always has _GLOBAL_OFFSET_TABLE_ point to .got[0].
Like some other architectures aarch64 has a special rule for the
_GLOBAL_OFFSET_TABLE_ symbol. Even if there is a .plt.got section the symbol
value still points to the start of the .got section. This is also what the
dynamic linker expects.
See https://sourceware.org/ml/libc-ports/2013-06/msg00057.html
Mark Wielaard [Wed, 18 Dec 2013 10:05:54 +0000 (11:05 +0100)]
libdwfl: Introduce dwfl_module_getsym_info and dwfl_module_addrinfo.
Some arches like ppc64 use function descriptor values instead of function
addresses causing matching of names and addresses to fail when using
dwfl_module_getsym or dwfl_module_addrsym.
Add ebl hook to resolve any function descriptor values found in non-ET_REL
modules.
The new function dwfl_module_getsym_info doesn't adjust the symbol value
in any way, but returns the adjusted and/or resolved address associated
with the symbol separately. The new function dwfl_module_addrinfo resolves
against both the address associated with the symbol (which could be the
function entry address) value and the adjusted st_value. So that it is
easy to resolve and match either function descriptors and/or function
entry addresses.
Since these new functions also return more information they replace the
dwfl_module_getsym_elf and dwfl_module_addrsym_elf functions that never
made it into a released elfutils version.
addr2line and readelf now use the new functions when looking up functions
names. addr2line will now also display the section the address was found
in when given -x.
Extra testcases were added for both addr2line and the dwflsyms testscase.
Mark Wielaard [Wed, 18 Dec 2013 17:02:42 +0000 (18:02 +0100)]
backends: Avoid type-punning issue in s390_set_initial_registers_tid.
Use union to avoid type-punning when assigning a double to a Dwarf_Word.
gcc complains otherwise. error: dereferencing type-punned pointer will
break strict-aliasing rules.
Masatake YAMATO [Tue, 17 Dec 2013 03:03:29 +0000 (12:03 +0900)]
stack: show binary and source file names where a function is defined
This patch adds the module and source file information to the
each stack trace line. `-m' is for module file information
and `-s' is for source file information. `-v' is for both and more.
This is based on private discussion with Jan Kratochvil
<jan.kratochvil@redhat.com>.
In v2 patch, `-s' and `-m' options are introduced instead
of using `-v' repeatedly as suggested by Mark Wielaard <mjw@redhat.com>.
In v3 patch `-a' is added as extra option and source lines are
printed on their own line.
Signed-off-by: Masatake YAMATO <yamato@redhat.com> Signed-off-by: Mark Wielaard <mjw@redhat.com>
Mark Wielaard [Mon, 16 Dec 2013 12:28:59 +0000 (13:28 +0100)]
libdwfl: Add dwfl_module_getsymtab_first_global.
New function that provides the index after the last non-local symbol as
returned by dwfl_module_getsym and dwfl_module_getsym_info. Allows users to
first search through all global symbols before searching the local symbols
in the table like dwfl_module_addrsym and dwfl_module_addrsym_info do as
optimization.
Mark Wielaard [Sat, 14 Dec 2013 14:02:56 +0000 (15:02 +0100)]
libdwfl: Fix various frame related memory leaks.
The result of dwarf_cfi_addrframe should have been freed when done.
Dwfl_Module cached the reloc_info and the eh_cfi it which also should
have been released when disposing of the module.
Mark Wielaard [Fri, 13 Dec 2013 21:42:46 +0000 (22:42 +0100)]
tests: Disable valgrind for core dumping and for self-introspecting tests.
The only tests that fork and exec are those that will then try to ptrace
attach the child for unwind testing. That won't work when the child is
also running under valgrind. So remove --trace-children=yes. Also disable
valgrind while dumping a core or for tests that try to inspect their own
state. They will get confused otherwise finding pieces of valgrind in
their maps.
Note we still seem to hit the following valgrind bug because the way we
map and unmap some shared libraries gets valgrind confused:
https://bugs.kde.org/show_bug.cgi?id=327427
Josh Stone [Fri, 13 Dec 2013 00:34:15 +0000 (16:34 -0800)]
lib: Avoid the hash-lookup division if possible
For Dwarf_Abbrev codes, the most common case is that they're packed at
the low end, saving uleb128 encoding size. Since the hash table is
always resized to be no more than 90% full, those codes are always less
than the table size, and dividing for the remainder is unnecessary.
Dwarf_Dies are frequently created anew, and need to find abbrev each
time, so even that one division becomes a noticeable hotspot. This
patch adds a branch to avoid it, which is very predictable for the CPU.
Josh Stone [Mon, 9 Dec 2013 21:52:10 +0000 (13:52 -0800)]
libdw: Add an inlined fast path for __libdw_form_val_len
Quite a few DW_FORMs have a fixed length for their data, and we can
easily represent these in a small lookup table. The rest of the forms
are left in the old function to compute as needed. Combined with
inlining, this takes care of many forms with fewer branches and without
any call. (It's conceivable that a smart compiler could make a similar
lookup transformation from the large switch itself, but GCC doesn't.)
Mark Wielaard [Mon, 9 Dec 2013 15:33:26 +0000 (16:33 +0100)]
libdw: Handle empty location expression for (indirect) DIE locations.
When dwarf_getlocation_implicit_pointer and dwarf_getlocation_attr
refer to a DIE that doesn't contain a DW_AT_location then don't generate
an error, but return an empty location expression to signal the actual
value pointed to is not available. This isn't invalid DWARF. Also make
sure that __libdw_intern_expression handles empty location expressions.
Mark Wielaard [Thu, 5 Dec 2013 14:26:51 +0000 (15:26 +0100)]
tests: SKIP backtrace-data and backtrace-dwarf on unsupported arches.
run-backtrace-data.sh did check stderr whether the arch was unsupported
but the test didn't print that message yet. backtrace-dwarf did print
the message but was missing a check_unsupported test. Also add an explicit
check_main test for backtrace-dwarf.
Mark Wielaard [Wed, 27 Nov 2013 15:45:44 +0000 (16:45 +0100)]
libdwfl: Add dwfl_module_addrsym_elf and dwfl_module_getsym_elf.
Introduce two new functions that also return the elf associated with a
symbol to make symbol section indexing work for non-special sections.
Simplify code by removing dwfl_file where appropriate and just track Elf
directly. Document limitations of shndx with existing dwfl_module_addrsym
and dwfl_module_getsym. Extend dwflsyms testcase to check some more symbol
and section (index) properties.
Mark Wielaard [Tue, 26 Nov 2013 14:13:22 +0000 (15:13 +0100)]
Mark new dwfl functions with version ELFUTILS_0.158.
It took two versions to get the new thread state and unwind dwfl functions
in. Make sure they carry the latest elfutils symbol version in which they
were actually added.
Mark Wielaard [Fri, 15 Nov 2013 23:11:38 +0000 (00:11 +0100)]
tests: Fix broken dwflsyms tests, extend test and add prelink tests.
The two test files that contains minisymtab tables and the expected test
output were wrongly generated. Also prelink tests were missing to check
that a prelinked main ELF file with separate debug or minisyms generated
correctly adjusted symbol values. The dwflsyms test was also extended to
check print and check the result of dwfl_module_relocate_address.
Reported-by: Josh Stone <jistone@redhat.com> Signed-off-by: Mark Wielaard <mjw@redhat.com>
Mark Wielaard [Fri, 8 Nov 2013 18:28:16 +0000 (19:28 +0100)]
libelf: Only memcpy ehdr back when not already directly mmapped.
When the elf file is mmapped ehdr can still point to ehdr_mem if the
address is unaligned. In such cases we do need to memcpy the ehdr back
to the file. We shouldn't when ehdr already comes directly from the mmapped
file, because that would cause an memcpy call with overlapping addresses
(ehdr being copied directly over itself).
Mark Wielaard [Sat, 9 Nov 2013 15:45:22 +0000 (16:45 +0100)]
Fix some (harmless) cppcheck warnings.
[dwarf_getaranges.c:149]: (warning) Ineffective statement similar to '*A++;'.
Did you intend to write '(*A)++;'?
There was already an XXX statement that we weren't using the result.
Explicitly read the segment_size and check it is zero. And report an
error if it isn't, since we aren't prepared to handle such a case.
cppcheck is wrong. tmpbuf is initialized in the snprintf call whose result
is use in the same memcpy call. It does make the code less readable and
harder to understand. So explicitly split the snprintf and memcpy calls.
The cnt variable was only used for this assert sanity check.
But it is bad style to do have side effects inside an assert statement.
Increase cnt after the assert.
Reported-by: David Binderman <dcb314@hotmail.com> Signed-off-by: Mark Wielaard <mjw@redhat.com>
Mark Wielaard [Tue, 5 Nov 2013 15:27:32 +0000 (16:27 +0100)]
libelf: Write all section headers if elf flags contains ELF_F_DIRTY.
When ehdr e_shoff changes, elf flags is set dirty. This indicates that
the section header moved because sections were added/removed or changed
in size.
Reported-by: Jiri Slaby <jslaby@suse.cz> Signed-off-by: Mark Wielaard <mjw@redhat.com>
Mark Wielaard [Tue, 5 Nov 2013 10:27:19 +0000 (11:27 +0100)]
tests: allfcts.c (main): Correct dwarf_getfuncs return value check.
The return value of dwarf_getfuncs is a ptrdiff_t that is zero on success,
or non-zero (an offset to continue the search) when the callback returned
DWARF_CB_ABORT or on error. When an error occurs dwarf_errno is set.
Mark Wielaard [Mon, 4 Nov 2013 14:19:32 +0000 (15:19 +0100)]
README: Document that tools and libraries should be upgraded together.
On some distributions it was possible to upgrade separate parts of elfutils
individually. That causes hard to debug issues since some tools (e.g. readelf)
and some libraries (e.g. libdw) depend on libelf internal data structures,
which could be different between versions (or when configured with different
flags). So explicitly document that they should always be upgraded together.
Jan Kratochvil [Wed, 30 Oct 2013 09:48:20 +0000 (10:48 +0100)]
Fix executable_for_core for non-dwfl_standard_argp
./
2013-10-30 Jan Kratochvil <jan.kratochvil@redhat.com>
* NEWS (Version 0.158): New.
libdw/
2013-10-30 Jan Kratochvil <jan.kratochvil@redhat.com>
* libdw.map (ELFUTILS_0.158): New.
libdwfl/
2013-10-30 Jan Kratochvil <jan.kratochvil@redhat.com>
* argp-std.c (parse_opt): Use executable parameter of
dwfl_core_file_report.
* core-file.c (dwfl_core_file_report): Add parameter executable. Set
it to DWFL. Add NEW_VERSION for it.
(_compat_without_executable_dwfl_core_file_report): New. Twice.
* libdwfl.h (dwfl_core_file_report): Add parameter executable, update
the function comment.
Signed-off-by: Jan Kratochvil <jan.kratochvil@redhat.com>
Mark Wielaard [Thu, 10 Oct 2013 09:40:12 +0000 (11:40 +0200)]
libdw: Handle dwz multi files correctly in dwarf_getfuncs.
Don't use DIE offsets, but use their addresses to make sure they are unique.
Include test cases where main and alt file have subprograms at same offsets.
Reported-by: Josh Stone <jistone@redhat.com> Signed-off-by: Mark Wielaard <mjw@redhat.com>
Mark Wielaard [Tue, 15 Oct 2013 21:53:32 +0000 (23:53 +0200)]
libdwfl: Don't report and abort on non-file mappings in maps.
proc_maps_report, used for -p or -M, could report modules with names
that were not absolute file names (and not the special vdso marker).
dwfl_linux_proc_find_elf would abort on such names. This isn't a very
nice thing to do in a library. Make sure only real (absolute) file
mappings are reported and don't abort when unexpected file names are
found, just report failure.
Test case with some examples as found in Linux /proc/PID/maps added.
Mark Wielaard [Sun, 6 Oct 2013 15:04:07 +0000 (17:04 +0200)]
backends: ppc_abi_cfi reg1 use DW_CFA_val_offset not DW_CFA_val_expression.
Register rules using expressions are stored using an offset from the
start of the .eh_frame or .debug_frame ELF section data. Since abi_cfi
rules aren't stored in those ELF sections they should use neither
DW_CFA_expression nor DW_CFA_val_expression. The only backend that used
DW_CFA_val_expression was ppc_cfi.c. It was easier to express the same
rule using DW_CFA_val_offset than to change the code to handle register
rules using expressions. On most architectures this did work by accident.
See the definition of struct dwarf_frame_register value in libdw/cfi.h to
see why. But on ia64 the abi_cfi data and actual frame data were placed
too far apart and caused a crash in tests/run-addrcfi.sh for ppc32.
Josh Stone [Thu, 3 Oct 2013 19:38:25 +0000 (12:38 -0700)]
libdw: Make sure that every debug_types sig8 is hashed
When dwarf_formref_die can't find a sig8 in the hash, it walks
__libdw_intern_next_unit, and was then adding those to the hash.
However, if dwarf_offdie_types is called earlier, which also uses that
next_unit, then they are missed from the hash (and never revisited).
This patch makes __libdw_intern_next_unit do the sig8 hash insert, so no
type unit is ever missed.
Mark Wielaard [Sun, 29 Sep 2013 22:39:07 +0000 (00:39 +0200)]
libdw: Fix compiler warnings on 32-bit.
Don't cast directly to/from Dwarf_Word (uint64_t) to/from pointers,
but use uintptr_t as intermediary to prevent cast to pointer from
integer of different size warnings.