深入理解PHP原理之扩展载入过程

why xdebug extension must be loaded as a zend extension?

what is zend extension and what are the differents between regular php extension and zend extension?

let’s start from that the extension loading process.

PHP是可以被扩展的, PHP的核心引擎Zend Engine也是可以被扩展的, 如果你也对Apache Module的编写也有所了解的话, 那么, 你就会对如下的结构很熟悉了:

struct _zend_extension {
        char *name;
        char *version;
        char *author;
        char *URL;
        char *copyright;
        startup_func_t startup;
        shutdown_func_t shutdown;
        activate_func_t activate;
        deactivate_func_t deactivate;
        message_handler_func_t message_handler;
        op_array_handler_func_t op_array_handler;
        statement_handler_func_t statement_handler;
        fcall_begin_handler_func_t fcall_begin_handler;
        fcall_end_handler_func_t fcall_end_handler;
        op_array_ctor_func_t op_array_ctor;
        op_array_dtor_func_t op_array_dtor;
        int (*api_no_check)(int api_no);
        void *reserved2;
        void *reserved3;
        void *reserved4;
        void *reserved5;
        void *reserved6;
        void *reserved7;
        void *reserved8;
        DL_HANDLE handle;
        int resource_number;
};

然后, 让我们对比下PHP extension的module entry:

struct _zend_module_entry {
          unsigned short size;
          unsigned int zend_api;
          unsigned char zend_debug;
          unsigned char zts;
          struct _zend_ini_entry *ini_entry;
          struct _zend_module_dep *deps;
          char *name;
          struct _zend_function_entry *functions;
          int (*module_startup_func)(INIT_FUNC_ARGS);
          int (*module_shutdown_func)(SHUTDOWN_FUNC_ARGS);
          int (*request_startup_func)(INIT_FUNC_ARGS);
          int (*request_shutdown_func)(SHUTDOWN_FUNC_ARGS);
          void (*info_func)(ZEND_MODULE_INFO_FUNC_ARGS);
          char *version;
          size_t globals_size;
     #ifdef ZTS
          ts_rsrc_id* globals_id_ptr;
     #else
          void* globals_ptr;
     #endif
          void (*globals_ctor)(void *global TSRMLS_DC);
          void (*globals_dtor)(void *global TSRMLS_DC);
          int (*post_deactivate_func)(void);
          int module_started;
          unsigned char type;
          void *handle;
          int module_number;
     };

上面的结构, 可以结合我之前的文章用C/C++扩展你的PHP来帮助理解.

恩,回到主题:既然Xdebug要以Zend Extension方式加载, 那么它必然有基于Zend Extension的需求, 会是什么呢?

恩, 我们知道Xdebug有profile PHP的功能, 对, 就是statement_handler:
the statement handler callback inserts an additional opcode at the end of every statement in a script in which the callback is called. One of the primary uses for this sort of callback is to implement per-line profiling, stepping debuggers, or code-coverage utilities.

并且,因为Xdebug也提供了给用户脚本使用的函数, 所以, 它也会有部分PHP extension的实现, 并且由于它要以ZendExt方式载入的原因,所以它必须自己实现本身PHPExt部分的载入过程.

最后, 将PHP Extension的载入过程罗列如下(我会慢慢加上注释), 当然, 如果你等不及想知道, 也欢迎你直接在我的博客风雪之隅留言探讨.

以apache/mod_php5.c为例

1. 在mod_php5.c中,定义了Apache模块结构:

module MODULE_VAR_EXPORT php5_module =
     {
          STANDARD_MODULE_STUFF,
          php_init_handler,           /* initializer */
          php_create_dir,             /* per-directory config creator */
          php_merge_dir,              /* dir merger */
          NULL,                       /* per-server config creator */
          NULL,                       /* merge server config */
          php_commands,               /* command table */
          php_handlers,               /* handlers */
          NULL,                       /* filename translation */
          NULL,                       /* check_user_id */
          NULL,                       /* check auth */
          NULL,                       /* check access */
          NULL,                       /* type_checker */
          NULL,                       /* fixups */
          NULL                        /* logger */
     #if MODULE_MAGIC_NUMBER >= 19970103
          , NULL                      /* header parser */
     #endif
     #if MODULE_MAGIC_NUMBER >= 19970719
          , NULL                      /* child_init */
     #endif
     #if MODULE_MAGIC_NUMBER >= 19970728
          , php_child_exit_handler        /* child_exit */
     #endif
     #if MODULE_MAGIC_NUMBER >= 19970902
          , NULL                      /* post read-request */
     #endif
     };
