1 //===-- ThreadPlanCallOnFunctionExit.cpp ------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/Target/ThreadPlanCallOnFunctionExit.h"
10 
11 using namespace lldb;
12 using namespace lldb_private;
13 
14 ThreadPlanCallOnFunctionExit::ThreadPlanCallOnFunctionExit(
15     Thread &thread, const Callback &callback)
16     : ThreadPlan(ThreadPlanKind::eKindGeneric, "CallOnFunctionExit", thread,
17                  eVoteNoOpinion, eVoteNoOpinion // TODO check with Jim on these
18                  ),
19       m_callback(callback) {
20   // We are not a user-generated plan.
21   SetIsMasterPlan(false);
22 }
23 
24 void ThreadPlanCallOnFunctionExit::DidPush() {
25   // We now want to queue the "step out" thread plan so it executes and
26   // completes.
27 
28   // Set stop vote to eVoteNo.
29   Status status;
30   m_step_out_threadplan_sp = GetThread().QueueThreadPlanForStepOut(
31       false,             // abort other plans
32       nullptr,           // addr_context
33       true,              // first instruction
34       true,              // stop other threads
35       eVoteNo,           // do not say "we're stopping"
36       eVoteNoOpinion,    // don't care about run state broadcasting
37       0,                 // frame_idx
38       status,            // status
39       eLazyBoolCalculate // avoid code w/o debinfo
40   );
41 }
42 
43 // -------------------------------------------------------------------------
44 // ThreadPlan API
45 // -------------------------------------------------------------------------
46 
47 void ThreadPlanCallOnFunctionExit::GetDescription(
48     Stream *s, lldb::DescriptionLevel level) {
49   if (!s)
50     return;
51   s->Printf("Running until completion of current function, then making "
52             "callback.");
53 }
54 
55 bool ThreadPlanCallOnFunctionExit::ValidatePlan(Stream *error) {
56   // We'll say we're always good since I don't know what would make this
57   // invalid.
58   return true;
59 }
60 
61 bool ThreadPlanCallOnFunctionExit::ShouldStop(Event *event_ptr) {
62   // If this is where we find out that an internal stop came in, then: Check if
63   // the step-out plan completed.  If it did, then we want to run the callback
64   // here (our reason for living...)
65   if (m_step_out_threadplan_sp && m_step_out_threadplan_sp->IsPlanComplete()) {
66     m_callback();
67 
68     // We no longer need the pointer to the step-out thread plan.
69     m_step_out_threadplan_sp.reset();
70 
71     // Indicate that this plan is done and can be discarded.
72     SetPlanComplete();
73 
74     // We're done now, but we want to return false so that we don't cause the
75     // thread to really stop.
76   }
77 
78   return false;
79 }
80 
81 bool ThreadPlanCallOnFunctionExit::WillStop() {
82   // The code looks like the return value is ignored via ThreadList::
83   // ShouldStop(). This is called when we really are going to stop.  We don't
84   // care and don't need to do anything here.
85   return false;
86 }
87 
88 bool ThreadPlanCallOnFunctionExit::DoPlanExplainsStop(Event *event_ptr) {
89   // We don't ever explain a stop.  The only stop that is relevant to us
90   // directly is the step_out plan we added to do the heavy lifting of getting
91   // us past the current method.
92   return false;
93 }
94 
95 lldb::StateType ThreadPlanCallOnFunctionExit::GetPlanRunState() {
96   // This value doesn't matter - we'll never be the top thread plan, so nobody
97   // will ask us this question.
98   return eStateRunning;
99 }
100