2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #include "hphp/runtime/debugger/dummy_sandbox.h"
19 #include "hphp/runtime/debugger/debugger.h"
20 #include "hphp/runtime/debugger/debugger_hook_handler.h"
21 #include "hphp/runtime/debugger/cmd/cmd_signal.h"
22 #include "hphp/runtime/base/program-functions.h"
23 #include "hphp/runtime/base/request-info.h"
24 #include "hphp/runtime/server/source-root-info.h"
25 #include "hphp/runtime/base/php-globals.h"
26 #include "hphp/runtime/base/file-util.h"
28 #include "hphp/util/logger.h"
30 namespace HPHP::Eval
{
31 ///////////////////////////////////////////////////////////////////////////////
32 TRACE_SET_MOD(debugger
);
34 DummySandbox::DummySandbox(DebuggerProxy
*proxy
,
35 const std::string
&defaultPath
,
36 const std::string
&startupFile
)
37 : m_proxy(proxy
), m_defaultPath(defaultPath
), m_startupFile(startupFile
),
38 m_thread(this, &DummySandbox::run
), m_stopped(false),
39 m_signum(CmdSignal::SignalNone
) { }
41 void DummySandbox::start() {
42 TRACE(2, "DummySandbox::start\n");
46 // Stop the sandbox thread, and wait for it to end. Timeout is in
47 // seconds. This can be called multiple times.
48 bool DummySandbox::stop(int timeout
) {
49 TRACE(2, "DummySandbox::stop\n");
51 notify(); // Wakeup the sandbox thread so it will notice the stopped flag
52 return m_thread
.waitForEnd(timeout
);
59 TRACE(2, "CLISession::CLISession\n");
60 char *argv
[] = {"", nullptr};
61 execute_command_line_begin(1, argv
);
64 TRACE(2, "CLISession::~CLISession\n");
65 Debugger::UnregisterSandbox(g_context
->getSandboxId());
66 DebuggerHook::detach();
67 execute_command_line_end(false, nullptr);
70 CLISession(const CLISession
&) = delete;
71 CLISession
& operator=(const CLISession
&) = delete;
76 const StaticString
s__SERVER("_SERVER");
78 void DummySandbox::run() {
79 TRACE(2, "DummySandbox::run\n");
80 RequestInfo
*ti
= RequestInfo::s_requestInfo
.getNoCheck();
83 CLISession hphpSession
;
85 DSandboxInfo sandbox
= m_proxy
->getSandbox();
87 if (sandbox
.valid()) {
88 SourceRootInfo::WithRoot
sri(sandbox
.m_user
, sandbox
.m_name
);
89 if (sandbox
.m_path
.empty()) {
90 sandbox
.m_path
= SourceRootInfo::GetCurrentSourceRoot();
92 if (!SourceRootInfo::SandboxOn()) {
93 msg
= "Invalid sandbox was specified. "
94 "PHP files may not be loaded properly.\n";
96 auto server
= php_global_exchange(s__SERVER
, init_null());
98 Array arr
= server
.asArrRef();
100 php_global_set(s__SERVER
,
101 SourceRootInfo::SetServerVariables(std::move(arr
)));
103 Debugger::RegisterSandbox(sandbox
);
104 g_context
->setSandboxId(sandbox
.id());
106 std::string doc
= getStartupDoc(sandbox
);
109 getcwd(cwd
, sizeof(cwd
));
110 Logger::Info("Start loading startup doc '%s', pwd = '%s'",
112 bool error
; std::string errorMsg
;
113 bool ret
= hphp_invoke(g_context
.getNoCheck(), doc
, false, null_array
,
114 nullptr, "", "", error
, errorMsg
, true,
115 false, true, RuntimeOption::EvalPreludePath
);
117 msg
+= "Unable to pre-load " + doc
;
118 if (!errorMsg
.empty()) {
119 msg
+= ": " + errorMsg
;
122 Logger::Info("Startup doc " + doc
+ " loaded");
125 g_context
->setSandboxId(m_proxy
->getDummyInfo().id());
128 if (!DebuggerHook::attach
<HphpdHook
>(ti
)) {
129 const char* fail
= "Could not attach hphpd to request: another debugger"
130 " is already attached.";
131 Logger::Error("%s", fail
);
132 Debugger::InterruptSessionStarted(nullptr, fail
);
133 throw DebuggerClientAttachFailureException();
136 DebuggerDummyEnv dde
;
137 // This is really the entire point of having the dummy sandbox. This
138 // fires the initial session started interrupt to the client after
139 // it first attaches.
140 Debugger::InterruptSessionStarted(nullptr, msg
.c_str());
143 // Blocking until Ctrl-C is issued by end user and DebuggerProxy cannot
144 // find a real sandbox thread to handle it.
147 while (!m_stopped
&& m_signum
!= CmdSignal::SignalBreak
) {
151 // stopped by worker thread
154 m_signum
= CmdSignal::SignalNone
;
156 } catch (const DebuggerClientExitException
& ) {
157 // stopped by the dummy sandbox thread itself
159 } catch (const DebuggerException
& ) {
164 void DummySandbox::notifySignal(int signum
) {
165 TRACE(2, "DummySandbox::notifySignal\n");
171 std::string
DummySandbox::getStartupDoc(const DSandboxInfo
&sandbox
) {
172 TRACE(2, "DummySandbox::getStartupDoc\n");
174 if (!m_startupFile
.empty()) {
175 // if relative path, prepend directory
176 if (m_startupFile
[0] != '/' && m_startupFile
[0] != '~') {
177 path
= sandbox
.m_path
;
179 path
= m_defaultPath
;
182 if (!path
.empty() && path
[path
.size() - 1] != '/') {
185 path
+= m_startupFile
;
186 path
= FileUtil::expandUser(path
, sandbox
.m_user
);
191 ///////////////////////////////////////////////////////////////////////////////