blob: 8978f73a0fee4abc72b02dc9ff84c847ef573938 [file] [log] [blame]
Greg Clayton643ee732010-08-04 01:40:35 +00001//===-- StopInfo.cpp ---------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Target/StopInfo.h"
11
12// C Includes
13// C++ Includes
14#include <string>
15
16// Other libraries and framework includes
17// Project includes
18#include "lldb/Core/Log.h"
Greg Clayton640dc6b2010-11-18 18:52:36 +000019#include "lldb/Breakpoint/Breakpoint.h"
Greg Clayton643ee732010-08-04 01:40:35 +000020#include "lldb/Breakpoint/BreakpointLocation.h"
21#include "lldb/Breakpoint/StoppointCallbackContext.h"
Jim Ingham21f37ad2011-08-09 02:12:22 +000022#include "lldb/Core/Debugger.h"
Greg Clayton643ee732010-08-04 01:40:35 +000023#include "lldb/Core/StreamString.h"
Jim Ingham21f37ad2011-08-09 02:12:22 +000024#include "lldb/Expression/ClangUserExpression.h"
25#include "lldb/Target/Target.h"
Greg Clayton643ee732010-08-04 01:40:35 +000026#include "lldb/Target/Thread.h"
27#include "lldb/Target/ThreadPlan.h"
28#include "lldb/Target/Process.h"
29#include "lldb/Target/UnixSignals.h"
30
31using namespace lldb;
32using namespace lldb_private;
33
34StopInfo::StopInfo (Thread &thread, uint64_t value) :
35 m_thread (thread),
36 m_stop_id (thread.GetProcess().GetStopID()),
37 m_value (value)
38{
39}
40
41bool
42StopInfo::IsValid () const
43{
44 return m_thread.GetProcess().GetStopID() == m_stop_id;
45}
46
Jim Ingham15dcb7c2011-01-20 02:03:18 +000047void
48StopInfo::MakeStopInfoValid ()
49{
50 m_stop_id = m_thread.GetProcess().GetStopID();
51}
52
Jim Ingham21f37ad2011-08-09 02:12:22 +000053lldb::StateType
54StopInfo::GetPrivateState ()
55{
56 return m_thread.GetProcess().GetPrivateState();
57}
58
Greg Clayton643ee732010-08-04 01:40:35 +000059//----------------------------------------------------------------------
60// StopInfoBreakpoint
61//----------------------------------------------------------------------
62
Jim Ingham21f37ad2011-08-09 02:12:22 +000063namespace lldb_private
64{
Greg Clayton643ee732010-08-04 01:40:35 +000065class StopInfoBreakpoint : public StopInfo
66{
67public:
68
69 StopInfoBreakpoint (Thread &thread, break_id_t break_id) :
70 StopInfo (thread, break_id),
71 m_description(),
72 m_should_stop (false),
Jim Inghamf9f40c22011-02-08 05:20:59 +000073 m_should_stop_is_valid (false),
74 m_should_perform_action (true)
Greg Clayton643ee732010-08-04 01:40:35 +000075 {
76 }
77
Jim Inghamd1686902010-10-14 23:45:03 +000078 StopInfoBreakpoint (Thread &thread, break_id_t break_id, bool should_stop) :
79 StopInfo (thread, break_id),
80 m_description(),
81 m_should_stop (should_stop),
Jim Inghamf9f40c22011-02-08 05:20:59 +000082 m_should_stop_is_valid (true),
83 m_should_perform_action (true)
Jim Inghamd1686902010-10-14 23:45:03 +000084 {
85 }
86
Greg Clayton643ee732010-08-04 01:40:35 +000087 virtual ~StopInfoBreakpoint ()
88 {
89 }
90
91 virtual StopReason
92 GetStopReason () const
93 {
94 return eStopReasonBreakpoint;
95 }
96
97 virtual bool
98 ShouldStop (Event *event_ptr)
99 {
100 if (!m_should_stop_is_valid)
101 {
102 // Only check once if we should stop at a breakpoint
103 BreakpointSiteSP bp_site_sp (m_thread.GetProcess().GetBreakpointSiteList().FindByID (m_value));
104 if (bp_site_sp)
105 {
106 StoppointCallbackContext context (event_ptr,
107 &m_thread.GetProcess(),
108 &m_thread,
109 m_thread.GetStackFrameAtIndex(0).get(),
Jim Ingham7508e732010-08-09 23:31:02 +0000110 true);
Greg Clayton643ee732010-08-04 01:40:35 +0000111
112 m_should_stop = bp_site_sp->ShouldStop (&context);
113 }
114 else
115 {
Greg Claytone005f2c2010-11-06 01:53:30 +0000116 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Clayton643ee732010-08-04 01:40:35 +0000117
118 if (log)
119 log->Printf ("Process::%s could not find breakpoint site id: %lld...", __FUNCTION__, m_value);
120
121 m_should_stop = true;
122 }
123 m_should_stop_is_valid = true;
124 }
125 return m_should_stop;
126 }
Jim Ingham6fb8baa2010-08-10 00:59:59 +0000127
128 virtual void
129 PerformAction (Event *event_ptr)
130 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000131 if (!m_should_perform_action)
132 return;
133 m_should_perform_action = false;
134
Jim Ingham21f37ad2011-08-09 02:12:22 +0000135 LogSP log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
136 // We're going to calculate whether we should stop or not in some way during the course of
137 // this code. So set the valid flag here. Also by default we're going to stop, so
138 // set that here too.
139 // m_should_stop_is_valid = true;
140 m_should_stop = true;
141
Jim Ingham6fb8baa2010-08-10 00:59:59 +0000142 BreakpointSiteSP bp_site_sp (m_thread.GetProcess().GetBreakpointSiteList().FindByID (m_value));
143 if (bp_site_sp)
144 {
145 size_t num_owners = bp_site_sp->GetNumberOfOwners();
Jim Ingham21f37ad2011-08-09 02:12:22 +0000146
147 // We only continue from the callbacks if ALL the callbacks want us to continue.
148 // However we want to run all the callbacks, except of course if one of them actually
149 // resumes the target.
150 // So we use stop_requested to track what we're were asked to do.
151 bool stop_requested = true;
Jim Ingham6fb8baa2010-08-10 00:59:59 +0000152 for (size_t j = 0; j < num_owners; j++)
153 {
Jim Ingham6fb8baa2010-08-10 00:59:59 +0000154 lldb::BreakpointLocationSP bp_loc_sp = bp_site_sp->GetOwnerAtIndex(j);
155 StoppointCallbackContext context (event_ptr,
156 &m_thread.GetProcess(),
157 &m_thread,
158 m_thread.GetStackFrameAtIndex(0).get(),
159 false);
Jim Ingham21f37ad2011-08-09 02:12:22 +0000160 stop_requested = bp_loc_sp->InvokeCallback (&context);
161 // Also make sure that the callback hasn't continued the target.
162 // If it did, when we'll set m_should_start to false and get out of here.
163 if (GetPrivateState() == eStateRunning)
164 m_should_stop = false;
165 }
166
167 if (m_should_stop && !stop_requested)
168 {
169 m_should_stop_is_valid = true;
170 m_should_stop = false;
171 }
172
173 // Okay, so now if all the callbacks say we should stop, let's try the Conditions:
174 if (m_should_stop)
175 {
176 size_t num_owners = bp_site_sp->GetNumberOfOwners();
177 for (size_t j = 0; j < num_owners; j++)
178 {
179 lldb::BreakpointLocationSP bp_loc_sp = bp_site_sp->GetOwnerAtIndex(j);
180 if (bp_loc_sp->GetConditionText() != NULL)
181 {
182 // We need to make sure the user sees any parse errors in their condition, so we'll hook the
183 // constructor errors up to the debugger's Async I/O.
184
185 StoppointCallbackContext context (event_ptr,
186 &m_thread.GetProcess(),
187 &m_thread,
188 m_thread.GetStackFrameAtIndex(0).get(),
189 false);
190 ValueObjectSP result_valobj_sp;
191
192 ExecutionResults result_code;
193 ValueObjectSP result_value_sp;
194 const bool discard_on_error = true;
195 Error error;
196 result_code = ClangUserExpression::EvaluateWithError (context.exe_ctx,
Sean Callanan47dc4572011-09-15 02:13:07 +0000197 eExecutionPolicyAlways,
198 discard_on_error,
199 bp_loc_sp->GetConditionText(),
200 NULL,
201 result_value_sp,
202 error);
Jim Ingham21f37ad2011-08-09 02:12:22 +0000203 if (result_code == eExecutionCompleted)
204 {
205 if (result_value_sp)
206 {
207 Scalar scalar_value;
208 if (result_value_sp->ResolveValue (scalar_value))
209 {
210 if (scalar_value.ULongLong(1) == 0)
211 m_should_stop = false;
212 else
213 m_should_stop = true;
214 if (log)
215 log->Printf("Condition successfully evaluated, result is %s.\n",
216 m_should_stop ? "true" : "false");
217 }
218 else
219 {
220 m_should_stop = true;
221 if (log)
222 log->Printf("Failed to get an integer result from the expression.");
223 }
224 }
225 }
226 else
227 {
228 Debugger &debugger = context.exe_ctx.target->GetDebugger();
229 StreamSP error_sp = debugger.GetAsyncErrorStream ();
230 error_sp->Printf ("Stopped due to an error evaluating condition of breakpoint ");
231 bp_loc_sp->GetDescription (error_sp.get(), eDescriptionLevelBrief);
232 error_sp->Printf (": \"%s\"",
233 bp_loc_sp->GetConditionText());
234 error_sp->EOL();
235 const char *err_str = error.AsCString("<Unknown Error>");
236 if (log)
237 log->Printf("Error evaluating condition: \"%s\"\n", err_str);
238
239 error_sp->PutCString (err_str);
240 error_sp->EOL();
241 error_sp->Flush();
242 // If the condition fails to be parsed or run, we should stop.
243 m_should_stop = true;
244 }
245 }
246
247 // If any condition says we should stop, then we're going to stop, so we don't need
248 // to evaluate the others.
249 if (m_should_stop)
250 break;
251 }
Jim Ingham6fb8baa2010-08-10 00:59:59 +0000252 }
253 }
254 else
255 {
Greg Claytone005f2c2010-11-06 01:53:30 +0000256 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Jim Ingham6fb8baa2010-08-10 00:59:59 +0000257
258 if (log)
259 log->Printf ("Process::%s could not find breakpoint site id: %lld...", __FUNCTION__, m_value);
260 }
Jim Ingham21f37ad2011-08-09 02:12:22 +0000261 if (log)
262 log->Printf ("Process::%s returning from action with m_should_stop: %d.", __FUNCTION__, m_should_stop);
Jim Ingham6fb8baa2010-08-10 00:59:59 +0000263 }
Greg Clayton643ee732010-08-04 01:40:35 +0000264
265 virtual bool
266 ShouldNotify (Event *event_ptr)
267 {
268 BreakpointSiteSP bp_site_sp (m_thread.GetProcess().GetBreakpointSiteList().FindByID (m_value));
269 if (bp_site_sp)
270 {
271 bool all_internal = true;
272
273 for (uint32_t i = 0; i < bp_site_sp->GetNumberOfOwners(); i++)
274 {
275 if (!bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint().IsInternal())
276 {
277 all_internal = false;
278 break;
279 }
280 }
281 return all_internal == false;
282 }
283 return true;
284 }
285
286 virtual const char *
287 GetDescription ()
288 {
289 if (m_description.empty())
290 {
Jim Ingham6fb8baa2010-08-10 00:59:59 +0000291 BreakpointSiteSP bp_site_sp (m_thread.GetProcess().GetBreakpointSiteList().FindByID (m_value));
292 if (bp_site_sp)
293 {
294 StreamString strm;
295 strm.Printf("breakpoint ");
296 bp_site_sp->GetDescription(&strm, eDescriptionLevelBrief);
297 m_description.swap (strm.GetString());
298 }
299 else
300 {
301 StreamString strm;
302 strm.Printf("breakpoint site %lli", m_value);
303 m_description.swap (strm.GetString());
304 }
Greg Clayton643ee732010-08-04 01:40:35 +0000305 }
306 return m_description.c_str();
307 }
308
309private:
310 std::string m_description;
311 bool m_should_stop;
312 bool m_should_stop_is_valid;
Jim Inghamf9f40c22011-02-08 05:20:59 +0000313 bool m_should_perform_action; // Since we are trying to preserve the "state" of the system even if we run functions
314 // etc. behind the users backs, we need to make sure we only REALLY perform the action once.
Greg Clayton643ee732010-08-04 01:40:35 +0000315};
316
317
318//----------------------------------------------------------------------
319// StopInfoWatchpoint
320//----------------------------------------------------------------------
321
322class StopInfoWatchpoint : public StopInfo
323{
324public:
325
326 StopInfoWatchpoint (Thread &thread, break_id_t watch_id) :
Johnny Chen043f8c22011-09-21 22:47:15 +0000327 StopInfo(thread, watch_id),
328 m_description(),
329 m_should_stop(false),
330 m_should_stop_is_valid(false)
Greg Clayton643ee732010-08-04 01:40:35 +0000331 {
332 }
333
334 virtual ~StopInfoWatchpoint ()
335 {
336 }
337
338 virtual StopReason
339 GetStopReason () const
340 {
341 return eStopReasonWatchpoint;
342 }
343
Johnny Chen043f8c22011-09-21 22:47:15 +0000344 virtual bool
345 ShouldStop (Event *event_ptr)
346 {
347 // ShouldStop() method is idempotent and should not affect hit count.
348 if (m_should_stop_is_valid)
349 return m_should_stop;
350
351 WatchpointLocationSP wp_loc_sp =
352 m_thread.GetProcess().GetTarget().GetWatchpointLocationList().FindByID(GetValue());
353 if (wp_loc_sp)
354 {
355 // Check if we should stop at a watchpoint.
356 StoppointCallbackContext context (event_ptr,
357 &m_thread.GetProcess(),
358 &m_thread,
359 m_thread.GetStackFrameAtIndex(0).get(),
360 true);
361
362 m_should_stop = wp_loc_sp->ShouldStop (&context);
363 }
364 else
365 {
366 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
367
368 if (log)
369 log->Printf ("Process::%s could not find watchpoint location id: %lld...",
370 __FUNCTION__, GetValue());
371
372 m_should_stop = true;
373 }
374 m_should_stop_is_valid = true;
375 return m_should_stop;
376 }
377
Greg Clayton643ee732010-08-04 01:40:35 +0000378 virtual const char *
379 GetDescription ()
380 {
381 if (m_description.empty())
382 {
383 StreamString strm;
384 strm.Printf("watchpoint %lli", m_value);
385 m_description.swap (strm.GetString());
386 }
387 return m_description.c_str();
388 }
389
Greg Clayton643ee732010-08-04 01:40:35 +0000390private:
391 std::string m_description;
Johnny Chen043f8c22011-09-21 22:47:15 +0000392 bool m_should_stop;
393 bool m_should_stop_is_valid;
Greg Clayton643ee732010-08-04 01:40:35 +0000394};
395
396
397
398//----------------------------------------------------------------------
399// StopInfoUnixSignal
400//----------------------------------------------------------------------
401
402class StopInfoUnixSignal : public StopInfo
403{
404public:
405
406 StopInfoUnixSignal (Thread &thread, int signo) :
Greg Clayton65611552011-06-04 01:26:29 +0000407 StopInfo (thread, signo)
Greg Clayton643ee732010-08-04 01:40:35 +0000408 {
409 }
410
411 virtual ~StopInfoUnixSignal ()
412 {
413 }
414
415
416 virtual StopReason
417 GetStopReason () const
418 {
419 return eStopReasonSignal;
420 }
421
422 virtual bool
423 ShouldStop (Event *event_ptr)
424 {
425 return m_thread.GetProcess().GetUnixSignals().GetShouldStop (m_value);
426 }
427
428
429 // If should stop returns false, check if we should notify of this event
430 virtual bool
431 ShouldNotify (Event *event_ptr)
432 {
433 return m_thread.GetProcess().GetUnixSignals().GetShouldNotify (m_value);
434 }
435
436
437 virtual void
438 WillResume (lldb::StateType resume_state)
439 {
440 if (m_thread.GetProcess().GetUnixSignals().GetShouldSuppress(m_value) == false)
441 m_thread.SetResumeSignal(m_value);
442 }
443
444 virtual const char *
445 GetDescription ()
446 {
447 if (m_description.empty())
448 {
449 StreamString strm;
450 const char *signal_name = m_thread.GetProcess().GetUnixSignals().GetSignalAsCString (m_value);
451 if (signal_name)
Greg Claytona830adb2010-10-04 01:05:56 +0000452 strm.Printf("signal %s", signal_name);
Greg Clayton643ee732010-08-04 01:40:35 +0000453 else
Greg Claytona830adb2010-10-04 01:05:56 +0000454 strm.Printf("signal %lli", m_value);
Greg Clayton643ee732010-08-04 01:40:35 +0000455 m_description.swap (strm.GetString());
456 }
457 return m_description.c_str();
458 }
Greg Clayton643ee732010-08-04 01:40:35 +0000459};
460
461//----------------------------------------------------------------------
462// StopInfoTrace
463//----------------------------------------------------------------------
464
465class StopInfoTrace : public StopInfo
466{
467public:
468
469 StopInfoTrace (Thread &thread) :
470 StopInfo (thread, LLDB_INVALID_UID)
471 {
472 }
473
474 virtual ~StopInfoTrace ()
475 {
476 }
477
478 virtual StopReason
479 GetStopReason () const
480 {
481 return eStopReasonTrace;
482 }
483
484 virtual const char *
485 GetDescription ()
486 {
Greg Clayton65611552011-06-04 01:26:29 +0000487 if (m_description.empty())
Greg Clayton643ee732010-08-04 01:40:35 +0000488 return "trace";
Greg Clayton65611552011-06-04 01:26:29 +0000489 else
490 return m_description.c_str();
491 }
492};
493
494
495//----------------------------------------------------------------------
496// StopInfoException
497//----------------------------------------------------------------------
498
499class StopInfoException : public StopInfo
500{
501public:
502
503 StopInfoException (Thread &thread, const char *description) :
504 StopInfo (thread, LLDB_INVALID_UID)
505 {
506 if (description)
507 SetDescription (description);
508 }
509
510 virtual
511 ~StopInfoException ()
512 {
513 }
514
515 virtual StopReason
516 GetStopReason () const
517 {
518 return eStopReasonException;
519 }
520
521 virtual const char *
522 GetDescription ()
523 {
524 if (m_description.empty())
525 return "exception";
526 else
527 return m_description.c_str();
Greg Clayton643ee732010-08-04 01:40:35 +0000528 }
529};
530
531
532//----------------------------------------------------------------------
533// StopInfoThreadPlan
534//----------------------------------------------------------------------
535
536class StopInfoThreadPlan : public StopInfo
537{
538public:
539
540 StopInfoThreadPlan (ThreadPlanSP &plan_sp) :
541 StopInfo (plan_sp->GetThread(), LLDB_INVALID_UID),
542 m_plan_sp (plan_sp)
543 {
544 }
545
546 virtual ~StopInfoThreadPlan ()
547 {
548 }
549
550 virtual StopReason
551 GetStopReason () const
552 {
553 return eStopReasonPlanComplete;
554 }
555
556 virtual const char *
557 GetDescription ()
558 {
559 if (m_description.empty())
560 {
561 StreamString strm;
562 m_plan_sp->GetDescription (&strm, eDescriptionLevelBrief);
563 m_description.swap (strm.GetString());
564 }
565 return m_description.c_str();
566 }
567
568private:
569 ThreadPlanSP m_plan_sp;
Greg Clayton643ee732010-08-04 01:40:35 +0000570};
Jim Ingham21f37ad2011-08-09 02:12:22 +0000571} // namespace lldb_private
Greg Clayton643ee732010-08-04 01:40:35 +0000572
573StopInfoSP
574StopInfo::CreateStopReasonWithBreakpointSiteID (Thread &thread, break_id_t break_id)
575{
576 return StopInfoSP (new StopInfoBreakpoint (thread, break_id));
577}
578
579StopInfoSP
Jim Inghamd1686902010-10-14 23:45:03 +0000580StopInfo::CreateStopReasonWithBreakpointSiteID (Thread &thread, break_id_t break_id, bool should_stop)
581{
582 return StopInfoSP (new StopInfoBreakpoint (thread, break_id, should_stop));
583}
584
585StopInfoSP
Greg Clayton643ee732010-08-04 01:40:35 +0000586StopInfo::CreateStopReasonWithWatchpointID (Thread &thread, break_id_t watch_id)
587{
588 return StopInfoSP (new StopInfoWatchpoint (thread, watch_id));
589}
590
591StopInfoSP
592StopInfo::CreateStopReasonWithSignal (Thread &thread, int signo)
593{
594 return StopInfoSP (new StopInfoUnixSignal (thread, signo));
595}
596
597StopInfoSP
598StopInfo::CreateStopReasonToTrace (Thread &thread)
599{
600 return StopInfoSP (new StopInfoTrace (thread));
601}
602
603StopInfoSP
604StopInfo::CreateStopReasonWithPlan (ThreadPlanSP &plan_sp)
605{
606 return StopInfoSP (new StopInfoThreadPlan (plan_sp));
607}
Greg Clayton65611552011-06-04 01:26:29 +0000608
609StopInfoSP
610StopInfo::CreateStopReasonWithException (Thread &thread, const char *description)
611{
612 return StopInfoSP (new StopInfoException (thread, description));
613}