[lldb][NFC] Fix all formatting errors in .cpp file headers
Summary:
A *.cpp file header in LLDB (and in LLDB) should like this:
```
//===-- TestUtilities.cpp -------------------------------------------------===//
```
However in LLDB most of our source files have arbitrary changes to this format and
these changes are spreading through LLDB as folks usually just use the existing
source files as templates for their new files (most notably the unnecessary
editor language indicator `-*- C++ -*-` is spreading and in every review
someone is pointing out that this is wrong, resulting in people pointing out that this
is done in the same way in other files).
This patch removes most of these inconsistencies including the editor language indicators,
all the different missing/additional '-' characters, files that center the file name, missing
trailing `===//` (mostly caused by clang-format breaking the line).
Reviewers: aprantl, espindola, jfb, shafik, JDevlieghere
Reviewed By: JDevlieghere
Subscribers: dexonsmith, wuzish, emaste, sdardis, nemanjai, kbarton, MaskRay, atanasyan, arphaman, jfb, abidh, jsji, JDevlieghere, usaxena95, lldb-commits
Tags: #lldb
Differential Revision: https://reviews.llvm.org/D73258
2020-01-24 08:23:27 +01:00
|
|
|
//===-- ScriptInterpreter.cpp ---------------------------------------------===//
|
2010-06-08 16:52:24 +00:00
|
|
|
//
|
2019-01-19 08:50:56 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-06-08 16:52:24 +00:00
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
#include "lldb/Interpreter/ScriptInterpreter.h"
|
2020-06-25 09:43:17 -07:00
|
|
|
#include "lldb/Core/Debugger.h"
|
|
|
|
|
#include "lldb/Host/ConnectionFileDescriptor.h"
|
|
|
|
|
#include "lldb/Host/Pipe.h"
|
2017-02-16 19:38:21 +00:00
|
|
|
#include "lldb/Host/PseudoTerminal.h"
|
2010-06-08 16:52:24 +00:00
|
|
|
#include "lldb/Interpreter/CommandReturnObject.h"
|
2017-05-12 04:51:55 +00:00
|
|
|
#include "lldb/Utility/Status.h"
|
2017-02-02 21:39:50 +00:00
|
|
|
#include "lldb/Utility/Stream.h"
|
2017-03-21 18:25:04 +00:00
|
|
|
#include "lldb/Utility/StringList.h"
|
2020-06-25 12:19:00 -07:00
|
|
|
#if defined(_WIN32)
|
|
|
|
|
#include "lldb/Host/windows/ConnectionGenericFileWindows.h"
|
|
|
|
|
#endif
|
2021-05-26 12:19:37 +02:00
|
|
|
#include <cstdio>
|
|
|
|
|
#include <cstdlib>
|
2020-06-25 12:19:00 -07:00
|
|
|
#include <memory>
|
2023-01-07 13:43:00 -08:00
|
|
|
#include <optional>
|
2020-06-25 12:19:00 -07:00
|
|
|
#include <string>
|
2010-06-08 16:52:24 +00:00
|
|
|
|
|
|
|
|
using namespace lldb;
|
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
|
2023-11-07 09:56:22 -08:00
|
|
|
ScriptInterpreter::ScriptInterpreter(Debugger &debugger,
|
|
|
|
|
lldb::ScriptLanguage script_lang)
|
|
|
|
|
: m_debugger(debugger), m_script_lang(script_lang) {}
|
2010-06-08 16:52:24 +00:00
|
|
|
|
|
|
|
|
void ScriptInterpreter::CollectDataForBreakpointCommandCallback(
|
2021-06-11 17:00:46 -07:00
|
|
|
std::vector<std::reference_wrapper<BreakpointOptions>> &bp_options_vec,
|
2010-06-08 16:52:24 +00:00
|
|
|
CommandReturnObject &result) {
|
|
|
|
|
result.AppendError(
|
2019-12-21 22:33:01 -08:00
|
|
|
"This script interpreter does not support breakpoint callbacks.");
|
2010-06-08 16:52:24 +00:00
|
|
|
}
|
|
|
|
|
|
2012-08-09 23:09:42 +00:00
|
|
|
void ScriptInterpreter::CollectDataForWatchpointCommandCallback(
|
|
|
|
|
WatchpointOptions *bp_options, CommandReturnObject &result) {
|
|
|
|
|
result.AppendError(
|
2019-12-21 22:33:01 -08:00
|
|
|
"This script interpreter does not support watchpoint callbacks.");
|
2012-08-09 23:09:42 +00:00
|
|
|
}
|
|
|
|
|
|
[lldb] make it easier to find LLDB's python
It is surprisingly difficult to write a simple python script that
can reliably `import lldb` without failing, or crashing. I'm
currently resorting to convolutions like this:
def find_lldb(may_reexec=False):
if prefix := os.environ.get('LLDB_PYTHON_PREFIX'):
if os.path.realpath(prefix) != os.path.realpath(sys.prefix):
raise Exception("cannot import lldb.\n"
f" sys.prefix should be: {prefix}\n"
f" but it is: {sys.prefix}")
else:
line1, line2 = subprocess.run(
['lldb', '-x', '-b', '-o', 'script print(sys.prefix)'],
encoding='utf8', stdout=subprocess.PIPE,
check=True).stdout.strip().splitlines()
assert line1.strip() == '(lldb) script print(sys.prefix)'
prefix = line2.strip()
os.environ['LLDB_PYTHON_PREFIX'] = prefix
if sys.prefix != prefix:
if not may_reexec:
raise Exception(
"cannot import lldb.\n" +
f" This python, at {sys.prefix}\n"
f" does not math LLDB's python at {prefix}")
os.environ['LLDB_PYTHON_PREFIX'] = prefix
python_exe = os.path.join(prefix, 'bin', 'python3')
os.execl(python_exe, python_exe, *sys.argv)
lldb_path = subprocess.run(['lldb', '-P'],
check=True, stdout=subprocess.PIPE,
encoding='utf8').stdout.strip()
sys.path = [lldb_path] + sys.path
This patch aims to replace all that with:
#!/usr/bin/env lldb-python
import lldb
...
... by adding the following features:
* new command line option: --print-script-interpreter-info. This
prints language-specific information about the script interpreter
in JSON format.
* new tool (unix only): lldb-python which finds python and exec's it.
Reviewed By: JDevlieghere
Differential Revision: https://reviews.llvm.org/D112973
2021-11-10 10:33:33 -08:00
|
|
|
StructuredData::DictionarySP ScriptInterpreter::GetInterpreterInfo() {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-27 09:14:40 -07:00
|
|
|
bool ScriptInterpreter::LoadScriptingModule(const char *filename,
|
[lldb] Add the ability to silently import scripted commands
Add the ability to silence command script import. The motivation for
this change is being able to add command script import -s
lldb.macosx.crashlog to your ~/.lldbinit without it printing the
following message at the beginning of every debug session.
"malloc_info", "ptr_refs", "cstr_refs", "find_variable", and
"objc_refs" commands have been installed, use the "--help" options on
these commands for detailed help.
In addition to forwarding the silent option to LoadScriptingModule, this
also changes ScriptInterpreterPythonImpl::ExecuteOneLineWithReturn and
ScriptInterpreterPythonImpl::ExecuteMultipleLines to honor the enable IO
option in ExecuteScriptOptions, which until now was ignored.
Note that IO is only enabled (or disabled) at the start of a session,
and for this particular use case, that's done when taking the Python
lock in LoadScriptingModule, which means that the changes to these two
functions are not strictly necessary, but (IMO) desirable nonetheless.
Differential revision: https://reviews.llvm.org/D105327
2021-07-09 09:23:54 -07:00
|
|
|
const LoadScriptOptions &options,
|
2020-10-27 09:14:40 -07:00
|
|
|
lldb_private::Status &error,
|
|
|
|
|
StructuredData::ObjectSP *module_sp,
|
|
|
|
|
FileSpec extra_search_dir) {
|
2019-12-22 16:46:01 -08:00
|
|
|
error.SetErrorString(
|
|
|
|
|
"This script interpreter does not support importing modules.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-04 00:03:46 +00:00
|
|
|
std::string ScriptInterpreter::LanguageToString(lldb::ScriptLanguage language) {
|
|
|
|
|
switch (language) {
|
|
|
|
|
case eScriptLanguageNone:
|
2019-12-21 21:54:44 -08:00
|
|
|
return "None";
|
2010-09-04 00:03:46 +00:00
|
|
|
case eScriptLanguagePython:
|
2019-12-21 21:54:44 -08:00
|
|
|
return "Python";
|
|
|
|
|
case eScriptLanguageLua:
|
|
|
|
|
return "Lua";
|
2016-09-26 19:47:37 +00:00
|
|
|
case eScriptLanguageUnknown:
|
2019-12-21 21:54:44 -08:00
|
|
|
return "Unknown";
|
2010-09-04 00:03:46 +00:00
|
|
|
}
|
2019-12-23 11:06:50 +01:00
|
|
|
llvm_unreachable("Unhandled ScriptInterpreter!");
|
2010-09-04 00:03:46 +00:00
|
|
|
}
|
2011-01-14 00:29:16 +00:00
|
|
|
|
2021-03-23 16:22:18 +00:00
|
|
|
lldb::DataExtractorSP
|
|
|
|
|
ScriptInterpreter::GetDataExtractorFromSBData(const lldb::SBData &data) const {
|
|
|
|
|
return data.m_opaque_sp;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-25 15:03:15 -07:00
|
|
|
lldb::BreakpointSP ScriptInterpreter::GetOpaqueTypeFromSBBreakpoint(
|
|
|
|
|
const lldb::SBBreakpoint &breakpoint) const {
|
|
|
|
|
return breakpoint.m_opaque_wp.lock();
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-03 15:17:59 -08:00
|
|
|
lldb::ProcessAttachInfoSP ScriptInterpreter::GetOpaqueTypeFromSBAttachInfo(
|
|
|
|
|
const lldb::SBAttachInfo &attach_info) const {
|
|
|
|
|
return attach_info.m_opaque_sp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lldb::ProcessLaunchInfoSP ScriptInterpreter::GetOpaqueTypeFromSBLaunchInfo(
|
|
|
|
|
const lldb::SBLaunchInfo &launch_info) const {
|
|
|
|
|
return std::make_shared<ProcessLaunchInfo>(
|
|
|
|
|
*reinterpret_cast<ProcessLaunchInfo *>(launch_info.m_opaque_sp.get()));
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 16:22:18 +00:00
|
|
|
Status
|
|
|
|
|
ScriptInterpreter::GetStatusFromSBError(const lldb::SBError &error) const {
|
|
|
|
|
if (error.m_opaque_up)
|
2023-01-04 22:05:40 -08:00
|
|
|
return *error.m_opaque_up;
|
2021-03-23 16:22:18 +00:00
|
|
|
|
|
|
|
|
return Status();
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-27 01:45:30 -07:00
|
|
|
Event *
|
|
|
|
|
ScriptInterpreter::GetOpaqueTypeFromSBEvent(const lldb::SBEvent &event) const {
|
|
|
|
|
return event.m_opaque_ptr;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-28 11:53:19 -07:00
|
|
|
lldb::StreamSP ScriptInterpreter::GetOpaqueTypeFromSBStream(
|
2024-06-27 01:45:30 -07:00
|
|
|
const lldb::SBStream &stream) const {
|
2024-06-28 11:53:19 -07:00
|
|
|
if (stream.m_opaque_up) {
|
|
|
|
|
lldb::StreamSP s = std::make_shared<lldb_private::StreamString>();
|
|
|
|
|
*s << reinterpret_cast<StreamString *>(stream.m_opaque_up.get())->m_packet;
|
|
|
|
|
return s;
|
|
|
|
|
}
|
2024-06-27 01:45:30 -07:00
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-07 14:18:35 -08:00
|
|
|
std::optional<MemoryRegionInfo>
|
[lldb/Plugins] Add memory region support in ScriptedProcess
This patch adds support for memory regions in Scripted Processes.
This is necessary to read the stack memory region in order to
reconstruct each stackframe of the program.
In order to do so, this patch makes some changes to the SBAPI, namely:
- Add a new constructor for `SBMemoryRegionInfo` that takes arguments
such as the memory region name, address range, permissions ...
This is used when reading memory at some address to compute the offset
in the binary blob provided by the user.
- Add a `GetMemoryRegionContainingAddress` method to `SBMemoryRegionInfoList`
to simplify the access to a specific memory region.
With these changes, lldb is now able to unwind the stack and reconstruct
each frame. On top of that, reloading the target module at offset 0 allows
lldb to symbolicate the `ScriptedProcess` using debug info, similarly to an
ordinary Process.
To test this, I wrote a simple program with multiple function calls, ran it in
lldb, stopped at a leaf function and read the registers values and copied
the stack memory into a binary file. These are then used in the python script.
Differential Revision: https://reviews.llvm.org/D108953
Signed-off-by: Med Ismail Bennani <medismail.bennani@gmail.com>
2021-10-08 12:25:04 +00:00
|
|
|
ScriptInterpreter::GetOpaqueTypeFromSBMemoryRegionInfo(
|
|
|
|
|
const lldb::SBMemoryRegionInfo &mem_region) const {
|
|
|
|
|
if (!mem_region.m_opaque_up)
|
2022-12-04 16:51:25 -08:00
|
|
|
return std::nullopt;
|
[lldb/Plugins] Add memory region support in ScriptedProcess
This patch adds support for memory regions in Scripted Processes.
This is necessary to read the stack memory region in order to
reconstruct each stackframe of the program.
In order to do so, this patch makes some changes to the SBAPI, namely:
- Add a new constructor for `SBMemoryRegionInfo` that takes arguments
such as the memory region name, address range, permissions ...
This is used when reading memory at some address to compute the offset
in the binary blob provided by the user.
- Add a `GetMemoryRegionContainingAddress` method to `SBMemoryRegionInfoList`
to simplify the access to a specific memory region.
With these changes, lldb is now able to unwind the stack and reconstruct
each frame. On top of that, reloading the target module at offset 0 allows
lldb to symbolicate the `ScriptedProcess` using debug info, similarly to an
ordinary Process.
To test this, I wrote a simple program with multiple function calls, ran it in
lldb, stopped at a leaf function and read the registers values and copied
the stack memory into a binary file. These are then used in the python script.
Differential Revision: https://reviews.llvm.org/D108953
Signed-off-by: Med Ismail Bennani <medismail.bennani@gmail.com>
2021-10-08 12:25:04 +00:00
|
|
|
return *mem_region.m_opaque_up.get();
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-26 19:47:37 +00:00
|
|
|
lldb::ScriptLanguage
|
|
|
|
|
ScriptInterpreter::StringToLanguage(const llvm::StringRef &language) {
|
2021-06-24 11:06:42 +03:00
|
|
|
if (language.equals_insensitive(LanguageToString(eScriptLanguageNone)))
|
2016-09-26 19:47:37 +00:00
|
|
|
return eScriptLanguageNone;
|
2021-06-24 11:06:42 +03:00
|
|
|
if (language.equals_insensitive(LanguageToString(eScriptLanguagePython)))
|
2016-09-26 19:47:37 +00:00
|
|
|
return eScriptLanguagePython;
|
2021-06-24 11:06:42 +03:00
|
|
|
if (language.equals_insensitive(LanguageToString(eScriptLanguageLua)))
|
2019-12-21 21:54:44 -08:00
|
|
|
return eScriptLanguageLua;
|
2017-09-03 19:27:56 +00:00
|
|
|
return eScriptLanguageUnknown;
|
2016-09-26 19:47:37 +00:00
|
|
|
}
|
|
|
|
|
|
2017-05-12 04:51:55 +00:00
|
|
|
Status ScriptInterpreter::SetBreakpointCommandCallback(
|
2021-06-11 17:00:46 -07:00
|
|
|
std::vector<std::reference_wrapper<BreakpointOptions>> &bp_options_vec,
|
2014-08-29 17:34:17 +00:00
|
|
|
const char *callback_text) {
|
2022-05-31 17:22:18 -07:00
|
|
|
Status error;
|
2021-06-11 17:00:46 -07:00
|
|
|
for (BreakpointOptions &bp_options : bp_options_vec) {
|
[lldb] Fix {break,watch}point command function stopping behaviour
In order to run a {break,watch}point command, lldb can resolve to the
script interpreter to run an arbitrary piece of code or call into a
user-provided function. To do so, we will generate a wrapping function,
where we first copy lldb's internal dictionary keys into the
interpreter's global dictionary, copied inline the user code before
resetting the global dictionary to its previous state.
However, {break,watch}point commands can optionally return a value that
would tell lldb whether we should stop or not. This feature was
only implemented for breakpoint commands and since we inlined the user
code directly into the wrapping function, introducing an early return,
that caused lldb to let the interpreter global dictionary tinted with the
internal dictionary keys.
This patch fixes that issue while also adding the stopping behaviour to
watchpoint commands.
To do so, this patch refactors the {break,watch}point command creation
method, to let the lldb wrapper function generator know if the user code is
a function call or a arbitrary expression.
Then the wrapper generator, if the user input was a function call, the
wrapper function will call the user function and save the return value into
a variable. If the user input was an arbitrary expression, the wrapper will
inline it into a nested function, call the nested function and save the
return value into the same variable. After resetting the interpreter global
dictionary to its previous state, the generated wrapper function will return
the varible containing the return value.
rdar://105461140
Differential Revision: https://reviews.llvm.org/D144688
Signed-off-by: Med Ismail Bennani <medismail.bennani@gmail.com>
2023-02-28 09:24:46 -08:00
|
|
|
error = SetBreakpointCommandCallback(bp_options, callback_text,
|
|
|
|
|
/*is_callback=*/false);
|
2022-05-31 17:22:18 -07:00
|
|
|
if (!error.Success())
|
2014-08-29 17:34:17 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2022-05-31 17:22:18 -07:00
|
|
|
return error;
|
2014-08-29 17:34:17 +00:00
|
|
|
}
|
|
|
|
|
|
2019-10-25 14:05:07 -07:00
|
|
|
Status ScriptInterpreter::SetBreakpointCommandCallbackFunction(
|
2021-06-11 17:00:46 -07:00
|
|
|
std::vector<std::reference_wrapper<BreakpointOptions>> &bp_options_vec,
|
|
|
|
|
const char *function_name, StructuredData::ObjectSP extra_args_sp) {
|
2019-10-25 14:05:07 -07:00
|
|
|
Status error;
|
2021-06-11 17:00:46 -07:00
|
|
|
for (BreakpointOptions &bp_options : bp_options_vec) {
|
2019-12-21 21:54:44 -08:00
|
|
|
error = SetBreakpointCommandCallbackFunction(bp_options, function_name,
|
|
|
|
|
extra_args_sp);
|
2019-10-25 14:05:07 -07:00
|
|
|
if (!error.Success())
|
|
|
|
|
return error;
|
2014-08-29 17:34:17 +00:00
|
|
|
}
|
2019-10-25 14:05:07 -07:00
|
|
|
return error;
|
2014-08-29 17:34:17 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-18 22:45:39 +00:00
|
|
|
std::unique_ptr<ScriptInterpreterLocker>
|
2013-03-27 22:38:11 +00:00
|
|
|
ScriptInterpreter::AcquireInterpreterLock() {
|
2020-06-24 17:44:33 -07:00
|
|
|
return std::make_unique<ScriptInterpreterLocker>();
|
2013-03-27 22:38:11 +00:00
|
|
|
}
|
2020-06-25 09:43:17 -07:00
|
|
|
|
|
|
|
|
static void ReadThreadBytesReceived(void *baton, const void *src,
|
|
|
|
|
size_t src_len) {
|
|
|
|
|
if (src && src_len) {
|
|
|
|
|
Stream *strm = (Stream *)baton;
|
|
|
|
|
strm->Write(src, src_len);
|
|
|
|
|
strm->Flush();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
llvm::Expected<std::unique_ptr<ScriptInterpreterIORedirect>>
|
2020-06-25 09:51:55 -07:00
|
|
|
ScriptInterpreterIORedirect::Create(bool enable_io, Debugger &debugger,
|
|
|
|
|
CommandReturnObject *result) {
|
|
|
|
|
if (enable_io)
|
|
|
|
|
return std::unique_ptr<ScriptInterpreterIORedirect>(
|
|
|
|
|
new ScriptInterpreterIORedirect(debugger, result));
|
|
|
|
|
|
2020-06-25 09:43:17 -07:00
|
|
|
auto nullin = FileSystem::Instance().Open(FileSpec(FileSystem::DEV_NULL),
|
2021-07-28 20:07:03 +02:00
|
|
|
File::eOpenOptionReadOnly);
|
2020-06-25 09:43:17 -07:00
|
|
|
if (!nullin)
|
|
|
|
|
return nullin.takeError();
|
|
|
|
|
|
|
|
|
|
auto nullout = FileSystem::Instance().Open(FileSpec(FileSystem::DEV_NULL),
|
2021-07-28 20:07:03 +02:00
|
|
|
File::eOpenOptionWriteOnly);
|
2020-06-25 09:43:17 -07:00
|
|
|
if (!nullout)
|
|
|
|
|
return nullin.takeError();
|
|
|
|
|
|
|
|
|
|
return std::unique_ptr<ScriptInterpreterIORedirect>(
|
|
|
|
|
new ScriptInterpreterIORedirect(std::move(*nullin), std::move(*nullout)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ScriptInterpreterIORedirect::ScriptInterpreterIORedirect(
|
|
|
|
|
std::unique_ptr<File> input, std::unique_ptr<File> output)
|
|
|
|
|
: m_input_file_sp(std::move(input)),
|
|
|
|
|
m_output_file_sp(std::make_shared<StreamFile>(std::move(output))),
|
|
|
|
|
m_error_file_sp(m_output_file_sp),
|
|
|
|
|
m_communication("lldb.ScriptInterpreterIORedirect.comm"),
|
|
|
|
|
m_disconnect(false) {}
|
|
|
|
|
|
|
|
|
|
ScriptInterpreterIORedirect::ScriptInterpreterIORedirect(
|
|
|
|
|
Debugger &debugger, CommandReturnObject *result)
|
|
|
|
|
: m_communication("lldb.ScriptInterpreterIORedirect.comm"),
|
|
|
|
|
m_disconnect(false) {
|
|
|
|
|
|
|
|
|
|
if (result) {
|
|
|
|
|
m_input_file_sp = debugger.GetInputFileSP();
|
|
|
|
|
|
|
|
|
|
Pipe pipe;
|
|
|
|
|
Status pipe_result = pipe.CreateNew(false);
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
|
lldb::file_t read_file = pipe.GetReadNativeHandle();
|
|
|
|
|
pipe.ReleaseReadFileDescriptor();
|
|
|
|
|
std::unique_ptr<ConnectionGenericFile> conn_up =
|
|
|
|
|
std::make_unique<ConnectionGenericFile>(read_file, true);
|
|
|
|
|
#else
|
|
|
|
|
std::unique_ptr<ConnectionFileDescriptor> conn_up =
|
|
|
|
|
std::make_unique<ConnectionFileDescriptor>(
|
|
|
|
|
pipe.ReleaseReadFileDescriptor(), true);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if (conn_up->IsConnected()) {
|
|
|
|
|
m_communication.SetConnection(std::move(conn_up));
|
|
|
|
|
m_communication.SetReadThreadBytesReceivedCallback(
|
|
|
|
|
ReadThreadBytesReceived, &result->GetOutputStream());
|
|
|
|
|
m_communication.StartReadThread();
|
|
|
|
|
m_disconnect = true;
|
|
|
|
|
|
|
|
|
|
FILE *outfile_handle = fdopen(pipe.ReleaseWriteFileDescriptor(), "w");
|
|
|
|
|
m_output_file_sp = std::make_shared<StreamFile>(outfile_handle, true);
|
|
|
|
|
m_error_file_sp = m_output_file_sp;
|
|
|
|
|
if (outfile_handle)
|
|
|
|
|
::setbuf(outfile_handle, nullptr);
|
|
|
|
|
|
|
|
|
|
result->SetImmediateOutputFile(debugger.GetOutputStream().GetFileSP());
|
|
|
|
|
result->SetImmediateErrorFile(debugger.GetErrorStream().GetFileSP());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!m_input_file_sp || !m_output_file_sp || !m_error_file_sp)
|
|
|
|
|
debugger.AdoptTopIOHandlerFilesIfInvalid(m_input_file_sp, m_output_file_sp,
|
|
|
|
|
m_error_file_sp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ScriptInterpreterIORedirect::Flush() {
|
|
|
|
|
if (m_output_file_sp)
|
|
|
|
|
m_output_file_sp->Flush();
|
|
|
|
|
if (m_error_file_sp)
|
|
|
|
|
m_error_file_sp->Flush();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ScriptInterpreterIORedirect::~ScriptInterpreterIORedirect() {
|
|
|
|
|
if (!m_disconnect)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
assert(m_output_file_sp);
|
|
|
|
|
assert(m_error_file_sp);
|
|
|
|
|
assert(m_output_file_sp == m_error_file_sp);
|
|
|
|
|
|
|
|
|
|
// Close the write end of the pipe since we are done with our one line
|
|
|
|
|
// script. This should cause the read thread that output_comm is using to
|
|
|
|
|
// exit.
|
|
|
|
|
m_output_file_sp->GetFile().Close();
|
|
|
|
|
// The close above should cause this thread to exit when it gets to the end
|
|
|
|
|
// of file, so let it get all its data.
|
|
|
|
|
m_communication.JoinReadThread();
|
|
|
|
|
// Now we can close the read end of the pipe.
|
|
|
|
|
m_communication.Disconnect();
|
|
|
|
|
}
|