Commit graph

178 commits

Author SHA1 Message Date
Christopher Ferris
6dbc28ece3 Fix null pointer dereference in RegsArm.
Fix RegsArm::GetPcAdjustment to check for an invalid elf before trying
to read memory.
Modify the tests for this so it crashes without this change.

Also modify the GetPcAdjustment for all different architectures so
that unless the relative pc is too small, it will return the minimum
amount that should be adjusted. This is to handle cases where we still
want to adjust the pc but it's in an invalid elf. Mostly this is for
handling cases when the pc is in jit gdb debug code so that we use the
right unwind information.

Bug: 77233204

Test: Passes unit tests for libbacktrace/libunwindstack.
Change-Id: Id73609adaf3b80a583584441de228156fec3afa7
2018-03-28 17:21:01 -07:00
Yabin Cui
d5b22c5f04 Support a map that represents gdb jit elf data.
Changes:
- Add a new flag to the libbacktrace and libunwindstack map data.
- Modify the unwinder to handle this map to use the raw pc when stepping.
- Add new unit tests for this case.

Bug: http://b/73127105
Test: Run simpleperf to unwind through jit symfiles.
Test: Run new unit tests.
Test: Run 137-cfi test on host.
Change-Id: I10bc0410680accc6d35fe51e9f1098911f667e01
2018-03-21 17:23:54 -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
e8d1b75c0c Merge "Don't include map name if the consumer doesn't want names." 2018-03-19 11:23:10 +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
704ec9adba Handle ARM thumb function symbols.
ARM thumb functions will have the zero bit set to one, which can cause
function name lookups to fail.

Add an ARM special GetFunctionName that handles this condition.

Fix a couple of the function offsets in unit tests.

Bug: 74844137

Test: Ran unit tests.
Test: Ran debuggerd -b on processes on a bullhead device.
Change-Id: Ibd407db34eaaa641f91fdb4f589c44a0dcc0216a
2018-03-15 15:14:18 -07:00
David Srbecky
ff9b1db67c Merge "Remove libunwindstack_dex" 2018-03-13 11:31:22 +00:00
David Srbecky
18121dc4d0 Remove libunwindstack_dex
Thanks to David Sehr's recent work the isolation is no longer needed.

Bug: 72216369
Test: backtrace_test
Test: libunwindstack_test
Test: testrunner.py -t 137
Change-Id: I0f430e0c18bdbc83c5584a2461ebbcc7eeb91313
2018-03-12 20:13:21 +00:00
David Srbecky
3692f25199 Don't implicitly dereference DW_CFA_def_cfa_expression.
The specification isn't entirely clear. Follow the gdb interpretation.

Bug: 73954823
Test: libunwindstack_test
Change-Id: If72717f54eed84e442f43af19e615143a9796b68
2018-03-08 14:44:11 -08:00
Christopher Ferris
81744ac69f Remove leftover commented out code.
Test: Compiles.
Change-Id: Icf1cf1640e76450d5327395eb94dbd87d2faac36
2018-03-06 11:24:55 -08:00
David Srbecky
8fbdbba9f5 Merge "Adjust DEX file reading to follow layout changes in ART." 2018-02-23 17:53:23 +00:00
Christopher Ferris
a2ec50bf57 Change the GetAdjustedRelPc to GetPcAdjustment.
This cleans up a bit of the Unwinder code to make it clear what's
going on.

Modify the offline unit tests to verify the pc and sp to make sure
that those values get computed correctly.

Test: Passes unit tests.
Test: Passes 137-cfi art tests.
Change-Id: I0787a1d77b8726d3defd08f31c7476f6798f8d0d
2018-02-21 20:16:39 -08: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
Treehugger Robot
0609e8d231 Merge "bpfmt." 2018-02-20 02:02:59 +00:00
Elliott Hughes
dc699a269f bpfmt.
Bug: N/A
Test: builds
Change-Id: I89ad00e1c4c7e0767bc80a7ac7935a4d55e090ac
2018-02-16 17:58:14 -08: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
David Srbecky
4015ef411a Adjust DEX file reading to follow layout changes in ART.
I have changed the root DEX debug descriptor in ART to more
closely follow the JIT debug descriptor. Add the appropriate
offsets to correctly fetch the linked list head for DEX files.

