Skip to content

[LLDB][LoongArch] Fix build errors about NT_LOONGARCH_HW_{BREAK,WATCH} #126020

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 7, 2025
Merged

[LLDB][LoongArch] Fix build errors about NT_LOONGARCH_HW_{BREAK,WATCH} #126020

merged 1 commit into from
Feb 7, 2025

Conversation

seehearfeel
Copy link
Contributor

@seehearfeel seehearfeel commented Feb 6, 2025

On some OS distros such as LoongArch Fedora 38 mate-5 [1], there are
no macro definitions NT_LOONGARCH_HW_BREAK and NT_LOONGARCH_HW_WATCH
in the system header, then there exist some errors when building LLDB
on LoongArch.

(1) Description of Problem:

llvm-project/lldb/source/Plugins/Process/Linux/NativeRegisterContextLinux_loongarch64.cpp:529:16:
error: 'NT_LOONGARCH_HW_WATCH' was not declared in this scope; did you mean 'NT_LOONGARCH_LBT'?
  529 |   int regset = NT_LOONGARCH_HW_WATCH;
      |                ^~~~~~~~~~~~~~~~~~~~~
      |                NT_LOONGARCH_LBT
llvm-project/lldb/source/Plugins/Process/Linux/NativeRegisterContextLinux_loongarch64.cpp:543:12:
error: 'NT_LOONGARCH_HW_BREAK' was not declared in this scope; did you mean 'NT_LOONGARCH_CSR'?
  543 |   regset = NT_LOONGARCH_HW_BREAK;
      |            ^~~~~~~~~~~~~~~~~~~~~
      |            NT_LOONGARCH_CSR

(2) Steps to Reproduce:

git clone https://github.com/llvm/llvm-project.git
mkdir -p llvm-project/llvm/build && cd llvm-project/llvm/build
cmake .. -G "Ninja" \
         -DCMAKE_BUILD_TYPE=Release \
         -DLLVM_BUILD_RUNTIME=OFF \
         -DLLVM_ENABLE_PROJECTS="clang;lldb" \
         -DCMAKE_INSTALL_PREFIX=/usr/local/llvm \
         -DLLVM_TARGETS_TO_BUILD="LoongArch" \
         -DLLVM_HOST_TRIPLE=loongarch64-redhat-linux
ninja

(3) Additional Info:

Maybe there are no problems on the OS distros with newer glibc devel
library, so this issue is related with OS distros.

(4) Root Cause Analysis:

This is because the related Linux kernel commit [2] was merged in
2023-02-25 and the glibc devel library has some delay with kernel,
the glibc version of specified OS distros is not updated in time.

(5) Final Solution:

One way is to ask the maintainer of OS distros to update glibc devel
library, but it is better to not depend on the glibc version.

In order to avoid the build errors, just define NT_LOONGARCH_HW_BREAK
and NT_LOONGARCH_HW_WATCH in LLDB if there are no these definitions in
the system header.

By the way, in order to fit within 80 columns, use C++-style comments
for the new added NT_LOONGARCH_HW_BREAK and NT_LOONGARCH_HW_WATCH.

While at it, for consistency, just modify the current NT_LOONGARCH_LSX
and NT_LOONGARCH_LASX to C++-style comments too.

[1] https://mirrors.wsyu.edu.cn/fedora/linux/development/rawhide/Everything/loongarch64/iso/livecd-fedora-mate-5.loongarch64.iso
[2] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=1a69f7a161a7

Signed-off-by: Tiezhu Yang <[email protected]>

Copy link

github-actions bot commented Feb 6, 2025

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the lldb label Feb 6, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 6, 2025

@llvm/pr-subscribers-lldb

Author: None (seehearfeel)

Changes
  1. [LLDB][LoongArch] Fix build errors about NT_LOONGARCH_HW_{BREAK,WATCH}
  2. [LLDB][LoongArch] Extend the maximum number of watchpoints

Full diff: https://github.com/llvm/llvm-project/pull/126020.diff

1 Files Affected:

  • (modified) lldb/source/Plugins/Process/Linux/NativeRegisterContextLinux_loongarch64.cpp (+22-2)
