Commit Graph

460 Commits

Author SHA1 Message Date
Shafik Yaghmour 320f65ee65 [LLDB][NFC] Remove parameter names from forward declarations from hand written expressions used in heap.py part 2
heap.py has a lot of large hand written expressions and each name in the
expression will be looked up by clang during expression parsing. For
function parameters this will be in Sema::ActOnParamDeclarator(...) in order to
catch redeclarations of parameters. The names are not needed and we have seen
some rare cases where since we don't have symbols we end up in
SymbolContext::FindBestGlobalDataSymbol(...) which may conflict with other global
symbols.

There may be a way to make this lookup smarter to avoid these cases but it is
not clear how well tested this path is and how much work it would be to fix it.
So we will go with this fix while we investigate more.

This is a second try at getting all the cases we care about.

Ref: rdar://78265641
2021-10-19 16:52:36 -07:00
Med Ismail Bennani a758c9f720 [lldb/Plugins] Add memory region support in ScriptedProcess
This patch adds support for memory regions in Scripted Processes.
This is necessary to read the stack memory region in order to
reconstruct each stackframe of the program.

In order to do so, this patch makes some changes to the SBAPI, namely:
- Add a new constructor for `SBMemoryRegionInfo` that takes arguments
  such as the memory region name, address range, permissions ...
  This is used when reading memory at some address to compute the offset
  in the binary blob provided by the user.
- Add a `GetMemoryRegionContainingAddress` method to `SBMemoryRegionInfoList`
  to simplify the access to a specific memory region.

With these changes, lldb is now able to unwind the stack and reconstruct
each frame. On top of that, reloading the target module at offset 0 allows
lldb to symbolicate the `ScriptedProcess` using debug info, similarly to an
ordinary Process.

To test this, I wrote a simple program with multiple function calls, ran it in
lldb, stopped at a leaf function and read the registers values and copied
the stack memory into a binary file. These are then used in the python script.

Differential Revision: https://reviews.llvm.org/D108953

Signed-off-by: Med Ismail Bennani <medismail.bennani@gmail.com>
2021-10-08 14:54:07 +02:00
Med Ismail Bennani 59d8dd79e1 [lldb/Plugins] Add support for ScriptedThread in ScriptedProcess
This patch introduces the `ScriptedThread` class with its python
interface.

When used with `ScriptedProcess`, `ScriptedThreaad` can provide various
information such as the thread state, stop reason or even its register
context.

This can be used to reconstruct the program stack frames using lldb's unwinder.

rdar://74503836

Differential Revision: https://reviews.llvm.org/D107585

Signed-off-by: Med Ismail Bennani <medismail.bennani@gmail.com>
2021-10-08 14:54:07 +02:00
Jonas Devlieghere b225c5f786 [lldb] Parse and display reporting errors from JSON crashlogs
JSON crashlogs have an optional field named reportNotes that contains
any potential errors encountered by the crash reporter when generating
the crashlog. Parse and display them in LLDB.

Differential revision: https://reviews.llvm.org/D111339
2021-10-07 15:53:52 -07:00
Jonas Devlieghere b913065bf4 [lldb] Support missing threadState in JSON crashlogs
Gracefully deal with JSON crashlogs that don't have thread state
available and print an error saying as much: "No thread state (register
information) available".

rdar://83955858

Differential revision: https://reviews.llvm.org/D111341
2021-10-07 15:53:52 -07:00
Jonas Devlieghere 730fca46fc [lldb] Improve meta data stripping from JSON crashlogs
JSON crashlogs normally start with a single line of meta data that we
strip unconditionally. Some producers started omitting the meta data
which tripped up crashlog. Be more resilient by only removing the first
line when we know it really is meta data.

rdar://82641662
2021-10-05 12:15:54 -07:00
Vedant Kumar e31b2d7d7b [lldb][crashlog] Avoid specifying arch for image when a UUID is present
When adding an image to a target for crashlog purposes, avoid specifying
the architecture of the image.

This has the effect of making SBTarget::AddModule infer the ArchSpec for
the image based on the SBTarget's architecture, which LLDB puts serious
effort into calculating correctly (in TargetList::CreateTargetInternal).

The status quo is that LLDB randomly guesses the ArchSpec for a module
if its architecture is specified, via:

```
  SBTarget::AddModule -> Platform::GetAugmentedArchSpec -> Platform::IsCompatibleArchitecture ->
GetSupportedArchitectureAtIndex -> {ARM,x86}GetSupportedArchitectureAtIndex
```

