Commit graph

23 commits

Author SHA1 Message Date
Christopher Ferris
eb0772ff4a Add support for UnwinderFromPid object.
This object is able to be easily used from other code and can be used
to replace the libbacktrace calls in other parts of the platform.

Also, demangle the function names when calling FormatFrame.

Bug: 120606663

Test: Unit tests pass, debuggerd using this code directly passes unit
Test: tests.
Change-Id: Ifd8cf9bdd89174c1736810711d20e9f37f29b1bf
2019-01-16 15:13:25 -08:00
Christopher Ferris
a09c4a6ff2 Fix offsets when shared lib split across maps.
The linker was modified so that a shared library has a read-only
map, then a read-execute map to represent the whole shared library.
When backtraces are created, then the offsets are set incorrectly
for backtraces. For example, all backtraces wind up with an offset now,
and a shared library loaded out of an apk shows the wrong offset.

Changes:
- Fix the FormatFrame function which was putting the offset before the
  map name.
- Refactor the Maps and MapInfo classes to keep track of the previous map
  instead of all maps. This was the only map that was ever needed.
- Modify the unwind_for_offline tool to capture both the read-only and
  read-execute map when using the read-only segment option.

Bug: 120981155

Test: Updated unit tests pass.
Test: Unwinds on device don't show the offsets everywhere any more.
Change-Id: I75b3727221be9c20316dfdcd7a1c6d242d7ce948
2018-12-18 11:22:14 -08:00
Christopher Ferris
065f156195 Do not remove speculative frames in all cases.
If the first frame of an unwind is a totally invalid pc that's not in
any map, a speculative frame is added. Rather than deleting this frame
if no more unwinding is possible, leave it. This fixes a case where
the only frame you get is an invalid one, but the speculative frame
winds up in a shared library or somewhere else and gets removed.

Bug: 120505086

Test: New unit tests to catch this case pass.
Test: Verified original crashing program now emits two backtrace lines.
Change-Id: I088dff21c057386dcdaeb3fc2578b24322683bd0
2018-12-13 10:41:08 -08:00
Christopher Ferris
9d0ad238ca Fix problem adding too many frames.
When adding a frame with a dex pc, two frames will be added total. However,
if there is only enough room for a single frame, two get added any way.

Only add a single frame in this case, and add a unit test for this case.

Test: Passes unit tests.
Change-Id: If320584b126967a042c623d8fdf3f51dbc1c2251
2018-10-12 16:36:35 -07:00
Christopher Ferris
9d5712c123 Implement support for linker rosegment option.
The rosegment linker option results in two maps containing the elf data
existing. One is an execute map where the code lives, and the other is the
read-only segment which contains the elf header information. If the file
backing a shared library in memory is not readable, then the new code
will attempt to find the read-only map that has the same name as the
current execute segment, and that is at offest zero in the file.

Add new unit tests for this functionality.

Add the missing MapInfoCreateMemoryTest.cpp to the list of tests.

Bug: 109657296

Test: Pass new unit tests.
Test: All unit libbacktrace/libunwindstack tests pass with rosegment enabled.
Change-Id: If8f69e4a067d77b3f2a7c31e2e5cd989a0702a8c
2018-10-03 20:48:45 -07:00
Christopher Ferris
b22451f178 Merge "Always set the sp reg to the cfa for DWARF." 2018-03-19 17:48:59 +00:00
David Srbecky
e62f4c4d4c Don't include map name if the consumer doesn't want names.
Simpleperf doesn't need the name, so avoid the string copies.

This seems to speed up simpleperf by around 20%.

Test: backtrace_test libunwindstack_test
Change-Id: Iba064c7078f7e5ecfc9d8539ba09c14495a52158
2018-03-19 11:20:00 +00:00
Yabin Cui
11e96fe48a Always set the sp reg to the cfa for DWARF.
There are a few places where it is assumed that this register is
set to the cfa value when interpreting DWARF information.

Add a testcase for unwinding art_quick_osr_stub on ARM.

Bug: 73954823

