Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 1 | //===-- Watchpoint.cpp ------------------------------------------*- C++ -*-===// |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 10 | #include "lldb/Breakpoint/Watchpoint.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 11 | |
| 12 | // C Includes |
| 13 | // C++ Includes |
| 14 | // Other libraries and framework includes |
| 15 | // Project includes |
Johnny Chen | 16dcf71 | 2011-10-17 18:58:00 +0000 | [diff] [blame] | 16 | #include "lldb/Breakpoint/StoppointCallbackContext.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 17 | #include "lldb/Core/Stream.h" |
Johnny Chen | 16dcf71 | 2011-10-17 18:58:00 +0000 | [diff] [blame] | 18 | #include "lldb/Target/Process.h" |
| 19 | #include "lldb/Target/Target.h" |
| 20 | #include "lldb/Target/ThreadSpec.h" |
| 21 | #include "lldb/Target/ThreadPlanTestCondition.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace lldb; |
| 24 | using namespace lldb_private; |
| 25 | |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 26 | Watchpoint::Watchpoint (lldb::addr_t addr, size_t size, bool hardware) : |
Johnny Chen | 8ed0ef9 | 2011-09-06 02:52:28 +0000 | [diff] [blame] | 27 | StoppointLocation (GetNextID(), addr, size, hardware), |
Johnny Chen | 5d04346 | 2011-09-26 22:40:50 +0000 | [diff] [blame] | 28 | m_target(NULL), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | m_enabled(0), |
Johnny Chen | f04ee93 | 2011-09-22 18:04:58 +0000 | [diff] [blame] | 30 | m_is_hardware(hardware), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 31 | m_watch_read(0), |
| 32 | m_watch_write(0), |
| 33 | m_watch_was_read(0), |
| 34 | m_watch_was_written(0), |
| 35 | m_ignore_count(0), |
| 36 | m_callback(NULL), |
Johnny Chen | ed456eb | 2011-10-14 19:15:48 +0000 | [diff] [blame] | 37 | m_callback_baton(NULL), |
| 38 | m_decl_str(), |
| 39 | m_error() |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 40 | { |
| 41 | } |
| 42 | |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 43 | Watchpoint::~Watchpoint() |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 44 | { |
| 45 | } |
| 46 | |
| 47 | break_id_t |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 48 | Watchpoint::GetNextID() |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 49 | { |
| 50 | static break_id_t g_next_ID = 0; |
| 51 | return ++g_next_ID; |
| 52 | } |
| 53 | |
| 54 | bool |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 55 | Watchpoint::SetCallback (WatchpointHitCallback callback, void *callback_baton) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 56 | { |
| 57 | m_callback = callback; |
| 58 | m_callback_baton = callback_baton; |
| 59 | return true; |
| 60 | } |
| 61 | |
Johnny Chen | de6bd24 | 2011-09-16 21:41:42 +0000 | [diff] [blame] | 62 | void |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 63 | Watchpoint::SetDeclInfo (std::string &str) |
Johnny Chen | de6bd24 | 2011-09-16 21:41:42 +0000 | [diff] [blame] | 64 | { |
| 65 | m_decl_str = str; |
| 66 | return; |
| 67 | } |
| 68 | |
Johnny Chen | fab7a91 | 2012-01-23 23:03:59 +0000 | [diff] [blame] | 69 | // Override default impl of StoppointLocation::IsHardware() since m_is_hardware |
| 70 | // member field is more accurate. |
Johnny Chen | f04ee93 | 2011-09-22 18:04:58 +0000 | [diff] [blame] | 71 | bool |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 72 | Watchpoint::IsHardware () const |
Johnny Chen | f04ee93 | 2011-09-22 18:04:58 +0000 | [diff] [blame] | 73 | { |
| 74 | return m_is_hardware; |
| 75 | } |
| 76 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 77 | // RETURNS - true if we should stop at this breakpoint, false if we |
| 78 | // should continue. |
| 79 | |
| 80 | bool |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 81 | Watchpoint::ShouldStop (StoppointCallbackContext *context) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 82 | { |
Johnny Chen | fab7a91 | 2012-01-23 23:03:59 +0000 | [diff] [blame] | 83 | IncrementHitCount(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 84 | |
Johnny Chen | f04ee93 | 2011-09-22 18:04:58 +0000 | [diff] [blame] | 85 | if (!IsEnabled()) |
| 86 | return false; |
Johnny Chen | fd158f4 | 2011-09-21 22:47:15 +0000 | [diff] [blame] | 87 | |
Johnny Chen | fab7a91 | 2012-01-23 23:03:59 +0000 | [diff] [blame] | 88 | if (GetHitCount() <= GetIgnoreCount()) |
Johnny Chen | f04ee93 | 2011-09-22 18:04:58 +0000 | [diff] [blame] | 89 | return false; |
| 90 | |
Johnny Chen | 16dcf71 | 2011-10-17 18:58:00 +0000 | [diff] [blame] | 91 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 95 | Watchpoint::GetDescription (Stream *s, lldb::DescriptionLevel level) |
Johnny Chen | 887062a | 2011-09-12 23:38:44 +0000 | [diff] [blame] | 96 | { |
Johnny Chen | de6bd24 | 2011-09-16 21:41:42 +0000 | [diff] [blame] | 97 | DumpWithLevel(s, level); |
Johnny Chen | 887062a | 2011-09-12 23:38:44 +0000 | [diff] [blame] | 98 | return; |
| 99 | } |
| 100 | |
| 101 | void |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 102 | Watchpoint::Dump(Stream *s) const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 103 | { |
Johnny Chen | de6bd24 | 2011-09-16 21:41:42 +0000 | [diff] [blame] | 104 | DumpWithLevel(s, lldb::eDescriptionLevelBrief); |
| 105 | } |
| 106 | |
| 107 | void |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 108 | Watchpoint::DumpWithLevel(Stream *s, lldb::DescriptionLevel description_level) const |
Johnny Chen | de6bd24 | 2011-09-16 21:41:42 +0000 | [diff] [blame] | 109 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 110 | if (s == NULL) |
| 111 | return; |
| 112 | |
Johnny Chen | de6bd24 | 2011-09-16 21:41:42 +0000 | [diff] [blame] | 113 | assert(description_level >= lldb::eDescriptionLevelBrief && |
| 114 | description_level <= lldb::eDescriptionLevelVerbose); |
| 115 | |
Greg Clayton | c91d804 | 2011-12-03 00:46:21 +0000 | [diff] [blame] | 116 | s->Printf("Watchpoint %u: addr = 0x%8.8llx size = %u state = %s type = %s%s", |
Johnny Chen | de6bd24 | 2011-09-16 21:41:42 +0000 | [diff] [blame] | 117 | GetID(), |
Johnny Chen | a5cde26 | 2012-01-24 00:11:02 +0000 | [diff] [blame] | 118 | GetLoadAddress(), |
Johnny Chen | de6bd24 | 2011-09-16 21:41:42 +0000 | [diff] [blame] | 119 | m_byte_size, |
| 120 | m_enabled ? "enabled" : "disabled", |
| 121 | m_watch_read ? "r" : "", |
| 122 | m_watch_write ? "w" : ""); |
| 123 | |
Johnny Chen | 16dcf71 | 2011-10-17 18:58:00 +0000 | [diff] [blame] | 124 | if (description_level >= lldb::eDescriptionLevelFull) { |
Johnny Chen | dedb67a | 2012-01-30 21:46:17 +0000 | [diff] [blame^] | 125 | if (!m_decl_str.empty()) |
Johnny Chen | 16dcf71 | 2011-10-17 18:58:00 +0000 | [diff] [blame] | 126 | s->Printf("\n declare @ '%s'", m_decl_str.c_str()); |
| 127 | if (GetConditionText()) |
| 128 | s->Printf("\n condition = '%s'", GetConditionText()); |
| 129 | } |
Johnny Chen | de6bd24 | 2011-09-16 21:41:42 +0000 | [diff] [blame] | 130 | |
| 131 | if (description_level >= lldb::eDescriptionLevelVerbose) |
Johnny Chen | 98a49d9b | 2011-10-06 20:27:05 +0000 | [diff] [blame] | 132 | if (m_callback) |
| 133 | s->Printf("\n hw_index = %i hit_count = %-4u ignore_count = %-4u callback = %8p baton = %8p", |
| 134 | GetHardwareIndex(), |
| 135 | GetHitCount(), |
| 136 | GetIgnoreCount(), |
| 137 | m_callback, |
| 138 | m_callback_baton); |
| 139 | else |
| 140 | s->Printf("\n hw_index = %i hit_count = %-4u ignore_count = %-4u", |
| 141 | GetHardwareIndex(), |
| 142 | GetHitCount(), |
| 143 | GetIgnoreCount()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | bool |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 147 | Watchpoint::IsEnabled() const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 148 | { |
| 149 | return m_enabled; |
| 150 | } |
| 151 | |
| 152 | void |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 153 | Watchpoint::SetEnabled(bool enabled) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 154 | { |
| 155 | if (!enabled) |
| 156 | SetHardwareIndex(LLDB_INVALID_INDEX32); |
| 157 | m_enabled = enabled; |
| 158 | } |
| 159 | |
| 160 | void |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 161 | Watchpoint::SetWatchpointType (uint32_t type) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 162 | { |
| 163 | m_watch_read = (type & LLDB_WATCH_TYPE_READ) != 0; |
| 164 | m_watch_write = (type & LLDB_WATCH_TYPE_WRITE) != 0; |
| 165 | } |
| 166 | |
| 167 | bool |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 168 | Watchpoint::WatchpointRead () const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 169 | { |
| 170 | return m_watch_read != 0; |
| 171 | } |
| 172 | bool |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 173 | Watchpoint::WatchpointWrite () const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 174 | { |
| 175 | return m_watch_write != 0; |
| 176 | } |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 177 | uint32_t |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 178 | Watchpoint::GetIgnoreCount () const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 179 | { |
| 180 | return m_ignore_count; |
| 181 | } |
| 182 | |
| 183 | void |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 184 | Watchpoint::SetIgnoreCount (uint32_t n) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 185 | { |
| 186 | m_ignore_count = n; |
| 187 | } |
Johnny Chen | 16dcf71 | 2011-10-17 18:58:00 +0000 | [diff] [blame] | 188 | |
| 189 | bool |
| 190 | Watchpoint::InvokeCallback (StoppointCallbackContext *context) |
| 191 | { |
| 192 | if (m_callback && context->is_synchronous) |
| 193 | { |
| 194 | uint32_t access = 0; |
| 195 | if (m_watch_was_read) |
| 196 | access |= LLDB_WATCH_TYPE_READ; |
| 197 | if (m_watch_was_written) |
| 198 | access |= LLDB_WATCH_TYPE_WRITE; |
| 199 | return m_callback(m_callback_baton, context, GetID(), access); |
| 200 | } |
| 201 | else |
| 202 | return true; |
| 203 | } |
| 204 | |
| 205 | void |
| 206 | Watchpoint::SetCondition (const char *condition) |
| 207 | { |
| 208 | if (condition == NULL || condition[0] == '\0') |
| 209 | { |
| 210 | if (m_condition_ap.get()) |
| 211 | m_condition_ap.reset(); |
| 212 | } |
| 213 | else |
| 214 | { |
| 215 | // Pass NULL for expr_prefix (no translation-unit level definitions). |
Sean Callanan | 20bb3aa | 2011-12-21 22:22:58 +0000 | [diff] [blame] | 216 | m_condition_ap.reset(new ClangUserExpression (condition, NULL, lldb::eLanguageTypeUnknown, ClangUserExpression::eResultTypeAny)); |
Johnny Chen | 16dcf71 | 2011-10-17 18:58:00 +0000 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
| 220 | const char * |
| 221 | Watchpoint::GetConditionText () const |
| 222 | { |
| 223 | if (m_condition_ap.get()) |
| 224 | return m_condition_ap->GetUserText(); |
| 225 | else |
| 226 | return NULL; |
| 227 | } |
| 228 | |