/* }}} */

可见, 最开始被调用的将会是php_init_handler,

static void php_init_handler(server_rec *s, pool *p)
{
    register_cleanup(p, NULL, (void (*)(void *))apache_php_module_shutdown_wrapper, (void (*)(void *))php_module_shutdown_for_exec);
    if (!apache_php_initialized) {
        apache_php_initialized = 1;
#ifdef ZTS
        tsrm_startup(1, 1, 0, NULL);
#endif
        sapi_startup(&apache_sapi_module);
        php_apache_startup(&apache_sapi_module);
    }
#if MODULE_MAGIC_NUMBER >= 19980527
    {
        TSRMLS_FETCH();
        if (PG(expose_php)) {
            ap_add_version_component("PHP/" PHP_VERSION);
        }
    }
#endif
}

这里, 调用了sapi_startup, 这部分是初始化php的apache sapi,
然后是调用,php_apache_startup:

static int php_apache_startup(sapi_module_struct *sapi_module)
{
    if (php_module_startup(sapi_module, &apache_module_entry, 1) == FAILURE) {
        return FAILURE;
    } else {
        return SUCCESS;
    }
}

这个时候,调用了php_module_startup, 其中有:

/* this will read in php.ini, set up the configuration parameters,
       load zend extensions and register php function extensions
       to be loaded later */
    if (php_init_config(TSRMLS_C) == FAILURE) {
        return FAILURE;
    }

调用了php_init_config, 这部分读取所有的php.ini和关联的ini文件, 然后对于每一条配置指令调用:

....
 if (sapi_module.ini_entries) {
        zend_parse_ini_string(sapi_module.ini_entries, 1, php_config_ini_parser_cb, &extension_lists);
    }
然后在php_config_ini_parser_cb中:
               if (!strcasecmp(Z_STRVAL_P(arg1), "extension")) { /* load function module */
                    zval copy;

                    copy = *arg2;
                    zval_copy_ctor(&copy);
                    copy.refcount = 0;
                    zend_llist_add_element(&extension_lists.functions, &copy);
                } else if (!strcasecmp(Z_STRVAL_P(arg1), ZEND_EXTENSION_TOKEN)) { /* load Zend extension */
                    char *extension_name = estrndup(Z_STRVAL_P(arg2), Z_STRLEN_P(arg2));

                    zend_llist_add_element(&extension_lists.engine, &extension_name);
                } else {
                    zend_hash_update(&configuration_hash, Z_STRVAL_P(arg1), Z_STRLEN_P(arg1) + 1, arg2, sizeof(zval), (void **) &entry);
                    Z_STRVAL_P(entry) = zend_strndup(Z_STRVAL_P(entry), Z_STRLEN_P(entry));
                }

这里记录下来所有要载入的php extension和zend extension,
然后, 让我们回到php_module_startup, 后面有调用到了
php_ini_register_extensions(TSRMLS_C);

void php_ini_register_extensions(TSRMLS_D)
{
    zend_llist_apply(&extension_lists.engine, php_load_zend_extension_cb TSRMLS_CC);
    zend_llist_apply(&extension_lists.functions, php_load_function_extension_cb TSRMLS_CC);

    zend_llist_destroy(&extension_lists.engine);
    zend_llist_destroy(&extension_lists.functions);
}

我们可以看到, 对于每一个扩展记录, 都调用了一个回叫函数, 我们这里只看php_load_function_extension_cb:

