blob: 9d62dd9df9a48b5d88be4a7f0a5d52d9949e6ef8 [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
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Greg Claytonf4b47e12010-08-04 01:40:35 +000012#include "lldb/Target/StopInfo.h"
13
14// C Includes
15// C++ Includes
16#include <string>
17
18// Other libraries and framework includes
19// Project includes
20#include "lldb/Core/Log.h"
Greg Clayton4e78f602010-11-18 18:52:36 +000021#include "lldb/Breakpoint/Breakpoint.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000022#include "lldb/Breakpoint/BreakpointLocation.h"
23#include "lldb/Breakpoint/StoppointCallbackContext.h"
Johnny Chen01a67862011-10-14 00:42:25 +000024#include "lldb/Breakpoint/Watchpoint.h"
Jim Ingham4b536182011-08-09 02:12:22 +000025#include "lldb/Core/Debugger.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000026#include "lldb/Core/StreamString.h"
Jim Ingham4b536182011-08-09 02:12:22 +000027#include "lldb/Expression/ClangUserExpression.h"
28#include "lldb/Target/Target.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000029#include "lldb/Target/Thread.h"
30#include "lldb/Target/ThreadPlan.h"
31#include "lldb/Target/Process.h"
32#include "lldb/Target/UnixSignals.h"
33
34using namespace lldb;
35using namespace lldb_private;
36
37StopInfo::StopInfo (Thread &thread, uint64_t value) :
38 m_thread (thread),
Greg Clayton1ac04c32012-02-21 00:09:25 +000039 m_stop_id (thread.GetProcess()->GetStopID()),
40 m_resume_id (thread.GetProcess()->GetResumeID()),
Greg Claytonf4b47e12010-08-04 01:40:35 +000041 m_value (value)
42{
43}
44
45bool
46StopInfo::IsValid () const
47{
Greg Clayton1ac04c32012-02-21 00:09:25 +000048 return m_thread.GetProcess()->GetStopID() == m_stop_id;
Greg Claytonf4b47e12010-08-04 01:40:35 +000049}
50
Jim Ingham77787032011-01-20 02:03:18 +000051void
52StopInfo::MakeStopInfoValid ()
53{
Greg Clayton1ac04c32012-02-21 00:09:25 +000054 m_stop_id = m_thread.GetProcess()->GetStopID();
55 m_resume_id = m_thread.GetProcess()->GetResumeID();
Jim Ingham77787032011-01-20 02:03:18 +000056}
57
Jim Ingham0faa43f2011-11-08 03:00:11 +000058bool
59StopInfo::HasTargetRunSinceMe ()
Jim Ingham4b536182011-08-09 02:12:22 +000060{
Greg Clayton1ac04c32012-02-21 00:09:25 +000061 lldb::StateType ret_type = m_thread.GetProcess()->GetPrivateState();
Jim Ingham0faa43f2011-11-08 03:00:11 +000062 if (ret_type == eStateRunning)
63 {
64 return true;
65 }
66 else if (ret_type == eStateStopped)
67 {
68 // This is a little tricky. We want to count "run and stopped again before you could
69 // ask this question as a "TRUE" answer to HasTargetRunSinceMe. But we don't want to
70 // include any running of the target done for expressions. So we track both resumes,
71 // and resumes caused by expressions, and check if there are any resumes NOT caused
72 // by expressions.
73
Greg Clayton1ac04c32012-02-21 00:09:25 +000074 uint32_t curr_resume_id = m_thread.GetProcess()->GetResumeID();
75 uint32_t last_user_expression_id = m_thread.GetProcess()->GetLastUserExpressionResumeID ();
Jim Ingham0faa43f2011-11-08 03:00:11 +000076 if (curr_resume_id == m_resume_id)
77 {
78 return false;
79 }
80 else if (curr_resume_id > last_user_expression_id)
81 {
82 return true;
83 }
84 }
85 return false;
Jim Ingham4b536182011-08-09 02:12:22 +000086}
87
Greg Claytonf4b47e12010-08-04 01:40:35 +000088//----------------------------------------------------------------------
89// StopInfoBreakpoint
90//----------------------------------------------------------------------
91
Jim Ingham4b536182011-08-09 02:12:22 +000092namespace lldb_private
93{
Greg Claytonf4b47e12010-08-04 01:40:35 +000094class StopInfoBreakpoint : public StopInfo
95{
96public:
97
98 StopInfoBreakpoint (Thread &thread, break_id_t break_id) :
99 StopInfo (thread, break_id),
100 m_description(),
101 m_should_stop (false),
Jim Ingham0f16e732011-02-08 05:20:59 +0000102 m_should_stop_is_valid (false),
Jim Ingham52c7d202011-10-28 01:12:19 +0000103 m_should_perform_action (true),
Jim Inghamca36cd12012-10-05 19:16:31 +0000104 m_address (LLDB_INVALID_ADDRESS),
105 m_break_id(LLDB_INVALID_BREAK_ID),
106 m_was_one_shot (false)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000107 {
Jim Inghamca36cd12012-10-05 19:16:31 +0000108 StoreBPInfo();
Greg Claytonf4b47e12010-08-04 01:40:35 +0000109 }
110
Jim Ingham36f3b362010-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 Ingham0f16e732011-02-08 05:20:59 +0000115 m_should_stop_is_valid (true),
Jim Ingham52c7d202011-10-28 01:12:19 +0000116 m_should_perform_action (true),
Jim Inghamca36cd12012-10-05 19:16:31 +0000117 m_address (LLDB_INVALID_ADDRESS),
118 m_break_id(LLDB_INVALID_BREAK_ID),
119 m_was_one_shot (false)
120 {
121 StoreBPInfo();
122 }
123
124 void StoreBPInfo ()
Jim Ingham36f3b362010-10-14 23:45:03 +0000125 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000126 BreakpointSiteSP bp_site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (m_value));
Jim Ingham52c7d202011-10-28 01:12:19 +0000127 if (bp_site_sp)
128 {
Jim Inghamca36cd12012-10-05 19:16:31 +0000129 if (bp_site_sp->GetNumberOfOwners() == 1)
130 {
131 BreakpointLocationSP bp_loc_sp = bp_site_sp->GetOwnerAtIndex(0);
132 if (bp_loc_sp)
133 {
134 m_break_id = bp_loc_sp->GetBreakpoint().GetID();
135 m_was_one_shot = bp_loc_sp->GetBreakpoint().IsOneShot();
136 }
137 }
138 m_address = bp_site_sp->GetLoadAddress();
Jim Ingham52c7d202011-10-28 01:12:19 +0000139 }
Jim Ingham36f3b362010-10-14 23:45:03 +0000140 }
141
Greg Claytonf4b47e12010-08-04 01:40:35 +0000142 virtual ~StopInfoBreakpoint ()
143 {
144 }
145
146 virtual StopReason
147 GetStopReason () const
148 {
149 return eStopReasonBreakpoint;
150 }
151
152 virtual bool
Jim Ingham6d66ce62012-04-20 21:16:56 +0000153 ShouldStopSynchronous (Event *event_ptr)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000154 {
155 if (!m_should_stop_is_valid)
156 {
157 // Only check once if we should stop at a breakpoint
Greg Clayton1ac04c32012-02-21 00:09:25 +0000158 BreakpointSiteSP bp_site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (m_value));
Greg Claytonf4b47e12010-08-04 01:40:35 +0000159 if (bp_site_sp)
160 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000161 ExecutionContext exe_ctx (m_thread.GetStackFrameAtIndex(0));
162 StoppointCallbackContext context (event_ptr, exe_ctx, true);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000163 m_should_stop = bp_site_sp->ShouldStop (&context);
164 }
165 else
166 {
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000167 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Claytonf4b47e12010-08-04 01:40:35 +0000168
169 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000170 log->Printf ("Process::%s could not find breakpoint site id: %" PRId64 "...", __FUNCTION__, m_value);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000171
172 m_should_stop = true;
173 }
174 m_should_stop_is_valid = true;
175 }
176 return m_should_stop;
177 }
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000178
Jim Inghamf57b4352012-11-10 02:08:14 +0000179 virtual bool
180 ShouldNotify (Event *event_ptr)
181 {
182 BreakpointSiteSP bp_site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (m_value));
183 if (bp_site_sp)
184 {
185 bool all_internal = true;
186
187 for (uint32_t i = 0; i < bp_site_sp->GetNumberOfOwners(); i++)
188 {
189 if (!bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint().IsInternal())
190 {
191 all_internal = false;
192 break;
193 }
194 }
195 return all_internal == false;
196 }
197 return true;
198 }
199
200 virtual const char *
201 GetDescription ()
202 {
203 if (m_description.empty())
204 {
205 BreakpointSiteSP bp_site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (m_value));
206 if (bp_site_sp)
207 {
208 StreamString strm;
209 strm.Printf("breakpoint ");
210 bp_site_sp->GetDescription(&strm, eDescriptionLevelBrief);
211 m_description.swap (strm.GetString());
212 }
213 else
214 {
215 StreamString strm;
216 if (m_break_id != LLDB_INVALID_BREAK_ID)
217 {
218 if (m_was_one_shot)
219 strm.Printf ("one-shot breakpoint %d", m_break_id);
220 else
221 strm.Printf ("breakpoint %d which has been deleted.", m_break_id);
222 }
223 else if (m_address == LLDB_INVALID_ADDRESS)
Daniel Malead01b2952012-11-29 21:49:15 +0000224 strm.Printf("breakpoint site %" PRIi64 " which has been deleted - unknown address", m_value);
Jim Inghamf57b4352012-11-10 02:08:14 +0000225 else
Daniel Malead01b2952012-11-29 21:49:15 +0000226 strm.Printf("breakpoint site %" PRIi64 " which has been deleted - was at 0x%" PRIx64, m_value, m_address);
Jim Inghamf57b4352012-11-10 02:08:14 +0000227
228 m_description.swap (strm.GetString());
229 }
230 }
231 return m_description.c_str();
232 }
233
234protected:
Jim Ingham6d66ce62012-04-20 21:16:56 +0000235 bool
236 ShouldStop (Event *event_ptr)
237 {
238 // This just reports the work done by PerformAction or the synchronous stop. It should
239 // only ever get called after they have had a chance to run.
240 assert (m_should_stop_is_valid);
241 return m_should_stop;
242 }
243
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000244 virtual void
245 PerformAction (Event *event_ptr)
246 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000247 if (!m_should_perform_action)
248 return;
249 m_should_perform_action = false;
250
Jim Ingham4b536182011-08-09 02:12:22 +0000251 LogSP log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
Jim Ingham4b536182011-08-09 02:12:22 +0000252
Greg Clayton1ac04c32012-02-21 00:09:25 +0000253 BreakpointSiteSP bp_site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (m_value));
Jim Ingham79ea1d82011-12-09 04:17:31 +0000254
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000255 if (bp_site_sp)
256 {
257 size_t num_owners = bp_site_sp->GetNumberOfOwners();
Jim Ingham79ea1d82011-12-09 04:17:31 +0000258
259 if (num_owners == 0)
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000260 {
Jim Ingham79ea1d82011-12-09 04:17:31 +0000261 m_should_stop = true;
262 }
263 else
264 {
265 // We go through each location, and test first its condition. If the condition says to stop,
266 // then we run the callback for that location. If that callback says to stop as well, then
267 // we set m_should_stop to true; we are going to stop.
268 // But we still want to give all the breakpoints whose conditions say we are going to stop a
269 // chance to run their callbacks.
270 // Of course if any callback restarts the target by putting "continue" in the callback, then
271 // we're going to restart, without running the rest of the callbacks. And in this case we will
272 // end up not stopping even if another location said we should stop. But that's better than not
273 // running all the callbacks.
274
275 m_should_stop = false;
276
Greg Clayton1ac04c32012-02-21 00:09:25 +0000277 ExecutionContext exe_ctx (m_thread.GetStackFrameAtIndex(0));
278 StoppointCallbackContext context (event_ptr, exe_ctx, false);
Jim Ingham4b536182011-08-09 02:12:22 +0000279
Jim Ingham4b536182011-08-09 02:12:22 +0000280 for (size_t j = 0; j < num_owners; j++)
281 {
282 lldb::BreakpointLocationSP bp_loc_sp = bp_site_sp->GetOwnerAtIndex(j);
Jim Ingham79ea1d82011-12-09 04:17:31 +0000283
284 // First run the condition for the breakpoint. If that says we should stop, then we'll run
285 // the callback for the breakpoint. If the callback says we shouldn't stop that will win.
286
287 bool condition_says_stop = true;
Jim Ingham4b536182011-08-09 02:12:22 +0000288 if (bp_loc_sp->GetConditionText() != NULL)
289 {
290 // We need to make sure the user sees any parse errors in their condition, so we'll hook the
291 // constructor errors up to the debugger's Async I/O.
292
Jim Ingham4b536182011-08-09 02:12:22 +0000293 ValueObjectSP result_valobj_sp;
294
295 ExecutionResults result_code;
296 ValueObjectSP result_value_sp;
297 const bool discard_on_error = true;
298 Error error;
Greg Clayton1ac04c32012-02-21 00:09:25 +0000299 result_code = ClangUserExpression::EvaluateWithError (exe_ctx,
Jim Ingham6d66ce62012-04-20 21:16:56 +0000300 eExecutionPolicyOnlyWhenNeeded,
Sean Callananc7b65062011-11-07 23:35:40 +0000301 lldb::eLanguageTypeUnknown,
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000302 ClangUserExpression::eResultTypeAny,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000303 discard_on_error,
304 bp_loc_sp->GetConditionText(),
305 NULL,
306 result_value_sp,
Greg Clayton26ab83d2012-10-31 20:49:04 +0000307 error,
308 true,
309 ClangUserExpression::kDefaultTimeout);
Jim Ingham4b536182011-08-09 02:12:22 +0000310 if (result_code == eExecutionCompleted)
311 {
312 if (result_value_sp)
313 {
314 Scalar scalar_value;
315 if (result_value_sp->ResolveValue (scalar_value))
316 {
317 if (scalar_value.ULongLong(1) == 0)
Jim Ingham79ea1d82011-12-09 04:17:31 +0000318 condition_says_stop = false;
Jim Ingham4b536182011-08-09 02:12:22 +0000319 else
Jim Ingham79ea1d82011-12-09 04:17:31 +0000320 condition_says_stop = true;
Jim Ingham4b536182011-08-09 02:12:22 +0000321 if (log)
322 log->Printf("Condition successfully evaluated, result is %s.\n",
323 m_should_stop ? "true" : "false");
324 }
325 else
326 {
Jim Ingham79ea1d82011-12-09 04:17:31 +0000327 condition_says_stop = true;
Jim Ingham4b536182011-08-09 02:12:22 +0000328 if (log)
329 log->Printf("Failed to get an integer result from the expression.");
330 }
331 }
332 }
333 else
334 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000335 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
Jim Ingham4b536182011-08-09 02:12:22 +0000336 StreamSP error_sp = debugger.GetAsyncErrorStream ();
337 error_sp->Printf ("Stopped due to an error evaluating condition of breakpoint ");
338 bp_loc_sp->GetDescription (error_sp.get(), eDescriptionLevelBrief);
339 error_sp->Printf (": \"%s\"",
340 bp_loc_sp->GetConditionText());
341 error_sp->EOL();
342 const char *err_str = error.AsCString("<Unknown Error>");
343 if (log)
344 log->Printf("Error evaluating condition: \"%s\"\n", err_str);
345
346 error_sp->PutCString (err_str);
347 error_sp->EOL();
348 error_sp->Flush();
349 // If the condition fails to be parsed or run, we should stop.
Jim Ingham79ea1d82011-12-09 04:17:31 +0000350 condition_says_stop = true;
Jim Ingham4b536182011-08-09 02:12:22 +0000351 }
352 }
353
Jim Ingham79ea1d82011-12-09 04:17:31 +0000354 // If this location's condition says we should aren't going to stop,
355 // then don't run the callback for this location.
356 if (!condition_says_stop)
357 continue;
358
359 bool callback_says_stop;
360
361 // FIXME: For now the callbacks have to run in async mode - the first time we restart we need
362 // to get out of there. So set it here.
363 // When we figure out how to nest breakpoint hits then this will change.
364
Greg Clayton1ac04c32012-02-21 00:09:25 +0000365 Debugger &debugger = m_thread.CalculateTarget()->GetDebugger();
Jim Ingham79ea1d82011-12-09 04:17:31 +0000366 bool old_async = debugger.GetAsyncExecution();
367 debugger.SetAsyncExecution (true);
368
369 callback_says_stop = bp_loc_sp->InvokeCallback (&context);
370
371 debugger.SetAsyncExecution (old_async);
372
373 if (callback_says_stop)
374 m_should_stop = true;
Jim Inghamca36cd12012-10-05 19:16:31 +0000375
376 // If we are going to stop for this breakpoint, then remove the breakpoint.
377 if (callback_says_stop && bp_loc_sp && bp_loc_sp->GetBreakpoint().IsOneShot())
378 {
379 m_thread.GetProcess()->GetTarget().RemoveBreakpointByID (bp_loc_sp->GetBreakpoint().GetID());
380 }
Jim Ingham79ea1d82011-12-09 04:17:31 +0000381
382 // Also make sure that the callback hasn't continued the target.
383 // If it did, when we'll set m_should_start to false and get out of here.
384 if (HasTargetRunSinceMe ())
385 {
386 m_should_stop = false;
Jim Ingham4b536182011-08-09 02:12:22 +0000387 break;
Jim Ingham79ea1d82011-12-09 04:17:31 +0000388 }
Jim Ingham4b536182011-08-09 02:12:22 +0000389 }
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000390 }
Jim Ingham79ea1d82011-12-09 04:17:31 +0000391 // We've figured out what this stop wants to do, so mark it as valid so we don't compute it again.
392 m_should_stop_is_valid = true;
393
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000394 }
395 else
396 {
Jim Ingham79ea1d82011-12-09 04:17:31 +0000397 m_should_stop = true;
398 m_should_stop_is_valid = true;
Jason Molendaccd41e52012-10-04 22:47:07 +0000399 LogSP log_process(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000400
Jason Molendaccd41e52012-10-04 22:47:07 +0000401 if (log_process)
Daniel Malead01b2952012-11-29 21:49:15 +0000402 log_process->Printf ("Process::%s could not find breakpoint site id: %" PRId64 "...", __FUNCTION__, m_value);
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000403 }
Jim Ingham4b536182011-08-09 02:12:22 +0000404 if (log)
405 log->Printf ("Process::%s returning from action with m_should_stop: %d.", __FUNCTION__, m_should_stop);
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000406 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000407
408private:
409 std::string m_description;
410 bool m_should_stop;
411 bool m_should_stop_is_valid;
Jim Ingham0f16e732011-02-08 05:20:59 +0000412 bool m_should_perform_action; // Since we are trying to preserve the "state" of the system even if we run functions
413 // etc. behind the users backs, we need to make sure we only REALLY perform the action once.
Jim Ingham52c7d202011-10-28 01:12:19 +0000414 lldb::addr_t m_address; // We use this to capture the breakpoint site address when we create the StopInfo,
415 // in case somebody deletes it between the time the StopInfo is made and the
416 // description is asked for.
Jim Inghamca36cd12012-10-05 19:16:31 +0000417 lldb::break_id_t m_break_id;
418 bool m_was_one_shot;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000419};
420
421
422//----------------------------------------------------------------------
423// StopInfoWatchpoint
424//----------------------------------------------------------------------
425
426class StopInfoWatchpoint : public StopInfo
427{
428public:
Jim Inghamf57b4352012-11-10 02:08:14 +0000429 // Make sure watchpoint is properly disabled and subsequently enabled while performing watchpoint actions.
430 class WatchpointSentry {
431 public:
432 WatchpointSentry(Process *p, Watchpoint *w):
433 process(p),
434 watchpoint(w)
435 {
436 if (process && watchpoint)
437 {
438 watchpoint->TurnOnEphemeralMode();
439 process->DisableWatchpoint(watchpoint);
440 }
441 }
442 ~WatchpointSentry()
443 {
444 if (process && watchpoint)
445 {
446 if (!watchpoint->IsDisabledDuringEphemeralMode())
447 process->EnableWatchpoint(watchpoint);
448 watchpoint->TurnOffEphemeralMode();
449 }
450 }
451 private:
452 Process *process;
453 Watchpoint *watchpoint;
454 };
Greg Claytonf4b47e12010-08-04 01:40:35 +0000455
456 StopInfoWatchpoint (Thread &thread, break_id_t watch_id) :
Johnny Chenfd158f42011-09-21 22:47:15 +0000457 StopInfo(thread, watch_id),
458 m_description(),
459 m_should_stop(false),
460 m_should_stop_is_valid(false)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000461 {
462 }
463
464 virtual ~StopInfoWatchpoint ()
465 {
466 }
467
468 virtual StopReason
469 GetStopReason () const
470 {
471 return eStopReasonWatchpoint;
472 }
473
Jim Inghamf57b4352012-11-10 02:08:14 +0000474 virtual const char *
475 GetDescription ()
476 {
477 if (m_description.empty())
478 {
479 StreamString strm;
Daniel Malead01b2952012-11-29 21:49:15 +0000480 strm.Printf("watchpoint %" PRIi64, m_value);
Jim Inghamf57b4352012-11-10 02:08:14 +0000481 m_description.swap (strm.GetString());
482 }
483 return m_description.c_str();
484 }
485
486protected:
Johnny Chenfd158f42011-09-21 22:47:15 +0000487 virtual bool
488 ShouldStop (Event *event_ptr)
489 {
490 // ShouldStop() method is idempotent and should not affect hit count.
Johnny Chen16dcf712011-10-17 18:58:00 +0000491 // See Process::RunPrivateStateThread()->Process()->HandlePrivateEvent()
492 // -->Process()::ShouldBroadcastEvent()->ThreadList::ShouldStop()->
493 // Thread::ShouldStop()->ThreadPlanBase::ShouldStop()->
494 // StopInfoWatchpoint::ShouldStop() and
495 // Event::DoOnRemoval()->Process::ProcessEventData::DoOnRemoval()->
496 // StopInfoWatchpoint::PerformAction().
Johnny Chenfd158f42011-09-21 22:47:15 +0000497 if (m_should_stop_is_valid)
498 return m_should_stop;
499
Johnny Chen01a67862011-10-14 00:42:25 +0000500 WatchpointSP wp_sp =
Greg Clayton1ac04c32012-02-21 00:09:25 +0000501 m_thread.CalculateTarget()->GetWatchpointList().FindByID(GetValue());
Johnny Chen01a67862011-10-14 00:42:25 +0000502 if (wp_sp)
Johnny Chenfd158f42011-09-21 22:47:15 +0000503 {
504 // Check if we should stop at a watchpoint.
Greg Clayton1ac04c32012-02-21 00:09:25 +0000505 ExecutionContext exe_ctx (m_thread.GetStackFrameAtIndex(0));
506 StoppointCallbackContext context (event_ptr, exe_ctx, true);
Johnny Chen01a67862011-10-14 00:42:25 +0000507 m_should_stop = wp_sp->ShouldStop (&context);
Johnny Chenfd158f42011-09-21 22:47:15 +0000508 }
509 else
510 {
511 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
512
513 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000514 log->Printf ("Process::%s could not find watchpoint location id: %" PRId64 "...",
Johnny Chenfd158f42011-09-21 22:47:15 +0000515 __FUNCTION__, GetValue());
516
517 m_should_stop = true;
518 }
519 m_should_stop_is_valid = true;
520 return m_should_stop;
521 }
522
Johnny Chen16dcf712011-10-17 18:58:00 +0000523 virtual void
524 PerformAction (Event *event_ptr)
525 {
Johnny Chen209bd652012-08-13 21:09:54 +0000526 LogSP log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS);
Johnny Chen16dcf712011-10-17 18:58:00 +0000527 // We're going to calculate if we should stop or not in some way during the course of
528 // this code. Also by default we're going to stop, so set that here.
529 m_should_stop = true;
530
531 WatchpointSP wp_sp =
Greg Clayton1ac04c32012-02-21 00:09:25 +0000532 m_thread.CalculateTarget()->GetWatchpointList().FindByID(GetValue());
Johnny Chen16dcf712011-10-17 18:58:00 +0000533 if (wp_sp)
534 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000535 ExecutionContext exe_ctx (m_thread.GetStackFrameAtIndex(0));
Johnny Chen209bd652012-08-13 21:09:54 +0000536 Process* process = exe_ctx.GetProcessPtr();
Johnny Chen0f7ad8d2012-08-21 22:06:34 +0000537
Johnny Chen66535a32012-08-21 22:15:58 +0000538 // This sentry object makes sure the current watchpoint is disabled while performing watchpoint actions,
539 // and it is then enabled after we are finished.
Johnny Chen0f7ad8d2012-08-21 22:06:34 +0000540 WatchpointSentry sentry(process, wp_sp.get());
541
Enrico Granataf04a2192012-07-13 23:18:48 +0000542 {
543 // check if this process is running on an architecture where watchpoints trigger
544 // before the associated instruction runs. if so, disable the WP, single-step and then
545 // re-enable the watchpoint
Enrico Granataf04a2192012-07-13 23:18:48 +0000546 if (process)
547 {
548 uint32_t num; bool wp_triggers_after;
549 if (process->GetWatchpointSupportInfo(num, wp_triggers_after).Success())
550 {
551 if (!wp_triggers_after)
552 {
Enrico Granataf04a2192012-07-13 23:18:48 +0000553 ThreadPlan *new_plan = m_thread.QueueThreadPlanForStepSingleInstruction(false, // step-over
554 false, // abort_other_plans
555 true); // stop_other_threads
556 new_plan->SetIsMasterPlan (true);
557 new_plan->SetOkayToDiscard (false);
558 process->GetThreadList().SetSelectedThreadByID (m_thread.GetID());
559 process->Resume ();
560 process->WaitForProcessToStop (NULL);
561 process->GetThreadList().SetSelectedThreadByID (m_thread.GetID());
562 MakeStopInfoValid(); // make sure we do not fail to stop because of the single-step taken above
Enrico Granataf04a2192012-07-13 23:18:48 +0000563 }
564 }
565 }
566 }
Johnny Chen209bd652012-08-13 21:09:54 +0000567
Johnny Chen16dcf712011-10-17 18:58:00 +0000568 if (m_should_stop && wp_sp->GetConditionText() != NULL)
569 {
570 // We need to make sure the user sees any parse errors in their condition, so we'll hook the
571 // constructor errors up to the debugger's Async I/O.
Johnny Chen16dcf712011-10-17 18:58:00 +0000572 ExecutionResults result_code;
573 ValueObjectSP result_value_sp;
574 const bool discard_on_error = true;
575 Error error;
Greg Clayton1ac04c32012-02-21 00:09:25 +0000576 result_code = ClangUserExpression::EvaluateWithError (exe_ctx,
Sean Callanan22019052012-09-08 01:51:48 +0000577 eExecutionPolicyOnlyWhenNeeded,
Sean Callananc7b65062011-11-07 23:35:40 +0000578 lldb::eLanguageTypeUnknown,
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000579 ClangUserExpression::eResultTypeAny,
Johnny Chen16dcf712011-10-17 18:58:00 +0000580 discard_on_error,
581 wp_sp->GetConditionText(),
582 NULL,
583 result_value_sp,
Enrico Granata3372f582012-07-16 23:10:35 +0000584 error,
Greg Clayton26ab83d2012-10-31 20:49:04 +0000585 true,
586 ClangUserExpression::kDefaultTimeout);
Johnny Chen16dcf712011-10-17 18:58:00 +0000587 if (result_code == eExecutionCompleted)
588 {
589 if (result_value_sp)
590 {
591 Scalar scalar_value;
592 if (result_value_sp->ResolveValue (scalar_value))
593 {
594 if (scalar_value.ULongLong(1) == 0)
595 {
596 // We have been vetoed. This takes precedence over querying
597 // the watchpoint whether it should stop (aka ignore count and
598 // friends). See also StopInfoWatchpoint::ShouldStop() as well
599 // as Process::ProcessEventData::DoOnRemoval().
600 m_should_stop = false;
601 }
602 else
603 m_should_stop = true;
604 if (log)
605 log->Printf("Condition successfully evaluated, result is %s.\n",
606 m_should_stop ? "true" : "false");
607 }
608 else
609 {
610 m_should_stop = true;
611 if (log)
612 log->Printf("Failed to get an integer result from the expression.");
613 }
614 }
615 }
616 else
617 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000618 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
Johnny Chen16dcf712011-10-17 18:58:00 +0000619 StreamSP error_sp = debugger.GetAsyncErrorStream ();
Johnny Chen5f3bf632012-01-24 23:19:25 +0000620 error_sp->Printf ("Stopped due to an error evaluating condition of watchpoint ");
Johnny Chen16dcf712011-10-17 18:58:00 +0000621 wp_sp->GetDescription (error_sp.get(), eDescriptionLevelBrief);
622 error_sp->Printf (": \"%s\"",
623 wp_sp->GetConditionText());
624 error_sp->EOL();
625 const char *err_str = error.AsCString("<Unknown Error>");
626 if (log)
627 log->Printf("Error evaluating condition: \"%s\"\n", err_str);
628
629 error_sp->PutCString (err_str);
630 error_sp->EOL();
631 error_sp->Flush();
632 // If the condition fails to be parsed or run, we should stop.
633 m_should_stop = true;
634 }
635 }
Johnny Chen13206412012-08-09 23:10:57 +0000636
637 // If the condition says to stop, we run the callback to further decide whether to stop.
638 if (m_should_stop)
639 {
Johnny Chen209bd652012-08-13 21:09:54 +0000640 StoppointCallbackContext context (event_ptr, exe_ctx, false);
Johnny Chen13206412012-08-09 23:10:57 +0000641 bool stop_requested = wp_sp->InvokeCallback (&context);
642 // Also make sure that the callback hasn't continued the target.
643 // If it did, when we'll set m_should_stop to false and get out of here.
644 if (HasTargetRunSinceMe ())
645 m_should_stop = false;
646
647 if (m_should_stop && !stop_requested)
648 {
649 // We have been vetoed by the callback mechanism.
650 m_should_stop = false;
651 }
652 }
Jim Inghama7dfb662012-10-23 07:20:06 +0000653 // Finally, if we are going to stop, print out the new & old values:
654 if (m_should_stop)
655 {
656 wp_sp->CaptureWatchedValue(exe_ctx);
657
658 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
659 StreamSP output_sp = debugger.GetAsyncOutputStream ();
660 wp_sp->DumpSnapshots(output_sp.get());
661 output_sp->EOL();
662 output_sp->Flush();
663 }
664
Johnny Chen16dcf712011-10-17 18:58:00 +0000665 }
666 else
667 {
Jason Molendaccd41e52012-10-04 22:47:07 +0000668 LogSP log_process(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Johnny Chen16dcf712011-10-17 18:58:00 +0000669
Jason Molendaccd41e52012-10-04 22:47:07 +0000670 if (log_process)
Daniel Malead01b2952012-11-29 21:49:15 +0000671 log_process->Printf ("Process::%s could not find watchpoint id: %" PRId64 "...", __FUNCTION__, m_value);
Johnny Chen16dcf712011-10-17 18:58:00 +0000672 }
673 if (log)
674 log->Printf ("Process::%s returning from action with m_should_stop: %d.", __FUNCTION__, m_should_stop);
Jim Inghama7dfb662012-10-23 07:20:06 +0000675
Johnny Chen16dcf712011-10-17 18:58:00 +0000676 }
677
Greg Claytonf4b47e12010-08-04 01:40:35 +0000678private:
679 std::string m_description;
Johnny Chenfd158f42011-09-21 22:47:15 +0000680 bool m_should_stop;
681 bool m_should_stop_is_valid;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000682};
683
684
685
686//----------------------------------------------------------------------
687// StopInfoUnixSignal
688//----------------------------------------------------------------------
689
690class StopInfoUnixSignal : public StopInfo
691{
692public:
693
694 StopInfoUnixSignal (Thread &thread, int signo) :
Greg Claytona658fd22011-06-04 01:26:29 +0000695 StopInfo (thread, signo)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000696 {
697 }
698
699 virtual ~StopInfoUnixSignal ()
700 {
701 }
702
703
704 virtual StopReason
705 GetStopReason () const
706 {
707 return eStopReasonSignal;
708 }
709
710 virtual bool
711 ShouldStop (Event *event_ptr)
712 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000713 return m_thread.GetProcess()->GetUnixSignals().GetShouldStop (m_value);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000714 }
715
716
717 // If should stop returns false, check if we should notify of this event
718 virtual bool
719 ShouldNotify (Event *event_ptr)
720 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000721 return m_thread.GetProcess()->GetUnixSignals().GetShouldNotify (m_value);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000722 }
723
724
725 virtual void
726 WillResume (lldb::StateType resume_state)
727 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000728 if (m_thread.GetProcess()->GetUnixSignals().GetShouldSuppress(m_value) == false)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000729 m_thread.SetResumeSignal(m_value);
730 }
731
732 virtual const char *
733 GetDescription ()
734 {
735 if (m_description.empty())
736 {
737 StreamString strm;
Greg Clayton1ac04c32012-02-21 00:09:25 +0000738 const char *signal_name = m_thread.GetProcess()->GetUnixSignals().GetSignalAsCString (m_value);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000739 if (signal_name)
Greg Clayton0603aa92010-10-04 01:05:56 +0000740 strm.Printf("signal %s", signal_name);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000741 else
Daniel Malead01b2952012-11-29 21:49:15 +0000742 strm.Printf("signal %" PRIi64, m_value);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000743 m_description.swap (strm.GetString());
744 }
745 return m_description.c_str();
746 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000747};
748
749//----------------------------------------------------------------------
750// StopInfoTrace
751//----------------------------------------------------------------------
752
753class StopInfoTrace : public StopInfo
754{
755public:
756
757 StopInfoTrace (Thread &thread) :
758 StopInfo (thread, LLDB_INVALID_UID)
759 {
760 }
761
762 virtual ~StopInfoTrace ()
763 {
764 }
765
766 virtual StopReason
767 GetStopReason () const
768 {
769 return eStopReasonTrace;
770 }
771
772 virtual const char *
773 GetDescription ()
774 {
Greg Claytona658fd22011-06-04 01:26:29 +0000775 if (m_description.empty())
Greg Claytonf4b47e12010-08-04 01:40:35 +0000776 return "trace";
Greg Claytona658fd22011-06-04 01:26:29 +0000777 else
778 return m_description.c_str();
779 }
780};
781
782
783//----------------------------------------------------------------------
784// StopInfoException
785//----------------------------------------------------------------------
786
787class StopInfoException : public StopInfo
788{
789public:
790
791 StopInfoException (Thread &thread, const char *description) :
792 StopInfo (thread, LLDB_INVALID_UID)
793 {
794 if (description)
795 SetDescription (description);
796 }
797
798 virtual
799 ~StopInfoException ()
800 {
801 }
802
803 virtual StopReason
804 GetStopReason () const
805 {
806 return eStopReasonException;
807 }
808
809 virtual const char *
810 GetDescription ()
811 {
812 if (m_description.empty())
813 return "exception";
814 else
815 return m_description.c_str();
Greg Claytonf4b47e12010-08-04 01:40:35 +0000816 }
817};
818
819
820//----------------------------------------------------------------------
821// StopInfoThreadPlan
822//----------------------------------------------------------------------
823
824class StopInfoThreadPlan : public StopInfo
825{
826public:
827
Jim Ingham73ca05a2011-12-17 01:35:57 +0000828 StopInfoThreadPlan (ThreadPlanSP &plan_sp, ValueObjectSP &return_valobj_sp) :
Greg Claytonf4b47e12010-08-04 01:40:35 +0000829 StopInfo (plan_sp->GetThread(), LLDB_INVALID_UID),
Jim Ingham73ca05a2011-12-17 01:35:57 +0000830 m_plan_sp (plan_sp),
831 m_return_valobj_sp (return_valobj_sp)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000832 {
833 }
834
835 virtual ~StopInfoThreadPlan ()
836 {
837 }
838
839 virtual StopReason
840 GetStopReason () const
841 {
842 return eStopReasonPlanComplete;
843 }
844
845 virtual const char *
846 GetDescription ()
847 {
848 if (m_description.empty())
849 {
850 StreamString strm;
851 m_plan_sp->GetDescription (&strm, eDescriptionLevelBrief);
852 m_description.swap (strm.GetString());
853 }
854 return m_description.c_str();
855 }
Jim Ingham73ca05a2011-12-17 01:35:57 +0000856
857 ValueObjectSP
858 GetReturnValueObject()
859 {
860 return m_return_valobj_sp;
861 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000862
863private:
864 ThreadPlanSP m_plan_sp;
Jim Ingham73ca05a2011-12-17 01:35:57 +0000865 ValueObjectSP m_return_valobj_sp;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000866};
Greg Clayton90ba8112012-12-05 00:16:59 +0000867
868class StopInfoExec : public StopInfo
869{
870public:
871
872 StopInfoExec (Thread &thread) :
873 StopInfo (thread, LLDB_INVALID_UID),
874 m_performed_action (false)
875 {
876 }
877
878 virtual
879 ~StopInfoExec ()
880 {
881 }
882
883 virtual StopReason
884 GetStopReason () const
885 {
886 return eStopReasonExec;
887 }
888
889 virtual const char *
890 GetDescription ()
891 {
892 return "exec";
893 }
894protected:
895protected:
896
897 virtual void
898 PerformAction (Event *event_ptr)
899 {
900 // Only perform the action once
901 if (m_performed_action)
902 return;
903 m_performed_action = true;
904 m_thread.GetProcess()->DidExec();
905 }
906
907 bool m_performed_action;
908};
909
Jim Ingham4b536182011-08-09 02:12:22 +0000910} // namespace lldb_private
Greg Claytonf4b47e12010-08-04 01:40:35 +0000911
912StopInfoSP
913StopInfo::CreateStopReasonWithBreakpointSiteID (Thread &thread, break_id_t break_id)
914{
915 return StopInfoSP (new StopInfoBreakpoint (thread, break_id));
916}
917
918StopInfoSP
Jim Ingham36f3b362010-10-14 23:45:03 +0000919StopInfo::CreateStopReasonWithBreakpointSiteID (Thread &thread, break_id_t break_id, bool should_stop)
920{
921 return StopInfoSP (new StopInfoBreakpoint (thread, break_id, should_stop));
922}
923
924StopInfoSP
Greg Claytonf4b47e12010-08-04 01:40:35 +0000925StopInfo::CreateStopReasonWithWatchpointID (Thread &thread, break_id_t watch_id)
926{
927 return StopInfoSP (new StopInfoWatchpoint (thread, watch_id));
928}
929
930StopInfoSP
931StopInfo::CreateStopReasonWithSignal (Thread &thread, int signo)
932{
933 return StopInfoSP (new StopInfoUnixSignal (thread, signo));
934}
935
936StopInfoSP
937StopInfo::CreateStopReasonToTrace (Thread &thread)
938{
939 return StopInfoSP (new StopInfoTrace (thread));
940}
941
942StopInfoSP
Jim Ingham73ca05a2011-12-17 01:35:57 +0000943StopInfo::CreateStopReasonWithPlan (ThreadPlanSP &plan_sp, ValueObjectSP return_valobj_sp)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000944{
Jim Ingham73ca05a2011-12-17 01:35:57 +0000945 return StopInfoSP (new StopInfoThreadPlan (plan_sp, return_valobj_sp));
Greg Claytonf4b47e12010-08-04 01:40:35 +0000946}
Greg Claytona658fd22011-06-04 01:26:29 +0000947
948StopInfoSP
949StopInfo::CreateStopReasonWithException (Thread &thread, const char *description)
950{
951 return StopInfoSP (new StopInfoException (thread, description));
952}
Jim Ingham73ca05a2011-12-17 01:35:57 +0000953
Greg Clayton90ba8112012-12-05 00:16:59 +0000954StopInfoSP
955StopInfo::CreateStopReasonWithExec (Thread &thread)
956{
957 return StopInfoSP (new StopInfoExec (thread));
958}
959
Jim Ingham73ca05a2011-12-17 01:35:57 +0000960ValueObjectSP
961StopInfo::GetReturnValueObject(StopInfoSP &stop_info_sp)
962{
963 if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonPlanComplete)
964 {
965 StopInfoThreadPlan *plan_stop_info = static_cast<StopInfoThreadPlan *>(stop_info_sp.get());
966 return plan_stop_info->GetReturnValueObject();
967 }
968 else
969 return ValueObjectSP();
970}