PHP扩展编写、PHP扩展调试、VLD源码分析、基于嵌入式Embed SAPI实现opcode查看

利用测井数据进行正演 井中地震数据处理。利用测井曲线和反射率方法进行角度集地震记录正演 立即下载

catalogue

复制代码

1. 编译PHP源码
2. 扩展结构、优缺点
3. 使用PHP原生扩展框架wizard ext_skel编写扩展
4. 编译安装VLD
5. Debug调试VLD
6. VLD源码分析
7. 嵌入式Embed SAPI编程

复制代码

 

1. 编译PHP源码

复制代码

wget http://cn2.php.net/distributions/php-5.5.31.tar.gz
tar -zvzf php-5.5.31.tar.gz
//为了尽快得到可以测试的环境,我们仅编译一个最精简的PHP。通过执行 ./configure –disable-all来进行配置。 以后如果需要其他功能可以重新编译。如果configure命令出现错误,可能是缺少PHP所依赖的库,各个系统的环境可能不一样。 出现错误可根据出错信息上网搜索。 直到完成configure。configure完成后我们就可以开始编译
./configure --enable-debug --enable-tokenizer  
/*
apt-get install -y libxml2
*/
make
//运行编译后PHP
./sapi/cli/php -v

//install NetBeans
http://download.netbeans.org/netbeans/8.1/final/bundles/netbeans-8.1-linux.sh
chmod 777 ./*
./netbeans-8.1-linux.sh

./configure CC=${IDE_CC} CXX=${IDE_CXX} CFLAGS="-g3 -gdwarf-2" CXXFLAGS="-g3 -gdwarf-2" --disable-all --enable-debug --enable-tokenizer

复制代码

Relevant Link:

http://www.cnblogs.com/LittleHann/p/3562259.html

 

2. 扩展结构、优缺点

0x1: C/C++扩展PHP的优点

复制代码

1. 效率 
减少PHP脚本的复杂度,极端情况下,你只需要在PHP脚本中,简单的调用一个扩展实现的函数,然后所有的功能都就被扩展实现 

2. 与外部的库做交互
比如你有一个C/C++的库, 不妨假设,这个库呢就是实现了一个字符串加密和解密,而你并没有这个库的源码,也就是说,你无法把这个库在PHP中实现,那么你只有编写一个PHP扩展,来做为一个桥梁,连接起你的PHP和这个库

3. 复用Zend的词法/语法处理逻辑
在特殊领域的应用,例如WEBSHELL检测中,通过扩展Hook机制实现Token AST树的获取,然后在此基础上进行语法还原、和语法层面模拟执行

复制代码

0x2: 缺点

1. 开发复杂
2. 可维护性降低
开发周期变长, 最简单的一个例子,当你用PHP脚本的时候, 如果你发现某个判断条件出错,你只要修改了这一行,保存,那么就立刻能见效。而如果是在C/C++编写的PHP扩展中, 那你可需要,修改源码,重新编译,然后重新load进PHP,然后重启Apache,才能见效 

0x3: 扩展框架的基础结构

复制代码

root@ubuntu1204-virtual-machine:/home/ubuntu1204/phpsourcecode/php-5.5.31/ext# cd helloworld/
root@ubuntu1204-virtual-machine:/home/ubuntu1204/phpsourcecode/php-5.5.31/ext/helloworld# ll
total 44
drwxr-xr-x  3 root       root       4096 Jan 27 16:10 ./
drwxr-xr-x 80 ubuntu1204 ubuntu1204 4096 Jan 27 16:10 ../
-rw-r--r--  1 root       root       2178 Jan 27 16:10 config.m4
-rw-r--r--  1 root       root        324 Jan 27 16:10 config.w32
-rw-r--r--  1 root       root         10 Jan 27 16:10 CREDITS
-rw-r--r--  1 root       root          0 Jan 27 16:10 EXPERIMENTAL
-rw-r--r--  1 root       root       5296 Jan 27 16:10 helloworld.c
-rw-r--r--  1 root       root        514 Jan 27 16:10 helloworld.php
-rw-r--r--  1 root       root       2962 Jan 27 16:10 php_helloworld.h
-rw-r--r--  1 root       root         16 Jan 27 16:10 .svnignore
drwxr-xr-x  2 root       root       4096 Jan 27 16:10 tests/

复制代码

1. helloworld.c

这个文件是我们扩展的主要文件,扩展的主体逻辑都在里面实现

复制代码

/*
  +----------------------------------------------------------------------+
  | PHP Version 5                                                        |
  +----------------------------------------------------------------------+
  | Copyright (c) 1997-2015 The PHP Group                                |
  +----------------------------------------------------------------------+
  | This source file is subject to version 3.01 of the PHP 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.php.net/license/3_01.txt                                  |
  | If you did not receive a copy of the PHP license and are unable to   |
  | obtain it through the world-wide-web, please send a note to          |
  | license@php.net so we can mail you a copy immediately.               |
  +----------------------------------------------------------------------+
  | Author:                                                              |
  +----------------------------------------------------------------------+
*/

/* $Id$ */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_helloworld.h"

/* If you declare any globals in php_helloworld.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(helloworld)
*/

/* True global resources - no need for thread safety here */
static int le_helloworld;

/* {{{ helloworld_functions[]
 *
 * Every user visible function must have an entry in helloworld_functions[].
 */
const zend_function_entry helloworld_functions[] = {
    PHP_FE(confirm_helloworld_compiled,    NULL)        /* For testing, remove later. */
    PHP_FE_END    /* Must be the last line in helloworld_functions[] */
};
/* }}} */

/* {{{ helloworld_module_entry
 */
zend_module_entry helloworld_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
    STANDARD_MODULE_HEADER,
#endif
    "helloworld",
    helloworld_functions,
    PHP_MINIT(helloworld),
    PHP_MSHUTDOWN(helloworld),
    PHP_RINIT(helloworld),        /* Replace with NULL if there's nothing to do at request start */
    PHP_RSHUTDOWN(helloworld),    /* Replace with NULL if there's nothing to do at request end */
    PHP_MINFO(helloworld),
#if ZEND_MODULE_API_NO >= 20010901
    PHP_HELLOWORLD_VERSION,
#endif
    STANDARD_MODULE_PROPERTIES
};
/* }}} */

#ifdef COMPILE_DL_HELLOWORLD
ZEND_GET_MODULE(helloworld)
#endif

/* {{{ PHP_INI
 */
/* Remove comments and fill if you need to have entries in php.ini
PHP_INI_BEGIN()
    STD_PHP_INI_ENTRY("helloworld.global_value",      "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_helloworld_globals, helloworld_globals)
    STD_PHP_INI_ENTRY("helloworld.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_helloworld_globals, helloworld_globals)
PHP_INI_END()
*/
/* }}} */

/* {{{ php_helloworld_init_globals
 */
/* Uncomment this function if you have INI entries
static void php_helloworld_init_globals(zend_helloworld_globals *helloworld_globals)
{
    helloworld_globals->global_value = 0;
    helloworld_globals->global_string = NULL;
}
*/
/* }}} */

/* {{{ PHP_MINIT_FUNCTION
 */
PHP_MINIT_FUNCTION(helloworld)
{
    /* If you have INI entries, uncomment these lines 
    REGISTER_INI_ENTRIES();
    */
    return SUCCESS;
}
/* }}} */

/* {{{ PHP_MSHUTDOWN_FUNCTION
 */