Test: Ran libunwindstack/libbacktrace unit tests.
Test: Random debuggerd -b of process on a hikey.
Test: Ran the 137 art test on host.
Change-Id: Ida6ccdc38c3cfeea6b57fe861a0cc127b150b790
2018-03-16 23:38:07 -07:00
Christopher Ferris
e4b3a6aaa4 Add ability to skip function name resolution.
Bug: 73558129

Test: New unit tests pass.
Change-Id: I3a6b17d2590aacb367ab3e3a51cc85be73ad64eb
2018-02-20 17:10:23 -08:00
Christopher Ferris
e4df5f5ab5 Fix computation of pc.
Fix the case where a non-zero load bias or a non-zero elf offset causes
the pc to be set incorrectly.

Add unit tests for these cases.

Bug: 73172903

Test: Ran unit tests.
Test: Ran the unit tests from the simpleperf CL that detected the failure.
Change-Id: Id8802c00b34c66875edd4926a20c5fccd2bb7d72
2018-02-12 14:06:13 -08:00
Christopher Ferris
a8c397317d Clear dex pc to avoid duplicate frames.
Bug: 73228466

Test: Passes new unit tests, fails without change.
Change-Id: I3660bfd16cbf2d5cf96490ca7714bc5f97914884
2018-02-12 08:46:19 -08:00
Christopher Ferris
e762f1f193 Check that dex pc is in a valid map.
Add new unit tests for dex pc being non-zero.

Bug: 73004673

Test: Ran unit tests.
Test: Ran art 137-cfi test on host for interpreter.
Change-Id: I09bbf96d0ed65fc1e5896e4ab2bc67867e3b7fdb
2018-02-06 15:00:04 -08:00
Christopher Ferris
0b79ae155f Add a global elf cache.
Bug: 65682279

Test: Ran new unit tests.
Change-Id: I19c64614b2b11a27f58204d4cc34913c02e04c36
2018-01-25 17:11:33 -08:00
Christopher Ferris
2fcf4cf13e Add error propagation into Unwinder/Elf objects.
The backtrace offline code uses these error codes to diagnose errors.
In addtion, I've had cases where seeing these errors would help diagnose
failures.

This also allows us to add a few features to indicate why an unwind
terminated (such as max frames exceeded).

Bug: 65682279

Test: Updated unit tests pass.
Change-Id: If82b5092698e8a194016d670efff1320f9b44d50
2018-01-24 17:50:46 -08:00
Douglas Leung
61b1a1ae77 Add libunwindstack support for Mips and Mips64
This patch requires v3.18 kernel or above, because v3.10 kernel
has a bug (as of 8/1/2017) in the ptrace(GETREGSET) function for mips
and mips64.

Change-Id: I004c1fa190193eebe1c84440b366289122a6bd8a
Signed-off-by: Douglas Leung <douglas.leung@mips.com>
Signed-off-by: Dejan Jovicevic <dejan.jovicevic@mips.com>
2017-12-11 15:44:40 +01:00
Christopher Ferris
02fdb569f6 Do not check arch for format check.
Use a generic check if the address is 32 bits when using the default
formating of a backtrace line instead of an arch check.

Test: New unit tests pass.
Change-Id: Id609abc037d7b437a02d52763aa91fbefe5f4d5b
2017-12-08 15:44:14 -08:00
Christopher Ferris
d06001d6e0 Split arch data into separate files.
Add an ArchEnum to express the arch of the Elf objects and Reg objects.

Split out the regs code into per arch pieces and generic parts.
Also, split out the header files for each arch.

Do not break out the test code yet, there isn't enough and it doesn't
help to maintain the tests.

Test: libunwindstack/libbacktrace/debuggerd unit tests pass.
Test: Running debuggerd -b <PIDS> yields valid data on bullhead.
Change-Id: If61f6c730c9ff2249f986b41de8c4d62f7158325
2017-12-06 16:01:40 -08:00
Christopher Ferris
be788d891d Allow multiple threads sharing a map to unwind.
Add a mutex in MapInfo, and a mutex in Elf. Lock the creation of an Elf
file using the MapInfo mutex, and lock when calling Step, GetFunctionName,
or GetSoname since they can modify information in the object. It might
be beneficial to use a fine grained lock in the future.

Change the Maps object to contain a vector of MapInfo pointers rather
than the total objects. This avoids copying this data around.

