blob: d5ef53b46cb3f2f9b04b42b0ad287152edd3ca99 [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
137 ShouldStop (Event *event_ptr)
138 {
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
163 virtual void
164 PerformAction (Event *event_ptr)
165 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000166 if (!m_should_perform_action)
167 return;
168 m_should_perform_action = false;
169
Jim Ingham21f37ad2011-08-09 02:12:22 +0000170 LogSP log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
Jim Ingham21f37ad2011-08-09 02:12:22 +0000171
Greg Claytonf4124de2012-02-21 00:09:25 +0000172 BreakpointSiteSP bp_site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (m_value));
Jim Ingham982a6ca2011-12-09 04:17:31 +0000173
Jim Ingham6fb8baa2010-08-10 00:59:59 +0000174 if (bp_site_sp)
175 {
176 size_t num_owners = bp_site_sp->GetNumberOfOwners();
Jim Ingham982a6ca2011-12-09 04:17:31 +0000177
178 if (num_owners == 0)
Jim Ingham6fb8baa2010-08-10 00:59:59 +0000179 {
Jim Ingham982a6ca2011-12-09 04:17:31 +0000180 m_should_stop = true;
181 }
182 else
183 {
184 // We go through each location, and test first its condition. If the condition says to stop,
185 // then we run the callback for that location. If that callback says to stop as well, then
186 // we set m_should_stop to true; we are going to stop.
187 // But we still want to give all the breakpoints whose conditions say we are going to stop a
188 // chance to run their callbacks.
189 // Of course if any callback restarts the target by putting "continue" in the callback, then
190 // we're going to restart, without running the rest of the callbacks. And in this case we will
191 // end up not stopping even if another location said we should stop. But that's better than not
192 // running all the callbacks.
193
194 m_should_stop = false;
195
Greg Claytonf4124de2012-02-21 00:09:25 +0000196 ExecutionContext exe_ctx (m_thread.GetStackFrameAtIndex(0));
197 StoppointCallbackContext context (event_ptr, exe_ctx, false);
Jim Ingham21f37ad2011-08-09 02:12:22 +0000198
Jim Ingham21f37ad2011-08-09 02:12:22 +0000199 for (size_t j = 0; j < num_owners; j++)
200 {
201 lldb::BreakpointLocationSP bp_loc_sp = bp_site_sp->GetOwnerAtIndex(j);
Jim Ingham982a6ca2011-12-09 04:17:31 +0000202
203 // First run the condition for the breakpoint. If that says we should stop, then we'll run
204 // the callback for the breakpoint. If the callback says we shouldn't stop that will win.
205
206 bool condition_says_stop = true;
Jim Ingham21f37ad2011-08-09 02:12:22 +0000207 if (bp_loc_sp->GetConditionText() != NULL)
208 {
209 // We need to make sure the user sees any parse errors in their condition, so we'll hook the
210 // constructor errors up to the debugger's Async I/O.
211
Jim Ingham21f37ad2011-08-09 02:12:22 +0000212 ValueObjectSP result_valobj_sp;
213
214 ExecutionResults result_code;
215 ValueObjectSP result_value_sp;
216 const bool discard_on_error = true;
217 Error error;
Greg Claytonf4124de2012-02-21 00:09:25 +0000218 result_code = ClangUserExpression::EvaluateWithError (exe_ctx,
Sean Callanan47dc4572011-09-15 02:13:07 +0000219 eExecutionPolicyAlways,
Sean Callanan5b658cc2011-11-07 23:35:40 +0000220 lldb::eLanguageTypeUnknown,
Sean Callanandaa6efe2011-12-21 22:22:58 +0000221 ClangUserExpression::eResultTypeAny,
Sean Callanan47dc4572011-09-15 02:13:07 +0000222 discard_on_error,
223 bp_loc_sp->GetConditionText(),
224 NULL,
225 result_value_sp,
226 error);
Jim Ingham21f37ad2011-08-09 02:12:22 +0000227 if (result_code == eExecutionCompleted)
228 {
229 if (result_value_sp)
230 {
231 Scalar scalar_value;
232 if (result_value_sp->ResolveValue (scalar_value))
233 {
234 if (scalar_value.ULongLong(1) == 0)
Jim Ingham982a6ca2011-12-09 04:17:31 +0000235 condition_says_stop = false;
Jim Ingham21f37ad2011-08-09 02:12:22 +0000236 else
Jim Ingham982a6ca2011-12-09 04:17:31 +0000237 condition_says_stop = true;
Jim Ingham21f37ad2011-08-09 02:12:22 +0000238 if (log)
239 log->Printf("Condition successfully evaluated, result is %s.\n",
240 m_should_stop ? "true" : "false");
241 }
242 else
243 {
Jim Ingham982a6ca2011-12-09 04:17:31 +0000244 condition_says_stop = true;
Jim Ingham21f37ad2011-08-09 02:12:22 +0000245 if (log)
246 log->Printf("Failed to get an integer result from the expression.");
247 }
248 }
249 }
250 else
251 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000252 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
Jim Ingham21f37ad2011-08-09 02:12:22 +0000253 StreamSP error_sp = debugger.GetAsyncErrorStream ();
254 error_sp->Printf ("Stopped due to an error evaluating condition of breakpoint ");
255 bp_loc_sp->GetDescription (error_sp.get(), eDescriptionLevelBrief);
256 error_sp->Printf (": \"%s\"",
257 bp_loc_sp->GetConditionText());
258 error_sp->EOL();
259 const char *err_str = error.AsCString("<Unknown Error>");
260 if (log)
261 log->Printf("Error evaluating condition: \"%s\"\n", err_str);
262
263 error_sp->PutCString (err_str);
264 error_sp->EOL();
265 error_sp->Flush();
266 // If the condition fails to be parsed or run, we should stop.
Jim Ingham982a6ca2011-12-09 04:17:31 +0000267 condition_says_stop = true;
Jim Ingham21f37ad2011-08-09 02:12:22 +0000268 }
269 }
270
Jim Ingham982a6ca2011-12-09 04:17:31 +0000271 // If this location's condition says we should aren't going to stop,
272 // then don't run the callback for this location.
273 if (!condition_says_stop)
274 continue;
275
276 bool callback_says_stop;
277
278 // FIXME: For now the callbacks have to run in async mode - the first time we restart we need
279 // to get out of there. So set it here.
280 // When we figure out how to nest breakpoint hits then this will change.
281
Greg Claytonf4124de2012-02-21 00:09:25 +0000282 Debugger &debugger = m_thread.CalculateTarget()->GetDebugger();
Jim Ingham982a6ca2011-12-09 04:17:31 +0000283 bool old_async = debugger.GetAsyncExecution();
284 debugger.SetAsyncExecution (true);
285
286 callback_says_stop = bp_loc_sp->InvokeCallback (&context);
287
288 debugger.SetAsyncExecution (old_async);
289
290 if (callback_says_stop)
291 m_should_stop = true;
292
293 // Also make sure that the callback hasn't continued the target.
294 // If it did, when we'll set m_should_start to false and get out of here.
295 if (HasTargetRunSinceMe ())
296 {
297 m_should_stop = false;
Jim Ingham21f37ad2011-08-09 02:12:22 +0000298 break;
Jim Ingham982a6ca2011-12-09 04:17:31 +0000299 }
Jim Ingham21f37ad2011-08-09 02:12:22 +0000300 }
Jim Ingham6fb8baa2010-08-10 00:59:59 +0000301 }
Jim Ingham982a6ca2011-12-09 04:17:31 +0000302 // We've figured out what this stop wants to do, so mark it as valid so we don't compute it again.
303 m_should_stop_is_valid = true;
304
Jim Ingham6fb8baa2010-08-10 00:59:59 +0000305 }
306 else
307 {
Jim Ingham982a6ca2011-12-09 04:17:31 +0000308 m_should_stop = true;
309 m_should_stop_is_valid = true;
Greg Claytone005f2c2010-11-06 01:53:30 +0000310 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Jim Ingham6fb8baa2010-08-10 00:59:59 +0000311
312 if (log)
313 log->Printf ("Process::%s could not find breakpoint site id: %lld...", __FUNCTION__, m_value);
314 }
Jim Ingham21f37ad2011-08-09 02:12:22 +0000315 if (log)
316 log->Printf ("Process::%s returning from action with m_should_stop: %d.", __FUNCTION__, m_should_stop);
Jim Ingham6fb8baa2010-08-10 00:59:59 +0000317 }
Greg Clayton643ee732010-08-04 01:40:35 +0000318
319 virtual bool
320 ShouldNotify (Event *event_ptr)
321 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000322 BreakpointSiteSP bp_site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (m_value));
Greg Clayton643ee732010-08-04 01:40:35 +0000323 if (bp_site_sp)
324 {
325 bool all_internal = true;
326
327 for (uint32_t i = 0; i < bp_site_sp->GetNumberOfOwners(); i++)
328 {
329 if (!bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint().IsInternal())
330 {
331 all_internal = false;
332 break;
333 }
334 }
335 return all_internal == false;
336 }
337 return true;
338 }
339
340 virtual const char *
341 GetDescription ()
342 {
343 if (m_description.empty())
344 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000345 BreakpointSiteSP bp_site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (m_value));
Jim Ingham6fb8baa2010-08-10 00:59:59 +0000346 if (bp_site_sp)
347 {
348 StreamString strm;
349 strm.Printf("breakpoint ");
350 bp_site_sp->GetDescription(&strm, eDescriptionLevelBrief);
351 m_description.swap (strm.GetString());
352 }
353 else
354 {
355 StreamString strm;
Jim Ingham1c658532011-10-28 01:12:19 +0000356 if (m_address == LLDB_INVALID_ADDRESS)
357 strm.Printf("breakpoint site %lli which has been deleted - unknown address", m_value);
358 else
359 strm.Printf("breakpoint site %lli which has been deleted - was at 0x%llx", m_value, m_address);
Jim Ingham6fb8baa2010-08-10 00:59:59 +0000360 m_description.swap (strm.GetString());
361 }
Greg Clayton643ee732010-08-04 01:40:35 +0000362 }
363 return m_description.c_str();
364 }
365
366private:
367 std::string m_description;
368 bool m_should_stop;
369 bool m_should_stop_is_valid;
Jim Inghamf9f40c22011-02-08 05:20:59 +0000370 bool m_should_perform_action; // Since we are trying to preserve the "state" of the system even if we run functions
371 // etc. behind the users backs, we need to make sure we only REALLY perform the action once.
Jim Ingham1c658532011-10-28 01:12:19 +0000372 lldb::addr_t m_address; // We use this to capture the breakpoint site address when we create the StopInfo,
373 // in case somebody deletes it between the time the StopInfo is made and the
374 // description is asked for.
Greg Clayton643ee732010-08-04 01:40:35 +0000375};
376
377
378//----------------------------------------------------------------------
379// StopInfoWatchpoint
380//----------------------------------------------------------------------
381
382class StopInfoWatchpoint : public StopInfo
383{
384public:
385
386 StopInfoWatchpoint (Thread &thread, break_id_t watch_id) :
Johnny Chen043f8c22011-09-21 22:47:15 +0000387 StopInfo(thread, watch_id),
388 m_description(),
389 m_should_stop(false),
390 m_should_stop_is_valid(false)
Greg Clayton643ee732010-08-04 01:40:35 +0000391 {
392 }
393
394 virtual ~StopInfoWatchpoint ()
395 {
396 }
397
398 virtual StopReason
399 GetStopReason () const
400 {
401 return eStopReasonWatchpoint;
402 }
403
Johnny Chen043f8c22011-09-21 22:47:15 +0000404 virtual bool
405 ShouldStop (Event *event_ptr)
406 {
407 // ShouldStop() method is idempotent and should not affect hit count.
Johnny Chen712a6282011-10-17 18:58:00 +0000408 // See Process::RunPrivateStateThread()->Process()->HandlePrivateEvent()
409 // -->Process()::ShouldBroadcastEvent()->ThreadList::ShouldStop()->
410 // Thread::ShouldStop()->ThreadPlanBase::ShouldStop()->
411 // StopInfoWatchpoint::ShouldStop() and
412 // Event::DoOnRemoval()->Process::ProcessEventData::DoOnRemoval()->
413 // StopInfoWatchpoint::PerformAction().
Johnny Chen043f8c22011-09-21 22:47:15 +0000414 if (m_should_stop_is_valid)
415 return m_should_stop;
416
Johnny Chenecd4feb2011-10-14 00:42:25 +0000417 WatchpointSP wp_sp =
Greg Claytonf4124de2012-02-21 00:09:25 +0000418 m_thread.CalculateTarget()->GetWatchpointList().FindByID(GetValue());
Johnny Chenecd4feb2011-10-14 00:42:25 +0000419 if (wp_sp)
Johnny Chen043f8c22011-09-21 22:47:15 +0000420 {
421 // Check if we should stop at a watchpoint.
Greg Claytonf4124de2012-02-21 00:09:25 +0000422 ExecutionContext exe_ctx (m_thread.GetStackFrameAtIndex(0));
423 StoppointCallbackContext context (event_ptr, exe_ctx, true);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000424 m_should_stop = wp_sp->ShouldStop (&context);
Johnny Chen043f8c22011-09-21 22:47:15 +0000425 }
426 else
427 {
428 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
429
430 if (log)
431 log->Printf ("Process::%s could not find watchpoint location id: %lld...",
432 __FUNCTION__, GetValue());
433
434 m_should_stop = true;
435 }
436 m_should_stop_is_valid = true;
437 return m_should_stop;
438 }
439
Johnny Chen712a6282011-10-17 18:58:00 +0000440 // Perform any action that is associated with this stop. This is done as the
441 // Event is removed from the event queue.
442 virtual void
443 PerformAction (Event *event_ptr)
444 {
445 LogSP log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
446 // We're going to calculate if we should stop or not in some way during the course of
447 // this code. Also by default we're going to stop, so set that here.
448 m_should_stop = true;
449
450 WatchpointSP wp_sp =
Greg Claytonf4124de2012-02-21 00:09:25 +0000451 m_thread.CalculateTarget()->GetWatchpointList().FindByID(GetValue());
Johnny Chen712a6282011-10-17 18:58:00 +0000452 if (wp_sp)
453 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000454 ExecutionContext exe_ctx (m_thread.GetStackFrameAtIndex(0));
455 StoppointCallbackContext context (event_ptr, exe_ctx, false);
Johnny Chen712a6282011-10-17 18:58:00 +0000456 bool stop_requested = wp_sp->InvokeCallback (&context);
457 // Also make sure that the callback hasn't continued the target.
458 // If it did, when we'll set m_should_start to false and get out of here.
Jim Ingham0296fe72011-11-08 03:00:11 +0000459 if (HasTargetRunSinceMe ())
Johnny Chen712a6282011-10-17 18:58:00 +0000460 m_should_stop = false;
461
462 if (m_should_stop && !stop_requested)
463 {
464 // We have been vetoed.
465 m_should_stop = false;
466 }
467
468 if (m_should_stop && wp_sp->GetConditionText() != NULL)
469 {
470 // We need to make sure the user sees any parse errors in their condition, so we'll hook the
471 // constructor errors up to the debugger's Async I/O.
Johnny Chen712a6282011-10-17 18:58:00 +0000472 ExecutionResults result_code;
473 ValueObjectSP result_value_sp;
474 const bool discard_on_error = true;
475 Error error;
Greg Claytonf4124de2012-02-21 00:09:25 +0000476 result_code = ClangUserExpression::EvaluateWithError (exe_ctx,
Johnny Chen712a6282011-10-17 18:58:00 +0000477 eExecutionPolicyAlways,
Sean Callanan5b658cc2011-11-07 23:35:40 +0000478 lldb::eLanguageTypeUnknown,
Sean Callanandaa6efe2011-12-21 22:22:58 +0000479 ClangUserExpression::eResultTypeAny,
Johnny Chen712a6282011-10-17 18:58:00 +0000480 discard_on_error,
481 wp_sp->GetConditionText(),
482 NULL,
483 result_value_sp,
484 error);
485 if (result_code == eExecutionCompleted)
486 {
487 if (result_value_sp)
488 {
489 Scalar scalar_value;
490 if (result_value_sp->ResolveValue (scalar_value))
491 {
492 if (scalar_value.ULongLong(1) == 0)
493 {
494 // We have been vetoed. This takes precedence over querying
495 // the watchpoint whether it should stop (aka ignore count and
496 // friends). See also StopInfoWatchpoint::ShouldStop() as well
497 // as Process::ProcessEventData::DoOnRemoval().
498 m_should_stop = false;
499 }
500 else
501 m_should_stop = true;
502 if (log)
503 log->Printf("Condition successfully evaluated, result is %s.\n",
504 m_should_stop ? "true" : "false");
505 }
506 else
507 {
508 m_should_stop = true;
509 if (log)
510 log->Printf("Failed to get an integer result from the expression.");
511 }
512 }
513 }
514 else
515 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000516 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
Johnny Chen712a6282011-10-17 18:58:00 +0000517 StreamSP error_sp = debugger.GetAsyncErrorStream ();
Johnny Chen0c5c43b2012-01-24 23:19:25 +0000518 error_sp->Printf ("Stopped due to an error evaluating condition of watchpoint ");
Johnny Chen712a6282011-10-17 18:58:00 +0000519 wp_sp->GetDescription (error_sp.get(), eDescriptionLevelBrief);
520 error_sp->Printf (": \"%s\"",
521 wp_sp->GetConditionText());
522 error_sp->EOL();
523 const char *err_str = error.AsCString("<Unknown Error>");
524 if (log)
525 log->Printf("Error evaluating condition: \"%s\"\n", err_str);
526
527 error_sp->PutCString (err_str);
528 error_sp->EOL();
529 error_sp->Flush();
530 // If the condition fails to be parsed or run, we should stop.
531 m_should_stop = true;
532 }
533 }
534 }
535 else
536 {
537 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
538
539 if (log)
540 log->Printf ("Process::%s could not find watchpoint id: %lld...", __FUNCTION__, m_value);
541 }
542 if (log)
543 log->Printf ("Process::%s returning from action with m_should_stop: %d.", __FUNCTION__, m_should_stop);
544 }
545
Greg Clayton643ee732010-08-04 01:40:35 +0000546 virtual const char *
547 GetDescription ()
548 {
549 if (m_description.empty())
550 {
551 StreamString strm;
552 strm.Printf("watchpoint %lli", m_value);
553 m_description.swap (strm.GetString());
554 }
555 return m_description.c_str();
556 }
557
Greg Clayton643ee732010-08-04 01:40:35 +0000558private:
559 std::string m_description;
Johnny Chen043f8c22011-09-21 22:47:15 +0000560 bool m_should_stop;
561 bool m_should_stop_is_valid;
Greg Clayton643ee732010-08-04 01:40:35 +0000562};
563
564
565
566//----------------------------------------------------------------------
567// StopInfoUnixSignal
568//----------------------------------------------------------------------
569
570class StopInfoUnixSignal : public StopInfo
571{
572public:
573
574 StopInfoUnixSignal (Thread &thread, int signo) :
Greg Clayton65611552011-06-04 01:26:29 +0000575 StopInfo (thread, signo)
Greg Clayton643ee732010-08-04 01:40:35 +0000576 {
577 }
578
579 virtual ~StopInfoUnixSignal ()
580 {
581 }
582
583
584 virtual StopReason
585 GetStopReason () const
586 {
587 return eStopReasonSignal;
588 }
589
590 virtual bool
591 ShouldStop (Event *event_ptr)
592 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000593 return m_thread.GetProcess()->GetUnixSignals().GetShouldStop (m_value);
Greg Clayton643ee732010-08-04 01:40:35 +0000594 }
595
596
597 // If should stop returns false, check if we should notify of this event
598 virtual bool
599 ShouldNotify (Event *event_ptr)
600 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000601 return m_thread.GetProcess()->GetUnixSignals().GetShouldNotify (m_value);
Greg Clayton643ee732010-08-04 01:40:35 +0000602 }
603
604
605 virtual void
606 WillResume (lldb::StateType resume_state)
607 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000608 if (m_thread.GetProcess()->GetUnixSignals().GetShouldSuppress(m_value) == false)
Greg Clayton643ee732010-08-04 01:40:35 +0000609 m_thread.SetResumeSignal(m_value);
610 }
611
612 virtual const char *
613 GetDescription ()
614 {
615 if (m_description.empty())
616 {
617 StreamString strm;
Greg Claytonf4124de2012-02-21 00:09:25 +0000618 const char *signal_name = m_thread.GetProcess()->GetUnixSignals().GetSignalAsCString (m_value);
Greg Clayton643ee732010-08-04 01:40:35 +0000619 if (signal_name)
Greg Claytona830adb2010-10-04 01:05:56 +0000620 strm.Printf("signal %s", signal_name);
Greg Clayton643ee732010-08-04 01:40:35 +0000621 else
Greg Claytona830adb2010-10-04 01:05:56 +0000622 strm.Printf("signal %lli", m_value);
Greg Clayton643ee732010-08-04 01:40:35 +0000623 m_description.swap (strm.GetString());
624 }
625 return m_description.c_str();
626 }
Greg Clayton643ee732010-08-04 01:40:35 +0000627};
628
629//----------------------------------------------------------------------
630// StopInfoTrace
631//----------------------------------------------------------------------
632
633class StopInfoTrace : public StopInfo
634{
635public:
636
637 StopInfoTrace (Thread &thread) :
638 StopInfo (thread, LLDB_INVALID_UID)
639 {
640 }
641
642 virtual ~StopInfoTrace ()
643 {
644 }
645
646 virtual StopReason
647 GetStopReason () const
648 {
649 return eStopReasonTrace;
650 }
651
652 virtual const char *
653 GetDescription ()
654 {
Greg Clayton65611552011-06-04 01:26:29 +0000655 if (m_description.empty())
Greg Clayton643ee732010-08-04 01:40:35 +0000656 return "trace";
Greg Clayton65611552011-06-04 01:26:29 +0000657 else
658 return m_description.c_str();
659 }
660};
661
662
663//----------------------------------------------------------------------
664// StopInfoException
665//----------------------------------------------------------------------
666
667class StopInfoException : public StopInfo
668{
669public:
670
671 StopInfoException (Thread &thread, const char *description) :
672 StopInfo (thread, LLDB_INVALID_UID)
673 {
674 if (description)
675 SetDescription (description);
676 }
677
678 virtual
679 ~StopInfoException ()
680 {
681 }
682
683 virtual StopReason
684 GetStopReason () const
685 {
686 return eStopReasonException;
687 }
688
689 virtual const char *
690 GetDescription ()
691 {
692 if (m_description.empty())
693 return "exception";
694 else
695 return m_description.c_str();
Greg Clayton643ee732010-08-04 01:40:35 +0000696 }
697};
698
699
700//----------------------------------------------------------------------
701// StopInfoThreadPlan
702//----------------------------------------------------------------------
703
704class StopInfoThreadPlan : public StopInfo
705{
706public:
707
Jim Ingham1586d972011-12-17 01:35:57 +0000708 StopInfoThreadPlan (ThreadPlanSP &plan_sp, ValueObjectSP &return_valobj_sp) :
Greg Clayton643ee732010-08-04 01:40:35 +0000709 StopInfo (plan_sp->GetThread(), LLDB_INVALID_UID),
Jim Ingham1586d972011-12-17 01:35:57 +0000710 m_plan_sp (plan_sp),
711 m_return_valobj_sp (return_valobj_sp)
Greg Clayton643ee732010-08-04 01:40:35 +0000712 {
713 }
714
715 virtual ~StopInfoThreadPlan ()
716 {
717 }
718
719 virtual StopReason
720 GetStopReason () const
721 {
722 return eStopReasonPlanComplete;
723 }
724
725 virtual const char *
726 GetDescription ()
727 {
728 if (m_description.empty())
729 {
730 StreamString strm;
731 m_plan_sp->GetDescription (&strm, eDescriptionLevelBrief);
732 m_description.swap (strm.GetString());
733 }
734 return m_description.c_str();
735 }
Jim Ingham1586d972011-12-17 01:35:57 +0000736
737 ValueObjectSP
738 GetReturnValueObject()
739 {
740 return m_return_valobj_sp;
741 }
Greg Clayton643ee732010-08-04 01:40:35 +0000742
743private:
744 ThreadPlanSP m_plan_sp;
Jim Ingham1586d972011-12-17 01:35:57 +0000745 ValueObjectSP m_return_valobj_sp;
Greg Clayton643ee732010-08-04 01:40:35 +0000746};
Jim Ingham21f37ad2011-08-09 02:12:22 +0000747} // namespace lldb_private
Greg Clayton643ee732010-08-04 01:40:35 +0000748
749StopInfoSP
750StopInfo::CreateStopReasonWithBreakpointSiteID (Thread &thread, break_id_t break_id)
751{
752 return StopInfoSP (new StopInfoBreakpoint (thread, break_id));
753}
754
755StopInfoSP
Jim Inghamd1686902010-10-14 23:45:03 +0000756StopInfo::CreateStopReasonWithBreakpointSiteID (Thread &thread, break_id_t break_id, bool should_stop)
757{
758 return StopInfoSP (new StopInfoBreakpoint (thread, break_id, should_stop));
759}
760
761StopInfoSP
Greg Clayton643ee732010-08-04 01:40:35 +0000762StopInfo::CreateStopReasonWithWatchpointID (Thread &thread, break_id_t watch_id)
763{
764 return StopInfoSP (new StopInfoWatchpoint (thread, watch_id));
765}
766
767StopInfoSP
768StopInfo::CreateStopReasonWithSignal (Thread &thread, int signo)
769{
770 return StopInfoSP (new StopInfoUnixSignal (thread, signo));
771}
772
773StopInfoSP
774StopInfo::CreateStopReasonToTrace (Thread &thread)
775{
776 return StopInfoSP (new StopInfoTrace (thread));
777}
778
779StopInfoSP
Jim Ingham1586d972011-12-17 01:35:57 +0000780StopInfo::CreateStopReasonWithPlan (ThreadPlanSP &plan_sp, ValueObjectSP return_valobj_sp)
Greg Clayton643ee732010-08-04 01:40:35 +0000781{
Jim Ingham1586d972011-12-17 01:35:57 +0000782 return StopInfoSP (new StopInfoThreadPlan (plan_sp, return_valobj_sp));
Greg Clayton643ee732010-08-04 01:40:35 +0000783}
Greg Clayton65611552011-06-04 01:26:29 +0000784
785StopInfoSP
786StopInfo::CreateStopReasonWithException (Thread &thread, const char *description)
787{
788 return StopInfoSP (new StopInfoException (thread, description));
789}
Jim Ingham1586d972011-12-17 01:35:57 +0000790
791ValueObjectSP
792StopInfo::GetReturnValueObject(StopInfoSP &stop_info_sp)
793{
794 if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonPlanComplete)
795 {
796 StopInfoThreadPlan *plan_stop_info = static_cast<StopInfoThreadPlan *>(stop_info_sp.get());
797 return plan_stop_info->GetReturnValueObject();
798 }
799 else
800 return ValueObjectSP();
801}