Skip to content

Commit

Permalink
Refactor dbgp (#26)
Browse files Browse the repository at this point in the history
* Refactor dbgp
  • Loading branch information
huanghantao authored Dec 15, 2020
1 parent 082f412 commit 938aa68
Show file tree
Hide file tree
Showing 12 changed files with 621 additions and 404 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
Language: Cpp
# BasedOnStyle: Google
AccessModifierOffset: -2
AccessModifierOffset: -3
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
Expand Down
1 change: 1 addition & 0 deletions config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ if test "$PHP_YASD" != "no"; then
src/context.cc \
src/global.cc \
src/source_reader.cc \
src/dbgp.cc \
src/debuger_mode_base.cc \
src/cmder_debugger.cc \
src/remote_debugger.cc \
Expand Down
1 change: 1 addition & 0 deletions include/cmder_debugger.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class CmderDebugger: public DebuggerModeBase {

void show_welcome_info();
void show_breakpoint_hit_info();
void reload_cache_breakpoint();

int get_listsize() {
return listsize;
Expand Down
178 changes: 178 additions & 0 deletions include/dbgp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*
+----------------------------------------------------------------------+
| Yasd |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: codinghuang <[email protected]> |
+----------------------------------------------------------------------+
*/
#pragma once

#include <string>

#include "thirdparty/tinyxml2/tinyxml2.h"

namespace yasd {

class DbgpInitElement {
public:
std::string debugger_name;
std::string debugger_version;
std::string fileuri;
std::string language;
std::string language_version;
std::string appid;
std::string idekey;

std::string author;
std::string url;
std::string copyright;

DbgpInitElement &set_debugger_name(std::string _debugger_name) {
debugger_name = _debugger_name;
return *this;
}

DbgpInitElement &set_debugger_version(std::string _debugger_version) {
debugger_version = _debugger_version;
return *this;
}

DbgpInitElement &set_fileuri(std::string _fileuri) {
fileuri = _fileuri;
return *this;
}

DbgpInitElement &set_language(std::string _language) {
language = _language;
return *this;
}

DbgpInitElement &set_language_version(std::string _language_version) {
language_version = _language_version;
return *this;
}

DbgpInitElement &set_idekey(std::string _idekey) {
idekey = _idekey;
return *this;
}

DbgpInitElement &set_appid(std::string _appid) {
appid = _appid;
return *this;
}

DbgpInitElement &set_author(std::string _author) {
author = _author;
return *this;
}

DbgpInitElement &set_url(std::string _url) {
url = _url;
return *this;
}

DbgpInitElement &set_copyright(std::string _copyright) {
copyright = _copyright;
return *this;
}
};

class ResponseElement {
public:
std::string cmd;
int transaction_id;

ResponseElement &set_cmd(std::string _cmd) {
cmd = _cmd;
return *this;
}

ResponseElement &set_transaction_id(int _transaction_id) {
transaction_id = _transaction_id;
return *this;
}
};

class MessageElement {
public:
std::string filename;
int lineno;

MessageElement &set_filename(std::string _filename) {
filename = _filename;
return *this;
}

MessageElement &set_lineno(int _lineno) {
lineno = _lineno;
return *this;
}
};

class PropertyElement {
public:
std::string type;
std::string name;
std::string fullname;
zval *value;
int level = 0;
bool encoding = false;

PropertyElement &set_type(std::string _type) {
type = _type;
return *this;
}

PropertyElement &set_name(std::string _name) {
name = _name;
return *this;
}

PropertyElement &set_fullname(std::string _fullname) {
fullname = _fullname;
return *this;
}

PropertyElement &set_value(zval *_value) {
value = _value;
return *this;
}

PropertyElement &set_level(int _level) {
level = _level;
return *this;
}

PropertyElement &set_encoding(bool _encoding) {
encoding = _encoding;
return *this;
}
};

class Dbgp {
public:
Dbgp() {}
~Dbgp() {}

static std::string make_message(tinyxml2::XMLDocument *doc);
static void get_zend_string_property_doc(tinyxml2::XMLElement *root, const PropertyElement &property_element);
static void get_zend_array_child_property_doc(tinyxml2::XMLElement *child, const PropertyElement &property_element);
static void get_zend_object_child_property_doc(tinyxml2::XMLElement *child,
const PropertyElement &property_element);
static void get_init_event_doc(tinyxml2::XMLDocument *doc, const DbgpInitElement &init_element);
static void get_response_doc(tinyxml2::XMLElement *root, const ResponseElement &response_element);
static void get_property_doc(tinyxml2::XMLElement *root, const PropertyElement &property_element);
static void get_message_doc(tinyxml2::XMLDocument *doc,
const ResponseElement &response_element,
const MessageElement &message_element);
};
} // namespace yasd
10 changes: 0 additions & 10 deletions include/remote_debugger.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,16 @@ class RemoteDebugger : public DebuggerModeBase {
std::string get_next_cmd();
int execute_cmd();

void init_response_xml_root_node(tinyxml2::XMLElement *root, std::string cmd);
void init_xml_property_node(
tinyxml2::XMLElement *child, std::string name, zval *value, int level = 0, bool encoding = false);
void init_local_variables_xml_child_node(tinyxml2::XMLElement *root);
void init_superglobal_variables_xml_child_node(tinyxml2::XMLElement *root);
void init_user_defined_constant_variables_xml_child_node(tinyxml2::XMLElement *root);

void init_zend_array_element_xml_property_node(
tinyxml2::XMLElement *child, std::string name, zval *value, int level = 0, bool encoding = false);

void init_zend_object_property_xml_property_node(
tinyxml2::XMLElement *child, std::string name, zval *value, int level = 0, bool encoding = false);

public:
RemoteDebugger() {}
~RemoteDebugger() {}

void init();
void handle_request(const char *filename, int lineno);
std::string make_message(tinyxml2::XMLDocument *doc);
ssize_t send_doc(tinyxml2::XMLDocument *doc);

ssize_t send_init_event_message();
Expand Down
3 changes: 0 additions & 3 deletions include/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
namespace yasd {
class Util {
public:
static std::vector<std::string> explode(const std::string &str, const std::string &delimiter);

static HashTable *get_defined_vars();
static zval *find_variable(std::string var_name);
static zval *find_variable(zend_array *symbol_table, std::string var_name);
Expand All @@ -52,7 +50,6 @@ class Util {
static int get_prev_executed_file_lineno();
static bool is_match(std::string sub_str, std::string target_str);

static void reload_cache_breakpoint();
static void clear_breakpoint_cache_file();
static std::string get_breakpoint_cache_filename();
static void cache_breakpoint(std::string filename, int lineno);
Expand Down
16 changes: 0 additions & 16 deletions php_yasd.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,4 @@ extern ZEND_DECLARE_MODULE_GLOBALS(yasd);
#define php_yasd_fatal_error(level, fmt_str, ...) \
php_error_docref(NULL, level, (const char *) (fmt_str), ##__VA_ARGS__)

namespace yasd { namespace function {
class ReturnValue {
public:
zval value;
ReturnValue() {
value = {};
}
~ReturnValue() {
zval_dtor(&value);
}
};

ReturnValue call(const std::string &func_name, int argc, zval *argv);
}
}

#endif /* PHP_YASD_H_ */
55 changes: 47 additions & 8 deletions src/cmder_debugger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void CmderDebugger::init() {

show_welcome_info();

yasd::Util::reload_cache_breakpoint();
reload_cache_breakpoint();
register_cmd_handler();

do {
Expand All @@ -51,8 +51,9 @@ void CmderDebugger::handle_request(const char *filename, int lineno) {
int status;
std::string cmd;
yasd::SourceReader reader(filename);
std::vector<std::string> exploded_cmd;

auto exploded_cmd = yasd::Util::explode(last_cmd, " ");
boost::split(exploded_cmd, last_cmd, boost::is_any_of(" "), boost::token_compress_on);

if (get_full_name(exploded_cmd[0]) == "run" || get_full_name(exploded_cmd[0]) == "continue") {
yasd::Util::printf_info(yasd::Color::YASD_ECHO_MAGENTA, "stop because of breakpoint ");
Expand Down Expand Up @@ -101,8 +102,9 @@ int CmderDebugger::parse_run_cmd() {
int CmderDebugger::parse_breakpoint_cmd() {
int lineno;
std::string filename;
std::vector<std::string> exploded_cmd;

auto exploded_cmd = yasd::Util::explode(last_cmd, " ");
boost::split(exploded_cmd, last_cmd, boost::is_any_of(" "), boost::token_compress_on);

// breakpoint in current file with lineno
if (exploded_cmd.size() == 2) {
Expand Down Expand Up @@ -139,8 +141,9 @@ int CmderDebugger::parse_breakpoint_cmd() {
int CmderDebugger::parse_delete_breakpoint_cmd() {
int lineno;
std::string filename;
std::vector<std::string> exploded_cmd;

auto exploded_cmd = yasd::Util::explode(last_cmd, " ");
boost::split(exploded_cmd, last_cmd, boost::is_any_of(" "), boost::token_compress_on);

if (exploded_cmd.size() == 2) {
filename = yasd::Util::get_executed_filename();
Expand Down Expand Up @@ -232,8 +235,9 @@ int CmderDebugger::parse_list_cmd() {
int lineno = last_list_lineno;
const char *filename = yasd::Util::get_executed_filename();
yasd::SourceReader reader(filename);
std::vector<std::string> exploded_cmd;

auto exploded_cmd = yasd::Util::explode(last_cmd, " ");
boost::split(exploded_cmd, last_cmd, boost::is_any_of(" "), boost::token_compress_on);

// list lineno or list -
if (exploded_cmd.size() == 2) {
Expand All @@ -255,7 +259,9 @@ int CmderDebugger::parse_list_cmd() {
}

int CmderDebugger::parse_set_cmd() {
auto exploded_cmd = yasd::Util::explode(last_cmd, " ");
std::vector<std::string> exploded_cmd;

boost::split(exploded_cmd, last_cmd, boost::is_any_of(" "), boost::token_compress_on);

if (exploded_cmd[1] == "listsize") {
listsize = atoi(exploded_cmd[2].c_str());
Expand All @@ -267,8 +273,9 @@ int CmderDebugger::parse_watch_cmd() {
zend_function *func = EG(current_execute_data)->func;
std::string var_name;
yasd::WatchPointElement element;
std::vector<std::string> exploded_cmd;

auto exploded_cmd = yasd::Util::explode(last_cmd, " ");
boost::split(exploded_cmd, last_cmd, boost::is_any_of(" "), boost::token_compress_on);
if (exploded_cmd.size() < 2) {
yasd::Util::printfln_info(yasd::Color::YASD_ECHO_RED, "you should set watch point");
return RECV_CMD_AGAIN;
Expand Down Expand Up @@ -314,8 +321,9 @@ int CmderDebugger::parse_watch_cmd() {

int CmderDebugger::parse_unwatch_cmd() {
zend_function *func = EG(current_execute_data)->func;
std::vector<std::string> exploded_cmd;

auto exploded_cmd = yasd::Util::explode(last_cmd, " ");
boost::split(exploded_cmd, last_cmd, boost::is_any_of(" "), boost::token_compress_on);
if (exploded_cmd.size() < 2) {
yasd::Util::printfln_info(yasd::Color::YASD_ECHO_RED, "you should set watch point");
return RECV_CMD_AGAIN;
Expand Down Expand Up @@ -364,6 +372,37 @@ void CmderDebugger::show_welcome_info() {
yasd::Util::printfln_info(YASD_ECHO_GREEN, "[You can set breakpoint now]");
}

void CmderDebugger::reload_cache_breakpoint() {
std::string content;
std::fstream file(yasd::Util::get_breakpoint_cache_filename());
std::string filename;
int lineno;

while (getline(file, content)) {
std::vector<std::string> exploded_content;

boost::split(exploded_content, content, boost::is_any_of(":"), boost::token_compress_on);
if (exploded_content.size() != 2) {
continue;
}

filename = exploded_content[0];
lineno = atoi(exploded_content[1].c_str());

auto iter = global->breakpoints->find(filename);

yasd::Util::printfln_info(yasd::Color::YASD_ECHO_GREEN, "reload breakpoint at %s:%d", filename.c_str(), lineno);

if (iter != global->breakpoints->end()) {
iter->second.insert(lineno);
} else {
std::set<int> lineno_set;
lineno_set.insert(lineno);
global->breakpoints->insert(std::make_pair(filename, lineno_set));
}
}
}

int CmderDebugger::execute_cmd() {
std::vector<std::string> exploded_cmd;

Expand Down
Loading

0 comments on commit 938aa68

Please sign in to comment.