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" |
Jim Ingham | c7dccb7 | 2012-05-02 00:30:53 +0000 | [diff] [blame] | 21 | #include "lldb/Expression/ClangUserExpression.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 | a4d6bc9 | 2012-02-25 06:44:30 +0000 | [diff] [blame] | 27 | StoppointLocation (0, addr, size, hardware), |
Johnny Chen | 5d04346 | 2011-09-26 22:40:50 +0000 | [diff] [blame] | 28 | m_target(NULL), |
Johnny Chen | a4d6bc9 | 2012-02-25 06:44:30 +0000 | [diff] [blame] | 29 | m_enabled(false), |
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), |
Johnny Chen | ed456eb | 2011-10-14 19:15:48 +0000 | [diff] [blame] | 36 | m_decl_str(), |
Johnny Chen | a4d6bc9 | 2012-02-25 06:44:30 +0000 | [diff] [blame] | 37 | m_watch_spec_str(), |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame^] | 38 | m_error(), |
| 39 | m_options () |
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 | |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame^] | 47 | // This function is used when "baton" doesn't need to be freed |
| 48 | void |
| 49 | Watchpoint::SetCallback (WatchpointHitCallback callback, void *baton, bool is_synchronous) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 50 | { |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame^] | 51 | // The default "Baton" class will keep a copy of "baton" and won't free |
| 52 | // or delete it when it goes goes out of scope. |
| 53 | m_options.SetCallback(callback, BatonSP (new Baton(baton)), is_synchronous); |
| 54 | |
| 55 | //SendWatchpointChangedEvent (eWatchpointEventTypeCommandChanged); |
| 56 | } |
| 57 | |
| 58 | // This function is used when a baton needs to be freed and therefore is |
| 59 | // contained in a "Baton" subclass. |
| 60 | void |
| 61 | Watchpoint::SetCallback (WatchpointHitCallback callback, const BatonSP &callback_baton_sp, bool is_synchronous) |
| 62 | { |
| 63 | m_options.SetCallback(callback, callback_baton_sp, is_synchronous); |
| 64 | } |
| 65 | |
| 66 | void |
| 67 | Watchpoint::ClearCallback () |
| 68 | { |
| 69 | m_options.ClearCallback (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Johnny Chen | de6bd24 | 2011-09-16 21:41:42 +0000 | [diff] [blame] | 72 | void |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 73 | Watchpoint::SetDeclInfo (std::string &str) |
Johnny Chen | de6bd24 | 2011-09-16 21:41:42 +0000 | [diff] [blame] | 74 | { |
| 75 | m_decl_str = str; |
| 76 | return; |
| 77 | } |
| 78 | |
Johnny Chen | a4d6bc9 | 2012-02-25 06:44:30 +0000 | [diff] [blame] | 79 | void |
| 80 | Watchpoint::SetWatchSpec (std::string &str) |
| 81 | { |
| 82 | m_watch_spec_str = str; |
| 83 | return; |
| 84 | } |
| 85 | |
Johnny Chen | fab7a91 | 2012-01-23 23:03:59 +0000 | [diff] [blame] | 86 | // Override default impl of StoppointLocation::IsHardware() since m_is_hardware |
| 87 | // member field is more accurate. |
Johnny Chen | f04ee93 | 2011-09-22 18:04:58 +0000 | [diff] [blame] | 88 | bool |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 89 | Watchpoint::IsHardware () const |
Johnny Chen | f04ee93 | 2011-09-22 18:04:58 +0000 | [diff] [blame] | 90 | { |
| 91 | return m_is_hardware; |
| 92 | } |
| 93 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 94 | // RETURNS - true if we should stop at this breakpoint, false if we |
| 95 | // should continue. |
| 96 | |
| 97 | bool |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 98 | Watchpoint::ShouldStop (StoppointCallbackContext *context) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 99 | { |
Johnny Chen | fab7a91 | 2012-01-23 23:03:59 +0000 | [diff] [blame] | 100 | IncrementHitCount(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 101 | |
Johnny Chen | f04ee93 | 2011-09-22 18:04:58 +0000 | [diff] [blame] | 102 | if (!IsEnabled()) |
| 103 | return false; |
Johnny Chen | fd158f4 | 2011-09-21 22:47:15 +0000 | [diff] [blame] | 104 | |
Johnny Chen | fab7a91 | 2012-01-23 23:03:59 +0000 | [diff] [blame] | 105 | if (GetHitCount() <= GetIgnoreCount()) |
Johnny Chen | f04ee93 | 2011-09-22 18:04:58 +0000 | [diff] [blame] | 106 | return false; |
| 107 | |
Johnny Chen | 16dcf71 | 2011-10-17 18:58:00 +0000 | [diff] [blame] | 108 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 112 | Watchpoint::GetDescription (Stream *s, lldb::DescriptionLevel level) |
Johnny Chen | 887062a | 2011-09-12 23:38:44 +0000 | [diff] [blame] | 113 | { |
Johnny Chen | de6bd24 | 2011-09-16 21:41:42 +0000 | [diff] [blame] | 114 | DumpWithLevel(s, level); |
Johnny Chen | 887062a | 2011-09-12 23:38:44 +0000 | [diff] [blame] | 115 | return; |
| 116 | } |
| 117 | |
| 118 | void |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 119 | Watchpoint::Dump(Stream *s) const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 120 | { |
Johnny Chen | de6bd24 | 2011-09-16 21:41:42 +0000 | [diff] [blame] | 121 | DumpWithLevel(s, lldb::eDescriptionLevelBrief); |
| 122 | } |
| 123 | |
| 124 | void |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 125 | Watchpoint::DumpWithLevel(Stream *s, lldb::DescriptionLevel description_level) const |
Johnny Chen | de6bd24 | 2011-09-16 21:41:42 +0000 | [diff] [blame] | 126 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 127 | if (s == NULL) |
| 128 | return; |
| 129 | |
Johnny Chen | de6bd24 | 2011-09-16 21:41:42 +0000 | [diff] [blame] | 130 | assert(description_level >= lldb::eDescriptionLevelBrief && |
| 131 | description_level <= lldb::eDescriptionLevelVerbose); |
| 132 | |
Greg Clayton | c91d804 | 2011-12-03 00:46:21 +0000 | [diff] [blame] | 133 | 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] | 134 | GetID(), |
Johnny Chen | a5cde26 | 2012-01-24 00:11:02 +0000 | [diff] [blame] | 135 | GetLoadAddress(), |
Johnny Chen | de6bd24 | 2011-09-16 21:41:42 +0000 | [diff] [blame] | 136 | m_byte_size, |
Johnny Chen | a4d6bc9 | 2012-02-25 06:44:30 +0000 | [diff] [blame] | 137 | IsEnabled() ? "enabled" : "disabled", |
Johnny Chen | de6bd24 | 2011-09-16 21:41:42 +0000 | [diff] [blame] | 138 | m_watch_read ? "r" : "", |
| 139 | m_watch_write ? "w" : ""); |
| 140 | |
Johnny Chen | 16dcf71 | 2011-10-17 18:58:00 +0000 | [diff] [blame] | 141 | if (description_level >= lldb::eDescriptionLevelFull) { |
Johnny Chen | dedb67a | 2012-01-30 21:46:17 +0000 | [diff] [blame] | 142 | if (!m_decl_str.empty()) |
Johnny Chen | 16dcf71 | 2011-10-17 18:58:00 +0000 | [diff] [blame] | 143 | s->Printf("\n declare @ '%s'", m_decl_str.c_str()); |
Johnny Chen | a4d6bc9 | 2012-02-25 06:44:30 +0000 | [diff] [blame] | 144 | if (!m_watch_spec_str.empty()) |
| 145 | s->Printf("\n static watchpoint spec = '%s'", m_watch_spec_str.c_str()); |
Johnny Chen | 16dcf71 | 2011-10-17 18:58:00 +0000 | [diff] [blame] | 146 | if (GetConditionText()) |
| 147 | s->Printf("\n condition = '%s'", GetConditionText()); |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame^] | 148 | m_options.GetCallbackDescription(s, description_level); |
Johnny Chen | 16dcf71 | 2011-10-17 18:58:00 +0000 | [diff] [blame] | 149 | } |
Johnny Chen | de6bd24 | 2011-09-16 21:41:42 +0000 | [diff] [blame] | 150 | |
| 151 | if (description_level >= lldb::eDescriptionLevelVerbose) |
Jason Molenda | 7e1f45f | 2012-02-23 22:32:13 +0000 | [diff] [blame] | 152 | { |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame^] | 153 | s->Printf("\n hw_index = %i hit_count = %-4u ignore_count = %-4u", |
| 154 | GetHardwareIndex(), |
| 155 | GetHitCount(), |
| 156 | GetIgnoreCount()); |
Jason Molenda | 7e1f45f | 2012-02-23 22:32:13 +0000 | [diff] [blame] | 157 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | bool |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 161 | Watchpoint::IsEnabled() const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 162 | { |
| 163 | return m_enabled; |
| 164 | } |
| 165 | |
| 166 | void |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 167 | Watchpoint::SetEnabled(bool enabled) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 168 | { |
| 169 | if (!enabled) |
| 170 | SetHardwareIndex(LLDB_INVALID_INDEX32); |
| 171 | m_enabled = enabled; |
| 172 | } |
| 173 | |
| 174 | void |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 175 | Watchpoint::SetWatchpointType (uint32_t type) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 176 | { |
| 177 | m_watch_read = (type & LLDB_WATCH_TYPE_READ) != 0; |
| 178 | m_watch_write = (type & LLDB_WATCH_TYPE_WRITE) != 0; |
| 179 | } |
| 180 | |
| 181 | bool |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 182 | Watchpoint::WatchpointRead () const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 183 | { |
| 184 | return m_watch_read != 0; |
| 185 | } |
| 186 | bool |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 187 | Watchpoint::WatchpointWrite () const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 188 | { |
| 189 | return m_watch_write != 0; |
| 190 | } |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 191 | uint32_t |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 192 | Watchpoint::GetIgnoreCount () const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 193 | { |
| 194 | return m_ignore_count; |
| 195 | } |
| 196 | |
| 197 | void |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 198 | Watchpoint::SetIgnoreCount (uint32_t n) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 199 | { |
| 200 | m_ignore_count = n; |
| 201 | } |
Johnny Chen | 16dcf71 | 2011-10-17 18:58:00 +0000 | [diff] [blame] | 202 | |
| 203 | bool |
| 204 | Watchpoint::InvokeCallback (StoppointCallbackContext *context) |
| 205 | { |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame^] | 206 | return m_options.InvokeCallback (context, GetID()); |
Johnny Chen | 16dcf71 | 2011-10-17 18:58:00 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | void |
| 210 | Watchpoint::SetCondition (const char *condition) |
| 211 | { |
| 212 | if (condition == NULL || condition[0] == '\0') |
| 213 | { |
| 214 | if (m_condition_ap.get()) |
| 215 | m_condition_ap.reset(); |
| 216 | } |
| 217 | else |
| 218 | { |
| 219 | // Pass NULL for expr_prefix (no translation-unit level definitions). |
Sean Callanan | 20bb3aa | 2011-12-21 22:22:58 +0000 | [diff] [blame] | 220 | m_condition_ap.reset(new ClangUserExpression (condition, NULL, lldb::eLanguageTypeUnknown, ClangUserExpression::eResultTypeAny)); |
Johnny Chen | 16dcf71 | 2011-10-17 18:58:00 +0000 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
| 224 | const char * |
| 225 | Watchpoint::GetConditionText () const |
| 226 | { |
| 227 | if (m_condition_ap.get()) |
| 228 | return m_condition_ap->GetUserText(); |
| 229 | else |
| 230 | return NULL; |
| 231 | } |
| 232 | |