Test: testrunner.py -t 137
Test: libunwindstack_test
Change-Id: I90402befc88fec42658f7330d51ee79756a7f872
2018-02-16 14:54:49 +00:00
Christopher Ferris
beae42bc7f Fix soname reading code.
The dynamic section contained an address, not an offset into the elf
file to indicate where the soname exists. Changed to use the strtab
entries in the section headers to map this address to the actual offset.

Refactor the soname test a bit to make it easier to verify the code.

Bug: 73499044

Test: Passes new unit tests.
Test: Ran unwind_info on the failing shared elf and verified the soinfo
Test: is correct.
Change-Id: I16ba148389bcb9aadd3566fb442dac27f89fe894
2018-02-15 17:57:13 -08:00
David Sehr
3a26c27252 Move libdexfile out of runtime
Move the files out of runtime, leaving unfortunate dependences still
there on runtime/base (for the moment).  Also separates the build logic
into two Android.bp files.

Bug: 22322814
Test: make -j 50 test-art-host
Change-Id: I54b06f035034e7564480cd5d4c33c4f62785682d
2018-02-14 17:42:59 +00:00
Christopher Ferris
559c7f2092 Implement new DEX PC lookup scheme.
GDB wasn't handling the old one gracefully.

- Create a RegsInfo structure that can be used to properly eval expression
  data.
- Remove the versions on Dwarf ops. It doesn't work the in the real world
  and doesn't add useful information.
- Fix dex pc frame number bug.

Test: testrunner.py -j40 --host --cdex-fast -t 137
Test: libunwindstack_test
Test: All unit tests pass.
Change-Id: Iac4fea651b81cb6087fd237a9a5027a352a49245
2018-02-13 16:45:38 -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
73a2508dc4 Merge "Fix off by 4 error handling eh_frame hdr." 2018-02-12 18:58:34 +00: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
e37e2d05b1 Fix off by 4 error handling eh_frame hdr.
- Add a new tool that can dump the register information given a pc.
- Add a new offline unwinder test that fails without this change.
- Update unit tests.
- Refactor offline unwind tests to make it easier to add tests.

Bug: 73048324

Test: Passes new unit tests.
Test: Passes libbacktrace unit tests.
Test: Ran debuggerd -b on random processes.
Change-Id: If6c70a044299505c2274ae6888b35bf9ac34b74b
2018-02-10 17:11:23 -08:00
Christopher Ferris
53914165e4 Expose per arch headers.
Bug: 73013352

Test: Compiles.
Change-Id: Ic05660db7d7858a9857143ee5e58cdaddcf52a35
2018-02-08 19:29:08 -08:00
David Sehr
892e675a89 Remove dependency on -no_art variant
Remove an external dependency on a shim I used to protect against
renaming.

Bug: 22322814
Test: make -j 50
Change-Id: I7e6eaee7d82a1a42d4c37daa74803cefc7bb68ce
2018-02-07 15:19:22 -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
David Srbecky
d663ef9772 Merge "Fix in-memory loading of cdex files with shared data." 2018-02-06 11:00:31 +00:00
Christopher Ferris
5f5cb238f0 Small test clean up.
Shrink a few files that are huge by removing all of the .debug_XXX
sections except .debug_frame since they aren't used.

Rename all of the arm32 to arm and x86_32 to x86.

Test: All unit tests pass.
Change-Id: Ia0f0baadf2a7fbc42a544aff2f14d5ed5f9287b7
2018-02-05 13:12:54 -08:00
David Srbecky
417f7c3720 Fix in-memory loading of cdex files with shared data.
Several cdex files may share the same data for de-duplication.