PHP_MSHUTDOWN_FUNCTION(helloworld)
{
    /* uncomment this line if you have INI entries
    UNREGISTER_INI_ENTRIES();
    */
    return SUCCESS;
}
/* }}} */

/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
 */
PHP_RINIT_FUNCTION(helloworld)
{
    return SUCCESS;
}
/* }}} */

/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
 */
PHP_RSHUTDOWN_FUNCTION(helloworld)
{
    return SUCCESS;
}
/* }}} */

/* {{{ PHP_MINFO_FUNCTION
 */
PHP_MINFO_FUNCTION(helloworld)
{
    php_info_print_table_start();
    php_info_print_table_header(2, "helloworld support", "enabled");
    php_info_print_table_end();

    /* Remove comments if you have entries in php.ini
    DISPLAY_INI_ENTRIES();
    */
}
/* }}} */


/* Remove the following function when you have successfully modified config.m4
   so that your module can be compiled into PHP, it exists only for testing
   purposes. */

/* Every user-visible function in PHP should document itself in the source */
/* {{{ proto string confirm_helloworld_compiled(string arg)
   Return a string to confirm that the module is compiled in */
PHP_FUNCTION(confirm_helloworld_compiled)
{
    char *arg = NULL;
    int arg_len, len;
    char *strg;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
        return;
    }

    len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "helloworld", arg);
    RETURN_STRINGL(strg, len, 0);
}
/* }}} */
/* The previous line is meant for vim and emacs, so it can correctly fold and 
   unfold functions in source code. See the corresponding marks just before 
   function definition, where the functions purpose is also documented. Please 
   follow this convention for the convenience of others editing your code.
*/


/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: noet sw=4 ts=4 fdm=marker
 * vim<600: noet sw=4 ts=4
 */

复制代码

函数自身的定义使用了宏PHP_FUNCTION(),该宏可以生成一个适合于Zend引擎的函数原型
为了获得函数传递的参数,可以使用zend_parse_parameters()API函数

zend_parse_parameters(int num_args TSRMLS_DC, char *type_spec, ...);
1. num_args: 传递给函数的参数个数。通常的做法是传给它ZEND_NUM_ARGS(),这是一个表示传递给函数参数总个数的宏。
2. TSRMLS_DC: 为了线程安全,总是传递TSRMLS_CC宏
3. *type_spec: 是一个字符串,指定了函数期望的参数类型
4. ...: 需要随参数值更新的变量列表,是一个变长数量参数传递

2. config.m4
m4是一个宏解释工具,它会把输入文件中的宏展开到输出文件。所以这个config.m4是PHP扩展框架所必须的,也是关键的一个文件,用来生成我们扩展的makefile

复制代码

dnl $Id$
dnl config.m4 for extension helloworld

//在m4中,dnl表示注释,如果需要启用对应配置项,则删除行首的dnl即可
dnl Comments in this file start with the string 'dnl'.
dnl Remove where necessary. This file will not work
dnl without editing.

dnl If your extension references something external, use with:

/*
with说明了,要启用这个模块,必须要的先决条件,也就是说这个模块依赖于某些其他模块
*/
dnl PHP_ARG_WITH(helloworld, for helloworld support,
dnl Make sure that the comment is aligned:
dnl [  --with-helloworld             Include helloworld support])

dnl Otherwise use enable:

/*
这段指令创建了一个configure时的参数“enable-example”, 第二个参数会显示在当configure处理到这个模块的configure文件的时候。第三个参数,会在用户输入./configurehelp的时候,作为一个可选的选项被显示
即相当于:  ./configure --enable-helloworld 
*/
dnl PHP_ARG_ENABLE(helloworld, whether to enable helloworld support,
dnl Make sure that the comment is aligned:
dnl [  --enable-helloworld           Enable helloworld support])

if test "$PHP_HELLOWORLD" != "no"; then
  dnl Write more examples of tests here...

  dnl # --with-helloworld -> check with-path
  dnl SEARCH_PATH="/usr/local /usr"     # you might want to change this
  dnl SEARCH_FOR="/include/helloworld.h"  # you most likely want to change this
  dnl if test -r $PHP_HELLOWORLD/$SEARCH_FOR; then # path given as parameter
  dnl   HELLOWORLD_DIR=$PHP_HELLOWORLD
  dnl else # search default path list
  dnl   AC_MSG_CHECKING([for helloworld files in default path])
  dnl   for i in $SEARCH_PATH ; do
  dnl     if test -r $i/$SEARCH_FOR; then
  dnl       HELLOWORLD_DIR=$i
  dnl       AC_MSG_RESULT(found in $i)
  dnl     fi
  dnl   done
  dnl fi
  dnl
  dnl if test -z "$HELLOWORLD_DIR"; then
  dnl   AC_MSG_RESULT([not found])
  dnl   AC_MSG_ERROR([Please reinstall the helloworld distribution])
  dnl fi

  dnl # --with-helloworld -> add include path
  dnl PHP_ADD_INCLUDE($HELLOWORLD_DIR/include)

  dnl # --with-helloworld -> check for lib and symbol presence
  dnl LIBNAME=helloworld # you may want to change this
  dnl LIBSYMBOL=helloworld # you most likely want to change this 

/*
在库library中查找第二个参数是否存在(这里是AC_DEFINE(HAVE_HELLOWORLDLIB,1,[ ])),如果存在则这个宏会被展开成found,否则not-found;
*/
  dnl PHP_CHECK_LIBRARY($LIBNAME,$LIBSYMBOL,
  dnl [
  dnl   PHP_ADD_LIBRARY_WITH_PATH($LIBNAME, $HELLOWORLD_DIR/$PHP_LIBDIR, HELLOWORLD_SHARED_LIBADD)
  dnl   AC_DEFINE(HAVE_HELLOWORLDLIB,1,[ ])
  dnl ],[
  dnl   AC_MSG_ERROR([wrong helloworld lib version or lib not found])
  dnl ],[
  dnl   -L$HELLOWORLD_DIR/$PHP_LIBDIR -lm
  dnl ])
  dnl
  dnl PHP_SUBST(HELLOWORLD_SHARED_LIBADD)

/*
这个就是对AC_DEFUN简单包装,最终会被展开成: #define what value
*/
  dnl PHP_DEFINE(what, [value]) 

/*
如果你的扩展是使用C++编写,那么你就必须使用这个宏,来告诉编译器使用C++编译器。这个宏会被展开成
AC_PROG_CXX
AC_PROG_CXXCPP
*/
  dnl PHP_REQURE_CXX 

  PHP_NEW_EXTENSION(helloworld, helloworld.c, $ext_shared)
fi

复制代码

3. CREDITES

用来在发布你的扩展的时候附加一些其他信息 ,比如等等

4. helloworld.php

用来简单测试你的扩展的

复制代码

<?php
$br = (php_sapi_name() == "cli")? "":"<br>";

if(!extension_loaded('helloworld')) {
    dl('helloworld.' . PHP_SHLIB_SUFFIX);
}
$module = 'helloworld';
$functions = get_extension_funcs($module);
echo "Functions available in the test extension:$br\n";
foreach($functions as $func) {
    echo $func."$br\n";
}
echo "$br\n";
$function = 'confirm_' . $module . '_compiled';
if (extension_loaded($module)) {
    $str = $function($module);
} else {
    $str = "Module $module is not compiled into PHP";
}
echo "$str\n";
?>

复制代码

5. php_helloworld.h

复制代码

