New flag and error message for class to memo key conversion
[hiphop-php.git] / hphp / runtime / debugger / dummy_sandbox.cpp
blob972f9a74f44a5a1b9f84085370936e95a66b12be
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, 0);
63 ~CLISession() {
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();
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 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";
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, sri.setServerVariables(std::move(arr)));
102 Debugger::RegisterSandbox(sandbox);
103 g_context->setSandboxId(sandbox.id());
105 std::string doc = getStartupDoc(sandbox);
106 if (!doc.empty()) {
107 char cwd[PATH_MAX];
108 getcwd(cwd, sizeof(cwd));
109 Logger::Info("Start loading startup doc '%s', pwd = '%s'",
110 doc.c_str(), cwd);
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);
115 if (!ret || error) {
116 msg += "Unable to pre-load " + doc;
117 if (!errorMsg.empty()) {
118 msg += ": " + errorMsg;
121 Logger::Info("Startup doc " + doc + " loaded");
123 } else {
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.
145 Lock lock(this);
146 while (!m_stopped && m_signum != CmdSignal::SignalBreak) {
147 wait(1);
149 if (m_stopped) {
150 // stopped by worker thread
151 break;
153 m_signum = CmdSignal::SignalNone;
155 } catch (const DebuggerClientExitException& e) {
156 // stopped by the dummy sandbox thread itself
157 break;
158 } catch (const DebuggerException& e) {
163 void DummySandbox::notifySignal(int signum) {
164 TRACE(2, "DummySandbox::notifySignal\n");
165 Lock lock(this);
166 m_signum = signum;
167 notify();
170 std::string DummySandbox::getStartupDoc(const DSandboxInfo &sandbox) {
171 TRACE(2, "DummySandbox::getStartupDoc\n");
172 std::string path;
173 if (!m_startupFile.empty()) {
174 // if relative path, prepend directory
175 if (m_startupFile[0] != '/' && m_startupFile[0] != '~') {
176 path = sandbox.m_path;
177 if (path.empty()) {
178 path = m_defaultPath;
181 if (!path.empty() && path[path.size() - 1] != '/') {
182 path += '/';
184 path += m_startupFile;
185 path = FileUtil::expandUser(path, sandbox.m_user);
187 return path;
190 ///////////////////////////////////////////////////////////////////////////////