static void php_load_function_extension_cb(void *arg TSRMLS_DC)
{
    zval *extension = (zval *) arg;
    zval zval;

    php_dl(extension, MODULE_PERSISTENT, &zval, 0 TSRMLS_CC);
}

最后, 就是核心的载入逻辑了:

void php_dl(zval *file, int type, zval *return_value, int start_now TSRMLS_DC)
{
        void *handle;
        char *libpath;
        zend_module_entry *module_entry;
        zend_module_entry *(*get_module)(void);
        int error_type;
        char *extension_dir;

        if (type == MODULE_PERSISTENT) {
                extension_dir = INI_STR("extension_dir");
        } else {
                extension_dir = PG(extension_dir);
        }

        if (type == MODULE_TEMPORARY) {
                error_type = E_WARNING;
        } else {
                error_type = E_CORE_WARNING;
        }

        if (extension_dir && extension_dir[0]){
                int extension_dir_len = strlen(extension_dir);

                if (type == MODULE_TEMPORARY) {
                        if (strchr(Z_STRVAL_P(file), ‘/‘) != NULL || strchr(Z_STRVAL_P(file), DEFAULT_SLASH) != NULL) {
                                php_error_docref(NULL TSRMLS_CC, E_WARNING, "Temporary module name should contain only filename");
                                RETURN_FALSE;
                        }
                }

                if (IS_SLASH(extension_dir[extension_dir_len-1])) {
                        spprintf(&libpath, 0, "%s%s", extension_dir, Z_STRVAL_P(file));
                } else {
                        spprintf(&libpath, 0, "%s%c%s", extension_dir, DEFAULT_SLASH, Z_STRVAL_P(file));
                }
        } else {
                libpath = estrndup(Z_STRVAL_P(file), Z_STRLEN_P(file));
        }

        /* load dynamic symbol */
        handle = DL_LOAD(libpath);
        if (!handle) {
                php_error_docref(NULL TSRMLS_CC, error_type, "Unable to load dynamic library ‘%s‘ - %s", libpath, GET_DL_ERROR());
                GET_DL_ERROR(); /* free the buffer storing the error */
                efree(libpath);
                RETURN_FALSE;
        }

        efree(libpath);

        get_module = (zend_module_entry *(*)(void)) DL_FETCH_SYMBOL(handle, "get_module");

        /*
         * some OS prepend _ to symbol names while their dynamic linker
         * does not do that automatically. Thus we check manually for
         * _get_module.
         */

        if (!get_module)
                get_module = (zend_module_entry *(*)(void)) DL_FETCH_SYMBOL(handle, "_get_module");

        if (!get_module) {
                DL_UNLOAD(handle);
                php_error_docref(NULL TSRMLS_CC, error_type, "Invalid library (maybe not a PHP library) ‘%s‘ ", Z_STRVAL_P(file));
                RETURN_FALSE;
        }
        module_entry = get_module();
        if ((module_entry->zend_debug != ZEND_DEBUG) || (module_entry->zts != USING_ZTS)
                || (module_entry->zend_api != ZEND_MODULE_API_NO)) {
                /* Check for pre-4.1.0 module which has a slightly different module_entry structure :(  */
                        struct pre_4_1_0_module_entry {
                                  char *name;
                                  zend_function_entry *functions;
                                  int (*module_startup_func)(INIT_FUNC_ARGS);
                                  int (*module_shutdown_func)(SHUTDOWN_FUNC_ARGS);
                                  int (*request_startup_func)(INIT_FUNC_ARGS);
                                  int (*request_shutdown_func)(SHUTDOWN_FUNC_ARGS);
                                  void (*info_func)(ZEND_MODULE_INFO_FUNC_ARGS);
                                  int (*global_startup_func)(void);
                                  int (*global_shutdown_func)(void);
                                  int globals_id;
                                  int module_started;
                                  unsigned char type;
                                  void *handle;
                                  int module_number;
                                  unsigned char zend_debug;
                                  unsigned char zts;
                                  unsigned int zend_api;
                        };

                        char *name;
                        int zend_api;
                        unsigned char zend_debug, zts;

                        if ((((struct pre_4_1_0_module_entry *)module_entry)->zend_api > 20000000) &&
                                (((struct pre_4_1_0_module_entry *)module_entry)->zend_api < 20010901)
                        ) {
                                name       = ((struct pre_4_1_0_module_entry *)module_entry)->name;
                                zend_api   = ((struct pre_4_1_0_module_entry *)module_entry)->zend_api;
                                zend_debug = ((struct pre_4_1_0_module_entry *)module_entry)->zend_debug;
                                zts        = ((struct pre_4_1_0_module_entry *)module_entry)->zts;
                        } else {
                                name       = module_entry->name;
                                zend_api   = module_entry->zend_api;
                                zend_debug = module_entry->zend_debug;
                                zts        = module_entry->zts;
                        }

                        php_error_docref(NULL TSRMLS_CC, error_type,
                                          "%s: Unable to initialize module\n"
                                          "Module compiled with module API=%d, debug=%d, thread-safety=%d\n"
                                          "PHP    compiled with module API=%d, debug=%d, thread-safety=%d\n"
                                          "These options need to match\n",
                                          name, zend_api, zend_debug, zts,
                                          ZEND_MODULE_API_NO, ZEND_DEBUG, USING_ZTS);
                        DL_UNLOAD(handle);
                        RETURN_FALSE;
        }
        module_entry->type = type;
        module_entry->module_number = zend_next_free_module();
        module_entry->handle = handle;

        if ((module_entry = zend_register_module_ex(module_entry TSRMLS_CC)) == NULL) {
                DL_UNLOAD(handle);
                RETURN_FALSE;
        }

        if ((type == MODULE_TEMPORARY || start_now) && zend_startup_module_ex(module_entry TSRMLS_CC) == FAILURE) {
                DL_UNLOAD(handle);
                RETURN_FALSE;
        }

        if ((type == MODULE_TEMPORARY || start_now) && module_entry->request_startup_func) {
                if (module_entry->request_startup_func(type, module_entry->module_number TSRMLS_CC) == FAILURE) {
                        php_error_docref(NULL TSRMLS_CC, error_type, "Unable to initialize module ‘%s‘", module_entry->name);
                        DL_UNLOAD(handle);
                        RETURN_FALSE;
                }
        }
        RETURN_TRUE;
}
时间: 2024-07-31 05:07:48

