Commit graph

21 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
01040b10b2 Fix handling of ro segments for embedded libs.
When a shared library is loaded directly from an apk, the new way the
linker splits a shared library into a read-only and execute segment
broke unwinding. Modify the code to handle this case.

Other changes:
- Modify the algorithm for finding read-only map entries. Before, the code
  would search the entire map for the closest offset. Now it simply looks
  at the previous map. I did this because the old code was too lenient and
  might still work even if the linker changes. I want this to break if the
  linker behavior changes so that I can analyze the change.
- Update the tools to use PTRACE_SEIZE instead of PTRACE_ATTACH since
  PTRACE_ATTACH doesn't work in all cases.
- Small refactor of the GetFileMemory function.
- Add new unit test cases and new offline unwind test cases.

Bug: 120618231

Test: Ran new unit tests, ran original failing test.
Change-Id: I4bade55cf33220d52f1d5e9b0cbbbcc8419669d4
2018-12-11 19:16:29 -08:00
Christopher Ferris
5565906732 Remove overly restrictive check for memory.
Remove unit test that is not needed any more.

Slightly reorganize the code around the changed check.

Bug: 77958880

Test: Ran backtrace tests with new linker options.
Test: Ran libunwindstack unit tests.
Change-Id: Ie95707323fb51616a093cafb83e0343fa24e7e61
2018-11-15 14:06:26 -08:00
Christopher Ferris
4568f4bc0f Verify that the elf matches the expected arch.
To avoid a case where a malicious app might try and trick the system to
create an elf and register object that mismatches, always verify that they
are the same arch.

Test: Ran unit tests.
Change-Id: I66978e9e02f8e4f396856912e7019528ead4838e
2018-10-29 18:08:09 -07:00
Christopher Ferris
e8c4ecf49e Always init gnu_debugdata interface if it exists.
Every real call always sets the value to true to init the gnu_debugdata
interface, so remove this parameter.

Test: Builds, unit tests pass.
Change-Id: I07fb3adbbd5b65b51c0dc7608561e820a5095051
2018-10-23 12:13:54 -07:00
Christopher Ferris
a2f38f1bde Fix up the definition of Elf::GetInfo.
The function should have returned a bool, so now it does.
Also use a different lighter weight function for validating the elf
in one place that didn't need to keep the max size value.

Test: Unit tests pass.
Change-Id: Ibde674e608091ba04abf22fad2fdc4dbdb2c6e73
2018-10-12 11:29:06 -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
d9575b668b Modify elf cache to handle elf_offsets properly.
Bug: 73498823

Test: All unit tests pass.
Test: Simpleperf run that previously failed, passes now.
Change-Id: Iff3a1f2f641a46ab9a0326579af3649f0c76fc65
2018-02-16 14:52:38 -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
e7b6624c3f Fix issues in libunwindstack.
- Add a load_bias field in MapInfo so that it can be loaded offline,
  and also so it can be cached.
- Add an Add function to the Maps class so that it's possible to manually
  create a map.
- Remove the OfflineMaps class since I haven't found a reason for this to
  exist.
- Add a pointer to the gnu debugdata compressed section in the interface
  itself and modify the step path to try eh_frame, then debug_frame, then
  gnu_debugdata. This way arm can add exidx as the last step behind
  gnu_debugdata. Add an offline test to verify the order of unwind.
- Fix x86_64_ucontext_t since it was a different size on 32 bit and 64 bit
  systems.

Test: Pass new unit tests.
Change-Id: I978b70d6c244bd307c62a29886d24c1a8cb2af23
2017-12-15 11:17:45 -08:00
Christopher Ferris
b7de5f5429 Demand read load bias for a map.
Add a static GetLoadBias method to the Elf object that only reads just
enough to get the load bias.

Add a method to MapInfo that gets the load bias. First attempt to get
it if the elf object already exists. If no elf object was created, use
the new static method to get the load bias.

In BacktraceMap, add a custom iterator so that when code dereferences
a map element, that's when the load bias will be retrieved if it hasn't
already been set.