Bug: 72520014
Test: Disable DexFileFromFile and then run ART's 137 test.
Change-Id: Icfe04255cc20a302f844c2e3e3016578856e1f82
2018-02-05 20:40:31 +00:00
Christopher Ferris
7747b60faa Load dex files from ART-specific data structure.
Fixes cdex which was recently changed to have shared data section,
which means the DEX PC cannot be used to find the right symbol,
as the bytecode is no longer within the dex file, and in-fact,
we might have to scan multiple dex files to find the method.

Bug: 72520014
Test: testrunner.py --host --cdex-none -t 137
Test: testrunner.py --host --cdex-fast -t 137
Test: All unit tests pass.
Change-Id: I80265d05ad69dd9cefbe3f8a75e4cd349002af5e
2018-02-02 15:03:00 -08:00
David Srbecky
f9006ec9b6 Fix symbol resolution within a dex file.
Fixes the ART 137-cfi test, currently failing on buildbot.

Test: testrunner.py -j40 --host --cdex-none -t 137 -b
Change-Id: I16a007b291702207bbd003fd1e78e8e5ced7cd68
2018-02-01 14:51:57 +00:00
Christopher Ferris
d70ea5ea85 Move dex pc frame creation into libunwindstack.
Test: Compiles, all unit tests pass.
Test: Ran 137-cfi art test in interpreter and verified interpreter
Test: frames still show up.

Change-Id: Icea90194986faa733a873e8cf467fc2513eb5573
2018-01-31 14:19:42 -08:00
Christopher Ferris
bb4d172f70 Merge "Add a global elf cache." 2018-01-26 14:55:15 +00: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
c85935de54 Remove DwarfError.h that moved.
I think a rebase recreated this file, so deleting this now.

Bug: 65682279

Test: Compiles.
Change-Id: I1f885ed6254089ef50f293b4c407b110f8c878da
2018-01-25 16:02:20 -08:00
Treehugger Robot
dc0edb586d Merge "Fix wrong mov instruction size." 2018-01-25 23:34:56 +00:00
Chih-Hung Hsieh
09f88076c8 Fix wrong mov instruction size.
* New clang 7.0 assembler rejects mov with wrong length.

Bug: 72412103
Test: build and boot in emulator
Change-Id: If1a1affaa4a149265f8cc2b8bf463e58e71cc47a
2018-01-25 11:44:19 -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
Christopher Ferris
1a141a0925 Small behavioral changes to the unwinder.
- Be a little more lenient when reading the cies/fdes. If next entry data
  winds up incorrect, don't fail, simply stop processing the entries. This
  only applies when reading all of the cies/fdes at once.
- Fail to init an eh_frame with no entries and fallback to assuming the
  eh_frame has no header instead.
- Change the step to always try debug_frame first which has the most
  accurate information.
- Add small unit tests and a couple of offline unit tests to verify
  this behavior.

These changes are needed to support offline unwinding since it depends
on this new behavior.

Bug: 65682279

Test: Ran new unit tests.
Change-Id: I3529f1b0c8e14cd7409494e5de2f3c9e78d0855e
2018-01-24 13:20:03 -08:00
Christopher Ferris
98984b41da Add support for getting a dex pc.
Changes:
- Change the register type from int16_t to uint32_t for the location data
  and the Eval processing. This is because the special dex pc is > 65535.
- Add the ability for Dwarf register location information to point to a
  register that is itself a Dwarf location register.
- Add dex_pc to the frame information.
- Modify the unwind tool to print the dex pc if non-zero.

This does not implement the printing of the dex information in anything
but the unwind tool. It's not the final form of this printing.

Bug: 72070049

