blob: e88b6467f7c6f077f6af00efc57ce36d1c77f688 [file] [log] [blame]
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001//===-- StopInfo.cpp --------------------------------------------*- C++ -*-===//
Greg Claytonf4b47e12010-08-04 01:40:35 +00002//
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 Claytonf4b47e12010-08-04 01:40:35 +000010// C Includes
11// C++ Includes
12#include <string>
13
14// Other libraries and framework includes
15// Project includes
Eugene Zelenko8f30a652015-10-23 18:39:37 +000016#include "lldb/Target/StopInfo.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000017#include "lldb/Core/Log.h"
Greg Clayton4e78f602010-11-18 18:52:36 +000018#include "lldb/Breakpoint/Breakpoint.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000019#include "lldb/Breakpoint/BreakpointLocation.h"
20#include "lldb/Breakpoint/StoppointCallbackContext.h"
Johnny Chen01a67862011-10-14 00:42:25 +000021#include "lldb/Breakpoint/Watchpoint.h"
Jim Ingham4b536182011-08-09 02:12:22 +000022#include "lldb/Core/Debugger.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000023#include "lldb/Core/StreamString.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000024#include "lldb/Core/ValueObject.h"
Jim Ingham151c0322015-09-15 21:13:50 +000025#include "lldb/Expression/UserExpression.h"
Jim Ingham4b536182011-08-09 02:12:22 +000026#include "lldb/Target/Target.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000027#include "lldb/Target/Thread.h"
28#include "lldb/Target/ThreadPlan.h"
29#include "lldb/Target/Process.h"
30#include "lldb/Target/UnixSignals.h"
31
32using namespace lldb;
33using namespace lldb_private;
34
35StopInfo::StopInfo (Thread &thread, uint64_t value) :
Greg Clayton46c2b6e2013-04-29 23:30:46 +000036 m_thread_wp (thread.shared_from_this()),
Greg Clayton1ac04c32012-02-21 00:09:25 +000037 m_stop_id (thread.GetProcess()->GetStopID()),
38 m_resume_id (thread.GetProcess()->GetResumeID()),
Jim Ingham0161b492013-02-09 01:29:05 +000039 m_value (value),
Jason Molenda0453be92015-05-15 00:19:28 +000040 m_description (),
Jim Ingham221d51c2013-05-08 00:35:16 +000041 m_override_should_notify (eLazyBoolCalculate),
Kuba Breckaafdf8422014-10-10 23:43:03 +000042 m_override_should_stop (eLazyBoolCalculate),
43 m_extended_info()
Greg Claytonf4b47e12010-08-04 01:40:35 +000044{
45}
46
47bool
48StopInfo::IsValid () const
49{
Greg Clayton46c2b6e2013-04-29 23:30:46 +000050 ThreadSP thread_sp (m_thread_wp.lock());
51 if (thread_sp)
52 return thread_sp->GetProcess()->GetStopID() == m_stop_id;
53 return false;
Greg Claytonf4b47e12010-08-04 01:40:35 +000054}
55
Jim Ingham77787032011-01-20 02:03:18 +000056void
57StopInfo::MakeStopInfoValid ()
58{
Greg Clayton46c2b6e2013-04-29 23:30:46 +000059 ThreadSP thread_sp (m_thread_wp.lock());
60 if (thread_sp)
61 {
62 m_stop_id = thread_sp->GetProcess()->GetStopID();
63 m_resume_id = thread_sp->GetProcess()->GetResumeID();
64 }
Jim Ingham77787032011-01-20 02:03:18 +000065}
66
Jim Ingham0faa43f2011-11-08 03:00:11 +000067bool
68StopInfo::HasTargetRunSinceMe ()
Jim Ingham4b536182011-08-09 02:12:22 +000069{
Greg Clayton46c2b6e2013-04-29 23:30:46 +000070 ThreadSP thread_sp (m_thread_wp.lock());
71
72 if (thread_sp)
Jim Ingham0faa43f2011-11-08 03:00:11 +000073 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +000074 lldb::StateType ret_type = thread_sp->GetProcess()->GetPrivateState();
75 if (ret_type == eStateRunning)
Jim Ingham0faa43f2011-11-08 03:00:11 +000076 {
77 return true;
78 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +000079 else if (ret_type == eStateStopped)
80 {
81 // This is a little tricky. We want to count "run and stopped again before you could
82 // ask this question as a "TRUE" answer to HasTargetRunSinceMe. But we don't want to
83 // include any running of the target done for expressions. So we track both resumes,
84 // and resumes caused by expressions, and check if there are any resumes NOT caused
85 // by expressions.
86
87 uint32_t curr_resume_id = thread_sp->GetProcess()->GetResumeID();
88 uint32_t last_user_expression_id = thread_sp->GetProcess()->GetLastUserExpressionResumeID ();
89 if (curr_resume_id == m_resume_id)
90 {
91 return false;
92 }
93 else if (curr_resume_id > last_user_expression_id)
94 {
95 return true;
96 }
97 }
Jim Ingham0faa43f2011-11-08 03:00:11 +000098 }
99 return false;
Jim Ingham4b536182011-08-09 02:12:22 +0000100}
101
Greg Claytonf4b47e12010-08-04 01:40:35 +0000102//----------------------------------------------------------------------
103// StopInfoBreakpoint
104//----------------------------------------------------------------------
105
Jim Ingham4b536182011-08-09 02:12:22 +0000106namespace lldb_private
107{
Greg Claytonf4b47e12010-08-04 01:40:35 +0000108class StopInfoBreakpoint : public StopInfo
109{
110public:
Greg Claytonf4b47e12010-08-04 01:40:35 +0000111 StopInfoBreakpoint (Thread &thread, break_id_t break_id) :
112 StopInfo (thread, break_id),
Greg Claytonf4b47e12010-08-04 01:40:35 +0000113 m_should_stop (false),
Jim Ingham0f16e732011-02-08 05:20:59 +0000114 m_should_stop_is_valid (false),
Jim Ingham52c7d202011-10-28 01:12:19 +0000115 m_should_perform_action (true),
Jim Inghamca36cd12012-10-05 19:16:31 +0000116 m_address (LLDB_INVALID_ADDRESS),
117 m_break_id(LLDB_INVALID_BREAK_ID),
118 m_was_one_shot (false)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000119 {
Jim Inghamca36cd12012-10-05 19:16:31 +0000120 StoreBPInfo();
Greg Claytonf4b47e12010-08-04 01:40:35 +0000121 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000122
Jim Ingham36f3b362010-10-14 23:45:03 +0000123 StopInfoBreakpoint (Thread &thread, break_id_t break_id, bool should_stop) :
124 StopInfo (thread, break_id),
Jim Ingham36f3b362010-10-14 23:45:03 +0000125 m_should_stop (should_stop),
Jim Ingham0f16e732011-02-08 05:20:59 +0000126 m_should_stop_is_valid (true),
Jim Ingham52c7d202011-10-28 01:12:19 +0000127 m_should_perform_action (true),
Jim Inghamca36cd12012-10-05 19:16:31 +0000128 m_address (LLDB_INVALID_ADDRESS),
129 m_break_id(LLDB_INVALID_BREAK_ID),
130 m_was_one_shot (false)
131 {
132 StoreBPInfo();
133 }
134
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000135 ~StopInfoBreakpoint() override = default;
136
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000137 void
138 StoreBPInfo ()
Jim Ingham36f3b362010-10-14 23:45:03 +0000139 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000140 ThreadSP thread_sp (m_thread_wp.lock());
141 if (thread_sp)
Jim Ingham52c7d202011-10-28 01:12:19 +0000142 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000143 BreakpointSiteSP bp_site_sp (thread_sp->GetProcess()->GetBreakpointSiteList().FindByID (m_value));
144 if (bp_site_sp)
Jim Inghamca36cd12012-10-05 19:16:31 +0000145 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000146 if (bp_site_sp->GetNumberOfOwners() == 1)
Jim Inghamca36cd12012-10-05 19:16:31 +0000147 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000148 BreakpointLocationSP bp_loc_sp = bp_site_sp->GetOwnerAtIndex(0);
149 if (bp_loc_sp)
150 {
151 m_break_id = bp_loc_sp->GetBreakpoint().GetID();
152 m_was_one_shot = bp_loc_sp->GetBreakpoint().IsOneShot();
153 }
Jim Inghamca36cd12012-10-05 19:16:31 +0000154 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000155 m_address = bp_site_sp->GetLoadAddress();
Jim Inghamca36cd12012-10-05 19:16:31 +0000156 }
Jim Ingham52c7d202011-10-28 01:12:19 +0000157 }
Jim Ingham36f3b362010-10-14 23:45:03 +0000158 }
159
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000160 bool
161 IsValidForOperatingSystemThread(Thread &thread) override
Greg Claytonab745c22015-04-07 22:17:41 +0000162 {
163 ProcessSP process_sp (thread.GetProcess());
164 if (process_sp)
165 {
166 BreakpointSiteSP bp_site_sp (process_sp->GetBreakpointSiteList().FindByID (m_value));
167 if (bp_site_sp)
168 return bp_site_sp->ValidForThisThread (&thread);
169 }
170 return false;
171 }
172
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000173 StopReason
174 GetStopReason() const override
Greg Claytonf4b47e12010-08-04 01:40:35 +0000175 {
176 return eStopReasonBreakpoint;
177 }
178
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000179 bool
180 ShouldStopSynchronous(Event *event_ptr) override
Greg Claytonf4b47e12010-08-04 01:40:35 +0000181 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000182 ThreadSP thread_sp (m_thread_wp.lock());
183 if (thread_sp)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000184 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000185 if (!m_should_stop_is_valid)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000186 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000187 // Only check once if we should stop at a breakpoint
188 BreakpointSiteSP bp_site_sp (thread_sp->GetProcess()->GetBreakpointSiteList().FindByID (m_value));
189 if (bp_site_sp)
190 {
191 ExecutionContext exe_ctx (thread_sp->GetStackFrameAtIndex(0));
192 StoppointCallbackContext context (event_ptr, exe_ctx, true);
Jim Inghama672ece2014-10-22 01:54:17 +0000193 bp_site_sp->BumpHitCounts();
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000194 m_should_stop = bp_site_sp->ShouldStop (&context);
195 }
196 else
197 {
198 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Claytonf4b47e12010-08-04 01:40:35 +0000199
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000200 if (log)
201 log->Printf ("Process::%s could not find breakpoint site id: %" PRId64 "...", __FUNCTION__, m_value);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000202
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000203 m_should_stop = true;
204 }
205 m_should_stop_is_valid = true;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000206 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000207 return m_should_stop;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000208 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000209 return false;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000210 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000211
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000212 bool
213 DoShouldNotify(Event *event_ptr) override
Jim Inghamf57b4352012-11-10 02:08:14 +0000214 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000215 ThreadSP thread_sp (m_thread_wp.lock());
216 if (thread_sp)
Jim Inghamf57b4352012-11-10 02:08:14 +0000217 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000218 BreakpointSiteSP bp_site_sp (thread_sp->GetProcess()->GetBreakpointSiteList().FindByID (m_value));
219 if (bp_site_sp)
Jim Inghamf57b4352012-11-10 02:08:14 +0000220 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000221 bool all_internal = true;
222
223 for (uint32_t i = 0; i < bp_site_sp->GetNumberOfOwners(); i++)
Jim Inghamf57b4352012-11-10 02:08:14 +0000224 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000225 if (!bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint().IsInternal())
226 {
227 all_internal = false;
228 break;
229 }
Jim Inghamf57b4352012-11-10 02:08:14 +0000230 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000231 return all_internal == false;
Jim Inghamf57b4352012-11-10 02:08:14 +0000232 }
Jim Inghamf57b4352012-11-10 02:08:14 +0000233 }
234 return true;
235 }
236
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000237 const char *
238 GetDescription() override
Jim Inghamf57b4352012-11-10 02:08:14 +0000239 {
240 if (m_description.empty())
241 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000242 ThreadSP thread_sp (m_thread_wp.lock());
243 if (thread_sp)
Jim Inghamf57b4352012-11-10 02:08:14 +0000244 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000245 BreakpointSiteSP bp_site_sp (thread_sp->GetProcess()->GetBreakpointSiteList().FindByID (m_value));
246 if (bp_site_sp)
Jim Ingham29950772013-01-26 02:19:28 +0000247 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000248 StreamString strm;
249 // If we have just hit an internal breakpoint, and it has a kind description, print that instead of the
250 // full breakpoint printing:
251 if (bp_site_sp->IsInternal())
Jim Ingham29950772013-01-26 02:19:28 +0000252 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000253 size_t num_owners = bp_site_sp->GetNumberOfOwners();
254 for (size_t idx = 0; idx < num_owners; idx++)
Jim Ingham29950772013-01-26 02:19:28 +0000255 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000256 const char *kind = bp_site_sp->GetOwnerAtIndex(idx)->GetBreakpoint().GetBreakpointKind();
257 if (kind != NULL)
258 {
259 m_description.assign (kind);
260 return kind;
261 }
Jim Ingham29950772013-01-26 02:19:28 +0000262 }
263 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000264
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000265 strm.Printf("breakpoint ");
266 bp_site_sp->GetDescription(&strm, eDescriptionLevelBrief);
267 m_description.swap (strm.GetString());
Jim Ingham29950772013-01-26 02:19:28 +0000268 }
Jim Inghamf57b4352012-11-10 02:08:14 +0000269 else
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000270 {
271 StreamString strm;
272 if (m_break_id != LLDB_INVALID_BREAK_ID)
273 {
274 BreakpointSP break_sp = thread_sp->GetProcess()->GetTarget().GetBreakpointByID(m_break_id);
275 if (break_sp)
276 {
277 if (break_sp->IsInternal())
278 {
279 const char *kind = break_sp->GetBreakpointKind();
280 if (kind)
281 strm.Printf ("internal %s breakpoint(%d).", kind, m_break_id);
282 else
283 strm.Printf ("internal breakpoint(%d).", m_break_id);
284 }
285 else
286 {
287 strm.Printf ("breakpoint %d.", m_break_id);
288 }
289 }
290 else
291 {
292 if (m_was_one_shot)
293 strm.Printf ("one-shot breakpoint %d", m_break_id);
294 else
295 strm.Printf ("breakpoint %d which has been deleted.", m_break_id);
296 }
297 }
298 else if (m_address == LLDB_INVALID_ADDRESS)
299 strm.Printf("breakpoint site %" PRIi64 " which has been deleted - unknown address", m_value);
300 else
301 strm.Printf("breakpoint site %" PRIi64 " which has been deleted - was at 0x%" PRIx64, m_value, m_address);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000302
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000303 m_description.swap (strm.GetString());
304 }
Jim Inghamf57b4352012-11-10 02:08:14 +0000305 }
306 }
307 return m_description.c_str();
308 }
309
310protected:
Jim Ingham6d66ce62012-04-20 21:16:56 +0000311 bool
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000312 ShouldStop(Event *event_ptr) override
Jim Ingham6d66ce62012-04-20 21:16:56 +0000313 {
314 // This just reports the work done by PerformAction or the synchronous stop. It should
315 // only ever get called after they have had a chance to run.
316 assert (m_should_stop_is_valid);
317 return m_should_stop;
318 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000319
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000320 void
321 PerformAction(Event *event_ptr) override
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000322 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000323 if (!m_should_perform_action)
324 return;
325 m_should_perform_action = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000326
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000327 ThreadSP thread_sp (m_thread_wp.lock());
328
329 if (thread_sp)
Jim Ingham5cb9a182013-03-28 00:13:30 +0000330 {
Jim Inghamcca89952014-08-05 01:58:14 +0000331 Log *log = lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS | LIBLLDB_LOG_STEP);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000332
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000333 if (!thread_sp->IsValid())
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000334 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000335 // This shouldn't ever happen, but just in case, don't do more harm.
Jason Molenda3f032ff2013-08-06 23:08:59 +0000336 if (log)
337 {
338 log->Printf ("PerformAction got called with an invalid thread.");
339 }
Jim Ingham79ea1d82011-12-09 04:17:31 +0000340 m_should_stop = true;
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000341 m_should_stop_is_valid = true;
342 return;
Jim Ingham79ea1d82011-12-09 04:17:31 +0000343 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000344
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000345 BreakpointSiteSP bp_site_sp (thread_sp->GetProcess()->GetBreakpointSiteList().FindByID (m_value));
Jim Ingham46e9f9d2015-04-23 00:28:25 +0000346 std::unordered_set<break_id_t> precondition_breakpoints;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000347
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000348 if (bp_site_sp)
Jim Ingham79ea1d82011-12-09 04:17:31 +0000349 {
Jim Ingham9d1ccda2015-07-29 17:51:36 +0000350 // Let's copy the owners list out of the site and store them in a local list. That way if
351 // one of the breakpoint actions changes the site, then we won't be operating on a bad list.
352 BreakpointLocationCollection site_locations;
353 size_t num_owners = bp_site_sp->CopyOwnersList(site_locations);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000354
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000355 if (num_owners == 0)
356 {
357 m_should_stop = true;
Jim Ingham184e9812013-01-15 02:47:48 +0000358 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000359 else
Jim Ingham4b536182011-08-09 02:12:22 +0000360 {
Jim Ingham46e9f9d2015-04-23 00:28:25 +0000361 // We go through each location, and test first its precondition - this overrides everything. Note,
362 // we only do this once per breakpoint - not once per location...
363 // Then check the condition. If the condition says to stop,
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000364 // then we run the callback for that location. If that callback says to stop as well, then
365 // we set m_should_stop to true; we are going to stop.
366 // But we still want to give all the breakpoints whose conditions say we are going to stop a
367 // chance to run their callbacks.
368 // Of course if any callback restarts the target by putting "continue" in the callback, then
369 // we're going to restart, without running the rest of the callbacks. And in this case we will
370 // end up not stopping even if another location said we should stop. But that's better than not
371 // running all the callbacks.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000372
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000373 m_should_stop = false;
374
375 ExecutionContext exe_ctx (thread_sp->GetStackFrameAtIndex(0));
376 Process *process = exe_ctx.GetProcessPtr();
377 if (process->GetModIDRef().IsLastResumeForUserExpression())
Jim Ingham4b536182011-08-09 02:12:22 +0000378 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000379 // If we are in the middle of evaluating an expression, don't run asynchronous breakpoint commands or
380 // expressions. That could lead to infinite recursion if the command or condition re-calls the function
381 // with this breakpoint.
382 // TODO: We can keep a list of the breakpoints we've seen while running expressions in the nested
383 // PerformAction calls that can arise when the action runs a function that hits another breakpoint,
384 // and only stop running commands when we see the same breakpoint hit a second time.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000385
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000386 m_should_stop_is_valid = true;
387 if (log)
388 log->Printf ("StopInfoBreakpoint::PerformAction - Hit a breakpoint while running an expression,"
389 " not running commands to avoid recursion.");
390 bool ignoring_breakpoints = process->GetIgnoreBreakpointsInExpressions();
391 if (ignoring_breakpoints)
Jim Ingham4b536182011-08-09 02:12:22 +0000392 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000393 m_should_stop = false;
394 // Internal breakpoints will always stop.
395 for (size_t j = 0; j < num_owners; j++)
396 {
397 lldb::BreakpointLocationSP bp_loc_sp = bp_site_sp->GetOwnerAtIndex(j);
398 if (bp_loc_sp->GetBreakpoint().IsInternal())
399 {
400 m_should_stop = true;
401 break;
402 }
403 }
Jim Ingham4b536182011-08-09 02:12:22 +0000404 }
Sean Callanan3dbf3462013-04-19 07:09:15 +0000405 else
406 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000407 m_should_stop = true;
408 }
409 if (log)
410 log->Printf ("StopInfoBreakpoint::PerformAction - in expression, continuing: %s.",
411 m_should_stop ? "true" : "false");
412 process->GetTarget().GetDebugger().GetAsyncOutputStream()->Printf("Warning: hit breakpoint while "
413 "running function, skipping commands and conditions to prevent recursion.");
414 return;
415 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000416
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000417 StoppointCallbackContext context (event_ptr, exe_ctx, false);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000418
Jim Inghama672ece2014-10-22 01:54:17 +0000419 // For safety's sake let's also grab an extra reference to the breakpoint owners of the locations we're
420 // going to examine, since the locations are going to have to get back to their breakpoints, and the
421 // locations don't keep their owners alive. I'm just sticking the BreakpointSP's in a vector since
Jim Ingham9d1ccda2015-07-29 17:51:36 +0000422 // I'm only using it to locally increment their retain counts.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000423
Jim Inghama672ece2014-10-22 01:54:17 +0000424 std::vector<lldb::BreakpointSP> location_owners;
425
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000426 for (size_t j = 0; j < num_owners; j++)
Jim Inghama672ece2014-10-22 01:54:17 +0000427 {
Jim Ingham9d1ccda2015-07-29 17:51:36 +0000428 BreakpointLocationSP loc(site_locations.GetByIndex(j));
Jim Inghama672ece2014-10-22 01:54:17 +0000429 location_owners.push_back(loc->GetBreakpoint().shared_from_this());
430
431 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000432
433 for (size_t j = 0; j < num_owners; j++)
434 {
435 lldb::BreakpointLocationSP bp_loc_sp = site_locations.GetByIndex(j);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000436
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000437 // If another action disabled this breakpoint or its location, then don't run the actions.
438 if (!bp_loc_sp->IsEnabled() || !bp_loc_sp->GetBreakpoint().IsEnabled())
439 continue;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000440
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000441 // The breakpoint site may have many locations associated with it, not all of them valid for
442 // this thread. Skip the ones that aren't:
443 if (!bp_loc_sp->ValidForThisThread(thread_sp.get()))
Jim Inghama8b99ca2014-01-15 03:30:04 +0000444 {
445 if (log)
446 {
447 StreamString s;
448 bp_loc_sp->GetDescription(&s, eDescriptionLevelBrief);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000449 log->Printf ("Breakpoint %s hit on thread 0x%llx but it was not for this thread, continuing.",
450 s.GetData(),
451 static_cast<unsigned long long>(thread_sp->GetID()));
Jim Inghama8b99ca2014-01-15 03:30:04 +0000452 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000453 continue;
Jim Inghama8b99ca2014-01-15 03:30:04 +0000454 }
Jim Ingham46e9f9d2015-04-23 00:28:25 +0000455
456 // First run the precondition, but since the precondition is per breakpoint, only run it once
457 // per breakpoint.
458 std::pair<std::unordered_set<break_id_t>::iterator, bool> result
459 = precondition_breakpoints.insert(bp_loc_sp->GetBreakpoint().GetID());
460 if (!result.second)
461 continue;
462
463 bool precondition_result = bp_loc_sp->GetBreakpoint().EvaluatePrecondition(context);
464 if (!precondition_result)
465 continue;
466
467 // Next run the condition for the breakpoint. If that says we should stop, then we'll run
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000468 // the callback for the breakpoint. If the callback says we shouldn't stop that will win.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000469
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000470 if (bp_loc_sp->GetConditionText() != NULL)
471 {
472 Error condition_error;
473 bool condition_says_stop = bp_loc_sp->ConditionSaysStop(exe_ctx, condition_error);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000474
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000475 if (!condition_error.Success())
476 {
477 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
478 StreamSP error_sp = debugger.GetAsyncErrorStream ();
479 error_sp->Printf ("Stopped due to an error evaluating condition of breakpoint ");
480 bp_loc_sp->GetDescription (error_sp.get(), eDescriptionLevelBrief);
481 error_sp->Printf (": \"%s\"",
482 bp_loc_sp->GetConditionText());
483 error_sp->EOL();
484 const char *err_str = condition_error.AsCString("<Unknown Error>");
485 if (log)
486 log->Printf("Error evaluating condition: \"%s\"\n", err_str);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000487
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000488 error_sp->PutCString (err_str);
489 error_sp->EOL();
490 error_sp->Flush();
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000491 }
492 else
493 {
Jim Inghama8b99ca2014-01-15 03:30:04 +0000494 if (log)
495 {
496 StreamString s;
497 bp_loc_sp->GetDescription(&s, eDescriptionLevelBrief);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000498 log->Printf ("Condition evaluated for breakpoint %s on thread 0x%llx conditon_says_stop: %i.",
499 s.GetData(),
500 static_cast<unsigned long long>(thread_sp->GetID()),
501 condition_says_stop);
Jim Inghama8b99ca2014-01-15 03:30:04 +0000502 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000503 if (!condition_says_stop)
Jim Inghamd762df82015-01-15 01:41:04 +0000504 {
505 // We don't want to increment the hit count of breakpoints if the condition fails.
506 // We've already bumped it by the time we get here, so undo the bump:
507 bp_loc_sp->UndoBumpHitCount();
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000508 continue;
Jim Inghamd762df82015-01-15 01:41:04 +0000509 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000510 }
511 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000512
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000513 bool callback_says_stop;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000514
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000515 // FIXME: For now the callbacks have to run in async mode - the first time we restart we need
516 // to get out of there. So set it here.
517 // When we figure out how to nest breakpoint hits then this will change.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000518
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000519 Debugger &debugger = thread_sp->CalculateTarget()->GetDebugger();
520 bool old_async = debugger.GetAsyncExecution();
521 debugger.SetAsyncExecution (true);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000522
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000523 callback_says_stop = bp_loc_sp->InvokeCallback (&context);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000524
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000525 debugger.SetAsyncExecution (old_async);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000526
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000527 if (callback_says_stop)
528 m_should_stop = true;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000529
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000530 // If we are going to stop for this breakpoint, then remove the breakpoint.
531 if (callback_says_stop && bp_loc_sp && bp_loc_sp->GetBreakpoint().IsOneShot())
532 {
533 thread_sp->GetProcess()->GetTarget().RemoveBreakpointByID (bp_loc_sp->GetBreakpoint().GetID());
534 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000535
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000536 // Also make sure that the callback hasn't continued the target.
537 // If it did, when we'll set m_should_start to false and get out of here.
538 if (HasTargetRunSinceMe ())
539 {
540 m_should_stop = false;
541 break;
Sean Callanan3dbf3462013-04-19 07:09:15 +0000542 }
Jim Ingham4b536182011-08-09 02:12:22 +0000543 }
Jim Ingham4b536182011-08-09 02:12:22 +0000544 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000545 // We've figured out what this stop wants to do, so mark it as valid so we don't compute it again.
546 m_should_stop_is_valid = true;
547
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000548 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000549 else
550 {
551 m_should_stop = true;
552 m_should_stop_is_valid = true;
553 Log * log_process(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Jim Ingham79ea1d82011-12-09 04:17:31 +0000554
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000555 if (log_process)
556 log_process->Printf ("Process::%s could not find breakpoint site id: %" PRId64 "...", __FUNCTION__, m_value);
557 }
558 if (log)
559 log->Printf ("Process::%s returning from action with m_should_stop: %d.", __FUNCTION__, m_should_stop);
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000560 }
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000561 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000562
563private:
Greg Claytonf4b47e12010-08-04 01:40:35 +0000564 bool m_should_stop;
565 bool m_should_stop_is_valid;
Jim Ingham0f16e732011-02-08 05:20:59 +0000566 bool m_should_perform_action; // Since we are trying to preserve the "state" of the system even if we run functions
567 // etc. behind the users backs, we need to make sure we only REALLY perform the action once.
Jim Ingham52c7d202011-10-28 01:12:19 +0000568 lldb::addr_t m_address; // We use this to capture the breakpoint site address when we create the StopInfo,
569 // in case somebody deletes it between the time the StopInfo is made and the
570 // description is asked for.
Jim Inghamca36cd12012-10-05 19:16:31 +0000571 lldb::break_id_t m_break_id;
572 bool m_was_one_shot;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000573};
574
Greg Claytonf4b47e12010-08-04 01:40:35 +0000575//----------------------------------------------------------------------
576// StopInfoWatchpoint
577//----------------------------------------------------------------------
578
579class StopInfoWatchpoint : public StopInfo
580{
581public:
Jim Inghamf57b4352012-11-10 02:08:14 +0000582 // Make sure watchpoint is properly disabled and subsequently enabled while performing watchpoint actions.
583 class WatchpointSentry {
584 public:
585 WatchpointSentry(Process *p, Watchpoint *w):
586 process(p),
587 watchpoint(w)
588 {
589 if (process && watchpoint)
590 {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000591 const bool notify = false;
Jim Inghamf57b4352012-11-10 02:08:14 +0000592 watchpoint->TurnOnEphemeralMode();
Jim Ingham1b5792e2012-12-18 02:03:49 +0000593 process->DisableWatchpoint(watchpoint, notify);
Jim Inghamf57b4352012-11-10 02:08:14 +0000594 }
595 }
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000596
Jim Inghamf57b4352012-11-10 02:08:14 +0000597 ~WatchpointSentry()
598 {
599 if (process && watchpoint)
600 {
601 if (!watchpoint->IsDisabledDuringEphemeralMode())
Jim Ingham1b5792e2012-12-18 02:03:49 +0000602 {
603 const bool notify = false;
604 process->EnableWatchpoint(watchpoint, notify);
605 }
Jim Inghamf57b4352012-11-10 02:08:14 +0000606 watchpoint->TurnOffEphemeralMode();
607 }
608 }
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000609
Jim Inghamf57b4352012-11-10 02:08:14 +0000610 private:
611 Process *process;
612 Watchpoint *watchpoint;
613 };
Greg Claytonf4b47e12010-08-04 01:40:35 +0000614
Jaydeep Patil83143502015-08-13 03:44:09 +0000615 StopInfoWatchpoint (Thread &thread, break_id_t watch_id, lldb::addr_t watch_hit_addr) :
Johnny Chenfd158f42011-09-21 22:47:15 +0000616 StopInfo(thread, watch_id),
Johnny Chenfd158f42011-09-21 22:47:15 +0000617 m_should_stop(false),
Jaydeep Patil83143502015-08-13 03:44:09 +0000618 m_should_stop_is_valid(false),
619 m_watch_hit_addr(watch_hit_addr)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000620 {
621 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000622
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000623 ~StopInfoWatchpoint() override = default;
624
625 StopReason
626 GetStopReason() const override
Greg Claytonf4b47e12010-08-04 01:40:35 +0000627 {
628 return eStopReasonWatchpoint;
629 }
630
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000631 const char *
632 GetDescription() override
Jim Inghamf57b4352012-11-10 02:08:14 +0000633 {
634 if (m_description.empty())
635 {
636 StreamString strm;
Daniel Malead01b2952012-11-29 21:49:15 +0000637 strm.Printf("watchpoint %" PRIi64, m_value);
Jim Inghamf57b4352012-11-10 02:08:14 +0000638 m_description.swap (strm.GetString());
639 }
640 return m_description.c_str();
641 }
642
643protected:
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000644 bool
645 ShouldStopSynchronous(Event *event_ptr) override
Johnny Chenfd158f42011-09-21 22:47:15 +0000646 {
647 // ShouldStop() method is idempotent and should not affect hit count.
Johnny Chen16dcf712011-10-17 18:58:00 +0000648 // See Process::RunPrivateStateThread()->Process()->HandlePrivateEvent()
649 // -->Process()::ShouldBroadcastEvent()->ThreadList::ShouldStop()->
650 // Thread::ShouldStop()->ThreadPlanBase::ShouldStop()->
651 // StopInfoWatchpoint::ShouldStop() and
652 // Event::DoOnRemoval()->Process::ProcessEventData::DoOnRemoval()->
653 // StopInfoWatchpoint::PerformAction().
Johnny Chenfd158f42011-09-21 22:47:15 +0000654 if (m_should_stop_is_valid)
655 return m_should_stop;
656
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000657 ThreadSP thread_sp (m_thread_wp.lock());
658 if (thread_sp)
Johnny Chenfd158f42011-09-21 22:47:15 +0000659 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000660 WatchpointSP wp_sp (thread_sp->CalculateTarget()->GetWatchpointList().FindByID(GetValue()));
661 if (wp_sp)
662 {
663 // Check if we should stop at a watchpoint.
664 ExecutionContext exe_ctx (thread_sp->GetStackFrameAtIndex(0));
665 StoppointCallbackContext context (event_ptr, exe_ctx, true);
666 m_should_stop = wp_sp->ShouldStop (&context);
667 }
668 else
669 {
670 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Johnny Chenfd158f42011-09-21 22:47:15 +0000671
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000672 if (log)
673 log->Printf ("Process::%s could not find watchpoint location id: %" PRId64 "...",
674 __FUNCTION__, GetValue());
Johnny Chenfd158f42011-09-21 22:47:15 +0000675
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000676 m_should_stop = true;
677 }
Johnny Chenfd158f42011-09-21 22:47:15 +0000678 }
679 m_should_stop_is_valid = true;
680 return m_should_stop;
681 }
682
Jim Ingham9b620f32013-02-22 21:23:43 +0000683 bool
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000684 ShouldStop(Event *event_ptr) override
Jim Ingham9b620f32013-02-22 21:23:43 +0000685 {
686 // This just reports the work done by PerformAction or the synchronous stop. It should
687 // only ever get called after they have had a chance to run.
688 assert (m_should_stop_is_valid);
689 return m_should_stop;
690 }
691
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000692 void
693 PerformAction(Event *event_ptr) override
Johnny Chen16dcf712011-10-17 18:58:00 +0000694 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000695 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS);
Johnny Chen16dcf712011-10-17 18:58:00 +0000696 // We're going to calculate if we should stop or not in some way during the course of
697 // this code. Also by default we're going to stop, so set that here.
698 m_should_stop = true;
699
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000700 ThreadSP thread_sp (m_thread_wp.lock());
701 if (thread_sp)
Johnny Chen16dcf712011-10-17 18:58:00 +0000702 {
Johnny Chen0f7ad8d2012-08-21 22:06:34 +0000703
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000704 WatchpointSP wp_sp (thread_sp->CalculateTarget()->GetWatchpointList().FindByID(GetValue()));
705 if (wp_sp)
Enrico Granataf04a2192012-07-13 23:18:48 +0000706 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000707 ExecutionContext exe_ctx (thread_sp->GetStackFrameAtIndex(0));
708 Process* process = exe_ctx.GetProcessPtr();
709
710 // This sentry object makes sure the current watchpoint is disabled while performing watchpoint actions,
711 // and it is then enabled after we are finished.
712 WatchpointSentry sentry(process, wp_sp.get());
713
Enrico Granataf04a2192012-07-13 23:18:48 +0000714 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000715 // check if this process is running on an architecture where watchpoints trigger
716 // before the associated instruction runs. if so, disable the WP, single-step and then
717 // re-enable the watchpoint
718 if (process)
Enrico Granataf04a2192012-07-13 23:18:48 +0000719 {
Jason Molendafe806902013-05-04 00:39:52 +0000720 uint32_t num;
721 bool wp_triggers_after;
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000722 if (process->GetWatchpointSupportInfo(num, wp_triggers_after).Success())
Enrico Granataf04a2192012-07-13 23:18:48 +0000723 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000724 if (!wp_triggers_after)
725 {
Jim Ingham363ddd72013-07-02 21:12:44 +0000726 StopInfoSP stored_stop_info_sp = thread_sp->GetStopInfo();
727 assert (stored_stop_info_sp.get() == this);
728
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000729 ThreadPlanSP new_plan_sp(thread_sp->QueueThreadPlanForStepSingleInstruction(false, // step-over
Greg Claytondc6224e2014-10-21 01:00:42 +0000730 false, // abort_other_plans
731 true)); // stop_other_threads
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000732 new_plan_sp->SetIsMasterPlan (true);
733 new_plan_sp->SetOkayToDiscard (false);
734 new_plan_sp->SetPrivate (true);
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000735 process->GetThreadList().SetSelectedThreadByID (thread_sp->GetID());
Greg Claytondc6224e2014-10-21 01:00:42 +0000736 process->ResumeSynchronous(NULL);
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000737 process->GetThreadList().SetSelectedThreadByID (thread_sp->GetID());
Jim Ingham363ddd72013-07-02 21:12:44 +0000738 thread_sp->SetStopInfo(stored_stop_info_sp);
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000739 }
Enrico Granataf04a2192012-07-13 23:18:48 +0000740 }
741 }
742 }
Johnny Chen209bd652012-08-13 21:09:54 +0000743
Jaydeep Patil83143502015-08-13 03:44:09 +0000744 /*
745 * MIPS: Last 3bits of the watchpoint address are masked by the kernel. For example:
746 * 'n' is at 0x120010d00 and 'm' is 0x120010d04. When a watchpoint is set at 'm', then
747 * watch exception is generated even when 'n' is read/written. To handle this case,
748 * server emulates the instruction at PC and finds the base address of the load/store
749 * instruction and appends it in the description of the stop-info packet. If watchpoint
750 * is not set on this address by user then this do not stop.
751 */
752 if (m_watch_hit_addr != LLDB_INVALID_ADDRESS)
753 {
754 WatchpointSP wp_hit_sp = thread_sp->CalculateTarget()->GetWatchpointList().FindByAddress(m_watch_hit_addr);
755 if (!wp_hit_sp)
Mohit K. Bhakkadacdff162015-10-06 05:25:17 +0000756 {
Jaydeep Patil83143502015-08-13 03:44:09 +0000757 m_should_stop = false;
Mohit K. Bhakkadacdff162015-10-06 05:25:17 +0000758 wp_sp->IncrementFalseAlarmsAndReviseHitCount();
759 }
Jaydeep Patil83143502015-08-13 03:44:09 +0000760 }
Mohit K. Bhakkadacdff162015-10-06 05:25:17 +0000761
Mohit K. Bhakkad13763c32015-11-03 09:04:33 +0000762 // TODO: This condition should be checked in the synchronous part of the watchpoint code
763 // (Watchpoint::ShouldStop), so that we avoid pulling an event even if the watchpoint fails
764 // the ignore count condition. It is moved here temporarily, because for archs with
765 // watchpoint_exceptions_received=before, the code in the previous lines takes care of moving
766 // the inferior to next PC. We have to check the ignore count condition after this is done,
767 // otherwise we will hit same watchpoint multiple times until we pass ignore condition, but we
768 // won't actually be ignoring them.
769 if (wp_sp->GetHitCount() <= wp_sp->GetIgnoreCount())
770 m_should_stop = false;
771
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000772 if (m_should_stop && wp_sp->GetConditionText() != NULL)
Johnny Chen16dcf712011-10-17 18:58:00 +0000773 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000774 // We need to make sure the user sees any parse errors in their condition, so we'll hook the
775 // constructor errors up to the debugger's Async I/O.
Jim Ingham1624a2d2014-05-05 02:26:40 +0000776 ExpressionResults result_code;
Greg Clayton62afb9f2013-11-04 19:35:17 +0000777 EvaluateExpressionOptions expr_options;
778 expr_options.SetUnwindOnError(true);
779 expr_options.SetIgnoreBreakpoints(true);
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000780 ValueObjectSP result_value_sp;
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000781 Error error;
Jim Ingham151c0322015-09-15 21:13:50 +0000782 result_code = UserExpression::Evaluate (exe_ctx,
783 expr_options,
784 wp_sp->GetConditionText(),
785 NULL,
786 result_value_sp,
787 error);
788
Jim Ingham8646d3c2014-05-05 02:47:44 +0000789 if (result_code == eExpressionCompleted)
Johnny Chen16dcf712011-10-17 18:58:00 +0000790 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000791 if (result_value_sp)
Johnny Chen16dcf712011-10-17 18:58:00 +0000792 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000793 Scalar scalar_value;
794 if (result_value_sp->ResolveValue (scalar_value))
Johnny Chen16dcf712011-10-17 18:58:00 +0000795 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000796 if (scalar_value.ULongLong(1) == 0)
797 {
798 // We have been vetoed. This takes precedence over querying
799 // the watchpoint whether it should stop (aka ignore count and
800 // friends). See also StopInfoWatchpoint::ShouldStop() as well
801 // as Process::ProcessEventData::DoOnRemoval().
802 m_should_stop = false;
803 }
804 else
805 m_should_stop = true;
806 if (log)
807 log->Printf("Condition successfully evaluated, result is %s.\n",
808 m_should_stop ? "true" : "false");
Johnny Chen16dcf712011-10-17 18:58:00 +0000809 }
810 else
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000811 {
Johnny Chen16dcf712011-10-17 18:58:00 +0000812 m_should_stop = true;
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000813 if (log)
814 log->Printf("Failed to get an integer result from the expression.");
815 }
Johnny Chen16dcf712011-10-17 18:58:00 +0000816 }
817 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000818 else
819 {
820 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
821 StreamSP error_sp = debugger.GetAsyncErrorStream ();
822 error_sp->Printf ("Stopped due to an error evaluating condition of watchpoint ");
823 wp_sp->GetDescription (error_sp.get(), eDescriptionLevelBrief);
824 error_sp->Printf (": \"%s\"",
825 wp_sp->GetConditionText());
826 error_sp->EOL();
827 const char *err_str = error.AsCString("<Unknown Error>");
828 if (log)
829 log->Printf("Error evaluating condition: \"%s\"\n", err_str);
830
831 error_sp->PutCString (err_str);
832 error_sp->EOL();
833 error_sp->Flush();
834 // If the condition fails to be parsed or run, we should stop.
835 m_should_stop = true;
836 }
Johnny Chen16dcf712011-10-17 18:58:00 +0000837 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000838
839 // If the condition says to stop, we run the callback to further decide whether to stop.
840 if (m_should_stop)
Johnny Chen16dcf712011-10-17 18:58:00 +0000841 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000842 StoppointCallbackContext context (event_ptr, exe_ctx, false);
843 bool stop_requested = wp_sp->InvokeCallback (&context);
844 // Also make sure that the callback hasn't continued the target.
845 // If it did, when we'll set m_should_stop to false and get out of here.
846 if (HasTargetRunSinceMe ())
847 m_should_stop = false;
848
849 if (m_should_stop && !stop_requested)
850 {
851 // We have been vetoed by the callback mechanism.
852 m_should_stop = false;
853 }
854 }
855 // Finally, if we are going to stop, print out the new & old values:
856 if (m_should_stop)
857 {
858 wp_sp->CaptureWatchedValue(exe_ctx);
859
Greg Clayton1ac04c32012-02-21 00:09:25 +0000860 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000861 StreamSP output_sp = debugger.GetAsyncOutputStream ();
862 wp_sp->DumpSnapshots(output_sp.get());
863 output_sp->EOL();
864 output_sp->Flush();
Johnny Chen16dcf712011-10-17 18:58:00 +0000865 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000866
Johnny Chen16dcf712011-10-17 18:58:00 +0000867 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000868 else
869 {
870 Log * log_process(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Johnny Chen13206412012-08-09 23:10:57 +0000871
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000872 if (log_process)
873 log_process->Printf ("Process::%s could not find watchpoint id: %" PRId64 "...", __FUNCTION__, m_value);
Johnny Chen13206412012-08-09 23:10:57 +0000874 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000875 if (log)
876 log->Printf ("Process::%s returning from action with m_should_stop: %d.", __FUNCTION__, m_should_stop);
Jim Inghama7dfb662012-10-23 07:20:06 +0000877
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000878 m_should_stop_is_valid = true;
Johnny Chen16dcf712011-10-17 18:58:00 +0000879 }
Johnny Chen16dcf712011-10-17 18:58:00 +0000880 }
881
Greg Claytonf4b47e12010-08-04 01:40:35 +0000882private:
Johnny Chenfd158f42011-09-21 22:47:15 +0000883 bool m_should_stop;
884 bool m_should_stop_is_valid;
Jaydeep Patil83143502015-08-13 03:44:09 +0000885 lldb::addr_t m_watch_hit_addr;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000886};
887
Greg Claytonf4b47e12010-08-04 01:40:35 +0000888//----------------------------------------------------------------------
889// StopInfoUnixSignal
890//----------------------------------------------------------------------
891
892class StopInfoUnixSignal : public StopInfo
893{
894public:
Pavel Labathc4e25c92015-05-29 10:13:03 +0000895 StopInfoUnixSignal (Thread &thread, int signo, const char *description) :
Greg Claytona658fd22011-06-04 01:26:29 +0000896 StopInfo (thread, signo)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000897 {
Pavel Labathc4e25c92015-05-29 10:13:03 +0000898 SetDescription (description);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000899 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000900
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000901 ~StopInfoUnixSignal() override = default;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000902
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000903 StopReason
904 GetStopReason() const override
Greg Claytonf4b47e12010-08-04 01:40:35 +0000905 {
906 return eStopReasonSignal;
907 }
908
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000909 bool
910 ShouldStopSynchronous(Event *event_ptr) override
Jim Ingham0161b492013-02-09 01:29:05 +0000911 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000912 ThreadSP thread_sp (m_thread_wp.lock());
913 if (thread_sp)
Chaoren Lin98d0a4b2015-07-14 01:09:28 +0000914 return thread_sp->GetProcess()->GetUnixSignals()->GetShouldStop(m_value);
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000915 return false;
Jim Ingham0161b492013-02-09 01:29:05 +0000916 }
917
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000918 bool
919 ShouldStop(Event *event_ptr) override
Greg Claytonf4b47e12010-08-04 01:40:35 +0000920 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000921 ThreadSP thread_sp (m_thread_wp.lock());
922 if (thread_sp)
Chaoren Lin98d0a4b2015-07-14 01:09:28 +0000923 return thread_sp->GetProcess()->GetUnixSignals()->GetShouldStop(m_value);
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000924 return false;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000925 }
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000926
Greg Claytonf4b47e12010-08-04 01:40:35 +0000927 // If should stop returns false, check if we should notify of this event
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000928 bool
929 DoShouldNotify(Event *event_ptr) override
Greg Claytonf4b47e12010-08-04 01:40:35 +0000930 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000931 ThreadSP thread_sp (m_thread_wp.lock());
932 if (thread_sp)
Jim Ingham0161b492013-02-09 01:29:05 +0000933 {
Chaoren Lin98d0a4b2015-07-14 01:09:28 +0000934 bool should_notify = thread_sp->GetProcess()->GetUnixSignals()->GetShouldNotify(m_value);
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000935 if (should_notify)
936 {
937 StreamString strm;
938 strm.Printf ("thread %d received signal: %s",
939 thread_sp->GetIndexID(),
Chaoren Lin98d0a4b2015-07-14 01:09:28 +0000940 thread_sp->GetProcess()->GetUnixSignals()->GetSignalAsCString(m_value));
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000941 Process::ProcessEventData::AddRestartedReason(event_ptr, strm.GetData());
942 }
943 return should_notify;
Jim Ingham0161b492013-02-09 01:29:05 +0000944 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000945 return true;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000946 }
947
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000948 void
949 WillResume(lldb::StateType resume_state) override
Greg Claytonf4b47e12010-08-04 01:40:35 +0000950 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000951 ThreadSP thread_sp (m_thread_wp.lock());
952 if (thread_sp)
953 {
Chaoren Lin98d0a4b2015-07-14 01:09:28 +0000954 if (thread_sp->GetProcess()->GetUnixSignals()->GetShouldSuppress(m_value) == false)
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000955 thread_sp->SetResumeSignal(m_value);
956 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000957 }
958
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000959 const char *
960 GetDescription() override
Greg Claytonf4b47e12010-08-04 01:40:35 +0000961 {
962 if (m_description.empty())
963 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000964 ThreadSP thread_sp (m_thread_wp.lock());
965 if (thread_sp)
966 {
967 StreamString strm;
Chaoren Lin98d0a4b2015-07-14 01:09:28 +0000968 const char *signal_name = thread_sp->GetProcess()->GetUnixSignals()->GetSignalAsCString(m_value);
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000969 if (signal_name)
970 strm.Printf("signal %s", signal_name);
971 else
972 strm.Printf("signal %" PRIi64, m_value);
973 m_description.swap (strm.GetString());
974 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000975 }
976 return m_description.c_str();
977 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000978};
979
980//----------------------------------------------------------------------
981// StopInfoTrace
982//----------------------------------------------------------------------
983
984class StopInfoTrace : public StopInfo
985{
986public:
Greg Claytonf4b47e12010-08-04 01:40:35 +0000987 StopInfoTrace (Thread &thread) :
988 StopInfo (thread, LLDB_INVALID_UID)
989 {
990 }
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000991
992 ~StopInfoTrace() override = default;
993
994 StopReason
995 GetStopReason() const override
Greg Claytonf4b47e12010-08-04 01:40:35 +0000996 {
997 return eStopReasonTrace;
998 }
999
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001000 const char *
1001 GetDescription() override
Greg Claytonf4b47e12010-08-04 01:40:35 +00001002 {
Greg Claytona658fd22011-06-04 01:26:29 +00001003 if (m_description.empty())
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001004 return "trace";
Greg Claytona658fd22011-06-04 01:26:29 +00001005 else
1006 return m_description.c_str();
1007 }
1008};
1009
Greg Claytona658fd22011-06-04 01:26:29 +00001010//----------------------------------------------------------------------
1011// StopInfoException
1012//----------------------------------------------------------------------
1013
1014class StopInfoException : public StopInfo
1015{
1016public:
Greg Claytona658fd22011-06-04 01:26:29 +00001017 StopInfoException (Thread &thread, const char *description) :
1018 StopInfo (thread, LLDB_INVALID_UID)
1019 {
1020 if (description)
1021 SetDescription (description);
1022 }
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001023
1024 ~StopInfoException() override = default;
1025
1026 StopReason
1027 GetStopReason() const override
Greg Claytona658fd22011-06-04 01:26:29 +00001028 {
1029 return eStopReasonException;
1030 }
1031
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001032 const char *
1033 GetDescription() override
Greg Claytona658fd22011-06-04 01:26:29 +00001034 {
1035 if (m_description.empty())
1036 return "exception";
1037 else
1038 return m_description.c_str();
Greg Claytonf4b47e12010-08-04 01:40:35 +00001039 }
1040};
1041
Greg Claytonf4b47e12010-08-04 01:40:35 +00001042//----------------------------------------------------------------------
1043// StopInfoThreadPlan
1044//----------------------------------------------------------------------
1045
1046class StopInfoThreadPlan : public StopInfo
1047{
1048public:
Sean Callananbc8ac342015-09-04 20:49:51 +00001049 StopInfoThreadPlan (ThreadPlanSP &plan_sp, ValueObjectSP &return_valobj_sp, ExpressionVariableSP &expression_variable_sp) :
Greg Claytonf4b47e12010-08-04 01:40:35 +00001050 StopInfo (plan_sp->GetThread(), LLDB_INVALID_UID),
Jim Ingham73ca05a2011-12-17 01:35:57 +00001051 m_plan_sp (plan_sp),
Jim Ingham30fadaf2014-07-08 01:07:32 +00001052 m_return_valobj_sp (return_valobj_sp),
1053 m_expression_variable_sp (expression_variable_sp)
Greg Claytonf4b47e12010-08-04 01:40:35 +00001054 {
1055 }
Greg Claytonf4b47e12010-08-04 01:40:35 +00001056
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001057 ~StopInfoThreadPlan() override = default;
1058
1059 StopReason
1060 GetStopReason() const override
Greg Claytonf4b47e12010-08-04 01:40:35 +00001061 {
1062 return eStopReasonPlanComplete;
1063 }
1064
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001065 const char *
1066 GetDescription() override
Greg Claytonf4b47e12010-08-04 01:40:35 +00001067 {
1068 if (m_description.empty())
1069 {
1070 StreamString strm;
1071 m_plan_sp->GetDescription (&strm, eDescriptionLevelBrief);
1072 m_description.swap (strm.GetString());
1073 }
1074 return m_description.c_str();
1075 }
Jim Ingham73ca05a2011-12-17 01:35:57 +00001076
1077 ValueObjectSP
1078 GetReturnValueObject()
1079 {
1080 return m_return_valobj_sp;
1081 }
Jim Ingham0161b492013-02-09 01:29:05 +00001082
Sean Callananbc8ac342015-09-04 20:49:51 +00001083 ExpressionVariableSP
Jim Ingham30fadaf2014-07-08 01:07:32 +00001084 GetExpressionVariable()
1085 {
1086 return m_expression_variable_sp;
1087 }
1088
Jim Ingham0161b492013-02-09 01:29:05 +00001089protected:
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001090 bool
1091 ShouldStop(Event *event_ptr) override
Jim Ingham0161b492013-02-09 01:29:05 +00001092 {
1093 if (m_plan_sp)
1094 return m_plan_sp->ShouldStop(event_ptr);
1095 else
1096 return StopInfo::ShouldStop(event_ptr);
1097 }
Greg Claytonf4b47e12010-08-04 01:40:35 +00001098
1099private:
1100 ThreadPlanSP m_plan_sp;
Jim Ingham73ca05a2011-12-17 01:35:57 +00001101 ValueObjectSP m_return_valobj_sp;
Sean Callananbc8ac342015-09-04 20:49:51 +00001102 ExpressionVariableSP m_expression_variable_sp;
Greg Claytonf4b47e12010-08-04 01:40:35 +00001103};
Greg Clayton90ba8112012-12-05 00:16:59 +00001104
1105class StopInfoExec : public StopInfo
1106{
1107public:
Greg Clayton90ba8112012-12-05 00:16:59 +00001108 StopInfoExec (Thread &thread) :
1109 StopInfo (thread, LLDB_INVALID_UID),
1110 m_performed_action (false)
1111 {
1112 }
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001113
1114 ~StopInfoExec() override = default;
1115
1116 StopReason
1117 GetStopReason() const override
Greg Clayton90ba8112012-12-05 00:16:59 +00001118 {
1119 return eStopReasonExec;
1120 }
1121
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001122 const char *
1123 GetDescription() override
Greg Clayton90ba8112012-12-05 00:16:59 +00001124 {
1125 return "exec";
1126 }
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001127
Greg Clayton90ba8112012-12-05 00:16:59 +00001128protected:
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001129 void
1130 PerformAction(Event *event_ptr) override
Greg Clayton90ba8112012-12-05 00:16:59 +00001131 {
1132 // Only perform the action once
1133 if (m_performed_action)
1134 return;
1135 m_performed_action = true;
Greg Clayton46c2b6e2013-04-29 23:30:46 +00001136 ThreadSP thread_sp (m_thread_wp.lock());
1137 if (thread_sp)
1138 thread_sp->GetProcess()->DidExec();
Greg Clayton90ba8112012-12-05 00:16:59 +00001139 }
1140
1141 bool m_performed_action;
1142};
1143
Jim Ingham4b536182011-08-09 02:12:22 +00001144} // namespace lldb_private
Greg Claytonf4b47e12010-08-04 01:40:35 +00001145
1146StopInfoSP
1147StopInfo::CreateStopReasonWithBreakpointSiteID (Thread &thread, break_id_t break_id)
1148{
1149 return StopInfoSP (new StopInfoBreakpoint (thread, break_id));
1150}
1151
1152StopInfoSP
Jim Ingham36f3b362010-10-14 23:45:03 +00001153StopInfo::CreateStopReasonWithBreakpointSiteID (Thread &thread, break_id_t break_id, bool should_stop)
1154{
1155 return StopInfoSP (new StopInfoBreakpoint (thread, break_id, should_stop));
1156}
1157
1158StopInfoSP
Jaydeep Patil83143502015-08-13 03:44:09 +00001159StopInfo::CreateStopReasonWithWatchpointID (Thread &thread, break_id_t watch_id, lldb::addr_t watch_hit_addr)
Greg Claytonf4b47e12010-08-04 01:40:35 +00001160{
Jaydeep Patil83143502015-08-13 03:44:09 +00001161 return StopInfoSP (new StopInfoWatchpoint (thread, watch_id, watch_hit_addr));
Greg Claytonf4b47e12010-08-04 01:40:35 +00001162}
1163
1164StopInfoSP
Pavel Labathc4e25c92015-05-29 10:13:03 +00001165StopInfo::CreateStopReasonWithSignal (Thread &thread, int signo, const char *description)
Greg Claytonf4b47e12010-08-04 01:40:35 +00001166{
Pavel Labathc4e25c92015-05-29 10:13:03 +00001167 return StopInfoSP (new StopInfoUnixSignal (thread, signo, description));
Greg Claytonf4b47e12010-08-04 01:40:35 +00001168}
1169
1170StopInfoSP
1171StopInfo::CreateStopReasonToTrace (Thread &thread)
1172{
1173 return StopInfoSP (new StopInfoTrace (thread));
1174}
1175
1176StopInfoSP
Jim Ingham30fadaf2014-07-08 01:07:32 +00001177StopInfo::CreateStopReasonWithPlan (ThreadPlanSP &plan_sp,
1178 ValueObjectSP return_valobj_sp,
Sean Callananbc8ac342015-09-04 20:49:51 +00001179 ExpressionVariableSP expression_variable_sp)
Greg Claytonf4b47e12010-08-04 01:40:35 +00001180{
Jim Ingham30fadaf2014-07-08 01:07:32 +00001181 return StopInfoSP (new StopInfoThreadPlan (plan_sp, return_valobj_sp, expression_variable_sp));
Greg Claytonf4b47e12010-08-04 01:40:35 +00001182}
Greg Claytona658fd22011-06-04 01:26:29 +00001183
1184StopInfoSP
1185StopInfo::CreateStopReasonWithException (Thread &thread, const char *description)
1186{
1187 return StopInfoSP (new StopInfoException (thread, description));
1188}
Jim Ingham73ca05a2011-12-17 01:35:57 +00001189
Greg Clayton90ba8112012-12-05 00:16:59 +00001190StopInfoSP
1191StopInfo::CreateStopReasonWithExec (Thread &thread)
1192{
1193 return StopInfoSP (new StopInfoExec (thread));
1194}
1195
Jim Ingham73ca05a2011-12-17 01:35:57 +00001196ValueObjectSP
1197StopInfo::GetReturnValueObject(StopInfoSP &stop_info_sp)
1198{
1199 if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonPlanComplete)
1200 {
1201 StopInfoThreadPlan *plan_stop_info = static_cast<StopInfoThreadPlan *>(stop_info_sp.get());
1202 return plan_stop_info->GetReturnValueObject();
1203 }
1204 else
1205 return ValueObjectSP();
1206}
Jim Ingham30fadaf2014-07-08 01:07:32 +00001207
Sean Callananbc8ac342015-09-04 20:49:51 +00001208ExpressionVariableSP
Jim Ingham30fadaf2014-07-08 01:07:32 +00001209StopInfo::GetExpressionVariable(StopInfoSP &stop_info_sp)
1210{
1211 if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonPlanComplete)
1212 {
1213 StopInfoThreadPlan *plan_stop_info = static_cast<StopInfoThreadPlan *>(stop_info_sp.get());
1214 return plan_stop_info->GetExpressionVariable();
1215 }
1216 else
Sean Callananbc8ac342015-09-04 20:49:51 +00001217 return ExpressionVariableSP();
Jim Ingham30fadaf2014-07-08 01:07:32 +00001218}