深入理解PHP原理之扩展载入过程的相关文章

深入理解FFM原理与实践

原文:http://tech.meituan.com/deep-understanding-of-ffm-principles-and-practices.html 深入理解FFM原理与实践 del2z, 大龙 ·2016-03-03 09:00 FM和FFM模型是最近几年提出的模型,凭借其在数据量比较大并且特征稀疏的情况下,仍然能够得到优秀的性能和效果的特性,屡次在各大公司举办的CTR预估比赛中获得不错的战绩.美团点评技术团队在搭建DSP的过程中,探索并使用了FM和FFM模型进行CTR和CVR

jvm载入过程

类载入过程 类从被载入到虚拟机内存中開始,到卸载出内存为止,它的整个生命周期包含:载入.验证.准备.解析.初始化.使用和卸载七个阶段.它们開始的顺序例如以下图所看到的: 当中类载入的过程包含了载入.验证.准备.解析.初始化五个阶段.在这五个阶段中,载入.验证.准备和初始化这四个阶段发生的顺序是确定的.而解析阶段则不一定,它在某些情况下能够在初始化阶段之后開始,这是为了支持 Java 语言的执行时绑定(也成为动态绑定或晚期绑定). 另外注意这里的几个阶段是按顺序開始,而不是按顺序进行或完毕,由于这

《深入理解mybatis原理》 MyBatis的一级缓存实现详解 及使用注意事项