diff --git a/lldb/source/Plugins/Process/Linux/NativeRegisterContextLinux_loongarch64.cpp b/lldb/source/Plugins/Process/Linux/NativeRegisterContextLinux_loongarch64.cpp
index b04018ee243fd7d..8eb2cb65c76d2ea 100644
--- a/lldb/source/Plugins/Process/Linux/NativeRegisterContextLinux_loongarch64.cpp
+++ b/lldb/source/Plugins/Process/Linux/NativeRegisterContextLinux_loongarch64.cpp
@@ -36,9 +36,29 @@
   0xa03 /* LoongArch Advanced SIMD eXtension registers */
 #endif
 
+#ifndef NT_LOONGARCH_HW_BREAK
+#define NT_LOONGARCH_HW_BREAK	0xa05   /* LoongArch hardware breakpoint registers */
+#endif
+
+#ifndef NT_LOONGARCH_HW_WATCH
+#define NT_LOONGARCH_HW_WATCH	0xa06   /* LoongArch hardware watchpoint registers */
+#endif
+
 #define REG_CONTEXT_SIZE                                                       \
   (GetGPRSize() + GetFPRSize() + sizeof(m_lsx) + sizeof(m_lasx))
 
+#ifndef user_watch_state_v2
+struct user_watch_state_v2 {
+	uint64_t dbg_info;
+	struct {
+		uint64_t    addr;
+		uint64_t    mask;
+		uint32_t    ctrl;
+		uint32_t    pad;
+	} dbg_regs[14];
+};
+#endif
+
 using namespace lldb;
 using namespace lldb_private;
 using namespace lldb_private::process_linux;
