Commit graph

86 commits

Author SHA1 Message Date
David Srbecky
85b5fecec9 Check for data races when reading JIT/DEX entries.
Update the entries only when the list is modified by the runtime.

Check that the list wasn't concurrently modified when being read.

Bug: 124287208
Test: libunwindstack_test
Test: art/test.py -b --host -r -t 137-cfi
Change-Id: I87ba70322053a01b3d5be1fdf6310e1dc21bb084
2019-03-29 14:01:32 +00:00
Christopher Ferris
7813345faa Add BuildId to frame information.
Update debuggerd to print BuildId information by default.

Bug: 120975492

Test: New unit tests pass.
Test: debuggerd -b <PID> shows build id information.
Test: tombstones include build id information.
Change-Id: I019b031113d0b77385516223c63455b868924440
2019-03-26 08:04:40 -07:00
Christopher Ferris
02a6c448c3 Add support for displaying soname in an apk.
Changes:
- Change GetSoname to always returns a std::string.
- Added new unit tests for the soname printing.
- Modify the GetElf() function to save the same elf when we see rosegment
  linkers that split the read-only and read-write across a map. This
  avoids creating multiple elf objects for each map.
- Fixed a few offline unwind tests.

Bug: 29218999

Test: Unit tests pass.
Change-Id: Iad7c38b5c2957a8c5fd4ba94ebec335bafcad57d
2019-03-13 17:07:46 -07:00
Casey Dahlin
6e96f68cf3 Add missing override keyword
Fuchsia builds with Clang, and it's a bit pickier about always using
override when it's appropriate.

Test: None
2019-03-11 12:35:41 -07:00
Casey Dahlin
0e113851a5 Add unistd.h to includes in Regs.h
This is required to use pid_t, but by luck nobody noticed its absence
until the Fuchsia build.

Test: Code now builds inside Fuchsia
2019-03-08 15:04:48 -08:00
Florian Mayer
3d67d347f5 Fix copy / move behaviour of Maps object.
Currently, moving or copying a Maps object leads to double free of MapInfo.

Even moving a Maps object  did not prevent this, as after a move
the object only has to be in an "unspecified but valid state", which can
be the original state for a vector of raw pointers (but not for a vector
of unique_ptrs).

Changing to unique_ptrs is the most failsafe way to make sure we never
accidentally destruct MapInfo.

Test: atest libuwindstack_test
      Failed LocalUnwinderTest#unwind_after_dlopen which also fails at master.

Change-Id: Id1c9739b334da5c1ba532fd55366e115940a66d3
2019-03-05 13:05:36 +00:00
Treehugger Robot
9f5e9978c2 Merge "Allow to move frames out of Unwinder." 2019-01-29 19:23:23 +00:00
Florian Mayer
1c993b76bf Allow to move frames out of Unwinder.
This saves a copy for every unwind we do in heapprofd.

Change-Id: Ife7aa1ce68831729d96ccd4c3e54ca531f98b6bc
2019-01-28 17:11:07 +00:00
Christopher Ferris
b1c9c20eb2 Add GetPrintableBuildID().
The GetBuildID() function returns the raw build id data, so add a function
to get the printable hex version of the data.

Bug: 120606663

Test: New unit tests pass.
Change-Id: Ia5aefc97457efb08bbd30ea96cbb2d47ae59f954
2019-01-25 14:31:37 -08:00
Christopher Ferris
4aec66e810 Fix memory leak in UnwinderFromPid object.
Actually make the destructor virtual so that objects in
UnwinderFromPid get freed.

Also, explicitly make the DexFiles and JitDebug destructors virtual.

Bug: 123099840

Test: Ran UnwindTest.local_from_pid thousands of times.
Change-Id: I8bf11c0eb3587fa82f5f14f4723ea5c91da0fd46
2019-01-23 09:49:28 -08:00
Christopher Ferris
bf373edc3c Add caching of build id in MapInfo object.
Change the GetBuildID function to return a std::string.
Added benchmark to check how long it takes to get the build id from
a file versus an elf object.
Added a way to get an elf without passing in a valid process_memory and
added tests for this.