0.写在前面 MyBatis是一个简单,小巧但功能非常强大的ORM开源框架,它的功能强大也体现在它的缓存机制上.MyBatis提供了一级缓存.二级缓存 这两个缓存机制,能够很好地处理和维护缓存,以提高系统的性能.本文的目的则是向读者详细介绍MyBatis的一级缓存,深入源码,解析MyBatis一级缓存的实现原理,并且针对一级缓存的特点提出了在实际使用过程中应该注意的事项. 读完本文,你将会学到: 1.什么是一级缓存?为什么使用一级缓存? 2.MyBatis的一级缓存是怎样组织的?(即SqlSes

九爷带你了解 深入理解 Memcache 原理

深入理解Memcache原理 1.为什么要使用memcache 由于网站的高并发读写需求,传统的关系型数据库开始出现瓶颈,例如: 1)对数据库的高并发读写: 关系型数据库本身就是个庞然大物,处理过程非常耗时(如解析SQL语句,事务处理等).如果对关系型数据库进行高并发读写(每秒上万次的访问),那么它是无法承受的. 2)对海量数据的处理: 对于大型的SNS网站,每天有上千万次的苏剧产生(如twitter, 新浪微博).对于关系型数据库,如果在一个有上亿条数据的数据表种查找某条记录,效率将非常低.

《深入理解mybatis原理》 Mybatis数据源与连接池

对于ORM框架而言,数据源的组织是一个非常重要的一部分,这直接影响到框架的性能问题.本文将通过对MyBatis框架的数据源结构进行详尽的分析,并且深入解析MyBatis的连接池. 本文首先会讲述MyBatis的数据源的分类,然后会介绍数据源是如何加载和使用的.紧接着将分类介绍UNPOOLED.POOLED和JNDI类型的数据源组织:期间我们会重点讲解POOLED类型的数据源和其实现的连接池原理. 以下是本章的组织结构: 一.MyBatis数据源DataSource分类 二.数据源DataSour

iOS runtime探究(一): 从runtime开始理解面向对象的类到面向过程的结构体

你要知道的runtime都在这里 转载请注明出处 http://blog.csdn.net/u014205968/article/details/67639205 本文主要讲解runtime相关知识,从原理到实践,由于包含内容过多分为以下五篇文章详细讲解,可自行选择需要了解的方向: 从runtime开始: 理解面向对象的类到面向过程的结构体 从runtime开始: 深入理解OC消息转发机制 从runtime开始: 理解OC的属性property 从runtime开始: 实践Category添加属

Java Serializable(序列化)的理解和总结、具体实现过程(转)

原文地址:http://www.apkbus.com/forum.php?mod=viewthread&tid=13576&fromuid=3402 Java Serializable(序列化)的理解和总结.具体实现过程 内存中的对象是怎么存在的?  内存中各种对象的状态是?  实例变量是什么(指的是实例化的对象吗)?    使用序列化的好处是什么? 1.序列化是干什么的?        简单说就是为了保存在内存中的各种对象的状态(也就是实例变量,不是方法),并且可以把保存的对象状态再读出

《深入理解mybatis原理》 MyBatis的架构设计以及实例分析

MyBatis是目前非常流行的ORM框架,它的功能很强大,然而其实现却比较简单.优雅.本文主要讲述MyBatis的架构设计思路,并且讨论MyBatis的几个核心部件,然后结合一个select查询实例,深入代码,来探究MyBatis的实现. 一.MyBatis的框架设计        注:上图很大程度上参考了iteye 上的chenjc_it所写的博文原理分析之二:框架整体设计 中的MyBatis架构体图,chenjc_it总结的非常好,赞一个! 1.接口层---和数据库交互的方式 MyBatis

《深入理解mybatis原理》 Mybatis初始化机制详解

对于任何框架而言,在使用前都要进行一系列的初始化,MyBatis也不例外.本章将通过以下几点详细介绍MyBatis的初始化过程. 1.MyBatis的初始化做了什么 2. MyBatis基于XML配置文件创建Configuration对象的过程 3. 手动加载XML配置文件创建Configuration对象完成初始化,创建并使用SqlSessionFactory对象 4. 涉及到的设计模式 一. MyBatis的初始化做了什么 任何框架的初始化,无非是加载自己运行时所需要的配置信息.MyBati