blob: 77a960a260a5dd9fb80d59788038ffae13805431 [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 {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000438 const bool notify = false;
Jim Inghamf57b4352012-11-10 02:08:14 +0000439 watchpoint->TurnOnEphemeralMode();
Jim Ingham1b5792e2012-12-18 02:03:49 +0000440 process->DisableWatchpoint(watchpoint, notify);
Jim Inghamf57b4352012-11-10 02:08:14 +0000441 }
442 }
443 ~WatchpointSentry()
444 {
445 if (process && watchpoint)
446 {
447 if (!watchpoint->IsDisabledDuringEphemeralMode())
Jim Ingham1b5792e2012-12-18 02:03:49 +0000448 {
449 const bool notify = false;
450 process->EnableWatchpoint(watchpoint, notify);
451 }
Jim Inghamf57b4352012-11-10 02:08:14 +0000452 watchpoint->TurnOffEphemeralMode();
453 }
454 }
455 private:
456 Process *process;
457 Watchpoint *watchpoint;
458 };
Greg Claytonf4b47e12010-08-04 01:40:35 +0000459
460 StopInfoWatchpoint (Thread &thread, break_id_t watch_id) :
Johnny Chenfd158f42011-09-21 22:47:15 +0000461 StopInfo(thread, watch_id),
462 m_description(),
463 m_should_stop(false),
464 m_should_stop_is_valid(false)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000465 {
466 }
467
468 virtual ~StopInfoWatchpoint ()
469 {
470 }
471
472 virtual StopReason
473 GetStopReason () const
474 {
475 return eStopReasonWatchpoint;
476 }
477
Jim Inghamf57b4352012-11-10 02:08:14 +0000478 virtual const char *
479 GetDescription ()
480 {
481 if (m_description.empty())
482 {
483 StreamString strm;
Daniel Malead01b2952012-11-29 21:49:15 +0000484 strm.Printf("watchpoint %" PRIi64, m_value);
Jim Inghamf57b4352012-11-10 02:08:14 +0000485 m_description.swap (strm.GetString());
486 }
487 return m_description.c_str();
488 }
489
490protected:
Johnny Chenfd158f42011-09-21 22:47:15 +0000491 virtual bool
492 ShouldStop (Event *event_ptr)
493 {
494 // ShouldStop() method is idempotent and should not affect hit count.
Johnny Chen16dcf712011-10-17 18:58:00 +0000495 // See Process::RunPrivateStateThread()->Process()->HandlePrivateEvent()
496 // -->Process()::ShouldBroadcastEvent()->ThreadList::ShouldStop()->
497 // Thread::ShouldStop()->ThreadPlanBase::ShouldStop()->
498 // StopInfoWatchpoint::ShouldStop() and
499 // Event::DoOnRemoval()->Process::ProcessEventData::DoOnRemoval()->
500 // StopInfoWatchpoint::PerformAction().
Johnny Chenfd158f42011-09-21 22:47:15 +0000501 if (m_should_stop_is_valid)
502 return m_should_stop;
503
Johnny Chen01a67862011-10-14 00:42:25 +0000504 WatchpointSP wp_sp =
Greg Clayton1ac04c32012-02-21 00:09:25 +0000505 m_thread.CalculateTarget()->GetWatchpointList().FindByID(GetValue());
Johnny Chen01a67862011-10-14 00:42:25 +0000506 if (wp_sp)
Johnny Chenfd158f42011-09-21 22:47:15 +0000507 {
508 // Check if we should stop at a watchpoint.
Greg Clayton1ac04c32012-02-21 00:09:25 +0000509 ExecutionContext exe_ctx (m_thread.GetStackFrameAtIndex(0));
510 StoppointCallbackContext context (event_ptr, exe_ctx, true);
Johnny Chen01a67862011-10-14 00:42:25 +0000511 m_should_stop = wp_sp->ShouldStop (&context);
Johnny Chenfd158f42011-09-21 22:47:15 +0000512 }
513 else
514 {
515 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
516
517 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000518 log->Printf ("Process::%s could not find watchpoint location id: %" PRId64 "...",
Johnny Chenfd158f42011-09-21 22:47:15 +0000519 __FUNCTION__, GetValue());
520
521 m_should_stop = true;
522 }
523 m_should_stop_is_valid = true;
524 return m_should_stop;
525 }
526
Johnny Chen16dcf712011-10-17 18:58:00 +0000527 virtual void
528 PerformAction (Event *event_ptr)
529 {
Johnny Chen209bd652012-08-13 21:09:54 +0000530 LogSP log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS);
Johnny Chen16dcf712011-10-17 18:58:00 +0000531 // We're going to calculate if we should stop or not in some way during the course of
532 // this code. Also by default we're going to stop, so set that here.
533 m_should_stop = true;
534
535 WatchpointSP wp_sp =
Greg Clayton1ac04c32012-02-21 00:09:25 +0000536 m_thread.CalculateTarget()->GetWatchpointList().FindByID(GetValue());
Johnny Chen16dcf712011-10-17 18:58:00 +0000537 if (wp_sp)
538 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000539 ExecutionContext exe_ctx (m_thread.GetStackFrameAtIndex(0));
Johnny Chen209bd652012-08-13 21:09:54 +0000540 Process* process = exe_ctx.GetProcessPtr();
Johnny Chen0f7ad8d2012-08-21 22:06:34 +0000541
Johnny Chen66535a32012-08-21 22:15:58 +0000542 // This sentry object makes sure the current watchpoint is disabled while performing watchpoint actions,
543 // and it is then enabled after we are finished.
Johnny Chen0f7ad8d2012-08-21 22:06:34 +0000544 WatchpointSentry sentry(process, wp_sp.get());
545
Enrico Granataf04a2192012-07-13 23:18:48 +0000546 {
547 // check if this process is running on an architecture where watchpoints trigger
548 // before the associated instruction runs. if so, disable the WP, single-step and then
549 // re-enable the watchpoint
Enrico Granataf04a2192012-07-13 23:18:48 +0000550 if (process)
551 {
552 uint32_t num; bool wp_triggers_after;
553 if (process->GetWatchpointSupportInfo(num, wp_triggers_after).Success())
554 {
555 if (!wp_triggers_after)
556 {
Enrico Granataf04a2192012-07-13 23:18:48 +0000557 ThreadPlan *new_plan = m_thread.QueueThreadPlanForStepSingleInstruction(false, // step-over
558 false, // abort_other_plans
559 true); // stop_other_threads
560 new_plan->SetIsMasterPlan (true);
561 new_plan->SetOkayToDiscard (false);
562 process->GetThreadList().SetSelectedThreadByID (m_thread.GetID());
563 process->Resume ();
564 process->WaitForProcessToStop (NULL);
565 process->GetThreadList().SetSelectedThreadByID (m_thread.GetID());
566 MakeStopInfoValid(); // make sure we do not fail to stop because of the single-step taken above
Enrico Granataf04a2192012-07-13 23:18:48 +0000567 }
568 }
569 }
570 }
Johnny Chen209bd652012-08-13 21:09:54 +0000571
Johnny Chen16dcf712011-10-17 18:58:00 +0000572 if (m_should_stop && wp_sp->GetConditionText() != NULL)
573 {
574 // We need to make sure the user sees any parse errors in their condition, so we'll hook the
575 // constructor errors up to the debugger's Async I/O.
Johnny Chen16dcf712011-10-17 18:58:00 +0000576 ExecutionResults result_code;
577 ValueObjectSP result_value_sp;
578 const bool discard_on_error = true;
579 Error error;
Greg Clayton1ac04c32012-02-21 00:09:25 +0000580 result_code = ClangUserExpression::EvaluateWithError (exe_ctx,
Sean Callanan22019052012-09-08 01:51:48 +0000581 eExecutionPolicyOnlyWhenNeeded,
Sean Callananc7b65062011-11-07 23:35:40 +0000582 lldb::eLanguageTypeUnknown,
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000583 ClangUserExpression::eResultTypeAny,
Johnny Chen16dcf712011-10-17 18:58:00 +0000584 discard_on_error,
585 wp_sp->GetConditionText(),
586 NULL,
587 result_value_sp,
Enrico Granata3372f582012-07-16 23:10:35 +0000588 error,
Greg Clayton26ab83d2012-10-31 20:49:04 +0000589 true,
590 ClangUserExpression::kDefaultTimeout);
Johnny Chen16dcf712011-10-17 18:58:00 +0000591 if (result_code == eExecutionCompleted)
592 {
593 if (result_value_sp)
594 {
595 Scalar scalar_value;
596 if (result_value_sp->ResolveValue (scalar_value))
597 {
598 if (scalar_value.ULongLong(1) == 0)
599 {
600 // We have been vetoed. This takes precedence over querying
601 // the watchpoint whether it should stop (aka ignore count and
602 // friends). See also StopInfoWatchpoint::ShouldStop() as well
603 // as Process::ProcessEventData::DoOnRemoval().
604 m_should_stop = false;
605 }
606 else
607 m_should_stop = true;
608 if (log)
609 log->Printf("Condition successfully evaluated, result is %s.\n",
610 m_should_stop ? "true" : "false");
611 }
612 else
613 {
614 m_should_stop = true;
615 if (log)
616 log->Printf("Failed to get an integer result from the expression.");
617 }
618 }
619 }
620 else
621 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000622 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
Johnny Chen16dcf712011-10-17 18:58:00 +0000623 StreamSP error_sp = debugger.GetAsyncErrorStream ();
Johnny Chen5f3bf632012-01-24 23:19:25 +0000624 error_sp->Printf ("Stopped due to an error evaluating condition of watchpoint ");
Johnny Chen16dcf712011-10-17 18:58:00 +0000625 wp_sp->GetDescription (error_sp.get(), eDescriptionLevelBrief);
626 error_sp->Printf (": \"%s\"",
627 wp_sp->GetConditionText());
628 error_sp->EOL();
629 const char *err_str = error.AsCString("<Unknown Error>");
630 if (log)
631 log->Printf("Error evaluating condition: \"%s\"\n", err_str);
632
633 error_sp->PutCString (err_str);
634 error_sp->EOL();
635 error_sp->Flush();
636 // If the condition fails to be parsed or run, we should stop.
637 m_should_stop = true;
638 }
639 }
Johnny Chen13206412012-08-09 23:10:57 +0000640
641 // If the condition says to stop, we run the callback to further decide whether to stop.
642 if (m_should_stop)
643 {
Johnny Chen209bd652012-08-13 21:09:54 +0000644 StoppointCallbackContext context (event_ptr, exe_ctx, false);
Johnny Chen13206412012-08-09 23:10:57 +0000645 bool stop_requested = wp_sp->InvokeCallback (&context);
646 // Also make sure that the callback hasn't continued the target.
647 // If it did, when we'll set m_should_stop to false and get out of here.
648 if (HasTargetRunSinceMe ())
649 m_should_stop = false;
650
651 if (m_should_stop && !stop_requested)
652 {
653 // We have been vetoed by the callback mechanism.
654 m_should_stop = false;
655 }
656 }
Jim Inghama7dfb662012-10-23 07:20:06 +0000657 // Finally, if we are going to stop, print out the new & old values:
658 if (m_should_stop)
659 {
660 wp_sp->CaptureWatchedValue(exe_ctx);
661
662 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
663 StreamSP output_sp = debugger.GetAsyncOutputStream ();
664 wp_sp->DumpSnapshots(output_sp.get());
665 output_sp->EOL();
666 output_sp->Flush();
667 }
668
Johnny Chen16dcf712011-10-17 18:58:00 +0000669 }
670 else
671 {
Jason Molendaccd41e52012-10-04 22:47:07 +0000672 LogSP log_process(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Johnny Chen16dcf712011-10-17 18:58:00 +0000673
Jason Molendaccd41e52012-10-04 22:47:07 +0000674 if (log_process)
Daniel Malead01b2952012-11-29 21:49:15 +0000675 log_process->Printf ("Process::%s could not find watchpoint id: %" PRId64 "...", __FUNCTION__, m_value);
Johnny Chen16dcf712011-10-17 18:58:00 +0000676 }
677 if (log)
678 log->Printf ("Process::%s returning from action with m_should_stop: %d.", __FUNCTION__, m_should_stop);
Jim Inghama7dfb662012-10-23 07:20:06 +0000679
Johnny Chen16dcf712011-10-17 18:58:00 +0000680 }
681
Greg Claytonf4b47e12010-08-04 01:40:35 +0000682private:
683 std::string m_description;
Johnny Chenfd158f42011-09-21 22:47:15 +0000684 bool m_should_stop;
685 bool m_should_stop_is_valid;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000686};
687
688
689
690//----------------------------------------------------------------------
691// StopInfoUnixSignal
692//----------------------------------------------------------------------
693
694class StopInfoUnixSignal : public StopInfo
695{
696public:
697
698 StopInfoUnixSignal (Thread &thread, int signo) :
Greg Claytona658fd22011-06-04 01:26:29 +0000699 StopInfo (thread, signo)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000700 {
701 }
702
703 virtual ~StopInfoUnixSignal ()
704 {
705 }
706
707
708 virtual StopReason
709 GetStopReason () const
710 {
711 return eStopReasonSignal;
712 }
713
714 virtual bool
715 ShouldStop (Event *event_ptr)
716 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000717 return m_thread.GetProcess()->GetUnixSignals().GetShouldStop (m_value);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000718 }
719
720
721 // If should stop returns false, check if we should notify of this event
722 virtual bool
723 ShouldNotify (Event *event_ptr)
724 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000725 return m_thread.GetProcess()->GetUnixSignals().GetShouldNotify (m_value);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000726 }
727
728
729 virtual void
730 WillResume (lldb::StateType resume_state)
731 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000732 if (m_thread.GetProcess()->GetUnixSignals().GetShouldSuppress(m_value) == false)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000733 m_thread.SetResumeSignal(m_value);
734 }
735
736 virtual const char *
737 GetDescription ()
738 {
739 if (m_description.empty())
740 {
741 StreamString strm;
Greg Clayton1ac04c32012-02-21 00:09:25 +0000742 const char *signal_name = m_thread.GetProcess()->GetUnixSignals().GetSignalAsCString (m_value);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000743 if (signal_name)
Greg Clayton0603aa92010-10-04 01:05:56 +0000744 strm.Printf("signal %s", signal_name);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000745 else
Daniel Malead01b2952012-11-29 21:49:15 +0000746 strm.Printf("signal %" PRIi64, m_value);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000747 m_description.swap (strm.GetString());
748 }
749 return m_description.c_str();
750 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000751};
752
753//----------------------------------------------------------------------
754// StopInfoTrace
755//----------------------------------------------------------------------
756
757class StopInfoTrace : public StopInfo
758{
759public:
760
761 StopInfoTrace (Thread &thread) :
762 StopInfo (thread, LLDB_INVALID_UID)
763 {
764 }
765
766 virtual ~StopInfoTrace ()
767 {
768 }
769
770 virtual StopReason
771 GetStopReason () const
772 {
773 return eStopReasonTrace;
774 }
775
776 virtual const char *
777 GetDescription ()
778 {
Greg Claytona658fd22011-06-04 01:26:29 +0000779 if (m_description.empty())
Greg Claytonf4b47e12010-08-04 01:40:35 +0000780 return "trace";
Greg Claytona658fd22011-06-04 01:26:29 +0000781 else
782 return m_description.c_str();
783 }
784};
785
786
787//----------------------------------------------------------------------
788// StopInfoException
789//----------------------------------------------------------------------
790
791class StopInfoException : public StopInfo
792{
793public:
794
795 StopInfoException (Thread &thread, const char *description) :
796 StopInfo (thread, LLDB_INVALID_UID)
797 {
798 if (description)
799 SetDescription (description);
800 }
801
802 virtual
803 ~StopInfoException ()
804 {
805 }
806
807 virtual StopReason
808 GetStopReason () const
809 {
810 return eStopReasonException;
811 }
812
813 virtual const char *
814 GetDescription ()
815 {
816 if (m_description.empty())
817 return "exception";
818 else
819 return m_description.c_str();
Greg Claytonf4b47e12010-08-04 01:40:35 +0000820 }
821};
822
823
824//----------------------------------------------------------------------
825// StopInfoThreadPlan
826//----------------------------------------------------------------------
827
828class StopInfoThreadPlan : public StopInfo
829{
830public:
831
Jim Ingham73ca05a2011-12-17 01:35:57 +0000832 StopInfoThreadPlan (ThreadPlanSP &plan_sp, ValueObjectSP &return_valobj_sp) :
Greg Claytonf4b47e12010-08-04 01:40:35 +0000833 StopInfo (plan_sp->GetThread(), LLDB_INVALID_UID),
Jim Ingham73ca05a2011-12-17 01:35:57 +0000834 m_plan_sp (plan_sp),
835 m_return_valobj_sp (return_valobj_sp)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000836 {
837 }
838
839 virtual ~StopInfoThreadPlan ()
840 {
841 }
842
843 virtual StopReason
844 GetStopReason () const
845 {
846 return eStopReasonPlanComplete;
847 }
848
849 virtual const char *
850 GetDescription ()
851 {
852 if (m_description.empty())
853 {
854 StreamString strm;
855 m_plan_sp->GetDescription (&strm, eDescriptionLevelBrief);
856 m_description.swap (strm.GetString());
857 }
858 return m_description.c_str();
859 }
Jim Ingham73ca05a2011-12-17 01:35:57 +0000860
861 ValueObjectSP
862 GetReturnValueObject()
863 {
864 return m_return_valobj_sp;
865 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000866
867private:
868 ThreadPlanSP m_plan_sp;
Jim Ingham73ca05a2011-12-17 01:35:57 +0000869 ValueObjectSP m_return_valobj_sp;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000870};
Greg Clayton90ba8112012-12-05 00:16:59 +0000871
872class StopInfoExec : public StopInfo
873{
874public:
875
876 StopInfoExec (Thread &thread) :
877 StopInfo (thread, LLDB_INVALID_UID),
878 m_performed_action (false)
879 {
880 }
881
882 virtual
883 ~StopInfoExec ()
884 {
885 }
886
887 virtual StopReason
888 GetStopReason () const
889 {
890 return eStopReasonExec;
891 }
892
893 virtual const char *
894 GetDescription ()
895 {
896 return "exec";
897 }
898protected:
899protected:
900
901 virtual void
902 PerformAction (Event *event_ptr)
903 {
904 // Only perform the action once
905 if (m_performed_action)
906 return;
907 m_performed_action = true;
908 m_thread.GetProcess()->DidExec();
909 }
910
911 bool m_performed_action;
912};
913
Jim Ingham4b536182011-08-09 02:12:22 +0000914} // namespace lldb_private
Greg Claytonf4b47e12010-08-04 01:40:35 +0000915
916StopInfoSP
917StopInfo::CreateStopReasonWithBreakpointSiteID (Thread &thread, break_id_t break_id)
918{
919 return StopInfoSP (new StopInfoBreakpoint (thread, break_id));
920}
921
922StopInfoSP
Jim Ingham36f3b362010-10-14 23:45:03 +0000923StopInfo::CreateStopReasonWithBreakpointSiteID (Thread &thread, break_id_t break_id, bool should_stop)
924{
925 return StopInfoSP (new StopInfoBreakpoint (thread, break_id, should_stop));
926}
927
928StopInfoSP
Greg Claytonf4b47e12010-08-04 01:40:35 +0000929StopInfo::CreateStopReasonWithWatchpointID (Thread &thread, break_id_t watch_id)
930{
931 return StopInfoSP (new StopInfoWatchpoint (thread, watch_id));
932}
933
934StopInfoSP
935StopInfo::CreateStopReasonWithSignal (Thread &thread, int signo)
936{
937 return StopInfoSP (new StopInfoUnixSignal (thread, signo));
938}
939
940StopInfoSP
941StopInfo::CreateStopReasonToTrace (Thread &thread)
942{
943 return StopInfoSP (new StopInfoTrace (thread));
944}
945
946StopInfoSP
Jim Ingham73ca05a2011-12-17 01:35:57 +0000947StopInfo::CreateStopReasonWithPlan (ThreadPlanSP &plan_sp, ValueObjectSP return_valobj_sp)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000948{
Jim Ingham73ca05a2011-12-17 01:35:57 +0000949 return StopInfoSP (new StopInfoThreadPlan (plan_sp, return_valobj_sp));
Greg Claytonf4b47e12010-08-04 01:40:35 +0000950}
Greg Claytona658fd22011-06-04 01:26:29 +0000951
952StopInfoSP
953StopInfo::CreateStopReasonWithException (Thread &thread, const char *description)
954{
955 return StopInfoSP (new StopInfoException (thread, description));
956}
Jim Ingham73ca05a2011-12-17 01:35:57 +0000957
Greg Clayton90ba8112012-12-05 00:16:59 +0000958StopInfoSP
959StopInfo::CreateStopReasonWithExec (Thread &thread)
960{
961 return StopInfoSP (new StopInfoExec (thread));
962}
963
Jim Ingham73ca05a2011-12-17 01:35:57 +0000964ValueObjectSP
965StopInfo::GetReturnValueObject(StopInfoSP &stop_info_sp)
966{
967 if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonPlanComplete)
968 {
969 StopInfoThreadPlan *plan_stop_info = static_cast<StopInfoThreadPlan *>(stop_info_sp.get());
970 return plan_stop_info->GetReturnValueObject();
971 }
972 else
973 return ValueObjectSP();
974}