hack warnings: allow to deactivate warnings with hh_single_type_check
[hiphop-php.git] / hphp / runtime / debugger / dummy_sandbox.cpp
blob8dee2c44790d8b37bcadd3cf52c62972f018cce8
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
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");
43 m_thread.start();
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");
50 m_stopped = true;
51 notify(); // Wakeup the sandbox thread so it will notice the stopped flag
52 return m_thread.waitForEnd(timeout);
55 namespace {
57 struct CLISession {
58 CLISession() {
59 TRACE(2, "CLISession::CLISession\n");
60 char *argv[] = {"", nullptr};
61 execute_command_line_begin(1, argv);
63 ~CLISession() {
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();
81 while (!m_stopped) {
82 try {
83 CLISession hphpSession;
85 DSandboxInfo sandbox = m_proxy->getSandbox();
86 std::string msg;
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";
95 } else {
96 auto server = php_global_exchange(s__SERVER, init_null());
97 forceToDict(server);
98 Array arr = server.asArrRef();
99 server.unset();
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);
107 if (!doc.empty()) {
108 char cwd[PATH_MAX];
109 getcwd(cwd, sizeof(cwd));
110 Logger::Info("Start loading startup doc '%s', pwd = '%s'",
111 doc.c_str(), cwd);
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);
116 if (!ret || error) {
117 msg += "Unable to pre-load " + doc;
118 if (!errorMsg.empty()) {
119 msg += ": " + errorMsg;
122 Logger::Info("Startup doc " + doc + " loaded");
124 } else {
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.
146 Lock lock(this);
147 while (!m_stopped && m_signum != CmdSignal::SignalBreak) {
148 wait(1);
150 if (m_stopped) {
151 // stopped by worker thread
152 break;
154 m_signum = CmdSignal::SignalNone;
156 } catch (const DebuggerClientExitException& ) {
157 // stopped by the dummy sandbox thread itself
158 break;
159 } catch (const DebuggerException& ) {
164 void DummySandbox::notifySignal(int signum) {
165 TRACE(2, "DummySandbox::notifySignal\n");
166 Lock lock(this);
167 m_signum = signum;
168 notify();
171 std::string DummySandbox::getStartupDoc(const DSandboxInfo &sandbox) {
172 TRACE(2, "DummySandbox::getStartupDoc\n");
173 std::string path;
174 if (!m_startupFile.empty()) {
175 // if relative path, prepend directory
176 if (m_startupFile[0] != '/' && m_startupFile[0] != '~') {
177 path = sandbox.m_path;
178 if (path.empty()) {
179 path = m_defaultPath;
182 if (!path.empty() && path[path.size() - 1] != '/') {
183 path += '/';
185 path += m_startupFile;
186 path = FileUtil::expandUser(path, sandbox.m_user);
188 return path;
191 ///////////////////////////////////////////////////////////////////////////////