Test: Ran new unit tests.
Test: Dumped stack while in interpreter running 137-cfi art test and
Test: verified dex pc is set to non-zero.
Change-Id: I6ce8a6b577fb4f92abacbd433b1f68977e272542
2018-01-17 19:18:00 -08:00
Christopher Ferris
ed37aca987 Find first non-zero jit debug entry.
Sometimes a process will have multiple shared libraries loaded
that have defined __jit_debug_descriptor. Specifically, art testing
will load libart.so and libartd.so, which would have broken unwinding
through jit'd code for art testing if libart.so winds up being found first.
In order to avoid duplicating the code for the linker to figure out
which one is live, change the algorithm to find the first non-zero
first_entry_ set in __jit_debug_descriptor.

Bug: 68396769

Test: Passes unit tests.
Test: Verified this can unwind 137-cfi on arm/arm64.
Change-Id: Ic4d403065d2c6f22476ef0171e7add17cd1464cd
2018-01-16 14:59:51 -08:00
Christopher Ferris
150db124f3 Add ability to read jit gdb data.
Changes:
- New JitDebug class to handle all of the jit gdb interface.
- Add unit tests for all, along with new offline test using debug data.
- Add new Memory type called MemoryOfflineParts that has multiple
  MemoryOffline objects to support the offline test.
- Update the tools to use the JitDebug object.
- Modify libbacktrace to use the JitDebug, but only looking in libart.so
  and libartd.so.
- Change the Format32Bits to Is32Bit since it's more accurate and I use
  it in a different context where original name didn't make sense.
- Add a new function to find global variables in an elf file
  (GetGlobalVariable).
- Add a new function to determine if a pc is valid for this elf (IsValidPc).

Bug: 68396769

Test: Ran new unit tests. Added new offline test that uses jit debug data.
Test: Ran art test that generates jit data and verified a crash unwinds
Test: through the jit data.
Change-Id: I6e7ee2f5bab2242028a06feece156dff21c0a974
2018-01-12 11:18:42 -08:00
Christopher Ferris
3dfd2aea7a Add tool to save information from a process.
Also, modify the ProcessVmRead function to allow arbitrarily large
reads and add a test for it.

Test: Run tool and verify the output can be used to do an offline
Test: unwind.
Test: Ran unit tests.
Change-Id: I0974ddca4f5cf72b4c9fa29b597a0a669e223828
2017-12-20 12:57:01 -08:00
Treehugger Robot
8b18000b39 Merge "unwindstack: add some perfunctory MemoryOffline tests." 2017-12-18 22:21:45 +00:00
Josh Gao
f4a94c44b6 unwindstack: add some perfunctory MemoryOffline tests.
Add some tests for the behavior of MemoryOffline.

Test: libunwindstack_test 32/64 on hikey960
Change-Id: Ib65a0fe5347b86062ffeaf0adaf9af57066edb76
2017-12-15 14:11:12 -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
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
70f7d47f3c Merge "Add method to detect remote read function to use." 2017-12-08 20:09:34 +00:00
Christopher Ferris
e328673a30 Add method to detect remote read function to use.
The process_vm_read function is much faster than ptrace, but sometimes
that will not work on a remote process. Modify the libunwindstack
MemoryRemote object to figure out which one it can use.

Wrote new unit test to verify this checking behavior.

Modify libbacktrace so that the read from libunwind is used instead of
using the default ptrace calls.

Add some benchmarks to libbacktrace to compare the two different methods.

Test: Ran unit tests libbacktrace/libunwindstack/debuggerd.
Test: Ran debuggerd -b <SYSTEM_SERVER_PID>
Test: Ran debuggerd -b <MEDIACODEC PID>
Test: Ran debuggerd -b <RANDOM_PID>
Test: Used crasher to create tombstones and verified stack data is
Test: dumped properly.
Change-Id: If75ca238289532dd8e1de430d569cabb2523380a
2017-12-07 19:00:00 -08:00
Elliott Hughes
3289b9c928 Merge "Add OWNERS." 2017-12-07 23:21:26 +00:00
Elliott Hughes
693d63f9cf Add OWNERS.
Bug: N/A
Test: N/A
Change-Id: Ie785058c0f5eb9b4086c98ccba6e63e3ed411b65
2017-12-07 13:30:03 -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
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
Christopher Ferris
e714ed2774 Switch MemoryRemote to use ptrace.
Some processes will fail to read when using process_vm_read, so switch
back to ptrace for now. For example, the system_server process only
gets two frames without this fix.

