[Lldb-commits] [lldb] Address mask sbprocess apis and new mask invalid const (PR #83663)

2024-03-06 Thread David Spickett via lldb-commits

https://github.com/DavidSpickett approved this pull request.

Still not sure this is the ideal  API but we can't know that until someone (aka 
you :) ) has used it for a bit, so let's get this in.

https://github.com/llvm/llvm-project/pull/83663
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] Address mask sbprocess apis and new mask invalid const (PR #83663)

2024-03-05 Thread Jason Molenda via lldb-commits


@@ -0,0 +1,130 @@
+"""Test Python APIs for setting, getting, and using address masks."""
+
+import os
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test import lldbutil
+
+
+class AddressMasksTestCase(TestBase):
+NO_DEBUG_INFO_TESTCASE = True
+
+def reset_all_masks(self, process):
+process.SetAddressMask(
+lldb.eAddressMaskTypeAll,
+lldb.LLDB_INVALID_ADDRESS_MASK,
+lldb.eAddressMaskRangeAll,
+)
+
+def test_address_masks(self):
+self.build()
+(target, process, t, bp) = lldbutil.run_to_source_breakpoint(
+self, "break here", lldb.SBFileSpec("main.c")
+)
+
+process.SetAddressableBits(lldb.eAddressMaskTypeAll, 42)
+self.assertEqual(0x02953F94, 
process.FixAddress(0x00265E953F94))
+self.reset_all_masks(process)
+
+# ~((1ULL<<42)-1) == 0xfc00
+process.SetAddressMask(lldb.eAddressMaskTypeAll, 0xFC00)
+self.assertEqual(0x02953F94, 
process.FixAddress(0x00265E953F94))
+self.reset_all_masks(process)
+
+# Check that all bits can pass through unmodified
+process.SetAddressableBits(lldb.eAddressMaskTypeAll, 64)
+self.assertEqual(0x00265E953F94, 
process.FixAddress(0x00265E953F94))
+self.reset_all_masks(process)
+
+process.SetAddressableBits(
+lldb.eAddressMaskTypeAll, 42, lldb.eAddressMaskRangeAll
+)
+self.assertEqual(0x02950001F694, 
process.FixAddress(0x00265E950001F694))
+self.assertEqual(0xFE95F694, 
process.FixAddress(0xFFA65E95F694))
+self.reset_all_masks(process)
+
+# Set a eAddressMaskTypeCode which has the low 3 bits marked as 
non-address
+# bits, confirm that they're cleared by FixAddress.
+process.SetAddressableBits(
+lldb.eAddressMaskTypeAll, 42, lldb.eAddressMaskRangeAll
+)
+mask = process.GetAddressMask(lldb.eAddressMaskTypeAny)
+process.SetAddressMask(lldb.eAddressMaskTypeCode, mask | 0x3)
+process.SetAddressMask(lldb.eAddressMaskTypeCode, 0xFC03)

jasonmolenda wrote:

Ah, thanks for spotting that.  I originally wrote it as a hex literal and then 
I thought "Oh it would be clearer to bitmask in the 0b111" and forgot to remove 
the lit.

https://github.com/llvm/llvm-project/pull/83663
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] Address mask sbprocess apis and new mask invalid const (PR #83663)

2024-03-05 Thread Jason Molenda via lldb-commits

https://github.com/jasonmolenda updated 
https://github.com/llvm/llvm-project/pull/83663

>From c993c7cc7c1669ca7d06e52f1a1ff8dbefe9ebc9 Mon Sep 17 00:00:00 2001
From: Jason Molenda 
Date: Thu, 29 Feb 2024 17:02:42 -0800
Subject: [PATCH 1/5] [lldb] Add SBProcess methods for get/set/use address
 masks (#83095)

I'm reviving a patch from phabracator, https://reviews.llvm.org/D155905
which was approved but I wasn't thrilled with all the API I was adding
to SBProcess for all of the address mask types / memory regions. In this
update, I added enums to control type address mask type (code, data,
any) and address space specifiers (low, high, all) with defaulted
arguments for the most common case.

This patch is also fixing a bug in the "addressable bits to address
mask" calculation I added in AddressableBits::SetProcessMasks. If lldb
were told that 64 bits are valid for addressing, this method would
overflow the calculation and set an invalid mask. Added tests to check
this specific bug while I was adding these APIs.

rdar://123530562
---
 lldb/include/lldb/API/SBProcess.h | 114 ++
 lldb/include/lldb/Utility/AddressableBits.h   |   3 +
 lldb/include/lldb/lldb-defines.h  |   5 +
 lldb/include/lldb/lldb-enumerations.h |  16 +++
 lldb/source/API/SBProcess.cpp |  92 ++
 lldb/source/Target/Process.cpp|  10 +-
 lldb/source/Utility/AddressableBits.cpp   |  12 +-
 .../python_api/process/address-masks/Makefile |   3 +
 .../process/address-masks/TestAddressMasks.py |  74 
 .../python_api/process/address-masks/main.c   |   5 +
 10 files changed, 328 insertions(+), 6 deletions(-)
 create mode 100644 lldb/test/API/python_api/process/address-masks/Makefile
 create mode 100644 
lldb/test/API/python_api/process/address-masks/TestAddressMasks.py
 create mode 100644 lldb/test/API/python_api/process/address-masks/main.c

diff --git a/lldb/include/lldb/API/SBProcess.h 
b/lldb/include/lldb/API/SBProcess.h
index 4f92a41f3028a2..7da3335a7234b7 100644
--- a/lldb/include/lldb/API/SBProcess.h
+++ b/lldb/include/lldb/API/SBProcess.h
@@ -407,6 +407,120 @@ class LLDB_API SBProcess {
   /// the process isn't loaded from a core file.
   lldb::SBFileSpec GetCoreFile();
 
+  /// \{
+  /// \group Mask Address Methods
+  ///
+  /// \a type
+  /// All of the methods in this group take \a type argument
+  /// which is an AddressMaskType enum value.
+  /// There can be different address masks for code addresses and
+  /// data addresses, this argument can select which to get/set,
+  /// or to use when clearing non-addressable bits from an address.
+  /// This choice of mask can be important for example on AArch32
+  /// systems. Where instructions where instructions start on even addresses,
+  /// the 0th bit may be used to indicate that a function is thumb code.  On
+  /// such a target, the eAddressMaskTypeCode may clear the 0th bit from an
+  /// address to get the actual address Whereas eAddressMaskTypeData would not.
+  ///
+  /// \a addr_range
+  /// Many of the methods in this group take an \a addr_range argument
+  /// which is an AddressMaskRange enum value.
+  /// Needing to specify the address range is highly unusual, and the
+  /// default argument can be used in nearly all circumstances.
+  /// On some architectures (e.g., AArch64), it is possible to have
+  /// different page table setups for low and high memory, so different
+  /// numbers of bits relevant to addressing. It is possible to have
+  /// a program running in one half of memory and accessing the other
+  /// as heap, so we need to maintain two different sets of address masks
+  /// to debug this correctly.
+
+  /// Get the current address mask that will be applied to addresses
+  /// before reading from memory.
+  ///
+  /// \param[in] type
+  /// See \ref Mask Address Methods description of this argument.
+  /// eAddressMaskTypeAny is often a suitable value when code and
+  /// data masks are the same on a given target.
+  ///
+  /// \param[in] addr_range
+  /// See \ref Mask Address Methods description of this argument.
+  /// This will default to eAddressMaskRangeLow which is the
+  /// only set of masks used normally.
+  ///
+  /// \return
+  /// The address mask currently in use.  Bits which are not used
+  /// for addressing will be set to 1 in the mask.
+  lldb::addr_t GetAddressMask(
+  lldb::AddressMaskType type,
+  lldb::AddressMaskRange addr_range = lldb::eAddressMaskRangeLow);
+
+  /// Set the current address mask that can be applied to addresses
+  /// before reading from memory.
+  ///
+  /// \param[in] type
+  /// See \ref Mask Address Methods description of this argument.
+  /// eAddressMaskTypeAll is often a suitable value when the
+  /// same mask is being set for both code and data.
+  ///
+  /// \param[in] mask
+  /// The address mask to set.  Bits which are not used for addressing
+  

[Lldb-commits] [lldb] Address mask sbprocess apis and new mask invalid const (PR #83663)

2024-03-05 Thread David Spickett via lldb-commits


@@ -0,0 +1,130 @@
+"""Test Python APIs for setting, getting, and using address masks."""
+
+import os
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test import lldbutil
+
+
+class AddressMasksTestCase(TestBase):
+NO_DEBUG_INFO_TESTCASE = True
+
+def reset_all_masks(self, process):
+process.SetAddressMask(
+lldb.eAddressMaskTypeAll,
+lldb.LLDB_INVALID_ADDRESS_MASK,
+lldb.eAddressMaskRangeAll,
+)
+
+def test_address_masks(self):
+self.build()
+(target, process, t, bp) = lldbutil.run_to_source_breakpoint(
+self, "break here", lldb.SBFileSpec("main.c")
+)
+
+process.SetAddressableBits(lldb.eAddressMaskTypeAll, 42)
+self.assertEqual(0x02953F94, 
process.FixAddress(0x00265E953F94))
+self.reset_all_masks(process)
+
+# ~((1ULL<<42)-1) == 0xfc00
+process.SetAddressMask(lldb.eAddressMaskTypeAll, 0xFC00)
+self.assertEqual(0x02953F94, 
process.FixAddress(0x00265E953F94))
+self.reset_all_masks(process)
+
+# Check that all bits can pass through unmodified
+process.SetAddressableBits(lldb.eAddressMaskTypeAll, 64)
+self.assertEqual(0x00265E953F94, 
process.FixAddress(0x00265E953F94))
+self.reset_all_masks(process)
+
+process.SetAddressableBits(
+lldb.eAddressMaskTypeAll, 42, lldb.eAddressMaskRangeAll
+)
+self.assertEqual(0x02950001F694, 
process.FixAddress(0x00265E950001F694))
+self.assertEqual(0xFE95F694, 
process.FixAddress(0xFFA65E95F694))
+self.reset_all_masks(process)
+
+# Set a eAddressMaskTypeCode which has the low 3 bits marked as 
non-address
+# bits, confirm that they're cleared by FixAddress.
+process.SetAddressableBits(
+lldb.eAddressMaskTypeAll, 42, lldb.eAddressMaskRangeAll
+)
+mask = process.GetAddressMask(lldb.eAddressMaskTypeAny)
+process.SetAddressMask(lldb.eAddressMaskTypeCode, mask | 0x3)
+process.SetAddressMask(lldb.eAddressMaskTypeCode, 0xFC03)

DavidSpickett wrote:

These two lines do the same thing?

https://github.com/llvm/llvm-project/pull/83663
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] Address mask sbprocess apis and new mask invalid const (PR #83663)

2024-03-05 Thread Jason Molenda via lldb-commits

https://github.com/jasonmolenda updated 
https://github.com/llvm/llvm-project/pull/83663

>From c993c7cc7c1669ca7d06e52f1a1ff8dbefe9ebc9 Mon Sep 17 00:00:00 2001
From: Jason Molenda 
Date: Thu, 29 Feb 2024 17:02:42 -0800
Subject: [PATCH 1/4] [lldb] Add SBProcess methods for get/set/use address
 masks (#83095)

I'm reviving a patch from phabracator, https://reviews.llvm.org/D155905
which was approved but I wasn't thrilled with all the API I was adding
to SBProcess for all of the address mask types / memory regions. In this
update, I added enums to control type address mask type (code, data,
any) and address space specifiers (low, high, all) with defaulted
arguments for the most common case.

This patch is also fixing a bug in the "addressable bits to address
mask" calculation I added in AddressableBits::SetProcessMasks. If lldb
were told that 64 bits are valid for addressing, this method would
overflow the calculation and set an invalid mask. Added tests to check
this specific bug while I was adding these APIs.

rdar://123530562
---
 lldb/include/lldb/API/SBProcess.h | 114 ++
 lldb/include/lldb/Utility/AddressableBits.h   |   3 +
 lldb/include/lldb/lldb-defines.h  |   5 +
 lldb/include/lldb/lldb-enumerations.h |  16 +++
 lldb/source/API/SBProcess.cpp |  92 ++
 lldb/source/Target/Process.cpp|  10 +-
 lldb/source/Utility/AddressableBits.cpp   |  12 +-
 .../python_api/process/address-masks/Makefile |   3 +
 .../process/address-masks/TestAddressMasks.py |  74 
 .../python_api/process/address-masks/main.c   |   5 +
 10 files changed, 328 insertions(+), 6 deletions(-)
 create mode 100644 lldb/test/API/python_api/process/address-masks/Makefile
 create mode 100644 
lldb/test/API/python_api/process/address-masks/TestAddressMasks.py
 create mode 100644 lldb/test/API/python_api/process/address-masks/main.c

diff --git a/lldb/include/lldb/API/SBProcess.h 
b/lldb/include/lldb/API/SBProcess.h
index 4f92a41f3028a2..7da3335a7234b7 100644
--- a/lldb/include/lldb/API/SBProcess.h
+++ b/lldb/include/lldb/API/SBProcess.h
@@ -407,6 +407,120 @@ class LLDB_API SBProcess {
   /// the process isn't loaded from a core file.
   lldb::SBFileSpec GetCoreFile();
 
+  /// \{
+  /// \group Mask Address Methods
+  ///
+  /// \a type
+  /// All of the methods in this group take \a type argument
+  /// which is an AddressMaskType enum value.
+  /// There can be different address masks for code addresses and
+  /// data addresses, this argument can select which to get/set,
+  /// or to use when clearing non-addressable bits from an address.
+  /// This choice of mask can be important for example on AArch32
+  /// systems. Where instructions where instructions start on even addresses,
+  /// the 0th bit may be used to indicate that a function is thumb code.  On
+  /// such a target, the eAddressMaskTypeCode may clear the 0th bit from an
+  /// address to get the actual address Whereas eAddressMaskTypeData would not.
+  ///
+  /// \a addr_range
+  /// Many of the methods in this group take an \a addr_range argument
+  /// which is an AddressMaskRange enum value.
+  /// Needing to specify the address range is highly unusual, and the
+  /// default argument can be used in nearly all circumstances.
+  /// On some architectures (e.g., AArch64), it is possible to have
+  /// different page table setups for low and high memory, so different
+  /// numbers of bits relevant to addressing. It is possible to have
+  /// a program running in one half of memory and accessing the other
+  /// as heap, so we need to maintain two different sets of address masks
+  /// to debug this correctly.
+
+  /// Get the current address mask that will be applied to addresses
+  /// before reading from memory.
+  ///
+  /// \param[in] type
+  /// See \ref Mask Address Methods description of this argument.
+  /// eAddressMaskTypeAny is often a suitable value when code and
+  /// data masks are the same on a given target.
+  ///
+  /// \param[in] addr_range
+  /// See \ref Mask Address Methods description of this argument.
+  /// This will default to eAddressMaskRangeLow which is the
+  /// only set of masks used normally.
+  ///
+  /// \return
+  /// The address mask currently in use.  Bits which are not used
+  /// for addressing will be set to 1 in the mask.
+  lldb::addr_t GetAddressMask(
+  lldb::AddressMaskType type,
+  lldb::AddressMaskRange addr_range = lldb::eAddressMaskRangeLow);
+
+  /// Set the current address mask that can be applied to addresses
+  /// before reading from memory.
+  ///
+  /// \param[in] type
+  /// See \ref Mask Address Methods description of this argument.
+  /// eAddressMaskTypeAll is often a suitable value when the
+  /// same mask is being set for both code and data.
+  ///
+  /// \param[in] mask
+  /// The address mask to set.  Bits which are not used for addressing
+  

[Lldb-commits] [lldb] Address mask sbprocess apis and new mask invalid const (PR #83663)

2024-03-05 Thread Jason Molenda via lldb-commits

jasonmolenda wrote:

I pushed an update to this PR where I have the base class Fix methods in 
ABI.cpp ignore the highmem masks completely, I added highmem mask use when it's 
a highmem address and they are set to the AArch64ABI class so all the AArch64 
ABI plugins are checking this.  I changed the API test to only do highmem tests 
on AArch64 targets, and on other targets they will test that highmem masks are 
ignored.  If any target wants this feature, I would like them to have to 
explicitly declare it in the TestAddressMasks.py so things don't 
kinda-work/fail unintentionally.

https://github.com/llvm/llvm-project/pull/83663
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] Address mask sbprocess apis and new mask invalid const (PR #83663)

2024-03-04 Thread Jason Molenda via lldb-commits

https://github.com/jasonmolenda updated 
https://github.com/llvm/llvm-project/pull/83663

>From c993c7cc7c1669ca7d06e52f1a1ff8dbefe9ebc9 Mon Sep 17 00:00:00 2001
From: Jason Molenda 
Date: Thu, 29 Feb 2024 17:02:42 -0800
Subject: [PATCH 1/3] [lldb] Add SBProcess methods for get/set/use address
 masks (#83095)

I'm reviving a patch from phabracator, https://reviews.llvm.org/D155905
which was approved but I wasn't thrilled with all the API I was adding
to SBProcess for all of the address mask types / memory regions. In this
update, I added enums to control type address mask type (code, data,
any) and address space specifiers (low, high, all) with defaulted
arguments for the most common case.

This patch is also fixing a bug in the "addressable bits to address
mask" calculation I added in AddressableBits::SetProcessMasks. If lldb
were told that 64 bits are valid for addressing, this method would
overflow the calculation and set an invalid mask. Added tests to check
this specific bug while I was adding these APIs.

rdar://123530562
---
 lldb/include/lldb/API/SBProcess.h | 114 ++
 lldb/include/lldb/Utility/AddressableBits.h   |   3 +
 lldb/include/lldb/lldb-defines.h  |   5 +
 lldb/include/lldb/lldb-enumerations.h |  16 +++
 lldb/source/API/SBProcess.cpp |  92 ++
 lldb/source/Target/Process.cpp|  10 +-
 lldb/source/Utility/AddressableBits.cpp   |  12 +-
 .../python_api/process/address-masks/Makefile |   3 +
 .../process/address-masks/TestAddressMasks.py |  74 
 .../python_api/process/address-masks/main.c   |   5 +
 10 files changed, 328 insertions(+), 6 deletions(-)
 create mode 100644 lldb/test/API/python_api/process/address-masks/Makefile
 create mode 100644 
lldb/test/API/python_api/process/address-masks/TestAddressMasks.py
 create mode 100644 lldb/test/API/python_api/process/address-masks/main.c

diff --git a/lldb/include/lldb/API/SBProcess.h 
b/lldb/include/lldb/API/SBProcess.h
index 4f92a41f3028a2..7da3335a7234b7 100644
--- a/lldb/include/lldb/API/SBProcess.h
+++ b/lldb/include/lldb/API/SBProcess.h
@@ -407,6 +407,120 @@ class LLDB_API SBProcess {
   /// the process isn't loaded from a core file.
   lldb::SBFileSpec GetCoreFile();
 
+  /// \{
+  /// \group Mask Address Methods
+  ///
+  /// \a type
+  /// All of the methods in this group take \a type argument
+  /// which is an AddressMaskType enum value.
+  /// There can be different address masks for code addresses and
+  /// data addresses, this argument can select which to get/set,
+  /// or to use when clearing non-addressable bits from an address.
+  /// This choice of mask can be important for example on AArch32
+  /// systems. Where instructions where instructions start on even addresses,
+  /// the 0th bit may be used to indicate that a function is thumb code.  On
+  /// such a target, the eAddressMaskTypeCode may clear the 0th bit from an
+  /// address to get the actual address Whereas eAddressMaskTypeData would not.
+  ///
+  /// \a addr_range
+  /// Many of the methods in this group take an \a addr_range argument
+  /// which is an AddressMaskRange enum value.
+  /// Needing to specify the address range is highly unusual, and the
+  /// default argument can be used in nearly all circumstances.
+  /// On some architectures (e.g., AArch64), it is possible to have
+  /// different page table setups for low and high memory, so different
+  /// numbers of bits relevant to addressing. It is possible to have
+  /// a program running in one half of memory and accessing the other
+  /// as heap, so we need to maintain two different sets of address masks
+  /// to debug this correctly.
+
+  /// Get the current address mask that will be applied to addresses
+  /// before reading from memory.
+  ///
+  /// \param[in] type
+  /// See \ref Mask Address Methods description of this argument.
+  /// eAddressMaskTypeAny is often a suitable value when code and
+  /// data masks are the same on a given target.
+  ///
+  /// \param[in] addr_range
+  /// See \ref Mask Address Methods description of this argument.
+  /// This will default to eAddressMaskRangeLow which is the
+  /// only set of masks used normally.
+  ///
+  /// \return
+  /// The address mask currently in use.  Bits which are not used
+  /// for addressing will be set to 1 in the mask.
+  lldb::addr_t GetAddressMask(
+  lldb::AddressMaskType type,
+  lldb::AddressMaskRange addr_range = lldb::eAddressMaskRangeLow);
+
+  /// Set the current address mask that can be applied to addresses
+  /// before reading from memory.
+  ///
+  /// \param[in] type
+  /// See \ref Mask Address Methods description of this argument.
+  /// eAddressMaskTypeAll is often a suitable value when the
+  /// same mask is being set for both code and data.
+  ///
+  /// \param[in] mask
+  /// The address mask to set.  Bits which are not used for addressing
+  

[Lldb-commits] [lldb] Address mask sbprocess apis and new mask invalid const (PR #83663)

2024-03-04 Thread Jason Molenda via lldb-commits

jasonmolenda wrote:

> > is about how TestAddressMasks.py assumes all Fix*Address implementations 
> > will handle a low and high memory address mask. The test currently assumes 
> > they do
> 
> I'm not sure it makes much difference unless handling low/high produces a 
> large amount of boilerplate in every plugin.
> 
> It is a bit of a foot gun to be able to tell the plugin for, for example, Arm 
> 32, that it has high/low mem masks. Since they won't make any sense, but it's 
> pretty well hidden.

I agree, the more I think about this the less I'm delighted by making every 
plugin support high/low address masks when it may not make any sense there.  I 
like keeping the support in the SysV AArch64 ABI even though it's not used on 
Linux in case this ABI were used for generic AArch64 firmware debug scenario 
where someone might try to use that ABI plugin.  

In the API test, I can have a dedicated test for the highmem checks an only run 
it on aarch64/arm64 targets.  I can add a test for the non-aarch64/arm64 
targets which tests that setting the highmem mask has no effect (and remove the 
highmem support from the base ABI method impl).  


https://github.com/llvm/llvm-project/pull/83663
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] Address mask sbprocess apis and new mask invalid const (PR #83663)

2024-03-04 Thread David Spickett via lldb-commits

DavidSpickett wrote:

>  is about how TestAddressMasks.py assumes all Fix*Address implementations 
> will handle a low and high memory address mask. The test currently assumes 
> they do

I'm not sure it makes much difference unless handling low/high produces a large 
amount of boilerplate in every plugin.

It is a bit of a foot gun to be able to tell the plugin for, for example, Arm 
32, that it has high/low mem masks. Since they won't make any sense, but it's 
pretty well hidden.

If somehow Arm/ppc/mips/intel/s390x grew a need to handle high/low mem and 
needed zero changes other than the mask, folks would be able to just set it 
without changing lldb (though I expect that wouldn't be the only change needed 
anyway).

https://github.com/llvm/llvm-project/pull/83663
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] Address mask sbprocess apis and new mask invalid const (PR #83663)

2024-03-01 Thread Jason Molenda via lldb-commits

jasonmolenda wrote:

@DavidSpickett I spent a little time debugging this on aarch64-ubuntu and also 
testing on x86_64-macos (which has no address masks at all).  This PR has the 
original commit from https://github.com/llvm/llvm-project/pull/83095 and then a 
second commit with the changes I needed to make as I debugged this in the full 
environment, so you only need to look at the second commit for the new changes.

The main question I have (beyond feedback on how I describe things in comments 
of course ;) ) is about how TestAddressMasks.py assumes all Fix*Address 
implementations will handle a low and high memory address mask.  The test 
currently assumes they do, but that means all people writing these methods need 
to get the highmem mask if it is a high-mem addr_t and apply that mask if it is 
set.  I think it's the right choice, but I'm not wedded to it; maybe the right 
choice is narrowing that part of TestAddressMasks.py to only run on darwin 
systems.

I also added a base class ABI::Fix*Address so these tests can be used on 
targets with no address masks at all, if someone wants to manipulate them via 
SB API for some reason (of course, the real reason is to make the test 
runnable).  Maybe the test should be limited to only running on aarch64-linux 
and arm64-apple, I also am not convinced my choice is best here.

https://github.com/llvm/llvm-project/pull/83663
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] Address mask sbprocess apis and new mask invalid const (PR #83663)

2024-03-01 Thread Jason Molenda via lldb-commits

https://github.com/jasonmolenda edited 
https://github.com/llvm/llvm-project/pull/83663
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] Address mask sbprocess apis and new mask invalid const (PR #83663)

2024-03-01 Thread via lldb-commits

llvmbot wrote:




@llvm/pr-subscribers-lldb

Author: Jason Molenda (jasonmolenda)


Changes

[lldb] Add SBProcess methods for get/set/use address masks (#83095)

I'm reviving a patch from phabracator, https://reviews.llvm.org/D155905
which was approved but I wasn't thrilled with all the API I was adding
to SBProcess for all of the address mask types / memory regions. In this
update, I added enums to control type address mask type (code, data,
any) and address space specifiers (low, high, all) with defaulted
arguments for the most common case.

This patch is also fixing a bug in the "addressable bits to address
mask" calculation I added in AddressableBits::SetProcessMasks. If lldb
were told that 64 bits are valid for addressing, this method would
overflow the calculation and set an invalid mask. Added tests to check
this specific bug while I was adding these APIs.

This patch changes the value of "no mask set" from 0 to
LLDB_INVALID_ADDRESS_MASK, which is UINT64_MAX. A mask of all 1's
means "no bits are used for addressing" which is an impossible mask,
whereas a mask of 0 means "all bits are used for addressing" which
is possible.

I added a base class implementation of ABI::FixCodeAddress and
ABI::FixDataAddress that will apply the Process mask values if they
are set to a value other than LLDB_INVALID_ADDRESS_MASK.

I updated all the callers/users of the Mask methods which were
handling a value of 0 to mean invalid mask to use
LLDB_INVALID_ADDRESS_MASK.

I added code to the ABISysV_arm64 Fix override methods to apply the
Highmem masks if they have been set.  These will not be set on a
Linux environment, but I have tests in TestAddressMasks.py which
set the highmem masks and I need all Fix*Mask implementations to
check  use them for the tests to pass.

rdar://123530562


---

Patch is 31.89 KiB, truncated to 20.00 KiB below, full version: 
https://github.com/llvm/llvm-project/pull/83663.diff


18 Files Affected:

- (modified) lldb/include/lldb/API/SBProcess.h (+114) 
- (modified) lldb/include/lldb/Target/ABI.h (+2-6) 
- (modified) lldb/include/lldb/Target/Process.h (+27-9) 
- (modified) lldb/include/lldb/Utility/AddressableBits.h (+3) 
- (modified) lldb/include/lldb/lldb-defines.h (+5) 
- (modified) lldb/include/lldb/lldb-enumerations.h (+16) 
- (modified) lldb/source/API/SBProcess.cpp (+92) 
- (modified) lldb/source/Commands/CommandObjectProcess.cpp (+1-1) 
- (modified) lldb/source/Plugins/ABI/AArch64/ABIMacOSX_arm64.cpp (+4-4) 
- (modified) lldb/source/Plugins/ABI/AArch64/ABISysV_arm64.cpp (+30-10) 
- (modified) 
lldb/source/Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.cpp 
(+2-1) 
- (modified) lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp (+2-2) 
- (modified) lldb/source/Target/ABI.cpp (+26) 
- (modified) lldb/source/Target/Process.cpp (+8-6) 
- (modified) lldb/source/Utility/AddressableBits.cpp (+10-2) 
- (added) lldb/test/API/python_api/process/address-masks/Makefile (+3) 
- (added) lldb/test/API/python_api/process/address-masks/TestAddressMasks.py 
(+74) 
- (added) lldb/test/API/python_api/process/address-masks/main.c (+5) 


``diff
diff --git a/lldb/include/lldb/API/SBProcess.h 
b/lldb/include/lldb/API/SBProcess.h
index 4f92a41f3028a2..7da3335a7234b7 100644
--- a/lldb/include/lldb/API/SBProcess.h
+++ b/lldb/include/lldb/API/SBProcess.h
@@ -407,6 +407,120 @@ class LLDB_API SBProcess {
   /// the process isn't loaded from a core file.
   lldb::SBFileSpec GetCoreFile();
 
+  /// \{
+  /// \group Mask Address Methods
+  ///
+  /// \a type
+  /// All of the methods in this group take \a type argument
+  /// which is an AddressMaskType enum value.
+  /// There can be different address masks for code addresses and
+  /// data addresses, this argument can select which to get/set,
+  /// or to use when clearing non-addressable bits from an address.
+  /// This choice of mask can be important for example on AArch32
+  /// systems. Where instructions where instructions start on even addresses,
+  /// the 0th bit may be used to indicate that a function is thumb code.  On
+  /// such a target, the eAddressMaskTypeCode may clear the 0th bit from an
+  /// address to get the actual address Whereas eAddressMaskTypeData would not.
+  ///
+  /// \a addr_range
+  /// Many of the methods in this group take an \a addr_range argument
+  /// which is an AddressMaskRange enum value.
+  /// Needing to specify the address range is highly unusual, and the
+  /// default argument can be used in nearly all circumstances.
+  /// On some architectures (e.g., AArch64), it is possible to have
+  /// different page table setups for low and high memory, so different
+  /// numbers of bits relevant to addressing. It is possible to have
+  /// a program running in one half of memory and accessing the other
+  /// as heap, so we need to maintain two different sets of address masks
+  /// to debug this correctly.
+
+  /// Get the current address mask that will be applied to addresses
+  /// 

[Lldb-commits] [lldb] Address mask sbprocess apis and new mask invalid const (PR #83663)

2024-03-01 Thread Jason Molenda via lldb-commits

https://github.com/jasonmolenda created 
https://github.com/llvm/llvm-project/pull/83663

[lldb] Add SBProcess methods for get/set/use address masks (#83095)

I'm reviving a patch from phabracator, https://reviews.llvm.org/D155905
which was approved but I wasn't thrilled with all the API I was adding
to SBProcess for all of the address mask types / memory regions. In this
update, I added enums to control type address mask type (code, data,
any) and address space specifiers (low, high, all) with defaulted
arguments for the most common case.

This patch is also fixing a bug in the "addressable bits to address
mask" calculation I added in AddressableBits::SetProcessMasks. If lldb
were told that 64 bits are valid for addressing, this method would
overflow the calculation and set an invalid mask. Added tests to check
this specific bug while I was adding these APIs.

This patch changes the value of "no mask set" from 0 to
LLDB_INVALID_ADDRESS_MASK, which is UINT64_MAX. A mask of all 1's
means "no bits are used for addressing" which is an impossible mask,
whereas a mask of 0 means "all bits are used for addressing" which
is possible.

I added a base class implementation of ABI::FixCodeAddress and
ABI::FixDataAddress that will apply the Process mask values if they
are set to a value other than LLDB_INVALID_ADDRESS_MASK.

I updated all the callers/users of the Mask methods which were
handling a value of 0 to mean invalid mask to use
LLDB_INVALID_ADDRESS_MASK.

I added code to the ABISysV_arm64 Fix override methods to apply the
Highmem masks if they have been set.  These will not be set on a
Linux environment, but I have tests in TestAddressMasks.py which
set the highmem masks and I need all Fix*Mask implementations to
check & use them for the tests to pass.

rdar://123530562


>From c993c7cc7c1669ca7d06e52f1a1ff8dbefe9ebc9 Mon Sep 17 00:00:00 2001
From: Jason Molenda 
Date: Thu, 29 Feb 2024 17:02:42 -0800
Subject: [PATCH 1/2] [lldb] Add SBProcess methods for get/set/use address
 masks (#83095)

I'm reviving a patch from phabracator, https://reviews.llvm.org/D155905
which was approved but I wasn't thrilled with all the API I was adding
to SBProcess for all of the address mask types / memory regions. In this
update, I added enums to control type address mask type (code, data,
any) and address space specifiers (low, high, all) with defaulted
arguments for the most common case.

This patch is also fixing a bug in the "addressable bits to address
mask" calculation I added in AddressableBits::SetProcessMasks. If lldb
were told that 64 bits are valid for addressing, this method would
overflow the calculation and set an invalid mask. Added tests to check
this specific bug while I was adding these APIs.

rdar://123530562
---
 lldb/include/lldb/API/SBProcess.h | 114 ++
 lldb/include/lldb/Utility/AddressableBits.h   |   3 +
 lldb/include/lldb/lldb-defines.h  |   5 +
 lldb/include/lldb/lldb-enumerations.h |  16 +++
 lldb/source/API/SBProcess.cpp |  92 ++
 lldb/source/Target/Process.cpp|  10 +-
 lldb/source/Utility/AddressableBits.cpp   |  12 +-
 .../python_api/process/address-masks/Makefile |   3 +
 .../process/address-masks/TestAddressMasks.py |  74 
 .../python_api/process/address-masks/main.c   |   5 +
 10 files changed, 328 insertions(+), 6 deletions(-)
 create mode 100644 lldb/test/API/python_api/process/address-masks/Makefile
 create mode 100644 
lldb/test/API/python_api/process/address-masks/TestAddressMasks.py
 create mode 100644 lldb/test/API/python_api/process/address-masks/main.c

diff --git a/lldb/include/lldb/API/SBProcess.h 
b/lldb/include/lldb/API/SBProcess.h
index 4f92a41f3028a2..7da3335a7234b7 100644
--- a/lldb/include/lldb/API/SBProcess.h
+++ b/lldb/include/lldb/API/SBProcess.h
@@ -407,6 +407,120 @@ class LLDB_API SBProcess {
   /// the process isn't loaded from a core file.
   lldb::SBFileSpec GetCoreFile();
 
+  /// \{
+  /// \group Mask Address Methods
+  ///
+  /// \a type
+  /// All of the methods in this group take \a type argument
+  /// which is an AddressMaskType enum value.
+  /// There can be different address masks for code addresses and
+  /// data addresses, this argument can select which to get/set,
+  /// or to use when clearing non-addressable bits from an address.
+  /// This choice of mask can be important for example on AArch32
+  /// systems. Where instructions where instructions start on even addresses,
+  /// the 0th bit may be used to indicate that a function is thumb code.  On
+  /// such a target, the eAddressMaskTypeCode may clear the 0th bit from an
+  /// address to get the actual address Whereas eAddressMaskTypeData would not.
+  ///
+  /// \a addr_range
+  /// Many of the methods in this group take an \a addr_range argument
+  /// which is an AddressMaskRange enum value.
+  /// Needing to specify the address range is highly unusual, and the
+  /// default argument