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  SetIsMasterPlan(true);
34  SetOkayToDiscard(true);
35  SetPrivate(false);
36 }
37 
39  // FIXME, do I need to decrement the ref count on this implementation object
40  // to make it go away?
41 }
42 
44  if (!m_did_push)
45  return true;
46 
47  if (!m_implementation_sp) {
48  if (error)
49  error->Printf("Error constructing Python ThreadPlan: %s",
50  m_error_str.empty() ? "<unknown error>"
51  : m_error_str.c_str());
52  return false;
53  }
54 
55  return true;
56 }
57 
60 }
61 
63  // We set up the script side in DidPush, so that it can push other plans in
64  // the constructor, and doesn't have to care about the details of DidPush.
65  m_did_push = true;
66  if (!m_class_name.empty()) {
67  ScriptInterpreter *script_interp = GetScriptInterpreter();
68  if (script_interp) {
71  this->shared_from_this());
72  }
73  }
74 }
75 
78  LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
79  m_class_name.c_str());
80 
81  bool should_stop = true;
82  if (m_implementation_sp) {
83  ScriptInterpreter *script_interp = GetScriptInterpreter();
84  if (script_interp) {
85  bool script_error;
86  should_stop = script_interp->ScriptedThreadPlanShouldStop(
87  m_implementation_sp, event_ptr, script_error);
88  if (script_error)
89  SetPlanComplete(false);
90  }
91  }
92  return should_stop;
93 }
94 
97  LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
98  m_class_name.c_str());
99 
100  bool is_stale = true;
101  if (m_implementation_sp) {
102  ScriptInterpreter *script_interp = GetScriptInterpreter();
103  if (script_interp) {
104  bool script_error;
105  is_stale = script_interp->ScriptedThreadPlanIsStale(m_implementation_sp,
106  script_error);
107  if (script_error)
108  SetPlanComplete(false);
109  }
110  }
111  return is_stale;
112 }
113 
116  LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
117  m_class_name.c_str());
118 
119  bool explains_stop = true;
120  if (m_implementation_sp) {
121  ScriptInterpreter *script_interp = GetScriptInterpreter();
122  if (script_interp) {
123  bool script_error;
124  explains_stop = script_interp->ScriptedThreadPlanExplainsStop(
125  m_implementation_sp, event_ptr, script_error);
126  if (script_error)
127  SetPlanComplete(false);
128  }
129  }
130  return explains_stop;
131 }
132 
135  LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
136  m_class_name.c_str());
137  bool mischief_managed = true;
138  if (m_implementation_sp) {
139  // I don't really need mischief_managed, since it's simpler to just call
140  // SetPlanComplete in should_stop.
141  mischief_managed = IsPlanComplete();
142  if (mischief_managed)
143  m_implementation_sp.reset();
144  }
145  return mischief_managed;
146 }
147 
150  LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
151  m_class_name.c_str());
152  lldb::StateType run_state = eStateRunning;
153  if (m_implementation_sp) {
154  ScriptInterpreter *script_interp = GetScriptInterpreter();
155  if (script_interp) {
156  bool script_error;
157  run_state = script_interp->ScriptedThreadPlanGetRunState(
158  m_implementation_sp, script_error);
159  }
160  }
161  return run_state;
162 }
163 
164 // The ones below are not currently exported to Python.
165 
167  // For now Python plans run all threads, but we should add some controls for
168  // this.
169  return false;
170 }
171 
173  s->Printf("Python thread plan implemented by class %s.",
174  m_class_name.c_str());
175 }
176 
179  LLDB_LOGF(log, "%s called on Python Thread Plan: %s )", LLVM_PRETTY_FUNCTION,
180  m_class_name.c_str());
181  return true;
182 }
void SetOkayToDiscard(bool value)
Definition: ThreadPlan.h:444
virtual lldb::StateType ScriptedThreadPlanGetRunState(StructuredData::ObjectSP implementor_sp, bool &script_error)
virtual StructuredData::ObjectSP CreateScriptedThreadPlan(const char *class_name, StructuredDataImpl *args_data, std::string &error_str, lldb::ThreadPlanSP thread_plan_sp)
A class that represents a running process on the host machine.
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
virtual bool ScriptedThreadPlanExplainsStop(StructuredData::ObjectSP implementor_sp, Event *event, bool &script_error)
ScriptInterpreter * GetScriptInterpreter()
ScriptInterpreter * GetScriptInterpreter(bool can_create=true, llvm::Optional< lldb::ScriptLanguage > language={})
Definition: Debugger.cpp:1187
virtual bool ScriptedThreadPlanIsStale(StructuredData::ObjectSP implementor_sp, bool &script_error)
StructuredData::ObjectSP m_implementation_sp
bool ShouldStop(Event *event_ptr) override
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
StructuredDataImpl * m_args_data
virtual bool ScriptedThreadPlanShouldStop(StructuredData::ObjectSP implementor_sp, Event *event, bool &script_error)
static llvm::raw_ostream & error(Stream &strm)
Log * GetLogIfAllCategoriesSet(uint32_t mask)
Definition: Logging.cpp:58
#define LIBLLDB_LOG_THREAD
Definition: Logging.h:16
StateType
Process and Thread States.
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
bool ValidatePlan(Stream *error) override
Returns whether this plan could be successfully created.
void SetPlanComplete(bool success=true)
Definition: ThreadPlan.cpp:65
#define LLDB_LOGF(log,...)
Definition: Log.h:249
Target & GetTarget()
Get the target object pointer for this module.
Definition: Process.h:1210
Definition: SBAddress.h:15
bool DoPlanExplainsStop(Event *event_ptr) override
void GetDescription(Stream *s, lldb::DescriptionLevel level) override
Print a description of this thread to the stream s.
bool SetIsMasterPlan(bool value)
Definition: ThreadPlan.h:436
void SetPrivate(bool input)
Definition: ThreadPlan.h:457
lldb::StateType GetPlanRunState() override
Debugger & GetDebugger()
Definition: Target.h:975
Process or thread is running and can&#39;t be examined.