1 //===-- SBWatchpoint.cpp --------------------------------*- C++ -*-===// 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 10 #include "lldb/API/SBWatchpoint.h" 11 #include "lldb/API/SBDefines.h" 12 #include "lldb/API/SBAddress.h" 13 #include "lldb/API/SBDebugger.h" 14 #include "lldb/API/SBEvent.h" 15 #include "lldb/API/SBStream.h" 16 17 #include "lldb/lldb-types.h" 18 #include "lldb/lldb-defines.h" 19 #include "lldb/Breakpoint/Watchpoint.h" 20 #include "lldb/Breakpoint/WatchpointList.h" 21 #include "lldb/Core/Log.h" 22 #include "lldb/Core/Stream.h" 23 #include "lldb/Core/StreamFile.h" 24 #include "lldb/Target/Target.h" 25 26 using namespace lldb; 27 using namespace lldb_private; 28 29 30 SBWatchpoint::SBWatchpoint () : 31 m_opaque_sp () 32 { 33 } 34 35 SBWatchpoint::SBWatchpoint (const lldb::WatchpointSP &wp_sp) : 36 m_opaque_sp (wp_sp) 37 { 38 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 39 40 if (log) 41 { 42 SBStream sstr; 43 GetDescription (sstr, lldb::eDescriptionLevelBrief); 44 log->Printf ("SBWatchpoint::SBWatchpoint (const lldb::WatchpointSP &wp_sp" 45 "=%p) => this.sp = %p (%s)", 46 static_cast<void*>(wp_sp.get()), 47 static_cast<void*>(m_opaque_sp.get()), sstr.GetData()); 48 } 49 } 50 51 SBWatchpoint::SBWatchpoint(const SBWatchpoint &rhs) : 52 m_opaque_sp (rhs.m_opaque_sp) 53 { 54 } 55 56 const SBWatchpoint & 57 SBWatchpoint::operator = (const SBWatchpoint &rhs) 58 { 59 if (this != &rhs) 60 m_opaque_sp = rhs.m_opaque_sp; 61 return *this; 62 } 63 64 65 SBWatchpoint::~SBWatchpoint () 66 { 67 } 68 69 watch_id_t 70 SBWatchpoint::GetID () 71 { 72 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 73 74 watch_id_t watch_id = LLDB_INVALID_WATCH_ID; 75 lldb::WatchpointSP watchpoint_sp(GetSP()); 76 if (watchpoint_sp) 77 watch_id = watchpoint_sp->GetID(); 78 79 if (log) 80 { 81 if (watch_id == LLDB_INVALID_WATCH_ID) 82 log->Printf ("SBWatchpoint(%p)::GetID () => LLDB_INVALID_WATCH_ID", 83 static_cast<void*>(watchpoint_sp.get())); 84 else 85 log->Printf ("SBWatchpoint(%p)::GetID () => %u", 86 static_cast<void*>(watchpoint_sp.get()), watch_id); 87 } 88 89 return watch_id; 90 } 91 92 bool 93 SBWatchpoint::IsValid() const 94 { 95 return (bool) m_opaque_sp; 96 } 97 98 SBError 99 SBWatchpoint::GetError () 100 { 101 SBError sb_error; 102 lldb::WatchpointSP watchpoint_sp(GetSP()); 103 if (watchpoint_sp) 104 { 105 sb_error.SetError(watchpoint_sp->GetError()); 106 } 107 return sb_error; 108 } 109 110 int32_t 111 SBWatchpoint::GetHardwareIndex () 112 { 113 int32_t hw_index = -1; 114 115 lldb::WatchpointSP watchpoint_sp(GetSP()); 116 if (watchpoint_sp) 117 { 118 std::lock_guard<std::recursive_mutex> guard(watchpoint_sp->GetTarget().GetAPIMutex()); 119 hw_index = watchpoint_sp->GetHardwareIndex(); 120 } 121 122 return hw_index; 123 } 124 125 addr_t 126 SBWatchpoint::GetWatchAddress () 127 { 128 addr_t ret_addr = LLDB_INVALID_ADDRESS; 129 130 lldb::WatchpointSP watchpoint_sp(GetSP()); 131 if (watchpoint_sp) 132 { 133 std::lock_guard<std::recursive_mutex> guard(watchpoint_sp->GetTarget().GetAPIMutex()); 134 ret_addr = watchpoint_sp->GetLoadAddress(); 135 } 136 137 return ret_addr; 138 } 139 140 size_t 141 SBWatchpoint::GetWatchSize () 142 { 143 size_t watch_size = 0; 144 145 lldb::WatchpointSP watchpoint_sp(GetSP()); 146 if (watchpoint_sp) 147 { 148 std::lock_guard<std::recursive_mutex> guard(watchpoint_sp->GetTarget().GetAPIMutex()); 149 watch_size = watchpoint_sp->GetByteSize(); 150 } 151 152 return watch_size; 153 } 154 155 void 156 SBWatchpoint::SetEnabled (bool enabled) 157 { 158 lldb::WatchpointSP watchpoint_sp(GetSP()); 159 if (watchpoint_sp) 160 { 161 std::lock_guard<std::recursive_mutex> guard(watchpoint_sp->GetTarget().GetAPIMutex()); 162 watchpoint_sp->GetTarget().DisableWatchpointByID(watchpoint_sp->GetID()); 163 } 164 } 165 166 bool 167 SBWatchpoint::IsEnabled () 168 { 169 lldb::WatchpointSP watchpoint_sp(GetSP()); 170 if (watchpoint_sp) 171 { 172 std::lock_guard<std::recursive_mutex> guard(watchpoint_sp->GetTarget().GetAPIMutex()); 173 return watchpoint_sp->IsEnabled(); 174 } 175 else 176 return false; 177 } 178 179 uint32_t 180 SBWatchpoint::GetHitCount () 181 { 182 uint32_t count = 0; 183 lldb::WatchpointSP watchpoint_sp(GetSP()); 184 if (watchpoint_sp) 185 { 186 std::lock_guard<std::recursive_mutex> guard(watchpoint_sp->GetTarget().GetAPIMutex()); 187 count = watchpoint_sp->GetHitCount(); 188 } 189 190 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 191 if (log) 192 log->Printf ("SBWatchpoint(%p)::GetHitCount () => %u", 193 static_cast<void*>(watchpoint_sp.get()), count); 194 195 return count; 196 } 197 198 uint32_t 199 SBWatchpoint::GetIgnoreCount () 200 { 201 lldb::WatchpointSP watchpoint_sp(GetSP()); 202 if (watchpoint_sp) 203 { 204 std::lock_guard<std::recursive_mutex> guard(watchpoint_sp->GetTarget().GetAPIMutex()); 205 return watchpoint_sp->GetIgnoreCount(); 206 } 207 else 208 return 0; 209 } 210 211 void 212 SBWatchpoint::SetIgnoreCount (uint32_t n) 213 { 214 lldb::WatchpointSP watchpoint_sp(GetSP()); 215 if (watchpoint_sp) 216 { 217 std::lock_guard<std::recursive_mutex> guard(watchpoint_sp->GetTarget().GetAPIMutex()); 218 watchpoint_sp->SetIgnoreCount (n); 219 } 220 } 221 222 const char * 223 SBWatchpoint::GetCondition () 224 { 225 lldb::WatchpointSP watchpoint_sp(GetSP()); 226 if (watchpoint_sp) 227 { 228 std::lock_guard<std::recursive_mutex> guard(watchpoint_sp->GetTarget().GetAPIMutex()); 229 return watchpoint_sp->GetConditionText (); 230 } 231 return NULL; 232 } 233 234 void 235 SBWatchpoint::SetCondition (const char *condition) 236 { 237 lldb::WatchpointSP watchpoint_sp(GetSP()); 238 if (watchpoint_sp) 239 { 240 std::lock_guard<std::recursive_mutex> guard(watchpoint_sp->GetTarget().GetAPIMutex()); 241 watchpoint_sp->SetCondition (condition); 242 } 243 } 244 245 bool 246 SBWatchpoint::GetDescription (SBStream &description, DescriptionLevel level) 247 { 248 Stream &strm = description.ref(); 249 250 lldb::WatchpointSP watchpoint_sp(GetSP()); 251 if (watchpoint_sp) 252 { 253 std::lock_guard<std::recursive_mutex> guard(watchpoint_sp->GetTarget().GetAPIMutex()); 254 watchpoint_sp->GetDescription (&strm, level); 255 strm.EOL(); 256 } 257 else 258 strm.PutCString ("No value"); 259 260 return true; 261 } 262 263 void 264 SBWatchpoint::Clear () 265 { 266 m_opaque_sp.reset(); 267 } 268 269 lldb::WatchpointSP 270 SBWatchpoint::GetSP () const 271 { 272 return m_opaque_sp; 273 } 274 275 void 276 SBWatchpoint::SetSP (const lldb::WatchpointSP &sp) 277 { 278 m_opaque_sp = sp; 279 } 280 281 bool 282 SBWatchpoint::EventIsWatchpointEvent (const lldb::SBEvent &event) 283 { 284 return Watchpoint::WatchpointEventData::GetEventDataFromEvent(event.get()) != NULL; 285 286 } 287 288 WatchpointEventType 289 SBWatchpoint::GetWatchpointEventTypeFromEvent (const SBEvent& event) 290 { 291 if (event.IsValid()) 292 return Watchpoint::WatchpointEventData::GetWatchpointEventTypeFromEvent (event.GetSP()); 293 return eWatchpointEventTypeInvalidType; 294 } 295 296 SBWatchpoint 297 SBWatchpoint::GetWatchpointFromEvent (const lldb::SBEvent& event) 298 { 299 SBWatchpoint sb_watchpoint; 300 if (event.IsValid()) 301 sb_watchpoint.m_opaque_sp = Watchpoint::WatchpointEventData::GetWatchpointFromEvent (event.GetSP()); 302 return sb_watchpoint; 303 } 304