blob: 1f221d687e60c080ebc7564fd35df59366b573fc [file] [log] [blame]
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001//===-- StopInfo.cpp --------------------------------------------*- C++ -*-===//
Greg Claytonf4b47e12010-08-04 01:40:35 +00002//
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
Greg Claytonf4b47e12010-08-04 01:40:35 +000010// C Includes
11// C++ Includes
12#include <string>
13
14// Other libraries and framework includes
15// Project includes
Greg Clayton4e78f602010-11-18 18:52:36 +000016#include "lldb/Breakpoint/Breakpoint.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000017#include "lldb/Breakpoint/BreakpointLocation.h"
18#include "lldb/Breakpoint/StoppointCallbackContext.h"
Johnny Chen01a67862011-10-14 00:42:25 +000019#include "lldb/Breakpoint/Watchpoint.h"
Jim Ingham4b536182011-08-09 02:12:22 +000020#include "lldb/Core/Debugger.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000021#include "lldb/Core/Log.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000022#include "lldb/Core/StreamString.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000023#include "lldb/Core/ValueObject.h"
Jim Ingham151c0322015-09-15 21:13:50 +000024#include "lldb/Expression/UserExpression.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000025#include "lldb/Target/Process.h"
26#include "lldb/Target/StopInfo.h"
Jim Ingham4b536182011-08-09 02:12:22 +000027#include "lldb/Target/Target.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000028#include "lldb/Target/Thread.h"
29#include "lldb/Target/ThreadPlan.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000030#include "lldb/Target/UnixSignals.h"
31
32using namespace lldb;
33using namespace lldb_private;
34
Kate Stoneb9c1b512016-09-06 20:57:50 +000035StopInfo::StopInfo(Thread &thread, uint64_t value)
36 : m_thread_wp(thread.shared_from_this()),
37 m_stop_id(thread.GetProcess()->GetStopID()),
38 m_resume_id(thread.GetProcess()->GetResumeID()), m_value(value),
39 m_description(), m_override_should_notify(eLazyBoolCalculate),
40 m_override_should_stop(eLazyBoolCalculate), m_extended_info() {}
41
42bool StopInfo::IsValid() const {
43 ThreadSP thread_sp(m_thread_wp.lock());
44 if (thread_sp)
45 return thread_sp->GetProcess()->GetStopID() == m_stop_id;
46 return false;
Greg Claytonf4b47e12010-08-04 01:40:35 +000047}
48
Kate Stoneb9c1b512016-09-06 20:57:50 +000049void StopInfo::MakeStopInfoValid() {
50 ThreadSP thread_sp(m_thread_wp.lock());
51 if (thread_sp) {
52 m_stop_id = thread_sp->GetProcess()->GetStopID();
53 m_resume_id = thread_sp->GetProcess()->GetResumeID();
54 }
Greg Claytonf4b47e12010-08-04 01:40:35 +000055}
56
Kate Stoneb9c1b512016-09-06 20:57:50 +000057bool StopInfo::HasTargetRunSinceMe() {
58 ThreadSP thread_sp(m_thread_wp.lock());
59
60 if (thread_sp) {
61 lldb::StateType ret_type = thread_sp->GetProcess()->GetPrivateState();
62 if (ret_type == eStateRunning) {
63 return true;
64 } else if (ret_type == eStateStopped) {
65 // This is a little tricky. We want to count "run and stopped again
66 // before you could
67 // ask this question as a "TRUE" answer to HasTargetRunSinceMe. But we
68 // don't want to
69 // include any running of the target done for expressions. So we track
70 // both resumes,
71 // and resumes caused by expressions, and check if there are any resumes
72 // NOT caused
73 // by expressions.
74
75 uint32_t curr_resume_id = thread_sp->GetProcess()->GetResumeID();
76 uint32_t last_user_expression_id =
77 thread_sp->GetProcess()->GetLastUserExpressionResumeID();
78 if (curr_resume_id == m_resume_id) {
79 return false;
80 } else if (curr_resume_id > last_user_expression_id) {
81 return true;
82 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +000083 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 }
85 return false;
Jim Ingham4b536182011-08-09 02:12:22 +000086}
87
Greg Claytonf4b47e12010-08-04 01:40:35 +000088//----------------------------------------------------------------------
89// StopInfoBreakpoint
90//----------------------------------------------------------------------
91
Kate Stoneb9c1b512016-09-06 20:57:50 +000092namespace lldb_private {
93class StopInfoBreakpoint : public StopInfo {
Greg Claytonf4b47e12010-08-04 01:40:35 +000094public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000095 StopInfoBreakpoint(Thread &thread, break_id_t break_id)
96 : StopInfo(thread, break_id), m_should_stop(false),
97 m_should_stop_is_valid(false), m_should_perform_action(true),
98 m_address(LLDB_INVALID_ADDRESS), m_break_id(LLDB_INVALID_BREAK_ID),
99 m_was_one_shot(false) {
100 StoreBPInfo();
101 }
102
103 StopInfoBreakpoint(Thread &thread, break_id_t break_id, bool should_stop)
104 : StopInfo(thread, break_id), m_should_stop(should_stop),
105 m_should_stop_is_valid(true), m_should_perform_action(true),
106 m_address(LLDB_INVALID_ADDRESS), m_break_id(LLDB_INVALID_BREAK_ID),
107 m_was_one_shot(false) {
108 StoreBPInfo();
109 }
110
111 ~StopInfoBreakpoint() override = default;
112
113 void StoreBPInfo() {
114 ThreadSP thread_sp(m_thread_wp.lock());
115 if (thread_sp) {
116 BreakpointSiteSP bp_site_sp(
117 thread_sp->GetProcess()->GetBreakpointSiteList().FindByID(m_value));
118 if (bp_site_sp) {
119 if (bp_site_sp->GetNumberOfOwners() == 1) {
120 BreakpointLocationSP bp_loc_sp = bp_site_sp->GetOwnerAtIndex(0);
121 if (bp_loc_sp) {
122 m_break_id = bp_loc_sp->GetBreakpoint().GetID();
123 m_was_one_shot = bp_loc_sp->GetBreakpoint().IsOneShot();
124 }
125 }
126 m_address = bp_site_sp->GetLoadAddress();
127 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000128 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000130
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131 bool IsValidForOperatingSystemThread(Thread &thread) override {
132 ProcessSP process_sp(thread.GetProcess());
133 if (process_sp) {
134 BreakpointSiteSP bp_site_sp(
135 process_sp->GetBreakpointSiteList().FindByID(m_value));
136 if (bp_site_sp)
137 return bp_site_sp->ValidForThisThread(&thread);
Jim Inghamca36cd12012-10-05 19:16:31 +0000138 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139 return false;
140 }
Jim Inghamca36cd12012-10-05 19:16:31 +0000141
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142 StopReason GetStopReason() const override { return eStopReasonBreakpoint; }
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000143
Kate Stoneb9c1b512016-09-06 20:57:50 +0000144 bool ShouldStopSynchronous(Event *event_ptr) override {
145 ThreadSP thread_sp(m_thread_wp.lock());
146 if (thread_sp) {
147 if (!m_should_stop_is_valid) {
148 // Only check once if we should stop at a breakpoint
149 BreakpointSiteSP bp_site_sp(
150 thread_sp->GetProcess()->GetBreakpointSiteList().FindByID(m_value));
151 if (bp_site_sp) {
152 ExecutionContext exe_ctx(thread_sp->GetStackFrameAtIndex(0));
153 StoppointCallbackContext context(event_ptr, exe_ctx, true);
154 bp_site_sp->BumpHitCounts();
155 m_should_stop = bp_site_sp->ShouldStop(&context);
156 } else {
157 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
158
159 if (log)
160 log->Printf(
161 "Process::%s could not find breakpoint site id: %" PRId64 "...",
162 __FUNCTION__, m_value);
163
164 m_should_stop = true;
165 }
166 m_should_stop_is_valid = true;
167 }
168 return m_should_stop;
169 }
170 return false;
171 }
172
173 bool DoShouldNotify(Event *event_ptr) override {
174 ThreadSP thread_sp(m_thread_wp.lock());
175 if (thread_sp) {
176 BreakpointSiteSP bp_site_sp(
177 thread_sp->GetProcess()->GetBreakpointSiteList().FindByID(m_value));
178 if (bp_site_sp) {
179 bool all_internal = true;
180
181 for (uint32_t i = 0; i < bp_site_sp->GetNumberOfOwners(); i++) {
182 if (!bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint().IsInternal()) {
183 all_internal = false;
184 break;
185 }
186 }
187 return !all_internal;
188 }
189 }
190 return true;
191 }
192
193 const char *GetDescription() override {
194 if (m_description.empty()) {
195 ThreadSP thread_sp(m_thread_wp.lock());
196 if (thread_sp) {
197 BreakpointSiteSP bp_site_sp(
198 thread_sp->GetProcess()->GetBreakpointSiteList().FindByID(m_value));
199 if (bp_site_sp) {
200 StreamString strm;
201 // If we have just hit an internal breakpoint, and it has a kind
202 // description, print that instead of the
203 // full breakpoint printing:
204 if (bp_site_sp->IsInternal()) {
205 size_t num_owners = bp_site_sp->GetNumberOfOwners();
206 for (size_t idx = 0; idx < num_owners; idx++) {
207 const char *kind = bp_site_sp->GetOwnerAtIndex(idx)
208 ->GetBreakpoint()
209 .GetBreakpointKind();
210 if (kind != nullptr) {
211 m_description.assign(kind);
212 return kind;
213 }
Jim Inghamca36cd12012-10-05 19:16:31 +0000214 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000215 }
Jim Ingham36f3b362010-10-14 23:45:03 +0000216
Kate Stoneb9c1b512016-09-06 20:57:50 +0000217 strm.Printf("breakpoint ");
218 bp_site_sp->GetDescription(&strm, eDescriptionLevelBrief);
219 m_description.swap(strm.GetString());
220 } else {
221 StreamString strm;
222 if (m_break_id != LLDB_INVALID_BREAK_ID) {
223 BreakpointSP break_sp =
224 thread_sp->GetProcess()->GetTarget().GetBreakpointByID(
225 m_break_id);
226 if (break_sp) {
227 if (break_sp->IsInternal()) {
228 const char *kind = break_sp->GetBreakpointKind();
229 if (kind)
230 strm.Printf("internal %s breakpoint(%d).", kind, m_break_id);
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000231 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000232 strm.Printf("internal breakpoint(%d).", m_break_id);
233 } else {
234 strm.Printf("breakpoint %d.", m_break_id);
235 }
236 } else {
237 if (m_was_one_shot)
238 strm.Printf("one-shot breakpoint %d", m_break_id);
239 else
240 strm.Printf("breakpoint %d which has been deleted.",
241 m_break_id);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000242 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000243 } else if (m_address == LLDB_INVALID_ADDRESS)
244 strm.Printf("breakpoint site %" PRIi64
245 " which has been deleted - unknown address",
246 m_value);
247 else
248 strm.Printf("breakpoint site %" PRIi64
249 " which has been deleted - was at 0x%" PRIx64,
250 m_value, m_address);
251
252 m_description.swap(strm.GetString());
Greg Claytonf4b47e12010-08-04 01:40:35 +0000253 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000255 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000256 return m_description.c_str();
257 }
Jim Inghamf57b4352012-11-10 02:08:14 +0000258
259protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000260 bool ShouldStop(Event *event_ptr) override {
261 // This just reports the work done by PerformAction or the synchronous stop.
262 // It should
263 // only ever get called after they have had a chance to run.
264 assert(m_should_stop_is_valid);
265 return m_should_stop;
266 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000267
Kate Stoneb9c1b512016-09-06 20:57:50 +0000268 void PerformAction(Event *event_ptr) override {
269 if (!m_should_perform_action)
270 return;
271 m_should_perform_action = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000272
Kate Stoneb9c1b512016-09-06 20:57:50 +0000273 ThreadSP thread_sp(m_thread_wp.lock());
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000274
Kate Stoneb9c1b512016-09-06 20:57:50 +0000275 if (thread_sp) {
276 Log *log = lldb_private::GetLogIfAnyCategoriesSet(
277 LIBLLDB_LOG_BREAKPOINTS | LIBLLDB_LOG_STEP);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000278
Kate Stoneb9c1b512016-09-06 20:57:50 +0000279 if (!thread_sp->IsValid()) {
280 // This shouldn't ever happen, but just in case, don't do more harm.
281 if (log) {
282 log->Printf("PerformAction got called with an invalid thread.");
283 }
284 m_should_stop = true;
285 m_should_stop_is_valid = true;
286 return;
287 }
288
289 BreakpointSiteSP bp_site_sp(
290 thread_sp->GetProcess()->GetBreakpointSiteList().FindByID(m_value));
291 std::unordered_set<break_id_t> precondition_breakpoints;
292
293 if (bp_site_sp) {
294 // Let's copy the owners list out of the site and store them in a local
295 // list. That way if
296 // one of the breakpoint actions changes the site, then we won't be
297 // operating on a bad list.
298 BreakpointLocationCollection site_locations;
299 size_t num_owners = bp_site_sp->CopyOwnersList(site_locations);
300
301 if (num_owners == 0) {
302 m_should_stop = true;
303 } else {
304 // We go through each location, and test first its precondition - this
305 // overrides everything. Note,
306 // we only do this once per breakpoint - not once per location...
307 // Then check the condition. If the condition says to stop,
308 // then we run the callback for that location. If that callback says
309 // to stop as well, then
310 // we set m_should_stop to true; we are going to stop.
311 // But we still want to give all the breakpoints whose conditions say
312 // we are going to stop a
313 // chance to run their callbacks.
314 // Of course if any callback restarts the target by putting "continue"
315 // in the callback, then
316 // we're going to restart, without running the rest of the callbacks.
317 // And in this case we will
318 // end up not stopping even if another location said we should stop.
319 // But that's better than not
320 // running all the callbacks.
321
322 m_should_stop = false;
323
324 // We don't select threads as we go through them testing breakpoint
325 // conditions and running commands.
326 // So we need to set the thread for expression evaluation here:
327 ThreadList::ExpressionExecutionThreadPusher thread_pusher(thread_sp);
328
329 ExecutionContext exe_ctx(thread_sp->GetStackFrameAtIndex(0));
330 Process *process = exe_ctx.GetProcessPtr();
331 if (process->GetModIDRef().IsLastResumeForUserExpression()) {
332 // If we are in the middle of evaluating an expression, don't run
333 // asynchronous breakpoint commands or
334 // expressions. That could lead to infinite recursion if the
335 // command or condition re-calls the function
336 // with this breakpoint.
337 // TODO: We can keep a list of the breakpoints we've seen while
338 // running expressions in the nested
339 // PerformAction calls that can arise when the action runs a
340 // function that hits another breakpoint,
341 // and only stop running commands when we see the same breakpoint
342 // hit a second time.
343
344 m_should_stop_is_valid = true;
345 if (log)
346 log->Printf("StopInfoBreakpoint::PerformAction - Hit a "
347 "breakpoint while running an expression,"
348 " not running commands to avoid recursion.");
349 bool ignoring_breakpoints =
350 process->GetIgnoreBreakpointsInExpressions();
351 if (ignoring_breakpoints) {
352 m_should_stop = false;
353 // Internal breakpoints will always stop.
354 for (size_t j = 0; j < num_owners; j++) {
355 lldb::BreakpointLocationSP bp_loc_sp =
356 bp_site_sp->GetOwnerAtIndex(j);
357 if (bp_loc_sp->GetBreakpoint().IsInternal()) {
358 m_should_stop = true;
359 break;
Jason Molenda3f032ff2013-08-06 23:08:59 +0000360 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000361 }
362 } else {
363 m_should_stop = true;
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000364 }
365 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000366 log->Printf("StopInfoBreakpoint::PerformAction - in expression, "
367 "continuing: %s.",
368 m_should_stop ? "true" : "false");
369 process->GetTarget().GetDebugger().GetAsyncOutputStream()->Printf(
370 "Warning: hit breakpoint while "
371 "running function, skipping commands and conditions to prevent "
372 "recursion.");
373 return;
374 }
375
376 StoppointCallbackContext context(event_ptr, exe_ctx, false);
377
378 // For safety's sake let's also grab an extra reference to the
379 // breakpoint owners of the locations we're
380 // going to examine, since the locations are going to have to get back
381 // to their breakpoints, and the
382 // locations don't keep their owners alive. I'm just sticking the
383 // BreakpointSP's in a vector since
384 // I'm only using it to locally increment their retain counts.
385
386 std::vector<lldb::BreakpointSP> location_owners;
387
388 for (size_t j = 0; j < num_owners; j++) {
389 BreakpointLocationSP loc(site_locations.GetByIndex(j));
390 location_owners.push_back(loc->GetBreakpoint().shared_from_this());
391 }
392
393 for (size_t j = 0; j < num_owners; j++) {
394 lldb::BreakpointLocationSP bp_loc_sp = site_locations.GetByIndex(j);
395
396 // If another action disabled this breakpoint or its location, then
397 // don't run the actions.
398 if (!bp_loc_sp->IsEnabled() ||
399 !bp_loc_sp->GetBreakpoint().IsEnabled())
400 continue;
401
402 // The breakpoint site may have many locations associated with it,
403 // not all of them valid for
404 // this thread. Skip the ones that aren't:
405 if (!bp_loc_sp->ValidForThisThread(thread_sp.get())) {
406 if (log) {
407 StreamString s;
408 bp_loc_sp->GetDescription(&s, eDescriptionLevelBrief);
409 log->Printf("Breakpoint %s hit on thread 0x%llx but it was not "
410 "for this thread, continuing.",
411 s.GetData(), static_cast<unsigned long long>(
412 thread_sp->GetID()));
413 }
414 continue;
415 }
416
417 // First run the precondition, but since the precondition is per
418 // breakpoint, only run it once
419 // per breakpoint.
420 std::pair<std::unordered_set<break_id_t>::iterator, bool> result =
421 precondition_breakpoints.insert(
422 bp_loc_sp->GetBreakpoint().GetID());
423 if (!result.second)
424 continue;
425
426 bool precondition_result =
427 bp_loc_sp->GetBreakpoint().EvaluatePrecondition(context);
428 if (!precondition_result)
429 continue;
430
431 // Next run the condition for the breakpoint. If that says we
432 // should stop, then we'll run
433 // the callback for the breakpoint. If the callback says we
434 // shouldn't stop that will win.
435
436 if (bp_loc_sp->GetConditionText() != nullptr) {
437 Error condition_error;
438 bool condition_says_stop =
439 bp_loc_sp->ConditionSaysStop(exe_ctx, condition_error);
440
441 if (!condition_error.Success()) {
442 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
443 StreamSP error_sp = debugger.GetAsyncErrorStream();
444 error_sp->Printf("Stopped due to an error evaluating condition "
445 "of breakpoint ");
446 bp_loc_sp->GetDescription(error_sp.get(),
447 eDescriptionLevelBrief);
448 error_sp->Printf(": \"%s\"", bp_loc_sp->GetConditionText());
449 error_sp->EOL();
450 const char *err_str =
451 condition_error.AsCString("<Unknown Error>");
452 if (log)
453 log->Printf("Error evaluating condition: \"%s\"\n", err_str);
454
455 error_sp->PutCString(err_str);
456 error_sp->EOL();
457 error_sp->Flush();
458 } else {
459 if (log) {
460 StreamString s;
461 bp_loc_sp->GetDescription(&s, eDescriptionLevelBrief);
462 log->Printf("Condition evaluated for breakpoint %s on thread "
463 "0x%llx conditon_says_stop: %i.",
464 s.GetData(), static_cast<unsigned long long>(
465 thread_sp->GetID()),
466 condition_says_stop);
467 }
468 if (!condition_says_stop) {
469 // We don't want to increment the hit count of breakpoints if
470 // the condition fails.
471 // We've already bumped it by the time we get here, so undo
472 // the bump:
473 bp_loc_sp->UndoBumpHitCount();
474 continue;
475 }
476 }
477 }
478
479 bool callback_says_stop;
480
481 // FIXME: For now the callbacks have to run in async mode - the
482 // first time we restart we need
483 // to get out of there. So set it here.
484 // When we figure out how to nest breakpoint hits then this will
485 // change.
486
487 Debugger &debugger = thread_sp->CalculateTarget()->GetDebugger();
488 bool old_async = debugger.GetAsyncExecution();
489 debugger.SetAsyncExecution(true);
490
491 callback_says_stop = bp_loc_sp->InvokeCallback(&context);
492
493 debugger.SetAsyncExecution(old_async);
494
495 if (callback_says_stop)
496 m_should_stop = true;
497
498 // If we are going to stop for this breakpoint, then remove the
499 // breakpoint.
500 if (callback_says_stop && bp_loc_sp &&
501 bp_loc_sp->GetBreakpoint().IsOneShot()) {
502 thread_sp->GetProcess()->GetTarget().RemoveBreakpointByID(
503 bp_loc_sp->GetBreakpoint().GetID());
504 }
505
506 // Also make sure that the callback hasn't continued the target.
507 // If it did, when we'll set m_should_start to false and get out of
508 // here.
509 if (HasTargetRunSinceMe()) {
510 m_should_stop = false;
511 break;
512 }
513 }
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000514 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000515 // We've figured out what this stop wants to do, so mark it as valid so
516 // we don't compute it again.
517 m_should_stop_is_valid = true;
518 } else {
519 m_should_stop = true;
520 m_should_stop_is_valid = true;
521 Log *log_process(
522 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
523
524 if (log_process)
525 log_process->Printf(
526 "Process::%s could not find breakpoint site id: %" PRId64 "...",
527 __FUNCTION__, m_value);
528 }
529 if (log)
530 log->Printf("Process::%s returning from action with m_should_stop: %d.",
531 __FUNCTION__, m_should_stop);
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000532 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000533 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000534
535private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000536 bool m_should_stop;
537 bool m_should_stop_is_valid;
538 bool m_should_perform_action; // Since we are trying to preserve the "state"
539 // of the system even if we run functions
540 // etc. behind the users backs, we need to make sure we only REALLY perform
541 // the action once.
542 lldb::addr_t m_address; // We use this to capture the breakpoint site address
543 // when we create the StopInfo,
544 // in case somebody deletes it between the time the StopInfo is made and the
545 // description is asked for.
546 lldb::break_id_t m_break_id;
547 bool m_was_one_shot;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000548};
549
Greg Claytonf4b47e12010-08-04 01:40:35 +0000550//----------------------------------------------------------------------
551// StopInfoWatchpoint
552//----------------------------------------------------------------------
553
Kate Stoneb9c1b512016-09-06 20:57:50 +0000554class StopInfoWatchpoint : public StopInfo {
Greg Claytonf4b47e12010-08-04 01:40:35 +0000555public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000556 // Make sure watchpoint is properly disabled and subsequently enabled while
557 // performing watchpoint actions.
558 class WatchpointSentry {
559 public:
Jim Ingham209a77d2016-10-25 20:34:32 +0000560 WatchpointSentry(ProcessSP p_sp, WatchpointSP w_sp) : process_sp(p_sp),
561 watchpoint_sp(w_sp) {
562 if (process_sp && watchpoint_sp) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000563 const bool notify = false;
Jim Ingham209a77d2016-10-25 20:34:32 +0000564 watchpoint_sp->TurnOnEphemeralMode();
565 process_sp->DisableWatchpoint(watchpoint_sp.get(), notify);
566 process_sp->AddPreResumeAction(SentryPreResumeAction, this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000567 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000568 }
Jim Ingham209a77d2016-10-25 20:34:32 +0000569
570 void DoReenable() {
571 if (process_sp && watchpoint_sp) {
572 bool was_disabled = watchpoint_sp->IsDisabledDuringEphemeralMode();
573 watchpoint_sp->TurnOffEphemeralMode();
574 const bool notify = false;
575 if (was_disabled) {
576 process_sp->DisableWatchpoint(watchpoint_sp.get(), notify);
577 } else {
578 process_sp->EnableWatchpoint(watchpoint_sp.get(), notify);
Pavel Labath2e8fe802016-10-21 10:52:11 +0000579 }
Pavel Labath2e8fe802016-10-21 10:52:11 +0000580 }
Jim Ingham94bd5752016-10-21 00:06:38 +0000581 }
Jim Ingham209a77d2016-10-25 20:34:32 +0000582
583 ~WatchpointSentry() {
584 DoReenable();
585 if (process_sp)
586 process_sp->ClearPreResumeAction(SentryPreResumeAction, this);
587 }
588
589 static bool SentryPreResumeAction(void *sentry_void) {
590 WatchpointSentry *sentry = (WatchpointSentry *) sentry_void;
591 sentry->DoReenable();
592 return true;
593 }
Jim Inghamf57b4352012-11-10 02:08:14 +0000594
Kate Stoneb9c1b512016-09-06 20:57:50 +0000595 private:
Jim Ingham209a77d2016-10-25 20:34:32 +0000596 ProcessSP process_sp;
597 WatchpointSP watchpoint_sp;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000598 };
599
600 StopInfoWatchpoint(Thread &thread, break_id_t watch_id,
601 lldb::addr_t watch_hit_addr)
602 : StopInfo(thread, watch_id), m_should_stop(false),
603 m_should_stop_is_valid(false), m_watch_hit_addr(watch_hit_addr) {}
604
605 ~StopInfoWatchpoint() override = default;
606
607 StopReason GetStopReason() const override { return eStopReasonWatchpoint; }
608
609 const char *GetDescription() override {
610 if (m_description.empty()) {
611 StreamString strm;
612 strm.Printf("watchpoint %" PRIi64, m_value);
613 m_description.swap(strm.GetString());
614 }
615 return m_description.c_str();
616 }
617
Jim Inghamf57b4352012-11-10 02:08:14 +0000618protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000619 bool ShouldStopSynchronous(Event *event_ptr) override {
620 // ShouldStop() method is idempotent and should not affect hit count.
621 // See Process::RunPrivateStateThread()->Process()->HandlePrivateEvent()
622 // -->Process()::ShouldBroadcastEvent()->ThreadList::ShouldStop()->
623 // Thread::ShouldStop()->ThreadPlanBase::ShouldStop()->
624 // StopInfoWatchpoint::ShouldStop() and
625 // Event::DoOnRemoval()->Process::ProcessEventData::DoOnRemoval()->
626 // StopInfoWatchpoint::PerformAction().
627 if (m_should_stop_is_valid)
628 return m_should_stop;
Johnny Chenfd158f42011-09-21 22:47:15 +0000629
Kate Stoneb9c1b512016-09-06 20:57:50 +0000630 ThreadSP thread_sp(m_thread_wp.lock());
631 if (thread_sp) {
632 WatchpointSP wp_sp(
633 thread_sp->CalculateTarget()->GetWatchpointList().FindByID(
634 GetValue()));
635 if (wp_sp) {
636 // Check if we should stop at a watchpoint.
637 ExecutionContext exe_ctx(thread_sp->GetStackFrameAtIndex(0));
638 StoppointCallbackContext context(event_ptr, exe_ctx, true);
639 m_should_stop = wp_sp->ShouldStop(&context);
640 } else {
641 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
Johnny Chenfd158f42011-09-21 22:47:15 +0000642
Kate Stoneb9c1b512016-09-06 20:57:50 +0000643 if (log)
644 log->Printf(
645 "Process::%s could not find watchpoint location id: %" PRId64
646 "...",
647 __FUNCTION__, GetValue());
Johnny Chenfd158f42011-09-21 22:47:15 +0000648
Johnny Chen16dcf712011-10-17 18:58:00 +0000649 m_should_stop = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000650 }
Johnny Chen16dcf712011-10-17 18:58:00 +0000651 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000652 m_should_stop_is_valid = true;
653 return m_should_stop;
654 }
655
656 bool ShouldStop(Event *event_ptr) override {
657 // This just reports the work done by PerformAction or the synchronous stop.
658 // It should
659 // only ever get called after they have had a chance to run.
660 assert(m_should_stop_is_valid);
661 return m_should_stop;
662 }
663
664 void PerformAction(Event *event_ptr) override {
665 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS);
666 // We're going to calculate if we should stop or not in some way during the
667 // course of
668 // this code. Also by default we're going to stop, so set that here.
669 m_should_stop = true;
Jim Ingham209a77d2016-10-25 20:34:32 +0000670
Kate Stoneb9c1b512016-09-06 20:57:50 +0000671
672 ThreadSP thread_sp(m_thread_wp.lock());
673 if (thread_sp) {
674
675 WatchpointSP wp_sp(
676 thread_sp->CalculateTarget()->GetWatchpointList().FindByID(
Jim Ingham209a77d2016-10-25 20:34:32 +0000677 GetValue()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000678 if (wp_sp) {
679 ExecutionContext exe_ctx(thread_sp->GetStackFrameAtIndex(0));
Jim Ingham209a77d2016-10-25 20:34:32 +0000680 ProcessSP process_sp = exe_ctx.GetProcessSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000681
682 {
683 // check if this process is running on an architecture where
684 // watchpoints trigger
685 // before the associated instruction runs. if so, disable the WP,
686 // single-step and then
687 // re-enable the watchpoint
Jim Ingham209a77d2016-10-25 20:34:32 +0000688 if (process_sp) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000689 uint32_t num;
690 bool wp_triggers_after;
Jim Ingham209a77d2016-10-25 20:34:32 +0000691
692 if (process_sp->GetWatchpointSupportInfo(num, wp_triggers_after)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000693 .Success()) {
694 if (!wp_triggers_after) {
Jim Ingham209a77d2016-10-25 20:34:32 +0000695 process_sp->DisableWatchpoint(wp_sp.get(), false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000696 StopInfoSP stored_stop_info_sp = thread_sp->GetStopInfo();
697 assert(stored_stop_info_sp.get() == this);
698
699 ThreadPlanSP new_plan_sp(
700 thread_sp->QueueThreadPlanForStepSingleInstruction(
701 false, // step-over
702 false, // abort_other_plans
703 true)); // stop_other_threads
704 new_plan_sp->SetIsMasterPlan(true);
705 new_plan_sp->SetOkayToDiscard(false);
706 new_plan_sp->SetPrivate(true);
Jim Ingham209a77d2016-10-25 20:34:32 +0000707 process_sp->GetThreadList().SetSelectedThreadByID(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000708 thread_sp->GetID());
Jim Ingham209a77d2016-10-25 20:34:32 +0000709 process_sp->ResumeSynchronous(nullptr);
710 process_sp->GetThreadList().SetSelectedThreadByID(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000711 thread_sp->GetID());
712 thread_sp->SetStopInfo(stored_stop_info_sp);
Jim Ingham209a77d2016-10-25 20:34:32 +0000713 process_sp->EnableWatchpoint(wp_sp.get(), false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000714 }
715 }
716 }
717 }
718
Jim Ingham209a77d2016-10-25 20:34:32 +0000719 // This sentry object makes sure the current watchpoint is disabled
720 // while performing watchpoint actions,
721 // and it is then enabled after we are finished.
722 WatchpointSentry sentry(process_sp, wp_sp);
723
Kate Stoneb9c1b512016-09-06 20:57:50 +0000724 /*
725 * MIPS: Last 3bits of the watchpoint address are masked by the kernel.
726 * For example:
727 * 'n' is at 0x120010d00 and 'm' is 0x120010d04. When a watchpoint is
728 * set at 'm', then
729 * watch exception is generated even when 'n' is read/written. To handle
730 * this case,
731 * server emulates the instruction at PC and finds the base address of
732 * the load/store
733 * instruction and appends it in the description of the stop-info
734 * packet. If watchpoint
735 * is not set on this address by user then this do not stop.
736 */
737 if (m_watch_hit_addr != LLDB_INVALID_ADDRESS) {
738 WatchpointSP wp_hit_sp =
739 thread_sp->CalculateTarget()->GetWatchpointList().FindByAddress(
740 m_watch_hit_addr);
741 if (!wp_hit_sp) {
742 m_should_stop = false;
743 wp_sp->IncrementFalseAlarmsAndReviseHitCount();
744 }
745 }
746
747 // TODO: This condition should be checked in the synchronous part of the
748 // watchpoint code
749 // (Watchpoint::ShouldStop), so that we avoid pulling an event even if
750 // the watchpoint fails
751 // the ignore count condition. It is moved here temporarily, because for
752 // archs with
753 // watchpoint_exceptions_received=before, the code in the previous lines
754 // takes care of moving
755 // the inferior to next PC. We have to check the ignore count condition
756 // after this is done,
757 // otherwise we will hit same watchpoint multiple times until we pass
758 // ignore condition, but we
759 // won't actually be ignoring them.
760 if (wp_sp->GetHitCount() <= wp_sp->GetIgnoreCount())
761 m_should_stop = false;
762
Jim Ingham209a77d2016-10-25 20:34:32 +0000763 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
764
Kate Stoneb9c1b512016-09-06 20:57:50 +0000765 if (m_should_stop && wp_sp->GetConditionText() != nullptr) {
766 // We need to make sure the user sees any parse errors in their
Zachary Turnerc5d7df92016-11-08 04:52:16 +0000767 // condition, so we'll hook the constructor errors up to the
768 // debugger's Async I/O.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000769 ExpressionResults result_code;
770 EvaluateExpressionOptions expr_options;
771 expr_options.SetUnwindOnError(true);
772 expr_options.SetIgnoreBreakpoints(true);
773 ValueObjectSP result_value_sp;
774 Error error;
775 result_code = UserExpression::Evaluate(
Zachary Turnerc5d7df92016-11-08 04:52:16 +0000776 exe_ctx, expr_options, wp_sp->GetConditionText(),
777 llvm::StringRef(), result_value_sp, error);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000778
779 if (result_code == eExpressionCompleted) {
780 if (result_value_sp) {
781 Scalar scalar_value;
782 if (result_value_sp->ResolveValue(scalar_value)) {
783 if (scalar_value.ULongLong(1) == 0) {
784 // We have been vetoed. This takes precedence over querying
785 // the watchpoint whether it should stop (aka ignore count and
786 // friends). See also StopInfoWatchpoint::ShouldStop() as
Zachary Turnerc5d7df92016-11-08 04:52:16 +0000787 // well as Process::ProcessEventData::DoOnRemoval().
Kate Stoneb9c1b512016-09-06 20:57:50 +0000788 m_should_stop = false;
789 } else
790 m_should_stop = true;
791 if (log)
792 log->Printf(
793 "Condition successfully evaluated, result is %s.\n",
794 m_should_stop ? "true" : "false");
795 } else {
796 m_should_stop = true;
797 if (log)
798 log->Printf(
799 "Failed to get an integer result from the expression.");
800 }
801 }
802 } else {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000803 StreamSP error_sp = debugger.GetAsyncErrorStream();
804 error_sp->Printf(
805 "Stopped due to an error evaluating condition of watchpoint ");
806 wp_sp->GetDescription(error_sp.get(), eDescriptionLevelBrief);
807 error_sp->Printf(": \"%s\"", wp_sp->GetConditionText());
808 error_sp->EOL();
809 const char *err_str = error.AsCString("<Unknown Error>");
810 if (log)
811 log->Printf("Error evaluating condition: \"%s\"\n", err_str);
812
813 error_sp->PutCString(err_str);
814 error_sp->EOL();
815 error_sp->Flush();
816 // If the condition fails to be parsed or run, we should stop.
817 m_should_stop = true;
818 }
819 }
820
821 // If the condition says to stop, we run the callback to further decide
822 // whether to stop.
823 if (m_should_stop) {
Jim Ingham209a77d2016-10-25 20:34:32 +0000824 // FIXME: For now the callbacks have to run in async mode - the
825 // first time we restart we need
826 // to get out of there. So set it here.
827 // When we figure out how to nest watchpoint hits then this will
828 // change.
829
830 bool old_async = debugger.GetAsyncExecution();
831 debugger.SetAsyncExecution(true);
832
Kate Stoneb9c1b512016-09-06 20:57:50 +0000833 StoppointCallbackContext context(event_ptr, exe_ctx, false);
834 bool stop_requested = wp_sp->InvokeCallback(&context);
Jim Ingham209a77d2016-10-25 20:34:32 +0000835
836 debugger.SetAsyncExecution(old_async);
837
Kate Stoneb9c1b512016-09-06 20:57:50 +0000838 // Also make sure that the callback hasn't continued the target.
839 // If it did, when we'll set m_should_stop to false and get out of
840 // here.
841 if (HasTargetRunSinceMe())
842 m_should_stop = false;
843
844 if (m_should_stop && !stop_requested) {
845 // We have been vetoed by the callback mechanism.
846 m_should_stop = false;
847 }
848 }
849 // Finally, if we are going to stop, print out the new & old values:
850 if (m_should_stop) {
851 wp_sp->CaptureWatchedValue(exe_ctx);
852
853 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
854 StreamSP output_sp = debugger.GetAsyncOutputStream();
855 wp_sp->DumpSnapshots(output_sp.get());
856 output_sp->EOL();
857 output_sp->Flush();
858 }
859
860 } else {
861 Log *log_process(
862 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
863
864 if (log_process)
865 log_process->Printf(
866 "Process::%s could not find watchpoint id: %" PRId64 "...",
867 __FUNCTION__, m_value);
868 }
869 if (log)
870 log->Printf("Process::%s returning from action with m_should_stop: %d.",
871 __FUNCTION__, m_should_stop);
872
873 m_should_stop_is_valid = true;
874 }
875 }
876
Greg Claytonf4b47e12010-08-04 01:40:35 +0000877private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000878 bool m_should_stop;
879 bool m_should_stop_is_valid;
880 lldb::addr_t m_watch_hit_addr;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000881};
882
Greg Claytonf4b47e12010-08-04 01:40:35 +0000883//----------------------------------------------------------------------
884// StopInfoUnixSignal
885//----------------------------------------------------------------------
886
Kate Stoneb9c1b512016-09-06 20:57:50 +0000887class StopInfoUnixSignal : public StopInfo {
Greg Claytonf4b47e12010-08-04 01:40:35 +0000888public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000889 StopInfoUnixSignal(Thread &thread, int signo, const char *description)
890 : StopInfo(thread, signo) {
891 SetDescription(description);
892 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000893
Kate Stoneb9c1b512016-09-06 20:57:50 +0000894 ~StopInfoUnixSignal() override = default;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000895
Kate Stoneb9c1b512016-09-06 20:57:50 +0000896 StopReason GetStopReason() const override { return eStopReasonSignal; }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000897
Kate Stoneb9c1b512016-09-06 20:57:50 +0000898 bool ShouldStopSynchronous(Event *event_ptr) override {
899 ThreadSP thread_sp(m_thread_wp.lock());
900 if (thread_sp)
901 return thread_sp->GetProcess()->GetUnixSignals()->GetShouldStop(m_value);
902 return false;
903 }
Jim Ingham0161b492013-02-09 01:29:05 +0000904
Kate Stoneb9c1b512016-09-06 20:57:50 +0000905 bool ShouldStop(Event *event_ptr) override {
906 ThreadSP thread_sp(m_thread_wp.lock());
907 if (thread_sp)
908 return thread_sp->GetProcess()->GetUnixSignals()->GetShouldStop(m_value);
909 return false;
910 }
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000911
Kate Stoneb9c1b512016-09-06 20:57:50 +0000912 // If should stop returns false, check if we should notify of this event
913 bool DoShouldNotify(Event *event_ptr) override {
914 ThreadSP thread_sp(m_thread_wp.lock());
915 if (thread_sp) {
916 bool should_notify =
917 thread_sp->GetProcess()->GetUnixSignals()->GetShouldNotify(m_value);
918 if (should_notify) {
919 StreamString strm;
920 strm.Printf(
921 "thread %d received signal: %s", thread_sp->GetIndexID(),
922 thread_sp->GetProcess()->GetUnixSignals()->GetSignalAsCString(
923 m_value));
924 Process::ProcessEventData::AddRestartedReason(event_ptr,
925 strm.GetData());
926 }
927 return should_notify;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000928 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000929 return true;
930 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000931
Kate Stoneb9c1b512016-09-06 20:57:50 +0000932 void WillResume(lldb::StateType resume_state) override {
933 ThreadSP thread_sp(m_thread_wp.lock());
934 if (thread_sp) {
935 if (!thread_sp->GetProcess()->GetUnixSignals()->GetShouldSuppress(
936 m_value))
937 thread_sp->SetResumeSignal(m_value);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000938 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000939 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000940
Kate Stoneb9c1b512016-09-06 20:57:50 +0000941 const char *GetDescription() override {
942 if (m_description.empty()) {
943 ThreadSP thread_sp(m_thread_wp.lock());
944 if (thread_sp) {
945 StreamString strm;
946 const char *signal_name =
947 thread_sp->GetProcess()->GetUnixSignals()->GetSignalAsCString(
948 m_value);
949 if (signal_name)
950 strm.Printf("signal %s", signal_name);
951 else
952 strm.Printf("signal %" PRIi64, m_value);
953 m_description.swap(strm.GetString());
954 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000955 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000956 return m_description.c_str();
957 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000958};
959
960//----------------------------------------------------------------------
961// StopInfoTrace
962//----------------------------------------------------------------------
963
Kate Stoneb9c1b512016-09-06 20:57:50 +0000964class StopInfoTrace : public StopInfo {
Greg Claytonf4b47e12010-08-04 01:40:35 +0000965public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000966 StopInfoTrace(Thread &thread) : StopInfo(thread, LLDB_INVALID_UID) {}
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000967
Kate Stoneb9c1b512016-09-06 20:57:50 +0000968 ~StopInfoTrace() override = default;
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000969
Kate Stoneb9c1b512016-09-06 20:57:50 +0000970 StopReason GetStopReason() const override { return eStopReasonTrace; }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000971
Kate Stoneb9c1b512016-09-06 20:57:50 +0000972 const char *GetDescription() override {
973 if (m_description.empty())
974 return "trace";
975 else
976 return m_description.c_str();
977 }
Greg Claytona658fd22011-06-04 01:26:29 +0000978};
979
Greg Claytona658fd22011-06-04 01:26:29 +0000980//----------------------------------------------------------------------
981// StopInfoException
982//----------------------------------------------------------------------
983
Kate Stoneb9c1b512016-09-06 20:57:50 +0000984class StopInfoException : public StopInfo {
Greg Claytona658fd22011-06-04 01:26:29 +0000985public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000986 StopInfoException(Thread &thread, const char *description)
987 : StopInfo(thread, LLDB_INVALID_UID) {
988 if (description)
989 SetDescription(description);
990 }
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000991
Kate Stoneb9c1b512016-09-06 20:57:50 +0000992 ~StopInfoException() override = default;
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000993
Kate Stoneb9c1b512016-09-06 20:57:50 +0000994 StopReason GetStopReason() const override { return eStopReasonException; }
995
996 const char *GetDescription() override {
997 if (m_description.empty())
998 return "exception";
999 else
1000 return m_description.c_str();
1001 }
Greg Claytonf4b47e12010-08-04 01:40:35 +00001002};
1003
Greg Claytonf4b47e12010-08-04 01:40:35 +00001004//----------------------------------------------------------------------
1005// StopInfoThreadPlan
1006//----------------------------------------------------------------------
1007
Kate Stoneb9c1b512016-09-06 20:57:50 +00001008class StopInfoThreadPlan : public StopInfo {
Greg Claytonf4b47e12010-08-04 01:40:35 +00001009public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001010 StopInfoThreadPlan(ThreadPlanSP &plan_sp, ValueObjectSP &return_valobj_sp,
1011 ExpressionVariableSP &expression_variable_sp)
1012 : StopInfo(plan_sp->GetThread(), LLDB_INVALID_UID), m_plan_sp(plan_sp),
1013 m_return_valobj_sp(return_valobj_sp),
1014 m_expression_variable_sp(expression_variable_sp) {}
Greg Claytonf4b47e12010-08-04 01:40:35 +00001015
Kate Stoneb9c1b512016-09-06 20:57:50 +00001016 ~StopInfoThreadPlan() override = default;
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001017
Kate Stoneb9c1b512016-09-06 20:57:50 +00001018 StopReason GetStopReason() const override { return eStopReasonPlanComplete; }
Greg Claytonf4b47e12010-08-04 01:40:35 +00001019
Kate Stoneb9c1b512016-09-06 20:57:50 +00001020 const char *GetDescription() override {
1021 if (m_description.empty()) {
1022 StreamString strm;
1023 m_plan_sp->GetDescription(&strm, eDescriptionLevelBrief);
1024 m_description.swap(strm.GetString());
Greg Claytonf4b47e12010-08-04 01:40:35 +00001025 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001026 return m_description.c_str();
1027 }
1028
1029 ValueObjectSP GetReturnValueObject() { return m_return_valobj_sp; }
1030
1031 ExpressionVariableSP GetExpressionVariable() {
1032 return m_expression_variable_sp;
1033 }
1034
Jim Ingham0161b492013-02-09 01:29:05 +00001035protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001036 bool ShouldStop(Event *event_ptr) override {
1037 if (m_plan_sp)
1038 return m_plan_sp->ShouldStop(event_ptr);
1039 else
1040 return StopInfo::ShouldStop(event_ptr);
1041 }
Greg Claytonf4b47e12010-08-04 01:40:35 +00001042
1043private:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001044 ThreadPlanSP m_plan_sp;
1045 ValueObjectSP m_return_valobj_sp;
1046 ExpressionVariableSP m_expression_variable_sp;
Greg Claytonf4b47e12010-08-04 01:40:35 +00001047};
Kate Stoneb9c1b512016-09-06 20:57:50 +00001048
1049class StopInfoExec : public StopInfo {
Greg Clayton90ba8112012-12-05 00:16:59 +00001050public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001051 StopInfoExec(Thread &thread)
1052 : StopInfo(thread, LLDB_INVALID_UID), m_performed_action(false) {}
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001053
Kate Stoneb9c1b512016-09-06 20:57:50 +00001054 ~StopInfoExec() override = default;
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001055
Kate Stoneb9c1b512016-09-06 20:57:50 +00001056 StopReason GetStopReason() const override { return eStopReasonExec; }
1057
1058 const char *GetDescription() override { return "exec"; }
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001059
Greg Clayton90ba8112012-12-05 00:16:59 +00001060protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001061 void PerformAction(Event *event_ptr) override {
1062 // Only perform the action once
1063 if (m_performed_action)
1064 return;
1065 m_performed_action = true;
1066 ThreadSP thread_sp(m_thread_wp.lock());
1067 if (thread_sp)
1068 thread_sp->GetProcess()->DidExec();
1069 }
1070
1071 bool m_performed_action;
Greg Clayton90ba8112012-12-05 00:16:59 +00001072};
1073
Jim Ingham4b536182011-08-09 02:12:22 +00001074} // namespace lldb_private
Greg Claytonf4b47e12010-08-04 01:40:35 +00001075
Kate Stoneb9c1b512016-09-06 20:57:50 +00001076StopInfoSP StopInfo::CreateStopReasonWithBreakpointSiteID(Thread &thread,
1077 break_id_t break_id) {
1078 return StopInfoSP(new StopInfoBreakpoint(thread, break_id));
1079}
1080
1081StopInfoSP StopInfo::CreateStopReasonWithBreakpointSiteID(Thread &thread,
1082 break_id_t break_id,
1083 bool should_stop) {
1084 return StopInfoSP(new StopInfoBreakpoint(thread, break_id, should_stop));
Greg Claytonf4b47e12010-08-04 01:40:35 +00001085}
1086
1087StopInfoSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001088StopInfo::CreateStopReasonWithWatchpointID(Thread &thread, break_id_t watch_id,
1089 lldb::addr_t watch_hit_addr) {
1090 return StopInfoSP(new StopInfoWatchpoint(thread, watch_id, watch_hit_addr));
Jim Ingham36f3b362010-10-14 23:45:03 +00001091}
1092
Kate Stoneb9c1b512016-09-06 20:57:50 +00001093StopInfoSP StopInfo::CreateStopReasonWithSignal(Thread &thread, int signo,
1094 const char *description) {
1095 return StopInfoSP(new StopInfoUnixSignal(thread, signo, description));
Greg Claytonf4b47e12010-08-04 01:40:35 +00001096}
1097
Kate Stoneb9c1b512016-09-06 20:57:50 +00001098StopInfoSP StopInfo::CreateStopReasonToTrace(Thread &thread) {
1099 return StopInfoSP(new StopInfoTrace(thread));
Greg Claytonf4b47e12010-08-04 01:40:35 +00001100}
1101
Kate Stoneb9c1b512016-09-06 20:57:50 +00001102StopInfoSP StopInfo::CreateStopReasonWithPlan(
1103 ThreadPlanSP &plan_sp, ValueObjectSP return_valobj_sp,
1104 ExpressionVariableSP expression_variable_sp) {
1105 return StopInfoSP(new StopInfoThreadPlan(plan_sp, return_valobj_sp,
1106 expression_variable_sp));
Greg Claytonf4b47e12010-08-04 01:40:35 +00001107}
1108
Kate Stoneb9c1b512016-09-06 20:57:50 +00001109StopInfoSP StopInfo::CreateStopReasonWithException(Thread &thread,
1110 const char *description) {
1111 return StopInfoSP(new StopInfoException(thread, description));
Greg Claytonf4b47e12010-08-04 01:40:35 +00001112}
Greg Claytona658fd22011-06-04 01:26:29 +00001113
Kate Stoneb9c1b512016-09-06 20:57:50 +00001114StopInfoSP StopInfo::CreateStopReasonWithExec(Thread &thread) {
1115 return StopInfoSP(new StopInfoExec(thread));
Greg Claytona658fd22011-06-04 01:26:29 +00001116}
Jim Ingham73ca05a2011-12-17 01:35:57 +00001117
Kate Stoneb9c1b512016-09-06 20:57:50 +00001118ValueObjectSP StopInfo::GetReturnValueObject(StopInfoSP &stop_info_sp) {
1119 if (stop_info_sp &&
1120 stop_info_sp->GetStopReason() == eStopReasonPlanComplete) {
1121 StopInfoThreadPlan *plan_stop_info =
1122 static_cast<StopInfoThreadPlan *>(stop_info_sp.get());
1123 return plan_stop_info->GetReturnValueObject();
1124 } else
1125 return ValueObjectSP();
Greg Clayton90ba8112012-12-05 00:16:59 +00001126}
1127
Kate Stoneb9c1b512016-09-06 20:57:50 +00001128ExpressionVariableSP StopInfo::GetExpressionVariable(StopInfoSP &stop_info_sp) {
1129 if (stop_info_sp &&
1130 stop_info_sp->GetStopReason() == eStopReasonPlanComplete) {
1131 StopInfoThreadPlan *plan_stop_info =
1132 static_cast<StopInfoThreadPlan *>(stop_info_sp.get());
1133 return plan_stop_info->GetExpressionVariable();
1134 } else
1135 return ExpressionVariableSP();
Jim Ingham30fadaf2014-07-08 01:07:32 +00001136}
Sean Callanan4740a732016-09-06 04:48:36 +00001137
1138lldb::ValueObjectSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001139StopInfo::GetCrashingDereference(StopInfoSP &stop_info_sp,
1140 lldb::addr_t *crashing_address) {
1141 if (!stop_info_sp) {
1142 return ValueObjectSP();
1143 }
Sean Callanan4740a732016-09-06 04:48:36 +00001144
Kate Stoneb9c1b512016-09-06 20:57:50 +00001145 const char *description = stop_info_sp->GetDescription();
1146 if (!description) {
1147 return ValueObjectSP();
1148 }
1149
1150 ThreadSP thread_sp = stop_info_sp->GetThread();
1151 if (!thread_sp) {
1152 return ValueObjectSP();
1153 }
1154
1155 StackFrameSP frame_sp = thread_sp->GetSelectedFrame();
1156
1157 if (!frame_sp) {
1158 return ValueObjectSP();
1159 }
1160
1161 const char address_string[] = "address=";
1162
1163 const char *address_loc = strstr(description, address_string);
1164 if (!address_loc) {
1165 return ValueObjectSP();
1166 }
1167
1168 address_loc += (sizeof(address_string) - 1);
1169
1170 uint64_t address = strtoull(address_loc, 0, 0);
1171 if (crashing_address) {
1172 *crashing_address = address;
1173 }
1174
1175 return frame_sp->GuessValueForAddress(address);
Sean Callanan4740a732016-09-06 04:48:36 +00001176}