Test: New unit tests.
Change-Id: I3029019767e0181c758d611fe635bc1bf72d6e8e
2019-01-17 20:14:00 -08:00
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
Martin Stjernholm
bb4f2b440a Revert^2 "Use libdexfile external API in libunwindstack."
This reverts commit cacf5bf6bc.

Reason for revert: Re-apply with proper fix for VNDK visibility on marlin and sailfish.

Test: Manual repro of http://b/121110092#comment1 on reported branch
Test: atest CtsRenderscriptTestCases
Test: mmma system/core/{libunwindstack,libbacktrace}, run host gtests
Test: Make image, flash, and reboot device.
Test: Forrest cts/art/gce-all: https://android-build.googleplex.com/builds/forrest/run/L00300000240828791
Test: Forrest cts/bionic/gce-all: https://android-build.googleplex.com/builds/forrest/run/L05600000240682947 (shows 27/2958 failed, but it doesn't pass on Blackbox either: http://screen/xbjioEf6UgR)
Test: Forrest cts/renderscript/gce-all: https://android-build.googleplex.com/builds/forrest/run/L66200000240680523
Bug: 119632407
Change-Id: I601aa97eac8127e30d753405f8bc1fc4ae7f849f
2018-12-19 21:04:35 +00:00
Treehugger Robot
1baa19b1a6 Merge "Fix offsets when shared lib split across maps." 2018-12-19 17:11:53 +00:00
Martin Stjernholm
cacf5bf6bc Revert "Use libdexfile external API in libunwindstack."
This reverts commit 444e23d2fc.

The rest of the topic doesn't need to be reverted.

Reason for revert: Breaks renderscript on marlin and sailfish.

Test: Manual repro of http://b/121110092#comment1 on reported branch
Test: "atest CtsRenderscriptTestCases" on that branch
Test: mmma system/core/{libunwindstack,libbacktrace}, run host gtests
Test: Make image, flash, and reboot device.
Bug: 121110092, 119632407
Change-Id: If1976b19ce386c95bc5bd4fd6d523745c167de18
2018-12-19 00:11:04 +00: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
Martin Stjernholm
444e23d2fc Use libdexfile external API in libunwindstack.
Test: mmma system/core/{libunwindstack,libbacktrace}, run host gtests
Test: Make image, flash, and reboot device.
Bug: 119632407
Change-Id: I370f089a1b20ba432e136818b4325d46f99df708
2018-12-13 17:58:23 +00: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
Florian Mayer
a5913bdd07 Merge "Read .note.gnu.build-id." 2018-12-06 10:57:26 +00:00
Florian Mayer
da459e56f7 Read .note.gnu.build-id.
This will be used by heapprofd to allow us to correlate build ids with
memory leaks in libraries and binaries.

Test: m
Test: host libunwindstack_test
Test: run unwind_info against my phone's libc.so (32/64) and compare
      to readelf Build ID output.

Bug: 120186412

Change-Id: I3cefd6cce9a8733509bf35b7175eb0f967783477
2018-12-05 14:13:30 +00:00
Christopher Ferris
ef6401091f Add support for caching small reads.
Add benchmarking to verify this is faster.

Test: Ran unit tests.
Change-Id: I1487114331f4581ec2368e56c4f18c6e3e6bcc7d
2018-11-28 13:04:58 -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
56d0e07d70 Fix which maps to search for globals.
If multiple threads are unwinding at the same time, new maps that contain
the global variables for dex files and jit information are created. This
leads to threads creating more new maps that then get searched, then
more maps, then more searching until virtual address space exhaustion.

Fix this so that we only search maps that have a corresponding rw map that
could contain the global memory.

Small refactor to combine the code to search for global variables into
one class that both classes inherit from.

Modify unit tests for the new pattern checking.

Bug: 117761427