... which means that the same crashlog can fail to load on an Apple
Silicon Mac (due to the random guess of arm64e-apple-macosx for the
module's ArchSpec not being compatible with the SBTarget's (correct)
ArchSpec), while loading just fine on an Intel Mac.

I'm not sure how to add a test for this (it doesn't look like there's
test coverage of this path in-tree). It seems like it would be pretty
complicated to regression test: the host LLDB would need to be built for
arm64e, we'd need a hand-crafted arm64e iOS crashlog, and we'd need a
binary with an iOS deployment target. I'm open to other / simpler
options.

rdar://82679400

Differential Revision: https://reviews.llvm.org/D110013
2021-09-20 10:23:35 -07:00
Jonas Devlieghere 4da5a446f8 [lldb] Update crashlog.py to accept multiple results from mdfind
mdfind can return multiple results, some of which are not even dSYM
bundles, but Xcode archives (.xcrachive).

Currently, we end up concatenating the paths, which is obviously bogus.
This patch not only fixes that, but now also skips paths that don't have
a Contents/Resources/DWARF subdirectory.

rdar://81270312

Differential revision: https://reviews.llvm.org/D109263
2021-09-07 08:36:58 -07:00
Jonas Devlieghere 05cdd294ab [lldb] Adjust parse_frames for unnamed images
Follow up to 2cbd3b04fe which added
support for unnamed images but missed the use case in parse_frames.
2021-09-03 13:23:24 -07:00
Med Ismail Bennani 312b43da05 [lldb/Plugins] Add ScriptedProcess Process Plugin
This patch introduces Scripted Processes to lldb.

The goal, here, is to be able to attach in the debugger to fake processes
that are backed by script files (in Python, Lua, Swift, etc ...) and
inspect them statically.

Scripted Processes can be used in cooperative multithreading environments
like the XNU Kernel or other real-time operating systems, but it can
also help us improve the debugger testing infrastructure by writting
synthetic tests that simulates hard-to-reproduce process/thread states.

Although ScriptedProcess is not feature-complete at the moment, it has
basic execution capabilities and will improve in the following patches.

rdar://65508855

Differential Revision: https://reviews.llvm.org/D100384

Signed-off-by: Med Ismail Bennani <medismail.bennani@gmail.com>
2021-07-22 14:47:33 +02:00
Jonas Devlieghere a5c3f10b75 [lldb] Update shebang in heap.py and crashlog.py 2021-07-02 15:33:57 -07:00
Shafik Yaghmour ae1a699554 [LLDB][NFC] Remove parameter names from forward declarations from hand written expressions used in heap.py
heap.py has a lot of large hand written expressions and each name in the
expression will be looked up by clang during expression parsing. For
function parameters this will be in Sema::ActOnParamDeclarator(...) in order to
catch redeclarations of parameters. The names are not needed and we have seen
some rare cases where since we don't have symbols we end up in
SymbolContext::FindBestGlobalDataSymbol(...) which may conflict with other global
symbols.

There may be a way to make this lookup smarter to avoid these cases but it is
not clear how well tested this path is and how much work it would be to fix it.
So we will go with this fix while we investigate more.

Ref: rdar://78265641
2021-06-08 14:27:02 -07:00
Bruce Mitchener 36597e4719 [lldb] Fix typos. NFC.
Differential Revision: https://reviews.llvm.org/D103381
2021-05-31 06:48:57 +07:00
Walter Erquinigo 0283abee5c [lldb] Fix gnu_libstdcpp's update methods
The variable.rst documentation says:

```
If it returns a value, and that value is True, LLDB will be allowed to cache the children and the children count it previously obtained, and will not return to the provider class to ask.  If nothing, None, or anything other than True is returned, LLDB will discard the cached information and ask. Regardless, whenever necessary LLDB will call update.
```

However, several update methods in gnu_libstdcpp.py were returning True,
which made lldb unaware of any changes in the corresponding objects.
This problem was visible by lldb-vscode in the following way:

- If a breakpoint is hit and there's a vector with the contents {1, 2},
  it'll be displayed correctly.
- Then the user steps and the next stop contains the vector modified.
  The program changed it to {1, 2, 3}
- frame var then displays {1, 2} incorrectly, due to the caching caused
by the update method

It's worth mentioning that none of libcxx.py'd update methods return True. Same for LibCxxVector.cpp, which returns false.

Added a very simple test that fails without this fix.

Differential Revision: https://reviews.llvm.org/D103209
2021-05-26 14:52:38 -07:00
Michał Górny 6c37984eba [lldb] [gdb-remote server] Introduce new stop reasons for fork and vfork
Introduce three new stop reasons for fork, vfork and vforkdone events.
This includes server support for serializing fork/vfork events into
gdb-remote protocol.  The stop infos for the two base events take a pair
of PID and TID for the newly forked process.

