Eugene Zelenko | 8f30a65 | 2015-10-23 18:39:37 +0000 | [diff] [blame] | 1 | //===-- StopInfo.cpp --------------------------------------------*- C++ -*-===// |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +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 | |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 10 | // C Includes |
| 11 | // C++ Includes |
| 12 | #include <string> |
| 13 | |
| 14 | // Other libraries and framework includes |
| 15 | // Project includes |
Greg Clayton | 4e78f60 | 2010-11-18 18:52:36 +0000 | [diff] [blame] | 16 | #include "lldb/Breakpoint/Breakpoint.h" |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 17 | #include "lldb/Breakpoint/BreakpointLocation.h" |
| 18 | #include "lldb/Breakpoint/StoppointCallbackContext.h" |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 19 | #include "lldb/Breakpoint/Watchpoint.h" |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 20 | #include "lldb/Core/Debugger.h" |
Zachary Turner | a78bd7f | 2015-03-03 23:11:11 +0000 | [diff] [blame] | 21 | #include "lldb/Core/ValueObject.h" |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 22 | #include "lldb/Expression/UserExpression.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 23 | #include "lldb/Target/Process.h" |
| 24 | #include "lldb/Target/StopInfo.h" |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 25 | #include "lldb/Target/Target.h" |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 26 | #include "lldb/Target/Thread.h" |
| 27 | #include "lldb/Target/ThreadPlan.h" |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 28 | #include "lldb/Target/UnixSignals.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 29 | #include "lldb/Utility/Log.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 30 | #include "lldb/Utility/StreamString.h" |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 31 | |
| 32 | using namespace lldb; |
| 33 | using namespace lldb_private; |
| 34 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 35 | StopInfo::StopInfo(Thread &thread, uint64_t value) |
| 36 | : m_thread_wp(thread.shared_from_this()), |
| 37 | m_stop_id(thread.GetProcess()->GetStopID()), |
| 38 | m_resume_id(thread.GetProcess()->GetResumeID()), m_value(value), |
| 39 | m_description(), m_override_should_notify(eLazyBoolCalculate), |
| 40 | m_override_should_stop(eLazyBoolCalculate), m_extended_info() {} |
| 41 | |
| 42 | bool StopInfo::IsValid() const { |
| 43 | ThreadSP thread_sp(m_thread_wp.lock()); |
| 44 | if (thread_sp) |
| 45 | return thread_sp->GetProcess()->GetStopID() == m_stop_id; |
| 46 | return false; |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 49 | void StopInfo::MakeStopInfoValid() { |
| 50 | ThreadSP thread_sp(m_thread_wp.lock()); |
| 51 | if (thread_sp) { |
| 52 | m_stop_id = thread_sp->GetProcess()->GetStopID(); |
| 53 | m_resume_id = thread_sp->GetProcess()->GetResumeID(); |
| 54 | } |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 57 | bool StopInfo::HasTargetRunSinceMe() { |
| 58 | ThreadSP thread_sp(m_thread_wp.lock()); |
| 59 | |
| 60 | if (thread_sp) { |
| 61 | lldb::StateType ret_type = thread_sp->GetProcess()->GetPrivateState(); |
| 62 | if (ret_type == eStateRunning) { |
| 63 | return true; |
| 64 | } else if (ret_type == eStateStopped) { |
| 65 | // This is a little tricky. We want to count "run and stopped again |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame^] | 66 | // before you could ask this question as a "TRUE" answer to |
| 67 | // HasTargetRunSinceMe. But we don't want to include any running of the |
| 68 | // target done for expressions. So we track both resumes, and resumes |
| 69 | // caused by expressions, and check if there are any resumes |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 70 | // NOT caused |
| 71 | // by expressions. |
| 72 | |
| 73 | uint32_t curr_resume_id = thread_sp->GetProcess()->GetResumeID(); |
| 74 | uint32_t last_user_expression_id = |
| 75 | thread_sp->GetProcess()->GetLastUserExpressionResumeID(); |
| 76 | if (curr_resume_id == m_resume_id) { |
| 77 | return false; |
| 78 | } else if (curr_resume_id > last_user_expression_id) { |
| 79 | return true; |
| 80 | } |
Greg Clayton | 46c2b6e | 2013-04-29 23:30:46 +0000 | [diff] [blame] | 81 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 82 | } |
| 83 | return false; |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 86 | //---------------------------------------------------------------------- |
| 87 | // StopInfoBreakpoint |
| 88 | //---------------------------------------------------------------------- |
| 89 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 90 | namespace lldb_private { |
| 91 | class StopInfoBreakpoint : public StopInfo { |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 92 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 93 | StopInfoBreakpoint(Thread &thread, break_id_t break_id) |
| 94 | : StopInfo(thread, break_id), m_should_stop(false), |
| 95 | m_should_stop_is_valid(false), m_should_perform_action(true), |
| 96 | m_address(LLDB_INVALID_ADDRESS), m_break_id(LLDB_INVALID_BREAK_ID), |
| 97 | m_was_one_shot(false) { |
| 98 | StoreBPInfo(); |
| 99 | } |
| 100 | |
| 101 | StopInfoBreakpoint(Thread &thread, break_id_t break_id, bool should_stop) |
| 102 | : StopInfo(thread, break_id), m_should_stop(should_stop), |
| 103 | m_should_stop_is_valid(true), m_should_perform_action(true), |
| 104 | m_address(LLDB_INVALID_ADDRESS), m_break_id(LLDB_INVALID_BREAK_ID), |
| 105 | m_was_one_shot(false) { |
| 106 | StoreBPInfo(); |
| 107 | } |
| 108 | |
| 109 | ~StopInfoBreakpoint() override = default; |
| 110 | |
| 111 | void StoreBPInfo() { |
| 112 | ThreadSP thread_sp(m_thread_wp.lock()); |
| 113 | if (thread_sp) { |
| 114 | BreakpointSiteSP bp_site_sp( |
| 115 | thread_sp->GetProcess()->GetBreakpointSiteList().FindByID(m_value)); |
| 116 | if (bp_site_sp) { |
| 117 | if (bp_site_sp->GetNumberOfOwners() == 1) { |
| 118 | BreakpointLocationSP bp_loc_sp = bp_site_sp->GetOwnerAtIndex(0); |
| 119 | if (bp_loc_sp) { |
| 120 | m_break_id = bp_loc_sp->GetBreakpoint().GetID(); |
| 121 | m_was_one_shot = bp_loc_sp->GetBreakpoint().IsOneShot(); |
| 122 | } |
| 123 | } |
| 124 | m_address = bp_site_sp->GetLoadAddress(); |
| 125 | } |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 126 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 127 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 128 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 129 | bool IsValidForOperatingSystemThread(Thread &thread) override { |
| 130 | ProcessSP process_sp(thread.GetProcess()); |
| 131 | if (process_sp) { |
| 132 | BreakpointSiteSP bp_site_sp( |
| 133 | process_sp->GetBreakpointSiteList().FindByID(m_value)); |
| 134 | if (bp_site_sp) |
| 135 | return bp_site_sp->ValidForThisThread(&thread); |
Jim Ingham | ca36cd1 | 2012-10-05 19:16:31 +0000 | [diff] [blame] | 136 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 137 | return false; |
| 138 | } |
Jim Ingham | ca36cd1 | 2012-10-05 19:16:31 +0000 | [diff] [blame] | 139 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 140 | StopReason GetStopReason() const override { return eStopReasonBreakpoint; } |
Eugene Zelenko | 8f30a65 | 2015-10-23 18:39:37 +0000 | [diff] [blame] | 141 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 142 | bool ShouldStopSynchronous(Event *event_ptr) override { |
| 143 | ThreadSP thread_sp(m_thread_wp.lock()); |
| 144 | if (thread_sp) { |
| 145 | if (!m_should_stop_is_valid) { |
| 146 | // Only check once if we should stop at a breakpoint |
| 147 | BreakpointSiteSP bp_site_sp( |
| 148 | thread_sp->GetProcess()->GetBreakpointSiteList().FindByID(m_value)); |
| 149 | if (bp_site_sp) { |
| 150 | ExecutionContext exe_ctx(thread_sp->GetStackFrameAtIndex(0)); |
| 151 | StoppointCallbackContext context(event_ptr, exe_ctx, true); |
| 152 | bp_site_sp->BumpHitCounts(); |
| 153 | m_should_stop = bp_site_sp->ShouldStop(&context); |
| 154 | } else { |
| 155 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 156 | |
| 157 | if (log) |
| 158 | log->Printf( |
| 159 | "Process::%s could not find breakpoint site id: %" PRId64 "...", |
| 160 | __FUNCTION__, m_value); |
| 161 | |
| 162 | m_should_stop = true; |
| 163 | } |
| 164 | m_should_stop_is_valid = true; |
| 165 | } |
| 166 | return m_should_stop; |
| 167 | } |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | bool DoShouldNotify(Event *event_ptr) override { |
| 172 | ThreadSP thread_sp(m_thread_wp.lock()); |
| 173 | if (thread_sp) { |
| 174 | BreakpointSiteSP bp_site_sp( |
| 175 | thread_sp->GetProcess()->GetBreakpointSiteList().FindByID(m_value)); |
| 176 | if (bp_site_sp) { |
| 177 | bool all_internal = true; |
| 178 | |
| 179 | for (uint32_t i = 0; i < bp_site_sp->GetNumberOfOwners(); i++) { |
| 180 | if (!bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint().IsInternal()) { |
| 181 | all_internal = false; |
| 182 | break; |
| 183 | } |
| 184 | } |
| 185 | return !all_internal; |
| 186 | } |
| 187 | } |
| 188 | return true; |
| 189 | } |
| 190 | |
| 191 | const char *GetDescription() override { |
| 192 | if (m_description.empty()) { |
| 193 | ThreadSP thread_sp(m_thread_wp.lock()); |
| 194 | if (thread_sp) { |
| 195 | BreakpointSiteSP bp_site_sp( |
| 196 | thread_sp->GetProcess()->GetBreakpointSiteList().FindByID(m_value)); |
| 197 | if (bp_site_sp) { |
| 198 | StreamString strm; |
| 199 | // If we have just hit an internal breakpoint, and it has a kind |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame^] | 200 | // description, print that instead of the full breakpoint printing: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 201 | if (bp_site_sp->IsInternal()) { |
| 202 | size_t num_owners = bp_site_sp->GetNumberOfOwners(); |
| 203 | for (size_t idx = 0; idx < num_owners; idx++) { |
| 204 | const char *kind = bp_site_sp->GetOwnerAtIndex(idx) |
| 205 | ->GetBreakpoint() |
| 206 | .GetBreakpointKind(); |
| 207 | if (kind != nullptr) { |
| 208 | m_description.assign(kind); |
| 209 | return kind; |
| 210 | } |
Jim Ingham | ca36cd1 | 2012-10-05 19:16:31 +0000 | [diff] [blame] | 211 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 212 | } |
Jim Ingham | 36f3b36 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 213 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 214 | strm.Printf("breakpoint "); |
| 215 | bp_site_sp->GetDescription(&strm, eDescriptionLevelBrief); |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 216 | m_description = strm.GetString(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 217 | } else { |
| 218 | StreamString strm; |
| 219 | if (m_break_id != LLDB_INVALID_BREAK_ID) { |
| 220 | BreakpointSP break_sp = |
| 221 | thread_sp->GetProcess()->GetTarget().GetBreakpointByID( |
| 222 | m_break_id); |
| 223 | if (break_sp) { |
| 224 | if (break_sp->IsInternal()) { |
| 225 | const char *kind = break_sp->GetBreakpointKind(); |
| 226 | if (kind) |
| 227 | strm.Printf("internal %s breakpoint(%d).", kind, m_break_id); |
Greg Clayton | 46c2b6e | 2013-04-29 23:30:46 +0000 | [diff] [blame] | 228 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 229 | strm.Printf("internal breakpoint(%d).", m_break_id); |
| 230 | } else { |
| 231 | strm.Printf("breakpoint %d.", m_break_id); |
| 232 | } |
| 233 | } else { |
| 234 | if (m_was_one_shot) |
| 235 | strm.Printf("one-shot breakpoint %d", m_break_id); |
| 236 | else |
| 237 | strm.Printf("breakpoint %d which has been deleted.", |
| 238 | m_break_id); |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 239 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 240 | } else if (m_address == LLDB_INVALID_ADDRESS) |
| 241 | strm.Printf("breakpoint site %" PRIi64 |
| 242 | " which has been deleted - unknown address", |
| 243 | m_value); |
| 244 | else |
| 245 | strm.Printf("breakpoint site %" PRIi64 |
| 246 | " which has been deleted - was at 0x%" PRIx64, |
| 247 | m_value, m_address); |
| 248 | |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 249 | m_description = strm.GetString(); |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 250 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 251 | } |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 252 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 253 | return m_description.c_str(); |
| 254 | } |
Jim Ingham | f57b435 | 2012-11-10 02:08:14 +0000 | [diff] [blame] | 255 | |
| 256 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 257 | bool ShouldStop(Event *event_ptr) override { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame^] | 258 | // This just reports the work done by PerformAction or the synchronous |
| 259 | // stop. It should only ever get called after they have had a chance to |
| 260 | // run. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 261 | assert(m_should_stop_is_valid); |
| 262 | return m_should_stop; |
| 263 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 264 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 265 | void PerformAction(Event *event_ptr) override { |
| 266 | if (!m_should_perform_action) |
| 267 | return; |
| 268 | m_should_perform_action = false; |
Boris Ulasevich | 86aaa8a | 2017-02-15 11:42:47 +0000 | [diff] [blame] | 269 | bool internal_breakpoint = true; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 270 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 271 | ThreadSP thread_sp(m_thread_wp.lock()); |
Greg Clayton | 46c2b6e | 2013-04-29 23:30:46 +0000 | [diff] [blame] | 272 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 273 | if (thread_sp) { |
| 274 | Log *log = lldb_private::GetLogIfAnyCategoriesSet( |
| 275 | LIBLLDB_LOG_BREAKPOINTS | LIBLLDB_LOG_STEP); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 276 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 277 | if (!thread_sp->IsValid()) { |
| 278 | // This shouldn't ever happen, but just in case, don't do more harm. |
| 279 | if (log) { |
| 280 | log->Printf("PerformAction got called with an invalid thread."); |
| 281 | } |
| 282 | m_should_stop = true; |
| 283 | m_should_stop_is_valid = true; |
| 284 | return; |
| 285 | } |
| 286 | |
| 287 | BreakpointSiteSP bp_site_sp( |
| 288 | thread_sp->GetProcess()->GetBreakpointSiteList().FindByID(m_value)); |
| 289 | std::unordered_set<break_id_t> precondition_breakpoints; |
| 290 | |
| 291 | if (bp_site_sp) { |
| 292 | // Let's copy the owners list out of the site and store them in a local |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame^] | 293 | // list. That way if one of the breakpoint actions changes the site, |
| 294 | // then we won't be operating on a bad list. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 295 | BreakpointLocationCollection site_locations; |
| 296 | size_t num_owners = bp_site_sp->CopyOwnersList(site_locations); |
| 297 | |
| 298 | if (num_owners == 0) { |
| 299 | m_should_stop = true; |
| 300 | } else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame^] | 301 | // We go through each location, and test first its precondition - |
| 302 | // this overrides everything. Note, we only do this once per |
| 303 | // breakpoint - not once per location... Then check the condition. |
| 304 | // If the condition says to stop, then we run the callback for that |
| 305 | // location. If that callback says to stop as well, then we set |
| 306 | // m_should_stop to true; we are going to stop. But we still want to |
| 307 | // give all the breakpoints whose conditions say we are going to stop |
| 308 | // a chance to run their callbacks. Of course if any callback |
| 309 | // restarts the target by putting "continue" in the callback, then |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 310 | // we're going to restart, without running the rest of the callbacks. |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame^] | 311 | // And in this case we will end up not stopping even if another |
| 312 | // location said we should stop. But that's better than not running |
| 313 | // all the callbacks. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 314 | |
| 315 | m_should_stop = false; |
| 316 | |
| 317 | // We don't select threads as we go through them testing breakpoint |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame^] | 318 | // conditions and running commands. So we need to set the thread for |
| 319 | // expression evaluation here: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 320 | ThreadList::ExpressionExecutionThreadPusher thread_pusher(thread_sp); |
| 321 | |
| 322 | ExecutionContext exe_ctx(thread_sp->GetStackFrameAtIndex(0)); |
| 323 | Process *process = exe_ctx.GetProcessPtr(); |
| 324 | if (process->GetModIDRef().IsLastResumeForUserExpression()) { |
| 325 | // If we are in the middle of evaluating an expression, don't run |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame^] | 326 | // asynchronous breakpoint commands or expressions. That could |
| 327 | // lead to infinite recursion if the command or condition re-calls |
| 328 | // the function with this breakpoint. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 329 | // TODO: We can keep a list of the breakpoints we've seen while |
| 330 | // running expressions in the nested |
| 331 | // PerformAction calls that can arise when the action runs a |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame^] | 332 | // function that hits another breakpoint, and only stop running |
| 333 | // commands when we see the same breakpoint hit a second time. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 334 | |
| 335 | m_should_stop_is_valid = true; |
| 336 | if (log) |
| 337 | log->Printf("StopInfoBreakpoint::PerformAction - Hit a " |
| 338 | "breakpoint while running an expression," |
| 339 | " not running commands to avoid recursion."); |
| 340 | bool ignoring_breakpoints = |
| 341 | process->GetIgnoreBreakpointsInExpressions(); |
| 342 | if (ignoring_breakpoints) { |
| 343 | m_should_stop = false; |
| 344 | // Internal breakpoints will always stop. |
| 345 | for (size_t j = 0; j < num_owners; j++) { |
| 346 | lldb::BreakpointLocationSP bp_loc_sp = |
| 347 | bp_site_sp->GetOwnerAtIndex(j); |
| 348 | if (bp_loc_sp->GetBreakpoint().IsInternal()) { |
| 349 | m_should_stop = true; |
| 350 | break; |
Jason Molenda | 3f032ff | 2013-08-06 23:08:59 +0000 | [diff] [blame] | 351 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 352 | } |
| 353 | } else { |
| 354 | m_should_stop = true; |
Greg Clayton | 46c2b6e | 2013-04-29 23:30:46 +0000 | [diff] [blame] | 355 | } |
| 356 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 357 | log->Printf("StopInfoBreakpoint::PerformAction - in expression, " |
| 358 | "continuing: %s.", |
| 359 | m_should_stop ? "true" : "false"); |
| 360 | process->GetTarget().GetDebugger().GetAsyncOutputStream()->Printf( |
| 361 | "Warning: hit breakpoint while " |
| 362 | "running function, skipping commands and conditions to prevent " |
| 363 | "recursion."); |
| 364 | return; |
| 365 | } |
| 366 | |
| 367 | StoppointCallbackContext context(event_ptr, exe_ctx, false); |
| 368 | |
| 369 | // For safety's sake let's also grab an extra reference to the |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame^] | 370 | // breakpoint owners of the locations we're going to examine, since |
| 371 | // the locations are going to have to get back to their breakpoints, |
| 372 | // and the locations don't keep their owners alive. I'm just |
| 373 | // sticking the BreakpointSP's in a vector since I'm only using it to |
| 374 | // locally increment their retain counts. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 375 | |
| 376 | std::vector<lldb::BreakpointSP> location_owners; |
| 377 | |
| 378 | for (size_t j = 0; j < num_owners; j++) { |
| 379 | BreakpointLocationSP loc(site_locations.GetByIndex(j)); |
| 380 | location_owners.push_back(loc->GetBreakpoint().shared_from_this()); |
| 381 | } |
| 382 | |
| 383 | for (size_t j = 0; j < num_owners; j++) { |
| 384 | lldb::BreakpointLocationSP bp_loc_sp = site_locations.GetByIndex(j); |
Jim Ingham | f08f5c9 | 2017-08-03 18:13:24 +0000 | [diff] [blame] | 385 | StreamString loc_desc; |
| 386 | if (log) { |
| 387 | bp_loc_sp->GetDescription(&loc_desc, eDescriptionLevelBrief); |
| 388 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 389 | // If another action disabled this breakpoint or its location, then |
| 390 | // don't run the actions. |
| 391 | if (!bp_loc_sp->IsEnabled() || |
| 392 | !bp_loc_sp->GetBreakpoint().IsEnabled()) |
| 393 | continue; |
| 394 | |
| 395 | // The breakpoint site may have many locations associated with it, |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame^] | 396 | // not all of them valid for this thread. Skip the ones that |
| 397 | // aren't: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 398 | if (!bp_loc_sp->ValidForThisThread(thread_sp.get())) { |
| 399 | if (log) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 400 | log->Printf("Breakpoint %s hit on thread 0x%llx but it was not " |
| 401 | "for this thread, continuing.", |
Jim Ingham | f08f5c9 | 2017-08-03 18:13:24 +0000 | [diff] [blame] | 402 | loc_desc.GetData(), static_cast<unsigned long long>( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 403 | thread_sp->GetID())); |
| 404 | } |
| 405 | continue; |
| 406 | } |
| 407 | |
Jim Ingham | f08f5c9 | 2017-08-03 18:13:24 +0000 | [diff] [blame] | 408 | internal_breakpoint = bp_loc_sp->GetBreakpoint().IsInternal(); |
| 409 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 410 | // First run the precondition, but since the precondition is per |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame^] | 411 | // breakpoint, only run it once per breakpoint. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 412 | std::pair<std::unordered_set<break_id_t>::iterator, bool> result = |
| 413 | precondition_breakpoints.insert( |
| 414 | bp_loc_sp->GetBreakpoint().GetID()); |
| 415 | if (!result.second) |
| 416 | continue; |
| 417 | |
| 418 | bool precondition_result = |
| 419 | bp_loc_sp->GetBreakpoint().EvaluatePrecondition(context); |
| 420 | if (!precondition_result) |
| 421 | continue; |
| 422 | |
| 423 | // Next run the condition for the breakpoint. If that says we |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame^] | 424 | // should stop, then we'll run the callback for the breakpoint. If |
| 425 | // the callback says we shouldn't stop that will win. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 426 | |
| 427 | if (bp_loc_sp->GetConditionText() != nullptr) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 428 | Status condition_error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 429 | bool condition_says_stop = |
| 430 | bp_loc_sp->ConditionSaysStop(exe_ctx, condition_error); |
| 431 | |
| 432 | if (!condition_error.Success()) { |
| 433 | Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger(); |
| 434 | StreamSP error_sp = debugger.GetAsyncErrorStream(); |
| 435 | error_sp->Printf("Stopped due to an error evaluating condition " |
| 436 | "of breakpoint "); |
| 437 | bp_loc_sp->GetDescription(error_sp.get(), |
| 438 | eDescriptionLevelBrief); |
| 439 | error_sp->Printf(": \"%s\"", bp_loc_sp->GetConditionText()); |
| 440 | error_sp->EOL(); |
| 441 | const char *err_str = |
| 442 | condition_error.AsCString("<Unknown Error>"); |
| 443 | if (log) |
| 444 | log->Printf("Error evaluating condition: \"%s\"\n", err_str); |
| 445 | |
| 446 | error_sp->PutCString(err_str); |
| 447 | error_sp->EOL(); |
| 448 | error_sp->Flush(); |
| 449 | } else { |
| 450 | if (log) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 451 | log->Printf("Condition evaluated for breakpoint %s on thread " |
| 452 | "0x%llx conditon_says_stop: %i.", |
Jim Ingham | f08f5c9 | 2017-08-03 18:13:24 +0000 | [diff] [blame] | 453 | loc_desc.GetData(), |
| 454 | static_cast<unsigned long long>( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 455 | thread_sp->GetID()), |
| 456 | condition_says_stop); |
| 457 | } |
| 458 | if (!condition_says_stop) { |
| 459 | // We don't want to increment the hit count of breakpoints if |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame^] | 460 | // the condition fails. We've already bumped it by the time |
| 461 | // we get here, so undo the bump: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 462 | bp_loc_sp->UndoBumpHitCount(); |
| 463 | continue; |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | |
Jim Ingham | f08f5c9 | 2017-08-03 18:13:24 +0000 | [diff] [blame] | 468 | // Check the auto-continue bit on the location, do this before the |
| 469 | // callback since it may change this, but that would be for the |
| 470 | // NEXT hit. Note, you might think you could check auto-continue |
| 471 | // before the condition, and not evaluate the condition if it says |
| 472 | // to continue. But failing the condition means the breakpoint was |
| 473 | // effectively NOT HIT. So these two states are different. |
| 474 | bool auto_continue_says_stop = true; |
| 475 | if (bp_loc_sp->IsAutoContinue()) |
| 476 | { |
| 477 | if (log) |
| 478 | log->Printf("Continuing breakpoint %s as AutoContinue was set.", |
| 479 | loc_desc.GetData()); |
| 480 | // We want this stop reported, so you will know we auto-continued |
| 481 | // but only for external breakpoints: |
| 482 | if (!internal_breakpoint) |
| 483 | thread_sp->SetShouldReportStop(eVoteYes); |
| 484 | auto_continue_says_stop = false; |
| 485 | } |
| 486 | |
| 487 | bool callback_says_stop = true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 488 | |
| 489 | // FIXME: For now the callbacks have to run in async mode - the |
| 490 | // first time we restart we need |
| 491 | // to get out of there. So set it here. |
| 492 | // When we figure out how to nest breakpoint hits then this will |
| 493 | // change. |
| 494 | |
| 495 | Debugger &debugger = thread_sp->CalculateTarget()->GetDebugger(); |
| 496 | bool old_async = debugger.GetAsyncExecution(); |
| 497 | debugger.SetAsyncExecution(true); |
| 498 | |
| 499 | callback_says_stop = bp_loc_sp->InvokeCallback(&context); |
| 500 | |
| 501 | debugger.SetAsyncExecution(old_async); |
| 502 | |
Jim Ingham | f08f5c9 | 2017-08-03 18:13:24 +0000 | [diff] [blame] | 503 | if (callback_says_stop && auto_continue_says_stop) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 504 | m_should_stop = true; |
Boris Ulasevich | 86aaa8a | 2017-02-15 11:42:47 +0000 | [diff] [blame] | 505 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 506 | // If we are going to stop for this breakpoint, then remove the |
| 507 | // breakpoint. |
| 508 | if (callback_says_stop && bp_loc_sp && |
| 509 | bp_loc_sp->GetBreakpoint().IsOneShot()) { |
| 510 | thread_sp->GetProcess()->GetTarget().RemoveBreakpointByID( |
| 511 | bp_loc_sp->GetBreakpoint().GetID()); |
| 512 | } |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame^] | 513 | // Also make sure that the callback hasn't continued the target. If |
| 514 | // it did, when we'll set m_should_start to false and get out of |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 515 | // here. |
| 516 | if (HasTargetRunSinceMe()) { |
| 517 | m_should_stop = false; |
| 518 | break; |
| 519 | } |
| 520 | } |
Jim Ingham | 3ebcf7f | 2010-08-10 00:59:59 +0000 | [diff] [blame] | 521 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 522 | // We've figured out what this stop wants to do, so mark it as valid so |
| 523 | // we don't compute it again. |
| 524 | m_should_stop_is_valid = true; |
| 525 | } else { |
| 526 | m_should_stop = true; |
| 527 | m_should_stop_is_valid = true; |
| 528 | Log *log_process( |
| 529 | lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 530 | |
| 531 | if (log_process) |
| 532 | log_process->Printf( |
| 533 | "Process::%s could not find breakpoint site id: %" PRId64 "...", |
| 534 | __FUNCTION__, m_value); |
| 535 | } |
Boris Ulasevich | 86aaa8a | 2017-02-15 11:42:47 +0000 | [diff] [blame] | 536 | |
| 537 | if ((m_should_stop == false || internal_breakpoint) |
| 538 | && thread_sp->CompletedPlanOverridesBreakpoint()) { |
| 539 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame^] | 540 | // Override should_stop decision when we have completed step plan |
| 541 | // additionally to the breakpoint |
Boris Ulasevich | 86aaa8a | 2017-02-15 11:42:47 +0000 | [diff] [blame] | 542 | m_should_stop = true; |
| 543 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame^] | 544 | // Here we clean the preset stop info so the next GetStopInfo call will |
| 545 | // find the appropriate stop info, which should be the stop info |
| 546 | // related to the completed plan |
Boris Ulasevich | 86aaa8a | 2017-02-15 11:42:47 +0000 | [diff] [blame] | 547 | thread_sp->ResetStopInfo(); |
| 548 | } |
| 549 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 550 | if (log) |
| 551 | log->Printf("Process::%s returning from action with m_should_stop: %d.", |
| 552 | __FUNCTION__, m_should_stop); |
Jim Ingham | 3ebcf7f | 2010-08-10 00:59:59 +0000 | [diff] [blame] | 553 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 554 | } |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 555 | |
| 556 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 557 | bool m_should_stop; |
| 558 | bool m_should_stop_is_valid; |
| 559 | bool m_should_perform_action; // Since we are trying to preserve the "state" |
| 560 | // of the system even if we run functions |
| 561 | // etc. behind the users backs, we need to make sure we only REALLY perform |
| 562 | // the action once. |
| 563 | lldb::addr_t m_address; // We use this to capture the breakpoint site address |
| 564 | // when we create the StopInfo, |
| 565 | // in case somebody deletes it between the time the StopInfo is made and the |
| 566 | // description is asked for. |
| 567 | lldb::break_id_t m_break_id; |
| 568 | bool m_was_one_shot; |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 569 | }; |
| 570 | |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 571 | //---------------------------------------------------------------------- |
| 572 | // StopInfoWatchpoint |
| 573 | //---------------------------------------------------------------------- |
| 574 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 575 | class StopInfoWatchpoint : public StopInfo { |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 576 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 577 | // Make sure watchpoint is properly disabled and subsequently enabled while |
| 578 | // performing watchpoint actions. |
| 579 | class WatchpointSentry { |
| 580 | public: |
Jim Ingham | 209a77d | 2016-10-25 20:34:32 +0000 | [diff] [blame] | 581 | WatchpointSentry(ProcessSP p_sp, WatchpointSP w_sp) : process_sp(p_sp), |
| 582 | watchpoint_sp(w_sp) { |
| 583 | if (process_sp && watchpoint_sp) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 584 | const bool notify = false; |
Jim Ingham | 209a77d | 2016-10-25 20:34:32 +0000 | [diff] [blame] | 585 | watchpoint_sp->TurnOnEphemeralMode(); |
| 586 | process_sp->DisableWatchpoint(watchpoint_sp.get(), notify); |
| 587 | process_sp->AddPreResumeAction(SentryPreResumeAction, this); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 588 | } |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 589 | } |
Jim Ingham | 209a77d | 2016-10-25 20:34:32 +0000 | [diff] [blame] | 590 | |
| 591 | void DoReenable() { |
| 592 | if (process_sp && watchpoint_sp) { |
| 593 | bool was_disabled = watchpoint_sp->IsDisabledDuringEphemeralMode(); |
| 594 | watchpoint_sp->TurnOffEphemeralMode(); |
| 595 | const bool notify = false; |
| 596 | if (was_disabled) { |
| 597 | process_sp->DisableWatchpoint(watchpoint_sp.get(), notify); |
| 598 | } else { |
| 599 | process_sp->EnableWatchpoint(watchpoint_sp.get(), notify); |
Pavel Labath | 2e8fe80 | 2016-10-21 10:52:11 +0000 | [diff] [blame] | 600 | } |
Pavel Labath | 2e8fe80 | 2016-10-21 10:52:11 +0000 | [diff] [blame] | 601 | } |
Jim Ingham | 94bd575 | 2016-10-21 00:06:38 +0000 | [diff] [blame] | 602 | } |
Jim Ingham | 209a77d | 2016-10-25 20:34:32 +0000 | [diff] [blame] | 603 | |
| 604 | ~WatchpointSentry() { |
| 605 | DoReenable(); |
| 606 | if (process_sp) |
| 607 | process_sp->ClearPreResumeAction(SentryPreResumeAction, this); |
| 608 | } |
| 609 | |
| 610 | static bool SentryPreResumeAction(void *sentry_void) { |
| 611 | WatchpointSentry *sentry = (WatchpointSentry *) sentry_void; |
| 612 | sentry->DoReenable(); |
| 613 | return true; |
| 614 | } |
Jim Ingham | f57b435 | 2012-11-10 02:08:14 +0000 | [diff] [blame] | 615 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 616 | private: |
Jim Ingham | 209a77d | 2016-10-25 20:34:32 +0000 | [diff] [blame] | 617 | ProcessSP process_sp; |
| 618 | WatchpointSP watchpoint_sp; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 619 | }; |
| 620 | |
| 621 | StopInfoWatchpoint(Thread &thread, break_id_t watch_id, |
| 622 | lldb::addr_t watch_hit_addr) |
| 623 | : StopInfo(thread, watch_id), m_should_stop(false), |
| 624 | m_should_stop_is_valid(false), m_watch_hit_addr(watch_hit_addr) {} |
| 625 | |
| 626 | ~StopInfoWatchpoint() override = default; |
| 627 | |
| 628 | StopReason GetStopReason() const override { return eStopReasonWatchpoint; } |
| 629 | |
| 630 | const char *GetDescription() override { |
| 631 | if (m_description.empty()) { |
| 632 | StreamString strm; |
| 633 | strm.Printf("watchpoint %" PRIi64, m_value); |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 634 | m_description = strm.GetString(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 635 | } |
| 636 | return m_description.c_str(); |
| 637 | } |
| 638 | |
Jim Ingham | f57b435 | 2012-11-10 02:08:14 +0000 | [diff] [blame] | 639 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 640 | bool ShouldStopSynchronous(Event *event_ptr) override { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame^] | 641 | // ShouldStop() method is idempotent and should not affect hit count. See |
| 642 | // Process::RunPrivateStateThread()->Process()->HandlePrivateEvent() |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 643 | // -->Process()::ShouldBroadcastEvent()->ThreadList::ShouldStop()-> |
| 644 | // Thread::ShouldStop()->ThreadPlanBase::ShouldStop()-> |
| 645 | // StopInfoWatchpoint::ShouldStop() and |
| 646 | // Event::DoOnRemoval()->Process::ProcessEventData::DoOnRemoval()-> |
| 647 | // StopInfoWatchpoint::PerformAction(). |
| 648 | if (m_should_stop_is_valid) |
| 649 | return m_should_stop; |
Johnny Chen | fd158f4 | 2011-09-21 22:47:15 +0000 | [diff] [blame] | 650 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 651 | ThreadSP thread_sp(m_thread_wp.lock()); |
| 652 | if (thread_sp) { |
| 653 | WatchpointSP wp_sp( |
| 654 | thread_sp->CalculateTarget()->GetWatchpointList().FindByID( |
| 655 | GetValue())); |
| 656 | if (wp_sp) { |
| 657 | // Check if we should stop at a watchpoint. |
| 658 | ExecutionContext exe_ctx(thread_sp->GetStackFrameAtIndex(0)); |
| 659 | StoppointCallbackContext context(event_ptr, exe_ctx, true); |
| 660 | m_should_stop = wp_sp->ShouldStop(&context); |
| 661 | } else { |
| 662 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Johnny Chen | fd158f4 | 2011-09-21 22:47:15 +0000 | [diff] [blame] | 663 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 664 | if (log) |
| 665 | log->Printf( |
| 666 | "Process::%s could not find watchpoint location id: %" PRId64 |
| 667 | "...", |
| 668 | __FUNCTION__, GetValue()); |
Johnny Chen | fd158f4 | 2011-09-21 22:47:15 +0000 | [diff] [blame] | 669 | |
Johnny Chen | 16dcf71 | 2011-10-17 18:58:00 +0000 | [diff] [blame] | 670 | m_should_stop = true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 671 | } |
Johnny Chen | 16dcf71 | 2011-10-17 18:58:00 +0000 | [diff] [blame] | 672 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 673 | m_should_stop_is_valid = true; |
| 674 | return m_should_stop; |
| 675 | } |
| 676 | |
| 677 | bool ShouldStop(Event *event_ptr) override { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame^] | 678 | // This just reports the work done by PerformAction or the synchronous |
| 679 | // stop. It should only ever get called after they have had a chance to |
| 680 | // run. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 681 | assert(m_should_stop_is_valid); |
| 682 | return m_should_stop; |
| 683 | } |
| 684 | |
| 685 | void PerformAction(Event *event_ptr) override { |
| 686 | Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS); |
| 687 | // We're going to calculate if we should stop or not in some way during the |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame^] | 688 | // course of this code. Also by default we're going to stop, so set that |
| 689 | // here. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 690 | m_should_stop = true; |
Jim Ingham | 209a77d | 2016-10-25 20:34:32 +0000 | [diff] [blame] | 691 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 692 | |
| 693 | ThreadSP thread_sp(m_thread_wp.lock()); |
| 694 | if (thread_sp) { |
| 695 | |
| 696 | WatchpointSP wp_sp( |
| 697 | thread_sp->CalculateTarget()->GetWatchpointList().FindByID( |
Jim Ingham | 209a77d | 2016-10-25 20:34:32 +0000 | [diff] [blame] | 698 | GetValue())); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 699 | if (wp_sp) { |
| 700 | ExecutionContext exe_ctx(thread_sp->GetStackFrameAtIndex(0)); |
Jim Ingham | 209a77d | 2016-10-25 20:34:32 +0000 | [diff] [blame] | 701 | ProcessSP process_sp = exe_ctx.GetProcessSP(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 702 | |
| 703 | { |
| 704 | // check if this process is running on an architecture where |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame^] | 705 | // watchpoints trigger before the associated instruction runs. if so, |
| 706 | // disable the WP, single-step and then re-enable the watchpoint |
Jim Ingham | 209a77d | 2016-10-25 20:34:32 +0000 | [diff] [blame] | 707 | if (process_sp) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 708 | uint32_t num; |
| 709 | bool wp_triggers_after; |
Jim Ingham | 209a77d | 2016-10-25 20:34:32 +0000 | [diff] [blame] | 710 | |
| 711 | if (process_sp->GetWatchpointSupportInfo(num, wp_triggers_after) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 712 | .Success()) { |
| 713 | if (!wp_triggers_after) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame^] | 714 | // We need to preserve the watch_index before watchpoint is |
| 715 | // disable. Since Watchpoint::SetEnabled will clear the watch |
| 716 | // index. This will fix TestWatchpointIter failure |
Nitesh Jain | 0546e84 | 2016-12-09 13:54:47 +0000 | [diff] [blame] | 717 | Watchpoint *wp = wp_sp.get(); |
| 718 | uint32_t watch_index = wp->GetHardwareIndex(); |
| 719 | process_sp->DisableWatchpoint(wp, false); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 720 | StopInfoSP stored_stop_info_sp = thread_sp->GetStopInfo(); |
| 721 | assert(stored_stop_info_sp.get() == this); |
| 722 | |
| 723 | ThreadPlanSP new_plan_sp( |
| 724 | thread_sp->QueueThreadPlanForStepSingleInstruction( |
| 725 | false, // step-over |
| 726 | false, // abort_other_plans |
| 727 | true)); // stop_other_threads |
| 728 | new_plan_sp->SetIsMasterPlan(true); |
| 729 | new_plan_sp->SetOkayToDiscard(false); |
| 730 | new_plan_sp->SetPrivate(true); |
Jim Ingham | 209a77d | 2016-10-25 20:34:32 +0000 | [diff] [blame] | 731 | process_sp->GetThreadList().SetSelectedThreadByID( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 732 | thread_sp->GetID()); |
Jim Ingham | 209a77d | 2016-10-25 20:34:32 +0000 | [diff] [blame] | 733 | process_sp->ResumeSynchronous(nullptr); |
| 734 | process_sp->GetThreadList().SetSelectedThreadByID( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 735 | thread_sp->GetID()); |
| 736 | thread_sp->SetStopInfo(stored_stop_info_sp); |
Nitesh Jain | 0546e84 | 2016-12-09 13:54:47 +0000 | [diff] [blame] | 737 | process_sp->EnableWatchpoint(wp, false); |
| 738 | wp->SetHardwareIndex(watch_index); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 739 | } |
| 740 | } |
| 741 | } |
| 742 | } |
| 743 | |
Jim Ingham | 209a77d | 2016-10-25 20:34:32 +0000 | [diff] [blame] | 744 | // This sentry object makes sure the current watchpoint is disabled |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame^] | 745 | // while performing watchpoint actions, and it is then enabled after we |
| 746 | // are finished. |
Jim Ingham | 209a77d | 2016-10-25 20:34:32 +0000 | [diff] [blame] | 747 | WatchpointSentry sentry(process_sp, wp_sp); |
| 748 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 749 | /* |
| 750 | * MIPS: Last 3bits of the watchpoint address are masked by the kernel. |
| 751 | * For example: |
| 752 | * 'n' is at 0x120010d00 and 'm' is 0x120010d04. When a watchpoint is |
| 753 | * set at 'm', then |
| 754 | * watch exception is generated even when 'n' is read/written. To handle |
| 755 | * this case, |
| 756 | * server emulates the instruction at PC and finds the base address of |
| 757 | * the load/store |
| 758 | * instruction and appends it in the description of the stop-info |
| 759 | * packet. If watchpoint |
| 760 | * is not set on this address by user then this do not stop. |
| 761 | */ |
| 762 | if (m_watch_hit_addr != LLDB_INVALID_ADDRESS) { |
| 763 | WatchpointSP wp_hit_sp = |
| 764 | thread_sp->CalculateTarget()->GetWatchpointList().FindByAddress( |
| 765 | m_watch_hit_addr); |
| 766 | if (!wp_hit_sp) { |
| 767 | m_should_stop = false; |
| 768 | wp_sp->IncrementFalseAlarmsAndReviseHitCount(); |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | // TODO: This condition should be checked in the synchronous part of the |
| 773 | // watchpoint code |
| 774 | // (Watchpoint::ShouldStop), so that we avoid pulling an event even if |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame^] | 775 | // the watchpoint fails the ignore count condition. It is moved here |
| 776 | // temporarily, because for archs with |
| 777 | // watchpoint_exceptions_received=before, the code in the previous |
| 778 | // lines takes care of moving the inferior to next PC. We have to check |
| 779 | // the ignore count condition after this is done, otherwise we will hit |
| 780 | // same watchpoint multiple times until we pass ignore condition, but |
| 781 | // we won't actually be ignoring them. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 782 | if (wp_sp->GetHitCount() <= wp_sp->GetIgnoreCount()) |
| 783 | m_should_stop = false; |
| 784 | |
Jim Ingham | 209a77d | 2016-10-25 20:34:32 +0000 | [diff] [blame] | 785 | Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger(); |
| 786 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 787 | if (m_should_stop && wp_sp->GetConditionText() != nullptr) { |
| 788 | // We need to make sure the user sees any parse errors in their |
Zachary Turner | c5d7df9 | 2016-11-08 04:52:16 +0000 | [diff] [blame] | 789 | // condition, so we'll hook the constructor errors up to the |
| 790 | // debugger's Async I/O. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 791 | ExpressionResults result_code; |
| 792 | EvaluateExpressionOptions expr_options; |
| 793 | expr_options.SetUnwindOnError(true); |
| 794 | expr_options.SetIgnoreBreakpoints(true); |
| 795 | ValueObjectSP result_value_sp; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 796 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 797 | result_code = UserExpression::Evaluate( |
Zachary Turner | c5d7df9 | 2016-11-08 04:52:16 +0000 | [diff] [blame] | 798 | exe_ctx, expr_options, wp_sp->GetConditionText(), |
| 799 | llvm::StringRef(), result_value_sp, error); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 800 | |
| 801 | if (result_code == eExpressionCompleted) { |
| 802 | if (result_value_sp) { |
| 803 | Scalar scalar_value; |
| 804 | if (result_value_sp->ResolveValue(scalar_value)) { |
| 805 | if (scalar_value.ULongLong(1) == 0) { |
| 806 | // We have been vetoed. This takes precedence over querying |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame^] | 807 | // the watchpoint whether it should stop (aka ignore count |
| 808 | // and friends). See also StopInfoWatchpoint::ShouldStop() |
| 809 | // as well as Process::ProcessEventData::DoOnRemoval(). |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 810 | m_should_stop = false; |
| 811 | } else |
| 812 | m_should_stop = true; |
| 813 | if (log) |
| 814 | log->Printf( |
| 815 | "Condition successfully evaluated, result is %s.\n", |
| 816 | m_should_stop ? "true" : "false"); |
| 817 | } else { |
| 818 | m_should_stop = true; |
| 819 | if (log) |
| 820 | log->Printf( |
| 821 | "Failed to get an integer result from the expression."); |
| 822 | } |
| 823 | } |
| 824 | } else { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 825 | StreamSP error_sp = debugger.GetAsyncErrorStream(); |
| 826 | error_sp->Printf( |
| 827 | "Stopped due to an error evaluating condition of watchpoint "); |
| 828 | wp_sp->GetDescription(error_sp.get(), eDescriptionLevelBrief); |
| 829 | error_sp->Printf(": \"%s\"", wp_sp->GetConditionText()); |
| 830 | error_sp->EOL(); |
| 831 | const char *err_str = error.AsCString("<Unknown Error>"); |
| 832 | if (log) |
| 833 | log->Printf("Error evaluating condition: \"%s\"\n", err_str); |
| 834 | |
| 835 | error_sp->PutCString(err_str); |
| 836 | error_sp->EOL(); |
| 837 | error_sp->Flush(); |
| 838 | // If the condition fails to be parsed or run, we should stop. |
| 839 | m_should_stop = true; |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | // If the condition says to stop, we run the callback to further decide |
| 844 | // whether to stop. |
| 845 | if (m_should_stop) { |
Jim Ingham | 209a77d | 2016-10-25 20:34:32 +0000 | [diff] [blame] | 846 | // FIXME: For now the callbacks have to run in async mode - the |
| 847 | // first time we restart we need |
| 848 | // to get out of there. So set it here. |
| 849 | // When we figure out how to nest watchpoint hits then this will |
| 850 | // change. |
| 851 | |
| 852 | bool old_async = debugger.GetAsyncExecution(); |
| 853 | debugger.SetAsyncExecution(true); |
| 854 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 855 | StoppointCallbackContext context(event_ptr, exe_ctx, false); |
| 856 | bool stop_requested = wp_sp->InvokeCallback(&context); |
Jim Ingham | 209a77d | 2016-10-25 20:34:32 +0000 | [diff] [blame] | 857 | |
| 858 | debugger.SetAsyncExecution(old_async); |
| 859 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame^] | 860 | // Also make sure that the callback hasn't continued the target. If |
| 861 | // it did, when we'll set m_should_stop to false and get out of here. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 862 | if (HasTargetRunSinceMe()) |
| 863 | m_should_stop = false; |
| 864 | |
| 865 | if (m_should_stop && !stop_requested) { |
| 866 | // We have been vetoed by the callback mechanism. |
| 867 | m_should_stop = false; |
| 868 | } |
| 869 | } |
| 870 | // Finally, if we are going to stop, print out the new & old values: |
| 871 | if (m_should_stop) { |
| 872 | wp_sp->CaptureWatchedValue(exe_ctx); |
| 873 | |
| 874 | Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger(); |
| 875 | StreamSP output_sp = debugger.GetAsyncOutputStream(); |
| 876 | wp_sp->DumpSnapshots(output_sp.get()); |
| 877 | output_sp->EOL(); |
| 878 | output_sp->Flush(); |
| 879 | } |
| 880 | |
| 881 | } else { |
| 882 | Log *log_process( |
| 883 | lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 884 | |
| 885 | if (log_process) |
| 886 | log_process->Printf( |
| 887 | "Process::%s could not find watchpoint id: %" PRId64 "...", |
| 888 | __FUNCTION__, m_value); |
| 889 | } |
| 890 | if (log) |
| 891 | log->Printf("Process::%s returning from action with m_should_stop: %d.", |
| 892 | __FUNCTION__, m_should_stop); |
| 893 | |
| 894 | m_should_stop_is_valid = true; |
| 895 | } |
| 896 | } |
| 897 | |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 898 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 899 | bool m_should_stop; |
| 900 | bool m_should_stop_is_valid; |
| 901 | lldb::addr_t m_watch_hit_addr; |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 902 | }; |
| 903 | |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 904 | //---------------------------------------------------------------------- |
| 905 | // StopInfoUnixSignal |
| 906 | //---------------------------------------------------------------------- |
| 907 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 908 | class StopInfoUnixSignal : public StopInfo { |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 909 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 910 | StopInfoUnixSignal(Thread &thread, int signo, const char *description) |
| 911 | : StopInfo(thread, signo) { |
| 912 | SetDescription(description); |
| 913 | } |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 914 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 915 | ~StopInfoUnixSignal() override = default; |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 916 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 917 | StopReason GetStopReason() const override { return eStopReasonSignal; } |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 918 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 919 | bool ShouldStopSynchronous(Event *event_ptr) override { |
| 920 | ThreadSP thread_sp(m_thread_wp.lock()); |
| 921 | if (thread_sp) |
| 922 | return thread_sp->GetProcess()->GetUnixSignals()->GetShouldStop(m_value); |
| 923 | return false; |
| 924 | } |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 925 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 926 | bool ShouldStop(Event *event_ptr) override { |
| 927 | ThreadSP thread_sp(m_thread_wp.lock()); |
| 928 | if (thread_sp) |
| 929 | return thread_sp->GetProcess()->GetUnixSignals()->GetShouldStop(m_value); |
| 930 | return false; |
| 931 | } |
Eugene Zelenko | 8f30a65 | 2015-10-23 18:39:37 +0000 | [diff] [blame] | 932 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 933 | // If should stop returns false, check if we should notify of this event |
| 934 | bool DoShouldNotify(Event *event_ptr) override { |
| 935 | ThreadSP thread_sp(m_thread_wp.lock()); |
| 936 | if (thread_sp) { |
| 937 | bool should_notify = |
| 938 | thread_sp->GetProcess()->GetUnixSignals()->GetShouldNotify(m_value); |
| 939 | if (should_notify) { |
| 940 | StreamString strm; |
| 941 | strm.Printf( |
| 942 | "thread %d received signal: %s", thread_sp->GetIndexID(), |
| 943 | thread_sp->GetProcess()->GetUnixSignals()->GetSignalAsCString( |
| 944 | m_value)); |
| 945 | Process::ProcessEventData::AddRestartedReason(event_ptr, |
| 946 | strm.GetData()); |
| 947 | } |
| 948 | return should_notify; |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 949 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 950 | return true; |
| 951 | } |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 952 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 953 | void WillResume(lldb::StateType resume_state) override { |
| 954 | ThreadSP thread_sp(m_thread_wp.lock()); |
| 955 | if (thread_sp) { |
| 956 | if (!thread_sp->GetProcess()->GetUnixSignals()->GetShouldSuppress( |
| 957 | m_value)) |
| 958 | thread_sp->SetResumeSignal(m_value); |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 959 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 960 | } |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 961 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 962 | const char *GetDescription() override { |
| 963 | if (m_description.empty()) { |
| 964 | ThreadSP thread_sp(m_thread_wp.lock()); |
| 965 | if (thread_sp) { |
| 966 | StreamString strm; |
| 967 | const char *signal_name = |
| 968 | thread_sp->GetProcess()->GetUnixSignals()->GetSignalAsCString( |
| 969 | m_value); |
| 970 | if (signal_name) |
| 971 | strm.Printf("signal %s", signal_name); |
| 972 | else |
| 973 | strm.Printf("signal %" PRIi64, m_value); |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 974 | m_description = strm.GetString(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 975 | } |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 976 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 977 | return m_description.c_str(); |
| 978 | } |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 979 | }; |
| 980 | |
| 981 | //---------------------------------------------------------------------- |
| 982 | // StopInfoTrace |
| 983 | //---------------------------------------------------------------------- |
| 984 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 985 | class StopInfoTrace : public StopInfo { |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 986 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 987 | StopInfoTrace(Thread &thread) : StopInfo(thread, LLDB_INVALID_UID) {} |
Eugene Zelenko | 8f30a65 | 2015-10-23 18:39:37 +0000 | [diff] [blame] | 988 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 989 | ~StopInfoTrace() override = default; |
Eugene Zelenko | 8f30a65 | 2015-10-23 18:39:37 +0000 | [diff] [blame] | 990 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 991 | StopReason GetStopReason() const override { return eStopReasonTrace; } |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 992 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 993 | const char *GetDescription() override { |
| 994 | if (m_description.empty()) |
| 995 | return "trace"; |
| 996 | else |
| 997 | return m_description.c_str(); |
| 998 | } |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 999 | }; |
| 1000 | |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1001 | //---------------------------------------------------------------------- |
| 1002 | // StopInfoException |
| 1003 | //---------------------------------------------------------------------- |
| 1004 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1005 | class StopInfoException : public StopInfo { |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1006 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1007 | StopInfoException(Thread &thread, const char *description) |
| 1008 | : StopInfo(thread, LLDB_INVALID_UID) { |
| 1009 | if (description) |
| 1010 | SetDescription(description); |
| 1011 | } |
Eugene Zelenko | 8f30a65 | 2015-10-23 18:39:37 +0000 | [diff] [blame] | 1012 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1013 | ~StopInfoException() override = default; |
Eugene Zelenko | 8f30a65 | 2015-10-23 18:39:37 +0000 | [diff] [blame] | 1014 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1015 | StopReason GetStopReason() const override { return eStopReasonException; } |
| 1016 | |
| 1017 | const char *GetDescription() override { |
| 1018 | if (m_description.empty()) |
| 1019 | return "exception"; |
| 1020 | else |
| 1021 | return m_description.c_str(); |
| 1022 | } |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 1023 | }; |
| 1024 | |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 1025 | //---------------------------------------------------------------------- |
| 1026 | // StopInfoThreadPlan |
| 1027 | //---------------------------------------------------------------------- |
| 1028 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1029 | class StopInfoThreadPlan : public StopInfo { |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 1030 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1031 | StopInfoThreadPlan(ThreadPlanSP &plan_sp, ValueObjectSP &return_valobj_sp, |
| 1032 | ExpressionVariableSP &expression_variable_sp) |
| 1033 | : StopInfo(plan_sp->GetThread(), LLDB_INVALID_UID), m_plan_sp(plan_sp), |
| 1034 | m_return_valobj_sp(return_valobj_sp), |
| 1035 | m_expression_variable_sp(expression_variable_sp) {} |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 1036 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1037 | ~StopInfoThreadPlan() override = default; |
Eugene Zelenko | 8f30a65 | 2015-10-23 18:39:37 +0000 | [diff] [blame] | 1038 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1039 | StopReason GetStopReason() const override { return eStopReasonPlanComplete; } |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 1040 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1041 | const char *GetDescription() override { |
| 1042 | if (m_description.empty()) { |
| 1043 | StreamString strm; |
| 1044 | m_plan_sp->GetDescription(&strm, eDescriptionLevelBrief); |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 1045 | m_description = strm.GetString(); |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 1046 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1047 | return m_description.c_str(); |
| 1048 | } |
| 1049 | |
| 1050 | ValueObjectSP GetReturnValueObject() { return m_return_valobj_sp; } |
| 1051 | |
| 1052 | ExpressionVariableSP GetExpressionVariable() { |
| 1053 | return m_expression_variable_sp; |
| 1054 | } |
| 1055 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 1056 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1057 | bool ShouldStop(Event *event_ptr) override { |
| 1058 | if (m_plan_sp) |
| 1059 | return m_plan_sp->ShouldStop(event_ptr); |
| 1060 | else |
| 1061 | return StopInfo::ShouldStop(event_ptr); |
| 1062 | } |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 1063 | |
| 1064 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1065 | ThreadPlanSP m_plan_sp; |
| 1066 | ValueObjectSP m_return_valobj_sp; |
| 1067 | ExpressionVariableSP m_expression_variable_sp; |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 1068 | }; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1069 | |
Jim Ingham | ba205c1 | 2017-12-05 02:50:45 +0000 | [diff] [blame] | 1070 | //---------------------------------------------------------------------- |
| 1071 | // StopInfoExec |
| 1072 | //---------------------------------------------------------------------- |
| 1073 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1074 | class StopInfoExec : public StopInfo { |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 1075 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1076 | StopInfoExec(Thread &thread) |
| 1077 | : StopInfo(thread, LLDB_INVALID_UID), m_performed_action(false) {} |
Eugene Zelenko | 8f30a65 | 2015-10-23 18:39:37 +0000 | [diff] [blame] | 1078 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1079 | ~StopInfoExec() override = default; |
Eugene Zelenko | 8f30a65 | 2015-10-23 18:39:37 +0000 | [diff] [blame] | 1080 | |
Jim Ingham | ba205c1 | 2017-12-05 02:50:45 +0000 | [diff] [blame] | 1081 | bool ShouldStop(Event *event_ptr) override { |
| 1082 | ThreadSP thread_sp(m_thread_wp.lock()); |
| 1083 | if (thread_sp) |
| 1084 | return thread_sp->GetProcess()->GetStopOnExec(); |
| 1085 | return false; |
| 1086 | } |
| 1087 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1088 | StopReason GetStopReason() const override { return eStopReasonExec; } |
| 1089 | |
| 1090 | const char *GetDescription() override { return "exec"; } |
Eugene Zelenko | 8f30a65 | 2015-10-23 18:39:37 +0000 | [diff] [blame] | 1091 | |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 1092 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1093 | void PerformAction(Event *event_ptr) override { |
| 1094 | // Only perform the action once |
| 1095 | if (m_performed_action) |
| 1096 | return; |
| 1097 | m_performed_action = true; |
| 1098 | ThreadSP thread_sp(m_thread_wp.lock()); |
| 1099 | if (thread_sp) |
| 1100 | thread_sp->GetProcess()->DidExec(); |
| 1101 | } |
| 1102 | |
| 1103 | bool m_performed_action; |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 1104 | }; |
| 1105 | |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1106 | } // namespace lldb_private |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 1107 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1108 | StopInfoSP StopInfo::CreateStopReasonWithBreakpointSiteID(Thread &thread, |
| 1109 | break_id_t break_id) { |
| 1110 | return StopInfoSP(new StopInfoBreakpoint(thread, break_id)); |
| 1111 | } |
| 1112 | |
| 1113 | StopInfoSP StopInfo::CreateStopReasonWithBreakpointSiteID(Thread &thread, |
| 1114 | break_id_t break_id, |
| 1115 | bool should_stop) { |
| 1116 | return StopInfoSP(new StopInfoBreakpoint(thread, break_id, should_stop)); |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | StopInfoSP |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1120 | StopInfo::CreateStopReasonWithWatchpointID(Thread &thread, break_id_t watch_id, |
| 1121 | lldb::addr_t watch_hit_addr) { |
| 1122 | return StopInfoSP(new StopInfoWatchpoint(thread, watch_id, watch_hit_addr)); |
Jim Ingham | 36f3b36 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1125 | StopInfoSP StopInfo::CreateStopReasonWithSignal(Thread &thread, int signo, |
| 1126 | const char *description) { |
| 1127 | return StopInfoSP(new StopInfoUnixSignal(thread, signo, description)); |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1130 | StopInfoSP StopInfo::CreateStopReasonToTrace(Thread &thread) { |
| 1131 | return StopInfoSP(new StopInfoTrace(thread)); |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1134 | StopInfoSP StopInfo::CreateStopReasonWithPlan( |
| 1135 | ThreadPlanSP &plan_sp, ValueObjectSP return_valobj_sp, |
| 1136 | ExpressionVariableSP expression_variable_sp) { |
| 1137 | return StopInfoSP(new StopInfoThreadPlan(plan_sp, return_valobj_sp, |
| 1138 | expression_variable_sp)); |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1141 | StopInfoSP StopInfo::CreateStopReasonWithException(Thread &thread, |
| 1142 | const char *description) { |
| 1143 | return StopInfoSP(new StopInfoException(thread, description)); |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 1144 | } |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1145 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1146 | StopInfoSP StopInfo::CreateStopReasonWithExec(Thread &thread) { |
| 1147 | return StopInfoSP(new StopInfoExec(thread)); |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1148 | } |
Jim Ingham | 73ca05a | 2011-12-17 01:35:57 +0000 | [diff] [blame] | 1149 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1150 | ValueObjectSP StopInfo::GetReturnValueObject(StopInfoSP &stop_info_sp) { |
| 1151 | if (stop_info_sp && |
| 1152 | stop_info_sp->GetStopReason() == eStopReasonPlanComplete) { |
| 1153 | StopInfoThreadPlan *plan_stop_info = |
| 1154 | static_cast<StopInfoThreadPlan *>(stop_info_sp.get()); |
| 1155 | return plan_stop_info->GetReturnValueObject(); |
| 1156 | } else |
| 1157 | return ValueObjectSP(); |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1160 | ExpressionVariableSP StopInfo::GetExpressionVariable(StopInfoSP &stop_info_sp) { |
| 1161 | if (stop_info_sp && |
| 1162 | stop_info_sp->GetStopReason() == eStopReasonPlanComplete) { |
| 1163 | StopInfoThreadPlan *plan_stop_info = |
| 1164 | static_cast<StopInfoThreadPlan *>(stop_info_sp.get()); |
| 1165 | return plan_stop_info->GetExpressionVariable(); |
| 1166 | } else |
| 1167 | return ExpressionVariableSP(); |
Jim Ingham | 30fadaf | 2014-07-08 01:07:32 +0000 | [diff] [blame] | 1168 | } |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 +0000 | [diff] [blame] | 1169 | |
| 1170 | lldb::ValueObjectSP |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1171 | StopInfo::GetCrashingDereference(StopInfoSP &stop_info_sp, |
| 1172 | lldb::addr_t *crashing_address) { |
| 1173 | if (!stop_info_sp) { |
| 1174 | return ValueObjectSP(); |
| 1175 | } |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 +0000 | [diff] [blame] | 1176 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1177 | const char *description = stop_info_sp->GetDescription(); |
| 1178 | if (!description) { |
| 1179 | return ValueObjectSP(); |
| 1180 | } |
| 1181 | |
| 1182 | ThreadSP thread_sp = stop_info_sp->GetThread(); |
| 1183 | if (!thread_sp) { |
| 1184 | return ValueObjectSP(); |
| 1185 | } |
| 1186 | |
| 1187 | StackFrameSP frame_sp = thread_sp->GetSelectedFrame(); |
| 1188 | |
| 1189 | if (!frame_sp) { |
| 1190 | return ValueObjectSP(); |
| 1191 | } |
| 1192 | |
| 1193 | const char address_string[] = "address="; |
| 1194 | |
| 1195 | const char *address_loc = strstr(description, address_string); |
| 1196 | if (!address_loc) { |
| 1197 | return ValueObjectSP(); |
| 1198 | } |
| 1199 | |
| 1200 | address_loc += (sizeof(address_string) - 1); |
| 1201 | |
| 1202 | uint64_t address = strtoull(address_loc, 0, 0); |
| 1203 | if (crashing_address) { |
| 1204 | *crashing_address = address; |
| 1205 | } |
| 1206 | |
| 1207 | return frame_sp->GuessValueForAddress(address); |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 +0000 | [diff] [blame] | 1208 | } |