blob: 2d48dccaca48fee14a63985c114b2dc1dbaa4c6a [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),
102 m_address (LLDB_INVALID_ADDRESS)
Greg Clayton643ee732010-08-04 01:40:35 +0000103 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000104 BreakpointSiteSP bp_site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (m_value));
Jim Ingham1c658532011-10-28 01:12:19 +0000105 if (bp_site_sp)
106 {
107 m_address = bp_site_sp->GetLoadAddress();
108 }
Greg Clayton643ee732010-08-04 01:40:35 +0000109 }
110
Jim Inghamd1686902010-10-14 23:45:03 +0000111 StopInfoBreakpoint (Thread &thread, break_id_t break_id, bool should_stop) :
112 StopInfo (thread, break_id),
113 m_description(),
114 m_should_stop (should_stop),
Jim Inghamf9f40c22011-02-08 05:20:59 +0000115 m_should_stop_is_valid (true),
Jim Ingham1c658532011-10-28 01:12:19 +0000116 m_should_perform_action (true),
117 m_address (LLDB_INVALID_ADDRESS)
Jim Inghamd1686902010-10-14 23:45:03 +0000118 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000119 BreakpointSiteSP bp_site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (m_value));
Jim Ingham1c658532011-10-28 01:12:19 +0000120 if (bp_site_sp)
121 {
122 m_address = bp_site_sp->GetLoadAddress();
123 }
Jim Inghamd1686902010-10-14 23:45:03 +0000124 }
125
Greg Clayton643ee732010-08-04 01:40:35 +0000126 virtual ~StopInfoBreakpoint ()
127 {
128 }
129
130 virtual StopReason
131 GetStopReason () const
132 {
133 return eStopReasonBreakpoint;
134 }
135
136 virtual bool
Jim Inghame787c7e2012-04-20 21:16:56 +0000137 ShouldStopSynchronous (Event *event_ptr)
Greg Clayton643ee732010-08-04 01:40:35 +0000138 {
139 if (!m_should_stop_is_valid)
140 {
141 // Only check once if we should stop at a breakpoint
Greg Claytonf4124de2012-02-21 00:09:25 +0000142 BreakpointSiteSP bp_site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (m_value));
Greg Clayton643ee732010-08-04 01:40:35 +0000143 if (bp_site_sp)
144 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000145 ExecutionContext exe_ctx (m_thread.GetStackFrameAtIndex(0));
146 StoppointCallbackContext context (event_ptr, exe_ctx, true);
Greg Clayton643ee732010-08-04 01:40:35 +0000147 m_should_stop = bp_site_sp->ShouldStop (&context);
148 }
149 else
150 {
Greg Claytone005f2c2010-11-06 01:53:30 +0000151 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Clayton643ee732010-08-04 01:40:35 +0000152
153 if (log)
154 log->Printf ("Process::%s could not find breakpoint site id: %lld...", __FUNCTION__, m_value);
155
156 m_should_stop = true;
157 }
158 m_should_stop_is_valid = true;
159 }
160 return m_should_stop;
161 }
Jim Ingham6fb8baa2010-08-10 00:59:59 +0000162
Jim Inghame787c7e2012-04-20 21:16:56 +0000163 bool
164 ShouldStop (Event *event_ptr)
165 {
166 // This just reports the work done by PerformAction or the synchronous stop. It should
167 // only ever get called after they have had a chance to run.
168 assert (m_should_stop_is_valid);
169 return m_should_stop;
170 }
171
Jim Ingham6fb8baa2010-08-10 00:59:59 +0000172 virtual void
173 PerformAction (Event *event_ptr)
174 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000175 if (!m_should_perform_action)
176 return;
177 m_should_perform_action = false;
178
Jim Ingham21f37ad2011-08-09 02:12:22 +0000179 LogSP log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
Jim Ingham21f37ad2011-08-09 02:12:22 +0000180
Greg Claytonf4124de2012-02-21 00:09:25 +0000181 BreakpointSiteSP bp_site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (m_value));
Jim Ingham982a6ca2011-12-09 04:17:31 +0000182
Jim Ingham6fb8baa2010-08-10 00:59:59 +0000183 if (bp_site_sp)
184 {
185 size_t num_owners = bp_site_sp->GetNumberOfOwners();
Jim Ingham982a6ca2011-12-09 04:17:31 +0000186
187 if (num_owners == 0)
Jim Ingham6fb8baa2010-08-10 00:59:59 +0000188 {
Jim Ingham982a6ca2011-12-09 04:17:31 +0000189 m_should_stop = true;
190 }
191 else
192 {
193 // We go through each location, and test first its condition. If the condition says to stop,
194 // then we run the callback for that location. If that callback says to stop as well, then
195 // we set m_should_stop to true; we are going to stop.
196 // But we still want to give all the breakpoints whose conditions say we are going to stop a
197 // chance to run their callbacks.
198 // Of course if any callback restarts the target by putting "continue" in the callback, then
199 // we're going to restart, without running the rest of the callbacks. And in this case we will
200 // end up not stopping even if another location said we should stop. But that's better than not
201 // running all the callbacks.
202
203 m_should_stop = false;
204
Greg Claytonf4124de2012-02-21 00:09:25 +0000205 ExecutionContext exe_ctx (m_thread.GetStackFrameAtIndex(0));
206 StoppointCallbackContext context (event_ptr, exe_ctx, false);
Jim Ingham21f37ad2011-08-09 02:12:22 +0000207
Jim Ingham21f37ad2011-08-09 02:12:22 +0000208 for (size_t j = 0; j < num_owners; j++)
209 {
210 lldb::BreakpointLocationSP bp_loc_sp = bp_site_sp->GetOwnerAtIndex(j);
Jim Ingham982a6ca2011-12-09 04:17:31 +0000211
212 // First run the condition for the breakpoint. If that says we should stop, then we'll run
213 // the callback for the breakpoint. If the callback says we shouldn't stop that will win.
214
215 bool condition_says_stop = true;
Jim Ingham21f37ad2011-08-09 02:12:22 +0000216 if (bp_loc_sp->GetConditionText() != NULL)
217 {
218 // We need to make sure the user sees any parse errors in their condition, so we'll hook the
219 // constructor errors up to the debugger's Async I/O.
220
Jim Ingham21f37ad2011-08-09 02:12:22 +0000221 ValueObjectSP result_valobj_sp;
222
223 ExecutionResults result_code;
224 ValueObjectSP result_value_sp;
225 const bool discard_on_error = true;
226 Error error;
Greg Claytonf4124de2012-02-21 00:09:25 +0000227 result_code = ClangUserExpression::EvaluateWithError (exe_ctx,
Jim Inghame787c7e2012-04-20 21:16:56 +0000228 eExecutionPolicyOnlyWhenNeeded,
Sean Callanan5b658cc2011-11-07 23:35:40 +0000229 lldb::eLanguageTypeUnknown,
Sean Callanandaa6efe2011-12-21 22:22:58 +0000230 ClangUserExpression::eResultTypeAny,
Sean Callanan47dc4572011-09-15 02:13:07 +0000231 discard_on_error,
232 bp_loc_sp->GetConditionText(),
233 NULL,
234 result_value_sp,
235 error);
Jim Ingham21f37ad2011-08-09 02:12:22 +0000236 if (result_code == eExecutionCompleted)
237 {
238 if (result_value_sp)
239 {
240 Scalar scalar_value;
241 if (result_value_sp->ResolveValue (scalar_value))
242 {
243 if (scalar_value.ULongLong(1) == 0)
Jim Ingham982a6ca2011-12-09 04:17:31 +0000244 condition_says_stop = false;
Jim Ingham21f37ad2011-08-09 02:12:22 +0000245 else
Jim Ingham982a6ca2011-12-09 04:17:31 +0000246 condition_says_stop = true;
Jim Ingham21f37ad2011-08-09 02:12:22 +0000247 if (log)
248 log->Printf("Condition successfully evaluated, result is %s.\n",
249 m_should_stop ? "true" : "false");
250 }
251 else
252 {
Jim Ingham982a6ca2011-12-09 04:17:31 +0000253 condition_says_stop = true;
Jim Ingham21f37ad2011-08-09 02:12:22 +0000254 if (log)
255 log->Printf("Failed to get an integer result from the expression.");
256 }
257 }
258 }
259 else
260 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000261 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
Jim Ingham21f37ad2011-08-09 02:12:22 +0000262 StreamSP error_sp = debugger.GetAsyncErrorStream ();
263 error_sp->Printf ("Stopped due to an error evaluating condition of breakpoint ");
264 bp_loc_sp->GetDescription (error_sp.get(), eDescriptionLevelBrief);
265 error_sp->Printf (": \"%s\"",
266 bp_loc_sp->GetConditionText());
267 error_sp->EOL();
268 const char *err_str = error.AsCString("<Unknown Error>");
269 if (log)
270 log->Printf("Error evaluating condition: \"%s\"\n", err_str);
271
272 error_sp->PutCString (err_str);
273 error_sp->EOL();
274 error_sp->Flush();
275 // If the condition fails to be parsed or run, we should stop.
Jim Ingham982a6ca2011-12-09 04:17:31 +0000276 condition_says_stop = true;
Jim Ingham21f37ad2011-08-09 02:12:22 +0000277 }
278 }
279
Jim Ingham982a6ca2011-12-09 04:17:31 +0000280 // If this location's condition says we should aren't going to stop,
281 // then don't run the callback for this location.
282 if (!condition_says_stop)
283 continue;
284
285 bool callback_says_stop;
286
287 // FIXME: For now the callbacks have to run in async mode - the first time we restart we need
288 // to get out of there. So set it here.
289 // When we figure out how to nest breakpoint hits then this will change.
290
Greg Claytonf4124de2012-02-21 00:09:25 +0000291 Debugger &debugger = m_thread.CalculateTarget()->GetDebugger();
Jim Ingham982a6ca2011-12-09 04:17:31 +0000292 bool old_async = debugger.GetAsyncExecution();
293 debugger.SetAsyncExecution (true);
294
295 callback_says_stop = bp_loc_sp->InvokeCallback (&context);
296
297 debugger.SetAsyncExecution (old_async);
298
299 if (callback_says_stop)
300 m_should_stop = true;
301
302 // Also make sure that the callback hasn't continued the target.
303 // If it did, when we'll set m_should_start to false and get out of here.
304 if (HasTargetRunSinceMe ())
305 {
306 m_should_stop = false;
Jim Ingham21f37ad2011-08-09 02:12:22 +0000307 break;
Jim Ingham982a6ca2011-12-09 04:17:31 +0000308 }
Jim Ingham21f37ad2011-08-09 02:12:22 +0000309 }
Jim Ingham6fb8baa2010-08-10 00:59:59 +0000310 }
Jim Ingham982a6ca2011-12-09 04:17:31 +0000311 // We've figured out what this stop wants to do, so mark it as valid so we don't compute it again.
312 m_should_stop_is_valid = true;
313
Jim Ingham6fb8baa2010-08-10 00:59:59 +0000314 }
315 else
316 {
Jim Ingham982a6ca2011-12-09 04:17:31 +0000317 m_should_stop = true;
318 m_should_stop_is_valid = true;
Greg Claytone005f2c2010-11-06 01:53:30 +0000319 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Jim Ingham6fb8baa2010-08-10 00:59:59 +0000320
321 if (log)
322 log->Printf ("Process::%s could not find breakpoint site id: %lld...", __FUNCTION__, m_value);
323 }
Jim Ingham21f37ad2011-08-09 02:12:22 +0000324 if (log)
325 log->Printf ("Process::%s returning from action with m_should_stop: %d.", __FUNCTION__, m_should_stop);
Jim Ingham6fb8baa2010-08-10 00:59:59 +0000326 }
Greg Clayton643ee732010-08-04 01:40:35 +0000327
328 virtual bool
329 ShouldNotify (Event *event_ptr)
330 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000331 BreakpointSiteSP bp_site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (m_value));
Greg Clayton643ee732010-08-04 01:40:35 +0000332 if (bp_site_sp)
333 {
334 bool all_internal = true;
335
336 for (uint32_t i = 0; i < bp_site_sp->GetNumberOfOwners(); i++)
337 {
338 if (!bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint().IsInternal())
339 {
340 all_internal = false;
341 break;
342 }
343 }
344 return all_internal == false;
345 }
346 return true;
347 }
348
349 virtual const char *
350 GetDescription ()
351 {
352 if (m_description.empty())
353 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000354 BreakpointSiteSP bp_site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (m_value));
Jim Ingham6fb8baa2010-08-10 00:59:59 +0000355 if (bp_site_sp)
356 {
357 StreamString strm;
358 strm.Printf("breakpoint ");
359 bp_site_sp->GetDescription(&strm, eDescriptionLevelBrief);
360 m_description.swap (strm.GetString());
361 }
362 else
363 {
364 StreamString strm;
Jim Ingham1c658532011-10-28 01:12:19 +0000365 if (m_address == LLDB_INVALID_ADDRESS)
366 strm.Printf("breakpoint site %lli which has been deleted - unknown address", m_value);
367 else
368 strm.Printf("breakpoint site %lli which has been deleted - was at 0x%llx", m_value, m_address);
Jim Ingham6fb8baa2010-08-10 00:59:59 +0000369 m_description.swap (strm.GetString());
370 }
Greg Clayton643ee732010-08-04 01:40:35 +0000371 }
372 return m_description.c_str();
373 }
374
375private:
376 std::string m_description;
377 bool m_should_stop;
378 bool m_should_stop_is_valid;
Jim Inghamf9f40c22011-02-08 05:20:59 +0000379 bool m_should_perform_action; // Since we are trying to preserve the "state" of the system even if we run functions
380 // etc. behind the users backs, we need to make sure we only REALLY perform the action once.
Jim Ingham1c658532011-10-28 01:12:19 +0000381 lldb::addr_t m_address; // We use this to capture the breakpoint site address when we create the StopInfo,
382 // in case somebody deletes it between the time the StopInfo is made and the
383 // description is asked for.
Greg Clayton643ee732010-08-04 01:40:35 +0000384};
385
386
387//----------------------------------------------------------------------
388// StopInfoWatchpoint
389//----------------------------------------------------------------------
390
391class StopInfoWatchpoint : public StopInfo
392{
393public:
394
395 StopInfoWatchpoint (Thread &thread, break_id_t watch_id) :
Johnny Chen043f8c22011-09-21 22:47:15 +0000396 StopInfo(thread, watch_id),
397 m_description(),
398 m_should_stop(false),
399 m_should_stop_is_valid(false)
Greg Clayton643ee732010-08-04 01:40:35 +0000400 {
401 }
402
403 virtual ~StopInfoWatchpoint ()
404 {
405 }
406
407 virtual StopReason
408 GetStopReason () const
409 {
410 return eStopReasonWatchpoint;
411 }
412
Johnny Chen043f8c22011-09-21 22:47:15 +0000413 virtual bool
414 ShouldStop (Event *event_ptr)
415 {
416 // ShouldStop() method is idempotent and should not affect hit count.
Johnny Chen712a6282011-10-17 18:58:00 +0000417 // See Process::RunPrivateStateThread()->Process()->HandlePrivateEvent()
418 // -->Process()::ShouldBroadcastEvent()->ThreadList::ShouldStop()->
419 // Thread::ShouldStop()->ThreadPlanBase::ShouldStop()->
420 // StopInfoWatchpoint::ShouldStop() and
421 // Event::DoOnRemoval()->Process::ProcessEventData::DoOnRemoval()->
422 // StopInfoWatchpoint::PerformAction().
Johnny Chen043f8c22011-09-21 22:47:15 +0000423 if (m_should_stop_is_valid)
424 return m_should_stop;
425
Johnny Chenecd4feb2011-10-14 00:42:25 +0000426 WatchpointSP wp_sp =
Greg Claytonf4124de2012-02-21 00:09:25 +0000427 m_thread.CalculateTarget()->GetWatchpointList().FindByID(GetValue());
Johnny Chenecd4feb2011-10-14 00:42:25 +0000428 if (wp_sp)
Johnny Chen043f8c22011-09-21 22:47:15 +0000429 {
430 // Check if we should stop at a watchpoint.
Greg Claytonf4124de2012-02-21 00:09:25 +0000431 ExecutionContext exe_ctx (m_thread.GetStackFrameAtIndex(0));
432 StoppointCallbackContext context (event_ptr, exe_ctx, true);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000433 m_should_stop = wp_sp->ShouldStop (&context);
Johnny Chen043f8c22011-09-21 22:47:15 +0000434 }
435 else
436 {
437 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
438
439 if (log)
440 log->Printf ("Process::%s could not find watchpoint location id: %lld...",
441 __FUNCTION__, GetValue());
442
443 m_should_stop = true;
444 }
445 m_should_stop_is_valid = true;
446 return m_should_stop;
447 }
448
Johnny Chenbd446f12012-08-21 22:06:34 +0000449 // Make sure watchpoint is properly disabled and subsequently enabled while performing watchpoint actions.
450 class WatchpointSentry {
451 public:
452 WatchpointSentry(Process *p, Watchpoint *w):
453 process(p),
454 watchpoint(w)
455 {
456 if (process && watchpoint)
457 process->DisableWatchpoint(watchpoint);
458 }
459 ~WatchpointSentry()
460 {
461 if (process && watchpoint)
462 process->EnableWatchpoint(watchpoint);
463 }
464 private:
465 Process *process;
466 Watchpoint *watchpoint;
467 };
468
Johnny Chen712a6282011-10-17 18:58:00 +0000469 // Perform any action that is associated with this stop. This is done as the
470 // Event is removed from the event queue.
471 virtual void
472 PerformAction (Event *event_ptr)
473 {
Johnny Chen9e985592012-08-13 21:09:54 +0000474 LogSP log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS);
Johnny Chen712a6282011-10-17 18:58:00 +0000475 // We're going to calculate if we should stop or not in some way during the course of
476 // this code. Also by default we're going to stop, so set that here.
477 m_should_stop = true;
478
479 WatchpointSP wp_sp =
Greg Claytonf4124de2012-02-21 00:09:25 +0000480 m_thread.CalculateTarget()->GetWatchpointList().FindByID(GetValue());
Johnny Chen712a6282011-10-17 18:58:00 +0000481 if (wp_sp)
482 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000483 ExecutionContext exe_ctx (m_thread.GetStackFrameAtIndex(0));
Johnny Chen9e985592012-08-13 21:09:54 +0000484 Process* process = exe_ctx.GetProcessPtr();
Johnny Chenbd446f12012-08-21 22:06:34 +0000485
Johnny Chenaf4e9662012-08-21 22:15:58 +0000486 // This sentry object makes sure the current watchpoint is disabled while performing watchpoint actions,
487 // and it is then enabled after we are finished.
Johnny Chenbd446f12012-08-21 22:06:34 +0000488 WatchpointSentry sentry(process, wp_sp.get());
489
Enrico Granata7de2a3b2012-07-13 23:18:48 +0000490 {
491 // check if this process is running on an architecture where watchpoints trigger
492 // before the associated instruction runs. if so, disable the WP, single-step and then
493 // re-enable the watchpoint
Enrico Granata7de2a3b2012-07-13 23:18:48 +0000494 if (process)
495 {
496 uint32_t num; bool wp_triggers_after;
497 if (process->GetWatchpointSupportInfo(num, wp_triggers_after).Success())
498 {
499 if (!wp_triggers_after)
500 {
Enrico Granata7de2a3b2012-07-13 23:18:48 +0000501 ThreadPlan *new_plan = m_thread.QueueThreadPlanForStepSingleInstruction(false, // step-over
502 false, // abort_other_plans
503 true); // stop_other_threads
504 new_plan->SetIsMasterPlan (true);
505 new_plan->SetOkayToDiscard (false);
506 process->GetThreadList().SetSelectedThreadByID (m_thread.GetID());
507 process->Resume ();
508 process->WaitForProcessToStop (NULL);
509 process->GetThreadList().SetSelectedThreadByID (m_thread.GetID());
510 MakeStopInfoValid(); // make sure we do not fail to stop because of the single-step taken above
Enrico Granata7de2a3b2012-07-13 23:18:48 +0000511 }
512 }
513 }
514 }
Johnny Chen9e985592012-08-13 21:09:54 +0000515
516 // Record the snapshot of our watchpoint.
517 VariableSP var_sp;
518 ValueObjectSP valobj_sp;
519 StackFrame *frame = exe_ctx.GetFramePtr();
520 if (frame)
521 {
Johnny Chen0b093662012-08-14 20:56:37 +0000522 bool snapshot_taken = true;
Johnny Chen9e985592012-08-13 21:09:54 +0000523 if (!wp_sp->IsWatchVariable())
524 {
Johnny Chen4dc86bb2012-08-13 21:19:11 +0000525 // We are not watching a variable, just read from the process memory for the watched location.
Johnny Chen9e985592012-08-13 21:09:54 +0000526 assert (process);
527 Error error;
528 uint64_t val = process->ReadUnsignedIntegerFromMemory(wp_sp->GetLoadAddress(),
529 wp_sp->GetByteSize(),
530 0,
531 error);
532 if (log)
533 {
534 if (error.Success())
535 log->Printf("Watchpoint snapshot val taken: 0x%llx\n", val);
536 else
537 log->Printf("Watchpoint snapshot val taking failed.\n");
538 }
539 wp_sp->SetNewSnapshotVal(val);
540 }
541 else if (!wp_sp->GetWatchSpec().empty())
542 {
Johnny Chen4dc86bb2012-08-13 21:19:11 +0000543 // Use our frame to evaluate the variable expression.
Johnny Chen9e985592012-08-13 21:09:54 +0000544 Error error;
545 uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
546 StackFrame::eExpressionPathOptionsAllowDirectIVarAccess;
547 valobj_sp = frame->GetValueForVariableExpressionPath (wp_sp->GetWatchSpec().c_str(),
548 eNoDynamicValues,
549 expr_path_options,
550 var_sp,
551 error);
552 if (valobj_sp)
553 {
554 // We're in business.
555 StreamString ss;
556 ValueObject::DumpValueObject(ss, valobj_sp.get());
557 wp_sp->SetNewSnapshot(ss.GetString());
558 }
559 else
Johnny Chen0b093662012-08-14 20:56:37 +0000560 {
561 // The variable expression has become out of scope?
562 // Let us forget about this stop info.
563 if (log)
564 log->Printf("Snapshot attempt failed. Variable expression has become out of scope?");
565 snapshot_taken = false;
566 m_should_stop = false;
567 wp_sp->IncrementFalseAlarmsAndReviseHitCount();
568 }
Johnny Chen9e985592012-08-13 21:09:54 +0000569
Johnny Chen0b093662012-08-14 20:56:37 +0000570 if (log && snapshot_taken)
Johnny Chen9e985592012-08-13 21:09:54 +0000571 log->Printf("Watchpoint snapshot taken: '%s'\n", wp_sp->GetNewSnapshot().c_str());
572 }
573
574 // Now dump the snapshots we have taken.
Johnny Chen0b093662012-08-14 20:56:37 +0000575 if (snapshot_taken)
576 {
577 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
578 StreamSP output_sp = debugger.GetAsyncOutputStream ();
579 wp_sp->DumpSnapshots(output_sp.get());
580 output_sp->EOL();
581 output_sp->Flush();
582 }
Johnny Chen9e985592012-08-13 21:09:54 +0000583 }
Johnny Chen712a6282011-10-17 18:58:00 +0000584
585 if (m_should_stop && wp_sp->GetConditionText() != NULL)
586 {
587 // We need to make sure the user sees any parse errors in their condition, so we'll hook the
588 // constructor errors up to the debugger's Async I/O.
Johnny Chen712a6282011-10-17 18:58:00 +0000589 ExecutionResults result_code;
590 ValueObjectSP result_value_sp;
591 const bool discard_on_error = true;
592 Error error;
Greg Claytonf4124de2012-02-21 00:09:25 +0000593 result_code = ClangUserExpression::EvaluateWithError (exe_ctx,
Johnny Chen712a6282011-10-17 18:58:00 +0000594 eExecutionPolicyAlways,
Sean Callanan5b658cc2011-11-07 23:35:40 +0000595 lldb::eLanguageTypeUnknown,
Sean Callanandaa6efe2011-12-21 22:22:58 +0000596 ClangUserExpression::eResultTypeAny,
Johnny Chen712a6282011-10-17 18:58:00 +0000597 discard_on_error,
598 wp_sp->GetConditionText(),
599 NULL,
600 result_value_sp,
Enrico Granata6cca9692012-07-16 23:10:35 +0000601 error,
602 500000);
Johnny Chen712a6282011-10-17 18:58:00 +0000603 if (result_code == eExecutionCompleted)
604 {
605 if (result_value_sp)
606 {
607 Scalar scalar_value;
608 if (result_value_sp->ResolveValue (scalar_value))
609 {
610 if (scalar_value.ULongLong(1) == 0)
611 {
612 // We have been vetoed. This takes precedence over querying
613 // the watchpoint whether it should stop (aka ignore count and
614 // friends). See also StopInfoWatchpoint::ShouldStop() as well
615 // as Process::ProcessEventData::DoOnRemoval().
616 m_should_stop = false;
617 }
618 else
619 m_should_stop = true;
620 if (log)
621 log->Printf("Condition successfully evaluated, result is %s.\n",
622 m_should_stop ? "true" : "false");
623 }
624 else
625 {
626 m_should_stop = true;
627 if (log)
628 log->Printf("Failed to get an integer result from the expression.");
629 }
630 }
631 }
632 else
633 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000634 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
Johnny Chen712a6282011-10-17 18:58:00 +0000635 StreamSP error_sp = debugger.GetAsyncErrorStream ();
Johnny Chen0c5c43b2012-01-24 23:19:25 +0000636 error_sp->Printf ("Stopped due to an error evaluating condition of watchpoint ");
Johnny Chen712a6282011-10-17 18:58:00 +0000637 wp_sp->GetDescription (error_sp.get(), eDescriptionLevelBrief);
638 error_sp->Printf (": \"%s\"",
639 wp_sp->GetConditionText());
640 error_sp->EOL();
641 const char *err_str = error.AsCString("<Unknown Error>");
642 if (log)
643 log->Printf("Error evaluating condition: \"%s\"\n", err_str);
644
645 error_sp->PutCString (err_str);
646 error_sp->EOL();
647 error_sp->Flush();
648 // If the condition fails to be parsed or run, we should stop.
649 m_should_stop = true;
650 }
651 }
Johnny Chen97600c12012-08-09 23:10:57 +0000652
653 // If the condition says to stop, we run the callback to further decide whether to stop.
654 if (m_should_stop)
655 {
Johnny Chen9e985592012-08-13 21:09:54 +0000656 StoppointCallbackContext context (event_ptr, exe_ctx, false);
Johnny Chen97600c12012-08-09 23:10:57 +0000657 bool stop_requested = wp_sp->InvokeCallback (&context);
658 // Also make sure that the callback hasn't continued the target.
659 // If it did, when we'll set m_should_stop to false and get out of here.
660 if (HasTargetRunSinceMe ())
661 m_should_stop = false;
662
663 if (m_should_stop && !stop_requested)
664 {
665 // We have been vetoed by the callback mechanism.
666 m_should_stop = false;
667 }
668 }
Johnny Chen712a6282011-10-17 18:58:00 +0000669 }
670 else
671 {
672 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
673
674 if (log)
675 log->Printf ("Process::%s could not find watchpoint id: %lld...", __FUNCTION__, m_value);
676 }
677 if (log)
678 log->Printf ("Process::%s returning from action with m_should_stop: %d.", __FUNCTION__, m_should_stop);
679 }
680
Greg Clayton643ee732010-08-04 01:40:35 +0000681 virtual const char *
682 GetDescription ()
683 {
684 if (m_description.empty())
685 {
686 StreamString strm;
687 strm.Printf("watchpoint %lli", m_value);
688 m_description.swap (strm.GetString());
689 }
690 return m_description.c_str();
691 }
692
Greg Clayton643ee732010-08-04 01:40:35 +0000693private:
694 std::string m_description;
Johnny Chen043f8c22011-09-21 22:47:15 +0000695 bool m_should_stop;
696 bool m_should_stop_is_valid;
Greg Clayton643ee732010-08-04 01:40:35 +0000697};
698
699
700
701//----------------------------------------------------------------------
702// StopInfoUnixSignal
703//----------------------------------------------------------------------
704
705class StopInfoUnixSignal : public StopInfo
706{
707public:
708
709 StopInfoUnixSignal (Thread &thread, int signo) :
Greg Clayton65611552011-06-04 01:26:29 +0000710 StopInfo (thread, signo)
Greg Clayton643ee732010-08-04 01:40:35 +0000711 {
712 }
713
714 virtual ~StopInfoUnixSignal ()
715 {
716 }
717
718
719 virtual StopReason
720 GetStopReason () const
721 {
722 return eStopReasonSignal;
723 }
724
725 virtual bool
726 ShouldStop (Event *event_ptr)
727 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000728 return m_thread.GetProcess()->GetUnixSignals().GetShouldStop (m_value);
Greg Clayton643ee732010-08-04 01:40:35 +0000729 }
730
731
732 // If should stop returns false, check if we should notify of this event
733 virtual bool
734 ShouldNotify (Event *event_ptr)
735 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000736 return m_thread.GetProcess()->GetUnixSignals().GetShouldNotify (m_value);
Greg Clayton643ee732010-08-04 01:40:35 +0000737 }
738
739
740 virtual void
741 WillResume (lldb::StateType resume_state)
742 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000743 if (m_thread.GetProcess()->GetUnixSignals().GetShouldSuppress(m_value) == false)
Greg Clayton643ee732010-08-04 01:40:35 +0000744 m_thread.SetResumeSignal(m_value);
745 }
746
747 virtual const char *
748 GetDescription ()
749 {
750 if (m_description.empty())
751 {
752 StreamString strm;
Greg Claytonf4124de2012-02-21 00:09:25 +0000753 const char *signal_name = m_thread.GetProcess()->GetUnixSignals().GetSignalAsCString (m_value);
Greg Clayton643ee732010-08-04 01:40:35 +0000754 if (signal_name)
Greg Claytona830adb2010-10-04 01:05:56 +0000755 strm.Printf("signal %s", signal_name);
Greg Clayton643ee732010-08-04 01:40:35 +0000756 else
Greg Claytona830adb2010-10-04 01:05:56 +0000757 strm.Printf("signal %lli", m_value);
Greg Clayton643ee732010-08-04 01:40:35 +0000758 m_description.swap (strm.GetString());
759 }
760 return m_description.c_str();
761 }
Greg Clayton643ee732010-08-04 01:40:35 +0000762};
763
764//----------------------------------------------------------------------
765// StopInfoTrace
766//----------------------------------------------------------------------
767
768class StopInfoTrace : public StopInfo
769{
770public:
771
772 StopInfoTrace (Thread &thread) :
773 StopInfo (thread, LLDB_INVALID_UID)
774 {
775 }
776
777 virtual ~StopInfoTrace ()
778 {
779 }
780
781 virtual StopReason
782 GetStopReason () const
783 {
784 return eStopReasonTrace;
785 }
786
787 virtual const char *
788 GetDescription ()
789 {
Greg Clayton65611552011-06-04 01:26:29 +0000790 if (m_description.empty())
Greg Clayton643ee732010-08-04 01:40:35 +0000791 return "trace";
Greg Clayton65611552011-06-04 01:26:29 +0000792 else
793 return m_description.c_str();
794 }
795};
796
797
798//----------------------------------------------------------------------
799// StopInfoException
800//----------------------------------------------------------------------
801
802class StopInfoException : public StopInfo
803{
804public:
805
806 StopInfoException (Thread &thread, const char *description) :
807 StopInfo (thread, LLDB_INVALID_UID)
808 {
809 if (description)
810 SetDescription (description);
811 }
812
813 virtual
814 ~StopInfoException ()
815 {
816 }
817
818 virtual StopReason
819 GetStopReason () const
820 {
821 return eStopReasonException;
822 }
823
824 virtual const char *
825 GetDescription ()
826 {
827 if (m_description.empty())
828 return "exception";
829 else
830 return m_description.c_str();
Greg Clayton643ee732010-08-04 01:40:35 +0000831 }
832};
833
834
835//----------------------------------------------------------------------
836// StopInfoThreadPlan
837//----------------------------------------------------------------------
838
839class StopInfoThreadPlan : public StopInfo
840{
841public:
842
Jim Ingham1586d972011-12-17 01:35:57 +0000843 StopInfoThreadPlan (ThreadPlanSP &plan_sp, ValueObjectSP &return_valobj_sp) :
Greg Clayton643ee732010-08-04 01:40:35 +0000844 StopInfo (plan_sp->GetThread(), LLDB_INVALID_UID),
Jim Ingham1586d972011-12-17 01:35:57 +0000845 m_plan_sp (plan_sp),
846 m_return_valobj_sp (return_valobj_sp)
Greg Clayton643ee732010-08-04 01:40:35 +0000847 {
848 }
849
850 virtual ~StopInfoThreadPlan ()
851 {
852 }
853
854 virtual StopReason
855 GetStopReason () const
856 {
857 return eStopReasonPlanComplete;
858 }
859
860 virtual const char *
861 GetDescription ()
862 {
863 if (m_description.empty())
864 {
865 StreamString strm;
866 m_plan_sp->GetDescription (&strm, eDescriptionLevelBrief);
867 m_description.swap (strm.GetString());
868 }
869 return m_description.c_str();
870 }
Jim Ingham1586d972011-12-17 01:35:57 +0000871
872 ValueObjectSP
873 GetReturnValueObject()
874 {
875 return m_return_valobj_sp;
876 }
Greg Clayton643ee732010-08-04 01:40:35 +0000877
878private:
879 ThreadPlanSP m_plan_sp;
Jim Ingham1586d972011-12-17 01:35:57 +0000880 ValueObjectSP m_return_valobj_sp;
Greg Clayton643ee732010-08-04 01:40:35 +0000881};
Jim Ingham21f37ad2011-08-09 02:12:22 +0000882} // namespace lldb_private
Greg Clayton643ee732010-08-04 01:40:35 +0000883
884StopInfoSP
885StopInfo::CreateStopReasonWithBreakpointSiteID (Thread &thread, break_id_t break_id)
886{
887 return StopInfoSP (new StopInfoBreakpoint (thread, break_id));
888}
889
890StopInfoSP
Jim Inghamd1686902010-10-14 23:45:03 +0000891StopInfo::CreateStopReasonWithBreakpointSiteID (Thread &thread, break_id_t break_id, bool should_stop)
892{
893 return StopInfoSP (new StopInfoBreakpoint (thread, break_id, should_stop));
894}
895
896StopInfoSP
Greg Clayton643ee732010-08-04 01:40:35 +0000897StopInfo::CreateStopReasonWithWatchpointID (Thread &thread, break_id_t watch_id)
898{
899 return StopInfoSP (new StopInfoWatchpoint (thread, watch_id));
900}
901
902StopInfoSP
903StopInfo::CreateStopReasonWithSignal (Thread &thread, int signo)
904{
905 return StopInfoSP (new StopInfoUnixSignal (thread, signo));
906}
907
908StopInfoSP
909StopInfo::CreateStopReasonToTrace (Thread &thread)
910{
911 return StopInfoSP (new StopInfoTrace (thread));
912}
913
914StopInfoSP
Jim Ingham1586d972011-12-17 01:35:57 +0000915StopInfo::CreateStopReasonWithPlan (ThreadPlanSP &plan_sp, ValueObjectSP return_valobj_sp)
Greg Clayton643ee732010-08-04 01:40:35 +0000916{
Jim Ingham1586d972011-12-17 01:35:57 +0000917 return StopInfoSP (new StopInfoThreadPlan (plan_sp, return_valobj_sp));
Greg Clayton643ee732010-08-04 01:40:35 +0000918}
Greg Clayton65611552011-06-04 01:26:29 +0000919
920StopInfoSP
921StopInfo::CreateStopReasonWithException (Thread &thread, const char *description)
922{
923 return StopInfoSP (new StopInfoException (thread, description));
924}
Jim Ingham1586d972011-12-17 01:35:57 +0000925
926ValueObjectSP
927StopInfo::GetReturnValueObject(StopInfoSP &stop_info_sp)
928{
929 if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonPlanComplete)
930 {
931 StopInfoThreadPlan *plan_stop_info = static_cast<StopInfoThreadPlan *>(stop_info_sp.get());
932 return plan_stop_info->GetReturnValueObject();
933 }
934 else
935 return ValueObjectSP();
936}