Differential Revision: https://reviews.llvm.org/D100196
2021-04-24 11:08:33 +02:00
Jonas Devlieghere 91d3f73937 [lldb] Update register state parsing for JSON crashlogs
- The register encoding state in the JSON crashlog format changes.
   Update the parser accordingly.
 - Print the register state when printing the symbolicated thread.
2021-04-22 16:40:59 -07:00
Jonas Devlieghere a62cbd9a02 [lldb] Include thread name in crashlog.py output
Update the JSON parser to include the thread name in the Thread object.

rdar://76677320
2021-04-22 11:38:53 -07:00
Jonas Devlieghere 2cbd3b04fe [lldb] Support "absolute memory address" images in crashlog.py
The binary image list contains the following entry when a frame is not
found in any know binary image:

  {
    "size" : 0,
    "source" : "A",
    "base" : 0,
    "uuid" : "00000000-0000-0000-0000-000000000000"
  }

Note that this object is missing the name and path keys. This patch
makes the JSON parser resilient against their absence.
2021-04-19 10:27:11 -07:00
Jonas Devlieghere 8639e2aaaf [lldb] Raise a CrashLogParseException when failing to parse JSON crashlog
Throw an exception with an actually helpful message when we fail to
parse a JSON crashlog.
2021-04-15 15:28:23 -07:00
Med Ismail Bennani f3176f5fed [lldb/bindings] Add Python ScriptedProcess base class to lldb module
In order to facilitate the writting of Scripted Processes, this patch
introduces a `ScriptedProcess` python base class in the lldb module.

The base class holds the python interface with all the - abstract -
methods that need to be implemented by the inherited class but also some
methods that can be overwritten.

This patch also provides an example of a Scripted Process with the
`MyScriptedProcess` class.

rdar://65508855

Differential Revision: https://reviews.llvm.org/D95712

Signed-off-by: Med Ismail Bennani <medismail.bennani@gmail.com>
2021-03-23 18:24:47 +01:00
Jonas Devlieghere cc52ea3001 [lldb] Update crashlog script for JSON changes
Update the crashlog script for changes to the JSON schema.

rdar://75122914

Differential revision: https://reviews.llvm.org/D98219
2021-03-09 10:44:34 -08:00
Med Ismail Bennani 36254f1a0f
[lldb] Revert ScriptedProcess patches
This patch reverts the following commits:
- 5a9c34918b
- 46796762af
- 2cff3dec11
- 182f0d1a34
- d62a53aaf1

Signed-off-by: Med Ismail Bennani <medismail.bennani@gmail.com>
2021-03-01 23:23:27 +00:00
Med Ismail Bennani 2cff3dec11 [lldb/bindings] Add Python ScriptedProcess base class to lldb module
In order to facilitate the writting of Scripted Processes, this patch
introduces a `ScriptedProcess` python base class in the lldb module.

The base class holds the python interface with all the - abstract -
methods that need to be implemented by the inherited class but also some
methods that can be overwritten.

This patch also provides an example of a Scripted Process with the
`MyScriptedProcess` class.

rdar://65508855

Differential Revision: https://reviews.llvm.org/D95712

Signed-off-by: Med Ismail Bennani <medismail.bennani@gmail.com>
2021-03-01 21:13:32 +01:00
Raphael Isemann e97b991eef [lldb] Remove LLDB session dir and just store test traces in the respective test build directory
Test runs log some of their output to files inside the LLDB session dir. This
session dir is shared between all tests, so all the tests have to make sure they
choose a unique file name inside that directory. We currently choose by default
`<test-class-name>-<test-method-name>` as the log file name. However, that means
that if not every test class in the test suite has a unique class name, then we
end up with a race condition as two tests will try to write to the same log
file.

I already tried in D83767 changing the format to use the test file basename
instead (which we already require to be unique for some other functionality),
but it seems the code for getting the basename didn't work on Windows.

This patch instead just changes that dotest stores the log files in the build
directory for the current test. We know that directory is unique for this test,
so no need to generate some unique file name now. Also removes all the
environment vars and parameters related to the now unused session dir.

The new log paths now look like this for a failure in 'TestCppOperators`:
```
./lldb-test-build.noindex/lang/cpp/operators/TestCppOperators.test_dwarf/Failure.log
./lldb-test-build.noindex/lang/cpp/operators/TestCppOperators.test_dsym/Failure.log
./lldb-test-build.noindex/lang/cpp/operators/TestCppOperators.test_gmodules/Failure.log
```

Reviewed By: labath

