Johnny Chen | ecd4feb | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 1 | //===-- Watchpoint.cpp ------------------------------------------*- C++ -*-===// |
Chris Lattner | 24943d2 | 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 | ecd4feb | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 10 | #include "lldb/Breakpoint/Watchpoint.h" |
Chris Lattner | 24943d2 | 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 | 712a628 | 2011-10-17 18:58:00 +0000 | [diff] [blame] | 16 | #include "lldb/Breakpoint/StoppointCallbackContext.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 17 | #include "lldb/Core/Stream.h" |
Johnny Chen | 712a628 | 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 | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace lldb; |
| 24 | using namespace lldb_private; |
| 25 | |
Johnny Chen | ecd4feb | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 26 | Watchpoint::Watchpoint (lldb::addr_t addr, size_t size, bool hardware) : |
Johnny Chen | edda23f | 2011-09-06 02:52:28 +0000 | [diff] [blame] | 27 | StoppointLocation (GetNextID(), addr, size, hardware), |
Johnny Chen | 096c293 | 2011-09-26 22:40:50 +0000 | [diff] [blame] | 28 | m_target(NULL), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | m_enabled(0), |
Johnny Chen | 01acfa7 | 2011-09-22 18:04:58 +0000 | [diff] [blame] | 30 | m_is_hardware(hardware), |
Chris Lattner | 24943d2 | 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 | 41a55ef | 2011-10-14 19:15:48 +0000 | [diff] [blame] | 37 | m_callback_baton(NULL), |
| 38 | m_decl_str(), |
| 39 | m_error() |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 40 | { |
| 41 | } |
| 42 | |
Johnny Chen | ecd4feb | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 43 | Watchpoint::~Watchpoint() |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 44 | { |
| 45 | } |
| 46 | |
| 47 | break_id_t |
Johnny Chen | ecd4feb | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 48 | Watchpoint::GetNextID() |
Chris Lattner | 24943d2 | 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 | ecd4feb | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 55 | Watchpoint::SetCallback (WatchpointHitCallback callback, void *callback_baton) |
Chris Lattner | 24943d2 | 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 | 10b12b3 | 2011-09-16 21:41:42 +0000 | [diff] [blame] | 62 | void |
Johnny Chen | ecd4feb | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 63 | Watchpoint::SetDeclInfo (std::string &str) |
Johnny Chen | 10b12b3 | 2011-09-16 21:41:42 +0000 | [diff] [blame] | 64 | { |
| 65 | m_decl_str = str; |
| 66 | return; |
| 67 | } |
| 68 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 69 | |
Johnny Chen | 01acfa7 | 2011-09-22 18:04:58 +0000 | [diff] [blame] | 70 | bool |
Johnny Chen | ecd4feb | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 71 | Watchpoint::IsHardware () const |
Johnny Chen | 01acfa7 | 2011-09-22 18:04:58 +0000 | [diff] [blame] | 72 | { |
| 73 | return m_is_hardware; |
| 74 | } |
| 75 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 76 | // RETURNS - true if we should stop at this breakpoint, false if we |
| 77 | // should continue. |
| 78 | |
| 79 | bool |
Johnny Chen | ecd4feb | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 80 | Watchpoint::ShouldStop (StoppointCallbackContext *context) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 81 | { |
Johnny Chen | 01acfa7 | 2011-09-22 18:04:58 +0000 | [diff] [blame] | 82 | ++m_hit_count; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 83 | |
Johnny Chen | 01acfa7 | 2011-09-22 18:04:58 +0000 | [diff] [blame] | 84 | if (!IsEnabled()) |
| 85 | return false; |
Johnny Chen | 043f8c2 | 2011-09-21 22:47:15 +0000 | [diff] [blame] | 86 | |
Johnny Chen | 01acfa7 | 2011-09-22 18:04:58 +0000 | [diff] [blame] | 87 | if (m_hit_count <= GetIgnoreCount()) |
| 88 | return false; |
| 89 | |
Johnny Chen | 712a628 | 2011-10-17 18:58:00 +0000 | [diff] [blame] | 90 | return true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void |
Johnny Chen | ecd4feb | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 94 | Watchpoint::GetDescription (Stream *s, lldb::DescriptionLevel level) |
Johnny Chen | 34bbf85 | 2011-09-12 23:38:44 +0000 | [diff] [blame] | 95 | { |
Johnny Chen | 10b12b3 | 2011-09-16 21:41:42 +0000 | [diff] [blame] | 96 | DumpWithLevel(s, level); |
Johnny Chen | 34bbf85 | 2011-09-12 23:38:44 +0000 | [diff] [blame] | 97 | return; |
| 98 | } |
| 99 | |
| 100 | void |
Johnny Chen | ecd4feb | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 101 | Watchpoint::Dump(Stream *s) const |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 102 | { |
Johnny Chen | 10b12b3 | 2011-09-16 21:41:42 +0000 | [diff] [blame] | 103 | DumpWithLevel(s, lldb::eDescriptionLevelBrief); |
| 104 | } |
| 105 | |
| 106 | void |
Johnny Chen | ecd4feb | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 107 | Watchpoint::DumpWithLevel(Stream *s, lldb::DescriptionLevel description_level) const |
Johnny Chen | 10b12b3 | 2011-09-16 21:41:42 +0000 | [diff] [blame] | 108 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 109 | if (s == NULL) |
| 110 | return; |
| 111 | |
Johnny Chen | 10b12b3 | 2011-09-16 21:41:42 +0000 | [diff] [blame] | 112 | assert(description_level >= lldb::eDescriptionLevelBrief && |
| 113 | description_level <= lldb::eDescriptionLevelVerbose); |
| 114 | |
Johnny Chen | ecd4feb | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 115 | s->Printf("Watchpoint %u: addr = 0x%8.8llx size = %zu state = %s type = %s%s", |
Johnny Chen | 10b12b3 | 2011-09-16 21:41:42 +0000 | [diff] [blame] | 116 | GetID(), |
| 117 | (uint64_t)m_addr, |
| 118 | m_byte_size, |
| 119 | m_enabled ? "enabled" : "disabled", |
| 120 | m_watch_read ? "r" : "", |
| 121 | m_watch_write ? "w" : ""); |
| 122 | |
Johnny Chen | 712a628 | 2011-10-17 18:58:00 +0000 | [diff] [blame] | 123 | if (description_level >= lldb::eDescriptionLevelFull) { |
| 124 | if (m_decl_str.c_str()) |
| 125 | s->Printf("\n declare @ '%s'", m_decl_str.c_str()); |
| 126 | if (GetConditionText()) |
| 127 | s->Printf("\n condition = '%s'", GetConditionText()); |
| 128 | } |
Johnny Chen | 10b12b3 | 2011-09-16 21:41:42 +0000 | [diff] [blame] | 129 | |
| 130 | if (description_level >= lldb::eDescriptionLevelVerbose) |
Johnny Chen | b238517 | 2011-10-06 20:27:05 +0000 | [diff] [blame] | 131 | if (m_callback) |
| 132 | s->Printf("\n hw_index = %i hit_count = %-4u ignore_count = %-4u callback = %8p baton = %8p", |
| 133 | GetHardwareIndex(), |
| 134 | GetHitCount(), |
| 135 | GetIgnoreCount(), |
| 136 | m_callback, |
| 137 | m_callback_baton); |
| 138 | else |
| 139 | s->Printf("\n hw_index = %i hit_count = %-4u ignore_count = %-4u", |
| 140 | GetHardwareIndex(), |
| 141 | GetHitCount(), |
| 142 | GetIgnoreCount()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | bool |
Johnny Chen | ecd4feb | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 146 | Watchpoint::IsEnabled() const |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 147 | { |
| 148 | return m_enabled; |
| 149 | } |
| 150 | |
| 151 | void |
Johnny Chen | ecd4feb | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 152 | Watchpoint::SetEnabled(bool enabled) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 153 | { |
| 154 | if (!enabled) |
| 155 | SetHardwareIndex(LLDB_INVALID_INDEX32); |
| 156 | m_enabled = enabled; |
| 157 | } |
| 158 | |
| 159 | void |
Johnny Chen | ecd4feb | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 160 | Watchpoint::SetWatchpointType (uint32_t type) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 161 | { |
| 162 | m_watch_read = (type & LLDB_WATCH_TYPE_READ) != 0; |
| 163 | m_watch_write = (type & LLDB_WATCH_TYPE_WRITE) != 0; |
| 164 | } |
| 165 | |
| 166 | bool |
Johnny Chen | ecd4feb | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 167 | Watchpoint::WatchpointRead () const |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 168 | { |
| 169 | return m_watch_read != 0; |
| 170 | } |
| 171 | bool |
Johnny Chen | ecd4feb | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 172 | Watchpoint::WatchpointWrite () const |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 173 | { |
| 174 | return m_watch_write != 0; |
| 175 | } |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 176 | uint32_t |
Johnny Chen | ecd4feb | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 177 | Watchpoint::GetIgnoreCount () const |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 178 | { |
| 179 | return m_ignore_count; |
| 180 | } |
| 181 | |
| 182 | void |
Johnny Chen | ecd4feb | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 183 | Watchpoint::SetIgnoreCount (uint32_t n) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 184 | { |
| 185 | m_ignore_count = n; |
| 186 | } |
Johnny Chen | 712a628 | 2011-10-17 18:58:00 +0000 | [diff] [blame] | 187 | |
| 188 | bool |
| 189 | Watchpoint::InvokeCallback (StoppointCallbackContext *context) |
| 190 | { |
| 191 | if (m_callback && context->is_synchronous) |
| 192 | { |
| 193 | uint32_t access = 0; |
| 194 | if (m_watch_was_read) |
| 195 | access |= LLDB_WATCH_TYPE_READ; |
| 196 | if (m_watch_was_written) |
| 197 | access |= LLDB_WATCH_TYPE_WRITE; |
| 198 | return m_callback(m_callback_baton, context, GetID(), access); |
| 199 | } |
| 200 | else |
| 201 | return true; |
| 202 | } |
| 203 | |
| 204 | void |
| 205 | Watchpoint::SetCondition (const char *condition) |
| 206 | { |
| 207 | if (condition == NULL || condition[0] == '\0') |
| 208 | { |
| 209 | if (m_condition_ap.get()) |
| 210 | m_condition_ap.reset(); |
| 211 | } |
| 212 | else |
| 213 | { |
| 214 | // Pass NULL for expr_prefix (no translation-unit level definitions). |
Sean Callanan | 5b658cc | 2011-11-07 23:35:40 +0000 | [diff] [blame^] | 215 | m_condition_ap.reset(new ClangUserExpression (condition, NULL, lldb::eLanguageTypeUnknown)); |
Johnny Chen | 712a628 | 2011-10-17 18:58:00 +0000 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
| 219 | const char * |
| 220 | Watchpoint::GetConditionText () const |
| 221 | { |
| 222 | if (m_condition_ap.get()) |
| 223 | return m_condition_ap->GetUserText(); |
| 224 | else |
| 225 | return NULL; |
| 226 | } |
| 227 | |