Test: Ran unit tests for libunwindstack/libbacktrace/simpleperf.
Test: Ran art 004-ThreadStress that used to fail.
Change-Id: I837ca6b9d0383100079de090bc7d019598e0cdfe
2018-10-17 14:23:10 -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
Florian Mayer
932247e900 Allow to get raw register data on all platforms.
No new test failures introduced.

atest libunwindstack_test on aosp_walleye-userdebug:

Without patch:
Total: 800, Passed: 781, Failed: 19
With patch:
Total: 800, Passed: 781, Failed: 19

Change-Id: I8853c3e1594799761b481464b75e2904b38c4830
2018-08-08 11:19:41 +01:00
Christopher Ferris
5acf069a7a Be permissive about badly formed elf files.
Here is the allowable issues with an elf file that will not result in an error:
- The program headers/section headers offset points to unreadable memory.
- Allow missing program header and/or section headers.
- Allow a symbol table section header to point to invalid symbol table values.

There is no real reason to require the elf file be perfect. Everything in
the code has sane defaults, so any missing information won't cause any
problems.

This gets rid of the warning that occurs any time an elf is loaded
from memory. In memory elf files never contain all of the section headers,
and do not contain the symbol table data.

Update tests to test these new cases.

Test: Builds and unit tests all pass.
Change-Id: Iaefe2cd6b6c965a01ed425a112d6afae339f3b78
2018-08-03 22:02:05 +00:00
Christopher Ferris
5afddb0637 Remove Memory::ReadField.
In almost all cases, it is faster to read the entire structure rather
than do multiple reads using ReadField. The only case where it would be
slower is if doing a remote unwind and ptrace is the only way to read. In
all other cases, it's a single system call. In the ptrace call, it will be
multiple calls. Given that it is unusual to be forced to use ptrace,
it's better to avoid it.

It also reduces the code complexity to do a single read, and avoids
issues where the code forgets to read the field it needs.

Test: Unit tests pass on host and target.
Change-Id: I7b3875b2c85d0d88115b1776e1be28521dc0b932
2018-07-12 12:45:31 -07:00
Christopher Ferris
92acaac8c7 Refactor the DwarfSection classes.
Modify the code for the no header sections because it turns out that
it is not okay to assume that the fdes are non-overlapping. It's necessary
to read the fdes in order and match as you go.

Modify the code so that it only reads until it finds the given pc rather than
reading all of the cie/fde entries at once.

Rewrote the tests to verify the new behavior.

Bug: 68998033
Bug: 110235461

Test: Ran libbacktrace/libunwindstack unit tests.
Test: Unwind the mediaserver process on a walleye and verify it
Test: unwinds properly.

Change-Id: I7bb59d1db72c13fa34caa9735ec34c1a60e20ed2
2018-06-27 14:52:21 -07:00
Christopher Ferris
f882a38864 Fix ARM program header values used for exidx.
Before, I was using p_vaddr to get the offset into the elf file where
the exidx frame starts. I changed that to use p_offset since this already
has the load bias offset in it and some elf files do not set p_vaddr
properly.

Also, use p_filesz instead of p_memsz, since again, some elf files do
not set p_memsz to the same as p_filesz.

Bug: 110704153

Test: All libbacktrace/libunwindstack unit tests pass.
Test: Randomly unwind process on a walleye.
Test: Verified that this properly dumps and unwinds the shared
Test: library that sets p_vaddr and p_memsz differently.
Change-Id: Ic7b1e5d07439f4636fa02cd884a8727a5737372b
2018-06-22 16:48:02 -07:00
Christopher Ferris
4cc36d2b43 Fix handling of load bias values.
It turns out that for the dwarf information, if a FDE indicates it's pc
relative, then pc has to be incremented by the load bias. If not, then
it should not be incremented.

Previously, the code always subtracted load bias values from pcs, and assumed
that all fdes were incremented by load bias values. The new code actually
reads the fdes and adjusted the pcs in the fde and in the eh frame hdr so
that load bias values are already handled properly.

In addition, add dumping of arm exidx values in unwind_reg_info. This allowed
verifying that the debug frame in those elf files was being handled properly.