Fix up some of the remote memory read unit tests to allow passing
even when using ptrace.

Bug: 70160908

Test: All unit tests pass, debuggerd -b <system_server pid> works.
Change-Id: I37c67bf9c480d7268c9fe32dad6a8d78a4020b85
2017-12-04 14:27:34 -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
c3d79f7c07 Fix incorrect usage of relative pcs.
When stepping, it's necessary to use both the unaltered relative pc
and the adjusted relative pc. If the adjusted pc is not used, the
wrong unwind information can be used.

Added new offline unit tests that take real data and verifies that it
unwinds properly.

Fix a bug in the map code that would not properly parse map data for
a 64 bit process when done in a 32 bit process.

Fix bug in eh_frame processing that didn't adjust the pc correctly.
Fix unit tests related to the pc adjustment.

Bug: 69475565

Test: Passes libbacktrace/libunwindstack unit tests.
Test: Run debuggerd -b on processes on a hikey.
Change-Id: Ic501a1c4549c5f61d2742a7105c42a960f2c892b
2017-11-29 16:10:08 -08:00
Christopher Ferris
f819c1d94b Merge "Allow multiple threads sharing a map to unwind." 2017-11-29 02:58:06 +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
Christopher Ferris
a141709b21 Add way to specify an offset in an elf file.
This is to support the ability to dump an elf embedded in an apk.

Test: Ran unwind_info on a file, then ran it on an apk with an offset.
Change-Id: I8f23f4bdaadfd3665900bdd45d50cb270e2e0eed
2017-11-28 10:59:33 -08:00
Josh Gao
b837763b49 unwindstack: rename Memory::ReadPartially to Read.
Test: mma
Test: treehugger
Change-Id: I53c2c5600fb44620b629c01db85a67789594595f
2017-11-15 15:38:13 -08:00
Josh Gao
ef35aa5d40 unwindstack: rename Memory::Read to ReadFully.
Rename Memory::Read to ReadFully to match its semantics with that of
android::base. ReadPartially will be renamed to Read in a follow up
commit, kept intentionally separate so that there aren't any callers
accidentally switched from ReadFully to Read.

Test: treehugger
Change-Id: I7d845ac5244c3025d92c8512e960e5d0d1da05af
2017-11-15 15:38:13 -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
d96cbae4d4 Fix another nullptr dereference.
In this case, if the .eh_frame_hdr doesn't exist, we would crash.

Bug: 68813077

Test: Pass new unit tests, verified that without the fix, the unit test
Test: would crash.
Change-Id: I4f1365a76fe5c2fb69fa106a1ef15889c14e7611
2017-11-08 11:04:48 -08:00
Christopher Ferris
c9dee84d81 Add support for only a .eh_frame.
Static executables only have a .eh_frame section and no .eh_frame_hdr
section. Add support for this by rearranging the class hierarchy and
creating a DwarfEhFrameWithHdr class and a DwarfEhFrame class to handle
the different cases.

Add new unit tests for DwarfEhFrame and for the new functionality.

Bug: 68820189

Test: Passes new unit tests, unwinds static executables.
Change-Id: I63d7cb8c52a686e96579a2266e18c0d06bbb6e63
2017-11-07 13:22:39 -08:00
Christopher Ferris
13b86658b9 Fix nullptr dereference.
If an entry is found in .eh_frame_hdr, but it's not properly in .eh_frame,
then the code would crash. The assumption that the header always points
to valid fde data is not guaranteed.

