blob: 9d9bfd21b89f70e014205b24368cbab1c06846d2 [file] [log] [blame]
Greg Claytonf4b47e12010-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 Clayton4e78f602010-11-18 18:52:36 +000019#include "lldb/Breakpoint/Breakpoint.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000020#include "lldb/Breakpoint/BreakpointLocation.h"
21#include "lldb/Breakpoint/StoppointCallbackContext.h"
Johnny Chen01a67862011-10-14 00:42:25 +000022#include "lldb/Breakpoint/Watchpoint.h"
Jim Ingham4b536182011-08-09 02:12:22 +000023#include "lldb/Core/Debugger.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000024#include "lldb/Core/StreamString.h"
Jim Ingham4b536182011-08-09 02:12:22 +000025#include "lldb/Expression/ClangUserExpression.h"
26#include "lldb/Target/Target.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000027#include "lldb/Target/Thread.h"
28#include "lldb/Target/ThreadPlan.h"
29#include "lldb/Target/Process.h"
30#include "lldb/Target/UnixSignals.h"
31
32using namespace lldb;
33using namespace lldb_private;
34
35StopInfo::StopInfo (Thread &thread, uint64_t value) :
36 m_thread (thread),
Greg Clayton1ac04c32012-02-21 00:09:25 +000037 m_stop_id (thread.GetProcess()->GetStopID()),
38 m_resume_id (thread.GetProcess()->GetResumeID()),
Greg Claytonf4b47e12010-08-04 01:40:35 +000039 m_value (value)
40{
41}
42
43bool
44StopInfo::IsValid () const
45{
Greg Clayton1ac04c32012-02-21 00:09:25 +000046 return m_thread.GetProcess()->GetStopID() == m_stop_id;
Greg Claytonf4b47e12010-08-04 01:40:35 +000047}
48
Jim Ingham77787032011-01-20 02:03:18 +000049void
50StopInfo::MakeStopInfoValid ()
51{
Greg Clayton1ac04c32012-02-21 00:09:25 +000052 m_stop_id = m_thread.GetProcess()->GetStopID();
53 m_resume_id = m_thread.GetProcess()->GetResumeID();
Jim Ingham77787032011-01-20 02:03:18 +000054}
55
Jim Ingham0faa43f2011-11-08 03:00:11 +000056bool
57StopInfo::HasTargetRunSinceMe ()
Jim Ingham4b536182011-08-09 02:12:22 +000058{
Greg Clayton1ac04c32012-02-21 00:09:25 +000059 lldb::StateType ret_type = m_thread.GetProcess()->GetPrivateState();
Jim Ingham0faa43f2011-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 Clayton1ac04c32012-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 Ingham0faa43f2011-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 Ingham4b536182011-08-09 02:12:22 +000084}
85
Greg Claytonf4b47e12010-08-04 01:40:35 +000086//----------------------------------------------------------------------
87// StopInfoBreakpoint
88//----------------------------------------------------------------------
89
Jim Ingham4b536182011-08-09 02:12:22 +000090namespace lldb_private
91{
Greg Claytonf4b47e12010-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 Ingham0f16e732011-02-08 05:20:59 +0000100 m_should_stop_is_valid (false),
Jim Ingham52c7d202011-10-28 01:12:19 +0000101 m_should_perform_action (true),
Jim Inghamca36cd12012-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 Claytonf4b47e12010-08-04 01:40:35 +0000105 {
Jim Inghamca36cd12012-10-05 19:16:31 +0000106 StoreBPInfo();
Greg Claytonf4b47e12010-08-04 01:40:35 +0000107 }
108
Jim Ingham36f3b362010-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 Ingham0f16e732011-02-08 05:20:59 +0000113 m_should_stop_is_valid (true),
Jim Ingham52c7d202011-10-28 01:12:19 +0000114 m_should_perform_action (true),
Jim Inghamca36cd12012-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 Ingham36f3b362010-10-14 23:45:03 +0000123 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000124 BreakpointSiteSP bp_site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (m_value));
Jim Ingham52c7d202011-10-28 01:12:19 +0000125 if (bp_site_sp)
126 {
Jim Inghamca36cd12012-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 Ingham52c7d202011-10-28 01:12:19 +0000137 }
Jim Ingham36f3b362010-10-14 23:45:03 +0000138 }
139
Greg Claytonf4b47e12010-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 Ingham6d66ce62012-04-20 21:16:56 +0000151 ShouldStopSynchronous (Event *event_ptr)
Greg Claytonf4b47e12010-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 Clayton1ac04c32012-02-21 00:09:25 +0000156 BreakpointSiteSP bp_site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (m_value));
Greg Claytonf4b47e12010-08-04 01:40:35 +0000157 if (bp_site_sp)
158 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000159 ExecutionContext exe_ctx (m_thread.GetStackFrameAtIndex(0));
160 StoppointCallbackContext context (event_ptr, exe_ctx, true);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000161 m_should_stop = bp_site_sp->ShouldStop (&context);
162 }
163 else
164 {
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000165 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Claytonf4b47e12010-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 Ingham3ebcf7f2010-08-10 00:59:59 +0000176
Jim Inghamf57b4352012-11-10 02:08:14 +0000177 virtual bool
178 ShouldNotify (Event *event_ptr)
179 {
180 BreakpointSiteSP bp_site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (m_value));
181 if (bp_site_sp)
182 {
183 bool all_internal = true;
184
185 for (uint32_t i = 0; i < bp_site_sp->GetNumberOfOwners(); i++)
186 {
187 if (!bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint().IsInternal())
188 {
189 all_internal = false;
190 break;
191 }
192 }
193 return all_internal == false;
194 }
195 return true;
196 }
197
198 virtual const char *
199 GetDescription ()
200 {
201 if (m_description.empty())
202 {
203 BreakpointSiteSP bp_site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (m_value));
204 if (bp_site_sp)
205 {
206 StreamString strm;
207 strm.Printf("breakpoint ");
208 bp_site_sp->GetDescription(&strm, eDescriptionLevelBrief);
209 m_description.swap (strm.GetString());
210 }
211 else
212 {
213 StreamString strm;
214 if (m_break_id != LLDB_INVALID_BREAK_ID)
215 {
216 if (m_was_one_shot)
217 strm.Printf ("one-shot breakpoint %d", m_break_id);
218 else
219 strm.Printf ("breakpoint %d which has been deleted.", m_break_id);
220 }
221 else if (m_address == LLDB_INVALID_ADDRESS)
222 strm.Printf("breakpoint site %lli which has been deleted - unknown address", m_value);
223 else
224 strm.Printf("breakpoint site %lli which has been deleted - was at 0x%llx", m_value, m_address);
225
226 m_description.swap (strm.GetString());
227 }
228 }
229 return m_description.c_str();
230 }
231
232protected:
Jim Ingham6d66ce62012-04-20 21:16:56 +0000233 bool
234 ShouldStop (Event *event_ptr)
235 {
236 // This just reports the work done by PerformAction or the synchronous stop. It should
237 // only ever get called after they have had a chance to run.
238 assert (m_should_stop_is_valid);
239 return m_should_stop;
240 }
241
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000242 virtual void
243 PerformAction (Event *event_ptr)
244 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000245 if (!m_should_perform_action)
246 return;
247 m_should_perform_action = false;
248
Jim Ingham4b536182011-08-09 02:12:22 +0000249 LogSP log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
Jim Ingham4b536182011-08-09 02:12:22 +0000250
Greg Clayton1ac04c32012-02-21 00:09:25 +0000251 BreakpointSiteSP bp_site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (m_value));
Jim Ingham79ea1d82011-12-09 04:17:31 +0000252
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000253 if (bp_site_sp)
254 {
255 size_t num_owners = bp_site_sp->GetNumberOfOwners();
Jim Ingham79ea1d82011-12-09 04:17:31 +0000256
257 if (num_owners == 0)
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000258 {
Jim Ingham79ea1d82011-12-09 04:17:31 +0000259 m_should_stop = true;
260 }
261 else
262 {
263 // We go through each location, and test first its condition. If the condition says to stop,
264 // then we run the callback for that location. If that callback says to stop as well, then
265 // we set m_should_stop to true; we are going to stop.
266 // But we still want to give all the breakpoints whose conditions say we are going to stop a
267 // chance to run their callbacks.
268 // Of course if any callback restarts the target by putting "continue" in the callback, then
269 // we're going to restart, without running the rest of the callbacks. And in this case we will
270 // end up not stopping even if another location said we should stop. But that's better than not
271 // running all the callbacks.
272
273 m_should_stop = false;
274
Greg Clayton1ac04c32012-02-21 00:09:25 +0000275 ExecutionContext exe_ctx (m_thread.GetStackFrameAtIndex(0));
276 StoppointCallbackContext context (event_ptr, exe_ctx, false);
Jim Ingham4b536182011-08-09 02:12:22 +0000277
Jim Ingham4b536182011-08-09 02:12:22 +0000278 for (size_t j = 0; j < num_owners; j++)
279 {
280 lldb::BreakpointLocationSP bp_loc_sp = bp_site_sp->GetOwnerAtIndex(j);
Jim Ingham79ea1d82011-12-09 04:17:31 +0000281
282 // First run the condition for the breakpoint. If that says we should stop, then we'll run
283 // the callback for the breakpoint. If the callback says we shouldn't stop that will win.
284
285 bool condition_says_stop = true;
Jim Ingham4b536182011-08-09 02:12:22 +0000286 if (bp_loc_sp->GetConditionText() != NULL)
287 {
288 // We need to make sure the user sees any parse errors in their condition, so we'll hook the
289 // constructor errors up to the debugger's Async I/O.
290
Jim Ingham4b536182011-08-09 02:12:22 +0000291 ValueObjectSP result_valobj_sp;
292
293 ExecutionResults result_code;
294 ValueObjectSP result_value_sp;
295 const bool discard_on_error = true;
296 Error error;
Greg Clayton1ac04c32012-02-21 00:09:25 +0000297 result_code = ClangUserExpression::EvaluateWithError (exe_ctx,
Jim Ingham6d66ce62012-04-20 21:16:56 +0000298 eExecutionPolicyOnlyWhenNeeded,
Sean Callananc7b65062011-11-07 23:35:40 +0000299 lldb::eLanguageTypeUnknown,
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000300 ClangUserExpression::eResultTypeAny,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000301 discard_on_error,
302 bp_loc_sp->GetConditionText(),
303 NULL,
304 result_value_sp,
Greg Clayton26ab83d2012-10-31 20:49:04 +0000305 error,
306 true,
307 ClangUserExpression::kDefaultTimeout);
Jim Ingham4b536182011-08-09 02:12:22 +0000308 if (result_code == eExecutionCompleted)
309 {
310 if (result_value_sp)
311 {
312 Scalar scalar_value;
313 if (result_value_sp->ResolveValue (scalar_value))
314 {
315 if (scalar_value.ULongLong(1) == 0)
Jim Ingham79ea1d82011-12-09 04:17:31 +0000316 condition_says_stop = false;
Jim Ingham4b536182011-08-09 02:12:22 +0000317 else
Jim Ingham79ea1d82011-12-09 04:17:31 +0000318 condition_says_stop = true;
Jim Ingham4b536182011-08-09 02:12:22 +0000319 if (log)
320 log->Printf("Condition successfully evaluated, result is %s.\n",
321 m_should_stop ? "true" : "false");
322 }
323 else
324 {
Jim Ingham79ea1d82011-12-09 04:17:31 +0000325 condition_says_stop = true;
Jim Ingham4b536182011-08-09 02:12:22 +0000326 if (log)
327 log->Printf("Failed to get an integer result from the expression.");
328 }
329 }
330 }
331 else
332 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000333 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
Jim Ingham4b536182011-08-09 02:12:22 +0000334 StreamSP error_sp = debugger.GetAsyncErrorStream ();
335 error_sp->Printf ("Stopped due to an error evaluating condition of breakpoint ");
336 bp_loc_sp->GetDescription (error_sp.get(), eDescriptionLevelBrief);
337 error_sp->Printf (": \"%s\"",
338 bp_loc_sp->GetConditionText());
339 error_sp->EOL();
340 const char *err_str = error.AsCString("<Unknown Error>");
341 if (log)
342 log->Printf("Error evaluating condition: \"%s\"\n", err_str);
343
344 error_sp->PutCString (err_str);
345 error_sp->EOL();
346 error_sp->Flush();
347 // If the condition fails to be parsed or run, we should stop.
Jim Ingham79ea1d82011-12-09 04:17:31 +0000348 condition_says_stop = true;
Jim Ingham4b536182011-08-09 02:12:22 +0000349 }
350 }
351
Jim Ingham79ea1d82011-12-09 04:17:31 +0000352 // If this location's condition says we should aren't going to stop,
353 // then don't run the callback for this location.
354 if (!condition_says_stop)
355 continue;
356
357 bool callback_says_stop;
358
359 // FIXME: For now the callbacks have to run in async mode - the first time we restart we need
360 // to get out of there. So set it here.
361 // When we figure out how to nest breakpoint hits then this will change.
362
Greg Clayton1ac04c32012-02-21 00:09:25 +0000363 Debugger &debugger = m_thread.CalculateTarget()->GetDebugger();
Jim Ingham79ea1d82011-12-09 04:17:31 +0000364 bool old_async = debugger.GetAsyncExecution();
365 debugger.SetAsyncExecution (true);
366
367 callback_says_stop = bp_loc_sp->InvokeCallback (&context);
368
369 debugger.SetAsyncExecution (old_async);
370
371 if (callback_says_stop)
372 m_should_stop = true;
Jim Inghamca36cd12012-10-05 19:16:31 +0000373
374 // If we are going to stop for this breakpoint, then remove the breakpoint.
375 if (callback_says_stop && bp_loc_sp && bp_loc_sp->GetBreakpoint().IsOneShot())
376 {
377 m_thread.GetProcess()->GetTarget().RemoveBreakpointByID (bp_loc_sp->GetBreakpoint().GetID());
378 }
Jim Ingham79ea1d82011-12-09 04:17:31 +0000379
380 // Also make sure that the callback hasn't continued the target.
381 // If it did, when we'll set m_should_start to false and get out of here.
382 if (HasTargetRunSinceMe ())
383 {
384 m_should_stop = false;
Jim Ingham4b536182011-08-09 02:12:22 +0000385 break;
Jim Ingham79ea1d82011-12-09 04:17:31 +0000386 }
Jim Ingham4b536182011-08-09 02:12:22 +0000387 }
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000388 }
Jim Ingham79ea1d82011-12-09 04:17:31 +0000389 // We've figured out what this stop wants to do, so mark it as valid so we don't compute it again.
390 m_should_stop_is_valid = true;
391
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000392 }
393 else
394 {
Jim Ingham79ea1d82011-12-09 04:17:31 +0000395 m_should_stop = true;
396 m_should_stop_is_valid = true;
Jason Molendaccd41e52012-10-04 22:47:07 +0000397 LogSP log_process(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000398
Jason Molendaccd41e52012-10-04 22:47:07 +0000399 if (log_process)
400 log_process->Printf ("Process::%s could not find breakpoint site id: %lld...", __FUNCTION__, m_value);
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000401 }
Jim Ingham4b536182011-08-09 02:12:22 +0000402 if (log)
403 log->Printf ("Process::%s returning from action with m_should_stop: %d.", __FUNCTION__, m_should_stop);
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000404 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000405
406private:
407 std::string m_description;
408 bool m_should_stop;
409 bool m_should_stop_is_valid;
Jim Ingham0f16e732011-02-08 05:20:59 +0000410 bool m_should_perform_action; // Since we are trying to preserve the "state" of the system even if we run functions
411 // etc. behind the users backs, we need to make sure we only REALLY perform the action once.
Jim Ingham52c7d202011-10-28 01:12:19 +0000412 lldb::addr_t m_address; // We use this to capture the breakpoint site address when we create the StopInfo,
413 // in case somebody deletes it between the time the StopInfo is made and the
414 // description is asked for.
Jim Inghamca36cd12012-10-05 19:16:31 +0000415 lldb::break_id_t m_break_id;
416 bool m_was_one_shot;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000417};
418
419
420//----------------------------------------------------------------------
421// StopInfoWatchpoint
422//----------------------------------------------------------------------
423
424class StopInfoWatchpoint : public StopInfo
425{
426public:
Jim Inghamf57b4352012-11-10 02:08:14 +0000427 // Make sure watchpoint is properly disabled and subsequently enabled while performing watchpoint actions.
428 class WatchpointSentry {
429 public:
430 WatchpointSentry(Process *p, Watchpoint *w):
431 process(p),
432 watchpoint(w)
433 {
434 if (process && watchpoint)
435 {
436 watchpoint->TurnOnEphemeralMode();
437 process->DisableWatchpoint(watchpoint);
438 }
439 }
440 ~WatchpointSentry()
441 {
442 if (process && watchpoint)
443 {
444 if (!watchpoint->IsDisabledDuringEphemeralMode())
445 process->EnableWatchpoint(watchpoint);
446 watchpoint->TurnOffEphemeralMode();
447 }
448 }
449 private:
450 Process *process;
451 Watchpoint *watchpoint;
452 };
Greg Claytonf4b47e12010-08-04 01:40:35 +0000453
454 StopInfoWatchpoint (Thread &thread, break_id_t watch_id) :
Johnny Chenfd158f42011-09-21 22:47:15 +0000455 StopInfo(thread, watch_id),
456 m_description(),
457 m_should_stop(false),
458 m_should_stop_is_valid(false)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000459 {
460 }
461
462 virtual ~StopInfoWatchpoint ()
463 {
464 }
465
466 virtual StopReason
467 GetStopReason () const
468 {
469 return eStopReasonWatchpoint;
470 }
471
Jim Inghamf57b4352012-11-10 02:08:14 +0000472 virtual const char *
473 GetDescription ()
474 {
475 if (m_description.empty())
476 {
477 StreamString strm;
478 strm.Printf("watchpoint %lli", m_value);
479 m_description.swap (strm.GetString());
480 }
481 return m_description.c_str();
482 }
483
484protected:
Johnny Chenfd158f42011-09-21 22:47:15 +0000485 virtual bool
486 ShouldStop (Event *event_ptr)
487 {
488 // ShouldStop() method is idempotent and should not affect hit count.
Johnny Chen16dcf712011-10-17 18:58:00 +0000489 // See Process::RunPrivateStateThread()->Process()->HandlePrivateEvent()
490 // -->Process()::ShouldBroadcastEvent()->ThreadList::ShouldStop()->
491 // Thread::ShouldStop()->ThreadPlanBase::ShouldStop()->
492 // StopInfoWatchpoint::ShouldStop() and
493 // Event::DoOnRemoval()->Process::ProcessEventData::DoOnRemoval()->
494 // StopInfoWatchpoint::PerformAction().
Johnny Chenfd158f42011-09-21 22:47:15 +0000495 if (m_should_stop_is_valid)
496 return m_should_stop;
497
Johnny Chen01a67862011-10-14 00:42:25 +0000498 WatchpointSP wp_sp =
Greg Clayton1ac04c32012-02-21 00:09:25 +0000499 m_thread.CalculateTarget()->GetWatchpointList().FindByID(GetValue());
Johnny Chen01a67862011-10-14 00:42:25 +0000500 if (wp_sp)
Johnny Chenfd158f42011-09-21 22:47:15 +0000501 {
502 // Check if we should stop at a watchpoint.
Greg Clayton1ac04c32012-02-21 00:09:25 +0000503 ExecutionContext exe_ctx (m_thread.GetStackFrameAtIndex(0));
504 StoppointCallbackContext context (event_ptr, exe_ctx, true);
Johnny Chen01a67862011-10-14 00:42:25 +0000505 m_should_stop = wp_sp->ShouldStop (&context);
Johnny Chenfd158f42011-09-21 22:47:15 +0000506 }
507 else
508 {
509 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
510
511 if (log)
512 log->Printf ("Process::%s could not find watchpoint location id: %lld...",
513 __FUNCTION__, GetValue());
514
515 m_should_stop = true;
516 }
517 m_should_stop_is_valid = true;
518 return m_should_stop;
519 }
520
Johnny Chen16dcf712011-10-17 18:58:00 +0000521 virtual void
522 PerformAction (Event *event_ptr)
523 {
Johnny Chen209bd652012-08-13 21:09:54 +0000524 LogSP log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS);
Johnny Chen16dcf712011-10-17 18:58:00 +0000525 // We're going to calculate if we should stop or not in some way during the course of
526 // this code. Also by default we're going to stop, so set that here.
527 m_should_stop = true;
528
529 WatchpointSP wp_sp =
Greg Clayton1ac04c32012-02-21 00:09:25 +0000530 m_thread.CalculateTarget()->GetWatchpointList().FindByID(GetValue());
Johnny Chen16dcf712011-10-17 18:58:00 +0000531 if (wp_sp)
532 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000533 ExecutionContext exe_ctx (m_thread.GetStackFrameAtIndex(0));
Johnny Chen209bd652012-08-13 21:09:54 +0000534 Process* process = exe_ctx.GetProcessPtr();
Johnny Chen0f7ad8d2012-08-21 22:06:34 +0000535
Johnny Chen66535a32012-08-21 22:15:58 +0000536 // This sentry object makes sure the current watchpoint is disabled while performing watchpoint actions,
537 // and it is then enabled after we are finished.
Johnny Chen0f7ad8d2012-08-21 22:06:34 +0000538 WatchpointSentry sentry(process, wp_sp.get());
539
Enrico Granataf04a2192012-07-13 23:18:48 +0000540 {
541 // check if this process is running on an architecture where watchpoints trigger
542 // before the associated instruction runs. if so, disable the WP, single-step and then
543 // re-enable the watchpoint
Enrico Granataf04a2192012-07-13 23:18:48 +0000544 if (process)
545 {
546 uint32_t num; bool wp_triggers_after;
547 if (process->GetWatchpointSupportInfo(num, wp_triggers_after).Success())
548 {
549 if (!wp_triggers_after)
550 {
Enrico Granataf04a2192012-07-13 23:18:48 +0000551 ThreadPlan *new_plan = m_thread.QueueThreadPlanForStepSingleInstruction(false, // step-over
552 false, // abort_other_plans
553 true); // stop_other_threads
554 new_plan->SetIsMasterPlan (true);
555 new_plan->SetOkayToDiscard (false);
556 process->GetThreadList().SetSelectedThreadByID (m_thread.GetID());
557 process->Resume ();
558 process->WaitForProcessToStop (NULL);
559 process->GetThreadList().SetSelectedThreadByID (m_thread.GetID());
560 MakeStopInfoValid(); // make sure we do not fail to stop because of the single-step taken above
Enrico Granataf04a2192012-07-13 23:18:48 +0000561 }
562 }
563 }
564 }
Johnny Chen209bd652012-08-13 21:09:54 +0000565
Johnny Chen16dcf712011-10-17 18:58:00 +0000566 if (m_should_stop && wp_sp->GetConditionText() != NULL)
567 {
568 // We need to make sure the user sees any parse errors in their condition, so we'll hook the
569 // constructor errors up to the debugger's Async I/O.
Johnny Chen16dcf712011-10-17 18:58:00 +0000570 ExecutionResults result_code;
571 ValueObjectSP result_value_sp;
572 const bool discard_on_error = true;
573 Error error;
Greg Clayton1ac04c32012-02-21 00:09:25 +0000574 result_code = ClangUserExpression::EvaluateWithError (exe_ctx,
Sean Callanan22019052012-09-08 01:51:48 +0000575 eExecutionPolicyOnlyWhenNeeded,
Sean Callananc7b65062011-11-07 23:35:40 +0000576 lldb::eLanguageTypeUnknown,
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000577 ClangUserExpression::eResultTypeAny,
Johnny Chen16dcf712011-10-17 18:58:00 +0000578 discard_on_error,
579 wp_sp->GetConditionText(),
580 NULL,
581 result_value_sp,
Enrico Granata3372f582012-07-16 23:10:35 +0000582 error,
Greg Clayton26ab83d2012-10-31 20:49:04 +0000583 true,
584 ClangUserExpression::kDefaultTimeout);
Johnny Chen16dcf712011-10-17 18:58:00 +0000585 if (result_code == eExecutionCompleted)
586 {
587 if (result_value_sp)
588 {
589 Scalar scalar_value;
590 if (result_value_sp->ResolveValue (scalar_value))
591 {
592 if (scalar_value.ULongLong(1) == 0)
593 {
594 // We have been vetoed. This takes precedence over querying
595 // the watchpoint whether it should stop (aka ignore count and
596 // friends). See also StopInfoWatchpoint::ShouldStop() as well
597 // as Process::ProcessEventData::DoOnRemoval().
598 m_should_stop = false;
599 }
600 else
601 m_should_stop = true;
602 if (log)
603 log->Printf("Condition successfully evaluated, result is %s.\n",
604 m_should_stop ? "true" : "false");
605 }
606 else
607 {
608 m_should_stop = true;
609 if (log)
610 log->Printf("Failed to get an integer result from the expression.");
611 }
612 }
613 }
614 else
615 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000616 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
Johnny Chen16dcf712011-10-17 18:58:00 +0000617 StreamSP error_sp = debugger.GetAsyncErrorStream ();
Johnny Chen5f3bf632012-01-24 23:19:25 +0000618 error_sp->Printf ("Stopped due to an error evaluating condition of watchpoint ");
Johnny Chen16dcf712011-10-17 18:58:00 +0000619 wp_sp->GetDescription (error_sp.get(), eDescriptionLevelBrief);
620 error_sp->Printf (": \"%s\"",
621 wp_sp->GetConditionText());
622 error_sp->EOL();
623 const char *err_str = error.AsCString("<Unknown Error>");
624 if (log)
625 log->Printf("Error evaluating condition: \"%s\"\n", err_str);
626
627 error_sp->PutCString (err_str);
628 error_sp->EOL();
629 error_sp->Flush();
630 // If the condition fails to be parsed or run, we should stop.
631 m_should_stop = true;
632 }
633 }
Johnny Chen13206412012-08-09 23:10:57 +0000634
635 // If the condition says to stop, we run the callback to further decide whether to stop.
636 if (m_should_stop)
637 {
Johnny Chen209bd652012-08-13 21:09:54 +0000638 StoppointCallbackContext context (event_ptr, exe_ctx, false);
Johnny Chen13206412012-08-09 23:10:57 +0000639 bool stop_requested = wp_sp->InvokeCallback (&context);
640 // Also make sure that the callback hasn't continued the target.
641 // If it did, when we'll set m_should_stop to false and get out of here.
642 if (HasTargetRunSinceMe ())
643 m_should_stop = false;
644
645 if (m_should_stop && !stop_requested)
646 {
647 // We have been vetoed by the callback mechanism.
648 m_should_stop = false;
649 }
650 }
Jim Inghama7dfb662012-10-23 07:20:06 +0000651 // Finally, if we are going to stop, print out the new & old values:
652 if (m_should_stop)
653 {
654 wp_sp->CaptureWatchedValue(exe_ctx);
655
656 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
657 StreamSP output_sp = debugger.GetAsyncOutputStream ();
658 wp_sp->DumpSnapshots(output_sp.get());
659 output_sp->EOL();
660 output_sp->Flush();
661 }
662
Johnny Chen16dcf712011-10-17 18:58:00 +0000663 }
664 else
665 {
Jason Molendaccd41e52012-10-04 22:47:07 +0000666 LogSP log_process(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Johnny Chen16dcf712011-10-17 18:58:00 +0000667
Jason Molendaccd41e52012-10-04 22:47:07 +0000668 if (log_process)
669 log_process->Printf ("Process::%s could not find watchpoint id: %lld...", __FUNCTION__, m_value);
Johnny Chen16dcf712011-10-17 18:58:00 +0000670 }
671 if (log)
672 log->Printf ("Process::%s returning from action with m_should_stop: %d.", __FUNCTION__, m_should_stop);
Jim Inghama7dfb662012-10-23 07:20:06 +0000673
Johnny Chen16dcf712011-10-17 18:58:00 +0000674 }
675
Greg Claytonf4b47e12010-08-04 01:40:35 +0000676private:
677 std::string m_description;
Johnny Chenfd158f42011-09-21 22:47:15 +0000678 bool m_should_stop;
679 bool m_should_stop_is_valid;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000680};
681
682
683
684//----------------------------------------------------------------------
685// StopInfoUnixSignal
686//----------------------------------------------------------------------
687
688class StopInfoUnixSignal : public StopInfo
689{
690public:
691
692 StopInfoUnixSignal (Thread &thread, int signo) :
Greg Claytona658fd22011-06-04 01:26:29 +0000693 StopInfo (thread, signo)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000694 {
695 }
696
697 virtual ~StopInfoUnixSignal ()
698 {
699 }
700
701
702 virtual StopReason
703 GetStopReason () const
704 {
705 return eStopReasonSignal;
706 }
707
708 virtual bool
709 ShouldStop (Event *event_ptr)
710 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000711 return m_thread.GetProcess()->GetUnixSignals().GetShouldStop (m_value);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000712 }
713
714
715 // If should stop returns false, check if we should notify of this event
716 virtual bool
717 ShouldNotify (Event *event_ptr)
718 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000719 return m_thread.GetProcess()->GetUnixSignals().GetShouldNotify (m_value);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000720 }
721
722
723 virtual void
724 WillResume (lldb::StateType resume_state)
725 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000726 if (m_thread.GetProcess()->GetUnixSignals().GetShouldSuppress(m_value) == false)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000727 m_thread.SetResumeSignal(m_value);
728 }
729
730 virtual const char *
731 GetDescription ()
732 {
733 if (m_description.empty())
734 {
735 StreamString strm;
Greg Clayton1ac04c32012-02-21 00:09:25 +0000736 const char *signal_name = m_thread.GetProcess()->GetUnixSignals().GetSignalAsCString (m_value);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000737 if (signal_name)
Greg Clayton0603aa92010-10-04 01:05:56 +0000738 strm.Printf("signal %s", signal_name);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000739 else
Greg Clayton0603aa92010-10-04 01:05:56 +0000740 strm.Printf("signal %lli", m_value);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000741 m_description.swap (strm.GetString());
742 }
743 return m_description.c_str();
744 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000745};
746
747//----------------------------------------------------------------------
748// StopInfoTrace
749//----------------------------------------------------------------------
750
751class StopInfoTrace : public StopInfo
752{
753public:
754
755 StopInfoTrace (Thread &thread) :
756 StopInfo (thread, LLDB_INVALID_UID)
757 {
758 }
759
760 virtual ~StopInfoTrace ()
761 {
762 }
763
764 virtual StopReason
765 GetStopReason () const
766 {
767 return eStopReasonTrace;
768 }
769
770 virtual const char *
771 GetDescription ()
772 {
Greg Claytona658fd22011-06-04 01:26:29 +0000773 if (m_description.empty())
Greg Claytonf4b47e12010-08-04 01:40:35 +0000774 return "trace";
Greg Claytona658fd22011-06-04 01:26:29 +0000775 else
776 return m_description.c_str();
777 }
778};
779
780
781//----------------------------------------------------------------------
782// StopInfoException
783//----------------------------------------------------------------------
784
785class StopInfoException : public StopInfo
786{
787public:
788
789 StopInfoException (Thread &thread, const char *description) :
790 StopInfo (thread, LLDB_INVALID_UID)
791 {
792 if (description)
793 SetDescription (description);
794 }
795
796 virtual
797 ~StopInfoException ()
798 {
799 }
800
801 virtual StopReason
802 GetStopReason () const
803 {
804 return eStopReasonException;
805 }
806
807 virtual const char *
808 GetDescription ()
809 {
810 if (m_description.empty())
811 return "exception";
812 else
813 return m_description.c_str();
Greg Claytonf4b47e12010-08-04 01:40:35 +0000814 }
815};
816
817
818//----------------------------------------------------------------------
819// StopInfoThreadPlan
820//----------------------------------------------------------------------
821
822class StopInfoThreadPlan : public StopInfo
823{
824public:
825
Jim Ingham73ca05a2011-12-17 01:35:57 +0000826 StopInfoThreadPlan (ThreadPlanSP &plan_sp, ValueObjectSP &return_valobj_sp) :
Greg Claytonf4b47e12010-08-04 01:40:35 +0000827 StopInfo (plan_sp->GetThread(), LLDB_INVALID_UID),
Jim Ingham73ca05a2011-12-17 01:35:57 +0000828 m_plan_sp (plan_sp),
829 m_return_valobj_sp (return_valobj_sp)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000830 {
831 }
832
833 virtual ~StopInfoThreadPlan ()
834 {
835 }
836
837 virtual StopReason
838 GetStopReason () const
839 {
840 return eStopReasonPlanComplete;
841 }
842
843 virtual const char *
844 GetDescription ()
845 {
846 if (m_description.empty())
847 {
848 StreamString strm;
849 m_plan_sp->GetDescription (&strm, eDescriptionLevelBrief);
850 m_description.swap (strm.GetString());
851 }
852 return m_description.c_str();
853 }
Jim Ingham73ca05a2011-12-17 01:35:57 +0000854
855 ValueObjectSP
856 GetReturnValueObject()
857 {
858 return m_return_valobj_sp;
859 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000860
861private:
862 ThreadPlanSP m_plan_sp;
Jim Ingham73ca05a2011-12-17 01:35:57 +0000863 ValueObjectSP m_return_valobj_sp;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000864};
Jim Ingham4b536182011-08-09 02:12:22 +0000865} // namespace lldb_private
Greg Claytonf4b47e12010-08-04 01:40:35 +0000866
867StopInfoSP
868StopInfo::CreateStopReasonWithBreakpointSiteID (Thread &thread, break_id_t break_id)
869{
870 return StopInfoSP (new StopInfoBreakpoint (thread, break_id));
871}
872
873StopInfoSP
Jim Ingham36f3b362010-10-14 23:45:03 +0000874StopInfo::CreateStopReasonWithBreakpointSiteID (Thread &thread, break_id_t break_id, bool should_stop)
875{
876 return StopInfoSP (new StopInfoBreakpoint (thread, break_id, should_stop));
877}
878
879StopInfoSP
Greg Claytonf4b47e12010-08-04 01:40:35 +0000880StopInfo::CreateStopReasonWithWatchpointID (Thread &thread, break_id_t watch_id)
881{
882 return StopInfoSP (new StopInfoWatchpoint (thread, watch_id));
883}
884
885StopInfoSP
886StopInfo::CreateStopReasonWithSignal (Thread &thread, int signo)
887{
888 return StopInfoSP (new StopInfoUnixSignal (thread, signo));
889}
890
891StopInfoSP
892StopInfo::CreateStopReasonToTrace (Thread &thread)
893{
894 return StopInfoSP (new StopInfoTrace (thread));
895}
896
897StopInfoSP
Jim Ingham73ca05a2011-12-17 01:35:57 +0000898StopInfo::CreateStopReasonWithPlan (ThreadPlanSP &plan_sp, ValueObjectSP return_valobj_sp)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000899{
Jim Ingham73ca05a2011-12-17 01:35:57 +0000900 return StopInfoSP (new StopInfoThreadPlan (plan_sp, return_valobj_sp));
Greg Claytonf4b47e12010-08-04 01:40:35 +0000901}
Greg Claytona658fd22011-06-04 01:26:29 +0000902
903StopInfoSP
904StopInfo::CreateStopReasonWithException (Thread &thread, const char *description)
905{
906 return StopInfoSP (new StopInfoException (thread, description));
907}
Jim Ingham73ca05a2011-12-17 01:35:57 +0000908
909ValueObjectSP
910StopInfo::GetReturnValueObject(StopInfoSP &stop_info_sp)
911{
912 if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonPlanComplete)
913 {
914 StopInfoThreadPlan *plan_stop_info = static_cast<StopInfoThreadPlan *>(stop_info_sp.get());
915 return plan_stop_info->GetReturnValueObject();
916 }
917 else
918 return ValueObjectSP();
919}