/*
  +----------------------------------------------------------------------+
  | PHP Version 5                                                        |
  +----------------------------------------------------------------------+
  | Copyright (c) 1997-2015 The PHP Group                                |
  +----------------------------------------------------------------------+
  | This source file is subject to version 3.01 of the PHP 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.php.net/license/3_01.txt                                  |
  | If you did not receive a copy of the PHP license and are unable to   |
  | obtain it through the world-wide-web, please send a note to          |
  | license@php.net so we can mail you a copy immediately.               |
  +----------------------------------------------------------------------+
  | Author:                                                              |
  +----------------------------------------------------------------------+
*/

/* $Id$ */

#ifndef PHP_HELLOWORLD_H
#define PHP_HELLOWORLD_H

extern zend_module_entry helloworld_module_entry;
#define phpext_helloworld_ptr &helloworld_module_entry

#define PHP_HELLOWORLD_VERSION "0.1.0" /* Replace with version number for your extension */

#ifdef PHP_WIN32
#    define PHP_HELLOWORLD_API __declspec(dllexport)
#elif defined(__GNUC__) && __GNUC__ >= 4
#    define PHP_HELLOWORLD_API __attribute__ ((visibility("default")))
#else
#    define PHP_HELLOWORLD_API
#endif

#ifdef ZTS
#include "TSRM.h"
#endif

PHP_MINIT_FUNCTION(helloworld);
PHP_MSHUTDOWN_FUNCTION(helloworld);
PHP_RINIT_FUNCTION(helloworld);
PHP_RSHUTDOWN_FUNCTION(helloworld);
PHP_MINFO_FUNCTION(helloworld);

PHP_FUNCTION(confirm_helloworld_compiled);    /* For testing, remove later. */

/* 
      Declare any global variables you may need between the BEGIN
    and END macros here:     

ZEND_BEGIN_MODULE_GLOBALS(helloworld)
    long  global_value;
    char *global_string;
ZEND_END_MODULE_GLOBALS(helloworld)
*/

/* In every utility function you add that needs to use variables 
   in php_helloworld_globals, call TSRMLS_FETCH(); after declaring other 
   variables used by that function, or better yet, pass in TSRMLS_CC
   after the last function argument and declare your utility function
   with TSRMLS_DC after the last declared argument.  Always refer to
   the globals in your function as HELLOWORLD_G(variable).  You are 
   encouraged to rename these macros something shorter, see
   examples in any other php module directory.
*/

#ifdef ZTS
#define HELLOWORLD_G(v) TSRMG(helloworld_globals_id, zend_helloworld_globals *, v)
#else
#define HELLOWORLD_G(v) (helloworld_globals.v)
#endif

#endif    /* PHP_HELLOWORLD_H */


/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: noet sw=4 ts=4 fdm=marker
 * vim<600: noet sw=4 ts=4
 */

复制代码

Relevant Link:

http://www.laruence.com/2008/08/16/301.html

 

3. 使用PHP原生扩展框架wizard ext_skel编写扩展

0x1: Hello world扩展框架代码生成

复制代码

root@ubuntu1204-virtual-machine:/home/ubuntu1204/phpsourcecode/php-5.5.31# cd ext/
root@ubuntu1204-virtual-machine:/home/ubuntu1204/phpsourcecode/php-5.5.31/ext# pwd
/home/ubuntu1204/phpsourcecode/php-5.5.31/ext 
root@ubuntu1204-virtual-machine:/home/ubuntu1204/phpsourcecode/php-5.5.31/ext# ./ext_skel --extname=helloworld
Creating directory helloworld
Creating basic files: config.m4 config.w32 .svnignore helloworld.c php_helloworld.h CREDITS EXPERIMENTAL tests/001.phpt helloworld.php [done].

To use your new extension, you will have to execute the following steps:

1.  $ cd ..
2.  $ vi ext/helloworld/config.m4
3.  $ ./buildconf
4.  $ ./configure --[with|enable]-helloworld
5.  $ make
6.  $ ./sapi/cli/php -f ext/helloworld/helloworld.php
7.  $ vi ext/helloworld/helloworld.c
8.  $ make

Repeat steps 3-6 until you are satisfied with ext/helloworld/config.m4 and
step 6 confirms that your module is compiled into PHP. Then, start writing
code and repeat the last two steps as often as necessary.

复制代码

在ext目录下生成了一个名为example的目录,并在这个目录下生成了所有的要完成一个PHP扩展所必须的文件和代码

0x2: 修改config.m4

去除注释

PHP_ARG_WITH(helloworld, for helloworld support,
dnl Make sure that the comment is aligned:
[  --with-helloworld             Include helloworld support])

0x3: 编译

编译扩展有两种方法

1. 可装载模块或者DSO(动态共享对象)
2. 静态编译到PHP

1. 可装载模块或者DSO(动态共享对象)

复制代码

./buildconf --force
root@ubuntu1204-virtual-machine:/home/ubuntu1204/phpsourcecode/php-5.5.31# ./configure --help | grep helloworld
  --with-helloworld             Include helloworld support
make
/*
安装PHP
Installing shared extensions:     /usr/local/lib/php/extensions/debug-non-zts-20121212/
Installing PHP CLI binary:        /usr/local/bin/
Installing PHP CLI man page:      /usr/local/php/man/man1/
Installing PHP CGI binary:        /usr/local/bin/
Installing PHP CGI man page:      /usr/local/php/man/man1/
Installing build environment:     /usr/local/lib/php/build/
Installing header files:          /usr/local/include/php/
Installing helper programs:       /usr/local/bin/
  program: phpize
  program: php-config
Installing man pages:             /usr/local/php/man/man1/
  page: phpize.1
  page: php-config.1
Installing PEAR environment:      /usr/local/lib/php/
[PEAR] Archive_Tar    - installed: 1.4.0
[PEAR] Console_Getopt - installed: 1.4.1
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util       - installed: 1.3.0
[PEAR] PEAR           - installed: 1.10.1
Wrote PEAR system config file at: /usr/local/etc/pear.conf
You may want to add: /usr/local/lib/php to your php.ini include_path
/home/ubuntu1204/phpsourcecode/php-5.5.31/build/shtool install -c ext/phar/phar.phar /usr/local/bin
ln -s -f phar.phar /usr/local/bin/phar
Installing PDO headers:          /usr/local/include/php/ext/pdo/
*/
make install

//编译扩展
cd ./ext/helloword
phpize
./configure --with-helloworld
make

//复制扩展so库到PHP目录
make install

复制代码

2. 静态编译到PHP

将静态库编译进PHP,和普通的静态库编译链接是一个道理,我们使用C/C++编写逻辑代码,并编译成静态.a库,然后重新编译PHP主程序源代码,并在编译参数中显式说明需要链接对应静态库

1. 用C/C++写PHP扩展
2. 编译静态库.a库
3. 把静态库加入PHP: 假设静态库的文件名叫libnpc.a,放在/home目录下。在PHP的安装目录下输入如下命令: export LDFLAGS="–L/home –lnpc",这个环境变量的作用就是让PHP在编译时知道要把这个库也一起编译进去
4. 编译PHP

Relevant Link:

http://www.laruence.com/2011/09/13/2139.html
https://segmentfault.com/a/1190000003952548
http://hzcsky.blog.51cto.com/1560073/820232
http://www.laruence.com/2009/04/28/719.html
http://www.thinksaas.cn/manual/php/features.commandline.html
http://weizhifeng.net/write-php-extension-part1.html

 

4. 编译安装VLD