Differential Revision: https://reviews.llvm.org/D92498
2020-12-04 11:43:10 +01:00
Jonas Devlieghere 99ea2c461d [lldb] Refactor the Symbolicator initializer
We found out that we have clients relying on the old signature of the
Symbolicator initializer. Make the signature compatible again and
provide a factory method to initialize the class correctly based on
whether you have a target or want the symbolicator to create one for
you.

Differential revision: D92601
2020-12-03 14:31:59 -08:00
Jonas Devlieghere c7cbf32f57 [crashlog] Implement parser for JSON encoded crashlogs
Add a parser for JSON crashlogs. The CrashLogParser now defers to either
the JSONCrashLogParser or the TextCrashLogParser. It first tries to
interpret the input as JSON, and if that fails falling back to the
textual parser.

Differential revision: https://reviews.llvm.org/D91130
2020-11-16 13:50:37 -08:00
Jonas Devlieghere c29c24be63 [crashlog] Pass the debugger around instead of relying on lldb.debugger
The lldb.debugger et al convenience variables are only available from
the interactive script interpreter. In all other scenarios, they are
None (since fc1fd6bf9f) before that they
were default initialized.

The crashlog script was hacking around that by setting the lldb.debugger
to a newly created debugger instance when running outside of the script
interpreter, which works fine until lldb creates a script interpreter
instance under the hood and clears the variables. This was resulting in
an AttributeError when invoking the script directly (from outside of
lldb):

  AttributeError: 'NoneType' object has no attribute 'GetSourceManager'

This patch fixes that by passing around the debugger instance.

rdar://64775776

Differential revision: https://reviews.llvm.org/D90706
2020-11-04 12:51:26 -08:00
Jonas Devlieghere f0fd4349a7 [crashlog] Print the actual exception in the CommandReturnObject
Before:

  error: python exception <class 'AttributeError'>

After:

  error: python exception: 'DarwinImage' object has no attribute 'debugger'
2020-11-03 11:51:40 -08:00
Jonas Devlieghere 16dd69347d [crashlog] Modularize parser
Instead of parsing the crashlog in one big loop, use methods that
correspond to the different parsing modes.

Differential revision: https://reviews.llvm.org/D90665
2020-11-03 10:21:21 -08:00
Jonas Devlieghere 4b84682044 [crashlog] Move crash log parsing into its own class
Move crash log parsing out of the CrashLog class and into its own class
and add more tests.

Differential revision: https://reviews.llvm.org/D90664
2020-11-03 09:04:35 -08:00
Jonas Devlieghere 66009a19e5 [crashlog] Remove commented out code (NFC)
Remove commented out code and print statements.
2020-11-02 19:46:53 -08:00
Jonas Devlieghere 6c9f3fe908 [crashlog] Turn crash log parsing modes into a Python 'enum' (NFC)
Python doesn't support enums before PEP 435, but using a class with
constants is how it's commonly emulated. It can be converted into a real
Enum (in Python 3.4 and later) by extending the Enum class:

  class CrashLogParseMode(Enum):
      NORMAL = 0
      THREAD = 1
      IMAGES = 2
      THREGS = 3
      SYSTEM = 4
      INSTRS = 5
2020-11-02 19:42:34 -08:00
Jonas Devlieghere fb6d1c020c [crashlog] Fix and simplify the way we import lldb
Don't try to guess the location of LLDB.framework but use xcrun to ask
the command line driver for the location of the lldb module.
2020-11-02 18:59:48 -08:00
Jonas Devlieghere 5b17b6d924 [lldb] Ignore binary data in crashlog
Skip the instruction stream section in the crashlog section.

Differential revision: https://reviews.llvm.org/D90414
2020-10-30 09:17:45 -07:00
Dave Lee ada1e2ffa1 [lldb/examples] Add missing declaration in heap.py
Add missing declaration for `malloc_get_all_zones` in heap.py.