@@ -528,7 +548,7 @@ llvm::Error NativeRegisterContextLinux_loongarch64::ReadHardwareDebugInfo() {
 
   int regset = NT_LOONGARCH_HW_WATCH;
   struct iovec ioVec;
-  struct user_watch_state dreg_state;
+  struct user_watch_state_v2 dreg_state;
   Status error;
 
   ioVec.iov_base = &dreg_state;
@@ -556,7 +576,7 @@ llvm::Error NativeRegisterContextLinux_loongarch64::ReadHardwareDebugInfo() {
 llvm::Error NativeRegisterContextLinux_loongarch64::WriteHardwareDebugRegs(
     DREGType hwbType) {
   struct iovec ioVec;
-  struct user_watch_state dreg_state;
+  struct user_watch_state_v2 dreg_state;
   int regset;
 
   memset(&dreg_state, 0, sizeof(dreg_state));

@wangleiat wangleiat requested a review from SixWeining February 6, 2025 08:17
Copy link
Contributor

@wangleiat wangleiat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, execpt for the comment style.

Comment on lines 50 to 54
/*
* In order to avoid undefined or redefined error, just add a new struct
* loongarch_user_watch_state in LLDB which is same with the uapi struct
* user_watch_state_v2.
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://llvm.org/docs/CodingStandards.html#comment-formatting
In general, prefer C++-style comments // .

Copy link
Contributor

@SixWeining SixWeining left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with a nit.

LLVM uses squash merge. If you'd to keep the 2 commits, please summit 2 PRs.

// In order to avoid undefined or redefined error, just add a new struct
// loongarch_user_watch_state in LLDB which is same with the uapi struct
// user_watch_state_v2.
struct loongarch_user_watch_state {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems the code indent is inappropriate.

Copy link
Collaborator

@DavidSpickett DavidSpickett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that the basics of this were added 2 years ago - torvalds/linux@1a69f7a.

So I assume the build error here is not that NT_LOONGARCH_HW_BREAK is not defined, it's that user_watch_state got redefined?

Please explain all this in the PR description. GitHub does not use the contents of the commit messages for the final commit. Link to kernel commits as well, that will be easier than writing out the same justifications again.

Also include something in the title so it's like "Fix build errors with user_watch_v2". If you need to find this commit again, that will help you a lot.

I'd like to know what your intent is with supporting older kernels, if at all. Given that this new structure is not the same size as the other one, or is it? A pad was added but perhaps that just made the existing compiler padding explicit?

(I don't care if you do support older kernels or not, but let's get the intent documented)

#define REG_CONTEXT_SIZE \
(GetGPRSize() + GetFPRSize() + sizeof(m_lsx) + sizeof(m_lasx))

// In order to avoid undefined or redefined error, just add a new struct
// loongarch_user_watch_state in LLDB which is same with the uapi struct
// user_watch_state_v2.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment needs to give more context, explain what came before, which one lldb uses and the consequences of that re. support for older kernels or not.

@DavidSpickett
Copy link
Collaborator

So I assume the build error here is not that NT_LOONGARCH_HW_BREAK is not defined, it's that user_watch_state got redefined?

And if so, why are you adding a redefinition of it?

This would only help if you are building lldb against kernel headers from before a time where the kernel had hardware watchpoint support. Which could be a valid use case, I'm not sure. But is that in fact the use case here?

@seehearfeel seehearfeel changed the title [LLDB][LoongArch] Fix build errors and extend watchpoint numbers [LLDB][LoongArch] Fix build errors about NT_LOONGARCH_HW_{BREAK,WATCH} Feb 6, 2025
@seehearfeel
Copy link
Contributor Author

I see that the basics of this were added 2 years ago - torvalds/linux@1a69f7a.

So I assume the build error here is not that NT_LOONGARCH_HW_BREAK is not defined, it's that user_watch_state got redefined?

Actually, as the commit message said, it is because NT_LOONGARCH_HW_{BREAK,WATCH} are not defined in the system header.

Please explain all this in the PR description. GitHub does not use the contents of the commit messages for the final commit. Link to kernel commits as well, that will be easier than writing out the same justifications again.

Also include something in the title so it's like "Fix build errors with user_watch_v2". If you need to find this commit again, that will help you a lot.

I'd like to know what your intent is with supporting older kernels, if at all. Given that this new structure is not the same size as the other one, or is it? A pad was added but perhaps that just made the existing compiler padding explicit?

(I don't care if you do support older kernels or not, but let's get the intent documented)

This PR only has one patch now, the PR title is same with the patch title.
There will be another patch to do what you said once this PR is merged.

Copy link
Collaborator

@DavidSpickett DavidSpickett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! New description is very clear. Fedora 38 is a 2023 release so I can understand why there'd be folks still using it.

I assume you're going to deal with the change in struct definition in a new PR?

@DavidSpickett
Copy link
Collaborator

There will be another patch to do what you said once this PR is merged.

Which answers my question, sounds good to me.

Copy link

github-actions bot commented Feb 6, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

On some OS distros such as LoongArch Fedora 38 mate-5 [1], there are
no macro definitions NT_LOONGARCH_HW_BREAK and NT_LOONGARCH_HW_WATCH
in the system header, then there exist some errors when building LLDB
on LoongArch.

(1) Description of Problem:

llvm-project/lldb/source/Plugins/Process/Linux/NativeRegisterContextLinux_loongarch64.cpp:529:16:
error: 'NT_LOONGARCH_HW_WATCH' was not declared in this scope; did you mean 'NT_LOONGARCH_LBT'?
  529 |   int regset = NT_LOONGARCH_HW_WATCH;
      |                ^~~~~~~~~~~~~~~~~~~~~
      |                NT_LOONGARCH_LBT
llvm-project/lldb/source/Plugins/Process/Linux/NativeRegisterContextLinux_loongarch64.cpp:543:12:
error: 'NT_LOONGARCH_HW_BREAK' was not declared in this scope; did you mean 'NT_LOONGARCH_CSR'?
  543 |   regset = NT_LOONGARCH_HW_BREAK;
      |            ^~~~~~~~~~~~~~~~~~~~~
      |            NT_LOONGARCH_CSR

(2) Steps to Reproduce:

git clone https://github.com/llvm/llvm-project.git
mkdir -p llvm-project/llvm/build && cd llvm-project/llvm/build
cmake .. -G "Ninja" \
         -DCMAKE_BUILD_TYPE=Release \
         -DLLVM_BUILD_RUNTIME=OFF \
         -DLLVM_ENABLE_PROJECTS="clang;lldb" \
         -DCMAKE_INSTALL_PREFIX=/usr/local/llvm \
         -DLLVM_TARGETS_TO_BUILD="LoongArch" \
         -DLLVM_HOST_TRIPLE=loongarch64-redhat-linux
ninja

(3) Additional Info:

Maybe there are no problems on the OS distros with newer glibc devel
library, so this issue is related with OS distros.

(4) Root Cause Analysis:

This is because the related Linux kernel commit [2] was merged in
2023-02-25 and the glibc devel library has some delay with kernel,
the glibc version of specified OS distros is not updated in time.

(5) Final Solution:

One way is to ask the maintainer of OS distros to update glibc devel
library, but it is better to not depend on the glibc version.

In order to avoid the build errors, just define NT_LOONGARCH_HW_BREAK
and NT_LOONGARCH_HW_WATCH in LLDB if there are no these definitions in
the system header.

By the way, in order to fit within 80 columns, use C++-style comments
for the new added NT_LOONGARCH_HW_BREAK and NT_LOONGARCH_HW_WATCH.

While at it, for consistency, just modify the current NT_LOONGARCH_LSX
and NT_LOONGARCH_LASX to C++-style comments too.

[1] https://mirrors.wsyu.edu.cn/fedora/linux/development/rawhide/Everything/loongarch64/iso/livecd-fedora-mate-5.loongarch64.iso
[2] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=1a69f7a161a7

Signed-off-by: Tiezhu Yang <[email protected]>
@seehearfeel
Copy link
Contributor Author

By now, the format of pr and patch seem no problems, but my email setting of GitHub is not correct, I will update the comment
once it is OK, please wait some time before merging it, thank you.

@SixWeining SixWeining merged commit 50ae1c7 into llvm:main Feb 7, 2025
5 of 6 checks passed
Copy link

github-actions bot commented Feb 7, 2025

@seehearfeel Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

Icohedron pushed a commit to Icohedron/llvm-project that referenced this pull request Feb 11, 2025
llvm#126020)

On some OS distros such as LoongArch Fedora 38 mate-5 [1], there are
no macro definitions NT_LOONGARCH_HW_BREAK and NT_LOONGARCH_HW_WATCH
in the system header, then there exist some errors when building LLDB
on LoongArch.

(1) Description of Problem:

```
llvm-project/lldb/source/Plugins/Process/Linux/NativeRegisterContextLinux_loongarch64.cpp:529:16:
error: 'NT_LOONGARCH_HW_WATCH' was not declared in this scope; did you mean 'NT_LOONGARCH_LBT'?
  529 |   int regset = NT_LOONGARCH_HW_WATCH;
      |                ^~~~~~~~~~~~~~~~~~~~~
      |                NT_LOONGARCH_LBT
llvm-project/lldb/source/Plugins/Process/Linux/NativeRegisterContextLinux_loongarch64.cpp:543:12:
error: 'NT_LOONGARCH_HW_BREAK' was not declared in this scope; did you mean 'NT_LOONGARCH_CSR'?
  543 |   regset = NT_LOONGARCH_HW_BREAK;
      |            ^~~~~~~~~~~~~~~~~~~~~
      |            NT_LOONGARCH_CSR
```

(2) Steps to Reproduce:

```
git clone https://github.com/llvm/llvm-project.git
mkdir -p llvm-project/llvm/build && cd llvm-project/llvm/build
cmake .. -G "Ninja" \
         -DCMAKE_BUILD_TYPE=Release \
         -DLLVM_BUILD_RUNTIME=OFF \
         -DLLVM_ENABLE_PROJECTS="clang;lldb" \
         -DCMAKE_INSTALL_PREFIX=/usr/local/llvm \
         -DLLVM_TARGETS_TO_BUILD="LoongArch" \
         -DLLVM_HOST_TRIPLE=loongarch64-redhat-linux
ninja
```

(3) Additional Info:

Maybe there are no problems on the OS distros with newer glibc devel
library, so this issue is related with OS distros.

(4) Root Cause Analysis:

This is because the related Linux kernel commit [2] was merged in
2023-02-25 and the glibc devel library has some delay with kernel,
the glibc version of specified OS distros is not updated in time.

(5) Final Solution:

One way is to ask the maintainer of OS distros to update glibc devel
library, but it is better to not depend on the glibc version.

In order to avoid the build errors, just define NT_LOONGARCH_HW_BREAK
and NT_LOONGARCH_HW_WATCH in LLDB if there are no these definitions in
the system header.

By the way, in order to fit within 80 columns, use C++-style comments
for the new added NT_LOONGARCH_HW_BREAK and NT_LOONGARCH_HW_WATCH.

While at it, for consistency, just modify the current NT_LOONGARCH_LSX
and NT_LOONGARCH_LASX to C++-style comments too.

[1]
https://mirrors.wsyu.edu.cn/fedora/linux/development/rawhide/Everything/loongarch64/iso/livecd-fedora-mate-5.loongarch64.iso
[2]
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=1a69f7a161a7

Signed-off-by: Tiezhu Yang <[email protected]>
swift-ci pushed a commit to swiftlang/llvm-project that referenced this pull request Apr 11, 2025
llvm#126020)

On some OS distros such as LoongArch Fedora 38 mate-5 [1], there are
no macro definitions NT_LOONGARCH_HW_BREAK and NT_LOONGARCH_HW_WATCH
in the system header, then there exist some errors when building LLDB
on LoongArch.

(1) Description of Problem:

```
llvm-project/lldb/source/Plugins/Process/Linux/NativeRegisterContextLinux_loongarch64.cpp:529:16:
error: 'NT_LOONGARCH_HW_WATCH' was not declared in this scope; did you mean 'NT_LOONGARCH_LBT'?
  529 |   int regset = NT_LOONGARCH_HW_WATCH;
      |                ^~~~~~~~~~~~~~~~~~~~~
      |                NT_LOONGARCH_LBT
llvm-project/lldb/source/Plugins/Process/Linux/NativeRegisterContextLinux_loongarch64.cpp:543:12:
error: 'NT_LOONGARCH_HW_BREAK' was not declared in this scope; did you mean 'NT_LOONGARCH_CSR'?
  543 |   regset = NT_LOONGARCH_HW_BREAK;
      |            ^~~~~~~~~~~~~~~~~~~~~
      |            NT_LOONGARCH_CSR
```

(2) Steps to Reproduce:

```
git clone https://github.com/llvm/llvm-project.git
mkdir -p llvm-project/llvm/build && cd llvm-project/llvm/build
cmake .. -G "Ninja" \
         -DCMAKE_BUILD_TYPE=Release \
         -DLLVM_BUILD_RUNTIME=OFF \
         -DLLVM_ENABLE_PROJECTS="clang;lldb" \
         -DCMAKE_INSTALL_PREFIX=/usr/local/llvm \
         -DLLVM_TARGETS_TO_BUILD="LoongArch" \
         -DLLVM_HOST_TRIPLE=loongarch64-redhat-linux
ninja
```

(3) Additional Info:

Maybe there are no problems on the OS distros with newer glibc devel
library, so this issue is related with OS distros.

(4) Root Cause Analysis:

This is because the related Linux kernel commit [2] was merged in
2023-02-25 and the glibc devel library has some delay with kernel,
the glibc version of specified OS distros is not updated in time.

(5) Final Solution:

One way is to ask the maintainer of OS distros to update glibc devel
library, but it is better to not depend on the glibc version.

In order to avoid the build errors, just define NT_LOONGARCH_HW_BREAK
and NT_LOONGARCH_HW_WATCH in LLDB if there are no these definitions in
the system header.

By the way, in order to fit within 80 columns, use C++-style comments
for the new added NT_LOONGARCH_HW_BREAK and NT_LOONGARCH_HW_WATCH.

While at it, for consistency, just modify the current NT_LOONGARCH_LSX
and NT_LOONGARCH_LASX to C++-style comments too.

[1]
https://mirrors.wsyu.edu.cn/fedora/linux/development/rawhide/Everything/loongarch64/iso/livecd-fedora-mate-5.loongarch64.iso
[2]
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=1a69f7a161a7

Signed-off-by: Tiezhu Yang <[email protected]>
(cherry picked from commit 50ae1c7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants