blob: c5c9c7051f4b746d3f050bbbfc77e2ae5a5c499e [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
553 // Record the snapshot of our watchpoint.
554 VariableSP var_sp;
555 ValueObjectSP valobj_sp;
556 StackFrame *frame = exe_ctx.GetFramePtr();
557 if (frame)
558 {
Johnny Chen0b093662012-08-14 20:56:37 +0000559 bool snapshot_taken = true;
Johnny Chen9e985592012-08-13 21:09:54 +0000560 if (!wp_sp->IsWatchVariable())
561 {
Johnny Chen4dc86bb2012-08-13 21:19:11 +0000562 // We are not watching a variable, just read from the process memory for the watched location.
Johnny Chen9e985592012-08-13 21:09:54 +0000563 assert (process);
564 Error error;
565 uint64_t val = process->ReadUnsignedIntegerFromMemory(wp_sp->GetLoadAddress(),
566 wp_sp->GetByteSize(),
567 0,
568 error);
569 if (log)
570 {
571 if (error.Success())
572 log->Printf("Watchpoint snapshot val taken: 0x%llx\n", val);
573 else
574 log->Printf("Watchpoint snapshot val taking failed.\n");
575 }
576 wp_sp->SetNewSnapshotVal(val);
577 }
578 else if (!wp_sp->GetWatchSpec().empty())
579 {
Johnny Chen4dc86bb2012-08-13 21:19:11 +0000580 // Use our frame to evaluate the variable expression.
Johnny Chen9e985592012-08-13 21:09:54 +0000581 Error error;
582 uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
583 StackFrame::eExpressionPathOptionsAllowDirectIVarAccess;
584 valobj_sp = frame->GetValueForVariableExpressionPath (wp_sp->GetWatchSpec().c_str(),
585 eNoDynamicValues,
586 expr_path_options,
587 var_sp,
588 error);
589 if (valobj_sp)
590 {
591 // We're in business.
592 StreamString ss;
593 ValueObject::DumpValueObject(ss, valobj_sp.get());
594 wp_sp->SetNewSnapshot(ss.GetString());
595 }
596 else
Johnny Chen0b093662012-08-14 20:56:37 +0000597 {
598 // The variable expression has become out of scope?
599 // Let us forget about this stop info.
600 if (log)
601 log->Printf("Snapshot attempt failed. Variable expression has become out of scope?");
602 snapshot_taken = false;
603 m_should_stop = false;
604 wp_sp->IncrementFalseAlarmsAndReviseHitCount();
605 }
Johnny Chen9e985592012-08-13 21:09:54 +0000606
Johnny Chen0b093662012-08-14 20:56:37 +0000607 if (log && snapshot_taken)
Johnny Chen9e985592012-08-13 21:09:54 +0000608 log->Printf("Watchpoint snapshot taken: '%s'\n", wp_sp->GetNewSnapshot().c_str());
609 }
610
611 // Now dump the snapshots we have taken.
Johnny Chen0b093662012-08-14 20:56:37 +0000612 if (snapshot_taken)
613 {
614 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
615 StreamSP output_sp = debugger.GetAsyncOutputStream ();
616 wp_sp->DumpSnapshots(output_sp.get());
617 output_sp->EOL();
618 output_sp->Flush();
619 }
Johnny Chen9e985592012-08-13 21:09:54 +0000620 }
Johnny Chen712a6282011-10-17 18:58:00 +0000621
622 if (m_should_stop && wp_sp->GetConditionText() != NULL)
623 {
624 // We need to make sure the user sees any parse errors in their condition, so we'll hook the
625 // constructor errors up to the debugger's Async I/O.
Johnny Chen712a6282011-10-17 18:58:00 +0000626 ExecutionResults result_code;
627 ValueObjectSP result_value_sp;
628 const bool discard_on_error = true;
629 Error error;
Greg Claytonf4124de2012-02-21 00:09:25 +0000630 result_code = ClangUserExpression::EvaluateWithError (exe_ctx,
Sean Callanana539e5b2012-09-08 01:51:48 +0000631 eExecutionPolicyOnlyWhenNeeded,
Sean Callanan5b658cc2011-11-07 23:35:40 +0000632 lldb::eLanguageTypeUnknown,
Sean Callanandaa6efe2011-12-21 22:22:58 +0000633 ClangUserExpression::eResultTypeAny,
Johnny Chen712a6282011-10-17 18:58:00 +0000634 discard_on_error,
635 wp_sp->GetConditionText(),
636 NULL,
637 result_value_sp,
Enrico Granata6cca9692012-07-16 23:10:35 +0000638 error,
639 500000);
Johnny Chen712a6282011-10-17 18:58:00 +0000640 if (result_code == eExecutionCompleted)
641 {
642 if (result_value_sp)
643 {
644 Scalar scalar_value;
645 if (result_value_sp->ResolveValue (scalar_value))
646 {
647 if (scalar_value.ULongLong(1) == 0)
648 {
649 // We have been vetoed. This takes precedence over querying
650 // the watchpoint whether it should stop (aka ignore count and
651 // friends). See also StopInfoWatchpoint::ShouldStop() as well
652 // as Process::ProcessEventData::DoOnRemoval().
653 m_should_stop = false;
654 }
655 else
656 m_should_stop = true;
657 if (log)
658 log->Printf("Condition successfully evaluated, result is %s.\n",
659 m_should_stop ? "true" : "false");
660 }
661 else
662 {
663 m_should_stop = true;
664 if (log)
665 log->Printf("Failed to get an integer result from the expression.");
666 }
667 }
668 }
669 else
670 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000671 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
Johnny Chen712a6282011-10-17 18:58:00 +0000672 StreamSP error_sp = debugger.GetAsyncErrorStream ();
Johnny Chen0c5c43b2012-01-24 23:19:25 +0000673 error_sp->Printf ("Stopped due to an error evaluating condition of watchpoint ");
Johnny Chen712a6282011-10-17 18:58:00 +0000674 wp_sp->GetDescription (error_sp.get(), eDescriptionLevelBrief);
675 error_sp->Printf (": \"%s\"",
676 wp_sp->GetConditionText());
677 error_sp->EOL();
678 const char *err_str = error.AsCString("<Unknown Error>");
679 if (log)
680 log->Printf("Error evaluating condition: \"%s\"\n", err_str);
681
682 error_sp->PutCString (err_str);
683 error_sp->EOL();
684 error_sp->Flush();
685 // If the condition fails to be parsed or run, we should stop.
686 m_should_stop = true;
687 }
688 }
Johnny Chen97600c12012-08-09 23:10:57 +0000689
690 // If the condition says to stop, we run the callback to further decide whether to stop.
691 if (m_should_stop)
692 {
Johnny Chen9e985592012-08-13 21:09:54 +0000693 StoppointCallbackContext context (event_ptr, exe_ctx, false);
Johnny Chen97600c12012-08-09 23:10:57 +0000694 bool stop_requested = wp_sp->InvokeCallback (&context);
695 // Also make sure that the callback hasn't continued the target.
696 // If it did, when we'll set m_should_stop to false and get out of here.
697 if (HasTargetRunSinceMe ())
698 m_should_stop = false;
699
700 if (m_should_stop && !stop_requested)
701 {
702 // We have been vetoed by the callback mechanism.
703 m_should_stop = false;
704 }
705 }
Johnny Chen712a6282011-10-17 18:58:00 +0000706 }
707 else
708 {
Jason Molendabf41e192012-10-04 22:47:07 +0000709 LogSP log_process(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Johnny Chen712a6282011-10-17 18:58:00 +0000710
Jason Molendabf41e192012-10-04 22:47:07 +0000711 if (log_process)
712 log_process->Printf ("Process::%s could not find watchpoint id: %lld...", __FUNCTION__, m_value);
Johnny Chen712a6282011-10-17 18:58:00 +0000713 }
714 if (log)
715 log->Printf ("Process::%s returning from action with m_should_stop: %d.", __FUNCTION__, m_should_stop);
716 }
717
Greg Clayton643ee732010-08-04 01:40:35 +0000718 virtual const char *
719 GetDescription ()
720 {
721 if (m_description.empty())
722 {
723 StreamString strm;
724 strm.Printf("watchpoint %lli", m_value);
725 m_description.swap (strm.GetString());
726 }
727 return m_description.c_str();
728 }
729
Greg Clayton643ee732010-08-04 01:40:35 +0000730private:
731 std::string m_description;
Johnny Chen043f8c22011-09-21 22:47:15 +0000732 bool m_should_stop;
733 bool m_should_stop_is_valid;
Greg Clayton643ee732010-08-04 01:40:35 +0000734};
735
736
737
738//----------------------------------------------------------------------
739// StopInfoUnixSignal
740//----------------------------------------------------------------------
741
742class StopInfoUnixSignal : public StopInfo
743{
744public:
745
746 StopInfoUnixSignal (Thread &thread, int signo) :
Greg Clayton65611552011-06-04 01:26:29 +0000747 StopInfo (thread, signo)
Greg Clayton643ee732010-08-04 01:40:35 +0000748 {
749 }
750
751 virtual ~StopInfoUnixSignal ()
752 {
753 }
754
755
756 virtual StopReason
757 GetStopReason () const
758 {
759 return eStopReasonSignal;
760 }
761
762 virtual bool
763 ShouldStop (Event *event_ptr)
764 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000765 return m_thread.GetProcess()->GetUnixSignals().GetShouldStop (m_value);
Greg Clayton643ee732010-08-04 01:40:35 +0000766 }
767
768
769 // If should stop returns false, check if we should notify of this event
770 virtual bool
771 ShouldNotify (Event *event_ptr)
772 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000773 return m_thread.GetProcess()->GetUnixSignals().GetShouldNotify (m_value);
Greg Clayton643ee732010-08-04 01:40:35 +0000774 }
775
776
777 virtual void
778 WillResume (lldb::StateType resume_state)
779 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000780 if (m_thread.GetProcess()->GetUnixSignals().GetShouldSuppress(m_value) == false)
Greg Clayton643ee732010-08-04 01:40:35 +0000781 m_thread.SetResumeSignal(m_value);
782 }
783
784 virtual const char *
785 GetDescription ()
786 {
787 if (m_description.empty())
788 {
789 StreamString strm;
Greg Claytonf4124de2012-02-21 00:09:25 +0000790 const char *signal_name = m_thread.GetProcess()->GetUnixSignals().GetSignalAsCString (m_value);
Greg Clayton643ee732010-08-04 01:40:35 +0000791 if (signal_name)
Greg Claytona830adb2010-10-04 01:05:56 +0000792 strm.Printf("signal %s", signal_name);
Greg Clayton643ee732010-08-04 01:40:35 +0000793 else
Greg Claytona830adb2010-10-04 01:05:56 +0000794 strm.Printf("signal %lli", m_value);
Greg Clayton643ee732010-08-04 01:40:35 +0000795 m_description.swap (strm.GetString());
796 }
797 return m_description.c_str();
798 }
Greg Clayton643ee732010-08-04 01:40:35 +0000799};
800
801//----------------------------------------------------------------------
802// StopInfoTrace
803//----------------------------------------------------------------------
804
805class StopInfoTrace : public StopInfo
806{
807public:
808
809 StopInfoTrace (Thread &thread) :
810 StopInfo (thread, LLDB_INVALID_UID)
811 {
812 }
813
814 virtual ~StopInfoTrace ()
815 {
816 }
817
818 virtual StopReason
819 GetStopReason () const
820 {
821 return eStopReasonTrace;
822 }
823
824 virtual const char *
825 GetDescription ()
826 {
Greg Clayton65611552011-06-04 01:26:29 +0000827 if (m_description.empty())
Greg Clayton643ee732010-08-04 01:40:35 +0000828 return "trace";
Greg Clayton65611552011-06-04 01:26:29 +0000829 else
830 return m_description.c_str();
831 }
832};
833
834
835//----------------------------------------------------------------------
836// StopInfoException
837//----------------------------------------------------------------------
838
839class StopInfoException : public StopInfo
840{
841public:
842
843 StopInfoException (Thread &thread, const char *description) :
844 StopInfo (thread, LLDB_INVALID_UID)
845 {
846 if (description)
847 SetDescription (description);
848 }
849
850 virtual
851 ~StopInfoException ()
852 {
853 }
854
855 virtual StopReason
856 GetStopReason () const
857 {
858 return eStopReasonException;
859 }
860
861 virtual const char *
862 GetDescription ()
863 {
864 if (m_description.empty())
865 return "exception";
866 else
867 return m_description.c_str();
Greg Clayton643ee732010-08-04 01:40:35 +0000868 }
869};
870
871
872//----------------------------------------------------------------------
873// StopInfoThreadPlan
874//----------------------------------------------------------------------
875
876class StopInfoThreadPlan : public StopInfo
877{
878public:
879
Jim Ingham1586d972011-12-17 01:35:57 +0000880 StopInfoThreadPlan (ThreadPlanSP &plan_sp, ValueObjectSP &return_valobj_sp) :
Greg Clayton643ee732010-08-04 01:40:35 +0000881 StopInfo (plan_sp->GetThread(), LLDB_INVALID_UID),
Jim Ingham1586d972011-12-17 01:35:57 +0000882 m_plan_sp (plan_sp),
883 m_return_valobj_sp (return_valobj_sp)
Greg Clayton643ee732010-08-04 01:40:35 +0000884 {
885 }
886
887 virtual ~StopInfoThreadPlan ()
888 {
889 }
890
891 virtual StopReason
892 GetStopReason () const
893 {
894 return eStopReasonPlanComplete;
895 }
896
897 virtual const char *
898 GetDescription ()
899 {
900 if (m_description.empty())
901 {
902 StreamString strm;
903 m_plan_sp->GetDescription (&strm, eDescriptionLevelBrief);
904 m_description.swap (strm.GetString());
905 }
906 return m_description.c_str();
907 }
Jim Ingham1586d972011-12-17 01:35:57 +0000908
909 ValueObjectSP
910 GetReturnValueObject()
911 {
912 return m_return_valobj_sp;
913 }
Greg Clayton643ee732010-08-04 01:40:35 +0000914
915private:
916 ThreadPlanSP m_plan_sp;
Jim Ingham1586d972011-12-17 01:35:57 +0000917 ValueObjectSP m_return_valobj_sp;
Greg Clayton643ee732010-08-04 01:40:35 +0000918};
Jim Ingham21f37ad2011-08-09 02:12:22 +0000919} // namespace lldb_private
Greg Clayton643ee732010-08-04 01:40:35 +0000920
921StopInfoSP
922StopInfo::CreateStopReasonWithBreakpointSiteID (Thread &thread, break_id_t break_id)
923{
924 return StopInfoSP (new StopInfoBreakpoint (thread, break_id));
925}
926
927StopInfoSP
Jim Inghamd1686902010-10-14 23:45:03 +0000928StopInfo::CreateStopReasonWithBreakpointSiteID (Thread &thread, break_id_t break_id, bool should_stop)
929{
930 return StopInfoSP (new StopInfoBreakpoint (thread, break_id, should_stop));
931}
932
933StopInfoSP
Greg Clayton643ee732010-08-04 01:40:35 +0000934StopInfo::CreateStopReasonWithWatchpointID (Thread &thread, break_id_t watch_id)
935{
936 return StopInfoSP (new StopInfoWatchpoint (thread, watch_id));
937}
938
939StopInfoSP
940StopInfo::CreateStopReasonWithSignal (Thread &thread, int signo)
941{
942 return StopInfoSP (new StopInfoUnixSignal (thread, signo));
943}
944
945StopInfoSP
946StopInfo::CreateStopReasonToTrace (Thread &thread)
947{
948 return StopInfoSP (new StopInfoTrace (thread));
949}
950
951StopInfoSP
Jim Ingham1586d972011-12-17 01:35:57 +0000952StopInfo::CreateStopReasonWithPlan (ThreadPlanSP &plan_sp, ValueObjectSP return_valobj_sp)
Greg Clayton643ee732010-08-04 01:40:35 +0000953{
Jim Ingham1586d972011-12-17 01:35:57 +0000954 return StopInfoSP (new StopInfoThreadPlan (plan_sp, return_valobj_sp));
Greg Clayton643ee732010-08-04 01:40:35 +0000955}
Greg Clayton65611552011-06-04 01:26:29 +0000956
957StopInfoSP
958StopInfo::CreateStopReasonWithException (Thread &thread, const char *description)
959{
960 return StopInfoSP (new StopInfoException (thread, description));
961}
Jim Ingham1586d972011-12-17 01:35:57 +0000962
963ValueObjectSP
964StopInfo::GetReturnValueObject(StopInfoSP &stop_info_sp)
965{
966 if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonPlanComplete)
967 {
968 StopInfoThreadPlan *plan_stop_info = static_cast<StopInfoThreadPlan *>(stop_info_sp.get());
969 return plan_stop_info->GetReturnValueObject();
970 }
971 else
972 return ValueObjectSP();
973}