Bug: 68813077

Test: Passes new unit test that crashed before the change.
Change-Id: I914d9bda0d442cd232e2a056ae490301a8850105
2017-11-05 14:01:43 -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
Josh Gao
e22701ee8a libbacktrace: remove exit time destructors.
If libbacktrace is statically linked into a library that interposes
libc, exit-time destructors can lead to a segfault upon exit if static
variables are used after their destruction.

Remove the one use of a static variable (with inconsequential
performance impact of less than 1us per backtrace), and enable
-Wexit-time-destructors to ensure this stays fixed in the future.

Test: mma
Change-Id: Icdaf1cf1c1f166cf501d9ecd1983c44e4305f7e9
2017-10-26 14:20:17 -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
09123383cb Merge "If the pc is set to zero, the unwind is done." 2017-10-24 20:50:30 +00:00
Treehugger Robot
1c65e77e6d Merge changes I8f33830f,Icd2b891b,I8736ff5c,Iae6e342a
* changes:
  libunwindstack: add method to iterate across registers.
  libbacktrace: expose BacktraceMap's unwindstack::Memory.
  libunwindstack: expose UnwindStackMap::GetFunctionName.
  libbacktrace: expose libunwindstack Unwind routine.
2017-10-24 04:03:18 +00:00
Christopher Ferris
2502a60ea1 If the pc is set to zero, the unwind is done.
Bug: 68047085

Test: Ran new unit tests, verified new unwinder does not show an
Test: extra pc zero frame for arm 32 bit processes.
Change-Id: Ic6532e56fbb786a8b7d41638abae777c2d0f1d59
2017-10-23 14:55:16 -07:00
Josh Gao
6f580d8b84 libunwindstack: add method to iterate across registers.
Add a function to iterate over Regs with a human-friendly name and the
value of the register, to make it easy to string-format registers.

Test: mma
Change-Id: I8f33830fa79b245b5030b7bc9bc3c1c7ced80c75
2017-10-20 13:52:56 -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
ae3b136506 Use Unwinder object for unwind tool.
Test: Ran unwind on arm and arm64 processes.
Change-Id: I8a2a3ed31482044fe51d7e0d8c7f5588d5aad81c
2017-10-05 16:05:44 -07:00
Christopher Ferris
9e6c11da18 Merge "Update the Unwinder object and add tests." 2017-10-05 20:46:01 +00: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
Dan Willemsen
cd58088ccf Move host flags from linux -> host
There will be a second linux host config (linux_bionic), so move this to
apply to all supported host configs instead of just linux_glibc.

Test: out/soong/build.ninja is the same before/after
Change-Id: Id8db10e0a3db889736070eea16f387785c913179
2017-10-02 10:44:28 -07:00
Dan Willemsen
1e45d533b3 Remove default libraries
libdl is part of system_shared_libs now. -ldl -lpthread -lm are now defaults
for host_ldlibs on Linux and Darwin. -lrt is a default for host_ldlibs on
Linux.

Test: m host
Change-Id: I0b3c147b00a8ab6ff289b85db55b88836c905f5c
Exempt-From-Owner-Approval: build system cleanup
2017-09-29 13:17:06 -07:00
Christopher Ferris
b9de87f7ed Add a new unwind method on error.
If a function crashes by jumping into unexecutable code, the old method
could not unwind through that. Add a fallback method to set the pc from
the default return address location.

In addition, add a new finished check for steps. This will provide a method
to indicate that this step is the last step. This prevents cases where
the fallback method might be triggered incorrectly.

Update the libbacktrace code to unwind using the new methodology.

Update the unwind tool to use the new unwind methodology.

Add a new option to crasher that calls through a null function.

Create a new object, Unwinder, that encapsulates the a basic unwind. For now,
libbacktrace will still use the custom code.