Differential Revision: https://reviews.llvm.org/D88158
2020-09-24 08:44:45 -07:00
serge-sans-paille 515bc8c155 Harmonize Python shebang
Differential Revision: https://reviews.llvm.org/D83857
2020-07-16 21:53:45 +02:00
Eric Christopher 2db1d75396 As part of using inclusive language within the llvm project,
migrate away from the use of blacklist and whitelist.
2020-06-19 14:51:04 -07:00
Konrad Kleine eaebcbc679 [lldb] NFC remove DISALLOW_COPY_AND_ASSIGN
Summary:
This is how I applied my clang-tidy check (see
https://reviews.llvm.org/D80531) in order to remove
`DISALLOW_COPY_AND_ASSIGN` and have deleted copy ctors and deleted
assignment operators instead.

```
lang=bash
grep DISALLOW_COPY_AND_ASSIGN /opt/notnfs/kkleine/llvm/lldb -r -l | sort | uniq > files

for i in $(cat files);
do
  clang-tidy \
    --checks="-*,modernize-replace-disallow-copy-and-assign-macro" \
    --format-style=LLVM \
    --header-filter=.* \
    --fix \
    -fix-errors \
    $i;
done
```

Reviewers: espindola, labath, aprantl, teemperor

Reviewed By: labath, aprantl, teemperor

Subscribers: teemperor, aprantl, labath, emaste, sbc100, aheejin, MaskRay, arphaman, usaxena95, lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D80543
2020-06-02 13:23:53 -04:00
Jim Ingham 723a1caa37 Fix the crashlog.py script's use of the load_address property.
This property is explicitly for use only in the interactive editor,
and NOT in commands.  It's use worked until we got more careful about
not leaving lldb.target lying around in the script interpreter.

I also added a quick sniff test for the save_crashlog command.

<rdar://problem/60350620>
Differential Revision: https://reviews.llvm.org/D80680
2020-05-28 09:55:40 -07:00
Kazuaki Ishizaki e9264b746b [lldb] NFC: Fix trivial typo in comments, documents, and messages
Differential Revision: https://reviews.llvm.org/D77460
2020-04-07 01:06:16 +09:00
Pavel Labath cb6c9f731b [lldb] Make gdbremote.py utility py2and3 compatible 2020-02-13 09:18:55 +01:00
Jonas Devlieghere b6ae524cd2 [Examples] Move structured-data unpacking out of the loop. (NFC)
There's no need to repeat this work in the loop.
2019-11-22 15:43:39 -08:00
Jonas Devlieghere 1b099c1df0 [Examples] Add in_call_stack breakpoint function.
The in_call_stack Python script makes it possible to modify the last
breakpoint to only stop if a given function is present in the call
stack. It will check both the symbol name and the function name (coming
from the debug info, in case the binary is stripped).

To use this, you have to:

1. Import the script into lldb.

(lldb) command script import in_call_stack.py

2. Set a breakpoint and use the in_call_stack alias.

(lldb) b foo
(lldb) in_call_stack bar

Note that this alias operates on the last set breakpoint. You can re-run
the in_call_stack command to modify the condition.
2019-11-22 15:36:42 -08:00
Adrian Prantl ff9d732887 crashlog.py: Improve regular expressions
This is yet another change to the regular expressions in crashlog.py
that fix a few edge cases, and attempt to improve the readability
quite a bit in the process. My last change to support spaces in
filenames introduced a bug that caused the version/archspec field to
be parsed as part of the image name.

For example, in "0x1111111 - 0x22222 +MyApp Pro arm64 <01234>", the
name of the image was recognized as "MyApp Pro arm64" instead of
"MyApp Pro" with a "version" of arm64.

The bugfix makes the space following an optional field mandatory
*inside* the optional group.

rdar://problem/56883435

Differential Revision: https://reviews.llvm.org/D69871
2019-11-07 10:52:06 -08:00
Vedant Kumar 64adf7b6ae Revert [heap.py] Add missing declaration for malloc_get_all_zones
This reverts r369684 (git commit cc62e38d25)

Adding a declaration doesn't appear to be a sufficient fix.

llvm-svn: 369706
2019-08-22 21:01:45 +00:00
Vedant Kumar cc62e38d25 [heap.py] Add missing declaration for malloc_get_all_zones
The evaluation context isn't guaranteed to have this declaration.

Fixes "error: use of undeclared identifier 'malloc_get_all_zones'" bugs.

llvm-svn: 369684
2019-08-22 18:51:03 +00:00
Davide Italiano 9a5fbc8163 [Symbolication] Remove some dead code. Nothing exciting.
llvm-svn: 367262
2019-07-29 21:25:51 +00:00
Davide Italiano f80c72be20 [Symbolication] Remove a duplicate assignment.
llvm-svn: 367261
2019-07-29 21:25:45 +00:00
Davide Italiano acc626bc57 [Symbolication] Fix unicode compatibility between 2 and 3.
Triples are always ASCII for now, but we were handed out a
unicode object.

<rdar://problem/53592772>

llvm-svn: 367260
2019-07-29 21:25:37 +00:00
Davide Italiano 68946d10ad [crashlog] Fix a mismatch between bytes and strings.
The functions in read_plist() want bytes as input, not
strings.

<rdar://problem/52600712>

llvm-svn: 365416
2019-07-09 01:05:12 +00:00