blob: 1537b4db2232ed158d3bab74634ac9d68258642f [file] [log] [blame]
Greg Clayton643ee732010-08-04 01:40:35 +00001//===-- StopInfo.cpp ---------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Target/StopInfo.h"
11
12// C Includes
13// C++ Includes
14#include <string>
15
16// Other libraries and framework includes
17// Project includes
18#include "lldb/Core/Log.h"
Greg Clayton640dc6b2010-11-18 18:52:36 +000019#include "lldb/Breakpoint/Breakpoint.h"
Greg Clayton643ee732010-08-04 01:40:35 +000020#include "lldb/Breakpoint/BreakpointLocation.h"
21#include "lldb/Breakpoint/StoppointCallbackContext.h"
Johnny Chenecd4feb2011-10-14 00:42:25 +000022#include "lldb/Breakpoint/Watchpoint.h"
Jim Ingham21f37ad2011-08-09 02:12:22 +000023#include "lldb/Core/Debugger.h"
Greg Clayton643ee732010-08-04 01:40:35 +000024#include "lldb/Core/StreamString.h"
Jim Ingham21f37ad2011-08-09 02:12:22 +000025#include "lldb/Expression/ClangUserExpression.h"
26#include "lldb/Target/Target.h"
Greg Clayton643ee732010-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) :
36 m_thread (thread),
Greg Claytonf4124de2012-02-21 00:09:25 +000037 m_stop_id (thread.GetProcess()->GetStopID()),
38 m_resume_id (thread.GetProcess()->GetResumeID()),
Greg Clayton643ee732010-08-04 01:40:35 +000039 m_value (value)
40{
41}
42
43bool
44StopInfo::IsValid () const
45{
Greg Claytonf4124de2012-02-21 00:09:25 +000046 return m_thread.GetProcess()->GetStopID() == m_stop_id;
Greg Clayton643ee732010-08-04 01:40:35 +000047}
48
Jim Ingham15dcb7c2011-01-20 02:03:18 +000049void
50StopInfo::MakeStopInfoValid ()
51{
Greg Claytonf4124de2012-02-21 00:09:25 +000052 m_stop_id = m_thread.GetProcess()->GetStopID();
53 m_resume_id = m_thread.GetProcess()->GetResumeID();
Jim Ingham15dcb7c2011-01-20 02:03:18 +000054}
55
Jim Ingham0296fe72011-11-08 03:00:11 +000056bool
57StopInfo::HasTargetRunSinceMe ()
Jim Ingham21f37ad2011-08-09 02:12:22 +000058{
Greg Claytonf4124de2012-02-21 00:09:25 +000059 lldb::StateType ret_type = m_thread.GetProcess()->GetPrivateState();
Jim Ingham0296fe72011-11-08 03:00:11 +000060 if (ret_type == eStateRunning)
61 {
62 return true;
63 }
64 else if (ret_type == eStateStopped)
65 {
66 // This is a little tricky. We want to count "run and stopped again before you could
67 // ask this question as a "TRUE" answer to HasTargetRunSinceMe. But we don't want to
68 // include any running of the target done for expressions. So we track both resumes,
69 // and resumes caused by expressions, and check if there are any resumes NOT caused
70 // by expressions.
71
Greg Claytonf4124de2012-02-21 00:09:25 +000072 uint32_t curr_resume_id = m_thread.GetProcess()->GetResumeID();
73 uint32_t last_user_expression_id = m_thread.GetProcess()->GetLastUserExpressionResumeID ();
Jim Ingham0296fe72011-11-08 03:00:11 +000074 if (curr_resume_id == m_resume_id)
75 {
76 return false;
77 }
78 else if (curr_resume_id > last_user_expression_id)
79 {
80 return true;
81 }
82 }
83 return false;
Jim Ingham21f37ad2011-08-09 02:12:22 +000084}
85
Greg Clayton643ee732010-08-04 01:40:35 +000086//----------------------------------------------------------------------
87// StopInfoBreakpoint
88//----------------------------------------------------------------------
89
Jim Ingham21f37ad2011-08-09 02:12:22 +000090namespace lldb_private
91{
Greg Clayton643ee732010-08-04 01:40:35 +000092class StopInfoBreakpoint : public StopInfo
93{
94public:
95
96 StopInfoBreakpoint (Thread &thread, break_id_t break_id) :
97 StopInfo (thread, break_id),
98 m_description(),
99 m_should_stop (false),
Jim Inghamf9f40c22011-02-08 05:20:59 +0000100 m_should_stop_is_valid (false),
Jim Ingham1c658532011-10-28 01:12:19 +0000101 m_should_perform_action (true),
Jim Ingham2753a022012-10-05 19:16:31 +0000102 m_address (LLDB_INVALID_ADDRESS),
103 m_break_id(LLDB_INVALID_BREAK_ID),
104 m_was_one_shot (false)
Greg Clayton643ee732010-08-04 01:40:35 +0000105 {
Jim Ingham2753a022012-10-05 19:16:31 +0000106 StoreBPInfo();
Greg Clayton643ee732010-08-04 01:40:35 +0000107 }
108
Jim Inghamd1686902010-10-14 23:45:03 +0000109 StopInfoBreakpoint (Thread &thread, break_id_t break_id, bool should_stop) :
110 StopInfo (thread, break_id),
111 m_description(),
112 m_should_stop (should_stop),
Jim Inghamf9f40c22011-02-08 05:20:59 +0000113 m_should_stop_is_valid (true),
Jim Ingham1c658532011-10-28 01:12:19 +0000114 m_should_perform_action (true),
Jim Ingham2753a022012-10-05 19:16:31 +0000115 m_address (LLDB_INVALID_ADDRESS),
116 m_break_id(LLDB_INVALID_BREAK_ID),
117 m_was_one_shot (false)
118 {
119 StoreBPInfo();
120 }
121
122 void StoreBPInfo ()
Jim Inghamd1686902010-10-14 23:45:03 +0000123 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000124 BreakpointSiteSP bp_site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (m_value));
Jim Ingham1c658532011-10-28 01:12:19 +0000125 if (bp_site_sp)
126 {
Jim Ingham2753a022012-10-05 19:16:31 +0000127 if (bp_site_sp->GetNumberOfOwners() == 1)
128 {
129 BreakpointLocationSP bp_loc_sp = bp_site_sp->GetOwnerAtIndex(0);
130 if (bp_loc_sp)
131 {
132 m_break_id = bp_loc_sp->GetBreakpoint().GetID();
133 m_was_one_shot = bp_loc_sp->GetBreakpoint().IsOneShot();
134 }
135 }
136 m_address = bp_site_sp->GetLoadAddress();
Jim Ingham1c658532011-10-28 01:12:19 +0000137 }
Jim Inghamd1686902010-10-14 23:45:03 +0000138 }
139
Greg Clayton643ee732010-08-04 01:40:35 +0000140 virtual ~StopInfoBreakpoint ()
141 {
142 }
143
144 virtual StopReason
145 GetStopReason () const
146 {
147 return eStopReasonBreakpoint;
148 }
149
150 virtual bool
Jim Inghame787c7e2012-04-20 21:16:56 +0000151 ShouldStopSynchronous (Event *event_ptr)
Greg Clayton643ee732010-08-04 01:40:35 +0000152 {
153 if (!m_should_stop_is_valid)
154 {
155 // Only check once if we should stop at a breakpoint
Greg Claytonf4124de2012-02-21 00:09:25 +0000156 BreakpointSiteSP bp_site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (m_value));
Greg Clayton643ee732010-08-04 01:40:35 +0000157 if (bp_site_sp)
158 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000159 ExecutionContext exe_ctx (m_thread.GetStackFrameAtIndex(0));
160 StoppointCallbackContext context (event_ptr, exe_ctx, true);
Greg Clayton643ee732010-08-04 01:40:35 +0000161 m_should_stop = bp_site_sp->ShouldStop (&context);
162 }
163 else
164 {
Greg Claytone005f2c2010-11-06 01:53:30 +0000165 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Clayton643ee732010-08-04 01:40:35 +0000166
167 if (log)
168 log->Printf ("Process::%s could not find breakpoint site id: %lld...", __FUNCTION__, m_value);
169
170 m_should_stop = true;
171 }
172 m_should_stop_is_valid = true;
173 }
174 return m_should_stop;
175 }
Jim Ingham6fb8baa2010-08-10 00:59:59 +0000176
Jim Inghame787c7e2012-04-20 21:16:56 +0000177 bool
178 ShouldStop (Event *event_ptr)
179 {
180 // This just reports the work done by PerformAction or the synchronous stop. It should
181 // only ever get called after they have had a chance to run.
182 assert (m_should_stop_is_valid);
183 return m_should_stop;
184 }
185
Jim Ingham6fb8baa2010-08-10 00:59:59 +0000186 virtual void
187 PerformAction (Event *event_ptr)
188 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000189 if (!m_should_perform_action)
190 return;
191 m_should_perform_action = false;
192
Jim Ingham21f37ad2011-08-09 02:12:22 +0000193 LogSP log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
Jim Ingham21f37ad2011-08-09 02:12:22 +0000194
Greg Claytonf4124de2012-02-21 00:09:25 +0000195 BreakpointSiteSP bp_site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (m_value));
Jim Ingham982a6ca2011-12-09 04:17:31 +0000196
Jim Ingham6fb8baa2010-08-10 00:59:59 +0000197 if (bp_site_sp)
198 {
199 size_t num_owners = bp_site_sp->GetNumberOfOwners();
Jim Ingham982a6ca2011-12-09 04:17:31 +0000200
201 if (num_owners == 0)
Jim Ingham6fb8baa2010-08-10 00:59:59 +0000202 {
Jim Ingham982a6ca2011-12-09 04:17:31 +0000203 m_should_stop = true;
204 }
205 else
206 {
207 // We go through each location, and test first its condition. If the condition says to stop,
208 // then we run the callback for that location. If that callback says to stop as well, then
209 // we set m_should_stop to true; we are going to stop.
210 // But we still want to give all the breakpoints whose conditions say we are going to stop a
211 // chance to run their callbacks.
212 // Of course if any callback restarts the target by putting "continue" in the callback, then
213 // we're going to restart, without running the rest of the callbacks. And in this case we will
214 // end up not stopping even if another location said we should stop. But that's better than not
215 // running all the callbacks.
216
217 m_should_stop = false;
218
Greg Claytonf4124de2012-02-21 00:09:25 +0000219 ExecutionContext exe_ctx (m_thread.GetStackFrameAtIndex(0));
220 StoppointCallbackContext context (event_ptr, exe_ctx, false);
Jim Ingham21f37ad2011-08-09 02:12:22 +0000221
Jim Ingham21f37ad2011-08-09 02:12:22 +0000222 for (size_t j = 0; j < num_owners; j++)
223 {
224 lldb::BreakpointLocationSP bp_loc_sp = bp_site_sp->GetOwnerAtIndex(j);
Jim Ingham982a6ca2011-12-09 04:17:31 +0000225
226 // First run the condition for the breakpoint. If that says we should stop, then we'll run
227 // the callback for the breakpoint. If the callback says we shouldn't stop that will win.
228
229 bool condition_says_stop = true;
Jim Ingham21f37ad2011-08-09 02:12:22 +0000230 if (bp_loc_sp->GetConditionText() != NULL)
231 {
232 // We need to make sure the user sees any parse errors in their condition, so we'll hook the
233 // constructor errors up to the debugger's Async I/O.
234
Jim Ingham21f37ad2011-08-09 02:12:22 +0000235 ValueObjectSP result_valobj_sp;
236
237 ExecutionResults result_code;
238 ValueObjectSP result_value_sp;
239 const bool discard_on_error = true;
240 Error error;
Greg Claytonf4124de2012-02-21 00:09:25 +0000241 result_code = ClangUserExpression::EvaluateWithError (exe_ctx,
Jim Inghame787c7e2012-04-20 21:16:56 +0000242 eExecutionPolicyOnlyWhenNeeded,
Sean Callanan5b658cc2011-11-07 23:35:40 +0000243 lldb::eLanguageTypeUnknown,
Sean Callanandaa6efe2011-12-21 22:22:58 +0000244 ClangUserExpression::eResultTypeAny,
Sean Callanan47dc4572011-09-15 02:13:07 +0000245 discard_on_error,
246 bp_loc_sp->GetConditionText(),
247 NULL,
248 result_value_sp,
249 error);
Jim Ingham21f37ad2011-08-09 02:12:22 +0000250 if (result_code == eExecutionCompleted)
251 {
252 if (result_value_sp)
253 {
254 Scalar scalar_value;
255 if (result_value_sp->ResolveValue (scalar_value))
256 {
257 if (scalar_value.ULongLong(1) == 0)
Jim Ingham982a6ca2011-12-09 04:17:31 +0000258 condition_says_stop = false;
Jim Ingham21f37ad2011-08-09 02:12:22 +0000259 else
Jim Ingham982a6ca2011-12-09 04:17:31 +0000260 condition_says_stop = true;
Jim Ingham21f37ad2011-08-09 02:12:22 +0000261 if (log)
262 log->Printf("Condition successfully evaluated, result is %s.\n",
263 m_should_stop ? "true" : "false");
264 }
265 else
266 {
Jim Ingham982a6ca2011-12-09 04:17:31 +0000267 condition_says_stop = true;
Jim Ingham21f37ad2011-08-09 02:12:22 +0000268 if (log)
269 log->Printf("Failed to get an integer result from the expression.");
270 }
271 }
272 }
273 else
274 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000275 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
Jim Ingham21f37ad2011-08-09 02:12:22 +0000276 StreamSP error_sp = debugger.GetAsyncErrorStream ();
277 error_sp->Printf ("Stopped due to an error evaluating condition of breakpoint ");
278 bp_loc_sp->GetDescription (error_sp.get(), eDescriptionLevelBrief);
279 error_sp->Printf (": \"%s\"",
280 bp_loc_sp->GetConditionText());
281 error_sp->EOL();
282 const char *err_str = error.AsCString("<Unknown Error>");
283 if (log)
284 log->Printf("Error evaluating condition: \"%s\"\n", err_str);
285
286 error_sp->PutCString (err_str);
287 error_sp->EOL();
288 error_sp->Flush();
289 // If the condition fails to be parsed or run, we should stop.
Jim Ingham982a6ca2011-12-09 04:17:31 +0000290 condition_says_stop = true;
Jim Ingham21f37ad2011-08-09 02:12:22 +0000291 }
292 }
293
Jim Ingham982a6ca2011-12-09 04:17:31 +0000294 // If this location's condition says we should aren't going to stop,
295 // then don't run the callback for this location.
296 if (!condition_says_stop)
297 continue;
298
299 bool callback_says_stop;
300
301 // FIXME: For now the callbacks have to run in async mode - the first time we restart we need
302 // to get out of there. So set it here.
303 // When we figure out how to nest breakpoint hits then this will change.
304
Greg Claytonf4124de2012-02-21 00:09:25 +0000305 Debugger &debugger = m_thread.CalculateTarget()->GetDebugger();
Jim Ingham982a6ca2011-12-09 04:17:31 +0000306 bool old_async = debugger.GetAsyncExecution();
307 debugger.SetAsyncExecution (true);
308
309 callback_says_stop = bp_loc_sp->InvokeCallback (&context);
310
311 debugger.SetAsyncExecution (old_async);
312
313 if (callback_says_stop)
314 m_should_stop = true;
Jim Ingham2753a022012-10-05 19:16:31 +0000315
316 // If we are going to stop for this breakpoint, then remove the breakpoint.
317 if (callback_says_stop && bp_loc_sp && bp_loc_sp->GetBreakpoint().IsOneShot())
318 {
319 m_thread.GetProcess()->GetTarget().RemoveBreakpointByID (bp_loc_sp->GetBreakpoint().GetID());
320 }
Jim Ingham982a6ca2011-12-09 04:17:31 +0000321
322 // Also make sure that the callback hasn't continued the target.
323 // If it did, when we'll set m_should_start to false and get out of here.
324 if (HasTargetRunSinceMe ())
325 {
326 m_should_stop = false;
Jim Ingham21f37ad2011-08-09 02:12:22 +0000327 break;
Jim Ingham982a6ca2011-12-09 04:17:31 +0000328 }
Jim Ingham21f37ad2011-08-09 02:12:22 +0000329 }
Jim Ingham6fb8baa2010-08-10 00:59:59 +0000330 }
Jim Ingham982a6ca2011-12-09 04:17:31 +0000331 // We've figured out what this stop wants to do, so mark it as valid so we don't compute it again.
332 m_should_stop_is_valid = true;
333
Jim Ingham6fb8baa2010-08-10 00:59:59 +0000334 }
335 else
336 {
Jim Ingham982a6ca2011-12-09 04:17:31 +0000337 m_should_stop = true;
338 m_should_stop_is_valid = true;
Jason Molendabf41e192012-10-04 22:47:07 +0000339 LogSP log_process(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Jim Ingham6fb8baa2010-08-10 00:59:59 +0000340
Jason Molendabf41e192012-10-04 22:47:07 +0000341 if (log_process)
342 log_process->Printf ("Process::%s could not find breakpoint site id: %lld...", __FUNCTION__, m_value);
Jim Ingham6fb8baa2010-08-10 00:59:59 +0000343 }
Jim Ingham21f37ad2011-08-09 02:12:22 +0000344 if (log)
345 log->Printf ("Process::%s returning from action with m_should_stop: %d.", __FUNCTION__, m_should_stop);
Jim Ingham6fb8baa2010-08-10 00:59:59 +0000346 }
Greg Clayton643ee732010-08-04 01:40:35 +0000347
348 virtual bool
349 ShouldNotify (Event *event_ptr)
350 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000351 BreakpointSiteSP bp_site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (m_value));
Greg Clayton643ee732010-08-04 01:40:35 +0000352 if (bp_site_sp)
353 {
354 bool all_internal = true;
355
356 for (uint32_t i = 0; i < bp_site_sp->GetNumberOfOwners(); i++)
357 {
358 if (!bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint().IsInternal())
359 {
360 all_internal = false;
361 break;
362 }
363 }
364 return all_internal == false;
365 }
366 return true;
367 }
368
369 virtual const char *
370 GetDescription ()
371 {
372 if (m_description.empty())
373 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000374 BreakpointSiteSP bp_site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (m_value));
Jim Ingham6fb8baa2010-08-10 00:59:59 +0000375 if (bp_site_sp)
376 {
377 StreamString strm;
378 strm.Printf("breakpoint ");
379 bp_site_sp->GetDescription(&strm, eDescriptionLevelBrief);
380 m_description.swap (strm.GetString());
381 }
382 else
383 {
384 StreamString strm;
Jim Ingham2753a022012-10-05 19:16:31 +0000385 if (m_break_id != LLDB_INVALID_BREAK_ID)
386 {
387 if (m_was_one_shot)
388 strm.Printf ("one-shot breakpoint %d", m_break_id);
389 else
390 strm.Printf ("breakpoint %d which has been deleted.", m_break_id);
391 }
392 else if (m_address == LLDB_INVALID_ADDRESS)
Jim Ingham1c658532011-10-28 01:12:19 +0000393 strm.Printf("breakpoint site %lli which has been deleted - unknown address", m_value);
394 else
395 strm.Printf("breakpoint site %lli which has been deleted - was at 0x%llx", m_value, m_address);
Jim Ingham2753a022012-10-05 19:16:31 +0000396
Jim Ingham6fb8baa2010-08-10 00:59:59 +0000397 m_description.swap (strm.GetString());
398 }
Greg Clayton643ee732010-08-04 01:40:35 +0000399 }
400 return m_description.c_str();
401 }
402
403private:
404 std::string m_description;
405 bool m_should_stop;
406 bool m_should_stop_is_valid;
Jim Inghamf9f40c22011-02-08 05:20:59 +0000407 bool m_should_perform_action; // Since we are trying to preserve the "state" of the system even if we run functions
408 // etc. behind the users backs, we need to make sure we only REALLY perform the action once.
Jim Ingham1c658532011-10-28 01:12:19 +0000409 lldb::addr_t m_address; // We use this to capture the breakpoint site address when we create the StopInfo,
410 // in case somebody deletes it between the time the StopInfo is made and the
411 // description is asked for.
Jim Ingham2753a022012-10-05 19:16:31 +0000412 lldb::break_id_t m_break_id;
413 bool m_was_one_shot;
Greg Clayton643ee732010-08-04 01:40:35 +0000414};
415
416
417//----------------------------------------------------------------------
418// StopInfoWatchpoint
419//----------------------------------------------------------------------
420
421class StopInfoWatchpoint : public StopInfo
422{
423public:
424
425 StopInfoWatchpoint (Thread &thread, break_id_t watch_id) :
Johnny Chen043f8c22011-09-21 22:47:15 +0000426 StopInfo(thread, watch_id),
427 m_description(),
428 m_should_stop(false),
429 m_should_stop_is_valid(false)
Greg Clayton643ee732010-08-04 01:40:35 +0000430 {
431 }
432
433 virtual ~StopInfoWatchpoint ()
434 {
435 }
436
437 virtual StopReason
438 GetStopReason () const
439 {
440 return eStopReasonWatchpoint;
441 }
442
Johnny Chen043f8c22011-09-21 22:47:15 +0000443 virtual bool
444 ShouldStop (Event *event_ptr)
445 {
446 // ShouldStop() method is idempotent and should not affect hit count.
Johnny Chen712a6282011-10-17 18:58:00 +0000447 // See Process::RunPrivateStateThread()->Process()->HandlePrivateEvent()
448 // -->Process()::ShouldBroadcastEvent()->ThreadList::ShouldStop()->
449 // Thread::ShouldStop()->ThreadPlanBase::ShouldStop()->
450 // StopInfoWatchpoint::ShouldStop() and
451 // Event::DoOnRemoval()->Process::ProcessEventData::DoOnRemoval()->
452 // StopInfoWatchpoint::PerformAction().
Johnny Chen043f8c22011-09-21 22:47:15 +0000453 if (m_should_stop_is_valid)
454 return m_should_stop;
455
Johnny Chenecd4feb2011-10-14 00:42:25 +0000456 WatchpointSP wp_sp =
Greg Claytonf4124de2012-02-21 00:09:25 +0000457 m_thread.CalculateTarget()->GetWatchpointList().FindByID(GetValue());
Johnny Chenecd4feb2011-10-14 00:42:25 +0000458 if (wp_sp)
Johnny Chen043f8c22011-09-21 22:47:15 +0000459 {
460 // Check if we should stop at a watchpoint.
Greg Claytonf4124de2012-02-21 00:09:25 +0000461 ExecutionContext exe_ctx (m_thread.GetStackFrameAtIndex(0));
462 StoppointCallbackContext context (event_ptr, exe_ctx, true);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000463 m_should_stop = wp_sp->ShouldStop (&context);
Johnny Chen043f8c22011-09-21 22:47:15 +0000464 }
465 else
466 {
467 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
468
469 if (log)
470 log->Printf ("Process::%s could not find watchpoint location id: %lld...",
471 __FUNCTION__, GetValue());
472
473 m_should_stop = true;
474 }
475 m_should_stop_is_valid = true;
476 return m_should_stop;
477 }
478
Johnny Chenbd446f12012-08-21 22:06:34 +0000479 // Make sure watchpoint is properly disabled and subsequently enabled while performing watchpoint actions.
480 class WatchpointSentry {
481 public:
482 WatchpointSentry(Process *p, Watchpoint *w):
483 process(p),
484 watchpoint(w)
485 {
486 if (process && watchpoint)
Johnny Chenf84e5662012-08-21 23:17:04 +0000487 {
488 watchpoint->TurnOnEphemeralMode();
Johnny Chenbd446f12012-08-21 22:06:34 +0000489 process->DisableWatchpoint(watchpoint);
Johnny Chenf84e5662012-08-21 23:17:04 +0000490 }
Johnny Chenbd446f12012-08-21 22:06:34 +0000491 }
492 ~WatchpointSentry()
493 {
494 if (process && watchpoint)
Johnny Chenf84e5662012-08-21 23:17:04 +0000495 {
Johnny Chen258db3a2012-08-23 22:28:26 +0000496 if (!watchpoint->IsDisabledDuringEphemeralMode())
497 process->EnableWatchpoint(watchpoint);
Johnny Chenf84e5662012-08-21 23:17:04 +0000498 watchpoint->TurnOffEphemeralMode();
499 }
Johnny Chenbd446f12012-08-21 22:06:34 +0000500 }
501 private:
502 Process *process;
503 Watchpoint *watchpoint;
504 };
505
Johnny Chen712a6282011-10-17 18:58:00 +0000506 // Perform any action that is associated with this stop. This is done as the
507 // Event is removed from the event queue.
508 virtual void
509 PerformAction (Event *event_ptr)
510 {
Johnny Chen9e985592012-08-13 21:09:54 +0000511 LogSP log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS);
Johnny Chen712a6282011-10-17 18:58:00 +0000512 // We're going to calculate if we should stop or not in some way during the course of
513 // this code. Also by default we're going to stop, so set that here.
514 m_should_stop = true;
515
516 WatchpointSP wp_sp =
Greg Claytonf4124de2012-02-21 00:09:25 +0000517 m_thread.CalculateTarget()->GetWatchpointList().FindByID(GetValue());
Johnny Chen712a6282011-10-17 18:58:00 +0000518 if (wp_sp)
519 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000520 ExecutionContext exe_ctx (m_thread.GetStackFrameAtIndex(0));
Johnny Chen9e985592012-08-13 21:09:54 +0000521 Process* process = exe_ctx.GetProcessPtr();
Johnny Chenbd446f12012-08-21 22:06:34 +0000522
Johnny Chenaf4e9662012-08-21 22:15:58 +0000523 // This sentry object makes sure the current watchpoint is disabled while performing watchpoint actions,
524 // and it is then enabled after we are finished.
Johnny Chenbd446f12012-08-21 22:06:34 +0000525 WatchpointSentry sentry(process, wp_sp.get());
526
Enrico Granata7de2a3b2012-07-13 23:18:48 +0000527 {
528 // check if this process is running on an architecture where watchpoints trigger
529 // before the associated instruction runs. if so, disable the WP, single-step and then
530 // re-enable the watchpoint
Enrico Granata7de2a3b2012-07-13 23:18:48 +0000531 if (process)
532 {
533 uint32_t num; bool wp_triggers_after;
534 if (process->GetWatchpointSupportInfo(num, wp_triggers_after).Success())
535 {
536 if (!wp_triggers_after)
537 {
Enrico Granata7de2a3b2012-07-13 23:18:48 +0000538 ThreadPlan *new_plan = m_thread.QueueThreadPlanForStepSingleInstruction(false, // step-over
539 false, // abort_other_plans
540 true); // stop_other_threads
541 new_plan->SetIsMasterPlan (true);
542 new_plan->SetOkayToDiscard (false);
543 process->GetThreadList().SetSelectedThreadByID (m_thread.GetID());
544 process->Resume ();
545 process->WaitForProcessToStop (NULL);
546 process->GetThreadList().SetSelectedThreadByID (m_thread.GetID());
547 MakeStopInfoValid(); // make sure we do not fail to stop because of the single-step taken above
Enrico Granata7de2a3b2012-07-13 23:18:48 +0000548 }
549 }
550 }
551 }
Johnny Chen9e985592012-08-13 21:09:54 +0000552
Johnny Chen712a6282011-10-17 18:58:00 +0000553 if (m_should_stop && wp_sp->GetConditionText() != NULL)
554 {
555 // We need to make sure the user sees any parse errors in their condition, so we'll hook the
556 // constructor errors up to the debugger's Async I/O.
Johnny Chen712a6282011-10-17 18:58:00 +0000557 ExecutionResults result_code;
558 ValueObjectSP result_value_sp;
559 const bool discard_on_error = true;
560 Error error;
Greg Claytonf4124de2012-02-21 00:09:25 +0000561 result_code = ClangUserExpression::EvaluateWithError (exe_ctx,
Sean Callanana539e5b2012-09-08 01:51:48 +0000562 eExecutionPolicyOnlyWhenNeeded,
Sean Callanan5b658cc2011-11-07 23:35:40 +0000563 lldb::eLanguageTypeUnknown,
Sean Callanandaa6efe2011-12-21 22:22:58 +0000564 ClangUserExpression::eResultTypeAny,
Johnny Chen712a6282011-10-17 18:58:00 +0000565 discard_on_error,
566 wp_sp->GetConditionText(),
567 NULL,
568 result_value_sp,
Enrico Granata6cca9692012-07-16 23:10:35 +0000569 error,
570 500000);
Johnny Chen712a6282011-10-17 18:58:00 +0000571 if (result_code == eExecutionCompleted)
572 {
573 if (result_value_sp)
574 {
575 Scalar scalar_value;
576 if (result_value_sp->ResolveValue (scalar_value))
577 {
578 if (scalar_value.ULongLong(1) == 0)
579 {
580 // We have been vetoed. This takes precedence over querying
581 // the watchpoint whether it should stop (aka ignore count and
582 // friends). See also StopInfoWatchpoint::ShouldStop() as well
583 // as Process::ProcessEventData::DoOnRemoval().
584 m_should_stop = false;
585 }
586 else
587 m_should_stop = true;
588 if (log)
589 log->Printf("Condition successfully evaluated, result is %s.\n",
590 m_should_stop ? "true" : "false");
591 }
592 else
593 {
594 m_should_stop = true;
595 if (log)
596 log->Printf("Failed to get an integer result from the expression.");
597 }
598 }
599 }
600 else
601 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000602 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
Johnny Chen712a6282011-10-17 18:58:00 +0000603 StreamSP error_sp = debugger.GetAsyncErrorStream ();
Johnny Chen0c5c43b2012-01-24 23:19:25 +0000604 error_sp->Printf ("Stopped due to an error evaluating condition of watchpoint ");
Johnny Chen712a6282011-10-17 18:58:00 +0000605 wp_sp->GetDescription (error_sp.get(), eDescriptionLevelBrief);
606 error_sp->Printf (": \"%s\"",
607 wp_sp->GetConditionText());
608 error_sp->EOL();
609 const char *err_str = error.AsCString("<Unknown Error>");
610 if (log)
611 log->Printf("Error evaluating condition: \"%s\"\n", err_str);
612
613 error_sp->PutCString (err_str);
614 error_sp->EOL();
615 error_sp->Flush();
616 // If the condition fails to be parsed or run, we should stop.
617 m_should_stop = true;
618 }
619 }
Johnny Chen97600c12012-08-09 23:10:57 +0000620
621 // If the condition says to stop, we run the callback to further decide whether to stop.
622 if (m_should_stop)
623 {
Johnny Chen9e985592012-08-13 21:09:54 +0000624 StoppointCallbackContext context (event_ptr, exe_ctx, false);
Johnny Chen97600c12012-08-09 23:10:57 +0000625 bool stop_requested = wp_sp->InvokeCallback (&context);
626 // Also make sure that the callback hasn't continued the target.
627 // If it did, when we'll set m_should_stop to false and get out of here.
628 if (HasTargetRunSinceMe ())
629 m_should_stop = false;
630
631 if (m_should_stop && !stop_requested)
632 {
633 // We have been vetoed by the callback mechanism.
634 m_should_stop = false;
635 }
636 }
Jim Ingham9e376622012-10-23 07:20:06 +0000637 // Finally, if we are going to stop, print out the new & old values:
638 if (m_should_stop)
639 {
640 wp_sp->CaptureWatchedValue(exe_ctx);
641
642 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
643 StreamSP output_sp = debugger.GetAsyncOutputStream ();
644 wp_sp->DumpSnapshots(output_sp.get());
645 output_sp->EOL();
646 output_sp->Flush();
647 }
648
Johnny Chen712a6282011-10-17 18:58:00 +0000649 }
650 else
651 {
Jason Molendabf41e192012-10-04 22:47:07 +0000652 LogSP log_process(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Johnny Chen712a6282011-10-17 18:58:00 +0000653
Jason Molendabf41e192012-10-04 22:47:07 +0000654 if (log_process)
655 log_process->Printf ("Process::%s could not find watchpoint id: %lld...", __FUNCTION__, m_value);
Johnny Chen712a6282011-10-17 18:58:00 +0000656 }
657 if (log)
658 log->Printf ("Process::%s returning from action with m_should_stop: %d.", __FUNCTION__, m_should_stop);
Jim Ingham9e376622012-10-23 07:20:06 +0000659
Johnny Chen712a6282011-10-17 18:58:00 +0000660 }
661
Greg Clayton643ee732010-08-04 01:40:35 +0000662 virtual const char *
663 GetDescription ()
664 {
665 if (m_description.empty())
666 {
667 StreamString strm;
668 strm.Printf("watchpoint %lli", m_value);
669 m_description.swap (strm.GetString());
670 }
671 return m_description.c_str();
672 }
673
Greg Clayton643ee732010-08-04 01:40:35 +0000674private:
675 std::string m_description;
Johnny Chen043f8c22011-09-21 22:47:15 +0000676 bool m_should_stop;
677 bool m_should_stop_is_valid;
Greg Clayton643ee732010-08-04 01:40:35 +0000678};
679
680
681
682//----------------------------------------------------------------------
683// StopInfoUnixSignal
684//----------------------------------------------------------------------
685
686class StopInfoUnixSignal : public StopInfo
687{
688public:
689
690 StopInfoUnixSignal (Thread &thread, int signo) :
Greg Clayton65611552011-06-04 01:26:29 +0000691 StopInfo (thread, signo)
Greg Clayton643ee732010-08-04 01:40:35 +0000692 {
693 }
694
695 virtual ~StopInfoUnixSignal ()
696 {
697 }
698
699
700 virtual StopReason
701 GetStopReason () const
702 {
703 return eStopReasonSignal;
704 }
705
706 virtual bool
707 ShouldStop (Event *event_ptr)
708 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000709 return m_thread.GetProcess()->GetUnixSignals().GetShouldStop (m_value);
Greg Clayton643ee732010-08-04 01:40:35 +0000710 }
711
712
713 // If should stop returns false, check if we should notify of this event
714 virtual bool
715 ShouldNotify (Event *event_ptr)
716 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000717 return m_thread.GetProcess()->GetUnixSignals().GetShouldNotify (m_value);
Greg Clayton643ee732010-08-04 01:40:35 +0000718 }
719
720
721 virtual void
722 WillResume (lldb::StateType resume_state)
723 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000724 if (m_thread.GetProcess()->GetUnixSignals().GetShouldSuppress(m_value) == false)
Greg Clayton643ee732010-08-04 01:40:35 +0000725 m_thread.SetResumeSignal(m_value);
726 }
727
728 virtual const char *
729 GetDescription ()
730 {
731 if (m_description.empty())
732 {
733 StreamString strm;
Greg Claytonf4124de2012-02-21 00:09:25 +0000734 const char *signal_name = m_thread.GetProcess()->GetUnixSignals().GetSignalAsCString (m_value);
Greg Clayton643ee732010-08-04 01:40:35 +0000735 if (signal_name)
Greg Claytona830adb2010-10-04 01:05:56 +0000736 strm.Printf("signal %s", signal_name);
Greg Clayton643ee732010-08-04 01:40:35 +0000737 else
Greg Claytona830adb2010-10-04 01:05:56 +0000738 strm.Printf("signal %lli", m_value);
Greg Clayton643ee732010-08-04 01:40:35 +0000739 m_description.swap (strm.GetString());
740 }
741 return m_description.c_str();
742 }
Greg Clayton643ee732010-08-04 01:40:35 +0000743};
744
745//----------------------------------------------------------------------
746// StopInfoTrace
747//----------------------------------------------------------------------
748
749class StopInfoTrace : public StopInfo
750{
751public:
752
753 StopInfoTrace (Thread &thread) :
754 StopInfo (thread, LLDB_INVALID_UID)
755 {
756 }
757
758 virtual ~StopInfoTrace ()
759 {
760 }
761
762 virtual StopReason
763 GetStopReason () const
764 {
765 return eStopReasonTrace;
766 }
767
768 virtual const char *
769 GetDescription ()
770 {
Greg Clayton65611552011-06-04 01:26:29 +0000771 if (m_description.empty())
Greg Clayton643ee732010-08-04 01:40:35 +0000772 return "trace";
Greg Clayton65611552011-06-04 01:26:29 +0000773 else
774 return m_description.c_str();
775 }
776};
777
778
779//----------------------------------------------------------------------
780// StopInfoException
781//----------------------------------------------------------------------
782
783class StopInfoException : public StopInfo
784{
785public:
786
787 StopInfoException (Thread &thread, const char *description) :
788 StopInfo (thread, LLDB_INVALID_UID)
789 {
790 if (description)
791 SetDescription (description);
792 }
793
794 virtual
795 ~StopInfoException ()
796 {
797 }
798
799 virtual StopReason
800 GetStopReason () const
801 {
802 return eStopReasonException;
803 }
804
805 virtual const char *
806 GetDescription ()
807 {
808 if (m_description.empty())
809 return "exception";
810 else
811 return m_description.c_str();
Greg Clayton643ee732010-08-04 01:40:35 +0000812 }
813};
814
815
816//----------------------------------------------------------------------
817// StopInfoThreadPlan
818//----------------------------------------------------------------------
819
820class StopInfoThreadPlan : public StopInfo
821{
822public:
823
Jim Ingham1586d972011-12-17 01:35:57 +0000824 StopInfoThreadPlan (ThreadPlanSP &plan_sp, ValueObjectSP &return_valobj_sp) :
Greg Clayton643ee732010-08-04 01:40:35 +0000825 StopInfo (plan_sp->GetThread(), LLDB_INVALID_UID),
Jim Ingham1586d972011-12-17 01:35:57 +0000826 m_plan_sp (plan_sp),
827 m_return_valobj_sp (return_valobj_sp)
Greg Clayton643ee732010-08-04 01:40:35 +0000828 {
829 }
830
831 virtual ~StopInfoThreadPlan ()
832 {
833 }
834
835 virtual StopReason
836 GetStopReason () const
837 {
838 return eStopReasonPlanComplete;
839 }
840
841 virtual const char *
842 GetDescription ()
843 {
844 if (m_description.empty())
845 {
846 StreamString strm;
847 m_plan_sp->GetDescription (&strm, eDescriptionLevelBrief);
848 m_description.swap (strm.GetString());
849 }
850 return m_description.c_str();
851 }
Jim Ingham1586d972011-12-17 01:35:57 +0000852
853 ValueObjectSP
854 GetReturnValueObject()
855 {
856 return m_return_valobj_sp;
857 }
Greg Clayton643ee732010-08-04 01:40:35 +0000858
859private:
860 ThreadPlanSP m_plan_sp;
Jim Ingham1586d972011-12-17 01:35:57 +0000861 ValueObjectSP m_return_valobj_sp;
Greg Clayton643ee732010-08-04 01:40:35 +0000862};
Jim Ingham21f37ad2011-08-09 02:12:22 +0000863} // namespace lldb_private
Greg Clayton643ee732010-08-04 01:40:35 +0000864
865StopInfoSP
866StopInfo::CreateStopReasonWithBreakpointSiteID (Thread &thread, break_id_t break_id)
867{
868 return StopInfoSP (new StopInfoBreakpoint (thread, break_id));
869}
870
871StopInfoSP
Jim Inghamd1686902010-10-14 23:45:03 +0000872StopInfo::CreateStopReasonWithBreakpointSiteID (Thread &thread, break_id_t break_id, bool should_stop)
873{
874 return StopInfoSP (new StopInfoBreakpoint (thread, break_id, should_stop));
875}
876
877StopInfoSP
Greg Clayton643ee732010-08-04 01:40:35 +0000878StopInfo::CreateStopReasonWithWatchpointID (Thread &thread, break_id_t watch_id)
879{
880 return StopInfoSP (new StopInfoWatchpoint (thread, watch_id));
881}
882
883StopInfoSP
884StopInfo::CreateStopReasonWithSignal (Thread &thread, int signo)
885{
886 return StopInfoSP (new StopInfoUnixSignal (thread, signo));
887}
888
889StopInfoSP
890StopInfo::CreateStopReasonToTrace (Thread &thread)
891{
892 return StopInfoSP (new StopInfoTrace (thread));
893}
894
895StopInfoSP
Jim Ingham1586d972011-12-17 01:35:57 +0000896StopInfo::CreateStopReasonWithPlan (ThreadPlanSP &plan_sp, ValueObjectSP return_valobj_sp)
Greg Clayton643ee732010-08-04 01:40:35 +0000897{
Jim Ingham1586d972011-12-17 01:35:57 +0000898 return StopInfoSP (new StopInfoThreadPlan (plan_sp, return_valobj_sp));
Greg Clayton643ee732010-08-04 01:40:35 +0000899}
Greg Clayton65611552011-06-04 01:26:29 +0000900
901StopInfoSP
902StopInfo::CreateStopReasonWithException (Thread &thread, const char *description)
903{
904 return StopInfoSP (new StopInfoException (thread, description));
905}
Jim Ingham1586d972011-12-17 01:35:57 +0000906
907ValueObjectSP
908StopInfo::GetReturnValueObject(StopInfoSP &stop_info_sp)
909{
910 if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonPlanComplete)
911 {
912 StopInfoThreadPlan *plan_stop_info = static_cast<StopInfoThreadPlan *>(stop_info_sp.get());
913 return plan_stop_info->GetReturnValueObject();
914 }
915 else
916 return ValueObjectSP();
917}