Add a test to libbacktrace to verify that sharing a map while doing
unwinds in different threads works.

Add concurrency tests in libunwindstack to verify the locking works.

Add always inline to the RegsGetLocal arm and aarch64 functions. I had
a case where clang did not inline the code, so make sure this is specified.

Bug: 68813077

Test: New unit tests to cover the case. Passes all unit tests.
Test: Ran a monkey test while dumping bugreports and verified that
Test: no crashes in libunwind.
Test: Remove the locking and verified that all of the concurrenty tests fail.
Change-Id: I769e728c676f6bdae9e64ce4cdc03b6749beae03
2017-11-28 15:20:38 -08:00
Christopher Ferris
fda7edd13e Move sp/pc not changing check into Unwinder.
Remove this check from the DwarfSection class.

Rather than have every step function make the check, doing it at the
top level avoids having every function do the same check.

Bug: 68167269

Test: New unit tests, ran debuggerd -b on processes.
Change-Id: I23b7c799faaf26c93c1b72848df18c78de6c42fb
2017-10-31 17:51:02 -07:00
Christopher Ferris
1cb84cea2f Add an interface for stopping in certain maps.
Also, change the std::set parameters to std::vector. As jmgao points out,
a small std::set is not really the best choice for performance reasons.

Test: All unit tests pass, enabled the new unwinder and did a kill -3 on
Test: an android process.
Change-Id: I81227d7b79a9b7cf1d54fb0e3331d3cf4d4d3c4f
2017-10-24 15:45:59 -07:00
Christopher Ferris
e69f470933 Multiple bugfixes, small restructuring.
- Move the load bias stored out of ElfInterface into Elf. For the compressed
  sections, the load bias was not the same as the data from the uncompressed
  section.
- Move the initialization of the compressed section into Init. It was too easy
  to forget to call the init of the compressed section.
- Do not automatically add in load bias to the pc before calling ElfInterface
  code. Do all of the pc manipulations in the Elf object.
- Change the interface GetFunctionName code to pass in the load_bias instead
  of modifying the pc inside the code.
- Modify the Step function to pass in the elf offset, not add it to the pc.
  It is necessary to have two different relative values when executing the
  Step: a pc that is relative to the beginning of the elf for the reading data
  the actual instructions when trying to determine if this is in a signal
  frame, and a pc that is relative to the map for finding the appropriate
  unwind information.
- Add a feature to Unwinder so that an unwind can be stopped if it ends up
  in map that has a specified suffix. This is so that the ART unwinding
  code doesn't require skipping the compressed section. Instead, stop at
  if trying to unwind through a known suffix code that means the code is
  in java code. This is important because the compressed section data is
  not only used by the jave compiled code, so that will continue to work.
- Fix tests for restructuring, add new tests for new functionality.

Test: Ran art test 137-cfi using new unwinder as default.
Test: Ran new unit tests.
Change-Id: I42e658c64c5e14f698ba34944a3043afac967884
2017-10-20 11:15:32 -07:00
Christopher Ferris
c5a3baa5e3 Fix setting of map_offset in frame data.
The old code set the map_offset from elf_offset, but it should have used
offset. Without this change, frame data is not properly shown for shared
libraries in an apk.

Add new unit test to verify that map_offset is set properly.

Test: Ran new unit tests.
Change-Id: I2fe5abda23a5de51c8fcf9361ba9350b80446f15
2017-10-17 18:42:03 -07:00
Christopher Ferris
f6f691b63c Update the Unwinder object and add tests.
Changes:
- Remove unused GetReturnAddressFromDefault function and tests.
- Modify the unwinder to stop when a pc/sp in a device map.
- Modify the unwinder to skip initial frames based on map names.
- Unit tests that exercise all of the paths in the unwinder code.
- Move the test Elf/ElfInterface objects into their own file.
- Update RegsFake to handle extra cases.
- Modify libbacktrace code to use this unwinder.

The new unwinder does not implement the ignore frame functionality since
this is not used very often and is better implemented using a skip frames
in named libraries functionality.

Test: Ran new unit tests, ran backtrace tests.
Change-Id: Ifd65e9acd66ac5e2d0e04bd32a9ad870b54610ff
2017-10-04 14:40:14 -07:00