Bug: 69871050

Test: New unit tests, verify tombstones have non-zero load bias values for
Test: libraries with a non-zero load bias.
Change-Id: I125f4abc827589957fce2f0df24b0f25d037d732
2017-12-05 13:12:47 -08:00
Treehugger Robot
b4fda18d83 Merge changes I53c2c560,I7d845ac5,I8b11d923
* changes:
  unwindstack: rename Memory::ReadPartially to Read.
  unwindstack: rename Memory::Read to ReadFully.
  unwindstack: add Memory::ReadPartially.
2017-12-02 00:12:21 +00: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
Josh Gao
29c5378e91 unwindstack: add Memory::ReadPartially.
Add a way to read while allowing for partial reads.

Test: new tests added to libunwindstack_test, ran 32/64 on hikey960, sailfish
Test: ran unwind on hikey960/sailfish
Change-Id: I8b11d9230fcd3122148ef3f980863ac1404ad70a
2017-11-15 15:38:13 -08: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
5f118519fd Add a method to share the process memory object.
New function to create the process memory object. This allows for
a future where different remote process memory objects could be created
depending on the way remote memory can be created. Even different local
memory objects that access memory without doing any checks.

It also allows MemoryRange objects to share one single process memory object
and could help if the process memory object caches data.

Small changes to MapInfo::CreateMemory to when some errors are detected.
- Always check if the map is a device map, instead of only if the name
  is not empty.
- Check if a memory map is readable before creating the memory from process
  memory.

Bug: 23762183

Test: Ran unit tests, unwound on device using the new code.
Change-Id: I12a93c2dc19639689a528ec41c67bfac74d431b3
2017-09-05 14:30:22 -07:00
Christopher Ferris
3f805ac3f8 Add proper support for embedded elf files.
- Add a method to get the max size of an elf file by reading the
  section header offset + size. This will properly map an elf
  file embedded into an apk, instead of just mapping in what is done
  by the dynamic linker. It does assume that the section headers are
  at the end of the elf file.
- Add new tests for the above functionality.
- Update the unwind_symbols tool to take an address for finding a
  function instead of dumping the entire symbol table.

Bug: 23762183

Test: Unit tests pass, unwind through the camera process and verify
Test: the GoogleCamera.apk shows some function names.
Change-Id: I00c021680fe1d43b60d652bf91bbf6667d9617be
2017-08-30 15:50:11 -07:00
Christopher Ferris
d226a51409 Make the library usable as a library.
- Add namespace unwindstack everywhere so that it's easier for other
  code to use the library.
- Move some of the header files into include/unwindstack so that they
  can be exposed.
- Modify the headers so that only a limited number need to be exposed.
- Update the tools to use the new headers.
- Add a GetLoadBias() call on the Elf object. This prevents the need
  to get the interface object out of the Elf object.
- Move the GetRelPc() call out of the Reg class, to the Elf class. It's
  not always the case that a Reg object will be around when you want to
  get a relative pc. The tests for this moved to ElfTest.cpp.

Bug: 23762183

Test: Unit tests pass.
Change-Id: Iac609dac1dd90ed83d1a1e24ff2579c96c023bc3
2017-07-14 12:20:23 -07:00
Christopher Ferris
570b76f04f Init .gnu_debugdata in GetElf().
- Modify the tests to share some code for the generation of the elf data.
- Move the gnu_debugdata files into a separate directory.
- Add tests for GetElf().
- Move the CreateMemory() tests and the GetElf() tests into separate files.

Bug: 23762183

Test: New unit tests pass.
Change-Id: Ie3380296bb49753c2ac8801cfa11f93d6ff7121d
2017-06-30 17:24:58 -07:00
Christopher Ferris
0d7cf3e8dd Add function definitions for MapInfo.
Bug: 23762183

Test: All new unit tests pass.
Change-Id: I26a1f6e84bf1a02447eff48ff6e35068554330fe
2017-04-19 15:48:02 -07:00