LLDB  mainline
ThreadPlanPython.cpp
Go to the documentation of this file.
1 //===-- ThreadPlanPython.cpp ----------------------------------------------===//
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 
10 
11 #include "lldb/Core/Debugger.h"
14 #include "lldb/Target/Process.h"
16 #include "lldb/Target/Target.h"
17 #include "lldb/Target/Thread.h"
18 #include "lldb/Target/ThreadPlan.h"
20 #include "lldb/Utility/Log.h"
21 #include "lldb/Utility/State.h"
22 
23 using namespace lldb;
24 using namespace lldb_private;
25 
26 // ThreadPlanPython
27 
28 ThreadPlanPython::ThreadPlanPython(Thread &thread, const char *class_name,
29  StructuredDataImpl *args_data)
30  : ThreadPlan(ThreadPlan::eKindPython, "Python based Thread Plan", thread,
32  m_class_name(class_name), m_args_data(args_data), m_did_push(false),
33  m_stop_others(false) {
34  SetIsMasterPlan(true);
35  SetOkayToDiscard(true);
36  SetPrivate(false);
37 }
38 
40  // FIXME, do I need to decrement the ref count on this implementation object
41  // to make it go away?
42 }
43 
45  if (!m_did_push)
46  return true;
47 
48  if (!m_implementation_sp) {
49  if (error)
50  error->Printf("Error constructing Python ThreadPlan: %s",
51  m_error_str.empty() ? "<unknown error>"
52  : m_error_str.c_str());
53  return false;
54  }
55 
56  return true;
57 }
58 
61 }
62 
64  // We set up the script side in DidPush, so that it can push other plans in
65  // the constructor, and doesn't have to care about the details of DidPush.
66  m_did_push = true;
67  if (!m_class_name.empty()) {
68  ScriptInterpreter *script_interp = GetScriptInterpreter();
69  if (script_interp) {
72  this->shared_from_this());
73  }
74  }
75 }
76 
79  LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
80  m_class_name.c_str());
81 
82  bool should_stop = true;
83  if (m_implementation_sp) {
84  ScriptInterpreter *script_interp = GetScriptInterpreter();
85  if (script_interp) {
86  bool script_error;
87  should_stop = script_interp->ScriptedThreadPlanShouldStop(
88  m_implementation_sp, event_ptr, script_error);
89  if (script_error)
90  SetPlanComplete(false);
91  }
92  }
93  return should_stop;
94 }
95 
98  LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
99  m_class_name.c_str());
100 
101  bool is_stale = true;
102  if (m_implementation_sp) {
103  ScriptInterpreter *script_interp = GetScriptInterpreter();
104  if (script_interp) {
105  bool script_error;
106  is_stale = script_interp->ScriptedThreadPlanIsStale(m_implementation_sp,
107  script_error);
108  if (script_error)
109  SetPlanComplete(false);
110  }
111  }
112  return is_stale;
113 }
114 
117  LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
118  m_class_name.c_str());
119 
120  bool explains_stop = true;
121  if (m_implementation_sp) {
122  ScriptInterpreter *script_interp = GetScriptInterpreter();
123  if (script_interp) {
124  bool script_error;
125  explains_stop = script_interp->ScriptedThreadPlanExplainsStop(
126  m_implementation_sp, event_ptr, script_error);
127  if (script_error)
128  SetPlanComplete(false);
129  }
130  }
131  return explains_stop;
132 }
133 
136  LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
137  m_class_name.c_str());
138  bool mischief_managed = true;
139  if (m_implementation_sp) {
140  // I don't really need mischief_managed, since it's simpler to just call
141  // SetPlanComplete in should_stop.
142  mischief_managed = IsPlanComplete();
143  if (mischief_managed)
144  m_implementation_sp.reset();
145  }
146  return mischief_managed;
147 }
148 
151  LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
152  m_class_name.c_str());
153  lldb::StateType run_state = eStateRunning;
154  if (m_implementation_sp) {
155  ScriptInterpreter *script_interp = GetScriptInterpreter();
156  if (script_interp) {
157  bool script_error;
158  run_state = script_interp->ScriptedThreadPlanGetRunState(
159  m_implementation_sp, script_error);
160  }
161  }
162  return run_state;
163 }
164 
165 // The ones below are not currently exported to Python.
167  s->Printf("Python thread plan implemented by class %s.",
168  m_class_name.c_str());
169 }
170 
173  LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
174  m_class_name.c_str());
175  return true;
176 }
lldb_private::ThreadPlanPython::GetPlanRunState
lldb::StateType GetPlanRunState() override
Definition: ThreadPlanPython.cpp:149
lldb_private::ThreadPlanPython::ShouldStop
bool ShouldStop(Event *event_ptr) override
Definition: ThreadPlanPython.cpp:77
lldb_private::Event
Definition: Event.h:180
lldb_private::ThreadPlan::IsPlanComplete
bool IsPlanComplete()
Definition: ThreadPlan.cpp:60
lldb_private::ThreadPlan::SetOkayToDiscard
void SetOkayToDiscard(bool value)
Definition: ThreadPlan.h:399
lldb_private::ThreadPlanPython::DidPush
void DidPush() override
Definition: ThreadPlanPython.cpp:63
lldb_private::ThreadPlanPython::WillStop
bool WillStop() override
Definition: ThreadPlanPython.cpp:171
LLDB_LOGF
#define LLDB_LOGF(log,...)
Definition: Log.h:249
lldb_private::Process::GetTarget
Target & GetTarget()
Get the target object pointer for this module.
Definition: Process.h:1214
lldb_private::eVoteNoOpinion
@ eVoteNoOpinion
Definition: lldb-private-enumerations.h:59
lldb_private::Stream
Definition: Stream.h:28
ThreadPlanPython.h
lldb_private::ThreadPlanPython::m_args_data
StructuredDataImpl * m_args_data
Definition: ThreadPlanPython.h:65
lldb_private::Target::GetDebugger
Debugger & GetDebugger()
Definition: Target.h:1003
Debugger.h
lldb_private::Debugger::GetScriptInterpreter
ScriptInterpreter * GetScriptInterpreter(bool can_create=true, llvm::Optional< lldb::ScriptLanguage > language={})
Definition: Debugger.cpp:1275
lldb_private::ScriptInterpreter::CreateScriptedThreadPlan
virtual StructuredData::ObjectSP CreateScriptedThreadPlan(const char *class_name, StructuredDataImpl *args_data, std::string &error_str, lldb::ThreadPlanSP thread_plan_sp)
Definition: ScriptInterpreter.h:273
Process.h
lldb_private::ThreadPlan::SetIsMasterPlan
bool SetIsMasterPlan(bool value)
Definition: ThreadPlan.h:391
lldb_private::ScriptInterpreter::ScriptedThreadPlanGetRunState
virtual lldb::StateType ScriptedThreadPlanGetRunState(StructuredData::ObjectSP implementor_sp, bool &script_error)
Definition: ScriptInterpreter.h:302
Target.h
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::ThreadPlan
Definition: ThreadPlan.h:282
lldb_private::ThreadPlanPython::MischiefManaged
bool MischiefManaged() override
Definition: ThreadPlanPython.cpp:134
lldb_private::ThreadPlanPython::ValidatePlan
bool ValidatePlan(Stream *error) override
Returns whether this plan could be successfully created.
Definition: ThreadPlanPython.cpp:44
lldb_private::ThreadPlanPython::m_error_str
std::string m_error_str
Definition: ThreadPlanPython.h:69
Log.h
lldb_private::ThreadPlanPython::GetDescription
void GetDescription(Stream *s, lldb::DescriptionLevel level) override
Print a description of this thread to the stream s.
Definition: ThreadPlanPython.cpp:166
lldb_private::Thread
Definition: Thread.h:62
lldb_private::ThreadPlanPython::DoPlanExplainsStop
bool DoPlanExplainsStop(Event *event_ptr) override
Definition: ThreadPlanPython.cpp:115
lldb_private::ThreadPlanPython::m_did_push
bool m_did_push
Definition: ThreadPlanPython.h:71
lldb_private::ThreadPlanPython::GetScriptInterpreter
ScriptInterpreter * GetScriptInterpreter()
Definition: ThreadPlanPython.cpp:59
lldb_private::GetLogIfAllCategoriesSet
Log * GetLogIfAllCategoriesSet(uint32_t mask)
Definition: Logging.cpp:58
lldb_private::StructuredDataImpl
Definition: StructuredDataImpl.h:26
Thread.h
lldb_private::ThreadPlan::SetPrivate
void SetPrivate(bool input)
Definition: ThreadPlan.h:412
ThreadPlan.h
lldb_private::ThreadPlan::SetPlanComplete
void SetPlanComplete(bool success=true)
Definition: ThreadPlan.cpp:65
lldb_private::ThreadPlanPython::IsPlanStale
bool IsPlanStale() override
Definition: ThreadPlanPython.cpp:96
lldb_private::ScriptInterpreter::ScriptedThreadPlanIsStale
virtual bool ScriptedThreadPlanIsStale(StructuredData::ObjectSP implementor_sp, bool &script_error)
Definition: ScriptInterpreter.h:295
lldb_private::ThreadPlanPython::m_class_name
std::string m_class_name
Definition: ThreadPlanPython.h:64
lldb_private::ScriptInterpreter
Definition: ScriptInterpreter.h:125
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
lldb_private::ThreadPlanPython::m_implementation_sp
StructuredData::ObjectSP m_implementation_sp
Definition: ThreadPlanPython.h:70
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::ScriptInterpreter::ScriptedThreadPlanExplainsStop
virtual bool ScriptedThreadPlanExplainsStop(StructuredData::ObjectSP implementor_sp, Event *event, bool &script_error)
Definition: ScriptInterpreter.h:281
CommandInterpreter.h
State.h
lldb::StateType
StateType
Process and Thread States.
Definition: lldb-enumerations.h:73
lldb_private::Log
Definition: Log.h:49
lldb_private::ThreadPlan::m_process
Process & m_process
Definition: ThreadPlan.h:523
ScriptInterpreter.h
lldb
Definition: SBAddress.h:15
LIBLLDB_LOG_THREAD
#define LIBLLDB_LOG_THREAD
Definition: Logging.h:16
lldb::eStateRunning
@ eStateRunning
Process or thread is running and can't be examined.
Definition: lldb-enumerations.h:85
RegisterContext.h
lldb_private::ThreadPlanPython::~ThreadPlanPython
~ThreadPlanPython() override
Definition: ThreadPlanPython.cpp:39
lldb::DescriptionLevel
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
Definition: lldb-enumerations.h:207
lldb_private::ScriptInterpreter::ScriptedThreadPlanShouldStop
virtual bool ScriptedThreadPlanShouldStop(StructuredData::ObjectSP implementor_sp, Event *event, bool &script_error)
Definition: ScriptInterpreter.h:288