Added new unit tests to cover the new cases. Also add a test that
crashes calling a nullptr as a function, and then has call frames in
the signal stack.

Bug: 65842173

Test: Pass all unit tests, verify crasher dumps properly.
Change-Id: Ia18430ab107e9f7bdf0e14a9b74710b1280bd7f4
2017-09-22 16:55:12 -07:00
Dan Willemsen
0f048675b2 Enable libdemangle and libunwindstack on host bionic
libunwindstack_test:UnwindTest.* are all failing, but at least this
builds.

Test: Enable host bionic; m
Test: out/soong/host/linux_bionic-x86/nativetest64/libdemangle_test/libdemangle_test
Test: out/soong/host/linux_bionic-x86/nativetest64/libunwindstack_test/libunwindstack_test
Change-Id: Ie6c774ecb4d7a467b5ea7ae977833e9cba178f2d
2017-09-20 13:16:13 -07:00
Christopher Ferris
edccd84763 Fix UnwindTest repeatability.
- Rewrite some of the UnwindTest tests to properly wait for the process
  to be ready.
- Add a TestScopedPidReaper to make sure that fork process get killed even
  if the test fails. Add this to all tests that fail.
- Create a quiesce function to be used by all of the tests that will
  wait after attaching to a process.

Bug: 65287279

Test: Ran unit tests on hikey960 board and on host repeatedly.
Change-Id: I57084120396f34d8dfb852f3d814bef2056f1b54
2017-09-06 17:08:44 -07:00
Christopher Ferris
18149b6764 Merge "Add a method to share the process memory object." 2017-09-07 00:01:10 +00: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
Justin Yun
73bd4f05bf Add vndk tag for libunwindstack which must be a vndk-sp
As a VNDK-SP module, Android.bp must have 'vndk' tag as well as
'vendor_available: true'.

The 'vndk' tag for VNDK-SP module is formated as below:
vndk: {
    enabled: true,
    support_system_process: true,
},

VNDK-SP modules will be installed both in system/lib(64) as normal and
in system/lib(64)/vndk-sp as a vendor variant.

Bug: 64395154
Bug: 63866913
Test: build with BOARD_VNDK_VERSION=current
Merged-In: Idb4757988d1799ffcd3341343fcc4dac34bf7ef3
Change-Id: Idb4757988d1799ffcd3341343fcc4dac34bf7ef3
(cherry picked from commit 14d540a156)
2017-09-01 13:40:23 +09: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
Treehugger Robot
75a40988c0 Merge "libunwindstack: make machine type a property of Regs." 2017-08-26 00:02:46 +00:00
Josh Gao
0953ecd03a libunwindstack: make machine type a property of Regs.
There are no actual users of the machine type output parameter to
Regs::RemoteGet. The concrete implementations of Regs know what machine
type they represent anyway, so provide an accessor to query.

Test: treehugger
Test: libunwindstack tests on 32/64-bit host, hikey960
Change-Id: Ia25910531d36c41b2b6919f154cfa914aae63117
2017-08-25 14:48:09 -07:00
Christopher Ferris
60521c7d52 Speed up map creation.
- Rewrite the Maps::Parse to use open, and a buffer on the stack.
- Rewrite the line parser away from sscanf. The current way sscanf
  is used does not catch many malformed lines. In addition, this
  new version improves performance by 50% over sscanf on sailfish.
- Add a lot of unit tests for the parser to make sure there are
  no problems. In addition, add a special line that was not rejected
  with the previous version of the code.
- Add new accessor to get the map at a particular index.
- Add a backtrace benchmark for map creation for both new and old.

This cl results in ~5% speedup and makes the new unwinder map creation
about the same for 64 bit. It's still a bit slower, but not by much.
On 32 bit, we are still about 5% slower than the old creation method,
though.

Bug: 23762183