Added a new unit test that only has a debug frame that has a non-zero load
bias and has fde entries that do not have pc relative encoding.

Fix a couple of other small bugs.

Bug: 109824792

Test: All libbacktrace/libunwindstack unit tests pass.
Test: Ran ART 137-cfi test and 004-ThreadStress.
Test: Verify that displaying the fde start and end pc actually match the
Test: real data for fde that have pc relative set, and that don't.
Test: Verified that the unwind information for arm exidx matches the
Test: debug frame data.

Change-Id: I707555286b5cb05df9f25489e8c5ede753cfe0fb
2018-06-11 18:53:55 -07:00
Christopher Ferris
ca9a54b2c0 Add a specialized LocalUnwinder object.
This object is for doing many local unwinds across different threads
at any point during a program's execution.

Also add LocalUpdatableMaps that will re-read the maps data under certain
circumstances.

This first version does not support jit or dex pc data.

Bug: 74361929

Test: Ran unit tests.
Change-Id: I790662366d3fed677f31b3288182950c494de9ad
2018-05-23 20:02:30 -07:00
Christopher Ferris
239425b9fa Fix bug when doing signal handler lookup.
The new lld linker uses all non-zero offset executable maps. There was
a bug when trying to find if the stack is in a signal handler that caused
the code to read the wrong place in the elf. Fixed by not adding the elf
offset to the relative pc.

Also fixed the unwind_for_offline tool to dump multiple stacks if necessary.

Added new offline unit test that would have failed with the old code.

Bug: 79936827

Test: Ran unit tests and libbacktrace unit tests.
Test: Dumped backtraces of system pids.
Test: Ran 137-cfi art test.
Change-Id: Iaca8c18c2a314902e64c3f72831234297e8dcb1b
2018-05-17 18:58:42 -07:00
Yabin Cui
3841accba8 libprocinfo: add functions reading process map file.
Add test and benchmark.
Also switch libbacktrace, libunwindstack, libmemunreachable
to use libprocinfo for map file reading.
The benchmark shows using libprocinfo speeds up map file reading
in libbacktrace and libunwindstack 18% - 36% on walleye.

Bug: http://b/79118393
Test: run procinfo_test.
Test: run libunwindstack_test.
Test: run libbacktrace_test.
Test: run memunreachable_test.

Change-Id: Icf281c352f4103fc8d4ba6732c5c07b943330ca1
2018-05-14 14:00:18 -07:00
Josh Gao
2f37a15472 libunwindstack: add Regs::Clone.
Add a way to copy a Regs object.

Bug: http://b/77296294
Test: libunwindstack_test
Change-Id: I28aff510aa3e1b677d5ae46dc3bfe7652817ce52
2018-04-20 13:23:20 -07:00
Christopher Ferris
6633b0ca09 Add a MemoryOfflineBuffer object.
Use this for offline stack data so that it's not necessary to
copy any data around.

Add unit tests for the new object.

Bug: 77258731
Bug: 74354410

Test: Ran libunwindstack/libbacktrace unit tests.
Change-Id: I9b0f25d9520c96e64aedef5f295423c60ddb3488
2018-04-03 18:37:52 -07:00
David Srbecky
3386ebade2 Cache DWARF location rules for a given pc.
Decoding the DWARF opcodes is expensive so make sure we cache it.

This speeds unwinding in simpleperf by over a factor of 3x.

Add unit tests for this new behavior.

Bug: 77258731

Test: libbacktrace/libunwindstack unit tests on host and target.
Test: Ran debuggerd -b on various processes on target.
Change-Id: Ia516c0fa5d3e5f76746190bb4b6fdf49fd1c9388
2018-03-29 14:47:13 +00: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
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
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
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
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
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
53914165e4 Expose per arch headers.
Bug: 73013352

Test: Compiles.
Change-Id: Ic05660db7d7858a9857143ee5e58cdaddcf52a35
2018-02-08 19:29:08 -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
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