VLD(Vulcan Logic Dumper)是一个在Zend引擎中,以挂钩的方式实现的用于输出PHP脚本生成的中间代码(执行单元)的扩展

0x1: 编译安装

复制代码

cd /home/ubuntu1204/phpsourcecode/php-5.5.31/ext/vld
解压VLD(Vulcan Logic Dumper)
phpize
./configure --enable-vld  
make
make install

//需要注意的,在PHP CLI模式下,php.ini的配置会被覆盖,我们可以自己显式配置php.ini指令
./sapi/cli/php -d extension=vld.so -dvld.active=1 /home/ubuntu1204/phpsourcecode/sample/shell.php

复制代码

0x2: VLD扩展的参数列表

复制代码

//./sapi/cli/php -d extension=vld.so -dvld.active=1 -dvld.verbosity=3 /home/ubuntu1204/phpsourcecode/sample/shell.php
//./sapi/cli/php -d extension=vld.so -dvld.active=1 -dvld.execute=0 /home/ubuntu1204/phpsourcecode/sample/shell.php

1. -dvld.active: 是否在执行PHP时激活VLD挂钩
    1) 默认为0: 表示禁用
    2) 使用-dvld.active=1启用
2. -dvld.skip_prepend: 是否跳过php.ini配置文件中auto_prepend_file指定的文件
    1) 默认为0,即不跳过包含的文件,显示这些包含的文件中的代码所生成的中间代码。此参数生效有一个前提条件:-dvld.execute=0
3. -dvld.execute: 是否执行这段PHP脚本
    1) 默认值为1,表示执行
    2) 使用-dvld.execute=0,表示只显示中间代码,不执行生成的中间代码 
4. -dvld.format: 是否以自定义的格式显示
    1) 默认为0,表示否
    2) 使用-dvld.format=1,表示以自己定义的格式显示。这里自定义的格式输出是以-dvld.col_sep指定的参数间隔
5. -dvld.col_sep: 在-dvld.format参数启用时此函数才会有效,默认为 "t"
6. -dvld.verbosity: 是否显示更详细的信息
    1) 默认为1
    2) 其值可以为0,1,2,3 其实比0小的也可以,只是效果和0一样,比如0.1之类,但是负数除外,负数和效果和3的效果一样 比3大的值也是可以的,只是效果和3一样,3代表最详细
7. -dvld.save_dir: 指定文件输出的路径,默认路径为/tmp 
8. -dvld.save_paths: 控制是否输出文件,默认为0,表示不输出文件
9. -dvld.dump_paths: 控制输出的内容,现在只有0和1两种情况,默认为1,输出内容

复制代码

Relevant Link:

http://techlog.cn/article/list/10182879
http://pecl.php.net/package/vld
http://www.lampweb.org/seo/8/20.html
http://hilojack.com/p/php-vld/

 

5. Debug调试VLD

复制代码

cd /home/ubuntu1204/phpsourcecode/php-5.5.31/ext/vld
vim config.m4
/*
在最后一行添加
if test -z "$PHP_DEBUG"; then
        AC_ARG_ENABLE(debug,
                [--enable-debg  compile with debugging system],
                [PHP_DEBUG=$enableval], [PHP_DEBUG=no]
        )
fi

这样就表示该扩展能够进行调试了,然后编译该扩展
*/ 
 
phpize
./configure --enable-vld  
make
make install

//编辑netbeans Debug参数
"${OUTPUT_PATH}" -d extension=vld.so -dvld.active=3 /home/ubuntu1204/phpsourcecode/sample/shell.php

复制代码

Relevant Link:

https://segmentfault.com/a/1190000002703073
http://www.codefrom.com/paper/%E4%BD%BF%E7%94%A8gdb%E8%B0%83%E8%AF%95php%E6%89%A9%E5%B1%95

 

6. VLD源码分析

0x1: vld_compile_file

vld.c

复制代码

/* {{{ zend_op_array vld_compile_file (file_handle, type)
 *    This function provides a hook for compilation */
static zend_op_array *vld_compile_file(zend_file_handle *file_handle, int type TSRMLS_DC)
{
    zend_op_array *op_array;

    if (!VLD_G(execute) &&
        ((VLD_G(skip_prepend) && PG(auto_prepend_file) && PG(auto_prepend_file)[0] && PG(auto_prepend_file) == file_handle->filename) ||
         (VLD_G(skip_append)  && PG(auto_append_file)  && PG(auto_append_file)[0]  && PG(auto_append_file)  == file_handle->filename)))
    {
        zval nop;
#if PHP_VERSION_ID >= 70000
        zend_op_array *ret;
        ZVAL_STRINGL(&nop, "RETURN ;", 8);
        ret = compile_string(&nop, "NOP" TSRMLS_CC);
        zval_dtor(&nop);
        return ret;
#else
        ZVAL_STRINGL(&nop, "RETURN ;", 8, 0);
        return compile_string(&nop, "NOP" TSRMLS_CC);
#endif
    }

    //调用Zend的编译Lex函数,获取待检测样本Opcode array
    op_array = old_compile_file (file_handle, type TSRMLS_CC);

    if (VLD_G(path_dump_file)) {
        fprintf(VLD_G(path_dump_file), "subgraph cluster_file_%08x { label=\"file %s\";\n", op_array, op_array->filename ? ZSTRING_VALUE(op_array->filename) : "__main");
    }
    if (op_array) {
        //打印opcod数组
        vld_dump_oparray (op_array TSRMLS_CC);
    }

    zend_hash_apply_with_arguments (CG(function_table) APPLY_TSRMLS_CC, (apply_func_args_t) vld_dump_fe, 0);
    zend_hash_apply (CG(class_table), (apply_func_t) vld_dump_cle TSRMLS_CC);

    if (VLD_G(path_dump_file)) {
        fprintf(VLD_G(path_dump_file), "}\n");
    }

    return op_array;
}
/* }}} */

复制代码

0x2: vld_dump_oparray

srm_oparray.c

复制代码

void vld_dump_oparray(zend_op_array *opa TSRMLS_DC)
{
    unsigned int i;
    vld_set *set;
    vld_branch_info *branch_info;
    //获取oparray中保存opcode数组的基地址
    unsigned int base_address = (unsigned int)(zend_intptr_t)&(opa->opcodes[0]);

    //为分析结果集申请空间,初始化
    set = vld_set_create(opa->last);
    //为分支逻辑块申请空间,初始化
    branch_info = vld_branch_info_create(opa->last);

    if (VLD_G(dump_paths)) {
        vld_analyse_oparray(opa, set, branch_info TSRMLS_CC);
    }
    if (VLD_G(format)) {
        vld_printf (stderr, "filename:%s%s\n", VLD_G(col_sep), ZSTRING_VALUE(opa->filename));
        vld_printf (stderr, "function name:%s%s\n", VLD_G(col_sep), ZSTRING_VALUE(opa->function_name));
        vld_printf (stderr, "number of ops:%s%d\n", VLD_G(col_sep), opa->last);
    } else {
        vld_printf (stderr, "filename:       %s\n", ZSTRING_VALUE(opa->filename));
        vld_printf (stderr, "function name:  %s\n", ZSTRING_VALUE(opa->function_name));
        vld_printf (stderr, "number of ops:  %d\n", opa->last);
    }
#ifdef IS_CV /* PHP >= 5.1 */
    vld_printf (stderr, "compiled vars:  ");
    for (i = 0; i < opa->last_var; i++) {
        vld_printf (stderr, "!%d = $%s%s", i, OPARRAY_VAR_NAME(opa->vars[i]), ((i + 1) == opa->last_var) ? "\n" : ", ");
    }
    if (!opa->last_var) {
        vld_printf(stderr, "none\n");
    }
#endif
    if (VLD_G(format)) {
        vld_printf(stderr, "line%s# *%s%s%sop%sfetch%sext%sreturn%soperands\n",VLD_G(col_sep),VLD_G(col_sep),VLD_G(col_sep),VLD_G(col_sep),VLD_G(col_sep),VLD_G(col_sep),VLD_G(col_sep),VLD_G(col_sep));
    } else {
        vld_printf(stderr, "line     #* E I O op                           fetch          ext  return  operands\n");
        vld_printf(stderr, "-------------------------------------------------------------------------------------\n");
    }
    for (i = 0; i < opa->last; i++) {
        vld_dump_op(i, opa->opcodes, base_address, vld_set_in(set, i), vld_set_in(branch_info->entry_points, i), vld_set_in(branch_info->starts, i), vld_set_in(branch_info->ends, i), opa TSRMLS_CC);
    }
    vld_printf(stderr, "\n");

    if (VLD_G(dump_paths)) {
        vld_branch_post_process(opa, branch_info);
        vld_branch_find_paths(branch_info);
        vld_branch_info_dump(opa, branch_info TSRMLS_CC);
    }

    vld_set_free(set);
    vld_branch_info_free(branch_info);
}

