Skip to content

Commit

Permalink
Support fpm (#53)
Browse files Browse the repository at this point in the history
* Support fpm
  • Loading branch information
huanghantao authored Dec 26, 2020
1 parent 2b30d69 commit a5109ea
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 34 deletions.
5 changes: 5 additions & 0 deletions include/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,10 @@
#include "main/php.h"

void yasd_rinit(int module_number);
void yasd_rshutdown(int module_number);
void yasd_minit(int module_number);
void replace_execute_ex();
void resume_execute_ex();
void yasd_execute_ex(zend_execute_data *execute_data);
void register_get_cid_function();
void disable_opcache_optimizer();
4 changes: 2 additions & 2 deletions include/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class Global {
bool do_next = false;
bool do_finish = false;

bool first_entry = true;

bool is_detach = false;

// because cid does not repeat, next_cid can be global
Expand All @@ -41,8 +43,6 @@ class Global {

DebuggerModeBase *debugger = nullptr;

char *entry_file = nullptr;

std::map<int, Context *> *contexts;

// filename, [lineno]
Expand Down
56 changes: 53 additions & 3 deletions src/base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@
+----------------------------------------------------------------------+
*/

#include <iostream>

#include "include/util.h"
#include "include/context.h"
#include "include/global.h"
#include "include/base.h"

#include <iostream>
#include "main/SAPI.h"

extern sapi_module_struct sapi_module;

static void (*old_execute_ex)(zend_execute_data *execute_data);

Expand Down Expand Up @@ -85,10 +90,21 @@ void clear_watch_point(zend_execute_data *execute_data) {
}

void yasd_execute_ex(zend_execute_data *execute_data) {
// if not set -e, we will not initialize global
if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_INFO)) {
old_execute_ex(execute_data);
return;
}

if (skip_swoole_library(execute_data) || skip_eval(execute_data)) {
return;
}

if (UNEXPECTED(global->first_entry)) {
global->debugger->init();
global->first_entry = false;
}

yasd::Context *context = global->get_current_context();

context->level++;
Expand All @@ -103,7 +119,8 @@ void yasd_execute_ex(zend_execute_data *execute_data) {
}

void register_get_cid_function() {
if (zend_hash_str_find_ptr(&module_registry, ZEND_STRL("swoole"))) {
// if not install swoole or not cli, we will not register get_cid_function. so get_cid will always return 0
if (zend_hash_str_find_ptr(&module_registry, ZEND_STRL("swoole")) && (strcmp("cli", sapi_module.name) == 0)) {
zend_string *classname = zend_string_init(ZEND_STRL("Swoole\\Coroutine"), 0);
zend_class_entry *class_handle = zend_lookup_class(classname);
zend_string_release(classname);
Expand All @@ -113,9 +130,42 @@ void register_get_cid_function() {
}
}

void disable_opcache_optimizer() {
zend_string *key = zend_string_init(ZEND_STRL("opcache.optimization_level"), 1);
zend_string *value = zend_string_init(ZEND_STRL("0"), 1);

zend_alter_ini_entry(key, value, ZEND_INI_SYSTEM, ZEND_INI_STAGE_STARTUP);

zend_string_release(key);
zend_string_release(value);
}

void yasd_rinit(int module_number) {
global = new yasd::Global();

disable_opcache_optimizer();
}

void yasd_rshutdown(int module_number) {
delete global;
global = nullptr;
}

void yasd_minit(int module_number) {
replace_execute_ex();

// it seems that -e does not work in PHP-FPM mode. so we need to add ZEND_COMPILE_EXTENDED_INFO ourselves.
if (strcmp("cgi-fcgi", sapi_module.name) == 0 || strcmp("fpm-fcgi", sapi_module.name) == 0) {
CG(compiler_options) |= ZEND_COMPILE_EXTENDED_INFO;
}
}

void replace_execute_ex() {
old_execute_ex = zend_execute_ex;
zend_execute_ex = yasd_execute_ex;
}

global = new yasd::Global();
void resume_execute_ex() {
zend_execute_ex = old_execute_ex;
old_execute_ex = nullptr;
}
6 changes: 0 additions & 6 deletions src/cmder_debugger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,6 @@ int CmderDebugger::parse_breakpoint_cmd() {
// breakpoint in current file with lineno
if (exploded_cmd.size() == 2) {
filename = yasd::Util::get_executed_filename();
if (filename == "") {
filename = global->entry_file;
}
lineno = atoi(exploded_cmd[1].c_str());
} else if (exploded_cmd.size() == 3) {
filename = exploded_cmd[1];
Expand Down Expand Up @@ -153,9 +150,6 @@ int CmderDebugger::parse_delete_breakpoint_cmd() {

if (exploded_cmd.size() == 2) {
filename = yasd::Util::get_executed_filename();
if (filename == "") {
filename = global->entry_file;
}
lineno = atoi(exploded_cmd[1].c_str());
} else if (exploded_cmd.size() == 3) {
filename = exploded_cmd[1];
Expand Down
2 changes: 1 addition & 1 deletion src/global.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

#include "./php_yasd.h"

yasd::Global *global;
yasd::Global *global = nullptr;

zend_function *get_cid_function = nullptr;

Expand Down
4 changes: 3 additions & 1 deletion src/remote_debugger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ void RemoteDebugger::handle_stop() {
get_next_cmd();
status = execute_cmd();
} while (status != yasd::DebuggerModeBase::status::NEXT_OPLINE);

close(sock);
}

ssize_t RemoteDebugger::send_init_event_message() {
Expand All @@ -175,7 +177,7 @@ ssize_t RemoteDebugger::send_init_event_message() {
.set_copyright("Copyright (c) 2020-2021 by Codinghuang")
.set_debugger_name("Yasd")
.set_debugger_version(PHP_YASD_VERSION)
.set_fileuri("file://" + std::string(global->entry_file))
.set_fileuri("file://" + std::string(yasd::Util::get_executed_filename()))
.set_idekey("hantaohuang")
.set_language("PHP")
.set_language_version(PHP_VERSION)
Expand Down
4 changes: 0 additions & 4 deletions src/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,6 @@ const char *Util::get_executed_filename() {

filename = zend_get_executed_filename_ex();

if (UNEXPECTED(filename == nullptr)) {
return global->entry_file;
}

return ZSTR_VAL(filename);
}

Expand Down
21 changes: 4 additions & 17 deletions yasd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,8 @@ PHP_RINIT_FUNCTION(yasd) {

yasd_rinit(module_number);

global->entry_file = SG(request_info).path_translated;

register_get_cid_function();

global->debugger->init();

return SUCCESS;
}

Expand All @@ -110,31 +106,22 @@ PHP_RSHUTDOWN_FUNCTION(yasd) {

global->debugger->handle_stop();

yasd_rshutdown(module_number);

return SUCCESS;
}

PHP_MINIT_FUNCTION(yasd) {
ZEND_INIT_MODULE_GLOBALS(yasd, php_yasd_init_globals, nullptr);
REGISTER_INI_ENTRIES();

// yasd_rinit(module_number);
yasd_minit(module_number);

return SUCCESS;
}

// ZEND_MODULE_POST_ZEND_DEACTIVATE_D(yasd) {
// if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_INFO)) {
// return SUCCESS;
// }

// global->debugger->handle_stop();

// return SUCCESS;
// }

PHP_MSHUTDOWN_FUNCTION(yasd) {
delete global;

resume_execute_ex();
return SUCCESS;
}

Expand Down

0 comments on commit a5109ea

Please sign in to comment.