-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[LLDB][Process/Utility] Introduce NativeRegisterContextDBReg class #118043
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
wangleiat
merged 5 commits into
main
from
users/wangleiat/spr/lldbprocesslinux-introduce-loongarch64-hw-breakwatchpoint-support
Dec 12, 2024
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a7cba7e
[𝘀𝗽𝗿] initial version
wangleiat 9459652
Introduce NativeRegisterContextDBReg class
wangleiat d92113b
Extract SetHardware{Breakpoint/Watchpoint}
wangleiat 96a2af6
Address @DavidSpickett's comments
wangleiat 43fa015
Update some comments and restore the return type(Status->llvm::Error)
wangleiat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
271 changes: 271 additions & 0 deletions
271
lldb/source/Plugins/Process/Utility/NativeRegisterContextDBReg.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,271 @@ | ||
| //===-- NativeRegisterContextDBReg.cpp ------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "NativeRegisterContextDBReg.h" | ||
|
|
||
| #include "lldb/Utility/LLDBLog.h" | ||
| #include "lldb/Utility/Log.h" | ||
| #include "lldb/Utility/RegisterValue.h" | ||
|
|
||
| using namespace lldb_private; | ||
|
|
||
| uint32_t NativeRegisterContextDBReg::NumSupportedHardwareBreakpoints() { | ||
| Log *log = GetLog(LLDBLog::Breakpoints); | ||
|
|
||
| // Read hardware breakpoint and watchpoint information. | ||
| Status error = ReadHardwareDebugInfo(); | ||
|
|
||
| if (error.Fail()) { | ||
| LLDB_LOG(log, "failed to read debug registers"); | ||
| return 0; | ||
| } | ||
|
|
||
| LLDB_LOG(log, "{0}", m_max_hbp_supported); | ||
| return m_max_hbp_supported; | ||
| } | ||
|
|
||
| bool NativeRegisterContextDBReg::ClearHardwareBreakpoint(uint32_t hw_idx) { | ||
| Log *log = GetLog(LLDBLog::Breakpoints); | ||
| LLDB_LOG(log, "hw_idx: {0}", hw_idx); | ||
|
|
||
| // Read hardware breakpoint and watchpoint information. | ||
| Status error = ReadHardwareDebugInfo(); | ||
| if (error.Fail()) { | ||
| LLDB_LOG(log, | ||
| "unable to clear breakpoint: failed to read debug registers: {0}"); | ||
| return false; | ||
| } | ||
|
|
||
| if (hw_idx >= m_max_hbp_supported) | ||
| return false; | ||
|
|
||
| // Create a backup we can revert to in case of failure. | ||
| lldb::addr_t tempAddr = m_hbp_regs[hw_idx].address; | ||
| uint32_t tempControl = m_hbp_regs[hw_idx].control; | ||
|
|
||
| m_hbp_regs[hw_idx].control &= ~m_hw_dbg_enable_bit; | ||
| m_hbp_regs[hw_idx].address = 0; | ||
|
|
||
| // PTRACE call to clear corresponding hardware breakpoint register. | ||
| error = WriteHardwareDebugRegs(eDREGTypeBREAK); | ||
|
|
||
| if (error.Fail()) { | ||
| m_hbp_regs[hw_idx].control = tempControl; | ||
| m_hbp_regs[hw_idx].address = tempAddr; | ||
|
|
||
| LLDB_LOG(log, | ||
| "unable to clear breakpoint: failed to write debug registers"); | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| Status | ||
| NativeRegisterContextDBReg::GetHardwareBreakHitIndex(uint32_t &bp_index, | ||
| lldb::addr_t trap_addr) { | ||
| Log *log = GetLog(LLDBLog::Breakpoints); | ||
|
|
||
| LLDB_LOGF(log, "NativeRegisterContextDBReg::%s()", __FUNCTION__); | ||
|
|
||
| lldb::addr_t break_addr; | ||
|
|
||
| for (bp_index = 0; bp_index < m_max_hbp_supported; ++bp_index) { | ||
| break_addr = m_hbp_regs[bp_index].address; | ||
|
|
||
| if (BreakpointIsEnabled(bp_index) && trap_addr == break_addr) { | ||
| m_hbp_regs[bp_index].hit_addr = trap_addr; | ||
| return Status(); | ||
| } | ||
| } | ||
|
|
||
| bp_index = LLDB_INVALID_INDEX32; | ||
| return Status(); | ||
| } | ||
|
|
||
| Status NativeRegisterContextDBReg::ClearAllHardwareBreakpoints() { | ||
| Log *log = GetLog(LLDBLog::Breakpoints); | ||
|
|
||
| LLDB_LOGF(log, "NativeRegisterContextDBReg::%s()", __FUNCTION__); | ||
|
|
||
| // Read hardware breakpoint and watchpoint information. | ||
| Status error = ReadHardwareDebugInfo(); | ||
| if (error.Fail()) | ||
| return error; | ||
|
|
||
| for (uint32_t i = 0; i < m_max_hbp_supported; i++) { | ||
| if (!BreakpointIsEnabled(i)) | ||
| continue; | ||
| // Create a backup we can revert to in case of failure. | ||
| lldb::addr_t tempAddr = m_hbp_regs[i].address; | ||
| uint32_t tempControl = m_hbp_regs[i].control; | ||
|
|
||
| // Clear watchpoints in local cache | ||
| m_hbp_regs[i].control &= ~m_hw_dbg_enable_bit; | ||
| m_hbp_regs[i].address = 0; | ||
|
|
||
| // Ptrace call to update hardware debug registers | ||
| error = WriteHardwareDebugRegs(eDREGTypeBREAK); | ||
|
|
||
| if (error.Fail()) { | ||
| m_hbp_regs[i].control = tempControl; | ||
| m_hbp_regs[i].address = tempAddr; | ||
|
|
||
| return error; | ||
| } | ||
| } | ||
|
|
||
| return Status(); | ||
| } | ||
|
|
||
| bool NativeRegisterContextDBReg::BreakpointIsEnabled(uint32_t bp_index) { | ||
| return ((m_hbp_regs[bp_index].control & m_hw_dbg_enable_bit) != 0); | ||
| } | ||
|
|
||
| uint32_t NativeRegisterContextDBReg::NumSupportedHardwareWatchpoints() { | ||
| Log *log = GetLog(LLDBLog::Watchpoints); | ||
| Status error = ReadHardwareDebugInfo(); | ||
| if (error.Fail()) { | ||
| LLDB_LOG(log, "failed to read debug registers"); | ||
| return 0; | ||
| } | ||
|
|
||
| return m_max_hwp_supported; | ||
| } | ||
|
|
||
| bool NativeRegisterContextDBReg::ClearHardwareWatchpoint(uint32_t wp_index) { | ||
| Log *log = GetLog(LLDBLog::Watchpoints); | ||
| LLDB_LOG(log, "wp_index: {0}", wp_index); | ||
|
|
||
| // Read hardware breakpoint and watchpoint information. | ||
| Status error = ReadHardwareDebugInfo(); | ||
| if (error.Fail()) { | ||
| LLDB_LOG(log, "unable to set watchpoint: failed to read debug registers"); | ||
| return LLDB_INVALID_INDEX32; | ||
| } | ||
|
|
||
| if (wp_index >= m_max_hwp_supported) | ||
| return false; | ||
|
|
||
| // Create a backup we can revert to in case of failure. | ||
| lldb::addr_t tempAddr = m_hwp_regs[wp_index].address; | ||
| uint32_t tempControl = m_hwp_regs[wp_index].control; | ||
|
|
||
| // Update watchpoint in local cache | ||
| m_hwp_regs[wp_index].control &= ~m_hw_dbg_enable_bit; | ||
| m_hwp_regs[wp_index].address = 0; | ||
|
|
||
| // Ptrace call to update hardware debug registers | ||
| error = WriteHardwareDebugRegs(eDREGTypeWATCH); | ||
|
|
||
| if (error.Fail()) { | ||
| m_hwp_regs[wp_index].control = tempControl; | ||
| m_hwp_regs[wp_index].address = tempAddr; | ||
|
|
||
| LLDB_LOG(log, "unable to clear watchpoint: failed to read debug registers"); | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| Status NativeRegisterContextDBReg::ClearAllHardwareWatchpoints() { | ||
| // Read hardware breakpoint and watchpoint information. | ||
| Status error = ReadHardwareDebugInfo(); | ||
| if (error.Fail()) | ||
| return error; | ||
|
|
||
| for (uint32_t i = 0; i < m_max_hwp_supported; i++) { | ||
| if (!WatchpointIsEnabled(i)) | ||
| continue; | ||
| // Create a backup we can revert to in case of failure. | ||
| lldb::addr_t tempAddr = m_hwp_regs[i].address; | ||
| uint32_t tempControl = m_hwp_regs[i].control; | ||
|
|
||
| // Clear watchpoints in local cache | ||
| m_hwp_regs[i].control = 0; | ||
| m_hwp_regs[i].address = 0; | ||
|
|
||
| // Ptrace call to update hardware debug registers | ||
| error = WriteHardwareDebugRegs(eDREGTypeWATCH); | ||
|
|
||
| if (error.Fail()) { | ||
| m_hwp_regs[i].control = tempControl; | ||
| m_hwp_regs[i].address = tempAddr; | ||
|
|
||
| return error; | ||
| } | ||
| } | ||
|
|
||
| return Status(); | ||
| } | ||
|
|
||
| Status | ||
| NativeRegisterContextDBReg::GetWatchpointHitIndex(uint32_t &wp_index, | ||
| lldb::addr_t trap_addr) { | ||
| Log *log = GetLog(LLDBLog::Watchpoints); | ||
| LLDB_LOG(log, "wp_index: {0}, trap_addr: {1:x}", wp_index, trap_addr); | ||
|
|
||
| // Read hardware breakpoint and watchpoint information. | ||
| Status error = ReadHardwareDebugInfo(); | ||
| if (error.Fail()) | ||
| return error; | ||
|
|
||
| // Mask off ignored bits from watchpoint trap address. | ||
| // aarch64 | ||
| trap_addr = FixWatchpointHitAddress(trap_addr); | ||
|
|
||
| uint32_t watch_size; | ||
| lldb::addr_t watch_addr; | ||
|
|
||
| for (wp_index = 0; wp_index < m_max_hwp_supported; ++wp_index) { | ||
| watch_size = GetWatchpointSize(wp_index); | ||
| watch_addr = m_hwp_regs[wp_index].address; | ||
|
|
||
| if (WatchpointIsEnabled(wp_index) && trap_addr >= watch_addr && | ||
| trap_addr < watch_addr + watch_size) { | ||
| m_hwp_regs[wp_index].hit_addr = trap_addr; | ||
| return Status(); | ||
| } | ||
| } | ||
|
|
||
| wp_index = LLDB_INVALID_INDEX32; | ||
| return Status(); | ||
| } | ||
|
|
||
| bool NativeRegisterContextDBReg::WatchpointIsEnabled(uint32_t wp_index) { | ||
| Log *log = GetLog(LLDBLog::Watchpoints); | ||
| LLDB_LOG(log, "wp_index: {0}", wp_index); | ||
| return ((m_hwp_regs[wp_index].control & m_hw_dbg_enable_bit) != 0); | ||
| } | ||
|
|
||
| lldb::addr_t | ||
| NativeRegisterContextDBReg::GetWatchpointAddress(uint32_t wp_index) { | ||
| Log *log = GetLog(LLDBLog::Watchpoints); | ||
| LLDB_LOG(log, "wp_index: {0}", wp_index); | ||
|
|
||
| if (wp_index >= m_max_hwp_supported) | ||
| return LLDB_INVALID_ADDRESS; | ||
|
|
||
| if (WatchpointIsEnabled(wp_index)) | ||
| return m_hwp_regs[wp_index].real_addr; | ||
| return LLDB_INVALID_ADDRESS; | ||
| } | ||
|
|
||
| lldb::addr_t | ||
| NativeRegisterContextDBReg::GetWatchpointHitAddress(uint32_t wp_index) { | ||
| Log *log = GetLog(LLDBLog::Watchpoints); | ||
| LLDB_LOG(log, "wp_index: {0}", wp_index); | ||
|
|
||
| if (wp_index >= m_max_hwp_supported) | ||
| return LLDB_INVALID_ADDRESS; | ||
|
|
||
| if (WatchpointIsEnabled(wp_index)) | ||
| return m_hwp_regs[wp_index].hit_addr; | ||
| return LLDB_INVALID_ADDRESS; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@labath I seem to remember you saying we want to move to or away from Status, if I didn't imagine that, which is it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wangleiat can you explain why this switched from Status to llvm::Error? Was it that one copy of the code used Status and the other llvm::Erorr?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because I noticed that the function calling it returns a
Status(virtual Status ClearAllHardwareWatchpoints()in file lldb/include/lldb/Host/common/NativeRegisterContext.h), and other architectures, such as ARM (not AArch64) and PPC, also return a Status.Should consistency be maintained here? If
llvm::Erroris required, I will revert it. I also made a mistake—the definition in this fileNativeRegisterContextFreeBSD_arm64.hhasn't been updated yet. I apologize for my carelessness.