Test: libunwindstack unit tests pass. Ran the new benchmarks.
Change-Id: Id4431e539f400984e6fad62153fdf4152d518322
2017-08-23 15:43:39 -07:00
Christopher Ferris
9e484bdb4a Fix another set of bugs.
- The pc read from the eh frame binary table of pc/fde offset is off by 4.
  I verified that on arm/arm64/x86/x86_64 the pc in this table matches
  the fde pc_start value. I did this by adding an error if this occurred
  and ran unwind_info over everything in system/lib, system/lib64, system/bin.
- Fixed unit tests for the above change.
- Fix a small bug in the processing encoded values. The high
  bit of the encoding should be masked off, but I wasn't doing that. That
  meant during processing of the fde, I was incorrectly returning
  an error because the encoded value was unknown.
- Added a new test for this encoding change.

Bug: 23762183

Test: Build and all unit tests pass. Also, see above comments.
Change-Id: If074a410a1726392274cd72c64470ca0be48e0db
2017-08-10 17:37:32 -07:00
Christopher Ferris
3b4b075fea Small clean ups
- Remove redundant map_info checks.
- Initialize fde_count_ to zero.

Bug: 23762183

Contributed-By: Ivan Maidanski <i.maidanski@samsung.com>

Test: Builds, run backtrace_test modifying CreateNew to Create and vice-versa.
Change-Id: I6e9cdfa99734f8cc2d9915cc32c66a1455e79f1b
2017-08-09 14:22:12 -07:00
Christopher Ferris
6f3981c181 Add support for the new unwind method.
Also add a comment to the GetElf function to indicate that it never returns
nullptr.

Also needed to add this library to the a million and one places that the vndk
has hard-coded this data.

Bug: 23762183

Test: Built, nothing uses the new code.
Test: However, I did run backtrace_test using this code, and all tests pass.
Change-Id: Ib270665dcb7a7607075e36d88be76dbde6e2faa8
(cherry picked from commit dc4104b720)
2017-08-03 10:15:44 -07:00
Christopher Ferris
32766f1f52 Allow mips to build.
Test: Build mips.
Change-Id: Ice2fe1d11b7c628b325ea2ad1fd1252ad8e10668
2017-08-02 10:45:46 -07:00
Christopher Ferris
bd625efbca Revert "Add support for the new unwind method."
This reverts commit 5b460d13a4.

Reason for revert: Strange sailfish boot problem.

Change-Id: Ibde9375405cca4343c262335647dac120aab4d73
2017-08-02 01:47:31 +00:00
Christopher Ferris
5b460d13a4 Add support for the new unwind method.
Also add a comment to the GetElf function to indicate that it never returns
nullptr.

Bug: 23762183

Test: Built, nothing uses the new code.
Test: However, I did run backtrace_test using this code, and all tests pass.

Change-Id: I252b9c2497e2d3d94347dd6e506170bf50cbfe16
2017-07-31 13:00:35 -07:00
Christopher Ferris
eb4a6dbf5c Speed up StepIfSignalHandler path.
The StepIfSignalHandler code reads from the process memory object, which
is currently the slowest way to read memory. Change the code to read from
the elf memory object which will almost always be a memory mapped file and
should be really fast.

Also, move the tests for StepIfSignalHandler to its own file.

Bug: 23762183

Test: Unit tests pass.
Change-Id: I1100b50554e5ef736a87babd484f3f34e9071caa
2017-07-19 21:35:46 +00:00
Christopher Ferris
a019665b3c Add signal handling to the register object.
- Add the StepIfSignalHandler function to the Regs object that checks
  if the code is in a signal handler.
- Add tests for new code, also add a test that unwinds through a signal
  handler.
- Slight modification to Elf to fail if a bad machine type is encountered.
  Add tests for this.

Bug: 23762183

Test: Ran unit tests.
Change-Id: Idafa1105d00b91a9343d7464ac9ed1cb95830963
2017-07-18 17:27:48 -07:00