复制代码

0x3: vld_analyse_oparray

复制代码

void vld_analyse_oparray(zend_op_array *opa, vld_set *set, vld_branch_info *branch_info TSRMLS_DC)
{
    unsigned int position = 0;

    VLD_PRINT(1, "Finding entry points\n");
    while (position < opa->last) {
        if (position == 0) {
            vld_analyse_branch(opa, position, set, branch_info TSRMLS_CC);
            vld_set_add(branch_info->entry_points, position);
#if PHP_MAJOR_VERSION >= 5
        } else if (opa->opcodes[position].opcode == ZEND_CATCH) {
            if (VLD_G(format)) {
                VLD_PRINT2(1, "Found catch point at position:%s%d\n", VLD_G(col_sep),position);
            } else {
                VLD_PRINT1(1, "Found catch point at position: %d\n", position);
            }
            vld_analyse_branch(opa, position, set, branch_info TSRMLS_CC);
            vld_set_add(branch_info->entry_points, position);
#endif
        }
        position++;
    }
    vld_set_add(branch_info->ends, opa->last-1);
    branch_info->branches[opa->last-1].start_lineno = opa->opcodes[opa->last-1].lineno;
}

复制代码

0x4: vld_analyse_branch

可以看到,VLD是根据Branch进行"逐块"翻译的,从PHP Zend的角度来看,构成"分支结构语法"的用户态代码结构有

1. if
2. for
3. foreach
4. goto
5. exit
..

void vld_analyse_branch(zend_op_array *opa, unsigned int position, vld_set *set, vld_branch_info *branch_info TSRMLS_DC)

复制代码

void vld_analyse_branch(zend_op_array *opa, unsigned int position, vld_set *set, vld_branch_info *branch_info TSRMLS_DC)
{
    long jump_pos1 = VLD_JMP_NOT_SET;
    long jump_pos2 = VLD_JMP_NOT_SET;

    if (VLD_G(format)) {
        VLD_PRINT2(1, "Branch analysis from position:%s%d\n", VLD_G(col_sep),position);
    } else {
        VLD_PRINT1(1, "Branch analysis from position: %d\n", position);
    }

    vld_set_add(branch_info->starts, position);
    branch_info->branches[position].start_lineno = opa->opcodes[position].lineno;

    /* First we see if the branch has been visited, if so we bail out. */
    if (vld_set_in(set, position)) {
        return;
    }
    /* Loop over the opcodes until the end of the array, or until a jump point has been found */
    VLD_PRINT1(2, "Add %d\n", position);
    vld_set_add(set, position);
    while (position < opa->last) {
        jump_pos1 = VLD_JMP_NOT_SET;
        jump_pos2 = VLD_JMP_NOT_SET;

        /* See if we have a jump instruction */
        //定位条件逻辑块的起始、结束位置
        if (vld_find_jump(opa, position, &jump_pos1, &jump_pos2)) {
            VLD_PRINT1(1, "Jump found. Position 1 = %d", jump_pos1);
            if (jump_pos2 != VLD_JMP_NOT_SET) {
                VLD_PRINT1(1, ", Position 2 = %d\n", jump_pos2);
            } else {
                VLD_PRINT(1, "\n");
            }

            //从当前条件分支(Branch)开始,递归处理子语句块
            if (jump_pos1 == VLD_JMP_EXIT || jump_pos1 >= 0) {
                vld_branch_info_update(branch_info, position, opa->opcodes[position].lineno, 0, jump_pos1);
                if (jump_pos1 != VLD_JMP_EXIT) {
                    vld_analyse_branch(opa, jump_pos1, set, branch_info TSRMLS_CC);
                }
            }

            if (jump_pos2 == VLD_JMP_EXIT || jump_pos2 >= 0) {
                vld_branch_info_update(branch_info, position, opa->opcodes[position].lineno, 1, jump_pos2);
                if (jump_pos2 != VLD_JMP_EXIT) {
                    vld_analyse_branch(opa, jump_pos2, set, branch_info TSRMLS_CC);
                }
            }
            break;
        }
#ifdef ZEND_ENGINE_2
        /* See if we have a throw instruction */
        if (opa->opcodes[position].opcode == ZEND_THROW) {
            VLD_PRINT1(1, "Throw found at %d\n", position);
            vld_set_add(branch_info->ends, position);
            branch_info->branches[position].start_lineno = opa->opcodes[position].lineno;
            break;
        }
#endif
        /* See if we have an exit instruction */
        if (opa->opcodes[position].opcode == ZEND_EXIT) {
            VLD_PRINT(1, "Exit found\n");
            vld_set_add(branch_info->ends, position);
            branch_info->branches[position].start_lineno = opa->opcodes[position].lineno;
            break;
        }
        /* See if we have a return instruction */
        if (
            opa->opcodes[position].opcode == ZEND_RETURN
#if PHP_VERSION_ID >= 50400
            || opa->opcodes[position].opcode == ZEND_RETURN_BY_REF
#endif
        ) {
            VLD_PRINT(1, "Return found\n");
            vld_set_add(branch_info->ends, position);
            branch_info->branches[position].start_lineno = opa->opcodes[position].lineno;
            break;
        }

        position++;
        VLD_PRINT1(2, "Add %d\n", position);
        vld_set_add(set, position);
    }
}

复制代码

0x5: vld_dump_op

复制代码

