diff options
Diffstat (limited to 'lldb/tools/debugserver/source/MacOSX')
| -rw-r--r-- | lldb/tools/debugserver/source/MacOSX/CMakeLists.txt | 5 | ||||
| -rw-r--r-- | lldb/tools/debugserver/source/MacOSX/MachException.h | 2 | ||||
| -rw-r--r-- | lldb/tools/debugserver/source/MacOSX/MachProcess.h | 27 | ||||
| -rw-r--r-- | lldb/tools/debugserver/source/MacOSX/MachProcess.mm (renamed from lldb/tools/debugserver/source/MacOSX/MachProcess.cpp) | 737 | ||||
| -rw-r--r-- | lldb/tools/debugserver/source/MacOSX/MachTask.mm (renamed from lldb/tools/debugserver/source/MacOSX/MachTask.cpp) | 29 | ||||
| -rw-r--r-- | lldb/tools/debugserver/source/MacOSX/MachThreadList.cpp | 9 | ||||
| -rw-r--r-- | lldb/tools/debugserver/source/MacOSX/MachVMMemory.cpp | 9 | ||||
| -rw-r--r-- | lldb/tools/debugserver/source/MacOSX/arm/DNBArchImpl.cpp | 295 | ||||
| -rw-r--r-- | lldb/tools/debugserver/source/MacOSX/arm/DNBArchImpl.h | 41 | ||||
| -rw-r--r-- | lldb/tools/debugserver/source/MacOSX/arm64/DNBArchImplARM64.cpp | 2093 | ||||
| -rw-r--r-- | lldb/tools/debugserver/source/MacOSX/arm64/DNBArchImplARM64.h | 272 |
11 files changed, 3324 insertions, 195 deletions
diff --git a/lldb/tools/debugserver/source/MacOSX/CMakeLists.txt b/lldb/tools/debugserver/source/MacOSX/CMakeLists.txt index 8e2ec1c66e4..24342c84f87 100644 --- a/lldb/tools/debugserver/source/MacOSX/CMakeLists.txt +++ b/lldb/tools/debugserver/source/MacOSX/CMakeLists.txt @@ -1,5 +1,6 @@ set(LLVM_NO_RTTI 1) +#add_subdirectory(arm64) #add_subdirectory(arm) add_subdirectory(i386) #add_subdirectory(ppc) @@ -31,8 +32,8 @@ add_lldb_executable(debugserver CFData.cpp CFString.cpp MachException.cpp - MachProcess.cpp - MachTask.cpp + MachProcess.mm + MachTask.mm MachThread.cpp MachThreadList.cpp MachVMMemory.cpp diff --git a/lldb/tools/debugserver/source/MacOSX/MachException.h b/lldb/tools/debugserver/source/MacOSX/MachException.h index 08d22cd28e6..74041e0c4bd 100644 --- a/lldb/tools/debugserver/source/MacOSX/MachException.h +++ b/lldb/tools/debugserver/source/MacOSX/MachException.h @@ -82,7 +82,7 @@ public: } bool IsBreakpoint() const { - return (exc_type == EXC_BREAKPOINT) || ((exc_type == EXC_SOFTWARE) && exc_data[0] == 1); + return (exc_type == EXC_BREAKPOINT || ((exc_type == EXC_SOFTWARE) && exc_data[0] == 1)); } void Dump() const; void DumpStopReason() const; diff --git a/lldb/tools/debugserver/source/MacOSX/MachProcess.h b/lldb/tools/debugserver/source/MacOSX/MachProcess.h index 5fa5dbdcfc7..c98700c0fc5 100644 --- a/lldb/tools/debugserver/source/MacOSX/MachProcess.h +++ b/lldb/tools/debugserver/source/MacOSX/MachProcess.h @@ -55,7 +55,8 @@ public: const char *stderr_path, bool no_stdio, nub_launch_flavor_t launch_flavor, - int disable_aslr, + int disable_aslr, + const char *event_data, DNBError &err); static uint32_t GetCPUTypeForLocalProcess (pid_t pid); @@ -76,8 +77,14 @@ public: static const void * PrepareForAttach (const char *path, nub_launch_flavor_t launch_flavor, bool waitfor, DNBError &err_str); static void CleanupAfterAttach (const void *attach_token, bool success, DNBError &err_str); static nub_process_t CheckForProcess (const void *attach_token); +#ifdef WITH_BKS + pid_t BKSLaunchForDebug (const char *app_bundle_path, char const *argv[], char const *envp[], bool no_stdio, bool disable_aslr, const char *event_data, DNBError &launch_err); + pid_t BKSForkChildForPTraceDebugging (const char *path, char const *argv[], char const *envp[], bool no_stdio, bool disable_aslr, const char *event_data, DNBError &launch_err); + bool BKSSendEvent (const char *event, DNBError &error); + static void BKSCleanupAfterAttach (const void *attach_token, DNBError &err_str); +#endif #ifdef WITH_SPRINGBOARD - pid_t SBLaunchForDebug (const char *app_bundle_path, char const *argv[], char const *envp[], bool no_stdio, DNBError &launch_err); + pid_t SBLaunchForDebug (const char *app_bundle_path, char const *argv[], char const *envp[], bool no_stdio, bool disable_aslr, DNBError &launch_err); static pid_t SBForkChildForPTraceDebugging (const char *path, char const *argv[], char const *envp[], bool no_stdio, MachProcess* process, DNBError &launch_err); #endif nub_addr_t LookupSymbol (const char *name, const char *shlib); @@ -94,6 +101,7 @@ public: bool Resume (const DNBThreadResumeActions& thread_actions); bool Signal (int signal, const struct timespec *timeout_abstime = NULL); + bool SendEvent (const char *event, DNBError &send_err); bool Kill (const struct timespec *timeout_abstime = NULL); bool Detach (); nub_size_t ReadMemory (nub_addr_t addr, nub_size_t size, void *buf); @@ -213,6 +221,15 @@ public: m_exit_status = status; SetState(eStateExited); } + const char * GetExitInfo () + { + return m_exit_info.c_str(); + } + + void SetExitInfo (const char *info) + { + m_exit_info.assign(info); + } uint32_t StopCount() const { return m_stop_count; } void SetChildFileDescriptors (int stdin_fileno, int stdout_fileno, int stderr_fileno) @@ -248,6 +265,7 @@ public: } bool ProcessUsingSpringBoard() const { return (m_flags & eMachProcessFlagsUsingSBS) != 0; } + bool ProcessUsingBackBoard() const { return (m_flags & eMachProcessFlagsUsingBKS) != 0; } DNBProfileDataScanType GetProfileScanType () { return m_profile_scan_type; } @@ -256,7 +274,8 @@ private: { eMachProcessFlagsNone = 0, eMachProcessFlagsAttached = (1 << 0), - eMachProcessFlagsUsingSBS = (1 << 1) + eMachProcessFlagsUsingSBS = (1 << 1), + eMachProcessFlagsUsingBKS = (1 << 2) }; void Clear (bool detaching = false); void ReplyToAllExceptions (); @@ -273,6 +292,7 @@ private: std::string m_path; // A path to the executable if we have one std::vector<std::string> m_args; // The arguments with which the process was lauched int m_exit_status; // The exit status for the process + std::string m_exit_info; // Any extra info that we may have about the exit MachTask m_task; // The mach task for this process uint32_t m_flags; // Process specific flags (see eMachProcessFlags enums) uint32_t m_stop_count; // A count of many times have we stopped @@ -304,6 +324,7 @@ private: DNBCallbackCopyExecutableImageInfos m_image_infos_callback; void * m_image_infos_baton; + std::string m_bundle_id; // If we are a SB or BKS process, this will be our bundle ID. bool m_did_exec; }; diff --git a/lldb/tools/debugserver/source/MacOSX/MachProcess.cpp b/lldb/tools/debugserver/source/MacOSX/MachProcess.mm index c077f9be002..ca7a5b5dab5 100644 --- a/lldb/tools/debugserver/source/MacOSX/MachProcess.cpp +++ b/lldb/tools/debugserver/source/MacOSX/MachProcess.mm @@ -39,7 +39,7 @@ #include "CFData.h" #include "CFString.h" -static CFStringRef CopyBundleIDForPath (const char *app_buncle_path, DNBError &err_str); +static CFStringRef CopyBundleIDForPath (const char *app_bundle_path, DNBError &err_str); #ifdef WITH_SPRINGBOARD @@ -54,8 +54,44 @@ IsSBProcess (nub_process_t pid) return appIdsForPID.get() != NULL; } -#endif +#endif // WITH_SPRINGBOARD + +#ifdef WITH_BKS +#import <Foundation/Foundation.h> +extern "C" +{ +#import <BackBoardServices/BackBoardServices.h> +#import <BackBoardServices/BKSSystemService_LaunchServices.h> +#import <BackBoardServices/BKSOpenApplicationConstants_Private.h> +} +static bool +IsBKSProcess (nub_process_t pid) +{ + BKSApplicationStateMonitor *state_monitor = [[BKSApplicationStateMonitor alloc] init]; + BKSApplicationState app_state = [state_monitor mostElevatedApplicationStateForPID: pid]; + return app_state != BKSApplicationStateUnknown; +} + +static void +SetBKSError (BKSOpenApplicationErrorCode error_code, DNBError &error) +{ + error.SetError (error_code, DNBError::BackBoard); + NSString *err_nsstr = ::BKSOpenApplicationErrorCodeToString(error_code); + const char *err_str = NULL; + if (err_nsstr == NULL) + err_str = "unknown BKS error"; + else + { + err_str = [err_nsstr UTF8String]; + if (err_str == NULL) + err_str = "unknown BKS error"; + } + error.SetErrorString(err_str); +} + +static const int BKS_OPEN_APPLICATION_TIMEOUT_ERROR = 111; +#endif // WITH_BKS #if 0 #define DEBUG_LOG(fmt, ...) printf(fmt, ## __VA_ARGS__) #else @@ -456,6 +492,18 @@ MachProcess::Signal (int signal, const struct timespec *timeout_abstime) } +bool +MachProcess::SendEvent (const char *event, DNBError &send_err) +{ + DNBLogThreadedIf(LOG_PROCESS, "MachProcess::SendEvent (event = %s) to pid: %d", event, m_pid); + if (m_pid == INVALID_NUB_PROCESS) + return false; +#if WITH_BKS + return BKSSendEvent (event, send_err); +#endif + return true; +} + nub_state_t MachProcess::DoSIGSTOP (bool clear_bps_and_wps, bool allow_running, uint32_t *thread_idx_ptr) { @@ -1475,8 +1523,11 @@ MachProcess::AttachForDebug (pid_t pid, char *err_str, size_t err_len) SetState(eStateAttaching); m_pid = pid; - // Let ourselves know we are going to be using SBS if the correct flag bit is set... -#ifdef WITH_SPRINGBOARD + // Let ourselves know we are going to be using SBS or BKS if the correct flag bit is set... +#if defined (WITH_BKS) + if (IsBKSProcess (pid)) + m_flags |= eMachProcessFlagsUsingBKS; +#elif defined (WITH_SPRINGBOARD) if (IsSBProcess(pid)) m_flags |= eMachProcessFlagsUsingSBS; #endif @@ -1523,9 +1574,9 @@ MachProcess::AttachForDebug (pid_t pid, char *err_str, size_t err_len) // cleanup seems good. const void * -MachProcess::PrepareForAttach (const char *path, nub_launch_flavor_t launch_flavor, bool waitfor, DNBError &err_str) +MachProcess::PrepareForAttach (const char *path, nub_launch_flavor_t launch_flavor, bool waitfor, DNBError &attach_err) { -#ifdef WITH_SPRINGBOARD +#if defined (WITH_SPRINGBOARD) || defined (WITH_BKS) // Tell SpringBoard to halt the next launch of this application on startup. if (!waitfor) @@ -1535,50 +1586,142 @@ MachProcess::PrepareForAttach (const char *path, nub_launch_flavor_t launch_flav const bool is_app = app_ext != NULL && (app_ext[4] == '\0' || app_ext[4] == '/'); if (!is_app) { - DNBLogThreadedIf(LOG_PROCESS, "MachProcess::PrepareForAttach(): path '%s' doesn't contain .app, we can't tell springboard to wait for launch...", path); + DNBLogThreadedIf(LOG_PROCESS, "MachProcess::PrepareForAttach(): path '%s' doesn't contain .app, " + "we can't tell springboard to wait for launch...", + path); return NULL; } - if (launch_flavor != eLaunchFlavorSpringBoard - && launch_flavor != eLaunchFlavorDefault) +#if defined (WITH_BKS) + if (launch_flavor == eLaunchFlavorDefault) + launch_flavor = eLaunchFlavorBKS; + if (launch_flavor != eLaunchFlavorBKS) + return NULL; +#elif defined (WITH_SPRINGBOARD) + if (launch_flavor == eLaunchFlavorDefault) + launch_flavor = eLaunchFlavorSpringBoard; + if (launch_flavor != eLaunchFlavorSpringBoard) return NULL; +#endif std::string app_bundle_path(path, app_ext + strlen(".app")); - CFStringRef bundleIDCFStr = CopyBundleIDForPath (app_bundle_path.c_str (), err_str); + CFStringRef bundleIDCFStr = CopyBundleIDForPath (app_bundle_path.c_str (), attach_err); std::string bundleIDStr; CFString::UTF8(bundleIDCFStr, bundleIDStr); - DNBLogThreadedIf(LOG_PROCESS, "CopyBundleIDForPath (%s, err_str) returned @\"%s\"", app_bundle_path.c_str (), bundleIDStr.c_str()); + DNBLogThreadedIf(LOG_PROCESS, + "CopyBundleIDForPath (%s, err_str) returned @\"%s\"", + app_bundle_path.c_str (), + bundleIDStr.c_str()); if (bundleIDCFStr == NULL) { return NULL; } - SBSApplicationLaunchError sbs_error = 0; +#if defined (WITH_BKS) + if (launch_flavor == eLaunchFlavorBKS) + { + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - const char *stdout_err = "/dev/null"; - CFString stdio_path; - stdio_path.SetFileSystemRepresentation (stdout_err); + NSString *stdio_path = nil; + NSFileManager *file_manager = [NSFileManager defaultManager]; + const char *null_path = "/dev/null"; + stdio_path = [file_manager stringWithFileSystemRepresentation: null_path length: strlen(null_path)]; - DNBLogThreadedIf(LOG_PROCESS, "SBSLaunchApplicationForDebugging ( @\"%s\" , NULL, NULL, NULL, @\"%s\", @\"%s\", SBSApplicationDebugOnNextLaunch | SBSApplicationLaunchWaitForDebugger )", bundleIDStr.c_str(), stdout_err, stdout_err); - sbs_error = SBSLaunchApplicationForDebugging (bundleIDCFStr, - (CFURLRef)NULL, // openURL - NULL, // launch_argv.get(), - NULL, // launch_envp.get(), // CFDictionaryRef environment - stdio_path.get(), - stdio_path.get(), - SBSApplicationDebugOnNextLaunch | SBSApplicationLaunchWaitForDebugger); + NSMutableDictionary *debug_options = [NSMutableDictionary dictionary]; + NSMutableDictionary *options = [NSMutableDictionary dictionary]; + + DNBLogThreadedIf(LOG_PROCESS, "Calling BKSSystemService openApplication: @\"%s\",options include stdio path: \"%s\", " + "BKSDebugOptionKeyDebugOnNextLaunch & BKSDebugOptionKeyWaitForDebugger )", + bundleIDStr.c_str(), + null_path); + + [debug_options setObject: stdio_path forKey: BKSDebugOptionKeyStandardOutPath]; + [debug_options setObject: stdio_path forKey: BKSDebugOptionKeyStandardErrorPath]; + [debug_options setObject: [NSNumber numberWithBool: YES] forKey: BKSDebugOptionKeyWaitForDebugger]; + [debug_options setObject: [NSNumber numberWithBool: YES] forKey: BKSDebugOptionKeyDebugOnNextLaunch]; + + [options setObject: debug_options forKey: BKSOpenApplicationOptionKeyDebuggingOptions]; - if (sbs_error != SBSApplicationLaunchErrorSuccess) + BKSSystemService *system_service = [[BKSSystemService alloc] init]; + + mach_port_t client_port = [system_service createClientPort]; + __block dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); + __block BKSOpenApplicationErrorCode attach_error_code = BKSOpenApplicationErrorCodeNone; + + NSString *bundleIDNSStr = (NSString *) bundleIDCFStr; + + [system_service openApplication: bundleIDNSStr + options: options + clientPort: client_port + withResult: ^(NSError *error) + { + // The system service will cleanup the client port we created for us. + if (error) + attach_error_code = (BKSOpenApplicationErrorCode)[error code]; + + [system_service release]; + dispatch_semaphore_signal(semaphore); + } + ]; + + const uint32_t timeout_secs = 9; + + dispatch_time_t timeout = dispatch_time(DISPATCH_TIME_NOW, timeout_secs * NSEC_PER_SEC); + + long success = dispatch_semaphore_wait(semaphore, timeout) == 0; + + if (!success) + { + DNBLogError("timed out trying to launch %s.", bundleIDStr.c_str()); + attach_err.SetErrorString("debugserver timed out waiting for openApplication to complete."); + attach_err.SetError (BKS_OPEN_APPLICATION_TIMEOUT_ERROR, DNBError::Generic); + } + else if (attach_error_code != BKSOpenApplicationErrorCodeNone) + { + SetBKSError (attach_error_code, attach_err); + DNBLogError("unable to launch the application with CFBundleIdentifier '%s' bks_error = %u", + bundleIDStr.c_str(), + attach_error_code); + } + dispatch_release(semaphore); + [pool drain]; + } +#elif defined (WITH_SPRINGBOARD) + if (launch_flavor == eLaunchFlavorSpringBoard) { - err_str.SetError(sbs_error, DNBError::SpringBoard); - return NULL; + SBSApplicationLaunchError sbs_error = 0; + + const char *stdout_err = "/dev/null"; + CFString stdio_path; + stdio_path.SetFileSystemRepresentation (stdout_err); + + DNBLogThreadedIf(LOG_PROCESS, "SBSLaunchApplicationForDebugging ( @\"%s\" , NULL, NULL, NULL, @\"%s\", @\"%s\", " + "SBSApplicationDebugOnNextLaunch | SBSApplicationLaunchWaitForDebugger )", + bundleIDStr.c_str(), + stdout_err, + stdout_err); + + sbs_error = SBSLaunchApplicationForDebugging (bundleIDCFStr, + (CFURLRef)NULL, // openURL + NULL, // launch_argv.get(), + NULL, // launch_envp.get(), // CFDictionaryRef environment + stdio_path.get(), + stdio_path.get(), + SBSApplicationDebugOnNextLaunch | SBSApplicationLaunchWaitForDebugger); + + if (sbs_error != SBSApplicationLaunchErrorSuccess) + { + attach_err.SetError(sbs_error, DNBError::SpringBoard); + return NULL; + } } +#endif // WITH_SPRINGBOARD DNBLogThreadedIf(LOG_PROCESS, "Successfully set DebugOnNextLaunch."); return bundleIDCFStr; -# else +# else // defined (WITH_SPRINGBOARD) || defined (WITH_BKS) return NULL; #endif } @@ -1593,7 +1736,16 @@ MachProcess::CheckForProcess (const void *attach_token) if (attach_token == NULL) return INVALID_NUB_PROCESS; -#ifdef WITH_SPRINGBOARD +#if defined (WITH_BKS) + NSString *bundleIDNSStr = (NSString *) attach_token; + BKSSystemService *systemService = [[BKSSystemService alloc] init]; + pid_t pid = [systemService pidForApplication: bundleIDNSStr]; + [systemService release]; + if (pid == 0) + return INVALID_NUB_PROCESS; + else + return pid; +#elif defined (WITH_SPRINGBOARD) CFStringRef bundleIDCFStr = (CFStringRef) attach_token; Boolean got_it; nub_process_t attach_pid; @@ -1602,8 +1754,9 @@ MachProcess::CheckForProcess (const void *attach_token) return attach_pid; else return INVALID_NUB_PROCESS; -#endif +#else return INVALID_NUB_PROCESS; +#endif } // Call this to clean up after you have either attached or given up on the attach. @@ -1614,10 +1767,18 @@ MachProcess::CheckForProcess (const void *attach_token) void MachProcess::CleanupAfterAttach (const void *attach_token, bool success, DNBError &err_str) { -#ifdef WITH_SPRINGBOARD if (attach_token == NULL) return; +#if defined (WITH_BKS) + + if (!success) + { + BKSCleanupAfterAttach (attach_token, err_str); + } + CFRelease((CFStringRef) attach_token); + +#elif defined (WITH_SPRINGBOARD) // Tell SpringBoard to cancel the debug on next launch of this application // if we failed to attach if (!success) @@ -1657,6 +1818,7 @@ MachProcess::LaunchForDebug bool no_stdio, nub_launch_flavor_t launch_flavor, int disable_aslr, + const char *event_data, DNBError &launch_err ) { @@ -1673,7 +1835,23 @@ MachProcess::LaunchForDebug case eLaunchFlavorForkExec: m_pid = MachProcess::ForkChildForPTraceDebugging (path, argv, envp, this, launch_err); break; - +#ifdef WITH_BKS + case eLaunchFlavorBKS: + { + const char *app_ext = strstr(path, ".app"); + if (app_ext && (app_ext[4] == '\0' || app_ext[4] == '/')) + { + std::string app_bundle_path(path, app_ext + strlen(".app")); + if (BKSLaunchForDebug (app_bundle_path.c_str(), argv, envp, no_stdio, disable_aslr, event_data, launch_err) != 0) + return m_pid; // A successful SBLaunchForDebug() returns and assigns a non-zero m_pid. + else + break; // We tried a BKS launch, but didn't succeed lets get out + } + } + // In case the executable name has a ".app" fragment which confuses our debugserver, + // let's do an intentional fallthrough here... + launch_flavor = eLaunchFlavorPosixSpawn; +#endif #ifdef WITH_SPRINGBOARD case eLaunchFlavorSpringBoard: @@ -1696,7 +1874,7 @@ MachProcess::LaunchForDebug if (app_ext) { std::string app_bundle_path(path, app_ext + strlen(".app")); - if (SBLaunchForDebug (app_bundle_path.c_str(), argv, envp, no_stdio, launch_err) != 0) + if (SBLaunchForDebug (app_bundle_path.c_str(), argv, envp, no_stdio, disable_aslr, launch_err) != 0) return m_pid; // A successful SBLaunchForDebug() returns and assigns a non-zero m_pid. else break; // We tried a springboard launch, but didn't succeed lets get out @@ -2063,10 +2241,47 @@ MachProcess::ForkChildForPTraceDebugging return pid; } +#if defined (WITH_SPRINGBOARD) || defined (WITH_BKS) +// This returns a CFRetained pointer to the Bundle ID for app_bundle_path, +// or NULL if there was some problem getting the bundle id. +static CFStringRef +CopyBundleIDForPath (const char *app_bundle_path, DNBError &err_str) +{ + CFBundle bundle(app_bundle_path); + CFStringRef bundleIDCFStr = bundle.GetIdentifier(); + std::string bundleID; + if (CFString::UTF8(bundleIDCFStr, bundleID) == NULL) + { + struct stat app_bundle_stat; + char err_msg[PATH_MAX]; + + if (::stat (app_bundle_path, &app_bundle_stat) < 0) + { + err_str.SetError(errno, DNBError::POSIX); + snprintf(err_msg, sizeof(err_msg), "%s: \"%s\"", err_str.AsString(), app_bundle_path); + err_str.SetErrorString(err_msg); + DNBLogThreadedIf(LOG_PROCESS, "%s() error: %s", __FUNCTION__, err_msg); + } + else + { + err_str.SetError(-1, DNBError::Generic); + snprintf(err_msg, sizeof(err_msg), "failed to extract CFBundleIdentifier from %s", app_bundle_path); + err_str.SetErrorString(err_msg); + DNBLogThreadedIf(LOG_PROCESS, "%s() error: failed to extract CFBundleIdentifier from '%s'", __FUNCTION__, app_bundle_path); + } + return NULL; + } + + DNBLogThreadedIf(LOG_PROCESS, "%s() extracted CFBundleIdentifier: %s", __FUNCTION__, bundleID.c_str()); + CFRetain (bundleIDCFStr); + + return bundleIDCFStr; +} +#endif // #if defined 9WITH_SPRINGBOARD) || defined (WITH_BKS) #ifdef WITH_SPRINGBOARD pid_t -MachProcess::SBLaunchForDebug (const char *path, char const *argv[], char const *envp[], bool no_stdio, DNBError &launch_err) +MachProcess::SBLaunchForDebug (const char *path, char const *argv[], char const *envp[], bool no_stdio, bool disable_aslr, DNBError &launch_err) { // Clear out and clean up from any current state Clear(); @@ -2115,42 +2330,6 @@ MachProcess::SBLaunchForDebug (const char *path, char const *argv[], char const #include <servers/bootstrap.h> -// This returns a CFRetained pointer to the Bundle ID for app_bundle_path, -// or NULL if there was some problem getting the bundle id. -static CFStringRef -CopyBundleIDForPath (const char *app_bundle_path, DNBError &err_str) -{ - CFBundle bundle(app_bundle_path); - CFStringRef bundleIDCFStr = bundle.GetIdentifier(); - std::string bundleID; - if (CFString::UTF8(bundleIDCFStr, bundleID) == NULL) - { - struct stat app_bundle_stat; - char err_msg[PATH_MAX]; - - if (::stat (app_bundle_path, &app_bundle_stat) < 0) - { - err_str.SetError(errno, DNBError::POSIX); - snprintf(err_msg, sizeof(err_msg), "%s: \"%s\"", err_str.AsString(), app_bundle_path); - err_str.SetErrorString(err_msg); - DNBLogThreadedIf(LOG_PROCESS, "%s() error: %s", __FUNCTION__, err_msg); - } - else - { - err_str.SetError(-1, DNBError::Generic); - snprintf(err_msg, sizeof(err_msg), "failed to extract CFBundleIdentifier from %s", app_bundle_path); - err_str.SetErrorString(err_msg); - DNBLogThreadedIf(LOG_PROCESS, "%s() error: failed to extract CFBundleIdentifier from '%s'", __FUNCTION__, app_bundle_path); - } - return NULL; - } - - DNBLogThreadedIf(LOG_PROCESS, "%s() extracted CFBundleIdentifier: %s", __FUNCTION__, bundleID.c_str()); - CFRetain (bundleIDCFStr); - - return bundleIDCFStr; -} - pid_t MachProcess::SBForkChildForPTraceDebugging (const char *app_bundle_path, char const *argv[], char const *envp[], bool no_stdio, MachProcess* process, DNBError &launch_err) { @@ -2244,10 +2423,13 @@ MachProcess::SBForkChildForPTraceDebugging (const char *app_bundle_path, char co CFStringRef bundleIDCFStr = CopyBundleIDForPath (app_bundle_path, launch_err); if (bundleIDCFStr == NULL) return INVALID_NUB_PROCESS; - + + // This is just for logging: std::string bundleID; CFString::UTF8(bundleIDCFStr, bundleID); + DNBLogThreadedIf(LOG_PROCESS, "%s() serialized launch arg array", __FUNCTION__); + // Find SpringBoard SBSApplicationLaunchError sbs_error = 0; sbs_error = SBSLaunchApplicationForDebugging (bundleIDCFStr, @@ -2306,4 +2488,419 @@ MachProcess::SBForkChildForPTraceDebugging (const char *app_bundle_path, char co #endif // #ifdef WITH_SPRINGBOARD +#ifdef WITH_BKS + + +// This function runs the BKSSystemService method openApplication:options:clientPort:withResult, +// messaging the app passed in bundleIDNSStr. +// The function should be run inside of an NSAutoReleasePool. +// +// It will use the "options" dictionary passed in, and fill the error passed in if there is an error. +// If return_pid is not NULL, we'll fetch the pid that was made for the bundleID. +// If bundleIDNSStr is NULL, then the system application will be messaged. + +static bool +CallBKSSystemServiceOpenApplication (NSString *bundleIDNSStr, NSDictionary *options, DNBError &error, pid_t *return_pid) +{ + // Now make our systemService: + BKSSystemService *system_service = [[BKSSystemService alloc] init]; + + if (bundleIDNSStr == nil) + { + bundleIDNSStr = [system_service systemApplicationBundleIdentifier]; + if (bundleIDNSStr == nil) + { + // Okay, no system app... + error.SetErrorString("No system application to message."); + return false; + } + } + + mach_port_t client_port = [system_service createClientPort]; + __block dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); + __block BKSOpenApplicationErrorCode open_app_error = BKSOpenApplicationErrorCodeNone; + bool wants_pid = (return_pid != NULL); + __block pid_t pid_in_block; + + const char *cstr = [bundleIDNSStr UTF8String]; + if (!cstr) + cstr = "<Unknown Bundle ID>"; + + DNBLog ("About to launch process for bundle ID: %s", cstr); + [system_service openApplication: bundleIDNSStr + options: options + clientPort: client_port + withResult: ^(NSError *bks_error) + { + // The system service will cleanup the client port we created for us. + if (bks_error) + open_app_error = (BKSOpenApplicationErrorCode)[bks_error code]; + + if (open_app_error == BKSOpenApplicationErrorCodeNone) + { + if (wants_pid) + { + pid_in_block = [system_service pidForApplication: bundleIDNSStr]; + DNBLog("In completion handler, got pid for bundle id, pid: %d.", pid_in_block); + DNBLogThreadedIf(LOG_PROCESS, "In completion handler, got pid for bundle id, pid: %d.", pid_in_block); + } + else + DNBLogThreadedIf (LOG_PROCESS, "In completion handler: success."); + } + else + { + const char *error_str = [[bks_error localizedDescription] UTF8String]; + DNBLogThreadedIf(LOG_PROCESS, "In completion handler for send event, got error \"%s\"(%d).", + error_str ? error_str : "<unknown error>", + open_app_error); + // REMOVE ME + DNBLogError ("In completion handler for send event, got error \"%s\"(%d).", + error_str ? error_str : "<unknown error>", + open_app_error); + } + + [system_service release]; + dispatch_semaphore_signal(semaphore); + } + + ]; + + const uint32_t timeout_secs = 9; + + dispatch_time_t timeout = dispatch_time(DISPATCH_TIME_NOW, timeout_secs * NSEC_PER_SEC); + + long success = dispatch_semaphore_wait(semaphore, timeout) == 0; + + dispatch_release(semaphore); + + if (!success) + { + DNBLogError("timed out trying to send openApplication to %s.", cstr); + error.SetError (BKS_OPEN_APPLICATION_TIMEOUT_ERROR, DNBError::Generic); + error.SetErrorString ("timed out trying to launch app"); + } + else if (open_app_error != BKSOpenApplicationErrorCodeNone) + { + SetBKSError (open_app_error, error); + DNBLogError("unable to launch the application with CFBundleIdentifier '%s' bks_error = %u", cstr, open_app_error); + success = false; + } + else if (wants_pid) + { + *return_pid = pid_in_block; + DNBLogThreadedIf (LOG_PROCESS, "Out of completion handler, pid from block %d and passing out: %d", pid_in_block, *return_pid); + } + + + return success; +} + +void +MachProcess::BKSCleanupAfterAttach (const void *attach_token, DNBError &err_str) +{ + bool success; + + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + + // Instead of rewriting CopyBundleIDForPath for NSStrings, we'll just use toll-free bridging here: + NSString *bundleIDNSStr = (NSString *) attach_token; + + // Okay, now let's assemble all these goodies into the BackBoardServices options mega-dictionary: + + // First we have the debug sub-dictionary: + NSMutableDictionary *debug_options = [NSMutableDictionary dictionary]; + [debug_options setObject: [NSNumber numberWithBool: YES] forKey: BKSDebugOptionKeyCancelDebugOnNextLaunch]; + + // That will go in the overall dictionary: + + NSMutableDictionary *options = [NSMutableDictionary dictionary]; + [options setObject: debug_options forKey: BKSOpenApplicationOptionKeyDebuggingOptions]; + + success = CallBKSSystemServiceOpenApplication(bundleIDNSStr, options, err_str, NULL); + + if (!success) + { + DNBLogError ("error trying to cancel debug on next launch for %s: %s", [bundleIDNSStr UTF8String], err_str.AsString()); + } + + [pool drain]; +} + +bool +AddEventDataToOptions (NSMutableDictionary *options, const char *event_data, DNBError &option_error) +{ + if (strcmp (event_data, "BackgroundContentFetching") == 0) + { + DNBLog("Setting ActivateForEvent key in options dictionary."); + NSDictionary *event_details = [NSDictionary dictionary]; + NSDictionary *event_dictionary = [NSDictionary dictionaryWithObject:event_details forKey:BKSActivateForEventOptionTypeBackgroundContentFetching]; + [options setObject: event_dictionary forKey: BKSOpenApplicationOptionKeyActivateForEvent]; + return true; + } + else + { + DNBLogError ("Unrecognized event type: %s. Ignoring.", event_data); + option_error.SetErrorString("Unrecognized event data."); + return false; + } + +} + +pid_t +MachProcess::BKSLaunchForDebug (const char *path, char const *argv[], char const *envp[], bool no_stdio, bool disable_aslr, const char *event_data, DNBError &launch_err) +{ + // Clear out and clean up from any current state + Clear(); + + DNBLogThreadedIf(LOG_PROCESS, "%s( '%s', argv)", __FUNCTION__, path); + + // Fork a child process for debugging + SetState(eStateLaunching); + m_pid = BKSForkChildForPTraceDebugging(path, argv, envp, no_stdio, disable_aslr, event_data, launch_err); + if (m_pid != 0) + { + m_flags |= eMachProcessFlagsUsingBKS; + m_path = path; + size_t i; + char const *arg; + for (i=0; (arg = argv[i]) != NULL; i++) + m_args.push_back(arg); + m_task.StartExceptionThread(launch_err); + + if (launch_err.Fail()) + { + if (launch_err.AsString() == NULL) + launch_err.SetErrorString("unable to start the exception thread"); + DNBLog ("Could not get inferior's Mach exception port, sending ptrace PT_KILL and exiting."); + ::ptrace (PT_KILL, m_pid, 0, 0); + m_pid = INVALID_NUB_PROCESS; + return INVALID_NUB_PROCESS; + } + + StartSTDIOThread(); + SetState (eStateAttaching); + int err = ::ptrace (PT_ATTACHEXC, m_pid, 0, 0); + if (err == 0) + { + m_flags |= eMachProcessFlagsAttached; + DNBLogThreadedIf(LOG_PROCESS, "successfully attached to pid %d", m_pid); + } + else + { + SetState (eStateExited); + DNBLogThreadedIf(LOG_PROCESS, "error: failed to attach to pid %d", m_pid); + } + } + return m_pid; +} + +pid_t +MachProcess::BKSForkChildForPTraceDebugging (const char *app_bundle_path, + char const *argv[], + char const *envp[], + bool no_stdio, + bool disable_aslr, + const char *event_data, + DNBError &launch_err) +{ + if (argv[0] == NULL) + return INVALID_NUB_PROCESS; + DNBLogThreadedIf(LOG_PROCESS, "%s( '%s', argv, %p)", __FUNCTION__, app_bundle_path, this); + + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + + size_t argc = 0; + // Count the number of arguments + while (argv[argc] != NULL) + argc++; + + // Enumerate the arguments + size_t first_launch_arg_idx = 1; + + NSMutableArray *launch_argv = nil; + + if (argv[first_launch_arg_idx]) + { + size_t launch_argc = argc > 0 ? argc - 1 : 0; + launch_argv = [NSMutableArray arrayWithCapacity: launch_argc]; + size_t i; + char const *arg; + NSString *launch_arg; + for (i=first_launch_arg_idx; (i < argc) && ((arg = argv[i]) != NULL); i++) + { + launch_arg = [NSString stringWithUTF8String: arg]; + // FIXME: Should we silently eat an argument that we can't convert into a UTF8 string? + if (launch_arg != nil) + [launch_argv addObject: launch_arg]; + else + break; + } + } + + NSMutableDictionary *launch_envp = nil; + if (envp[0]) + { + launch_envp = [[NSMutableDictionary alloc] init]; + const char *value; + int name_len; + NSString *name_string, *value_string; + + for (int i = 0; envp[i] != NULL; i++) + { + value = strstr (envp[i], "="); + + // If the name field is empty or there's no =, skip it. Somebody's messing with us. + if (value == NULL || value == envp[i]) + continue; + + name_len = value - envp[i]; + + // Now move value over the "=" + value++; + name_string = [[NSString alloc] initWithBytes: envp[i] length: name_len encoding: NSUTF8StringEncoding]; + value_string = [NSString stringWithUTF8String: value]; + [launch_envp setObject: value_string forKey: name_string]; + } + } + + NSString *stdio_path = nil; + NSFileManager *file_manager = [NSFileManager defaultManager]; + + PseudoTerminal pty; + if (!no_stdio) + { + PseudoTerminal::Error pty_err = pty.OpenFirstAvailableMaster(O_RDWR|O_NOCTTY); + if (pty_err == PseudoTerminal::success) + { + const char* slave_name = pty.SlaveName(); + DNBLogThreadedIf(LOG_PROCESS, "%s() successfully opened master pty, slave is %s", __FUNCTION__, slave_name); + if (slave_name && slave_name[0]) + { + ::chmod (slave_name, S_IRWXU | S_IRWXG | S_IRWXO); + stdio_path = [file_manager stringWithFileSystemRepresentation: slave_name length: strlen(slave_name)]; + } + } + } + + if (stdio_path == nil) + { + const char *null_path = "/dev/null"; + stdio_path = [file_manager stringWithFileSystemRepresentation: null_path length: strlen(null_path)]; + } + + CFStringRef bundleIDCFStr = CopyBundleIDForPath (app_bundle_path, launch_err); + if (bundleIDCFStr == NULL) + { + [pool drain]; + return INVALID_NUB_PROCESS; + } + + // Instead of rewriting CopyBundleIDForPath for NSStrings, we'll just use toll-free bridging here: + NSString *bundleIDNSStr = (NSString *) bundleIDCFStr; + + // Okay, now let's assemble all these goodies into the BackBoardServices options mega-dictionary: + + // First we have the debug sub-dictionary: + NSMutableDictionary *debug_options = [NSMutableDictionary dictionary]; + if (launch_argv != nil) + [debug_options setObject: launch_argv forKey: BKSDebugOptionKeyArguments]; + if (launch_envp != nil) + [debug_options setObject: launch_envp forKey: BKSDebugOptionKeyEnvironment]; + + [debug_options setObject: stdio_path forKey: BKSDebugOptionKeyStandardOutPath]; + [debug_options setObject: stdio_path forKey: BKSDebugOptionKeyStandardErrorPath]; + [debug_options setObject: [NSNumber numberWithBool: YES] forKey: BKSDebugOptionKeyWaitForDebugger]; + if (disable_aslr) + [debug_options setObject: [NSNumber numberWithBool: YES] forKey: BKSDebugOptionKeyDisableASLR]; + + // That will go in the overall dictionary: + + NSMutableDictionary *options = [NSMutableDictionary dictionary]; + [options setObject: debug_options forKey: BKSOpenApplicationOptionKeyDebuggingOptions]; + + // For now we only support one kind of event: the "fetch" event, which is indicated by the fact that its data + // is an empty dictionary. + if (event_data != NULL && *event_data != '\0') + { + if (!AddEventDataToOptions(options, event_data, launch_err)) + { + [pool drain]; + return INVALID_NUB_PROCESS; + } + } + + // And there are some other options at the top level in this dictionary: + [options setObject: [NSNumber numberWithBool: YES] forKey: BKSOpenApplicationOptionKeyUnlockDevice]; + + pid_t return_pid = INVALID_NUB_PROCESS; + bool success = CallBKSSystemServiceOpenApplication(bundleIDNSStr, options, launch_err, &return_pid); + + if (success) + { + int master_fd = pty.ReleaseMasterFD(); + SetChildFileDescriptors(master_fd, master_fd, master_fd); + CFString::UTF8(bundleIDCFStr, m_bundle_id); + } + + [pool drain]; + + return return_pid; +} + +bool +MachProcess::BKSSendEvent (const char *event_data, DNBError &send_err) +{ + bool return_value = true; + + if (event_data == NULL || *event_data == '\0') + { + DNBLogError ("SendEvent called with NULL event data."); + send_err.SetErrorString("SendEvent called with empty event data"); + return false; + } + + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + + if (strcmp (event_data, "BackgroundApplication") == 0) + { + // This is an event I cooked up. What you actually do is foreground the system app, so: + return_value = CallBKSSystemServiceOpenApplication(nil, nil, send_err, NULL); + if (!return_value) + { + DNBLogError ("Failed to background application, error: %s.", send_err.AsString()); + } + } + else + { + if (m_bundle_id.empty()) + { + // See if we can figure out the bundle ID for this PID: + + DNBLogError ("Tried to send event \"%s\" to a process that has no bundle ID.", event_data); + return false; + } + + NSString *bundleIDNSStr = [NSString stringWithUTF8String:m_bundle_id.c_str()]; + + NSMutableDictionary *options = [NSMutableDictionary dictionary]; + + if (!AddEventDataToOptions(options, event_data, send_err)) + { + [pool drain]; + return false; + } + + + return_value = CallBKSSystemServiceOpenApplication(bundleIDNSStr, options, send_err, NULL); + + if (!return_value) + { + DNBLogError ("Failed to send event: %s, error: %s.", event_data, send_err.AsString()); + } + } + + [pool drain]; + return return_value; +} +#endif // WITH_BKS diff --git a/lldb/tools/debugserver/source/MacOSX/MachTask.cpp b/lldb/tools/debugserver/source/MacOSX/MachTask.mm index 30fc22c7dfd..8b3a4f076e1 100644 --- a/lldb/tools/debugserver/source/MacOSX/MachTask.cpp +++ b/lldb/tools/debugserver/source/MacOSX/MachTask.mm @@ -45,6 +45,15 @@ #endif +#ifdef WITH_BKS +extern "C" +{ + #import <Foundation/Foundation.h> + #import <BackBoardServices/BackBoardServices.h> + #import <BackBoardServices/BKSWatchdogAssertion.h> +} +#endif + //---------------------------------------------------------------------- // MachTask constructor //---------------------------------------------------------------------- @@ -693,7 +702,7 @@ MachTask::ExceptionThread (void *arg) task_t task = mach_task->TaskPort(); mach_msg_timeout_t periodic_timeout = 0; -#ifdef WITH_SPRINGBOARD +#if defined (WITH_SPRINGBOARD) && !defined (WITH_BKS) mach_msg_timeout_t watchdog_elapsed = 0; mach_msg_timeout_t watchdog_timeout = 60 * 1000; pid_t pid = mach_proc->ProcessID(); @@ -723,7 +732,17 @@ MachTask::ExceptionThread (void *arg) if (periodic_timeout == 0 || periodic_timeout > watchdog_timeout) periodic_timeout = watchdog_timeout; } -#endif // #ifdef WITH_SPRINGBOARD +#endif // #if defined (WITH_SPRINGBOARD) && !defined (WITH_BKS) + +#ifdef WITH_BKS + CFReleaser<BKSWatchdogAssertionRef> watchdog; + if (mach_proc->ProcessUsingBackBoard()) + { + pid_t pid = mach_proc->ProcessID(); + CFAllocatorRef alloc = kCFAllocatorDefault; + watchdog.reset(::BKSWatchdogAssertionCreateForPID(alloc, pid)); + } +#endif // #ifdef WITH_BKS while (mach_task->ExceptionPortIsValid()) { @@ -804,7 +823,7 @@ MachTask::ExceptionThread (void *arg) continue; } -#ifdef WITH_SPRINGBOARD +#if defined (WITH_SPRINGBOARD) && !defined (WITH_BKS) if (watchdog.get()) { watchdog_elapsed += periodic_timeout; @@ -832,7 +851,7 @@ MachTask::ExceptionThread (void *arg) } } -#ifdef WITH_SPRINGBOARD +#if defined (WITH_SPRINGBOARD) && !defined (WITH_BKS) if (watchdog.get()) { // TODO: change SBSWatchdogAssertionRelease to SBSWatchdogAssertionCancel when we @@ -842,7 +861,7 @@ MachTask::ExceptionThread (void *arg) DNBLogThreadedIf(LOG_TASK, "::SBSWatchdogAssertionRelease(%p)", watchdog.get()); ::SBSWatchdogAssertionRelease (watchdog.get()); } -#endif // #ifdef WITH_SPRINGBOARD +#endif // #if defined (WITH_SPRINGBOARD) && !defined (WITH_BKS) DNBLogThreadedIf(LOG_EXCEPTIONS, "MachTask::%s (%p): thread exiting...", __FUNCTION__, arg); return NULL; diff --git a/lldb/tools/debugserver/source/MacOSX/MachThreadList.cpp b/lldb/tools/debugserver/source/MacOSX/MachThreadList.cpp index adad95f2f7c..634d2576332 100644 --- a/lldb/tools/debugserver/source/MacOSX/MachThreadList.cpp +++ b/lldb/tools/debugserver/source/MacOSX/MachThreadList.cpp @@ -271,7 +271,6 @@ MachThreadList::UpdateThreadList(MachProcess *process, bool update, MachThreadLi DNBLogThreadedIf (LOG_THREAD, "MachThreadList::UpdateThreadList (pid = %4.4x, update = %u) process stop count = %u", process->ProcessID(), update, process->StopCount()); PTHREAD_MUTEX_LOCKER (locker, m_threads_mutex); -#if defined (__i386__) || defined (__x86_64__) if (process->StopCount() == 0) { int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process->ProcessID() }; @@ -283,12 +282,18 @@ MachThreadList::UpdateThreadList(MachProcess *process, bool update, MachThreadLi if (processInfo.kp_proc.p_flag & P_LP64) is_64_bit = true; } +#if defined (__i386__) || defined (__x86_64__) if (is_64_bit) DNBArchProtocol::SetArchitecture(CPU_TYPE_X86_64); else DNBArchProtocol::SetArchitecture(CPU_TYPE_I386); - } +#elif defined (__arm__) || defined (__arm64__) + if (is_64_bit) + DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM64); + else + DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM); #endif + } if (m_threads.empty() || update) { diff --git a/lldb/tools/debugserver/source/MacOSX/MachVMMemory.cpp b/lldb/tools/debugserver/source/MacOSX/MachVMMemory.cpp index f11906afd63..fc4108d94ea 100644 --- a/lldb/tools/debugserver/source/MacOSX/MachVMMemory.cpp +++ b/lldb/tools/debugserver/source/MacOSX/MachVMMemory.cpp @@ -311,11 +311,18 @@ static bool InSharedRegion(mach_vm_address_t addr, cpu_type_t type) mach_vm_address_t base = 0, size = 0; switch(type) { +#if defined (CPU_TYPE_ARM64) && defined (SHARED_REGION_BASE_ARM64) + case CPU_TYPE_ARM64: + base = SHARED_REGION_BASE_ARM64; + size = SHARED_REGION_SIZE_ARM64; + break; +#endif + case CPU_TYPE_ARM: base = SHARED_REGION_BASE_ARM; size = SHARED_REGION_SIZE_ARM; break; - + case CPU_TYPE_X86_64: base = SHARED_REGION_BASE_X86_64; size = SHARED_REGION_SIZE_X86_64; diff --git a/lldb/tools/debugserver/source/MacOSX/arm/DNBArchImpl.cpp b/lldb/tools/debugserver/source/MacOSX/arm/DNBArchImpl.cpp index e9781717d94..dc149666f0c 100644 --- a/lldb/tools/debugserver/source/MacOSX/arm/DNBArchImpl.cpp +++ b/lldb/tools/debugserver/source/MacOSX/arm/DNBArchImpl.cpp @@ -11,7 +11,7 @@ // //===----------------------------------------------------------------------===// -#if defined (__arm__) +#if defined (__arm__) || defined (__arm64__) #include "MacOSX/arm/DNBArchImpl.h" #include "MacOSX/MachProcess.h" @@ -23,6 +23,7 @@ #include "ARM_GCC_Registers.h" #include "ARM_DWARF_Registers.h" +#include <inttypes.h> #include <sys/sysctl.h> // BCR address match type @@ -65,6 +66,13 @@ static const uint8_t g_arm_breakpoint_opcode[] = { 0xFE, 0xDE, 0xFF, 0xE7 }; static const uint8_t g_thumb_breakpoint_opcode[] = { 0xFE, 0xDE }; +// A watchpoint may need to be implemented using two watchpoint registers. +// e.g. watching an 8-byte region when the device can only watch 4-bytes. +// +// This stores the lo->hi mappings. It's safe to initialize to all 0's +// since hi > lo and therefore LoHi[i] cannot be 0. +static uint32_t LoHi[16] = { 0 }; + // ARM constants used during decoding #define REG_RD 0 #define LDM_REGLIST 1 @@ -278,9 +286,15 @@ DNBArchMachARM::GetDBGState(bool force) return KERN_SUCCESS; // Read the registers from our thread +#if defined (ARM_DEBUG_STATE32) && defined (__arm64__) + mach_msg_type_number_t count = ARM_DEBUG_STATE32_COUNT; + kern_return_t kret = ::thread_get_state(m_thread->MachPortNumber(), ARM_DEBUG_STATE32, (thread_state_t)&m_state.dbg, &count); +#else mach_msg_type_number_t count = ARM_DEBUG_STATE_COUNT; kern_return_t kret = ::thread_get_state(m_thread->MachPortNumber(), ARM_DEBUG_STATE, (thread_state_t)&m_state.dbg, &count); +#endif m_state.SetError(set, Read, kret); + return kret; } @@ -318,6 +332,15 @@ kern_return_t DNBArchMachARM::SetDBGState(bool also_set_on_task) { int set = e_regSetDBG; +#if defined (ARM_DEBUG_STATE32) && defined (__arm64__) + kern_return_t kret = ::thread_set_state (m_thread->MachPortNumber(), ARM_DEBUG_STATE32, (thread_state_t)&m_state.dbg, ARM_DEBUG_STATE32_COUNT); + if (also_set_on_task) + { + kern_return_t task_kret = ::task_set_state (m_thread->Process()->Task().TaskPort(), ARM_DEBUG_STATE32, (thread_state_t)&m_state.dbg, ARM_DEBUG_STATE32_COUNT); + if (task_kret != KERN_SUCCESS) + DNBLogThreadedIf(LOG_WATCHPOINTS, "DNBArchMachARM::SetDBGState failed to set debug control register state: 0x%8.8x.", kret); + } +#else kern_return_t kret = ::thread_set_state (m_thread->MachPortNumber(), ARM_DEBUG_STATE, (thread_state_t)&m_state.dbg, ARM_DEBUG_STATE_COUNT); if (also_set_on_task) { @@ -325,6 +348,7 @@ DNBArchMachARM::SetDBGState(bool also_set_on_task) if (task_kret != KERN_SUCCESS) DNBLogThreadedIf(LOG_WATCHPOINTS, "DNBArchMachARM::SetDBGState failed to set debug control register state: 0x%8.8x.", kret); } +#endif m_state.SetError(set, Write, kret); // Set the current write error for this register set m_state.InvalidateRegisterSetState(set); // Invalidate the current register state in case registers are read back differently @@ -362,7 +386,7 @@ DNBArchMachARM::ThreadWillResume() return; } - DisableHardwareWatchpoint0(m_watchpoint_hw_index, true, false); + DisableHardwareWatchpoint(m_watchpoint_hw_index, false); DNBLogThreadedIf(LOG_WATCHPOINTS, "DNBArchMachARM::ThreadWillResume() DisableHardwareWatchpoint(%d) called", m_watchpoint_hw_index); @@ -399,7 +423,7 @@ DNBArchMachARM::ThreadDidStop() { if (m_watchpoint_did_occur && m_watchpoint_hw_index >= 0) { - EnableHardwareWatchpoint0(m_watchpoint_hw_index, true, false); + ReenableHardwareWatchpoint(m_watchpoint_hw_index); m_watchpoint_resume_single_step_enabled = false; m_watchpoint_did_occur = false; m_watchpoint_hw_index = -1; @@ -443,18 +467,24 @@ DNBArchMachARM::NotifyException(MachException::Data& exc) case EXC_BREAKPOINT: if (exc.exc_data.size() == 2 && exc.exc_data[0] == EXC_ARM_DA_DEBUG) { - // exc_code = EXC_ARM_DA_DEBUG - // - // Check whether this corresponds to a watchpoint hit event. - // If yes, retrieve the exc_sub_code as the data break address. - if (!HasWatchpointOccurred()) - break; - // The data break address is passed as exc_data[1]. nub_addr_t addr = exc.exc_data[1]; // Find the hardware index with the side effect of possibly massaging the // addr to return the starting address as seen from the debugger side. uint32_t hw_index = GetHardwareWatchpointHit(addr); + DNBLogThreadedIf(LOG_WATCHPOINTS, "DNBArchMachARM::NotifyException watchpoint %d was hit on address 0x%llx", hw_index, (uint64_t) addr); + const int num_watchpoints = NumSupportedHardwareWatchpoints (); + for (int i = 0; i < num_watchpoints; i++) + { + if (LoHi[i] != 0 + && LoHi[i] == hw_index + && LoHi[i] != i + && GetWatchpointAddressByIndex (i) != INVALID_NUB_ADDRESS) + { + addr = GetWatchpointAddressByIndex (i); + DNBLogThreadedIf(LOG_WATCHPOINTS, "DNBArchMachARM::NotifyException It is a linked watchpoint; rewritten to index %d addr 0x%llx", LoHi[i], (uint64_t) addr); + } + } if (hw_index != INVALID_NUB_HW_INDEX) { m_watchpoint_did_occur = true; @@ -492,7 +522,6 @@ DNBArchMachARM::StepNotComplete () return false; } - // Set the single step bit in the processor status register. kern_return_t DNBArchMachARM::EnableHardwareSingleStep (bool enable) @@ -516,6 +545,22 @@ DNBArchMachARM::EnableHardwareSingleStep (bool enable) return err.Error(); } +// The use of __arm64__ here is not ideal. If debugserver is running on +// an armv8 device, regardless of whether it was built for arch arm or arch arm64, +// it needs to use the MDSCR_EL1 SS bit to single instruction step. + +#if defined (__arm64__) + if (enable) + { + DNBLogThreadedIf(LOG_STEP, "%s: Setting MDSCR_EL1 Single Step bit at pc 0x%llx", __FUNCTION__, (uint64_t) m_state.context.gpr.__pc); + m_state.dbg.__mdscr_el1 |= 1; // Set bit 0 (single step, SS) in the MDSCR_EL1. + } + else + { + DNBLogThreadedIf(LOG_STEP, "%s: Clearing MDSCR_EL1 Single Step bit at pc 0x%llx", __FUNCTION__, (uint64_t) m_state.context.gpr.__pc); + m_state.dbg.__mdscr_el1 &= ~(1ULL); // Clear bit 0 (single step, SS) in the MDSCR_EL1. + } +#else const uint32_t i = 0; if (enable) { @@ -577,6 +622,7 @@ DNBArchMachARM::EnableHardwareSingleStep (bool enable) // Just restore the state we had before we did single stepping m_state.dbg = m_dbg_save; } +#endif return SetDBGState(false); } @@ -677,6 +723,7 @@ DNBArchMachARM::NumSupportedHardwareBreakpoints() } else { +#if !defined (__arm64__) // Read the DBGDIDR to get the number of available hardware breakpoints // However, in some of our current armv7 processors, hardware // breakpoints/watchpoints were not properly connected. So detect those @@ -708,6 +755,7 @@ DNBArchMachARM::NumSupportedHardwareBreakpoints() g_num_supported_hw_breakpoints = numBRPs; } } +#endif } } return g_num_supported_hw_breakpoints; @@ -736,6 +784,7 @@ DNBArchMachARM::NumSupportedHardwareWatchpoints() } else { +#if !defined (__arm64__) // Read the DBGDIDR to get the number of available hardware breakpoints // However, in some of our current armv7 processors, hardware // breakpoints/watchpoints were not properly connected. So detect those @@ -766,6 +815,7 @@ DNBArchMachARM::NumSupportedHardwareWatchpoints() g_num_supported_hw_watchpoints = numWRPs; } } +#endif } } return g_num_supported_hw_watchpoints; @@ -873,9 +923,16 @@ DNBArchMachARM::DisableHardwareBreakpoint (uint32_t hw_index) return false; } -// This stores the lo->hi mappings. It's safe to initialize to all 0's -// since hi > lo and therefore LoHi[i] cannot be 0. -static uint32_t LoHi[16] = { 0 }; +// ARM v7 watchpoints may be either word-size or double-word-size. +// It's implementation defined which they can handle. It looks like on an +// armv8 device, armv7 processes can watch dwords. But on a genuine armv7 +// device I tried, only word watchpoints are supported. + +#if defined (__arm64__) +#define WATCHPOINTS_ARE_DWORD 1 +#else +#undef WATCHPOINTS_ARE_DWORD +#endif uint32_t DNBArchMachARM::EnableHardwareWatchpoint (nub_addr_t addr, nub_size_t size, bool read, bool write, bool also_set_on_task) @@ -893,16 +950,55 @@ DNBArchMachARM::EnableHardwareWatchpoint (nub_addr_t addr, nub_size_t size, bool if (read == false && write == false) return INVALID_NUB_HW_INDEX; - // Divide-and-conquer for size == 8. - if (size == 8) + // Otherwise, can't watch more than 8 bytes per WVR/WCR pair + if (size > 8) + return INVALID_NUB_HW_INDEX; + + // Treat arm watchpoints as having an 8-byte alignment requirement. You can put a watchpoint on a 4-byte + // offset address but you can only watch 4 bytes with that watchpoint. + + // arm watchpoints on an 8-byte (double word) aligned addr can watch any bytes in that + // 8-byte long region of memory. They can watch the 1st byte, the 2nd byte, 3rd byte, etc, or any + // combination therein by setting the bits in the BAS [12:5] (Byte Address Select) field of + // the DBGWCRn_EL1 reg for the watchpoint. + + // If the MASK [28:24] bits in the DBGWCRn_EL1 allow a single watchpoint to monitor a larger region + // of memory (16 bytes, 32 bytes, or 2GB) but the Byte Address Select bitfield then selects a larger + // range of bytes, instead of individual bytes. See the ARMv8 Debug Architecture manual for details. + // This implementation does not currently use the MASK bits; the largest single region watched by a single + // watchpoint right now is 8-bytes. + +#if defined (WATCHPOINTS_ARE_DWORD) + nub_addr_t aligned_wp_address = addr & ~0x7; + uint32_t addr_dword_offset = addr & 0x7; + const int max_watchpoint_size = 8; +#else + nub_addr_t aligned_wp_address = addr & ~0x3; + uint32_t addr_dword_offset = addr & 0x3; + const int max_watchpoint_size = 4; +#endif + + DNBLogThreadedIf(LOG_WATCHPOINTS, "DNBArchMachARM::EnableHardwareWatchpoint aligned_wp_address is 0x%llx and addr_dword_offset is 0x%x", (uint64_t)aligned_wp_address, addr_dword_offset); + + // Do we need to split up this logical watchpoint into two hardware watchpoint + // registers? + // e.g. a watchpoint of length 4 on address 6. We need do this with + // one watchpoint on address 0 with bytes 6 & 7 being monitored + // one watchpoint on address 8 with bytes 0, 1, 2, 3 being monitored + + if (addr_dword_offset + size > max_watchpoint_size) { - uint32_t lo = EnableHardwareWatchpoint(addr, 4, read, write, also_set_on_task); + DNBLogThreadedIf(LOG_WATCHPOINTS, "DNBArchMachARM::EnableHardwareWatchpoint(addr = 0x%8.8llx, size = %zu) needs two hardware watchpoints slots to monitor", (uint64_t)addr, size); + int low_watchpoint_size = max_watchpoint_size - addr_dword_offset; + int high_watchpoint_size = addr_dword_offset + size - max_watchpoint_size; + + uint32_t lo = EnableHardwareWatchpoint(addr, low_watchpoint_size, read, write, also_set_on_task); if (lo == INVALID_NUB_HW_INDEX) return INVALID_NUB_HW_INDEX; - uint32_t hi = EnableHardwareWatchpoint(addr+4, 4, read, write, also_set_on_task); + uint32_t hi = EnableHardwareWatchpoint (aligned_wp_address + max_watchpoint_size, high_watchpoint_size, read, write, also_set_on_task); if (hi == INVALID_NUB_HW_INDEX) { - DisableHardwareWatchpoint(lo, also_set_on_task); + DisableHardwareWatchpoint (lo, also_set_on_task); return INVALID_NUB_HW_INDEX; } // Tag this lo->hi mapping in our database. @@ -910,36 +1006,16 @@ DNBArchMachARM::EnableHardwareWatchpoint (nub_addr_t addr, nub_size_t size, bool return lo; } - // Otherwise, can't watch more than 4 bytes per WVR/WCR pair - if (size > 4) - return INVALID_NUB_HW_INDEX; + // At this point + // 1 aligned_wp_address is the requested address rounded down to 8-byte alignment + // 2 addr_dword_offset is the offset into that double word (8-byte) region that we are watching + // 3 size is the number of bytes within that 8-byte region that we are watching - // We can only watch up to four bytes that follow a 4 byte aligned address - // per watchpoint register pair. Since we can only watch until the next 4 - // byte boundary, we need to make sure we can properly encode this. - - // addr_word_offset = addr % 4, i.e, is in set([0, 1, 2, 3]) - // - // +---+---+---+---+ - // | 0 | 1 | 2 | 3 | - // +---+---+---+---+ - // ^ - // | - // word address (4-byte aligned) = addr & 0xFFFFFFFC => goes into WVR - // - // examples: - // 1. addr_word_offset = 1, size = 1 to watch a uint_8 => byte_mask = (0b0001 << 1) = 0b0010 - // 2. addr_word_offset = 2, size = 2 to watch a uint_16 => byte_mask = (0b0011 << 2) = 0b1100 - // - // where byte_mask goes into WCR[8:5] - - uint32_t addr_word_offset = addr % 4; - DNBLogThreadedIf(LOG_WATCHPOINTS, "DNBArchMachARM::EnableHardwareWatchpoint() - addr_word_offset = 0x%8.8x", addr_word_offset); - - uint32_t byte_mask = ((1u << size) - 1u) << addr_word_offset; - DNBLogThreadedIf(LOG_WATCHPOINTS, "DNBArchMachARM::EnableHardwareWatchpoint() - byte_mask = 0x%8.8x", byte_mask); - if (byte_mask > 0xfu) - return INVALID_NUB_HW_INDEX; + // Set the Byte Address Selects bits DBGWCRn_EL1 bits [12:5] based on the above. + // The bit shift and negation operation will give us 0b11 for 2, 0b1111 for 4, etc, up to 0b11111111 for 8. + // then we shift those bits left by the offset into this dword that we are interested in. + // e.g. if we are watching bytes 4,5,6,7 in a dword we want a BAS of 0b11110000. + uint32_t byte_address_select = ((1 << size) - 1) << addr_dword_offset; // Read the debug state kern_return_t kret = GetDBGState(true); @@ -960,10 +1036,14 @@ DNBArchMachARM::EnableHardwareWatchpoint (nub_addr_t addr, nub_size_t size, bool { //DumpDBGState(m_state.dbg); - // Make the byte_mask into a valid Byte Address Select mask - uint32_t byte_address_select = byte_mask << 5; + // Clear any previous LoHi joined-watchpoint that may have been in use + LoHi[i] = 0; + + // shift our Byte Address Select bits up to the correct bit range for the DBGWCRn_EL1 + byte_address_select = byte_address_select << 5; + // Make sure bits 1:0 are clear in our address - m_state.dbg.__wvr[i] = addr & ~((nub_addr_t)3); // DVA (Data Virtual Address) + m_state.dbg.__wvr[i] = aligned_wp_address; // DVA (Data Virtual Address) m_state.dbg.__wcr[i] = byte_address_select | // Which bytes that follow the DVA that we will watch S_USER | // Stop only in user mode (read ? WCR_LOAD : 0) | // Stop on read access? @@ -972,6 +1052,8 @@ DNBArchMachARM::EnableHardwareWatchpoint (nub_addr_t addr, nub_size_t size, bool DNBLogThreadedIf(LOG_WATCHPOINTS, "DNBArchMachARM::EnableHardwareWatchpoint() adding watchpoint on address 0x%llx with control register value 0x%x", (uint64_t) m_state.dbg.__wvr[i], (uint32_t) m_state.dbg.__wcr[i]); + // The kernel will set the MDE_ENABLE bit in the MDSCR_EL1 for us automatically, don't need to do it here. + kret = SetDBGState(also_set_on_task); //DumpDBGState(m_state.dbg); @@ -989,29 +1071,42 @@ DNBArchMachARM::EnableHardwareWatchpoint (nub_addr_t addr, nub_size_t size, bool } bool -DNBArchMachARM::EnableHardwareWatchpoint0 (uint32_t hw_index, bool Delegate, bool also_set_on_task) +DNBArchMachARM::ReenableHardwareWatchpoint (uint32_t hw_index) +{ + // If this logical watchpoint # is actually implemented using + // two hardware watchpoint registers, re-enable both of them. + + if (hw_index < NumSupportedHardwareWatchpoints() && LoHi[hw_index]) + { + return ReenableHardwareWatchpoint_helper (hw_index) && ReenableHardwareWatchpoint_helper (LoHi[hw_index]); + } + else + { + return ReenableHardwareWatchpoint_helper (hw_index); + } +} + +bool +DNBArchMachARM::ReenableHardwareWatchpoint_helper (uint32_t hw_index) { kern_return_t kret = GetDBGState(false); if (kret != KERN_SUCCESS) return false; - const uint32_t num_hw_points = NumSupportedHardwareWatchpoints(); if (hw_index >= num_hw_points) return false; - if (Delegate && LoHi[hw_index]) { - // Enable lo and hi watchpoint hardware indexes. - return EnableHardwareWatchpoint0(hw_index, false, also_set_on_task) && - EnableHardwareWatchpoint0(LoHi[hw_index], false, also_set_on_task); - } + m_state.dbg.__wvr[hw_index] = m_disabled_watchpoints[hw_index].addr; + m_state.dbg.__wcr[hw_index] = m_disabled_watchpoints[hw_index].control; - m_state.dbg.__wcr[hw_index] |= (nub_addr_t)WCR_ENABLE; - DNBLogThreadedIf(LOG_WATCHPOINTS, "DNBArchMachARM::EnableHardwareWatchpoint( %u ) - WVR%u = 0x%8.8x WCR%u = 0x%8.8x", + DNBLogThreadedIf(LOG_WATCHPOINTS, "DNBArchMachARM::EnableHardwareWatchpoint( %u ) - WVR%u = 0x%8.8llx WCR%u = 0x%8.8llx", hw_index, hw_index, - m_state.dbg.__wvr[hw_index], + (uint64_t) m_state.dbg.__wvr[hw_index], hw_index, - m_state.dbg.__wcr[hw_index]); + (uint64_t) m_state.dbg.__wcr[hw_index]); + + // The kernel will set the MDE_ENABLE bit in the MDSCR_EL1 for us automatically, don't need to do it here. kret = SetDBGState(false); @@ -1021,10 +1116,18 @@ DNBArchMachARM::EnableHardwareWatchpoint0 (uint32_t hw_index, bool Delegate, boo bool DNBArchMachARM::DisableHardwareWatchpoint (uint32_t hw_index, bool also_set_on_task) { - return DisableHardwareWatchpoint0(hw_index, true, also_set_on_task); + if (hw_index < NumSupportedHardwareWatchpoints() && LoHi[hw_index]) + { + return DisableHardwareWatchpoint_helper (hw_index, also_set_on_task) && DisableHardwareWatchpoint_helper (LoHi[hw_index], also_set_on_task); + } + else + { + return DisableHardwareWatchpoint_helper (hw_index, also_set_on_task); + } } + bool -DNBArchMachARM::DisableHardwareWatchpoint0 (uint32_t hw_index, bool Delegate, bool also_set_on_task) +DNBArchMachARM::DisableHardwareWatchpoint_helper (uint32_t hw_index, bool also_set_on_task) { kern_return_t kret = GetDBGState(false); if (kret != KERN_SUCCESS) @@ -1034,19 +1137,17 @@ DNBArchMachARM::DisableHardwareWatchpoint0 (uint32_t hw_index, bool Delegate, bo if (hw_index >= num_hw_points) return false; - if (Delegate && LoHi[hw_index]) { - // Disable lo and hi watchpoint hardware indexes. - return DisableHardwareWatchpoint0(hw_index, false, also_set_on_task) && - DisableHardwareWatchpoint0(LoHi[hw_index], false, also_set_on_task); - } + m_disabled_watchpoints[hw_index].addr = m_state.dbg.__wvr[hw_index]; + m_disabled_watchpoints[hw_index].control = m_state.dbg.__wcr[hw_index]; - m_state.dbg.__wcr[hw_index] &= ~((nub_addr_t)WCR_ENABLE); - DNBLogThreadedIf(LOG_WATCHPOINTS, "DNBArchMachARM::DisableHardwareWatchpoint( %u ) - WVR%u = 0x%8.8x WCR%u = 0x%8.8x", + m_state.dbg.__wvr[hw_index] = 0; + m_state.dbg.__wcr[hw_index] = 0; + DNBLogThreadedIf(LOG_WATCHPOINTS, "DNBArchMachARM::DisableHardwareWatchpoint( %u ) - WVR%u = 0x%8.8llx WCR%u = 0x%8.8llx", hw_index, hw_index, - m_state.dbg.__wvr[hw_index], + (uint64_t) m_state.dbg.__wvr[hw_index], hw_index, - m_state.dbg.__wcr[hw_index]); + (uint64_t) m_state.dbg.__wcr[hw_index]); kret = SetDBGState(also_set_on_task); @@ -1079,7 +1180,11 @@ DNBArchMachARM::GetHardwareWatchpointHit(nub_addr_t &addr) DNBLogThreadedIf(LOG_WATCHPOINTS, "DNBArchMachARM::GetHardwareWatchpointHit() addr = 0x%llx", (uint64_t)addr); // This is the watchpoint value to match against, i.e., word address. +#if defined (WATCHPOINTS_ARE_DWORD) + nub_addr_t wp_val = addr & ~((nub_addr_t)7); +#else nub_addr_t wp_val = addr & ~((nub_addr_t)3); +#endif if (kret == KERN_SUCCESS) { DBG &debug_state = m_state.dbg; @@ -1091,7 +1196,11 @@ DNBArchMachARM::GetHardwareWatchpointHit(nub_addr_t &addr) "DNBArchMachARM::GetHardwareWatchpointHit() slot: %u (addr = 0x%llx).", i, (uint64_t)wp_addr); if (wp_val == wp_addr) { +#if defined (WATCHPOINTS_ARE_DWORD) + uint32_t byte_mask = bits(debug_state.__wcr[i], 12, 5); +#else uint32_t byte_mask = bits(debug_state.__wcr[i], 8, 5); +#endif // Sanity check the byte_mask, first. if (LowestBitSet(byte_mask) < 0) @@ -1106,36 +1215,18 @@ DNBArchMachARM::GetHardwareWatchpointHit(nub_addr_t &addr) return INVALID_NUB_HW_INDEX; } -// ThreadWillResume() calls this to clear bits[5:2] (Method of entry bits) of -// the Debug Status and Control Register (DSCR). -// -// b0010 = a watchpoint occurred -// b0000 is the reset value -void -DNBArchMachARM::ClearWatchpointOccurred() -{ - uint32_t register_DBGDSCR; - asm("mrc p14, 0, %0, c0, c1, 0" : "=r" (register_DBGDSCR)); - if (bits(register_DBGDSCR, 5, 2) == WATCHPOINT_OCCURRED) - { - uint32_t mask = ~(0xF << 2); - register_DBGDSCR &= mask; - asm("mcr p14, 0, %0, c0, c1, 0" : "=r" (register_DBGDSCR)); - } - return; -} - -// NotifyException() calls this to double check that a watchpoint has occurred -// by inspecting the bits[5:2] field of the Debug Status and Control Register -// (DSCR). -// -// b0010 = a watchpoint occurred -bool -DNBArchMachARM::HasWatchpointOccurred() +nub_addr_t +DNBArchMachARM::GetWatchpointAddressByIndex (uint32_t hw_index) { - uint32_t register_DBGDSCR; - asm("mrc p14, 0, %0, c0, c1, 0" : "=r" (register_DBGDSCR)); - return (bits(register_DBGDSCR, 5, 2) == WATCHPOINT_OCCURRED); + kern_return_t kret = GetDBGState(true); + if (kret != KERN_SUCCESS) + return INVALID_NUB_ADDRESS; + const uint32_t num = NumSupportedHardwareWatchpoints(); + if (hw_index >= num) + return INVALID_NUB_ADDRESS; + if (IsWatchpointEnabled (m_state.dbg, hw_index)) + return GetWatchAddress (m_state.dbg, hw_index); + return INVALID_NUB_ADDRESS; } bool diff --git a/lldb/tools/debugserver/source/MacOSX/arm/DNBArchImpl.h b/lldb/tools/debugserver/source/MacOSX/arm/DNBArchImpl.h index 8cda8025395..4d4609184e8 100644 --- a/lldb/tools/debugserver/source/MacOSX/arm/DNBArchImpl.h +++ b/lldb/tools/debugserver/source/MacOSX/arm/DNBArchImpl.h @@ -14,7 +14,7 @@ #ifndef __DebugNubArchMachARM_h__ #define __DebugNubArchMachARM_h__ -#if defined (__arm__) +#if defined (__arm__) || defined (__arm64__) #include "DNBArch.h" @@ -30,6 +30,7 @@ public: DNBArchMachARM(MachThread *thread) : m_thread(thread), m_state(), + m_disabled_watchpoints(), m_hw_single_chained_step_addr(INVALID_NUB_ADDRESS), m_last_decode_pc(INVALID_NUB_ADDRESS), m_watchpoint_hw_index(-1), @@ -37,6 +38,7 @@ public: m_watchpoint_resume_single_step_enabled(false), m_saved_register_states() { + m_disabled_watchpoints.resize (16); memset(&m_dbg_save, 0, sizeof(m_dbg_save)); #if defined (USE_ARM_DISASSEMBLER_FRAMEWORK) ThumbStaticsInit(&m_last_decode_thumb); @@ -76,15 +78,22 @@ public: virtual uint32_t NumSupportedHardwareBreakpoints(); virtual uint32_t NumSupportedHardwareWatchpoints(); virtual uint32_t EnableHardwareBreakpoint (nub_addr_t addr, nub_size_t size); - virtual uint32_t EnableHardwareWatchpoint (nub_addr_t addr, nub_size_t size, bool read, bool write, bool also_set_on_task); virtual bool DisableHardwareBreakpoint (uint32_t hw_break_index); + + virtual uint32_t EnableHardwareWatchpoint (nub_addr_t addr, nub_size_t size, bool read, bool write, bool also_set_on_task); virtual bool DisableHardwareWatchpoint (uint32_t hw_break_index, bool also_set_on_task); - virtual bool EnableHardwareWatchpoint0 (uint32_t hw_break_index, bool Delegate, bool also_set_on_task); - virtual bool DisableHardwareWatchpoint0 (uint32_t hw_break_index, bool Delegate, bool also_set_on_task); + virtual bool DisableHardwareWatchpoint_helper (uint32_t hw_break_index, bool also_set_on_task); + virtual bool ReenableHardwareWatchpoint (uint32_t hw_break_index); + virtual bool ReenableHardwareWatchpoint_helper (uint32_t hw_break_index); + virtual bool StepNotComplete (); virtual uint32_t GetHardwareWatchpointHit(nub_addr_t &addr); +#if defined (ARM_DEBUG_STATE32) && defined (__arm64__) + typedef arm_debug_state32_t DBG; +#else typedef arm_debug_state_t DBG; +#endif protected: @@ -106,7 +115,11 @@ protected: e_regSetGPR = ARM_THREAD_STATE, e_regSetVFP = ARM_VFP_STATE, e_regSetEXC = ARM_EXCEPTION_STATE, +#if defined (ARM_DEBUG_STATE32) && defined (__arm64__) + e_regSetDBG = ARM_DEBUG_STATE32, +#else e_regSetDBG = ARM_DEBUG_STATE, +#endif kNumRegisterSets } RegisterSet; @@ -232,16 +245,26 @@ protected: kern_return_t SetEXCState (); kern_return_t SetDBGState (bool also_set_on_task); - // Helper functions for watchpoint implementaions. - static void ClearWatchpointOccurred(); - static bool HasWatchpointOccurred(); - static bool IsWatchpointEnabled(const DBG &debug_state, uint32_t hw_index); - static nub_addr_t GetWatchAddress(const DBG &debug_state, uint32_t hw_index); + bool IsWatchpointEnabled(const DBG &debug_state, uint32_t hw_index); + nub_addr_t GetWatchpointAddressByIndex (uint32_t hw_index); + nub_addr_t GetWatchAddress(const DBG &debug_state, uint32_t hw_index); + + class disabled_watchpoint { + public: + disabled_watchpoint () { addr = 0; control = 0; } + nub_addr_t addr; + uint32_t control; + }; protected: MachThread * m_thread; State m_state; DBG m_dbg_save; + + // armv8 doesn't keep the disabled watchpoint values in the debug register context like armv7; + // we need to save them aside when we disable them temporarily. + std::vector<disabled_watchpoint> m_disabled_watchpoints; + nub_addr_t m_hw_single_chained_step_addr; nub_addr_t m_last_decode_pc; diff --git a/lldb/tools/debugserver/source/MacOSX/arm64/DNBArchImplARM64.cpp b/lldb/tools/debugserver/source/MacOSX/arm64/DNBArchImplARM64.cpp new file mode 100644 index 00000000000..909fac69f31 --- /dev/null +++ b/lldb/tools/debugserver/source/MacOSX/arm64/DNBArchImplARM64.cpp @@ -0,0 +1,2093 @@ +//===-- DNBArchMachARM64.cpp ------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// Created by Greg Clayton on 6/25/07. +// +//===----------------------------------------------------------------------===// + +#if defined (__arm__) || defined (__arm64__) + +#include "MacOSX/arm64/DNBArchImplARM64.h" + +#if defined (ARM_THREAD_STATE64_COUNT) + +#include "MacOSX/MachProcess.h" +#include "MacOSX/MachThread.h" +#include "DNBBreakpoint.h" +#include "DNBLog.h" +#include "DNBRegisterInfo.h" +#include "DNB.h" + +#include <inttypes.h> +#include <sys/sysctl.h> + +// Break only in privileged or user mode +// (PAC bits in the DBGWVRn_EL1 watchpoint control register) +#define S_USER ((uint32_t)(2u << 1)) + +#define BCR_ENABLE ((uint32_t)(1u)) +#define WCR_ENABLE ((uint32_t)(1u)) + +// Watchpoint load/store +// (LSC bits in the DBGWVRn_EL1 watchpoint control register) +#define WCR_LOAD ((uint32_t)(1u << 3)) +#define WCR_STORE ((uint32_t)(1u << 4)) + +// Enable breakpoint, watchpoint, and vector catch debug exceptions. +// (MDE bit in the MDSCR_EL1 register. Equivalent to the MDBGen bit in DBGDSCRext in Aarch32) +#define MDE_ENABLE ((uint32_t)(1u << 15)) + +// Single instruction step +// (SS bit in the MDSCR_EL1 register) +#define SS_ENABLE ((uint32_t)(1u)) + +static const uint8_t g_arm64_breakpoint_opcode[] = { 0x00, 0x00, 0x20, 0xD4 }; // "brk #0", 0xd4200000 in BE byte order +static const uint8_t g_arm_breakpoint_opcode[] = { 0xFE, 0xDE, 0xFF, 0xE7 }; // this armv7 insn also works in arm64 + +// If we need to set one logical watchpoint by using +// two hardware watchpoint registers, the watchpoint +// will be split into a "high" and "low" watchpoint. +// Record both of them in the LoHi array. + +// It's safe to initialize to all 0's since +// hi > lo and therefore LoHi[i] cannot be 0. +static uint32_t LoHi[16] = { 0 }; + + +void +DNBArchMachARM64::Initialize() +{ + DNBArchPluginInfo arch_plugin_info = + { + CPU_TYPE_ARM64, + DNBArchMachARM64::Create, + DNBArchMachARM64::GetRegisterSetInfo, + DNBArchMachARM64::SoftwareBreakpointOpcode + }; + + // Register this arch plug-in with the main protocol class + DNBArchProtocol::RegisterArchPlugin (arch_plugin_info); +} + + +DNBArchProtocol * +DNBArchMachARM64::Create (MachThread *thread) +{ + DNBArchMachARM64 *obj = new DNBArchMachARM64 (thread); + + return obj; +} + +const uint8_t * const +DNBArchMachARM64::SoftwareBreakpointOpcode (nub_size_t byte_size) +{ + return g_arm_breakpoint_opcode; +} + +uint32_t +DNBArchMachARM64::GetCPUType() +{ + return CPU_TYPE_ARM64; +} + +uint64_t +DNBArchMachARM64::GetPC(uint64_t failValue) +{ + // Get program counter + if (GetGPRState(false) == KERN_SUCCESS) + return m_state.context.gpr.__pc; + return failValue; +} + +kern_return_t +DNBArchMachARM64::SetPC(uint64_t value) +{ + // Get program counter + kern_return_t err = GetGPRState(false); + if (err == KERN_SUCCESS) + { + m_state.context.gpr.__pc = value; + err = SetGPRState(); + } + return err == KERN_SUCCESS; +} + +uint64_t +DNBArchMachARM64::GetSP(uint64_t failValue) +{ + // Get stack pointer + if (GetGPRState(false) == KERN_SUCCESS) + return m_state.context.gpr.__sp; + return failValue; +} + +kern_return_t +DNBArchMachARM64::GetGPRState(bool force) +{ + int set = e_regSetGPR; + // Check if we have valid cached registers + if (!force && m_state.GetError(set, Read) == KERN_SUCCESS) + return KERN_SUCCESS; + + // Read the registers from our thread + mach_msg_type_number_t count = e_regSetGPRCount; + kern_return_t kret = ::thread_get_state(m_thread->MachPortNumber(), ARM_THREAD_STATE64, (thread_state_t)&m_state.context.gpr, &count); + if (DNBLogEnabledForAny (LOG_THREAD)) + { + uint64_t *x = &m_state.context.gpr.__x[0]; + DNBLogThreaded("thread_get_state(0x%4.4x, %u, &gpr, %u) => 0x%8.8x (count = %u) regs" + "\n x0=%16.16llx" + "\n x1=%16.16llx" + "\n x2=%16.16llx" + "\n x3=%16.16llx" + "\n x4=%16.16llx" + "\n x5=%16.16llx" + "\n x6=%16.16llx" + "\n x7=%16.16llx" + "\n x8=%16.16llx" + "\n x9=%16.16llx" + "\n x10=%16.16llx" + "\n x11=%16.16llx" + "\n x12=%16.16llx" + "\n x13=%16.16llx" + "\n x14=%16.16llx" + "\n x15=%16.16llx" + "\n x16=%16.16llx" + "\n x17=%16.16llx" + "\n x18=%16.16llx" + "\n x19=%16.16llx" + "\n x20=%16.16llx" + "\n x21=%16.16llx" + "\n x22=%16.16llx" + "\n x23=%16.16llx" + "\n x24=%16.16llx" + "\n x25=%16.16llx" + "\n x26=%16.16llx" + "\n x27=%16.16llx" + "\n x28=%16.16llx" + "\n fp=%16.16llx" + "\n lr=%16.16llx" + "\n sp=%16.16llx" + "\n pc=%16.16llx" + "\n cpsr=%8.8x", + m_thread->MachPortNumber(), + e_regSetGPR, + e_regSetGPRCount, + kret, + count, + x[0], + x[1], + x[2], + x[3], + x[4], + x[5], + x[6], + x[7], + x[8], + x[9], + x[0], + x[11], + x[12], + x[13], + x[14], + x[15], + x[16], + x[17], + x[18], + x[19], + x[20], + x[21], + x[22], + x[23], + x[24], + x[25], + x[26], + x[27], + x[28], + m_state.context.gpr.__fp, + m_state.context.gpr.__lr, + m_state.context.gpr.__sp, + m_state.context.gpr.__pc, + m_state.context.gpr.__cpsr); + } + m_state.SetError(set, Read, kret); + return kret; +} + +kern_return_t +DNBArchMachARM64::GetVFPState(bool force) +{ + int set = e_regSetVFP; + // Check if we have valid cached registers + if (!force && m_state.GetError(set, Read) == KERN_SUCCESS) + return KERN_SUCCESS; + + // Read the registers from our thread + mach_msg_type_number_t count = e_regSetVFPCount; + kern_return_t kret = ::thread_get_state(m_thread->MachPortNumber(), ARM_NEON_STATE64, (thread_state_t)&m_state.context.vfp, &count); + if (DNBLogEnabledForAny (LOG_THREAD)) + { +#if defined (__arm64__) + DNBLogThreaded("thread_get_state(0x%4.4x, %u, &vfp, %u) => 0x%8.8x (count = %u) regs" + "\n q0 = 0x%16.16llx%16.16llx" + "\n q1 = 0x%16.16llx%16.16llx" + "\n q2 = 0x%16.16llx%16.16llx" + "\n q3 = 0x%16.16llx%16.16llx" + "\n q4 = 0x%16.16llx%16.16llx" + "\n q5 = 0x%16.16llx%16.16llx" + "\n q6 = 0x%16.16llx%16.16llx" + "\n q7 = 0x%16.16llx%16.16llx" + "\n q8 = 0x%16.16llx%16.16llx" + "\n q9 = 0x%16.16llx%16.16llx" + "\n q10 = 0x%16.16llx%16.16llx" + "\n q11 = 0x%16.16llx%16.16llx" + "\n q12 = 0x%16.16llx%16.16llx" + "\n q13 = 0x%16.16llx%16.16llx" + "\n q14 = 0x%16.16llx%16.16llx" + "\n q15 = 0x%16.16llx%16.16llx" + "\n q16 = 0x%16.16llx%16.16llx" + "\n q17 = 0x%16.16llx%16.16llx" + "\n q18 = 0x%16.16llx%16.16llx" + "\n q19 = 0x%16.16llx%16.16llx" + "\n q20 = 0x%16.16llx%16.16llx" + "\n q21 = 0x%16.16llx%16.16llx" + "\n q22 = 0x%16.16llx%16.16llx" + "\n q23 = 0x%16.16llx%16.16llx" + "\n q24 = 0x%16.16llx%16.16llx" + "\n q25 = 0x%16.16llx%16.16llx" + "\n q26 = 0x%16.16llx%16.16llx" + "\n q27 = 0x%16.16llx%16.16llx" + "\n q28 = 0x%16.16llx%16.16llx" + "\n q29 = 0x%16.16llx%16.16llx" + "\n q30 = 0x%16.16llx%16.16llx" + "\n q31 = 0x%16.16llx%16.16llx" + "\n fpsr = 0x%8.8x" + "\n fpcr = 0x%8.8x\n\n", + m_thread->MachPortNumber(), + e_regSetVFP, + e_regSetVFPCount, + kret, + count, + ((uint64_t *)&m_state.context.vfp.__v[0])[0] , ((uint64_t *)&m_state.context.vfp.__v[0])[1], + ((uint64_t *)&m_state.context.vfp.__v[1])[0] , ((uint64_t *)&m_state.context.vfp.__v[1])[1], + ((uint64_t *)&m_state.context.vfp.__v[2])[0] , ((uint64_t *)&m_state.context.vfp.__v[2])[1], + ((uint64_t *)&m_state.context.vfp.__v[3])[0] , ((uint64_t *)&m_state.context.vfp.__v[3])[1], + ((uint64_t *)&m_state.context.vfp.__v[4])[0] , ((uint64_t *)&m_state.context.vfp.__v[4])[1], + ((uint64_t *)&m_state.context.vfp.__v[5])[0] , ((uint64_t *)&m_state.context.vfp.__v[5])[1], + ((uint64_t *)&m_state.context.vfp.__v[6])[0] , ((uint64_t *)&m_state.context.vfp.__v[6])[1], + ((uint64_t *)&m_state.context.vfp.__v[7])[0] , ((uint64_t *)&m_state.context.vfp.__v[7])[1], + ((uint64_t *)&m_state.context.vfp.__v[8])[0] , ((uint64_t *)&m_state.context.vfp.__v[8])[1], + ((uint64_t *)&m_state.context.vfp.__v[9])[0] , ((uint64_t *)&m_state.context.vfp.__v[9])[1], + ((uint64_t *)&m_state.context.vfp.__v[10])[0], ((uint64_t *)&m_state.context.vfp.__v[10])[1], + ((uint64_t *)&m_state.context.vfp.__v[11])[0], ((uint64_t *)&m_state.context.vfp.__v[11])[1], + ((uint64_t *)&m_state.context.vfp.__v[12])[0], ((uint64_t *)&m_state.context.vfp.__v[12])[1], + ((uint64_t *)&m_state.context.vfp.__v[13])[0], ((uint64_t *)&m_state.context.vfp.__v[13])[1], + ((uint64_t *)&m_state.context.vfp.__v[14])[0], ((uint64_t *)&m_state.context.vfp.__v[14])[1], + ((uint64_t *)&m_state.context.vfp.__v[15])[0], ((uint64_t *)&m_state.context.vfp.__v[15])[1], + ((uint64_t *)&m_state.context.vfp.__v[16])[0], ((uint64_t *)&m_state.context.vfp.__v[16])[1], + ((uint64_t *)&m_state.context.vfp.__v[17])[0], ((uint64_t *)&m_state.context.vfp.__v[17])[1], + ((uint64_t *)&m_state.context.vfp.__v[18])[0], ((uint64_t *)&m_state.context.vfp.__v[18])[1], + ((uint64_t *)&m_state.context.vfp.__v[19])[0], ((uint64_t *)&m_state.context.vfp.__v[19])[1], + ((uint64_t *)&m_state.context.vfp.__v[20])[0], ((uint64_t *)&m_state.context.vfp.__v[20])[1], + ((uint64_t *)&m_state.context.vfp.__v[21])[0], ((uint64_t *)&m_state.context.vfp.__v[21])[1], + ((uint64_t *)&m_state.context.vfp.__v[22])[0], ((uint64_t *)&m_state.context.vfp.__v[22])[1], + ((uint64_t *)&m_state.context.vfp.__v[23])[0], ((uint64_t *)&m_state.context.vfp.__v[23])[1], + ((uint64_t *)&m_state.context.vfp.__v[24])[0], ((uint64_t *)&m_state.context.vfp.__v[24])[1], + ((uint64_t *)&m_state.context.vfp.__v[25])[0], ((uint64_t *)&m_state.context.vfp.__v[25])[1], + ((uint64_t *)&m_state.context.vfp.__v[26])[0], ((uint64_t *)&m_state.context.vfp.__v[26])[1], + ((uint64_t *)&m_state.context.vfp.__v[27])[0], ((uint64_t *)&m_state.context.vfp.__v[27])[1], + ((uint64_t *)&m_state.context.vfp.__v[28])[0], ((uint64_t *)&m_state.context.vfp.__v[28])[1], + ((uint64_t *)&m_state.context.vfp.__v[29])[0], ((uint64_t *)&m_state.context.vfp.__v[29])[1], + ((uint64_t *)&m_state.context.vfp.__v[30])[0], ((uint64_t *)&m_state.context.vfp.__v[30])[1], + ((uint64_t *)&m_state.context.vfp.__v[31])[0], ((uint64_t *)&m_state.context.vfp.__v[31])[1], + m_state.context.vfp.__fpsr, + m_state.context.vfp.__fpcr); +#endif + } + m_state.SetError(set, Read, kret); + return kret; +} + +kern_return_t +DNBArchMachARM64::GetEXCState(bool force) +{ + int set = e_regSetEXC; + // Check if we have valid cached registers + if (!force && m_state.GetError(set, Read) == KERN_SUCCESS) + return KERN_SUCCESS; + + // Read the registers from our thread + mach_msg_type_number_t count = e_regSetEXCCount; + kern_return_t kret = ::thread_get_state(m_thread->MachPortNumber(), ARM_EXCEPTION_STATE64, (thread_state_t)&m_state.context.exc, &count); + m_state.SetError(set, Read, kret); + return kret; +} + +static void +DumpDBGState(const arm_debug_state_t& dbg) +{ + uint32_t i = 0; + for (i=0; i<16; i++) + DNBLogThreadedIf(LOG_STEP, "BVR%-2u/BCR%-2u = { 0x%8.8x, 0x%8.8x } WVR%-2u/WCR%-2u = { 0x%8.8x, 0x%8.8x }", + i, i, dbg.__bvr[i], dbg.__bcr[i], + i, i, dbg.__wvr[i], dbg.__wcr[i]); +} + +kern_return_t +DNBArchMachARM64::GetDBGState(bool force) +{ + int set = e_regSetDBG; + + // Check if we have valid cached registers + if (!force && m_state.GetError(set, Read) == KERN_SUCCESS) + return KERN_SUCCESS; + + // Read the registers from our thread + mach_msg_type_number_t count = e_regSetDBGCount; + kern_return_t kret = ::thread_get_state(m_thread->MachPortNumber(), ARM_DEBUG_STATE64, (thread_state_t)&m_state.dbg, &count); + m_state.SetError(set, Read, kret); + + return kret; +} + +kern_return_t +DNBArchMachARM64::SetGPRState() +{ + int set = e_regSetGPR; + kern_return_t kret = ::thread_set_state(m_thread->MachPortNumber(), ARM_THREAD_STATE64, (thread_state_t)&m_state.context.gpr, e_regSetGPRCount); + m_state.SetError(set, Write, kret); // Set the current write error for this register set + m_state.InvalidateRegisterSetState(set); // Invalidate the current register state in case registers are read back differently + return kret; // Return the error code +} + +kern_return_t +DNBArchMachARM64::SetVFPState() +{ + int set = e_regSetVFP; + kern_return_t kret = ::thread_set_state (m_thread->MachPortNumber(), ARM_NEON_STATE64, (thread_state_t)&m_state.context.vfp, e_regSetVFPCount); + m_state.SetError(set, Write, kret); // Set the current write error for this register set + m_state.InvalidateRegisterSetState(set); // Invalidate the current register state in case registers are read back differently + return kret; // Return the error code +} + +kern_return_t +DNBArchMachARM64::SetEXCState() +{ + int set = e_regSetEXC; + kern_return_t kret = ::thread_set_state (m_thread->MachPortNumber(), ARM_EXCEPTION_STATE64, (thread_state_t)&m_state.context.exc, e_regSetEXCCount); + m_state.SetError(set, Write, kret); // Set the current write error for this register set + m_state.InvalidateRegisterSetState(set); // Invalidate the current register state in case registers are read back differently + return kret; // Return the error code +} + +kern_return_t +DNBArchMachARM64::SetDBGState(bool also_set_on_task) +{ + int set = e_regSetDBG; + kern_return_t kret = ::thread_set_state (m_thread->MachPortNumber(), ARM_DEBUG_STATE64, (thread_state_t)&m_state.dbg, e_regSetDBGCount); + if (also_set_on_task) + { + kern_return_t task_kret = task_set_state (m_thread->Process()->Task().TaskPort(), ARM_DEBUG_STATE64, (thread_state_t)&m_state.dbg, e_regSetDBGCount); + if (task_kret != KERN_SUCCESS) + DNBLogThreadedIf(LOG_WATCHPOINTS, "DNBArchMachARM64::SetDBGState failed to set debug control register state: 0x%8.8x.", task_kret); + } + m_state.SetError(set, Write, kret); // Set the current write error for this register set + m_state.InvalidateRegisterSetState(set); // Invalidate the current register state in case registers are read back differently + + return kret; // Return the error code +} + +void +DNBArchMachARM64::ThreadWillResume() +{ + // Do we need to step this thread? If so, let the mach thread tell us so. + if (m_thread->IsStepping()) + { + EnableHardwareSingleStep(true); + } + + // Disable the triggered watchpoint temporarily before we resume. + // Plus, we try to enable hardware single step to execute past the instruction which triggered our watchpoint. + if (m_watchpoint_did_occur) + { + if (m_watchpoint_hw_index >= 0) + { + kern_return_t kret = GetDBGState(false); + if (kret == KERN_SUCCESS && !IsWatchpointEnabled(m_state.dbg, m_watchpoint_hw_index)) { + // The watchpoint might have been disabled by the user. We don't need to do anything at all + // to enable hardware single stepping. + m_watchpoint_did_occur = false; + m_watchpoint_hw_index = -1; + return; + } + + DisableHardwareWatchpoint(m_watchpoint_hw_index, false); + DNBLogThreadedIf(LOG_WATCHPOINTS, "DNBArchMachARM::ThreadWillResume() DisableHardwareWatchpoint(%d) called", + m_watchpoint_hw_index); + + // Enable hardware single step to move past the watchpoint-triggering instruction. + m_watchpoint_resume_single_step_enabled = (EnableHardwareSingleStep(true) == KERN_SUCCESS); + + // If we are not able to enable single step to move past the watchpoint-triggering instruction, + // at least we should reset the two watchpoint member variables so that the next time around + // this callback function is invoked, the enclosing logical branch is skipped. + if (!m_watchpoint_resume_single_step_enabled) { + // Reset the two watchpoint member variables. + m_watchpoint_did_occur = false; + m_watchpoint_hw_index = -1; + DNBLogThreadedIf(LOG_WATCHPOINTS, "DNBArchMachARM::ThreadWillResume() failed to enable single step"); + } + else + DNBLogThreadedIf(LOG_WATCHPOINTS, "DNBArchMachARM::ThreadWillResume() succeeded to enable single step"); + } + } +} + +bool +DNBArchMachARM64::NotifyException(MachException::Data& exc) +{ + + switch (exc.exc_type) + { + default: + break; + case EXC_BREAKPOINT: + if (exc.exc_data.size() == 2 && exc.exc_data[0] == EXC_ARM_DA_DEBUG) + { + // The data break address is passed as exc_data[1]. + nub_addr_t addr = exc.exc_data[1]; + // Find the hardware index with the side effect of possibly massaging the + // addr to return the starting address as seen from the debugger side. + uint32_t hw_index = GetHardwareWatchpointHit(addr); + + // One logical watchpoint was split into two watchpoint locations because + // it was too big. If the watchpoint exception is indicating the 2nd half + // of the two-parter, find the address of the 1st half and report that -- + // that's what lldb is going to expect to see. + DNBLogThreadedIf(LOG_WATCHPOINTS, "DNBArchMachARM::NotifyException watchpoint %d was hit on address 0x%llx", hw_index, (uint64_t) addr); + const int num_watchpoints = NumSupportedHardwareWatchpoints (); + for (int i = 0; i < num_watchpoints; i++) + { + if (LoHi[i] != 0 + && LoHi[i] == hw_index + && LoHi[i] != i + && GetWatchpointAddressByIndex (i) != INVALID_NUB_ADDRESS) + { + addr = GetWatchpointAddressByIndex (i); + DNBLogThreadedIf(LOG_WATCHPOINTS, "DNBArchMachARM::NotifyException It is a linked watchpoint; rewritten to index %d addr 0x%llx", LoHi[i], (uint64_t) addr); + } + } + + if (hw_index != INVALID_NUB_HW_INDEX) + { + m_watchpoint_did_occur = true; + m_watchpoint_hw_index = hw_index; + exc.exc_data[1] = addr; + // Piggyback the hw_index in the exc.data. + exc.exc_data.push_back(hw_index); + } + + return true; + } + break; + } + return false; +} + +bool +DNBArchMachARM64::ThreadDidStop() +{ + bool success = true; + + m_state.InvalidateAllRegisterStates(); + + if (m_watchpoint_resume_single_step_enabled) + { + // Great! We now disable the hardware single step as well as re-enable the hardware watchpoint. + // See also ThreadWillResume(). + if (EnableHardwareSingleStep(false) == KERN_SUCCESS) + { + if (m_watchpoint_did_occur && m_watchpoint_hw_index >= 0) + { + ReenableHardwareWatchpoint(m_watchpoint_hw_index); + m_watchpoint_resume_single_step_enabled = false; + m_watchpoint_did_occur = false; + m_watchpoint_hw_index = -1; + } + else + { + DNBLogError("internal error detected: m_watchpoint_resume_step_enabled is true but (m_watchpoint_did_occur && m_watchpoint_hw_index >= 0) does not hold!"); + } + } + else + { + DNBLogError("internal error detected: m_watchpoint_resume_step_enabled is true but unable to disable single step!"); + } + } + + // Are we stepping a single instruction? + if (GetGPRState(true) == KERN_SUCCESS) + { + // We are single stepping, was this the primary thread? + if (m_thread->IsStepping()) + { + // This was the primary thread, we need to clear the trace + // bit if so. + success = EnableHardwareSingleStep(false) == KERN_SUCCESS; + } + else + { + // The MachThread will automatically restore the suspend count + // in ThreadDidStop(), so we don't need to do anything here if + // we weren't the primary thread the last time + } + } + return success; +} + +// Set the single step bit in the processor status register. +kern_return_t +DNBArchMachARM64::EnableHardwareSingleStep (bool enable) +{ + DNBError err; + DNBLogThreadedIf(LOG_STEP, "%s( enable = %d )", __FUNCTION__, enable); + + err = GetGPRState(false); + + if (err.Fail()) + { + err.LogThreaded("%s: failed to read the GPR registers", __FUNCTION__); + return err.Error(); + } + + err = GetDBGState(false); + + if (err.Fail()) + { + err.LogThreaded("%s: failed to read the DBG registers", __FUNCTION__); + return err.Error(); + } + + if (enable) + { + DNBLogThreadedIf(LOG_STEP, "%s: Setting MDSCR_EL1 Single Step bit at pc 0x%llx", __FUNCTION__, (uint64_t) m_state.context.gpr.__pc); + m_state.dbg.__mdscr_el1 |= SS_ENABLE; + } + else + { + DNBLogThreadedIf(LOG_STEP, "%s: Clearing MDSCR_EL1 Single Step bit at pc 0x%llx", __FUNCTION__, (uint64_t) m_state.context.gpr.__pc); + m_state.dbg.__mdscr_el1 &= ~(SS_ENABLE); + } + + return SetDBGState(false); +} + +// return 1 if bit "BIT" is set in "value" +static inline uint32_t bit(uint32_t value, uint32_t bit) +{ + return (value >> bit) & 1u; +} + +// return the bitfield "value[msbit:lsbit]". +static inline uint64_t bits(uint64_t value, uint32_t msbit, uint32_t lsbit) +{ + assert(msbit >= lsbit); + uint64_t shift_left = sizeof(value) * 8 - 1 - msbit; + value <<= shift_left; // shift anything above the msbit off of the unsigned edge + value >>= shift_left + lsbit; // shift it back again down to the lsbit (including undoing any shift from above) + return value; // return our result +} + +uint32_t +DNBArchMachARM64::NumSupportedHardwareWatchpoints() +{ + // Set the init value to something that will let us know that we need to + // autodetect how many watchpoints are supported dynamically... + static uint32_t g_num_supported_hw_watchpoints = UINT_MAX; + if (g_num_supported_hw_watchpoints == UINT_MAX) + { + // Set this to zero in case we can't tell if there are any HW breakpoints + g_num_supported_hw_watchpoints = 0; + + + size_t len; + uint32_t n = 0; + len = sizeof (n); + if (::sysctlbyname("hw.optional.watchpoint", &n, &len, NULL, 0) == 0) + { + g_num_supported_hw_watchpoints = n; + DNBLogThreadedIf(LOG_THREAD, "hw.optional.watchpoint=%u", n); + } + else + { + // For AArch64 we would need to look at ID_AA64DFR0_EL1 but debugserver runs in EL0 so it can't + // access that reg. The kernel should have filled in the sysctls based on it though. +#if defined (__arm__) + uint32_t register_DBGDIDR; + + asm("mrc p14, 0, %0, c0, c0, 0" : "=r" (register_DBGDIDR)); + uint32_t numWRPs = bits(register_DBGDIDR, 31, 28); + // Zero is reserved for the WRP count, so don't increment it if it is zero + if (numWRPs > 0) + numWRPs++; + g_num_supported_hw_watchpoints = numWRPs; + DNBLogThreadedIf(LOG_THREAD, "Number of supported hw watchpoints via asm(): %d", g_num_supported_hw_watchpoints); +#endif + } + } + return g_num_supported_hw_watchpoints; +} + +uint32_t +DNBArchMachARM64::EnableHardwareWatchpoint (nub_addr_t addr, nub_size_t size, bool read, bool write, bool also_set_on_task) +{ + DNBLogThreadedIf(LOG_WATCHPOINTS, "DNBArchMachARM64::EnableHardwareWatchpoint(addr = 0x%8.8llx, size = %zu, read = %u, write = %u)", (uint64_t)addr, size, read, write); + + const uint32_t num_hw_watchpoints = NumSupportedHardwareWatchpoints(); + + // Can't watch zero bytes + if (size == 0) + return INVALID_NUB_HW_INDEX; + + // We must watch for either read or write + if (read == false && write == false) + return INVALID_NUB_HW_INDEX; + + // Otherwise, can't watch more than 8 bytes per WVR/WCR pair + if (size > 8) + return INVALID_NUB_HW_INDEX; + + // arm64 watchpoints really have an 8-byte alignment requirement. You can put a watchpoint on a 4-byte + // offset address but you can only watch 4 bytes with that watchpoint. + + // arm64 watchpoints on an 8-byte (double word) aligned addr can watch any bytes in that + // 8-byte long region of memory. They can watch the 1st byte, the 2nd byte, 3rd byte, etc, or any + // combination therein by setting the bits in the BAS [12:5] (Byte Address Select) field of + // the DBGWCRn_EL1 reg for the watchpoint. + + // If the MASK [28:24] bits in the DBGWCRn_EL1 allow a single watchpoint to monitor a larger region + // of memory (16 bytes, 32 bytes, or 2GB) but the Byte Address Select bitfield then selects a larger + // range of bytes, instead of individual bytes. See the ARMv8 Debug Architecture manual for details. + // This implementation does not currently use the MASK bits; the largest single region watched by a single + // watchpoint right now is 8-bytes. + + nub_addr_t aligned_wp_address = addr & ~0x7; + uint32_t addr_dword_offset = addr & 0x7; + + // Do we need to split up this logical watchpoint into two hardware watchpoint + // registers? + // e.g. a watchpoint of length 4 on address 6. We need do this with + // one watchpoint on address 0 with bytes 6 & 7 being monitored + // one watchpoint on address 8 with bytes 0, 1, 2, 3 being monitored + + if (addr_dword_offset + size > 8) + { + DNBLogThreadedIf(LOG_WATCHPOINTS, "DNBArchMachARM64::EnableHardwareWatchpoint(addr = 0x%8.8llx, size = %zu) needs two hardware watchpoints slots to monitor", (uint64_t)addr, size); + int low_watchpoint_size = 8 - addr_dword_offset; + int high_watchpoint_size = addr_dword_offset + size - 8; + + uint32_t lo = EnableHardwareWatchpoint(addr, low_watchpoint_size, read, write, also_set_on_task); + if (lo == INVALID_NUB_HW_INDEX) + return INVALID_NUB_HW_INDEX; + uint32_t hi = EnableHardwareWatchpoint (aligned_wp_address + 8, high_watchpoint_size, read, write, also_set_on_task); + if (hi == INVALID_NUB_HW_INDEX) + { + DisableHardwareWatchpoint (lo, also_set_on_task); + return INVALID_NUB_HW_INDEX; + } + // Tag this lo->hi mapping in our database. + LoHi[lo] = hi; + return lo; + } + + // At this point + // 1 aligned_wp_address is the requested address rounded down to 8-byte alignment + // 2 addr_dword_offset is the offset into that double word (8-byte) region that we are watching + // 3 size is the number of bytes within that 8-byte region that we are watching + + // Set the Byte Address Selects bits DBGWCRn_EL1 bits [12:5] based on the above. + // The bit shift and negation operation will give us 0b11 for 2, 0b1111 for 4, etc, up to 0b11111111 for 8. + // then we shift those bits left by the offset into this dword that we are interested in. + // e.g. if we are watching bytes 4,5,6,7 in a dword we want a BAS of 0b11110000. + uint32_t byte_address_select = ((1 << size) - 1) << addr_dword_offset; + + // Read the debug state + kern_return_t kret = GetDBGState(false); + + if (kret == KERN_SUCCESS) + { + // Check to make sure we have the needed hardware support + uint32_t i = 0; + + for (i=0; i<num_hw_watchpoints; ++i) + { + if ((m_state.dbg.__wcr[i] & WCR_ENABLE) == 0) + break; // We found an available hw watchpoint slot (in i) + } + + // See if we found an available hw watchpoint slot above + if (i < num_hw_watchpoints) + { + //DumpDBGState(m_state.dbg); + + // Clear any previous LoHi joined-watchpoint that may have been in use + LoHi[i] = 0; + + // shift our Byte Address Select bits up to the correct bit range for the DBGWCRn_EL1 + byte_address_select = byte_address_select << 5; + + // Make sure bits 1:0 are clear in our address + m_state.dbg.__wvr[i] = aligned_wp_address; // DVA (Data Virtual Address) + m_state.dbg.__wcr[i] = byte_address_select | // Which bytes that follow the DVA that we will watch + S_USER | // Stop only in user mode + (read ? WCR_LOAD : 0) | // Stop on read access? + (write ? WCR_STORE : 0) | // Stop on write access? + WCR_ENABLE; // Enable this watchpoint; + + DNBLogThreadedIf(LOG_WATCHPOINTS, "DNBArchMachARM64::EnableHardwareWatchpoint() adding watchpoint on address 0x%llx with control register value 0x%x", (uint64_t) m_state.dbg.__wvr[i], (uint32_t) m_state.dbg.__wcr[i]); + + // The kernel will set the MDE_ENABLE bit in the MDSCR_EL1 for us automatically, don't need to do it here. + + kret = SetDBGState(also_set_on_task); + //DumpDBGState(m_state.dbg); + + DNBLogThreadedIf(LOG_WATCHPOINTS, "DNBArchMachARM64::EnableHardwareWatchpoint() SetDBGState() => 0x%8.8x.", kret); + + if (kret == KERN_SUCCESS) + return i; + } + else + { + DNBLogThreadedIf(LOG_WATCHPOINTS, "DNBArchMachARM64::EnableHardwareWatchpoint(): All hardware resources (%u) are in use.", num_hw_watchpoints); + } + } + return INVALID_NUB_HW_INDEX; +} + +bool +DNBArchMachARM64::ReenableHardwareWatchpoint (uint32_t hw_index) +{ + // If this logical watchpoint # is actually implemented using + // two hardware watchpoint registers, re-enable both of them. + + if (hw_index < NumSupportedHardwareWatchpoints() && LoHi[hw_index]) + { + return ReenableHardwareWatchpoint_helper (hw_index) && ReenableHardwareWatchpoint_helper (LoHi[hw_index]); + } + else + { + return ReenableHardwareWatchpoint_helper (hw_index); + } +} + +bool +DNBArchMachARM64::ReenableHardwareWatchpoint_helper (uint32_t hw_index) +{ + kern_return_t kret = GetDBGState(false); + if (kret != KERN_SUCCESS) + return false; + + const uint32_t num_hw_points = NumSupportedHardwareWatchpoints(); + if (hw_index >= num_hw_points) + return false; + + m_state.dbg.__wvr[hw_index] = m_disabled_watchpoints[hw_index].addr; + m_state.dbg.__wcr[hw_index] = m_disabled_watchpoints[hw_index].control; + + DNBLogThreadedIf(LOG_WATCHPOINTS, "DNBArchMachARM64::EnableHardwareWatchpoint( %u ) - WVR%u = 0x%8.8llx WCR%u = 0x%8.8llx", + hw_index, + hw_index, + (uint64_t) m_state.dbg.__wvr[hw_index], + hw_index, + (uint64_t) m_state.dbg.__wcr[hw_index]); + + // The kernel will set the MDE_ENABLE bit in the MDSCR_EL1 for us automatically, don't need to do it here. + + kret = SetDBGState(false); + + return (kret == KERN_SUCCESS); +} + +bool +DNBArchMachARM64::DisableHardwareWatchpoint (uint32_t hw_index, bool also_set_on_task) +{ + if (hw_index < NumSupportedHardwareWatchpoints() && LoHi[hw_index]) + { + return DisableHardwareWatchpoint_helper (hw_index, also_set_on_task) && DisableHardwareWatchpoint_helper (LoHi[hw_index], also_set_on_task); + } + else + { + return DisableHardwareWatchpoint_helper (hw_index, also_set_on_task); + } +} + +bool +DNBArchMachARM64::DisableHardwareWatchpoint_helper (uint32_t hw_index, bool also_set_on_task) +{ + kern_return_t kret = GetDBGState(false); + if (kret != KERN_SUCCESS) + return false; + + const uint32_t num_hw_points = NumSupportedHardwareWatchpoints(); + if (hw_index >= num_hw_points) + return false; + + m_disabled_watchpoints[hw_index].addr = m_state.dbg.__wvr[hw_index]; + m_disabled_watchpoints[hw_index].control = m_state.dbg.__wcr[hw_index]; + + m_state.dbg.__wcr[hw_index] &= ~((nub_addr_t)WCR_ENABLE); + DNBLogThreadedIf(LOG_WATCHPOINTS, "DNBArchMachARM64::DisableHardwareWatchpoint( %u ) - WVR%u = 0x%8.8llx WCR%u = 0x%8.8llx", + hw_index, + hw_index, + (uint64_t) m_state.dbg.__wvr[hw_index], + hw_index, + (uint64_t) m_state.dbg.__wcr[hw_index]); + + kret = SetDBGState(also_set_on_task); + + return (kret == KERN_SUCCESS); +} + +// This is for checking the Byte Address Select bits in the DBRWCRn_EL1 control register. +// Returns -1 if the trailing bit patterns are not one of: +// { 0b???????1, 0b??????10, 0b?????100, 0b????1000, 0b???10000, 0b??100000, 0b?1000000, 0b10000000 }. +static inline +int32_t +LowestBitSet(uint32_t val) +{ + for (unsigned i = 0; i < 8; ++i) { + if (bit(val, i)) + return i; + } + return -1; +} + +// Iterate through the debug registers; return the index of the first watchpoint whose address matches. +// As a side effect, the starting address as understood by the debugger is returned which could be +// different from 'addr' passed as an in/out argument. +uint32_t +DNBArchMachARM64::GetHardwareWatchpointHit(nub_addr_t &addr) +{ + // Read the debug state + kern_return_t kret = GetDBGState(true); + //DumpDBGState(m_state.dbg); + DNBLogThreadedIf(LOG_WATCHPOINTS, "DNBArchMachARM64::GetHardwareWatchpointHit() GetDBGState() => 0x%8.8x.", kret); + DNBLogThreadedIf(LOG_WATCHPOINTS, "DNBArchMachARM64::GetHardwareWatchpointHit() addr = 0x%llx", (uint64_t)addr); + + // This is the watchpoint value to match against, i.e., word address. + nub_addr_t wp_val = addr & ~((nub_addr_t)3); + if (kret == KERN_SUCCESS) + { + DBG &debug_state = m_state.dbg; + uint32_t i, num = NumSupportedHardwareWatchpoints(); + for (i = 0; i < num; ++i) + { + nub_addr_t wp_addr = GetWatchAddress(debug_state, i); + DNBLogThreadedIf(LOG_WATCHPOINTS, + "DNBArchMachARM64::GetHardwareWatchpointHit() slot: %u (addr = 0x%llx).", + i, (uint64_t)wp_addr); + if (wp_val == wp_addr) { + uint32_t byte_mask = bits(debug_state.__wcr[i], 12, 5); + + // Sanity check the byte_mask, first. + if (LowestBitSet(byte_mask) < 0) + continue; + + // Check that the watchpoint is enabled. + if (!IsWatchpointEnabled(debug_state, i)) + continue; + + // Compute the starting address (from the point of view of the debugger). + addr = wp_addr + LowestBitSet(byte_mask); + return i; + } + } + } + return INVALID_NUB_HW_INDEX; +} + +nub_addr_t +DNBArchMachARM64::GetWatchpointAddressByIndex (uint32_t hw_index) +{ + kern_return_t kret = GetDBGState(true); + if (kret != KERN_SUCCESS) + return INVALID_NUB_ADDRESS; + const uint32_t num = NumSupportedHardwareWatchpoints(); + if (hw_index >= num) + return INVALID_NUB_ADDRESS; + if (IsWatchpointEnabled (m_state.dbg, hw_index)) + return GetWatchAddress (m_state.dbg, hw_index); + return INVALID_NUB_ADDRESS; +} + +bool +DNBArchMachARM64::IsWatchpointEnabled(const DBG &debug_state, uint32_t hw_index) +{ + // Watchpoint Control Registers, bitfield definitions + // ... + // Bits Value Description + // [0] 0 Watchpoint disabled + // 1 Watchpoint enabled. + return (debug_state.__wcr[hw_index] & 1u); +} + +nub_addr_t +DNBArchMachARM64::GetWatchAddress(const DBG &debug_state, uint32_t hw_index) +{ + // Watchpoint Value Registers, bitfield definitions + // Bits Description + // [31:2] Watchpoint value (word address, i.e., 4-byte aligned) + // [1:0] RAZ/SBZP + return bits(debug_state.__wvr[hw_index], 63, 0); +} + +//---------------------------------------------------------------------- +// Register information defintions for 64 bit ARMv8. +//---------------------------------------------------------------------- +enum gpr_regnums +{ + gpr_x0 = 0, + gpr_x1, + gpr_x2, + gpr_x3, + gpr_x4, + gpr_x5, + gpr_x6, + gpr_x7, + gpr_x8, + gpr_x9, + gpr_x10, + gpr_x11, + gpr_x12, + gpr_x13, + gpr_x14, + gpr_x15, + gpr_x16, + gpr_x17, + gpr_x18, + gpr_x19, + gpr_x20, + gpr_x21, + gpr_x22, + gpr_x23, + gpr_x24, + gpr_x25, + gpr_x26, + gpr_x27, + gpr_x28, + gpr_fp, gpr_x29 = gpr_fp, + gpr_lr, gpr_x30 = gpr_lr, + gpr_sp, gpr_x31 = gpr_sp, + gpr_pc, + gpr_cpsr, + gpr_w0, + gpr_w1, + gpr_w2, + gpr_w3, + gpr_w4, + gpr_w5, + gpr_w6, + gpr_w7, + gpr_w8, + gpr_w9, + gpr_w10, + gpr_w11, + gpr_w12, + gpr_w13, + gpr_w14, + gpr_w15, + gpr_w16, + gpr_w17, + gpr_w18, + gpr_w19, + gpr_w20, + gpr_w21, + gpr_w22, + gpr_w23, + gpr_w24, + gpr_w25, + gpr_w26, + gpr_w27, + gpr_w28 + +}; + +enum +{ + vfp_v0 = 0, + vfp_v1, + vfp_v2, + vfp_v3, + vfp_v4, + vfp_v5, + vfp_v6, + vfp_v7, + vfp_v8, + vfp_v9, + vfp_v10, + vfp_v11, + vfp_v12, + vfp_v13, + vfp_v14, + vfp_v15, + vfp_v16, + vfp_v17, + vfp_v18, + vfp_v19, + vfp_v20, + vfp_v21, + vfp_v22, + vfp_v23, + vfp_v24, + vfp_v25, + vfp_v26, + vfp_v27, + vfp_v28, + vfp_v29, + vfp_v30, + vfp_v31, + vfp_fpsr, + vfp_fpcr, + + // lower 32 bits of the corresponding vfp_v<n> reg. + vfp_s0, + vfp_s1, + vfp_s2, + vfp_s3, + vfp_s4, + vfp_s5, + vfp_s6, + vfp_s7, + vfp_s8, + vfp_s9, + vfp_s10, + vfp_s11, + vfp_s12, + vfp_s13, + vfp_s14, + vfp_s15, + vfp_s16, + vfp_s17, + vfp_s18, + vfp_s19, + vfp_s20, + vfp_s21, + vfp_s22, + vfp_s23, + vfp_s24, + vfp_s25, + vfp_s26, + vfp_s27, + vfp_s28, + vfp_s29, + vfp_s30, + vfp_s31, + + // lower 64 bits of the corresponding vfp_v<n> reg. + vfp_d0, + vfp_d1, + vfp_d2, + vfp_d3, + vfp_d4, + vfp_d5, + vfp_d6, + vfp_d7, + vfp_d8, + vfp_d9, + vfp_d10, + vfp_d11, + vfp_d12, + vfp_d13, + vfp_d14, + vfp_d15, + vfp_d16, + vfp_d17, + vfp_d18, + vfp_d19, + vfp_d20, + vfp_d21, + vfp_d22, + vfp_d23, + vfp_d24, + vfp_d25, + vfp_d26, + vfp_d27, + vfp_d28, + vfp_d29, + vfp_d30, + vfp_d31 +}; + +enum +{ + exc_far = 0, + exc_esr, + exc_exception +}; + +// These numbers from the "DWARF for the ARM 64-bit Architecture (AArch64)" document. + +enum +{ + dwarf_x0 = 0, + dwarf_x1, + dwarf_x2, + dwarf_x3, + dwarf_x4, + dwarf_x5, + dwarf_x6, + dwarf_x7, + dwarf_x8, + dwarf_x9, + dwarf_x10, + dwarf_x11, + dwarf_x12, + dwarf_x13, + dwarf_x14, + dwarf_x15, + dwarf_x16, + dwarf_x17, + dwarf_x18, + dwarf_x19, + dwarf_x20, + dwarf_x21, + dwarf_x22, + dwarf_x23, + dwarf_x24, + dwarf_x25, + dwarf_x26, + dwarf_x27, + dwarf_x28, + dwarf_x29, + dwarf_x30, + dwarf_x31, + dwarf_pc = 32, + dwarf_elr_mode = 33, + dwarf_fp = dwarf_x29, + dwarf_lr = dwarf_x30, + dwarf_sp = dwarf_x31, + // 34-63 reserved + + // V0-V31 (128 bit vector registers) + dwarf_v0 = 64, + dwarf_v1, + dwarf_v2, + dwarf_v3, + dwarf_v4, + dwarf_v5, + dwarf_v6, + dwarf_v7, + dwarf_v8, + dwarf_v9, + dwarf_v10, + dwarf_v11, + dwarf_v12, + dwarf_v13, + dwarf_v14, + dwarf_v15, + dwarf_v16, + dwarf_v17, + dwarf_v18, + dwarf_v19, + dwarf_v20, + dwarf_v21, + dwarf_v22, + dwarf_v23, + dwarf_v24, + dwarf_v25, + dwarf_v26, + dwarf_v27, + dwarf_v28, + dwarf_v29, + dwarf_v30, + dwarf_v31 + + // 96-127 reserved +}; + +enum +{ + gdb_gpr_x0 = 0, + gdb_gpr_x1, + gdb_gpr_x2, + gdb_gpr_x3, + gdb_gpr_x4, + gdb_gpr_x5, + gdb_gpr_x6, + gdb_gpr_x7, + gdb_gpr_x8, + gdb_gpr_x9, + gdb_gpr_x10, + gdb_gpr_x11, + gdb_gpr_x12, + gdb_gpr_x13, + gdb_gpr_x14, + gdb_gpr_x15, + gdb_gpr_x16, + gdb_gpr_x17, + gdb_gpr_x18, + gdb_gpr_x19, + gdb_gpr_x20, + gdb_gpr_x21, + gdb_gpr_x22, + gdb_gpr_x23, + gdb_gpr_x24, + gdb_gpr_x25, + gdb_gpr_x26, + gdb_gpr_x27, + gdb_gpr_x28, + gdb_gpr_fp, // x29 + gdb_gpr_lr, // x30 + gdb_gpr_sp, // sp aka xsp + gdb_gpr_pc, + gdb_gpr_cpsr, + gdb_vfp_v0, + gdb_vfp_v1, + gdb_vfp_v2, + gdb_vfp_v3, + gdb_vfp_v4, + gdb_vfp_v5, + gdb_vfp_v6, + gdb_vfp_v7, + gdb_vfp_v8, + gdb_vfp_v9, + gdb_vfp_v10, + gdb_vfp_v11, + gdb_vfp_v12, + gdb_vfp_v13, + gdb_vfp_v14, + gdb_vfp_v15, + gdb_vfp_v16, + gdb_vfp_v17, + gdb_vfp_v18, + gdb_vfp_v19, + gdb_vfp_v20, + gdb_vfp_v21, + gdb_vfp_v22, + gdb_vfp_v23, + gdb_vfp_v24, + gdb_vfp_v25, + gdb_vfp_v26, + gdb_vfp_v27, + gdb_vfp_v28, + gdb_vfp_v29, + gdb_vfp_v30, + gdb_vfp_v31, + gdb_vfp_fpsr, + gdb_vfp_fpcr +}; + +const char *g_contained_x0[] {"x0", NULL }; +const char *g_contained_x1[] {"x1", NULL }; +const char *g_contained_x2[] {"x2", NULL }; +const char *g_contained_x3[] {"x3", NULL }; +const char *g_contained_x4[] {"x4", NULL }; +const char *g_contained_x5[] {"x5", NULL }; +const char *g_contained_x6[] {"x6", NULL }; +const char *g_contained_x7[] {"x7", NULL }; +const char *g_contained_x8[] {"x8", NULL }; +const char *g_contained_x9[] {"x9", NULL }; +const char *g_contained_x10[] {"x10", NULL }; +const char *g_contained_x11[] {"x11", NULL }; +const char *g_contained_x12[] {"x12", NULL }; +const char *g_contained_x13[] {"x13", NULL }; +const char *g_contained_x14[] {"x14", NULL }; +const char *g_contained_x15[] {"x15", NULL }; +const char *g_contained_x16[] {"x16", NULL }; +const char *g_contained_x17[] {"x17", NULL }; +const char *g_contained_x18[] {"x18", NULL }; +const char *g_contained_x19[] {"x19", NULL }; +const char *g_contained_x20[] {"x20", NULL }; +const char *g_contained_x21[] {"x21", NULL }; +const char *g_contained_x22[] {"x22", NULL }; +const char *g_contained_x23[] {"x23", NULL }; +const char *g_contained_x24[] {"x24", NULL }; +const char *g_contained_x25[] {"x25", NULL }; +const char *g_contained_x26[] {"x26", NULL }; +const char *g_contained_x27[] {"x27", NULL }; +const char *g_contained_x28[] {"x28", NULL }; + +const char *g_invalidate_x0[] {"x0", "w0", NULL }; +const char *g_invalidate_x1[] {"x1", "w1", NULL }; +const char *g_invalidate_x2[] {"x2", "w2", NULL }; +const char *g_invalidate_x3[] {"x3", "w3", NULL }; +const char *g_invalidate_x4[] {"x4", "w4", NULL }; +const char *g_invalidate_x5[] {"x5", "w5", NULL }; +const char *g_invalidate_x6[] {"x6", "w6", NULL }; +const char *g_invalidate_x7[] {"x7", "w7", NULL }; +const char *g_invalidate_x8[] {"x8", "w8", NULL }; +const char *g_invalidate_x9[] {"x9", "w9", NULL }; +const char *g_invalidate_x10[] {"x10", "w10", NULL }; +const char *g_invalidate_x11[] {"x11", "w11", NULL }; +const char *g_invalidate_x12[] {"x12", "w12", NULL }; +const char *g_invalidate_x13[] {"x13", "w13", NULL }; +const char *g_invalidate_x14[] {"x14", "w14", NULL }; +const char *g_invalidate_x15[] {"x15", "w15", NULL }; +const char *g_invalidate_x16[] {"x16", "w16", NULL }; +const char *g_invalidate_x17[] {"x17", "w17", NULL }; +const char *g_invalidate_x18[] {"x18", "w18", NULL }; +const char *g_invalidate_x19[] {"x19", "w19", NULL }; +const char *g_invalidate_x20[] {"x20", "w20", NULL }; +const char *g_invalidate_x21[] {"x21", "w21", NULL }; +const char *g_invalidate_x22[] {"x22", "w22", NULL }; +const char *g_invalidate_x23[] {"x23", "w23", NULL }; +const char *g_invalidate_x24[] {"x24", "w24", NULL }; +const char *g_invalidate_x25[] {"x25", "w25", NULL }; +const char *g_invalidate_x26[] {"x26", "w26", NULL }; +const char *g_invalidate_x27[] {"x27", "w27", NULL }; +const char *g_invalidate_x28[] {"x28", "w28", NULL }; + +#define GPR_OFFSET_IDX(idx) (offsetof (DNBArchMachARM64::GPR, __x[idx])) + +#define GPR_OFFSET_NAME(reg) (offsetof (DNBArchMachARM64::GPR , __##reg)) + +// These macros will auto define the register name, alt name, register size, +// register offset, encoding, format and native register. This ensures that +// the register state structures are defined correctly and have the correct +// sizes and offsets. +#define DEFINE_GPR_IDX(idx, reg, alt, gen) { e_regSetGPR, gpr_##reg, #reg, alt, Uint, Hex, 8, GPR_OFFSET_IDX(idx) , dwarf_##reg, dwarf_##reg, gen, gdb_gpr_##reg, NULL, g_invalidate_x##idx } +#define DEFINE_GPR_NAME(reg, alt, gen) { e_regSetGPR, gpr_##reg, #reg, alt, Uint, Hex, 8, GPR_OFFSET_NAME(reg), dwarf_##reg, dwarf_##reg, gen, gdb_gpr_##reg, NULL, NULL } +#define DEFINE_PSEUDO_GPR_IDX(idx, reg) { e_regSetGPR, gpr_##reg, #reg, NULL, Uint, Hex, 4, 0, INVALID_NUB_REGNUM, INVALID_NUB_REGNUM, INVALID_NUB_REGNUM, INVALID_NUB_REGNUM, g_contained_x##idx, g_invalidate_x##idx } + +//_STRUCT_ARM_THREAD_STATE64 +//{ +// uint64_t x[29]; /* General purpose registers x0-x28 */ +// uint64_t fp; /* Frame pointer x29 */ +// uint64_t lr; /* Link register x30 */ +// uint64_t sp; /* Stack pointer x31 */ +// uint64_t pc; /* Program counter */ +// uint32_t cpsr; /* Current program status register */ +//}; + + +// General purpose registers +const DNBRegisterInfo +DNBArchMachARM64::g_gpr_registers[] = +{ + DEFINE_GPR_IDX ( 0, x0, "arg1", GENERIC_REGNUM_ARG1 ), + DEFINE_GPR_IDX ( 1, x1, "arg2", GENERIC_REGNUM_ARG2 ), + DEFINE_GPR_IDX ( 2, x2, "arg3", GENERIC_REGNUM_ARG3 ), + DEFINE_GPR_IDX ( 3, x3, "arg4", GENERIC_REGNUM_ARG4 ), + DEFINE_GPR_IDX ( 4, x4, "arg5", GENERIC_REGNUM_ARG5 ), + DEFINE_GPR_IDX ( 5, x5, "arg6", GENERIC_REGNUM_ARG6 ), + DEFINE_GPR_IDX ( 6, x6, NULL, INVALID_NUB_REGNUM ), + DEFINE_GPR_IDX ( 7, x7, NULL, INVALID_NUB_REGNUM ), + DEFINE_GPR_IDX ( 8, x8, NULL, INVALID_NUB_REGNUM ), + DEFINE_GPR_IDX ( 9, x9, NULL, INVALID_NUB_REGNUM ), + DEFINE_GPR_IDX (10, x10, NULL, INVALID_NUB_REGNUM ), + DEFINE_GPR_IDX (11, x11, NULL, INVALID_NUB_REGNUM ), + DEFINE_GPR_IDX (12, x12, NULL, INVALID_NUB_REGNUM ), + DEFINE_GPR_IDX (13, x13, NULL, INVALID_NUB_REGNUM ), + DEFINE_GPR_IDX (14, x14, NULL, INVALID_NUB_REGNUM ), + DEFINE_GPR_IDX (15, x15, NULL, INVALID_NUB_REGNUM ), + DEFINE_GPR_IDX (16, x16, NULL, INVALID_NUB_REGNUM ), + DEFINE_GPR_IDX (17, x17, NULL, INVALID_NUB_REGNUM ), + DEFINE_GPR_IDX (18, x18, NULL, INVALID_NUB_REGNUM ), + DEFINE_GPR_IDX (19, x19, NULL, INVALID_NUB_REGNUM ), + DEFINE_GPR_IDX (20, x20, NULL, INVALID_NUB_REGNUM ), + DEFINE_GPR_IDX (21, x21, NULL, INVALID_NUB_REGNUM ), + DEFINE_GPR_IDX (22, x22, NULL, INVALID_NUB_REGNUM ), + DEFINE_GPR_IDX (23, x23, NULL, INVALID_NUB_REGNUM ), + DEFINE_GPR_IDX (24, x24, NULL, INVALID_NUB_REGNUM ), + DEFINE_GPR_IDX (25, x25, NULL, INVALID_NUB_REGNUM ), + DEFINE_GPR_IDX (26, x26, NULL, INVALID_NUB_REGNUM ), + DEFINE_GPR_IDX (27, x27, NULL, INVALID_NUB_REGNUM ), + DEFINE_GPR_IDX (28, x28, NULL, INVALID_NUB_REGNUM ), + DEFINE_GPR_NAME (fp, "x29", GENERIC_REGNUM_FP), + DEFINE_GPR_NAME (lr, "x30", GENERIC_REGNUM_RA), + DEFINE_GPR_NAME (sp, "xsp", GENERIC_REGNUM_SP), + DEFINE_GPR_NAME (pc, NULL, GENERIC_REGNUM_PC), + + // in armv7 we specify that writing to the CPSR should invalidate r8-12, sp, lr. + // this should be spcified for arm64 too even though debugserver is only used for + // userland debugging. + { e_regSetGPR, gpr_cpsr, "cpsr", "flags", Uint, Hex, 4, GPR_OFFSET_NAME(cpsr), dwarf_elr_mode, dwarf_elr_mode, INVALID_NUB_REGNUM, gdb_gpr_cpsr, NULL, NULL }, + + DEFINE_PSEUDO_GPR_IDX ( 0, w0), + DEFINE_PSEUDO_GPR_IDX ( 1, w1), + DEFINE_PSEUDO_GPR_IDX ( 2, w2), + DEFINE_PSEUDO_GPR_IDX ( 3, w3), + DEFINE_PSEUDO_GPR_IDX ( 4, w4), + DEFINE_PSEUDO_GPR_IDX ( 5, w5), + DEFINE_PSEUDO_GPR_IDX ( 6, w6), + DEFINE_PSEUDO_GPR_IDX ( 7, w7), + DEFINE_PSEUDO_GPR_IDX ( 8, w8), + DEFINE_PSEUDO_GPR_IDX ( 9, w9), + DEFINE_PSEUDO_GPR_IDX (10, w10), + DEFINE_PSEUDO_GPR_IDX (11, w11), + DEFINE_PSEUDO_GPR_IDX (12, w12), + DEFINE_PSEUDO_GPR_IDX (13, w13), + DEFINE_PSEUDO_GPR_IDX (14, w14), + DEFINE_PSEUDO_GPR_IDX (15, w15), + DEFINE_PSEUDO_GPR_IDX (16, w16), + DEFINE_PSEUDO_GPR_IDX (17, w17), + DEFINE_PSEUDO_GPR_IDX (18, w18), + DEFINE_PSEUDO_GPR_IDX (19, w19), + DEFINE_PSEUDO_GPR_IDX (20, w20), + DEFINE_PSEUDO_GPR_IDX (21, w21), + DEFINE_PSEUDO_GPR_IDX (22, w22), + DEFINE_PSEUDO_GPR_IDX (23, w23), + DEFINE_PSEUDO_GPR_IDX (24, w24), + DEFINE_PSEUDO_GPR_IDX (25, w25), + DEFINE_PSEUDO_GPR_IDX (26, w26), + DEFINE_PSEUDO_GPR_IDX (27, w27), + DEFINE_PSEUDO_GPR_IDX (28, w28) +}; + +const char *g_contained_v0[] {"v0", NULL }; +const char *g_contained_v1[] {"v1", NULL }; +const char *g_contained_v2[] {"v2", NULL }; +const char *g_contained_v3[] {"v3", NULL }; +const char *g_contained_v4[] {"v4", NULL }; +const char *g_contained_v5[] {"v5", NULL }; +const char *g_contained_v6[] {"v6", NULL }; +const char *g_contained_v7[] {"v7", NULL }; +const char *g_contained_v8[] {"v8", NULL }; +const char *g_contained_v9[] {"v9", NULL }; +const char *g_contained_v10[] {"v10", NULL }; +const char *g_contained_v11[] {"v11", NULL }; +const char *g_contained_v12[] {"v12", NULL }; +const char *g_contained_v13[] {"v13", NULL }; +const char *g_contained_v14[] {"v14", NULL }; +const char *g_contained_v15[] {"v15", NULL }; +const char *g_contained_v16[] {"v16", NULL }; +const char *g_contained_v17[] {"v17", NULL }; +const char *g_contained_v18[] {"v18", NULL }; +const char *g_contained_v19[] {"v19", NULL }; +const char *g_contained_v20[] {"v20", NULL }; +const char *g_contained_v21[] {"v21", NULL }; +const char *g_contained_v22[] {"v22", NULL }; +const char *g_contained_v23[] {"v23", NULL }; +const char *g_contained_v24[] {"v24", NULL }; +const char *g_contained_v25[] {"v25", NULL }; +const char *g_contained_v26[] {"v26", NULL }; +const char *g_contained_v27[] {"v27", NULL }; +const char *g_contained_v28[] {"v28", NULL }; +const char *g_contained_v29[] {"v29", NULL }; +const char *g_contained_v30[] {"v30", NULL }; +const char *g_contained_v31[] {"v31", NULL }; + +const char *g_invalidate_v0[] {"v0", "d0", "s0", NULL }; +const char *g_invalidate_v1[] {"v1", "d1", "s1", NULL }; +const char *g_invalidate_v2[] {"v2", "d2", "s2", NULL }; +const char *g_invalidate_v3[] {"v3", "d3", "s3", NULL }; +const char *g_invalidate_v4[] {"v4", "d4", "s4", NULL }; +const char *g_invalidate_v5[] {"v5", "d5", "s5", NULL }; +const char *g_invalidate_v6[] {"v6", "d6", "s6", NULL }; +const char *g_invalidate_v7[] {"v7", "d7", "s7", NULL }; +const char *g_invalidate_v8[] {"v8", "d8", "s8", NULL }; +const char *g_invalidate_v9[] {"v9", "d9", "s9", NULL }; +const char *g_invalidate_v10[] {"v10", "d10", "s10", NULL }; +const char *g_invalidate_v11[] {"v11", "d11", "s11", NULL }; +const char *g_invalidate_v12[] {"v12", "d12", "s12", NULL }; +const char *g_invalidate_v13[] {"v13", "d13", "s13", NULL }; +const char *g_invalidate_v14[] {"v14", "d14", "s14", NULL }; +const char *g_invalidate_v15[] {"v15", "d15", "s15", NULL }; +const char *g_invalidate_v16[] {"v16", "d16", "s16", NULL }; +const char *g_invalidate_v17[] {"v17", "d17", "s17", NULL }; +const char *g_invalidate_v18[] {"v18", "d18", "s18", NULL }; +const char *g_invalidate_v19[] {"v19", "d19", "s19", NULL }; +const char *g_invalidate_v20[] {"v20", "d20", "s20", NULL }; +const char *g_invalidate_v21[] {"v21", "d21", "s21", NULL }; +const char *g_invalidate_v22[] {"v22", "d22", "s22", NULL }; +const char *g_invalidate_v23[] {"v23", "d23", "s23", NULL }; +const char *g_invalidate_v24[] {"v24", "d24", "s24", NULL }; +const char *g_invalidate_v25[] {"v25", "d25", "s25", NULL }; +const char *g_invalidate_v26[] {"v26", "d26", "s26", NULL }; +const char *g_invalidate_v27[] {"v27", "d27", "s27", NULL }; +const char *g_invalidate_v28[] {"v28", "d28", "s28", NULL }; +const char *g_invalidate_v29[] {"v29", "d29", "s29", NULL }; +const char *g_invalidate_v30[] {"v30", "d30", "s30", NULL }; +const char *g_invalidate_v31[] {"v31", "d31", "s31", NULL }; + +#if defined (__arm64__) +#define VFP_V_OFFSET_IDX(idx) (offsetof (DNBArchMachARM64::FPU, __v) + (idx * 16) + offsetof (DNBArchMachARM64::Context, vfp)) +#else +#define VFP_V_OFFSET_IDX(idx) (offsetof (DNBArchMachARM64::FPU, opaque) + (idx * 16) + offsetof (DNBArchMachARM64::Context, vfp)) +#endif +#define VFP_OFFSET_NAME(reg) (offsetof (DNBArchMachARM64::FPU, reg) + offsetof (DNBArchMachARM64::Context, vfp)) +#define EXC_OFFSET(reg) (offsetof (DNBArchMachARM64::EXC, reg) + offsetof (DNBArchMachARM64::Context, exc)) + +//#define FLOAT_FORMAT Float +#define DEFINE_VFP_V_IDX(idx) { e_regSetVFP, vfp_v##idx, "v" #idx, "q" #idx, Vector, VectorOfUInt8, 16, VFP_V_OFFSET_IDX(idx), INVALID_NUB_REGNUM, dwarf_v##idx, INVALID_NUB_REGNUM, gdb_vfp_v##idx, NULL, g_invalidate_v##idx } +#define DEFINE_PSEUDO_VFP_S_IDX(idx) { e_regSetVFP, vfp_s##idx, "s" #idx, NULL, IEEE754, Float, 4, 0, INVALID_NUB_REGNUM, INVALID_NUB_REGNUM, INVALID_NUB_REGNUM, INVALID_NUB_REGNUM, g_contained_v##idx, g_invalidate_v##idx } +#define DEFINE_PSEUDO_VFP_D_IDX(idx) { e_regSetVFP, vfp_d##idx, "d" #idx, NULL, IEEE754, Float, 8, 0, INVALID_NUB_REGNUM, INVALID_NUB_REGNUM, INVALID_NUB_REGNUM, INVALID_NUB_REGNUM, g_contained_v##idx, g_invalidate_v##idx } + +// Floating point registers +const DNBRegisterInfo +DNBArchMachARM64::g_vfp_registers[] = +{ + DEFINE_VFP_V_IDX ( 0), + DEFINE_VFP_V_IDX ( 1), + DEFINE_VFP_V_IDX ( 2), + DEFINE_VFP_V_IDX ( 3), + DEFINE_VFP_V_IDX ( 4), + DEFINE_VFP_V_IDX ( 5), + DEFINE_VFP_V_IDX ( 6), + DEFINE_VFP_V_IDX ( 7), + DEFINE_VFP_V_IDX ( 8), + DEFINE_VFP_V_IDX ( 9), + DEFINE_VFP_V_IDX (10), + DEFINE_VFP_V_IDX (11), + DEFINE_VFP_V_IDX (12), + DEFINE_VFP_V_IDX (13), + DEFINE_VFP_V_IDX (14), + DEFINE_VFP_V_IDX (15), + DEFINE_VFP_V_IDX (16), + DEFINE_VFP_V_IDX (17), + DEFINE_VFP_V_IDX (18), + DEFINE_VFP_V_IDX (19), + DEFINE_VFP_V_IDX (20), + DEFINE_VFP_V_IDX (21), + DEFINE_VFP_V_IDX (22), + DEFINE_VFP_V_IDX (23), + DEFINE_VFP_V_IDX (24), + DEFINE_VFP_V_IDX (25), + DEFINE_VFP_V_IDX (26), + DEFINE_VFP_V_IDX (27), + DEFINE_VFP_V_IDX (28), + DEFINE_VFP_V_IDX (29), + DEFINE_VFP_V_IDX (30), + DEFINE_VFP_V_IDX (31), + { e_regSetVFP, vfp_fpsr, "fpsr", NULL, Uint, Hex, 4, VFP_V_OFFSET_IDX (32) + 0, INVALID_NUB_REGNUM, INVALID_NUB_REGNUM, INVALID_NUB_REGNUM, INVALID_NUB_REGNUM, NULL, NULL }, + { e_regSetVFP, vfp_fpcr, "fpcr", NULL, Uint, Hex, 4, VFP_V_OFFSET_IDX (32) + 4, INVALID_NUB_REGNUM, INVALID_NUB_REGNUM, INVALID_NUB_REGNUM, INVALID_NUB_REGNUM, NULL, NULL }, + + DEFINE_PSEUDO_VFP_S_IDX (0), + DEFINE_PSEUDO_VFP_S_IDX (1), + DEFINE_PSEUDO_VFP_S_IDX (2), + DEFINE_PSEUDO_VFP_S_IDX (3), + DEFINE_PSEUDO_VFP_S_IDX (4), + DEFINE_PSEUDO_VFP_S_IDX (5), + DEFINE_PSEUDO_VFP_S_IDX (6), + DEFINE_PSEUDO_VFP_S_IDX (7), + DEFINE_PSEUDO_VFP_S_IDX (8), + DEFINE_PSEUDO_VFP_S_IDX (9), + DEFINE_PSEUDO_VFP_S_IDX (10), + DEFINE_PSEUDO_VFP_S_IDX (11), + DEFINE_PSEUDO_VFP_S_IDX (12), + DEFINE_PSEUDO_VFP_S_IDX (13), + DEFINE_PSEUDO_VFP_S_IDX (14), + DEFINE_PSEUDO_VFP_S_IDX (15), + DEFINE_PSEUDO_VFP_S_IDX (16), + DEFINE_PSEUDO_VFP_S_IDX (17), + DEFINE_PSEUDO_VFP_S_IDX (18), + DEFINE_PSEUDO_VFP_S_IDX (19), + DEFINE_PSEUDO_VFP_S_IDX (20), + DEFINE_PSEUDO_VFP_S_IDX (21), + DEFINE_PSEUDO_VFP_S_IDX (22), + DEFINE_PSEUDO_VFP_S_IDX (23), + DEFINE_PSEUDO_VFP_S_IDX (24), + DEFINE_PSEUDO_VFP_S_IDX (25), + DEFINE_PSEUDO_VFP_S_IDX (26), + DEFINE_PSEUDO_VFP_S_IDX (27), + DEFINE_PSEUDO_VFP_S_IDX (28), + DEFINE_PSEUDO_VFP_S_IDX (29), + DEFINE_PSEUDO_VFP_S_IDX (30), + DEFINE_PSEUDO_VFP_S_IDX (31), + + DEFINE_PSEUDO_VFP_D_IDX (0), + DEFINE_PSEUDO_VFP_D_IDX (1), + DEFINE_PSEUDO_VFP_D_IDX (2), + DEFINE_PSEUDO_VFP_D_IDX (3), + DEFINE_PSEUDO_VFP_D_IDX (4), + DEFINE_PSEUDO_VFP_D_IDX (5), + DEFINE_PSEUDO_VFP_D_IDX (6), + DEFINE_PSEUDO_VFP_D_IDX (7), + DEFINE_PSEUDO_VFP_D_IDX (8), + DEFINE_PSEUDO_VFP_D_IDX (9), + DEFINE_PSEUDO_VFP_D_IDX (10), + DEFINE_PSEUDO_VFP_D_IDX (11), + DEFINE_PSEUDO_VFP_D_IDX (12), + DEFINE_PSEUDO_VFP_D_IDX (13), + DEFINE_PSEUDO_VFP_D_IDX (14), + DEFINE_PSEUDO_VFP_D_IDX (15), + DEFINE_PSEUDO_VFP_D_IDX (16), + DEFINE_PSEUDO_VFP_D_IDX (17), + DEFINE_PSEUDO_VFP_D_IDX (18), + DEFINE_PSEUDO_VFP_D_IDX (19), + DEFINE_PSEUDO_VFP_D_IDX (20), + DEFINE_PSEUDO_VFP_D_IDX (21), + DEFINE_PSEUDO_VFP_D_IDX (22), + DEFINE_PSEUDO_VFP_D_IDX (23), + DEFINE_PSEUDO_VFP_D_IDX (24), + DEFINE_PSEUDO_VFP_D_IDX (25), + DEFINE_PSEUDO_VFP_D_IDX (26), + DEFINE_PSEUDO_VFP_D_IDX (27), + DEFINE_PSEUDO_VFP_D_IDX (28), + DEFINE_PSEUDO_VFP_D_IDX (29), + DEFINE_PSEUDO_VFP_D_IDX (30), + DEFINE_PSEUDO_VFP_D_IDX (31) + +}; + + +//_STRUCT_ARM_EXCEPTION_STATE64 +//{ +// uint64_t far; /* Virtual Fault Address */ +// uint32_t esr; /* Exception syndrome */ +// uint32_t exception; /* number of arm exception taken */ +//}; + +// Exception registers +const DNBRegisterInfo +DNBArchMachARM64::g_exc_registers[] = +{ + { e_regSetEXC, exc_far , "far" , NULL, Uint, Hex, 8, EXC_OFFSET(__far) , INVALID_NUB_REGNUM, INVALID_NUB_REGNUM, INVALID_NUB_REGNUM, INVALID_NUB_REGNUM, NULL, NULL }, + { e_regSetEXC, exc_esr , "esr" , NULL, Uint, Hex, 4, EXC_OFFSET(__esr) , INVALID_NUB_REGNUM, INVALID_NUB_REGNUM, INVALID_NUB_REGNUM, INVALID_NUB_REGNUM, NULL, NULL }, + { e_regSetEXC, exc_exception , "exception" , NULL, Uint, Hex, 4, EXC_OFFSET(__exception) , INVALID_NUB_REGNUM, INVALID_NUB_REGNUM, INVALID_NUB_REGNUM, INVALID_NUB_REGNUM, NULL, NULL } +}; + +// Number of registers in each register set +const size_t DNBArchMachARM64::k_num_gpr_registers = sizeof(g_gpr_registers)/sizeof(DNBRegisterInfo); +const size_t DNBArchMachARM64::k_num_vfp_registers = sizeof(g_vfp_registers)/sizeof(DNBRegisterInfo); +const size_t DNBArchMachARM64::k_num_exc_registers = sizeof(g_exc_registers)/sizeof(DNBRegisterInfo); +const size_t DNBArchMachARM64::k_num_all_registers = k_num_gpr_registers + k_num_vfp_registers + k_num_exc_registers; + +//---------------------------------------------------------------------- +// Register set definitions. The first definitions at register set index +// of zero is for all registers, followed by other registers sets. The +// register information for the all register set need not be filled in. +//---------------------------------------------------------------------- +const DNBRegisterSetInfo +DNBArchMachARM64::g_reg_sets[] = +{ + { "ARM64 Registers", NULL, k_num_all_registers }, + { "General Purpose Registers", g_gpr_registers, k_num_gpr_registers }, + { "Floating Point Registers", g_vfp_registers, k_num_vfp_registers }, + { "Exception State Registers", g_exc_registers, k_num_exc_registers } +}; +// Total number of register sets for this architecture +const size_t DNBArchMachARM64::k_num_register_sets = sizeof(g_reg_sets)/sizeof(DNBRegisterSetInfo); + + +const DNBRegisterSetInfo * +DNBArchMachARM64::GetRegisterSetInfo(nub_size_t *num_reg_sets) +{ + *num_reg_sets = k_num_register_sets; + return g_reg_sets; +} + +bool +DNBArchMachARM64::FixGenericRegisterNumber (int &set, int ®) +{ + if (set == REGISTER_SET_GENERIC) + { + switch (reg) + { + case GENERIC_REGNUM_PC: // Program Counter + set = e_regSetGPR; + reg = gpr_pc; + break; + + case GENERIC_REGNUM_SP: // Stack Pointer + set = e_regSetGPR; + reg = gpr_sp; + break; + + case GENERIC_REGNUM_FP: // Frame Pointer + set = e_regSetGPR; + reg = gpr_fp; + break; + + case GENERIC_REGNUM_RA: // Return Address + set = e_regSetGPR; + reg = gpr_lr; + break; + + case GENERIC_REGNUM_FLAGS: // Processor flags register + set = e_regSetGPR; + reg = gpr_cpsr; + break; + + case GENERIC_REGNUM_ARG1: + case GENERIC_REGNUM_ARG2: + case GENERIC_REGNUM_ARG3: + case GENERIC_REGNUM_ARG4: + case GENERIC_REGNUM_ARG5: + case GENERIC_REGNUM_ARG6: + set = e_regSetGPR; + reg = gpr_x0 + reg - GENERIC_REGNUM_ARG1; + break; + + default: + return false; + } + } + return true; +} +bool +DNBArchMachARM64::GetRegisterValue(int set, int reg, DNBRegisterValue *value) +{ + if (!FixGenericRegisterNumber (set, reg)) + return false; + + if (GetRegisterState(set, false) != KERN_SUCCESS) + return false; + + const DNBRegisterInfo *regInfo = m_thread->GetRegisterInfo(set, reg); + if (regInfo) + { + value->info = *regInfo; + switch (set) + { + case e_regSetGPR: + if (reg <= gpr_pc) + { + value->value.uint64 = m_state.context.gpr.__x[reg]; + return true; + } + else if (reg == gpr_cpsr) + { + value->value.uint32 = m_state.context.gpr.__cpsr; + return true; + } + break; + + case e_regSetVFP: + + if (reg >= vfp_v0 && reg <= vfp_v31) + { +#if defined (__arm64__) + memcpy (&value->value.v_uint8, &m_state.context.vfp.__v[reg - vfp_v0], 16); +#else + memcpy (&value->value.v_uint8, ((uint8_t *) &m_state.context.vfp.opaque) + ((reg - vfp_v0) * 16), 16); +#endif + return true; + } + else if (reg == vfp_fpsr) + { +#if defined (__arm64__) + memcpy (&value->value.uint32, &m_state.context.vfp.__fpsr, 4); +#else + memcpy (&value->value.uint32, ((uint8_t *) &m_state.context.vfp.opaque) + (32 * 16) + 0, 4); +#endif + return true; + } + else if (reg == vfp_fpcr) + { +#if defined (__arm64__) + memcpy (&value->value.uint32, &m_state.context.vfp.__fpcr, 4); +#else + memcpy (&value->value.uint32, ((uint8_t *) &m_state.context.vfp.opaque) + (32 * 16) + 4, 4); +#endif + return true; + } + else if (reg >= vfp_s0 && reg <= vfp_s31) + { +#if defined (__arm64__) + memcpy (&value->value.v_uint8, &m_state.context.vfp.__v[reg - vfp_s0], 4); +#else + memcpy (&value->value.v_uint8, ((uint8_t *) &m_state.context.vfp.opaque) + ((reg - vfp_s0) * 16), 4); +#endif + return true; + } + else if (reg >= vfp_d0 && reg <= vfp_d31) + { +#if defined (__arm64__) + memcpy (&value->value.v_uint8, &m_state.context.vfp.__v[reg - vfp_d0], 8); +#else + memcpy (&value->value.v_uint8, ((uint8_t *) &m_state.context.vfp.opaque) + ((reg - vfp_d0) * 16), 8); +#endif + return true; + } + break; + + case e_regSetEXC: + if (reg == exc_far) + { + value->value.uint64 = m_state.context.exc.__far; + return true; + } + else if (reg == exc_esr) + { + value->value.uint32 = m_state.context.exc.__esr; + return true; + } + else if (reg == exc_exception) + { + value->value.uint32 = m_state.context.exc.__exception; + return true; + } + break; + } + } + return false; +} + +bool +DNBArchMachARM64::SetRegisterValue(int set, int reg, const DNBRegisterValue *value) +{ + if (!FixGenericRegisterNumber (set, reg)) + return false; + + if (GetRegisterState(set, false) != KERN_SUCCESS) + return false; + + bool success = false; + const DNBRegisterInfo *regInfo = m_thread->GetRegisterInfo(set, reg); + if (regInfo) + { + switch (set) + { + case e_regSetGPR: + if (reg <= gpr_pc) + { + m_state.context.gpr.__x[reg] = value->value.uint64; + success = true; + } + else if (reg == gpr_cpsr) + { + m_state.context.gpr.__cpsr = value->value.uint32; + success = true; + } + break; + + case e_regSetVFP: + if (reg >= vfp_v0 && reg <= vfp_v31) + { +#if defined (__arm64__) + memcpy (&m_state.context.vfp.__v[reg - vfp_v0], &value->value.v_uint8, 16); +#else + memcpy (((uint8_t *) &m_state.context.vfp.opaque) + ((reg - vfp_v0) * 16), &value->value.v_uint8, 16); +#endif + success = true; + } + else if (reg == vfp_fpsr) + { +#if defined (__arm64__) + memcpy (&m_state.context.vfp.__fpsr, &value->value.uint32, 4); +#else + memcpy (((uint8_t *) &m_state.context.vfp.opaque) + (32 * 16) + 0, &value->value.uint32, 4); +#endif + success = true; + } + else if (reg == vfp_fpcr) + { +#if defined (__arm64__) + memcpy (&m_state.context.vfp.__fpcr, &value->value.uint32, 4); +#else + memcpy (((uint8_t *) m_state.context.vfp.opaque) + (32 * 16) + 4, &value->value.uint32, 4); +#endif + success = true; + } + else if (reg >= vfp_s0 && reg <= vfp_s31) + { +#if defined (__arm64__) + memcpy (&m_state.context.vfp.__v[reg - vfp_s0], &value->value.v_uint8, 4); +#else + memcpy (((uint8_t *) &m_state.context.vfp.opaque) + ((reg - vfp_s0) * 16), &value->value.v_uint8, 4); +#endif + success = true; + } + else if (reg >= vfp_d0 && reg <= vfp_d31) + { +#if defined (__arm64__) + memcpy (&m_state.context.vfp.__v[reg - vfp_d0], &value->value.v_uint8, 8); +#else + memcpy (((uint8_t *) &m_state.context.vfp.opaque) + ((reg - vfp_d0) * 16), &value->value.v_uint8, 8); +#endif + success = true; + } + break; + + case e_regSetEXC: + if (reg == exc_far) + { + m_state.context.exc.__far = value->value.uint64; + success = true; + } + else if (reg == exc_esr) + { + m_state.context.exc.__esr = value->value.uint32; + success = true; + } + else if (reg == exc_exception) + { + m_state.context.exc.__exception = value->value.uint32; + success = true; + } + break; + } + + } + if (success) + return SetRegisterState(set) == KERN_SUCCESS; + return false; +} + +kern_return_t +DNBArchMachARM64::GetRegisterState(int set, bool force) +{ + switch (set) + { + case e_regSetALL: return GetGPRState(force) | + GetVFPState(force) | + GetEXCState(force) | + GetDBGState(force); + case e_regSetGPR: return GetGPRState(force); + case e_regSetVFP: return GetVFPState(force); + case e_regSetEXC: return GetEXCState(force); + case e_regSetDBG: return GetDBGState(force); + default: break; + } + return KERN_INVALID_ARGUMENT; +} + +kern_return_t +DNBArchMachARM64::SetRegisterState(int set) +{ + // Make sure we have a valid context to set. + kern_return_t err = GetRegisterState(set, false); + if (err != KERN_SUCCESS) + return err; + + switch (set) + { + case e_regSetALL: return SetGPRState() | + SetVFPState() | + SetEXCState() | + SetDBGState(false); + case e_regSetGPR: return SetGPRState(); + case e_regSetVFP: return SetVFPState(); + case e_regSetEXC: return SetEXCState(); + case e_regSetDBG: return SetDBGState(false); + default: break; + } + return KERN_INVALID_ARGUMENT; +} + +bool +DNBArchMachARM64::RegisterSetStateIsValid (int set) const +{ + return m_state.RegsAreValid(set); +} + + +nub_size_t +DNBArchMachARM64::GetRegisterContext (void *buf, nub_size_t buf_len) +{ + nub_size_t size = sizeof (m_state.context.gpr) + + sizeof (m_state.context.vfp) + + sizeof (m_state.context.exc); + + if (buf && buf_len) + { + if (size > buf_len) + size = buf_len; + + bool force = false; + if (GetGPRState(force) | GetVFPState(force) | GetEXCState(force)) + return 0; + + // Copy each struct individually to avoid any padding that might be between the structs in m_state.context + uint8_t *p = (uint8_t *)buf; + ::memcpy (p, &m_state.context.gpr, sizeof(m_state.context.gpr)); + p += sizeof(m_state.context.gpr); + ::memcpy (p, &m_state.context.vfp, sizeof(m_state.context.vfp)); + p += sizeof(m_state.context.vfp); + ::memcpy (p, &m_state.context.exc, sizeof(m_state.context.exc)); + p += sizeof(m_state.context.exc); + + size_t bytes_written = p - (uint8_t *)buf; + assert (bytes_written == size); + } + DNBLogThreadedIf (LOG_THREAD, "DNBArchMachARM64::GetRegisterContext (buf = %p, len = %zu) => %zu", buf, buf_len, size); + // Return the size of the register context even if NULL was passed in + return size; +} + +nub_size_t +DNBArchMachARM64::SetRegisterContext (const void *buf, nub_size_t buf_len) +{ + nub_size_t size = sizeof (m_state.context.gpr) + + sizeof (m_state.context.vfp) + + sizeof (m_state.context.exc); + + if (buf == NULL || buf_len == 0) + size = 0; + + if (size) + { + if (size > buf_len) + size = buf_len; + + // Copy each struct individually to avoid any padding that might be between the structs in m_state.context + uint8_t *p = (uint8_t *)buf; + ::memcpy (&m_state.context.gpr, p, sizeof(m_state.context.gpr)); + p += sizeof(m_state.context.gpr); + ::memcpy (&m_state.context.vfp, p, sizeof(m_state.context.vfp)); + p += sizeof(m_state.context.vfp); + ::memcpy (&m_state.context.exc, p, sizeof(m_state.context.exc)); + p += sizeof(m_state.context.exc); + + size_t bytes_written = p - (uint8_t *)buf; + assert (bytes_written == size); + SetGPRState(); + SetVFPState(); + SetEXCState(); + } + DNBLogThreadedIf (LOG_THREAD, "DNBArchMachARM64::SetRegisterContext (buf = %p, len = %zu) => %zu", buf, buf_len, size); + return size; +} + +uint32_t +DNBArchMachARM64::SaveRegisterState () +{ + kern_return_t kret = ::thread_abort_safely(m_thread->MachPortNumber()); + DNBLogThreadedIf (LOG_THREAD, "thread = 0x%4.4x calling thread_abort_safely (tid) => %u (SetGPRState() for stop_count = %u)", m_thread->MachPortNumber(), kret, m_thread->Process()->StopCount()); + + // Always re-read the registers because above we call thread_abort_safely(); + bool force = true; + + if ((kret = GetGPRState(force)) != KERN_SUCCESS) + { + DNBLogThreadedIf (LOG_THREAD, "DNBArchMachARM64::SaveRegisterState () error: GPR regs failed to read: %u ", kret); + } + else if ((kret = GetVFPState(force)) != KERN_SUCCESS) + { + DNBLogThreadedIf (LOG_THREAD, "DNBArchMachARM64::SaveRegisterState () error: %s regs failed to read: %u", "VFP", kret); + } + else + { + const uint32_t save_id = GetNextRegisterStateSaveID (); + m_saved_register_states[save_id] = m_state.context; + return save_id; + } + return UINT32_MAX; +} + +bool +DNBArchMachARM64::RestoreRegisterState (uint32_t save_id) +{ + SaveRegisterStates::iterator pos = m_saved_register_states.find(save_id); + if (pos != m_saved_register_states.end()) + { + m_state.context.gpr = pos->second.gpr; + m_state.context.vfp = pos->second.vfp; + kern_return_t kret; + bool success = true; + if ((kret = SetGPRState()) != KERN_SUCCESS) + { + DNBLogThreadedIf (LOG_THREAD, "DNBArchMachARM64::RestoreRegisterState (save_id = %u) error: GPR regs failed to write: %u", save_id, kret); + success = false; + } + else if ((kret = SetVFPState()) != KERN_SUCCESS) + { + DNBLogThreadedIf (LOG_THREAD, "DNBArchMachARM64::RestoreRegisterState (save_id = %u) error: %s regs failed to write: %u", save_id, "VFP", kret); + success = false; + } + m_saved_register_states.erase(pos); + return success; + } + return false; +} + + +#endif // #if defined (ARM_THREAD_STATE64_COUNT) +#endif // #if defined (__arm__) diff --git a/lldb/tools/debugserver/source/MacOSX/arm64/DNBArchImplARM64.h b/lldb/tools/debugserver/source/MacOSX/arm64/DNBArchImplARM64.h new file mode 100644 index 00000000000..0d2a998265f --- /dev/null +++ b/lldb/tools/debugserver/source/MacOSX/arm64/DNBArchImplARM64.h @@ -0,0 +1,272 @@ +//===-- DNBArchMachARM64.h --------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + + +#ifndef __DNBArchImplARM64_h__ +#define __DNBArchImplARM64_h__ + +#if defined (__arm__) || defined (__arm64__) + +#include <map> +#include <mach/thread_status.h> + +#if defined (ARM_THREAD_STATE64_COUNT) + +#include "DNBArch.h" + +class MachThread; + +class DNBArchMachARM64 : public DNBArchProtocol +{ +public: + enum { kMaxNumThumbITBreakpoints = 4 }; + + DNBArchMachARM64(MachThread *thread) : + m_thread(thread), + m_state(), + m_disabled_watchpoints(), + m_watchpoint_hw_index(-1), + m_watchpoint_did_occur(false), + m_watchpoint_resume_single_step_enabled(false), + m_saved_register_states() + { + m_disabled_watchpoints.resize (16); + memset(&m_dbg_save, 0, sizeof(m_dbg_save)); + } + + virtual ~DNBArchMachARM64() + { + } + + static void Initialize(); + static const DNBRegisterSetInfo * + GetRegisterSetInfo(nub_size_t *num_reg_sets); + + virtual bool GetRegisterValue(int set, int reg, DNBRegisterValue *value); + virtual bool SetRegisterValue(int set, int reg, const DNBRegisterValue *value); + virtual nub_size_t GetRegisterContext (void *buf, nub_size_t buf_len); + virtual nub_size_t SetRegisterContext (const void *buf, nub_size_t buf_len); + virtual uint32_t SaveRegisterState (); + virtual bool RestoreRegisterState (uint32_t save_id); + + virtual kern_return_t GetRegisterState (int set, bool force); + virtual kern_return_t SetRegisterState (int set); + virtual bool RegisterSetStateIsValid (int set) const; + + virtual uint64_t GetPC(uint64_t failValue); // Get program counter + virtual kern_return_t SetPC(uint64_t value); + virtual uint64_t GetSP(uint64_t failValue); // Get stack pointer + virtual void ThreadWillResume(); + virtual bool ThreadDidStop(); + virtual bool NotifyException(MachException::Data& exc); + + static DNBArchProtocol *Create (MachThread *thread); + static const uint8_t * const SoftwareBreakpointOpcode (nub_size_t byte_size); + static uint32_t GetCPUType(); + + virtual uint32_t NumSupportedHardwareWatchpoints(); + virtual uint32_t EnableHardwareWatchpoint (nub_addr_t addr, nub_size_t size, bool read, bool write, bool also_set_on_task); + virtual bool DisableHardwareWatchpoint (uint32_t hw_break_index, bool also_set_on_task); + virtual bool DisableHardwareWatchpoint_helper (uint32_t hw_break_index, bool also_set_on_task); + +protected: + + + kern_return_t EnableHardwareSingleStep (bool enable); + static bool FixGenericRegisterNumber (int &set, int ®); + + typedef enum RegisterSetTag + { + e_regSetALL = REGISTER_SET_ALL, + e_regSetGPR, // ARM_THREAD_STATE64, + e_regSetVFP, // ARM_NEON_STATE64, + e_regSetEXC, // ARM_EXCEPTION_STATE64, + e_regSetDBG, // ARM_DEBUG_STATE64, + kNumRegisterSets + } RegisterSet; + + enum + { + e_regSetGPRCount = ARM_THREAD_STATE64_COUNT, + e_regSetVFPCount = ARM_NEON_STATE64_COUNT, + e_regSetEXCCount = ARM_EXCEPTION_STATE64_COUNT, + e_regSetDBGCount = ARM_DEBUG_STATE64_COUNT, + }; + + enum + { + Read = 0, + Write = 1, + kNumErrors = 2 + }; + + typedef arm_thread_state64_t GPR; + typedef arm_neon_state64_t FPU; + typedef arm_exception_state64_t EXC; + + static const DNBRegisterInfo g_gpr_registers[]; + static const DNBRegisterInfo g_vfp_registers[]; + static const DNBRegisterInfo g_exc_registers[]; + static const DNBRegisterSetInfo g_reg_sets[]; + + static const size_t k_num_gpr_registers; + static const size_t k_num_vfp_registers; + static const size_t k_num_exc_registers; + static const size_t k_num_all_registers; + static const size_t k_num_register_sets; + + struct Context + { + GPR gpr; + FPU vfp; + EXC exc; + }; + + struct State + { + Context context; + arm_debug_state64_t dbg; + kern_return_t gpr_errs[2]; // Read/Write errors + kern_return_t vfp_errs[2]; // Read/Write errors + kern_return_t exc_errs[2]; // Read/Write errors + kern_return_t dbg_errs[2]; // Read/Write errors + State() + { + uint32_t i; + for (i=0; i<kNumErrors; i++) + { + gpr_errs[i] = -1; + vfp_errs[i] = -1; + exc_errs[i] = -1; + dbg_errs[i] = -1; + } + } + void InvalidateRegisterSetState(int set) + { + SetError (set, Read, -1); + } + + void + InvalidateAllRegisterStates() + { + SetError (e_regSetALL, Read, -1); + } + + kern_return_t GetError (int set, uint32_t err_idx) const + { + if (err_idx < kNumErrors) + { + switch (set) + { + // When getting all errors, just OR all values together to see if + // we got any kind of error. + case e_regSetALL: return gpr_errs[err_idx] | + vfp_errs[err_idx] | + exc_errs[err_idx] | + dbg_errs[err_idx] ; + case e_regSetGPR: return gpr_errs[err_idx]; + case e_regSetVFP: return vfp_errs[err_idx]; + case e_regSetEXC: return exc_errs[err_idx]; + //case e_regSetDBG: return dbg_errs[err_idx]; + default: break; + } + } + return -1; + } + bool SetError (int set, uint32_t err_idx, kern_return_t err) + { + if (err_idx < kNumErrors) + { + switch (set) + { + case e_regSetALL: + gpr_errs[err_idx] = err; + vfp_errs[err_idx] = err; + dbg_errs[err_idx] = err; + exc_errs[err_idx] = err; + return true; + + case e_regSetGPR: + gpr_errs[err_idx] = err; + return true; + + case e_regSetVFP: + vfp_errs[err_idx] = err; + return true; + + case e_regSetEXC: + exc_errs[err_idx] = err; + return true; + +// case e_regSetDBG: +// dbg_errs[err_idx] = err; +// return true; + default: break; + } + } + return false; + } + bool RegsAreValid (int set) const + { + return GetError(set, Read) == KERN_SUCCESS; + } + }; + + kern_return_t GetGPRState (bool force); + kern_return_t GetVFPState (bool force); + kern_return_t GetEXCState (bool force); + kern_return_t GetDBGState (bool force); + + kern_return_t SetGPRState (); + kern_return_t SetVFPState (); + kern_return_t SetEXCState (); + kern_return_t SetDBGState (bool also_set_on_task); + + // Helper functions for watchpoint implementaions. + + typedef arm_debug_state64_t DBG; + + void ClearWatchpointOccurred(); + bool HasWatchpointOccurred(); + bool IsWatchpointEnabled(const DBG &debug_state, uint32_t hw_index); + nub_addr_t GetWatchpointAddressByIndex (uint32_t hw_index); + nub_addr_t GetWatchAddress(const DBG &debug_state, uint32_t hw_index); + virtual bool ReenableHardwareWatchpoint (uint32_t hw_break_index); + virtual bool ReenableHardwareWatchpoint_helper (uint32_t hw_break_index); + virtual uint32_t GetHardwareWatchpointHit(nub_addr_t &addr); + + + class disabled_watchpoint { + public: + disabled_watchpoint () { addr = 0; control = 0; } + nub_addr_t addr; + uint32_t control; + }; + +protected: + MachThread * m_thread; + State m_state; + arm_debug_state64_t m_dbg_save; + + // arm64 doesn't keep the disabled watchpoint values in the debug register context like armv7; + // we need to save them aside when we disable them temporarily. + std::vector<disabled_watchpoint> m_disabled_watchpoints; + + // The following member variables should be updated atomically. + int32_t m_watchpoint_hw_index; + bool m_watchpoint_did_occur; + bool m_watchpoint_resume_single_step_enabled; + + typedef std::map<uint32_t, Context> SaveRegisterStates; + SaveRegisterStates m_saved_register_states; +}; + +#endif // #if defined (ARM_THREAD_STATE64_COUNT) +#endif // #if defined (__arm__) +#endif // #ifndef __DNBArchImplARM64_h__ |

