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
, 0);
64 TRACE(2, "CLISession::~CLISession\n");
65 Debugger::UnregisterSandbox(g_context
->getSandboxId());
66 DebuggerHook::detach();
67 execute_command_line_end(0, 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
sri(sandbox
.m_user
, sandbox
.m_name
);
89 if (sandbox
.m_path
.empty()) {
90 sandbox
.m_path
= sri
.path();
92 if (!sri
.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
, sri
.setServerVariables(std::move(arr
)));
102 Debugger::RegisterSandbox(sandbox
);
103 g_context
->setSandboxId(sandbox
.id());
105 std::string doc
= getStartupDoc(sandbox
);
108 getcwd(cwd
, sizeof(cwd
));
109 Logger::Info("Start loading startup doc '%s', pwd = '%s'",
111 bool error
; std::string errorMsg
;
112 bool ret
= hphp_invoke(g_context
.getNoCheck(), doc
, false, null_array
,
113 nullptr, "", "", error
, errorMsg
, true,
114 false, true, RuntimeOption::EvalPreludePath
);
116 msg
+= "Unable to pre-load " + doc
;
117 if (!errorMsg
.empty()) {
118 msg
+= ": " + errorMsg
;
121 Logger::Info("Startup doc " + doc
+ " loaded");
124 g_context
->setSandboxId(m_proxy
->getDummyInfo().id());
127 if (!DebuggerHook::attach
<HphpdHook
>(ti
)) {
128 const char* fail
= "Could not attach hphpd to request: another debugger"
129 " is already attached.";
130 Logger::Error("%s", fail
);
131 Debugger::InterruptSessionStarted(nullptr, fail
);
132 throw DebuggerClientAttachFailureException();
135 DebuggerDummyEnv dde
;
136 // This is really the entire point of having the dummy sandbox. This
137 // fires the initial session started interrupt to the client after
138 // it first attaches.
139 Debugger::InterruptSessionStarted(nullptr, msg
.c_str());
142 // Blocking until Ctrl-C is issued by end user and DebuggerProxy cannot
143 // find a real sandbox thread to handle it.
146 while (!m_stopped
&& m_signum
!= CmdSignal::SignalBreak
) {
150 // stopped by worker thread
153 m_signum
= CmdSignal::SignalNone
;
155 } catch (const DebuggerClientExitException
& e
) {
156 // stopped by the dummy sandbox thread itself
158 } catch (const DebuggerException
& e
) {
163 void DummySandbox::notifySignal(int signum
) {
164 TRACE(2, "DummySandbox::notifySignal\n");
170 std::string
DummySandbox::getStartupDoc(const DSandboxInfo
&sandbox
) {
171 TRACE(2, "DummySandbox::getStartupDoc\n");
173 if (!m_startupFile
.empty()) {
174 // if relative path, prepend directory
175 if (m_startupFile
[0] != '/' && m_startupFile
[0] != '~') {
176 path
= sandbox
.m_path
;
178 path
= m_defaultPath
;
181 if (!path
.empty() && path
[path
.size() - 1] != '/') {
184 path
+= m_startupFile
;
185 path
= FileUtil::expandUser(path
, sandbox
.m_user
);
190 ///////////////////////////////////////////////////////////////////////////////