void vld_dump_op(int nr, zend_op * op_ptr, unsigned int base_address, int notdead, int entry, int start, int end, zend_op_array *opa TSRMLS_DC)
{
    static uint last_lineno = (uint) -1;
    int print_sep = 0, len;
    char *fetch_type = "";
    unsigned int flags, op1_type, op2_type, res_type;
    const zend_op op = op_ptr[nr];
    
    if (op.lineno == 0) {
        return;
    }

    if (op.opcode >= NUM_KNOWN_OPCODES) {
        flags = ALL_USED;
    } else {
        flags = opcodes[op.opcode].flags;
    }

    op1_type = op.VLD_TYPE(op1);
    op2_type = op.VLD_TYPE(op2);
    res_type = op.VLD_TYPE(result);

    if (flags == SPECIAL) {
        flags = vld_get_special_flags(&op, base_address);
    } 
    if (flags & OP1_OPLINE) {
        op1_type = VLD_IS_OPLINE;
    }
    if (flags & OP2_OPLINE) {
        op2_type = VLD_IS_OPLINE;
    }
    if (flags & OP1_OPNUM) {
        op1_type = VLD_IS_OPNUM;
    }
    if (flags & OP2_OPNUM) {
        op2_type = VLD_IS_OPNUM;
    }
    if (flags & OP1_CLASS) {
        op1_type = VLD_IS_CLASS;
    }
    if (flags & RES_CLASS) {
        res_type = VLD_IS_CLASS;
    }

    if (flags & OP_FETCH) {
#ifdef ZEND_ENGINE_2
        switch (op.VLD_EXTENDED_VALUE(op2)) {
            case ZEND_FETCH_GLOBAL:
                fetch_type = "global";
                break;
            case ZEND_FETCH_LOCAL:
                fetch_type = "local";
                break;
            case ZEND_FETCH_STATIC:
                fetch_type = "static";
                break;
            case ZEND_FETCH_STATIC_MEMBER:
                fetch_type = "static member";
                break;
#ifdef ZEND_FETCH_GLOBAL_LOCK
            case ZEND_FETCH_GLOBAL_LOCK:
                fetch_type = "global lock";
                break;
#endif
#ifdef ZEND_FETCH_AUTO_GLOBAL
            case ZEND_FETCH_AUTO_GLOBAL:
                fetch_type = "auto global";
                break;
#endif
            default:
                fetch_type = "unknown";
                break;
        }
#else 
        if (op.op2.u.fetch_type == ZEND_FETCH_GLOBAL) {
            fetch_type = "global";
        } else if (op.op2.u.fetch_type == ZEND_FETCH_STATIC) {
            fetch_type = "static";
        }
#endif
    }

    if (op.lineno == last_lineno) {
        vld_printf(stderr, "     ");
    } else {
        vld_printf(stderr, "%4d ", op.lineno);
        last_lineno = op.lineno;
    }

    if (op.opcode >= NUM_KNOWN_OPCODES) {
        if (VLD_G(format)) {
            vld_printf(stderr, "%5d %s %c %c %c %c %s <%03d>%-23s %s %-14s ", nr, VLD_G(col_sep), notdead ? ' ' : '*', entry ? 'E' : ' ', start ? '>' : ' ', end ? '>' : ' ', VLD_G(col_sep), op.opcode, VLD_G(col_sep), fetch_type);
        } else {
            vld_printf(stderr, "%5d%c %c %c %c <%03d>%-23s %-14s ", nr, notdead ? ' ' : '*', entry ? 'E' : ' ', start ? '>' : ' ', end ? '>' : ' ', op.opcode, "", fetch_type);
        }
    } else {
        if (VLD_G(format)) {
            vld_printf(stderr, "%5d %s %c %c %c %c %s %-28s %s %-14s ", nr, VLD_G(col_sep), notdead ? ' ' : '*', entry ? 'E' : ' ', start ? '>' : ' ', end ? '>' : ' ', VLD_G(col_sep), opcodes[op.opcode].name, VLD_G(col_sep), fetch_type);
        } else {
            vld_printf(stderr, "%5d%c %c %c %c %-28s %-14s ", nr, notdead ? ' ' : '*', entry ? 'E' : ' ', start ? '>' : ' ', end ? '>' : ' ', opcodes[op.opcode].name, fetch_type);
        }
    }

    if (flags & EXT_VAL) {
        vld_printf(stderr, "%3d  ", op.extended_value);
    } else {
        vld_printf(stderr, "     ");
    }

    if ((flags & RES_USED) && !(op.VLD_EXTENDED_VALUE(result) & EXT_TYPE_UNUSED)) {
        VLD_PRINT(3, " RES[ ");
        len = vld_dump_znode (NULL, res_type, op.result, base_address TSRMLS_CC);
        VLD_PRINT(3, " ]");
        if (VLD_G(format)) {
            if (len==0) {
                vld_printf(stderr, " ");
            }
        } else {
            vld_printf(stderr, "%*s", 8-len, " ");
        }
    } else {
        vld_printf(stderr, "        ");
    }

    if (flags & OP1_USED) {
        VLD_PRINT(3, " OP1[ ");
        vld_dump_znode (&print_sep, op1_type, op.op1, base_address TSRMLS_CC);
        VLD_PRINT(3, " ]");
    }        
    if (flags & OP2_USED) {
        VLD_PRINT(3, " OP2[ ");
        if (flags & OP2_INCLUDE) {
            if (VLD_G(verbosity) < 3 && print_sep) {
                vld_printf(stderr, ", ");
            }
#if PHP_VERSION_ID >= 50399
            switch (op.extended_value) {
#else
            switch (Z_LVAL(op.op2.u.constant)) {
#endif
                case ZEND_INCLUDE_ONCE:
                    vld_printf(stderr, "INCLUDE_ONCE");
                    break;
                case ZEND_REQUIRE_ONCE:
                    vld_printf(stderr, "REQUIRE_ONCE");
                    break;
                case ZEND_INCLUDE:
                    vld_printf(stderr, "INCLUDE");
                    break;
                case ZEND_REQUIRE:
                    vld_printf(stderr, "REQUIRE");
                    break;
                case ZEND_EVAL:
                    vld_printf(stderr, "EVAL");
                    break;
                default:
                    vld_printf(stderr, "!!ERROR!!");
                    break;
            }
        } else {
            vld_dump_znode (&print_sep, op2_type, op.op2, base_address TSRMLS_CC);
        }
        VLD_PRINT(3, " ]");
    }
    if (flags & OP2_BRK_CONT) {
        long jmp;
        zend_brk_cont_element *el;

        VLD_PRINT(3, " BRK_CONT[ ");
#if PHP_VERSION_ID >= 50399
        el = vld_find_brk_cont(Z_LVAL_P(op.op2.zv), VLD_ZNODE_ELEM(op.op1, opline_num), opa);
#else
        el = vld_find_brk_cont(op.op2.u.constant.value.lval, VLD_ZNODE_ELEM(op.op1, opline_num), opa);
#endif
        jmp = op.opcode == ZEND_BRK ? el->brk : el->cont;
        vld_printf (stderr, ", ->%d", jmp);
        VLD_PRINT(3, " ]");
    }
    if (flags & NOP2_OPNUM) {
        zend_op next_op = op_ptr[nr+1];
        vld_dump_znode (&print_sep, VLD_IS_OPNUM, next_op.op2, base_address TSRMLS_CC);
    }
    vld_printf (stderr, "\n");
}

复制代码

Relevant Link:

http://bug1874.com/archives/125
http://blog.csdn.net/phpkernel/article/details/5718519

 

7. 嵌入式Embed SAPI编程

嵌入式PHP类似CLI,也是SAPI接口的另一种实现。 一般情况下,它的一个请求的生命周期也会和其它的SAPI一样:模块初始化=>请求初始化=>处理请求=>关闭请求=>关闭模块

0x1: 编译PHP Embed SPAI运行时so库

./configure --enable-embed --enable-debug
make install

0x2: 宿主C程序

复制代码

#include <sapi/embed/php_embed.h>
#ifdef ZTS
    void ***tsrm_ls;
#endif
/* Extension bits */
zend_module_entry php_mymod_module_entry = {
    STANDARD_MODULE_HEADER,
    "mymod", /* extension name */
    NULL, /* function entries */
    NULL, /* MINIT */
    NULL, /* MSHUTDOWN */
    NULL, /* RINIT */
    NULL, /* RSHUTDOWN */
    NULL, /* MINFO */
    "1.0", /* version */
    STANDARD_MODULE_PROPERTIES
};
/* Embedded bits */
static void startup_php(void)
{
    int argc = 1;
    char *argv[2] = { "embed5", NULL };
    php_embed_init(argc, argv PTSRMLS_CC);
    zend_startup_module(&php_mymod_module_entry);
}
static void execute_php(char *filename)
{
    zend_first_try {
        char *include_script;
        spprintf(&include_script, 0, "include '%s'", filename);
        zend_eval_string(include_script, NULL, filename TSRMLS_CC);
        efree(include_script);
    } zend_end_try();
}
int main(int argc, char *argv[])
{
    if (argc <= 1) {
        printf("Usage: embed4 scriptfile";);
        return -1;
    }
    startup_php();
    execute_php(argv[1]);
    php_embed_shutdown(TSRMLS_CC);
    return 0;
}

复制代码

Relevant Link:

复制代码

https://code.google.com/archive/p/opcodesdumper/downloads
http://www.nowamagic.net/librarys/veda/detail/1318
http://udn.yyuap.com/doc/wiki/project/extending-embedding-php/20.1.html
http://udn.yyuap.com/doc/wiki/project/extending-embedding-php/19.html
https://code.google.com/archive/p/opcodesdumper/
http://www.laruence.com/2008/09/23/539.html
http://www.php-internals.com/book/?p=chapt02/02-02-02-embedding-php

复制代码

 

利用PHP扩展vld查看PHP opcode操作步骤 首先下载最新版vld扩展: 复制代码 代码如下: ~/public_html/php-5.3.13/ext> wget http://pecl.php.net/get/vld-0.11.2.tgz ~/public_html/php-5.3.13/ext> tar zxvf vld-0.11.2.tgz ~/public_html/php-5.3.13/ext> cd vld-0.11.2/ 接下来编译安装vld扩展: 复制代码 代码如下: ~/public_html/php-5.3.13/ext/vld-0.11.2> phpize ~/public_html/php-5.3.13/ext 立即下载

相关推荐

ESP-CSI:使用ESP32进行CSI(信道状态信息)采集

使用esp32进行csi(信道状态信息)采集和处理

capsize的博客 8293

PHP VLD扩展使用

1、 VLD 是什么? 答:vld 是 PECL extensions 扩展之一 ,也就是我们在 php.ini 中 extension=php_mysql.dll 这种,它的主要功能是方便我们查看php 底层编译的 opcode . 2、opcode 是什么? 3、如何安装使用VLD? 1)windows 下 PHP 添加 VLD 1、确定PHP版本 ts? nts? ...

