blob: 1a7779d4fb9243ca37675956c98db5e27834da52 [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;
Jim Ingham29950772013-01-26 02:19:28 +0000209 // If we have just hit an internal breakpoint, and it has a kind description, print that instead of the
210 // full breakpoint printing:
211 if (bp_site_sp->IsInternal())
212 {
213 size_t num_owners = bp_site_sp->GetNumberOfOwners();
214 for (size_t idx = 0; idx < num_owners; idx++)
215 {
216 const char *kind = bp_site_sp->GetOwnerAtIndex(idx)->GetBreakpoint().GetBreakpointKind();
217 if (kind != NULL)
218 {
219 m_description.assign (kind);
220 return kind;
221 }
222 }
223 }
224
Jim Inghamf57b4352012-11-10 02:08:14 +0000225 strm.Printf("breakpoint ");
226 bp_site_sp->GetDescription(&strm, eDescriptionLevelBrief);
227 m_description.swap (strm.GetString());
228 }
229 else
230 {
231 StreamString strm;
232 if (m_break_id != LLDB_INVALID_BREAK_ID)
233 {
234 if (m_was_one_shot)
235 strm.Printf ("one-shot breakpoint %d", m_break_id);
236 else
237 strm.Printf ("breakpoint %d which has been deleted.", m_break_id);
238 }
239 else if (m_address == LLDB_INVALID_ADDRESS)
Daniel Malead01b2952012-11-29 21:49:15 +0000240 strm.Printf("breakpoint site %" PRIi64 " which has been deleted - unknown address", m_value);
Jim Inghamf57b4352012-11-10 02:08:14 +0000241 else
Daniel Malead01b2952012-11-29 21:49:15 +0000242 strm.Printf("breakpoint site %" PRIi64 " which has been deleted - was at 0x%" PRIx64, m_value, m_address);
Jim Inghamf57b4352012-11-10 02:08:14 +0000243
244 m_description.swap (strm.GetString());
245 }
246 }
247 return m_description.c_str();
248 }
249
250protected:
Jim Ingham6d66ce62012-04-20 21:16:56 +0000251 bool
252 ShouldStop (Event *event_ptr)
253 {
254 // This just reports the work done by PerformAction or the synchronous stop. It should
255 // only ever get called after they have had a chance to run.
256 assert (m_should_stop_is_valid);
257 return m_should_stop;
258 }
259
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000260 virtual void
261 PerformAction (Event *event_ptr)
262 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000263 if (!m_should_perform_action)
264 return;
265 m_should_perform_action = false;
266
Jim Ingham4b536182011-08-09 02:12:22 +0000267 LogSP log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
Jim Ingham4b536182011-08-09 02:12:22 +0000268
Greg Clayton1ac04c32012-02-21 00:09:25 +0000269 BreakpointSiteSP bp_site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (m_value));
Jim Ingham79ea1d82011-12-09 04:17:31 +0000270
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000271 if (bp_site_sp)
272 {
273 size_t num_owners = bp_site_sp->GetNumberOfOwners();
Jim Ingham79ea1d82011-12-09 04:17:31 +0000274
275 if (num_owners == 0)
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000276 {
Jim Ingham79ea1d82011-12-09 04:17:31 +0000277 m_should_stop = true;
278 }
279 else
280 {
281 // We go through each location, and test first its condition. If the condition says to stop,
282 // then we run the callback for that location. If that callback says to stop as well, then
283 // we set m_should_stop to true; we are going to stop.
284 // But we still want to give all the breakpoints whose conditions say we are going to stop a
285 // chance to run their callbacks.
286 // Of course if any callback restarts the target by putting "continue" in the callback, then
287 // we're going to restart, without running the rest of the callbacks. And in this case we will
288 // end up not stopping even if another location said we should stop. But that's better than not
289 // running all the callbacks.
290
291 m_should_stop = false;
292
Greg Clayton1ac04c32012-02-21 00:09:25 +0000293 ExecutionContext exe_ctx (m_thread.GetStackFrameAtIndex(0));
Jim Ingham184e9812013-01-15 02:47:48 +0000294 Process *process = exe_ctx.GetProcessPtr();
295 if (process->GetModIDRef().IsLastResumeForUserExpression())
296 {
297 // If we are in the middle of evaluating an expression, don't run asynchronous breakpoint commands or
298 // expressions. That could lead to infinite recursion if the command or condition re-calls the function
299 // with this breakpoint.
300 // TODO: We can keep a list of the breakpoints we've seen while running expressions in the nested
301 // PerformAction calls that can arise when the action runs a function that hits another breakpoint,
302 // and only stop running commands when we see the same breakpoint hit a second time.
303
304 m_should_stop_is_valid = true;
305 if (log)
306 log->Printf ("StopInfoBreakpoint::PerformAction - Hit a breakpoint while running an expression,"
307 " not running commands to avoid recursion.");
308 bool ignoring_breakpoints = process->GetIgnoreBreakpointsInExpressions();
309 if (ignoring_breakpoints)
310 {
311 m_should_stop = false;
312 // Internal breakpoints will always stop.
313 for (size_t j = 0; j < num_owners; j++)
314 {
315 lldb::BreakpointLocationSP bp_loc_sp = bp_site_sp->GetOwnerAtIndex(j);
316 if (bp_loc_sp->GetBreakpoint().IsInternal())
317 {
318 m_should_stop = true;
319 break;
320 }
321 }
322 }
323 else
324 {
325 m_should_stop = true;
326 }
327 if (log)
328 log->Printf ("StopInfoBreakpoint::PerformAction - in expression, continuing: %s.",
329 m_should_stop ? "true" : "false");
330 process->GetTarget().GetDebugger().GetAsyncOutputStream()->Printf("Warning: hit breakpoint while "
331 "running function, skipping commands and conditions to prevent recursion.");
332 return;
333 }
334
Greg Clayton1ac04c32012-02-21 00:09:25 +0000335 StoppointCallbackContext context (event_ptr, exe_ctx, false);
Jim Ingham4b536182011-08-09 02:12:22 +0000336
Jim Ingham4b536182011-08-09 02:12:22 +0000337 for (size_t j = 0; j < num_owners; j++)
338 {
339 lldb::BreakpointLocationSP bp_loc_sp = bp_site_sp->GetOwnerAtIndex(j);
Jim Ingham79ea1d82011-12-09 04:17:31 +0000340
341 // First run the condition for the breakpoint. If that says we should stop, then we'll run
342 // the callback for the breakpoint. If the callback says we shouldn't stop that will win.
343
344 bool condition_says_stop = true;
Jim Ingham4b536182011-08-09 02:12:22 +0000345 if (bp_loc_sp->GetConditionText() != NULL)
346 {
347 // We need to make sure the user sees any parse errors in their condition, so we'll hook the
348 // constructor errors up to the debugger's Async I/O.
349
Jim Ingham4b536182011-08-09 02:12:22 +0000350 ValueObjectSP result_valobj_sp;
351
352 ExecutionResults result_code;
353 ValueObjectSP result_value_sp;
Jim Ingham184e9812013-01-15 02:47:48 +0000354 const bool unwind_on_error = true;
355 const bool ignore_breakpoints = true;
Jim Ingham4b536182011-08-09 02:12:22 +0000356 Error error;
Greg Clayton1ac04c32012-02-21 00:09:25 +0000357 result_code = ClangUserExpression::EvaluateWithError (exe_ctx,
Jim Ingham6d66ce62012-04-20 21:16:56 +0000358 eExecutionPolicyOnlyWhenNeeded,
Sean Callananc7b65062011-11-07 23:35:40 +0000359 lldb::eLanguageTypeUnknown,
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000360 ClangUserExpression::eResultTypeAny,
Jim Ingham184e9812013-01-15 02:47:48 +0000361 unwind_on_error,
362 ignore_breakpoints,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000363 bp_loc_sp->GetConditionText(),
364 NULL,
365 result_value_sp,
Greg Clayton26ab83d2012-10-31 20:49:04 +0000366 error,
367 true,
368 ClangUserExpression::kDefaultTimeout);
Jim Ingham4b536182011-08-09 02:12:22 +0000369 if (result_code == eExecutionCompleted)
370 {
371 if (result_value_sp)
372 {
373 Scalar scalar_value;
374 if (result_value_sp->ResolveValue (scalar_value))
375 {
376 if (scalar_value.ULongLong(1) == 0)
Jim Ingham79ea1d82011-12-09 04:17:31 +0000377 condition_says_stop = false;
Jim Ingham4b536182011-08-09 02:12:22 +0000378 else
Jim Ingham79ea1d82011-12-09 04:17:31 +0000379 condition_says_stop = true;
Jim Ingham4b536182011-08-09 02:12:22 +0000380 if (log)
381 log->Printf("Condition successfully evaluated, result is %s.\n",
382 m_should_stop ? "true" : "false");
383 }
384 else
385 {
Jim Ingham79ea1d82011-12-09 04:17:31 +0000386 condition_says_stop = true;
Jim Ingham4b536182011-08-09 02:12:22 +0000387 if (log)
388 log->Printf("Failed to get an integer result from the expression.");
389 }
390 }
391 }
392 else
393 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000394 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
Jim Ingham4b536182011-08-09 02:12:22 +0000395 StreamSP error_sp = debugger.GetAsyncErrorStream ();
396 error_sp->Printf ("Stopped due to an error evaluating condition of breakpoint ");
397 bp_loc_sp->GetDescription (error_sp.get(), eDescriptionLevelBrief);
398 error_sp->Printf (": \"%s\"",
399 bp_loc_sp->GetConditionText());
400 error_sp->EOL();
401 const char *err_str = error.AsCString("<Unknown Error>");
402 if (log)
403 log->Printf("Error evaluating condition: \"%s\"\n", err_str);
404
405 error_sp->PutCString (err_str);
406 error_sp->EOL();
407 error_sp->Flush();
408 // If the condition fails to be parsed or run, we should stop.
Jim Ingham79ea1d82011-12-09 04:17:31 +0000409 condition_says_stop = true;
Jim Ingham4b536182011-08-09 02:12:22 +0000410 }
411 }
412
Jim Ingham79ea1d82011-12-09 04:17:31 +0000413 // If this location's condition says we should aren't going to stop,
414 // then don't run the callback for this location.
415 if (!condition_says_stop)
416 continue;
417
418 bool callback_says_stop;
419
420 // FIXME: For now the callbacks have to run in async mode - the first time we restart we need
421 // to get out of there. So set it here.
422 // When we figure out how to nest breakpoint hits then this will change.
423
Greg Clayton1ac04c32012-02-21 00:09:25 +0000424 Debugger &debugger = m_thread.CalculateTarget()->GetDebugger();
Jim Ingham79ea1d82011-12-09 04:17:31 +0000425 bool old_async = debugger.GetAsyncExecution();
426 debugger.SetAsyncExecution (true);
427
428 callback_says_stop = bp_loc_sp->InvokeCallback (&context);
429
430 debugger.SetAsyncExecution (old_async);
431
432 if (callback_says_stop)
433 m_should_stop = true;
Jim Inghamca36cd12012-10-05 19:16:31 +0000434
435 // If we are going to stop for this breakpoint, then remove the breakpoint.
436 if (callback_says_stop && bp_loc_sp && bp_loc_sp->GetBreakpoint().IsOneShot())
437 {
438 m_thread.GetProcess()->GetTarget().RemoveBreakpointByID (bp_loc_sp->GetBreakpoint().GetID());
439 }
Jim Ingham79ea1d82011-12-09 04:17:31 +0000440
441 // Also make sure that the callback hasn't continued the target.
442 // If it did, when we'll set m_should_start to false and get out of here.
443 if (HasTargetRunSinceMe ())
444 {
445 m_should_stop = false;
Jim Ingham4b536182011-08-09 02:12:22 +0000446 break;
Jim Ingham79ea1d82011-12-09 04:17:31 +0000447 }
Jim Ingham4b536182011-08-09 02:12:22 +0000448 }
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000449 }
Jim Ingham79ea1d82011-12-09 04:17:31 +0000450 // We've figured out what this stop wants to do, so mark it as valid so we don't compute it again.
451 m_should_stop_is_valid = true;
452
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000453 }
454 else
455 {
Jim Ingham79ea1d82011-12-09 04:17:31 +0000456 m_should_stop = true;
457 m_should_stop_is_valid = true;
Jason Molendaccd41e52012-10-04 22:47:07 +0000458 LogSP log_process(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000459
Jason Molendaccd41e52012-10-04 22:47:07 +0000460 if (log_process)
Daniel Malead01b2952012-11-29 21:49:15 +0000461 log_process->Printf ("Process::%s could not find breakpoint site id: %" PRId64 "...", __FUNCTION__, m_value);
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000462 }
Jim Ingham4b536182011-08-09 02:12:22 +0000463 if (log)
464 log->Printf ("Process::%s returning from action with m_should_stop: %d.", __FUNCTION__, m_should_stop);
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000465 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000466
467private:
468 std::string m_description;
469 bool m_should_stop;
470 bool m_should_stop_is_valid;
Jim Ingham0f16e732011-02-08 05:20:59 +0000471 bool m_should_perform_action; // Since we are trying to preserve the "state" of the system even if we run functions
472 // etc. behind the users backs, we need to make sure we only REALLY perform the action once.
Jim Ingham52c7d202011-10-28 01:12:19 +0000473 lldb::addr_t m_address; // We use this to capture the breakpoint site address when we create the StopInfo,
474 // in case somebody deletes it between the time the StopInfo is made and the
475 // description is asked for.
Jim Inghamca36cd12012-10-05 19:16:31 +0000476 lldb::break_id_t m_break_id;
477 bool m_was_one_shot;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000478};
479
480
481//----------------------------------------------------------------------
482// StopInfoWatchpoint
483//----------------------------------------------------------------------
484
485class StopInfoWatchpoint : public StopInfo
486{
487public:
Jim Inghamf57b4352012-11-10 02:08:14 +0000488 // Make sure watchpoint is properly disabled and subsequently enabled while performing watchpoint actions.
489 class WatchpointSentry {
490 public:
491 WatchpointSentry(Process *p, Watchpoint *w):
492 process(p),
493 watchpoint(w)
494 {
495 if (process && watchpoint)
496 {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000497 const bool notify = false;
Jim Inghamf57b4352012-11-10 02:08:14 +0000498 watchpoint->TurnOnEphemeralMode();
Jim Ingham1b5792e2012-12-18 02:03:49 +0000499 process->DisableWatchpoint(watchpoint, notify);
Jim Inghamf57b4352012-11-10 02:08:14 +0000500 }
501 }
502 ~WatchpointSentry()
503 {
504 if (process && watchpoint)
505 {
506 if (!watchpoint->IsDisabledDuringEphemeralMode())
Jim Ingham1b5792e2012-12-18 02:03:49 +0000507 {
508 const bool notify = false;
509 process->EnableWatchpoint(watchpoint, notify);
510 }
Jim Inghamf57b4352012-11-10 02:08:14 +0000511 watchpoint->TurnOffEphemeralMode();
512 }
513 }
514 private:
515 Process *process;
516 Watchpoint *watchpoint;
517 };
Greg Claytonf4b47e12010-08-04 01:40:35 +0000518
519 StopInfoWatchpoint (Thread &thread, break_id_t watch_id) :
Johnny Chenfd158f42011-09-21 22:47:15 +0000520 StopInfo(thread, watch_id),
521 m_description(),
522 m_should_stop(false),
523 m_should_stop_is_valid(false)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000524 {
525 }
526
527 virtual ~StopInfoWatchpoint ()
528 {
529 }
530
531 virtual StopReason
532 GetStopReason () const
533 {
534 return eStopReasonWatchpoint;
535 }
536
Jim Inghamf57b4352012-11-10 02:08:14 +0000537 virtual const char *
538 GetDescription ()
539 {
540 if (m_description.empty())
541 {
542 StreamString strm;
Daniel Malead01b2952012-11-29 21:49:15 +0000543 strm.Printf("watchpoint %" PRIi64, m_value);
Jim Inghamf57b4352012-11-10 02:08:14 +0000544 m_description.swap (strm.GetString());
545 }
546 return m_description.c_str();
547 }
548
549protected:
Johnny Chenfd158f42011-09-21 22:47:15 +0000550 virtual bool
551 ShouldStop (Event *event_ptr)
552 {
553 // ShouldStop() method is idempotent and should not affect hit count.
Johnny Chen16dcf712011-10-17 18:58:00 +0000554 // See Process::RunPrivateStateThread()->Process()->HandlePrivateEvent()
555 // -->Process()::ShouldBroadcastEvent()->ThreadList::ShouldStop()->
556 // Thread::ShouldStop()->ThreadPlanBase::ShouldStop()->
557 // StopInfoWatchpoint::ShouldStop() and
558 // Event::DoOnRemoval()->Process::ProcessEventData::DoOnRemoval()->
559 // StopInfoWatchpoint::PerformAction().
Johnny Chenfd158f42011-09-21 22:47:15 +0000560 if (m_should_stop_is_valid)
561 return m_should_stop;
562
Johnny Chen01a67862011-10-14 00:42:25 +0000563 WatchpointSP wp_sp =
Greg Clayton1ac04c32012-02-21 00:09:25 +0000564 m_thread.CalculateTarget()->GetWatchpointList().FindByID(GetValue());
Johnny Chen01a67862011-10-14 00:42:25 +0000565 if (wp_sp)
Johnny Chenfd158f42011-09-21 22:47:15 +0000566 {
567 // Check if we should stop at a watchpoint.
Greg Clayton1ac04c32012-02-21 00:09:25 +0000568 ExecutionContext exe_ctx (m_thread.GetStackFrameAtIndex(0));
569 StoppointCallbackContext context (event_ptr, exe_ctx, true);
Johnny Chen01a67862011-10-14 00:42:25 +0000570 m_should_stop = wp_sp->ShouldStop (&context);
Johnny Chenfd158f42011-09-21 22:47:15 +0000571 }
572 else
573 {
574 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
575
576 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000577 log->Printf ("Process::%s could not find watchpoint location id: %" PRId64 "...",
Johnny Chenfd158f42011-09-21 22:47:15 +0000578 __FUNCTION__, GetValue());
579
580 m_should_stop = true;
581 }
582 m_should_stop_is_valid = true;
583 return m_should_stop;
584 }
585
Johnny Chen16dcf712011-10-17 18:58:00 +0000586 virtual void
587 PerformAction (Event *event_ptr)
588 {
Johnny Chen209bd652012-08-13 21:09:54 +0000589 LogSP log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS);
Johnny Chen16dcf712011-10-17 18:58:00 +0000590 // We're going to calculate if we should stop or not in some way during the course of
591 // this code. Also by default we're going to stop, so set that here.
592 m_should_stop = true;
593
594 WatchpointSP wp_sp =
Greg Clayton1ac04c32012-02-21 00:09:25 +0000595 m_thread.CalculateTarget()->GetWatchpointList().FindByID(GetValue());
Johnny Chen16dcf712011-10-17 18:58:00 +0000596 if (wp_sp)
597 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000598 ExecutionContext exe_ctx (m_thread.GetStackFrameAtIndex(0));
Johnny Chen209bd652012-08-13 21:09:54 +0000599 Process* process = exe_ctx.GetProcessPtr();
Johnny Chen0f7ad8d2012-08-21 22:06:34 +0000600
Johnny Chen66535a32012-08-21 22:15:58 +0000601 // This sentry object makes sure the current watchpoint is disabled while performing watchpoint actions,
602 // and it is then enabled after we are finished.
Johnny Chen0f7ad8d2012-08-21 22:06:34 +0000603 WatchpointSentry sentry(process, wp_sp.get());
604
Enrico Granataf04a2192012-07-13 23:18:48 +0000605 {
606 // check if this process is running on an architecture where watchpoints trigger
607 // before the associated instruction runs. if so, disable the WP, single-step and then
608 // re-enable the watchpoint
Enrico Granataf04a2192012-07-13 23:18:48 +0000609 if (process)
610 {
611 uint32_t num; bool wp_triggers_after;
612 if (process->GetWatchpointSupportInfo(num, wp_triggers_after).Success())
613 {
614 if (!wp_triggers_after)
615 {
Enrico Granataf04a2192012-07-13 23:18:48 +0000616 ThreadPlan *new_plan = m_thread.QueueThreadPlanForStepSingleInstruction(false, // step-over
617 false, // abort_other_plans
618 true); // stop_other_threads
619 new_plan->SetIsMasterPlan (true);
620 new_plan->SetOkayToDiscard (false);
621 process->GetThreadList().SetSelectedThreadByID (m_thread.GetID());
622 process->Resume ();
623 process->WaitForProcessToStop (NULL);
624 process->GetThreadList().SetSelectedThreadByID (m_thread.GetID());
625 MakeStopInfoValid(); // make sure we do not fail to stop because of the single-step taken above
Enrico Granataf04a2192012-07-13 23:18:48 +0000626 }
627 }
628 }
629 }
Johnny Chen209bd652012-08-13 21:09:54 +0000630
Johnny Chen16dcf712011-10-17 18:58:00 +0000631 if (m_should_stop && wp_sp->GetConditionText() != NULL)
632 {
633 // We need to make sure the user sees any parse errors in their condition, so we'll hook the
634 // constructor errors up to the debugger's Async I/O.
Johnny Chen16dcf712011-10-17 18:58:00 +0000635 ExecutionResults result_code;
636 ValueObjectSP result_value_sp;
Jim Ingham184e9812013-01-15 02:47:48 +0000637 const bool unwind_on_error = true;
638 const bool ignore_breakpoints = true;
Johnny Chen16dcf712011-10-17 18:58:00 +0000639 Error error;
Greg Clayton1ac04c32012-02-21 00:09:25 +0000640 result_code = ClangUserExpression::EvaluateWithError (exe_ctx,
Sean Callanan22019052012-09-08 01:51:48 +0000641 eExecutionPolicyOnlyWhenNeeded,
Sean Callananc7b65062011-11-07 23:35:40 +0000642 lldb::eLanguageTypeUnknown,
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000643 ClangUserExpression::eResultTypeAny,
Jim Ingham184e9812013-01-15 02:47:48 +0000644 unwind_on_error,
645 ignore_breakpoints,
Johnny Chen16dcf712011-10-17 18:58:00 +0000646 wp_sp->GetConditionText(),
647 NULL,
648 result_value_sp,
Enrico Granata3372f582012-07-16 23:10:35 +0000649 error,
Greg Clayton26ab83d2012-10-31 20:49:04 +0000650 true,
651 ClangUserExpression::kDefaultTimeout);
Johnny Chen16dcf712011-10-17 18:58:00 +0000652 if (result_code == eExecutionCompleted)
653 {
654 if (result_value_sp)
655 {
656 Scalar scalar_value;
657 if (result_value_sp->ResolveValue (scalar_value))
658 {
659 if (scalar_value.ULongLong(1) == 0)
660 {
661 // We have been vetoed. This takes precedence over querying
662 // the watchpoint whether it should stop (aka ignore count and
663 // friends). See also StopInfoWatchpoint::ShouldStop() as well
664 // as Process::ProcessEventData::DoOnRemoval().
665 m_should_stop = false;
666 }
667 else
668 m_should_stop = true;
669 if (log)
670 log->Printf("Condition successfully evaluated, result is %s.\n",
671 m_should_stop ? "true" : "false");
672 }
673 else
674 {
675 m_should_stop = true;
676 if (log)
677 log->Printf("Failed to get an integer result from the expression.");
678 }
679 }
680 }
681 else
682 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000683 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
Johnny Chen16dcf712011-10-17 18:58:00 +0000684 StreamSP error_sp = debugger.GetAsyncErrorStream ();
Johnny Chen5f3bf632012-01-24 23:19:25 +0000685 error_sp->Printf ("Stopped due to an error evaluating condition of watchpoint ");
Johnny Chen16dcf712011-10-17 18:58:00 +0000686 wp_sp->GetDescription (error_sp.get(), eDescriptionLevelBrief);
687 error_sp->Printf (": \"%s\"",
688 wp_sp->GetConditionText());
689 error_sp->EOL();
690 const char *err_str = error.AsCString("<Unknown Error>");
691 if (log)
692 log->Printf("Error evaluating condition: \"%s\"\n", err_str);
693
694 error_sp->PutCString (err_str);
695 error_sp->EOL();
696 error_sp->Flush();
697 // If the condition fails to be parsed or run, we should stop.
698 m_should_stop = true;
699 }
700 }
Johnny Chen13206412012-08-09 23:10:57 +0000701
702 // If the condition says to stop, we run the callback to further decide whether to stop.
703 if (m_should_stop)
704 {
Johnny Chen209bd652012-08-13 21:09:54 +0000705 StoppointCallbackContext context (event_ptr, exe_ctx, false);
Johnny Chen13206412012-08-09 23:10:57 +0000706 bool stop_requested = wp_sp->InvokeCallback (&context);
707 // Also make sure that the callback hasn't continued the target.
708 // If it did, when we'll set m_should_stop to false and get out of here.
709 if (HasTargetRunSinceMe ())
710 m_should_stop = false;
711
712 if (m_should_stop && !stop_requested)
713 {
714 // We have been vetoed by the callback mechanism.
715 m_should_stop = false;
716 }
717 }
Jim Inghama7dfb662012-10-23 07:20:06 +0000718 // Finally, if we are going to stop, print out the new & old values:
719 if (m_should_stop)
720 {
721 wp_sp->CaptureWatchedValue(exe_ctx);
722
723 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
724 StreamSP output_sp = debugger.GetAsyncOutputStream ();
725 wp_sp->DumpSnapshots(output_sp.get());
726 output_sp->EOL();
727 output_sp->Flush();
728 }
729
Johnny Chen16dcf712011-10-17 18:58:00 +0000730 }
731 else
732 {
Jason Molendaccd41e52012-10-04 22:47:07 +0000733 LogSP log_process(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Johnny Chen16dcf712011-10-17 18:58:00 +0000734
Jason Molendaccd41e52012-10-04 22:47:07 +0000735 if (log_process)
Daniel Malead01b2952012-11-29 21:49:15 +0000736 log_process->Printf ("Process::%s could not find watchpoint id: %" PRId64 "...", __FUNCTION__, m_value);
Johnny Chen16dcf712011-10-17 18:58:00 +0000737 }
738 if (log)
739 log->Printf ("Process::%s returning from action with m_should_stop: %d.", __FUNCTION__, m_should_stop);
Jim Inghama7dfb662012-10-23 07:20:06 +0000740
Johnny Chen16dcf712011-10-17 18:58:00 +0000741 }
742
Greg Claytonf4b47e12010-08-04 01:40:35 +0000743private:
744 std::string m_description;
Johnny Chenfd158f42011-09-21 22:47:15 +0000745 bool m_should_stop;
746 bool m_should_stop_is_valid;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000747};
748
749
750
751//----------------------------------------------------------------------
752// StopInfoUnixSignal
753//----------------------------------------------------------------------
754
755class StopInfoUnixSignal : public StopInfo
756{
757public:
758
759 StopInfoUnixSignal (Thread &thread, int signo) :
Greg Claytona658fd22011-06-04 01:26:29 +0000760 StopInfo (thread, signo)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000761 {
762 }
763
764 virtual ~StopInfoUnixSignal ()
765 {
766 }
767
768
769 virtual StopReason
770 GetStopReason () const
771 {
772 return eStopReasonSignal;
773 }
774
775 virtual bool
776 ShouldStop (Event *event_ptr)
777 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000778 return m_thread.GetProcess()->GetUnixSignals().GetShouldStop (m_value);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000779 }
780
781
782 // If should stop returns false, check if we should notify of this event
783 virtual bool
784 ShouldNotify (Event *event_ptr)
785 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000786 return m_thread.GetProcess()->GetUnixSignals().GetShouldNotify (m_value);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000787 }
788
789
790 virtual void
791 WillResume (lldb::StateType resume_state)
792 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000793 if (m_thread.GetProcess()->GetUnixSignals().GetShouldSuppress(m_value) == false)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000794 m_thread.SetResumeSignal(m_value);
795 }
796
797 virtual const char *
798 GetDescription ()
799 {
800 if (m_description.empty())
801 {
802 StreamString strm;
Greg Clayton1ac04c32012-02-21 00:09:25 +0000803 const char *signal_name = m_thread.GetProcess()->GetUnixSignals().GetSignalAsCString (m_value);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000804 if (signal_name)
Greg Clayton0603aa92010-10-04 01:05:56 +0000805 strm.Printf("signal %s", signal_name);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000806 else
Daniel Malead01b2952012-11-29 21:49:15 +0000807 strm.Printf("signal %" PRIi64, m_value);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000808 m_description.swap (strm.GetString());
809 }
810 return m_description.c_str();
811 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000812};
813
814//----------------------------------------------------------------------
815// StopInfoTrace
816//----------------------------------------------------------------------
817
818class StopInfoTrace : public StopInfo
819{
820public:
821
822 StopInfoTrace (Thread &thread) :
823 StopInfo (thread, LLDB_INVALID_UID)
824 {
825 }
826
827 virtual ~StopInfoTrace ()
828 {
829 }
830
831 virtual StopReason
832 GetStopReason () const
833 {
834 return eStopReasonTrace;
835 }
836
837 virtual const char *
838 GetDescription ()
839 {
Greg Claytona658fd22011-06-04 01:26:29 +0000840 if (m_description.empty())
Greg Claytonf4b47e12010-08-04 01:40:35 +0000841 return "trace";
Greg Claytona658fd22011-06-04 01:26:29 +0000842 else
843 return m_description.c_str();
844 }
845};
846
847
848//----------------------------------------------------------------------
849// StopInfoException
850//----------------------------------------------------------------------
851
852class StopInfoException : public StopInfo
853{
854public:
855
856 StopInfoException (Thread &thread, const char *description) :
857 StopInfo (thread, LLDB_INVALID_UID)
858 {
859 if (description)
860 SetDescription (description);
861 }
862
863 virtual
864 ~StopInfoException ()
865 {
866 }
867
868 virtual StopReason
869 GetStopReason () const
870 {
871 return eStopReasonException;
872 }
873
874 virtual const char *
875 GetDescription ()
876 {
877 if (m_description.empty())
878 return "exception";
879 else
880 return m_description.c_str();
Greg Claytonf4b47e12010-08-04 01:40:35 +0000881 }
882};
883
884
885//----------------------------------------------------------------------
886// StopInfoThreadPlan
887//----------------------------------------------------------------------
888
889class StopInfoThreadPlan : public StopInfo
890{
891public:
892
Jim Ingham73ca05a2011-12-17 01:35:57 +0000893 StopInfoThreadPlan (ThreadPlanSP &plan_sp, ValueObjectSP &return_valobj_sp) :
Greg Claytonf4b47e12010-08-04 01:40:35 +0000894 StopInfo (plan_sp->GetThread(), LLDB_INVALID_UID),
Jim Ingham73ca05a2011-12-17 01:35:57 +0000895 m_plan_sp (plan_sp),
896 m_return_valobj_sp (return_valobj_sp)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000897 {
898 }
899
900 virtual ~StopInfoThreadPlan ()
901 {
902 }
903
904 virtual StopReason
905 GetStopReason () const
906 {
907 return eStopReasonPlanComplete;
908 }
909
910 virtual const char *
911 GetDescription ()
912 {
913 if (m_description.empty())
914 {
915 StreamString strm;
916 m_plan_sp->GetDescription (&strm, eDescriptionLevelBrief);
917 m_description.swap (strm.GetString());
918 }
919 return m_description.c_str();
920 }
Jim Ingham73ca05a2011-12-17 01:35:57 +0000921
922 ValueObjectSP
923 GetReturnValueObject()
924 {
925 return m_return_valobj_sp;
926 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000927
928private:
929 ThreadPlanSP m_plan_sp;
Jim Ingham73ca05a2011-12-17 01:35:57 +0000930 ValueObjectSP m_return_valobj_sp;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000931};
Greg Clayton90ba8112012-12-05 00:16:59 +0000932
933class StopInfoExec : public StopInfo
934{
935public:
936
937 StopInfoExec (Thread &thread) :
938 StopInfo (thread, LLDB_INVALID_UID),
939 m_performed_action (false)
940 {
941 }
942
943 virtual
944 ~StopInfoExec ()
945 {
946 }
947
948 virtual StopReason
949 GetStopReason () const
950 {
951 return eStopReasonExec;
952 }
953
954 virtual const char *
955 GetDescription ()
956 {
957 return "exec";
958 }
959protected:
960protected:
961
962 virtual void
963 PerformAction (Event *event_ptr)
964 {
965 // Only perform the action once
966 if (m_performed_action)
967 return;
968 m_performed_action = true;
969 m_thread.GetProcess()->DidExec();
970 }
971
972 bool m_performed_action;
973};
974
Jim Ingham4b536182011-08-09 02:12:22 +0000975} // namespace lldb_private
Greg Claytonf4b47e12010-08-04 01:40:35 +0000976
977StopInfoSP
978StopInfo::CreateStopReasonWithBreakpointSiteID (Thread &thread, break_id_t break_id)
979{
980 return StopInfoSP (new StopInfoBreakpoint (thread, break_id));
981}
982
983StopInfoSP
Jim Ingham36f3b362010-10-14 23:45:03 +0000984StopInfo::CreateStopReasonWithBreakpointSiteID (Thread &thread, break_id_t break_id, bool should_stop)
985{
986 return StopInfoSP (new StopInfoBreakpoint (thread, break_id, should_stop));
987}
988
989StopInfoSP
Greg Claytonf4b47e12010-08-04 01:40:35 +0000990StopInfo::CreateStopReasonWithWatchpointID (Thread &thread, break_id_t watch_id)
991{
992 return StopInfoSP (new StopInfoWatchpoint (thread, watch_id));
993}
994
995StopInfoSP
996StopInfo::CreateStopReasonWithSignal (Thread &thread, int signo)
997{
998 return StopInfoSP (new StopInfoUnixSignal (thread, signo));
999}
1000
1001StopInfoSP
1002StopInfo::CreateStopReasonToTrace (Thread &thread)
1003{
1004 return StopInfoSP (new StopInfoTrace (thread));
1005}
1006
1007StopInfoSP
Jim Ingham73ca05a2011-12-17 01:35:57 +00001008StopInfo::CreateStopReasonWithPlan (ThreadPlanSP &plan_sp, ValueObjectSP return_valobj_sp)
Greg Claytonf4b47e12010-08-04 01:40:35 +00001009{
Jim Ingham73ca05a2011-12-17 01:35:57 +00001010 return StopInfoSP (new StopInfoThreadPlan (plan_sp, return_valobj_sp));
Greg Claytonf4b47e12010-08-04 01:40:35 +00001011}
Greg Claytona658fd22011-06-04 01:26:29 +00001012
1013StopInfoSP
1014StopInfo::CreateStopReasonWithException (Thread &thread, const char *description)
1015{
1016 return StopInfoSP (new StopInfoException (thread, description));
1017}
Jim Ingham73ca05a2011-12-17 01:35:57 +00001018
Greg Clayton90ba8112012-12-05 00:16:59 +00001019StopInfoSP
1020StopInfo::CreateStopReasonWithExec (Thread &thread)
1021{
1022 return StopInfoSP (new StopInfoExec (thread));
1023}
1024
Jim Ingham73ca05a2011-12-17 01:35:57 +00001025ValueObjectSP
1026StopInfo::GetReturnValueObject(StopInfoSP &stop_info_sp)
1027{
1028 if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonPlanComplete)
1029 {
1030 StopInfoThreadPlan *plan_stop_info = static_cast<StopInfoThreadPlan *>(stop_info_sp.get());
1031 return plan_stop_info->GetReturnValueObject();
1032 }
1033 else
1034 return ValueObjectSP();
1035}