LLDB mainline
ScriptInterpreterPythonImpl.h
Go to the documentation of this file.
1//===-- ScriptInterpreterPythonImpl.h ---------------------------*- 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#ifndef LLDB_SOURCE_PLUGINS_SCRIPTINTERPRETER_PYTHON_SCRIPTINTERPRETERPYTHONIMPL_H
10#define LLDB_SOURCE_PLUGINS_SCRIPTINTERPRETER_PYTHON_SCRIPTINTERPRETERPYTHONIMPL_H
11
12#include "lldb-python.h"
13
14#include "PythonDataObjects.h"
16
17#include "lldb/Host/Terminal.h"
19
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/StringRef.h"
22
23namespace lldb_private {
26public:
28
30
32
33 bool Interrupt() override;
34
35 bool ExecuteOneLine(
36 llvm::StringRef command, CommandReturnObject *result,
37 const ExecuteScriptOptions &options = ExecuteScriptOptions()) override;
38
39 void ExecuteInterpreterLoop() override;
40
42 llvm::StringRef in_string,
43 ScriptInterpreter::ScriptReturnType return_type, void *ret_value,
44 const ExecuteScriptOptions &options = ExecuteScriptOptions()) override;
45
47 const char *in_string,
48 const ExecuteScriptOptions &options = ExecuteScriptOptions()) override;
49
50 Status
52
53 bool GenerateTypeScriptFunction(StringList &input, std::string &output,
54 const void *name_token = nullptr) override;
55
56 bool GenerateTypeSynthClass(StringList &input, std::string &output,
57 const void *name_token = nullptr) override;
58
59 bool GenerateTypeSynthClass(const char *oneliner, std::string &output,
60 const void *name_token = nullptr) override;
61
62 // use this if the function code is just a one-liner script
63 bool GenerateTypeScriptFunction(const char *oneliner, std::string &output,
64 const void *name_token = nullptr) override;
65
67 std::string &output) override;
68
70 CreateSyntheticScriptedProvider(const char *class_name,
71 lldb::ValueObjectSP valobj) override;
72
74 CreateScriptCommandObject(const char *class_name) override;
75
78
80 CreateFrameRecognizer(const char *class_name) override;
81
84 lldb::StackFrameSP frame_sp) override;
85
86 bool ShouldHide(const StructuredData::ObjectSP &implementor,
87 lldb::StackFrameSP frame_sp) override;
88
90
92
94
97
99
101
104
107
109
111 LoadPluginModule(const FileSpec &file_spec,
112 lldb_private::Status &error) override;
113
115 GetDynamicSettings(StructuredData::ObjectSP plugin_module_sp, Target *target,
116 const char *setting_name,
117 lldb_private::Status &error) override;
118
119 size_t CalculateNumChildren(const StructuredData::ObjectSP &implementor,
120 uint32_t max) override;
121
123 GetChildAtIndex(const StructuredData::ObjectSP &implementor,
124 uint32_t idx) override;
125
126 llvm::Expected<uint32_t>
128 const char *child_name) override;
129
131 const StructuredData::ObjectSP &implementor) override;
132
134 const StructuredData::ObjectSP &implementor) override;
135
137 GetSyntheticValue(const StructuredData::ObjectSP &implementor) override;
138
140 GetSyntheticTypeName(const StructuredData::ObjectSP &implementor) override;
141
142 bool
143 RunScriptBasedCommand(const char *impl_function, llvm::StringRef args,
144 ScriptedCommandSynchronicity synchronicity,
146 Status &error,
147 const lldb_private::ExecutionContext &exe_ctx) override;
148
150 StructuredData::GenericSP impl_obj_sp, llvm::StringRef args,
151 ScriptedCommandSynchronicity synchronicity,
153 const lldb_private::ExecutionContext &exe_ctx) override;
154
156 StructuredData::GenericSP impl_obj_sp, Args &args,
157 ScriptedCommandSynchronicity synchronicity,
159 const lldb_private::ExecutionContext &exe_ctx) override;
160
161 std::optional<std::string>
163 Args &args) override;
164
166 StructuredData::GenericSP impl_obj_sp, std::vector<llvm::StringRef> &args,
167 size_t args_pos, size_t char_in_arg) override;
168
170 StructuredData::GenericSP impl_obj_sp, llvm::StringRef &long_options,
171 size_t char_in_arg) override;
172
173 Status GenerateFunction(const char *signature, const StringList &input,
174 bool is_callback) override;
175
177 std::string &output,
178 bool has_extra_args,
179 bool is_callback) override;
180
182 std::string &output,
183 bool is_callback) override;
184
185 bool GetScriptedSummary(const char *function_name, lldb::ValueObjectSP valobj,
186 StructuredData::ObjectSP &callee_wrapper_sp,
187 const TypeSummaryOptions &options,
188 std::string &retval) override;
189
190 bool FormatterCallbackFunction(const char *function_name,
191 lldb::TypeImplSP type_impl_sp) override;
192
193 bool GetDocumentationForItem(const char *item, std::string &dest) override;
194
196 std::string &dest) override;
197
198 uint32_t
200
202 std::string &dest) override;
203
206
209
211 ExecutionContext *exe_ctx,
212 llvm::StringRef long_option,
213 llvm::StringRef value) override;
214
216 StructuredData::GenericSP cmd_obj_sp) override;
217
218 bool CheckObjectExists(const char *name) override {
219 if (!name || !name[0])
220 return false;
221 std::string temp;
222 return GetDocumentationForItem(name, temp);
223 }
224
225 bool RunScriptFormatKeyword(const char *impl_function, Process *process,
226 std::string &output, Status &error) override;
227
228 bool RunScriptFormatKeyword(const char *impl_function, Thread *thread,
229 std::string &output, Status &error) override;
230
231 bool RunScriptFormatKeyword(const char *impl_function, Target *target,
232 std::string &output, Status &error) override;
233
234 bool RunScriptFormatKeyword(const char *impl_function, StackFrame *frame,
235 std::string &output, Status &error) override;
236
237 bool RunScriptFormatKeyword(const char *impl_function, ValueObject *value,
238 std::string &output, Status &error) override;
239
240 bool LoadScriptingModule(const char *filename,
241 const LoadScriptOptions &options,
243 StructuredData::ObjectSP *module_sp = nullptr,
244 FileSpec extra_search_dir = {},
245 lldb::TargetSP loaded_into_target_sp = {}) override;
246
247 bool IsReservedWord(const char *word) override;
248
249 std::unique_ptr<ScriptInterpreterLocker> AcquireInterpreterLock() override;
250
252 std::vector<std::reference_wrapper<BreakpointOptions>> &bp_options_vec,
253 CommandReturnObject &result) override;
254
255 void
256 CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options,
257 CommandReturnObject &result) override;
258
259 /// Set the callback body text into the callback for the breakpoint.
260 Status SetBreakpointCommandCallback(BreakpointOptions &bp_options,
261 const char *callback_body,
262 bool is_callback) override;
263
265 BreakpointOptions &bp_options, const char *function_name,
266 StructuredData::ObjectSP extra_args_sp) override;
267
268 /// This one is for deserialization:
270 BreakpointOptions &bp_options,
271 std::unique_ptr<BreakpointOptions::CommandData> &data_up) override;
272
273 Status SetBreakpointCommandCallback(BreakpointOptions &bp_options,
274 const char *command_body_text,
275 StructuredData::ObjectSP extra_args_sp,
276 bool uses_extra_args,
277 bool is_callback);
278
279 /// Set a one-liner as the callback for the watchpoint.
280 void SetWatchpointCommandCallback(WatchpointOptions *wp_options,
281 const char *user_input,
282 bool is_callback) override;
283
284 const char *GetDictionaryName() { return m_dictionary_name.c_str(); }
285
286 PyThreadState *GetThreadState() { return m_command_thread_state; }
287
288 void SetThreadState(PyThreadState *s) {
289 if (s)
291 }
292
293 // IOHandlerDelegate
294 void IOHandlerActivated(IOHandler &io_handler, bool interactive) override;
295
296 void IOHandlerInputComplete(IOHandler &io_handler,
297 std::string &data) override;
298
300
301 // PluginInterface protocol
302 llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
303
305 public:
306 enum OnEntry {
307 AcquireLock = 0x0001,
308 InitSession = 0x0002,
309 InitGlobals = 0x0004,
310 NoSTDIN = 0x0008
311 };
312
313 enum OnLeave {
314 FreeLock = 0x0001,
315 FreeAcquiredLock = 0x0002, // do not free the lock if we already held it
316 // when calling constructor
318 };
319
320 Locker(ScriptInterpreterPythonImpl *py_interpreter,
321 uint16_t on_entry = AcquireLock | InitSession,
322 uint16_t on_leave = FreeLock | TearDownSession,
323 lldb::FileSP in = nullptr, lldb::FileSP out = nullptr,
324 lldb::FileSP err = nullptr);
325
326 ~Locker() override;
327
328 private:
329 bool DoAcquireLock();
330
331 bool DoInitSession(uint16_t on_entry_flags, lldb::FileSP in,
332 lldb::FileSP out, lldb::FileSP err);
333
334 bool DoFreeLock();
335
336 bool DoTearDownSession();
337
340 PyGILState_STATE m_GILState;
341 };
342
343 static bool BreakpointCallbackFunction(void *baton,
345 lldb::user_id_t break_id,
346 lldb::user_id_t break_loc_id);
347 static bool WatchpointCallbackFunction(void *baton,
349 lldb::user_id_t watch_id);
350 static void Initialize();
351
363
364 enum class AddLocation { Beginning, End };
365
366 static void AddToSysPath(AddLocation location, std::string path);
367
368 bool EnterSession(uint16_t on_entry_flags, lldb::FileSP in, lldb::FileSP out,
369 lldb::FileSP err);
370
371 void LeaveSession();
372
373 uint32_t IsExecutingPython() {
374 std::lock_guard<std::mutex> guard(m_mutex);
375 return m_lock_count > 0;
376 }
377
379 std::lock_guard<std::mutex> guard(m_mutex);
380 return ++m_lock_count;
381 }
382
384 std::lock_guard<std::mutex> guard(m_mutex);
385 if (m_lock_count > 0)
386 --m_lock_count;
387 return m_lock_count;
388 }
389
395
397
399
401
402 llvm::Expected<unsigned> GetMaxPositionalArgumentsForCallable(
403 const llvm::StringRef &callable_name) override;
404
406
407 bool SetStdHandle(lldb::FileSP file, const char *py_name,
408 python::PythonObject &save_file, const char *mode);
409
418 std::string m_dictionary_name;
423 uint32_t m_lock_count;
424 std::mutex m_mutex;
426};
427
429public:
434
435 ~IOHandlerPythonInterpreter() override = default;
436
437 llvm::StringRef GetControlSequence(char ch) override {
438 static constexpr llvm::StringLiteral control_sequence("quit()\n");
439 if (ch == 'd')
440 return control_sequence;
441 return {};
442 }
443
444 void Run() override {
445 if (m_python) {
446 int stdin_fd = GetInputFD();
447 if (stdin_fd >= 0) {
448 Terminal terminal(stdin_fd);
449 TerminalState terminal_state(terminal);
450
451 if (terminal.IsATerminal()) {
452 // FIXME: error handling?
453 llvm::consumeError(terminal.SetCanonical(false));
454 llvm::consumeError(terminal.SetEcho(true));
455 }
456
458 m_python,
464
465 // The following call drops into the embedded interpreter loop and
466 // stays there until the user chooses to exit from the Python
467 // interpreter. This embedded interpreter will, as any Python code that
468 // performs I/O, unlock the GIL before a system call that can hang, and
469 // lock it when the syscall has returned.
470
471 // We need to surround the call to the embedded interpreter with calls
472 // to PyGILState_Ensure and PyGILState_Release (using the Locker
473 // above). This is because Python has a global lock which must be held
474 // whenever we want to touch any Python objects. Otherwise, if the user
475 // calls Python code, the interpreter state will be off, and things
476 // could hang (it's happened before).
477
478 StreamString run_string;
479 run_string.Printf("run_python_interpreter (%s)",
480 m_python->GetDictionaryName());
481 python::RunSimpleString(run_string.GetData());
482 }
483 }
484 SetIsDone(true);
485 }
486
487 void Cancel() override {}
488
489 bool Interrupt() override { return m_python->Interrupt(); }
490
491 void GotEOF() override {}
492
493protected:
495};
496
497} // namespace lldb_private
498
499#endif // LLDB_SOURCE_PLUGINS_SCRIPTINTERPRETER_PYTHON_SCRIPTINTERPRETERPYTHONIMPL_H
static llvm::raw_ostream & error(Stream &strm)
ScriptInterpreterPythonImpl::Locker Locker
A command line argument class.
Definition Args.h:33
A uniqued constant string class.
Definition ConstString.h:40
A class to manage flag bits.
Definition Debugger.h:100
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
A file utility class.
Definition FileSpec.h:57
~IOHandlerPythonInterpreter() override=default
llvm::StringRef GetControlSequence(char ch) override
IOHandlerPythonInterpreter(Debugger &debugger, ScriptInterpreterPythonImpl *python)
IOHandler(Debugger &debugger, IOHandler::Type type)
Definition IOHandler.cpp:55
void SetIsDone(bool b)
Definition IOHandler.h:81
A plug-in interface definition class for debugging a process.
Definition Process.h:355
bool DoInitSession(uint16_t on_entry_flags, lldb::FileSP in, lldb::FileSP out, lldb::FileSP err)
Locker(ScriptInterpreterPythonImpl *py_interpreter, uint16_t on_entry=AcquireLock|InitSession, uint16_t on_leave=FreeLock|TearDownSession, lldb::FileSP in=nullptr, lldb::FileSP out=nullptr, lldb::FileSP err=nullptr)
SynchronicityHandler(lldb::DebuggerSP, ScriptedCommandSynchronicity)
bool GenerateTypeScriptFunction(StringList &input, std::string &output, const void *name_token=nullptr) override
Status GenerateFunction(const char *signature, const StringList &input, bool is_callback) override
bool GenerateScriptAliasFunction(StringList &input, std::string &output) override
lldb_private::Status ExecuteMultipleLines(const char *in_string, const ExecuteScriptOptions &options=ExecuteScriptOptions()) override
bool ShouldHide(const StructuredData::ObjectSP &implementor, lldb::StackFrameSP frame_sp) override
bool GenerateWatchpointCommandCallbackData(StringList &input, std::string &output, bool is_callback) override
StructuredData::DictionarySP HandleOptionArgumentCompletionForScriptedCommand(StructuredData::GenericSP impl_obj_sp, llvm::StringRef &long_options, size_t char_in_arg) override
bool RunScriptBasedParsedCommand(StructuredData::GenericSP impl_obj_sp, Args &args, ScriptedCommandSynchronicity synchronicity, lldb_private::CommandReturnObject &cmd_retobj, Status &error, const lldb_private::ExecutionContext &exe_ctx) override
void OptionParsingStartedForCommandObject(StructuredData::GenericSP cmd_obj_sp) override
bool GetLongHelpForCommandObject(StructuredData::GenericSP cmd_obj_sp, std::string &dest) override
lldb::ScriptedStopHookInterfaceSP CreateScriptedStopHookInterface() override
Status SetBreakpointCommandCallbackFunction(BreakpointOptions &bp_options, const char *function_name, StructuredData::ObjectSP extra_args_sp) override
Set a script function as the callback for the breakpoint.
lldb::ScriptedThreadInterfaceSP CreateScriptedThreadInterface() override
static bool BreakpointCallbackFunction(void *baton, StoppointCallbackContext *context, lldb::user_id_t break_id, lldb::user_id_t break_loc_id)
StructuredData::DictionarySP GetDynamicSettings(StructuredData::ObjectSP plugin_module_sp, Target *target, const char *setting_name, lldb_private::Status &error) override
void CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options, CommandReturnObject &result) override
StructuredData::DictionarySP HandleArgumentCompletionForScriptedCommand(StructuredData::GenericSP impl_obj_sp, std::vector< llvm::StringRef > &args, size_t args_pos, size_t char_in_arg) override
bool RunScriptBasedCommand(const char *impl_function, llvm::StringRef args, ScriptedCommandSynchronicity synchronicity, lldb_private::CommandReturnObject &cmd_retobj, Status &error, const lldb_private::ExecutionContext &exe_ctx) override
lldb::ScriptedFrameProviderInterfaceSP CreateScriptedFrameProviderInterface() override
std::optional< std::string > GetRepeatCommandForScriptedCommand(StructuredData::GenericSP impl_obj_sp, Args &args) override
Status SetBreakpointCommandCallback(BreakpointOptions &bp_options, const char *callback_body, bool is_callback) override
Set the callback body text into the callback for the breakpoint.
StructuredData::ObjectSP GetArgumentsForCommandObject(StructuredData::GenericSP cmd_obj_sp) override
bool EnterSession(uint16_t on_entry_flags, lldb::FileSP in, lldb::FileSP out, lldb::FileSP err)
void SetWatchpointCommandCallback(WatchpointOptions *wp_options, const char *user_input, bool is_callback) override
Set a one-liner as the callback for the watchpoint.
lldb::ValueObjectSP GetSyntheticValue(const StructuredData::ObjectSP &implementor) override
std::unique_ptr< ScriptInterpreterLocker > AcquireInterpreterLock() override
void CollectDataForBreakpointCommandCallback(std::vector< std::reference_wrapper< BreakpointOptions > > &bp_options_vec, CommandReturnObject &result) override
bool UpdateSynthProviderInstance(const StructuredData::ObjectSP &implementor) override
static void AddToSysPath(AddLocation location, std::string path)
bool MightHaveChildrenSynthProviderInstance(const StructuredData::ObjectSP &implementor) override
bool LoadScriptingModule(const char *filename, const LoadScriptOptions &options, lldb_private::Status &error, StructuredData::ObjectSP *module_sp=nullptr, FileSpec extra_search_dir={}, lldb::TargetSP loaded_into_target_sp={}) override
StructuredData::GenericSP CreateFrameRecognizer(const char *class_name) override
ConstString GetSyntheticTypeName(const StructuredData::ObjectSP &implementor) override
Status GenerateBreakpointCommandCallbackData(StringList &input, std::string &output, bool has_extra_args, bool is_callback) override
lldb::OperatingSystemInterfaceSP CreateOperatingSystemInterface() override
bool SetOptionValueForCommandObject(StructuredData::GenericSP cmd_obj_sp, ExecutionContext *exe_ctx, llvm::StringRef long_option, llvm::StringRef value) override
llvm::Expected< uint32_t > GetIndexOfChildWithName(const StructuredData::ObjectSP &implementor, const char *child_name) override
bool FormatterCallbackFunction(const char *function_name, lldb::TypeImplSP type_impl_sp) override
Status ExportFunctionDefinitionToInterpreter(StringList &function_def) override
StructuredData::ObjectSP CreateSyntheticScriptedProvider(const char *class_name, lldb::ValueObjectSP valobj) override
bool ExecuteOneLine(llvm::StringRef command, CommandReturnObject *result, const ExecuteScriptOptions &options=ExecuteScriptOptions()) override
bool GetDocumentationForItem(const char *item, std::string &dest) override
In Python, a special attribute doc contains the docstring for an object (function,...
lldb::ScriptedHookInterfaceSP CreateScriptedHookInterface() override
uint32_t GetFlagsForCommandObject(StructuredData::GenericSP cmd_obj_sp) override
void IOHandlerInputComplete(IOHandler &io_handler, std::string &data) override
Called when a line or lines have been retrieved.
void IOHandlerActivated(IOHandler &io_handler, bool interactive) override
bool GetScriptedSummary(const char *function_name, lldb::ValueObjectSP valobj, StructuredData::ObjectSP &callee_wrapper_sp, const TypeSummaryOptions &options, std::string &retval) override
lldb::ScriptedFrameInterfaceSP CreateScriptedFrameInterface() override
lldb::ScriptedProcessInterfaceUP CreateScriptedProcessInterface() override
StructuredData::GenericSP CreateScriptCommandObject(const char *class_name) override
size_t CalculateNumChildren(const StructuredData::ObjectSP &implementor, uint32_t max) override
bool ExecuteOneLineWithReturn(llvm::StringRef in_string, ScriptInterpreter::ScriptReturnType return_type, void *ret_value, const ExecuteScriptOptions &options=ExecuteScriptOptions()) override
lldb::ScriptedBreakpointInterfaceSP CreateScriptedBreakpointInterface() override
bool RunScriptFormatKeyword(const char *impl_function, Process *process, std::string &output, Status &error) override
bool SetStdHandle(lldb::FileSP file, const char *py_name, python::PythonObject &save_file, const char *mode)
lldb::ValueObjectSP GetChildAtIndex(const StructuredData::ObjectSP &implementor, uint32_t idx) override
bool GenerateTypeSynthClass(StringList &input, std::string &output, const void *name_token=nullptr) override
StructuredData::ObjectSP CreateStructuredDataFromScriptObject(ScriptObject obj) override
StructuredData::ObjectSP GetOptionsForCommandObject(StructuredData::GenericSP cmd_obj_sp) override
StructuredData::ObjectSP LoadPluginModule(const FileSpec &file_spec, lldb_private::Status &error) override
bool GetShortHelpForCommandObject(StructuredData::GenericSP cmd_obj_sp, std::string &dest) override
llvm::Expected< unsigned > GetMaxPositionalArgumentsForCallable(const llvm::StringRef &callable_name) override
static bool WatchpointCallbackFunction(void *baton, StoppointCallbackContext *context, lldb::user_id_t watch_id)
static lldb::ScriptInterpreterSP CreateInstance(Debugger &debugger)
lldb::ValueObjectListSP GetRecognizedArguments(const StructuredData::ObjectSP &implementor, lldb::StackFrameSP frame_sp) override
lldb::ScriptedThreadPlanInterfaceSP CreateScriptedThreadPlanInterface() override
This base class provides an interface to stack frames.
Definition StackFrame.h:44
An error handling class.
Definition Status.h:118
General Outline: When we hit a breakpoint we need to package up whatever information is needed to eva...
const char * GetData() const
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:132
std::shared_ptr< Generic > GenericSP
std::shared_ptr< Dictionary > DictionarySP
std::shared_ptr< Object > ObjectSP
A RAII-friendly terminal state saving/restoring class.
Definition Terminal.h:99
llvm::Error SetEcho(bool enabled)
Definition Terminal.cpp:80
llvm::Error SetCanonical(bool enabled)
Definition Terminal.cpp:96
bool IsATerminal() const
Definition Terminal.cpp:35
int RunSimpleString(const char *str)
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::ScriptedHookInterface > ScriptedHookInterfaceSP
std::shared_ptr< lldb_private::ScriptedStopHookInterface > ScriptedStopHookInterfaceSP
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
std::shared_ptr< lldb_private::ScriptInterpreter > ScriptInterpreterSP
std::shared_ptr< lldb_private::ScriptedThreadPlanInterface > ScriptedThreadPlanInterfaceSP
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
std::shared_ptr< lldb_private::OperatingSystemInterface > OperatingSystemInterfaceSP
std::shared_ptr< lldb_private::ScriptedBreakpointInterface > ScriptedBreakpointInterfaceSP
std::shared_ptr< lldb_private::ScriptedThreadInterface > ScriptedThreadInterfaceSP
std::shared_ptr< lldb_private::ValueObjectList > ValueObjectListSP
std::shared_ptr< lldb_private::Debugger > DebuggerSP
std::shared_ptr< lldb_private::ScriptedFrameProviderInterface > ScriptedFrameProviderInterfaceSP
uint64_t user_id_t
Definition lldb-types.h:82
std::shared_ptr< lldb_private::TypeImpl > TypeImplSP
std::shared_ptr< lldb_private::Target > TargetSP
std::shared_ptr< lldb_private::File > FileSP
std::unique_ptr< lldb_private::ScriptedProcessInterface > ScriptedProcessInterfaceUP
std::shared_ptr< lldb_private::ScriptedFrameInterface > ScriptedFrameInterfaceSP