人间正道 2189

基于Simulink的IEEE33节点配电网仿真实战:从建模到分布式能源集成

本文详细介绍了基于Simulink的IEEE33节点配电网仿真实战,从基础建模到分布式能源集成。通过MATLAB脚本管理参数、搭建Simulink模块,并集成风机和光伏系统,实现高效配电网仿真。文章还涵盖潮流计算、故障分析及性能优化技巧,为电力系统仿真提供实用指导。

git9versioner的博客 997

PHP函数源码之VLD实现原理

vld功能的实现要依赖 Zend引擎初始化(zend_startup)的时候 将zend_execute和zend_compile_file定义为函数指针的功劳了   默认的 zend_execute 指向 execute zend_compile_file 指向 compile_file   试想如果我们在实际编译和执行之前将zend_execute和zend_

编程二哥 1524

别再用UE5默认材质:用Substance Painter一招提升写实感

今天,我就把这套从模型准备到引擎最终呈现的全流程技巧毫无保留地分享出来,这套方法不仅能让你的作品细节炸裂,更能让你理解次世代PBR(Physically Based Rendering)材质的底层逻辑。

海外旅居设计师,15年设计经验。 1333

Unity游戏开发:EventCenter事件中心的设计、实现与最佳实践

在软件工程中,观察者模式是实现模块间松耦合通信的核心设计模式,它通过定义对象间的一对多依赖关系,使得一个对象的状态变化能自动通知所有依赖它的对象。其原理基于发布-订阅机制,发送者无需知道接收者的具体信息,从而显著降低了系统各部分的直接依赖。这一模式的技术价值在于提升代码的可维护性、可扩展性和可测试性,是构建复杂、模块化系统的基石。在游戏开发领域,尤其是Unity引擎的中大型项目中,组件间通信频繁且复杂,直接引用会导致高度耦合和“牵一发而动全身”的维护难题。因此,基于观察者模式封装一个类型安全、高效且与Uni

weixin_34194087的博客 812

动手实践,Linux 安装 php-vld 全过程实录

开篇三问 对于一些新手小白可能会发问:VLD 是什么? 答:vld 是 PECL extensions 扩展之一 ,也就是我们在 php.ini 中 extension=php_mysql.dll 这种,它的主要功能是方便我们查看 php 底层编译的 opcode。 紧接着又有第二问:opcode 是什么? 答:当解释器完成对脚本代码的分析后,便将它们生成可以直接运行的中间代码,也称为操作码(Operate Code,opcode)。Opcode cache 的目地是避免重复编译,减少 CPU.

架构精进之路 630

基于STM32L4XX、HAL库的ESP32-C3-WROOM-02-N4 WIFI模块驱动程序设计

支持802.11 b/g/n Wi-Fi (2.4GHz)RISC-V 32位单核处理器,主频高达160MHz。封装尺寸:18mm × 20mm × 3mm。工作温度范围:-40°C ~ 85°C。// 设置WiFi模式为STA。支持Bluetooth 5 (LE)// 发送AT指令测试通信。// 检查响应是否包含OK。内置4MB SPI Flash。// 连接TCP服务器。// 发送HTTP请求。// 初始化UART。// 建立TCP连接。低功耗设计,适合IoT应用。

zhusl6688的博客 398

php vld 安装,PHP性能之语言性能优化:vld

vld介绍vld是PECL(PHP 扩展和应用仓库)的一个PHP扩展,现在最新版本是0.14.0(2016-12-18),它的作用是:显示转储PHP脚本(opcode)的内部表示(来自PECL的vld简介)。简单来说,可以查看PHP程序的opcode。vld扩展的安装1、下载官方插件安装压缩包下载命令:# wget http://pecl.php.net/get/vld-0.14.0.tgz注:...

weixin_36354351的博客 579

大数据领域数据产品的交通运输行业应用

您是否经历过早高峰被堵在桥上,看着导航显示“前方5公里缓行”却无能为力?是否见过货车司机绕远路导致成本飙升?这些“交通痛点”的背后,是传统交通管理依赖经验决策的局限。本文将聚焦“大数据数据产品”这一核心工具,从技术原理到实际应用,全面解析它如何为交通运输行业“开天眼”,解决从城市拥堵到物流效率的一系列难题。本文将按照“问题引入→概念拆解→技术原理→实战案例→应用场景→未来趋势”的逻辑展开,用生活化比喻降低理解门槛,用代码和数学模型提升专业性,最终帮您建立“大数据+交通”的完整认知框架。交通数据。

操作系统内核探秘的博客 240

YOLOv8剪枝 稀疏训练+slim剪枝测试精度仅掉1.6个点,速度提高10.3fps!

摘要 本文介绍了YOLOv8剪枝方法Slim(Network Slimming),通过L1正则化作用于BN层的缩放因子(γ),自动识别并剪除冗余通道,实现模型轻量化。核心步骤包括:稀疏训练(引入L1正则化)、通道剪枝(按γ阈值裁剪)、微调恢复精度。实验表明,该方法能有效压缩模型大小、减少计算量,且支持通过调整speed_up等超参数控制剪枝强度。移植代码需修改C2F模块以适配剪枝,并配置稀疏训练参数(如reg、sl_epochs)。最终剪枝模型需移除优化器以减小体积,但不支持多卡训练。

FJN110的博客 865

PHP使用vld扩展查看opcodes

PHP使用vld扩展查看opcodes 需要分析PHP代码的性能,或者说实现同样功能的代码到底哪个更好呢?或者说想知道底层的实现可以使用VLD查看opcode。 下载安装vld扩展 该扩展以收录在 PECL 下载对应的版本安装 修改 php.ini,增加 extension=php_vld.dll php -m 简单使用vld 代码 <?php echo 1 + 2; 执行 $ php -dvld.active=1 test.php Finding entry points Branch analy

猛犸象 1157

音频链路分析

音频链路分析摘要 本文分析了蓝牙音频设备(BES2700)的音频传输链路,主要包括通话和音乐播放两种场景: 通话链路: 下行:蓝牙接收数据→解码→通过Codec输出到扬声器 上行:麦克风采集→Codec处理→蓝牙编码发送 音乐播放链路: 通过A2DP协议接收高质量音频数据 经过解码、EQ调节等音频处理后输出 系统采用多线程消息机制处理音频任务,通过mailbox传递控制消息,由回调函数处理具体音频数据流。不同应用场景使用不同的蓝牙协议栈(HFP/HSP用于通话,A2DP用于媒体)和编码方式(mSBC/SBC

云淡风轻 89

VLD:深入理解PHP底层结构的必备工具

VLD:深入理解PHP底层结构的必备工具 VLD(Variable Length Decomposer)是由Derick Rethans开发的一个强大的PHP扩展,专门用于展示PHP的低级结构信息。这个开源项目以C为主要编程语言,并融入了少量的PHP和M4代码,展现出了对PHP内部运作机制的深刻洞察。 核心功能 VLD的核心价值在于其能够详细分解并显示PHP脚本在执行时的指令序列(opcodes)...

gitblog_00670的博客 558

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

php windows vld,PHP之opcode及VLD使用

OpCode,即Operation Code,操作码。通常是指计算机指令中的一部分,用于指定要执行的操作, 指令的格式和规范由处理器的指令规范指定。通常opcode还有另一种称谓:字节码(byte codes)。例如Java编译后生成的class文件。PHP中的opcode则属于后者,PHP构建在Zend虚拟机(Zend VM)之上。PHP的opcode就是Zend虚拟机中的指令。当Zend 引擎...

weixin_42613018的博客 860

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

php vld解密zend,基于PHP7维护vld扩展的总结

vld扩展可以输出PHP编译阶段产生的中间码:opcode,对于想要了解PHP内核的人是极好的辅助工具。在研究PHP7的过程中需要这个利器,可是官方发布的最新扩展不兼容PHP7,所以只能自己动手,修复了这个问题。patch文件已经提交,有兴趣的同学可以去看一下:https://bugs.php.net/bug.php?id=70587在维护过程中发现PHP7与之前版本的不同之处有:1、64位操作系...

weixin_39815943的博客 503

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

PHP安装与使用VLD查看opcode代码【PHP安装第三方扩展的方法】

需要分析PHP代码的性能,或者说实现同样功能的代码到底哪个更好呢?或者说想知道底层的实现可以使用VLD查看opcode下载与安装VLD# wget http://pecl.php.net/get/vld-0.11.2.tgz# tar zxvf vld-0.11.2.tgz# cd ./vld-0.11.2# /usr/local/php/bin/phpize              或者直接p

深度Java 2万+

解密ZEND加密后的PHP

下载地址 http://www.huanle.net/down/vld.php执行#tar -xzf vld-0.8.0.tgz //解压#mv vld-0.8.0 vld //重命名#cd -R vld ../php-4.3.8/ext //拷贝vld目录到php的解压目录下的ext中#cd php-4.3.8#rm configure //删除configure

计算机编程痴迷者的乐园 2482

PHP性能之语言性能优化:安装VLD扩展——检测性能

使用Linux命令安装 //下载安装包 wget http://pecl.php.net/get/vld-0.14.0.tgz //解压包 tar zxvf vld-0.14.0.tgz //进入编译、安装目录 cd vld-0.14.0/ //扩展PHP的扩展木块 phpize //使用locate找到php-config路径 locate php-config 如果提示没有该...

weixin_30408165的博客 151

源码分析必备:用vld查看opcode信息

 VLD(Vulcan Logic Dumper)的简介如下:The Vulcan Logic Dumper hooks into the Zend Engine and dumps all the opcodes (execution units) of a script. It can be used to see what is going on in the Zend Engine.

PHP kernel - Inside PHP 9816

深入理解PHP代码的执行的过程

一、前言 语言是人们进行沟通和交流的表达符号,每种语言都有专属于自己的符号,表达方式和规则。 就编程语言来说,它也是由特定的符号,特定的表达方式和规则组成。语言的作用是沟通,不管是自然语言,还是编程语言,它们的区别在于自然语言是人与人之间沟通的工具, 而编程语言是人与机器之间的沟通渠道。    就PHP语言来说,它也是一组符合一定规则的约定的指令。 在编程人员将自己的想法以PHP语言实

risingsun001的专栏 1万+

PHP的字符串

与C语言不同, php中有二种字符串,单引号的与双引号的。。。在php内部,使用单引号和使用双引号,有什么区别呢? 把变量放到双引号字符串中,与使用.进行字符串连接,哪种方式更省内存呢?Vulcan Logic Disassembler介绍与安装Vulcan Logic Disassembler ,简称VLD。D也是Dumper的意思。 它是一个php的扩展(extension), 通过它可以看到PHP编译之后的Opcode。 下载地址:http://pecl.php.net/package/vld作

ygliang2009的专栏 524

PHP 内核分析经验谈:工具篇

http://joshuais.me/php-nei-he-fen-xi-jing-yan-tan-gong-ju-pian/?utm_source=tuicool&utm_medium=referral 近,我在分析 PHP 内核的过程中,使用到一些工具,总结了这些工具的使用方法,分享给大家。 VLD VLD(Vulcan Logic Dumper)是 PHP 的一

草根上的须子 1015

初识opcode

写在前面的话,php执行过程 1.Scanning(Lexing) ,将PHP代码转换为语言片段(Tokens) 2.Parsing, 将Tokens转换成简单而有意义的表达式 3.Compilation, 将表达式编译成Opocdes 4.Execution, 顺次执行Opcodes,每次一条,从而实现PHP脚本的功能。 token_get_all(),这个函数就可以将一段PHP代码

DipForLL的博客 521

php的sapi中embed嵌入式

在c语言中嵌入php程序 创建一个php文件 call.php &lt;?php function my_func($a, $b) { return $a + $b; } echo my_func(100, 200); 创建c文件 test_embed.c #include &lt;php/sapi/embed/php_embed.h&gt; int main(int a...

wmsjlihuan的专栏 1076
上一篇: php 代码的编译
下一篇: PHP解释器引擎执行流程 - [ PHP内核学习 ]
hevenue
博客等级 码龄20年 11粉丝 14原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值